read_write.c 41 KB

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