[PATCH] Fix bugs in analog tv i2c-helper chipset drivers
[linux-2.6/history.git] / fs / pipe.c
blobe75a087c84da30f3b3534d310c72b25e756a9e3c
1 /*
2 * linux/fs/pipe.c
4 * Copyright (C) 1991, 1992, 1999 Linus Torvalds
5 */
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 <asm/uaccess.h>
17 #include <asm/ioctls.h>
20 * We use a start+len construction, which provides full use of the
21 * allocated memory.
22 * -- Florian Coosmann (FGC)
24 * Reads with count = 0 should always return 0.
25 * -- Julian Bradfield 1999-06-07.
27 * FIFOs and Pipes now generate SIGIO for both readers and writers.
28 * -- Jeremy Elson <jelson@circlemud.org> 2001-08-16
30 * pipe_read & write cleanup
31 * -- Manfred Spraul <manfred@colorfullife.com> 2002-05-09
34 /* Drop the inode semaphore and wait for a pipe event, atomically */
35 void pipe_wait(struct inode * inode)
37 DEFINE_WAIT(wait);
39 prepare_to_wait(PIPE_WAIT(*inode), &wait, TASK_INTERRUPTIBLE);
40 up(PIPE_SEM(*inode));
41 schedule();
42 finish_wait(PIPE_WAIT(*inode), &wait);
43 down(PIPE_SEM(*inode));
46 static ssize_t
47 pipe_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
49 struct inode *inode = filp->f_dentry->d_inode;
50 int do_wakeup;
51 ssize_t ret;
53 /* pread is not allowed on pipes. */
54 if (unlikely(ppos != &filp->f_pos))
55 return -ESPIPE;
57 /* Null read succeeds. */
58 if (unlikely(count == 0))
59 return 0;
61 do_wakeup = 0;
62 ret = 0;
63 down(PIPE_SEM(*inode));
64 for (;;) {
65 int size = PIPE_LEN(*inode);
66 if (size) {
67 char *pipebuf = PIPE_BASE(*inode) + PIPE_START(*inode);
68 ssize_t chars = PIPE_MAX_RCHUNK(*inode);
70 if (chars > count)
71 chars = count;
72 if (chars > size)
73 chars = size;
75 if (copy_to_user(buf, pipebuf, chars)) {
76 if (!ret) ret = -EFAULT;
77 break;
79 ret += chars;
81 PIPE_START(*inode) += chars;
82 PIPE_START(*inode) &= (PIPE_SIZE - 1);
83 PIPE_LEN(*inode) -= chars;
84 count -= chars;
85 buf += chars;
86 do_wakeup = 1;
88 if (!count)
89 break; /* common path: read succeeded */
90 if (PIPE_LEN(*inode)) /* test for cyclic buffers */
91 continue;
92 if (!PIPE_WRITERS(*inode))
93 break;
94 if (!PIPE_WAITING_WRITERS(*inode)) {
95 /* syscall merging: Usually we must not sleep
96 * if O_NONBLOCK is set, or if we got some data.
97 * But if a writer sleeps in kernel space, then
98 * we can wait for that data without violating POSIX.
100 if (ret)
101 break;
102 if (filp->f_flags & O_NONBLOCK) {
103 ret = -EAGAIN;
104 break;
107 if (signal_pending(current)) {
108 if (!ret) ret = -ERESTARTSYS;
109 break;
111 if (do_wakeup) {
112 wake_up_interruptible_sync(PIPE_WAIT(*inode));
113 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
115 pipe_wait(inode);
117 up(PIPE_SEM(*inode));
118 /* Signal writers asynchronously that there is more room. */
119 if (do_wakeup) {
120 wake_up_interruptible(PIPE_WAIT(*inode));
121 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
123 if (ret > 0)
124 update_atime(inode);
125 return ret;
128 static ssize_t
129 pipe_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
131 struct inode *inode = filp->f_dentry->d_inode;
132 ssize_t ret;
133 size_t min;
134 int do_wakeup;
136 /* pwrite is not allowed on pipes. */
137 if (unlikely(ppos != &filp->f_pos))
138 return -ESPIPE;
140 /* Null write succeeds. */
141 if (unlikely(count == 0))
142 return 0;
144 do_wakeup = 0;
145 ret = 0;
146 min = count;
147 if (min > PIPE_BUF)
148 min = 1;
149 down(PIPE_SEM(*inode));
150 for (;;) {
151 int free;
152 if (!PIPE_READERS(*inode)) {
153 send_sig(SIGPIPE, current, 0);
154 if (!ret) ret = -EPIPE;
155 break;
157 free = PIPE_FREE(*inode);
158 if (free >= min) {
159 /* transfer data */
160 ssize_t chars = PIPE_MAX_WCHUNK(*inode);
161 char *pipebuf = PIPE_BASE(*inode) + PIPE_END(*inode);
162 /* Always wakeup, even if the copy fails. Otherwise
163 * we lock up (O_NONBLOCK-)readers that sleep due to
164 * syscall merging.
166 do_wakeup = 1;
167 if (chars > count)
168 chars = count;
169 if (chars > free)
170 chars = free;
172 if (copy_from_user(pipebuf, buf, chars)) {
173 if (!ret) ret = -EFAULT;
174 break;
177 ret += chars;
178 PIPE_LEN(*inode) += chars;
179 count -= chars;
180 buf += chars;
182 if (!count)
183 break;
184 if (PIPE_FREE(*inode) && ret) {
185 /* handle cyclic data buffers */
186 min = 1;
187 continue;
189 if (filp->f_flags & O_NONBLOCK) {
190 if (!ret) ret = -EAGAIN;
191 break;
193 if (signal_pending(current)) {
194 if (!ret) ret = -ERESTARTSYS;
195 break;
197 if (do_wakeup) {
198 wake_up_interruptible_sync(PIPE_WAIT(*inode));
199 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
200 do_wakeup = 0;
202 PIPE_WAITING_WRITERS(*inode)++;
203 pipe_wait(inode);
204 PIPE_WAITING_WRITERS(*inode)--;
206 up(PIPE_SEM(*inode));
207 if (do_wakeup) {
208 wake_up_interruptible(PIPE_WAIT(*inode));
209 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
211 if (ret > 0)
212 inode_update_time(inode, 1); /* mtime and ctime */
213 return ret;
216 static ssize_t
217 bad_pipe_r(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
219 return -EBADF;
222 static ssize_t
223 bad_pipe_w(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
225 return -EBADF;
228 static int
229 pipe_ioctl(struct inode *pino, struct file *filp,
230 unsigned int cmd, unsigned long arg)
232 switch (cmd) {
233 case FIONREAD:
234 return put_user(PIPE_LEN(*pino), (int __user *)arg);
235 default:
236 return -EINVAL;
240 /* No kernel lock held - fine */
241 static unsigned int
242 pipe_poll(struct file *filp, poll_table *wait)
244 unsigned int mask;
245 struct inode *inode = filp->f_dentry->d_inode;
247 poll_wait(filp, PIPE_WAIT(*inode), wait);
249 /* Reading only -- no need for acquiring the semaphore. */
250 mask = POLLIN | POLLRDNORM;
251 if (PIPE_EMPTY(*inode))
252 mask = POLLOUT | POLLWRNORM;
253 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
254 mask |= POLLHUP;
255 if (!PIPE_READERS(*inode))
256 mask |= POLLERR;
258 return mask;
261 /* FIXME: most Unices do not set POLLERR for fifos */
262 #define fifo_poll pipe_poll
264 static int
265 pipe_release(struct inode *inode, int decr, int decw)
267 down(PIPE_SEM(*inode));
268 PIPE_READERS(*inode) -= decr;
269 PIPE_WRITERS(*inode) -= decw;
270 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
271 struct pipe_inode_info *info = inode->i_pipe;
272 inode->i_pipe = NULL;
273 free_page((unsigned long) info->base);
274 kfree(info);
275 } else {
276 wake_up_interruptible(PIPE_WAIT(*inode));
277 kill_fasync(PIPE_FASYNC_READERS(*inode), SIGIO, POLL_IN);
278 kill_fasync(PIPE_FASYNC_WRITERS(*inode), SIGIO, POLL_OUT);
280 up(PIPE_SEM(*inode));
282 return 0;
285 static int
286 pipe_read_fasync(int fd, struct file *filp, int on)
288 struct inode *inode = filp->f_dentry->d_inode;
289 int retval;
291 down(PIPE_SEM(*inode));
292 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
293 up(PIPE_SEM(*inode));
295 if (retval < 0)
296 return retval;
298 return 0;
302 static int
303 pipe_write_fasync(int fd, struct file *filp, int on)
305 struct inode *inode = filp->f_dentry->d_inode;
306 int retval;
308 down(PIPE_SEM(*inode));
309 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
310 up(PIPE_SEM(*inode));
312 if (retval < 0)
313 return retval;
315 return 0;
319 static int
320 pipe_rdwr_fasync(int fd, struct file *filp, int on)
322 struct inode *inode = filp->f_dentry->d_inode;
323 int retval;
325 down(PIPE_SEM(*inode));
327 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_READERS(*inode));
329 if (retval >= 0)
330 retval = fasync_helper(fd, filp, on, PIPE_FASYNC_WRITERS(*inode));
332 up(PIPE_SEM(*inode));
334 if (retval < 0)
335 return retval;
337 return 0;
341 static int
342 pipe_read_release(struct inode *inode, struct file *filp)
344 pipe_read_fasync(-1, filp, 0);
345 return pipe_release(inode, 1, 0);
348 static int
349 pipe_write_release(struct inode *inode, struct file *filp)
351 pipe_write_fasync(-1, filp, 0);
352 return pipe_release(inode, 0, 1);
355 static int
356 pipe_rdwr_release(struct inode *inode, struct file *filp)
358 int decr, decw;
360 pipe_rdwr_fasync(-1, filp, 0);
361 decr = (filp->f_mode & FMODE_READ) != 0;
362 decw = (filp->f_mode & FMODE_WRITE) != 0;
363 return pipe_release(inode, decr, decw);
366 static int
367 pipe_read_open(struct inode *inode, struct file *filp)
369 /* We could have perhaps used atomic_t, but this and friends
370 below are the only places. So it doesn't seem worthwhile. */
371 down(PIPE_SEM(*inode));
372 PIPE_READERS(*inode)++;
373 up(PIPE_SEM(*inode));
375 return 0;
378 static int
379 pipe_write_open(struct inode *inode, struct file *filp)
381 down(PIPE_SEM(*inode));
382 PIPE_WRITERS(*inode)++;
383 up(PIPE_SEM(*inode));
385 return 0;
388 static int
389 pipe_rdwr_open(struct inode *inode, struct file *filp)
391 down(PIPE_SEM(*inode));
392 if (filp->f_mode & FMODE_READ)
393 PIPE_READERS(*inode)++;
394 if (filp->f_mode & FMODE_WRITE)
395 PIPE_WRITERS(*inode)++;
396 up(PIPE_SEM(*inode));
398 return 0;
402 * The file_operations structs are not static because they
403 * are also used in linux/fs/fifo.c to do operations on FIFOs.
405 struct file_operations read_fifo_fops = {
406 .llseek = no_llseek,
407 .read = pipe_read,
408 .write = bad_pipe_w,
409 .poll = fifo_poll,
410 .ioctl = pipe_ioctl,
411 .open = pipe_read_open,
412 .release = pipe_read_release,
413 .fasync = pipe_read_fasync,
416 struct file_operations write_fifo_fops = {
417 .llseek = no_llseek,
418 .read = bad_pipe_r,
419 .write = pipe_write,
420 .poll = fifo_poll,
421 .ioctl = pipe_ioctl,
422 .open = pipe_write_open,
423 .release = pipe_write_release,
424 .fasync = pipe_write_fasync,
427 struct file_operations rdwr_fifo_fops = {
428 .llseek = no_llseek,
429 .read = pipe_read,
430 .write = pipe_write,
431 .poll = fifo_poll,
432 .ioctl = pipe_ioctl,
433 .open = pipe_rdwr_open,
434 .release = pipe_rdwr_release,
435 .fasync = pipe_rdwr_fasync,
438 struct file_operations read_pipe_fops = {
439 .llseek = no_llseek,
440 .read = pipe_read,
441 .write = bad_pipe_w,
442 .poll = pipe_poll,
443 .ioctl = pipe_ioctl,
444 .open = pipe_read_open,
445 .release = pipe_read_release,
446 .fasync = pipe_read_fasync,
449 struct file_operations write_pipe_fops = {
450 .llseek = no_llseek,
451 .read = bad_pipe_r,
452 .write = pipe_write,
453 .poll = pipe_poll,
454 .ioctl = pipe_ioctl,
455 .open = pipe_write_open,
456 .release = pipe_write_release,
457 .fasync = pipe_write_fasync,
460 struct file_operations rdwr_pipe_fops = {
461 .llseek = no_llseek,
462 .read = pipe_read,
463 .write = pipe_write,
464 .poll = pipe_poll,
465 .ioctl = pipe_ioctl,
466 .open = pipe_rdwr_open,
467 .release = pipe_rdwr_release,
468 .fasync = pipe_rdwr_fasync,
471 struct inode* pipe_new(struct inode* inode)
473 unsigned long page;
475 page = __get_free_page(GFP_USER);
476 if (!page)
477 return NULL;
479 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
480 if (!inode->i_pipe)
481 goto fail_page;
483 init_waitqueue_head(PIPE_WAIT(*inode));
484 PIPE_BASE(*inode) = (char*) page;
485 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
486 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 0;
487 PIPE_WAITING_WRITERS(*inode) = 0;
488 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
489 *PIPE_FASYNC_READERS(*inode) = *PIPE_FASYNC_WRITERS(*inode) = NULL;
491 return inode;
492 fail_page:
493 free_page(page);
494 return NULL;
497 static struct vfsmount *pipe_mnt;
498 static int pipefs_delete_dentry(struct dentry *dentry)
500 return 1;
502 static struct dentry_operations pipefs_dentry_operations = {
503 .d_delete = pipefs_delete_dentry,
506 static struct inode * get_pipe_inode(void)
508 struct inode *inode = new_inode(pipe_mnt->mnt_sb);
510 if (!inode)
511 goto fail_inode;
513 if(!pipe_new(inode))
514 goto fail_iput;
515 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
516 inode->i_fop = &rdwr_pipe_fops;
519 * Mark the inode dirty from the very beginning,
520 * that way it will never be moved to the dirty
521 * list because "mark_inode_dirty()" will think
522 * that it already _is_ on the dirty list.
524 inode->i_state = I_DIRTY;
525 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
526 inode->i_uid = current->fsuid;
527 inode->i_gid = current->fsgid;
528 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
529 inode->i_blksize = PAGE_SIZE;
530 return inode;
532 fail_iput:
533 iput(inode);
534 fail_inode:
535 return NULL;
538 int do_pipe(int *fd)
540 struct qstr this;
541 char name[32];
542 struct dentry *dentry;
543 struct inode * inode;
544 struct file *f1, *f2;
545 int error;
546 int i,j;
548 error = -ENFILE;
549 f1 = get_empty_filp();
550 if (!f1)
551 goto no_files;
553 f2 = get_empty_filp();
554 if (!f2)
555 goto close_f1;
557 inode = get_pipe_inode();
558 if (!inode)
559 goto close_f12;
561 error = get_unused_fd();
562 if (error < 0)
563 goto close_f12_inode;
564 i = error;
566 error = get_unused_fd();
567 if (error < 0)
568 goto close_f12_inode_i;
569 j = error;
571 error = -ENOMEM;
572 sprintf(name, "[%lu]", inode->i_ino);
573 this.name = name;
574 this.len = strlen(name);
575 this.hash = inode->i_ino; /* will go */
576 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
577 if (!dentry)
578 goto close_f12_inode_i_j;
579 dentry->d_op = &pipefs_dentry_operations;
580 d_add(dentry, inode);
581 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
582 f1->f_dentry = f2->f_dentry = dget(dentry);
584 /* read file */
585 f1->f_pos = f2->f_pos = 0;
586 f1->f_flags = O_RDONLY;
587 f1->f_op = &read_pipe_fops;
588 f1->f_mode = 1;
589 f1->f_version = 0;
591 /* write file */
592 f2->f_flags = O_WRONLY;
593 f2->f_op = &write_pipe_fops;
594 f2->f_mode = 2;
595 f2->f_version = 0;
597 fd_install(i, f1);
598 fd_install(j, f2);
599 fd[0] = i;
600 fd[1] = j;
601 return 0;
603 close_f12_inode_i_j:
604 put_unused_fd(j);
605 close_f12_inode_i:
606 put_unused_fd(i);
607 close_f12_inode:
608 free_page((unsigned long) PIPE_BASE(*inode));
609 kfree(inode->i_pipe);
610 inode->i_pipe = NULL;
611 iput(inode);
612 close_f12:
613 put_filp(f2);
614 close_f1:
615 put_filp(f1);
616 no_files:
617 return error;
621 * pipefs should _never_ be mounted by userland - too much of security hassle,
622 * no real gain from having the whole whorehouse mounted. So we don't need
623 * any operations on the root directory. However, we need a non-trivial
624 * d_name - pipe: will go nicely and kill the special-casing in procfs.
627 static struct super_block *pipefs_get_sb(struct file_system_type *fs_type,
628 int flags, const char *dev_name, void *data)
630 return get_sb_pseudo(fs_type, "pipe:", NULL, PIPEFS_MAGIC);
633 static struct file_system_type pipe_fs_type = {
634 .name = "pipefs",
635 .get_sb = pipefs_get_sb,
636 .kill_sb = kill_anon_super,
639 static int __init init_pipe_fs(void)
641 int err = register_filesystem(&pipe_fs_type);
642 if (!err) {
643 pipe_mnt = kern_mount(&pipe_fs_type);
644 if (IS_ERR(pipe_mnt)) {
645 err = PTR_ERR(pipe_mnt);
646 unregister_filesystem(&pipe_fs_type);
649 return err;
652 static void __exit exit_pipe_fs(void)
654 unregister_filesystem(&pipe_fs_type);
655 mntput(pipe_mnt);
658 module_init(init_pipe_fs)
659 module_exit(exit_pipe_fs)