file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 >= ALIGN(i_size_read(inode), sb->s_blocksize))
  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. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  167. if (aio_mutex)
  168. mutex_unlock(aio_mutex);
  169. return ret;
  170. }
  171. #ifdef CONFIG_FS_DAX
  172. static void ext4_end_io_unwritten(struct buffer_head *bh, int uptodate)
  173. {
  174. struct inode *inode = bh->b_assoc_map->host;
  175. /* XXX: breaks on 32-bit > 16TB. Is that even supported? */
  176. loff_t offset = (loff_t)(uintptr_t)bh->b_private << inode->i_blkbits;
  177. int err;
  178. if (!uptodate)
  179. return;
  180. WARN_ON(!buffer_unwritten(bh));
  181. err = ext4_convert_unwritten_extents(NULL, inode, offset, bh->b_size);
  182. }
  183. static int ext4_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  184. {
  185. int result;
  186. handle_t *handle = NULL;
  187. struct inode *inode = file_inode(vma->vm_file);
  188. struct super_block *sb = inode->i_sb;
  189. bool write = vmf->flags & FAULT_FLAG_WRITE;
  190. if (write) {
  191. sb_start_pagefault(sb);
  192. file_update_time(vma->vm_file);
  193. down_read(&EXT4_I(inode)->i_mmap_sem);
  194. handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
  195. EXT4_DATA_TRANS_BLOCKS(sb));
  196. } else
  197. down_read(&EXT4_I(inode)->i_mmap_sem);
  198. if (IS_ERR(handle))
  199. result = VM_FAULT_SIGBUS;
  200. else
  201. result = __dax_fault(vma, vmf, ext4_get_block_dax,
  202. ext4_end_io_unwritten);
  203. if (write) {
  204. if (!IS_ERR(handle))
  205. ext4_journal_stop(handle);
  206. up_read(&EXT4_I(inode)->i_mmap_sem);
  207. sb_end_pagefault(sb);
  208. } else
  209. up_read(&EXT4_I(inode)->i_mmap_sem);
  210. return result;
  211. }
  212. static int ext4_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
  213. pmd_t *pmd, unsigned int flags)
  214. {
  215. int result;
  216. handle_t *handle = NULL;
  217. struct inode *inode = file_inode(vma->vm_file);
  218. struct super_block *sb = inode->i_sb;
  219. bool write = flags & FAULT_FLAG_WRITE;
  220. if (write) {
  221. sb_start_pagefault(sb);
  222. file_update_time(vma->vm_file);
  223. down_read(&EXT4_I(inode)->i_mmap_sem);
  224. handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
  225. ext4_chunk_trans_blocks(inode,
  226. PMD_SIZE / PAGE_SIZE));
  227. } else
  228. down_read(&EXT4_I(inode)->i_mmap_sem);
  229. if (IS_ERR(handle))
  230. result = VM_FAULT_SIGBUS;
  231. else
  232. result = __dax_pmd_fault(vma, addr, pmd, flags,
  233. ext4_get_block_dax, ext4_end_io_unwritten);
  234. if (write) {
  235. if (!IS_ERR(handle))
  236. ext4_journal_stop(handle);
  237. up_read(&EXT4_I(inode)->i_mmap_sem);
  238. sb_end_pagefault(sb);
  239. } else
  240. up_read(&EXT4_I(inode)->i_mmap_sem);
  241. return result;
  242. }
  243. static int ext4_dax_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  244. {
  245. int err;
  246. struct inode *inode = file_inode(vma->vm_file);
  247. sb_start_pagefault(inode->i_sb);
  248. file_update_time(vma->vm_file);
  249. down_read(&EXT4_I(inode)->i_mmap_sem);
  250. err = __dax_mkwrite(vma, vmf, ext4_get_block_dax,
  251. ext4_end_io_unwritten);
  252. up_read(&EXT4_I(inode)->i_mmap_sem);
  253. sb_end_pagefault(inode->i_sb);
  254. return err;
  255. }
  256. /*
  257. * Handle write fault for VM_MIXEDMAP mappings. Similarly to ext4_dax_mkwrite()
  258. * handler we check for races agaist truncate. Note that since we cycle through
  259. * i_mmap_sem, we are sure that also any hole punching that began before we
  260. * were called is finished by now and so if it included part of the file we
  261. * are working on, our pte will get unmapped and the check for pte_same() in
  262. * wp_pfn_shared() fails. Thus fault gets retried and things work out as
  263. * desired.
  264. */
  265. static int ext4_dax_pfn_mkwrite(struct vm_area_struct *vma,
  266. struct vm_fault *vmf)
  267. {
  268. struct inode *inode = file_inode(vma->vm_file);
  269. struct super_block *sb = inode->i_sb;
  270. int ret = VM_FAULT_NOPAGE;
  271. loff_t size;
  272. sb_start_pagefault(sb);
  273. file_update_time(vma->vm_file);
  274. down_read(&EXT4_I(inode)->i_mmap_sem);
  275. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  276. if (vmf->pgoff >= size)
  277. ret = VM_FAULT_SIGBUS;
  278. up_read(&EXT4_I(inode)->i_mmap_sem);
  279. sb_end_pagefault(sb);
  280. return ret;
  281. }
  282. static const struct vm_operations_struct ext4_dax_vm_ops = {
  283. .fault = ext4_dax_fault,
  284. .pmd_fault = ext4_dax_pmd_fault,
  285. .page_mkwrite = ext4_dax_mkwrite,
  286. .pfn_mkwrite = ext4_dax_pfn_mkwrite,
  287. };
  288. #else
  289. #define ext4_dax_vm_ops ext4_file_vm_ops
  290. #endif
  291. static const struct vm_operations_struct ext4_file_vm_ops = {
  292. .fault = ext4_filemap_fault,
  293. .map_pages = filemap_map_pages,
  294. .page_mkwrite = ext4_page_mkwrite,
  295. };
  296. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  297. {
  298. struct inode *inode = file->f_mapping->host;
  299. if (ext4_encrypted_inode(inode)) {
  300. int err = ext4_get_encryption_info(inode);
  301. if (err)
  302. return 0;
  303. if (ext4_encryption_info(inode) == NULL)
  304. return -ENOKEY;
  305. }
  306. file_accessed(file);
  307. if (IS_DAX(file_inode(file))) {
  308. vma->vm_ops = &ext4_dax_vm_ops;
  309. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  310. } else {
  311. vma->vm_ops = &ext4_file_vm_ops;
  312. }
  313. return 0;
  314. }
  315. static int ext4_file_open(struct inode * inode, struct file * filp)
  316. {
  317. struct super_block *sb = inode->i_sb;
  318. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  319. struct vfsmount *mnt = filp->f_path.mnt;
  320. struct path path;
  321. char buf[64], *cp;
  322. int ret;
  323. if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
  324. !(sb->s_flags & MS_RDONLY))) {
  325. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  326. /*
  327. * Sample where the filesystem has been mounted and
  328. * store it in the superblock for sysadmin convenience
  329. * when trying to sort through large numbers of block
  330. * devices or filesystem images.
  331. */
  332. memset(buf, 0, sizeof(buf));
  333. path.mnt = mnt;
  334. path.dentry = mnt->mnt_root;
  335. cp = d_path(&path, buf, sizeof(buf));
  336. if (!IS_ERR(cp)) {
  337. handle_t *handle;
  338. int err;
  339. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  340. if (IS_ERR(handle))
  341. return PTR_ERR(handle);
  342. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  343. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  344. if (err) {
  345. ext4_journal_stop(handle);
  346. return err;
  347. }
  348. strlcpy(sbi->s_es->s_last_mounted, cp,
  349. sizeof(sbi->s_es->s_last_mounted));
  350. ext4_handle_dirty_super(handle, sb);
  351. ext4_journal_stop(handle);
  352. }
  353. }
  354. if (ext4_encrypted_inode(inode)) {
  355. ret = ext4_get_encryption_info(inode);
  356. if (ret)
  357. return -EACCES;
  358. if (ext4_encryption_info(inode) == NULL)
  359. return -ENOKEY;
  360. }
  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. return dquot_file_open(inode, filp);
  371. }
  372. /*
  373. * Here we use ext4_map_blocks() to get a block mapping for a extent-based
  374. * file rather than ext4_ext_walk_space() because we can introduce
  375. * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
  376. * function. When extent status tree has been fully implemented, it will
  377. * track all extent status for a file and we can directly use it to
  378. * retrieve the offset for SEEK_DATA/SEEK_HOLE.
  379. */
  380. /*
  381. * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
  382. * lookup page cache to check whether or not there has some data between
  383. * [startoff, endoff] because, if this range contains an unwritten extent,
  384. * we determine this extent as a data or a hole according to whether the
  385. * page cache has data or not.
  386. */
  387. static int ext4_find_unwritten_pgoff(struct inode *inode,
  388. int whence,
  389. struct ext4_map_blocks *map,
  390. loff_t *offset)
  391. {
  392. struct pagevec pvec;
  393. unsigned int blkbits;
  394. pgoff_t index;
  395. pgoff_t end;
  396. loff_t endoff;
  397. loff_t startoff;
  398. loff_t lastoff;
  399. int found = 0;
  400. blkbits = inode->i_sb->s_blocksize_bits;
  401. startoff = *offset;
  402. lastoff = startoff;
  403. endoff = (loff_t)(map->m_lblk + map->m_len) << blkbits;
  404. index = startoff >> PAGE_CACHE_SHIFT;
  405. end = endoff >> PAGE_CACHE_SHIFT;
  406. pagevec_init(&pvec, 0);
  407. do {
  408. int i, num;
  409. unsigned long nr_pages;
  410. num = min_t(pgoff_t, end - index, PAGEVEC_SIZE - 1) + 1;
  411. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
  412. (pgoff_t)num);
  413. if (nr_pages == 0)
  414. break;
  415. for (i = 0; i < nr_pages; i++) {
  416. struct page *page = pvec.pages[i];
  417. struct buffer_head *bh, *head;
  418. /*
  419. * If current offset is smaller than the page offset,
  420. * there is a hole at this offset.
  421. */
  422. if (whence == SEEK_HOLE && lastoff < endoff &&
  423. lastoff < page_offset(pvec.pages[i])) {
  424. found = 1;
  425. *offset = lastoff;
  426. goto out;
  427. }
  428. if (page->index > end)
  429. goto out;
  430. lock_page(page);
  431. if (unlikely(page->mapping != inode->i_mapping)) {
  432. unlock_page(page);
  433. continue;
  434. }
  435. if (!page_has_buffers(page)) {
  436. unlock_page(page);
  437. continue;
  438. }
  439. if (page_has_buffers(page)) {
  440. lastoff = page_offset(page);
  441. bh = head = page_buffers(page);
  442. do {
  443. if (lastoff + bh->b_size <= startoff)
  444. goto next;
  445. if (buffer_uptodate(bh) ||
  446. buffer_unwritten(bh)) {
  447. if (whence == SEEK_DATA)
  448. found = 1;
  449. } else {
  450. if (whence == SEEK_HOLE)
  451. found = 1;
  452. }
  453. if (found) {
  454. *offset = max_t(loff_t,
  455. startoff, lastoff);
  456. unlock_page(page);
  457. goto out;
  458. }
  459. next:
  460. lastoff += bh->b_size;
  461. bh = bh->b_this_page;
  462. } while (bh != head);
  463. }
  464. lastoff = page_offset(page) + PAGE_SIZE;
  465. unlock_page(page);
  466. }
  467. /* The no. of pages is less than our desired, we are done. */
  468. if (nr_pages < num)
  469. break;
  470. index = pvec.pages[i - 1]->index + 1;
  471. pagevec_release(&pvec);
  472. } while (index <= end);
  473. if (whence == SEEK_HOLE && lastoff < endoff) {
  474. found = 1;
  475. *offset = lastoff;
  476. }
  477. out:
  478. pagevec_release(&pvec);
  479. return found;
  480. }
  481. /*
  482. * ext4_seek_data() retrieves the offset for SEEK_DATA.
  483. */
  484. static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
  485. {
  486. struct inode *inode = file->f_mapping->host;
  487. struct ext4_map_blocks map;
  488. struct extent_status es;
  489. ext4_lblk_t start, last, end;
  490. loff_t dataoff, isize;
  491. int blkbits;
  492. int ret = 0;
  493. mutex_lock(&inode->i_mutex);
  494. isize = i_size_read(inode);
  495. if (offset < 0 || offset >= isize) {
  496. mutex_unlock(&inode->i_mutex);
  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. map.m_lblk = last;
  506. map.m_len = end - last + 1;
  507. ret = ext4_map_blocks(NULL, inode, &map, 0);
  508. if (ret > 0 && !(map.m_flags & EXT4_MAP_UNWRITTEN)) {
  509. if (last != start)
  510. dataoff = (loff_t)last << blkbits;
  511. break;
  512. }
  513. /*
  514. * If there is a delay extent at this offset,
  515. * it will be as a data.
  516. */
  517. ext4_es_find_delayed_extent_range(inode, last, last, &es);
  518. if (es.es_len != 0 && in_range(last, es.es_lblk, es.es_len)) {
  519. if (last != start)
  520. dataoff = (loff_t)last << blkbits;
  521. break;
  522. }
  523. /*
  524. * If there is a unwritten extent at this offset,
  525. * it will be as a data or a hole according to page
  526. * cache that has data or not.
  527. */
  528. if (map.m_flags & EXT4_MAP_UNWRITTEN) {
  529. int unwritten;
  530. unwritten = ext4_find_unwritten_pgoff(inode, SEEK_DATA,
  531. &map, &dataoff);
  532. if (unwritten)
  533. break;
  534. }
  535. last++;
  536. dataoff = (loff_t)last << blkbits;
  537. } while (last <= end);
  538. mutex_unlock(&inode->i_mutex);
  539. if (dataoff > isize)
  540. return -ENXIO;
  541. return vfs_setpos(file, dataoff, maxsize);
  542. }
  543. /*
  544. * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
  545. */
  546. static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
  547. {
  548. struct inode *inode = file->f_mapping->host;
  549. struct ext4_map_blocks map;
  550. struct extent_status es;
  551. ext4_lblk_t start, last, end;
  552. loff_t holeoff, isize;
  553. int blkbits;
  554. int ret = 0;
  555. mutex_lock(&inode->i_mutex);
  556. isize = i_size_read(inode);
  557. if (offset < 0 || offset >= isize) {
  558. mutex_unlock(&inode->i_mutex);
  559. return -ENXIO;
  560. }
  561. blkbits = inode->i_sb->s_blocksize_bits;
  562. start = offset >> blkbits;
  563. last = start;
  564. end = isize >> blkbits;
  565. holeoff = offset;
  566. do {
  567. map.m_lblk = last;
  568. map.m_len = end - last + 1;
  569. ret = ext4_map_blocks(NULL, inode, &map, 0);
  570. if (ret > 0 && !(map.m_flags & EXT4_MAP_UNWRITTEN)) {
  571. last += ret;
  572. holeoff = (loff_t)last << blkbits;
  573. continue;
  574. }
  575. /*
  576. * If there is a delay extent at this offset,
  577. * we will skip this extent.
  578. */
  579. ext4_es_find_delayed_extent_range(inode, last, last, &es);
  580. if (es.es_len != 0 && in_range(last, es.es_lblk, es.es_len)) {
  581. last = es.es_lblk + es.es_len;
  582. holeoff = (loff_t)last << blkbits;
  583. continue;
  584. }
  585. /*
  586. * If there is a unwritten extent at this offset,
  587. * it will be as a data or a hole according to page
  588. * cache that has data or not.
  589. */
  590. if (map.m_flags & EXT4_MAP_UNWRITTEN) {
  591. int unwritten;
  592. unwritten = ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
  593. &map, &holeoff);
  594. if (!unwritten) {
  595. last += ret;
  596. holeoff = (loff_t)last << blkbits;
  597. continue;
  598. }
  599. }
  600. /* find a hole */
  601. break;
  602. } while (last <= end);
  603. mutex_unlock(&inode->i_mutex);
  604. if (holeoff > isize)
  605. holeoff = isize;
  606. return vfs_setpos(file, holeoff, maxsize);
  607. }
  608. /*
  609. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  610. * by calling generic_file_llseek_size() with the appropriate maxbytes
  611. * value for each.
  612. */
  613. loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
  614. {
  615. struct inode *inode = file->f_mapping->host;
  616. loff_t maxbytes;
  617. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  618. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  619. else
  620. maxbytes = inode->i_sb->s_maxbytes;
  621. switch (whence) {
  622. case SEEK_SET:
  623. case SEEK_CUR:
  624. case SEEK_END:
  625. return generic_file_llseek_size(file, offset, whence,
  626. maxbytes, i_size_read(inode));
  627. case SEEK_DATA:
  628. return ext4_seek_data(file, offset, maxbytes);
  629. case SEEK_HOLE:
  630. return ext4_seek_hole(file, offset, maxbytes);
  631. }
  632. return -EINVAL;
  633. }
  634. const struct file_operations ext4_file_operations = {
  635. .llseek = ext4_llseek,
  636. .read_iter = generic_file_read_iter,
  637. .write_iter = ext4_file_write_iter,
  638. .unlocked_ioctl = ext4_ioctl,
  639. #ifdef CONFIG_COMPAT
  640. .compat_ioctl = ext4_compat_ioctl,
  641. #endif
  642. .mmap = ext4_file_mmap,
  643. .open = ext4_file_open,
  644. .release = ext4_release_file,
  645. .fsync = ext4_sync_file,
  646. .splice_read = generic_file_splice_read,
  647. .splice_write = iter_file_splice_write,
  648. .fallocate = ext4_fallocate,
  649. };
  650. const struct inode_operations ext4_file_inode_operations = {
  651. .setattr = ext4_setattr,
  652. .getattr = ext4_getattr,
  653. .setxattr = generic_setxattr,
  654. .getxattr = generic_getxattr,
  655. .listxattr = ext4_listxattr,
  656. .removexattr = generic_removexattr,
  657. .get_acl = ext4_get_acl,
  658. .set_acl = ext4_set_acl,
  659. .fiemap = ext4_fiemap,
  660. };