2 * linux/fs/read_write.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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/file.h>
15 #include <linux/uio.h>
16 #include <linux/malloc.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/limits.h>
21 #include <asm/uaccess.h>
23 static loff_t
default_llseek(struct file
*file
, loff_t offset
, int origin
)
29 offset
+= file
->f_dentry
->d_inode
->i_size
;
32 offset
+= file
->f_pos
;
36 if (offset
!= file
->f_pos
) {
39 file
->f_version
= ++event
;
46 static inline loff_t
llseek(struct file
*file
, loff_t offset
, int origin
)
48 loff_t (*fn
)(struct file
*, loff_t
, int);
51 if (file
->f_op
&& file
->f_op
->llseek
)
52 fn
= file
->f_op
->llseek
;
53 return fn(file
, offset
, origin
);
56 asmlinkage off_t
sys_lseek(unsigned int fd
, off_t offset
, unsigned int origin
)
60 struct dentry
* dentry
;
68 /* N.B. Shouldn't this be ENOENT?? */
69 if (!(dentry
= file
->f_dentry
) ||
70 !(inode
= dentry
->d_inode
))
74 retval
= llseek(file
, offset
, origin
);
82 #if !defined(__alpha__)
83 asmlinkage
int sys_llseek(unsigned int fd
, unsigned long offset_high
,
84 unsigned long offset_low
, loff_t
* result
,
89 struct dentry
* dentry
;
98 /* N.B. Shouldn't this be ENOENT?? */
99 if (!(dentry
= file
->f_dentry
) ||
100 !(inode
= dentry
->d_inode
))
106 offset
= llseek(file
, ((loff_t
) offset_high
<< 32) | offset_low
,
109 retval
= (int)offset
;
112 if (!copy_to_user(result
, &offset
, sizeof(offset
)))
123 asmlinkage ssize_t
sys_read(unsigned int fd
, char * buf
, size_t count
)
127 ssize_t (*read
)(struct file
*, char *, size_t, loff_t
*);
135 if (!(file
->f_mode
& FMODE_READ
))
137 ret
= locks_verify_area(FLOCK_VERIFY_READ
, file
->f_dentry
->d_inode
,
138 file
, file
->f_pos
, count
);
142 if (!file
->f_op
|| !(read
= file
->f_op
->read
))
144 ret
= read(file
, buf
, count
, &file
->f_pos
);
152 asmlinkage ssize_t
sys_write(unsigned int fd
, const char * buf
, size_t count
)
156 struct inode
* inode
;
157 ssize_t (*write
)(struct file
*, const char *, size_t, loff_t
*);
165 if (!(file
->f_mode
& FMODE_WRITE
))
167 inode
= file
->f_dentry
->d_inode
;
168 ret
= locks_verify_area(FLOCK_VERIFY_WRITE
, inode
, file
,
173 if (!file
->f_op
|| !(write
= file
->f_op
->write
))
177 ret
= write(file
, buf
, count
, &file
->f_pos
);
187 static ssize_t
do_readv_writev(int type
, struct file
*file
,
188 const struct iovec
* vector
,
191 typedef ssize_t (*io_fn_t
)(struct file
*, char *, size_t, loff_t
*);
194 struct iovec iovstack
[UIO_FASTIOV
];
195 struct iovec
*iov
=iovstack
;
201 * First get the "struct iovec" from user memory and
202 * verify all the pointers
208 if (count
> UIO_MAXIOV
)
210 if (count
> UIO_FASTIOV
) {
212 iov
= kmalloc(count
*sizeof(struct iovec
), GFP_KERNEL
);
217 if (copy_from_user(iov
, vector
, count
*sizeof(*vector
)))
221 for (i
= 0 ; i
< count
; i
++)
222 tot_len
+= iov
[i
].iov_len
;
224 inode
= file
->f_dentry
->d_inode
;
225 ret
= locks_verify_area((type
== VERIFY_READ
226 ? FLOCK_VERIFY_READ
: FLOCK_VERIFY_WRITE
),
227 inode
, file
, file
->f_pos
, tot_len
);
231 * Then do the actual IO. Note that sockets need to be handled
232 * specially as they have atomicity guarantees and can handle
236 ret
= sock_readv_writev(type
,inode
,file
,iov
,count
,tot_len
);
244 /* VERIFY_WRITE actually means a read, as we write to user space */
245 fn
= file
->f_op
->read
;
246 if (type
== VERIFY_READ
)
247 fn
= (io_fn_t
) file
->f_op
->write
;
256 base
= vector
->iov_base
;
257 len
= vector
->iov_len
;
261 nr
= fn(file
, base
, len
, &file
->f_pos
);
279 asmlinkage ssize_t
sys_readv(unsigned long fd
, const struct iovec
* vector
,
291 if (file
->f_mode
& FMODE_READ
)
292 ret
= do_readv_writev(VERIFY_WRITE
, file
, vector
, count
);
300 asmlinkage ssize_t
sys_writev(unsigned long fd
, const struct iovec
* vector
,
312 if (file
->f_mode
& FMODE_WRITE
) {
313 down(&file
->f_dentry
->d_inode
->i_sem
);
314 ret
= do_readv_writev(VERIFY_READ
, file
, vector
, count
);
315 up(&file
->f_dentry
->d_inode
->i_sem
);
324 /* From the Single Unix Spec: pread & pwrite act like lseek to pos + op +
325 lseek back to original location. They fail just like lseek does on
326 non-seekable files. */
328 asmlinkage ssize_t
sys_pread(unsigned int fd
, char * buf
,
329 size_t count
, loff_t pos
)
333 ssize_t (*read
)(struct file
*, char *, size_t, loff_t
*);
341 if (!(file
->f_mode
& FMODE_READ
))
343 ret
= locks_verify_area(FLOCK_VERIFY_READ
, file
->f_dentry
->d_inode
,
348 if (!file
->f_op
|| !(read
= file
->f_op
->read
))
352 ret
= read(file
, buf
, count
, &pos
);
360 asmlinkage ssize_t
sys_pwrite(unsigned int fd
, const char * buf
,
361 size_t count
, loff_t pos
)
365 ssize_t (*write
)(struct file
*, const char *, size_t, loff_t
*);
373 if (!(file
->f_mode
& FMODE_WRITE
))
375 ret
= locks_verify_area(FLOCK_VERIFY_WRITE
, file
->f_dentry
->d_inode
,
380 if (!file
->f_op
|| !(write
= file
->f_op
->write
))
385 down(&file
->f_dentry
->d_inode
->i_sem
);
386 ret
= write(file
, buf
, count
, &pos
);
387 up(&file
->f_dentry
->d_inode
->i_sem
);