1
2
3
4
5
6
7#include <linux/mm.h>
8#include <linux/file.h>
9#include <linux/poll.h>
10#include <linux/malloc.h>
11#include <linux/smp_lock.h>
12
13#include <asm/uaccess.h>
14
15
16
17
18
19#ifdef __sparc__
20#define FIFO_SUNOS_BRAINDAMAGE
21#else
22#undef FIFO_SUNOS_BRAINDAMAGE
23#endif
24
25
26
27
28
29
30
31
32
33
34
35static void pipe_wait(struct inode * inode)
36{
37 DECLARE_WAITQUEUE(wait, current);
38 current->state = TASK_INTERRUPTIBLE;
39 add_wait_queue(PIPE_WAIT(*inode), &wait);
40 up(PIPE_SEM(*inode));
41 schedule();
42 remove_wait_queue(PIPE_WAIT(*inode), &wait);
43 current->state = TASK_RUNNING;
44}
45
46static ssize_t
47pipe_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
48{
49 struct inode *inode = filp->f_dentry->d_inode;
50 ssize_t size, read, ret;
51
52
53 ret = -ESPIPE;
54 if (ppos != &filp->f_pos)
55 goto out_nolock;
56
57
58 ret = 0;
59 if (count == 0)
60 goto out_nolock;
61
62
63 if (filp->f_flags & O_NONBLOCK) {
64 ret = -EAGAIN;
65 if (down_trylock(PIPE_SEM(*inode)))
66 goto out_nolock;
67 ret = PIPE_WRITERS(*inode) ? -EAGAIN : 0;
68 if (PIPE_EMPTY(*inode))
69 goto out;
70 } else {
71 ret = -ERESTARTSYS;
72 if (down_interruptible(PIPE_SEM(*inode)))
73 goto out_nolock;
74
75 if (PIPE_EMPTY(*inode)) {
76 ret = 0;
77 if (!PIPE_WRITERS(*inode))
78 goto out;
79
80 for (;;) {
81 pipe_wait(inode);
82 ret = -ERESTARTSYS;
83 if (signal_pending(current))
84 goto out_nolock;
85 if (down_interruptible(PIPE_SEM(*inode)))
86 goto out_nolock;
87 ret = 0;
88 if (!PIPE_EMPTY(*inode))
89 break;
90 if (!PIPE_WRITERS(*inode))
91 goto out;
92 }
93 }
94 }
95
96
97 ret = -EFAULT;
98 read = 0;
99 while (count > 0 && (size = PIPE_LEN(*inode))) {
100 char *pipebuf = PIPE_BASE(*inode) + PIPE_START(*inode);
101 ssize_t chars = PIPE_MAX_RCHUNK(*inode);
102
103 if (chars > count)
104 chars = count;
105 if (chars > size)
106 chars = size;
107
108 if (copy_to_user(buf, pipebuf, chars))
109 goto out;
110
111 read += chars;
112 PIPE_START(*inode) += chars;
113 PIPE_START(*inode) &= (PIPE_SIZE - 1);
114 PIPE_LEN(*inode) -= chars;
115 count -= chars;
116 buf += chars;
117 }
118
119
120 if (!PIPE_LEN(*inode))
121 PIPE_START(*inode) = 0;
122
123
124 wake_up_interruptible(PIPE_WAIT(*inode));
125
126 if (read)
127 UPDATE_ATIME(inode);
128 ret = read;
129
130out:
131 up(PIPE_SEM(*inode));
132out_nolock:
133 return ret;
134}
135
136static ssize_t
137pipe_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
138{
139 struct inode *inode = filp->f_dentry->d_inode;
140 ssize_t free, written, ret;
141
142
143 ret = -ESPIPE;
144 if (ppos != &filp->f_pos)
145 goto out_nolock;
146
147
148 ret = 0;
149 if (count == 0)
150 goto out_nolock;
151
152 ret = -ERESTARTSYS;
153 if (down_interruptible(PIPE_SEM(*inode)))
154 goto out_nolock;
155
156
157 if (!PIPE_READERS(*inode))
158 goto sigpipe;
159
160
161 free = (count <= PIPE_BUF ? count : 1);
162 written = 0;
163
164
165 if (filp->f_flags & O_NONBLOCK) {
166 ret = -EAGAIN;
167 if (PIPE_FREE(*inode) < free)
168 goto out;
169 } else {
170 while (PIPE_FREE(*inode) < free) {
171 pipe_wait(inode);
172 ret = -ERESTARTSYS;
173 if (signal_pending(current))
174 goto out_nolock;
175 if (down_interruptible(PIPE_SEM(*inode)))
176 goto out_nolock;
177
178 if (!PIPE_READERS(*inode))
179 goto sigpipe;
180 }
181 }
182
183
184 ret = -EFAULT;
185 while (count > 0) {
186 int space;
187 char *pipebuf = PIPE_BASE(*inode) + PIPE_END(*inode);
188 ssize_t chars = PIPE_MAX_WCHUNK(*inode);
189
190 if ((space = PIPE_FREE(*inode)) != 0) {
191 if (chars > count)
192 chars = count;
193 if (chars > space)
194 chars = space;
195
196 if (copy_from_user(pipebuf, buf, chars))
197 goto out;
198
199 written += chars;
200 PIPE_LEN(*inode) += chars;
201 count -= chars;
202 buf += chars;
203 space = PIPE_FREE(*inode);
204 continue;
205 }
206
207 ret = written;
208 if (filp->f_flags & O_NONBLOCK)
209 break;
210
211 do {
212
213 wake_up_interruptible(PIPE_WAIT(*inode));
214 pipe_wait(inode);
215 if (signal_pending(current))
216 goto out;
217 if (down_interruptible(PIPE_SEM(*inode)))
218 goto out_nolock;
219 if (!PIPE_READERS(*inode))
220 goto sigpipe;
221 } while (!PIPE_FREE(*inode));
222 ret = -EFAULT;
223 }
224
225
226 wake_up_interruptible(PIPE_WAIT(*inode));
227
228 ret = (written ? written : -EAGAIN);
229 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
230 mark_inode_dirty(inode);
231
232out:
233 up(PIPE_SEM(*inode));
234out_nolock:
235 return ret;
236
237sigpipe:
238 up(PIPE_SEM(*inode));
239 send_sig(SIGPIPE, current, 0);
240 return -EPIPE;
241}
242
243static loff_t
244pipe_lseek(struct file *file, loff_t offset, int orig)
245{
246 return -ESPIPE;
247}
248
249static ssize_t
250bad_pipe_r(struct file *filp, char *buf, size_t count, loff_t *ppos)
251{
252 return -EBADF;
253}
254
255static ssize_t
256bad_pipe_w(struct file *filp, const char *buf, size_t count, loff_t *ppos)
257{
258 return -EBADF;
259}
260
261static int
262pipe_ioctl(struct inode *pino, struct file *filp,
263 unsigned int cmd, unsigned long arg)
264{
265 switch (cmd) {
266 case FIONREAD:
267 return put_user(PIPE_LEN(*pino), (int *)arg);
268 default:
269 return -EINVAL;
270 }
271}
272
273static unsigned int
274pipe_poll(struct file *filp, poll_table *wait)
275{
276 unsigned int mask;
277 struct inode *inode = filp->f_dentry->d_inode;
278
279 poll_wait(filp, PIPE_WAIT(*inode), wait);
280
281
282 mask = POLLIN | POLLRDNORM;
283 if (PIPE_EMPTY(*inode))
284 mask = POLLOUT | POLLWRNORM;
285 if (!PIPE_WRITERS(*inode))
286 mask |= POLLHUP;
287 if (!PIPE_READERS(*inode))
288 mask |= POLLERR;
289
290 return mask;
291}
292
293#ifdef FIFO_SUNOS_BRAINDAMAGE
294
295
296
297
298static unsigned int
299fifo_poll(struct file *filp, poll_table *wait)
300{
301 unsigned int mask;
302 struct inode *inode = filp->f_dentry->d_inode;
303
304 poll_wait(filp, PIPE_WAIT(*inode), wait);
305
306
307 mask = POLLIN | POLLRDNORM;
308 if (PIPE_EMPTY(*inode))
309 mask = POLLOUT | POLLWRNORM;
310 if (!PIPE_READERS(*inode))
311 mask |= POLLERR;
312
313 return mask;
314}
315#else
316
317#define fifo_poll pipe_poll
318
319#endif
320
321
322
323
324
325
326static ssize_t
327connect_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
328{
329 struct inode *inode = filp->f_dentry->d_inode;
330
331
332 if (PIPE_EMPTY(*inode) && !PIPE_WRITERS(*inode))
333 return 0;
334
335 filp->f_op = &read_fifo_fops;
336 return pipe_read(filp, buf, count, ppos);
337}
338
339static unsigned int
340connect_poll(struct file *filp, poll_table *wait)
341{
342 struct inode *inode = filp->f_dentry->d_inode;
343 unsigned int mask = 0;
344
345 poll_wait(filp, PIPE_WAIT(*inode), wait);
346
347
348 if (!PIPE_EMPTY(*inode)) {
349 filp->f_op = &read_fifo_fops;
350 mask = POLLIN | POLLRDNORM;
351 } else if (PIPE_WRITERS(*inode)) {
352 filp->f_op = &read_fifo_fops;
353 mask = POLLOUT | POLLWRNORM;
354 }
355
356 return mask;
357}
358
359static int
360pipe_release(struct inode *inode, int decr, int decw)
361{
362 down(PIPE_SEM(*inode));
363 PIPE_READERS(*inode) -= decr;
364 PIPE_WRITERS(*inode) -= decw;
365 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
366 struct pipe_inode_info *info = inode->i_pipe;
367 inode->i_pipe = NULL;
368 free_page((unsigned long) info->base);
369 kfree(info);
370 } else {
371 wake_up_interruptible(PIPE_WAIT(*inode));
372 }
373 up(PIPE_SEM(*inode));
374
375 return 0;
376}
377
378static int
379pipe_read_release(struct inode *inode, struct file *filp)
380{
381 return pipe_release(inode, 1, 0);
382}
383
384static int
385pipe_write_release(struct inode *inode, struct file *filp)
386{
387 return pipe_release(inode, 0, 1);
388}
389
390static int
391pipe_rdwr_release(struct inode *inode, struct file *filp)
392{
393 int decr, decw;
394
395 decr = (filp->f_mode & FMODE_READ) != 0;
396 decw = (filp->f_mode & FMODE_WRITE) != 0;
397 return pipe_release(inode, decr, decw);
398}
399
400static int
401pipe_read_open(struct inode *inode, struct file *filp)
402{
403
404
405 down(PIPE_SEM(*inode));
406 PIPE_READERS(*inode)++;
407 up(PIPE_SEM(*inode));
408
409 return 0;
410}
411
412static int
413pipe_write_open(struct inode *inode, struct file *filp)
414{
415 down(PIPE_SEM(*inode));
416 PIPE_WRITERS(*inode)++;
417 up(PIPE_SEM(*inode));
418
419 return 0;
420}
421
422static int
423pipe_rdwr_open(struct inode *inode, struct file *filp)
424{
425 down(PIPE_SEM(*inode));
426 if (filp->f_mode & FMODE_READ)
427 PIPE_READERS(*inode)++;
428 if (filp->f_mode & FMODE_WRITE)
429 PIPE_WRITERS(*inode)++;
430 up(PIPE_SEM(*inode));
431
432 return 0;
433}
434
435
436
437
438
439struct file_operations connecting_fifo_fops = {
440 pipe_lseek,
441 connect_read,
442 bad_pipe_w,
443 NULL,
444 connect_poll,
445 pipe_ioctl,
446 NULL,
447 pipe_read_open,
448 NULL,
449 pipe_read_release,
450 NULL
451};
452
453struct file_operations read_fifo_fops = {
454 pipe_lseek,
455 pipe_read,
456 bad_pipe_w,
457 NULL,
458 fifo_poll,
459 pipe_ioctl,
460 NULL,
461 pipe_read_open,
462 NULL,
463 pipe_read_release,
464 NULL
465};
466
467struct file_operations write_fifo_fops = {
468 pipe_lseek,
469 bad_pipe_r,
470 pipe_write,
471 NULL,
472 fifo_poll,
473 pipe_ioctl,
474 NULL,
475 pipe_write_open,
476 NULL,
477 pipe_write_release,
478 NULL
479};
480
481struct file_operations rdwr_fifo_fops = {
482 pipe_lseek,
483 pipe_read,
484 pipe_write,
485 NULL,
486 fifo_poll,
487 pipe_ioctl,
488 NULL,
489 pipe_rdwr_open,
490 NULL,
491 pipe_rdwr_release,
492 NULL
493};
494
495struct file_operations read_pipe_fops = {
496 pipe_lseek,
497 pipe_read,
498 bad_pipe_w,
499 NULL,
500 pipe_poll,
501 pipe_ioctl,
502 NULL,
503 pipe_read_open,
504 NULL,
505 pipe_read_release,
506 NULL
507};
508
509struct file_operations write_pipe_fops = {
510 pipe_lseek,
511 bad_pipe_r,
512 pipe_write,
513 NULL,
514 pipe_poll,
515 pipe_ioctl,
516 NULL,
517 pipe_write_open,
518 NULL,
519 pipe_write_release,
520 NULL
521};
522
523struct file_operations rdwr_pipe_fops = {
524 pipe_lseek,
525 pipe_read,
526 pipe_write,
527 NULL,
528 pipe_poll,
529 pipe_ioctl,
530 NULL,
531 pipe_rdwr_open,
532 NULL,
533 pipe_rdwr_release,
534 NULL
535};
536
537static struct inode * get_pipe_inode(void)
538{
539 extern struct inode_operations pipe_inode_operations;
540 struct inode *inode = get_empty_inode();
541 unsigned long page;
542
543 if (!inode)
544 goto fail_inode;
545
546 page = __get_free_page(GFP_USER);
547 if (!page)
548 goto fail_iput;
549
550 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
551 if (!inode->i_pipe)
552 goto fail_page;
553
554 inode->i_op = &pipe_inode_operations;
555
556 init_waitqueue_head(PIPE_WAIT(*inode));
557 PIPE_BASE(*inode) = (char *) page;
558 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
559 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
560
561
562
563
564
565
566
567 inode->i_state = I_DIRTY;
568 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
569 inode->i_uid = current->fsuid;
570 inode->i_gid = current->fsgid;
571 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
572 inode->i_blksize = PAGE_SIZE;
573 return inode;
574
575fail_page:
576 free_page(page);
577fail_iput:
578 iput(inode);
579fail_inode:
580 return NULL;
581}
582
583struct inode_operations pipe_inode_operations = {
584 &rdwr_pipe_fops,
585 NULL,
586 NULL,
587 NULL,
588 NULL,
589 NULL,
590 NULL,
591 NULL,
592 NULL,
593 NULL,
594 NULL,
595 NULL,
596 NULL,
597 NULL,
598 NULL,
599 NULL,
600 NULL,
601 NULL,
602 NULL,
603 NULL
604};
605
606int do_pipe(int *fd)
607{
608 struct inode * inode;
609 struct file *f1, *f2;
610 int error;
611 int i,j;
612
613 error = -ENFILE;
614 f1 = get_empty_filp();
615 if (!f1)
616 goto no_files;
617
618 f2 = get_empty_filp();
619 if (!f2)
620 goto close_f1;
621
622 inode = get_pipe_inode();
623 if (!inode)
624 goto close_f12;
625
626 error = get_unused_fd();
627 if (error < 0)
628 goto close_f12_inode;
629 i = error;
630
631 error = get_unused_fd();
632 if (error < 0)
633 goto close_f12_inode_i;
634 j = error;
635
636 error = -ENOMEM;
637 f1->f_dentry = f2->f_dentry = dget(d_alloc_root(inode));
638 if (!f1->f_dentry)
639 goto close_f12_inode_i_j;
640
641
642 f1->f_pos = f2->f_pos = 0;
643 f1->f_flags = O_RDONLY;
644 f1->f_op = &read_pipe_fops;
645 f1->f_mode = 1;
646
647
648 f2->f_flags = O_WRONLY;
649 f2->f_op = &write_pipe_fops;
650 f2->f_mode = 2;
651
652 fd_install(i, f1);
653 fd_install(j, f2);
654 fd[0] = i;
655 fd[1] = j;
656 return 0;
657
658close_f12_inode_i_j:
659 put_unused_fd(j);
660close_f12_inode_i:
661 put_unused_fd(i);
662close_f12_inode:
663 free_page((unsigned long) PIPE_BASE(*inode));
664 kfree(inode->i_pipe);
665 inode->i_pipe = NULL;
666 iput(inode);
667close_f12:
668 put_filp(f2);
669close_f1:
670 put_filp(f1);
671no_files:
672 return error;
673}
674