read_write.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/read_write.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/stat.h>
  9. #include <linux/sched/xacct.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/file.h>
  12. #include <linux/uio.h>
  13. #include <linux/fsnotify.h>
  14. #include <linux/security.h>
  15. #include <linux/export.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/splice.h>
  19. #include <linux/compat.h>
  20. #include <linux/mount.h>
  21. #include <linux/fs.h>
  22. #include "internal.h"
  23. #include <linux/uaccess.h>
  24. #include <asm/unistd.h>
  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 bool 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. off_t ksys_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. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  300. {
  301. return ksys_lseek(fd, offset, whence);
  302. }
  303. #ifdef CONFIG_COMPAT
  304. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  305. {
  306. return ksys_lseek(fd, offset, whence);
  307. }
  308. #endif
  309. #ifdef __ARCH_WANT_SYS_LLSEEK
  310. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  311. unsigned long, offset_low, loff_t __user *, result,
  312. unsigned int, whence)
  313. {
  314. int retval;
  315. struct fd f = fdget_pos(fd);
  316. loff_t offset;
  317. if (!f.file)
  318. return -EBADF;
  319. retval = -EINVAL;
  320. if (whence > SEEK_MAX)
  321. goto out_putf;
  322. offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
  323. whence);
  324. retval = (int)offset;
  325. if (offset >= 0) {
  326. retval = -EFAULT;
  327. if (!copy_to_user(result, &offset, sizeof(offset)))
  328. retval = 0;
  329. }
  330. out_putf:
  331. fdput_pos(f);
  332. return retval;
  333. }
  334. #endif
  335. int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
  336. {
  337. struct inode *inode;
  338. loff_t pos;
  339. int retval = -EINVAL;
  340. inode = file_inode(file);
  341. if (unlikely((ssize_t) count < 0))
  342. return retval;
  343. pos = *ppos;
  344. if (unlikely(pos < 0)) {
  345. if (!unsigned_offsets(file))
  346. return retval;
  347. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  348. return -EOVERFLOW;
  349. } else if (unlikely((loff_t) (pos + count) < 0)) {
  350. if (!unsigned_offsets(file))
  351. return retval;
  352. }
  353. if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
  354. retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
  355. read_write == READ ? F_RDLCK : F_WRLCK);
  356. if (retval < 0)
  357. return retval;
  358. }
  359. return security_file_permission(file,
  360. read_write == READ ? MAY_READ : MAY_WRITE);
  361. }
  362. static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  363. {
  364. struct iovec iov = { .iov_base = buf, .iov_len = len };
  365. struct kiocb kiocb;
  366. struct iov_iter iter;
  367. ssize_t ret;
  368. init_sync_kiocb(&kiocb, filp);
  369. kiocb.ki_pos = *ppos;
  370. iov_iter_init(&iter, READ, &iov, 1, len);
  371. ret = call_read_iter(filp, &kiocb, &iter);
  372. BUG_ON(ret == -EIOCBQUEUED);
  373. *ppos = kiocb.ki_pos;
  374. return ret;
  375. }
  376. ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
  377. loff_t *pos)
  378. {
  379. if (file->f_op->read)
  380. return file->f_op->read(file, buf, count, pos);
  381. else if (file->f_op->read_iter)
  382. return new_sync_read(file, buf, count, pos);
  383. else
  384. return -EINVAL;
  385. }
  386. ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
  387. {
  388. mm_segment_t old_fs;
  389. ssize_t result;
  390. old_fs = get_fs();
  391. set_fs(get_ds());
  392. /* The cast to a user pointer is valid due to the set_fs() */
  393. result = vfs_read(file, (void __user *)buf, count, pos);
  394. set_fs(old_fs);
  395. return result;
  396. }
  397. EXPORT_SYMBOL(kernel_read);
  398. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  399. {
  400. ssize_t ret;
  401. if (!(file->f_mode & FMODE_READ))
  402. return -EBADF;
  403. if (!(file->f_mode & FMODE_CAN_READ))
  404. return -EINVAL;
  405. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  406. return -EFAULT;
  407. ret = rw_verify_area(READ, file, pos, count);
  408. if (!ret) {
  409. if (count > MAX_RW_COUNT)
  410. count = MAX_RW_COUNT;
  411. ret = __vfs_read(file, buf, count, pos);
  412. if (ret > 0) {
  413. fsnotify_access(file);
  414. add_rchar(current, ret);
  415. }
  416. inc_syscr(current);
  417. }
  418. return ret;
  419. }
  420. static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  421. {
  422. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  423. struct kiocb kiocb;
  424. struct iov_iter iter;
  425. ssize_t ret;
  426. init_sync_kiocb(&kiocb, filp);
  427. kiocb.ki_pos = *ppos;
  428. iov_iter_init(&iter, WRITE, &iov, 1, len);
  429. ret = call_write_iter(filp, &kiocb, &iter);
  430. BUG_ON(ret == -EIOCBQUEUED);
  431. if (ret > 0)
  432. *ppos = kiocb.ki_pos;
  433. return ret;
  434. }
  435. ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
  436. loff_t *pos)
  437. {
  438. if (file->f_op->write)
  439. return file->f_op->write(file, p, count, pos);
  440. else if (file->f_op->write_iter)
  441. return new_sync_write(file, p, count, pos);
  442. else
  443. return -EINVAL;
  444. }
  445. ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
  446. {
  447. mm_segment_t old_fs;
  448. const char __user *p;
  449. ssize_t ret;
  450. if (!(file->f_mode & FMODE_CAN_WRITE))
  451. return -EINVAL;
  452. old_fs = get_fs();
  453. set_fs(get_ds());
  454. p = (__force const char __user *)buf;
  455. if (count > MAX_RW_COUNT)
  456. count = MAX_RW_COUNT;
  457. ret = __vfs_write(file, p, count, pos);
  458. set_fs(old_fs);
  459. if (ret > 0) {
  460. fsnotify_modify(file);
  461. add_wchar(current, ret);
  462. }
  463. inc_syscw(current);
  464. return ret;
  465. }
  466. EXPORT_SYMBOL(__kernel_write);
  467. ssize_t kernel_write(struct file *file, const void *buf, size_t count,
  468. loff_t *pos)
  469. {
  470. mm_segment_t old_fs;
  471. ssize_t res;
  472. old_fs = get_fs();
  473. set_fs(get_ds());
  474. /* The cast to a user pointer is valid due to the set_fs() */
  475. res = vfs_write(file, (__force const char __user *)buf, count, pos);
  476. set_fs(old_fs);
  477. return res;
  478. }
  479. EXPORT_SYMBOL(kernel_write);
  480. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  481. {
  482. ssize_t ret;
  483. if (!(file->f_mode & FMODE_WRITE))
  484. return -EBADF;
  485. if (!(file->f_mode & FMODE_CAN_WRITE))
  486. return -EINVAL;
  487. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  488. return -EFAULT;
  489. ret = rw_verify_area(WRITE, file, pos, count);
  490. if (!ret) {
  491. if (count > MAX_RW_COUNT)
  492. count = MAX_RW_COUNT;
  493. file_start_write(file);
  494. ret = __vfs_write(file, buf, count, pos);
  495. if (ret > 0) {
  496. fsnotify_modify(file);
  497. add_wchar(current, ret);
  498. }
  499. inc_syscw(current);
  500. file_end_write(file);
  501. }
  502. return ret;
  503. }
  504. static inline loff_t file_pos_read(struct file *file)
  505. {
  506. return file->f_pos;
  507. }
  508. static inline void file_pos_write(struct file *file, loff_t pos)
  509. {
  510. file->f_pos = pos;
  511. }
  512. ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
  513. {
  514. struct fd f = fdget_pos(fd);
  515. ssize_t ret = -EBADF;
  516. if (f.file) {
  517. loff_t pos = file_pos_read(f.file);
  518. ret = vfs_read(f.file, buf, count, &pos);
  519. if (ret >= 0)
  520. file_pos_write(f.file, pos);
  521. fdput_pos(f);
  522. }
  523. return ret;
  524. }
  525. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  526. {
  527. return ksys_read(fd, buf, count);
  528. }
  529. ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
  530. {
  531. struct fd f = fdget_pos(fd);
  532. ssize_t ret = -EBADF;
  533. if (f.file) {
  534. loff_t pos = file_pos_read(f.file);
  535. ret = vfs_write(f.file, buf, count, &pos);
  536. if (ret >= 0)
  537. file_pos_write(f.file, pos);
  538. fdput_pos(f);
  539. }
  540. return ret;
  541. }
  542. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  543. size_t, count)
  544. {
  545. return ksys_write(fd, buf, count);
  546. }
  547. ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
  548. loff_t pos)
  549. {
  550. struct fd f;
  551. ssize_t ret = -EBADF;
  552. if (pos < 0)
  553. return -EINVAL;
  554. f = fdget(fd);
  555. if (f.file) {
  556. ret = -ESPIPE;
  557. if (f.file->f_mode & FMODE_PREAD)
  558. ret = vfs_read(f.file, buf, count, &pos);
  559. fdput(f);
  560. }
  561. return ret;
  562. }
  563. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  564. size_t, count, loff_t, pos)
  565. {
  566. return ksys_pread64(fd, buf, count, pos);
  567. }
  568. ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
  569. size_t count, loff_t pos)
  570. {
  571. struct fd f;
  572. ssize_t ret = -EBADF;
  573. if (pos < 0)
  574. return -EINVAL;
  575. f = fdget(fd);
  576. if (f.file) {
  577. ret = -ESPIPE;
  578. if (f.file->f_mode & FMODE_PWRITE)
  579. ret = vfs_write(f.file, buf, count, &pos);
  580. fdput(f);
  581. }
  582. return ret;
  583. }
  584. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  585. size_t, count, loff_t, pos)
  586. {
  587. return ksys_pwrite64(fd, buf, count, pos);
  588. }
  589. static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
  590. loff_t *ppos, int type, rwf_t flags)
  591. {
  592. struct kiocb kiocb;
  593. ssize_t ret;
  594. init_sync_kiocb(&kiocb, filp);
  595. ret = kiocb_set_rw_flags(&kiocb, flags);
  596. if (ret)
  597. return ret;
  598. kiocb.ki_pos = *ppos;
  599. if (type == READ)
  600. ret = call_read_iter(filp, &kiocb, iter);
  601. else
  602. ret = call_write_iter(filp, &kiocb, iter);
  603. BUG_ON(ret == -EIOCBQUEUED);
  604. *ppos = kiocb.ki_pos;
  605. return ret;
  606. }
  607. /* Do it by hand, with file-ops */
  608. static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
  609. loff_t *ppos, int type, rwf_t flags)
  610. {
  611. ssize_t ret = 0;
  612. if (flags & ~RWF_HIPRI)
  613. return -EOPNOTSUPP;
  614. while (iov_iter_count(iter)) {
  615. struct iovec iovec = iov_iter_iovec(iter);
  616. ssize_t nr;
  617. if (type == READ) {
  618. nr = filp->f_op->read(filp, iovec.iov_base,
  619. iovec.iov_len, ppos);
  620. } else {
  621. nr = filp->f_op->write(filp, iovec.iov_base,
  622. iovec.iov_len, ppos);
  623. }
  624. if (nr < 0) {
  625. if (!ret)
  626. ret = nr;
  627. break;
  628. }
  629. ret += nr;
  630. if (nr != iovec.iov_len)
  631. break;
  632. iov_iter_advance(iter, nr);
  633. }
  634. return ret;
  635. }
  636. /* A write operation does a read from user space and vice versa */
  637. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  638. /**
  639. * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
  640. * into the kernel and check that it is valid.
  641. *
  642. * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
  643. * @uvector: Pointer to the userspace array.
  644. * @nr_segs: Number of elements in userspace array.
  645. * @fast_segs: Number of elements in @fast_pointer.
  646. * @fast_pointer: Pointer to (usually small on-stack) kernel array.
  647. * @ret_pointer: (output parameter) Pointer to a variable that will point to
  648. * either @fast_pointer, a newly allocated kernel array, or NULL,
  649. * depending on which array was used.
  650. *
  651. * This function copies an array of &struct iovec of @nr_segs from
  652. * userspace into the kernel and checks that each element is valid (e.g.
  653. * it does not point to a kernel address or cause overflow by being too
  654. * large, etc.).
  655. *
  656. * As an optimization, the caller may provide a pointer to a small
  657. * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
  658. * (the size of this array, or 0 if unused, should be given in @fast_segs).
  659. *
  660. * @ret_pointer will always point to the array that was used, so the
  661. * caller must take care not to call kfree() on it e.g. in case the
  662. * @fast_pointer array was used and it was allocated on the stack.
  663. *
  664. * Return: The total number of bytes covered by the iovec array on success
  665. * or a negative error code on error.
  666. */
  667. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  668. unsigned long nr_segs, unsigned long fast_segs,
  669. struct iovec *fast_pointer,
  670. struct iovec **ret_pointer)
  671. {
  672. unsigned long seg;
  673. ssize_t ret;
  674. struct iovec *iov = fast_pointer;
  675. /*
  676. * SuS says "The readv() function *may* fail if the iovcnt argument
  677. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  678. * traditionally returned zero for zero segments, so...
  679. */
  680. if (nr_segs == 0) {
  681. ret = 0;
  682. goto out;
  683. }
  684. /*
  685. * First get the "struct iovec" from user memory and
  686. * verify all the pointers
  687. */
  688. if (nr_segs > UIO_MAXIOV) {
  689. ret = -EINVAL;
  690. goto out;
  691. }
  692. if (nr_segs > fast_segs) {
  693. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  694. if (iov == NULL) {
  695. ret = -ENOMEM;
  696. goto out;
  697. }
  698. }
  699. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  700. ret = -EFAULT;
  701. goto out;
  702. }
  703. /*
  704. * According to the Single Unix Specification we should return EINVAL
  705. * if an element length is < 0 when cast to ssize_t or if the
  706. * total length would overflow the ssize_t return value of the
  707. * system call.
  708. *
  709. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  710. * overflow case.
  711. */
  712. ret = 0;
  713. for (seg = 0; seg < nr_segs; seg++) {
  714. void __user *buf = iov[seg].iov_base;
  715. ssize_t len = (ssize_t)iov[seg].iov_len;
  716. /* see if we we're about to use an invalid len or if
  717. * it's about to overflow ssize_t */
  718. if (len < 0) {
  719. ret = -EINVAL;
  720. goto out;
  721. }
  722. if (type >= 0
  723. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  724. ret = -EFAULT;
  725. goto out;
  726. }
  727. if (len > MAX_RW_COUNT - ret) {
  728. len = MAX_RW_COUNT - ret;
  729. iov[seg].iov_len = len;
  730. }
  731. ret += len;
  732. }
  733. out:
  734. *ret_pointer = iov;
  735. return ret;
  736. }
  737. #ifdef CONFIG_COMPAT
  738. ssize_t compat_rw_copy_check_uvector(int type,
  739. const struct compat_iovec __user *uvector, unsigned long nr_segs,
  740. unsigned long fast_segs, struct iovec *fast_pointer,
  741. struct iovec **ret_pointer)
  742. {
  743. compat_ssize_t tot_len;
  744. struct iovec *iov = *ret_pointer = fast_pointer;
  745. ssize_t ret = 0;
  746. int seg;
  747. /*
  748. * SuS says "The readv() function *may* fail if the iovcnt argument
  749. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  750. * traditionally returned zero for zero segments, so...
  751. */
  752. if (nr_segs == 0)
  753. goto out;
  754. ret = -EINVAL;
  755. if (nr_segs > UIO_MAXIOV)
  756. goto out;
  757. if (nr_segs > fast_segs) {
  758. ret = -ENOMEM;
  759. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  760. if (iov == NULL)
  761. goto out;
  762. }
  763. *ret_pointer = iov;
  764. ret = -EFAULT;
  765. if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
  766. goto out;
  767. /*
  768. * Single unix specification:
  769. * We should -EINVAL if an element length is not >= 0 and fitting an
  770. * ssize_t.
  771. *
  772. * In Linux, the total length is limited to MAX_RW_COUNT, there is
  773. * no overflow possibility.
  774. */
  775. tot_len = 0;
  776. ret = -EINVAL;
  777. for (seg = 0; seg < nr_segs; seg++) {
  778. compat_uptr_t buf;
  779. compat_ssize_t len;
  780. if (__get_user(len, &uvector->iov_len) ||
  781. __get_user(buf, &uvector->iov_base)) {
  782. ret = -EFAULT;
  783. goto out;
  784. }
  785. if (len < 0) /* size_t not fitting in compat_ssize_t .. */
  786. goto out;
  787. if (type >= 0 &&
  788. !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
  789. ret = -EFAULT;
  790. goto out;
  791. }
  792. if (len > MAX_RW_COUNT - tot_len)
  793. len = MAX_RW_COUNT - tot_len;
  794. tot_len += len;
  795. iov->iov_base = compat_ptr(buf);
  796. iov->iov_len = (compat_size_t) len;
  797. uvector++;
  798. iov++;
  799. }
  800. ret = tot_len;
  801. out:
  802. return ret;
  803. }
  804. #endif
  805. static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
  806. loff_t *pos, rwf_t flags)
  807. {
  808. size_t tot_len;
  809. ssize_t ret = 0;
  810. if (!(file->f_mode & FMODE_READ))
  811. return -EBADF;
  812. if (!(file->f_mode & FMODE_CAN_READ))
  813. return -EINVAL;
  814. tot_len = iov_iter_count(iter);
  815. if (!tot_len)
  816. goto out;
  817. ret = rw_verify_area(READ, file, pos, tot_len);
  818. if (ret < 0)
  819. return ret;
  820. if (file->f_op->read_iter)
  821. ret = do_iter_readv_writev(file, iter, pos, READ, flags);
  822. else
  823. ret = do_loop_readv_writev(file, iter, pos, READ, flags);
  824. out:
  825. if (ret >= 0)
  826. fsnotify_access(file);
  827. return ret;
  828. }
  829. ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
  830. rwf_t flags)
  831. {
  832. if (!file->f_op->read_iter)
  833. return -EINVAL;
  834. return do_iter_read(file, iter, ppos, flags);
  835. }
  836. EXPORT_SYMBOL(vfs_iter_read);
  837. static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
  838. loff_t *pos, rwf_t flags)
  839. {
  840. size_t tot_len;
  841. ssize_t ret = 0;
  842. if (!(file->f_mode & FMODE_WRITE))
  843. return -EBADF;
  844. if (!(file->f_mode & FMODE_CAN_WRITE))
  845. return -EINVAL;
  846. tot_len = iov_iter_count(iter);
  847. if (!tot_len)
  848. return 0;
  849. ret = rw_verify_area(WRITE, file, pos, tot_len);
  850. if (ret < 0)
  851. return ret;
  852. if (file->f_op->write_iter)
  853. ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
  854. else
  855. ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
  856. if (ret > 0)
  857. fsnotify_modify(file);
  858. return ret;
  859. }
  860. ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
  861. rwf_t flags)
  862. {
  863. if (!file->f_op->write_iter)
  864. return -EINVAL;
  865. return do_iter_write(file, iter, ppos, flags);
  866. }
  867. EXPORT_SYMBOL(vfs_iter_write);
  868. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  869. unsigned long vlen, loff_t *pos, rwf_t flags)
  870. {
  871. struct iovec iovstack[UIO_FASTIOV];
  872. struct iovec *iov = iovstack;
  873. struct iov_iter iter;
  874. ssize_t ret;
  875. ret = import_iovec(READ, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
  876. if (ret >= 0) {
  877. ret = do_iter_read(file, &iter, pos, flags);
  878. kfree(iov);
  879. }
  880. return ret;
  881. }
  882. static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  883. unsigned long vlen, loff_t *pos, rwf_t flags)
  884. {
  885. struct iovec iovstack[UIO_FASTIOV];
  886. struct iovec *iov = iovstack;
  887. struct iov_iter iter;
  888. ssize_t ret;
  889. ret = import_iovec(WRITE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
  890. if (ret >= 0) {
  891. file_start_write(file);
  892. ret = do_iter_write(file, &iter, pos, flags);
  893. file_end_write(file);
  894. kfree(iov);
  895. }
  896. return ret;
  897. }
  898. static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
  899. unsigned long vlen, rwf_t flags)
  900. {
  901. struct fd f = fdget_pos(fd);
  902. ssize_t ret = -EBADF;
  903. if (f.file) {
  904. loff_t pos = file_pos_read(f.file);
  905. ret = vfs_readv(f.file, vec, vlen, &pos, flags);
  906. if (ret >= 0)
  907. file_pos_write(f.file, pos);
  908. fdput_pos(f);
  909. }
  910. if (ret > 0)
  911. add_rchar(current, ret);
  912. inc_syscr(current);
  913. return ret;
  914. }
  915. static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
  916. unsigned long vlen, rwf_t flags)
  917. {
  918. struct fd f = fdget_pos(fd);
  919. ssize_t ret = -EBADF;
  920. if (f.file) {
  921. loff_t pos = file_pos_read(f.file);
  922. ret = vfs_writev(f.file, vec, vlen, &pos, flags);
  923. if (ret >= 0)
  924. file_pos_write(f.file, pos);
  925. fdput_pos(f);
  926. }
  927. if (ret > 0)
  928. add_wchar(current, ret);
  929. inc_syscw(current);
  930. return ret;
  931. }
  932. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  933. {
  934. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  935. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  936. }
  937. static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
  938. unsigned long vlen, loff_t pos, rwf_t flags)
  939. {
  940. struct fd f;
  941. ssize_t ret = -EBADF;
  942. if (pos < 0)
  943. return -EINVAL;
  944. f = fdget(fd);
  945. if (f.file) {
  946. ret = -ESPIPE;
  947. if (f.file->f_mode & FMODE_PREAD)
  948. ret = vfs_readv(f.file, vec, vlen, &pos, flags);
  949. fdput(f);
  950. }
  951. if (ret > 0)
  952. add_rchar(current, ret);
  953. inc_syscr(current);
  954. return ret;
  955. }
  956. static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
  957. unsigned long vlen, loff_t pos, rwf_t flags)
  958. {
  959. struct fd f;
  960. ssize_t ret = -EBADF;
  961. if (pos < 0)
  962. return -EINVAL;
  963. f = fdget(fd);
  964. if (f.file) {
  965. ret = -ESPIPE;
  966. if (f.file->f_mode & FMODE_PWRITE)
  967. ret = vfs_writev(f.file, vec, vlen, &pos, flags);
  968. fdput(f);
  969. }
  970. if (ret > 0)
  971. add_wchar(current, ret);
  972. inc_syscw(current);
  973. return ret;
  974. }
  975. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  976. unsigned long, vlen)
  977. {
  978. return do_readv(fd, vec, vlen, 0);
  979. }
  980. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  981. unsigned long, vlen)
  982. {
  983. return do_writev(fd, vec, vlen, 0);
  984. }
  985. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  986. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  987. {
  988. loff_t pos = pos_from_hilo(pos_h, pos_l);
  989. return do_preadv(fd, vec, vlen, pos, 0);
  990. }
  991. SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
  992. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  993. rwf_t, flags)
  994. {
  995. loff_t pos = pos_from_hilo(pos_h, pos_l);
  996. if (pos == -1)
  997. return do_readv(fd, vec, vlen, flags);
  998. return do_preadv(fd, vec, vlen, pos, flags);
  999. }
  1000. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  1001. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  1002. {
  1003. loff_t pos = pos_from_hilo(pos_h, pos_l);
  1004. return do_pwritev(fd, vec, vlen, pos, 0);
  1005. }
  1006. SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
  1007. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  1008. rwf_t, flags)
  1009. {
  1010. loff_t pos = pos_from_hilo(pos_h, pos_l);
  1011. if (pos == -1)
  1012. return do_writev(fd, vec, vlen, flags);
  1013. return do_pwritev(fd, vec, vlen, pos, flags);
  1014. }
  1015. #ifdef CONFIG_COMPAT
  1016. static size_t compat_readv(struct file *file,
  1017. const struct compat_iovec __user *vec,
  1018. unsigned long vlen, loff_t *pos, rwf_t flags)
  1019. {
  1020. struct iovec iovstack[UIO_FASTIOV];
  1021. struct iovec *iov = iovstack;
  1022. struct iov_iter iter;
  1023. ssize_t ret;
  1024. ret = compat_import_iovec(READ, vec, vlen, UIO_FASTIOV, &iov, &iter);
  1025. if (ret >= 0) {
  1026. ret = do_iter_read(file, &iter, pos, flags);
  1027. kfree(iov);
  1028. }
  1029. if (ret > 0)
  1030. add_rchar(current, ret);
  1031. inc_syscr(current);
  1032. return ret;
  1033. }
  1034. static size_t do_compat_readv(compat_ulong_t fd,
  1035. const struct compat_iovec __user *vec,
  1036. compat_ulong_t vlen, rwf_t flags)
  1037. {
  1038. struct fd f = fdget_pos(fd);
  1039. ssize_t ret;
  1040. loff_t pos;
  1041. if (!f.file)
  1042. return -EBADF;
  1043. pos = f.file->f_pos;
  1044. ret = compat_readv(f.file, vec, vlen, &pos, flags);
  1045. if (ret >= 0)
  1046. f.file->f_pos = pos;
  1047. fdput_pos(f);
  1048. return ret;
  1049. }
  1050. COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
  1051. const struct compat_iovec __user *,vec,
  1052. compat_ulong_t, vlen)
  1053. {
  1054. return do_compat_readv(fd, vec, vlen, 0);
  1055. }
  1056. static long do_compat_preadv64(unsigned long fd,
  1057. const struct compat_iovec __user *vec,
  1058. unsigned long vlen, loff_t pos, rwf_t flags)
  1059. {
  1060. struct fd f;
  1061. ssize_t ret;
  1062. if (pos < 0)
  1063. return -EINVAL;
  1064. f = fdget(fd);
  1065. if (!f.file)
  1066. return -EBADF;
  1067. ret = -ESPIPE;
  1068. if (f.file->f_mode & FMODE_PREAD)
  1069. ret = compat_readv(f.file, vec, vlen, &pos, flags);
  1070. fdput(f);
  1071. return ret;
  1072. }
  1073. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
  1074. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  1075. const struct compat_iovec __user *,vec,
  1076. unsigned long, vlen, loff_t, pos)
  1077. {
  1078. return do_compat_preadv64(fd, vec, vlen, pos, 0);
  1079. }
  1080. #endif
  1081. COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
  1082. const struct compat_iovec __user *,vec,
  1083. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1084. {
  1085. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1086. return do_compat_preadv64(fd, vec, vlen, pos, 0);
  1087. }
  1088. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
  1089. COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
  1090. const struct compat_iovec __user *,vec,
  1091. unsigned long, vlen, loff_t, pos, rwf_t, flags)
  1092. {
  1093. return do_compat_preadv64(fd, vec, vlen, pos, flags);
  1094. }
  1095. #endif
  1096. COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
  1097. const struct compat_iovec __user *,vec,
  1098. compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
  1099. rwf_t, flags)
  1100. {
  1101. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1102. if (pos == -1)
  1103. return do_compat_readv(fd, vec, vlen, flags);
  1104. return do_compat_preadv64(fd, vec, vlen, pos, flags);
  1105. }
  1106. static size_t compat_writev(struct file *file,
  1107. const struct compat_iovec __user *vec,
  1108. unsigned long vlen, loff_t *pos, rwf_t flags)
  1109. {
  1110. struct iovec iovstack[UIO_FASTIOV];
  1111. struct iovec *iov = iovstack;
  1112. struct iov_iter iter;
  1113. ssize_t ret;
  1114. ret = compat_import_iovec(WRITE, vec, vlen, UIO_FASTIOV, &iov, &iter);
  1115. if (ret >= 0) {
  1116. file_start_write(file);
  1117. ret = do_iter_write(file, &iter, pos, flags);
  1118. file_end_write(file);
  1119. kfree(iov);
  1120. }
  1121. if (ret > 0)
  1122. add_wchar(current, ret);
  1123. inc_syscw(current);
  1124. return ret;
  1125. }
  1126. static size_t do_compat_writev(compat_ulong_t fd,
  1127. const struct compat_iovec __user* vec,
  1128. compat_ulong_t vlen, rwf_t flags)
  1129. {
  1130. struct fd f = fdget_pos(fd);
  1131. ssize_t ret;
  1132. loff_t pos;
  1133. if (!f.file)
  1134. return -EBADF;
  1135. pos = f.file->f_pos;
  1136. ret = compat_writev(f.file, vec, vlen, &pos, flags);
  1137. if (ret >= 0)
  1138. f.file->f_pos = pos;
  1139. fdput_pos(f);
  1140. return ret;
  1141. }
  1142. COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
  1143. const struct compat_iovec __user *, vec,
  1144. compat_ulong_t, vlen)
  1145. {
  1146. return do_compat_writev(fd, vec, vlen, 0);
  1147. }
  1148. static long do_compat_pwritev64(unsigned long fd,
  1149. const struct compat_iovec __user *vec,
  1150. unsigned long vlen, loff_t pos, rwf_t flags)
  1151. {
  1152. struct fd f;
  1153. ssize_t ret;
  1154. if (pos < 0)
  1155. return -EINVAL;
  1156. f = fdget(fd);
  1157. if (!f.file)
  1158. return -EBADF;
  1159. ret = -ESPIPE;
  1160. if (f.file->f_mode & FMODE_PWRITE)
  1161. ret = compat_writev(f.file, vec, vlen, &pos, flags);
  1162. fdput(f);
  1163. return ret;
  1164. }
  1165. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
  1166. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  1167. const struct compat_iovec __user *,vec,
  1168. unsigned long, vlen, loff_t, pos)
  1169. {
  1170. return do_compat_pwritev64(fd, vec, vlen, pos, 0);
  1171. }
  1172. #endif
  1173. COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
  1174. const struct compat_iovec __user *,vec,
  1175. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1176. {
  1177. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1178. return do_compat_pwritev64(fd, vec, vlen, pos, 0);
  1179. }
  1180. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
  1181. COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
  1182. const struct compat_iovec __user *,vec,
  1183. unsigned long, vlen, loff_t, pos, rwf_t, flags)
  1184. {
  1185. return do_compat_pwritev64(fd, vec, vlen, pos, flags);
  1186. }
  1187. #endif
  1188. COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
  1189. const struct compat_iovec __user *,vec,
  1190. compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
  1191. {
  1192. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1193. if (pos == -1)
  1194. return do_compat_writev(fd, vec, vlen, flags);
  1195. return do_compat_pwritev64(fd, vec, vlen, pos, flags);
  1196. }
  1197. #endif
  1198. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  1199. size_t count, loff_t max)
  1200. {
  1201. struct fd in, out;
  1202. struct inode *in_inode, *out_inode;
  1203. loff_t pos;
  1204. loff_t out_pos;
  1205. ssize_t retval;
  1206. int fl;
  1207. /*
  1208. * Get input file, and verify that it is ok..
  1209. */
  1210. retval = -EBADF;
  1211. in = fdget(in_fd);
  1212. if (!in.file)
  1213. goto out;
  1214. if (!(in.file->f_mode & FMODE_READ))
  1215. goto fput_in;
  1216. retval = -ESPIPE;
  1217. if (!ppos) {
  1218. pos = in.file->f_pos;
  1219. } else {
  1220. pos = *ppos;
  1221. if (!(in.file->f_mode & FMODE_PREAD))
  1222. goto fput_in;
  1223. }
  1224. retval = rw_verify_area(READ, in.file, &pos, count);
  1225. if (retval < 0)
  1226. goto fput_in;
  1227. if (count > MAX_RW_COUNT)
  1228. count = MAX_RW_COUNT;
  1229. /*
  1230. * Get output file, and verify that it is ok..
  1231. */
  1232. retval = -EBADF;
  1233. out = fdget(out_fd);
  1234. if (!out.file)
  1235. goto fput_in;
  1236. if (!(out.file->f_mode & FMODE_WRITE))
  1237. goto fput_out;
  1238. retval = -EINVAL;
  1239. in_inode = file_inode(in.file);
  1240. out_inode = file_inode(out.file);
  1241. out_pos = out.file->f_pos;
  1242. retval = rw_verify_area(WRITE, out.file, &out_pos, count);
  1243. if (retval < 0)
  1244. goto fput_out;
  1245. if (!max)
  1246. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  1247. if (unlikely(pos + count > max)) {
  1248. retval = -EOVERFLOW;
  1249. if (pos >= max)
  1250. goto fput_out;
  1251. count = max - pos;
  1252. }
  1253. fl = 0;
  1254. #if 0
  1255. /*
  1256. * We need to debate whether we can enable this or not. The
  1257. * man page documents EAGAIN return for the output at least,
  1258. * and the application is arguably buggy if it doesn't expect
  1259. * EAGAIN on a non-blocking file descriptor.
  1260. */
  1261. if (in.file->f_flags & O_NONBLOCK)
  1262. fl = SPLICE_F_NONBLOCK;
  1263. #endif
  1264. file_start_write(out.file);
  1265. retval = do_splice_direct(in.file, &pos, out.file, &out_pos, count, fl);
  1266. file_end_write(out.file);
  1267. if (retval > 0) {
  1268. add_rchar(current, retval);
  1269. add_wchar(current, retval);
  1270. fsnotify_access(in.file);
  1271. fsnotify_modify(out.file);
  1272. out.file->f_pos = out_pos;
  1273. if (ppos)
  1274. *ppos = pos;
  1275. else
  1276. in.file->f_pos = pos;
  1277. }
  1278. inc_syscr(current);
  1279. inc_syscw(current);
  1280. if (pos > max)
  1281. retval = -EOVERFLOW;
  1282. fput_out:
  1283. fdput(out);
  1284. fput_in:
  1285. fdput(in);
  1286. out:
  1287. return retval;
  1288. }
  1289. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  1290. {
  1291. loff_t pos;
  1292. off_t off;
  1293. ssize_t ret;
  1294. if (offset) {
  1295. if (unlikely(get_user(off, offset)))
  1296. return -EFAULT;
  1297. pos = off;
  1298. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1299. if (unlikely(put_user(pos, offset)))
  1300. return -EFAULT;
  1301. return ret;
  1302. }
  1303. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1304. }
  1305. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  1306. {
  1307. loff_t pos;
  1308. ssize_t ret;
  1309. if (offset) {
  1310. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1311. return -EFAULT;
  1312. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1313. if (unlikely(put_user(pos, offset)))
  1314. return -EFAULT;
  1315. return ret;
  1316. }
  1317. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1318. }
  1319. #ifdef CONFIG_COMPAT
  1320. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  1321. compat_off_t __user *, offset, compat_size_t, count)
  1322. {
  1323. loff_t pos;
  1324. off_t off;
  1325. ssize_t ret;
  1326. if (offset) {
  1327. if (unlikely(get_user(off, offset)))
  1328. return -EFAULT;
  1329. pos = off;
  1330. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1331. if (unlikely(put_user(pos, offset)))
  1332. return -EFAULT;
  1333. return ret;
  1334. }
  1335. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1336. }
  1337. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  1338. compat_loff_t __user *, offset, compat_size_t, count)
  1339. {
  1340. loff_t pos;
  1341. ssize_t ret;
  1342. if (offset) {
  1343. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1344. return -EFAULT;
  1345. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1346. if (unlikely(put_user(pos, offset)))
  1347. return -EFAULT;
  1348. return ret;
  1349. }
  1350. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1351. }
  1352. #endif
  1353. /*
  1354. * copy_file_range() differs from regular file read and write in that it
  1355. * specifically allows return partial success. When it does so is up to
  1356. * the copy_file_range method.
  1357. */
  1358. ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
  1359. struct file *file_out, loff_t pos_out,
  1360. size_t len, unsigned int flags)
  1361. {
  1362. struct inode *inode_in = file_inode(file_in);
  1363. struct inode *inode_out = file_inode(file_out);
  1364. ssize_t ret;
  1365. if (flags != 0)
  1366. return -EINVAL;
  1367. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1368. return -EISDIR;
  1369. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1370. return -EINVAL;
  1371. ret = rw_verify_area(READ, file_in, &pos_in, len);
  1372. if (unlikely(ret))
  1373. return ret;
  1374. ret = rw_verify_area(WRITE, file_out, &pos_out, len);
  1375. if (unlikely(ret))
  1376. return ret;
  1377. if (!(file_in->f_mode & FMODE_READ) ||
  1378. !(file_out->f_mode & FMODE_WRITE) ||
  1379. (file_out->f_flags & O_APPEND))
  1380. return -EBADF;
  1381. /* this could be relaxed once a method supports cross-fs copies */
  1382. if (inode_in->i_sb != inode_out->i_sb)
  1383. return -EXDEV;
  1384. if (len == 0)
  1385. return 0;
  1386. file_start_write(file_out);
  1387. /*
  1388. * Try cloning first, this is supported by more file systems, and
  1389. * more efficient if both clone and copy are supported (e.g. NFS).
  1390. */
  1391. if (file_in->f_op->clone_file_range) {
  1392. ret = file_in->f_op->clone_file_range(file_in, pos_in,
  1393. file_out, pos_out, len);
  1394. if (ret == 0) {
  1395. ret = len;
  1396. goto done;
  1397. }
  1398. }
  1399. if (file_out->f_op->copy_file_range) {
  1400. ret = file_out->f_op->copy_file_range(file_in, pos_in, file_out,
  1401. pos_out, len, flags);
  1402. if (ret != -EOPNOTSUPP)
  1403. goto done;
  1404. }
  1405. ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
  1406. len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
  1407. done:
  1408. if (ret > 0) {
  1409. fsnotify_access(file_in);
  1410. add_rchar(current, ret);
  1411. fsnotify_modify(file_out);
  1412. add_wchar(current, ret);
  1413. }
  1414. inc_syscr(current);
  1415. inc_syscw(current);
  1416. file_end_write(file_out);
  1417. return ret;
  1418. }
  1419. EXPORT_SYMBOL(vfs_copy_file_range);
  1420. SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
  1421. int, fd_out, loff_t __user *, off_out,
  1422. size_t, len, unsigned int, flags)
  1423. {
  1424. loff_t pos_in;
  1425. loff_t pos_out;
  1426. struct fd f_in;
  1427. struct fd f_out;
  1428. ssize_t ret = -EBADF;
  1429. f_in = fdget(fd_in);
  1430. if (!f_in.file)
  1431. goto out2;
  1432. f_out = fdget(fd_out);
  1433. if (!f_out.file)
  1434. goto out1;
  1435. ret = -EFAULT;
  1436. if (off_in) {
  1437. if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
  1438. goto out;
  1439. } else {
  1440. pos_in = f_in.file->f_pos;
  1441. }
  1442. if (off_out) {
  1443. if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
  1444. goto out;
  1445. } else {
  1446. pos_out = f_out.file->f_pos;
  1447. }
  1448. ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
  1449. flags);
  1450. if (ret > 0) {
  1451. pos_in += ret;
  1452. pos_out += ret;
  1453. if (off_in) {
  1454. if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
  1455. ret = -EFAULT;
  1456. } else {
  1457. f_in.file->f_pos = pos_in;
  1458. }
  1459. if (off_out) {
  1460. if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
  1461. ret = -EFAULT;
  1462. } else {
  1463. f_out.file->f_pos = pos_out;
  1464. }
  1465. }
  1466. out:
  1467. fdput(f_out);
  1468. out1:
  1469. fdput(f_in);
  1470. out2:
  1471. return ret;
  1472. }
  1473. static int clone_verify_area(struct file *file, loff_t pos, u64 len, bool write)
  1474. {
  1475. struct inode *inode = file_inode(file);
  1476. if (unlikely(pos < 0))
  1477. return -EINVAL;
  1478. if (unlikely((loff_t) (pos + len) < 0))
  1479. return -EINVAL;
  1480. if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
  1481. loff_t end = len ? pos + len - 1 : OFFSET_MAX;
  1482. int retval;
  1483. retval = locks_mandatory_area(inode, file, pos, end,
  1484. write ? F_WRLCK : F_RDLCK);
  1485. if (retval < 0)
  1486. return retval;
  1487. }
  1488. return security_file_permission(file, write ? MAY_WRITE : MAY_READ);
  1489. }
  1490. /*
  1491. * Check that the two inodes are eligible for cloning, the ranges make
  1492. * sense, and then flush all dirty data. Caller must ensure that the
  1493. * inodes have been locked against any other modifications.
  1494. *
  1495. * Returns: 0 for "nothing to clone", 1 for "something to clone", or
  1496. * the usual negative error code.
  1497. */
  1498. int vfs_clone_file_prep_inodes(struct inode *inode_in, loff_t pos_in,
  1499. struct inode *inode_out, loff_t pos_out,
  1500. u64 *len, bool is_dedupe)
  1501. {
  1502. loff_t bs = inode_out->i_sb->s_blocksize;
  1503. loff_t blen;
  1504. loff_t isize;
  1505. bool same_inode = (inode_in == inode_out);
  1506. int ret;
  1507. /* Don't touch certain kinds of inodes */
  1508. if (IS_IMMUTABLE(inode_out))
  1509. return -EPERM;
  1510. if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
  1511. return -ETXTBSY;
  1512. /* Don't reflink dirs, pipes, sockets... */
  1513. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1514. return -EISDIR;
  1515. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1516. return -EINVAL;
  1517. /* Are we going all the way to the end? */
  1518. isize = i_size_read(inode_in);
  1519. if (isize == 0)
  1520. return 0;
  1521. /* Zero length dedupe exits immediately; reflink goes to EOF. */
  1522. if (*len == 0) {
  1523. if (is_dedupe || pos_in == isize)
  1524. return 0;
  1525. if (pos_in > isize)
  1526. return -EINVAL;
  1527. *len = isize - pos_in;
  1528. }
  1529. /* Ensure offsets don't wrap and the input is inside i_size */
  1530. if (pos_in + *len < pos_in || pos_out + *len < pos_out ||
  1531. pos_in + *len > isize)
  1532. return -EINVAL;
  1533. /* Don't allow dedupe past EOF in the dest file */
  1534. if (is_dedupe) {
  1535. loff_t disize;
  1536. disize = i_size_read(inode_out);
  1537. if (pos_out >= disize || pos_out + *len > disize)
  1538. return -EINVAL;
  1539. }
  1540. /* If we're linking to EOF, continue to the block boundary. */
  1541. if (pos_in + *len == isize)
  1542. blen = ALIGN(isize, bs) - pos_in;
  1543. else
  1544. blen = *len;
  1545. /* Only reflink if we're aligned to block boundaries */
  1546. if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_in + blen, bs) ||
  1547. !IS_ALIGNED(pos_out, bs) || !IS_ALIGNED(pos_out + blen, bs))
  1548. return -EINVAL;
  1549. /* Don't allow overlapped reflink within the same file */
  1550. if (same_inode) {
  1551. if (pos_out + blen > pos_in && pos_out < pos_in + blen)
  1552. return -EINVAL;
  1553. }
  1554. /* Wait for the completion of any pending IOs on both files */
  1555. inode_dio_wait(inode_in);
  1556. if (!same_inode)
  1557. inode_dio_wait(inode_out);
  1558. ret = filemap_write_and_wait_range(inode_in->i_mapping,
  1559. pos_in, pos_in + *len - 1);
  1560. if (ret)
  1561. return ret;
  1562. ret = filemap_write_and_wait_range(inode_out->i_mapping,
  1563. pos_out, pos_out + *len - 1);
  1564. if (ret)
  1565. return ret;
  1566. /*
  1567. * Check that the extents are the same.
  1568. */
  1569. if (is_dedupe) {
  1570. bool is_same = false;
  1571. ret = vfs_dedupe_file_range_compare(inode_in, pos_in,
  1572. inode_out, pos_out, *len, &is_same);
  1573. if (ret)
  1574. return ret;
  1575. if (!is_same)
  1576. return -EBADE;
  1577. }
  1578. return 1;
  1579. }
  1580. EXPORT_SYMBOL(vfs_clone_file_prep_inodes);
  1581. int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
  1582. struct file *file_out, loff_t pos_out, u64 len)
  1583. {
  1584. struct inode *inode_in = file_inode(file_in);
  1585. struct inode *inode_out = file_inode(file_out);
  1586. int ret;
  1587. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1588. return -EISDIR;
  1589. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1590. return -EINVAL;
  1591. /*
  1592. * FICLONE/FICLONERANGE ioctls enforce that src and dest files are on
  1593. * the same mount. Practically, they only need to be on the same file
  1594. * system.
  1595. */
  1596. if (inode_in->i_sb != inode_out->i_sb)
  1597. return -EXDEV;
  1598. if (!(file_in->f_mode & FMODE_READ) ||
  1599. !(file_out->f_mode & FMODE_WRITE) ||
  1600. (file_out->f_flags & O_APPEND))
  1601. return -EBADF;
  1602. if (!file_in->f_op->clone_file_range)
  1603. return -EOPNOTSUPP;
  1604. ret = clone_verify_area(file_in, pos_in, len, false);
  1605. if (ret)
  1606. return ret;
  1607. ret = clone_verify_area(file_out, pos_out, len, true);
  1608. if (ret)
  1609. return ret;
  1610. if (pos_in + len > i_size_read(inode_in))
  1611. return -EINVAL;
  1612. ret = file_in->f_op->clone_file_range(file_in, pos_in,
  1613. file_out, pos_out, len);
  1614. if (!ret) {
  1615. fsnotify_access(file_in);
  1616. fsnotify_modify(file_out);
  1617. }
  1618. return ret;
  1619. }
  1620. EXPORT_SYMBOL(vfs_clone_file_range);
  1621. /*
  1622. * Read a page's worth of file data into the page cache. Return the page
  1623. * locked.
  1624. */
  1625. static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
  1626. {
  1627. struct address_space *mapping;
  1628. struct page *page;
  1629. pgoff_t n;
  1630. n = offset >> PAGE_SHIFT;
  1631. mapping = inode->i_mapping;
  1632. page = read_mapping_page(mapping, n, NULL);
  1633. if (IS_ERR(page))
  1634. return page;
  1635. if (!PageUptodate(page)) {
  1636. put_page(page);
  1637. return ERR_PTR(-EIO);
  1638. }
  1639. lock_page(page);
  1640. return page;
  1641. }
  1642. /*
  1643. * Compare extents of two files to see if they are the same.
  1644. * Caller must have locked both inodes to prevent write races.
  1645. */
  1646. int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
  1647. struct inode *dest, loff_t destoff,
  1648. loff_t len, bool *is_same)
  1649. {
  1650. loff_t src_poff;
  1651. loff_t dest_poff;
  1652. void *src_addr;
  1653. void *dest_addr;
  1654. struct page *src_page;
  1655. struct page *dest_page;
  1656. loff_t cmp_len;
  1657. bool same;
  1658. int error;
  1659. error = -EINVAL;
  1660. same = true;
  1661. while (len) {
  1662. src_poff = srcoff & (PAGE_SIZE - 1);
  1663. dest_poff = destoff & (PAGE_SIZE - 1);
  1664. cmp_len = min(PAGE_SIZE - src_poff,
  1665. PAGE_SIZE - dest_poff);
  1666. cmp_len = min(cmp_len, len);
  1667. if (cmp_len <= 0)
  1668. goto out_error;
  1669. src_page = vfs_dedupe_get_page(src, srcoff);
  1670. if (IS_ERR(src_page)) {
  1671. error = PTR_ERR(src_page);
  1672. goto out_error;
  1673. }
  1674. dest_page = vfs_dedupe_get_page(dest, destoff);
  1675. if (IS_ERR(dest_page)) {
  1676. error = PTR_ERR(dest_page);
  1677. unlock_page(src_page);
  1678. put_page(src_page);
  1679. goto out_error;
  1680. }
  1681. src_addr = kmap_atomic(src_page);
  1682. dest_addr = kmap_atomic(dest_page);
  1683. flush_dcache_page(src_page);
  1684. flush_dcache_page(dest_page);
  1685. if (memcmp(src_addr + src_poff, dest_addr + dest_poff, cmp_len))
  1686. same = false;
  1687. kunmap_atomic(dest_addr);
  1688. kunmap_atomic(src_addr);
  1689. unlock_page(dest_page);
  1690. unlock_page(src_page);
  1691. put_page(dest_page);
  1692. put_page(src_page);
  1693. if (!same)
  1694. break;
  1695. srcoff += cmp_len;
  1696. destoff += cmp_len;
  1697. len -= cmp_len;
  1698. }
  1699. *is_same = same;
  1700. return 0;
  1701. out_error:
  1702. return error;
  1703. }
  1704. EXPORT_SYMBOL(vfs_dedupe_file_range_compare);
  1705. int vfs_dedupe_file_range(struct file *file, struct file_dedupe_range *same)
  1706. {
  1707. struct file_dedupe_range_info *info;
  1708. struct inode *src = file_inode(file);
  1709. u64 off;
  1710. u64 len;
  1711. int i;
  1712. int ret;
  1713. bool is_admin = capable(CAP_SYS_ADMIN);
  1714. u16 count = same->dest_count;
  1715. struct file *dst_file;
  1716. loff_t dst_off;
  1717. ssize_t deduped;
  1718. if (!(file->f_mode & FMODE_READ))
  1719. return -EINVAL;
  1720. if (same->reserved1 || same->reserved2)
  1721. return -EINVAL;
  1722. off = same->src_offset;
  1723. len = same->src_length;
  1724. ret = -EISDIR;
  1725. if (S_ISDIR(src->i_mode))
  1726. goto out;
  1727. ret = -EINVAL;
  1728. if (!S_ISREG(src->i_mode))
  1729. goto out;
  1730. ret = clone_verify_area(file, off, len, false);
  1731. if (ret < 0)
  1732. goto out;
  1733. ret = 0;
  1734. if (off + len > i_size_read(src))
  1735. return -EINVAL;
  1736. /* pre-format output fields to sane values */
  1737. for (i = 0; i < count; i++) {
  1738. same->info[i].bytes_deduped = 0ULL;
  1739. same->info[i].status = FILE_DEDUPE_RANGE_SAME;
  1740. }
  1741. for (i = 0, info = same->info; i < count; i++, info++) {
  1742. struct inode *dst;
  1743. struct fd dst_fd = fdget(info->dest_fd);
  1744. dst_file = dst_fd.file;
  1745. if (!dst_file) {
  1746. info->status = -EBADF;
  1747. goto next_loop;
  1748. }
  1749. dst = file_inode(dst_file);
  1750. ret = mnt_want_write_file(dst_file);
  1751. if (ret) {
  1752. info->status = ret;
  1753. goto next_fdput;
  1754. }
  1755. dst_off = info->dest_offset;
  1756. ret = clone_verify_area(dst_file, dst_off, len, true);
  1757. if (ret < 0) {
  1758. info->status = ret;
  1759. goto next_file;
  1760. }
  1761. ret = 0;
  1762. if (info->reserved) {
  1763. info->status = -EINVAL;
  1764. } else if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) {
  1765. info->status = -EINVAL;
  1766. } else if (file->f_path.mnt != dst_file->f_path.mnt) {
  1767. info->status = -EXDEV;
  1768. } else if (S_ISDIR(dst->i_mode)) {
  1769. info->status = -EISDIR;
  1770. } else if (dst_file->f_op->dedupe_file_range == NULL) {
  1771. info->status = -EINVAL;
  1772. } else {
  1773. deduped = dst_file->f_op->dedupe_file_range(file, off,
  1774. len, dst_file,
  1775. info->dest_offset);
  1776. if (deduped == -EBADE)
  1777. info->status = FILE_DEDUPE_RANGE_DIFFERS;
  1778. else if (deduped < 0)
  1779. info->status = deduped;
  1780. else
  1781. info->bytes_deduped += deduped;
  1782. }
  1783. next_file:
  1784. mnt_drop_write_file(dst_file);
  1785. next_fdput:
  1786. fdput(dst_fd);
  1787. next_loop:
  1788. if (fatal_signal_pending(current))
  1789. goto out;
  1790. }
  1791. out:
  1792. return ret;
  1793. }
  1794. EXPORT_SYMBOL(vfs_dedupe_file_range);