read_write.c 47 KB

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