read_write.c 39 KB

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