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_read
  4. 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 not yet used: but we'll probably support reading several entries
  17  * at once in the future. Use count=1 in the library for future expansions.
  18  */
  19 asmlinkage int sys_readdir(unsigned int fd, struct dirent * dirent, unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  20 {
  21         int error;
  22         struct file * file;
  23         struct inode * inode;
  24 
  25         if (fd >= NR_OPEN || !(file = current->filp[fd]) ||
  26             !(inode = file->f_inode))
  27                 return -EBADF;
  28         error = -ENOTDIR;
  29         if (file->f_op && file->f_op->readdir) {
  30                 error = verify_area(VERIFY_WRITE, dirent, sizeof (*dirent));
  31                 if (!error)
  32                         error = file->f_op->readdir(inode,file,dirent,count);
  33         }
  34         return error;
  35 }
  36 
  37 asmlinkage int sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
  38 {
  39         struct file * file;
  40         int tmp = -1;
  41 
  42         if (fd >= NR_OPEN || !(file=current->filp[fd]) || !(file->f_inode))
  43                 return -EBADF;
  44         if (origin > 2)
  45                 return -EINVAL;
  46         if (file->f_op && file->f_op->lseek)
  47                 return file->f_op->lseek(file->f_inode,file,offset,origin);
  48 
  49 /* this is the default handler if no lseek handler is present */
  50         switch (origin) {
  51                 case 0:
  52                         tmp = offset;
  53                         break;
  54                 case 1:
  55                         tmp = file->f_pos + offset;
  56                         break;
  57                 case 2:
  58                         if (!file->f_inode)
  59                                 return -EINVAL;
  60                         tmp = file->f_inode->i_size + offset;
  61                         break;
  62         }
  63         if (tmp < 0)
  64                 return -EINVAL;
  65         file->f_pos = tmp;
  66         file->f_reada = 0;
  67         return file->f_pos;
  68 }
  69 
  70 asmlinkage int sys_read(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72         int error;
  73         struct file * file;
  74         struct inode * inode;
  75 
  76         if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
  77                 return -EBADF;
  78         if (!(file->f_mode & 1))
  79                 return -EBADF;
  80         if (!file->f_op || !file->f_op->read)
  81                 return -EINVAL;
  82         if (!count)
  83                 return 0;
  84         error = verify_area(VERIFY_WRITE,buf,count);
  85         if (error)
  86                 return error;
  87         return file->f_op->read(inode,file,buf,count);
  88 }
  89 
  90 asmlinkage int sys_write(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         int error;
  93         struct file * file;
  94         struct inode * inode;
  95         
  96         if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
  97                 return -EBADF;
  98         if (!(file->f_mode & 2))
  99                 return -EBADF;
 100         if (!file->f_op || !file->f_op->write)
 101                 return -EINVAL;
 102         if (!count)
 103                 return 0;
 104         error = verify_area(VERIFY_READ,buf,count);
 105         if (error)
 106                 return error;
 107         return file->f_op->write(inode,file,buf,count);
 108 }

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