file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. if (IS_ERR(handle)) {
  267. up_read(&EXT4_I(inode)->i_mmap_sem);
  268. sb_end_pagefault(sb);
  269. return VM_FAULT_SIGBUS;
  270. }
  271. } else {
  272. down_read(&EXT4_I(inode)->i_mmap_sem);
  273. }
  274. result = dax_iomap_fault(vmf, pe_size, NULL, &ext4_iomap_ops);
  275. if (write) {
  276. ext4_journal_stop(handle);
  277. up_read(&EXT4_I(inode)->i_mmap_sem);
  278. sb_end_pagefault(sb);
  279. } else {
  280. up_read(&EXT4_I(inode)->i_mmap_sem);
  281. }
  282. return result;
  283. }
  284. static int ext4_dax_fault(struct vm_fault *vmf)
  285. {
  286. return ext4_dax_huge_fault(vmf, PE_SIZE_PTE);
  287. }
  288. static const struct vm_operations_struct ext4_dax_vm_ops = {
  289. .fault = ext4_dax_fault,
  290. .huge_fault = ext4_dax_huge_fault,
  291. .page_mkwrite = ext4_dax_fault,
  292. .pfn_mkwrite = ext4_dax_fault,
  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 (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  306. return -EIO;
  307. file_accessed(file);
  308. if (IS_DAX(file_inode(file))) {
  309. vma->vm_ops = &ext4_dax_vm_ops;
  310. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  311. } else {
  312. vma->vm_ops = &ext4_file_vm_ops;
  313. }
  314. return 0;
  315. }
  316. static int ext4_file_open(struct inode * inode, struct file * filp)
  317. {
  318. struct super_block *sb = inode->i_sb;
  319. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  320. struct vfsmount *mnt = filp->f_path.mnt;
  321. struct dentry *dir;
  322. struct path path;
  323. char buf[64], *cp;
  324. int ret;
  325. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  326. return -EIO;
  327. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  328. !sb_rdonly(sb))) {
  329. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  330. /*
  331. * Sample where the filesystem has been mounted and
  332. * store it in the superblock for sysadmin convenience
  333. * when trying to sort through large numbers of block
  334. * devices or filesystem images.
  335. */
  336. memset(buf, 0, sizeof(buf));
  337. path.mnt = mnt;
  338. path.dentry = mnt->mnt_root;
  339. cp = d_path(&path, buf, sizeof(buf));
  340. if (!IS_ERR(cp)) {
  341. handle_t *handle;
  342. int err;
  343. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  344. if (IS_ERR(handle))
  345. return PTR_ERR(handle);
  346. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  347. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  348. if (err) {
  349. ext4_journal_stop(handle);
  350. return err;
  351. }
  352. strlcpy(sbi->s_es->s_last_mounted, cp,
  353. sizeof(sbi->s_es->s_last_mounted));
  354. ext4_handle_dirty_super(handle, sb);
  355. ext4_journal_stop(handle);
  356. }
  357. }
  358. if (ext4_encrypted_inode(inode)) {
  359. ret = fscrypt_get_encryption_info(inode);
  360. if (ret)
  361. return -EACCES;
  362. if (!fscrypt_has_encryption_key(inode))
  363. return -ENOKEY;
  364. }
  365. dir = dget_parent(file_dentry(filp));
  366. if (ext4_encrypted_inode(d_inode(dir)) &&
  367. !fscrypt_has_permitted_context(d_inode(dir), inode)) {
  368. ext4_warning(inode->i_sb,
  369. "Inconsistent encryption contexts: %lu/%lu",
  370. (unsigned long) d_inode(dir)->i_ino,
  371. (unsigned long) inode->i_ino);
  372. dput(dir);
  373. return -EPERM;
  374. }
  375. dput(dir);
  376. /*
  377. * Set up the jbd2_inode if we are opening the inode for
  378. * writing and the journal is present
  379. */
  380. if (filp->f_mode & FMODE_WRITE) {
  381. ret = ext4_inode_attach_jinode(inode);
  382. if (ret < 0)
  383. return ret;
  384. }
  385. filp->f_mode |= FMODE_NOWAIT;
  386. return dquot_file_open(inode, filp);
  387. }
  388. /*
  389. * Here we use ext4_map_blocks() to get a block mapping for a extent-based
  390. * file rather than ext4_ext_walk_space() because we can introduce
  391. * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
  392. * function. When extent status tree has been fully implemented, it will
  393. * track all extent status for a file and we can directly use it to
  394. * retrieve the offset for SEEK_DATA/SEEK_HOLE.
  395. */
  396. /*
  397. * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
  398. * lookup page cache to check whether or not there has some data between
  399. * [startoff, endoff] because, if this range contains an unwritten extent,
  400. * we determine this extent as a data or a hole according to whether the
  401. * page cache has data or not.
  402. */
  403. static int ext4_find_unwritten_pgoff(struct inode *inode,
  404. int whence,
  405. ext4_lblk_t end_blk,
  406. loff_t *offset)
  407. {
  408. struct pagevec pvec;
  409. unsigned int blkbits;
  410. pgoff_t index;
  411. pgoff_t end;
  412. loff_t endoff;
  413. loff_t startoff;
  414. loff_t lastoff;
  415. int found = 0;
  416. blkbits = inode->i_sb->s_blocksize_bits;
  417. startoff = *offset;
  418. lastoff = startoff;
  419. endoff = (loff_t)end_blk << blkbits;
  420. index = startoff >> PAGE_SHIFT;
  421. end = (endoff - 1) >> PAGE_SHIFT;
  422. pagevec_init(&pvec, 0);
  423. do {
  424. int i;
  425. unsigned long nr_pages;
  426. nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
  427. &index, end);
  428. if (nr_pages == 0)
  429. break;
  430. for (i = 0; i < nr_pages; i++) {
  431. struct page *page = pvec.pages[i];
  432. struct buffer_head *bh, *head;
  433. /*
  434. * If current offset is smaller than the page offset,
  435. * there is a hole at this offset.
  436. */
  437. if (whence == SEEK_HOLE && lastoff < endoff &&
  438. lastoff < page_offset(pvec.pages[i])) {
  439. found = 1;
  440. *offset = lastoff;
  441. goto out;
  442. }
  443. lock_page(page);
  444. if (unlikely(page->mapping != inode->i_mapping)) {
  445. unlock_page(page);
  446. continue;
  447. }
  448. if (!page_has_buffers(page)) {
  449. unlock_page(page);
  450. continue;
  451. }
  452. if (page_has_buffers(page)) {
  453. lastoff = page_offset(page);
  454. bh = head = page_buffers(page);
  455. do {
  456. if (lastoff + bh->b_size <= startoff)
  457. goto next;
  458. if (buffer_uptodate(bh) ||
  459. buffer_unwritten(bh)) {
  460. if (whence == SEEK_DATA)
  461. found = 1;
  462. } else {
  463. if (whence == SEEK_HOLE)
  464. found = 1;
  465. }
  466. if (found) {
  467. *offset = max_t(loff_t,
  468. startoff, lastoff);
  469. unlock_page(page);
  470. goto out;
  471. }
  472. next:
  473. lastoff += bh->b_size;
  474. bh = bh->b_this_page;
  475. } while (bh != head);
  476. }
  477. lastoff = page_offset(page) + PAGE_SIZE;
  478. unlock_page(page);
  479. }
  480. pagevec_release(&pvec);
  481. } while (index <= end);
  482. /* There are no pages upto endoff - that would be a hole in there. */
  483. if (whence == SEEK_HOLE && lastoff < endoff) {
  484. found = 1;
  485. *offset = lastoff;
  486. }
  487. out:
  488. pagevec_release(&pvec);
  489. return found;
  490. }
  491. /*
  492. * ext4_seek_data() retrieves the offset for SEEK_DATA.
  493. */
  494. static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
  495. {
  496. struct inode *inode = file->f_mapping->host;
  497. struct extent_status es;
  498. ext4_lblk_t start, last, end;
  499. loff_t dataoff, isize;
  500. int blkbits;
  501. int ret;
  502. inode_lock(inode);
  503. isize = i_size_read(inode);
  504. if (offset < 0 || offset >= isize) {
  505. inode_unlock(inode);
  506. return -ENXIO;
  507. }
  508. blkbits = inode->i_sb->s_blocksize_bits;
  509. start = offset >> blkbits;
  510. last = start;
  511. end = isize >> blkbits;
  512. dataoff = offset;
  513. do {
  514. ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
  515. if (ret <= 0) {
  516. /* No extent found -> no data */
  517. if (ret == 0)
  518. ret = -ENXIO;
  519. inode_unlock(inode);
  520. return ret;
  521. }
  522. last = es.es_lblk;
  523. if (last != start)
  524. dataoff = (loff_t)last << blkbits;
  525. if (!ext4_es_is_unwritten(&es))
  526. break;
  527. /*
  528. * If there is a unwritten extent at this offset,
  529. * it will be as a data or a hole according to page
  530. * cache that has data or not.
  531. */
  532. if (ext4_find_unwritten_pgoff(inode, SEEK_DATA,
  533. es.es_lblk + es.es_len, &dataoff))
  534. break;
  535. last += es.es_len;
  536. dataoff = (loff_t)last << blkbits;
  537. cond_resched();
  538. } while (last <= end);
  539. inode_unlock(inode);
  540. if (dataoff > isize)
  541. return -ENXIO;
  542. return vfs_setpos(file, dataoff, maxsize);
  543. }
  544. /*
  545. * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
  546. */
  547. static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
  548. {
  549. struct inode *inode = file->f_mapping->host;
  550. struct extent_status es;
  551. ext4_lblk_t start, last, end;
  552. loff_t holeoff, isize;
  553. int blkbits;
  554. int ret;
  555. inode_lock(inode);
  556. isize = i_size_read(inode);
  557. if (offset < 0 || offset >= isize) {
  558. inode_unlock(inode);
  559. return -ENXIO;
  560. }
  561. blkbits = inode->i_sb->s_blocksize_bits;
  562. start = offset >> blkbits;
  563. last = start;
  564. end = isize >> blkbits;
  565. holeoff = offset;
  566. do {
  567. ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
  568. if (ret < 0) {
  569. inode_unlock(inode);
  570. return ret;
  571. }
  572. /* Found a hole? */
  573. if (ret == 0 || es.es_lblk > last) {
  574. if (last != start)
  575. holeoff = (loff_t)last << blkbits;
  576. break;
  577. }
  578. /*
  579. * If there is a unwritten extent at this offset,
  580. * it will be as a data or a hole according to page
  581. * cache that has data or not.
  582. */
  583. if (ext4_es_is_unwritten(&es) &&
  584. ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
  585. last + es.es_len, &holeoff))
  586. break;
  587. last += es.es_len;
  588. holeoff = (loff_t)last << blkbits;
  589. cond_resched();
  590. } while (last <= end);
  591. inode_unlock(inode);
  592. if (holeoff > isize)
  593. holeoff = isize;
  594. return vfs_setpos(file, holeoff, maxsize);
  595. }
  596. /*
  597. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  598. * by calling generic_file_llseek_size() with the appropriate maxbytes
  599. * value for each.
  600. */
  601. loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
  602. {
  603. struct inode *inode = file->f_mapping->host;
  604. loff_t maxbytes;
  605. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  606. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  607. else
  608. maxbytes = inode->i_sb->s_maxbytes;
  609. switch (whence) {
  610. case SEEK_SET:
  611. case SEEK_CUR:
  612. case SEEK_END:
  613. return generic_file_llseek_size(file, offset, whence,
  614. maxbytes, i_size_read(inode));
  615. case SEEK_DATA:
  616. return ext4_seek_data(file, offset, maxbytes);
  617. case SEEK_HOLE:
  618. return ext4_seek_hole(file, offset, maxbytes);
  619. }
  620. return -EINVAL;
  621. }
  622. const struct file_operations ext4_file_operations = {
  623. .llseek = ext4_llseek,
  624. .read_iter = ext4_file_read_iter,
  625. .write_iter = ext4_file_write_iter,
  626. .unlocked_ioctl = ext4_ioctl,
  627. #ifdef CONFIG_COMPAT
  628. .compat_ioctl = ext4_compat_ioctl,
  629. #endif
  630. .mmap = ext4_file_mmap,
  631. .open = ext4_file_open,
  632. .release = ext4_release_file,
  633. .fsync = ext4_sync_file,
  634. .get_unmapped_area = thp_get_unmapped_area,
  635. .splice_read = generic_file_splice_read,
  636. .splice_write = iter_file_splice_write,
  637. .fallocate = ext4_fallocate,
  638. };
  639. const struct inode_operations ext4_file_inode_operations = {
  640. .setattr = ext4_setattr,
  641. .getattr = ext4_file_getattr,
  642. .listxattr = ext4_listxattr,
  643. .get_acl = ext4_get_acl,
  644. .set_acl = ext4_set_acl,
  645. .fiemap = ext4_fiemap,
  646. };