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. typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
  24. typedef ssize_t (*iter_fn_t)(struct kiocb *, struct iov_iter *);
  25. const struct file_operations generic_ro_fops = {
  26. .llseek = generic_file_llseek,
  27. .read_iter = generic_file_read_iter,
  28. .mmap = generic_file_readonly_mmap,
  29. .splice_read = generic_file_splice_read,
  30. };
  31. EXPORT_SYMBOL(generic_ro_fops);
  32. static inline int unsigned_offsets(struct file *file)
  33. {
  34. return file->f_mode & FMODE_UNSIGNED_OFFSET;
  35. }
  36. /**
  37. * vfs_setpos - update the file offset for lseek
  38. * @file: file structure in question
  39. * @offset: file offset to seek to
  40. * @maxsize: maximum file size
  41. *
  42. * This is a low-level filesystem helper for updating the file offset to
  43. * the value specified by @offset if the given offset is valid and it is
  44. * not equal to the current file offset.
  45. *
  46. * Return the specified offset on success and -EINVAL on invalid offset.
  47. */
  48. loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize)
  49. {
  50. if (offset < 0 && !unsigned_offsets(file))
  51. return -EINVAL;
  52. if (offset > maxsize)
  53. return -EINVAL;
  54. if (offset != file->f_pos) {
  55. file->f_pos = offset;
  56. file->f_version = 0;
  57. }
  58. return offset;
  59. }
  60. EXPORT_SYMBOL(vfs_setpos);
  61. /**
  62. * generic_file_llseek_size - generic llseek implementation for regular files
  63. * @file: file structure to seek on
  64. * @offset: file offset to seek to
  65. * @whence: type of seek
  66. * @size: max size of this file in file system
  67. * @eof: offset used for SEEK_END position
  68. *
  69. * This is a variant of generic_file_llseek that allows passing in a custom
  70. * maximum file size and a custom EOF position, for e.g. hashed directories
  71. *
  72. * Synchronization:
  73. * SEEK_SET and SEEK_END are unsynchronized (but atomic on 64bit platforms)
  74. * SEEK_CUR is synchronized against other SEEK_CURs, but not read/writes.
  75. * read/writes behave like SEEK_SET against seeks.
  76. */
  77. loff_t
  78. generic_file_llseek_size(struct file *file, loff_t offset, int whence,
  79. loff_t maxsize, loff_t eof)
  80. {
  81. switch (whence) {
  82. case SEEK_END:
  83. offset += eof;
  84. break;
  85. case SEEK_CUR:
  86. /*
  87. * Here we special-case the lseek(fd, 0, SEEK_CUR)
  88. * position-querying operation. Avoid rewriting the "same"
  89. * f_pos value back to the file because a concurrent read(),
  90. * write() or lseek() might have altered it
  91. */
  92. if (offset == 0)
  93. return file->f_pos;
  94. /*
  95. * f_lock protects against read/modify/write race with other
  96. * SEEK_CURs. Note that parallel writes and reads behave
  97. * like SEEK_SET.
  98. */
  99. spin_lock(&file->f_lock);
  100. offset = vfs_setpos(file, file->f_pos + offset, maxsize);
  101. spin_unlock(&file->f_lock);
  102. return offset;
  103. case SEEK_DATA:
  104. /*
  105. * In the generic case the entire file is data, so as long as
  106. * offset isn't at the end of the file then the offset is data.
  107. */
  108. if ((unsigned long long)offset >= eof)
  109. return -ENXIO;
  110. break;
  111. case SEEK_HOLE:
  112. /*
  113. * There is a virtual hole at the end of the file, so as long as
  114. * offset isn't i_size or larger, return i_size.
  115. */
  116. if ((unsigned long long)offset >= eof)
  117. return -ENXIO;
  118. offset = eof;
  119. break;
  120. }
  121. return vfs_setpos(file, offset, maxsize);
  122. }
  123. EXPORT_SYMBOL(generic_file_llseek_size);
  124. /**
  125. * generic_file_llseek - generic llseek implementation for regular files
  126. * @file: file structure to seek on
  127. * @offset: file offset to seek to
  128. * @whence: type of seek
  129. *
  130. * This is a generic implemenation of ->llseek useable for all normal local
  131. * filesystems. It just updates the file offset to the value specified by
  132. * @offset and @whence.
  133. */
  134. loff_t generic_file_llseek(struct file *file, loff_t offset, int whence)
  135. {
  136. struct inode *inode = file->f_mapping->host;
  137. return generic_file_llseek_size(file, offset, whence,
  138. inode->i_sb->s_maxbytes,
  139. i_size_read(inode));
  140. }
  141. EXPORT_SYMBOL(generic_file_llseek);
  142. /**
  143. * fixed_size_llseek - llseek implementation for fixed-sized devices
  144. * @file: file structure to seek on
  145. * @offset: file offset to seek to
  146. * @whence: type of seek
  147. * @size: size of the file
  148. *
  149. */
  150. loff_t fixed_size_llseek(struct file *file, loff_t offset, int whence, loff_t size)
  151. {
  152. switch (whence) {
  153. case SEEK_SET: case SEEK_CUR: case SEEK_END:
  154. return generic_file_llseek_size(file, offset, whence,
  155. size, size);
  156. default:
  157. return -EINVAL;
  158. }
  159. }
  160. EXPORT_SYMBOL(fixed_size_llseek);
  161. /**
  162. * no_seek_end_llseek - llseek implementation for fixed-sized devices
  163. * @file: file structure to seek on
  164. * @offset: file offset to seek to
  165. * @whence: type of seek
  166. *
  167. */
  168. loff_t no_seek_end_llseek(struct file *file, loff_t offset, int whence)
  169. {
  170. switch (whence) {
  171. case SEEK_SET: case SEEK_CUR:
  172. return generic_file_llseek_size(file, offset, whence,
  173. OFFSET_MAX, 0);
  174. default:
  175. return -EINVAL;
  176. }
  177. }
  178. EXPORT_SYMBOL(no_seek_end_llseek);
  179. /**
  180. * no_seek_end_llseek_size - llseek implementation for fixed-sized devices
  181. * @file: file structure to seek on
  182. * @offset: file offset to seek to
  183. * @whence: type of seek
  184. * @size: maximal offset allowed
  185. *
  186. */
  187. loff_t no_seek_end_llseek_size(struct file *file, loff_t offset, int whence, loff_t size)
  188. {
  189. switch (whence) {
  190. case SEEK_SET: case SEEK_CUR:
  191. return generic_file_llseek_size(file, offset, whence,
  192. size, 0);
  193. default:
  194. return -EINVAL;
  195. }
  196. }
  197. EXPORT_SYMBOL(no_seek_end_llseek_size);
  198. /**
  199. * noop_llseek - No Operation Performed llseek implementation
  200. * @file: file structure to seek on
  201. * @offset: file offset to seek to
  202. * @whence: type of seek
  203. *
  204. * This is an implementation of ->llseek useable for the rare special case when
  205. * userspace expects the seek to succeed but the (device) file is actually not
  206. * able to perform the seek. In this case you use noop_llseek() instead of
  207. * falling back to the default implementation of ->llseek.
  208. */
  209. loff_t noop_llseek(struct file *file, loff_t offset, int whence)
  210. {
  211. return file->f_pos;
  212. }
  213. EXPORT_SYMBOL(noop_llseek);
  214. loff_t no_llseek(struct file *file, loff_t offset, int whence)
  215. {
  216. return -ESPIPE;
  217. }
  218. EXPORT_SYMBOL(no_llseek);
  219. loff_t default_llseek(struct file *file, loff_t offset, int whence)
  220. {
  221. struct inode *inode = file_inode(file);
  222. loff_t retval;
  223. inode_lock(inode);
  224. switch (whence) {
  225. case SEEK_END:
  226. offset += i_size_read(inode);
  227. break;
  228. case SEEK_CUR:
  229. if (offset == 0) {
  230. retval = file->f_pos;
  231. goto out;
  232. }
  233. offset += file->f_pos;
  234. break;
  235. case SEEK_DATA:
  236. /*
  237. * In the generic case the entire file is data, so as
  238. * long as offset isn't at the end of the file then the
  239. * offset is data.
  240. */
  241. if (offset >= inode->i_size) {
  242. retval = -ENXIO;
  243. goto out;
  244. }
  245. break;
  246. case SEEK_HOLE:
  247. /*
  248. * There is a virtual hole at the end of the file, so
  249. * as long as offset isn't i_size or larger, return
  250. * i_size.
  251. */
  252. if (offset >= inode->i_size) {
  253. retval = -ENXIO;
  254. goto out;
  255. }
  256. offset = inode->i_size;
  257. break;
  258. }
  259. retval = -EINVAL;
  260. if (offset >= 0 || unsigned_offsets(file)) {
  261. if (offset != file->f_pos) {
  262. file->f_pos = offset;
  263. file->f_version = 0;
  264. }
  265. retval = offset;
  266. }
  267. out:
  268. inode_unlock(inode);
  269. return retval;
  270. }
  271. EXPORT_SYMBOL(default_llseek);
  272. loff_t vfs_llseek(struct file *file, loff_t offset, int whence)
  273. {
  274. loff_t (*fn)(struct file *, loff_t, int);
  275. fn = no_llseek;
  276. if (file->f_mode & FMODE_LSEEK) {
  277. if (file->f_op->llseek)
  278. fn = file->f_op->llseek;
  279. }
  280. return fn(file, offset, whence);
  281. }
  282. EXPORT_SYMBOL(vfs_llseek);
  283. SYSCALL_DEFINE3(lseek, unsigned int, fd, off_t, offset, unsigned int, whence)
  284. {
  285. off_t retval;
  286. struct fd f = fdget_pos(fd);
  287. if (!f.file)
  288. return -EBADF;
  289. retval = -EINVAL;
  290. if (whence <= SEEK_MAX) {
  291. loff_t res = vfs_llseek(f.file, offset, whence);
  292. retval = res;
  293. if (res != (loff_t)retval)
  294. retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
  295. }
  296. fdput_pos(f);
  297. return retval;
  298. }
  299. #ifdef CONFIG_COMPAT
  300. COMPAT_SYSCALL_DEFINE3(lseek, unsigned int, fd, compat_off_t, offset, unsigned int, whence)
  301. {
  302. return sys_lseek(fd, offset, whence);
  303. }
  304. #endif
  305. #ifdef __ARCH_WANT_SYS_LLSEEK
  306. SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
  307. unsigned long, offset_low, loff_t __user *, result,
  308. unsigned int, whence)
  309. {
  310. int retval;
  311. struct fd f = fdget_pos(fd);
  312. loff_t offset;
  313. if (!f.file)
  314. return -EBADF;
  315. retval = -EINVAL;
  316. if (whence > SEEK_MAX)
  317. goto out_putf;
  318. offset = vfs_llseek(f.file, ((loff_t) offset_high << 32) | offset_low,
  319. whence);
  320. retval = (int)offset;
  321. if (offset >= 0) {
  322. retval = -EFAULT;
  323. if (!copy_to_user(result, &offset, sizeof(offset)))
  324. retval = 0;
  325. }
  326. out_putf:
  327. fdput_pos(f);
  328. return retval;
  329. }
  330. #endif
  331. ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos)
  332. {
  333. struct kiocb kiocb;
  334. ssize_t ret;
  335. if (!file->f_op->read_iter)
  336. return -EINVAL;
  337. init_sync_kiocb(&kiocb, file);
  338. kiocb.ki_pos = *ppos;
  339. iter->type |= READ;
  340. ret = file->f_op->read_iter(&kiocb, iter);
  341. BUG_ON(ret == -EIOCBQUEUED);
  342. if (ret > 0)
  343. *ppos = kiocb.ki_pos;
  344. return ret;
  345. }
  346. EXPORT_SYMBOL(vfs_iter_read);
  347. ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos)
  348. {
  349. struct kiocb kiocb;
  350. ssize_t ret;
  351. if (!file->f_op->write_iter)
  352. return -EINVAL;
  353. init_sync_kiocb(&kiocb, file);
  354. kiocb.ki_pos = *ppos;
  355. iter->type |= WRITE;
  356. ret = file->f_op->write_iter(&kiocb, iter);
  357. BUG_ON(ret == -EIOCBQUEUED);
  358. if (ret > 0)
  359. *ppos = kiocb.ki_pos;
  360. return ret;
  361. }
  362. EXPORT_SYMBOL(vfs_iter_write);
  363. int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t count)
  364. {
  365. struct inode *inode;
  366. loff_t pos;
  367. int retval = -EINVAL;
  368. inode = file_inode(file);
  369. if (unlikely((ssize_t) count < 0))
  370. return retval;
  371. pos = *ppos;
  372. if (unlikely(pos < 0)) {
  373. if (!unsigned_offsets(file))
  374. return retval;
  375. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  376. return -EOVERFLOW;
  377. } else if (unlikely((loff_t) (pos + count) < 0)) {
  378. if (!unsigned_offsets(file))
  379. return retval;
  380. }
  381. if (unlikely(inode->i_flctx && mandatory_lock(inode))) {
  382. retval = locks_mandatory_area(inode, file, pos, pos + count - 1,
  383. read_write == READ ? F_RDLCK : F_WRLCK);
  384. if (retval < 0)
  385. return retval;
  386. }
  387. return security_file_permission(file,
  388. read_write == READ ? MAY_READ : MAY_WRITE);
  389. }
  390. static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
  391. {
  392. struct iovec iov = { .iov_base = buf, .iov_len = len };
  393. struct kiocb kiocb;
  394. struct iov_iter iter;
  395. ssize_t ret;
  396. init_sync_kiocb(&kiocb, filp);
  397. kiocb.ki_pos = *ppos;
  398. iov_iter_init(&iter, READ, &iov, 1, len);
  399. ret = filp->f_op->read_iter(&kiocb, &iter);
  400. BUG_ON(ret == -EIOCBQUEUED);
  401. *ppos = kiocb.ki_pos;
  402. return ret;
  403. }
  404. ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
  405. loff_t *pos)
  406. {
  407. if (file->f_op->read)
  408. return file->f_op->read(file, buf, count, pos);
  409. else if (file->f_op->read_iter)
  410. return new_sync_read(file, buf, count, pos);
  411. else
  412. return -EINVAL;
  413. }
  414. EXPORT_SYMBOL(__vfs_read);
  415. ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
  416. {
  417. ssize_t ret;
  418. if (!(file->f_mode & FMODE_READ))
  419. return -EBADF;
  420. if (!(file->f_mode & FMODE_CAN_READ))
  421. return -EINVAL;
  422. if (unlikely(!access_ok(VERIFY_WRITE, buf, count)))
  423. return -EFAULT;
  424. ret = rw_verify_area(READ, file, pos, count);
  425. if (!ret) {
  426. if (count > MAX_RW_COUNT)
  427. count = MAX_RW_COUNT;
  428. ret = __vfs_read(file, buf, count, pos);
  429. if (ret > 0) {
  430. fsnotify_access(file);
  431. add_rchar(current, ret);
  432. }
  433. inc_syscr(current);
  434. }
  435. return ret;
  436. }
  437. EXPORT_SYMBOL(vfs_read);
  438. static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
  439. {
  440. struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len };
  441. struct kiocb kiocb;
  442. struct iov_iter iter;
  443. ssize_t ret;
  444. init_sync_kiocb(&kiocb, filp);
  445. kiocb.ki_pos = *ppos;
  446. iov_iter_init(&iter, WRITE, &iov, 1, len);
  447. ret = filp->f_op->write_iter(&kiocb, &iter);
  448. BUG_ON(ret == -EIOCBQUEUED);
  449. if (ret > 0)
  450. *ppos = kiocb.ki_pos;
  451. return ret;
  452. }
  453. ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
  454. loff_t *pos)
  455. {
  456. if (file->f_op->write)
  457. return file->f_op->write(file, p, count, pos);
  458. else if (file->f_op->write_iter)
  459. return new_sync_write(file, p, count, pos);
  460. else
  461. return -EINVAL;
  462. }
  463. EXPORT_SYMBOL(__vfs_write);
  464. ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
  465. {
  466. mm_segment_t old_fs;
  467. const char __user *p;
  468. ssize_t ret;
  469. if (!(file->f_mode & FMODE_CAN_WRITE))
  470. return -EINVAL;
  471. old_fs = get_fs();
  472. set_fs(get_ds());
  473. p = (__force const char __user *)buf;
  474. if (count > MAX_RW_COUNT)
  475. count = MAX_RW_COUNT;
  476. ret = __vfs_write(file, p, count, pos);
  477. set_fs(old_fs);
  478. if (ret > 0) {
  479. fsnotify_modify(file);
  480. add_wchar(current, ret);
  481. }
  482. inc_syscw(current);
  483. return ret;
  484. }
  485. EXPORT_SYMBOL(__kernel_write);
  486. ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
  487. {
  488. ssize_t ret;
  489. if (!(file->f_mode & FMODE_WRITE))
  490. return -EBADF;
  491. if (!(file->f_mode & FMODE_CAN_WRITE))
  492. return -EINVAL;
  493. if (unlikely(!access_ok(VERIFY_READ, buf, count)))
  494. return -EFAULT;
  495. ret = rw_verify_area(WRITE, file, pos, count);
  496. if (!ret) {
  497. if (count > MAX_RW_COUNT)
  498. count = MAX_RW_COUNT;
  499. file_start_write(file);
  500. ret = __vfs_write(file, buf, count, pos);
  501. if (ret > 0) {
  502. fsnotify_modify(file);
  503. add_wchar(current, ret);
  504. }
  505. inc_syscw(current);
  506. file_end_write(file);
  507. }
  508. return ret;
  509. }
  510. EXPORT_SYMBOL(vfs_write);
  511. static inline loff_t file_pos_read(struct file *file)
  512. {
  513. return file->f_pos;
  514. }
  515. static inline void file_pos_write(struct file *file, loff_t pos)
  516. {
  517. file->f_pos = pos;
  518. }
  519. SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
  520. {
  521. struct fd f = fdget_pos(fd);
  522. ssize_t ret = -EBADF;
  523. if (f.file) {
  524. loff_t pos = file_pos_read(f.file);
  525. ret = vfs_read(f.file, buf, count, &pos);
  526. if (ret >= 0)
  527. file_pos_write(f.file, pos);
  528. fdput_pos(f);
  529. }
  530. return ret;
  531. }
  532. SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
  533. size_t, count)
  534. {
  535. struct fd f = fdget_pos(fd);
  536. ssize_t ret = -EBADF;
  537. if (f.file) {
  538. loff_t pos = file_pos_read(f.file);
  539. ret = vfs_write(f.file, buf, count, &pos);
  540. if (ret >= 0)
  541. file_pos_write(f.file, pos);
  542. fdput_pos(f);
  543. }
  544. return ret;
  545. }
  546. SYSCALL_DEFINE4(pread64, unsigned int, fd, char __user *, buf,
  547. size_t, count, loff_t, pos)
  548. {
  549. struct fd f;
  550. ssize_t ret = -EBADF;
  551. if (pos < 0)
  552. return -EINVAL;
  553. f = fdget(fd);
  554. if (f.file) {
  555. ret = -ESPIPE;
  556. if (f.file->f_mode & FMODE_PREAD)
  557. ret = vfs_read(f.file, buf, count, &pos);
  558. fdput(f);
  559. }
  560. return ret;
  561. }
  562. SYSCALL_DEFINE4(pwrite64, unsigned int, fd, const char __user *, buf,
  563. size_t, count, loff_t, pos)
  564. {
  565. struct fd f;
  566. ssize_t ret = -EBADF;
  567. if (pos < 0)
  568. return -EINVAL;
  569. f = fdget(fd);
  570. if (f.file) {
  571. ret = -ESPIPE;
  572. if (f.file->f_mode & FMODE_PWRITE)
  573. ret = vfs_write(f.file, buf, count, &pos);
  574. fdput(f);
  575. }
  576. return ret;
  577. }
  578. /*
  579. * Reduce an iovec's length in-place. Return the resulting number of segments
  580. */
  581. unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to)
  582. {
  583. unsigned long seg = 0;
  584. size_t len = 0;
  585. while (seg < nr_segs) {
  586. seg++;
  587. if (len + iov->iov_len >= to) {
  588. iov->iov_len = to - len;
  589. break;
  590. }
  591. len += iov->iov_len;
  592. iov++;
  593. }
  594. return seg;
  595. }
  596. EXPORT_SYMBOL(iov_shorten);
  597. static ssize_t do_iter_readv_writev(struct file *filp, struct iov_iter *iter,
  598. loff_t *ppos, iter_fn_t fn, int flags)
  599. {
  600. struct kiocb kiocb;
  601. ssize_t ret;
  602. if (flags & ~(RWF_HIPRI | RWF_DSYNC | RWF_SYNC))
  603. return -EOPNOTSUPP;
  604. init_sync_kiocb(&kiocb, filp);
  605. if (flags & RWF_HIPRI)
  606. kiocb.ki_flags |= IOCB_HIPRI;
  607. if (flags & RWF_DSYNC)
  608. kiocb.ki_flags |= IOCB_DSYNC;
  609. if (flags & RWF_SYNC)
  610. kiocb.ki_flags |= (IOCB_DSYNC | IOCB_SYNC);
  611. kiocb.ki_pos = *ppos;
  612. ret = fn(&kiocb, iter);
  613. BUG_ON(ret == -EIOCBQUEUED);
  614. *ppos = kiocb.ki_pos;
  615. return ret;
  616. }
  617. /* Do it by hand, with file-ops */
  618. static ssize_t do_loop_readv_writev(struct file *filp, struct iov_iter *iter,
  619. loff_t *ppos, io_fn_t fn, int flags)
  620. {
  621. ssize_t ret = 0;
  622. if (flags & ~RWF_HIPRI)
  623. return -EOPNOTSUPP;
  624. while (iov_iter_count(iter)) {
  625. struct iovec iovec = iov_iter_iovec(iter);
  626. ssize_t nr;
  627. nr = fn(filp, iovec.iov_base, iovec.iov_len, ppos);
  628. if (nr < 0) {
  629. if (!ret)
  630. ret = nr;
  631. break;
  632. }
  633. ret += nr;
  634. if (nr != iovec.iov_len)
  635. break;
  636. iov_iter_advance(iter, nr);
  637. }
  638. return ret;
  639. }
  640. /* A write operation does a read from user space and vice versa */
  641. #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
  642. /**
  643. * rw_copy_check_uvector() - Copy an array of &struct iovec from userspace
  644. * into the kernel and check that it is valid.
  645. *
  646. * @type: One of %CHECK_IOVEC_ONLY, %READ, or %WRITE.
  647. * @uvector: Pointer to the userspace array.
  648. * @nr_segs: Number of elements in userspace array.
  649. * @fast_segs: Number of elements in @fast_pointer.
  650. * @fast_pointer: Pointer to (usually small on-stack) kernel array.
  651. * @ret_pointer: (output parameter) Pointer to a variable that will point to
  652. * either @fast_pointer, a newly allocated kernel array, or NULL,
  653. * depending on which array was used.
  654. *
  655. * This function copies an array of &struct iovec of @nr_segs from
  656. * userspace into the kernel and checks that each element is valid (e.g.
  657. * it does not point to a kernel address or cause overflow by being too
  658. * large, etc.).
  659. *
  660. * As an optimization, the caller may provide a pointer to a small
  661. * on-stack array in @fast_pointer, typically %UIO_FASTIOV elements long
  662. * (the size of this array, or 0 if unused, should be given in @fast_segs).
  663. *
  664. * @ret_pointer will always point to the array that was used, so the
  665. * caller must take care not to call kfree() on it e.g. in case the
  666. * @fast_pointer array was used and it was allocated on the stack.
  667. *
  668. * Return: The total number of bytes covered by the iovec array on success
  669. * or a negative error code on error.
  670. */
  671. ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
  672. unsigned long nr_segs, unsigned long fast_segs,
  673. struct iovec *fast_pointer,
  674. struct iovec **ret_pointer)
  675. {
  676. unsigned long seg;
  677. ssize_t ret;
  678. struct iovec *iov = fast_pointer;
  679. /*
  680. * SuS says "The readv() function *may* fail if the iovcnt argument
  681. * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
  682. * traditionally returned zero for zero segments, so...
  683. */
  684. if (nr_segs == 0) {
  685. ret = 0;
  686. goto out;
  687. }
  688. /*
  689. * First get the "struct iovec" from user memory and
  690. * verify all the pointers
  691. */
  692. if (nr_segs > UIO_MAXIOV) {
  693. ret = -EINVAL;
  694. goto out;
  695. }
  696. if (nr_segs > fast_segs) {
  697. iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
  698. if (iov == NULL) {
  699. ret = -ENOMEM;
  700. goto out;
  701. }
  702. }
  703. if (copy_from_user(iov, uvector, nr_segs*sizeof(*uvector))) {
  704. ret = -EFAULT;
  705. goto out;
  706. }
  707. /*
  708. * According to the Single Unix Specification we should return EINVAL
  709. * if an element length is < 0 when cast to ssize_t or if the
  710. * total length would overflow the ssize_t return value of the
  711. * system call.
  712. *
  713. * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the
  714. * overflow case.
  715. */
  716. ret = 0;
  717. for (seg = 0; seg < nr_segs; seg++) {
  718. void __user *buf = iov[seg].iov_base;
  719. ssize_t len = (ssize_t)iov[seg].iov_len;
  720. /* see if we we're about to use an invalid len or if
  721. * it's about to overflow ssize_t */
  722. if (len < 0) {
  723. ret = -EINVAL;
  724. goto out;
  725. }
  726. if (type >= 0
  727. && unlikely(!access_ok(vrfy_dir(type), buf, len))) {
  728. ret = -EFAULT;
  729. goto out;
  730. }
  731. if (len > MAX_RW_COUNT - ret) {
  732. len = MAX_RW_COUNT - ret;
  733. iov[seg].iov_len = len;
  734. }
  735. ret += len;
  736. }
  737. out:
  738. *ret_pointer = iov;
  739. return ret;
  740. }
  741. static ssize_t do_readv_writev(int type, struct file *file,
  742. const struct iovec __user * uvector,
  743. unsigned long nr_segs, loff_t *pos,
  744. int flags)
  745. {
  746. size_t tot_len;
  747. struct iovec iovstack[UIO_FASTIOV];
  748. struct iovec *iov = iovstack;
  749. struct iov_iter iter;
  750. ssize_t ret;
  751. io_fn_t fn;
  752. iter_fn_t iter_fn;
  753. ret = import_iovec(type, uvector, nr_segs,
  754. ARRAY_SIZE(iovstack), &iov, &iter);
  755. if (ret < 0)
  756. return ret;
  757. tot_len = iov_iter_count(&iter);
  758. if (!tot_len)
  759. goto out;
  760. ret = rw_verify_area(type, file, pos, tot_len);
  761. if (ret < 0)
  762. goto out;
  763. if (type == READ) {
  764. fn = file->f_op->read;
  765. iter_fn = file->f_op->read_iter;
  766. } else {
  767. fn = (io_fn_t)file->f_op->write;
  768. iter_fn = file->f_op->write_iter;
  769. file_start_write(file);
  770. }
  771. if (iter_fn)
  772. ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
  773. else
  774. ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
  775. if (type != READ)
  776. file_end_write(file);
  777. out:
  778. kfree(iov);
  779. if ((ret + (type == READ)) > 0) {
  780. if (type == READ)
  781. fsnotify_access(file);
  782. else
  783. fsnotify_modify(file);
  784. }
  785. return ret;
  786. }
  787. ssize_t vfs_readv(struct file *file, const struct iovec __user *vec,
  788. unsigned long vlen, loff_t *pos, int flags)
  789. {
  790. if (!(file->f_mode & FMODE_READ))
  791. return -EBADF;
  792. if (!(file->f_mode & FMODE_CAN_READ))
  793. return -EINVAL;
  794. return do_readv_writev(READ, file, vec, vlen, pos, flags);
  795. }
  796. EXPORT_SYMBOL(vfs_readv);
  797. ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
  798. unsigned long vlen, loff_t *pos, int flags)
  799. {
  800. if (!(file->f_mode & FMODE_WRITE))
  801. return -EBADF;
  802. if (!(file->f_mode & FMODE_CAN_WRITE))
  803. return -EINVAL;
  804. return do_readv_writev(WRITE, file, vec, vlen, pos, flags);
  805. }
  806. EXPORT_SYMBOL(vfs_writev);
  807. static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
  808. unsigned long vlen, int flags)
  809. {
  810. struct fd f = fdget_pos(fd);
  811. ssize_t ret = -EBADF;
  812. if (f.file) {
  813. loff_t pos = file_pos_read(f.file);
  814. ret = vfs_readv(f.file, vec, vlen, &pos, flags);
  815. if (ret >= 0)
  816. file_pos_write(f.file, pos);
  817. fdput_pos(f);
  818. }
  819. if (ret > 0)
  820. add_rchar(current, ret);
  821. inc_syscr(current);
  822. return ret;
  823. }
  824. static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
  825. unsigned long vlen, int flags)
  826. {
  827. struct fd f = fdget_pos(fd);
  828. ssize_t ret = -EBADF;
  829. if (f.file) {
  830. loff_t pos = file_pos_read(f.file);
  831. ret = vfs_writev(f.file, vec, vlen, &pos, flags);
  832. if (ret >= 0)
  833. file_pos_write(f.file, pos);
  834. fdput_pos(f);
  835. }
  836. if (ret > 0)
  837. add_wchar(current, ret);
  838. inc_syscw(current);
  839. return ret;
  840. }
  841. static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
  842. {
  843. #define HALF_LONG_BITS (BITS_PER_LONG / 2)
  844. return (((loff_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
  845. }
  846. static ssize_t do_preadv(unsigned long fd, const struct iovec __user *vec,
  847. unsigned long vlen, loff_t pos, int flags)
  848. {
  849. struct fd f;
  850. ssize_t ret = -EBADF;
  851. if (pos < 0)
  852. return -EINVAL;
  853. f = fdget(fd);
  854. if (f.file) {
  855. ret = -ESPIPE;
  856. if (f.file->f_mode & FMODE_PREAD)
  857. ret = vfs_readv(f.file, vec, vlen, &pos, flags);
  858. fdput(f);
  859. }
  860. if (ret > 0)
  861. add_rchar(current, ret);
  862. inc_syscr(current);
  863. return ret;
  864. }
  865. static ssize_t do_pwritev(unsigned long fd, const struct iovec __user *vec,
  866. unsigned long vlen, loff_t pos, int flags)
  867. {
  868. struct fd f;
  869. ssize_t ret = -EBADF;
  870. if (pos < 0)
  871. return -EINVAL;
  872. f = fdget(fd);
  873. if (f.file) {
  874. ret = -ESPIPE;
  875. if (f.file->f_mode & FMODE_PWRITE)
  876. ret = vfs_writev(f.file, vec, vlen, &pos, flags);
  877. fdput(f);
  878. }
  879. if (ret > 0)
  880. add_wchar(current, ret);
  881. inc_syscw(current);
  882. return ret;
  883. }
  884. SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec __user *, vec,
  885. unsigned long, vlen)
  886. {
  887. return do_readv(fd, vec, vlen, 0);
  888. }
  889. SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec __user *, vec,
  890. unsigned long, vlen)
  891. {
  892. return do_writev(fd, vec, vlen, 0);
  893. }
  894. SYSCALL_DEFINE5(preadv, unsigned long, fd, const struct iovec __user *, vec,
  895. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  896. {
  897. loff_t pos = pos_from_hilo(pos_h, pos_l);
  898. return do_preadv(fd, vec, vlen, pos, 0);
  899. }
  900. SYSCALL_DEFINE6(preadv2, unsigned long, fd, const struct iovec __user *, vec,
  901. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  902. int, flags)
  903. {
  904. loff_t pos = pos_from_hilo(pos_h, pos_l);
  905. if (pos == -1)
  906. return do_readv(fd, vec, vlen, flags);
  907. return do_preadv(fd, vec, vlen, pos, flags);
  908. }
  909. SYSCALL_DEFINE5(pwritev, unsigned long, fd, const struct iovec __user *, vec,
  910. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h)
  911. {
  912. loff_t pos = pos_from_hilo(pos_h, pos_l);
  913. return do_pwritev(fd, vec, vlen, pos, 0);
  914. }
  915. SYSCALL_DEFINE6(pwritev2, unsigned long, fd, const struct iovec __user *, vec,
  916. unsigned long, vlen, unsigned long, pos_l, unsigned long, pos_h,
  917. int, flags)
  918. {
  919. loff_t pos = pos_from_hilo(pos_h, pos_l);
  920. if (pos == -1)
  921. return do_writev(fd, vec, vlen, flags);
  922. return do_pwritev(fd, vec, vlen, pos, flags);
  923. }
  924. #ifdef CONFIG_COMPAT
  925. static ssize_t compat_do_readv_writev(int type, struct file *file,
  926. const struct compat_iovec __user *uvector,
  927. unsigned long nr_segs, loff_t *pos,
  928. int flags)
  929. {
  930. compat_ssize_t tot_len;
  931. struct iovec iovstack[UIO_FASTIOV];
  932. struct iovec *iov = iovstack;
  933. struct iov_iter iter;
  934. ssize_t ret;
  935. io_fn_t fn;
  936. iter_fn_t iter_fn;
  937. ret = compat_import_iovec(type, uvector, nr_segs,
  938. UIO_FASTIOV, &iov, &iter);
  939. if (ret < 0)
  940. return ret;
  941. tot_len = iov_iter_count(&iter);
  942. if (!tot_len)
  943. goto out;
  944. ret = rw_verify_area(type, file, pos, tot_len);
  945. if (ret < 0)
  946. goto out;
  947. if (type == READ) {
  948. fn = file->f_op->read;
  949. iter_fn = file->f_op->read_iter;
  950. } else {
  951. fn = (io_fn_t)file->f_op->write;
  952. iter_fn = file->f_op->write_iter;
  953. file_start_write(file);
  954. }
  955. if (iter_fn)
  956. ret = do_iter_readv_writev(file, &iter, pos, iter_fn, flags);
  957. else
  958. ret = do_loop_readv_writev(file, &iter, pos, fn, flags);
  959. if (type != READ)
  960. file_end_write(file);
  961. out:
  962. kfree(iov);
  963. if ((ret + (type == READ)) > 0) {
  964. if (type == READ)
  965. fsnotify_access(file);
  966. else
  967. fsnotify_modify(file);
  968. }
  969. return ret;
  970. }
  971. static size_t compat_readv(struct file *file,
  972. const struct compat_iovec __user *vec,
  973. unsigned long vlen, loff_t *pos, int flags)
  974. {
  975. ssize_t ret = -EBADF;
  976. if (!(file->f_mode & FMODE_READ))
  977. goto out;
  978. ret = -EINVAL;
  979. if (!(file->f_mode & FMODE_CAN_READ))
  980. goto out;
  981. ret = compat_do_readv_writev(READ, file, vec, vlen, pos, flags);
  982. out:
  983. if (ret > 0)
  984. add_rchar(current, ret);
  985. inc_syscr(current);
  986. return ret;
  987. }
  988. static size_t do_compat_readv(compat_ulong_t fd,
  989. const struct compat_iovec __user *vec,
  990. compat_ulong_t vlen, int flags)
  991. {
  992. struct fd f = fdget_pos(fd);
  993. ssize_t ret;
  994. loff_t pos;
  995. if (!f.file)
  996. return -EBADF;
  997. pos = f.file->f_pos;
  998. ret = compat_readv(f.file, vec, vlen, &pos, flags);
  999. if (ret >= 0)
  1000. f.file->f_pos = pos;
  1001. fdput_pos(f);
  1002. return ret;
  1003. }
  1004. COMPAT_SYSCALL_DEFINE3(readv, compat_ulong_t, fd,
  1005. const struct compat_iovec __user *,vec,
  1006. compat_ulong_t, vlen)
  1007. {
  1008. return do_compat_readv(fd, vec, vlen, 0);
  1009. }
  1010. static long do_compat_preadv64(unsigned long fd,
  1011. const struct compat_iovec __user *vec,
  1012. unsigned long vlen, loff_t pos, int flags)
  1013. {
  1014. struct fd f;
  1015. ssize_t ret;
  1016. if (pos < 0)
  1017. return -EINVAL;
  1018. f = fdget(fd);
  1019. if (!f.file)
  1020. return -EBADF;
  1021. ret = -ESPIPE;
  1022. if (f.file->f_mode & FMODE_PREAD)
  1023. ret = compat_readv(f.file, vec, vlen, &pos, flags);
  1024. fdput(f);
  1025. return ret;
  1026. }
  1027. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64
  1028. COMPAT_SYSCALL_DEFINE4(preadv64, unsigned long, fd,
  1029. const struct compat_iovec __user *,vec,
  1030. unsigned long, vlen, loff_t, pos)
  1031. {
  1032. return do_compat_preadv64(fd, vec, vlen, pos, 0);
  1033. }
  1034. #endif
  1035. COMPAT_SYSCALL_DEFINE5(preadv, compat_ulong_t, fd,
  1036. const struct compat_iovec __user *,vec,
  1037. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1038. {
  1039. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1040. return do_compat_preadv64(fd, vec, vlen, pos, 0);
  1041. }
  1042. #ifdef __ARCH_WANT_COMPAT_SYS_PREADV64V2
  1043. COMPAT_SYSCALL_DEFINE5(preadv64v2, unsigned long, fd,
  1044. const struct compat_iovec __user *,vec,
  1045. unsigned long, vlen, loff_t, pos, int, flags)
  1046. {
  1047. return do_compat_preadv64(fd, vec, vlen, pos, flags);
  1048. }
  1049. #endif
  1050. COMPAT_SYSCALL_DEFINE6(preadv2, compat_ulong_t, fd,
  1051. const struct compat_iovec __user *,vec,
  1052. compat_ulong_t, vlen, u32, pos_low, u32, pos_high,
  1053. int, flags)
  1054. {
  1055. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1056. if (pos == -1)
  1057. return do_compat_readv(fd, vec, vlen, flags);
  1058. return do_compat_preadv64(fd, vec, vlen, pos, flags);
  1059. }
  1060. static size_t compat_writev(struct file *file,
  1061. const struct compat_iovec __user *vec,
  1062. unsigned long vlen, loff_t *pos, int flags)
  1063. {
  1064. ssize_t ret = -EBADF;
  1065. if (!(file->f_mode & FMODE_WRITE))
  1066. goto out;
  1067. ret = -EINVAL;
  1068. if (!(file->f_mode & FMODE_CAN_WRITE))
  1069. goto out;
  1070. ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos, flags);
  1071. out:
  1072. if (ret > 0)
  1073. add_wchar(current, ret);
  1074. inc_syscw(current);
  1075. return ret;
  1076. }
  1077. static size_t do_compat_writev(compat_ulong_t fd,
  1078. const struct compat_iovec __user* vec,
  1079. compat_ulong_t vlen, int flags)
  1080. {
  1081. struct fd f = fdget_pos(fd);
  1082. ssize_t ret;
  1083. loff_t pos;
  1084. if (!f.file)
  1085. return -EBADF;
  1086. pos = f.file->f_pos;
  1087. ret = compat_writev(f.file, vec, vlen, &pos, flags);
  1088. if (ret >= 0)
  1089. f.file->f_pos = pos;
  1090. fdput_pos(f);
  1091. return ret;
  1092. }
  1093. COMPAT_SYSCALL_DEFINE3(writev, compat_ulong_t, fd,
  1094. const struct compat_iovec __user *, vec,
  1095. compat_ulong_t, vlen)
  1096. {
  1097. return do_compat_writev(fd, vec, vlen, 0);
  1098. }
  1099. static long do_compat_pwritev64(unsigned long fd,
  1100. const struct compat_iovec __user *vec,
  1101. unsigned long vlen, loff_t pos, int flags)
  1102. {
  1103. struct fd f;
  1104. ssize_t ret;
  1105. if (pos < 0)
  1106. return -EINVAL;
  1107. f = fdget(fd);
  1108. if (!f.file)
  1109. return -EBADF;
  1110. ret = -ESPIPE;
  1111. if (f.file->f_mode & FMODE_PWRITE)
  1112. ret = compat_writev(f.file, vec, vlen, &pos, flags);
  1113. fdput(f);
  1114. return ret;
  1115. }
  1116. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64
  1117. COMPAT_SYSCALL_DEFINE4(pwritev64, unsigned long, fd,
  1118. const struct compat_iovec __user *,vec,
  1119. unsigned long, vlen, loff_t, pos)
  1120. {
  1121. return do_compat_pwritev64(fd, vec, vlen, pos, 0);
  1122. }
  1123. #endif
  1124. COMPAT_SYSCALL_DEFINE5(pwritev, compat_ulong_t, fd,
  1125. const struct compat_iovec __user *,vec,
  1126. compat_ulong_t, vlen, u32, pos_low, u32, pos_high)
  1127. {
  1128. loff_t pos = ((loff_t)pos_high << 32) | pos_low;
  1129. return do_compat_pwritev64(fd, vec, vlen, pos, 0);
  1130. }
  1131. #ifdef __ARCH_WANT_COMPAT_SYS_PWRITEV64V2
  1132. COMPAT_SYSCALL_DEFINE5(pwritev64v2, unsigned long, fd,
  1133. const struct compat_iovec __user *,vec,
  1134. unsigned long, vlen, loff_t, pos, int, flags)
  1135. {
  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);