file.c 18 KB

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