diff options
Diffstat (limited to 'fs/pipe.c')
-rw-r--r-- | fs/pipe.c | 310 |
1 files changed, 171 insertions, 139 deletions
@@ -36,7 +36,7 @@ | |||
36 | */ | 36 | */ |
37 | 37 | ||
38 | /* Drop the inode semaphore and wait for a pipe event, atomically */ | 38 | /* Drop the inode semaphore and wait for a pipe event, atomically */ |
39 | void pipe_wait(struct inode * inode) | 39 | void pipe_wait(struct pipe_inode_info *pipe) |
40 | { | 40 | { |
41 | DEFINE_WAIT(wait); | 41 | DEFINE_WAIT(wait); |
42 | 42 | ||
@@ -44,11 +44,14 @@ void pipe_wait(struct inode * inode) | |||
44 | * Pipes are system-local resources, so sleeping on them | 44 | * Pipes are system-local resources, so sleeping on them |
45 | * is considered a noninteractive wait: | 45 | * is considered a noninteractive wait: |
46 | */ | 46 | */ |
47 | prepare_to_wait(PIPE_WAIT(*inode), &wait, TASK_INTERRUPTIBLE|TASK_NONINTERACTIVE); | 47 | prepare_to_wait(&pipe->wait, &wait, |
48 | mutex_unlock(PIPE_MUTEX(*inode)); | 48 | TASK_INTERRUPTIBLE | TASK_NONINTERACTIVE); |
49 | if (pipe->inode) | ||
50 | mutex_unlock(&pipe->inode->i_mutex); | ||
49 | schedule(); | 51 | schedule(); |
50 | finish_wait(PIPE_WAIT(*inode), &wait); | 52 | finish_wait(&pipe->wait, &wait); |
51 | mutex_lock(PIPE_MUTEX(*inode)); | 53 | if (pipe->inode) |
54 | mutex_lock(&pipe->inode->i_mutex); | ||
52 | } | 55 | } |
53 | 56 | ||
54 | static int | 57 | static int |
@@ -91,7 +94,8 @@ pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len) | |||
91 | return 0; | 94 | return 0; |
92 | } | 95 | } |
93 | 96 | ||
94 | static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buffer *buf) | 97 | static void anon_pipe_buf_release(struct pipe_inode_info *pipe, |
98 | struct pipe_buffer *buf) | ||
95 | { | 99 | { |
96 | struct page *page = buf->page; | 100 | struct page *page = buf->page; |
97 | 101 | ||
@@ -100,30 +104,27 @@ static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buff | |||
100 | /* | 104 | /* |
101 | * If nobody else uses this page, and we don't already have a | 105 | * If nobody else uses this page, and we don't already have a |
102 | * temporary page, let's keep track of it as a one-deep | 106 | * temporary page, let's keep track of it as a one-deep |
103 | * allocation cache | 107 | * allocation cache. (Otherwise just release our reference to it) |
104 | */ | 108 | */ |
105 | if (page_count(page) == 1 && !info->tmp_page) { | 109 | if (page_count(page) == 1 && !pipe->tmp_page) |
106 | info->tmp_page = page; | 110 | pipe->tmp_page = page; |
107 | return; | 111 | else |
108 | } | 112 | page_cache_release(page); |
109 | |||
110 | /* | ||
111 | * Otherwise just release our reference to it | ||
112 | */ | ||
113 | page_cache_release(page); | ||
114 | } | 113 | } |
115 | 114 | ||
116 | static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *info, struct pipe_buffer *buf) | 115 | static void * anon_pipe_buf_map(struct file *file, struct pipe_inode_info *pipe, |
116 | struct pipe_buffer *buf) | ||
117 | { | 117 | { |
118 | return kmap(buf->page); | 118 | return kmap(buf->page); |
119 | } | 119 | } |
120 | 120 | ||
121 | static void anon_pipe_buf_unmap(struct pipe_inode_info *info, struct pipe_buffer *buf) | 121 | static void anon_pipe_buf_unmap(struct pipe_inode_info *pipe, |
122 | struct pipe_buffer *buf) | ||
122 | { | 123 | { |
123 | kunmap(buf->page); | 124 | kunmap(buf->page); |
124 | } | 125 | } |
125 | 126 | ||
126 | static int anon_pipe_buf_steal(struct pipe_inode_info *info, | 127 | static int anon_pipe_buf_steal(struct pipe_inode_info *pipe, |
127 | struct pipe_buffer *buf) | 128 | struct pipe_buffer *buf) |
128 | { | 129 | { |
129 | buf->flags |= PIPE_BUF_FLAG_STOLEN; | 130 | buf->flags |= PIPE_BUF_FLAG_STOLEN; |
@@ -143,7 +144,7 @@ pipe_readv(struct file *filp, const struct iovec *_iov, | |||
143 | unsigned long nr_segs, loff_t *ppos) | 144 | unsigned long nr_segs, loff_t *ppos) |
144 | { | 145 | { |
145 | struct inode *inode = filp->f_dentry->d_inode; | 146 | struct inode *inode = filp->f_dentry->d_inode; |
146 | struct pipe_inode_info *info; | 147 | struct pipe_inode_info *pipe; |
147 | int do_wakeup; | 148 | int do_wakeup; |
148 | ssize_t ret; | 149 | ssize_t ret; |
149 | struct iovec *iov = (struct iovec *)_iov; | 150 | struct iovec *iov = (struct iovec *)_iov; |
@@ -156,13 +157,13 @@ pipe_readv(struct file *filp, const struct iovec *_iov, | |||
156 | 157 | ||
157 | do_wakeup = 0; | 158 | do_wakeup = 0; |
158 | ret = 0; | 159 | ret = 0; |
159 | mutex_lock(PIPE_MUTEX(*inode)); | 160 | mutex_lock(&inode->i_mutex); |
160 | info = inode->i_pipe; | 161 | pipe = inode->i_pipe; |
161 | for (;;) { | 162 | for (;;) { |
162 | int bufs = info->nrbufs; | 163 | int bufs = pipe->nrbufs; |
163 | if (bufs) { | 164 | if (bufs) { |
164 | int curbuf = info->curbuf; | 165 | int curbuf = pipe->curbuf; |
165 | struct pipe_buffer *buf = info->bufs + curbuf; | 166 | struct pipe_buffer *buf = pipe->bufs + curbuf; |
166 | struct pipe_buf_operations *ops = buf->ops; | 167 | struct pipe_buf_operations *ops = buf->ops; |
167 | void *addr; | 168 | void *addr; |
168 | size_t chars = buf->len; | 169 | size_t chars = buf->len; |
@@ -171,16 +172,17 @@ pipe_readv(struct file *filp, const struct iovec *_iov, | |||
171 | if (chars > total_len) | 172 | if (chars > total_len) |
172 | chars = total_len; | 173 | chars = total_len; |
173 | 174 | ||
174 | addr = ops->map(filp, info, buf); | 175 | addr = ops->map(filp, pipe, buf); |
175 | if (IS_ERR(addr)) { | 176 | if (IS_ERR(addr)) { |
176 | if (!ret) | 177 | if (!ret) |
177 | ret = PTR_ERR(addr); | 178 | ret = PTR_ERR(addr); |
178 | break; | 179 | break; |
179 | } | 180 | } |
180 | error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars); | 181 | error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars); |
181 | ops->unmap(info, buf); | 182 | ops->unmap(pipe, buf); |
182 | if (unlikely(error)) { | 183 | if (unlikely(error)) { |
183 | if (!ret) ret = -EFAULT; | 184 | if (!ret) |
185 | ret = -EFAULT; | ||
184 | break; | 186 | break; |
185 | } | 187 | } |
186 | ret += chars; | 188 | ret += chars; |
@@ -188,10 +190,10 @@ pipe_readv(struct file *filp, const struct iovec *_iov, | |||
188 | buf->len -= chars; | 190 | buf->len -= chars; |
189 | if (!buf->len) { | 191 | if (!buf->len) { |
190 | buf->ops = NULL; | 192 | buf->ops = NULL; |
191 | ops->release(info, buf); | 193 | ops->release(pipe, buf); |
192 | curbuf = (curbuf + 1) & (PIPE_BUFFERS-1); | 194 | curbuf = (curbuf + 1) & (PIPE_BUFFERS-1); |
193 | info->curbuf = curbuf; | 195 | pipe->curbuf = curbuf; |
194 | info->nrbufs = --bufs; | 196 | pipe->nrbufs = --bufs; |
195 | do_wakeup = 1; | 197 | do_wakeup = 1; |
196 | } | 198 | } |
197 | total_len -= chars; | 199 | total_len -= chars; |
@@ -200,9 +202,9 @@ pipe_readv(struct file *filp, const struct iovec *_iov, | |||
200 | } | 202 | } |
201 | if (bufs) /* More to do? */ | 203 | if (bufs) /* More to do? */ |
202 | continue; | 204 | continue; |
203 | if (!PIPE_WRITERS(*inode)) | 205 | if (!pipe->writers) |
204 | break; | 206 | break; |
205 | if (!PIPE_WAITING_WRITERS(*inode)) { | 207 | if (!pipe->waiting_writers) { |
206 | /* syscall merging: Usually we must not sleep | 208 | /* syscall merging: Usually we must not sleep |
207 | * if O_NONBLOCK is set, or if we got some data. | 209 | * if O_NONBLOCK is set, or if we got some data. |
208 | * But if a writer sleeps in kernel space, then | 210 | * But if a writer sleeps in kernel space, then |
@@ -216,20 +218,22 @@ pipe_readv(struct file *filp, const struct iovec *_iov, | |||
216 | } | 218 | } |
217 | } | 219 | } |
218 | if (signal_pending(current)) { | 220 | if (signal_pending(current)) { |
219 | if (!ret) ret = -ERESTARTSYS; | 221 | if (!ret) |
222 | ret = -ERESTARTSYS; | ||
220 | break; | 223 | break; |
221 | } | 224 | } |
222 | if (do_wakeup) { | 225 | if (do_wakeup) { |
223 | wake_up_interruptible_sync(PIPE_WAIT(*inode)); | 226 | wake_up_interruptible_sync(&pipe->wait); |
224 | kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT); | 227 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
225 | } | 228 | } |
226 | pipe_wait(inode); | 229 | pipe_wait(pipe); |
227 | } | 230 | } |
228 | mutex_unlock(PIPE_MUTEX(*inode)); | 231 | mutex_unlock(&inode->i_mutex); |
229 | /* Signal writers asynchronously that there is more room. */ | 232 | |
233 | /* Signal writers asynchronously that there is more room. */ | ||
230 | if (do_wakeup) { | 234 | if (do_wakeup) { |
231 | wake_up_interruptible(PIPE_WAIT(*inode)); | 235 | wake_up_interruptible(&pipe->wait); |
232 | kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT); | 236 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
233 | } | 237 | } |
234 | if (ret > 0) | 238 | if (ret > 0) |
235 | file_accessed(filp); | 239 | file_accessed(filp); |
@@ -240,6 +244,7 @@ static ssize_t | |||
240 | pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | 244 | pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) |
241 | { | 245 | { |
242 | struct iovec iov = { .iov_base = buf, .iov_len = count }; | 246 | struct iovec iov = { .iov_base = buf, .iov_len = count }; |
247 | |||
243 | return pipe_readv(filp, &iov, 1, ppos); | 248 | return pipe_readv(filp, &iov, 1, ppos); |
244 | } | 249 | } |
245 | 250 | ||
@@ -248,7 +253,7 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
248 | unsigned long nr_segs, loff_t *ppos) | 253 | unsigned long nr_segs, loff_t *ppos) |
249 | { | 254 | { |
250 | struct inode *inode = filp->f_dentry->d_inode; | 255 | struct inode *inode = filp->f_dentry->d_inode; |
251 | struct pipe_inode_info *info; | 256 | struct pipe_inode_info *pipe; |
252 | ssize_t ret; | 257 | ssize_t ret; |
253 | int do_wakeup; | 258 | int do_wakeup; |
254 | struct iovec *iov = (struct iovec *)_iov; | 259 | struct iovec *iov = (struct iovec *)_iov; |
@@ -262,10 +267,10 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
262 | 267 | ||
263 | do_wakeup = 0; | 268 | do_wakeup = 0; |
264 | ret = 0; | 269 | ret = 0; |
265 | mutex_lock(PIPE_MUTEX(*inode)); | 270 | mutex_lock(&inode->i_mutex); |
266 | info = inode->i_pipe; | 271 | pipe = inode->i_pipe; |
267 | 272 | ||
268 | if (!PIPE_READERS(*inode)) { | 273 | if (!pipe->readers) { |
269 | send_sig(SIGPIPE, current, 0); | 274 | send_sig(SIGPIPE, current, 0); |
270 | ret = -EPIPE; | 275 | ret = -EPIPE; |
271 | goto out; | 276 | goto out; |
@@ -273,23 +278,25 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
273 | 278 | ||
274 | /* We try to merge small writes */ | 279 | /* We try to merge small writes */ |
275 | chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ | 280 | chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ |
276 | if (info->nrbufs && chars != 0) { | 281 | if (pipe->nrbufs && chars != 0) { |
277 | int lastbuf = (info->curbuf + info->nrbufs - 1) & (PIPE_BUFFERS-1); | 282 | int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) & |
278 | struct pipe_buffer *buf = info->bufs + lastbuf; | 283 | (PIPE_BUFFERS-1); |
284 | struct pipe_buffer *buf = pipe->bufs + lastbuf; | ||
279 | struct pipe_buf_operations *ops = buf->ops; | 285 | struct pipe_buf_operations *ops = buf->ops; |
280 | int offset = buf->offset + buf->len; | 286 | int offset = buf->offset + buf->len; |
287 | |||
281 | if (ops->can_merge && offset + chars <= PAGE_SIZE) { | 288 | if (ops->can_merge && offset + chars <= PAGE_SIZE) { |
282 | void *addr; | 289 | void *addr; |
283 | int error; | 290 | int error; |
284 | 291 | ||
285 | addr = ops->map(filp, info, buf); | 292 | addr = ops->map(filp, pipe, buf); |
286 | if (IS_ERR(addr)) { | 293 | if (IS_ERR(addr)) { |
287 | error = PTR_ERR(addr); | 294 | error = PTR_ERR(addr); |
288 | goto out; | 295 | goto out; |
289 | } | 296 | } |
290 | error = pipe_iov_copy_from_user(offset + addr, iov, | 297 | error = pipe_iov_copy_from_user(offset + addr, iov, |
291 | chars); | 298 | chars); |
292 | ops->unmap(info, buf); | 299 | ops->unmap(pipe, buf); |
293 | ret = error; | 300 | ret = error; |
294 | do_wakeup = 1; | 301 | do_wakeup = 1; |
295 | if (error) | 302 | if (error) |
@@ -304,16 +311,18 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
304 | 311 | ||
305 | for (;;) { | 312 | for (;;) { |
306 | int bufs; | 313 | int bufs; |
307 | if (!PIPE_READERS(*inode)) { | 314 | |
315 | if (!pipe->readers) { | ||
308 | send_sig(SIGPIPE, current, 0); | 316 | send_sig(SIGPIPE, current, 0); |
309 | if (!ret) ret = -EPIPE; | 317 | if (!ret) |
318 | ret = -EPIPE; | ||
310 | break; | 319 | break; |
311 | } | 320 | } |
312 | bufs = info->nrbufs; | 321 | bufs = pipe->nrbufs; |
313 | if (bufs < PIPE_BUFFERS) { | 322 | if (bufs < PIPE_BUFFERS) { |
314 | int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS-1); | 323 | int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1); |
315 | struct pipe_buffer *buf = info->bufs + newbuf; | 324 | struct pipe_buffer *buf = pipe->bufs + newbuf; |
316 | struct page *page = info->tmp_page; | 325 | struct page *page = pipe->tmp_page; |
317 | int error; | 326 | int error; |
318 | 327 | ||
319 | if (!page) { | 328 | if (!page) { |
@@ -322,9 +331,9 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
322 | ret = ret ? : -ENOMEM; | 331 | ret = ret ? : -ENOMEM; |
323 | break; | 332 | break; |
324 | } | 333 | } |
325 | info->tmp_page = page; | 334 | pipe->tmp_page = page; |
326 | } | 335 | } |
327 | /* Always wakeup, even if the copy fails. Otherwise | 336 | /* Always wake up, even if the copy fails. Otherwise |
328 | * we lock up (O_NONBLOCK-)readers that sleep due to | 337 | * we lock up (O_NONBLOCK-)readers that sleep due to |
329 | * syscall merging. | 338 | * syscall merging. |
330 | * FIXME! Is this really true? | 339 | * FIXME! Is this really true? |
@@ -337,7 +346,8 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
337 | error = pipe_iov_copy_from_user(kmap(page), iov, chars); | 346 | error = pipe_iov_copy_from_user(kmap(page), iov, chars); |
338 | kunmap(page); | 347 | kunmap(page); |
339 | if (unlikely(error)) { | 348 | if (unlikely(error)) { |
340 | if (!ret) ret = -EFAULT; | 349 | if (!ret) |
350 | ret = -EFAULT; | ||
341 | break; | 351 | break; |
342 | } | 352 | } |
343 | ret += chars; | 353 | ret += chars; |
@@ -347,8 +357,8 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
347 | buf->ops = &anon_pipe_buf_ops; | 357 | buf->ops = &anon_pipe_buf_ops; |
348 | buf->offset = 0; | 358 | buf->offset = 0; |
349 | buf->len = chars; | 359 | buf->len = chars; |
350 | info->nrbufs = ++bufs; | 360 | pipe->nrbufs = ++bufs; |
351 | info->tmp_page = NULL; | 361 | pipe->tmp_page = NULL; |
352 | 362 | ||
353 | total_len -= chars; | 363 | total_len -= chars; |
354 | if (!total_len) | 364 | if (!total_len) |
@@ -357,27 +367,29 @@ pipe_writev(struct file *filp, const struct iovec *_iov, | |||
357 | if (bufs < PIPE_BUFFERS) | 367 | if (bufs < PIPE_BUFFERS) |
358 | continue; | 368 | continue; |
359 | if (filp->f_flags & O_NONBLOCK) { | 369 | if (filp->f_flags & O_NONBLOCK) { |
360 | if (!ret) ret = -EAGAIN; | 370 | if (!ret) |
371 | ret = -EAGAIN; | ||
361 | break; | 372 | break; |
362 | } | 373 | } |
363 | if (signal_pending(current)) { | 374 | if (signal_pending(current)) { |
364 | if (!ret) ret = -ERESTARTSYS; | 375 | if (!ret) |
376 | ret = -ERESTARTSYS; | ||
365 | break; | 377 | break; |
366 | } | 378 | } |
367 | if (do_wakeup) { | 379 | if (do_wakeup) { |
368 | wake_up_interruptible_sync(PIPE_WAIT(*inode)); | 380 | wake_up_interruptible_sync(&pipe->wait); |
369 | kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); | 381 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
370 | do_wakeup = 0; | 382 | do_wakeup = 0; |
371 | } | 383 | } |
372 | PIPE_WAITING_WRITERS(*inode)++; | 384 | pipe->waiting_writers++; |
373 | pipe_wait(inode); | 385 | pipe_wait(pipe); |
374 | PIPE_WAITING_WRITERS(*inode)--; | 386 | pipe->waiting_writers--; |
375 | } | 387 | } |
376 | out: | 388 | out: |
377 | mutex_unlock(PIPE_MUTEX(*inode)); | 389 | mutex_unlock(&inode->i_mutex); |
378 | if (do_wakeup) { | 390 | if (do_wakeup) { |
379 | wake_up_interruptible(PIPE_WAIT(*inode)); | 391 | wake_up_interruptible(&pipe->wait); |
380 | kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); | 392 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
381 | } | 393 | } |
382 | if (ret > 0) | 394 | if (ret > 0) |
383 | file_update_time(filp); | 395 | file_update_time(filp); |
@@ -389,6 +401,7 @@ pipe_write(struct file *filp, const char __user *buf, | |||
389 | size_t count, loff_t *ppos) | 401 | size_t count, loff_t *ppos) |
390 | { | 402 | { |
391 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; | 403 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; |
404 | |||
392 | return pipe_writev(filp, &iov, 1, ppos); | 405 | return pipe_writev(filp, &iov, 1, ppos); |
393 | } | 406 | } |
394 | 407 | ||
@@ -399,7 +412,8 @@ bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | |||
399 | } | 412 | } |
400 | 413 | ||
401 | static ssize_t | 414 | static ssize_t |
402 | bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos) | 415 | bad_pipe_w(struct file *filp, const char __user *buf, size_t count, |
416 | loff_t *ppos) | ||
403 | { | 417 | { |
404 | return -EBADF; | 418 | return -EBADF; |
405 | } | 419 | } |
@@ -409,21 +423,22 @@ pipe_ioctl(struct inode *pino, struct file *filp, | |||
409 | unsigned int cmd, unsigned long arg) | 423 | unsigned int cmd, unsigned long arg) |
410 | { | 424 | { |
411 | struct inode *inode = filp->f_dentry->d_inode; | 425 | struct inode *inode = filp->f_dentry->d_inode; |
412 | struct pipe_inode_info *info; | 426 | struct pipe_inode_info *pipe; |
413 | int count, buf, nrbufs; | 427 | int count, buf, nrbufs; |
414 | 428 | ||
415 | switch (cmd) { | 429 | switch (cmd) { |
416 | case FIONREAD: | 430 | case FIONREAD: |
417 | mutex_lock(PIPE_MUTEX(*inode)); | 431 | mutex_lock(&inode->i_mutex); |
418 | info = inode->i_pipe; | 432 | pipe = inode->i_pipe; |
419 | count = 0; | 433 | count = 0; |
420 | buf = info->curbuf; | 434 | buf = pipe->curbuf; |
421 | nrbufs = info->nrbufs; | 435 | nrbufs = pipe->nrbufs; |
422 | while (--nrbufs >= 0) { | 436 | while (--nrbufs >= 0) { |
423 | count += info->bufs[buf].len; | 437 | count += pipe->bufs[buf].len; |
424 | buf = (buf+1) & (PIPE_BUFFERS-1); | 438 | buf = (buf+1) & (PIPE_BUFFERS-1); |
425 | } | 439 | } |
426 | mutex_unlock(PIPE_MUTEX(*inode)); | 440 | mutex_unlock(&inode->i_mutex); |
441 | |||
427 | return put_user(count, (int __user *)arg); | 442 | return put_user(count, (int __user *)arg); |
428 | default: | 443 | default: |
429 | return -EINVAL; | 444 | return -EINVAL; |
@@ -436,17 +451,17 @@ pipe_poll(struct file *filp, poll_table *wait) | |||
436 | { | 451 | { |
437 | unsigned int mask; | 452 | unsigned int mask; |
438 | struct inode *inode = filp->f_dentry->d_inode; | 453 | struct inode *inode = filp->f_dentry->d_inode; |
439 | struct pipe_inode_info *info = inode->i_pipe; | 454 | struct pipe_inode_info *pipe = inode->i_pipe; |
440 | int nrbufs; | 455 | int nrbufs; |
441 | 456 | ||
442 | poll_wait(filp, PIPE_WAIT(*inode), wait); | 457 | poll_wait(filp, &pipe->wait, wait); |
443 | 458 | ||
444 | /* Reading only -- no need for acquiring the semaphore. */ | 459 | /* Reading only -- no need for acquiring the semaphore. */ |
445 | nrbufs = info->nrbufs; | 460 | nrbufs = pipe->nrbufs; |
446 | mask = 0; | 461 | mask = 0; |
447 | if (filp->f_mode & FMODE_READ) { | 462 | if (filp->f_mode & FMODE_READ) { |
448 | mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; | 463 | mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; |
449 | if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode)) | 464 | if (!pipe->writers && filp->f_version != pipe->w_counter) |
450 | mask |= POLLHUP; | 465 | mask |= POLLHUP; |
451 | } | 466 | } |
452 | 467 | ||
@@ -456,7 +471,7 @@ pipe_poll(struct file *filp, poll_table *wait) | |||
456 | * Most Unices do not set POLLERR for FIFOs but on Linux they | 471 | * Most Unices do not set POLLERR for FIFOs but on Linux they |
457 | * behave exactly like pipes for poll(). | 472 | * behave exactly like pipes for poll(). |
458 | */ | 473 | */ |
459 | if (!PIPE_READERS(*inode)) | 474 | if (!pipe->readers) |
460 | mask |= POLLERR; | 475 | mask |= POLLERR; |
461 | } | 476 | } |
462 | 477 | ||
@@ -466,17 +481,21 @@ pipe_poll(struct file *filp, poll_table *wait) | |||
466 | static int | 481 | static int |
467 | pipe_release(struct inode *inode, int decr, int decw) | 482 | pipe_release(struct inode *inode, int decr, int decw) |
468 | { | 483 | { |
469 | mutex_lock(PIPE_MUTEX(*inode)); | 484 | struct pipe_inode_info *pipe; |
470 | PIPE_READERS(*inode) -= decr; | 485 | |
471 | PIPE_WRITERS(*inode) -= decw; | 486 | mutex_lock(&inode->i_mutex); |
472 | if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) { | 487 | pipe = inode->i_pipe; |
488 | pipe->readers -= decr; | ||
489 | pipe->writers -= decw; | ||
490 | |||
491 | if (!pipe->readers && !pipe->writers) { | ||
473 | free_pipe_info(inode); | 492 | free_pipe_info(inode); |
474 | } else { | 493 | } else { |
475 | wake_up_interruptible(PIPE_WAIT(*inode)); | 494 | wake_up_interruptible(&pipe->wait); |
476 | kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); | 495 | kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN); |
477 | kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT); | 496 | kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT); |
478 | } | 497 | } |
479 | mutex_unlock(PIPE_MUTEX(*inode)); | 498 | mutex_unlock(&inode->i_mutex); |
480 | 499 | ||
481 | return 0; | 500 | return 0; |
482 | } | 501 | } |
@@ -487,9 +506,9 @@ pipe_read_fasync(int fd, struct file *filp, int on) | |||
487 | struct inode *inode = filp->f_dentry->d_inode; | 506 | struct inode *inode = filp->f_dentry->d_inode; |
488 | int retval; | 507 | int retval; |
489 | 508 | ||
490 | mutex_lock(PIPE_MUTEX(*inode)); | 509 | mutex_lock(&inode->i_mutex); |
491 | retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode)); | 510 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers); |
492 | mutex_unlock(PIPE_MUTEX(*inode)); | 511 | mutex_unlock(&inode->i_mutex); |
493 | 512 | ||
494 | if (retval < 0) | 513 | if (retval < 0) |
495 | return retval; | 514 | return retval; |
@@ -504,9 +523,9 @@ pipe_write_fasync(int fd, struct file *filp, int on) | |||
504 | struct inode *inode = filp->f_dentry->d_inode; | 523 | struct inode *inode = filp->f_dentry->d_inode; |
505 | int retval; | 524 | int retval; |
506 | 525 | ||
507 | mutex_lock(PIPE_MUTEX(*inode)); | 526 | mutex_lock(&inode->i_mutex); |
508 | retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode)); | 527 | retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers); |
509 | mutex_unlock(PIPE_MUTEX(*inode)); | 528 | mutex_unlock(&inode->i_mutex); |
510 | 529 | ||
511 | if (retval < 0) | 530 | if (retval < 0) |
512 | return retval; | 531 | return retval; |
@@ -519,16 +538,17 @@ static int | |||
519 | pipe_rdwr_fasync(int fd, struct file *filp, int on) | 538 | pipe_rdwr_fasync(int fd, struct file *filp, int on) |
520 | { | 539 | { |
521 | struct inode *inode = filp->f_dentry->d_inode; | 540 | struct inode *inode = filp->f_dentry->d_inode; |
541 | struct pipe_inode_info *pipe = inode->i_pipe; | ||
522 | int retval; | 542 | int retval; |
523 | 543 | ||
524 | mutex_lock(PIPE_MUTEX(*inode)); | 544 | mutex_lock(&inode->i_mutex); |
525 | 545 | ||
526 | retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode)); | 546 | retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); |
527 | 547 | ||
528 | if (retval >= 0) | 548 | if (retval >= 0) |
529 | retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode)); | 549 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); |
530 | 550 | ||
531 | mutex_unlock(PIPE_MUTEX(*inode)); | 551 | mutex_unlock(&inode->i_mutex); |
532 | 552 | ||
533 | if (retval < 0) | 553 | if (retval < 0) |
534 | return retval; | 554 | return retval; |
@@ -567,9 +587,9 @@ pipe_read_open(struct inode *inode, struct file *filp) | |||
567 | { | 587 | { |
568 | /* We could have perhaps used atomic_t, but this and friends | 588 | /* We could have perhaps used atomic_t, but this and friends |
569 | below are the only places. So it doesn't seem worthwhile. */ | 589 | below are the only places. So it doesn't seem worthwhile. */ |
570 | mutex_lock(PIPE_MUTEX(*inode)); | 590 | mutex_lock(&inode->i_mutex); |
571 | PIPE_READERS(*inode)++; | 591 | inode->i_pipe->readers++; |
572 | mutex_unlock(PIPE_MUTEX(*inode)); | 592 | mutex_unlock(&inode->i_mutex); |
573 | 593 | ||
574 | return 0; | 594 | return 0; |
575 | } | 595 | } |
@@ -577,9 +597,9 @@ pipe_read_open(struct inode *inode, struct file *filp) | |||
577 | static int | 597 | static int |
578 | pipe_write_open(struct inode *inode, struct file *filp) | 598 | pipe_write_open(struct inode *inode, struct file *filp) |
579 | { | 599 | { |
580 | mutex_lock(PIPE_MUTEX(*inode)); | 600 | mutex_lock(&inode->i_mutex); |
581 | PIPE_WRITERS(*inode)++; | 601 | inode->i_pipe->writers++; |
582 | mutex_unlock(PIPE_MUTEX(*inode)); | 602 | mutex_unlock(&inode->i_mutex); |
583 | 603 | ||
584 | return 0; | 604 | return 0; |
585 | } | 605 | } |
@@ -587,12 +607,12 @@ pipe_write_open(struct inode *inode, struct file *filp) | |||
587 | static int | 607 | static int |
588 | pipe_rdwr_open(struct inode *inode, struct file *filp) | 608 | pipe_rdwr_open(struct inode *inode, struct file *filp) |
589 | { | 609 | { |
590 | mutex_lock(PIPE_MUTEX(*inode)); | 610 | mutex_lock(&inode->i_mutex); |
591 | if (filp->f_mode & FMODE_READ) | 611 | if (filp->f_mode & FMODE_READ) |
592 | PIPE_READERS(*inode)++; | 612 | inode->i_pipe->readers++; |
593 | if (filp->f_mode & FMODE_WRITE) | 613 | if (filp->f_mode & FMODE_WRITE) |
594 | PIPE_WRITERS(*inode)++; | 614 | inode->i_pipe->writers++; |
595 | mutex_unlock(PIPE_MUTEX(*inode)); | 615 | mutex_unlock(&inode->i_mutex); |
596 | 616 | ||
597 | return 0; | 617 | return 0; |
598 | } | 618 | } |
@@ -675,37 +695,38 @@ static struct file_operations rdwr_pipe_fops = { | |||
675 | .fasync = pipe_rdwr_fasync, | 695 | .fasync = pipe_rdwr_fasync, |
676 | }; | 696 | }; |
677 | 697 | ||
678 | void free_pipe_info(struct inode *inode) | 698 | struct pipe_inode_info * alloc_pipe_info(struct inode *inode) |
699 | { | ||
700 | struct pipe_inode_info *pipe; | ||
701 | |||
702 | pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL); | ||
703 | if (pipe) { | ||
704 | init_waitqueue_head(&pipe->wait); | ||
705 | pipe->r_counter = pipe->w_counter = 1; | ||
706 | pipe->inode = inode; | ||
707 | } | ||
708 | |||
709 | return pipe; | ||
710 | } | ||
711 | |||
712 | void __free_pipe_info(struct pipe_inode_info *pipe) | ||
679 | { | 713 | { |
680 | int i; | 714 | int i; |
681 | struct pipe_inode_info *info = inode->i_pipe; | ||
682 | 715 | ||
683 | inode->i_pipe = NULL; | ||
684 | for (i = 0; i < PIPE_BUFFERS; i++) { | 716 | for (i = 0; i < PIPE_BUFFERS; i++) { |
685 | struct pipe_buffer *buf = info->bufs + i; | 717 | struct pipe_buffer *buf = pipe->bufs + i; |
686 | if (buf->ops) | 718 | if (buf->ops) |
687 | buf->ops->release(info, buf); | 719 | buf->ops->release(pipe, buf); |
688 | } | 720 | } |
689 | if (info->tmp_page) | 721 | if (pipe->tmp_page) |
690 | __free_page(info->tmp_page); | 722 | __free_page(pipe->tmp_page); |
691 | kfree(info); | 723 | kfree(pipe); |
692 | } | 724 | } |
693 | 725 | ||
694 | struct inode* pipe_new(struct inode* inode) | 726 | void free_pipe_info(struct inode *inode) |
695 | { | 727 | { |
696 | struct pipe_inode_info *info; | 728 | __free_pipe_info(inode->i_pipe); |
697 | 729 | inode->i_pipe = NULL; | |
698 | info = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL); | ||
699 | if (!info) | ||
700 | goto fail_page; | ||
701 | inode->i_pipe = info; | ||
702 | |||
703 | init_waitqueue_head(PIPE_WAIT(*inode)); | ||
704 | PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1; | ||
705 | |||
706 | return inode; | ||
707 | fail_page: | ||
708 | return NULL; | ||
709 | } | 730 | } |
710 | 731 | ||
711 | static struct vfsmount *pipe_mnt __read_mostly; | 732 | static struct vfsmount *pipe_mnt __read_mostly; |
@@ -713,6 +734,7 @@ static int pipefs_delete_dentry(struct dentry *dentry) | |||
713 | { | 734 | { |
714 | return 1; | 735 | return 1; |
715 | } | 736 | } |
737 | |||
716 | static struct dentry_operations pipefs_dentry_operations = { | 738 | static struct dentry_operations pipefs_dentry_operations = { |
717 | .d_delete = pipefs_delete_dentry, | 739 | .d_delete = pipefs_delete_dentry, |
718 | }; | 740 | }; |
@@ -720,13 +742,17 @@ static struct dentry_operations pipefs_dentry_operations = { | |||
720 | static struct inode * get_pipe_inode(void) | 742 | static struct inode * get_pipe_inode(void) |
721 | { | 743 | { |
722 | struct inode *inode = new_inode(pipe_mnt->mnt_sb); | 744 | struct inode *inode = new_inode(pipe_mnt->mnt_sb); |
745 | struct pipe_inode_info *pipe; | ||
723 | 746 | ||
724 | if (!inode) | 747 | if (!inode) |
725 | goto fail_inode; | 748 | goto fail_inode; |
726 | 749 | ||
727 | if(!pipe_new(inode)) | 750 | pipe = alloc_pipe_info(inode); |
751 | if (!pipe) | ||
728 | goto fail_iput; | 752 | goto fail_iput; |
729 | PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1; | 753 | inode->i_pipe = pipe; |
754 | |||
755 | pipe->readers = pipe->writers = 1; | ||
730 | inode->i_fop = &rdwr_pipe_fops; | 756 | inode->i_fop = &rdwr_pipe_fops; |
731 | 757 | ||
732 | /* | 758 | /* |
@@ -741,10 +767,12 @@ static struct inode * get_pipe_inode(void) | |||
741 | inode->i_gid = current->fsgid; | 767 | inode->i_gid = current->fsgid; |
742 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | 768 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
743 | inode->i_blksize = PAGE_SIZE; | 769 | inode->i_blksize = PAGE_SIZE; |
770 | |||
744 | return inode; | 771 | return inode; |
745 | 772 | ||
746 | fail_iput: | 773 | fail_iput: |
747 | iput(inode); | 774 | iput(inode); |
775 | |||
748 | fail_inode: | 776 | fail_inode: |
749 | return NULL; | 777 | return NULL; |
750 | } | 778 | } |
@@ -757,7 +785,7 @@ int do_pipe(int *fd) | |||
757 | struct inode * inode; | 785 | struct inode * inode; |
758 | struct file *f1, *f2; | 786 | struct file *f1, *f2; |
759 | int error; | 787 | int error; |
760 | int i,j; | 788 | int i, j; |
761 | 789 | ||
762 | error = -ENFILE; | 790 | error = -ENFILE; |
763 | f1 = get_empty_filp(); | 791 | f1 = get_empty_filp(); |
@@ -790,6 +818,7 @@ int do_pipe(int *fd) | |||
790 | dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this); | 818 | dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this); |
791 | if (!dentry) | 819 | if (!dentry) |
792 | goto close_f12_inode_i_j; | 820 | goto close_f12_inode_i_j; |
821 | |||
793 | dentry->d_op = &pipefs_dentry_operations; | 822 | dentry->d_op = &pipefs_dentry_operations; |
794 | d_add(dentry, inode); | 823 | d_add(dentry, inode); |
795 | f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt)); | 824 | f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt)); |
@@ -813,6 +842,7 @@ int do_pipe(int *fd) | |||
813 | fd_install(j, f2); | 842 | fd_install(j, f2); |
814 | fd[0] = i; | 843 | fd[0] = i; |
815 | fd[1] = j; | 844 | fd[1] = j; |
845 | |||
816 | return 0; | 846 | return 0; |
817 | 847 | ||
818 | close_f12_inode_i_j: | 848 | close_f12_inode_i_j: |
@@ -837,8 +867,9 @@ no_files: | |||
837 | * d_name - pipe: will go nicely and kill the special-casing in procfs. | 867 | * d_name - pipe: will go nicely and kill the special-casing in procfs. |
838 | */ | 868 | */ |
839 | 869 | ||
840 | static struct super_block *pipefs_get_sb(struct file_system_type *fs_type, | 870 | static struct super_block * |
841 | int flags, const char *dev_name, void *data) | 871 | pipefs_get_sb(struct file_system_type *fs_type, int flags, |
872 | const char *dev_name, void *data) | ||
842 | { | 873 | { |
843 | return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC); | 874 | return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC); |
844 | } | 875 | } |
@@ -852,6 +883,7 @@ static struct file_system_type pipe_fs_type = { | |||
852 | static int __init init_pipe_fs(void) | 883 | static int __init init_pipe_fs(void) |
853 | { | 884 | { |
854 | int err = register_filesystem(&pipe_fs_type); | 885 | int err = register_filesystem(&pipe_fs_type); |
886 | |||
855 | if (!err) { | 887 | if (!err) { |
856 | pipe_mnt = kern_mount(&pipe_fs_type); | 888 | pipe_mnt = kern_mount(&pipe_fs_type); |
857 | if (IS_ERR(pipe_mnt)) { | 889 | if (IS_ERR(pipe_mnt)) { |