1 /*
2 * linux/fs/pipe.c
3 *
4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
5 */
6
7 #include <linux/mm.h>
8 #include <linux/file.h>
9 #include <linux/poll.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/fs.h>
14 #include <linux/log2.h>
15 #include <linux/mount.h>
16 #include <linux/magic.h>
17 #include <linux/pipe_fs_i.h>
18 #include <linux/uio.h>
19 #include <linux/highmem.h>
20 #include <linux/pagemap.h>
21 #include <linux/audit.h>
22 #include <linux/syscalls.h>
23 #include <linux/fcntl.h>
24
25 #include <asm/uaccess.h>
26 #include <asm/ioctls.h>
27
28 /*
29 * The max size that a non-root user is allowed to grow the pipe. Can
30 * be set by root in /proc/sys/fs/pipe-max-size
31 */
32 unsigned int pipe_max_size = 1048576;
33
34 /*
35 * Minimum pipe size, as required by POSIX
36 */
37 unsigned int pipe_min_size = PAGE_SIZE;
38
39 /*
40 * We use a start+len construction, which provides full use of the
41 * allocated memory.
42 * -- Florian Coosmann (FGC)
43 *
44 * Reads with count = 0 should always return 0.
45 * -- Julian Bradfield 1999-06-07.
46 *
47 * FIFOs and Pipes now generate SIGIO for both readers and writers.
48 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
49 *
50 * pipe_read & write cleanup
51 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
52 */
53
pipe_lock_nested(struct pipe_inode_info * pipe,int subclass)54 static void pipe_lock_nested(struct pipe_inode_info *pipe, int subclass)
55 {
56 if (pipe->inode)
57 mutex_lock_nested(&pipe->inode->i_mutex, subclass);
58 }
59
pipe_lock(struct pipe_inode_info * pipe)60 void pipe_lock(struct pipe_inode_info *pipe)
61 {
62 /*
63 * pipe_lock() nests non-pipe inode locks (for writing to a file)
64 */
65 pipe_lock_nested(pipe, I_MUTEX_PARENT);
66 }
67 EXPORT_SYMBOL(pipe_lock);
68
pipe_unlock(struct pipe_inode_info * pipe)69 void pipe_unlock(struct pipe_inode_info *pipe)
70 {
71 if (pipe->inode)
72 mutex_unlock(&pipe->inode->i_mutex);
73 }
74 EXPORT_SYMBOL(pipe_unlock);
75
pipe_double_lock(struct pipe_inode_info * pipe1,struct pipe_inode_info * pipe2)76 void pipe_double_lock(struct pipe_inode_info *pipe1,
77 struct pipe_inode_info *pipe2)
78 {
79 BUG_ON(pipe1 == pipe2);
80
81 if (pipe1 < pipe2) {
82 pipe_lock_nested(pipe1, I_MUTEX_PARENT);
83 pipe_lock_nested(pipe2, I_MUTEX_CHILD);
84 } else {
85 pipe_lock_nested(pipe2, I_MUTEX_PARENT);
86 pipe_lock_nested(pipe1, I_MUTEX_CHILD);
87 }
88 }
89
90 /* Drop the inode semaphore and wait for a pipe event, atomically */
pipe_wait(struct pipe_inode_info * pipe)91 void pipe_wait(struct pipe_inode_info *pipe)
92 {
93 DEFINE_WAIT(wait);
94
95 /*
96 * Pipes are system-local resources, so sleeping on them
97 * is considered a noninteractive wait:
98 */
99 prepare_to_wait(&pipe->wait, &wait, TASK_INTERRUPTIBLE);
100 pipe_unlock(pipe);
101 schedule();
102 finish_wait(&pipe->wait, &wait);
103 pipe_lock(pipe);
104 }
105
106 static int
pipe_iov_copy_from_user(void * addr,int * offset,struct iovec * iov,size_t * remaining,int atomic)107 pipe_iov_copy_from_user(void *addr, int *offset, struct iovec *iov,
108 size_t *remaining, int atomic)
109 {
110 unsigned long copy;
111
112 while (*remaining > 0) {
113 while (!iov->iov_len)
114 iov++;
115 copy = min_t(unsigned long, *remaining, iov->iov_len);
116
117 if (atomic) {
118 if (__copy_from_user_inatomic(addr + *offset,
119 iov->iov_base, copy))
120 return -EFAULT;
121 } else {
122 if (copy_from_user(addr + *offset,
123 iov->iov_base, copy))
124 return -EFAULT;
125 }
126 *offset += copy;
127 *remaining -= copy;
128 iov->iov_base += copy;
129 iov->iov_len -= copy;
130 }
131 return 0;
132 }
133
134 static int
pipe_iov_copy_to_user(struct iovec * iov,void * addr,int * offset,size_t * remaining,int atomic)135 pipe_iov_copy_to_user(struct iovec *iov, void *addr, int *offset,
136 size_t *remaining, int atomic)
137 {
138 unsigned long copy;
139
140 while (*remaining > 0) {
141 while (!iov->iov_len)
142 iov++;
143 copy = min_t(unsigned long, *remaining, iov->iov_len);
144
145 if (atomic) {
146 if (__copy_to_user_inatomic(iov->iov_base,
147 addr + *offset, copy))
148 return -EFAULT;
149 } else {
150 if (copy_to_user(iov->iov_base,
151 addr + *offset, copy))
152 return -EFAULT;
153 }
154 *offset += copy;
155 *remaining -= copy;
156 iov->iov_base += copy;
157 iov->iov_len -= copy;
158 }
159 return 0;
160 }
161
162 /*
163 * Attempt to pre-fault in the user memory, so we can use atomic copies.
164 * Returns the number of bytes not faulted in.
165 */
iov_fault_in_pages_write(struct iovec * iov,unsigned long len)166 static int iov_fault_in_pages_write(struct iovec *iov, unsigned long len)
167 {
168 while (!iov->iov_len)
169 iov++;
170
171 while (len > 0) {
172 unsigned long this_len;
173
174 this_len = min_t(unsigned long, len, iov->iov_len);
175 if (fault_in_pages_writeable(iov->iov_base, this_len))
176 break;
177
178 len -= this_len;
179 iov++;
180 }
181
182 return len;
183 }
184
185 /*
186 * Pre-fault in the user memory, so we can use atomic copies.
187 */
iov_fault_in_pages_read(struct iovec * iov,unsigned long len)188 static void iov_fault_in_pages_read(struct iovec *iov, unsigned long len)
189 {
190 while (!iov->iov_len)
191 iov++;
192
193 while (len > 0) {
194 unsigned long this_len;
195
196 this_len = min_t(unsigned long, len, iov->iov_len);
197 fault_in_pages_readable(iov->iov_base, this_len);
198 len -= this_len;
199 iov++;
200 }
201 }
202
anon_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)203 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
204 struct pipe_buffer *buf)
205 {
206 struct page *page = buf->page;
207
208 /*
209 * If nobody else uses this page, and we don't already have a
210 * temporary page, let's keep track of it as a one-deep
211 * allocation cache. (Otherwise just release our reference to it)
212 */
213 if (page_count(page) == 1 && !pipe->tmp_page)
214 pipe->tmp_page = page;
215 else
216 page_cache_release(page);
217 }
218
219 /**
220 * generic_pipe_buf_map - virtually map a pipe buffer
221 * @pipe: the pipe that the buffer belongs to
222 * @buf: the buffer that should be mapped
223 * @atomic: whether to use an atomic map
224 *
225 * Description:
226 * This function returns a kernel virtual address mapping for the
227 * pipe_buffer passed in @buf. If @atomic is set, an atomic map is provided
228 * and the caller has to be careful not to fault before calling
229 * the unmap function.
230 *
231 * Note that this function occupies KM_USER0 if @atomic != 0.
232 */
generic_pipe_buf_map(struct pipe_inode_info * pipe,struct pipe_buffer * buf,int atomic)233 void *generic_pipe_buf_map(struct pipe_inode_info *pipe,
234 struct pipe_buffer *buf, int atomic)
235 {
236 if (atomic) {
237 buf->flags |= PIPE_BUF_FLAG_ATOMIC;
238 return kmap_atomic(buf->page);
239 }
240
241 return kmap(buf->page);
242 }
243 EXPORT_SYMBOL(generic_pipe_buf_map);
244
245 /**
246 * generic_pipe_buf_unmap - unmap a previously mapped pipe buffer
247 * @pipe: the pipe that the buffer belongs to
248 * @buf: the buffer that should be unmapped
249 * @map_data: the data that the mapping function returned
250 *
251 * Description:
252 * This function undoes the mapping that ->map() provided.
253 */
generic_pipe_buf_unmap(struct pipe_inode_info * pipe,struct pipe_buffer * buf,void * map_data)254 void generic_pipe_buf_unmap(struct pipe_inode_info *pipe,
255 struct pipe_buffer *buf, void *map_data)
256 {
257 if (buf->flags & PIPE_BUF_FLAG_ATOMIC) {
258 buf->flags &= ~PIPE_BUF_FLAG_ATOMIC;
259 kunmap_atomic(map_data);
260 } else
261 kunmap(buf->page);
262 }
263 EXPORT_SYMBOL(generic_pipe_buf_unmap);
264
265 /**
266 * generic_pipe_buf_steal - attempt to take ownership of a &pipe_buffer
267 * @pipe: the pipe that the buffer belongs to
268 * @buf: the buffer to attempt to steal
269 *
270 * Description:
271 * This function attempts to steal the &struct page attached to
272 * @buf. If successful, this function returns 0 and returns with
273 * the page locked. The caller may then reuse the page for whatever
274 * he wishes; the typical use is insertion into a different file
275 * page cache.
276 */
generic_pipe_buf_steal(struct pipe_inode_info * pipe,struct pipe_buffer * buf)277 int generic_pipe_buf_steal(struct pipe_inode_info *pipe,
278 struct pipe_buffer *buf)
279 {
280 struct page *page = buf->page;
281
282 /*
283 * A reference of one is golden, that means that the owner of this
284 * page is the only one holding a reference to it. lock the page
285 * and return OK.
286 */
287 if (page_count(page) == 1) {
288 lock_page(page);
289 return 0;
290 }
291
292 return 1;
293 }
294 EXPORT_SYMBOL(generic_pipe_buf_steal);
295
296 /**
297 * generic_pipe_buf_get - get a reference to a &struct pipe_buffer
298 * @pipe: the pipe that the buffer belongs to
299 * @buf: the buffer to get a reference to
300 *
301 * Description:
302 * This function grabs an extra reference to @buf. It's used in
303 * in the tee() system call, when we duplicate the buffers in one
304 * pipe into another.
305 */
generic_pipe_buf_get(struct pipe_inode_info * pipe,struct pipe_buffer * buf)306 void generic_pipe_buf_get(struct pipe_inode_info *pipe, struct pipe_buffer *buf)
307 {
308 page_cache_get(buf->page);
309 }
310 EXPORT_SYMBOL(generic_pipe_buf_get);
311
312 /**
313 * generic_pipe_buf_confirm - verify contents of the pipe buffer
314 * @info: the pipe that the buffer belongs to
315 * @buf: the buffer to confirm
316 *
317 * Description:
318 * This function does nothing, because the generic pipe code uses
319 * pages that are always good when inserted into the pipe.
320 */
generic_pipe_buf_confirm(struct pipe_inode_info * info,struct pipe_buffer * buf)321 int generic_pipe_buf_confirm(struct pipe_inode_info *info,
322 struct pipe_buffer *buf)
323 {
324 return 0;
325 }
326 EXPORT_SYMBOL(generic_pipe_buf_confirm);
327
328 /**
329 * generic_pipe_buf_release - put a reference to a &struct pipe_buffer
330 * @pipe: the pipe that the buffer belongs to
331 * @buf: the buffer to put a reference to
332 *
333 * Description:
334 * This function releases a reference to @buf.
335 */
generic_pipe_buf_release(struct pipe_inode_info * pipe,struct pipe_buffer * buf)336 void generic_pipe_buf_release(struct pipe_inode_info *pipe,
337 struct pipe_buffer *buf)
338 {
339 page_cache_release(buf->page);
340 }
341 EXPORT_SYMBOL(generic_pipe_buf_release);
342
343 static const struct pipe_buf_operations anon_pipe_buf_ops = {
344 .can_merge = 1,
345 .map = generic_pipe_buf_map,
346 .unmap = generic_pipe_buf_unmap,
347 .confirm = generic_pipe_buf_confirm,
348 .release = anon_pipe_buf_release,
349 .steal = generic_pipe_buf_steal,
350 .get = generic_pipe_buf_get,
351 };
352
353 static const struct pipe_buf_operations packet_pipe_buf_ops = {
354 .can_merge = 0,
355 .map = generic_pipe_buf_map,
356 .unmap = generic_pipe_buf_unmap,
357 .confirm = generic_pipe_buf_confirm,
358 .release = anon_pipe_buf_release,
359 .steal = generic_pipe_buf_steal,
360 .get = generic_pipe_buf_get,
361 };
362
363 static ssize_t
pipe_read(struct kiocb * iocb,const struct iovec * _iov,unsigned long nr_segs,loff_t pos)364 pipe_read(struct kiocb *iocb, const struct iovec *_iov,
365 unsigned long nr_segs, loff_t pos)
366 {
367 struct file *filp = iocb->ki_filp;
368 struct inode *inode = filp->f_path.dentry->d_inode;
369 struct pipe_inode_info *pipe;
370 int do_wakeup;
371 ssize_t ret;
372 struct iovec *iov = (struct iovec *)_iov;
373 size_t total_len;
374
375 total_len = iov_length(iov, nr_segs);
376 /* Null read succeeds. */
377 if (unlikely(total_len == 0))
378 return 0;
379
380 do_wakeup = 0;
381 ret = 0;
382 mutex_lock(&inode->i_mutex);
383 pipe = inode->i_pipe;
384 for (;;) {
385 int bufs = pipe->nrbufs;
386 if (bufs) {
387 int curbuf = pipe->curbuf;
388 struct pipe_buffer *buf = pipe->bufs + curbuf;
389 const struct pipe_buf_operations *ops = buf->ops;
390 void *addr;
391 size_t chars = buf->len, remaining;
392 int error, atomic;
393 int offset;
394
395 if (chars > total_len)
396 chars = total_len;
397
398 error = ops->confirm(pipe, buf);
399 if (error) {
400 if (!ret)
401 ret = error;
402 break;
403 }
404
405 atomic = !iov_fault_in_pages_write(iov, chars);
406 remaining = chars;
407 offset = buf->offset;
408 redo:
409 addr = ops->map(pipe, buf, atomic);
410 error = pipe_iov_copy_to_user(iov, addr, &offset,
411 &remaining, atomic);
412 ops->unmap(pipe, buf, addr);
413 if (unlikely(error)) {
414 /*
415 * Just retry with the slow path if we failed.
416 */
417 if (atomic) {
418 atomic = 0;
419 goto redo;
420 }
421 if (!ret)
422 ret = error;
423 break;
424 }
425 ret += chars;
426 buf->offset += chars;
427 buf->len -= chars;
428
429 /* Was it a packet buffer? Clean up and exit */
430 if (buf->flags & PIPE_BUF_FLAG_PACKET) {
431 total_len = chars;
432 buf->len = 0;
433 }
434
435 if (!buf->len) {
436 buf->ops = NULL;
437 ops->release(pipe, buf);
438 curbuf = (curbuf + 1) & (pipe->buffers - 1);
439 pipe->curbuf = curbuf;
440 pipe->nrbufs = --bufs;
441 do_wakeup = 1;
442 }
443 total_len -= chars;
444 if (!total_len)
445 break; /* common path: read succeeded */
446 }
447 if (bufs) /* More to do? */
448 continue;
449 if (!pipe->writers)
450 break;
451 if (!pipe->waiting_writers) {
452 /* syscall merging: Usually we must not sleep
453 * if O_NONBLOCK is set, or if we got some data.
454 * But if a writer sleeps in kernel space, then
455 * we can wait for that data without violating POSIX.
456 */
457 if (ret)
458 break;
459 if (filp->f_flags & O_NONBLOCK) {
460 ret = -EAGAIN;
461 break;
462 }
463 }
464 if (signal_pending(current)) {
465 if (!ret)
466 ret = -ERESTARTSYS;
467 break;
468 }
469 if (do_wakeup) {
470 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
471 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
472 }
473 pipe_wait(pipe);
474 }
475 mutex_unlock(&inode->i_mutex);
476
477 /* Signal writers asynchronously that there is more room. */
478 if (do_wakeup) {
479 wake_up_interruptible_sync_poll(&pipe->wait, POLLOUT | POLLWRNORM);
480 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
481 }
482 if (ret > 0)
483 file_accessed(filp);
484 return ret;
485 }
486
is_packetized(struct file * file)487 static inline int is_packetized(struct file *file)
488 {
489 return (file->f_flags & O_DIRECT) != 0;
490 }
491
492 static ssize_t
pipe_write(struct kiocb * iocb,const struct iovec * _iov,unsigned long nr_segs,loff_t ppos)493 pipe_write(struct kiocb *iocb, const struct iovec *_iov,
494 unsigned long nr_segs, loff_t ppos)
495 {
496 struct file *filp = iocb->ki_filp;
497 struct inode *inode = filp->f_path.dentry->d_inode;
498 struct pipe_inode_info *pipe;
499 ssize_t ret;
500 int do_wakeup;
501 struct iovec *iov = (struct iovec *)_iov;
502 size_t total_len;
503 ssize_t chars;
504
505 total_len = iov_length(iov, nr_segs);
506 /* Null write succeeds. */
507 if (unlikely(total_len == 0))
508 return 0;
509
510 do_wakeup = 0;
511 ret = 0;
512 mutex_lock(&inode->i_mutex);
513 pipe = inode->i_pipe;
514
515 if (!pipe->readers) {
516 send_sig(SIGPIPE, current, 0);
517 ret = -EPIPE;
518 goto out;
519 }
520
521 /* We try to merge small writes */
522 chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
523 if (pipe->nrbufs && chars != 0) {
524 int lastbuf = (pipe->curbuf + pipe->nrbufs - 1) &
525 (pipe->buffers - 1);
526 struct pipe_buffer *buf = pipe->bufs + lastbuf;
527 const struct pipe_buf_operations *ops = buf->ops;
528 int offset = buf->offset + buf->len;
529
530 if (ops->can_merge && offset + chars <= PAGE_SIZE) {
531 int error, atomic = 1;
532 void *addr;
533 size_t remaining = chars;
534
535 error = ops->confirm(pipe, buf);
536 if (error)
537 goto out;
538
539 iov_fault_in_pages_read(iov, chars);
540 redo1:
541 addr = ops->map(pipe, buf, atomic);
542 error = pipe_iov_copy_from_user(addr, &offset, iov,
543 &remaining, atomic);
544 ops->unmap(pipe, buf, addr);
545 ret = error;
546 do_wakeup = 1;
547 if (error) {
548 if (atomic) {
549 atomic = 0;
550 goto redo1;
551 }
552 goto out;
553 }
554 buf->len += chars;
555 total_len -= chars;
556 ret = chars;
557 if (!total_len)
558 goto out;
559 }
560 }
561
562 for (;;) {
563 int bufs;
564
565 if (!pipe->readers) {
566 send_sig(SIGPIPE, current, 0);
567 if (!ret)
568 ret = -EPIPE;
569 break;
570 }
571 bufs = pipe->nrbufs;
572 if (bufs < pipe->buffers) {
573 int newbuf = (pipe->curbuf + bufs) & (pipe->buffers-1);
574 struct pipe_buffer *buf = pipe->bufs + newbuf;
575 struct page *page = pipe->tmp_page;
576 char *src;
577 int error, atomic = 1;
578 int offset = 0;
579 size_t remaining;
580
581 if (!page) {
582 page = alloc_page(GFP_HIGHUSER);
583 if (unlikely(!page)) {
584 ret = ret ? : -ENOMEM;
585 break;
586 }
587 pipe->tmp_page = page;
588 }
589 /* Always wake up, even if the copy fails. Otherwise
590 * we lock up (O_NONBLOCK-)readers that sleep due to
591 * syscall merging.
592 * FIXME! Is this really true?
593 */
594 do_wakeup = 1;
595 chars = PAGE_SIZE;
596 if (chars > total_len)
597 chars = total_len;
598
599 iov_fault_in_pages_read(iov, chars);
600 remaining = chars;
601 redo2:
602 if (atomic)
603 src = kmap_atomic(page);
604 else
605 src = kmap(page);
606
607 error = pipe_iov_copy_from_user(src, &offset, iov,
608 &remaining, atomic);
609 if (atomic)
610 kunmap_atomic(src);
611 else
612 kunmap(page);
613
614 if (unlikely(error)) {
615 if (atomic) {
616 atomic = 0;
617 goto redo2;
618 }
619 if (!ret)
620 ret = error;
621 break;
622 }
623 ret += chars;
624
625 /* Insert it into the buffer array */
626 buf->page = page;
627 buf->ops = &anon_pipe_buf_ops;
628 buf->offset = 0;
629 buf->len = chars;
630 buf->flags = 0;
631 if (is_packetized(filp)) {
632 buf->ops = &packet_pipe_buf_ops;
633 buf->flags = PIPE_BUF_FLAG_PACKET;
634 }
635 pipe->nrbufs = ++bufs;
636 pipe->tmp_page = NULL;
637
638 total_len -= chars;
639 if (!total_len)
640 break;
641 }
642 if (bufs < pipe->buffers)
643 continue;
644 if (filp->f_flags & O_NONBLOCK) {
645 if (!ret)
646 ret = -EAGAIN;
647 break;
648 }
649 if (signal_pending(current)) {
650 if (!ret)
651 ret = -ERESTARTSYS;
652 break;
653 }
654 if (do_wakeup) {
655 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
656 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
657 do_wakeup = 0;
658 }
659 pipe->waiting_writers++;
660 pipe_wait(pipe);
661 pipe->waiting_writers--;
662 }
663 out:
664 mutex_unlock(&inode->i_mutex);
665 if (do_wakeup) {
666 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLRDNORM);
667 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
668 }
669 if (ret > 0) {
670 int err = file_update_time(filp);
671 if (err)
672 ret = err;
673 }
674 return ret;
675 }
676
677 static ssize_t
bad_pipe_r(struct file * filp,char __user * buf,size_t count,loff_t * ppos)678 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
679 {
680 return -EBADF;
681 }
682
683 static ssize_t
bad_pipe_w(struct file * filp,const char __user * buf,size_t count,loff_t * ppos)684 bad_pipe_w(struct file *filp, const char __user *buf, size_t count,
685 loff_t *ppos)
686 {
687 return -EBADF;
688 }
689
pipe_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)690 static long pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
691 {
692 struct inode *inode = filp->f_path.dentry->d_inode;
693 struct pipe_inode_info *pipe;
694 int count, buf, nrbufs;
695
696 switch (cmd) {
697 case FIONREAD:
698 mutex_lock(&inode->i_mutex);
699 pipe = inode->i_pipe;
700 count = 0;
701 buf = pipe->curbuf;
702 nrbufs = pipe->nrbufs;
703 while (--nrbufs >= 0) {
704 count += pipe->bufs[buf].len;
705 buf = (buf+1) & (pipe->buffers - 1);
706 }
707 mutex_unlock(&inode->i_mutex);
708
709 return put_user(count, (int __user *)arg);
710 default:
711 return -EINVAL;
712 }
713 }
714
715 /* No kernel lock held - fine */
716 static unsigned int
pipe_poll(struct file * filp,poll_table * wait)717 pipe_poll(struct file *filp, poll_table *wait)
718 {
719 unsigned int mask;
720 struct inode *inode = filp->f_path.dentry->d_inode;
721 struct pipe_inode_info *pipe = inode->i_pipe;
722 int nrbufs;
723
724 poll_wait(filp, &pipe->wait, wait);
725
726 /* Reading only -- no need for acquiring the semaphore. */
727 nrbufs = pipe->nrbufs;
728 mask = 0;
729 if (filp->f_mode & FMODE_READ) {
730 mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
731 if (!pipe->writers && filp->f_version != pipe->w_counter)
732 mask |= POLLHUP;
733 }
734
735 if (filp->f_mode & FMODE_WRITE) {
736 mask |= (nrbufs < pipe->buffers) ? POLLOUT | POLLWRNORM : 0;
737 /*
738 * Most Unices do not set POLLERR for FIFOs but on Linux they
739 * behave exactly like pipes for poll().
740 */
741 if (!pipe->readers)
742 mask |= POLLERR;
743 }
744
745 return mask;
746 }
747
748 static int
pipe_release(struct inode * inode,int decr,int decw)749 pipe_release(struct inode *inode, int decr, int decw)
750 {
751 struct pipe_inode_info *pipe;
752
753 mutex_lock(&inode->i_mutex);
754 pipe = inode->i_pipe;
755 pipe->readers -= decr;
756 pipe->writers -= decw;
757
758 if (!pipe->readers && !pipe->writers) {
759 free_pipe_info(inode);
760 } else {
761 wake_up_interruptible_sync_poll(&pipe->wait, POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM | POLLERR | POLLHUP);
762 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
763 kill_fasync(&pipe->fasync_writers, SIGIO, POLL_OUT);
764 }
765 mutex_unlock(&inode->i_mutex);
766
767 return 0;
768 }
769
770 static int
pipe_read_fasync(int fd,struct file * filp,int on)771 pipe_read_fasync(int fd, struct file *filp, int on)
772 {
773 struct inode *inode = filp->f_path.dentry->d_inode;
774 int retval;
775
776 mutex_lock(&inode->i_mutex);
777 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_readers);
778 mutex_unlock(&inode->i_mutex);
779
780 return retval;
781 }
782
783
784 static int
pipe_write_fasync(int fd,struct file * filp,int on)785 pipe_write_fasync(int fd, struct file *filp, int on)
786 {
787 struct inode *inode = filp->f_path.dentry->d_inode;
788 int retval;
789
790 mutex_lock(&inode->i_mutex);
791 retval = fasync_helper(fd, filp, on, &inode->i_pipe->fasync_writers);
792 mutex_unlock(&inode->i_mutex);
793
794 return retval;
795 }
796
797
798 static int
pipe_rdwr_fasync(int fd,struct file * filp,int on)799 pipe_rdwr_fasync(int fd, struct file *filp, int on)
800 {
801 struct inode *inode = filp->f_path.dentry->d_inode;
802 struct pipe_inode_info *pipe = inode->i_pipe;
803 int retval;
804
805 mutex_lock(&inode->i_mutex);
806 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
807 if (retval >= 0) {
808 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
809 if (retval < 0) /* this can happen only if on == T */
810 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
811 }
812 mutex_unlock(&inode->i_mutex);
813 return retval;
814 }
815
816
817 static int
pipe_read_release(struct inode * inode,struct file * filp)818 pipe_read_release(struct inode *inode, struct file *filp)
819 {
820 return pipe_release(inode, 1, 0);
821 }
822
823 static int
pipe_write_release(struct inode * inode,struct file * filp)824 pipe_write_release(struct inode *inode, struct file *filp)
825 {
826 return pipe_release(inode, 0, 1);
827 }
828
829 static int
pipe_rdwr_release(struct inode * inode,struct file * filp)830 pipe_rdwr_release(struct inode *inode, struct file *filp)
831 {
832 int decr, decw;
833
834 decr = (filp->f_mode & FMODE_READ) != 0;
835 decw = (filp->f_mode & FMODE_WRITE) != 0;
836 return pipe_release(inode, decr, decw);
837 }
838
839 static int
pipe_read_open(struct inode * inode,struct file * filp)840 pipe_read_open(struct inode *inode, struct file *filp)
841 {
842 int ret = -ENOENT;
843
844 mutex_lock(&inode->i_mutex);
845
846 if (inode->i_pipe) {
847 ret = 0;
848 inode->i_pipe->readers++;
849 }
850
851 mutex_unlock(&inode->i_mutex);
852
853 return ret;
854 }
855
856 static int
pipe_write_open(struct inode * inode,struct file * filp)857 pipe_write_open(struct inode *inode, struct file *filp)
858 {
859 int ret = -ENOENT;
860
861 mutex_lock(&inode->i_mutex);
862
863 if (inode->i_pipe) {
864 ret = 0;
865 inode->i_pipe->writers++;
866 }
867
868 mutex_unlock(&inode->i_mutex);
869
870 return ret;
871 }
872
873 static int
pipe_rdwr_open(struct inode * inode,struct file * filp)874 pipe_rdwr_open(struct inode *inode, struct file *filp)
875 {
876 int ret = -ENOENT;
877
878 if (!(filp->f_mode & (FMODE_READ|FMODE_WRITE)))
879 return -EINVAL;
880
881 mutex_lock(&inode->i_mutex);
882
883 if (inode->i_pipe) {
884 ret = 0;
885 if (filp->f_mode & FMODE_READ)
886 inode->i_pipe->readers++;
887 if (filp->f_mode & FMODE_WRITE)
888 inode->i_pipe->writers++;
889 }
890
891 mutex_unlock(&inode->i_mutex);
892
893 return ret;
894 }
895
896 /*
897 * The file_operations structs are not static because they
898 * are also used in linux/fs/fifo.c to do operations on FIFOs.
899 *
900 * Pipes reuse fifos' file_operations structs.
901 */
902 const struct file_operations read_pipefifo_fops = {
903 .llseek = no_llseek,
904 .read = do_sync_read,
905 .aio_read = pipe_read,
906 .write = bad_pipe_w,
907 .poll = pipe_poll,
908 .unlocked_ioctl = pipe_ioctl,
909 .open = pipe_read_open,
910 .release = pipe_read_release,
911 .fasync = pipe_read_fasync,
912 };
913
914 const struct file_operations write_pipefifo_fops = {
915 .llseek = no_llseek,
916 .read = bad_pipe_r,
917 .write = do_sync_write,
918 .aio_write = pipe_write,
919 .poll = pipe_poll,
920 .unlocked_ioctl = pipe_ioctl,
921 .open = pipe_write_open,
922 .release = pipe_write_release,
923 .fasync = pipe_write_fasync,
924 };
925
926 const struct file_operations rdwr_pipefifo_fops = {
927 .llseek = no_llseek,
928 .read = do_sync_read,
929 .aio_read = pipe_read,
930 .write = do_sync_write,
931 .aio_write = pipe_write,
932 .poll = pipe_poll,
933 .unlocked_ioctl = pipe_ioctl,
934 .open = pipe_rdwr_open,
935 .release = pipe_rdwr_release,
936 .fasync = pipe_rdwr_fasync,
937 };
938
alloc_pipe_info(struct inode * inode)939 struct pipe_inode_info * alloc_pipe_info(struct inode *inode)
940 {
941 struct pipe_inode_info *pipe;
942
943 pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
944 if (pipe) {
945 pipe->bufs = kzalloc(sizeof(struct pipe_buffer) * PIPE_DEF_BUFFERS, GFP_KERNEL);
946 if (pipe->bufs) {
947 init_waitqueue_head(&pipe->wait);
948 pipe->r_counter = pipe->w_counter = 1;
949 pipe->inode = inode;
950 pipe->buffers = PIPE_DEF_BUFFERS;
951 return pipe;
952 }
953 kfree(pipe);
954 }
955
956 return NULL;
957 }
958
__free_pipe_info(struct pipe_inode_info * pipe)959 void __free_pipe_info(struct pipe_inode_info *pipe)
960 {
961 int i;
962
963 for (i = 0; i < pipe->buffers; i++) {
964 struct pipe_buffer *buf = pipe->bufs + i;
965 if (buf->ops)
966 buf->ops->release(pipe, buf);
967 }
968 if (pipe->tmp_page)
969 __free_page(pipe->tmp_page);
970 kfree(pipe->bufs);
971 kfree(pipe);
972 }
973
free_pipe_info(struct inode * inode)974 void free_pipe_info(struct inode *inode)
975 {
976 __free_pipe_info(inode->i_pipe);
977 inode->i_pipe = NULL;
978 }
979
980 static struct vfsmount *pipe_mnt __read_mostly;
981
982 /*
983 * pipefs_dname() is called from d_path().
984 */
pipefs_dname(struct dentry * dentry,char * buffer,int buflen)985 static char *pipefs_dname(struct dentry *dentry, char *buffer, int buflen)
986 {
987 return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
988 dentry->d_inode->i_ino);
989 }
990
991 static const struct dentry_operations pipefs_dentry_operations = {
992 .d_dname = pipefs_dname,
993 };
994
get_pipe_inode(void)995 static struct inode * get_pipe_inode(void)
996 {
997 struct inode *inode = new_inode_pseudo(pipe_mnt->mnt_sb);
998 struct pipe_inode_info *pipe;
999
1000 if (!inode)
1001 goto fail_inode;
1002
1003 inode->i_ino = get_next_ino();
1004
1005 pipe = alloc_pipe_info(inode);
1006 if (!pipe)
1007 goto fail_iput;
1008 inode->i_pipe = pipe;
1009
1010 pipe->readers = pipe->writers = 1;
1011 inode->i_fop = &rdwr_pipefifo_fops;
1012
1013 /*
1014 * Mark the inode dirty from the very beginning,
1015 * that way it will never be moved to the dirty
1016 * list because "mark_inode_dirty()" will think
1017 * that it already _is_ on the dirty list.
1018 */
1019 inode->i_state = I_DIRTY;
1020 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
1021 inode->i_uid = current_fsuid();
1022 inode->i_gid = current_fsgid();
1023 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1024
1025 return inode;
1026
1027 fail_iput:
1028 iput(inode);
1029
1030 fail_inode:
1031 return NULL;
1032 }
1033
create_write_pipe(int flags)1034 struct file *create_write_pipe(int flags)
1035 {
1036 int err;
1037 struct inode *inode;
1038 struct file *f;
1039 struct path path;
1040 struct qstr name = { .name = "" };
1041
1042 err = -ENFILE;
1043 inode = get_pipe_inode();
1044 if (!inode)
1045 goto err;
1046
1047 err = -ENOMEM;
1048 path.dentry = d_alloc_pseudo(pipe_mnt->mnt_sb, &name);
1049 if (!path.dentry)
1050 goto err_inode;
1051 path.mnt = mntget(pipe_mnt);
1052
1053 d_instantiate(path.dentry, inode);
1054
1055 err = -ENFILE;
1056 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
1057 if (!f)
1058 goto err_dentry;
1059 f->f_mapping = inode->i_mapping;
1060
1061 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
1062 f->f_version = 0;
1063
1064 return f;
1065
1066 err_dentry:
1067 free_pipe_info(inode);
1068 path_put(&path);
1069 return ERR_PTR(err);
1070
1071 err_inode:
1072 free_pipe_info(inode);
1073 iput(inode);
1074 err:
1075 return ERR_PTR(err);
1076 }
1077
free_write_pipe(struct file * f)1078 void free_write_pipe(struct file *f)
1079 {
1080 free_pipe_info(f->f_dentry->d_inode);
1081 path_put(&f->f_path);
1082 put_filp(f);
1083 }
1084
create_read_pipe(struct file * wrf,int flags)1085 struct file *create_read_pipe(struct file *wrf, int flags)
1086 {
1087 /* Grab pipe from the writer */
1088 struct file *f = alloc_file(&wrf->f_path, FMODE_READ,
1089 &read_pipefifo_fops);
1090 if (!f)
1091 return ERR_PTR(-ENFILE);
1092
1093 path_get(&wrf->f_path);
1094 f->f_flags = O_RDONLY | (flags & O_NONBLOCK);
1095
1096 return f;
1097 }
1098
do_pipe_flags(int * fd,int flags)1099 int do_pipe_flags(int *fd, int flags)
1100 {
1101 struct file *fw, *fr;
1102 int error;
1103 int fdw, fdr;
1104
1105 if (flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT))
1106 return -EINVAL;
1107
1108 fw = create_write_pipe(flags);
1109 if (IS_ERR(fw))
1110 return PTR_ERR(fw);
1111 fr = create_read_pipe(fw, flags);
1112 error = PTR_ERR(fr);
1113 if (IS_ERR(fr))
1114 goto err_write_pipe;
1115
1116 error = get_unused_fd_flags(flags);
1117 if (error < 0)
1118 goto err_read_pipe;
1119 fdr = error;
1120
1121 error = get_unused_fd_flags(flags);
1122 if (error < 0)
1123 goto err_fdr;
1124 fdw = error;
1125
1126 audit_fd_pair(fdr, fdw);
1127 fd_install(fdr, fr);
1128 fd_install(fdw, fw);
1129 fd[0] = fdr;
1130 fd[1] = fdw;
1131
1132 return 0;
1133
1134 err_fdr:
1135 put_unused_fd(fdr);
1136 err_read_pipe:
1137 path_put(&fr->f_path);
1138 put_filp(fr);
1139 err_write_pipe:
1140 free_write_pipe(fw);
1141 return error;
1142 }
1143
1144 /*
1145 * sys_pipe() is the normal C calling standard for creating
1146 * a pipe. It's not the way Unix traditionally does this, though.
1147 */
SYSCALL_DEFINE2(pipe2,int __user *,fildes,int,flags)1148 SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags)
1149 {
1150 int fd[2];
1151 int error;
1152
1153 error = do_pipe_flags(fd, flags);
1154 if (!error) {
1155 if (copy_to_user(fildes, fd, sizeof(fd))) {
1156 sys_close(fd[0]);
1157 sys_close(fd[1]);
1158 error = -EFAULT;
1159 }
1160 }
1161 return error;
1162 }
1163
SYSCALL_DEFINE1(pipe,int __user *,fildes)1164 SYSCALL_DEFINE1(pipe, int __user *, fildes)
1165 {
1166 return sys_pipe2(fildes, 0);
1167 }
1168
1169 /*
1170 * Allocate a new array of pipe buffers and copy the info over. Returns the
1171 * pipe size if successful, or return -ERROR on error.
1172 */
pipe_set_size(struct pipe_inode_info * pipe,unsigned long nr_pages)1173 static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long nr_pages)
1174 {
1175 struct pipe_buffer *bufs;
1176
1177 /*
1178 * We can shrink the pipe, if arg >= pipe->nrbufs. Since we don't
1179 * expect a lot of shrink+grow operations, just free and allocate
1180 * again like we would do for growing. If the pipe currently
1181 * contains more buffers than arg, then return busy.
1182 */
1183 if (nr_pages < pipe->nrbufs)
1184 return -EBUSY;
1185
1186 bufs = kcalloc(nr_pages, sizeof(*bufs), GFP_KERNEL | __GFP_NOWARN);
1187 if (unlikely(!bufs))
1188 return -ENOMEM;
1189
1190 /*
1191 * The pipe array wraps around, so just start the new one at zero
1192 * and adjust the indexes.
1193 */
1194 if (pipe->nrbufs) {
1195 unsigned int tail;
1196 unsigned int head;
1197
1198 tail = pipe->curbuf + pipe->nrbufs;
1199 if (tail < pipe->buffers)
1200 tail = 0;
1201 else
1202 tail &= (pipe->buffers - 1);
1203
1204 head = pipe->nrbufs - tail;
1205 if (head)
1206 memcpy(bufs, pipe->bufs + pipe->curbuf, head * sizeof(struct pipe_buffer));
1207 if (tail)
1208 memcpy(bufs + head, pipe->bufs, tail * sizeof(struct pipe_buffer));
1209 }
1210
1211 pipe->curbuf = 0;
1212 kfree(pipe->bufs);
1213 pipe->bufs = bufs;
1214 pipe->buffers = nr_pages;
1215 return nr_pages * PAGE_SIZE;
1216 }
1217
1218 /*
1219 * Currently we rely on the pipe array holding a power-of-2 number
1220 * of pages.
1221 */
round_pipe_size(unsigned int size)1222 static inline unsigned int round_pipe_size(unsigned int size)
1223 {
1224 unsigned long nr_pages;
1225
1226 nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1227 return roundup_pow_of_two(nr_pages) << PAGE_SHIFT;
1228 }
1229
1230 /*
1231 * This should work even if CONFIG_PROC_FS isn't set, as proc_dointvec_minmax
1232 * will return an error.
1233 */
pipe_proc_fn(struct ctl_table * table,int write,void __user * buf,size_t * lenp,loff_t * ppos)1234 int pipe_proc_fn(struct ctl_table *table, int write, void __user *buf,
1235 size_t *lenp, loff_t *ppos)
1236 {
1237 int ret;
1238
1239 ret = proc_dointvec_minmax(table, write, buf, lenp, ppos);
1240 if (ret < 0 || !write)
1241 return ret;
1242
1243 pipe_max_size = round_pipe_size(pipe_max_size);
1244 return ret;
1245 }
1246
1247 /*
1248 * After the inode slimming patch, i_pipe/i_bdev/i_cdev share the same
1249 * location, so checking ->i_pipe is not enough to verify that this is a
1250 * pipe.
1251 */
get_pipe_info(struct file * file)1252 struct pipe_inode_info *get_pipe_info(struct file *file)
1253 {
1254 struct inode *i = file->f_path.dentry->d_inode;
1255
1256 return S_ISFIFO(i->i_mode) ? i->i_pipe : NULL;
1257 }
1258
pipe_fcntl(struct file * file,unsigned int cmd,unsigned long arg)1259 long pipe_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
1260 {
1261 struct pipe_inode_info *pipe;
1262 long ret;
1263
1264 pipe = get_pipe_info(file);
1265 if (!pipe)
1266 return -EBADF;
1267
1268 mutex_lock(&pipe->inode->i_mutex);
1269
1270 switch (cmd) {
1271 case F_SETPIPE_SZ: {
1272 unsigned int size, nr_pages;
1273
1274 size = round_pipe_size(arg);
1275 nr_pages = size >> PAGE_SHIFT;
1276
1277 ret = -EINVAL;
1278 if (!nr_pages)
1279 goto out;
1280
1281 if (!capable(CAP_SYS_RESOURCE) && size > pipe_max_size) {
1282 ret = -EPERM;
1283 goto out;
1284 }
1285 ret = pipe_set_size(pipe, nr_pages);
1286 break;
1287 }
1288 case F_GETPIPE_SZ:
1289 ret = pipe->buffers * PAGE_SIZE;
1290 break;
1291 default:
1292 ret = -EINVAL;
1293 break;
1294 }
1295
1296 out:
1297 mutex_unlock(&pipe->inode->i_mutex);
1298 return ret;
1299 }
1300
1301 static const struct super_operations pipefs_ops = {
1302 .destroy_inode = free_inode_nonrcu,
1303 .statfs = simple_statfs,
1304 };
1305
1306 /*
1307 * pipefs should _never_ be mounted by userland - too much of security hassle,
1308 * no real gain from having the whole whorehouse mounted. So we don't need
1309 * any operations on the root directory. However, we need a non-trivial
1310 * d_name - pipe: will go nicely and kill the special-casing in procfs.
1311 */
pipefs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)1312 static struct dentry *pipefs_mount(struct file_system_type *fs_type,
1313 int flags, const char *dev_name, void *data)
1314 {
1315 return mount_pseudo(fs_type, "pipe:", &pipefs_ops,
1316 &pipefs_dentry_operations, PIPEFS_MAGIC);
1317 }
1318
1319 static struct file_system_type pipe_fs_type = {
1320 .name = "pipefs",
1321 .mount = pipefs_mount,
1322 .kill_sb = kill_anon_super,
1323 };
1324
init_pipe_fs(void)1325 static int __init init_pipe_fs(void)
1326 {
1327 int err = register_filesystem(&pipe_fs_type);
1328
1329 if (!err) {
1330 pipe_mnt = kern_mount(&pipe_fs_type);
1331 if (IS_ERR(pipe_mnt)) {
1332 err = PTR_ERR(pipe_mnt);
1333 unregister_filesystem(&pipe_fs_type);
1334 }
1335 }
1336 return err;
1337 }
1338
1339 fs_initcall(init_pipe_fs);
1340