read_write.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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/aio.h>
  12. #include <linux/fsnotify.h>
  13. #include <linux/security.h>
  14. #include <linux/export.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/splice.h>
  18. #include <linux/compat.h>
  19. #include "internal.h"
  20. #include <asm/uaccess.h>
  21. #include <asm/unistd.h>
  22. typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
  23. typedef ssize_t (*iov_fn_t)(struct kiocb *, const struct iovec *,
  24. unsigned long, loff_t);
  25. typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
  26. const struct file_operations generic_ro_fops = {
  27. .llseek = generic_file_llseek,
  28. .read = new_sync_read,
  29. .read_iter = generic_file_read_iter,
  30. .mmap = generic_file_readonly_mmap,
  31. .splice_read = generic_file_splice_read,
  32. };
  33. EXPORT_SYMBOL(generic_ro_fops);
  34. static inline int unsigned_offsets(struct file *file)
  35. {
  36. return file->f_mode & FMODE_UNSIGNED_OFFSET;
  37. }
  38. /**
  39. * vfs_setpos - update the file offset for lseek
  40. * @file: file structure in question
  41. * @offset: file offset to seek to
  42. * @maxsize: maximum file size
  43. *
  44. * This is a low-level filesystem helper for updating the file offset to
  45. * the value specified by @offset if the given offset is valid and it is
  46. * not equal to the current file offset.
  47. *
  48. * Return the specified offset on success and -EINVAL on invalid offset.
  49. */
  50. loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
  51. {
  52. if (offset < 0 && !unsigned_offsets(file))
  53. return -EINVAL;
  54. if (offset > maxsize)
  55. return -EINVAL;
  56. if (offset != file->f_pos) {
  57. file->f_pos = offset;
  58. file->f_version = 0;
  59. }
  60. return offset;
  61. }
  62. EXPORT_SYMBOL(vfs_setpos);
  63. /**
  64. * generic_file_llseek_size - generic llseek implementation for regular files
  65. * @file: file structure to seek on
  66. * @offset: file offset to seek to
  67. * @whence: type of seek
  68. * @size: max size of this file in file system
  69. * @eof: offset used for SEEK_END position
  70. *
  71. * This is a variant of generic_file_llseek that allows passing in a custom
  72. * maximum file size and a custom EOF position, for e.g. hashed directories
  73. *
  74. * Synchronization:
  75. * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
  76. * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
  77. * read/writes behave like SEEK_SET against seeks.
  78. */
  79. loff_t
  80. generic_file_llseek_size(struct file *file, loff_t offset, int whence,
  81. loff_t maxsize, loff_t eof)
  82. {
  83. switch (whence) {
  84. case SEEK_END:
  85. offset += eof;
  86. break;
  87. case SEEK_CUR:
  88. /*
  89. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  90. * position-querying operation. Avoid rewriting the "same"
  91. * f_pos value back to the file because a concurrent read(),
  92. * write() or lseek() might have altered it
  93. */
  94. if (offset == 0)
  95. return file->f_pos;
  96. /*
  97. * f_lock protects against read/modify/write race with other
  98. * SEEK_CURs. Note that parallel writes and reads behave
  99. * like SEEK_SET.
  100. */
  101. spin_lock(&file->f_lock);
  102. offset = vfs_setpos(file, file->f_pos + offset, maxsize);
  103. spin_unlock(&file->f_lock);
  104. return offset;
  105. case SEEK_DATA:
  106. /*
  107. * In the generic case the entire file is data, so as long as
  108. * offset isn't at the end of the file then the offset is data.
  109. */
  110. if (offset >= eof)
  111. return -ENXIO;
  112. break;
  113. case SEEK_HOLE:
  114. /*
  115. * There is a virtual hole at the end of the file, so as long as
  116. * offset isn't i_size or larger, return i_size.
  117. */
  118. if (offset >= eof)
  119. return -ENXIO;
  120. offset = eof;
  121. break;
  122. }
  123. return vfs_setpos(file, offset, maxsize);
  124. }
  125. EXPORT_SYMBOL(generic_file_llseek_size);
  126. /**
  127. * generic_file_llseek - generic llseek implementation for regular files
  128. * @file: file structure to seek on
  129. * @offset: file offset to seek to
  130. * @whence: type of seek
  131. *
  132. * This is a generic implemenation of ->llseek useable for all normal local
  133. * filesystems. It just updates the file offset to the value specified by
  134. * @offset and @whence.
  135. */
  136. loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
  137. {
  138. struct inode *inode = file->f_mapping->host;
  139. return generic_file_llseek_size(file, offset, whence,
  140. inode->i_sb->s_maxbytes,
  141. i_size_read(inode));
  142. }
  143. EXPORT_SYMBOL(generic_file_llseek);
  144. /**
  145. * fixed_size_llseek - llseek implementation for fixed-sized devices
  146. * @file: file structure to seek on
  147. * @offset: file offset to seek to
  148. * @whence: type of seek
  149. * @size: size of the file
  150. *
  151. */
  152. loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
  153. {
  154. switch (whence) {
  155. case SEEK_SET: case SEEK_CUR: case SEEK_END:
  156. return generic_file_llseek_size(file, offset, whence,
  157. size, size);
  158. default:
  159. return -EINVAL;
  160. }
  161. }
  162. EXPORT_SYMBOL(fixed_size_llseek);
  163. /**
  164. * noop_llseek - No Operation Performed llseek implementation
  165. * @file: file structure to seek on
  166. * @offset: file offset to seek to
  167. * @whence: type of seek
  168. *
  169. * This is an implementation of ->llseek useable for the rare special case when
  170. * userspace expects the seek to succeed but the (device) file is actually not
  171. * able to perform the seek. In this case you use noop_llseek() instead of
  172. * falling back to the default implementation of ->llseek.
  173. */
  174. loff_t noop_llseek(struct file *file, loff_t offset, int whence)
  175. {
  176. return file->f_pos;
  177. }
  178. EXPORT_SYMBOL(noop_llseek);
  179. loff_t no_llseek(struct file *file, loff_t offset, int whence)
  180. {
  181. return -ESPIPE;
  182. }
  183. EXPORT_SYMBOL(no_llseek);
  184. loff_t default_llseek(struct file *file, loff_t offset, int whence)
  185. {
  186. struct inode *inode = file_inode(file);
  187. loff_t retval;
  188. mutex_lock(&inode->i_mutex);
  189. switch (whence) {
  190. case SEEK_END:
  191. offset += i_size_read(inode);
  192. break;
  193. case SEEK_CUR:
  194. if (offset == 0) {
  195. retval = file->f_pos;
  196. goto out;
  197. }
  198. offset += file->f_pos;
  199. break;
  200. case SEEK_DATA:
  201. /*
  202. * In the generic case the entire file is data, so as
  203. * long as offset isn't at the end of the file then the
  204. * offset is data.
  205. */
  206. if (offset >= inode->i_size) {
  207. retval = -ENXIO;
  208. goto out;
  209. }
  210. break;
  211. case SEEK_HOLE:
  212. /*
  213. * There is a virtual hole at the end of the file, so
  214. * as long as offset isn't i_size or larger, return
  215. * i_size.
  216. */
  217. if (offset >= inode->i_size) {
  218. retval = -ENXIO;
  219. goto out;
  220. }
  221. offset = inode->i_size;
  222. break;
  223. }
  224. retval = -EINVAL;
  225. if (offset >= 0 || unsigned_offsets(file)) {
  226. if (offset != file->f_pos) {
  227. file->f_pos = offset;
  228. file->f_version = 0;
  229. }
  230. retval = offset;
  231. }
  232. out:
  233. mutex_unlock(&inode->i_mutex);
  234. return retval;
  235. }
  236. EXPORT_SYMBOL(default_llseek);
  237. loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
  238. {
  239. loff_t (*fn)(struct file *, loff_t, int);
  240. fn = no_llseek;
  241. if (file->f_mode & FMODE_LSEEK) {
  242. if (file->f_op->llseek)
  243. fn = file->f_op->llseek;
  244. }
  245. return fn(file, offset, whence);
  246. }
  247. EXPORT_SYMBOL(vfs_llseek);
  248. static inline struct fd fdget_pos(int fd)
  249. {
  250. return __to_fd(__fdget_pos(fd));
  251. }
  252. static inline void fdput_pos(struct fd f)
  253. {
  254. if (f.flags & FDPUT_POS_UNLOCK)
  255. mutex_unlock(&f.file->f_pos_lock);
  256. fdput(f);
  257. }
  258. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  259. {
  260. off_t retval;
  261. struct fd f = fdget_pos(fd);
  262. if (!f.file)
  263. return -EBADF;
  264. retval = -EINVAL;
  265. if (whence <= SEEK_MAX) {
  266. loff_t res = vfs_llseek(f.file, offset, whence);
  267. retval = res;
  268. if (res != (loff_t)retval)
  269. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  270. }
  271. fdput_pos(f);
  272. return retval;
  273. }
  274. #ifdef CONFIG_COMPAT
  275. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  276. {
  277. return sys_lseek(fd, offset, whence);
  278. }
  279. #endif
  280. #ifdef __ARCH_WANT_SYS_LLSEEK
  281. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  282. unsigned long, offset_low, loff_t __user *, result,
  283. unsigned int, whence)
  284. {
  285. int retval;
  286. struct fd f = fdget_pos(fd);
  287. loff_t offset;
  288. if (!f.file)
  289. return -EBADF;
  290. retval = -EINVAL;
  291. if (whence > SEEK_MAX)
  292. goto out_putf;
  293. offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
  294. whence);
  295. retval = (int)offset;
  296. if (offset >= 0) {
  297. retval = -EFAULT;
  298. if (!copy_to_user(result, &offset, sizeof(offset)))
  299. retval = 0;
  300. }
  301. out_putf:
  302. fdput_pos(f);
  303. return retval;
  304. }
  305. #endif
  306. ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
  307. {
  308. struct kiocb kiocb;
  309. ssize_t ret;
  310. if (!file->f_op->read_iter)
  311. return -EINVAL;
  312. init_sync_kiocb(&kiocb, file);
  313. kiocb.ki_pos = *ppos;
  314. kiocb.ki_nbytes = iov_iter_count(iter);
  315. iter->type |= READ;
  316. ret = file->f_op->read_iter(&kiocb, iter);
  317. if (ret == -EIOCBQUEUED)
  318. ret = wait_on_sync_kiocb(&kiocb);
  319. if (ret > 0)
  320. *ppos = kiocb.ki_pos;
  321. return ret;
  322. }
  323. EXPORT_SYMBOL(vfs_iter_read);
  324. ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
  325. {
  326. struct kiocb kiocb;
  327. ssize_t ret;
  328. if (!file->f_op->write_iter)
  329. return -EINVAL;
  330. init_sync_kiocb(&kiocb, file);
  331. kiocb.ki_pos = *ppos;
  332. kiocb.ki_nbytes = iov_iter_count(iter);
  333. iter->type |= WRITE;
  334. ret = file->f_op->write_iter(&kiocb, iter);
  335. if (ret == -EIOCBQUEUED)
  336. ret = wait_on_sync_kiocb(&kiocb);
  337. if (ret > 0)
  338. *ppos = kiocb.ki_pos;
  339. return ret;
  340. }
  341. EXPORT_SYMBOL(vfs_iter_write);
  342. /*
  343. * rw_verify_area doesn't like huge counts. We limit
  344. * them to something that fits in "int" so that others
  345. * won't have to do range checks all the time.
  346. */
  347. int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
  348. {
  349. struct inode *inode;
  350. loff_t pos;
  351. int retval = -EINVAL;
  352. inode = file_inode(file);
  353. if (unlikely((ssize_t) count < 0))
  354. return retval;
  355. pos = *ppos;
  356. if (unlikely(pos < 0)) {
  357. if (!unsigned_offsets(file))
  358. return retval;
  359. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  360. return -EOVERFLOW;
  361. } else if (unlikely((loff_t) (pos + count) < 0)) {
  362. if (!unsigned_offsets(file))
  363. return retval;
  364. }
  365. if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
  366. retval = locks_mandatory_area(
  367. read_write == READ ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE,
  368. inode, file, pos, count);
  369. if (retval < 0)
  370. return retval;
  371. }
  372. retval = security_file_permission(file,
  373. read_write == READ ? MAY_READ : MAY_WRITE);
  374. if (retval)
  375. return retval;
  376. return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
  377. }
  378. ssize_t do_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  379. {
  380. struct iovec iov = { .iov_base = buf, .iov_len = len };
  381. struct kiocb kiocb;
  382. ssize_t ret;
  383. init_sync_kiocb(&kiocb, filp);
  384. kiocb.ki_pos = *ppos;
  385. kiocb.ki_nbytes = len;
  386. ret = filp->f_op->aio_read(&kiocb, &iov, 1, kiocb.ki_pos);
  387. if (-EIOCBQUEUED == ret)
  388. ret = wait_on_sync_kiocb(&kiocb);
  389. *ppos = kiocb.ki_pos;
  390. return ret;
  391. }
  392. EXPORT_SYMBOL(do_sync_read);
  393. ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  394. {
  395. struct iovec iov = { .iov_base = buf, .iov_len = len };
  396. struct kiocb kiocb;
  397. struct iov_iter iter;
  398. ssize_t ret;
  399. init_sync_kiocb(&kiocb, filp);
  400. kiocb.ki_pos = *ppos;
  401. kiocb.ki_nbytes = len;
  402. iov_iter_init(&iter, READ, &iov, 1, len);
  403. ret = filp->f_op->read_iter(&kiocb, &iter);
  404. if (-EIOCBQUEUED == ret)
  405. ret = wait_on_sync_kiocb(&kiocb);
  406. *ppos = kiocb.ki_pos;
  407. return ret;
  408. }
  409. EXPORT_SYMBOL(new_sync_read);
  410. ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
  411. loff_t *pos)
  412. {
  413. ssize_t ret;
  414. if (file->f_op->read)
  415. ret = file->f_op->read(file, buf, count, pos);
  416. else if (file->f_op->aio_read)
  417. ret = do_sync_read(file, buf, count, pos);
  418. else if (file->f_op->read_iter)
  419. ret = new_sync_read(file, buf, count, pos);
  420. else
  421. ret = -EINVAL;
  422. return ret;
  423. }
  424. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  425. {
  426. ssize_t ret;
  427. if (!(file->f_mode & FMODE_READ))
  428. return -EBADF;
  429. if (!(file->f_mode & FMODE_CAN_READ))
  430. return -EINVAL;
  431. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  432. return -EFAULT;
  433. ret = rw_verify_area(READ, file, pos, count);
  434. if (ret >= 0) {
  435. count = ret;
  436. ret = __vfs_read(file, buf, count, pos);
  437. if (ret > 0) {
  438. fsnotify_access(file);
  439. add_rchar(current, ret);
  440. }
  441. inc_syscr(current);
  442. }
  443. return ret;
  444. }
  445. EXPORT_SYMBOL(vfs_read);
  446. ssize_t do_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  447. {
  448. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  449. struct kiocb kiocb;
  450. ssize_t ret;
  451. init_sync_kiocb(&kiocb, filp);
  452. kiocb.ki_pos = *ppos;
  453. kiocb.ki_nbytes = len;
  454. ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);
  455. if (-EIOCBQUEUED == ret)
  456. ret = wait_on_sync_kiocb(&kiocb);
  457. *ppos = kiocb.ki_pos;
  458. return ret;
  459. }
  460. EXPORT_SYMBOL(do_sync_write);
  461. ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  462. {
  463. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  464. struct kiocb kiocb;
  465. struct iov_iter iter;
  466. ssize_t ret;
  467. init_sync_kiocb(&kiocb, filp);
  468. kiocb.ki_pos = *ppos;
  469. kiocb.ki_nbytes = len;
  470. iov_iter_init(&iter, WRITE, &iov, 1, len);
  471. ret = filp->f_op->write_iter(&kiocb, &iter);
  472. if (-EIOCBQUEUED == ret)
  473. ret = wait_on_sync_kiocb(&kiocb);
  474. *ppos = kiocb.ki_pos;
  475. return ret;
  476. }
  477. EXPORT_SYMBOL(new_sync_write);
  478. ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
  479. {
  480. mm_segment_t old_fs;
  481. const char __user *p;
  482. ssize_t ret;
  483. if (!(file->f_mode & FMODE_CAN_WRITE))
  484. return -EINVAL;
  485. old_fs = get_fs();
  486. set_fs(get_ds());
  487. p = (__force const char __user *)buf;
  488. if (count > MAX_RW_COUNT)
  489. count = MAX_RW_COUNT;
  490. if (file->f_op->write)
  491. ret = file->f_op->write(file, p, count, pos);
  492. else if (file->f_op->aio_write)
  493. ret = do_sync_write(file, p, count, pos);
  494. else
  495. ret = new_sync_write(file, p, count, pos);
  496. set_fs(old_fs);
  497. if (ret > 0) {
  498. fsnotify_modify(file);
  499. add_wchar(current, ret);
  500. }
  501. inc_syscw(current);
  502. return ret;
  503. }
  504. EXPORT_SYMBOL(__kernel_write);
  505. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  506. {
  507. ssize_t ret;
  508. if (!(file->f_mode & FMODE_WRITE))
  509. return -EBADF;
  510. if (!(file->f_mode & FMODE_CAN_WRITE))
  511. return -EINVAL;
  512. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  513. return -EFAULT;
  514. ret = rw_verify_area(WRITE, file, pos, count);
  515. if (ret >= 0) {
  516. count = ret;
  517. file_start_write(file);
  518. if (file->f_op->write)
  519. ret = file->f_op->write(file, buf, count, pos);
  520. else if (file->f_op->aio_write)
  521. ret = do_sync_write(file, buf, count, pos);
  522. else
  523. ret = new_sync_write(file, buf, count, pos);
  524. if (ret > 0) {
  525. fsnotify_modify(file);
  526. add_wchar(current, ret);
  527. }
  528. inc_syscw(current);
  529. file_end_write(file);
  530. }
  531. return ret;
  532. }
  533. EXPORT_SYMBOL(vfs_write);
  534. static inline loff_t file_pos_read(struct file *file)
  535. {
  536. return file->f_pos;
  537. }
  538. static inline void file_pos_write(struct file *file, loff_t pos)
  539. {
  540. file->f_pos = pos;
  541. }
  542. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  543. {
  544. struct fd f = fdget_pos(fd);
  545. ssize_t ret = -EBADF;
  546. if (f.file) {
  547. loff_t pos = file_pos_read(f.file);
  548. ret = vfs_read(f.file, buf, count, &pos);
  549. if (ret >= 0)
  550. file_pos_write(f.file, pos);
  551. fdput_pos(f);
  552. }
  553. return ret;
  554. }
  555. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  556. size_t, count)
  557. {
  558. struct fd f = fdget_pos(fd);
  559. ssize_t ret = -EBADF;
  560. if (f.file) {
  561. loff_t pos = file_pos_read(f.file);
  562. ret = vfs_write(f.file, buf, count, &pos);
  563. if (ret >= 0)
  564. file_pos_write(f.file, pos);
  565. fdput_pos(f);
  566. }
  567. return ret;
  568. }
  569. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  570. size_t, count, loff_t, pos)
  571. {
  572. struct fd f;
  573. ssize_t ret = -EBADF;
  574. if (pos < 0)
  575. return -EINVAL;
  576. f = fdget(fd);
  577. if (f.file) {
  578. ret = -ESPIPE;
  579. if (f.file->f_mode & FMODE_PREAD)
  580. ret = vfs_read(f.file, buf, count, &pos);
  581. fdput(f);
  582. }
  583. return ret;
  584. }
  585. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  586. size_t, count, loff_t, pos)
  587. {
  588. struct fd f;
  589. ssize_t ret = -EBADF;
  590. if (pos < 0)
  591. return -EINVAL;
  592. f = fdget(fd);
  593. if (f.file) {
  594. ret = -ESPIPE;
  595. if (f.file->f_mode & FMODE_PWRITE)
  596. ret = vfs_write(f.file, buf, count, &pos);
  597. fdput(f);
  598. }
  599. return ret;
  600. }
  601. /*
  602. * Reduce an iovec's length in-place. Return the resulting number of segments
  603. */
  604. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  605. {
  606. unsigned long seg = 0;
  607. size_t len = 0;
  608. while (seg < nr_segs) {
  609. seg++;
  610. if (len + iov->iov_len >= to) {
  611. iov->iov_len = to - len;
  612. break;
  613. }
  614. len += iov->iov_len;
  615. iov++;
  616. }
  617. return seg;
  618. }
  619. EXPORT_SYMBOL(iov_shorten);
  620. static ssize_t do_iter_readv_writev(struct file *filp, int rw, const struct iovec *iov,
  621. unsigned long nr_segs, size_t len, loff_t *ppos, iter_fn_t fn)
  622. {
  623. struct kiocb kiocb;
  624. struct iov_iter iter;
  625. ssize_t ret;
  626. init_sync_kiocb(&kiocb, filp);
  627. kiocb.ki_pos = *ppos;
  628. kiocb.ki_nbytes = len;
  629. iov_iter_init(&iter, rw, iov, nr_segs, len);
  630. ret = fn(&kiocb, &iter);
  631. if (ret == -EIOCBQUEUED)
  632. ret = wait_on_sync_kiocb(&kiocb);
  633. *ppos = kiocb.ki_pos;
  634. return ret;
  635. }
  636. static ssize_t do_sync_readv_writev(struct file *filp, const struct iovec *iov,
  637. unsigned long nr_segs, size_t len, loff_t *ppos, iov_fn_t fn)
  638. {
  639. struct kiocb kiocb;
  640. ssize_t ret;
  641. init_sync_kiocb(&kiocb, filp);
  642. kiocb.ki_pos = *ppos;
  643. kiocb.ki_nbytes = len;
  644. ret = fn(&kiocb, iov, nr_segs, kiocb.ki_pos);
  645. if (ret == -EIOCBQUEUED)
  646. ret = wait_on_sync_kiocb(&kiocb);
  647. *ppos = kiocb.ki_pos;
  648. return ret;
  649. }
  650. /* Do it by hand, with file-ops */
  651. static ssize_t do_loop_readv_writev(struct file *filp, struct iovec *iov,
  652. unsigned long nr_segs, loff_t *ppos, io_fn_t fn)
  653. {
  654. struct iovec *vector = iov;
  655. ssize_t ret = 0;
  656. while (nr_segs > 0) {
  657. void __user *base;
  658. size_t len;
  659. ssize_t nr;
  660. base = vector->iov_base;
  661. len = vector->iov_len;
  662. vector++;
  663. nr_segs--;
  664. nr = fn(filp, base, len, ppos);
  665. if (nr < 0) {
  666. if (!ret)
  667. ret = nr;
  668. break;
  669. }
  670. ret += nr;
  671. if (nr != len)
  672. break;
  673. }
  674. return ret;
  675. }
  676. /* A write operation does a read from user space and vice versa */
  677. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  678. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  679. unsigned long nr_segs, unsigned long fast_segs,
  680. struct iovec *fast_pointer,
  681. struct iovec **ret_pointer)
  682. {
  683. unsigned long seg;
  684. ssize_t ret;
  685. struct iovec *iov = fast_pointer;
  686. /*
  687. * SuS says "The readv() function *may* fail if the iovcnt argument
  688. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  689. * traditionally returned zero for zero segments, so...
  690. */
  691. if (nr_segs == 0) {
  692. ret = 0;
  693. goto out;
  694. }
  695. /*
  696. * First get the "struct iovec" from user memory and
  697. * verify all the pointers
  698. */
  699. if (nr_segs > UIO_MAXIOV) {
  700. ret = -EINVAL;
  701. goto out;
  702. }
  703. if (nr_segs > fast_segs) {
  704. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  705. if (iov == NULL) {
  706. ret = -ENOMEM;
  707. goto out;
  708. }
  709. }
  710. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  711. ret = -EFAULT;
  712. goto out;
  713. }
  714. /*
  715. * According to the Single Unix Specification we should return EINVAL
  716. * if an element length is < 0 when cast to ssize_t or if the
  717. * total length would overflow the ssize_t return value of the
  718. * system call.
  719. *
  720. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  721. * overflow case.
  722. */
  723. ret = 0;
  724. for (seg = 0; seg < nr_segs; seg++) {
  725. void __user *buf = iov[seg].iov_base;
  726. ssize_t len = (ssize_t)iov[seg].iov_len;
  727. /* see if we we're about to use an invalid len or if
  728. * it's about to overflow ssize_t */
  729. if (len < 0) {
  730. ret = -EINVAL;
  731. goto out;
  732. }
  733. if (type >= 0
  734. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  735. ret = -EFAULT;
  736. goto out;
  737. }
  738. if (len > MAX_RW_COUNT - ret) {
  739. len = MAX_RW_COUNT - ret;
  740. iov[seg].iov_len = len;
  741. }
  742. ret += len;
  743. }
  744. out:
  745. *ret_pointer = iov;
  746. return ret;
  747. }
  748. static ssize_t do_readv_writev(int type, struct file *file,
  749. const struct iovec __user * uvector,
  750. unsigned long nr_segs, loff_t *pos)
  751. {
  752. size_t tot_len;
  753. struct iovec iovstack[UIO_FASTIOV];
  754. struct iovec *iov = iovstack;
  755. ssize_t ret;
  756. io_fn_t fn;
  757. iov_fn_t fnv;
  758. iter_fn_t iter_fn;
  759. ret = rw_copy_check_uvector(type, uvector, nr_segs,
  760. ARRAY_SIZE(iovstack), iovstack, &iov);
  761. if (ret <= 0)
  762. goto out;
  763. tot_len = ret;
  764. ret = rw_verify_area(type, file, pos, tot_len);
  765. if (ret < 0)
  766. goto out;
  767. fnv = NULL;
  768. if (type == READ) {
  769. fn = file->f_op->read;
  770. fnv = file->f_op->aio_read;
  771. iter_fn = file->f_op->read_iter;
  772. } else {
  773. fn = (io_fn_t)file->f_op->write;
  774. fnv = file->f_op->aio_write;
  775. iter_fn = file->f_op->write_iter;
  776. file_start_write(file);
  777. }
  778. if (iter_fn)
  779. ret = do_iter_readv_writev(file, type, iov, nr_segs, tot_len,
  780. pos, iter_fn);
  781. else if (fnv)
  782. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  783. pos, fnv);
  784. else
  785. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  786. if (type != READ)
  787. file_end_write(file);
  788. out:
  789. if (iov != iovstack)
  790. kfree(iov);
  791. if ((ret + (type == READ)) > 0) {
  792. if (type == READ)
  793. fsnotify_access(file);
  794. else
  795. fsnotify_modify(file);
  796. }
  797. return ret;
  798. }
  799. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  800. unsigned long vlen, loff_t *pos)
  801. {
  802. if (!(file->f_mode & FMODE_READ))
  803. return -EBADF;
  804. if (!(file->f_mode & FMODE_CAN_READ))
  805. return -EINVAL;
  806. return do_readv_writev(READ, file, vec, vlen, pos);
  807. }
  808. EXPORT_SYMBOL(vfs_readv);
  809. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  810. unsigned long vlen, loff_t *pos)
  811. {
  812. if (!(file->f_mode & FMODE_WRITE))
  813. return -EBADF;
  814. if (!(file->f_mode & FMODE_CAN_WRITE))
  815. return -EINVAL;
  816. return do_readv_writev(WRITE, file, vec, vlen, pos);
  817. }
  818. EXPORT_SYMBOL(vfs_writev);
  819. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  820. unsigned long, vlen)
  821. {
  822. struct fd f = fdget_pos(fd);
  823. ssize_t ret = -EBADF;
  824. if (f.file) {
  825. loff_t pos = file_pos_read(f.file);
  826. ret = vfs_readv(f.file, vec, vlen, &pos);
  827. if (ret >= 0)
  828. file_pos_write(f.file, pos);
  829. fdput_pos(f);
  830. }
  831. if (ret > 0)
  832. add_rchar(current, ret);
  833. inc_syscr(current);
  834. return ret;
  835. }
  836. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  837. unsigned long, vlen)
  838. {
  839. struct fd f = fdget_pos(fd);
  840. ssize_t ret = -EBADF;
  841. if (f.file) {
  842. loff_t pos = file_pos_read(f.file);
  843. ret = vfs_writev(f.file, vec, vlen, &pos);
  844. if (ret >= 0)
  845. file_pos_write(f.file, pos);
  846. fdput_pos(f);
  847. }
  848. if (ret > 0)
  849. add_wchar(current, ret);
  850. inc_syscw(current);
  851. return ret;
  852. }
  853. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  854. {
  855. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  856. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  857. }
  858. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  859. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  860. {
  861. loff_t pos = pos_from_hilo(pos_h, pos_l);
  862. struct fd f;
  863. ssize_t ret = -EBADF;
  864. if (pos < 0)
  865. return -EINVAL;
  866. f = fdget(fd);
  867. if (f.file) {
  868. ret = -ESPIPE;
  869. if (f.file->f_mode & FMODE_PREAD)
  870. ret = vfs_readv(f.file, vec, vlen, &pos);
  871. fdput(f);
  872. }
  873. if (ret > 0)
  874. add_rchar(current, ret);
  875. inc_syscr(current);
  876. return ret;
  877. }
  878. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  879. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  880. {
  881. loff_t pos = pos_from_hilo(pos_h, pos_l);
  882. struct fd f;
  883. ssize_t ret = -EBADF;
  884. if (pos < 0)
  885. return -EINVAL;
  886. f = fdget(fd);
  887. if (f.file) {
  888. ret = -ESPIPE;
  889. if (f.file->f_mode & FMODE_PWRITE)
  890. ret = vfs_writev(f.file, vec, vlen, &pos);
  891. fdput(f);
  892. }
  893. if (ret > 0)
  894. add_wchar(current, ret);
  895. inc_syscw(current);
  896. return ret;
  897. }
  898. #ifdef CONFIG_COMPAT
  899. static ssize_t compat_do_readv_writev(int type, struct file *file,
  900. const struct compat_iovec __user *uvector,
  901. unsigned long nr_segs, loff_t *pos)
  902. {
  903. compat_ssize_t tot_len;
  904. struct iovec iovstack[UIO_FASTIOV];
  905. struct iovec *iov = iovstack;
  906. ssize_t ret;
  907. io_fn_t fn;
  908. iov_fn_t fnv;
  909. iter_fn_t iter_fn;
  910. ret = compat_rw_copy_check_uvector(type, uvector, nr_segs,
  911. UIO_FASTIOV, iovstack, &iov);
  912. if (ret <= 0)
  913. goto out;
  914. tot_len = ret;
  915. ret = rw_verify_area(type, file, pos, tot_len);
  916. if (ret < 0)
  917. goto out;
  918. fnv = NULL;
  919. if (type == READ) {
  920. fn = file->f_op->read;
  921. fnv = file->f_op->aio_read;
  922. iter_fn = file->f_op->read_iter;
  923. } else {
  924. fn = (io_fn_t)file->f_op->write;
  925. fnv = file->f_op->aio_write;
  926. iter_fn = file->f_op->write_iter;
  927. file_start_write(file);
  928. }
  929. if (iter_fn)
  930. ret = do_iter_readv_writev(file, type, iov, nr_segs, tot_len,
  931. pos, iter_fn);
  932. else if (fnv)
  933. ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
  934. pos, fnv);
  935. else
  936. ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
  937. if (type != READ)
  938. file_end_write(file);
  939. out:
  940. if (iov != iovstack)
  941. kfree(iov);
  942. if ((ret + (type == READ)) > 0) {
  943. if (type == READ)
  944. fsnotify_access(file);
  945. else
  946. fsnotify_modify(file);
  947. }
  948. return ret;
  949. }
  950. static size_t compat_readv(struct file *file,
  951. const struct compat_iovec __user *vec,
  952. unsigned long vlen, loff_t *pos)
  953. {
  954. ssize_t ret = -EBADF;
  955. if (!(file->f_mode & FMODE_READ))
  956. goto out;
  957. ret = -EINVAL;
  958. if (!(file->f_mode & FMODE_CAN_READ))
  959. goto out;
  960. ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
  961. out:
  962. if (ret > 0)
  963. add_rchar(current, ret);
  964. inc_syscr(current);
  965. return ret;
  966. }
  967. COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
  968. const struct compat_iovec __user *,vec,
  969. compat_ulong_t, vlen)
  970. {
  971. struct fd f = fdget_pos(fd);
  972. ssize_t ret;
  973. loff_t pos;
  974. if (!f.file)
  975. return -EBADF;
  976. pos = f.file->f_pos;
  977. ret = compat_readv(f.file, vec, vlen, &pos);
  978. if (ret >= 0)
  979. f.file->f_pos = pos;
  980. fdput_pos(f);
  981. return ret;
  982. }
  983. static long __compat_sys_preadv64(unsigned long fd,
  984. const struct compat_iovec __user *vec,
  985. unsigned long vlen, loff_t pos)
  986. {
  987. struct fd f;
  988. ssize_t ret;
  989. if (pos < 0)
  990. return -EINVAL;
  991. f = fdget(fd);
  992. if (!f.file)
  993. return -EBADF;
  994. ret = -ESPIPE;
  995. if (f.file->f_mode & FMODE_PREAD)
  996. ret = compat_readv(f.file, vec, vlen, &pos);
  997. fdput(f);
  998. return ret;
  999. }
  1000. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
  1001. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  1002. const struct compat_iovec __user *,vec,
  1003. unsigned long, vlen, loff_t, pos)
  1004. {
  1005. return __compat_sys_preadv64(fd, vec, vlen, pos);
  1006. }
  1007. #endif
  1008. COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
  1009. const struct compat_iovec __user *,vec,
  1010. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1011. {
  1012. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1013. return __compat_sys_preadv64(fd, vec, vlen, pos);
  1014. }
  1015. static size_t compat_writev(struct file *file,
  1016. const struct compat_iovec __user *vec,
  1017. unsigned long vlen, loff_t *pos)
  1018. {
  1019. ssize_t ret = -EBADF;
  1020. if (!(file->f_mode & FMODE_WRITE))
  1021. goto out;
  1022. ret = -EINVAL;
  1023. if (!(file->f_mode & FMODE_CAN_WRITE))
  1024. goto out;
  1025. ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
  1026. out:
  1027. if (ret > 0)
  1028. add_wchar(current, ret);
  1029. inc_syscw(current);
  1030. return ret;
  1031. }
  1032. COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
  1033. const struct compat_iovec __user *, vec,
  1034. compat_ulong_t, vlen)
  1035. {
  1036. struct fd f = fdget_pos(fd);
  1037. ssize_t ret;
  1038. loff_t pos;
  1039. if (!f.file)
  1040. return -EBADF;
  1041. pos = f.file->f_pos;
  1042. ret = compat_writev(f.file, vec, vlen, &pos);
  1043. if (ret >= 0)
  1044. f.file->f_pos = pos;
  1045. fdput_pos(f);
  1046. return ret;
  1047. }
  1048. static long __compat_sys_pwritev64(unsigned long fd,
  1049. const struct compat_iovec __user *vec,
  1050. unsigned long vlen, loff_t pos)
  1051. {
  1052. struct fd f;
  1053. ssize_t ret;
  1054. if (pos < 0)
  1055. return -EINVAL;
  1056. f = fdget(fd);
  1057. if (!f.file)
  1058. return -EBADF;
  1059. ret = -ESPIPE;
  1060. if (f.file->f_mode & FMODE_PWRITE)
  1061. ret = compat_writev(f.file, vec, vlen, &pos);
  1062. fdput(f);
  1063. return ret;
  1064. }
  1065. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
  1066. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  1067. const struct compat_iovec __user *,vec,
  1068. unsigned long, vlen, loff_t, pos)
  1069. {
  1070. return __compat_sys_pwritev64(fd, vec, vlen, pos);
  1071. }
  1072. #endif
  1073. COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
  1074. const struct compat_iovec __user *,vec,
  1075. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1076. {
  1077. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1078. return __compat_sys_pwritev64(fd, vec, vlen, pos);
  1079. }
  1080. #endif
  1081. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  1082. size_t count, loff_t max)
  1083. {
  1084. struct fd in, out;
  1085. struct inode *in_inode, *out_inode;
  1086. loff_t pos;
  1087. loff_t out_pos;
  1088. ssize_t retval;
  1089. int fl;
  1090. /*
  1091. * Get input file, and verify that it is ok..
  1092. */
  1093. retval = -EBADF;
  1094. in = fdget(in_fd);
  1095. if (!in.file)
  1096. goto out;
  1097. if (!(in.file->f_mode & FMODE_READ))
  1098. goto fput_in;
  1099. retval = -ESPIPE;
  1100. if (!ppos) {
  1101. pos = in.file->f_pos;
  1102. } else {
  1103. pos = *ppos;
  1104. if (!(in.file->f_mode & FMODE_PREAD))
  1105. goto fput_in;
  1106. }
  1107. retval = rw_verify_area(READ, in.file, &pos, count);
  1108. if (retval < 0)
  1109. goto fput_in;
  1110. count = retval;
  1111. /*
  1112. * Get output file, and verify that it is ok..
  1113. */
  1114. retval = -EBADF;
  1115. out = fdget(out_fd);
  1116. if (!out.file)
  1117. goto fput_in;
  1118. if (!(out.file->f_mode & FMODE_WRITE))
  1119. goto fput_out;
  1120. retval = -EINVAL;
  1121. in_inode = file_inode(in.file);
  1122. out_inode = file_inode(out.file);
  1123. out_pos = out.file->f_pos;
  1124. retval = rw_verify_area(WRITE, out.file, &out_pos, count);
  1125. if (retval < 0)
  1126. goto fput_out;
  1127. count = retval;
  1128. if (!max)
  1129. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  1130. if (unlikely(pos + count > max)) {
  1131. retval = -EOVERFLOW;
  1132. if (pos >= max)
  1133. goto fput_out;
  1134. count = max - pos;
  1135. }
  1136. fl = 0;
  1137. #if 0
  1138. /*
  1139. * We need to debate whether we can enable this or not. The
  1140. * man page documents EAGAIN return for the output at least,
  1141. * and the application is arguably buggy if it doesn't expect
  1142. * EAGAIN on a non-blocking file descriptor.
  1143. */
  1144. if (in.file->f_flags & O_NONBLOCK)
  1145. fl = SPLICE_F_NONBLOCK;
  1146. #endif
  1147. file_start_write(out.file);
  1148. retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
  1149. file_end_write(out.file);
  1150. if (retval > 0) {
  1151. add_rchar(current, retval);
  1152. add_wchar(current, retval);
  1153. fsnotify_access(in.file);
  1154. fsnotify_modify(out.file);
  1155. out.file->f_pos = out_pos;
  1156. if (ppos)
  1157. *ppos = pos;
  1158. else
  1159. in.file->f_pos = pos;
  1160. }
  1161. inc_syscr(current);
  1162. inc_syscw(current);
  1163. if (pos > max)
  1164. retval = -EOVERFLOW;
  1165. fput_out:
  1166. fdput(out);
  1167. fput_in:
  1168. fdput(in);
  1169. out:
  1170. return retval;
  1171. }
  1172. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  1173. {
  1174. loff_t pos;
  1175. off_t off;
  1176. ssize_t ret;
  1177. if (offset) {
  1178. if (unlikely(get_user(off, offset)))
  1179. return -EFAULT;
  1180. pos = off;
  1181. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1182. if (unlikely(put_user(pos, offset)))
  1183. return -EFAULT;
  1184. return ret;
  1185. }
  1186. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1187. }
  1188. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  1189. {
  1190. loff_t pos;
  1191. ssize_t ret;
  1192. if (offset) {
  1193. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1194. return -EFAULT;
  1195. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1196. if (unlikely(put_user(pos, offset)))
  1197. return -EFAULT;
  1198. return ret;
  1199. }
  1200. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1201. }
  1202. #ifdef CONFIG_COMPAT
  1203. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  1204. compat_off_t __user *, offset, compat_size_t, count)
  1205. {
  1206. loff_t pos;
  1207. off_t off;
  1208. ssize_t ret;
  1209. if (offset) {
  1210. if (unlikely(get_user(off, offset)))
  1211. return -EFAULT;
  1212. pos = off;
  1213. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1214. if (unlikely(put_user(pos, offset)))
  1215. return -EFAULT;
  1216. return ret;
  1217. }
  1218. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1219. }
  1220. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  1221. compat_loff_t __user *, offset, compat_size_t, count)
  1222. {
  1223. loff_t pos;
  1224. ssize_t ret;
  1225. if (offset) {
  1226. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1227. return -EFAULT;
  1228. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1229. if (unlikely(put_user(pos, offset)))
  1230. return -EFAULT;
  1231. return ret;
  1232. }
  1233. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1234. }
  1235. #endif