Import 2.1.118
[davej-history.git] / fs / pipe.c
blob8308eb06ce16f1d8cb67ddae9bd8678af9fdfc21
1 /*
2 * linux/fs/pipe.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/signal.h>
11 #include <linux/fcntl.h>
12 #include <linux/termios.h>
13 #include <linux/mm.h>
14 #include <linux/file.h>
15 #include <linux/poll.h>
17 #include <asm/uaccess.h>
20 * Define this if you want SunOS compatibility wrt braindead
21 * select behaviour on FIFO's.
23 #ifdef __sparc__
24 #define FIFO_SUNOS_BRAINDAMAGE
25 #else
26 #undef FIFO_SUNOS_BRAINDAMAGE
27 #endif
29 /* We don't use the head/tail construction any more. Now we use the start/len*/
30 /* construction providing full use of PIPE_BUF (multiple of PAGE_SIZE) */
31 /* Florian Coosmann (FGC) ^ current = 1 */
32 /* Additionally, we now use locking technique. This prevents race condition */
33 /* in case of paging and multiple read/write on the same pipe. (FGC) */
36 static ssize_t pipe_read(struct file * filp, char * buf,
37 size_t count, loff_t *ppos)
39 struct inode * inode = filp->f_dentry->d_inode;
40 ssize_t chars = 0, size = 0, read = 0;
41 char *pipebuf;
43 if (ppos != &filp->f_pos)
44 return -ESPIPE;
46 if (filp->f_flags & O_NONBLOCK) {
47 if (PIPE_LOCK(*inode))
48 return -EAGAIN;
49 if (PIPE_EMPTY(*inode)) {
50 if (PIPE_WRITERS(*inode))
51 return -EAGAIN;
52 else
53 return 0;
55 } else while (PIPE_EMPTY(*inode) || PIPE_LOCK(*inode)) {
56 if (PIPE_EMPTY(*inode)) {
57 if (!PIPE_WRITERS(*inode) || !count)
58 return 0;
60 if (signal_pending(current))
61 return -ERESTARTSYS;
62 interruptible_sleep_on(&PIPE_WAIT(*inode));
64 PIPE_LOCK(*inode)++;
65 while (count>0 && (size = PIPE_SIZE(*inode))) {
66 chars = PIPE_MAX_RCHUNK(*inode);
67 if (chars > count)
68 chars = count;
69 if (chars > size)
70 chars = size;
71 read += chars;
72 pipebuf = PIPE_BASE(*inode)+PIPE_START(*inode);
73 PIPE_START(*inode) += chars;
74 PIPE_START(*inode) &= (PIPE_BUF-1);
75 PIPE_LEN(*inode) -= chars;
76 count -= chars;
77 copy_to_user(buf, pipebuf, chars );
78 buf += chars;
80 PIPE_LOCK(*inode)--;
81 wake_up_interruptible(&PIPE_WAIT(*inode));
82 if (read) {
83 UPDATE_ATIME(inode);
84 return read;
86 if (PIPE_WRITERS(*inode))
87 return -EAGAIN;
88 return 0;
91 static ssize_t pipe_write(struct file * filp, const char * buf,
92 size_t count, loff_t *ppos)
94 struct inode * inode = filp->f_dentry->d_inode;
95 ssize_t chars = 0, free = 0, written = 0;
96 char *pipebuf;
98 if (ppos != &filp->f_pos)
99 return -ESPIPE;
101 if (!PIPE_READERS(*inode)) { /* no readers */
102 send_sig(SIGPIPE,current,0);
103 return -EPIPE;
105 /* if count <= PIPE_BUF, we have to make it atomic */
106 if (count <= PIPE_BUF)
107 free = count;
108 else
109 free = 1; /* can't do it atomically, wait for any free space */
110 while (count>0) {
111 while ((PIPE_FREE(*inode) < free) || PIPE_LOCK(*inode)) {
112 if (!PIPE_READERS(*inode)) { /* no readers */
113 send_sig(SIGPIPE,current,0);
114 return written? :-EPIPE;
116 if (signal_pending(current))
117 return written? :-ERESTARTSYS;
118 if (filp->f_flags & O_NONBLOCK)
119 return written? :-EAGAIN;
120 interruptible_sleep_on(&PIPE_WAIT(*inode));
122 PIPE_LOCK(*inode)++;
123 while (count>0 && (free = PIPE_FREE(*inode))) {
124 chars = PIPE_MAX_WCHUNK(*inode);
125 if (chars > count)
126 chars = count;
127 if (chars > free)
128 chars = free;
129 pipebuf = PIPE_BASE(*inode)+PIPE_END(*inode);
130 written += chars;
131 PIPE_LEN(*inode) += chars;
132 count -= chars;
133 copy_from_user(pipebuf, buf, chars );
134 buf += chars;
136 PIPE_LOCK(*inode)--;
137 wake_up_interruptible(&PIPE_WAIT(*inode));
138 free = 1;
140 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
141 mark_inode_dirty(inode);
142 return written;
145 static long long pipe_lseek(struct file * file, long long offset, int orig)
147 return -ESPIPE;
150 static ssize_t bad_pipe_r(struct file * filp, char * buf,
151 size_t count, loff_t *ppos)
153 return -EBADF;
156 static ssize_t bad_pipe_w(struct file * filp, const char * buf,
157 size_t count, loff_t *ppos)
159 return -EBADF;
162 static int pipe_ioctl(struct inode *pino, struct file * filp,
163 unsigned int cmd, unsigned long arg)
165 switch (cmd) {
166 case FIONREAD:
167 return put_user(PIPE_SIZE(*pino),(int *) arg);
168 default:
169 return -EINVAL;
173 static unsigned int pipe_poll(struct file * filp, poll_table * wait)
175 unsigned int mask;
176 struct inode * inode = filp->f_dentry->d_inode;
178 poll_wait(filp, &PIPE_WAIT(*inode), wait);
179 mask = POLLIN | POLLRDNORM;
180 if (PIPE_EMPTY(*inode))
181 mask = POLLOUT | POLLWRNORM;
182 if (!PIPE_WRITERS(*inode))
183 mask |= POLLHUP;
184 if (!PIPE_READERS(*inode))
185 mask |= POLLERR;
186 return mask;
189 #ifdef FIFO_SUNOS_BRAINDAMAGE
191 * Argh! Why does SunOS have to have different select() behaviour
192 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
194 static unsigned int fifo_poll(struct file * filp, poll_table * wait)
196 unsigned int mask;
197 struct inode * inode = filp->f_dentry->d_inode;
199 poll_wait(filp, &PIPE_WAIT(*inode), wait);
200 mask = POLLIN | POLLRDNORM;
201 if (PIPE_EMPTY(*inode))
202 mask = POLLOUT | POLLWRNORM;
203 if (!PIPE_READERS(*inode))
204 mask |= POLLERR;
205 return mask;
207 #else
209 #define fifo_poll pipe_poll
211 #endif /* FIFO_SUNOS_BRAINDAMAGE */
214 * The 'connect_xxx()' functions are needed for named pipes when
215 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
216 * and we need to act differently until we do get a writer..
218 static ssize_t connect_read(struct file * filp, char * buf,
219 size_t count, loff_t *ppos)
221 struct inode * inode = filp->f_dentry->d_inode;
222 if (PIPE_EMPTY(*inode) && !PIPE_WRITERS(*inode))
223 return 0;
224 filp->f_op = &read_fifo_fops;
225 return pipe_read(filp,buf,count,ppos);
228 static unsigned int connect_poll(struct file * filp, poll_table * wait)
230 struct inode * inode = filp->f_dentry->d_inode;
232 poll_wait(filp, &PIPE_WAIT(*inode), wait);
233 if (!PIPE_EMPTY(*inode)) {
234 filp->f_op = &read_fifo_fops;
235 return POLLIN | POLLRDNORM;
237 if (PIPE_WRITERS(*inode))
238 filp->f_op = &read_fifo_fops;
239 return POLLOUT | POLLWRNORM;
242 static int pipe_release(struct inode * inode)
244 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
245 free_page((unsigned long) PIPE_BASE(*inode));
246 PIPE_BASE(*inode) = NULL;
248 wake_up_interruptible(&PIPE_WAIT(*inode));
249 return 0;
252 static int pipe_read_release(struct inode * inode, struct file * filp)
254 PIPE_READERS(*inode)--;
255 return pipe_release(inode);
258 static int pipe_write_release(struct inode * inode, struct file * filp)
260 PIPE_WRITERS(*inode)--;
261 return pipe_release(inode);
264 static int pipe_rdwr_release(struct inode * inode, struct file * filp)
266 if (filp->f_mode & FMODE_READ)
267 PIPE_READERS(*inode)--;
268 if (filp->f_mode & FMODE_WRITE)
269 PIPE_WRITERS(*inode)--;
270 return pipe_release(inode);
273 static int pipe_read_open(struct inode * inode, struct file * filp)
275 PIPE_READERS(*inode)++;
276 return 0;
279 static int pipe_write_open(struct inode * inode, struct file * filp)
281 PIPE_WRITERS(*inode)++;
282 return 0;
285 static int pipe_rdwr_open(struct inode * inode, struct file * filp)
287 if (filp->f_mode & FMODE_READ)
288 PIPE_READERS(*inode)++;
289 if (filp->f_mode & FMODE_WRITE)
290 PIPE_WRITERS(*inode)++;
291 return 0;
295 * The file_operations structs are not static because they
296 * are also used in linux/fs/fifo.c to do operations on FIFOs.
298 struct file_operations connecting_fifo_fops = {
299 pipe_lseek,
300 connect_read,
301 bad_pipe_w,
302 NULL, /* no readdir */
303 connect_poll,
304 pipe_ioctl,
305 NULL, /* no mmap on pipes.. surprise */
306 pipe_read_open,
307 NULL, /* flush */
308 pipe_read_release,
309 NULL
312 struct file_operations read_fifo_fops = {
313 pipe_lseek,
314 pipe_read,
315 bad_pipe_w,
316 NULL, /* no readdir */
317 fifo_poll,
318 pipe_ioctl,
319 NULL, /* no mmap on pipes.. surprise */
320 pipe_read_open,
321 NULL, /* flush */
322 pipe_read_release,
323 NULL
326 struct file_operations write_fifo_fops = {
327 pipe_lseek,
328 bad_pipe_r,
329 pipe_write,
330 NULL, /* no readdir */
331 fifo_poll,
332 pipe_ioctl,
333 NULL, /* mmap */
334 pipe_write_open,
335 NULL, /* flush */
336 pipe_write_release,
337 NULL
340 struct file_operations rdwr_fifo_fops = {
341 pipe_lseek,
342 pipe_read,
343 pipe_write,
344 NULL, /* no readdir */
345 fifo_poll,
346 pipe_ioctl,
347 NULL, /* mmap */
348 pipe_rdwr_open,
349 NULL, /* flush */
350 pipe_rdwr_release,
351 NULL
354 struct file_operations read_pipe_fops = {
355 pipe_lseek,
356 pipe_read,
357 bad_pipe_w,
358 NULL, /* no readdir */
359 pipe_poll,
360 pipe_ioctl,
361 NULL, /* no mmap on pipes.. surprise */
362 pipe_read_open,
363 NULL, /* flush */
364 pipe_read_release,
365 NULL
368 struct file_operations write_pipe_fops = {
369 pipe_lseek,
370 bad_pipe_r,
371 pipe_write,
372 NULL, /* no readdir */
373 pipe_poll,
374 pipe_ioctl,
375 NULL, /* mmap */
376 pipe_write_open,
377 NULL, /* flush */
378 pipe_write_release,
379 NULL
382 struct file_operations rdwr_pipe_fops = {
383 pipe_lseek,
384 pipe_read,
385 pipe_write,
386 NULL, /* no readdir */
387 pipe_poll,
388 pipe_ioctl,
389 NULL, /* mmap */
390 pipe_rdwr_open,
391 NULL, /* flush */
392 pipe_rdwr_release,
393 NULL
396 static struct inode * get_pipe_inode(void)
398 extern struct inode_operations pipe_inode_operations;
399 struct inode *inode = get_empty_inode();
401 if (inode) {
402 unsigned long page = __get_free_page(GFP_USER);
404 if (!page) {
405 iput(inode);
406 inode = NULL;
407 } else {
408 PIPE_BASE(*inode) = (char *) page;
409 inode->i_op = &pipe_inode_operations;
410 PIPE_WAIT(*inode) = NULL;
411 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
412 PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
413 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
414 PIPE_LOCK(*inode) = 0;
416 * Mark the inode dirty from the very beginning,
417 * that way it will never be moved to the dirty
418 * list because "mark_inode_dirty()" will think
419 * that it already _is_ on the dirty list.
421 inode->i_state = I_DIRTY;
422 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
423 inode->i_uid = current->fsuid;
424 inode->i_gid = current->fsgid;
425 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
426 inode->i_blksize = PAGE_SIZE;
429 return inode;
432 struct inode_operations pipe_inode_operations = {
433 &rdwr_pipe_fops,
434 NULL, /* create */
435 NULL, /* lookup */
436 NULL, /* link */
437 NULL, /* unlink */
438 NULL, /* symlink */
439 NULL, /* mkdir */
440 NULL, /* rmdir */
441 NULL, /* mknod */
442 NULL, /* rename */
443 NULL, /* readlink */
444 NULL, /* readpage */
445 NULL, /* writepage */
446 NULL, /* bmap */
447 NULL, /* truncate */
448 NULL /* permission */
451 int do_pipe(int *fd)
453 struct inode * inode;
454 struct file *f1, *f2;
455 int error;
456 int i,j;
458 error = -ENFILE;
459 f1 = get_empty_filp();
460 if (!f1)
461 goto no_files;
463 f2 = get_empty_filp();
464 if (!f2)
465 goto close_f1;
467 inode = get_pipe_inode();
468 if (!inode)
469 goto close_f12;
471 error = get_unused_fd();
472 if (error < 0)
473 goto close_f12_inode;
474 i = error;
476 error = get_unused_fd();
477 if (error < 0)
478 goto close_f12_inode_i;
479 j = error;
481 error = -ENOMEM;
482 f1->f_dentry = f2->f_dentry = dget(d_alloc_root(inode, NULL));
483 if (!f1->f_dentry)
484 goto close_f12_inode_i_j;
486 /* read file */
487 f1->f_pos = f2->f_pos = 0;
488 f1->f_flags = O_RDONLY;
489 f1->f_op = &read_pipe_fops;
490 f1->f_mode = 1;
492 /* write file */
493 f2->f_flags = O_WRONLY;
494 f2->f_op = &write_pipe_fops;
495 f2->f_mode = 2;
497 fd_install(i, f1);
498 fd_install(j, f2);
499 fd[0] = i;
500 fd[1] = j;
501 return 0;
503 close_f12_inode_i_j:
504 put_unused_fd(j);
505 close_f12_inode_i:
506 put_unused_fd(i);
507 close_f12_inode:
508 free_page((unsigned long) PIPE_BASE(*inode));
509 iput(inode);
510 close_f12:
511 put_filp(f2);
512 close_f1:
513 put_filp(f1);
514 no_files:
515 return error;