root/fs/read_write.c

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

DEFINITIONS

This source file includes following definitions.
  1. sys_lseek
  2. sys_llseek
  3. sys_read
  4. sys_write
  5. sock_readv_writev
  6. do_readv_writev
  7. sys_readv
  8. sys_writev

   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 #include <linux/fcntl.h>
  13 #include <linux/mm.h>
  14 #include <linux/uio.h>
  15 
  16 #include <asm/segment.h>
  17 
  18 asmlinkage int sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
  19 {
  20         struct file * file;
  21         int tmp = -1;
  22 
  23         if (fd >= NR_OPEN || !(file=current->files->fd[fd]) || !(file->f_inode))
  24                 return -EBADF;
  25         if (origin > 2)
  26                 return -EINVAL;
  27         if (file->f_op && file->f_op->lseek)
  28                 return file->f_op->lseek(file->f_inode,file,offset,origin);
  29 
  30 /* this is the default handler if no lseek handler is present */
  31         switch (origin) {
  32                 case 0:
  33                         tmp = offset;
  34                         break;
  35                 case 1:
  36                         tmp = file->f_pos + offset;
  37                         break;
  38                 case 2:
  39                         if (!file->f_inode)
  40                                 return -EINVAL;
  41                         tmp = file->f_inode->i_size + offset;
  42                         break;
  43         }
  44         if (tmp < 0)
  45                 return -EINVAL;
  46         if (tmp != file->f_pos) {
  47                 file->f_pos = tmp;
  48                 file->f_reada = 0;
  49                 file->f_version = ++event;
  50         }
  51         return file->f_pos;
  52 }
  53 
  54 asmlinkage int sys_llseek(unsigned int fd, unsigned long offset_high,
     /* [previous][next][first][last][top][bottom][index][help] */
  55                           unsigned long offset_low, loff_t * result,
  56                           unsigned int origin)
  57 {
  58         struct file * file;
  59         loff_t tmp = -1;
  60         loff_t offset;
  61         int err;
  62 
  63         if (fd >= NR_OPEN || !(file=current->files->fd[fd]) || !(file->f_inode))
  64                 return -EBADF;
  65         if (origin > 2)
  66                 return -EINVAL;
  67         if ((err = verify_area(VERIFY_WRITE, result, sizeof(loff_t))))
  68                 return err;
  69         offset = (loff_t) (((unsigned long long) offset_high << 32) | offset_low);
  70 
  71         /* if there is a fs-specific handler, we can't just ignore it.. */
  72         /* accept llseek() only for the signed long subset of long long */
  73         if (file->f_op && file->f_op->lseek) {
  74                 if (offset != (long) offset)
  75                         return -EINVAL;
  76                 return file->f_op->lseek(file->f_inode,file,offset,origin);
  77         }
  78 
  79         switch (origin) {
  80                 case 0:
  81                         tmp = offset;
  82                         break;
  83                 case 1:
  84                         tmp = file->f_pos + offset;
  85                         break;
  86                 case 2:
  87                         if (!file->f_inode)
  88                                 return -EINVAL;
  89                         tmp = file->f_inode->i_size + offset;
  90                         break;
  91         }
  92         if (tmp < 0)
  93                 return -EINVAL;
  94         if (tmp != file->f_pos) {
  95                 file->f_pos = tmp;
  96                 file->f_reada = 0;
  97                 file->f_version = ++event;
  98         }
  99         memcpy_tofs(result, &file->f_pos, sizeof(loff_t));
 100         return 0;
 101 }
 102 
 103 asmlinkage int sys_read(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105         int error;
 106         struct file * file;
 107         struct inode * inode;
 108 
 109         if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode))
 110                 return -EBADF;
 111         if (!(file->f_mode & 1))
 112                 return -EBADF;
 113         if (!file->f_op || !file->f_op->read)
 114                 return -EINVAL;
 115         if (!count)
 116                 return 0;
 117         error = verify_area(VERIFY_WRITE,buf,count);
 118         if (error)
 119                 return error;
 120         return file->f_op->read(inode,file,buf,count);
 121 }
 122 
 123 asmlinkage int sys_write(unsigned int fd,char * buf,unsigned int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         int error;
 126         struct file * file;
 127         struct inode * inode;
 128         int written;
 129         
 130         if (fd>=NR_OPEN || !(file=current->files->fd[fd]) || !(inode=file->f_inode))
 131                 return -EBADF;
 132         if (!(file->f_mode & 2))
 133                 return -EBADF;
 134         if (!file->f_op || !file->f_op->write)
 135                 return -EINVAL;
 136         if (!count)
 137                 return 0;
 138         error = verify_area(VERIFY_READ,buf,count);
 139         if (error)
 140                 return error;
 141         written = file->f_op->write(inode,file,buf,count);
 142         /*
 143          * If data has been written to the file, remove the setuid and
 144          * the setgid bits
 145          */
 146         if (written > 0 && !suser() && (inode->i_mode & (S_ISUID | S_ISGID))) {
 147                 struct iattr newattrs;
 148                 newattrs.ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID);
 149                 newattrs.ia_valid = ATTR_MODE;
 150                 notify_change(inode, &newattrs);
 151         }
 152         return written;
 153 }
 154 
 155 static int sock_readv_writev(int type, struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 156         const struct iovec * iov, long count, long size)
 157 {
 158         struct msghdr msg;
 159         struct socket *sock;
 160 
 161         sock = &inode->u.socket_i;
 162         if (!sock->ops)
 163                 return -EOPNOTSUPP;
 164         msg.msg_name = NULL;
 165         msg.msg_namelen = 0;
 166         msg.msg_accrights = NULL;
 167         msg.msg_iov = (struct iovec *) iov;
 168         msg.msg_iovlen = count;
 169 
 170         /* read() does a VERIFY_WRITE */
 171         if (type == VERIFY_WRITE) {
 172                 if (!sock->ops->recvmsg)
 173                         return -EOPNOTSUPP;
 174                 return sock->ops->recvmsg(sock, &msg, size,
 175                         (file->f_flags & O_NONBLOCK), 0, NULL);
 176         }
 177         if (!sock->ops->sendmsg)
 178                 return -EOPNOTSUPP;
 179         return sock->ops->sendmsg(sock, &msg, size,
 180                 (file->f_flags & O_NONBLOCK), 0);
 181 }
 182 
 183 typedef int (*IO_fn_t)(struct inode *, struct file *, char *, int);
 184 
 185 static int do_readv_writev(int type, struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 186         const struct iovec * vector, unsigned long count)
 187 {
 188         size_t tot_len;
 189         struct iovec iov[MAX_IOVEC];
 190         int retval, i;
 191         IO_fn_t fn;
 192 
 193         /*
 194          * First get the "struct iovec" from user memory and
 195          * verify all the pointers
 196          */
 197         if (!count)
 198                 return 0;
 199         if (count > MAX_IOVEC)
 200                 return -EINVAL;
 201         retval = verify_area(VERIFY_READ, vector, count*sizeof(*vector));
 202         if (retval)
 203                 return retval;
 204         memcpy_fromfs(iov, vector, count*sizeof(*vector));
 205         tot_len = 0;
 206         for (i = 0 ; i < count ; i++) {
 207                 tot_len += iov[i].iov_len;
 208                 retval = verify_area(type, iov[i].iov_base, iov[i].iov_len);
 209                 if (retval)
 210                         return retval;
 211         }
 212 
 213         /*
 214          * Then do the actual IO.  Note that sockets need to be handled
 215          * specially as they have atomicity guarantees and can handle
 216          * iovec's natively
 217          */
 218         if (inode->i_sock)
 219                 return sock_readv_writev(type, inode, file, iov, count, tot_len);
 220 
 221         if (!file->f_op)
 222                 return -EINVAL;
 223         /* VERIFY_WRITE actually means a read, as we write to user space */
 224         fn = file->f_op->read;
 225         if (type == VERIFY_READ)
 226                 fn = (IO_fn_t) file->f_op->write;               
 227         vector = iov;
 228         while (count > 0) {
 229                 void * base;
 230                 int len, nr;
 231 
 232                 base = vector->iov_base;
 233                 len = vector->iov_len;
 234                 vector++;
 235                 count--;
 236                 nr = fn(inode, file, base, len);
 237                 if (nr < 0) {
 238                         if (retval)
 239                                 break;
 240                         retval = nr;
 241                         break;
 242                 }
 243                 retval += nr;
 244                 if (nr != len)
 245                         break;
 246         }
 247         return retval;
 248 }
 249 
 250 asmlinkage int sys_readv(unsigned long fd, const struct iovec * vector, long count)
     /* [previous][next][first][last][top][bottom][index][help] */
 251 {
 252         struct file * file;
 253         struct inode * inode;
 254 
 255         if (fd >= NR_OPEN || !(file = current->files->fd[fd]) || !(inode = file->f_inode))
 256                 return -EBADF;
 257         if (!(file->f_mode & 1))
 258                 return -EBADF;
 259         return do_readv_writev(VERIFY_WRITE, inode, file, vector, count);
 260 }
 261 
 262 asmlinkage int sys_writev(unsigned long fd, const struct iovec * vector, long count)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264         struct file * file;
 265         struct inode * inode;
 266 
 267         if (fd >= NR_OPEN || !(file = current->files->fd[fd]) || !(inode = file->f_inode))
 268                 return -EBADF;
 269         if (!(file->f_mode & 2))
 270                 return -EBADF;
 271         return do_readv_writev(VERIFY_READ, inode, file, vector, count);
 272 }

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