read_write.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/read_write.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/stat.h>
  9. #include <linux/sched/xacct.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/file.h>
  12. #include <linux/uio.h>
  13. #include <linux/fsnotify.h>
  14. #include <linux/security.h>
  15. #include <linux/export.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/splice.h>
  19. #include <linux/compat.h>
  20. #include <linux/mount.h>
  21. #include <linux/fs.h>
  22. #include "internal.h"
  23. #include <linux/uaccess.h>
  24. #include <asm/unistd.h>
  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 bool 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 ((unsigned long long)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 ((unsigned long long)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 default_llseek(struct file *file, loff_t offset, int whence)
  215. {
  216. struct inode *inode = file_inode(file);
  217. loff_t retval;
  218. inode_lock(inode);
  219. switch (whence) {
  220. case SEEK_END:
  221. offset += i_size_read(inode);
  222. break;
  223. case SEEK_CUR:
  224. if (offset == 0) {
  225. retval = file->f_pos;
  226. goto out;
  227. }
  228. offset += file->f_pos;
  229. break;
  230. case SEEK_DATA:
  231. /*
  232. * In the generic case the entire file is data, so as
  233. * long as offset isn't at the end of the file then the
  234. * offset is data.
  235. */
  236. if (offset >= inode->i_size) {
  237. retval = -ENXIO;
  238. goto out;
  239. }
  240. break;
  241. case SEEK_HOLE:
  242. /*
  243. * There is a virtual hole at the end of the file, so
  244. * as long as offset isn't i_size or larger, return
  245. * i_size.
  246. */
  247. if (offset >= inode->i_size) {
  248. retval = -ENXIO;
  249. goto out;
  250. }
  251. offset = inode->i_size;
  252. break;
  253. }
  254. retval = -EINVAL;
  255. if (offset >= 0 || unsigned_offsets(file)) {
  256. if (offset != file->f_pos) {
  257. file->f_pos = offset;
  258. file->f_version = 0;
  259. }
  260. retval = offset;
  261. }
  262. out:
  263. inode_unlock(inode);
  264. return retval;
  265. }
  266. EXPORT_SYMBOL(default_llseek);
  267. loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
  268. {
  269. if (!(file->f_mode & FMODE_LSEEK))
  270. return -ESPIPE;
  271. return file->f_op->llseek(file, offset, whence);
  272. }
  273. EXPORT_SYMBOL(vfs_llseek);
  274. static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
  275. {
  276. off_t retval;
  277. struct fd f = fdget_pos(fd);
  278. if (!f.file)
  279. return -EBADF;
  280. retval = -EINVAL;
  281. if (whence <= SEEK_MAX) {
  282. loff_t res = vfs_llseek(f.file, offset, whence);
  283. retval = res;
  284. if (res != (loff_t)retval)
  285. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  286. }
  287. fdput_pos(f);
  288. return retval;
  289. }
  290. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  291. {
  292. return ksys_lseek(fd, offset, whence);
  293. }
  294. #ifdef CONFIG_COMPAT
  295. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  296. {
  297. return ksys_lseek(fd, offset, whence);
  298. }
  299. #endif
  300. #if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \
  301. defined(__ARCH_WANT_SYS_LLSEEK)
  302. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  303. unsigned long, offset_low, loff_t __user *, result,
  304. unsigned int, whence)
  305. {
  306. int retval;
  307. struct fd f = fdget_pos(fd);
  308. loff_t offset;
  309. if (!f.file)
  310. return -EBADF;
  311. retval = -EINVAL;
  312. if (whence > SEEK_MAX)
  313. goto out_putf;
  314. offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
  315. whence);
  316. retval = (int)offset;
  317. if (offset >= 0) {
  318. retval = -EFAULT;
  319. if (!copy_to_user(result, &offset, sizeof(offset)))
  320. retval = 0;
  321. }
  322. out_putf:
  323. fdput_pos(f);
  324. return retval;
  325. }
  326. #endif
  327. int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
  328. {
  329. if (unlikely((ssize_t) count < 0))
  330. return -EINVAL;
  331. if (ppos) {
  332. loff_t pos = *ppos;
  333. if (unlikely(pos < 0)) {
  334. if (!unsigned_offsets(file))
  335. return -EINVAL;
  336. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  337. return -EOVERFLOW;
  338. } else if (unlikely((loff_t) (pos + count) < 0)) {
  339. if (!unsigned_offsets(file))
  340. return -EINVAL;
  341. }
  342. }
  343. return security_file_permission(file,
  344. read_write == READ ? MAY_READ : MAY_WRITE);
  345. }
  346. EXPORT_SYMBOL(rw_verify_area);
  347. static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  348. {
  349. struct kiocb kiocb;
  350. struct iov_iter iter;
  351. ssize_t ret;
  352. init_sync_kiocb(&kiocb, filp);
  353. kiocb.ki_pos = (ppos ? *ppos : 0);
  354. iov_iter_ubuf(&iter, ITER_DEST, buf, len);
  355. ret = call_read_iter(filp, &kiocb, &iter);
  356. BUG_ON(ret == -EIOCBQUEUED);
  357. if (ppos)
  358. *ppos = kiocb.ki_pos;
  359. return ret;
  360. }
  361. static int warn_unsupported(struct file *file, const char *op)
  362. {
  363. pr_warn_ratelimited(
  364. "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
  365. op, file, current->pid, current->comm);
  366. return -EINVAL;
  367. }
  368. ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
  369. {
  370. struct kvec iov = {
  371. .iov_base = buf,
  372. .iov_len = min_t(size_t, count, MAX_RW_COUNT),
  373. };
  374. struct kiocb kiocb;
  375. struct iov_iter iter;
  376. ssize_t ret;
  377. if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
  378. return -EINVAL;
  379. if (!(file->f_mode & FMODE_CAN_READ))
  380. return -EINVAL;
  381. /*
  382. * Also fail if ->read_iter and ->read are both wired up as that
  383. * implies very convoluted semantics.
  384. */
  385. if (unlikely(!file->f_op->read_iter || file->f_op->read))
  386. return warn_unsupported(file, "read");
  387. init_sync_kiocb(&kiocb, file);
  388. kiocb.ki_pos = pos ? *pos : 0;
  389. iov_iter_kvec(&iter, ITER_DEST, &iov, 1, iov.iov_len);
  390. ret = file->f_op->read_iter(&kiocb, &iter);
  391. if (ret > 0) {
  392. if (pos)
  393. *pos = kiocb.ki_pos;
  394. fsnotify_access(file);
  395. add_rchar(current, ret);
  396. }
  397. inc_syscr(current);
  398. return ret;
  399. }
  400. ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
  401. {
  402. ssize_t ret;
  403. ret = rw_verify_area(READ, file, pos, count);
  404. if (ret)
  405. return ret;
  406. return __kernel_read(file, buf, count, pos);
  407. }
  408. EXPORT_SYMBOL(kernel_read);
  409. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  410. {
  411. ssize_t ret;
  412. if (!(file->f_mode & FMODE_READ))
  413. return -EBADF;
  414. if (!(file->f_mode & FMODE_CAN_READ))
  415. return -EINVAL;
  416. if (unlikely(!access_ok(buf, count)))
  417. return -EFAULT;
  418. ret = rw_verify_area(READ, file, pos, count);
  419. if (ret)
  420. return ret;
  421. if (count > MAX_RW_COUNT)
  422. count = MAX_RW_COUNT;
  423. if (file->f_op->read)
  424. ret = file->f_op->read(file, buf, count, pos);
  425. else if (file->f_op->read_iter)
  426. ret = new_sync_read(file, buf, count, pos);
  427. else
  428. ret = -EINVAL;
  429. if (ret > 0) {
  430. fsnotify_access(file);
  431. add_rchar(current, ret);
  432. }
  433. inc_syscr(current);
  434. return ret;
  435. }
  436. static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  437. {
  438. struct kiocb kiocb;
  439. struct iov_iter iter;
  440. ssize_t ret;
  441. init_sync_kiocb(&kiocb, filp);
  442. kiocb.ki_pos = (ppos ? *ppos : 0);
  443. iov_iter_ubuf(&iter, ITER_SOURCE, (void __user *)buf, len);
  444. ret = call_write_iter(filp, &kiocb, &iter);
  445. BUG_ON(ret == -EIOCBQUEUED);
  446. if (ret > 0 && ppos)
  447. *ppos = kiocb.ki_pos;
  448. return ret;
  449. }
  450. /* caller is responsible for file_start_write/file_end_write */
  451. ssize_t __kernel_write_iter(struct file *file, struct iov_iter *from, loff_t *pos)
  452. {
  453. struct kiocb kiocb;
  454. ssize_t ret;
  455. if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
  456. return -EBADF;
  457. if (!(file->f_mode & FMODE_CAN_WRITE))
  458. return -EINVAL;
  459. /*
  460. * Also fail if ->write_iter and ->write are both wired up as that
  461. * implies very convoluted semantics.
  462. */
  463. if (unlikely(!file->f_op->write_iter || file->f_op->write))
  464. return warn_unsupported(file, "write");
  465. init_sync_kiocb(&kiocb, file);
  466. kiocb.ki_pos = pos ? *pos : 0;
  467. ret = file->f_op->write_iter(&kiocb, from);
  468. if (ret > 0) {
  469. if (pos)
  470. *pos = kiocb.ki_pos;
  471. fsnotify_modify(file);
  472. add_wchar(current, ret);
  473. }
  474. inc_syscw(current);
  475. return ret;
  476. }
  477. /* caller is responsible for file_start_write/file_end_write */
  478. ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
  479. {
  480. struct kvec iov = {
  481. .iov_base = (void *)buf,
  482. .iov_len = min_t(size_t, count, MAX_RW_COUNT),
  483. };
  484. struct iov_iter iter;
  485. iov_iter_kvec(&iter, ITER_SOURCE, &iov, 1, iov.iov_len);
  486. return __kernel_write_iter(file, &iter, pos);
  487. }
  488. /*
  489. * This "EXPORT_SYMBOL_GPL()" is more of a "EXPORT_SYMBOL_DONTUSE()",
  490. * but autofs is one of the few internal kernel users that actually
  491. * wants this _and_ can be built as a module. So we need to export
  492. * this symbol for autofs, even though it really isn't appropriate
  493. * for any other kernel modules.
  494. */
  495. EXPORT_SYMBOL_GPL(__kernel_write);
  496. ssize_t kernel_write(struct file *file, const void *buf, size_t count,
  497. loff_t *pos)
  498. {
  499. ssize_t ret;
  500. ret = rw_verify_area(WRITE, file, pos, count);
  501. if (ret)
  502. return ret;
  503. file_start_write(file);
  504. ret = __kernel_write(file, buf, count, pos);
  505. file_end_write(file);
  506. return ret;
  507. }
  508. EXPORT_SYMBOL(kernel_write);
  509. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  510. {
  511. ssize_t ret;
  512. if (!(file->f_mode & FMODE_WRITE))
  513. return -EBADF;
  514. if (!(file->f_mode & FMODE_CAN_WRITE))
  515. return -EINVAL;
  516. if (unlikely(!access_ok(buf, count)))
  517. return -EFAULT;
  518. ret = rw_verify_area(WRITE, file, pos, count);
  519. if (ret)
  520. return ret;
  521. if (count > MAX_RW_COUNT)
  522. count = MAX_RW_COUNT;
  523. file_start_write(file);
  524. if (file->f_op->write)
  525. ret = file->f_op->write(file, buf, count, pos);
  526. else if (file->f_op->write_iter)
  527. ret = new_sync_write(file, buf, count, pos);
  528. else
  529. ret = -EINVAL;
  530. if (ret > 0) {
  531. fsnotify_modify(file);
  532. add_wchar(current, ret);
  533. }
  534. inc_syscw(current);
  535. file_end_write(file);
  536. return ret;
  537. }
  538. /* file_ppos returns &file->f_pos or NULL if file is stream */
  539. static inline loff_t *file_ppos(struct file *file)
  540. {
  541. return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
  542. }
  543. ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
  544. {
  545. struct fd f = fdget_pos(fd);
  546. ssize_t ret = -EBADF;
  547. if (f.file) {
  548. loff_t pos, *ppos = file_ppos(f.file);
  549. if (ppos) {
  550. pos = *ppos;
  551. ppos = &pos;
  552. }
  553. ret = vfs_read(f.file, buf, count, ppos);
  554. if (ret >= 0 && ppos)
  555. f.file->f_pos = pos;
  556. fdput_pos(f);
  557. }
  558. return ret;
  559. }
  560. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  561. {
  562. return ksys_read(fd, buf, count);
  563. }
  564. ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
  565. {
  566. struct fd f = fdget_pos(fd);
  567. ssize_t ret = -EBADF;
  568. if (f.file) {
  569. loff_t pos, *ppos = file_ppos(f.file);
  570. if (ppos) {
  571. pos = *ppos;
  572. ppos = &pos;
  573. }
  574. ret = vfs_write(f.file, buf, count, ppos);
  575. if (ret >= 0 && ppos)
  576. f.file->f_pos = pos;
  577. fdput_pos(f);
  578. }
  579. return ret;
  580. }
  581. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  582. size_t, count)
  583. {
  584. return ksys_write(fd, buf, count);
  585. }
  586. ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
  587. loff_t pos)
  588. {
  589. struct fd f;
  590. ssize_t ret = -EBADF;
  591. if (pos < 0)
  592. return -EINVAL;
  593. f = fdget(fd);
  594. if (f.file) {
  595. ret = -ESPIPE;
  596. if (f.file->f_mode & FMODE_PREAD)
  597. ret = vfs_read(f.file, buf, count, &pos);
  598. fdput(f);
  599. }
  600. return ret;
  601. }
  602. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  603. size_t, count, loff_t, pos)
  604. {
  605. return ksys_pread64(fd, buf, count, pos);
  606. }
  607. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PREAD64)
  608. COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf,
  609. size_t, count, compat_arg_u64_dual(pos))
  610. {
  611. return ksys_pread64(fd, buf, count, compat_arg_u64_glue(pos));
  612. }
  613. #endif
  614. ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
  615. size_t count, loff_t pos)
  616. {
  617. struct fd f;
  618. ssize_t ret = -EBADF;
  619. if (pos < 0)
  620. return -EINVAL;
  621. f = fdget(fd);
  622. if (f.file) {
  623. ret = -ESPIPE;
  624. if (f.file->f_mode & FMODE_PWRITE)
  625. ret = vfs_write(f.file, buf, count, &pos);
  626. fdput(f);
  627. }
  628. return ret;
  629. }
  630. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  631. size_t, count, loff_t, pos)
  632. {
  633. return ksys_pwrite64(fd, buf, count, pos);
  634. }
  635. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PWRITE64)
  636. COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, fd, const char __user *, buf,
  637. size_t, count, compat_arg_u64_dual(pos))
  638. {
  639. return ksys_pwrite64(fd, buf, count, compat_arg_u64_glue(pos));
  640. }
  641. #endif
  642. static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
  643. loff_t *ppos, int type, rwf_t flags)
  644. {
  645. struct kiocb kiocb;
  646. ssize_t ret;
  647. init_sync_kiocb(&kiocb, filp);
  648. ret = kiocb_set_rw_flags(&kiocb, flags);
  649. if (ret)
  650. return ret;
  651. kiocb.ki_pos = (ppos ? *ppos : 0);
  652. if (type == READ)
  653. ret = call_read_iter(filp, &kiocb, iter);
  654. else
  655. ret = call_write_iter(filp, &kiocb, iter);
  656. BUG_ON(ret == -EIOCBQUEUED);
  657. if (ppos)
  658. *ppos = kiocb.ki_pos;
  659. return ret;
  660. }
  661. /* Do it by hand, with file-ops */
  662. static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
  663. loff_t *ppos, int type, rwf_t flags)
  664. {
  665. ssize_t ret = 0;
  666. if (flags & ~RWF_HIPRI)
  667. return -EOPNOTSUPP;
  668. while (iov_iter_count(iter)) {
  669. struct iovec iovec = iov_iter_iovec(iter);
  670. ssize_t nr;
  671. if (type == READ) {
  672. nr = filp->f_op->read(filp, iovec.iov_base,
  673. iovec.iov_len, ppos);
  674. } else {
  675. nr = filp->f_op->write(filp, iovec.iov_base,
  676. iovec.iov_len, ppos);
  677. }
  678. if (nr < 0) {
  679. if (!ret)
  680. ret = nr;
  681. break;
  682. }
  683. ret += nr;
  684. if (nr != iovec.iov_len)
  685. break;
  686. iov_iter_advance(iter, nr);
  687. }
  688. return ret;
  689. }
  690. static ssize_t do_iter_read(struct file *file, struct iov_iter *iter,
  691. loff_t *pos, rwf_t flags)
  692. {
  693. size_t tot_len;
  694. ssize_t ret = 0;
  695. if (!(file->f_mode & FMODE_READ))
  696. return -EBADF;
  697. if (!(file->f_mode & FMODE_CAN_READ))
  698. return -EINVAL;
  699. tot_len = iov_iter_count(iter);
  700. if (!tot_len)
  701. goto out;
  702. ret = rw_verify_area(READ, file, pos, tot_len);
  703. if (ret < 0)
  704. return ret;
  705. if (file->f_op->read_iter)
  706. ret = do_iter_readv_writev(file, iter, pos, READ, flags);
  707. else
  708. ret = do_loop_readv_writev(file, iter, pos, READ, flags);
  709. out:
  710. if (ret >= 0)
  711. fsnotify_access(file);
  712. return ret;
  713. }
  714. ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
  715. struct iov_iter *iter)
  716. {
  717. size_t tot_len;
  718. ssize_t ret = 0;
  719. if (!file->f_op->read_iter)
  720. return -EINVAL;
  721. if (!(file->f_mode & FMODE_READ))
  722. return -EBADF;
  723. if (!(file->f_mode & FMODE_CAN_READ))
  724. return -EINVAL;
  725. tot_len = iov_iter_count(iter);
  726. if (!tot_len)
  727. goto out;
  728. ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
  729. if (ret < 0)
  730. return ret;
  731. ret = call_read_iter(file, iocb, iter);
  732. out:
  733. if (ret >= 0)
  734. fsnotify_access(file);
  735. return ret;
  736. }
  737. EXPORT_SYMBOL(vfs_iocb_iter_read);
  738. ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
  739. rwf_t flags)
  740. {
  741. if (!file->f_op->read_iter)
  742. return -EINVAL;
  743. return do_iter_read(file, iter, ppos, flags);
  744. }
  745. EXPORT_SYMBOL(vfs_iter_read);
  746. static ssize_t do_iter_write(struct file *file, struct iov_iter *iter,
  747. loff_t *pos, rwf_t flags)
  748. {
  749. size_t tot_len;
  750. ssize_t ret = 0;
  751. if (!(file->f_mode & FMODE_WRITE))
  752. return -EBADF;
  753. if (!(file->f_mode & FMODE_CAN_WRITE))
  754. return -EINVAL;
  755. tot_len = iov_iter_count(iter);
  756. if (!tot_len)
  757. return 0;
  758. ret = rw_verify_area(WRITE, file, pos, tot_len);
  759. if (ret < 0)
  760. return ret;
  761. if (file->f_op->write_iter)
  762. ret = do_iter_readv_writev(file, iter, pos, WRITE, flags);
  763. else
  764. ret = do_loop_readv_writev(file, iter, pos, WRITE, flags);
  765. if (ret > 0)
  766. fsnotify_modify(file);
  767. return ret;
  768. }
  769. ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
  770. struct iov_iter *iter)
  771. {
  772. size_t tot_len;
  773. ssize_t ret = 0;
  774. if (!file->f_op->write_iter)
  775. return -EINVAL;
  776. if (!(file->f_mode & FMODE_WRITE))
  777. return -EBADF;
  778. if (!(file->f_mode & FMODE_CAN_WRITE))
  779. return -EINVAL;
  780. tot_len = iov_iter_count(iter);
  781. if (!tot_len)
  782. return 0;
  783. ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
  784. if (ret < 0)
  785. return ret;
  786. ret = call_write_iter(file, iocb, iter);
  787. if (ret > 0)
  788. fsnotify_modify(file);
  789. return ret;
  790. }
  791. EXPORT_SYMBOL(vfs_iocb_iter_write);
  792. ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
  793. rwf_t flags)
  794. {
  795. if (!file->f_op->write_iter)
  796. return -EINVAL;
  797. return do_iter_write(file, iter, ppos, flags);
  798. }
  799. EXPORT_SYMBOL(vfs_iter_write);
  800. static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  801. unsigned long vlen, loff_t *pos, rwf_t flags)
  802. {
  803. struct iovec iovstack[UIO_FASTIOV];
  804. struct iovec *iov = iovstack;
  805. struct iov_iter iter;
  806. ssize_t ret;
  807. ret = import_iovec(ITER_DEST, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
  808. if (ret >= 0) {
  809. ret = do_iter_read(file, &iter, pos, flags);
  810. kfree(iov);
  811. }
  812. return ret;
  813. }
  814. static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  815. unsigned long vlen, loff_t *pos, rwf_t flags)
  816. {
  817. struct iovec iovstack[UIO_FASTIOV];
  818. struct iovec *iov = iovstack;
  819. struct iov_iter iter;
  820. ssize_t ret;
  821. ret = import_iovec(ITER_SOURCE, vec, vlen, ARRAY_SIZE(iovstack), &iov, &iter);
  822. if (ret >= 0) {
  823. file_start_write(file);
  824. ret = do_iter_write(file, &iter, pos, flags);
  825. file_end_write(file);
  826. kfree(iov);
  827. }
  828. return ret;
  829. }
  830. static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
  831. unsigned long vlen, rwf_t flags)
  832. {
  833. struct fd f = fdget_pos(fd);
  834. ssize_t ret = -EBADF;
  835. if (f.file) {
  836. loff_t pos, *ppos = file_ppos(f.file);
  837. if (ppos) {
  838. pos = *ppos;
  839. ppos = &pos;
  840. }
  841. ret = vfs_readv(f.file, vec, vlen, ppos, flags);
  842. if (ret >= 0 && ppos)
  843. f.file->f_pos = pos;
  844. fdput_pos(f);
  845. }
  846. if (ret > 0)
  847. add_rchar(current, ret);
  848. inc_syscr(current);
  849. return ret;
  850. }
  851. static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
  852. unsigned long vlen, rwf_t flags)
  853. {
  854. struct fd f = fdget_pos(fd);
  855. ssize_t ret = -EBADF;
  856. if (f.file) {
  857. loff_t pos, *ppos = file_ppos(f.file);
  858. if (ppos) {
  859. pos = *ppos;
  860. ppos = &pos;
  861. }
  862. ret = vfs_writev(f.file, vec, vlen, ppos, flags);
  863. if (ret >= 0 && ppos)
  864. f.file->f_pos = pos;
  865. fdput_pos(f);
  866. }
  867. if (ret > 0)
  868. add_wchar(current, ret);
  869. inc_syscw(current);
  870. return ret;
  871. }
  872. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  873. {
  874. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  875. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  876. }
  877. static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
  878. unsigned long vlen, loff_t pos, rwf_t flags)
  879. {
  880. struct fd f;
  881. ssize_t ret = -EBADF;
  882. if (pos < 0)
  883. return -EINVAL;
  884. f = fdget(fd);
  885. if (f.file) {
  886. ret = -ESPIPE;
  887. if (f.file->f_mode & FMODE_PREAD)
  888. ret = vfs_readv(f.file, vec, vlen, &pos, flags);
  889. fdput(f);
  890. }
  891. if (ret > 0)
  892. add_rchar(current, ret);
  893. inc_syscr(current);
  894. return ret;
  895. }
  896. static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
  897. unsigned long vlen, loff_t pos, rwf_t flags)
  898. {
  899. struct fd f;
  900. ssize_t ret = -EBADF;
  901. if (pos < 0)
  902. return -EINVAL;
  903. f = fdget(fd);
  904. if (f.file) {
  905. ret = -ESPIPE;
  906. if (f.file->f_mode & FMODE_PWRITE)
  907. ret = vfs_writev(f.file, vec, vlen, &pos, flags);
  908. fdput(f);
  909. }
  910. if (ret > 0)
  911. add_wchar(current, ret);
  912. inc_syscw(current);
  913. return ret;
  914. }
  915. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  916. unsigned long, vlen)
  917. {
  918. return do_readv(fd, vec, vlen, 0);
  919. }
  920. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  921. unsigned long, vlen)
  922. {
  923. return do_writev(fd, vec, vlen, 0);
  924. }
  925. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  926. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  927. {
  928. loff_t pos = pos_from_hilo(pos_h, pos_l);
  929. return do_preadv(fd, vec, vlen, pos, 0);
  930. }
  931. SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
  932. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  933. rwf_t, flags)
  934. {
  935. loff_t pos = pos_from_hilo(pos_h, pos_l);
  936. if (pos == -1)
  937. return do_readv(fd, vec, vlen, flags);
  938. return do_preadv(fd, vec, vlen, pos, flags);
  939. }
  940. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  941. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  942. {
  943. loff_t pos = pos_from_hilo(pos_h, pos_l);
  944. return do_pwritev(fd, vec, vlen, pos, 0);
  945. }
  946. SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
  947. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  948. rwf_t, flags)
  949. {
  950. loff_t pos = pos_from_hilo(pos_h, pos_l);
  951. if (pos == -1)
  952. return do_writev(fd, vec, vlen, flags);
  953. return do_pwritev(fd, vec, vlen, pos, flags);
  954. }
  955. /*
  956. * Various compat syscalls. Note that they all pretend to take a native
  957. * iovec - import_iovec will properly treat those as compat_iovecs based on
  958. * in_compat_syscall().
  959. */
  960. #ifdef CONFIG_COMPAT
  961. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
  962. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  963. const struct iovec __user *, vec,
  964. unsigned long, vlen, loff_t, pos)
  965. {
  966. return do_preadv(fd, vec, vlen, pos, 0);
  967. }
  968. #endif
  969. COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
  970. const struct iovec __user *, vec,
  971. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  972. {
  973. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  974. return do_preadv(fd, vec, vlen, pos, 0);
  975. }
  976. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
  977. COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
  978. const struct iovec __user *, vec,
  979. unsigned long, vlen, loff_t, pos, rwf_t, flags)
  980. {
  981. if (pos == -1)
  982. return do_readv(fd, vec, vlen, flags);
  983. return do_preadv(fd, vec, vlen, pos, flags);
  984. }
  985. #endif
  986. COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
  987. const struct iovec __user *, vec,
  988. compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
  989. rwf_t, flags)
  990. {
  991. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  992. if (pos == -1)
  993. return do_readv(fd, vec, vlen, flags);
  994. return do_preadv(fd, vec, vlen, pos, flags);
  995. }
  996. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
  997. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  998. const struct iovec __user *, vec,
  999. unsigned long, vlen, loff_t, pos)
  1000. {
  1001. return do_pwritev(fd, vec, vlen, pos, 0);
  1002. }
  1003. #endif
  1004. COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
  1005. const struct iovec __user *,vec,
  1006. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1007. {
  1008. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1009. return do_pwritev(fd, vec, vlen, pos, 0);
  1010. }
  1011. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
  1012. COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
  1013. const struct iovec __user *, vec,
  1014. unsigned long, vlen, loff_t, pos, rwf_t, flags)
  1015. {
  1016. if (pos == -1)
  1017. return do_writev(fd, vec, vlen, flags);
  1018. return do_pwritev(fd, vec, vlen, pos, flags);
  1019. }
  1020. #endif
  1021. COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
  1022. const struct iovec __user *,vec,
  1023. compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
  1024. {
  1025. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1026. if (pos == -1)
  1027. return do_writev(fd, vec, vlen, flags);
  1028. return do_pwritev(fd, vec, vlen, pos, flags);
  1029. }
  1030. #endif /* CONFIG_COMPAT */
  1031. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  1032. size_t count, loff_t max)
  1033. {
  1034. struct fd in, out;
  1035. struct inode *in_inode, *out_inode;
  1036. struct pipe_inode_info *opipe;
  1037. loff_t pos;
  1038. loff_t out_pos;
  1039. ssize_t retval;
  1040. int fl;
  1041. /*
  1042. * Get input file, and verify that it is ok..
  1043. */
  1044. retval = -EBADF;
  1045. in = fdget(in_fd);
  1046. if (!in.file)
  1047. goto out;
  1048. if (!(in.file->f_mode & FMODE_READ))
  1049. goto fput_in;
  1050. retval = -ESPIPE;
  1051. if (!ppos) {
  1052. pos = in.file->f_pos;
  1053. } else {
  1054. pos = *ppos;
  1055. if (!(in.file->f_mode & FMODE_PREAD))
  1056. goto fput_in;
  1057. }
  1058. retval = rw_verify_area(READ, in.file, &pos, count);
  1059. if (retval < 0)
  1060. goto fput_in;
  1061. if (count > MAX_RW_COUNT)
  1062. count = MAX_RW_COUNT;
  1063. /*
  1064. * Get output file, and verify that it is ok..
  1065. */
  1066. retval = -EBADF;
  1067. out = fdget(out_fd);
  1068. if (!out.file)
  1069. goto fput_in;
  1070. if (!(out.file->f_mode & FMODE_WRITE))
  1071. goto fput_out;
  1072. in_inode = file_inode(in.file);
  1073. out_inode = file_inode(out.file);
  1074. out_pos = out.file->f_pos;
  1075. if (!max)
  1076. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  1077. if (unlikely(pos + count > max)) {
  1078. retval = -EOVERFLOW;
  1079. if (pos >= max)
  1080. goto fput_out;
  1081. count = max - pos;
  1082. }
  1083. fl = 0;
  1084. #if 0
  1085. /*
  1086. * We need to debate whether we can enable this or not. The
  1087. * man page documents EAGAIN return for the output at least,
  1088. * and the application is arguably buggy if it doesn't expect
  1089. * EAGAIN on a non-blocking file descriptor.
  1090. */
  1091. if (in.file->f_flags & O_NONBLOCK)
  1092. fl = SPLICE_F_NONBLOCK;
  1093. #endif
  1094. opipe = get_pipe_info(out.file, true);
  1095. if (!opipe) {
  1096. retval = rw_verify_area(WRITE, out.file, &out_pos, count);
  1097. if (retval < 0)
  1098. goto fput_out;
  1099. file_start_write(out.file);
  1100. retval = do_splice_direct(in.file, &pos, out.file, &out_pos,
  1101. count, fl);
  1102. file_end_write(out.file);
  1103. } else {
  1104. if (out.file->f_flags & O_NONBLOCK)
  1105. fl |= SPLICE_F_NONBLOCK;
  1106. retval = splice_file_to_pipe(in.file, opipe, &pos, count, fl);
  1107. }
  1108. if (retval > 0) {
  1109. add_rchar(current, retval);
  1110. add_wchar(current, retval);
  1111. fsnotify_access(in.file);
  1112. fsnotify_modify(out.file);
  1113. out.file->f_pos = out_pos;
  1114. if (ppos)
  1115. *ppos = pos;
  1116. else
  1117. in.file->f_pos = pos;
  1118. }
  1119. inc_syscr(current);
  1120. inc_syscw(current);
  1121. if (pos > max)
  1122. retval = -EOVERFLOW;
  1123. fput_out:
  1124. fdput(out);
  1125. fput_in:
  1126. fdput(in);
  1127. out:
  1128. return retval;
  1129. }
  1130. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  1131. {
  1132. loff_t pos;
  1133. off_t off;
  1134. ssize_t ret;
  1135. if (offset) {
  1136. if (unlikely(get_user(off, offset)))
  1137. return -EFAULT;
  1138. pos = off;
  1139. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1140. if (unlikely(put_user(pos, offset)))
  1141. return -EFAULT;
  1142. return ret;
  1143. }
  1144. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1145. }
  1146. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  1147. {
  1148. loff_t pos;
  1149. ssize_t ret;
  1150. if (offset) {
  1151. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1152. return -EFAULT;
  1153. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1154. if (unlikely(put_user(pos, offset)))
  1155. return -EFAULT;
  1156. return ret;
  1157. }
  1158. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1159. }
  1160. #ifdef CONFIG_COMPAT
  1161. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  1162. compat_off_t __user *, offset, compat_size_t, count)
  1163. {
  1164. loff_t pos;
  1165. off_t off;
  1166. ssize_t ret;
  1167. if (offset) {
  1168. if (unlikely(get_user(off, offset)))
  1169. return -EFAULT;
  1170. pos = off;
  1171. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1172. if (unlikely(put_user(pos, offset)))
  1173. return -EFAULT;
  1174. return ret;
  1175. }
  1176. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1177. }
  1178. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  1179. compat_loff_t __user *, offset, compat_size_t, count)
  1180. {
  1181. loff_t pos;
  1182. ssize_t ret;
  1183. if (offset) {
  1184. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1185. return -EFAULT;
  1186. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1187. if (unlikely(put_user(pos, offset)))
  1188. return -EFAULT;
  1189. return ret;
  1190. }
  1191. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1192. }
  1193. #endif
  1194. /**
  1195. * generic_copy_file_range - copy data between two files
  1196. * @file_in: file structure to read from
  1197. * @pos_in: file offset to read from
  1198. * @file_out: file structure to write data to
  1199. * @pos_out: file offset to write data to
  1200. * @len: amount of data to copy
  1201. * @flags: copy flags
  1202. *
  1203. * This is a generic filesystem helper to copy data from one file to another.
  1204. * It has no constraints on the source or destination file owners - the files
  1205. * can belong to different superblocks and different filesystem types. Short
  1206. * copies are allowed.
  1207. *
  1208. * This should be called from the @file_out filesystem, as per the
  1209. * ->copy_file_range() method.
  1210. *
  1211. * Returns the number of bytes copied or a negative error indicating the
  1212. * failure.
  1213. */
  1214. ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
  1215. struct file *file_out, loff_t pos_out,
  1216. size_t len, unsigned int flags)
  1217. {
  1218. lockdep_assert(sb_write_started(file_inode(file_out)->i_sb));
  1219. return do_splice_direct(file_in, &pos_in, file_out, &pos_out,
  1220. len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
  1221. }
  1222. EXPORT_SYMBOL(generic_copy_file_range);
  1223. /*
  1224. * Performs necessary checks before doing a file copy
  1225. *
  1226. * Can adjust amount of bytes to copy via @req_count argument.
  1227. * Returns appropriate error code that caller should return or
  1228. * zero in case the copy should be allowed.
  1229. */
  1230. static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
  1231. struct file *file_out, loff_t pos_out,
  1232. size_t *req_count, unsigned int flags)
  1233. {
  1234. struct inode *inode_in = file_inode(file_in);
  1235. struct inode *inode_out = file_inode(file_out);
  1236. uint64_t count = *req_count;
  1237. loff_t size_in;
  1238. int ret;
  1239. ret = generic_file_rw_checks(file_in, file_out);
  1240. if (ret)
  1241. return ret;
  1242. /*
  1243. * We allow some filesystems to handle cross sb copy, but passing
  1244. * a file of the wrong filesystem type to filesystem driver can result
  1245. * in an attempt to dereference the wrong type of ->private_data, so
  1246. * avoid doing that until we really have a good reason.
  1247. *
  1248. * nfs and cifs define several different file_system_type structures
  1249. * and several different sets of file_operations, but they all end up
  1250. * using the same ->copy_file_range() function pointer.
  1251. */
  1252. if (flags & COPY_FILE_SPLICE) {
  1253. /* cross sb splice is allowed */
  1254. } else if (file_out->f_op->copy_file_range) {
  1255. if (file_in->f_op->copy_file_range !=
  1256. file_out->f_op->copy_file_range)
  1257. return -EXDEV;
  1258. } else if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) {
  1259. return -EXDEV;
  1260. }
  1261. /* Don't touch certain kinds of inodes */
  1262. if (IS_IMMUTABLE(inode_out))
  1263. return -EPERM;
  1264. if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
  1265. return -ETXTBSY;
  1266. /* Ensure offsets don't wrap. */
  1267. if (pos_in + count < pos_in || pos_out + count < pos_out)
  1268. return -EOVERFLOW;
  1269. /* Shorten the copy to EOF */
  1270. size_in = i_size_read(inode_in);
  1271. if (pos_in >= size_in)
  1272. count = 0;
  1273. else
  1274. count = min(count, size_in - (uint64_t)pos_in);
  1275. ret = generic_write_check_limits(file_out, pos_out, &count);
  1276. if (ret)
  1277. return ret;
  1278. /* Don't allow overlapped copying within the same file. */
  1279. if (inode_in == inode_out &&
  1280. pos_out + count > pos_in &&
  1281. pos_out < pos_in + count)
  1282. return -EINVAL;
  1283. *req_count = count;
  1284. return 0;
  1285. }
  1286. /*
  1287. * copy_file_range() differs from regular file read and write in that it
  1288. * specifically allows return partial success. When it does so is up to
  1289. * the copy_file_range method.
  1290. */
  1291. ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
  1292. struct file *file_out, loff_t pos_out,
  1293. size_t len, unsigned int flags)
  1294. {
  1295. ssize_t ret;
  1296. bool splice = flags & COPY_FILE_SPLICE;
  1297. if (flags & ~COPY_FILE_SPLICE)
  1298. return -EINVAL;
  1299. ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
  1300. flags);
  1301. if (unlikely(ret))
  1302. return ret;
  1303. ret = rw_verify_area(READ, file_in, &pos_in, len);
  1304. if (unlikely(ret))
  1305. return ret;
  1306. ret = rw_verify_area(WRITE, file_out, &pos_out, len);
  1307. if (unlikely(ret))
  1308. return ret;
  1309. if (len == 0)
  1310. return 0;
  1311. file_start_write(file_out);
  1312. /*
  1313. * Cloning is supported by more file systems, so we implement copy on
  1314. * same sb using clone, but for filesystems where both clone and copy
  1315. * are supported (e.g. nfs,cifs), we only call the copy method.
  1316. */
  1317. if (!splice && file_out->f_op->copy_file_range) {
  1318. ret = file_out->f_op->copy_file_range(file_in, pos_in,
  1319. file_out, pos_out,
  1320. len, flags);
  1321. goto done;
  1322. }
  1323. if (!splice && file_in->f_op->remap_file_range &&
  1324. file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
  1325. ret = file_in->f_op->remap_file_range(file_in, pos_in,
  1326. file_out, pos_out,
  1327. min_t(loff_t, MAX_RW_COUNT, len),
  1328. REMAP_FILE_CAN_SHORTEN);
  1329. if (ret > 0)
  1330. goto done;
  1331. }
  1332. /*
  1333. * We can get here for same sb copy of filesystems that do not implement
  1334. * ->copy_file_range() in case filesystem does not support clone or in
  1335. * case filesystem supports clone but rejected the clone request (e.g.
  1336. * because it was not block aligned).
  1337. *
  1338. * In both cases, fall back to kernel copy so we are able to maintain a
  1339. * consistent story about which filesystems support copy_file_range()
  1340. * and which filesystems do not, that will allow userspace tools to
  1341. * make consistent desicions w.r.t using copy_file_range().
  1342. *
  1343. * We also get here if caller (e.g. nfsd) requested COPY_FILE_SPLICE.
  1344. */
  1345. ret = generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
  1346. flags);
  1347. done:
  1348. if (ret > 0) {
  1349. fsnotify_access(file_in);
  1350. add_rchar(current, ret);
  1351. fsnotify_modify(file_out);
  1352. add_wchar(current, ret);
  1353. }
  1354. inc_syscr(current);
  1355. inc_syscw(current);
  1356. file_end_write(file_out);
  1357. return ret;
  1358. }
  1359. EXPORT_SYMBOL(vfs_copy_file_range);
  1360. SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
  1361. int, fd_out, loff_t __user *, off_out,
  1362. size_t, len, unsigned int, flags)
  1363. {
  1364. loff_t pos_in;
  1365. loff_t pos_out;
  1366. struct fd f_in;
  1367. struct fd f_out;
  1368. ssize_t ret = -EBADF;
  1369. f_in = fdget(fd_in);
  1370. if (!f_in.file)
  1371. goto out2;
  1372. f_out = fdget(fd_out);
  1373. if (!f_out.file)
  1374. goto out1;
  1375. ret = -EFAULT;
  1376. if (off_in) {
  1377. if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
  1378. goto out;
  1379. } else {
  1380. pos_in = f_in.file->f_pos;
  1381. }
  1382. if (off_out) {
  1383. if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
  1384. goto out;
  1385. } else {
  1386. pos_out = f_out.file->f_pos;
  1387. }
  1388. ret = -EINVAL;
  1389. if (flags != 0)
  1390. goto out;
  1391. ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
  1392. flags);
  1393. if (ret > 0) {
  1394. pos_in += ret;
  1395. pos_out += ret;
  1396. if (off_in) {
  1397. if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
  1398. ret = -EFAULT;
  1399. } else {
  1400. f_in.file->f_pos = pos_in;
  1401. }
  1402. if (off_out) {
  1403. if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
  1404. ret = -EFAULT;
  1405. } else {
  1406. f_out.file->f_pos = pos_out;
  1407. }
  1408. }
  1409. out:
  1410. fdput(f_out);
  1411. out1:
  1412. fdput(f_in);
  1413. out2:
  1414. return ret;
  1415. }
  1416. /*
  1417. * Don't operate on ranges the page cache doesn't support, and don't exceed the
  1418. * LFS limits. If pos is under the limit it becomes a short access. If it
  1419. * exceeds the limit we return -EFBIG.
  1420. */
  1421. int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
  1422. {
  1423. struct inode *inode = file->f_mapping->host;
  1424. loff_t max_size = inode->i_sb->s_maxbytes;
  1425. loff_t limit = rlimit(RLIMIT_FSIZE);
  1426. if (limit != RLIM_INFINITY) {
  1427. if (pos >= limit) {
  1428. send_sig(SIGXFSZ, current, 0);
  1429. return -EFBIG;
  1430. }
  1431. *count = min(*count, limit - pos);
  1432. }
  1433. if (!(file->f_flags & O_LARGEFILE))
  1434. max_size = MAX_NON_LFS;
  1435. if (unlikely(pos >= max_size))
  1436. return -EFBIG;
  1437. *count = min(*count, max_size - pos);
  1438. return 0;
  1439. }
  1440. /* Like generic_write_checks(), but takes size of write instead of iter. */
  1441. int generic_write_checks_count(struct kiocb *iocb, loff_t *count)
  1442. {
  1443. struct file *file = iocb->ki_filp;
  1444. struct inode *inode = file->f_mapping->host;
  1445. if (IS_SWAPFILE(inode))
  1446. return -ETXTBSY;
  1447. if (!*count)
  1448. return 0;
  1449. if (iocb->ki_flags & IOCB_APPEND)
  1450. iocb->ki_pos = i_size_read(inode);
  1451. if ((iocb->ki_flags & IOCB_NOWAIT) &&
  1452. !((iocb->ki_flags & IOCB_DIRECT) ||
  1453. (file->f_mode & FMODE_BUF_WASYNC)))
  1454. return -EINVAL;
  1455. return generic_write_check_limits(iocb->ki_filp, iocb->ki_pos, count);
  1456. }
  1457. EXPORT_SYMBOL(generic_write_checks_count);
  1458. /*
  1459. * Performs necessary checks before doing a write
  1460. *
  1461. * Can adjust writing position or amount of bytes to write.
  1462. * Returns appropriate error code that caller should return or
  1463. * zero in case that write should be allowed.
  1464. */
  1465. ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
  1466. {
  1467. loff_t count = iov_iter_count(from);
  1468. int ret;
  1469. ret = generic_write_checks_count(iocb, &count);
  1470. if (ret)
  1471. return ret;
  1472. iov_iter_truncate(from, count);
  1473. return iov_iter_count(from);
  1474. }
  1475. EXPORT_SYMBOL(generic_write_checks);
  1476. /*
  1477. * Performs common checks before doing a file copy/clone
  1478. * from @file_in to @file_out.
  1479. */
  1480. int generic_file_rw_checks(struct file *file_in, struct file *file_out)
  1481. {
  1482. struct inode *inode_in = file_inode(file_in);
  1483. struct inode *inode_out = file_inode(file_out);
  1484. /* Don't copy dirs, pipes, sockets... */
  1485. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1486. return -EISDIR;
  1487. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1488. return -EINVAL;
  1489. if (!(file_in->f_mode & FMODE_READ) ||
  1490. !(file_out->f_mode & FMODE_WRITE) ||
  1491. (file_out->f_flags & O_APPEND))
  1492. return -EBADF;
  1493. return 0;
  1494. }