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/mount.h>
15 : #include <linux/pipe_fs_i.h>
16 : #include <linux/uio.h>
17 : #include <linux/highmem.h>
18 :
19 : #include <asm/uaccess.h>
20 : #include <asm/ioctls.h>
21 :
22 : /*
23 : * We use a start+len construction, which provides full use of the
24 : * allocated memory.
25 : * -- Florian Coosmann (FGC)
26 : *
27 : * Reads with count = 0 should always return 0.
28 : * -- Julian Bradfield 1999-06-07.
29 : *
30 : * FIFOs and Pipes now generate SIGIO for both readers and writers.
31 : * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
32 : *
33 : * pipe_read & write cleanup
34 : * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
35 : */
36 :
37 : /* Drop the inode semaphore and wait for a pipe event, atomically */
38 : void pipe_wait(struct inode * inode)
39 1203 : {
40 1203 : DEFINE_WAIT(wait);
41 :
42 : /*
43 : * Pipes are system-local resources, so sleeping on them
44 : * is considered a noninteractive wait:
45 : */
46 1203 : prepare_to_wait(PIPE_WAIT(*inode), &wait, TASK_INTERRUPTIBLE|TASK_NONINTERACTIVE);
47 1203 : up(PIPE_SEM(*inode));
48 1203 : schedule();
49 1203 : finish_wait(PIPE_WAIT(*inode), &wait);
50 1203 : down(PIPE_SEM(*inode));
51 : }
52 :
53 : static inline int
54 : pipe_iov_copy_from_user(void *to, struct iovec *iov, unsigned long len)
55 20362 : {
56 10181 : unsigned long copy;
57 :
58 10181 : while (len > 0) {
59 10181 : while (!iov->iov_len)
60 0 : iov++;
61 10181 : copy = min_t(unsigned long, len, iov->iov_len);
62 :
63 10181 : if (copy_from_user(to, iov->iov_base, copy))
64 : return -EFAULT;
65 10181 : to += copy;
66 10181 : len -= copy;
67 10181 : iov->iov_base += copy;
68 10181 : iov->iov_len -= copy;
69 10181 : }
70 : return 0;
71 : }
72 :
73 : static inline int
74 : pipe_iov_copy_to_user(struct iovec *iov, const void *from, unsigned long len)
75 8492 : {
76 4246 : unsigned long copy;
77 :
78 4246 : while (len > 0) {
79 4246 : while (!iov->iov_len)
80 0 : iov++;
81 4246 : copy = min_t(unsigned long, len, iov->iov_len);
82 :
83 4246 : if (copy_to_user(iov->iov_base, from, copy))
84 : return -EFAULT;
85 4246 : from += copy;
86 4246 : len -= copy;
87 4246 : iov->iov_base += copy;
88 4246 : iov->iov_len -= copy;
89 4246 : }
90 : return 0;
91 : }
92 :
93 : static void anon_pipe_buf_release(struct pipe_inode_info *info, struct pipe_buffer *buf)
94 3210 : {
95 3210 : struct page *page = buf->page;
96 :
97 3210 : if (info->tmp_page) {
98 449 : __free_page(page);
99 2761 : return;
100 : }
101 2761 : info->tmp_page = page;
102 : }
103 :
104 : static void *anon_pipe_buf_map(struct file *file, struct pipe_inode_info *info, struct pipe_buffer *buf)
105 11217 : {
106 11217 : return kmap(buf->page);
107 : }
108 :
109 : static void anon_pipe_buf_unmap(struct pipe_inode_info *info, struct pipe_buffer *buf)
110 11217 : {
111 11217 : kunmap(buf->page);
112 : }
113 :
114 : static struct pipe_buf_operations anon_pipe_buf_ops = {
115 : .can_merge = 1,
116 : .map = anon_pipe_buf_map,
117 : .unmap = anon_pipe_buf_unmap,
118 : .release = anon_pipe_buf_release,
119 : };
120 :
121 : static ssize_t
122 : pipe_readv(struct file *filp, const struct iovec *_iov,
123 : unsigned long nr_segs, loff_t *ppos)
124 6719 : {
125 6719 : struct inode *inode = filp->f_dentry->d_inode;
126 6719 : struct pipe_inode_info *info;
127 6719 : int do_wakeup;
128 6719 : ssize_t ret;
129 6719 : struct iovec *iov = (struct iovec *)_iov;
130 6719 : size_t total_len;
131 :
132 6719 : total_len = iov_length(iov, nr_segs);
133 : /* Null read succeeds. */
134 6719 : if (unlikely(total_len == 0))
135 0 : return 0;
136 :
137 6719 : do_wakeup = 0;
138 6719 : ret = 0;
139 6719 : down(PIPE_SEM(*inode));
140 6719 : info = inode->i_pipe;
141 9207 : for (;;) {
142 8224 : int bufs = info->nrbufs;
143 8224 : if (bufs) {
144 4246 : int curbuf = info->curbuf;
145 4246 : struct pipe_buffer *buf = info->bufs + curbuf;
146 4246 : struct pipe_buf_operations *ops = buf->ops;
147 4246 : void *addr;
148 4246 : size_t chars = buf->len;
149 4246 : int error;
150 :
151 4246 : if (chars > total_len)
152 1036 : chars = total_len;
153 :
154 4246 : addr = ops->map(filp, info, buf);
155 4246 : error = pipe_iov_copy_to_user(iov, addr + buf->offset, chars);
156 4246 : ops->unmap(info, buf);
157 4246 : if (unlikely(error)) {
158 0 : if (!ret) ret = -EFAULT;
159 0 : break;
160 : }
161 4246 : ret += chars;
162 4246 : buf->offset += chars;
163 4246 : buf->len -= chars;
164 4246 : if (!buf->len) {
165 3210 : buf->ops = NULL;
166 3210 : ops->release(info, buf);
167 3210 : curbuf = (curbuf + 1) & (PIPE_BUFFERS-1);
168 3210 : info->curbuf = curbuf;
169 3210 : info->nrbufs = --bufs;
170 3210 : do_wakeup = 1;
171 : }
172 4246 : total_len -= chars;
173 4246 : if (!total_len)
174 2964 : break; /* common path: read succeeded */
175 : }
176 2964 : if (bufs) /* More to do? */
177 6420 : continue;
178 6420 : if (!PIPE_WRITERS(*inode))
179 1302 : break;
180 1302 : if (!PIPE_WAITING_WRITERS(*inode)) {
181 : /* syscall merging: Usually we must not sleep
182 : * if O_NONBLOCK is set, or if we got some data.
183 : * But if a writer sleeps in kernel space, then
184 : * we can wait for that data without violating POSIX.
185 : */
186 1300 : if (ret)
187 1079 : break;
188 1079 : if (filp->f_flags & O_NONBLOCK) {
189 98 : ret = -EAGAIN;
190 98 : break;
191 : }
192 : }
193 983 : if (signal_pending(current)) {
194 0 : if (!ret) ret = -ERESTARTSYS;
195 0 : break;
196 : }
197 983 : if (do_wakeup) {
198 2 : wake_up_interruptible_sync(PIPE_WAIT(*inode));
199 2 : kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
200 : }
201 983 : pipe_wait(inode);
202 : }
203 6719 : up(PIPE_SEM(*inode));
204 : /* Signal writers asynchronously that there is more room. */
205 6719 : if (do_wakeup) {
206 2820 : wake_up_interruptible(PIPE_WAIT(*inode));
207 2820 : kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
208 : }
209 6719 : if (ret > 0)
210 3722 : file_accessed(filp);
211 6719 : return ret;
212 : }
213 :
214 : static ssize_t
215 : pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
216 6719 : {
217 6719 : struct iovec iov = { .iov_base = buf, .iov_len = count };
218 6719 : return pipe_readv(filp, &iov, 1, ppos);
219 : }
220 :
221 : static ssize_t
222 : pipe_writev(struct file *filp, const struct iovec *_iov,
223 : unsigned long nr_segs, loff_t *ppos)
224 10170 : {
225 10170 : struct inode *inode = filp->f_dentry->d_inode;
226 10170 : struct pipe_inode_info *info;
227 10170 : ssize_t ret;
228 10170 : int do_wakeup;
229 10170 : struct iovec *iov = (struct iovec *)_iov;
230 10170 : size_t total_len;
231 10170 : ssize_t chars;
232 :
233 10170 : total_len = iov_length(iov, nr_segs);
234 : /* Null write succeeds. */
235 10170 : if (unlikely(total_len == 0))
236 0 : return 0;
237 :
238 10170 : do_wakeup = 0;
239 10170 : ret = 0;
240 10170 : down(PIPE_SEM(*inode));
241 10170 : info = inode->i_pipe;
242 :
243 10170 : if (!PIPE_READERS(*inode)) {
244 0 : send_sig(SIGPIPE, current, 0);
245 0 : ret = -EPIPE;
246 0 : goto out;
247 : }
248 :
249 : /* We try to merge small writes */
250 10170 : chars = total_len & (PAGE_SIZE-1); /* size of the last buffer */
251 10170 : if (info->nrbufs && chars != 0) {
252 7065 : int lastbuf = (info->curbuf + info->nrbufs - 1) & (PIPE_BUFFERS-1);
253 7065 : struct pipe_buffer *buf = info->bufs + lastbuf;
254 7065 : struct pipe_buf_operations *ops = buf->ops;
255 7065 : int offset = buf->offset + buf->len;
256 7065 : if (ops->can_merge && offset + chars <= PAGE_SIZE) {
257 6971 : void *addr = ops->map(filp, info, buf);
258 6971 : int error = pipe_iov_copy_from_user(offset + addr, iov, chars);
259 6971 : ops->unmap(info, buf);
260 6971 : ret = error;
261 6971 : do_wakeup = 1;
262 6971 : if (error)
263 6971 : goto out;
264 6971 : buf->len += chars;
265 6971 : total_len -= chars;
266 6971 : ret = chars;
267 6971 : if (!total_len)
268 0 : goto out;
269 : }
270 : }
271 :
272 231 : for (;;) {
273 3430 : int bufs;
274 3430 : if (!PIPE_READERS(*inode)) {
275 0 : send_sig(SIGPIPE, current, 0);
276 0 : if (!ret) ret = -EPIPE;
277 0 : break;
278 : }
279 3430 : bufs = info->nrbufs;
280 3430 : if (bufs < PIPE_BUFFERS) {
281 3210 : int newbuf = (info->curbuf + bufs) & (PIPE_BUFFERS-1);
282 3210 : struct pipe_buffer *buf = info->bufs + newbuf;
283 3210 : struct page *page = info->tmp_page;
284 3210 : int error;
285 :
286 3210 : if (!page) {
287 2885 : page = alloc_page(GFP_HIGHUSER);
288 2885 : if (unlikely(!page)) {
289 0 : ret = ret ? : -ENOMEM;
290 0 : break;
291 : }
292 2885 : info->tmp_page = page;
293 : }
294 : /* Always wakeup, even if the copy fails. Otherwise
295 : * we lock up (O_NONBLOCK-)readers that sleep due to
296 : * syscall merging.
297 : * FIXME! Is this really true?
298 : */
299 3210 : do_wakeup = 1;
300 3210 : chars = PAGE_SIZE;
301 3210 : if (chars > total_len)
302 2616 : chars = total_len;
303 :
304 3210 : error = pipe_iov_copy_from_user(kmap(page), iov, chars);
305 0 : kunmap(page);
306 0 : if (unlikely(error)) {
307 0 : if (!ret) ret = -EFAULT;
308 0 : break;
309 : }
310 3210 : ret += chars;
311 :
312 : /* Insert it into the buffer array */
313 3210 : buf->page = page;
314 3210 : buf->ops = &anon_pipe_buf_ops;
315 3210 : buf->offset = 0;
316 3210 : buf->len = chars;
317 3210 : info->nrbufs = ++bufs;
318 3210 : info->tmp_page = NULL;
319 :
320 3210 : total_len -= chars;
321 3210 : if (!total_len)
322 11 : break;
323 : }
324 11 : if (bufs < PIPE_BUFFERS)
325 220 : continue;
326 220 : if (filp->f_flags & O_NONBLOCK) {
327 0 : if (!ret) ret = -EAGAIN;
328 0 : break;
329 : }
330 220 : if (signal_pending(current)) {
331 0 : if (!ret) ret = -ERESTARTSYS;
332 0 : break;
333 : }
334 220 : if (do_wakeup) {
335 0 : wake_up_interruptible_sync(PIPE_WAIT(*inode));
336 0 : kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
337 0 : do_wakeup = 0;
338 : }
339 220 : PIPE_WAITING_WRITERS(*inode)++;
340 220 : pipe_wait(inode);
341 220 : PIPE_WAITING_WRITERS(*inode)--;
342 : }
343 : out:
344 10170 : up(PIPE_SEM(*inode));
345 10170 : if (do_wakeup) {
346 10170 : wake_up_interruptible(PIPE_WAIT(*inode));
347 10170 : kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
348 : }
349 10170 : if (ret > 0)
350 10170 : inode_update_time(inode, 1); /* mtime and ctime */
351 10170 : return ret;
352 : }
353 :
354 : static ssize_t
355 : pipe_write(struct file *filp, const char __user *buf,
356 : size_t count, loff_t *ppos)
357 10170 : {
358 10170 : struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
359 10170 : return pipe_writev(filp, &iov, 1, ppos);
360 : }
361 :
362 : static ssize_t
363 : bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
364 0 : {
365 0 : return -EBADF;
366 : }
367 :
368 : static ssize_t
369 : bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
370 0 : {
371 0 : return -EBADF;
372 : }
373 :
374 : static int
375 : pipe_ioctl(struct inode *pino, struct file *filp,
376 : unsigned int cmd, unsigned long arg)
377 455 : {
378 455 : struct inode *inode = filp->f_dentry->d_inode;
379 455 : struct pipe_inode_info *info;
380 455 : int count, buf, nrbufs;
381 :
382 455 : switch (cmd) {
383 : case FIONREAD:
384 0 : down(PIPE_SEM(*inode));
385 0 : info = inode->i_pipe;
386 0 : count = 0;
387 0 : buf = info->curbuf;
388 0 : nrbufs = info->nrbufs;
389 0 : while (--nrbufs >= 0) {
390 0 : count += info->bufs[buf].len;
391 0 : buf = (buf+1) & (PIPE_BUFFERS-1);
392 : }
393 0 : up(PIPE_SEM(*inode));
394 0 : return put_user(count, (int __user *)arg);
395 : default:
396 455 : return -EINVAL;
397 : }
398 : }
399 :
400 : /* No kernel lock held - fine */
401 : static unsigned int
402 : pipe_poll(struct file *filp, poll_table *wait)
403 594 : {
404 594 : unsigned int mask;
405 594 : struct inode *inode = filp->f_dentry->d_inode;
406 594 : struct pipe_inode_info *info = inode->i_pipe;
407 594 : int nrbufs;
408 :
409 594 : poll_wait(filp, PIPE_WAIT(*inode), wait);
410 :
411 : /* Reading only -- no need for acquiring the semaphore. */
412 594 : nrbufs = info->nrbufs;
413 594 : mask = 0;
414 594 : if (filp->f_mode & FMODE_READ) {
415 594 : mask = (nrbufs > 0) ? POLLIN | POLLRDNORM : 0;
416 594 : if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
417 0 : mask |= POLLHUP;
418 : }
419 :
420 594 : if (filp->f_mode & FMODE_WRITE) {
421 99 : mask |= (nrbufs < PIPE_BUFFERS) ? POLLOUT | POLLWRNORM : 0;
422 : /*
423 : * Most Unices do not set POLLERR for FIFOs but on Linux they
424 : * behave exactly like pipes for poll().
425 : */
426 99 : if (!PIPE_READERS(*inode))
427 0 : mask |= POLLERR;
428 : }
429 :
430 594 : return mask;
431 : }
432 :
433 : static int
434 : pipe_release(struct inode *inode, int decr, int decw)
435 5875 : {
436 5875 : down(PIPE_SEM(*inode));
437 5875 : PIPE_READERS(*inode) -= decr;
438 5875 : PIPE_WRITERS(*inode) -= decw;
439 5875 : if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
440 2924 : free_pipe_info(inode);
441 : } else {
442 2951 : wake_up_interruptible(PIPE_WAIT(*inode));
443 2951 : kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
444 2951 : kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
445 : }
446 5875 : up(PIPE_SEM(*inode));
447 :
448 5875 : return 0;
449 : }
450 :
451 : static int
452 : pipe_read_fasync(int fd, struct file *filp, int on)
453 2924 : {
454 2924 : struct inode *inode = filp->f_dentry->d_inode;
455 2924 : int retval;
456 :
457 2924 : down(PIPE_SEM(*inode));
458 2924 : retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
459 2924 : up(PIPE_SEM(*inode));
460 :
461 2924 : if (retval < 0)
462 0 : return retval;
463 :
464 2924 : return 0;
465 : }
466 :
467 :
468 : static int
469 : pipe_write_fasync(int fd, struct file *filp, int on)
470 2925 : {
471 2925 : struct inode *inode = filp->f_dentry->d_inode;
472 2925 : int retval;
473 :
474 2925 : down(PIPE_SEM(*inode));
475 2925 : retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
476 2925 : up(PIPE_SEM(*inode));
477 :
478 2925 : if (retval < 0)
479 0 : return retval;
480 :
481 2925 : return 0;
482 : }
483 :
484 :
485 : static int
486 : pipe_rdwr_fasync(int fd, struct file *filp, int on)
487 26 : {
488 26 : struct inode *inode = filp->f_dentry->d_inode;
489 26 : int retval;
490 :
491 26 : down(PIPE_SEM(*inode));
492 :
493 26 : retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
494 :
495 26 : if (retval >= 0)
496 26 : retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
497 :
498 26 : up(PIPE_SEM(*inode));
499 :
500 26 : if (retval < 0)
501 0 : return retval;
502 :
503 26 : return 0;
504 : }
505 :
506 :
507 : static int
508 : pipe_read_release(struct inode *inode, struct file *filp)
509 2924 : {
510 2924 : pipe_read_fasync(-1, filp, 0);
511 2924 : return pipe_release(inode, 1, 0);
512 : }
513 :
514 : static int
515 : pipe_write_release(struct inode *inode, struct file *filp)
516 2925 : {
517 2925 : pipe_write_fasync(-1, filp, 0);
518 2925 : return pipe_release(inode, 0, 1);
519 : }
520 :
521 : static int
522 : pipe_rdwr_release(struct inode *inode, struct file *filp)
523 26 : {
524 26 : int decr, decw;
525 :
526 26 : pipe_rdwr_fasync(-1, filp, 0);
527 26 : decr = (filp->f_mode & FMODE_READ) != 0;
528 26 : decw = (filp->f_mode & FMODE_WRITE) != 0;
529 26 : return pipe_release(inode, decr, decw);
530 : }
531 :
532 : static int
533 : pipe_read_open(struct inode *inode, struct file *filp)
534 0 : {
535 : /* We could have perhaps used atomic_t, but this and friends
536 : below are the only places. So it doesn't seem worthwhile. */
537 0 : down(PIPE_SEM(*inode));
538 0 : PIPE_READERS(*inode)++;
539 0 : up(PIPE_SEM(*inode));
540 :
541 0 : return 0;
542 : }
543 :
544 : static int
545 : pipe_write_open(struct inode *inode, struct file *filp)
546 0 : {
547 0 : down(PIPE_SEM(*inode));
548 0 : PIPE_WRITERS(*inode)++;
549 0 : up(PIPE_SEM(*inode));
550 :
551 0 : return 0;
552 : }
553 :
554 : static int
555 : pipe_rdwr_open(struct inode *inode, struct file *filp)
556 26 : {
557 26 : down(PIPE_SEM(*inode));
558 26 : if (filp->f_mode & FMODE_READ)
559 26 : PIPE_READERS(*inode)++;
560 26 : if (filp->f_mode & FMODE_WRITE)
561 0 : PIPE_WRITERS(*inode)++;
562 26 : up(PIPE_SEM(*inode));
563 :
564 26 : return 0;
565 : }
566 :
567 : /*
568 : * The file_operations structs are not static because they
569 : * are also used in linux/fs/fifo.c to do operations on FIFOs.
570 : */
571 : struct file_operations read_fifo_fops = {
572 : .llseek = no_llseek,
573 : .read = pipe_read,
574 : .readv = pipe_readv,
575 : .write = bad_pipe_w,
576 : .poll = pipe_poll,
577 : .ioctl = pipe_ioctl,
578 : .open = pipe_read_open,
579 : .release = pipe_read_release,
580 : .fasync = pipe_read_fasync,
581 : };
582 :
583 : struct file_operations write_fifo_fops = {
584 : .llseek = no_llseek,
585 : .read = bad_pipe_r,
586 : .write = pipe_write,
587 : .writev = pipe_writev,
588 : .poll = pipe_poll,
589 : .ioctl = pipe_ioctl,
590 : .open = pipe_write_open,
591 : .release = pipe_write_release,
592 : .fasync = pipe_write_fasync,
593 : };
594 :
595 : struct file_operations rdwr_fifo_fops = {
596 : .llseek = no_llseek,
597 : .read = pipe_read,
598 : .readv = pipe_readv,
599 : .write = pipe_write,
600 : .writev = pipe_writev,
601 : .poll = pipe_poll,
602 : .ioctl = pipe_ioctl,
603 : .open = pipe_rdwr_open,
604 : .release = pipe_rdwr_release,
605 : .fasync = pipe_rdwr_fasync,
606 : };
607 :
608 : struct file_operations read_pipe_fops = {
609 : .llseek = no_llseek,
610 : .read = pipe_read,
611 : .readv = pipe_readv,
612 : .write = bad_pipe_w,
613 : .poll = pipe_poll,
614 : .ioctl = pipe_ioctl,
615 : .open = pipe_read_open,
616 : .release = pipe_read_release,
617 : .fasync = pipe_read_fasync,
618 : };
619 :
620 : struct file_operations write_pipe_fops = {
621 : .llseek = no_llseek,
622 : .read = bad_pipe_r,
623 : .write = pipe_write,
624 : .writev = pipe_writev,
625 : .poll = pipe_poll,
626 : .ioctl = pipe_ioctl,
627 : .open = pipe_write_open,
628 : .release = pipe_write_release,
629 : .fasync = pipe_write_fasync,
630 : };
631 :
632 : struct file_operations rdwr_pipe_fops = {
633 : .llseek = no_llseek,
634 : .read = pipe_read,
635 : .readv = pipe_readv,
636 : .write = pipe_write,
637 : .writev = pipe_writev,
638 : .poll = pipe_poll,
639 : .ioctl = pipe_ioctl,
640 : .open = pipe_rdwr_open,
641 : .release = pipe_rdwr_release,
642 : .fasync = pipe_rdwr_fasync,
643 : };
644 :
645 : void free_pipe_info(struct inode *inode)
646 2924 : {
647 2924 : int i;
648 2924 : struct pipe_inode_info *info = inode->i_pipe;
649 :
650 2924 : inode->i_pipe = NULL;
651 49708 : for (i = 0; i < PIPE_BUFFERS; i++) {
652 46784 : struct pipe_buffer *buf = info->bufs + i;
653 46784 : if (buf->ops)
654 0 : buf->ops->release(info, buf);
655 : }
656 2924 : if (info->tmp_page)
657 2435 : __free_page(info->tmp_page);
658 2924 : kfree(info);
659 : }
660 :
661 : struct inode* pipe_new(struct inode* inode)
662 2928 : {
663 2928 : struct pipe_inode_info *info;
664 :
665 2928 : info = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
666 2928 : if (!info)
667 2928 : goto fail_page;
668 2928 : memset(info, 0, sizeof(*info));
669 2928 : inode->i_pipe = info;
670 :
671 2928 : init_waitqueue_head(PIPE_WAIT(*inode));
672 2928 : PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
673 :
674 2928 : return inode;
675 : fail_page:
676 0 : return NULL;
677 : }
678 :
679 : static struct vfsmount *pipe_mnt;
680 : static int pipefs_delete_dentry(struct dentry *dentry)
681 2924 : {
682 2924 : return 1;
683 : }
684 : static struct dentry_operations pipefs_dentry_operations = {
685 : .d_delete = pipefs_delete_dentry,
686 : };
687 :
688 : static struct inode * get_pipe_inode(void)
689 2927 : {
690 2927 : struct inode *inode = new_inode(pipe_mnt->mnt_sb);
691 :
692 2927 : if (!inode)
693 2927 : goto fail_inode;
694 :
695 2927 : if(!pipe_new(inode))
696 2927 : goto fail_iput;
697 2927 : PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
698 2927 : inode->i_fop = &rdwr_pipe_fops;
699 :
700 : /*
701 : * Mark the inode dirty from the very beginning,
702 : * that way it will never be moved to the dirty
703 : * list because "mark_inode_dirty()" will think
704 : * that it already _is_ on the dirty list.
705 : */
706 2927 : inode->i_state = I_DIRTY;
707 2927 : inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
708 2927 : inode->i_uid = current->fsuid;
709 2927 : inode->i_gid = current->fsgid;
710 2927 : inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
711 2927 : inode->i_blksize = PAGE_SIZE;
712 2927 : return inode;
713 :
714 : fail_iput:
715 0 : iput(inode);
716 : fail_inode:
717 0 : return NULL;
718 : }
719 :
720 : int do_pipe(int *fd)
721 2927 : {
722 2927 : struct qstr this;
723 2927 : char name[32];
724 2927 : struct dentry *dentry;
725 2927 : struct inode * inode;
726 2927 : struct file *f1, *f2;
727 2927 : int error;
728 2927 : int i,j;
729 :
730 2927 : error = -ENFILE;
731 2927 : f1 = get_empty_filp();
732 2927 : if (!f1)
733 2927 : goto no_files;
734 :
735 2927 : f2 = get_empty_filp();
736 2927 : if (!f2)
737 2927 : goto close_f1;
738 :
739 2927 : inode = get_pipe_inode();
740 2927 : if (!inode)
741 2927 : goto close_f12;
742 :
743 2927 : error = get_unused_fd();
744 2927 : if (error < 0)
745 2927 : goto close_f12_inode;
746 2927 : i = error;
747 :
748 2927 : error = get_unused_fd();
749 2927 : if (error < 0)
750 2927 : goto close_f12_inode_i;
751 2927 : j = error;
752 :
753 2927 : error = -ENOMEM;
754 2927 : sprintf(name, "[%lu]", inode->i_ino);
755 2927 : this.name = name;
756 2927 : this.len = strlen(name);
757 2927 : this.hash = inode->i_ino; /* will go */
758 2927 : dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
759 2927 : if (!dentry)
760 2927 : goto close_f12_inode_i_j;
761 2927 : dentry->d_op = &pipefs_dentry_operations;
762 2927 : d_add(dentry, inode);
763 2927 : f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
764 2927 : f1->f_dentry = f2->f_dentry = dget(dentry);
765 2927 : f1->f_mapping = f2->f_mapping = inode->i_mapping;
766 :
767 : /* read file */
768 2927 : f1->f_pos = f2->f_pos = 0;
769 2927 : f1->f_flags = O_RDONLY;
770 2927 : f1->f_op = &read_pipe_fops;
771 2927 : f1->f_mode = FMODE_READ;
772 2927 : f1->f_version = 0;
773 :
774 : /* write file */
775 2927 : f2->f_flags = O_WRONLY;
776 2927 : f2->f_op = &write_pipe_fops;
777 2927 : f2->f_mode = FMODE_WRITE;
778 2927 : f2->f_version = 0;
779 :
780 2927 : fd_install(i, f1);
781 2927 : fd_install(j, f2);
782 2927 : fd[0] = i;
783 2927 : fd[1] = j;
784 2927 : return 0;
785 :
786 : close_f12_inode_i_j:
787 0 : put_unused_fd(j);
788 : close_f12_inode_i:
789 0 : put_unused_fd(i);
790 : close_f12_inode:
791 0 : free_pipe_info(inode);
792 0 : iput(inode);
793 : close_f12:
794 0 : put_filp(f2);
795 : close_f1:
796 0 : put_filp(f1);
797 : no_files:
798 0 : return error;
799 : }
800 :
801 : /*
802 : * pipefs should _never_ be mounted by userland - too much of security hassle,
803 : * no real gain from having the whole whorehouse mounted. So we don't need
804 : * any operations on the root directory. However, we need a non-trivial
805 : * d_name - pipe: will go nicely and kill the special-casing in procfs.
806 : */
807 :
808 : static struct super_block *pipefs_get_sb(struct file_system_type *fs_type,
809 : int flags, const char *dev_name, void *data)
810 1 : {
811 1 : return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
812 : }
813 :
814 : static struct file_system_type pipe_fs_type = {
815 : .name = "pipefs",
816 : .get_sb = pipefs_get_sb,
817 : .kill_sb = kill_anon_super,
818 : };
819 :
820 : static int __init init_pipe_fs(void)
821 1 : {
822 1 : int err = register_filesystem(&pipe_fs_type);
823 1 : if (!err) {
824 1 : pipe_mnt = kern_mount(&pipe_fs_type);
825 1 : if (IS_ERR(pipe_mnt)) {
826 0 : err = PTR_ERR(pipe_mnt);
827 0 : unregister_filesystem(&pipe_fs_type);
828 : }
829 : }
830 1 : return err;
831 : }
832 :
833 : static void __exit exit_pipe_fs(void)
834 0 : {
835 0 : unregister_filesystem(&pipe_fs_type);
836 0 : mntput(pipe_mnt);
837 : }
838 :
839 : fs_initcall(init_pipe_fs);
840 : module_exit(exit_pipe_fs);
|