aboutsummaryrefslogtreecommitdiffstats
path: root/fs/pipe.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/pipe.c')
-rw-r--r--fs/pipe.c317
1 files changed, 178 insertions, 139 deletions
diff --git a/fs/pipe.c b/fs/pipe.c
index 795df987cd38..7fefb10db8d9 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -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 */
39void pipe_wait(struct inode * inode) 39void 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
54static int 57static 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
94static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buffer *buf) 97static 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,42 +104,46 @@ 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
116static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *info, struct pipe_buffer *buf) 115static 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
121static void anon_pipe_buf_unmap(struct pipe_inode_info *info, struct pipe_buffer *buf) 121static 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
126static int anon_pipe_buf_steal(struct pipe_inode_info *info, 127static 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;
130 return 0; 131 return 0;
131} 132}
132 133
134static void anon_pipe_buf_get(struct pipe_inode_info *info,
135 struct pipe_buffer *buf)
136{
137 page_cache_get(buf->page);
138}
139
133static struct pipe_buf_operations anon_pipe_buf_ops = { 140static struct pipe_buf_operations anon_pipe_buf_ops = {
134 .can_merge = 1, 141 .can_merge = 1,
135 .map = anon_pipe_buf_map, 142 .map = anon_pipe_buf_map,
136 .unmap = anon_pipe_buf_unmap, 143 .unmap = anon_pipe_buf_unmap,
137 .release = anon_pipe_buf_release, 144 .release = anon_pipe_buf_release,
138 .steal = anon_pipe_buf_steal, 145 .steal = anon_pipe_buf_steal,
146 .get = anon_pipe_buf_get,
139}; 147};
140 148
141static ssize_t 149static ssize_t
@@ -143,7 +151,7 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
143 unsigned long nr_segs, loff_t *ppos) 151 unsigned long nr_segs, loff_t *ppos)
144{ 152{
145 struct inode *inode = filp->f_dentry->d_inode; 153 struct inode *inode = filp->f_dentry->d_inode;
146 struct pipe_inode_info *info; 154 struct pipe_inode_info *pipe;
147 int do_wakeup; 155 int do_wakeup;
148 ssize_t ret; 156 ssize_t ret;
149 struct iovec *iov = (struct iovec *)_iov; 157 struct iovec *iov = (struct iovec *)_iov;
@@ -156,13 +164,13 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
156 164
157 do_wakeup = 0; 165 do_wakeup = 0;
158 ret = 0; 166 ret = 0;
159 mutex_lock(PIPE_MUTEX(*inode)); 167 mutex_lock(&inode->i_mutex);
160 info = inode->i_pipe; 168 pipe = inode->i_pipe;
161 for (;;) { 169 for (;;) {
162 int bufs = info->nrbufs; 170 int bufs = pipe->nrbufs;
163 if (bufs) { 171 if (bufs) {
164 int curbuf = info->curbuf; 172 int curbuf = pipe->curbuf;
165 struct pipe_buffer *buf = info->bufs + curbuf; 173 struct pipe_buffer *buf = pipe->bufs + curbuf;
166 struct pipe_buf_operations *ops = buf->ops; 174 struct pipe_buf_operations *ops = buf->ops;
167 void *addr; 175 void *addr;
168 size_t chars = buf->len; 176 size_t chars = buf->len;
@@ -171,16 +179,17 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
171 if (chars > total_len) 179 if (chars > total_len)
172 chars = total_len; 180 chars = total_len;
173 181
174 addr = ops->map(filp, info, buf); 182 addr = ops->map(filp, pipe, buf);
175 if (IS_ERR(addr)) { 183 if (IS_ERR(addr)) {
176 if (!ret) 184 if (!ret)
177 ret = PTR_ERR(addr); 185 ret = PTR_ERR(addr);
178 break; 186 break;
179 } 187 }
180 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars); 188 error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
181 ops->unmap(info, buf); 189 ops->unmap(pipe, buf);
182 if (unlikely(error)) { 190 if (unlikely(error)) {
183 if (!ret) ret = -EFAULT; 191 if (!ret)
192 ret = -EFAULT;
184 break; 193 break;
185 } 194 }
186 ret += chars; 195 ret += chars;
@@ -188,10 +197,10 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
188 buf->len -= chars; 197 buf->len -= chars;
189 if (!buf->len) { 198 if (!buf->len) {
190 buf->ops = NULL; 199 buf->ops = NULL;
191 ops->release(info, buf); 200 ops->release(pipe, buf);
192 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1); 201 curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
193 info->curbuf = curbuf; 202 pipe->curbuf = curbuf;
194 info->nrbufs = --bufs; 203 pipe->nrbufs = --bufs;
195 do_wakeup = 1; 204 do_wakeup = 1;
196 } 205 }
197 total_len -= chars; 206 total_len -= chars;
@@ -200,9 +209,9 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
200 } 209 }
201 if (bufs) /* More to do? */ 210 if (bufs) /* More to do? */
202 continue; 211 continue;
203 if (!PIPE_WRITERS(*inode)) 212 if (!pipe->writers)
204 break; 213 break;
205 if (!PIPE_WAITING_WRITERS(*inode)) { 214 if (!pipe->waiting_writers) {
206 /* syscall merging: Usually we must not sleep 215 /* syscall merging: Usually we must not sleep
207 * if O_NONBLOCK is set, or if we got some data. 216 * if O_NONBLOCK is set, or if we got some data.
208 * But if a writer sleeps in kernel space, then 217 * But if a writer sleeps in kernel space, then
@@ -216,20 +225,22 @@ pipe_readv(struct file *filp, const struct iovec *_iov,
216 } 225 }
217 } 226 }
218 if (signal_pending(current)) { 227 if (signal_pending(current)) {
219 if (!ret) ret = -ERESTARTSYS; 228 if (!ret)
229 ret = -ERESTARTSYS;
220 break; 230 break;
221 } 231 }
222 if (do_wakeup) { 232 if (do_wakeup) {
223 wake_up_interruptible_sync(PIPE_WAIT(*inode)); 233 wake_up_interruptible_sync(&pipe->wait);
224 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT); 234 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
225 } 235 }
226 pipe_wait(inode); 236 pipe_wait(pipe);
227 } 237 }
228 mutex_unlock(PIPE_MUTEX(*inode)); 238 mutex_unlock(&inode->i_mutex);
229 /* Signal writers asynchronously that there is more room. */ 239
240 /* Signal writers asynchronously that there is more room. */
230 if (do_wakeup) { 241 if (do_wakeup) {
231 wake_up_interruptible(PIPE_WAIT(*inode)); 242 wake_up_interruptible(&pipe->wait);
232 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT); 243 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
233 } 244 }
234 if (ret > 0) 245 if (ret > 0)
235 file_accessed(filp); 246 file_accessed(filp);
@@ -240,6 +251,7 @@ static ssize_t
240pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) 251pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
241{ 252{
242 struct iovec iov = { .iov_base = buf, .iov_len = count }; 253 struct iovec iov = { .iov_base = buf, .iov_len = count };
254
243 return pipe_readv(filp, &iov, 1, ppos); 255 return pipe_readv(filp, &iov, 1, ppos);
244} 256}
245 257
@@ -248,7 +260,7 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
248 unsigned long nr_segs, loff_t *ppos) 260 unsigned long nr_segs, loff_t *ppos)
249{ 261{
250 struct inode *inode = filp->f_dentry->d_inode; 262 struct inode *inode = filp->f_dentry->d_inode;
251 struct pipe_inode_info *info; 263 struct pipe_inode_info *pipe;
252 ssize_t ret; 264 ssize_t ret;
253 int do_wakeup; 265 int do_wakeup;
254 struct iovec *iov = (struct iovec *)_iov; 266 struct iovec *iov = (struct iovec *)_iov;
@@ -262,10 +274,10 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
262 274
263 do_wakeup = 0; 275 do_wakeup = 0;
264 ret = 0; 276 ret = 0;
265 mutex_lock(PIPE_MUTEX(*inode)); 277 mutex_lock(&inode->i_mutex);
266 info = inode->i_pipe; 278 pipe = inode->i_pipe;
267 279
268 if (!PIPE_READERS(*inode)) { 280 if (!pipe->readers) {
269 send_sig(SIGPIPE, current, 0); 281 send_sig(SIGPIPE, current, 0);
270 ret = -EPIPE; 282 ret = -EPIPE;
271 goto out; 283 goto out;
@@ -273,23 +285,25 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
273 285
274 /* We try to merge small writes */ 286 /* We try to merge small writes */
275 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */ 287 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
276 if (info->nrbufs && chars != 0) { 288 if (pipe->nrbufs && chars != 0) {
277 int lastbuf = (info->curbuf + info->nrbufs - 1) & (PIPE_BUFFERS-1); 289 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
278 struct pipe_buffer *buf = info->bufs + lastbuf; 290 (PIPE_BUFFERS-1);
291 struct pipe_buffer *buf = pipe->bufs + lastbuf;
279 struct pipe_buf_operations *ops = buf->ops; 292 struct pipe_buf_operations *ops = buf->ops;
280 int offset = buf->offset + buf->len; 293 int offset = buf->offset + buf->len;
294
281 if (ops->can_merge && offset + chars <= PAGE_SIZE) { 295 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
282 void *addr; 296 void *addr;
283 int error; 297 int error;
284 298
285 addr = ops->map(filp, info, buf); 299 addr = ops->map(filp, pipe, buf);
286 if (IS_ERR(addr)) { 300 if (IS_ERR(addr)) {
287 error = PTR_ERR(addr); 301 error = PTR_ERR(addr);
288 goto out; 302 goto out;
289 } 303 }
290 error = pipe_iov_copy_from_user(offset + addr, iov, 304 error = pipe_iov_copy_from_user(offset + addr, iov,
291 chars); 305 chars);
292 ops->unmap(info, buf); 306 ops->unmap(pipe, buf);
293 ret = error; 307 ret = error;
294 do_wakeup = 1; 308 do_wakeup = 1;
295 if (error) 309 if (error)
@@ -304,16 +318,18 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
304 318
305 for (;;) { 319 for (;;) {
306 int bufs; 320 int bufs;
307 if (!PIPE_READERS(*inode)) { 321
322 if (!pipe->readers) {
308 send_sig(SIGPIPE, current, 0); 323 send_sig(SIGPIPE, current, 0);
309 if (!ret) ret = -EPIPE; 324 if (!ret)
325 ret = -EPIPE;
310 break; 326 break;
311 } 327 }
312 bufs = info->nrbufs; 328 bufs = pipe->nrbufs;
313 if (bufs < PIPE_BUFFERS) { 329 if (bufs < PIPE_BUFFERS) {
314 int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS-1); 330 int newbuf = (pipe->curbuf + bufs) & (PIPE_BUFFERS-1);
315 struct pipe_buffer *buf = info->bufs + newbuf; 331 struct pipe_buffer *buf = pipe->bufs + newbuf;
316 struct page *page = info->tmp_page; 332 struct page *page = pipe->tmp_page;
317 int error; 333 int error;
318 334
319 if (!page) { 335 if (!page) {
@@ -322,9 +338,9 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
322 ret = ret ? : -ENOMEM; 338 ret = ret ? : -ENOMEM;
323 break; 339 break;
324 } 340 }
325 info->tmp_page = page; 341 pipe->tmp_page = page;
326 } 342 }
327 /* Always wakeup, even if the copy fails. Otherwise 343 /* Always wake up, even if the copy fails. Otherwise
328 * we lock up (O_NONBLOCK-)readers that sleep due to 344 * we lock up (O_NONBLOCK-)readers that sleep due to
329 * syscall merging. 345 * syscall merging.
330 * FIXME! Is this really true? 346 * FIXME! Is this really true?
@@ -337,7 +353,8 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
337 error = pipe_iov_copy_from_user(kmap(page), iov, chars); 353 error = pipe_iov_copy_from_user(kmap(page), iov, chars);
338 kunmap(page); 354 kunmap(page);
339 if (unlikely(error)) { 355 if (unlikely(error)) {
340 if (!ret) ret = -EFAULT; 356 if (!ret)
357 ret = -EFAULT;
341 break; 358 break;
342 } 359 }
343 ret += chars; 360 ret += chars;
@@ -347,8 +364,8 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
347 buf->ops = &anon_pipe_buf_ops; 364 buf->ops = &anon_pipe_buf_ops;
348 buf->offset = 0; 365 buf->offset = 0;
349 buf->len = chars; 366 buf->len = chars;
350 info->nrbufs = ++bufs; 367 pipe->nrbufs = ++bufs;
351 info->tmp_page = NULL; 368 pipe->tmp_page = NULL;
352 369
353 total_len -= chars; 370 total_len -= chars;
354 if (!total_len) 371 if (!total_len)
@@ -357,27 +374,29 @@ pipe_writev(struct file *filp, const struct iovec *_iov,
357 if (bufs < PIPE_BUFFERS) 374 if (bufs < PIPE_BUFFERS)
358 continue; 375 continue;
359 if (filp->f_flags & O_NONBLOCK) { 376 if (filp->f_flags & O_NONBLOCK) {
360 if (!ret) ret = -EAGAIN; 377 if (!ret)
378 ret = -EAGAIN;
361 break; 379 break;
362 } 380 }
363 if (signal_pending(current)) { 381 if (signal_pending(current)) {
364 if (!ret) ret = -ERESTARTSYS; 382 if (!ret)
383 ret = -ERESTARTSYS;
365 break; 384 break;
366 } 385 }
367 if (do_wakeup) { 386 if (do_wakeup) {
368 wake_up_interruptible_sync(PIPE_WAIT(*inode)); 387 wake_up_interruptible_sync(&pipe->wait);
369 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); 388 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
370 do_wakeup = 0; 389 do_wakeup = 0;
371 } 390 }
372 PIPE_WAITING_WRITERS(*inode)++; 391 pipe->waiting_writers++;
373 pipe_wait(inode); 392 pipe_wait(pipe);
374 PIPE_WAITING_WRITERS(*inode)--; 393 pipe->waiting_writers--;
375 } 394 }
376out: 395out:
377 mutex_unlock(PIPE_MUTEX(*inode)); 396 mutex_unlock(&inode->i_mutex);
378 if (do_wakeup) { 397 if (do_wakeup) {
379 wake_up_interruptible(PIPE_WAIT(*inode)); 398 wake_up_interruptible(&pipe->wait);
380 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); 399 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
381 } 400 }
382 if (ret > 0) 401 if (ret > 0)
383 file_update_time(filp); 402 file_update_time(filp);
@@ -389,6 +408,7 @@ pipe_write(struct file *filp, const char __user *buf,
389 size_t count, loff_t *ppos) 408 size_t count, loff_t *ppos)
390{ 409{
391 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count }; 410 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
411
392 return pipe_writev(filp, &iov, 1, ppos); 412 return pipe_writev(filp, &iov, 1, ppos);
393} 413}
394 414
@@ -399,7 +419,8 @@ bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
399} 419}
400 420
401static ssize_t 421static ssize_t
402bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos) 422bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
423 loff_t *ppos)
403{ 424{
404 return -EBADF; 425 return -EBADF;
405} 426}
@@ -409,21 +430,22 @@ pipe_ioctl(struct inode *pino, struct file *filp,
409 unsigned int cmd, unsigned long arg) 430 unsigned int cmd, unsigned long arg)
410{ 431{
411 struct inode *inode = filp->f_dentry->d_inode; 432 struct inode *inode = filp->f_dentry->d_inode;
412 struct pipe_inode_info *info; 433 struct pipe_inode_info *pipe;
413 int count, buf, nrbufs; 434 int count, buf, nrbufs;
414 435
415 switch (cmd) { 436 switch (cmd) {
416 case FIONREAD: 437 case FIONREAD:
417 mutex_lock(PIPE_MUTEX(*inode)); 438 mutex_lock(&inode->i_mutex);
418 info = inode->i_pipe; 439 pipe = inode->i_pipe;
419 count = 0; 440 count = 0;
420 buf = info->curbuf; 441 buf = pipe->curbuf;
421 nrbufs = info->nrbufs; 442 nrbufs = pipe->nrbufs;
422 while (--nrbufs >= 0) { 443 while (--nrbufs >= 0) {
423 count += info->bufs[buf].len; 444 count += pipe->bufs[buf].len;
424 buf = (buf+1) & (PIPE_BUFFERS-1); 445 buf = (buf+1) & (PIPE_BUFFERS-1);
425 } 446 }
426 mutex_unlock(PIPE_MUTEX(*inode)); 447 mutex_unlock(&inode->i_mutex);
448
427 return put_user(count, (int __user *)arg); 449 return put_user(count, (int __user *)arg);
428 default: 450 default:
429 return -EINVAL; 451 return -EINVAL;
@@ -436,17 +458,17 @@ pipe_poll(struct file *filp, poll_table *wait)
436{ 458{
437 unsigned int mask; 459 unsigned int mask;
438 struct inode *inode = filp->f_dentry->d_inode; 460 struct inode *inode = filp->f_dentry->d_inode;
439 struct pipe_inode_info *info = inode->i_pipe; 461 struct pipe_inode_info *pipe = inode->i_pipe;
440 int nrbufs; 462 int nrbufs;
441 463
442 poll_wait(filp, PIPE_WAIT(*inode), wait); 464 poll_wait(filp, &pipe->wait, wait);
443 465
444 /* Reading only -- no need for acquiring the semaphore. */ 466 /* Reading only -- no need for acquiring the semaphore. */
445 nrbufs = info->nrbufs; 467 nrbufs = pipe->nrbufs;
446 mask = 0; 468 mask = 0;
447 if (filp->f_mode & FMODE_READ) { 469 if (filp->f_mode & FMODE_READ) {
448 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0; 470 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
449 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode)) 471 if (!pipe->writers && filp->f_version != pipe->w_counter)
450 mask |= POLLHUP; 472 mask |= POLLHUP;
451 } 473 }
452 474
@@ -456,7 +478,7 @@ pipe_poll(struct file *filp, poll_table *wait)
456 * Most Unices do not set POLLERR for FIFOs but on Linux they 478 * Most Unices do not set POLLERR for FIFOs but on Linux they
457 * behave exactly like pipes for poll(). 479 * behave exactly like pipes for poll().
458 */ 480 */
459 if (!PIPE_READERS(*inode)) 481 if (!pipe->readers)
460 mask |= POLLERR; 482 mask |= POLLERR;
461 } 483 }
462 484
@@ -466,17 +488,21 @@ pipe_poll(struct file *filp, poll_table *wait)
466static int 488static int
467pipe_release(struct inode *inode, int decr, int decw) 489pipe_release(struct inode *inode, int decr, int decw)
468{ 490{
469 mutex_lock(PIPE_MUTEX(*inode)); 491 struct pipe_inode_info *pipe;
470 PIPE_READERS(*inode) -= decr; 492
471 PIPE_WRITERS(*inode) -= decw; 493 mutex_lock(&inode->i_mutex);
472 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) { 494 pipe = inode->i_pipe;
495 pipe->readers -= decr;
496 pipe->writers -= decw;
497
498 if (!pipe->readers && !pipe->writers) {
473 free_pipe_info(inode); 499 free_pipe_info(inode);
474 } else { 500 } else {
475 wake_up_interruptible(PIPE_WAIT(*inode)); 501 wake_up_interruptible(&pipe->wait);
476 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN); 502 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
477 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT); 503 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
478 } 504 }
479 mutex_unlock(PIPE_MUTEX(*inode)); 505 mutex_unlock(&inode->i_mutex);
480 506
481 return 0; 507 return 0;
482} 508}
@@ -487,9 +513,9 @@ pipe_read_fasync(int fd, struct file *filp, int on)
487 struct inode *inode = filp->f_dentry->d_inode; 513 struct inode *inode = filp->f_dentry->d_inode;
488 int retval; 514 int retval;
489 515
490 mutex_lock(PIPE_MUTEX(*inode)); 516 mutex_lock(&inode->i_mutex);
491 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode)); 517 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
492 mutex_unlock(PIPE_MUTEX(*inode)); 518 mutex_unlock(&inode->i_mutex);
493 519
494 if (retval < 0) 520 if (retval < 0)
495 return retval; 521 return retval;
@@ -504,9 +530,9 @@ pipe_write_fasync(int fd, struct file *filp, int on)
504 struct inode *inode = filp->f_dentry->d_inode; 530 struct inode *inode = filp->f_dentry->d_inode;
505 int retval; 531 int retval;
506 532
507 mutex_lock(PIPE_MUTEX(*inode)); 533 mutex_lock(&inode->i_mutex);
508 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode)); 534 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
509 mutex_unlock(PIPE_MUTEX(*inode)); 535 mutex_unlock(&inode->i_mutex);
510 536
511 if (retval < 0) 537 if (retval < 0)
512 return retval; 538 return retval;
@@ -519,16 +545,17 @@ static int
519pipe_rdwr_fasync(int fd, struct file *filp, int on) 545pipe_rdwr_fasync(int fd, struct file *filp, int on)
520{ 546{
521 struct inode *inode = filp->f_dentry->d_inode; 547 struct inode *inode = filp->f_dentry->d_inode;
548 struct pipe_inode_info *pipe = inode->i_pipe;
522 int retval; 549 int retval;
523 550
524 mutex_lock(PIPE_MUTEX(*inode)); 551 mutex_lock(&inode->i_mutex);
525 552
526 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode)); 553 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
527 554
528 if (retval >= 0) 555 if (retval >= 0)
529 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode)); 556 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
530 557
531 mutex_unlock(PIPE_MUTEX(*inode)); 558 mutex_unlock(&inode->i_mutex);
532 559
533 if (retval < 0) 560 if (retval < 0)
534 return retval; 561 return retval;
@@ -567,9 +594,9 @@ pipe_read_open(struct inode *inode, struct file *filp)
567{ 594{
568 /* We could have perhaps used atomic_t, but this and friends 595 /* We could have perhaps used atomic_t, but this and friends
569 below are the only places. So it doesn't seem worthwhile. */ 596 below are the only places. So it doesn't seem worthwhile. */
570 mutex_lock(PIPE_MUTEX(*inode)); 597 mutex_lock(&inode->i_mutex);
571 PIPE_READERS(*inode)++; 598 inode->i_pipe->readers++;
572 mutex_unlock(PIPE_MUTEX(*inode)); 599 mutex_unlock(&inode->i_mutex);
573 600
574 return 0; 601 return 0;
575} 602}
@@ -577,9 +604,9 @@ pipe_read_open(struct inode *inode, struct file *filp)
577static int 604static int
578pipe_write_open(struct inode *inode, struct file *filp) 605pipe_write_open(struct inode *inode, struct file *filp)
579{ 606{
580 mutex_lock(PIPE_MUTEX(*inode)); 607 mutex_lock(&inode->i_mutex);
581 PIPE_WRITERS(*inode)++; 608 inode->i_pipe->writers++;
582 mutex_unlock(PIPE_MUTEX(*inode)); 609 mutex_unlock(&inode->i_mutex);
583 610
584 return 0; 611 return 0;
585} 612}
@@ -587,12 +614,12 @@ pipe_write_open(struct inode *inode, struct file *filp)
587static int 614static int
588pipe_rdwr_open(struct inode *inode, struct file *filp) 615pipe_rdwr_open(struct inode *inode, struct file *filp)
589{ 616{
590 mutex_lock(PIPE_MUTEX(*inode)); 617 mutex_lock(&inode->i_mutex);
591 if (filp->f_mode & FMODE_READ) 618 if (filp->f_mode & FMODE_READ)
592 PIPE_READERS(*inode)++; 619 inode->i_pipe->readers++;
593 if (filp->f_mode & FMODE_WRITE) 620 if (filp->f_mode & FMODE_WRITE)
594 PIPE_WRITERS(*inode)++; 621 inode->i_pipe->writers++;
595 mutex_unlock(PIPE_MUTEX(*inode)); 622 mutex_unlock(&inode->i_mutex);
596 623
597 return 0; 624 return 0;
598} 625}
@@ -675,37 +702,38 @@ static struct file_operations rdwr_pipe_fops = {
675 .fasync = pipe_rdwr_fasync, 702 .fasync = pipe_rdwr_fasync,
676}; 703};
677 704
678void free_pipe_info(struct inode *inode) 705struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
706{
707 struct pipe_inode_info *pipe;
708
709 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
710 if (pipe) {
711 init_waitqueue_head(&pipe->wait);
712 pipe->r_counter = pipe->w_counter = 1;
713 pipe->inode = inode;
714 }
715
716 return pipe;
717}
718
719void __free_pipe_info(struct pipe_inode_info *pipe)
679{ 720{
680 int i; 721 int i;
681 struct pipe_inode_info *info = inode->i_pipe;
682 722
683 inode->i_pipe = NULL;
684 for (i = 0; i < PIPE_BUFFERS; i++) { 723 for (i = 0; i < PIPE_BUFFERS; i++) {
685 struct pipe_buffer *buf = info->bufs + i; 724 struct pipe_buffer *buf = pipe->bufs + i;
686 if (buf->ops) 725 if (buf->ops)
687 buf->ops->release(info, buf); 726 buf->ops->release(pipe, buf);
688 } 727 }
689 if (info->tmp_page) 728 if (pipe->tmp_page)
690 __free_page(info->tmp_page); 729 __free_page(pipe->tmp_page);
691 kfree(info); 730 kfree(pipe);
692} 731}
693 732
694struct inode* pipe_new(struct inode* inode) 733void free_pipe_info(struct inode *inode)
695{ 734{
696 struct pipe_inode_info *info; 735 __free_pipe_info(inode->i_pipe);
697 736 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;
707fail_page:
708 return NULL;
709} 737}
710 738
711static struct vfsmount *pipe_mnt __read_mostly; 739static struct vfsmount *pipe_mnt __read_mostly;
@@ -713,6 +741,7 @@ static int pipefs_delete_dentry(struct dentry *dentry)
713{ 741{
714 return 1; 742 return 1;
715} 743}
744
716static struct dentry_operations pipefs_dentry_operations = { 745static struct dentry_operations pipefs_dentry_operations = {
717 .d_delete = pipefs_delete_dentry, 746 .d_delete = pipefs_delete_dentry,
718}; 747};
@@ -720,13 +749,17 @@ static struct dentry_operations pipefs_dentry_operations = {
720static struct inode * get_pipe_inode(void) 749static struct inode * get_pipe_inode(void)
721{ 750{
722 struct inode *inode = new_inode(pipe_mnt->mnt_sb); 751 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
752 struct pipe_inode_info *pipe;
723 753
724 if (!inode) 754 if (!inode)
725 goto fail_inode; 755 goto fail_inode;
726 756
727 if(!pipe_new(inode)) 757 pipe = alloc_pipe_info(inode);
758 if (!pipe)
728 goto fail_iput; 759 goto fail_iput;
729 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1; 760 inode->i_pipe = pipe;
761
762 pipe->readers = pipe->writers = 1;
730 inode->i_fop = &rdwr_pipe_fops; 763 inode->i_fop = &rdwr_pipe_fops;
731 764
732 /* 765 /*
@@ -741,10 +774,12 @@ static struct inode * get_pipe_inode(void)
741 inode->i_gid = current->fsgid; 774 inode->i_gid = current->fsgid;
742 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 775 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
743 inode->i_blksize = PAGE_SIZE; 776 inode->i_blksize = PAGE_SIZE;
777
744 return inode; 778 return inode;
745 779
746fail_iput: 780fail_iput:
747 iput(inode); 781 iput(inode);
782
748fail_inode: 783fail_inode:
749 return NULL; 784 return NULL;
750} 785}
@@ -757,7 +792,7 @@ int do_pipe(int *fd)
757 struct inode * inode; 792 struct inode * inode;
758 struct file *f1, *f2; 793 struct file *f1, *f2;
759 int error; 794 int error;
760 int i,j; 795 int i, j;
761 796
762 error = -ENFILE; 797 error = -ENFILE;
763 f1 = get_empty_filp(); 798 f1 = get_empty_filp();
@@ -790,6 +825,7 @@ int do_pipe(int *fd)
790 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this); 825 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
791 if (!dentry) 826 if (!dentry)
792 goto close_f12_inode_i_j; 827 goto close_f12_inode_i_j;
828
793 dentry->d_op = &pipefs_dentry_operations; 829 dentry->d_op = &pipefs_dentry_operations;
794 d_add(dentry, inode); 830 d_add(dentry, inode);
795 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt)); 831 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
@@ -813,6 +849,7 @@ int do_pipe(int *fd)
813 fd_install(j, f2); 849 fd_install(j, f2);
814 fd[0] = i; 850 fd[0] = i;
815 fd[1] = j; 851 fd[1] = j;
852
816 return 0; 853 return 0;
817 854
818close_f12_inode_i_j: 855close_f12_inode_i_j:
@@ -837,8 +874,9 @@ no_files:
837 * d_name - pipe: will go nicely and kill the special-casing in procfs. 874 * d_name - pipe: will go nicely and kill the special-casing in procfs.
838 */ 875 */
839 876
840static struct super_block *pipefs_get_sb(struct file_system_type *fs_type, 877static struct super_block *
841 int flags, const char *dev_name, void *data) 878pipefs_get_sb(struct file_system_type *fs_type, int flags,
879 const char *dev_name, void *data)
842{ 880{
843 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC); 881 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
844} 882}
@@ -852,6 +890,7 @@ static struct file_system_type pipe_fs_type = {
852static int __init init_pipe_fs(void) 890static int __init init_pipe_fs(void)
853{ 891{
854 int err = register_filesystem(&pipe_fs_type); 892 int err = register_filesystem(&pipe_fs_type);
893
855 if (!err) { 894 if (!err) {
856 pipe_mnt = kern_mount(&pipe_fs_type); 895 pipe_mnt = kern_mount(&pipe_fs_type);
857 if (IS_ERR(pipe_mnt)) { 896 if (IS_ERR(pipe_mnt)) {