root/fs/fifo.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fifo_open
  2. init_fifo

   1 /*
   2  *  linux/fs/fifo.c
   3  *
   4  *  written by Paul H. Hargrove
   5  */
   6 
   7 #include <linux/sched.h>
   8 #include <linux/kernel.h>
   9 #include <linux/errno.h>
  10 #include <linux/fcntl.h>
  11 
  12 static int fifo_open(struct inode * inode,struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
  13 {
  14         int retval = 0;
  15         unsigned long page;
  16 
  17         switch( filp->f_mode ) {
  18 
  19         case 1:
  20         /*
  21          *  O_RDONLY
  22          *  POSIX.1 says that O_NONBLOCK means return with the FIFO
  23          *  opened, even when there is no process writing the FIFO.
  24          */
  25                 filp->f_op = &connecting_pipe_fops;
  26                 if (!PIPE_READERS(*inode)++)
  27                         wake_up(&PIPE_WRITE_WAIT(*inode));
  28                 if (!(filp->f_flags & O_NONBLOCK) && !PIPE_WRITERS(*inode)) {
  29                         PIPE_RD_OPENERS(*inode)++;
  30                         while (!PIPE_WRITERS(*inode)) {
  31                                 if (current->signal & ~current->blocked) {
  32                                         retval = -ERESTARTSYS;
  33                                         break;
  34                                 }
  35                                 interruptible_sleep_on(&PIPE_READ_WAIT(*inode));
  36                         }
  37                         if (!--PIPE_RD_OPENERS(*inode))
  38                                 wake_up(&PIPE_WRITE_WAIT(*inode));
  39                 }
  40                 while (PIPE_WR_OPENERS(*inode))
  41                         interruptible_sleep_on(&PIPE_READ_WAIT(*inode));
  42                 if (PIPE_WRITERS(*inode))
  43                         filp->f_op = &read_pipe_fops;
  44                 if (retval && !--PIPE_READERS(*inode))
  45                         wake_up(&PIPE_WRITE_WAIT(*inode));
  46                 break;
  47         
  48         case 2:
  49         /*
  50          *  O_WRONLY
  51          *  POSIX.1 says that O_NONBLOCK means return -1 with
  52          *  errno=ENXIO when there is no process reading the FIFO.
  53          */
  54                 if ((filp->f_flags & O_NONBLOCK) && !PIPE_READERS(*inode)) {
  55                         retval = -ENXIO;
  56                         break;
  57                 }
  58                 filp->f_op = &write_pipe_fops;
  59                 if (!PIPE_WRITERS(*inode)++)
  60                         wake_up(&PIPE_READ_WAIT(*inode));
  61                 if (!PIPE_READERS(*inode)) {
  62                         PIPE_WR_OPENERS(*inode)++;
  63                         while (!PIPE_READERS(*inode)) {
  64                                 if (current->signal & ~current->blocked) {
  65                                         retval = -ERESTARTSYS;
  66                                         break;
  67                                 }
  68                                 interruptible_sleep_on(&PIPE_WRITE_WAIT(*inode));
  69                         }
  70                         if (!--PIPE_WR_OPENERS(*inode))
  71                                 wake_up(&PIPE_READ_WAIT(*inode));
  72                 }
  73                 while (PIPE_RD_OPENERS(*inode))
  74                         interruptible_sleep_on(&PIPE_WRITE_WAIT(*inode));
  75                 if (retval && !--PIPE_WRITERS(*inode))
  76                         wake_up(&PIPE_READ_WAIT(*inode));
  77                 break;
  78         
  79         case 3:
  80         /*
  81          *  O_RDWR
  82          *  POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  83          *  This implementation will NEVER block on a O_RDWR open, since
  84          *  the process can at least talk to itself.
  85          */
  86                 filp->f_op = &rdwr_pipe_fops;
  87                 if (!PIPE_READERS(*inode)++)
  88                         wake_up(&PIPE_WRITE_WAIT(*inode));
  89                 while (PIPE_WR_OPENERS(*inode))
  90                         interruptible_sleep_on(&PIPE_READ_WAIT(*inode));
  91                 if (!PIPE_WRITERS(*inode)++)
  92                         wake_up(&PIPE_READ_WAIT(*inode));
  93                 while (PIPE_RD_OPENERS(*inode))
  94                         interruptible_sleep_on(&PIPE_WRITE_WAIT(*inode));
  95                 break;
  96 
  97         default:
  98                 retval = -EINVAL;
  99         }
 100         if (retval || PIPE_BASE(*inode))
 101                 return retval;
 102         page = __get_free_page(GFP_KERNEL);
 103         if (PIPE_BASE(*inode)) {
 104                 free_page(page);
 105                 return 0;
 106         }
 107         if (!page)
 108                 return -ENOMEM;
 109         PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
 110         PIPE_BASE(*inode) = (char *) page;
 111         return 0;
 112 }
 113 
 114 /*
 115  * Dummy default file-operations: the only thing this does
 116  * is contain the open that then fills in the correct operations
 117  * depending on the access mode of the file...
 118  */
 119 static struct file_operations def_fifo_fops = {
 120         NULL,
 121         NULL,
 122         NULL,
 123         NULL,
 124         NULL,
 125         NULL,
 126         NULL,
 127         fifo_open,              /* will set read or write pipe_fops */
 128         NULL,
 129         NULL
 130 };
 131 
 132 static struct inode_operations fifo_inode_operations = {
 133         &def_fifo_fops,         /* default file operations */
 134         NULL,                   /* create */
 135         NULL,                   /* lookup */
 136         NULL,                   /* link */
 137         NULL,                   /* unlink */
 138         NULL,                   /* symlink */
 139         NULL,                   /* mkdir */
 140         NULL,                   /* rmdir */
 141         NULL,                   /* mknod */
 142         NULL,                   /* rename */
 143         NULL,                   /* readlink */
 144         NULL,                   /* follow_link */
 145         NULL,                   /* bmap */
 146         NULL,                   /* truncate */
 147         NULL                    /* permission */
 148 };
 149 
 150 void init_fifo(struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152         inode->i_op = &fifo_inode_operations;
 153         inode->i_pipe = 1;
 154         PIPE_BASE(*inode) = NULL;
 155         PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
 156         PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
 157         PIPE_READ_WAIT(*inode) = PIPE_WRITE_WAIT(*inode) = NULL;
 158         PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 0;
 159 }

/* [previous][next][first][last][top][bottom][index][help] */