read_write.c 46 KB

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