read_write.c 50 KB

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