read_write.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * linux/fs/read_write.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/stat.h>
  8. #include <linux/fcntl.h>
  9. #include <linux/file.h>
  10. #include <linux/uio.h>
  11. #include <linux/smp_lock.h>
  12. #include <linux/fsnotify.h>
  13. #include <linux/security.h>
  14. #include <linux/module.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/splice.h>
  18. #include "read_write.h"
  19. #include <asm/uaccess.h>
  20. #include <asm/unistd.h>
  21. const struct file_operations generic_ro_fops = {
  22. .llseek = generic_file_llseek,
  23. .read = do_sync_read,
  24. .aio_read = generic_file_aio_read,
  25. .mmap = generic_file_readonly_mmap,
  26. .splice_read = generic_file_splice_read,
  27. };
  28. EXPORT_SYMBOL(generic_ro_fops);
  29. loff_t generic_file_llseek(struct file *file, loff_t offset, int origin)
  30. {
  31. long long retval;
  32. struct inode *inode = file->f_mapping->host;
  33. mutex_lock(&inode->i_mutex);
  34. switch (origin) {
  35. case SEEK_END:
  36. offset += inode->i_size;
  37. break;
  38. case SEEK_CUR:
  39. offset += file->f_pos;
  40. }
  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;
  46. }
  47. retval = offset;
  48. }
  49. mutex_unlock(&inode->i_mutex);
  50. return retval;
  51. }
  52. EXPORT_SYMBOL(generic_file_llseek);
  53. loff_t remote_llseek(struct file *file, loff_t offset, int origin)
  54. {
  55. long long retval;
  56. lock_kernel();
  57. switch (origin) {
  58. case SEEK_END:
  59. offset += i_size_read(file->f_path.dentry->d_inode);
  60. break;
  61. case SEEK_CUR:
  62. offset += file->f_pos;
  63. }
  64. retval = -EINVAL;
  65. if (offset>=0 && offset<=file->f_path.dentry->d_inode->i_sb->s_maxbytes) {
  66. if (offset != file->f_pos) {
  67. file->f_pos = offset;
  68. file->f_version = 0;
  69. }
  70. retval = offset;
  71. }
  72. unlock_kernel();
  73. return retval;
  74. }
  75. EXPORT_SYMBOL(remote_llseek);
  76. loff_t no_llseek(struct file *file, loff_t offset, int origin)
  77. {
  78. return -ESPIPE;
  79. }
  80. EXPORT_SYMBOL(no_llseek);
  81. loff_t default_llseek(struct file *file, loff_t offset, int origin)
  82. {
  83. long long retval;
  84. lock_kernel();
  85. switch (origin) {
  86. case SEEK_END:
  87. offset += i_size_read(file->f_path.dentry->d_inode);
  88. break;
  89. case SEEK_CUR:
  90. offset += file->f_pos;
  91. }
  92. retval = -EINVAL;
  93. if (offset >= 0) {
  94. if (offset != file->f_pos) {
  95. file->f_pos = offset;
  96. file->f_version = 0;
  97. }
  98. retval = offset;
  99. }
  100. unlock_kernel();
  101. return retval;
  102. }
  103. EXPORT_SYMBOL(default_llseek);
  104. loff_t vfs_llseek(struct file *file, loff_t offset, int origin)
  105. {
  106. loff_t (*fn)(struct file *, loff_t, int);
  107. fn = no_llseek;
  108. if (file->f_mode & FMODE_LSEEK) {
  109. fn = default_llseek;
  110. if (file->f_op && file->f_op->llseek)
  111. fn = file->f_op->llseek;
  112. }
  113. return fn(file, offset, origin);
  114. }
  115. EXPORT_SYMBOL(vfs_llseek);
  116. asmlinkage off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
  117. {
  118. off_t retval;
  119. struct file * file;
  120. int fput_needed;
  121. retval = -EBADF;
  122. file = fget_light(fd, &fput_needed);
  123. if (!file)
  124. goto bad;
  125. retval = -EINVAL;
  126. if (origin <= SEEK_MAX) {
  127. loff_t res = vfs_llseek(file, offset, origin);
  128. retval = res;
  129. if (res != (loff_t)retval)
  130. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  131. }
  132. fput_light(file, fput_needed);
  133. bad:
  134. return retval;
  135. }
  136. #ifdef __ARCH_WANT_SYS_LLSEEK
  137. asmlinkage long sys_llseek(unsigned int fd, unsigned long offset_high,
  138. unsigned long offset_low, loff_t __user * result,
  139. unsigned int origin)
  140. {
  141. int retval;
  142. struct file * file;
  143. loff_t offset;
  144. int fput_needed;
  145. retval = -EBADF;
  146. file = fget_light(fd, &fput_needed);
  147. if (!file)
  148. goto bad;
  149. retval = -EINVAL;
  150. if (origin > SEEK_MAX)
  151. goto out_putf;
  152. offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
  153. origin);
  154. retval = (int)offset;
  155. if (offset >= 0) {
  156. retval = -EFAULT;
  157. if (!copy_to_user(result, &offset, sizeof(offset)))
  158. retval = 0;
  159. }
  160. out_putf:
  161. fput_light(file, fput_needed);
  162. bad:
  163. return retval;
  164. }
  165. #endif
  166. /*
  167. * rw_verify_area doesn't like huge counts. We limit
  168. * them to something that fits in "int" so that others
  169. * won't have to do range checks all the time.
  170. */
  171. #define MAX_RW_COUNT (INT_MAX & PAGE_CACHE_MASK)
  172. int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count)
  173. {
  174. struct inode *inode;
  175. loff_t pos;
  176. inode = file->f_path.dentry->d_inode;
  177. if (unlikely((ssize_t) count < 0))
  178. goto Einval;
  179. pos = *ppos;
  180. if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
  181. goto Einval;
  182. if (unlikely(inode->i_flock && mandatory_lock(inode))) {
  183. int retval = locks_mandatory_area(
  184. read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
  185. inode, file, pos, count);
  186. if (retval < 0)
  187. return retval;
  188. }
  189. return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
  190. Einval:
  191. return -EINVAL;
  192. }
  193. static void wait_on_retry_sync_kiocb(struct kiocb *iocb)
  194. {
  195. set_current_state(TASK_UNINTERRUPTIBLE);
  196. if (!kiocbIsKicked(iocb))
  197. schedule();
  198. else
  199. kiocbClearKicked(iocb);
  200. __set_current_state(TASK_RUNNING);
  201. }
  202. ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  203. {
  204. struct iovec iov = { .iov_base = buf, .iov_len = len };
  205. struct kiocb kiocb;
  206. ssize_t ret;
  207. init_sync_kiocb(&kiocb, filp);
  208. kiocb.ki_pos = *ppos;
  209. kiocb.ki_left = len;
  210. for (;;) {
  211. ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
  212. if (ret != -EIOCBRETRY)
  213. break;
  214. wait_on_retry_sync_kiocb(&kiocb);
  215. }
  216. if (-EIOCBQUEUED == ret)
  217. ret = wait_on_sync_kiocb(&kiocb);
  218. *ppos = kiocb.ki_pos;
  219. return ret;
  220. }
  221. EXPORT_SYMBOL(do_sync_read);
  222. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  223. {
  224. ssize_t ret;
  225. if (!(file->f_mode & FMODE_READ))
  226. return -EBADF;
  227. if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read))
  228. return -EINVAL;
  229. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  230. return -EFAULT;
  231. ret = rw_verify_area(READ, file, pos, count);
  232. if (ret >= 0) {
  233. count = ret;
  234. ret = security_file_permission (file, MAY_READ);
  235. if (!ret) {
  236. if (file->f_op->read)
  237. ret = file->f_op->read(file, buf, count, pos);
  238. else
  239. ret = do_sync_read(file, buf, count, pos);
  240. if (ret > 0) {
  241. fsnotify_access(file->f_path.dentry);
  242. add_rchar(current, ret);
  243. }
  244. inc_syscr(current);
  245. }
  246. }
  247. return ret;
  248. }
  249. EXPORT_SYMBOL(vfs_read);
  250. ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  251. {
  252. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  253. struct kiocb kiocb;
  254. ssize_t ret;
  255. init_sync_kiocb(&kiocb, filp);
  256. kiocb.ki_pos = *ppos;
  257. kiocb.ki_left = len;
  258. for (;;) {
  259. ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
  260. if (ret != -EIOCBRETRY)
  261. break;
  262. wait_on_retry_sync_kiocb(&kiocb);
  263. }
  264. if (-EIOCBQUEUED == ret)
  265. ret = wait_on_sync_kiocb(&kiocb);
  266. *ppos = kiocb.ki_pos;
  267. return ret;
  268. }
  269. EXPORT_SYMBOL(do_sync_write);
  270. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  271. {
  272. ssize_t ret;
  273. if (!(file->f_mode & FMODE_WRITE))
  274. return -EBADF;
  275. if (!file->f_op || (!file->f_op->write && !file->f_op->aio_write))
  276. return -EINVAL;
  277. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  278. return -EFAULT;
  279. ret = rw_verify_area(WRITE, file, pos, count);
  280. if (ret >= 0) {
  281. count = ret;
  282. ret = security_file_permission (file, MAY_WRITE);
  283. if (!ret) {
  284. if (file->f_op->write)
  285. ret = file->f_op->write(file, buf, count, pos);
  286. else
  287. ret = do_sync_write(file, buf, count, pos);
  288. if (ret > 0) {
  289. fsnotify_modify(file->f_path.dentry);
  290. add_wchar(current, ret);
  291. }
  292. inc_syscw(current);
  293. }
  294. }
  295. return ret;
  296. }
  297. EXPORT_SYMBOL(vfs_write);
  298. static inline loff_t file_pos_read(struct file *file)
  299. {
  300. return file->f_pos;
  301. }
  302. static inline void file_pos_write(struct file *file, loff_t pos)
  303. {
  304. file->f_pos = pos;
  305. }
  306. asmlinkage ssize_t sys_read(unsigned int fd, char __user * buf, size_t count)
  307. {
  308. struct file *file;
  309. ssize_t ret = -EBADF;
  310. int fput_needed;
  311. file = fget_light(fd, &fput_needed);
  312. if (file) {
  313. loff_t pos = file_pos_read(file);
  314. ret = vfs_read(file, buf, count, &pos);
  315. file_pos_write(file, pos);
  316. fput_light(file, fput_needed);
  317. }
  318. return ret;
  319. }
  320. EXPORT_SYMBOL_GPL(sys_read);
  321. asmlinkage ssize_t sys_write(unsigned int fd, const char __user * buf, size_t count)
  322. {
  323. struct file *file;
  324. ssize_t ret = -EBADF;
  325. int fput_needed;
  326. file = fget_light(fd, &fput_needed);
  327. if (file) {
  328. loff_t pos = file_pos_read(file);
  329. ret = vfs_write(file, buf, count, &pos);
  330. file_pos_write(file, pos);
  331. fput_light(file, fput_needed);
  332. }
  333. return ret;
  334. }
  335. asmlinkage ssize_t sys_pread64(unsigned int fd, char __user *buf,
  336. size_t count, loff_t pos)
  337. {
  338. struct file *file;
  339. ssize_t ret = -EBADF;
  340. int fput_needed;
  341. if (pos < 0)
  342. return -EINVAL;
  343. file = fget_light(fd, &fput_needed);
  344. if (file) {
  345. ret = -ESPIPE;
  346. if (file->f_mode & FMODE_PREAD)
  347. ret = vfs_read(file, buf, count, &pos);
  348. fput_light(file, fput_needed);
  349. }
  350. return ret;
  351. }
  352. asmlinkage ssize_t sys_pwrite64(unsigned int fd, const char __user *buf,
  353. size_t count, loff_t pos)
  354. {
  355. struct file *file;
  356. ssize_t ret = -EBADF;
  357. int fput_needed;
  358. if (pos < 0)
  359. return -EINVAL;
  360. file = fget_light(fd, &fput_needed);
  361. if (file) {
  362. ret = -ESPIPE;
  363. if (file->f_mode & FMODE_PWRITE)
  364. ret = vfs_write(file, buf, count, &pos);
  365. fput_light(file, fput_needed);
  366. }
  367. return ret;
  368. }
  369. /*
  370. * Reduce an iovec's length in-place. Return the resulting number of segments
  371. */
  372. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  373. {
  374. unsigned long seg = 0;
  375. size_t len = 0;
  376. while (seg < nr_segs) {
  377. seg++;
  378. if (len + iov->iov_len >= to) {
  379. iov->iov_len = to - len;
  380. break;
  381. }
  382. len += iov->iov_len;
  383. iov++;
  384. }
  385. return seg;
  386. }
  387. ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  388. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  389. {
  390. struct kiocb kiocb;
  391. ssize_t ret;
  392. init_sync_kiocb(&kiocb, filp);
  393. kiocb.ki_pos = *ppos;
  394. kiocb.ki_left = len;
  395. kiocb.ki_nbytes = len;
  396. for (;;) {
  397. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  398. if (ret != -EIOCBRETRY)
  399. break;
  400. wait_on_retry_sync_kiocb(&kiocb);
  401. }
  402. if (ret == -EIOCBQUEUED)
  403. ret = wait_on_sync_kiocb(&kiocb);
  404. *ppos = kiocb.ki_pos;
  405. return ret;
  406. }
  407. /* Do it by hand, with file-ops */
  408. ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  409. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  410. {
  411. struct iovec *vector = iov;
  412. ssize_t ret = 0;
  413. while (nr_segs > 0) {
  414. void __user *base;
  415. size_t len;
  416. ssize_t nr;
  417. base = vector->iov_base;
  418. len = vector->iov_len;
  419. vector++;
  420. nr_segs--;
  421. nr = fn(filp, base, len, ppos);
  422. if (nr < 0) {
  423. if (!ret)
  424. ret = nr;
  425. break;
  426. }
  427. ret += nr;
  428. if (nr != len)
  429. break;
  430. }
  431. return ret;
  432. }
  433. /* A write operation does a read from user space and vice versa */
  434. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  435. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  436. unsigned long nr_segs, unsigned long fast_segs,
  437. struct iovec *fast_pointer,
  438. struct iovec **ret_pointer)
  439. {
  440. unsigned long seg;
  441. ssize_t ret;
  442. struct iovec *iov = fast_pointer;
  443. /*
  444. * SuS says "The readv() function *may* fail if the iovcnt argument
  445. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  446. * traditionally returned zero for zero segments, so...
  447. */
  448. if (nr_segs == 0) {
  449. ret = 0;
  450. goto out;
  451. }
  452. /*
  453. * First get the "struct iovec" from user memory and
  454. * verify all the pointers
  455. */
  456. if (nr_segs > UIO_MAXIOV) {
  457. ret = -EINVAL;
  458. goto out;
  459. }
  460. if (nr_segs > fast_segs) {
  461. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  462. if (iov == NULL) {
  463. ret = -ENOMEM;
  464. goto out;
  465. }
  466. }
  467. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  468. ret = -EFAULT;
  469. goto out;
  470. }
  471. /*
  472. * According to the Single Unix Specification we should return EINVAL
  473. * if an element length is < 0 when cast to ssize_t or if the
  474. * total length would overflow the ssize_t return value of the
  475. * system call.
  476. */
  477. ret = 0;
  478. for (seg = 0; seg < nr_segs; seg++) {
  479. void __user *buf = iov[seg].iov_base;
  480. ssize_t len = (ssize_t)iov[seg].iov_len;
  481. /* see if we we're about to use an invalid len or if
  482. * it's about to overflow ssize_t */
  483. if (len < 0 || (ret + len < ret)) {
  484. ret = -EINVAL;
  485. goto out;
  486. }
  487. if (unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  488. ret = -EFAULT;
  489. goto out;
  490. }
  491. ret += len;
  492. }
  493. out:
  494. *ret_pointer = iov;
  495. return ret;
  496. }
  497. static ssize_t do_readv_writev(int type, struct file *file,
  498. const struct iovec __user * uvector,
  499. unsigned long nr_segs, loff_t *pos)
  500. {
  501. size_t tot_len;
  502. struct iovec iovstack[UIO_FASTIOV];
  503. struct iovec *iov = iovstack;
  504. ssize_t ret;
  505. io_fn_t fn;
  506. iov_fn_t fnv;
  507. if (!file->f_op) {
  508. ret = -EINVAL;
  509. goto out;
  510. }
  511. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  512. ARRAY_SIZE(iovstack), iovstack, &iov);
  513. if (ret <= 0)
  514. goto out;
  515. tot_len = ret;
  516. ret = rw_verify_area(type, file, pos, tot_len);
  517. if (ret < 0)
  518. goto out;
  519. ret = security_file_permission(file, type == READ ? MAY_READ : MAY_WRITE);
  520. if (ret)
  521. goto out;
  522. fnv = NULL;
  523. if (type == READ) {
  524. fn = file->f_op->read;
  525. fnv = file->f_op->aio_read;
  526. } else {
  527. fn = (io_fn_t)file->f_op->write;
  528. fnv = file->f_op->aio_write;
  529. }
  530. if (fnv)
  531. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  532. pos, fnv);
  533. else
  534. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  535. out:
  536. if (iov != iovstack)
  537. kfree(iov);
  538. if ((ret + (type == READ)) > 0) {
  539. if (type == READ)
  540. fsnotify_access(file->f_path.dentry);
  541. else
  542. fsnotify_modify(file->f_path.dentry);
  543. }
  544. return ret;
  545. }
  546. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  547. unsigned long vlen, loff_t *pos)
  548. {
  549. if (!(file->f_mode & FMODE_READ))
  550. return -EBADF;
  551. if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
  552. return -EINVAL;
  553. return do_readv_writev(READ, file, vec, vlen, pos);
  554. }
  555. EXPORT_SYMBOL(vfs_readv);
  556. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  557. unsigned long vlen, loff_t *pos)
  558. {
  559. if (!(file->f_mode & FMODE_WRITE))
  560. return -EBADF;
  561. if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
  562. return -EINVAL;
  563. return do_readv_writev(WRITE, file, vec, vlen, pos);
  564. }
  565. EXPORT_SYMBOL(vfs_writev);
  566. asmlinkage ssize_t
  567. sys_readv(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
  568. {
  569. struct file *file;
  570. ssize_t ret = -EBADF;
  571. int fput_needed;
  572. file = fget_light(fd, &fput_needed);
  573. if (file) {
  574. loff_t pos = file_pos_read(file);
  575. ret = vfs_readv(file, vec, vlen, &pos);
  576. file_pos_write(file, pos);
  577. fput_light(file, fput_needed);
  578. }
  579. if (ret > 0)
  580. add_rchar(current, ret);
  581. inc_syscr(current);
  582. return ret;
  583. }
  584. asmlinkage ssize_t
  585. sys_writev(unsigned long fd, const struct iovec __user *vec, unsigned long vlen)
  586. {
  587. struct file *file;
  588. ssize_t ret = -EBADF;
  589. int fput_needed;
  590. file = fget_light(fd, &fput_needed);
  591. if (file) {
  592. loff_t pos = file_pos_read(file);
  593. ret = vfs_writev(file, vec, vlen, &pos);
  594. file_pos_write(file, pos);
  595. fput_light(file, fput_needed);
  596. }
  597. if (ret > 0)
  598. add_wchar(current, ret);
  599. inc_syscw(current);
  600. return ret;
  601. }
  602. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  603. size_t count, loff_t max)
  604. {
  605. struct file * in_file, * out_file;
  606. struct inode * in_inode, * out_inode;
  607. loff_t pos;
  608. ssize_t retval;
  609. int fput_needed_in, fput_needed_out, fl;
  610. /*
  611. * Get input file, and verify that it is ok..
  612. */
  613. retval = -EBADF;
  614. in_file = fget_light(in_fd, &fput_needed_in);
  615. if (!in_file)
  616. goto out;
  617. if (!(in_file->f_mode & FMODE_READ))
  618. goto fput_in;
  619. retval = -EINVAL;
  620. in_inode = in_file->f_path.dentry->d_inode;
  621. if (!in_inode)
  622. goto fput_in;
  623. if (!in_file->f_op || !in_file->f_op->splice_read)
  624. goto fput_in;
  625. retval = -ESPIPE;
  626. if (!ppos)
  627. ppos = &in_file->f_pos;
  628. else
  629. if (!(in_file->f_mode & FMODE_PREAD))
  630. goto fput_in;
  631. retval = rw_verify_area(READ, in_file, ppos, count);
  632. if (retval < 0)
  633. goto fput_in;
  634. count = retval;
  635. retval = security_file_permission (in_file, MAY_READ);
  636. if (retval)
  637. goto fput_in;
  638. /*
  639. * Get output file, and verify that it is ok..
  640. */
  641. retval = -EBADF;
  642. out_file = fget_light(out_fd, &fput_needed_out);
  643. if (!out_file)
  644. goto fput_in;
  645. if (!(out_file->f_mode & FMODE_WRITE))
  646. goto fput_out;
  647. retval = -EINVAL;
  648. if (!out_file->f_op || !out_file->f_op->sendpage)
  649. goto fput_out;
  650. out_inode = out_file->f_path.dentry->d_inode;
  651. retval = rw_verify_area(WRITE, out_file, &out_file->f_pos, count);
  652. if (retval < 0)
  653. goto fput_out;
  654. count = retval;
  655. retval = security_file_permission (out_file, MAY_WRITE);
  656. if (retval)
  657. goto fput_out;
  658. if (!max)
  659. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  660. pos = *ppos;
  661. retval = -EINVAL;
  662. if (unlikely(pos < 0))
  663. goto fput_out;
  664. if (unlikely(pos + count > max)) {
  665. retval = -EOVERFLOW;
  666. if (pos >= max)
  667. goto fput_out;
  668. count = max - pos;
  669. }
  670. fl = 0;
  671. #if 0
  672. /*
  673. * We need to debate whether we can enable this or not. The
  674. * man page documents EAGAIN return for the output at least,
  675. * and the application is arguably buggy if it doesn't expect
  676. * EAGAIN on a non-blocking file descriptor.
  677. */
  678. if (in_file->f_flags & O_NONBLOCK)
  679. fl = SPLICE_F_NONBLOCK;
  680. #endif
  681. retval = do_splice_direct(in_file, ppos, out_file, count, fl);
  682. if (retval > 0) {
  683. add_rchar(current, retval);
  684. add_wchar(current, retval);
  685. }
  686. inc_syscr(current);
  687. inc_syscw(current);
  688. if (*ppos > max)
  689. retval = -EOVERFLOW;
  690. fput_out:
  691. fput_light(out_file, fput_needed_out);
  692. fput_in:
  693. fput_light(in_file, fput_needed_in);
  694. out:
  695. return retval;
  696. }
  697. asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t __user *offset, size_t count)
  698. {
  699. loff_t pos;
  700. off_t off;
  701. ssize_t ret;
  702. if (offset) {
  703. if (unlikely(get_user(off, offset)))
  704. return -EFAULT;
  705. pos = off;
  706. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  707. if (unlikely(put_user(pos, offset)))
  708. return -EFAULT;
  709. return ret;
  710. }
  711. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  712. }
  713. asmlinkage ssize_t sys_sendfile64(int out_fd, int in_fd, loff_t __user *offset, size_t count)
  714. {
  715. loff_t pos;
  716. ssize_t ret;
  717. if (offset) {
  718. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  719. return -EFAULT;
  720. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  721. if (unlikely(put_user(pos, offset)))
  722. return -EFAULT;
  723. return ret;
  724. }
  725. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  726. }