read_write.c 36 KB

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