file.c 18 KB

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