file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * linux/fs/ext4/file.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/file.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * ext4 fs regular file handling primitives
  16. *
  17. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  18. * (jj@sunsite.ms.mff.cuni.cz)
  19. */
  20. #include <linux/time.h>
  21. #include <linux/fs.h>
  22. #include <linux/mount.h>
  23. #include <linux/path.h>
  24. #include <linux/dax.h>
  25. #include <linux/quotaops.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/uio.h>
  28. #include "ext4.h"
  29. #include "ext4_jbd2.h"
  30. #include "xattr.h"
  31. #include "acl.h"
  32. #ifdef CONFIG_FS_DAX
  33. static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
  34. {
  35. struct inode *inode = file_inode(iocb->ki_filp);
  36. ssize_t ret;
  37. inode_lock_shared(inode);
  38. /*
  39. * Recheck under inode lock - at this point we are sure it cannot
  40. * change anymore
  41. */
  42. if (!IS_DAX(inode)) {
  43. inode_unlock_shared(inode);
  44. /* Fallback to buffered IO in case we cannot support DAX */
  45. return generic_file_read_iter(iocb, to);
  46. }
  47. ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
  48. inode_unlock_shared(inode);
  49. file_accessed(iocb->ki_filp);
  50. return ret;
  51. }
  52. #endif
  53. static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  54. {
  55. if (!iov_iter_count(to))
  56. return 0; /* skip atime */
  57. #ifdef CONFIG_FS_DAX
  58. if (IS_DAX(file_inode(iocb->ki_filp)))
  59. return ext4_dax_read_iter(iocb, to);
  60. #endif
  61. return generic_file_read_iter(iocb, to);
  62. }
  63. /*
  64. * Called when an inode is released. Note that this is different
  65. * from ext4_file_open: open gets called at every open, but release
  66. * gets called only when /all/ the files are closed.
  67. */
  68. static int ext4_release_file(struct inode *inode, struct file *filp)
  69. {
  70. if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
  71. ext4_alloc_da_blocks(inode);
  72. ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  73. }
  74. /* if we are the last writer on the inode, drop the block reservation */
  75. if ((filp->f_mode & FMODE_WRITE) &&
  76. (atomic_read(&inode->i_writecount) == 1) &&
  77. !EXT4_I(inode)->i_reserved_data_blocks)
  78. {
  79. down_write(&EXT4_I(inode)->i_data_sem);
  80. ext4_discard_preallocations(inode);
  81. up_write(&EXT4_I(inode)->i_data_sem);
  82. }
  83. if (is_dx(inode) && filp->private_data)
  84. ext4_htree_free_dir_info(filp->private_data);
  85. return 0;
  86. }
  87. static void ext4_unwritten_wait(struct inode *inode)
  88. {
  89. wait_queue_head_t *wq = ext4_ioend_wq(inode);
  90. wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_unwritten) == 0));
  91. }
  92. /*
  93. * This tests whether the IO in question is block-aligned or not.
  94. * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
  95. * are converted to written only after the IO is complete. Until they are
  96. * mapped, these blocks appear as holes, so dio_zero_block() will assume that
  97. * it needs to zero out portions of the start and/or end block. If 2 AIO
  98. * threads are at work on the same unwritten block, they must be synchronized
  99. * or one thread will zero the other's data, causing corruption.
  100. */
  101. static int
  102. ext4_unaligned_aio(struct inode *inode, struct iov_iter *from, loff_t pos)
  103. {
  104. struct super_block *sb = inode->i_sb;
  105. int blockmask = sb->s_blocksize - 1;
  106. if (pos >= i_size_read(inode))
  107. return 0;
  108. if ((pos | iov_iter_alignment(from)) & blockmask)
  109. return 1;
  110. return 0;
  111. }
  112. /* Is IO overwriting allocated and initialized blocks? */
  113. static bool ext4_overwrite_io(struct inode *inode, loff_t pos, loff_t len)
  114. {
  115. struct ext4_map_blocks map;
  116. unsigned int blkbits = inode->i_blkbits;
  117. int err, blklen;
  118. if (pos + len > i_size_read(inode))
  119. return false;
  120. map.m_lblk = pos >> blkbits;
  121. map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
  122. blklen = map.m_len;
  123. err = ext4_map_blocks(NULL, inode, &map, 0);
  124. /*
  125. * 'err==len' means that all of the blocks have been preallocated,
  126. * regardless of whether they have been initialized or not. To exclude
  127. * unwritten extents, we need to check m_flags.
  128. */
  129. return err == blklen && (map.m_flags & EXT4_MAP_MAPPED);
  130. }
  131. static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
  132. {
  133. struct inode *inode = file_inode(iocb->ki_filp);
  134. ssize_t ret;
  135. ret = generic_write_checks(iocb, from);
  136. if (ret <= 0)
  137. return ret;
  138. /*
  139. * If we have encountered a bitmap-format file, the size limit
  140. * is smaller than s_maxbytes, which is for extent-mapped files.
  141. */
  142. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  143. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  144. if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
  145. return -EFBIG;
  146. iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
  147. }
  148. return iov_iter_count(from);
  149. }
  150. #ifdef CONFIG_FS_DAX
  151. static ssize_t
  152. ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
  153. {
  154. struct inode *inode = file_inode(iocb->ki_filp);
  155. ssize_t ret;
  156. bool overwrite = false;
  157. inode_lock(inode);
  158. ret = ext4_write_checks(iocb, from);
  159. if (ret <= 0)
  160. goto out;
  161. ret = file_remove_privs(iocb->ki_filp);
  162. if (ret)
  163. goto out;
  164. ret = file_update_time(iocb->ki_filp);
  165. if (ret)
  166. goto out;
  167. if (ext4_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from))) {
  168. overwrite = true;
  169. downgrade_write(&inode->i_rwsem);
  170. }
  171. ret = dax_iomap_rw(iocb, from, &ext4_iomap_ops);
  172. out:
  173. if (!overwrite)
  174. inode_unlock(inode);
  175. else
  176. inode_unlock_shared(inode);
  177. if (ret > 0)
  178. ret = generic_write_sync(iocb, ret);
  179. return ret;
  180. }
  181. #endif
  182. static ssize_t
  183. ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  184. {
  185. struct inode *inode = file_inode(iocb->ki_filp);
  186. int o_direct = iocb->ki_flags & IOCB_DIRECT;
  187. int unaligned_aio = 0;
  188. int overwrite = 0;
  189. ssize_t ret;
  190. #ifdef CONFIG_FS_DAX
  191. if (IS_DAX(inode))
  192. return ext4_dax_write_iter(iocb, from);
  193. #endif
  194. inode_lock(inode);
  195. ret = ext4_write_checks(iocb, from);
  196. if (ret <= 0)
  197. goto out;
  198. /*
  199. * Unaligned direct AIO must be serialized among each other as zeroing
  200. * of partial blocks of two competing unaligned AIOs can result in data
  201. * corruption.
  202. */
  203. if (o_direct && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
  204. !is_sync_kiocb(iocb) &&
  205. ext4_unaligned_aio(inode, from, iocb->ki_pos)) {
  206. unaligned_aio = 1;
  207. ext4_unwritten_wait(inode);
  208. }
  209. iocb->private = &overwrite;
  210. /* Check whether we do a DIO overwrite or not */
  211. if (o_direct && ext4_should_dioread_nolock(inode) && !unaligned_aio &&
  212. ext4_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from)))
  213. overwrite = 1;
  214. ret = __generic_file_write_iter(iocb, from);
  215. inode_unlock(inode);
  216. if (ret > 0)
  217. ret = generic_write_sync(iocb, ret);
  218. return ret;
  219. out:
  220. inode_unlock(inode);
  221. return ret;
  222. }
  223. #ifdef CONFIG_FS_DAX
  224. static int ext4_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  225. {
  226. int result;
  227. struct inode *inode = file_inode(vma->vm_file);
  228. struct super_block *sb = inode->i_sb;
  229. bool write = vmf->flags & FAULT_FLAG_WRITE;
  230. if (write) {
  231. sb_start_pagefault(sb);
  232. file_update_time(vma->vm_file);
  233. }
  234. down_read(&EXT4_I(inode)->i_mmap_sem);
  235. result = dax_iomap_fault(vma, vmf, &ext4_iomap_ops);
  236. up_read(&EXT4_I(inode)->i_mmap_sem);
  237. if (write)
  238. sb_end_pagefault(sb);
  239. return result;
  240. }
  241. static int ext4_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
  242. pmd_t *pmd, unsigned int flags)
  243. {
  244. int result;
  245. struct inode *inode = file_inode(vma->vm_file);
  246. struct super_block *sb = inode->i_sb;
  247. bool write = flags & FAULT_FLAG_WRITE;
  248. if (write) {
  249. sb_start_pagefault(sb);
  250. file_update_time(vma->vm_file);
  251. }
  252. down_read(&EXT4_I(inode)->i_mmap_sem);
  253. result = dax_iomap_pmd_fault(vma, addr, pmd, flags,
  254. &ext4_iomap_ops);
  255. up_read(&EXT4_I(inode)->i_mmap_sem);
  256. if (write)
  257. sb_end_pagefault(sb);
  258. return result;
  259. }
  260. /*
  261. * Handle write fault for VM_MIXEDMAP mappings. Similarly to ext4_dax_fault()
  262. * handler we check for races agaist truncate. Note that since we cycle through
  263. * i_mmap_sem, we are sure that also any hole punching that began before we
  264. * were called is finished by now and so if it included part of the file we
  265. * are working on, our pte will get unmapped and the check for pte_same() in
  266. * wp_pfn_shared() fails. Thus fault gets retried and things work out as
  267. * desired.
  268. */
  269. static int ext4_dax_pfn_mkwrite(struct vm_area_struct *vma,
  270. struct vm_fault *vmf)
  271. {
  272. struct inode *inode = file_inode(vma->vm_file);
  273. struct super_block *sb = inode->i_sb;
  274. loff_t size;
  275. int ret;
  276. sb_start_pagefault(sb);
  277. file_update_time(vma->vm_file);
  278. down_read(&EXT4_I(inode)->i_mmap_sem);
  279. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  280. if (vmf->pgoff >= size)
  281. ret = VM_FAULT_SIGBUS;
  282. else
  283. ret = dax_pfn_mkwrite(vma, vmf);
  284. up_read(&EXT4_I(inode)->i_mmap_sem);
  285. sb_end_pagefault(sb);
  286. return ret;
  287. }
  288. static const struct vm_operations_struct ext4_dax_vm_ops = {
  289. .fault = ext4_dax_fault,
  290. .pmd_fault = ext4_dax_pmd_fault,
  291. .page_mkwrite = ext4_dax_fault,
  292. .pfn_mkwrite = ext4_dax_pfn_mkwrite,
  293. };
  294. #else
  295. #define ext4_dax_vm_ops ext4_file_vm_ops
  296. #endif
  297. static const struct vm_operations_struct ext4_file_vm_ops = {
  298. .fault = ext4_filemap_fault,
  299. .map_pages = filemap_map_pages,
  300. .page_mkwrite = ext4_page_mkwrite,
  301. };
  302. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  303. {
  304. struct inode *inode = file->f_mapping->host;
  305. if (ext4_encrypted_inode(inode)) {
  306. int err = fscrypt_get_encryption_info(inode);
  307. if (err)
  308. return 0;
  309. if (!fscrypt_has_encryption_key(inode))
  310. return -ENOKEY;
  311. }
  312. file_accessed(file);
  313. if (IS_DAX(file_inode(file))) {
  314. vma->vm_ops = &ext4_dax_vm_ops;
  315. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  316. } else {
  317. vma->vm_ops = &ext4_file_vm_ops;
  318. }
  319. return 0;
  320. }
  321. static int ext4_file_open(struct inode * inode, struct file * filp)
  322. {
  323. struct super_block *sb = inode->i_sb;
  324. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  325. struct vfsmount *mnt = filp->f_path.mnt;
  326. struct dentry *dir;
  327. struct path path;
  328. char buf[64], *cp;
  329. int ret;
  330. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  331. !(sb->s_flags & MS_RDONLY))) {
  332. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  333. /*
  334. * Sample where the filesystem has been mounted and
  335. * store it in the superblock for sysadmin convenience
  336. * when trying to sort through large numbers of block
  337. * devices or filesystem images.
  338. */
  339. memset(buf, 0, sizeof(buf));
  340. path.mnt = mnt;
  341. path.dentry = mnt->mnt_root;
  342. cp = d_path(&path, buf, sizeof(buf));
  343. if (!IS_ERR(cp)) {
  344. handle_t *handle;
  345. int err;
  346. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  347. if (IS_ERR(handle))
  348. return PTR_ERR(handle);
  349. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  350. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  351. if (err) {
  352. ext4_journal_stop(handle);
  353. return err;
  354. }
  355. strlcpy(sbi->s_es->s_last_mounted, cp,
  356. sizeof(sbi->s_es->s_last_mounted));
  357. ext4_handle_dirty_super(handle, sb);
  358. ext4_journal_stop(handle);
  359. }
  360. }
  361. if (ext4_encrypted_inode(inode)) {
  362. ret = fscrypt_get_encryption_info(inode);
  363. if (ret)
  364. return -EACCES;
  365. if (!fscrypt_has_encryption_key(inode))
  366. return -ENOKEY;
  367. }
  368. dir = dget_parent(file_dentry(filp));
  369. if (ext4_encrypted_inode(d_inode(dir)) &&
  370. !fscrypt_has_permitted_context(d_inode(dir), inode)) {
  371. ext4_warning(inode->i_sb,
  372. "Inconsistent encryption contexts: %lu/%lu",
  373. (unsigned long) d_inode(dir)->i_ino,
  374. (unsigned long) inode->i_ino);
  375. dput(dir);
  376. return -EPERM;
  377. }
  378. dput(dir);
  379. /*
  380. * Set up the jbd2_inode if we are opening the inode for
  381. * writing and the journal is present
  382. */
  383. if (filp->f_mode & FMODE_WRITE) {
  384. ret = ext4_inode_attach_jinode(inode);
  385. if (ret < 0)
  386. return ret;
  387. }
  388. return dquot_file_open(inode, filp);
  389. }
  390. /*
  391. * Here we use ext4_map_blocks() to get a block mapping for a extent-based
  392. * file rather than ext4_ext_walk_space() because we can introduce
  393. * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
  394. * function. When extent status tree has been fully implemented, it will
  395. * track all extent status for a file and we can directly use it to
  396. * retrieve the offset for SEEK_DATA/SEEK_HOLE.
  397. */
  398. /*
  399. * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
  400. * lookup page cache to check whether or not there has some data between
  401. * [startoff, endoff] because, if this range contains an unwritten extent,
  402. * we determine this extent as a data or a hole according to whether the
  403. * page cache has data or not.
  404. */
  405. static int ext4_find_unwritten_pgoff(struct inode *inode,
  406. int whence,
  407. ext4_lblk_t end_blk,
  408. loff_t *offset)
  409. {
  410. struct pagevec pvec;
  411. unsigned int blkbits;
  412. pgoff_t index;
  413. pgoff_t end;
  414. loff_t endoff;
  415. loff_t startoff;
  416. loff_t lastoff;
  417. int found = 0;
  418. blkbits = inode->i_sb->s_blocksize_bits;
  419. startoff = *offset;
  420. lastoff = startoff;
  421. endoff = (loff_t)end_blk << blkbits;
  422. index = startoff >> PAGE_SHIFT;
  423. end = endoff >> PAGE_SHIFT;
  424. pagevec_init(&pvec, 0);
  425. do {
  426. int i, num;
  427. unsigned long nr_pages;
  428. num = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
  429. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
  430. (pgoff_t)num);
  431. if (nr_pages == 0) {
  432. if (whence == SEEK_DATA)
  433. break;
  434. BUG_ON(whence != SEEK_HOLE);
  435. /*
  436. * If this is the first time to go into the loop and
  437. * offset is not beyond the end offset, it will be a
  438. * hole at this offset
  439. */
  440. if (lastoff == startoff || lastoff < endoff)
  441. found = 1;
  442. break;
  443. }
  444. /*
  445. * If this is the first time to go into the loop and
  446. * offset is smaller than the first page offset, it will be a
  447. * hole at this offset.
  448. */
  449. if (lastoff == startoff && whence == SEEK_HOLE &&
  450. lastoff < page_offset(pvec.pages[0])) {
  451. found = 1;
  452. break;
  453. }
  454. for (i = 0; i < nr_pages; i++) {
  455. struct page *page = pvec.pages[i];
  456. struct buffer_head *bh, *head;
  457. /*
  458. * If the current offset is not beyond the end of given
  459. * range, it will be a hole.
  460. */
  461. if (lastoff < endoff && whence == SEEK_HOLE &&
  462. page->index > end) {
  463. found = 1;
  464. *offset = lastoff;
  465. goto out;
  466. }
  467. lock_page(page);
  468. if (unlikely(page->mapping != inode->i_mapping)) {
  469. unlock_page(page);
  470. continue;
  471. }
  472. if (!page_has_buffers(page)) {
  473. unlock_page(page);
  474. continue;
  475. }
  476. if (page_has_buffers(page)) {
  477. lastoff = page_offset(page);
  478. bh = head = page_buffers(page);
  479. do {
  480. if (buffer_uptodate(bh) ||
  481. buffer_unwritten(bh)) {
  482. if (whence == SEEK_DATA)
  483. found = 1;
  484. } else {
  485. if (whence == SEEK_HOLE)
  486. found = 1;
  487. }
  488. if (found) {
  489. *offset = max_t(loff_t,
  490. startoff, lastoff);
  491. unlock_page(page);
  492. goto out;
  493. }
  494. lastoff += bh->b_size;
  495. bh = bh->b_this_page;
  496. } while (bh != head);
  497. }
  498. lastoff = page_offset(page) + PAGE_SIZE;
  499. unlock_page(page);
  500. }
  501. /*
  502. * The no. of pages is less than our desired, that would be a
  503. * hole in there.
  504. */
  505. if (nr_pages < num && whence == SEEK_HOLE) {
  506. found = 1;
  507. *offset = lastoff;
  508. break;
  509. }
  510. index = pvec.pages[i - 1]->index + 1;
  511. pagevec_release(&pvec);
  512. } while (index <= end);
  513. out:
  514. pagevec_release(&pvec);
  515. return found;
  516. }
  517. /*
  518. * ext4_seek_data() retrieves the offset for SEEK_DATA.
  519. */
  520. static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
  521. {
  522. struct inode *inode = file->f_mapping->host;
  523. struct extent_status es;
  524. ext4_lblk_t start, last, end;
  525. loff_t dataoff, isize;
  526. int blkbits;
  527. int ret;
  528. inode_lock(inode);
  529. isize = i_size_read(inode);
  530. if (offset >= isize) {
  531. inode_unlock(inode);
  532. return -ENXIO;
  533. }
  534. blkbits = inode->i_sb->s_blocksize_bits;
  535. start = offset >> blkbits;
  536. last = start;
  537. end = isize >> blkbits;
  538. dataoff = offset;
  539. do {
  540. ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
  541. if (ret <= 0) {
  542. /* No extent found -> no data */
  543. if (ret == 0)
  544. ret = -ENXIO;
  545. inode_unlock(inode);
  546. return ret;
  547. }
  548. last = es.es_lblk;
  549. if (last != start)
  550. dataoff = (loff_t)last << blkbits;
  551. if (!ext4_es_is_unwritten(&es))
  552. break;
  553. /*
  554. * If there is a unwritten extent at this offset,
  555. * it will be as a data or a hole according to page
  556. * cache that has data or not.
  557. */
  558. if (ext4_find_unwritten_pgoff(inode, SEEK_DATA,
  559. es.es_lblk + es.es_len, &dataoff))
  560. break;
  561. last += es.es_len;
  562. dataoff = (loff_t)last << blkbits;
  563. cond_resched();
  564. } while (last <= end);
  565. inode_unlock(inode);
  566. if (dataoff > isize)
  567. return -ENXIO;
  568. return vfs_setpos(file, dataoff, maxsize);
  569. }
  570. /*
  571. * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
  572. */
  573. static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
  574. {
  575. struct inode *inode = file->f_mapping->host;
  576. struct extent_status es;
  577. ext4_lblk_t start, last, end;
  578. loff_t holeoff, isize;
  579. int blkbits;
  580. int ret;
  581. inode_lock(inode);
  582. isize = i_size_read(inode);
  583. if (offset >= isize) {
  584. inode_unlock(inode);
  585. return -ENXIO;
  586. }
  587. blkbits = inode->i_sb->s_blocksize_bits;
  588. start = offset >> blkbits;
  589. last = start;
  590. end = isize >> blkbits;
  591. holeoff = offset;
  592. do {
  593. ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
  594. if (ret < 0) {
  595. inode_unlock(inode);
  596. return ret;
  597. }
  598. /* Found a hole? */
  599. if (ret == 0 || es.es_lblk > last) {
  600. if (last != start)
  601. holeoff = (loff_t)last << blkbits;
  602. break;
  603. }
  604. /*
  605. * If there is a unwritten extent at this offset,
  606. * it will be as a data or a hole according to page
  607. * cache that has data or not.
  608. */
  609. if (ext4_es_is_unwritten(&es) &&
  610. ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
  611. last + es.es_len, &holeoff))
  612. break;
  613. last += es.es_len;
  614. holeoff = (loff_t)last << blkbits;
  615. cond_resched();
  616. } while (last <= end);
  617. inode_unlock(inode);
  618. if (holeoff > isize)
  619. holeoff = isize;
  620. return vfs_setpos(file, holeoff, maxsize);
  621. }
  622. /*
  623. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  624. * by calling generic_file_llseek_size() with the appropriate maxbytes
  625. * value for each.
  626. */
  627. loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
  628. {
  629. struct inode *inode = file->f_mapping->host;
  630. loff_t maxbytes;
  631. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  632. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  633. else
  634. maxbytes = inode->i_sb->s_maxbytes;
  635. switch (whence) {
  636. case SEEK_SET:
  637. case SEEK_CUR:
  638. case SEEK_END:
  639. return generic_file_llseek_size(file, offset, whence,
  640. maxbytes, i_size_read(inode));
  641. case SEEK_DATA:
  642. return ext4_seek_data(file, offset, maxbytes);
  643. case SEEK_HOLE:
  644. return ext4_seek_hole(file, offset, maxbytes);
  645. }
  646. return -EINVAL;
  647. }
  648. const struct file_operations ext4_file_operations = {
  649. .llseek = ext4_llseek,
  650. .read_iter = ext4_file_read_iter,
  651. .write_iter = ext4_file_write_iter,
  652. .unlocked_ioctl = ext4_ioctl,
  653. #ifdef CONFIG_COMPAT
  654. .compat_ioctl = ext4_compat_ioctl,
  655. #endif
  656. .mmap = ext4_file_mmap,
  657. .open = ext4_file_open,
  658. .release = ext4_release_file,
  659. .fsync = ext4_sync_file,
  660. .get_unmapped_area = thp_get_unmapped_area,
  661. .splice_read = generic_file_splice_read,
  662. .splice_write = iter_file_splice_write,
  663. .fallocate = ext4_fallocate,
  664. };
  665. const struct inode_operations ext4_file_inode_operations = {
  666. .setattr = ext4_setattr,
  667. .getattr = ext4_getattr,
  668. .listxattr = ext4_listxattr,
  669. .get_acl = ext4_get_acl,
  670. .set_acl = ext4_set_acl,
  671. .fiemap = ext4_fiemap,
  672. };