[PATCH] DVB: Documentation and Kconfig updazes
[linux-2.6/history.git] / fs / read_write.c
blob5c205418c3c9f0331a928183954b34d693f78625
1 /*
2 * linux/fs/read_write.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/slab.h>
8 #include <linux/stat.h>
9 #include <linux/fcntl.h>
10 #include <linux/file.h>
11 #include <linux/uio.h>
12 #include <linux/smp_lock.h>
13 #include <linux/dnotify.h>
14 #include <linux/security.h>
15 #include <linux/module.h>
17 #include <asm/uaccess.h>
19 struct file_operations generic_ro_fops = {
20 .llseek = generic_file_llseek,
21 .read = generic_file_read,
22 .mmap = generic_file_readonly_mmap,
23 .sendfile = generic_file_sendfile,
26 EXPORT_SYMBOL(generic_ro_fops);
28 loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
30 long long retval;
31 struct inode *inode = file->f_mapping->host;
33 down(&inode->i_sem);
34 switch (origin) {
35 case 2:
36 offset += inode->i_size;
37 break;
38 case 1:
39 offset += file->f_pos;
41 retval = -EINVAL;
42 if (offset>=0 && offset<=inode->i_sb->s_maxbytes) {
43 if (offset != file->f_pos) {
44 file->f_pos = offset;
45 file->f_version = 0;
47 retval = offset;
49 up(&inode->i_sem);
50 return retval;
53 EXPORT_SYMBOL(generic_file_llseek);
55 loff_t remote_llseek(struct file *file, loff_t offset, int origin)
57 long long retval;
59 lock_kernel();
60 switch (origin) {
61 case 2:
62 offset += i_size_read(file->f_dentry->d_inode);
63 break;
64 case 1:
65 offset += file->f_pos;
67 retval = -EINVAL;
68 if (offset>=0 && offset<=file->f_dentry->d_inode->i_sb->s_maxbytes) {
69 if (offset != file->f_pos) {
70 file->f_pos = offset;
71 file->f_version = 0;
73 retval = offset;
75 unlock_kernel();
76 return retval;
79 EXPORT_SYMBOL(remote_llseek);
81 loff_t no_llseek(struct file *file, loff_t offset, int origin)
83 return -ESPIPE;
86 EXPORT_SYMBOL(no_llseek);
88 loff_t default_llseek(struct file *file, loff_t offset, int origin)
90 long long retval;
92 lock_kernel();
93 switch (origin) {
94 case 2:
95 offset += i_size_read(file->f_dentry->d_inode);
96 break;
97 case 1:
98 offset += file->f_pos;
100 retval = -EINVAL;
101 if (offset >= 0) {
102 if (offset != file->f_pos) {
103 file->f_pos = offset;
104 file->f_version = 0;
106 retval = offset;
108 unlock_kernel();
109 return retval;
112 EXPORT_SYMBOL(default_llseek);
114 static inline loff_t llseek(struct file *file, loff_t offset, int origin)
116 loff_t (*fn)(struct file *, loff_t, int);
118 fn = default_llseek;
119 if (file->f_op && file->f_op->llseek)
120 fn = file->f_op->llseek;
121 return fn(file, offset, origin);
124 asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
126 off_t retval;
127 struct file * file;
128 int fput_needed;
130 retval = -EBADF;
131 file = fget_light(fd, &fput_needed);
132 if (!file)
133 goto bad;
135 retval = -EINVAL;
136 if (origin <= 2) {
137 loff_t res = llseek(file, offset, origin);
138 retval = res;
139 if (res != (loff_t)retval)
140 retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
142 fput_light(file, fput_needed);
143 bad:
144 return retval;
146 EXPORT_SYMBOL_GPL(sys_lseek);
148 #if !defined(__alpha__)
149 asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high,
150 unsigned long offset_low, loff_t __user * result,
151 unsigned int origin)
153 int retval;
154 struct file * file;
155 loff_t offset;
156 int fput_needed;
158 retval = -EBADF;
159 file = fget_light(fd, &fput_needed);
160 if (!file)
161 goto bad;
163 retval = -EINVAL;
164 if (origin > 2)
165 goto out_putf;
167 offset = llseek(file, ((loff_t) offset_high << 32) | offset_low,
168 origin);
170 retval = (int)offset;
171 if (offset >= 0) {
172 retval = -EFAULT;
173 if (!copy_to_user(result, &offset, sizeof(offset)))
174 retval = 0;
176 out_putf:
177 fput_light(file, fput_needed);
178 bad:
179 return retval;
181 #endif
183 ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
185 struct kiocb kiocb;
186 ssize_t ret;
188 init_sync_kiocb(&kiocb, filp);
189 kiocb.ki_pos = *ppos;
190 ret = filp->f_op->aio_read(&kiocb, buf, len, kiocb.ki_pos);
191 if (-EIOCBQUEUED == ret)
192 ret = wait_on_sync_kiocb(&kiocb);
193 *ppos = kiocb.ki_pos;
194 return ret;
197 EXPORT_SYMBOL(do_sync_read);
199 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
201 struct inode *inode = file->f_dentry->d_inode;
202 ssize_t ret;
204 if (!(file->f_mode & FMODE_READ))
205 return -EBADF;
206 if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
207 return -EINVAL;
209 ret = locks_verify_area(FLOCK_VERIFY_READ, inode, file, *pos, count);
210 if (!ret) {
211 ret = security_file_permission (file, MAY_READ);
212 if (!ret) {
213 if (file->f_op->read)
214 ret = file->f_op->read(file, buf, count, pos);
215 else
216 ret = do_sync_read(file, buf, count, pos);
217 if (ret > 0)
218 dnotify_parent(file->f_dentry, DN_ACCESS);
222 return ret;
225 EXPORT_SYMBOL(vfs_read);
227 ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
229 struct kiocb kiocb;
230 ssize_t ret;
232 init_sync_kiocb(&kiocb, filp);
233 kiocb.ki_pos = *ppos;
234 ret = filp->f_op->aio_write(&kiocb, buf, len, kiocb.ki_pos);
235 if (-EIOCBQUEUED == ret)
236 ret = wait_on_sync_kiocb(&kiocb);
237 *ppos = kiocb.ki_pos;
238 return ret;
241 EXPORT_SYMBOL(do_sync_write);
243 ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
245 struct inode *inode = file->f_dentry->d_inode;
246 ssize_t ret;
248 if (!(file->f_mode & FMODE_WRITE))
249 return -EBADF;
250 if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
251 return -EINVAL;
253 ret = locks_verify_area(FLOCK_VERIFY_WRITE, inode, file, *pos, count);
254 if (!ret) {
255 ret = security_file_permission (file, MAY_WRITE);
256 if (!ret) {
257 if (file->f_op->write)
258 ret = file->f_op->write(file, buf, count, pos);
259 else
260 ret = do_sync_write(file, buf, count, pos);
261 if (ret > 0)
262 dnotify_parent(file->f_dentry, DN_MODIFY);
266 return ret;
269 EXPORT_SYMBOL(vfs_write);
271 asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
273 struct file *file;
274 ssize_t ret = -EBADF;
275 int fput_needed;
277 file = fget_light(fd, &fput_needed);
278 if (file) {
279 ret = vfs_read(file, buf, count, &file->f_pos);
280 fput_light(file, fput_needed);
283 return ret;
285 EXPORT_SYMBOL_GPL(sys_read);
287 asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
289 struct file *file;
290 ssize_t ret = -EBADF;
291 int fput_needed;
293 file = fget_light(fd, &fput_needed);
294 if (file) {
295 ret = vfs_write(file, buf, count, &file->f_pos);
296 fput_light(file, fput_needed);
299 return ret;
302 asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
303 size_t count, loff_t pos)
305 struct file *file;
306 ssize_t ret = -EBADF;
307 int fput_needed;
309 if (pos < 0)
310 return -EINVAL;
312 file = fget_light(fd, &fput_needed);
313 if (file) {
314 ret = vfs_read(file, buf, count, &pos);
315 fput_light(file, fput_needed);
318 return ret;
321 asmlinkage ssize_t sys_pwrite64(unsigned int fd, const char __user *buf,
322 size_t count, loff_t pos)
324 struct file *file;
325 ssize_t ret = -EBADF;
326 int fput_needed;
328 if (pos < 0)
329 return -EINVAL;
331 file = fget_light(fd, &fput_needed);
332 if (file) {
333 ret = vfs_write(file, buf, count, &pos);
334 fput_light(file, fput_needed);
337 return ret;
341 * Reduce an iovec's length in-place. Return the resulting number of segments
343 unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
345 unsigned long seg = 0;
346 size_t len = 0;
348 while (seg < nr_segs) {
349 seg++;
350 if (len + iov->iov_len >= to) {
351 iov->iov_len = to - len;
352 break;
354 len += iov->iov_len;
355 iov++;
357 return seg;
360 EXPORT_SYMBOL(iov_shorten);
362 static ssize_t do_readv_writev(int type, struct file *file,
363 const struct iovec __user * uvector,
364 unsigned long nr_segs, loff_t *pos)
366 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
367 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
369 size_t tot_len;
370 struct iovec iovstack[UIO_FASTIOV];
371 struct iovec *iov=iovstack, *vector;
372 ssize_t ret;
373 int seg;
374 io_fn_t fn;
375 iov_fn_t fnv;
376 struct inode *inode;
379 * SuS says "The readv() function *may* fail if the iovcnt argument
380 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
381 * traditionally returned zero for zero segments, so...
383 ret = 0;
384 if (nr_segs == 0)
385 goto out;
388 * First get the "struct iovec" from user memory and
389 * verify all the pointers
391 ret = -EINVAL;
392 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
393 goto out;
394 if (!file->f_op)
395 goto out;
396 if (nr_segs > UIO_FASTIOV) {
397 ret = -ENOMEM;
398 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
399 if (!iov)
400 goto out;
402 ret = -EFAULT;
403 if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector)))
404 goto out;
407 * Single unix specification:
408 * We should -EINVAL if an element length is not >= 0 and fitting an
409 * ssize_t. The total length is fitting an ssize_t
411 * Be careful here because iov_len is a size_t not an ssize_t
413 tot_len = 0;
414 ret = -EINVAL;
415 for (seg = 0 ; seg < nr_segs; seg++) {
416 ssize_t tmp = tot_len;
417 ssize_t len = (ssize_t)iov[seg].iov_len;
418 if (len < 0) /* size_t not fitting an ssize_t .. */
419 goto out;
420 tot_len += len;
421 if (tot_len < tmp) /* maths overflow on the ssize_t */
422 goto out;
424 if (tot_len == 0) {
425 ret = 0;
426 goto out;
429 inode = file->f_dentry->d_inode;
430 /* VERIFY_WRITE actually means a read, as we write to user space */
431 ret = locks_verify_area((type == READ
432 ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE),
433 inode, file, *pos, tot_len);
434 if (ret)
435 goto out;
437 fnv = NULL;
438 if (type == READ) {
439 fn = file->f_op->read;
440 fnv = file->f_op->readv;
441 } else {
442 fn = (io_fn_t)file->f_op->write;
443 fnv = file->f_op->writev;
445 if (fnv) {
446 ret = fnv(file, iov, nr_segs, pos);
447 goto out;
450 /* Do it by hand, with file-ops */
451 ret = 0;
452 vector = iov;
453 while (nr_segs > 0) {
454 void __user * base;
455 size_t len;
456 ssize_t nr;
458 base = vector->iov_base;
459 len = vector->iov_len;
460 vector++;
461 nr_segs--;
463 nr = fn(file, base, len, pos);
465 if (nr < 0) {
466 if (!ret) ret = nr;
467 break;
469 ret += nr;
470 if (nr != len)
471 break;
473 out:
474 if (iov != iovstack)
475 kfree(iov);
476 if ((ret + (type == READ)) > 0)
477 dnotify_parent(file->f_dentry,
478 (type == READ) ? DN_ACCESS : DN_MODIFY);
479 return ret;
482 ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
483 unsigned long vlen, loff_t *pos)
485 if (!(file->f_mode & FMODE_READ))
486 return -EBADF;
487 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
488 return -EINVAL;
490 return do_readv_writev(READ, file, vec, vlen, pos);
493 EXPORT_SYMBOL(vfs_readv);
495 ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
496 unsigned long vlen, loff_t *pos)
498 if (!(file->f_mode & FMODE_WRITE))
499 return -EBADF;
500 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
501 return -EINVAL;
503 return do_readv_writev(WRITE, file, vec, vlen, pos);
506 EXPORT_SYMBOL(vfs_writev);
508 asmlinkage ssize_t
509 sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
511 struct file *file;
512 ssize_t ret = -EBADF;
513 int fput_needed;
515 file = fget_light(fd, &fput_needed);
516 if (file) {
517 ret = vfs_readv(file, vec, vlen, &file->f_pos);
518 fput_light(file, fput_needed);
521 return ret;
524 asmlinkage ssize_t
525 sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
527 struct file *file;
528 ssize_t ret = -EBADF;
529 int fput_needed;
531 file = fget_light(fd, &fput_needed);
532 if (file) {
533 ret = vfs_writev(file, vec, vlen, &file->f_pos);
534 fput_light(file, fput_needed);
537 return ret;
540 static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
541 size_t count, loff_t max)
543 struct file * in_file, * out_file;
544 struct inode * in_inode, * out_inode;
545 loff_t pos;
546 ssize_t retval;
547 int fput_needed_in, fput_needed_out;
550 * Get input file, and verify that it is ok..
552 retval = -EBADF;
553 in_file = fget_light(in_fd, &fput_needed_in);
554 if (!in_file)
555 goto out;
556 if (!(in_file->f_mode & FMODE_READ))
557 goto fput_in;
558 retval = -EINVAL;
559 in_inode = in_file->f_dentry->d_inode;
560 if (!in_inode)
561 goto fput_in;
562 if (!in_file->f_op || !in_file->f_op->sendfile)
563 goto fput_in;
564 if (!ppos)
565 ppos = &in_file->f_pos;
566 retval = locks_verify_area(FLOCK_VERIFY_READ, in_inode, in_file, *ppos, count);
567 if (retval)
568 goto fput_in;
570 retval = security_file_permission (in_file, MAY_READ);
571 if (retval)
572 goto fput_in;
575 * Get output file, and verify that it is ok..
577 retval = -EBADF;
578 out_file = fget_light(out_fd, &fput_needed_out);
579 if (!out_file)
580 goto fput_in;
581 if (!(out_file->f_mode & FMODE_WRITE))
582 goto fput_out;
583 retval = -EINVAL;
584 if (!out_file->f_op || !out_file->f_op->sendpage)
585 goto fput_out;
586 out_inode = out_file->f_dentry->d_inode;
587 retval = locks_verify_area(FLOCK_VERIFY_WRITE, out_inode, out_file, out_file->f_pos, count);
588 if (retval)
589 goto fput_out;
591 retval = security_file_permission (out_file, MAY_WRITE);
592 if (retval)
593 goto fput_out;
595 if (!max)
596 max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
598 pos = *ppos;
599 retval = -EINVAL;
600 if (unlikely(pos < 0))
601 goto fput_out;
602 if (unlikely(pos + count > max)) {
603 retval = -EOVERFLOW;
604 if (pos >= max)
605 goto fput_out;
606 count = max - pos;
609 retval = in_file->f_op->sendfile(in_file, ppos, count, file_send_actor, out_file);
611 if (*ppos > max)
612 retval = -EOVERFLOW;
614 fput_out:
615 fput_light(out_file, fput_needed_out);
616 fput_in:
617 fput_light(in_file, fput_needed_in);
618 out:
619 return retval;
622 asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t __user *offset, size_t count)
624 loff_t pos;
625 off_t off;
626 ssize_t ret;
628 if (offset) {
629 if (unlikely(get_user(off, offset)))
630 return -EFAULT;
631 pos = off;
632 ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
633 if (unlikely(put_user(pos, offset)))
634 return -EFAULT;
635 return ret;
638 return do_sendfile(out_fd, in_fd, NULL, count, MAX_NON_LFS);
641 asmlinkage ssize_t sys_sendfile64(int out_fd, int in_fd, loff_t __user *offset, size_t count)
643 loff_t pos;
644 ssize_t ret;
646 if (offset) {
647 if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
648 return -EFAULT;
649 ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
650 if (unlikely(put_user(pos, offset)))
651 return -EFAULT;
652 return ret;
655 return do_sendfile(out_fd, in_fd, NULL, count, 0);