read_write.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  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 <linux/filelock.h>
  23. #include "internal.h"
  24. #include <linux/uaccess.h>
  25. #include <asm/unistd.h>
  26. const struct file_operations generic_ro_fops = {
  27. .llseek = generic_file_llseek,
  28. .read_iter = generic_file_read_iter,
  29. .mmap_prepare = generic_file_readonly_mmap_prepare,
  30. .splice_read = filemap_splice_read,
  31. .setlease = generic_setlease,
  32. };
  33. EXPORT_SYMBOL(generic_ro_fops);
  34. static inline bool unsigned_offsets(struct file *file)
  35. {
  36. return file->f_op->fop_flags & FOP_UNSIGNED_OFFSET;
  37. }
  38. /**
  39. * vfs_setpos_cookie - update the file offset for lseek and reset cookie
  40. * @file: file structure in question
  41. * @offset: file offset to seek to
  42. * @maxsize: maximum file size
  43. * @cookie: cookie to reset
  44. *
  45. * Update the file offset to the value specified by @offset if the given
  46. * offset is valid and it is not equal to the current file offset and
  47. * reset the specified cookie to indicate that a seek happened.
  48. *
  49. * Return the specified offset on success and -EINVAL on invalid offset.
  50. */
  51. static loff_t vfs_setpos_cookie(struct file *file, loff_t offset,
  52. loff_t maxsize, u64 *cookie)
  53. {
  54. if (offset < 0 && !unsigned_offsets(file))
  55. return -EINVAL;
  56. if (offset > maxsize)
  57. return -EINVAL;
  58. if (offset != file->f_pos) {
  59. file->f_pos = offset;
  60. if (cookie)
  61. *cookie = 0;
  62. }
  63. return offset;
  64. }
  65. /**
  66. * vfs_setpos - update the file offset for lseek
  67. * @file: file structure in question
  68. * @offset: file offset to seek to
  69. * @maxsize: maximum file size
  70. *
  71. * This is a low-level filesystem helper for updating the file offset to
  72. * the value specified by @offset if the given offset is valid and it is
  73. * not equal to the current file offset.
  74. *
  75. * Return the specified offset on success and -EINVAL on invalid offset.
  76. */
  77. loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
  78. {
  79. return vfs_setpos_cookie(file, offset, maxsize, NULL);
  80. }
  81. EXPORT_SYMBOL(vfs_setpos);
  82. /**
  83. * must_set_pos - check whether f_pos has to be updated
  84. * @file: file to seek on
  85. * @offset: offset to use
  86. * @whence: type of seek operation
  87. * @eof: end of file
  88. *
  89. * Check whether f_pos needs to be updated and update @offset according
  90. * to @whence.
  91. *
  92. * Return: 0 if f_pos doesn't need to be updated, 1 if f_pos has to be
  93. * updated, and negative error code on failure.
  94. */
  95. static int must_set_pos(struct file *file, loff_t *offset, int whence, loff_t eof)
  96. {
  97. switch (whence) {
  98. case SEEK_END:
  99. *offset += eof;
  100. break;
  101. case SEEK_CUR:
  102. /*
  103. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  104. * position-querying operation. Avoid rewriting the "same"
  105. * f_pos value back to the file because a concurrent read(),
  106. * write() or lseek() might have altered it
  107. */
  108. if (*offset == 0) {
  109. *offset = file->f_pos;
  110. return 0;
  111. }
  112. break;
  113. case SEEK_DATA:
  114. /*
  115. * In the generic case the entire file is data, so as long as
  116. * offset isn't at the end of the file then the offset is data.
  117. */
  118. if ((unsigned long long)*offset >= eof)
  119. return -ENXIO;
  120. break;
  121. case SEEK_HOLE:
  122. /*
  123. * There is a virtual hole at the end of the file, so as long as
  124. * offset isn't i_size or larger, return i_size.
  125. */
  126. if ((unsigned long long)*offset >= eof)
  127. return -ENXIO;
  128. *offset = eof;
  129. break;
  130. }
  131. return 1;
  132. }
  133. /**
  134. * generic_file_llseek_size - generic llseek implementation for regular files
  135. * @file: file structure to seek on
  136. * @offset: file offset to seek to
  137. * @whence: type of seek
  138. * @maxsize: max size of this file in file system
  139. * @eof: offset used for SEEK_END position
  140. *
  141. * This is a variant of generic_file_llseek that allows passing in a custom
  142. * maximum file size and a custom EOF position, for e.g. hashed directories
  143. *
  144. * Synchronization:
  145. * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
  146. * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
  147. * read/writes behave like SEEK_SET against seeks.
  148. */
  149. loff_t
  150. generic_file_llseek_size(struct file *file, loff_t offset, int whence,
  151. loff_t maxsize, loff_t eof)
  152. {
  153. int ret;
  154. ret = must_set_pos(file, &offset, whence, eof);
  155. if (ret < 0)
  156. return ret;
  157. if (ret == 0)
  158. return offset;
  159. if (whence == SEEK_CUR) {
  160. /*
  161. * If the file requires locking via f_pos_lock we know
  162. * that mutual exclusion for SEEK_CUR on the same file
  163. * is guaranteed. If the file isn't locked, we take
  164. * f_lock to protect against f_pos races with other
  165. * SEEK_CURs.
  166. */
  167. if (file_seek_cur_needs_f_lock(file)) {
  168. guard(spinlock)(&file->f_lock);
  169. return vfs_setpos(file, file->f_pos + offset, maxsize);
  170. }
  171. return vfs_setpos(file, file->f_pos + offset, maxsize);
  172. }
  173. return vfs_setpos(file, offset, maxsize);
  174. }
  175. EXPORT_SYMBOL(generic_file_llseek_size);
  176. /**
  177. * generic_llseek_cookie - versioned llseek implementation
  178. * @file: file structure to seek on
  179. * @offset: file offset to seek to
  180. * @whence: type of seek
  181. * @cookie: cookie to update
  182. *
  183. * See generic_file_llseek for a general description and locking assumptions.
  184. *
  185. * In contrast to generic_file_llseek, this function also resets a
  186. * specified cookie to indicate a seek took place.
  187. */
  188. loff_t generic_llseek_cookie(struct file *file, loff_t offset, int whence,
  189. u64 *cookie)
  190. {
  191. struct inode *inode = file->f_mapping->host;
  192. loff_t maxsize = inode->i_sb->s_maxbytes;
  193. loff_t eof = i_size_read(inode);
  194. int ret;
  195. if (WARN_ON_ONCE(!cookie))
  196. return -EINVAL;
  197. /*
  198. * Require that this is only used for directories that guarantee
  199. * synchronization between readdir and seek so that an update to
  200. * @cookie is correctly synchronized with concurrent readdir.
  201. */
  202. if (WARN_ON_ONCE(!(file->f_mode & FMODE_ATOMIC_POS)))
  203. return -EINVAL;
  204. ret = must_set_pos(file, &offset, whence, eof);
  205. if (ret < 0)
  206. return ret;
  207. if (ret == 0)
  208. return offset;
  209. /* No need to hold f_lock because we know that f_pos_lock is held. */
  210. if (whence == SEEK_CUR)
  211. return vfs_setpos_cookie(file, file->f_pos + offset, maxsize, cookie);
  212. return vfs_setpos_cookie(file, offset, maxsize, cookie);
  213. }
  214. EXPORT_SYMBOL(generic_llseek_cookie);
  215. /**
  216. * generic_file_llseek - generic llseek implementation for regular files
  217. * @file: file structure to seek on
  218. * @offset: file offset to seek to
  219. * @whence: type of seek
  220. *
  221. * This is a generic implementation of ->llseek useable for all normal local
  222. * filesystems. It just updates the file offset to the value specified by
  223. * @offset and @whence.
  224. */
  225. loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
  226. {
  227. struct inode *inode = file->f_mapping->host;
  228. return generic_file_llseek_size(file, offset, whence,
  229. inode->i_sb->s_maxbytes,
  230. i_size_read(inode));
  231. }
  232. EXPORT_SYMBOL(generic_file_llseek);
  233. /**
  234. * fixed_size_llseek - llseek implementation for fixed-sized devices
  235. * @file: file structure to seek on
  236. * @offset: file offset to seek to
  237. * @whence: type of seek
  238. * @size: size of the file
  239. *
  240. */
  241. loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
  242. {
  243. switch (whence) {
  244. case SEEK_SET: case SEEK_CUR: case SEEK_END:
  245. return generic_file_llseek_size(file, offset, whence,
  246. size, size);
  247. default:
  248. return -EINVAL;
  249. }
  250. }
  251. EXPORT_SYMBOL(fixed_size_llseek);
  252. /**
  253. * no_seek_end_llseek - llseek implementation for fixed-sized devices
  254. * @file: file structure to seek on
  255. * @offset: file offset to seek to
  256. * @whence: type of seek
  257. *
  258. */
  259. loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
  260. {
  261. switch (whence) {
  262. case SEEK_SET: case SEEK_CUR:
  263. return generic_file_llseek_size(file, offset, whence,
  264. OFFSET_MAX, 0);
  265. default:
  266. return -EINVAL;
  267. }
  268. }
  269. EXPORT_SYMBOL(no_seek_end_llseek);
  270. /**
  271. * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
  272. * @file: file structure to seek on
  273. * @offset: file offset to seek to
  274. * @whence: type of seek
  275. * @size: maximal offset allowed
  276. *
  277. */
  278. loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
  279. {
  280. switch (whence) {
  281. case SEEK_SET: case SEEK_CUR:
  282. return generic_file_llseek_size(file, offset, whence,
  283. size, 0);
  284. default:
  285. return -EINVAL;
  286. }
  287. }
  288. EXPORT_SYMBOL(no_seek_end_llseek_size);
  289. /**
  290. * noop_llseek - No Operation Performed llseek implementation
  291. * @file: file structure to seek on
  292. * @offset: file offset to seek to
  293. * @whence: type of seek
  294. *
  295. * This is an implementation of ->llseek useable for the rare special case when
  296. * userspace expects the seek to succeed but the (device) file is actually not
  297. * able to perform the seek. In this case you use noop_llseek() instead of
  298. * falling back to the default implementation of ->llseek.
  299. */
  300. loff_t noop_llseek(struct file *file, loff_t offset, int whence)
  301. {
  302. return file->f_pos;
  303. }
  304. EXPORT_SYMBOL(noop_llseek);
  305. loff_t default_llseek(struct file *file, loff_t offset, int whence)
  306. {
  307. struct inode *inode = file_inode(file);
  308. loff_t retval;
  309. retval = inode_lock_killable(inode);
  310. if (retval)
  311. return retval;
  312. switch (whence) {
  313. case SEEK_END:
  314. offset += i_size_read(inode);
  315. break;
  316. case SEEK_CUR:
  317. if (offset == 0) {
  318. retval = file->f_pos;
  319. goto out;
  320. }
  321. offset += file->f_pos;
  322. break;
  323. case SEEK_DATA:
  324. /*
  325. * In the generic case the entire file is data, so as
  326. * long as offset isn't at the end of the file then the
  327. * offset is data.
  328. */
  329. if (offset >= inode->i_size) {
  330. retval = -ENXIO;
  331. goto out;
  332. }
  333. break;
  334. case SEEK_HOLE:
  335. /*
  336. * There is a virtual hole at the end of the file, so
  337. * as long as offset isn't i_size or larger, return
  338. * i_size.
  339. */
  340. if (offset >= inode->i_size) {
  341. retval = -ENXIO;
  342. goto out;
  343. }
  344. offset = inode->i_size;
  345. break;
  346. }
  347. retval = -EINVAL;
  348. if (offset >= 0 || unsigned_offsets(file)) {
  349. if (offset != file->f_pos)
  350. file->f_pos = offset;
  351. retval = offset;
  352. }
  353. out:
  354. inode_unlock(inode);
  355. return retval;
  356. }
  357. EXPORT_SYMBOL(default_llseek);
  358. loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
  359. {
  360. if (!(file->f_mode & FMODE_LSEEK))
  361. return -ESPIPE;
  362. return file->f_op->llseek(file, offset, whence);
  363. }
  364. EXPORT_SYMBOL(vfs_llseek);
  365. static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
  366. {
  367. off_t retval;
  368. CLASS(fd_pos, f)(fd);
  369. if (fd_empty(f))
  370. return -EBADF;
  371. retval = -EINVAL;
  372. if (whence <= SEEK_MAX) {
  373. loff_t res = vfs_llseek(fd_file(f), offset, whence);
  374. retval = res;
  375. if (res != (loff_t)retval)
  376. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  377. }
  378. return retval;
  379. }
  380. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  381. {
  382. return ksys_lseek(fd, offset, whence);
  383. }
  384. #ifdef CONFIG_COMPAT
  385. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  386. {
  387. return ksys_lseek(fd, offset, whence);
  388. }
  389. #endif
  390. #if !defined(CONFIG_64BIT) || defined(CONFIG_COMPAT) || \
  391. defined(__ARCH_WANT_SYS_LLSEEK)
  392. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  393. unsigned long, offset_low, loff_t __user *, result,
  394. unsigned int, whence)
  395. {
  396. int retval;
  397. CLASS(fd_pos, f)(fd);
  398. loff_t offset;
  399. if (fd_empty(f))
  400. return -EBADF;
  401. if (whence > SEEK_MAX)
  402. return -EINVAL;
  403. offset = vfs_llseek(fd_file(f), ((loff_t) offset_high << 32) | offset_low,
  404. whence);
  405. retval = (int)offset;
  406. if (offset >= 0) {
  407. retval = -EFAULT;
  408. if (!copy_to_user(result, &offset, sizeof(offset)))
  409. retval = 0;
  410. }
  411. return retval;
  412. }
  413. #endif
  414. int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
  415. {
  416. int mask = read_write == READ ? MAY_READ : MAY_WRITE;
  417. int ret;
  418. if (unlikely((ssize_t) count < 0))
  419. return -EINVAL;
  420. if (ppos) {
  421. loff_t pos = *ppos;
  422. if (unlikely(pos < 0)) {
  423. if (!unsigned_offsets(file))
  424. return -EINVAL;
  425. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  426. return -EOVERFLOW;
  427. } else if (unlikely((loff_t) (pos + count) < 0)) {
  428. if (!unsigned_offsets(file))
  429. return -EINVAL;
  430. }
  431. }
  432. ret = security_file_permission(file, mask);
  433. if (ret)
  434. return ret;
  435. return fsnotify_file_area_perm(file, mask, ppos, count);
  436. }
  437. EXPORT_SYMBOL(rw_verify_area);
  438. static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  439. {
  440. struct kiocb kiocb;
  441. struct iov_iter iter;
  442. ssize_t ret;
  443. init_sync_kiocb(&kiocb, filp);
  444. kiocb.ki_pos = (ppos ? *ppos : 0);
  445. iov_iter_ubuf(&iter, ITER_DEST, buf, len);
  446. ret = filp->f_op->read_iter(&kiocb, &iter);
  447. BUG_ON(ret == -EIOCBQUEUED);
  448. if (ppos)
  449. *ppos = kiocb.ki_pos;
  450. return ret;
  451. }
  452. static int warn_unsupported(struct file *file, const char *op)
  453. {
  454. pr_warn_ratelimited(
  455. "kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
  456. op, file, current->pid, current->comm);
  457. return -EINVAL;
  458. }
  459. ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
  460. {
  461. struct kvec iov = {
  462. .iov_base = buf,
  463. .iov_len = min_t(size_t, count, MAX_RW_COUNT),
  464. };
  465. struct kiocb kiocb;
  466. struct iov_iter iter;
  467. ssize_t ret;
  468. if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
  469. return -EINVAL;
  470. if (!(file->f_mode & FMODE_CAN_READ))
  471. return -EINVAL;
  472. /*
  473. * Also fail if ->read_iter and ->read are both wired up as that
  474. * implies very convoluted semantics.
  475. */
  476. if (unlikely(!file->f_op->read_iter || file->f_op->read))
  477. return warn_unsupported(file, "read");
  478. init_sync_kiocb(&kiocb, file);
  479. kiocb.ki_pos = pos ? *pos : 0;
  480. iov_iter_kvec(&iter, ITER_DEST, &iov, 1, iov.iov_len);
  481. ret = file->f_op->read_iter(&kiocb, &iter);
  482. if (ret > 0) {
  483. if (pos)
  484. *pos = kiocb.ki_pos;
  485. fsnotify_access(file);
  486. add_rchar(current, ret);
  487. }
  488. inc_syscr(current);
  489. return ret;
  490. }
  491. ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
  492. {
  493. ssize_t ret;
  494. ret = rw_verify_area(READ, file, pos, count);
  495. if (ret)
  496. return ret;
  497. return __kernel_read(file, buf, count, pos);
  498. }
  499. EXPORT_SYMBOL(kernel_read);
  500. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  501. {
  502. ssize_t ret;
  503. if (!(file->f_mode & FMODE_READ))
  504. return -EBADF;
  505. if (!(file->f_mode & FMODE_CAN_READ))
  506. return -EINVAL;
  507. if (unlikely(!access_ok(buf, count)))
  508. return -EFAULT;
  509. ret = rw_verify_area(READ, file, pos, count);
  510. if (ret)
  511. return ret;
  512. if (count > MAX_RW_COUNT)
  513. count = MAX_RW_COUNT;
  514. if (file->f_op->read)
  515. ret = file->f_op->read(file, buf, count, pos);
  516. else if (file->f_op->read_iter)
  517. ret = new_sync_read(file, buf, count, pos);
  518. else
  519. ret = -EINVAL;
  520. if (ret > 0) {
  521. fsnotify_access(file);
  522. add_rchar(current, ret);
  523. }
  524. inc_syscr(current);
  525. return ret;
  526. }
  527. static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  528. {
  529. struct kiocb kiocb;
  530. struct iov_iter iter;
  531. ssize_t ret;
  532. init_sync_kiocb(&kiocb, filp);
  533. kiocb.ki_pos = (ppos ? *ppos : 0);
  534. iov_iter_ubuf(&iter, ITER_SOURCE, (void __user *)buf, len);
  535. ret = filp->f_op->write_iter(&kiocb, &iter);
  536. BUG_ON(ret == -EIOCBQUEUED);
  537. if (ret > 0 && ppos)
  538. *ppos = kiocb.ki_pos;
  539. return ret;
  540. }
  541. /* caller is responsible for file_start_write/file_end_write */
  542. ssize_t __kernel_write_iter(struct file *file, struct iov_iter *from, loff_t *pos)
  543. {
  544. struct kiocb kiocb;
  545. ssize_t ret;
  546. if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
  547. return -EBADF;
  548. if (!(file->f_mode & FMODE_CAN_WRITE))
  549. return -EINVAL;
  550. /*
  551. * Also fail if ->write_iter and ->write are both wired up as that
  552. * implies very convoluted semantics.
  553. */
  554. if (unlikely(!file->f_op->write_iter || file->f_op->write))
  555. return warn_unsupported(file, "write");
  556. init_sync_kiocb(&kiocb, file);
  557. kiocb.ki_pos = pos ? *pos : 0;
  558. ret = file->f_op->write_iter(&kiocb, from);
  559. if (ret > 0) {
  560. if (pos)
  561. *pos = kiocb.ki_pos;
  562. fsnotify_modify(file);
  563. add_wchar(current, ret);
  564. }
  565. inc_syscw(current);
  566. return ret;
  567. }
  568. /* caller is responsible for file_start_write/file_end_write */
  569. ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
  570. {
  571. struct kvec iov = {
  572. .iov_base = (void *)buf,
  573. .iov_len = min_t(size_t, count, MAX_RW_COUNT),
  574. };
  575. struct iov_iter iter;
  576. iov_iter_kvec(&iter, ITER_SOURCE, &iov, 1, iov.iov_len);
  577. return __kernel_write_iter(file, &iter, pos);
  578. }
  579. /*
  580. * This "EXPORT_SYMBOL_GPL()" is more of a "EXPORT_SYMBOL_DONTUSE()",
  581. * but autofs is one of the few internal kernel users that actually
  582. * wants this _and_ can be built as a module. So we need to export
  583. * this symbol for autofs, even though it really isn't appropriate
  584. * for any other kernel modules.
  585. */
  586. EXPORT_SYMBOL_GPL(__kernel_write);
  587. ssize_t kernel_write(struct file *file, const void *buf, size_t count,
  588. loff_t *pos)
  589. {
  590. ssize_t ret;
  591. ret = rw_verify_area(WRITE, file, pos, count);
  592. if (ret)
  593. return ret;
  594. file_start_write(file);
  595. ret = __kernel_write(file, buf, count, pos);
  596. file_end_write(file);
  597. return ret;
  598. }
  599. EXPORT_SYMBOL(kernel_write);
  600. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  601. {
  602. ssize_t ret;
  603. if (!(file->f_mode & FMODE_WRITE))
  604. return -EBADF;
  605. if (!(file->f_mode & FMODE_CAN_WRITE))
  606. return -EINVAL;
  607. if (unlikely(!access_ok(buf, count)))
  608. return -EFAULT;
  609. ret = rw_verify_area(WRITE, file, pos, count);
  610. if (ret)
  611. return ret;
  612. if (count > MAX_RW_COUNT)
  613. count = MAX_RW_COUNT;
  614. file_start_write(file);
  615. if (file->f_op->write)
  616. ret = file->f_op->write(file, buf, count, pos);
  617. else if (file->f_op->write_iter)
  618. ret = new_sync_write(file, buf, count, pos);
  619. else
  620. ret = -EINVAL;
  621. if (ret > 0) {
  622. fsnotify_modify(file);
  623. add_wchar(current, ret);
  624. }
  625. inc_syscw(current);
  626. file_end_write(file);
  627. return ret;
  628. }
  629. /* file_ppos returns &file->f_pos or NULL if file is stream */
  630. static inline loff_t *file_ppos(struct file *file)
  631. {
  632. return file->f_mode & FMODE_STREAM ? NULL : &file->f_pos;
  633. }
  634. ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
  635. {
  636. CLASS(fd_pos, f)(fd);
  637. ssize_t ret = -EBADF;
  638. if (!fd_empty(f)) {
  639. loff_t pos, *ppos = file_ppos(fd_file(f));
  640. if (ppos) {
  641. pos = *ppos;
  642. ppos = &pos;
  643. }
  644. ret = vfs_read(fd_file(f), buf, count, ppos);
  645. if (ret >= 0 && ppos)
  646. fd_file(f)->f_pos = pos;
  647. }
  648. return ret;
  649. }
  650. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  651. {
  652. return ksys_read(fd, buf, count);
  653. }
  654. ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
  655. {
  656. CLASS(fd_pos, f)(fd);
  657. ssize_t ret = -EBADF;
  658. if (!fd_empty(f)) {
  659. loff_t pos, *ppos = file_ppos(fd_file(f));
  660. if (ppos) {
  661. pos = *ppos;
  662. ppos = &pos;
  663. }
  664. ret = vfs_write(fd_file(f), buf, count, ppos);
  665. if (ret >= 0 && ppos)
  666. fd_file(f)->f_pos = pos;
  667. }
  668. return ret;
  669. }
  670. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  671. size_t, count)
  672. {
  673. return ksys_write(fd, buf, count);
  674. }
  675. ssize_t ksys_pread64(unsigned int fd, char __user *buf, size_t count,
  676. loff_t pos)
  677. {
  678. if (pos < 0)
  679. return -EINVAL;
  680. CLASS(fd, f)(fd);
  681. if (fd_empty(f))
  682. return -EBADF;
  683. if (fd_file(f)->f_mode & FMODE_PREAD)
  684. return vfs_read(fd_file(f), buf, count, &pos);
  685. return -ESPIPE;
  686. }
  687. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  688. size_t, count, loff_t, pos)
  689. {
  690. return ksys_pread64(fd, buf, count, pos);
  691. }
  692. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PREAD64)
  693. COMPAT_SYSCALL_DEFINE5(pread64, unsigned int, fd, char __user *, buf,
  694. size_t, count, compat_arg_u64_dual(pos))
  695. {
  696. return ksys_pread64(fd, buf, count, compat_arg_u64_glue(pos));
  697. }
  698. #endif
  699. ssize_t ksys_pwrite64(unsigned int fd, const char __user *buf,
  700. size_t count, loff_t pos)
  701. {
  702. if (pos < 0)
  703. return -EINVAL;
  704. CLASS(fd, f)(fd);
  705. if (fd_empty(f))
  706. return -EBADF;
  707. if (fd_file(f)->f_mode & FMODE_PWRITE)
  708. return vfs_write(fd_file(f), buf, count, &pos);
  709. return -ESPIPE;
  710. }
  711. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  712. size_t, count, loff_t, pos)
  713. {
  714. return ksys_pwrite64(fd, buf, count, pos);
  715. }
  716. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_PWRITE64)
  717. COMPAT_SYSCALL_DEFINE5(pwrite64, unsigned int, fd, const char __user *, buf,
  718. size_t, count, compat_arg_u64_dual(pos))
  719. {
  720. return ksys_pwrite64(fd, buf, count, compat_arg_u64_glue(pos));
  721. }
  722. #endif
  723. static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
  724. loff_t *ppos, int type, rwf_t flags)
  725. {
  726. struct kiocb kiocb;
  727. ssize_t ret;
  728. init_sync_kiocb(&kiocb, filp);
  729. ret = kiocb_set_rw_flags(&kiocb, flags, type);
  730. if (ret)
  731. return ret;
  732. kiocb.ki_pos = (ppos ? *ppos : 0);
  733. if (type == READ)
  734. ret = filp->f_op->read_iter(&kiocb, iter);
  735. else
  736. ret = filp->f_op->write_iter(&kiocb, iter);
  737. BUG_ON(ret == -EIOCBQUEUED);
  738. if (ppos)
  739. *ppos = kiocb.ki_pos;
  740. return ret;
  741. }
  742. /* Do it by hand, with file-ops */
  743. static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
  744. loff_t *ppos, int type, rwf_t flags)
  745. {
  746. ssize_t ret = 0;
  747. if (flags & ~RWF_HIPRI)
  748. return -EOPNOTSUPP;
  749. while (iov_iter_count(iter)) {
  750. ssize_t nr;
  751. if (type == READ) {
  752. nr = filp->f_op->read(filp, iter_iov_addr(iter),
  753. iter_iov_len(iter), ppos);
  754. } else {
  755. nr = filp->f_op->write(filp, iter_iov_addr(iter),
  756. iter_iov_len(iter), ppos);
  757. }
  758. if (nr < 0) {
  759. if (!ret)
  760. ret = nr;
  761. break;
  762. }
  763. ret += nr;
  764. if (nr != iter_iov_len(iter))
  765. break;
  766. iov_iter_advance(iter, nr);
  767. }
  768. return ret;
  769. }
  770. ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
  771. struct iov_iter *iter)
  772. {
  773. size_t tot_len;
  774. ssize_t ret = 0;
  775. if (!file->f_op->read_iter)
  776. return -EINVAL;
  777. if (!(file->f_mode & FMODE_READ))
  778. return -EBADF;
  779. if (!(file->f_mode & FMODE_CAN_READ))
  780. return -EINVAL;
  781. tot_len = iov_iter_count(iter);
  782. if (!tot_len)
  783. goto out;
  784. ret = rw_verify_area(READ, file, &iocb->ki_pos, tot_len);
  785. if (ret < 0)
  786. return ret;
  787. ret = file->f_op->read_iter(iocb, iter);
  788. out:
  789. if (ret >= 0)
  790. fsnotify_access(file);
  791. return ret;
  792. }
  793. EXPORT_SYMBOL(vfs_iocb_iter_read);
  794. ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
  795. rwf_t flags)
  796. {
  797. size_t tot_len;
  798. ssize_t ret = 0;
  799. if (!file->f_op->read_iter)
  800. return -EINVAL;
  801. if (!(file->f_mode & FMODE_READ))
  802. return -EBADF;
  803. if (!(file->f_mode & FMODE_CAN_READ))
  804. return -EINVAL;
  805. tot_len = iov_iter_count(iter);
  806. if (!tot_len)
  807. goto out;
  808. ret = rw_verify_area(READ, file, ppos, tot_len);
  809. if (ret < 0)
  810. return ret;
  811. ret = do_iter_readv_writev(file, iter, ppos, READ, flags);
  812. out:
  813. if (ret >= 0)
  814. fsnotify_access(file);
  815. return ret;
  816. }
  817. EXPORT_SYMBOL(vfs_iter_read);
  818. /*
  819. * Caller is responsible for calling kiocb_end_write() on completion
  820. * if async iocb was queued.
  821. */
  822. ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
  823. struct iov_iter *iter)
  824. {
  825. size_t tot_len;
  826. ssize_t ret = 0;
  827. if (!file->f_op->write_iter)
  828. return -EINVAL;
  829. if (!(file->f_mode & FMODE_WRITE))
  830. return -EBADF;
  831. if (!(file->f_mode & FMODE_CAN_WRITE))
  832. return -EINVAL;
  833. tot_len = iov_iter_count(iter);
  834. if (!tot_len)
  835. return 0;
  836. ret = rw_verify_area(WRITE, file, &iocb->ki_pos, tot_len);
  837. if (ret < 0)
  838. return ret;
  839. kiocb_start_write(iocb);
  840. ret = file->f_op->write_iter(iocb, iter);
  841. if (ret != -EIOCBQUEUED)
  842. kiocb_end_write(iocb);
  843. if (ret > 0)
  844. fsnotify_modify(file);
  845. return ret;
  846. }
  847. EXPORT_SYMBOL(vfs_iocb_iter_write);
  848. ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
  849. rwf_t flags)
  850. {
  851. size_t tot_len;
  852. ssize_t ret;
  853. if (!(file->f_mode & FMODE_WRITE))
  854. return -EBADF;
  855. if (!(file->f_mode & FMODE_CAN_WRITE))
  856. return -EINVAL;
  857. if (!file->f_op->write_iter)
  858. return -EINVAL;
  859. tot_len = iov_iter_count(iter);
  860. if (!tot_len)
  861. return 0;
  862. ret = rw_verify_area(WRITE, file, ppos, tot_len);
  863. if (ret < 0)
  864. return ret;
  865. file_start_write(file);
  866. ret = do_iter_readv_writev(file, iter, ppos, WRITE, flags);
  867. if (ret > 0)
  868. fsnotify_modify(file);
  869. file_end_write(file);
  870. return ret;
  871. }
  872. EXPORT_SYMBOL(vfs_iter_write);
  873. static ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  874. unsigned long vlen, loff_t *pos, rwf_t flags)
  875. {
  876. struct iovec iovstack[UIO_FASTIOV];
  877. struct iovec *iov = iovstack;
  878. struct iov_iter iter;
  879. size_t tot_len;
  880. ssize_t ret = 0;
  881. if (!(file->f_mode & FMODE_READ))
  882. return -EBADF;
  883. if (!(file->f_mode & FMODE_CAN_READ))
  884. return -EINVAL;
  885. ret = import_iovec(ITER_DEST, vec, vlen, ARRAY_SIZE(iovstack), &iov,
  886. &iter);
  887. if (ret < 0)
  888. return ret;
  889. tot_len = iov_iter_count(&iter);
  890. if (!tot_len)
  891. goto out;
  892. ret = rw_verify_area(READ, file, pos, tot_len);
  893. if (ret < 0)
  894. goto out;
  895. if (file->f_op->read_iter)
  896. ret = do_iter_readv_writev(file, &iter, pos, READ, flags);
  897. else
  898. ret = do_loop_readv_writev(file, &iter, pos, READ, flags);
  899. out:
  900. if (ret >= 0)
  901. fsnotify_access(file);
  902. kfree(iov);
  903. return ret;
  904. }
  905. static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  906. unsigned long vlen, loff_t *pos, rwf_t flags)
  907. {
  908. struct iovec iovstack[UIO_FASTIOV];
  909. struct iovec *iov = iovstack;
  910. struct iov_iter iter;
  911. size_t tot_len;
  912. ssize_t ret = 0;
  913. if (!(file->f_mode & FMODE_WRITE))
  914. return -EBADF;
  915. if (!(file->f_mode & FMODE_CAN_WRITE))
  916. return -EINVAL;
  917. ret = import_iovec(ITER_SOURCE, vec, vlen, ARRAY_SIZE(iovstack), &iov,
  918. &iter);
  919. if (ret < 0)
  920. return ret;
  921. tot_len = iov_iter_count(&iter);
  922. if (!tot_len)
  923. goto out;
  924. ret = rw_verify_area(WRITE, file, pos, tot_len);
  925. if (ret < 0)
  926. goto out;
  927. file_start_write(file);
  928. if (file->f_op->write_iter)
  929. ret = do_iter_readv_writev(file, &iter, pos, WRITE, flags);
  930. else
  931. ret = do_loop_readv_writev(file, &iter, pos, WRITE, flags);
  932. if (ret > 0)
  933. fsnotify_modify(file);
  934. file_end_write(file);
  935. out:
  936. kfree(iov);
  937. return ret;
  938. }
  939. static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
  940. unsigned long vlen, rwf_t flags)
  941. {
  942. CLASS(fd_pos, f)(fd);
  943. ssize_t ret = -EBADF;
  944. if (!fd_empty(f)) {
  945. loff_t pos, *ppos = file_ppos(fd_file(f));
  946. if (ppos) {
  947. pos = *ppos;
  948. ppos = &pos;
  949. }
  950. ret = vfs_readv(fd_file(f), vec, vlen, ppos, flags);
  951. if (ret >= 0 && ppos)
  952. fd_file(f)->f_pos = pos;
  953. }
  954. if (ret > 0)
  955. add_rchar(current, ret);
  956. inc_syscr(current);
  957. return ret;
  958. }
  959. static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
  960. unsigned long vlen, rwf_t flags)
  961. {
  962. CLASS(fd_pos, f)(fd);
  963. ssize_t ret = -EBADF;
  964. if (!fd_empty(f)) {
  965. loff_t pos, *ppos = file_ppos(fd_file(f));
  966. if (ppos) {
  967. pos = *ppos;
  968. ppos = &pos;
  969. }
  970. ret = vfs_writev(fd_file(f), vec, vlen, ppos, flags);
  971. if (ret >= 0 && ppos)
  972. fd_file(f)->f_pos = pos;
  973. }
  974. if (ret > 0)
  975. add_wchar(current, ret);
  976. inc_syscw(current);
  977. return ret;
  978. }
  979. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  980. {
  981. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  982. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  983. }
  984. static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
  985. unsigned long vlen, loff_t pos, rwf_t flags)
  986. {
  987. ssize_t ret = -EBADF;
  988. if (pos < 0)
  989. return -EINVAL;
  990. CLASS(fd, f)(fd);
  991. if (!fd_empty(f)) {
  992. ret = -ESPIPE;
  993. if (fd_file(f)->f_mode & FMODE_PREAD)
  994. ret = vfs_readv(fd_file(f), vec, vlen, &pos, flags);
  995. }
  996. if (ret > 0)
  997. add_rchar(current, ret);
  998. inc_syscr(current);
  999. return ret;
  1000. }
  1001. static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
  1002. unsigned long vlen, loff_t pos, rwf_t flags)
  1003. {
  1004. ssize_t ret = -EBADF;
  1005. if (pos < 0)
  1006. return -EINVAL;
  1007. CLASS(fd, f)(fd);
  1008. if (!fd_empty(f)) {
  1009. ret = -ESPIPE;
  1010. if (fd_file(f)->f_mode & FMODE_PWRITE)
  1011. ret = vfs_writev(fd_file(f), vec, vlen, &pos, flags);
  1012. }
  1013. if (ret > 0)
  1014. add_wchar(current, ret);
  1015. inc_syscw(current);
  1016. return ret;
  1017. }
  1018. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  1019. unsigned long, vlen)
  1020. {
  1021. return do_readv(fd, vec, vlen, 0);
  1022. }
  1023. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  1024. unsigned long, vlen)
  1025. {
  1026. return do_writev(fd, vec, vlen, 0);
  1027. }
  1028. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  1029. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  1030. {
  1031. loff_t pos = pos_from_hilo(pos_h, pos_l);
  1032. return do_preadv(fd, vec, vlen, pos, 0);
  1033. }
  1034. SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
  1035. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  1036. rwf_t, flags)
  1037. {
  1038. loff_t pos = pos_from_hilo(pos_h, pos_l);
  1039. if (pos == -1)
  1040. return do_readv(fd, vec, vlen, flags);
  1041. return do_preadv(fd, vec, vlen, pos, flags);
  1042. }
  1043. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  1044. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  1045. {
  1046. loff_t pos = pos_from_hilo(pos_h, pos_l);
  1047. return do_pwritev(fd, vec, vlen, pos, 0);
  1048. }
  1049. SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
  1050. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  1051. rwf_t, flags)
  1052. {
  1053. loff_t pos = pos_from_hilo(pos_h, pos_l);
  1054. if (pos == -1)
  1055. return do_writev(fd, vec, vlen, flags);
  1056. return do_pwritev(fd, vec, vlen, pos, flags);
  1057. }
  1058. /*
  1059. * Various compat syscalls. Note that they all pretend to take a native
  1060. * iovec - import_iovec will properly treat those as compat_iovecs based on
  1061. * in_compat_syscall().
  1062. */
  1063. #ifdef CONFIG_COMPAT
  1064. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
  1065. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  1066. const struct iovec __user *, vec,
  1067. unsigned long, vlen, loff_t, pos)
  1068. {
  1069. return do_preadv(fd, vec, vlen, pos, 0);
  1070. }
  1071. #endif
  1072. COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
  1073. const struct iovec __user *, vec,
  1074. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1075. {
  1076. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1077. return do_preadv(fd, vec, vlen, pos, 0);
  1078. }
  1079. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
  1080. COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
  1081. const struct iovec __user *, vec,
  1082. unsigned long, vlen, loff_t, pos, rwf_t, flags)
  1083. {
  1084. if (pos == -1)
  1085. return do_readv(fd, vec, vlen, flags);
  1086. return do_preadv(fd, vec, vlen, pos, flags);
  1087. }
  1088. #endif
  1089. COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
  1090. const struct iovec __user *, vec,
  1091. compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
  1092. rwf_t, flags)
  1093. {
  1094. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1095. if (pos == -1)
  1096. return do_readv(fd, vec, vlen, flags);
  1097. return do_preadv(fd, vec, vlen, pos, flags);
  1098. }
  1099. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
  1100. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  1101. const struct iovec __user *, vec,
  1102. unsigned long, vlen, loff_t, pos)
  1103. {
  1104. return do_pwritev(fd, vec, vlen, pos, 0);
  1105. }
  1106. #endif
  1107. COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
  1108. const struct iovec __user *,vec,
  1109. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1110. {
  1111. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1112. return do_pwritev(fd, vec, vlen, pos, 0);
  1113. }
  1114. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
  1115. COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
  1116. const struct iovec __user *, vec,
  1117. unsigned long, vlen, loff_t, pos, rwf_t, flags)
  1118. {
  1119. if (pos == -1)
  1120. return do_writev(fd, vec, vlen, flags);
  1121. return do_pwritev(fd, vec, vlen, pos, flags);
  1122. }
  1123. #endif
  1124. COMPAT_SYSCALL_DEFINE6(pwritev2, compat_ulong_t, fd,
  1125. const struct iovec __user *,vec,
  1126. compat_ulong_t, vlen, u32, pos_low, u32, pos_high, rwf_t, flags)
  1127. {
  1128. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1129. if (pos == -1)
  1130. return do_writev(fd, vec, vlen, flags);
  1131. return do_pwritev(fd, vec, vlen, pos, flags);
  1132. }
  1133. #endif /* CONFIG_COMPAT */
  1134. static ssize_t do_sendfile(int out_fd, int in_fd, loff_t *ppos,
  1135. size_t count, loff_t max)
  1136. {
  1137. struct inode *in_inode, *out_inode;
  1138. struct pipe_inode_info *opipe;
  1139. loff_t pos;
  1140. loff_t out_pos;
  1141. ssize_t retval;
  1142. int fl;
  1143. /*
  1144. * Get input file, and verify that it is ok..
  1145. */
  1146. CLASS(fd, in)(in_fd);
  1147. if (fd_empty(in))
  1148. return -EBADF;
  1149. if (!(fd_file(in)->f_mode & FMODE_READ))
  1150. return -EBADF;
  1151. if (!ppos) {
  1152. pos = fd_file(in)->f_pos;
  1153. } else {
  1154. pos = *ppos;
  1155. if (!(fd_file(in)->f_mode & FMODE_PREAD))
  1156. return -ESPIPE;
  1157. }
  1158. retval = rw_verify_area(READ, fd_file(in), &pos, count);
  1159. if (retval < 0)
  1160. return retval;
  1161. if (count > MAX_RW_COUNT)
  1162. count = MAX_RW_COUNT;
  1163. /*
  1164. * Get output file, and verify that it is ok..
  1165. */
  1166. CLASS(fd, out)(out_fd);
  1167. if (fd_empty(out))
  1168. return -EBADF;
  1169. if (!(fd_file(out)->f_mode & FMODE_WRITE))
  1170. return -EBADF;
  1171. in_inode = file_inode(fd_file(in));
  1172. out_inode = file_inode(fd_file(out));
  1173. out_pos = fd_file(out)->f_pos;
  1174. if (!max)
  1175. max = min(in_inode->i_sb->s_maxbytes, out_inode->i_sb->s_maxbytes);
  1176. if (unlikely(pos + count > max)) {
  1177. if (pos >= max)
  1178. return -EOVERFLOW;
  1179. count = max - pos;
  1180. }
  1181. fl = 0;
  1182. #if 0
  1183. /*
  1184. * We need to debate whether we can enable this or not. The
  1185. * man page documents EAGAIN return for the output at least,
  1186. * and the application is arguably buggy if it doesn't expect
  1187. * EAGAIN on a non-blocking file descriptor.
  1188. */
  1189. if (fd_file(in)->f_flags & O_NONBLOCK)
  1190. fl = SPLICE_F_NONBLOCK;
  1191. #endif
  1192. opipe = get_pipe_info(fd_file(out), true);
  1193. if (!opipe) {
  1194. retval = rw_verify_area(WRITE, fd_file(out), &out_pos, count);
  1195. if (retval < 0)
  1196. return retval;
  1197. retval = do_splice_direct(fd_file(in), &pos, fd_file(out), &out_pos,
  1198. count, fl);
  1199. } else {
  1200. if (fd_file(out)->f_flags & O_NONBLOCK)
  1201. fl |= SPLICE_F_NONBLOCK;
  1202. retval = splice_file_to_pipe(fd_file(in), opipe, &pos, count, fl);
  1203. }
  1204. if (retval > 0) {
  1205. add_rchar(current, retval);
  1206. add_wchar(current, retval);
  1207. fsnotify_access(fd_file(in));
  1208. fsnotify_modify(fd_file(out));
  1209. fd_file(out)->f_pos = out_pos;
  1210. if (ppos)
  1211. *ppos = pos;
  1212. else
  1213. fd_file(in)->f_pos = pos;
  1214. }
  1215. inc_syscr(current);
  1216. inc_syscw(current);
  1217. if (pos > max)
  1218. retval = -EOVERFLOW;
  1219. return retval;
  1220. }
  1221. SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd, off_t __user *, offset, size_t, count)
  1222. {
  1223. loff_t pos;
  1224. off_t off;
  1225. ssize_t ret;
  1226. if (offset) {
  1227. if (unlikely(get_user(off, offset)))
  1228. return -EFAULT;
  1229. pos = off;
  1230. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1231. if (unlikely(put_user(pos, offset)))
  1232. return -EFAULT;
  1233. return ret;
  1234. }
  1235. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1236. }
  1237. SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, loff_t __user *, offset, size_t, count)
  1238. {
  1239. loff_t pos;
  1240. ssize_t ret;
  1241. if (offset) {
  1242. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1243. return -EFAULT;
  1244. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1245. if (unlikely(put_user(pos, offset)))
  1246. return -EFAULT;
  1247. return ret;
  1248. }
  1249. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1250. }
  1251. #ifdef CONFIG_COMPAT
  1252. COMPAT_SYSCALL_DEFINE4(sendfile, int, out_fd, int, in_fd,
  1253. compat_off_t __user *, offset, compat_size_t, count)
  1254. {
  1255. loff_t pos;
  1256. off_t off;
  1257. ssize_t ret;
  1258. if (offset) {
  1259. if (unlikely(get_user(off, offset)))
  1260. return -EFAULT;
  1261. pos = off;
  1262. ret = do_sendfile(out_fd, in_fd, &pos, count, MAX_NON_LFS);
  1263. if (unlikely(put_user(pos, offset)))
  1264. return -EFAULT;
  1265. return ret;
  1266. }
  1267. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1268. }
  1269. COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
  1270. compat_loff_t __user *, offset, compat_size_t, count)
  1271. {
  1272. loff_t pos;
  1273. ssize_t ret;
  1274. if (offset) {
  1275. if (unlikely(copy_from_user(&pos, offset, sizeof(loff_t))))
  1276. return -EFAULT;
  1277. ret = do_sendfile(out_fd, in_fd, &pos, count, 0);
  1278. if (unlikely(put_user(pos, offset)))
  1279. return -EFAULT;
  1280. return ret;
  1281. }
  1282. return do_sendfile(out_fd, in_fd, NULL, count, 0);
  1283. }
  1284. #endif
  1285. /*
  1286. * Performs necessary checks before doing a file copy
  1287. *
  1288. * Can adjust amount of bytes to copy via @req_count argument.
  1289. * Returns appropriate error code that caller should return or
  1290. * zero in case the copy should be allowed.
  1291. */
  1292. static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
  1293. struct file *file_out, loff_t pos_out,
  1294. size_t *req_count, unsigned int flags)
  1295. {
  1296. struct inode *inode_in = file_inode(file_in);
  1297. struct inode *inode_out = file_inode(file_out);
  1298. uint64_t count = *req_count;
  1299. loff_t size_in;
  1300. int ret;
  1301. ret = generic_file_rw_checks(file_in, file_out);
  1302. if (ret)
  1303. return ret;
  1304. /*
  1305. * We allow some filesystems to handle cross sb copy, but passing
  1306. * a file of the wrong filesystem type to filesystem driver can result
  1307. * in an attempt to dereference the wrong type of ->private_data, so
  1308. * avoid doing that until we really have a good reason.
  1309. *
  1310. * nfs and cifs define several different file_system_type structures
  1311. * and several different sets of file_operations, but they all end up
  1312. * using the same ->copy_file_range() function pointer.
  1313. */
  1314. if (flags & COPY_FILE_SPLICE) {
  1315. /* cross sb splice is allowed */
  1316. } else if (file_out->f_op->copy_file_range) {
  1317. if (file_in->f_op->copy_file_range !=
  1318. file_out->f_op->copy_file_range)
  1319. return -EXDEV;
  1320. } else if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb) {
  1321. return -EXDEV;
  1322. }
  1323. /* Don't touch certain kinds of inodes */
  1324. if (IS_IMMUTABLE(inode_out))
  1325. return -EPERM;
  1326. if (IS_SWAPFILE(inode_in) || IS_SWAPFILE(inode_out))
  1327. return -ETXTBSY;
  1328. /* Ensure offsets don't wrap. */
  1329. if (pos_in + count < pos_in || pos_out + count < pos_out)
  1330. return -EOVERFLOW;
  1331. /* Shorten the copy to EOF */
  1332. size_in = i_size_read(inode_in);
  1333. if (pos_in >= size_in)
  1334. count = 0;
  1335. else
  1336. count = min(count, size_in - (uint64_t)pos_in);
  1337. ret = generic_write_check_limits(file_out, pos_out, &count);
  1338. if (ret)
  1339. return ret;
  1340. /* Don't allow overlapped copying within the same file. */
  1341. if (inode_in == inode_out &&
  1342. pos_out + count > pos_in &&
  1343. pos_out < pos_in + count)
  1344. return -EINVAL;
  1345. *req_count = count;
  1346. return 0;
  1347. }
  1348. /*
  1349. * copy_file_range() differs from regular file read and write in that it
  1350. * specifically allows return partial success. When it does so is up to
  1351. * the copy_file_range method.
  1352. */
  1353. ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
  1354. struct file *file_out, loff_t pos_out,
  1355. size_t len, unsigned int flags)
  1356. {
  1357. ssize_t ret;
  1358. bool splice = flags & COPY_FILE_SPLICE;
  1359. bool samesb = file_inode(file_in)->i_sb == file_inode(file_out)->i_sb;
  1360. if (flags & ~COPY_FILE_SPLICE)
  1361. return -EINVAL;
  1362. ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
  1363. flags);
  1364. if (unlikely(ret))
  1365. return ret;
  1366. ret = rw_verify_area(READ, file_in, &pos_in, len);
  1367. if (unlikely(ret))
  1368. return ret;
  1369. ret = rw_verify_area(WRITE, file_out, &pos_out, len);
  1370. if (unlikely(ret))
  1371. return ret;
  1372. if (len == 0)
  1373. return 0;
  1374. /*
  1375. * Make sure return value doesn't overflow in 32bit compat mode. Also
  1376. * limit the size for all cases except when calling ->copy_file_range().
  1377. */
  1378. if (splice || !file_out->f_op->copy_file_range || in_compat_syscall())
  1379. len = min_t(size_t, MAX_RW_COUNT, len);
  1380. file_start_write(file_out);
  1381. /*
  1382. * Cloning is supported by more file systems, so we implement copy on
  1383. * same sb using clone, but for filesystems where both clone and copy
  1384. * are supported (e.g. nfs,cifs), we only call the copy method.
  1385. */
  1386. if (!splice && file_out->f_op->copy_file_range) {
  1387. ret = file_out->f_op->copy_file_range(file_in, pos_in,
  1388. file_out, pos_out,
  1389. len, flags);
  1390. } else if (!splice && file_in->f_op->remap_file_range && samesb) {
  1391. ret = file_in->f_op->remap_file_range(file_in, pos_in,
  1392. file_out, pos_out, len, REMAP_FILE_CAN_SHORTEN);
  1393. /* fallback to splice */
  1394. if (ret <= 0)
  1395. splice = true;
  1396. } else if (samesb) {
  1397. /* Fallback to splice for same sb copy for backward compat */
  1398. splice = true;
  1399. }
  1400. file_end_write(file_out);
  1401. if (!splice)
  1402. goto done;
  1403. /*
  1404. * We can get here for same sb copy of filesystems that do not implement
  1405. * ->copy_file_range() in case filesystem does not support clone or in
  1406. * case filesystem supports clone but rejected the clone request (e.g.
  1407. * because it was not block aligned).
  1408. *
  1409. * In both cases, fall back to kernel copy so we are able to maintain a
  1410. * consistent story about which filesystems support copy_file_range()
  1411. * and which filesystems do not, that will allow userspace tools to
  1412. * make consistent desicions w.r.t using copy_file_range().
  1413. *
  1414. * We also get here if caller (e.g. nfsd) requested COPY_FILE_SPLICE
  1415. * for server-side-copy between any two sb.
  1416. *
  1417. * In any case, we call do_splice_direct() and not splice_file_range(),
  1418. * without file_start_write() held, to avoid possible deadlocks related
  1419. * to splicing from input file, while file_start_write() is held on
  1420. * the output file on a different sb.
  1421. */
  1422. ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out, len, 0);
  1423. done:
  1424. if (ret > 0) {
  1425. fsnotify_access(file_in);
  1426. add_rchar(current, ret);
  1427. fsnotify_modify(file_out);
  1428. add_wchar(current, ret);
  1429. }
  1430. inc_syscr(current);
  1431. inc_syscw(current);
  1432. return ret;
  1433. }
  1434. EXPORT_SYMBOL(vfs_copy_file_range);
  1435. SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
  1436. int, fd_out, loff_t __user *, off_out,
  1437. size_t, len, unsigned int, flags)
  1438. {
  1439. loff_t pos_in;
  1440. loff_t pos_out;
  1441. ssize_t ret = -EBADF;
  1442. CLASS(fd, f_in)(fd_in);
  1443. if (fd_empty(f_in))
  1444. return -EBADF;
  1445. CLASS(fd, f_out)(fd_out);
  1446. if (fd_empty(f_out))
  1447. return -EBADF;
  1448. if (off_in) {
  1449. if (copy_from_user(&pos_in, off_in, sizeof(loff_t)))
  1450. return -EFAULT;
  1451. } else {
  1452. pos_in = fd_file(f_in)->f_pos;
  1453. }
  1454. if (off_out) {
  1455. if (copy_from_user(&pos_out, off_out, sizeof(loff_t)))
  1456. return -EFAULT;
  1457. } else {
  1458. pos_out = fd_file(f_out)->f_pos;
  1459. }
  1460. if (flags != 0)
  1461. return -EINVAL;
  1462. ret = vfs_copy_file_range(fd_file(f_in), pos_in, fd_file(f_out), pos_out, len,
  1463. flags);
  1464. if (ret > 0) {
  1465. pos_in += ret;
  1466. pos_out += ret;
  1467. if (off_in) {
  1468. if (copy_to_user(off_in, &pos_in, sizeof(loff_t)))
  1469. ret = -EFAULT;
  1470. } else {
  1471. fd_file(f_in)->f_pos = pos_in;
  1472. }
  1473. if (off_out) {
  1474. if (copy_to_user(off_out, &pos_out, sizeof(loff_t)))
  1475. ret = -EFAULT;
  1476. } else {
  1477. fd_file(f_out)->f_pos = pos_out;
  1478. }
  1479. }
  1480. return ret;
  1481. }
  1482. /*
  1483. * Don't operate on ranges the page cache doesn't support, and don't exceed the
  1484. * LFS limits. If pos is under the limit it becomes a short access. If it
  1485. * exceeds the limit we return -EFBIG.
  1486. */
  1487. int generic_write_check_limits(struct file *file, loff_t pos, loff_t *count)
  1488. {
  1489. struct inode *inode = file->f_mapping->host;
  1490. loff_t max_size = inode->i_sb->s_maxbytes;
  1491. loff_t limit = rlimit(RLIMIT_FSIZE);
  1492. if (limit != RLIM_INFINITY) {
  1493. if (pos >= limit) {
  1494. send_sig(SIGXFSZ, current, 0);
  1495. return -EFBIG;
  1496. }
  1497. *count = min(*count, limit - pos);
  1498. }
  1499. if (!(file->f_flags & O_LARGEFILE))
  1500. max_size = MAX_NON_LFS;
  1501. if (unlikely(pos >= max_size))
  1502. return -EFBIG;
  1503. *count = min(*count, max_size - pos);
  1504. return 0;
  1505. }
  1506. EXPORT_SYMBOL_GPL(generic_write_check_limits);
  1507. /* Like generic_write_checks(), but takes size of write instead of iter. */
  1508. int generic_write_checks_count(struct kiocb *iocb, loff_t *count)
  1509. {
  1510. struct file *file = iocb->ki_filp;
  1511. struct inode *inode = file->f_mapping->host;
  1512. if (IS_SWAPFILE(inode))
  1513. return -ETXTBSY;
  1514. if (!*count)
  1515. return 0;
  1516. if (iocb->ki_flags & IOCB_APPEND)
  1517. iocb->ki_pos = i_size_read(inode);
  1518. if ((iocb->ki_flags & IOCB_NOWAIT) &&
  1519. !((iocb->ki_flags & IOCB_DIRECT) ||
  1520. (file->f_op->fop_flags & FOP_BUFFER_WASYNC)))
  1521. return -EINVAL;
  1522. return generic_write_check_limits(iocb->ki_filp, iocb->ki_pos, count);
  1523. }
  1524. EXPORT_SYMBOL(generic_write_checks_count);
  1525. /*
  1526. * Performs necessary checks before doing a write
  1527. *
  1528. * Can adjust writing position or amount of bytes to write.
  1529. * Returns appropriate error code that caller should return or
  1530. * zero in case that write should be allowed.
  1531. */
  1532. ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
  1533. {
  1534. loff_t count = iov_iter_count(from);
  1535. int ret;
  1536. ret = generic_write_checks_count(iocb, &count);
  1537. if (ret)
  1538. return ret;
  1539. iov_iter_truncate(from, count);
  1540. return iov_iter_count(from);
  1541. }
  1542. EXPORT_SYMBOL(generic_write_checks);
  1543. /*
  1544. * Performs common checks before doing a file copy/clone
  1545. * from @file_in to @file_out.
  1546. */
  1547. int generic_file_rw_checks(struct file *file_in, struct file *file_out)
  1548. {
  1549. struct inode *inode_in = file_inode(file_in);
  1550. struct inode *inode_out = file_inode(file_out);
  1551. /* Don't copy dirs, pipes, sockets... */
  1552. if (S_ISDIR(inode_in->i_mode) || S_ISDIR(inode_out->i_mode))
  1553. return -EISDIR;
  1554. if (!S_ISREG(inode_in->i_mode) || !S_ISREG(inode_out->i_mode))
  1555. return -EINVAL;
  1556. if (!(file_in->f_mode & FMODE_READ) ||
  1557. !(file_out->f_mode & FMODE_WRITE) ||
  1558. (file_out->f_flags & O_APPEND))
  1559. return -EBADF;
  1560. return 0;
  1561. }
  1562. int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter)
  1563. {
  1564. size_t len = iov_iter_count(iter);
  1565. if (!iter_is_ubuf(iter))
  1566. return -EINVAL;
  1567. if (!is_power_of_2(len))
  1568. return -EINVAL;
  1569. if (!IS_ALIGNED(iocb->ki_pos, len))
  1570. return -EINVAL;
  1571. if (!(iocb->ki_flags & IOCB_DIRECT))
  1572. return -EOPNOTSUPP;
  1573. return 0;
  1574. }
  1575. EXPORT_SYMBOL_GPL(generic_atomic_write_valid);