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         /*
 142          * If data has been written to the file, remove the setuid and
 143          * the setgid bits. We do it anyway otherwise there is an
 144          * extremely exploitable race - does your OS get it right |->
 145          *
 146          * Set ATTR_FORCE so it will always be changed.
 147          */
 148         if (!suser() && (inode->i_mode & (S_ISUID | S_ISGID))) {
 149                 struct iattr newattrs;
 150                 newattrs.ia_mode = inode->i_mode & ~(S_ISUID | S_ISGID);
 151                 newattrs.ia_valid = ATTR_CTIME | ATTR_MODE | ATTR_FORCE;
 152                 notify_change(inode, &newattrs);
 153         }
 154 
 155         down(&inode->i_sem);
 156         written = file->f_op->write(inode,file,buf,count);
 157         up(&inode->i_sem);
 158         return written;
 159 }
 160 
 161 static int sock_readv_writev(int type, struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 162         const struct iovec * iov, long count, long size)
 163 {
 164         struct msghdr msg;
 165         struct socket *sock;
 166 
 167         sock = &inode->u.socket_i;
 168         if (!sock->ops)
 169                 return -EOPNOTSUPP;
 170         msg.msg_name = NULL;
 171         msg.msg_namelen = 0;
 172         msg.msg_accrights = NULL;
 173         msg.msg_iov = (struct iovec *) iov;
 174         msg.msg_iovlen = count;
 175 
 176         /* read() does a VERIFY_WRITE */
 177         if (type == VERIFY_WRITE) {
 178                 if (!sock->ops->recvmsg)
 179                         return -EOPNOTSUPP;
 180                 return sock->ops->recvmsg(sock, &msg, size,
 181                         (file->f_flags & O_NONBLOCK), 0, NULL);
 182         }
 183         if (!sock->ops->sendmsg)
 184                 return -EOPNOTSUPP;
 185         return sock->ops->sendmsg(sock, &msg, size,
 186                 (file->f_flags & O_NONBLOCK), 0);
 187 }
 188 
 189 typedef int (*IO_fn_t)(struct inode *, struct file *, char *, int);
 190 
 191 static int do_readv_writev(int type, struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 192         const struct iovec * vector, unsigned long count)
 193 {
 194         size_t tot_len;
 195         struct iovec iov[MAX_IOVEC];
 196         int retval, i;
 197         IO_fn_t fn;
 198 
 199         /*
 200          * First get the "struct iovec" from user memory and
 201          * verify all the pointers
 202          */
 203         if (!count)
 204                 return 0;
 205         if (count > MAX_IOVEC)
 206                 return -EINVAL;
 207         retval = verify_area(VERIFY_READ, vector, count*sizeof(*vector));
 208         if (retval)
 209                 return retval;
 210         memcpy_fromfs(iov, vector, count*sizeof(*vector));
 211         tot_len = 0;
 212         for (i = 0 ; i < count ; i++) {
 213                 tot_len += iov[i].iov_len;
 214                 retval = verify_area(type, iov[i].iov_base, iov[i].iov_len);
 215                 if (retval)
 216                         return retval;
 217         }
 218 
 219         /*
 220          * Then do the actual IO.  Note that sockets need to be handled
 221          * specially as they have atomicity guarantees and can handle
 222          * iovec's natively
 223          */
 224         if (inode->i_sock)
 225                 return sock_readv_writev(type, inode, file, iov, count, tot_len);
 226 
 227         if (!file->f_op)
 228                 return -EINVAL;
 229         /* VERIFY_WRITE actually means a read, as we write to user space */
 230         fn = file->f_op->read;
 231         if (type == VERIFY_READ)
 232                 fn = (IO_fn_t) file->f_op->write;               
 233         vector = iov;
 234         while (count > 0) {
 235                 void * base;
 236                 int len, nr;
 237 
 238                 base = vector->iov_base;
 239                 len = vector->iov_len;
 240                 vector++;
 241                 count--;
 242                 nr = fn(inode, file, base, len);
 243                 if (nr < 0) {
 244                         if (retval)
 245                                 break;
 246                         retval = nr;
 247                         break;
 248                 }
 249                 retval += nr;
 250                 if (nr != len)
 251                         break;
 252         }
 253         return retval;
 254 }
 255 
 256 asmlinkage int sys_readv(unsigned long fd, const struct iovec * vector, long count)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258         struct file * file;
 259         struct inode * inode;
 260 
 261         if (fd >= NR_OPEN || !(file = current->files->fd[fd]) || !(inode = file->f_inode))
 262                 return -EBADF;
 263         if (!(file->f_mode & 1))
 264                 return -EBADF;
 265         return do_readv_writev(VERIFY_WRITE, inode, file, vector, count);
 266 }
 267 
 268 asmlinkage int sys_writev(unsigned long fd, const struct iovec * vector, long count)
     /* [previous][next][first][last][top][bottom][index][help] */
 269 {
 270         int error;
 271         struct file * file;
 272         struct inode * inode;
 273 
 274         if (fd >= NR_OPEN || !(file = current->files->fd[fd]) || !(inode = file->f_inode))
 275                 return -EBADF;
 276         if (!(file->f_mode & 2))
 277                 return -EBADF;
 278         down(&inode->i_sem);
 279         error = do_readv_writev(VERIFY_READ, inode, file, vector, count);
 280         up(&inode->i_sem);
 281         return error;
 282 }

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