Remove references to CONFIG_PROFILE. Kernel profiling is no longer a
[linux-2.6/linux-mips.git] / fs / pipe.c
bloba30985a538bac83b7f6ecb6c71f07b83517105da
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/malloc.h>
11 #include <linux/smp_lock.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <asm/uaccess.h>
18 * We use a start+len construction, which provides full use of the
19 * allocated memory.
20 * -- Florian Coosmann (FGC)
22 * Reads with count = 0 should always return 0.
23 * -- Julian Bradfield 1999-06-07.
26 /* Drop the inode semaphore and wait for a pipe event, atomically */
27 void pipe_wait(struct inode * inode)
29 DECLARE_WAITQUEUE(wait, current);
30 current->state = TASK_INTERRUPTIBLE;
31 add_wait_queue(PIPE_WAIT(*inode), &wait);
32 up(PIPE_SEM(*inode));
33 schedule();
34 remove_wait_queue(PIPE_WAIT(*inode), &wait);
35 current->state = TASK_RUNNING;
36 down(PIPE_SEM(*inode));
39 static ssize_t
40 pipe_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
42 struct inode *inode = filp->f_dentry->d_inode;
43 ssize_t size, read, ret;
45 /* Seeks are not allowed on pipes. */
46 ret = -ESPIPE;
47 read = 0;
48 if (ppos != &filp->f_pos)
49 goto out_nolock;
51 /* Always return 0 on null read. */
52 ret = 0;
53 if (count == 0)
54 goto out_nolock;
56 /* Get the pipe semaphore */
57 ret = -ERESTARTSYS;
58 if (down_interruptible(PIPE_SEM(*inode)))
59 goto out_nolock;
61 if (PIPE_EMPTY(*inode)) {
62 do_more_read:
63 ret = 0;
64 if (!PIPE_WRITERS(*inode))
65 goto out;
67 ret = -EAGAIN;
68 if (filp->f_flags & O_NONBLOCK)
69 goto out;
71 for (;;) {
72 PIPE_WAITING_READERS(*inode)++;
73 pipe_wait(inode);
74 PIPE_WAITING_READERS(*inode)--;
75 ret = -ERESTARTSYS;
76 if (signal_pending(current))
77 goto out;
78 ret = 0;
79 if (!PIPE_EMPTY(*inode))
80 break;
81 if (!PIPE_WRITERS(*inode))
82 goto out;
86 /* Read what data is available. */
87 ret = -EFAULT;
88 while (count > 0 && (size = PIPE_LEN(*inode))) {
89 char *pipebuf = PIPE_BASE(*inode) + PIPE_START(*inode);
90 ssize_t chars = PIPE_MAX_RCHUNK(*inode);
92 if (chars > count)
93 chars = count;
94 if (chars > size)
95 chars = size;
97 if (copy_to_user(buf, pipebuf, chars))
98 goto out;
100 read += chars;
101 PIPE_START(*inode) += chars;
102 PIPE_START(*inode) &= (PIPE_SIZE - 1);
103 PIPE_LEN(*inode) -= chars;
104 count -= chars;
105 buf += chars;
108 /* Cache behaviour optimization */
109 if (!PIPE_LEN(*inode))
110 PIPE_START(*inode) = 0;
112 if (count && PIPE_WAITING_WRITERS(*inode) && !(filp->f_flags & O_NONBLOCK)) {
114 * We know that we are going to sleep: signal
115 * writers synchronously that there is more
116 * room.
118 wake_up_interruptible_sync(PIPE_WAIT(*inode));
119 if (!PIPE_EMPTY(*inode))
120 BUG();
121 goto do_more_read;
123 /* Signal writers asynchronously that there is more room. */
124 wake_up_interruptible(PIPE_WAIT(*inode));
126 ret = read;
127 out:
128 up(PIPE_SEM(*inode));
129 out_nolock:
130 if (read)
131 ret = read;
132 return ret;
135 static ssize_t
136 pipe_write(struct file *filp, const char *buf, size_t count, loff_t *ppos)
138 struct inode *inode = filp->f_dentry->d_inode;
139 ssize_t free, written, ret;
141 /* Seeks are not allowed on pipes. */
142 ret = -ESPIPE;
143 written = 0;
144 if (ppos != &filp->f_pos)
145 goto out_nolock;
147 /* Null write succeeds. */
148 ret = 0;
149 if (count == 0)
150 goto out_nolock;
152 ret = -ERESTARTSYS;
153 if (down_interruptible(PIPE_SEM(*inode)))
154 goto out_nolock;
156 /* No readers yields SIGPIPE. */
157 if (!PIPE_READERS(*inode))
158 goto sigpipe;
160 /* If count <= PIPE_BUF, we have to make it atomic. */
161 free = (count <= PIPE_BUF ? count : 1);
163 /* Wait, or check for, available space. */
164 if (filp->f_flags & O_NONBLOCK) {
165 ret = -EAGAIN;
166 if (PIPE_FREE(*inode) < free)
167 goto out;
168 } else {
169 while (PIPE_FREE(*inode) < free) {
170 PIPE_WAITING_WRITERS(*inode)++;
171 pipe_wait(inode);
172 PIPE_WAITING_WRITERS(*inode)--;
173 ret = -ERESTARTSYS;
174 if (signal_pending(current))
175 goto out;
177 if (!PIPE_READERS(*inode))
178 goto sigpipe;
182 /* Copy into available space. */
183 ret = -EFAULT;
184 while (count > 0) {
185 int space;
186 char *pipebuf = PIPE_BASE(*inode) + PIPE_END(*inode);
187 ssize_t chars = PIPE_MAX_WCHUNK(*inode);
189 if ((space = PIPE_FREE(*inode)) != 0) {
190 if (chars > count)
191 chars = count;
192 if (chars > space)
193 chars = space;
195 if (copy_from_user(pipebuf, buf, chars))
196 goto out;
198 written += chars;
199 PIPE_LEN(*inode) += chars;
200 count -= chars;
201 buf += chars;
202 space = PIPE_FREE(*inode);
203 continue;
206 ret = written;
207 if (filp->f_flags & O_NONBLOCK)
208 break;
210 do {
212 * Synchronous wake-up: it knows that this process
213 * is going to give up this CPU, so it doesnt have
214 * to do idle reschedules.
216 wake_up_interruptible_sync(PIPE_WAIT(*inode));
217 PIPE_WAITING_WRITERS(*inode)++;
218 pipe_wait(inode);
219 PIPE_WAITING_WRITERS(*inode)--;
220 if (signal_pending(current))
221 goto out;
222 if (!PIPE_READERS(*inode))
223 goto sigpipe;
224 } while (!PIPE_FREE(*inode));
225 ret = -EFAULT;
228 /* Signal readers asynchronously that there is more data. */
229 wake_up_interruptible(PIPE_WAIT(*inode));
231 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
232 mark_inode_dirty(inode);
234 out:
235 up(PIPE_SEM(*inode));
236 out_nolock:
237 if (written)
238 ret = written;
239 return ret;
241 sigpipe:
242 if (written)
243 goto out;
244 up(PIPE_SEM(*inode));
245 send_sig(SIGPIPE, current, 0);
246 return -EPIPE;
249 static loff_t
250 pipe_lseek(struct file *file, loff_t offset, int orig)
252 return -ESPIPE;
255 static ssize_t
256 bad_pipe_r(struct file *filp, char *buf, size_t count, loff_t *ppos)
258 return -EBADF;
261 static ssize_t
262 bad_pipe_w(struct file *filp, const char *buf, size_t count, loff_t *ppos)
264 return -EBADF;
267 static int
268 pipe_ioctl(struct inode *pino, struct file *filp,
269 unsigned int cmd, unsigned long arg)
271 switch (cmd) {
272 case FIONREAD:
273 return put_user(PIPE_LEN(*pino), (int *)arg);
274 default:
275 return -EINVAL;
279 /* No kernel lock held - fine */
280 static unsigned int
281 pipe_poll(struct file *filp, poll_table *wait)
283 unsigned int mask;
284 struct inode *inode = filp->f_dentry->d_inode;
286 poll_wait(filp, PIPE_WAIT(*inode), wait);
288 /* Reading only -- no need for aquiring the semaphore. */
289 mask = POLLIN | POLLRDNORM;
290 if (PIPE_EMPTY(*inode))
291 mask = POLLOUT | POLLWRNORM;
292 if (!PIPE_WRITERS(*inode) && filp->f_version != PIPE_WCOUNTER(*inode))
293 mask |= POLLHUP;
294 if (!PIPE_READERS(*inode))
295 mask |= POLLERR;
297 return mask;
300 /* FIXME: most Unices do not set POLLERR for fifos */
301 #define fifo_poll pipe_poll
303 static int
304 pipe_release(struct inode *inode, int decr, int decw)
306 down(PIPE_SEM(*inode));
307 PIPE_READERS(*inode) -= decr;
308 PIPE_WRITERS(*inode) -= decw;
309 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
310 struct pipe_inode_info *info = inode->i_pipe;
311 inode->i_pipe = NULL;
312 free_page((unsigned long) info->base);
313 kfree(info);
314 } else {
315 wake_up_interruptible(PIPE_WAIT(*inode));
317 up(PIPE_SEM(*inode));
319 return 0;
322 static int
323 pipe_read_release(struct inode *inode, struct file *filp)
325 return pipe_release(inode, 1, 0);
328 static int
329 pipe_write_release(struct inode *inode, struct file *filp)
331 return pipe_release(inode, 0, 1);
334 static int
335 pipe_rdwr_release(struct inode *inode, struct file *filp)
337 int decr, decw;
339 decr = (filp->f_mode & FMODE_READ) != 0;
340 decw = (filp->f_mode & FMODE_WRITE) != 0;
341 return pipe_release(inode, decr, decw);
344 static int
345 pipe_read_open(struct inode *inode, struct file *filp)
347 /* We could have perhaps used atomic_t, but this and friends
348 below are the only places. So it doesn't seem worthwhile. */
349 down(PIPE_SEM(*inode));
350 PIPE_READERS(*inode)++;
351 up(PIPE_SEM(*inode));
353 return 0;
356 static int
357 pipe_write_open(struct inode *inode, struct file *filp)
359 down(PIPE_SEM(*inode));
360 PIPE_WRITERS(*inode)++;
361 up(PIPE_SEM(*inode));
363 return 0;
366 static int
367 pipe_rdwr_open(struct inode *inode, struct file *filp)
369 down(PIPE_SEM(*inode));
370 if (filp->f_mode & FMODE_READ)
371 PIPE_READERS(*inode)++;
372 if (filp->f_mode & FMODE_WRITE)
373 PIPE_WRITERS(*inode)++;
374 up(PIPE_SEM(*inode));
376 return 0;
380 * The file_operations structs are not static because they
381 * are also used in linux/fs/fifo.c to do operations on FIFOs.
383 struct file_operations read_fifo_fops = {
384 llseek: pipe_lseek,
385 read: pipe_read,
386 write: bad_pipe_w,
387 poll: fifo_poll,
388 ioctl: pipe_ioctl,
389 open: pipe_read_open,
390 release: pipe_read_release,
393 struct file_operations write_fifo_fops = {
394 llseek: pipe_lseek,
395 read: bad_pipe_r,
396 write: pipe_write,
397 poll: fifo_poll,
398 ioctl: pipe_ioctl,
399 open: pipe_write_open,
400 release: pipe_write_release,
403 struct file_operations rdwr_fifo_fops = {
404 llseek: pipe_lseek,
405 read: pipe_read,
406 write: pipe_write,
407 poll: fifo_poll,
408 ioctl: pipe_ioctl,
409 open: pipe_rdwr_open,
410 release: pipe_rdwr_release,
413 struct file_operations read_pipe_fops = {
414 llseek: pipe_lseek,
415 read: pipe_read,
416 write: bad_pipe_w,
417 poll: pipe_poll,
418 ioctl: pipe_ioctl,
419 open: pipe_read_open,
420 release: pipe_read_release,
423 struct file_operations write_pipe_fops = {
424 llseek: pipe_lseek,
425 read: bad_pipe_r,
426 write: pipe_write,
427 poll: pipe_poll,
428 ioctl: pipe_ioctl,
429 open: pipe_write_open,
430 release: pipe_write_release,
433 struct file_operations rdwr_pipe_fops = {
434 llseek: pipe_lseek,
435 read: pipe_read,
436 write: pipe_write,
437 poll: pipe_poll,
438 ioctl: pipe_ioctl,
439 open: pipe_rdwr_open,
440 release: pipe_rdwr_release,
443 struct inode* pipe_new(struct inode* inode)
445 unsigned long page;
447 page = __get_free_page(GFP_USER);
448 if (!page)
449 return NULL;
451 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
452 if (!inode->i_pipe)
453 goto fail_page;
455 init_waitqueue_head(PIPE_WAIT(*inode));
456 PIPE_BASE(*inode) = (char*) page;
457 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
458 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 0;
459 PIPE_WAITING_READERS(*inode) = PIPE_WAITING_WRITERS(*inode) = 0;
460 PIPE_RCOUNTER(*inode) = PIPE_WCOUNTER(*inode) = 1;
462 return inode;
463 fail_page:
464 free_page(page);
465 return NULL;
468 static struct vfsmount *pipe_mnt;
470 static struct inode * get_pipe_inode(void)
472 struct inode *inode = get_empty_inode();
474 if (!inode)
475 goto fail_inode;
477 if(!pipe_new(inode))
478 goto fail_iput;
479 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
480 inode->i_fop = &rdwr_pipe_fops;
481 inode->i_sb = pipe_mnt->mnt_sb;
484 * Mark the inode dirty from the very beginning,
485 * that way it will never be moved to the dirty
486 * list because "mark_inode_dirty()" will think
487 * that it already _is_ on the dirty list.
489 inode->i_state = I_DIRTY;
490 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
491 inode->i_uid = current->fsuid;
492 inode->i_gid = current->fsgid;
493 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
494 inode->i_blksize = PAGE_SIZE;
495 return inode;
497 fail_iput:
498 iput(inode);
499 fail_inode:
500 return NULL;
503 int do_pipe(int *fd)
505 struct qstr this;
506 char name[32];
507 struct dentry *dentry;
508 struct inode * inode;
509 struct file *f1, *f2;
510 int error;
511 int i,j;
513 error = -ENFILE;
514 f1 = get_empty_filp();
515 if (!f1)
516 goto no_files;
518 f2 = get_empty_filp();
519 if (!f2)
520 goto close_f1;
522 inode = get_pipe_inode();
523 if (!inode)
524 goto close_f12;
526 error = get_unused_fd();
527 if (error < 0)
528 goto close_f12_inode;
529 i = error;
531 error = get_unused_fd();
532 if (error < 0)
533 goto close_f12_inode_i;
534 j = error;
536 error = -ENOMEM;
537 sprintf(name, "%lu", inode->i_ino);
538 this.name = name;
539 this.len = strlen(name);
540 /* We don't care for hash - it will never be looked up */
541 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &this);
542 if (!dentry)
543 goto close_f12_inode_i_j;
544 d_instantiate(dentry, inode);
545 f1->f_vfsmnt = f2->f_vfsmnt = mntget(mntget(pipe_mnt));
546 f1->f_dentry = f2->f_dentry = dget(dentry);
548 /* read file */
549 f1->f_pos = f2->f_pos = 0;
550 f1->f_flags = O_RDONLY;
551 f1->f_op = &read_pipe_fops;
552 f1->f_mode = 1;
553 f1->f_version = 0;
555 /* write file */
556 f2->f_flags = O_WRONLY;
557 f2->f_op = &write_pipe_fops;
558 f2->f_mode = 2;
559 f2->f_version = 0;
561 fd_install(i, f1);
562 fd_install(j, f2);
563 fd[0] = i;
564 fd[1] = j;
565 return 0;
567 close_f12_inode_i_j:
568 put_unused_fd(j);
569 close_f12_inode_i:
570 put_unused_fd(i);
571 close_f12_inode:
572 free_page((unsigned long) PIPE_BASE(*inode));
573 kfree(inode->i_pipe);
574 inode->i_pipe = NULL;
575 iput(inode);
576 close_f12:
577 put_filp(f2);
578 close_f1:
579 put_filp(f1);
580 no_files:
581 return error;
585 * pipefs should _never_ be mounted by userland - too much of security hassle,
586 * no real gain from having the whole whorehouse mounted. So we don't need
587 * any operations on the root directory. However, we need a non-trivial
588 * d_name - pipe: will go nicely and kill the special-casing in procfs.
590 static int pipefs_statfs(struct super_block *sb, struct statfs *buf)
592 buf->f_type = PIPEFS_MAGIC;
593 buf->f_bsize = 1024;
594 buf->f_namelen = 255;
595 return 0;
598 static struct super_operations pipefs_ops = {
599 statfs: pipefs_statfs,
602 static struct super_block * pipefs_read_super(struct super_block *sb, void *data, int silent)
604 struct inode *root = get_empty_inode();
605 if (!root)
606 return NULL;
607 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
608 root->i_uid = root->i_gid = 0;
609 root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
610 root->i_sb = sb;
611 root->i_dev = sb->s_dev;
612 sb->s_blocksize = 1024;
613 sb->s_blocksize_bits = 10;
614 sb->s_magic = PIPEFS_MAGIC;
615 sb->s_op = &pipefs_ops;
616 sb->s_root = d_alloc(NULL, &(const struct qstr) { "pipe:", 5, 0 });
617 if (!sb->s_root) {
618 iput(root);
619 return NULL;
621 sb->s_root->d_sb = sb;
622 sb->s_root->d_parent = sb->s_root;
623 d_instantiate(sb->s_root, root);
624 return sb;
627 static DECLARE_FSTYPE(pipe_fs_type, "pipefs", pipefs_read_super,
628 FS_NOMOUNT|FS_SINGLE);
630 static int __init init_pipe_fs(void)
632 int err = register_filesystem(&pipe_fs_type);
633 if (!err) {
634 pipe_mnt = kern_mount(&pipe_fs_type);
635 err = PTR_ERR(pipe_mnt);
636 if (!IS_ERR(pipe_mnt))
637 err = 0;
639 return err;
642 static void __exit exit_pipe_fs(void)
644 unregister_filesystem(&pipe_fs_type);
645 kern_umount(pipe_mnt);
648 module_init(init_pipe_fs)
649 module_exit(exit_pipe_fs)