read_write.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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 ((unsigned long long)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 ((unsigned long long)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. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  284. {
  285. off_t retval;
  286. struct fd f = fdget_pos(fd);
  287. if (!f.file)
  288. return -EBADF;
  289. retval = -EINVAL;
  290. if (whence <= SEEK_MAX) {
  291. loff_t res = vfs_llseek(f.file, offset, whence);
  292. retval = res;
  293. if (res != (loff_t)retval)
  294. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  295. }
  296. fdput_pos(f);
  297. return retval;
  298. }
  299. #ifdef CONFIG_COMPAT
  300. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  301. {
  302. return sys_lseek(fd, offset, whence);
  303. }
  304. #endif
  305. #ifdef __ARCH_WANT_SYS_LLSEEK
  306. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  307. unsigned long, offset_low, loff_t __user *, result,
  308. unsigned int, whence)
  309. {
  310. int retval;
  311. struct fd f = fdget_pos(fd);
  312. loff_t offset;
  313. if (!f.file)
  314. return -EBADF;
  315. retval = -EINVAL;
  316. if (whence > SEEK_MAX)
  317. goto out_putf;
  318. offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
  319. whence);
  320. retval = (int)offset;
  321. if (offset >= 0) {
  322. retval = -EFAULT;
  323. if (!copy_to_user(result, &offset, sizeof(offset)))
  324. retval = 0;
  325. }
  326. out_putf:
  327. fdput_pos(f);
  328. return retval;
  329. }
  330. #endif
  331. ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
  332. {
  333. struct kiocb kiocb;
  334. ssize_t ret;
  335. if (!file->f_op->read_iter)
  336. return -EINVAL;
  337. init_sync_kiocb(&kiocb, file);
  338. kiocb.ki_pos = *ppos;
  339. iter->type |= READ;
  340. ret = file->f_op->read_iter(&kiocb, iter);
  341. BUG_ON(ret == -EIOCBQUEUED);
  342. if (ret > 0)
  343. *ppos = kiocb.ki_pos;
  344. return ret;
  345. }
  346. EXPORT_SYMBOL(vfs_iter_read);
  347. ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
  348. {
  349. struct kiocb kiocb;
  350. ssize_t ret;
  351. if (!file->f_op->write_iter)
  352. return -EINVAL;
  353. init_sync_kiocb(&kiocb, file);
  354. kiocb.ki_pos = *ppos;
  355. iter->type |= WRITE;
  356. ret = file->f_op->write_iter(&kiocb, iter);
  357. BUG_ON(ret == -EIOCBQUEUED);
  358. if (ret > 0) {
  359. *ppos = kiocb.ki_pos;
  360. fsnotify_modify(file);
  361. }
  362. return ret;
  363. }
  364. EXPORT_SYMBOL(vfs_iter_write);
  365. int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
  366. {
  367. struct inode *inode;
  368. loff_t pos;
  369. int retval = -EINVAL;
  370. inode = file_inode(file);
  371. if (unlikely((ssize_t) count < 0))
  372. return retval;
  373. pos = *ppos;
  374. if (unlikely(pos < 0)) {
  375. if (!unsigned_offsets(file))
  376. return retval;
  377. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  378. return -EOVERFLOW;
  379. } else if (unlikely((loff_t) (pos + count) < 0)) {
  380. if (!unsigned_offsets(file))
  381. return retval;
  382. }
  383. if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
  384. retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
  385. read_write == READ ? F_RDLCK : F_WRLCK);
  386. if (retval < 0)
  387. return retval;
  388. }
  389. return security_file_permission(file,
  390. read_write == READ ? MAY_READ : MAY_WRITE);
  391. }
  392. static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  393. {
  394. struct iovec iov = { .iov_base = buf, .iov_len = len };
  395. struct kiocb kiocb;
  396. struct iov_iter iter;
  397. ssize_t ret;
  398. init_sync_kiocb(&kiocb, filp);
  399. kiocb.ki_pos = *ppos;
  400. iov_iter_init(&iter, READ, &iov, 1, len);
  401. ret = filp->f_op->read_iter(&kiocb, &iter);
  402. BUG_ON(ret == -EIOCBQUEUED);
  403. *ppos = kiocb.ki_pos;
  404. return ret;
  405. }
  406. ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
  407. loff_t *pos)
  408. {
  409. if (file->f_op->read)
  410. return file->f_op->read(file, buf, count, pos);
  411. else if (file->f_op->read_iter)
  412. return new_sync_read(file, buf, count, pos);
  413. else
  414. return -EINVAL;
  415. }
  416. EXPORT_SYMBOL(__vfs_read);
  417. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  418. {
  419. ssize_t ret;
  420. if (!(file->f_mode & FMODE_READ))
  421. return -EBADF;
  422. if (!(file->f_mode & FMODE_CAN_READ))
  423. return -EINVAL;
  424. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  425. return -EFAULT;
  426. ret = rw_verify_area(READ, file, pos, count);
  427. if (!ret) {
  428. if (count > MAX_RW_COUNT)
  429. count = MAX_RW_COUNT;
  430. ret = __vfs_read(file, buf, count, pos);
  431. if (ret > 0) {
  432. fsnotify_access(file);
  433. add_rchar(current, ret);
  434. }
  435. inc_syscr(current);
  436. }
  437. return ret;
  438. }
  439. EXPORT_SYMBOL(vfs_read);
  440. static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  441. {
  442. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  443. struct kiocb kiocb;
  444. struct iov_iter iter;
  445. ssize_t ret;
  446. init_sync_kiocb(&kiocb, filp);
  447. kiocb.ki_pos = *ppos;
  448. iov_iter_init(&iter, WRITE, &iov, 1, len);
  449. ret = filp->f_op->write_iter(&kiocb, &iter);
  450. BUG_ON(ret == -EIOCBQUEUED);
  451. if (ret > 0)
  452. *ppos = kiocb.ki_pos;
  453. return ret;
  454. }
  455. ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
  456. loff_t *pos)
  457. {
  458. if (file->f_op->write)
  459. return file->f_op->write(file, p, count, pos);
  460. else if (file->f_op->write_iter)
  461. return new_sync_write(file, p, count, pos);
  462. else
  463. return -EINVAL;
  464. }
  465. EXPORT_SYMBOL(__vfs_write);
  466. ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
  467. {
  468. mm_segment_t old_fs;
  469. const char __user *p;
  470. ssize_t ret;
  471. if (!(file->f_mode & FMODE_CAN_WRITE))
  472. return -EINVAL;
  473. old_fs = get_fs();
  474. set_fs(get_ds());
  475. p = (__force const char __user *)buf;
  476. if (count > MAX_RW_COUNT)
  477. count = MAX_RW_COUNT;
  478. ret = __vfs_write(file, p, count, pos);
  479. set_fs(old_fs);
  480. if (ret > 0) {
  481. fsnotify_modify(file);
  482. add_wchar(current, ret);
  483. }
  484. inc_syscw(current);
  485. return ret;
  486. }
  487. EXPORT_SYMBOL(__kernel_write);
  488. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  489. {
  490. ssize_t ret;
  491. if (!(file->f_mode & FMODE_WRITE))
  492. return -EBADF;
  493. if (!(file->f_mode & FMODE_CAN_WRITE))
  494. return -EINVAL;
  495. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  496. return -EFAULT;
  497. ret = rw_verify_area(WRITE, file, pos, count);
  498. if (!ret) {
  499. if (count > MAX_RW_COUNT)
  500. count = MAX_RW_COUNT;
  501. file_start_write(file);
  502. ret = __vfs_write(file, buf, count, pos);
  503. if (ret > 0) {
  504. fsnotify_modify(file);
  505. add_wchar(current, ret);
  506. }
  507. inc_syscw(current);
  508. file_end_write(file);
  509. }
  510. return ret;
  511. }
  512. EXPORT_SYMBOL(vfs_write);
  513. static inline loff_t file_pos_read(struct file *file)
  514. {
  515. return file->f_pos;
  516. }
  517. static inline void file_pos_write(struct file *file, loff_t pos)
  518. {
  519. file->f_pos = pos;
  520. }
  521. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  522. {
  523. struct fd f = fdget_pos(fd);
  524. ssize_t ret = -EBADF;
  525. if (f.file) {
  526. loff_t pos = file_pos_read(f.file);
  527. ret = vfs_read(f.file, buf, count, &pos);
  528. if (ret >= 0)
  529. file_pos_write(f.file, pos);
  530. fdput_pos(f);
  531. }
  532. return ret;
  533. }
  534. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  535. 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_write(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_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  549. size_t, count, loff_t, pos)
  550. {
  551. struct fd f;
  552. ssize_t ret = -EBADF;
  553. if (pos < 0)
  554. return -EINVAL;
  555. f = fdget(fd);
  556. if (f.file) {
  557. ret = -ESPIPE;
  558. if (f.file->f_mode & FMODE_PREAD)
  559. ret = vfs_read(f.file, buf, count, &pos);
  560. fdput(f);
  561. }
  562. return ret;
  563. }
  564. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  565. size_t, count, loff_t, pos)
  566. {
  567. struct fd f;
  568. ssize_t ret = -EBADF;
  569. if (pos < 0)
  570. return -EINVAL;
  571. f = fdget(fd);
  572. if (f.file) {
  573. ret = -ESPIPE;
  574. if (f.file->f_mode & FMODE_PWRITE)
  575. ret = vfs_write(f.file, buf, count, &pos);
  576. fdput(f);
  577. }
  578. return ret;
  579. }
  580. /*
  581. * Reduce an iovec's length in-place. Return the resulting number of segments
  582. */
  583. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  584. {
  585. unsigned long seg = 0;
  586. size_t len = 0;
  587. while (seg < nr_segs) {
  588. seg++;
  589. if (len + iov->iov_len >= to) {
  590. iov->iov_len = to - len;
  591. break;
  592. }
  593. len += iov->iov_len;
  594. iov++;
  595. }
  596. return seg;
  597. }
  598. EXPORT_SYMBOL(iov_shorten);
  599. static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
  600. loff_t *ppos, iter_fn_t fn, int flags)
  601. {
  602. struct kiocb kiocb;
  603. ssize_t ret;
  604. if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC))
  605. return -EOPNOTSUPP;
  606. init_sync_kiocb(&kiocb, filp);
  607. if (flags & RWF_HIPRI)
  608. kiocb.ki_flags |= IOCB_HIPRI;
  609. if (flags & RWF_DSYNC)
  610. kiocb.ki_flags |= IOCB_DSYNC;
  611. if (flags & RWF_SYNC)
  612. kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC);
  613. kiocb.ki_pos = *ppos;
  614. ret = fn(&kiocb, iter);
  615. BUG_ON(ret == -EIOCBQUEUED);
  616. *ppos = kiocb.ki_pos;
  617. return ret;
  618. }
  619. /* Do it by hand, with file-ops */
  620. static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
  621. loff_t *ppos, io_fn_t fn, int flags)
  622. {
  623. ssize_t ret = 0;
  624. if (flags & ~RWF_HIPRI)
  625. return -EOPNOTSUPP;
  626. while (iov_iter_count(iter)) {
  627. struct iovec iovec = iov_iter_iovec(iter);
  628. ssize_t nr;
  629. nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
  630. if (nr < 0) {
  631. if (!ret)
  632. ret = nr;
  633. break;
  634. }
  635. ret += nr;
  636. if (nr != iovec.iov_len)
  637. break;
  638. iov_iter_advance(iter, nr);
  639. }
  640. return ret;
  641. }
  642. /* A write operation does a read from user space and vice versa */
  643. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  644. /**
  645. * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
  646. * into the kernel and check that it is valid.
  647. *
  648. * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
  649. * @uvector: Pointer to the userspace array.
  650. * @nr_segs: Number of elements in userspace array.
  651. * @fast_segs: Number of elements in @fast_pointer.
  652. * @fast_pointer: Pointer to (usually small on-stack) kernel array.
  653. * @ret_pointer: (output parameter) Pointer to a variable that will point to
  654. * either @fast_pointer, a newly allocated kernel array, or NULL,
  655. * depending on which array was used.
  656. *
  657. * This function copies an array of &struct iovec of @nr_segs from
  658. * userspace into the kernel and checks that each element is valid (e.g.
  659. * it does not point to a kernel address or cause overflow by being too
  660. * large, etc.).
  661. *
  662. * As an optimization, the caller may provide a pointer to a small
  663. * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
  664. * (the size of this array, or 0 if unused, should be given in @fast_segs).
  665. *
  666. * @ret_pointer will always point to the array that was used, so the
  667. * caller must take care not to call kfree() on it e.g. in case the
  668. * @fast_pointer array was used and it was allocated on the stack.
  669. *
  670. * Return: The total number of bytes covered by the iovec array on success
  671. * or a negative error code on error.
  672. */
  673. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  674. unsigned long nr_segs, unsigned long fast_segs,
  675. struct iovec *fast_pointer,
  676. struct iovec **ret_pointer)
  677. {
  678. unsigned long seg;
  679. ssize_t ret;
  680. struct iovec *iov = fast_pointer;
  681. /*
  682. * SuS says "The readv() function *may* fail if the iovcnt argument
  683. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  684. * traditionally returned zero for zero segments, so...
  685. */
  686. if (nr_segs == 0) {
  687. ret = 0;
  688. goto out;
  689. }
  690. /*
  691. * First get the "struct iovec" from user memory and
  692. * verify all the pointers
  693. */
  694. if (nr_segs > UIO_MAXIOV) {
  695. ret = -EINVAL;
  696. goto out;
  697. }
  698. if (nr_segs > fast_segs) {
  699. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  700. if (iov == NULL) {
  701. ret = -ENOMEM;
  702. goto out;
  703. }
  704. }
  705. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  706. ret = -EFAULT;
  707. goto out;
  708. }
  709. /*
  710. * According to the Single Unix Specification we should return EINVAL
  711. * if an element length is < 0 when cast to ssize_t or if the
  712. * total length would overflow the ssize_t return value of the
  713. * system call.
  714. *
  715. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  716. * overflow case.
  717. */
  718. ret = 0;
  719. for (seg = 0; seg < nr_segs; seg++) {
  720. void __user *buf = iov[seg].iov_base;
  721. ssize_t len = (ssize_t)iov[seg].iov_len;
  722. /* see if we we're about to use an invalid len or if
  723. * it's about to overflow ssize_t */
  724. if (len < 0) {
  725. ret = -EINVAL;
  726. goto out;
  727. }
  728. if (type >= 0
  729. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  730. ret = -EFAULT;
  731. goto out;
  732. }
  733. if (len > MAX_RW_COUNT - ret) {
  734. len = MAX_RW_COUNT - ret;
  735. iov[seg].iov_len = len;
  736. }
  737. ret += len;
  738. }
  739. out:
  740. *ret_pointer = iov;
  741. return ret;
  742. }
  743. static ssize_t do_readv_writev(int type, struct file *file,
  744. const struct iovec __user * uvector,
  745. unsigned long nr_segs, loff_t *pos,
  746. int flags)
  747. {
  748. size_t tot_len;
  749. struct iovec iovstack[UIO_FASTIOV];
  750. struct iovec *iov = iovstack;
  751. struct iov_iter iter;
  752. ssize_t ret;
  753. io_fn_t fn;
  754. iter_fn_t iter_fn;
  755. ret = import_iovec(type, uvector, nr_segs,
  756. ARRAY_SIZE(iovstack), &iov, &iter);
  757. if (ret < 0)
  758. return ret;
  759. tot_len = iov_iter_count(&iter);
  760. if (!tot_len)
  761. goto out;
  762. ret = rw_verify_area(type, file, pos, tot_len);
  763. if (ret < 0)
  764. goto out;
  765. if (type == READ) {
  766. fn = file->f_op->read;
  767. iter_fn = file->f_op->read_iter;
  768. } else {
  769. fn = (io_fn_t)file->f_op->write;
  770. iter_fn = file->f_op->write_iter;
  771. file_start_write(file);
  772. }
  773. if (iter_fn)
  774. ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
  775. else
  776. ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
  777. if (type != READ)
  778. file_end_write(file);
  779. out:
  780. kfree(iov);
  781. if ((ret + (type == READ)) > 0) {
  782. if (type == READ)
  783. fsnotify_access(file);
  784. else
  785. fsnotify_modify(file);
  786. }
  787. return ret;
  788. }
  789. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  790. unsigned long vlen, loff_t *pos, int flags)
  791. {
  792. if (!(file->f_mode & FMODE_READ))
  793. return -EBADF;
  794. if (!(file->f_mode & FMODE_CAN_READ))
  795. return -EINVAL;
  796. return do_readv_writev(READ, file, vec, vlen, pos, flags);
  797. }
  798. EXPORT_SYMBOL(vfs_readv);
  799. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  800. unsigned long vlen, loff_t *pos, int flags)
  801. {
  802. if (!(file->f_mode & FMODE_WRITE))
  803. return -EBADF;
  804. if (!(file->f_mode & FMODE_CAN_WRITE))
  805. return -EINVAL;
  806. return do_readv_writev(WRITE, file, vec, vlen, pos, flags);
  807. }
  808. EXPORT_SYMBOL(vfs_writev);
  809. static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
  810. unsigned long vlen, int flags)
  811. {
  812. struct fd f = fdget_pos(fd);
  813. ssize_t ret = -EBADF;
  814. if (f.file) {
  815. loff_t pos = file_pos_read(f.file);
  816. ret = vfs_readv(f.file, vec, vlen, &pos, flags);
  817. if (ret >= 0)
  818. file_pos_write(f.file, pos);
  819. fdput_pos(f);
  820. }
  821. if (ret > 0)
  822. add_rchar(current, ret);
  823. inc_syscr(current);
  824. return ret;
  825. }
  826. static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
  827. unsigned long vlen, int flags)
  828. {
  829. struct fd f = fdget_pos(fd);
  830. ssize_t ret = -EBADF;
  831. if (f.file) {
  832. loff_t pos = file_pos_read(f.file);
  833. ret = vfs_writev(f.file, vec, vlen, &pos, flags);
  834. if (ret >= 0)
  835. file_pos_write(f.file, pos);
  836. fdput_pos(f);
  837. }
  838. if (ret > 0)
  839. add_wchar(current, ret);
  840. inc_syscw(current);
  841. return ret;
  842. }
  843. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  844. {
  845. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  846. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  847. }
  848. static ssize_t do_preadv(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_PREAD)
  859. ret = vfs_readv(f.file, vec, vlen, &pos, flags);
  860. fdput(f);
  861. }
  862. if (ret > 0)
  863. add_rchar(current, ret);
  864. inc_syscr(current);
  865. return ret;
  866. }
  867. static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
  868. unsigned long vlen, loff_t pos, int flags)
  869. {
  870. struct fd f;
  871. ssize_t ret = -EBADF;
  872. if (pos < 0)
  873. return -EINVAL;
  874. f = fdget(fd);
  875. if (f.file) {
  876. ret = -ESPIPE;
  877. if (f.file->f_mode & FMODE_PWRITE)
  878. ret = vfs_writev(f.file, vec, vlen, &pos, flags);
  879. fdput(f);
  880. }
  881. if (ret > 0)
  882. add_wchar(current, ret);
  883. inc_syscw(current);
  884. return ret;
  885. }
  886. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  887. unsigned long, vlen)
  888. {
  889. return do_readv(fd, vec, vlen, 0);
  890. }
  891. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  892. unsigned long, vlen)
  893. {
  894. return do_writev(fd, vec, vlen, 0);
  895. }
  896. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  897. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  898. {
  899. loff_t pos = pos_from_hilo(pos_h, pos_l);
  900. return do_preadv(fd, vec, vlen, pos, 0);
  901. }
  902. SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
  903. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  904. int, flags)
  905. {
  906. loff_t pos = pos_from_hilo(pos_h, pos_l);
  907. if (pos == -1)
  908. return do_readv(fd, vec, vlen, flags);
  909. return do_preadv(fd, vec, vlen, pos, flags);
  910. }
  911. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  912. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  913. {
  914. loff_t pos = pos_from_hilo(pos_h, pos_l);
  915. return do_pwritev(fd, vec, vlen, pos, 0);
  916. }
  917. SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
  918. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  919. int, flags)
  920. {
  921. loff_t pos = pos_from_hilo(pos_h, pos_l);
  922. if (pos == -1)
  923. return do_writev(fd, vec, vlen, flags);
  924. return do_pwritev(fd, vec, vlen, pos, flags);
  925. }
  926. #ifdef CONFIG_COMPAT
  927. static ssize_t compat_do_readv_writev(int type, struct file *file,
  928. const struct compat_iovec __user *uvector,
  929. unsigned long nr_segs, loff_t *pos,
  930. int flags)
  931. {
  932. compat_ssize_t tot_len;
  933. struct iovec iovstack[UIO_FASTIOV];
  934. struct iovec *iov = iovstack;
  935. struct iov_iter iter;
  936. ssize_t ret;
  937. io_fn_t fn;
  938. iter_fn_t iter_fn;
  939. ret = compat_import_iovec(type, uvector, nr_segs,
  940. UIO_FASTIOV, &iov, &iter);
  941. if (ret < 0)
  942. return ret;
  943. tot_len = iov_iter_count(&iter);
  944. if (!tot_len)
  945. goto out;
  946. ret = rw_verify_area(type, file, pos, tot_len);
  947. if (ret < 0)
  948. goto out;
  949. if (type == READ) {
  950. fn = file->f_op->read;
  951. iter_fn = file->f_op->read_iter;
  952. } else {
  953. fn = (io_fn_t)file->f_op->write;
  954. iter_fn = file->f_op->write_iter;
  955. file_start_write(file);
  956. }
  957. if (iter_fn)
  958. ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
  959. else
  960. ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
  961. if (type != READ)
  962. file_end_write(file);
  963. out:
  964. kfree(iov);
  965. if ((ret + (type == READ)) > 0) {
  966. if (type == READ)
  967. fsnotify_access(file);
  968. else
  969. fsnotify_modify(file);
  970. }
  971. return ret;
  972. }
  973. static size_t compat_readv(struct file *file,
  974. const struct compat_iovec __user *vec,
  975. unsigned long vlen, loff_t *pos, int flags)
  976. {
  977. ssize_t ret = -EBADF;
  978. if (!(file->f_mode & FMODE_READ))
  979. goto out;
  980. ret = -EINVAL;
  981. if (!(file->f_mode & FMODE_CAN_READ))
  982. goto out;
  983. ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags);
  984. out:
  985. if (ret > 0)
  986. add_rchar(current, ret);
  987. inc_syscr(current);
  988. return ret;
  989. }
  990. static size_t do_compat_readv(compat_ulong_t fd,
  991. const struct compat_iovec __user *vec,
  992. compat_ulong_t vlen, int flags)
  993. {
  994. struct fd f = fdget_pos(fd);
  995. ssize_t ret;
  996. loff_t pos;
  997. if (!f.file)
  998. return -EBADF;
  999. pos = f.file->f_pos;
  1000. ret = compat_readv(f.file, vec, vlen, &pos, flags);
  1001. if (ret >= 0)
  1002. f.file->f_pos = pos;
  1003. fdput_pos(f);
  1004. return ret;
  1005. }
  1006. COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
  1007. const struct compat_iovec __user *,vec,
  1008. compat_ulong_t, vlen)
  1009. {
  1010. return do_compat_readv(fd, vec, vlen, 0);
  1011. }
  1012. static long do_compat_preadv64(unsigned long fd,
  1013. const struct compat_iovec __user *vec,
  1014. unsigned long vlen, loff_t pos, int flags)
  1015. {
  1016. struct fd f;
  1017. ssize_t ret;
  1018. if (pos < 0)
  1019. return -EINVAL;
  1020. f = fdget(fd);
  1021. if (!f.file)
  1022. return -EBADF;
  1023. ret = -ESPIPE;
  1024. if (f.file->f_mode & FMODE_PREAD)
  1025. ret = compat_readv(f.file, vec, vlen, &pos, flags);
  1026. fdput(f);
  1027. return ret;
  1028. }
  1029. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
  1030. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  1031. const struct compat_iovec __user *,vec,
  1032. unsigned long, vlen, loff_t, pos)
  1033. {
  1034. return do_compat_preadv64(fd, vec, vlen, pos, 0);
  1035. }
  1036. #endif
  1037. COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
  1038. const struct compat_iovec __user *,vec,
  1039. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1040. {
  1041. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1042. return do_compat_preadv64(fd, vec, vlen, pos, 0);
  1043. }
  1044. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
  1045. COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
  1046. const struct compat_iovec __user *,vec,
  1047. unsigned long, vlen, loff_t, pos, int, flags)
  1048. {
  1049. if (pos == -1)
  1050. return do_compat_readv(fd, vec, vlen, flags);
  1051. return do_compat_preadv64(fd, vec, vlen, pos, flags);
  1052. }
  1053. #endif
  1054. COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
  1055. const struct compat_iovec __user *,vec,
  1056. compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
  1057. int, flags)
  1058. {
  1059. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1060. if (pos == -1)
  1061. return do_compat_readv(fd, vec, vlen, flags);
  1062. return do_compat_preadv64(fd, vec, vlen, pos, flags);
  1063. }
  1064. static size_t compat_writev(struct file *file,
  1065. const struct compat_iovec __user *vec,
  1066. unsigned long vlen, loff_t *pos, int flags)
  1067. {
  1068. ssize_t ret = -EBADF;
  1069. if (!(file->f_mode & FMODE_WRITE))
  1070. goto out;
  1071. ret = -EINVAL;
  1072. if (!(file->f_mode & FMODE_CAN_WRITE))
  1073. goto out;
  1074. ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, flags);
  1075. out:
  1076. if (ret > 0)
  1077. add_wchar(current, ret);
  1078. inc_syscw(current);
  1079. return ret;
  1080. }
  1081. static size_t do_compat_writev(compat_ulong_t fd,
  1082. const struct compat_iovec __user* vec,
  1083. compat_ulong_t vlen, int flags)
  1084. {
  1085. struct fd f = fdget_pos(fd);
  1086. ssize_t ret;
  1087. loff_t pos;
  1088. if (!f.file)
  1089. return -EBADF;
  1090. pos = f.file->f_pos;
  1091. ret = compat_writev(f.file, vec, vlen, &pos, flags);
  1092. if (ret >= 0)
  1093. f.file->f_pos = pos;
  1094. fdput_pos(f);
  1095. return ret;
  1096. }
  1097. COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
  1098. const struct compat_iovec __user *, vec,
  1099. compat_ulong_t, vlen)
  1100. {
  1101. return do_compat_writev(fd, vec, vlen, 0);
  1102. }
  1103. static long do_compat_pwritev64(unsigned long fd,
  1104. const struct compat_iovec __user *vec,
  1105. unsigned long vlen, loff_t pos, int flags)
  1106. {
  1107. struct fd f;
  1108. ssize_t ret;
  1109. if (pos < 0)
  1110. return -EINVAL;
  1111. f = fdget(fd);
  1112. if (!f.file)
  1113. return -EBADF;
  1114. ret = -ESPIPE;
  1115. if (f.file->f_mode & FMODE_PWRITE)
  1116. ret = compat_writev(f.file, vec, vlen, &pos, flags);
  1117. fdput(f);
  1118. return ret;
  1119. }
  1120. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
  1121. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  1122. const struct compat_iovec __user *,vec,
  1123. unsigned long, vlen, loff_t, pos)
  1124. {
  1125. return do_compat_pwritev64(fd, vec, vlen, pos, 0);
  1126. }
  1127. #endif
  1128. COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
  1129. const struct compat_iovec __user *,vec,
  1130. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1131. {
  1132. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1133. return do_compat_pwritev64(fd, vec, vlen, pos, 0);
  1134. }
  1135. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
  1136. COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
  1137. const struct compat_iovec __user *,vec,
  1138. unsigned long, vlen, loff_t, pos, int, flags)
  1139. {
  1140. if (pos == -1)
  1141. return do_compat_writev(fd, vec, vlen, flags);
  1142. return do_compat_pwritev64(fd, vec, vlen, pos, flags);
  1143. }
  1144. #endif
  1145. COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
  1146. const struct compat_iovec __user *,vec,
  1147. compat_ulong_t, vlen, u32, pos_low, u32, pos_high, int, flags)
  1148. {
  1149. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1150. if (pos == -1)
  1151. return do_compat_writev(fd, vec, vlen, flags);
  1152. return do_compat_pwritev64(fd, vec, vlen, pos, flags);
  1153. }
  1154. #endif
  1155. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  1156. size_t count, loff_t max)
  1157. {
  1158. struct fd in, out;
  1159. struct inode *in_inode, *out_inode;
  1160. loff_t pos;
  1161. loff_t out_pos;
  1162. ssize_t retval;
  1163. int fl;
  1164. /*
  1165. * Get input file, and verify that it is ok..
  1166. */
  1167. retval = -EBADF;
  1168. in = fdget(in_fd);
  1169. if (!in.file)
  1170. goto out;
  1171. if (!(in.file->f_mode & FMODE_READ))
  1172. goto fput_in;
  1173. retval = -ESPIPE;
  1174. if (!ppos) {
  1175. pos = in.file->f_pos;
  1176. } else {
  1177. pos = *ppos;
  1178. if (!(in.file->f_mode & FMODE_PREAD))
  1179. goto fput_in;
  1180. }
  1181. retval = rw_verify_area(READ, in.file, &pos, count);
  1182. if (retval < 0)
  1183. goto fput_in;
  1184. if (count > MAX_RW_COUNT)
  1185. count = MAX_RW_COUNT;
  1186. /*
  1187. * Get output file, and verify that it is ok..
  1188. */
  1189. retval = -EBADF;
  1190. out = fdget(out_fd);
  1191. if (!out.file)
  1192. goto fput_in;
  1193. if (!(out.file->f_mode & FMODE_WRITE))
  1194. goto fput_out;
  1195. retval = -EINVAL;
  1196. in_inode = file_inode(in.file);
  1197. out_inode = file_inode(out.file);
  1198. out_pos = out.file->f_pos;
  1199. retval = rw_verify_area(WRITE, out.file, &out_pos, count);
  1200. if (retval < 0)
  1201. goto fput_out;
  1202. if (!max)
  1203. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  1204. if (unlikely(pos + count > max)) {
  1205. retval = -EOVERFLOW;
  1206. if (pos >= max)
  1207. goto fput_out;
  1208. count = max - pos;
  1209. }
  1210. fl = 0;
  1211. #if 0
  1212. /*
  1213. * We need to debate whether we can enable this or not. The
  1214. * man page documents EAGAIN return for the output at least,
  1215. * and the application is arguably buggy if it doesn't expect
  1216. * EAGAIN on a non-blocking file descriptor.
  1217. */
  1218. if (in.file->f_flags & O_NONBLOCK)
  1219. fl = SPLICE_F_NONBLOCK;
  1220. #endif
  1221. file_start_write(out.file);
  1222. retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
  1223. file_end_write(out.file);
  1224. if (retval > 0) {
  1225. add_rchar(current, retval);
  1226. add_wchar(current, retval);
  1227. fsnotify_access(in.file);
  1228. fsnotify_modify(out.file);
  1229. out.file->f_pos = out_pos;
  1230. if (ppos)
  1231. *ppos = pos;
  1232. else
  1233. in.file->f_pos = pos;
  1234. }
  1235. inc_syscr(current);
  1236. inc_syscw(current);
  1237. if (pos > max)
  1238. retval = -EOVERFLOW;
  1239. fput_out:
  1240. fdput(out);
  1241. fput_in:
  1242. fdput(in);
  1243. out:
  1244. return retval;
  1245. }
  1246. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  1247. {
  1248. loff_t pos;
  1249. off_t off;
  1250. ssize_t ret;
  1251. if (offset) {
  1252. if (unlikely(get_user(off, offset)))
  1253. return -EFAULT;
  1254. pos = off;
  1255. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1256. if (unlikely(put_user(pos, offset)))
  1257. return -EFAULT;
  1258. return ret;
  1259. }
  1260. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1261. }
  1262. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  1263. {
  1264. loff_t pos;
  1265. ssize_t ret;
  1266. if (offset) {
  1267. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1268. return -EFAULT;
  1269. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1270. if (unlikely(put_user(pos, offset)))
  1271. return -EFAULT;
  1272. return ret;
  1273. }
  1274. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1275. }
  1276. #ifdef CONFIG_COMPAT
  1277. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  1278. compat_off_t __user *, offset, compat_size_t, count)
  1279. {
  1280. loff_t pos;
  1281. off_t off;
  1282. ssize_t ret;
  1283. if (offset) {
  1284. if (unlikely(get_user(off, offset)))
  1285. return -EFAULT;
  1286. pos = off;
  1287. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1288. if (unlikely(put_user(pos, offset)))
  1289. return -EFAULT;
  1290. return ret;
  1291. }
  1292. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1293. }
  1294. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  1295. compat_loff_t __user *, offset, compat_size_t, count)
  1296. {
  1297. loff_t pos;
  1298. ssize_t ret;
  1299. if (offset) {
  1300. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1301. return -EFAULT;
  1302. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1303. if (unlikely(put_user(pos, offset)))
  1304. return -EFAULT;
  1305. return ret;
  1306. }
  1307. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1308. }
  1309. #endif
  1310. /*
  1311. * copy_file_range() differs from regular file read and write in that it
  1312. * specifically allows return partial success. When it does so is up to
  1313. * the copy_file_range method.
  1314. */
  1315. ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
  1316. struct file *file_out, loff_t pos_out,
  1317. size_t len, unsigned int flags)
  1318. {
  1319. struct inode *inode_in = file_inode(file_in);
  1320. struct inode *inode_out = file_inode(file_out);
  1321. ssize_t ret;
  1322. if (flags != 0)
  1323. return -EINVAL;
  1324. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1325. return -EISDIR;
  1326. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1327. return -EINVAL;
  1328. ret = rw_verify_area(READ, file_in, &pos_in, len);
  1329. if (unlikely(ret))
  1330. return ret;
  1331. ret = rw_verify_area(WRITE, file_out, &pos_out, len);
  1332. if (unlikely(ret))
  1333. return ret;
  1334. if (!(file_in->f_mode & FMODE_READ) ||
  1335. !(file_out->f_mode & FMODE_WRITE) ||
  1336. (file_out->f_flags & O_APPEND))
  1337. return -EBADF;
  1338. /* this could be relaxed once a method supports cross-fs copies */
  1339. if (inode_in->i_sb != inode_out->i_sb)
  1340. return -EXDEV;
  1341. if (len == 0)
  1342. return 0;
  1343. ret = mnt_want_write_file(file_out);
  1344. if (ret)
  1345. return ret;
  1346. ret = -EOPNOTSUPP;
  1347. if (file_out->f_op->copy_file_range)
  1348. ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
  1349. pos_out, len, flags);
  1350. if (ret == -EOPNOTSUPP)
  1351. ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
  1352. len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
  1353. if (ret > 0) {
  1354. fsnotify_access(file_in);
  1355. add_rchar(current, ret);
  1356. fsnotify_modify(file_out);
  1357. add_wchar(current, ret);
  1358. }
  1359. inc_syscr(current);
  1360. inc_syscw(current);
  1361. mnt_drop_write_file(file_out);
  1362. return ret;
  1363. }
  1364. EXPORT_SYMBOL(vfs_copy_file_range);
  1365. SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
  1366. int, fd_out, loff_t __user *, off_out,
  1367. size_t, len, unsigned int, flags)
  1368. {
  1369. loff_t pos_in;
  1370. loff_t pos_out;
  1371. struct fd f_in;
  1372. struct fd f_out;
  1373. ssize_t ret = -EBADF;
  1374. f_in = fdget(fd_in);
  1375. if (!f_in.file)
  1376. goto out2;
  1377. f_out = fdget(fd_out);
  1378. if (!f_out.file)
  1379. goto out1;
  1380. ret = -EFAULT;
  1381. if (off_in) {
  1382. if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
  1383. goto out;
  1384. } else {
  1385. pos_in = f_in.file->f_pos;
  1386. }
  1387. if (off_out) {
  1388. if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
  1389. goto out;
  1390. } else {
  1391. pos_out = f_out.file->f_pos;
  1392. }
  1393. ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
  1394. flags);
  1395. if (ret > 0) {
  1396. pos_in += ret;
  1397. pos_out += ret;
  1398. if (off_in) {
  1399. if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
  1400. ret = -EFAULT;
  1401. } else {
  1402. f_in.file->f_pos = pos_in;
  1403. }
  1404. if (off_out) {
  1405. if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
  1406. ret = -EFAULT;
  1407. } else {
  1408. f_out.file->f_pos = pos_out;
  1409. }
  1410. }
  1411. out:
  1412. fdput(f_out);
  1413. out1:
  1414. fdput(f_in);
  1415. out2:
  1416. return ret;
  1417. }
  1418. static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
  1419. {
  1420. struct inode *inode = file_inode(file);
  1421. if (unlikely(pos < 0))
  1422. return -EINVAL;
  1423. if (unlikely((loff_t) (pos + len) < 0))
  1424. return -EINVAL;
  1425. if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
  1426. loff_t end = len ? pos + len - 1 : OFFSET_MAX;
  1427. int retval;
  1428. retval = locks_mandatory_area(inode, file, pos, end,
  1429. write ? F_WRLCK : F_RDLCK);
  1430. if (retval < 0)
  1431. return retval;
  1432. }
  1433. return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
  1434. }
  1435. int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
  1436. struct file *file_out, loff_t pos_out, u64 len)
  1437. {
  1438. struct inode *inode_in = file_inode(file_in);
  1439. struct inode *inode_out = file_inode(file_out);
  1440. int ret;
  1441. if (inode_in->i_sb != inode_out->i_sb ||
  1442. file_in->f_path.mnt != file_out->f_path.mnt)
  1443. return -EXDEV;
  1444. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1445. return -EISDIR;
  1446. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1447. return -EINVAL;
  1448. if (!(file_in->f_mode & FMODE_READ) ||
  1449. !(file_out->f_mode & FMODE_WRITE) ||
  1450. (file_out->f_flags & O_APPEND))
  1451. return -EBADF;
  1452. if (!file_in->f_op->clone_file_range)
  1453. return -EOPNOTSUPP;
  1454. ret = clone_verify_area(file_in, pos_in, len, false);
  1455. if (ret)
  1456. return ret;
  1457. ret = clone_verify_area(file_out, pos_out, len, true);
  1458. if (ret)
  1459. return ret;
  1460. if (pos_in + len > i_size_read(inode_in))
  1461. return -EINVAL;
  1462. ret = mnt_want_write_file(file_out);
  1463. if (ret)
  1464. return ret;
  1465. ret = file_in->f_op->clone_file_range(file_in, pos_in,
  1466. file_out, pos_out, len);
  1467. if (!ret) {
  1468. fsnotify_access(file_in);
  1469. fsnotify_modify(file_out);
  1470. }
  1471. mnt_drop_write_file(file_out);
  1472. return ret;
  1473. }
  1474. EXPORT_SYMBOL(vfs_clone_file_range);
  1475. int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
  1476. {
  1477. struct file_dedupe_range_info *info;
  1478. struct inode *src = file_inode(file);
  1479. u64 off;
  1480. u64 len;
  1481. int i;
  1482. int ret;
  1483. bool is_admin = capable(CAP_SYS_ADMIN);
  1484. u16 count = same->dest_count;
  1485. struct file *dst_file;
  1486. loff_t dst_off;
  1487. ssize_t deduped;
  1488. if (!(file->f_mode & FMODE_READ))
  1489. return -EINVAL;
  1490. if (same->reserved1 || same->reserved2)
  1491. return -EINVAL;
  1492. off = same->src_offset;
  1493. len = same->src_length;
  1494. ret = -EISDIR;
  1495. if (S_ISDIR(src->i_mode))
  1496. goto out;
  1497. ret = -EINVAL;
  1498. if (!S_ISREG(src->i_mode))
  1499. goto out;
  1500. ret = clone_verify_area(file, off, len, false);
  1501. if (ret < 0)
  1502. goto out;
  1503. ret = 0;
  1504. /* pre-format output fields to sane values */
  1505. for (i = 0; i < count; i++) {
  1506. same->info[i].bytes_deduped = 0ULL;
  1507. same->info[i].status = FILE_DEDUPE_RANGE_SAME;
  1508. }
  1509. for (i = 0, info = same->info; i < count; i++, info++) {
  1510. struct inode *dst;
  1511. struct fd dst_fd = fdget(info->dest_fd);
  1512. dst_file = dst_fd.file;
  1513. if (!dst_file) {
  1514. info->status = -EBADF;
  1515. goto next_loop;
  1516. }
  1517. dst = file_inode(dst_file);
  1518. ret = mnt_want_write_file(dst_file);
  1519. if (ret) {
  1520. info->status = ret;
  1521. goto next_loop;
  1522. }
  1523. dst_off = info->dest_offset;
  1524. ret = clone_verify_area(dst_file, dst_off, len, true);
  1525. if (ret < 0) {
  1526. info->status = ret;
  1527. goto next_file;
  1528. }
  1529. ret = 0;
  1530. if (info->reserved) {
  1531. info->status = -EINVAL;
  1532. } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
  1533. info->status = -EINVAL;
  1534. } else if (file->f_path.mnt != dst_file->f_path.mnt) {
  1535. info->status = -EXDEV;
  1536. } else if (S_ISDIR(dst->i_mode)) {
  1537. info->status = -EISDIR;
  1538. } else if (dst_file->f_op->dedupe_file_range == NULL) {
  1539. info->status = -EINVAL;
  1540. } else {
  1541. deduped = dst_file->f_op->dedupe_file_range(file, off,
  1542. len, dst_file,
  1543. info->dest_offset);
  1544. if (deduped == -EBADE)
  1545. info->status = FILE_DEDUPE_RANGE_DIFFERS;
  1546. else if (deduped < 0)
  1547. info->status = deduped;
  1548. else
  1549. info->bytes_deduped += deduped;
  1550. }
  1551. next_file:
  1552. mnt_drop_write_file(dst_file);
  1553. next_loop:
  1554. fdput(dst_fd);
  1555. if (fatal_signal_pending(current))
  1556. goto out;
  1557. }
  1558. out:
  1559. return ret;
  1560. }
  1561. EXPORT_SYMBOL(vfs_dedupe_file_range);