file.c 18 KB

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