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

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