read_write.c 47 KB

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