read_write.c 47 KB

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