Import 2.3.9pre5
[davej-history.git] / fs / pipe.c
blobc68114035f40116fd51f7237fca9f4ff41d72481
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>
12 #include <asm/uaccess.h>
15 * Define this if you want SunOS compatibility wrt braindead
16 * select behaviour on FIFO's.
18 #ifdef __sparc__
19 #define FIFO_SUNOS_BRAINDAMAGE
20 #else
21 #undef FIFO_SUNOS_BRAINDAMAGE
22 #endif
24 /* We don't use the head/tail construction any more. Now we use the start/len*/
25 /* construction providing full use of PIPE_BUF (multiple of PAGE_SIZE) */
26 /* Florian Coosmann (FGC) ^ current = 1 */
27 /* Additionally, we now use locking technique. This prevents race condition */
28 /* in case of paging and multiple read/write on the same pipe. (FGC) */
31 static ssize_t pipe_read(struct file * filp, char * buf,
32 size_t count, loff_t *ppos)
34 struct inode * inode = filp->f_dentry->d_inode;
35 ssize_t chars = 0, size = 0, read = 0;
36 char *pipebuf;
38 if (ppos != &filp->f_pos)
39 return -ESPIPE;
41 if (filp->f_flags & O_NONBLOCK) {
42 if (PIPE_LOCK(*inode))
43 return -EAGAIN;
44 if (PIPE_EMPTY(*inode)) {
45 if (PIPE_WRITERS(*inode))
46 return -EAGAIN;
47 else
48 return 0;
50 } else while (PIPE_EMPTY(*inode) || PIPE_LOCK(*inode)) {
51 if (PIPE_EMPTY(*inode)) {
52 if (!PIPE_WRITERS(*inode) || !count)
53 return 0;
55 if (signal_pending(current))
56 return -ERESTARTSYS;
57 interruptible_sleep_on(&PIPE_WAIT(*inode));
59 PIPE_LOCK(*inode)++;
60 while (count>0 && (size = PIPE_SIZE(*inode))) {
61 chars = PIPE_MAX_RCHUNK(*inode);
62 if (chars > count)
63 chars = count;
64 if (chars > size)
65 chars = size;
66 read += chars;
67 pipebuf = PIPE_BASE(*inode)+PIPE_START(*inode);
68 PIPE_START(*inode) += chars;
69 PIPE_START(*inode) &= (PIPE_BUF-1);
70 PIPE_LEN(*inode) -= chars;
71 count -= chars;
72 copy_to_user(buf, pipebuf, chars );
73 buf += chars;
75 PIPE_LOCK(*inode)--;
76 wake_up_interruptible(&PIPE_WAIT(*inode));
77 if (read) {
78 UPDATE_ATIME(inode);
79 return read;
81 if (PIPE_WRITERS(*inode))
82 return -EAGAIN;
83 return 0;
86 static ssize_t pipe_write(struct file * filp, const char * buf,
87 size_t count, loff_t *ppos)
89 struct inode * inode = filp->f_dentry->d_inode;
90 ssize_t chars = 0, free = 0, written = 0, err=0;
91 char *pipebuf;
93 if (ppos != &filp->f_pos)
94 return -ESPIPE;
96 if (!PIPE_READERS(*inode)) { /* no readers */
97 send_sig(SIGPIPE,current,0);
98 return -EPIPE;
100 /* if count <= PIPE_BUF, we have to make it atomic */
101 if (count <= PIPE_BUF)
102 free = count;
103 else
104 free = 1; /* can't do it atomically, wait for any free space */
105 if (down_interruptible(&inode->i_sem)) {
106 return -ERESTARTSYS;
108 while (count>0) {
109 while ((PIPE_FREE(*inode) < free) || PIPE_LOCK(*inode)) {
110 if (!PIPE_READERS(*inode)) { /* no readers */
111 send_sig(SIGPIPE,current,0);
112 err = -EPIPE;
113 goto errout;
115 if (signal_pending(current)) {
116 err = -ERESTARTSYS;
117 goto errout;
119 if (filp->f_flags & O_NONBLOCK) {
120 err = -EAGAIN;
121 goto errout;
123 interruptible_sleep_on(&PIPE_WAIT(*inode));
125 PIPE_LOCK(*inode)++;
126 while (count>0 && (free = PIPE_FREE(*inode))) {
127 chars = PIPE_MAX_WCHUNK(*inode);
128 if (chars > count)
129 chars = count;
130 if (chars > free)
131 chars = free;
132 pipebuf = PIPE_BASE(*inode)+PIPE_END(*inode);
133 written += chars;
134 PIPE_LEN(*inode) += chars;
135 count -= chars;
136 copy_from_user(pipebuf, buf, chars );
137 buf += chars;
139 PIPE_LOCK(*inode)--;
140 wake_up_interruptible(&PIPE_WAIT(*inode));
141 free = 1;
143 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
144 mark_inode_dirty(inode);
145 errout:
146 up(&inode->i_sem);
147 return written ? written : err;
150 static long long pipe_lseek(struct file * file, long long offset, int orig)
152 return -ESPIPE;
155 static ssize_t bad_pipe_r(struct file * filp, char * buf,
156 size_t count, loff_t *ppos)
158 return -EBADF;
161 static ssize_t bad_pipe_w(struct file * filp, const char * buf,
162 size_t count, loff_t *ppos)
164 return -EBADF;
167 static int pipe_ioctl(struct inode *pino, struct file * filp,
168 unsigned int cmd, unsigned long arg)
170 switch (cmd) {
171 case FIONREAD:
172 return put_user(PIPE_SIZE(*pino),(int *) arg);
173 default:
174 return -EINVAL;
178 static unsigned int pipe_poll(struct file * filp, poll_table * wait)
180 unsigned int mask;
181 struct inode * inode = filp->f_dentry->d_inode;
183 poll_wait(filp, &PIPE_WAIT(*inode), wait);
184 mask = POLLIN | POLLRDNORM;
185 if (PIPE_EMPTY(*inode))
186 mask = POLLOUT | POLLWRNORM;
187 if (!PIPE_WRITERS(*inode))
188 mask |= POLLHUP;
189 if (!PIPE_READERS(*inode))
190 mask |= POLLERR;
191 return mask;
194 #ifdef FIFO_SUNOS_BRAINDAMAGE
196 * Argh! Why does SunOS have to have different select() behaviour
197 * for pipes and FIFOs? Hate, hate, hate! SunOS lacks POLLHUP.
199 static unsigned int fifo_poll(struct file * filp, poll_table * wait)
201 unsigned int mask;
202 struct inode * inode = filp->f_dentry->d_inode;
204 poll_wait(filp, &PIPE_WAIT(*inode), wait);
205 mask = POLLIN | POLLRDNORM;
206 if (PIPE_EMPTY(*inode))
207 mask = POLLOUT | POLLWRNORM;
208 if (!PIPE_READERS(*inode))
209 mask |= POLLERR;
210 return mask;
212 #else
214 #define fifo_poll pipe_poll
216 #endif /* FIFO_SUNOS_BRAINDAMAGE */
219 * The 'connect_xxx()' functions are needed for named pipes when
220 * the open() code hasn't guaranteed a connection (O_NONBLOCK),
221 * and we need to act differently until we do get a writer..
223 static ssize_t connect_read(struct file * filp, char * buf,
224 size_t count, loff_t *ppos)
226 struct inode * inode = filp->f_dentry->d_inode;
227 if (PIPE_EMPTY(*inode) && !PIPE_WRITERS(*inode))
228 return 0;
229 filp->f_op = &read_fifo_fops;
230 return pipe_read(filp,buf,count,ppos);
233 static unsigned int connect_poll(struct file * filp, poll_table * wait)
235 struct inode * inode = filp->f_dentry->d_inode;
237 poll_wait(filp, &PIPE_WAIT(*inode), wait);
238 if (!PIPE_EMPTY(*inode)) {
239 filp->f_op = &read_fifo_fops;
240 return POLLIN | POLLRDNORM;
242 if (PIPE_WRITERS(*inode))
243 filp->f_op = &read_fifo_fops;
244 return POLLOUT | POLLWRNORM;
247 static int pipe_release(struct inode * inode)
249 if (!PIPE_READERS(*inode) && !PIPE_WRITERS(*inode)) {
250 struct pipe_inode_info *info = inode->i_pipe;
251 inode->i_pipe = NULL;
252 free_page((unsigned long) info->base);
253 kfree(info);
254 return 0;
256 wake_up_interruptible(&PIPE_WAIT(*inode));
257 return 0;
260 static int pipe_read_release(struct inode * inode, struct file * filp)
262 PIPE_READERS(*inode)--;
263 return pipe_release(inode);
266 static int pipe_write_release(struct inode * inode, struct file * filp)
268 PIPE_WRITERS(*inode)--;
269 return pipe_release(inode);
272 static int pipe_rdwr_release(struct inode * inode, struct file * filp)
274 if (filp->f_mode & FMODE_READ)
275 PIPE_READERS(*inode)--;
276 if (filp->f_mode & FMODE_WRITE)
277 PIPE_WRITERS(*inode)--;
278 return pipe_release(inode);
281 static int pipe_read_open(struct inode * inode, struct file * filp)
283 PIPE_READERS(*inode)++;
284 return 0;
287 static int pipe_write_open(struct inode * inode, struct file * filp)
289 PIPE_WRITERS(*inode)++;
290 return 0;
293 static int pipe_rdwr_open(struct inode * inode, struct file * filp)
295 if (filp->f_mode & FMODE_READ)
296 PIPE_READERS(*inode)++;
297 if (filp->f_mode & FMODE_WRITE)
298 PIPE_WRITERS(*inode)++;
299 return 0;
303 * The file_operations structs are not static because they
304 * are also used in linux/fs/fifo.c to do operations on FIFOs.
306 struct file_operations connecting_fifo_fops = {
307 pipe_lseek,
308 connect_read,
309 bad_pipe_w,
310 NULL, /* no readdir */
311 connect_poll,
312 pipe_ioctl,
313 NULL, /* no mmap on pipes.. surprise */
314 pipe_read_open,
315 NULL, /* flush */
316 pipe_read_release,
317 NULL
320 struct file_operations read_fifo_fops = {
321 pipe_lseek,
322 pipe_read,
323 bad_pipe_w,
324 NULL, /* no readdir */
325 fifo_poll,
326 pipe_ioctl,
327 NULL, /* no mmap on pipes.. surprise */
328 pipe_read_open,
329 NULL, /* flush */
330 pipe_read_release,
331 NULL
334 struct file_operations write_fifo_fops = {
335 pipe_lseek,
336 bad_pipe_r,
337 pipe_write,
338 NULL, /* no readdir */
339 fifo_poll,
340 pipe_ioctl,
341 NULL, /* mmap */
342 pipe_write_open,
343 NULL, /* flush */
344 pipe_write_release,
345 NULL
348 struct file_operations rdwr_fifo_fops = {
349 pipe_lseek,
350 pipe_read,
351 pipe_write,
352 NULL, /* no readdir */
353 fifo_poll,
354 pipe_ioctl,
355 NULL, /* mmap */
356 pipe_rdwr_open,
357 NULL, /* flush */
358 pipe_rdwr_release,
359 NULL
362 struct file_operations read_pipe_fops = {
363 pipe_lseek,
364 pipe_read,
365 bad_pipe_w,
366 NULL, /* no readdir */
367 pipe_poll,
368 pipe_ioctl,
369 NULL, /* no mmap on pipes.. surprise */
370 pipe_read_open,
371 NULL, /* flush */
372 pipe_read_release,
373 NULL
376 struct file_operations write_pipe_fops = {
377 pipe_lseek,
378 bad_pipe_r,
379 pipe_write,
380 NULL, /* no readdir */
381 pipe_poll,
382 pipe_ioctl,
383 NULL, /* mmap */
384 pipe_write_open,
385 NULL, /* flush */
386 pipe_write_release,
387 NULL
390 struct file_operations rdwr_pipe_fops = {
391 pipe_lseek,
392 pipe_read,
393 pipe_write,
394 NULL, /* no readdir */
395 pipe_poll,
396 pipe_ioctl,
397 NULL, /* mmap */
398 pipe_rdwr_open,
399 NULL, /* flush */
400 pipe_rdwr_release,
401 NULL
404 static struct inode * get_pipe_inode(void)
406 extern struct inode_operations pipe_inode_operations;
407 struct inode *inode = get_empty_inode();
408 unsigned long page;
410 if (!inode)
411 goto fail_inode;
413 page = __get_free_page(GFP_USER);
415 if (!page)
416 goto fail_iput;
418 /* XXX */
419 inode->i_pipe = kmalloc(sizeof(struct pipe_inode_info), GFP_KERNEL);
420 if (!inode->i_pipe)
421 goto fail_page;
423 PIPE_BASE(*inode) = (char *) page;
424 inode->i_op = &pipe_inode_operations;
425 init_waitqueue_head(&PIPE_WAIT(*inode));
426 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
427 PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
428 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
429 PIPE_LOCK(*inode) = 0;
431 * Mark the inode dirty from the very beginning,
432 * that way it will never be moved to the dirty
433 * list because "mark_inode_dirty()" will think
434 * that it already _is_ on the dirty list.
436 inode->i_state = I_DIRTY;
437 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
438 inode->i_uid = current->fsuid;
439 inode->i_gid = current->fsgid;
440 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
441 inode->i_blksize = PAGE_SIZE;
442 return inode;
444 fail_page:
445 free_page(page);
446 fail_iput:
447 iput(inode);
448 fail_inode:
449 return NULL;
452 struct inode_operations pipe_inode_operations = {
453 &rdwr_pipe_fops,
454 NULL, /* create */
455 NULL, /* lookup */
456 NULL, /* link */
457 NULL, /* unlink */
458 NULL, /* symlink */
459 NULL, /* mkdir */
460 NULL, /* rmdir */
461 NULL, /* mknod */
462 NULL, /* rename */
463 NULL, /* readlink */
464 NULL, /* get_block */
465 NULL, /* readpage */
466 NULL, /* writepage */
467 NULL, /* flushpage */
468 NULL, /* truncate */
469 NULL, /* permission */
470 NULL, /* smap */
471 NULL /* revalidate */
474 int do_pipe(int *fd)
476 struct inode * inode;
477 struct file *f1, *f2;
478 int error;
479 int i,j;
481 error = -ENFILE;
482 f1 = get_empty_filp();
483 if (!f1)
484 goto no_files;
486 f2 = get_empty_filp();
487 if (!f2)
488 goto close_f1;
490 inode = get_pipe_inode();
491 if (!inode)
492 goto close_f12;
494 error = get_unused_fd();
495 if (error < 0)
496 goto close_f12_inode;
497 i = error;
499 error = get_unused_fd();
500 if (error < 0)
501 goto close_f12_inode_i;
502 j = error;
504 error = -ENOMEM;
505 f1->f_dentry = f2->f_dentry = dget(d_alloc_root(inode));
506 if (!f1->f_dentry)
507 goto close_f12_inode_i_j;
509 /* read file */
510 f1->f_pos = f2->f_pos = 0;
511 f1->f_flags = O_RDONLY;
512 f1->f_op = &read_pipe_fops;
513 f1->f_mode = 1;
515 /* write file */
516 f2->f_flags = O_WRONLY;
517 f2->f_op = &write_pipe_fops;
518 f2->f_mode = 2;
520 fd_install(i, f1);
521 fd_install(j, f2);
522 fd[0] = i;
523 fd[1] = j;
524 return 0;
526 close_f12_inode_i_j:
527 put_unused_fd(j);
528 close_f12_inode_i:
529 put_unused_fd(i);
530 close_f12_inode:
531 free_page((unsigned long) PIPE_BASE(*inode));
532 kfree(inode->i_pipe);
533 inode->i_pipe = NULL;
534 iput(inode);
535 close_f12:
536 put_filp(f2);
537 close_f1:
538 put_filp(f1);
539 no_files:
540 return error;