read_write.c 39 KB

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