file.c 17 KB

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