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

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