Import 2.3.12pre9
[davej-history.git] / fs / pipe.c
blob2303ca32849897cd7fcdcf0f0266e5d1788195a2
1 /*
2 * linux/fs/pipe.c
4 * Copyright (C) 1991, 1992 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>
13 #include <asm/uaccess.h>
16 * Define this if you want SunOS compatibility wrt braindead
17 * select behaviour on FIFO's.
19 #ifdef __sparc__
20 #define FIFO_SUNOS_BRAINDAMAGE
21 #else
22 #undef FIFO_SUNOS_BRAINDAMAGE
23 #endif
25 /* We don't use the head/tail construction any more. Now we use the start/len*/
26 /* construction providing full use of PIPE_BUF (multiple of PAGE_SIZE) */
27 /* Florian Coosmann (FGC) ^ current = 1 */
28 /* Additionally, we now use locking technique. This prevents race condition */
29 /* in case of paging and multiple read/write on the same pipe. (FGC) */
30 /* Reads with count = 0 should always return 0. Julian Bradfield 1999-06-07. */
32 static ssize_t do_pipe_read(struct file * filp, char * buf, size_t count)
34 struct inode * inode = filp->f_dentry->d_inode;
35 ssize_t chars = 0, size = 0, read = 0;
36 char *pipebuf;
38 if (filp->f_flags & O_NONBLOCK) {
39 if (PIPE_LOCK(*inode))
40 return -EAGAIN;
41 if (PIPE_EMPTY(*inode)) {
42 if (PIPE_WRITERS(*inode))
43 return -EAGAIN;
44 else
45 return 0;
47 } else while (PIPE_EMPTY(*inode) || PIPE_LOCK(*inode)) {
48 if (PIPE_EMPTY(*inode)) {
49 if (!PIPE_WRITERS(*inode) || !count)
50 return 0;
52 if (signal_pending(current))
53 return -ERESTARTSYS;
54 interruptible_sleep_on(&PIPE_WAIT(*inode));
56 PIPE_LOCK(*inode)++;
57 while (count>0 && (size = PIPE_SIZE(*inode))) {
58 chars = PIPE_MAX_RCHUNK(*inode);
59 if (chars > count)
60 chars = count;
61 if (chars > size)
62 chars = size;
63 read += chars;
64 pipebuf = PIPE_BASE(*inode)+PIPE_START(*inode);
65 PIPE_START(*inode) += chars;
66 PIPE_START(*inode) &= (PIPE_BUF-1);
67 PIPE_LEN(*inode) -= chars;
68 count -= chars;
69 copy_to_user(buf, pipebuf, chars );
70 buf += chars;
72 PIPE_LOCK(*inode)--;
73 wake_up_interruptible(&PIPE_WAIT(*inode));
74 if (read) {
75 UPDATE_ATIME(inode);
76 return read;
78 if (PIPE_WRITERS(*inode))
79 return -EAGAIN;
80 return 0;
83 static ssize_t do_pipe_write(struct file * filp, const char * buf, size_t count)
85 struct inode * inode = filp->f_dentry->d_inode;
86 ssize_t chars = 0, free = 0, written = 0, err=0;
87 char *pipebuf;
89 if (!PIPE_READERS(*inode)) { /* no readers */
90 send_sig(SIGPIPE,current,0);
91 return -EPIPE;
93 /* if count <= PIPE_BUF, we have to make it atomic */
94 if (count <= PIPE_BUF)
95 free = count;
96 else
97 free = 1; /* can't do it atomically, wait for any free space */
98 if (down_interruptible(&inode->i_sem)) {
99 return -ERESTARTSYS;
101 while (count>0) {
102 while ((PIPE_FREE(*inode) < free) || PIPE_LOCK(*inode)) {
103 if (!PIPE_READERS(*inode)) { /* no readers */
104 send_sig(SIGPIPE,current,0);
105 err = -EPIPE;
106 goto errout;
108 if (signal_pending(current)) {
109 err = -ERESTARTSYS;
110 goto errout;
112 if (filp->f_flags & O_NONBLOCK) {
113 err = -EAGAIN;
114 goto errout;
116 interruptible_sleep_on(&PIPE_WAIT(*inode));
118 PIPE_LOCK(*inode)++;
119 while (count>0 && (free = PIPE_FREE(*inode))) {
120 chars = PIPE_MAX_WCHUNK(*inode);
121 if (chars > count)
122 chars = count;
123 if (chars > free)
124 chars = free;
125 pipebuf = PIPE_BASE(*inode)+PIPE_END(*inode);
126 written += chars;
127 PIPE_LEN(*inode) += chars;
128 count -= chars;
129 copy_from_user(pipebuf, buf, chars );
130 buf += chars;
132 PIPE_LOCK(*inode)--;
133 wake_up_interruptible(&PIPE_WAIT(*inode));
134 free = 1;
136 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
137 mark_inode_dirty(inode);
138 errout:
139 up(&inode->i_sem);
140 return written ? written : err;
143 static ssize_t pipe_read(struct file * filp, char * buf, size_t count, loff_t *ppos)
145 ssize_t retval;
148 if (ppos != &filp->f_pos)
149 return -ESPIPE;
151 if ( !count ) return 0;
153 lock_kernel();
154 retval = do_pipe_read(filp, buf, count);
155 unlock_kernel();
156 return retval;
159 static ssize_t pipe_write(struct file * filp, const char * buf, size_t count, loff_t *ppos)
161 ssize_t retval;
163 if (ppos != &filp->f_pos)
164 return -ESPIPE;
166 lock_kernel();
167 retval = do_pipe_write(filp, buf, count);
168 unlock_kernel();
169 return retval;
172 static long long pipe_lseek(struct file * file, long long offset, int orig)
174 return -ESPIPE;
177 static ssize_t bad_pipe_r(struct file * filp, char * buf,
178 size_t count, loff_t *ppos)
180 return -EBADF;
183 static ssize_t bad_pipe_w(struct file * filp, const char * buf,
184 size_t count, loff_t *ppos)
186 return -EBADF;
189 static int pipe_ioctl(struct inode *pino, struct file * filp,
190 unsigned int cmd, unsigned long arg)
192 switch (cmd) {
193 case FIONREAD:
194 return put_user(PIPE_SIZE(*pino),(int *) arg);
195 default:
196 return -EINVAL;
200 static unsigned int pipe_poll(struct file * filp, poll_table * wait)
202 unsigned int mask;
203 struct inode * inode = filp->f_dentry->d_inode;
205 poll_wait(filp, &PIPE_WAIT(*inode), wait);
206 mask = POLLIN | POLLRDNORM;
207 if (PIPE_EMPTY(*inode))
208 mask = POLLOUT | POLLWRNORM;
209 if (!PIPE_WRITERS(*inode))
210 mask |= POLLHUP;
211 if (!PIPE_READERS(*inode))
212 mask |= POLLERR;
213 return mask;
216 #ifdef FIFO_SUNOS_BRAINDAMAGE
218 * Argh! Why does SunOS have to have different select() behaviour
219 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
221 static unsigned int fifo_poll(struct file * filp, poll_table * wait)
223 unsigned int mask;
224 struct inode * inode = filp->f_dentry->d_inode;
226 poll_wait(filp, &PIPE_WAIT(*inode), wait);
227 mask = POLLIN | POLLRDNORM;
228 if (PIPE_EMPTY(*inode))
229 mask = POLLOUT | POLLWRNORM;
230 if (!PIPE_READERS(*inode))
231 mask |= POLLERR;
232 return mask;
234 #else
236 #define fifo_poll pipe_poll
238 #endif /* FIFO_SUNOS_BRAINDAMAGE */
241 * The 'connect_xxx()' functions are needed for named pipes when
242 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
243 * and we need to act differently until we do get a writer..
245 static ssize_t connect_read(struct file * filp, char * buf,
246 size_t count, loff_t *ppos)
248 struct inode * inode = filp->f_dentry->d_inode;
249 if (PIPE_EMPTY(*inode) && !PIPE_WRITERS(*inode))
250 return 0;
251 filp->f_op = &read_fifo_fops;
252 return pipe_read(filp,buf,count,ppos);
255 static unsigned int connect_poll(struct file * filp, poll_table * wait)
257 struct inode * inode = filp->f_dentry->d_inode;
259 poll_wait(filp, &PIPE_WAIT(*inode), wait);
260 if (!PIPE_EMPTY(*inode)) {
261 filp->f_op = &read_fifo_fops;
262 return POLLIN | POLLRDNORM;
264 if (PIPE_WRITERS(*inode))
265 filp->f_op = &read_fifo_fops;
266 return POLLOUT | POLLWRNORM;
269 static int pipe_release(struct inode * inode)
271 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
272 struct pipe_inode_info *info = inode->i_pipe;
273 inode->i_pipe = NULL;
274 free_page((unsigned long) info->base);
275 kfree(info);
276 return 0;
278 wake_up_interruptible(&PIPE_WAIT(*inode));
279 return 0;
282 static int pipe_read_release(struct inode * inode, struct file * filp)
284 PIPE_READERS(*inode)--;
285 return pipe_release(inode);
288 static int pipe_write_release(struct inode * inode, struct file * filp)
290 PIPE_WRITERS(*inode)--;
291 return pipe_release(inode);
294 static int pipe_rdwr_release(struct inode * inode, struct file * filp)
296 if (filp->f_mode & FMODE_READ)
297 PIPE_READERS(*inode)--;
298 if (filp->f_mode & FMODE_WRITE)
299 PIPE_WRITERS(*inode)--;
300 return pipe_release(inode);
303 static int pipe_read_open(struct inode * inode, struct file * filp)
305 PIPE_READERS(*inode)++;
306 return 0;
309 static int pipe_write_open(struct inode * inode, struct file * filp)
311 PIPE_WRITERS(*inode)++;
312 return 0;
315 static int pipe_rdwr_open(struct inode * inode, struct file * filp)
317 if (filp->f_mode & FMODE_READ)
318 PIPE_READERS(*inode)++;
319 if (filp->f_mode & FMODE_WRITE)
320 PIPE_WRITERS(*inode)++;
321 return 0;
325 * The file_operations structs are not static because they
326 * are also used in linux/fs/fifo.c to do operations on FIFOs.
328 struct file_operations connecting_fifo_fops = {
329 pipe_lseek,
330 connect_read,
331 bad_pipe_w,
332 NULL, /* no readdir */
333 connect_poll,
334 pipe_ioctl,
335 NULL, /* no mmap on pipes.. surprise */
336 pipe_read_open,
337 NULL, /* flush */
338 pipe_read_release,
339 NULL
342 struct file_operations read_fifo_fops = {
343 pipe_lseek,
344 pipe_read,
345 bad_pipe_w,
346 NULL, /* no readdir */
347 fifo_poll,
348 pipe_ioctl,
349 NULL, /* no mmap on pipes.. surprise */
350 pipe_read_open,
351 NULL, /* flush */
352 pipe_read_release,
353 NULL
356 struct file_operations write_fifo_fops = {
357 pipe_lseek,
358 bad_pipe_r,
359 pipe_write,
360 NULL, /* no readdir */
361 fifo_poll,
362 pipe_ioctl,
363 NULL, /* mmap */
364 pipe_write_open,
365 NULL, /* flush */
366 pipe_write_release,
367 NULL
370 struct file_operations rdwr_fifo_fops = {
371 pipe_lseek,
372 pipe_read,
373 pipe_write,
374 NULL, /* no readdir */
375 fifo_poll,
376 pipe_ioctl,
377 NULL, /* mmap */
378 pipe_rdwr_open,
379 NULL, /* flush */
380 pipe_rdwr_release,
381 NULL
384 struct file_operations read_pipe_fops = {
385 pipe_lseek,
386 pipe_read,
387 bad_pipe_w,
388 NULL, /* no readdir */
389 pipe_poll,
390 pipe_ioctl,
391 NULL, /* no mmap on pipes.. surprise */
392 pipe_read_open,
393 NULL, /* flush */
394 pipe_read_release,
395 NULL
398 struct file_operations write_pipe_fops = {
399 pipe_lseek,
400 bad_pipe_r,
401 pipe_write,
402 NULL, /* no readdir */
403 pipe_poll,
404 pipe_ioctl,
405 NULL, /* mmap */
406 pipe_write_open,
407 NULL, /* flush */
408 pipe_write_release,
409 NULL
412 struct file_operations rdwr_pipe_fops = {
413 pipe_lseek,
414 pipe_read,
415 pipe_write,
416 NULL, /* no readdir */
417 pipe_poll,
418 pipe_ioctl,
419 NULL, /* mmap */
420 pipe_rdwr_open,
421 NULL, /* flush */
422 pipe_rdwr_release,
423 NULL
426 static struct inode * get_pipe_inode(void)
428 extern struct inode_operations pipe_inode_operations;
429 struct inode *inode = get_empty_inode();
430 unsigned long page;
432 if (!inode)
433 goto fail_inode;
435 page = __get_free_page(GFP_USER);
437 if (!page)
438 goto fail_iput;
440 /* XXX */
441 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
442 if (!inode->i_pipe)
443 goto fail_page;
445 PIPE_BASE(*inode) = (char *) page;
446 inode->i_op = &pipe_inode_operations;
447 init_waitqueue_head(&PIPE_WAIT(*inode));
448 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
449 PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
450 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
451 PIPE_LOCK(*inode) = 0;
453 * Mark the inode dirty from the very beginning,
454 * that way it will never be moved to the dirty
455 * list because "mark_inode_dirty()" will think
456 * that it already _is_ on the dirty list.
458 inode->i_state = I_DIRTY;
459 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
460 inode->i_uid = current->fsuid;
461 inode->i_gid = current->fsgid;
462 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
463 inode->i_blksize = PAGE_SIZE;
464 return inode;
466 fail_page:
467 free_page(page);
468 fail_iput:
469 iput(inode);
470 fail_inode:
471 return NULL;
474 struct inode_operations pipe_inode_operations = {
475 &rdwr_pipe_fops,
476 NULL, /* create */
477 NULL, /* lookup */
478 NULL, /* link */
479 NULL, /* unlink */
480 NULL, /* symlink */
481 NULL, /* mkdir */
482 NULL, /* rmdir */
483 NULL, /* mknod */
484 NULL, /* rename */
485 NULL, /* readlink */
486 NULL, /* follow_link */
487 NULL, /* get_block */
488 NULL, /* readpage */
489 NULL, /* writepage */
490 NULL, /* flushpage */
491 NULL, /* truncate */
492 NULL, /* permission */
493 NULL, /* smap */
494 NULL /* revalidate */
497 int do_pipe(int *fd)
499 struct inode * inode;
500 struct file *f1, *f2;
501 int error;
502 int i,j;
504 error = -ENFILE;
505 f1 = get_empty_filp();
506 if (!f1)
507 goto no_files;
509 f2 = get_empty_filp();
510 if (!f2)
511 goto close_f1;
513 inode = get_pipe_inode();
514 if (!inode)
515 goto close_f12;
517 error = get_unused_fd();
518 if (error < 0)
519 goto close_f12_inode;
520 i = error;
522 error = get_unused_fd();
523 if (error < 0)
524 goto close_f12_inode_i;
525 j = error;
527 error = -ENOMEM;
528 f1->f_dentry = f2->f_dentry = dget(d_alloc_root(inode));
529 if (!f1->f_dentry)
530 goto close_f12_inode_i_j;
532 /* read file */
533 f1->f_pos = f2->f_pos = 0;
534 f1->f_flags = O_RDONLY;
535 f1->f_op = &read_pipe_fops;
536 f1->f_mode = 1;
538 /* write file */
539 f2->f_flags = O_WRONLY;
540 f2->f_op = &write_pipe_fops;
541 f2->f_mode = 2;
543 fd_install(i, f1);
544 fd_install(j, f2);
545 fd[0] = i;
546 fd[1] = j;
547 return 0;
549 close_f12_inode_i_j:
550 put_unused_fd(j);
551 close_f12_inode_i:
552 put_unused_fd(i);
553 close_f12_inode:
554 free_page((unsigned long) PIPE_BASE(*inode));
555 kfree(inode->i_pipe);
556 inode->i_pipe = NULL;
557 iput(inode);
558 close_f12:
559 put_filp(f2);
560 close_f1:
561 put_filp(f1);
562 no_files:
563 return error;