file.c 19 KB

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