root/fs/read_write.c

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

DEFINITIONS

This source file includes following definitions.
  1. sys_readdir
  2. sys_lseek
  3. sys_llseek
  4. sys_read
  5. sys_write

   1 /*
   2  *  linux/fs/read_write.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <linux/types.h>
   8 #include <linux/errno.h>
   9 #include <linux/stat.h>
  10 #include <linux/kernel.h>
  11 #include <linux/sched.h>
  12 
  13 #include <asm/segment.h>
  14 
  15 /*
  16  * Count is now a supported feature, but currently only the ext2fs
  17  * uses it.  A count value of 1 is supported for compatibility with
  18  * earlier libraries, but larger values are supported: count should
  19  * indicate the total buffer space available for filling with dirents.
  20  * The d_off entry in the dirents will then indicate the offset from
  21  * each dirent to the next, and the return value will indicate the
  22  * number of bytes written.  All dirents will be written at
  23  * word-aligned addresses.  [sct Oct 1994]
  24  */
  25 asmlinkage int sys_readdir(unsigned int fd, struct dirent * dirent, unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  26 {
  27         int error;
  28         struct file * file;
  29         struct inode * inode;
  30 
  31         if (fd >= NR_OPEN || !(file = current->files->fd[fd]) ||
  32             !(inode = file->f_inode))
  33                 return -EBADF;
  34         error = -ENOTDIR;
  35         if (file->f_op && file->f_op->readdir) {
  36                 error = verify_area(VERIFY_WRITE, dirent, sizeof (*dirent));
  37                 if (!error)
  38                         error = file->f_op->readdir(inode,file,dirent,count);
  39         }
  40         return error;
  41 }
  42 
  43 asmlinkage int sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
  44 {
  45         struct file * file;
  46         int tmp = -1;
  47 
  48         if (fd >= NR_OPEN || !(file=current->files->fd[fd]) || !(file->f_inode))
  49                 return -EBADF;
  50         if (origin > 2)
  51                 return -EINVAL;
  52         if (file->f_op && file->f_op->lseek)
  53                 return file->f_op->lseek(file->f_inode,file,offset,origin);
  54 
  55 /* this is the default handler if no lseek handler is present */
  56         switch (origin) {
  57                 case 0:
  58                         tmp = offset;
  59                         break;
  60                 case 1:
  61                         tmp = file->f_pos + offset;
  62                         break;
  63                 case 2:
  64                         if (!file->f_inode)
  65                                 return -EINVAL;
  66                         tmp = file->f_inode->i_size + offset;
  67                         break;
  68         }
  69         if (tmp < 0)
  70                 return -EINVAL;
  71         if (tmp != file->f_pos) {
  72                 file->f_pos = tmp;
  73                 file->f_reada = 0;
  74         }
  75         return file->f_pos;
  76 }
  77 
  78 asmlinkage int sys_llseek(unsigned int fd, unsigned long offset_high,
     /* [previous][next][first][last][top][bottom][index][help] */
  79                           unsigned long offset_low, loff_t * result,
  80                           unsigned int origin)
  81 {
  82         struct file * file;
  83         loff_t tmp = -1;
  84         loff_t offset;
  85         int err;
  86 
  87         if (fd >= NR_OPEN || !(file=current->files->fd[fd]) || !(file->f_inode))
  88                 return -EBADF;
  89         if (origin > 2)
  90                 return -EINVAL;
  91         if ((err = verify_area(VERIFY_WRITE, result, sizeof(loff_t))))
  92                 return err;
  93         offset = (loff_t) (((unsigned long long) offset_high << 32) | offset_low);
  94 /* there is no fs specific llseek handler */
  95         switch (origin) {
  96                 case 0:
  97                         tmp = offset;
  98                         break;
  99                 case 1:
 100                         tmp = file->f_pos + offset;
 101                         break;
 102                 case 2:
 103                         if (!file->f_inode)
 104                                 return -EINVAL;
 105                         tmp = file->f_inode->i_size + offset;
 106                         break;
 107         }
 108         if (tmp < 0)
 109                 return -EINVAL;
 110         file->f_pos = tmp;
 111         file->f_reada = 0;
 112         memcpy_tofs(result, &file->f_pos, sizeof(loff_t));
 113         return 0;
 114 }
 115 
 116 asmlinkage int sys_read(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         int error;
 119         struct file * file;
 120         struct inode * inode;
 121 
 122         if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode))
 123                 return -EBADF;
 124         if (!(file->f_mode & 1))
 125                 return -EBADF;
 126         if (!file->f_op || !file->f_op->read)
 127                 return -EINVAL;
 128         if (!count)
 129                 return 0;
 130         error = verify_area(VERIFY_WRITE,buf,count);
 131         if (error)
 132                 return error;
 133         return file->f_op->read(inode,file,buf,count);
 134 }
 135 
 136 asmlinkage int sys_write(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 137 {
 138         int error;
 139         struct file * file;
 140         struct inode * inode;
 141         int written;
 142         
 143         if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode))
 144                 return -EBADF;
 145         if (!(file->f_mode & 2))
 146                 return -EBADF;
 147         if (!file->f_op || !file->f_op->write)
 148                 return -EINVAL;
 149         if (!count)
 150                 return 0;
 151         error = verify_area(VERIFY_READ,buf,count);
 152         if (error)
 153                 return error;
 154         written = file->f_op->write(inode,file,buf,count);
 155         /*
 156          * If data has been written to the file, remove the setuid and
 157          * the setgid bits
 158          */
 159         if (written > 0 && !suser() && (inode->i_mode & (S_ISUID | S_ISGID))) {
 160                 inode->i_mode &= ~(S_ISUID | S_ISGID);
 161                 notify_change (NOTIFY_MODE, inode);
 162         }
 163         return written;
 164 }

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