file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/file.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/file.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * ext4 fs regular file handling primitives
  17. *
  18. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  19. * (jj@sunsite.ms.mff.cuni.cz)
  20. */
  21. #include <linux/time.h>
  22. #include <linux/fs.h>
  23. #include <linux/iomap.h>
  24. #include <linux/mount.h>
  25. #include <linux/path.h>
  26. #include <linux/dax.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/pagevec.h>
  29. #include <linux/uio.h>
  30. #include <linux/mman.h>
  31. #include "ext4.h"
  32. #include "ext4_jbd2.h"
  33. #include "xattr.h"
  34. #include "acl.h"
  35. #ifdef CONFIG_FS_DAX
  36. static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
  37. {
  38. struct inode *inode = file_inode(iocb->ki_filp);
  39. ssize_t ret;
  40. if (iocb->ki_flags & IOCB_NOWAIT) {
  41. if (!inode_trylock_shared(inode))
  42. return -EAGAIN;
  43. } else {
  44. inode_lock_shared(inode);
  45. }
  46. /*
  47. * Recheck under inode lock - at this point we are sure it cannot
  48. * change anymore
  49. */
  50. if (!IS_DAX(inode)) {
  51. inode_unlock_shared(inode);
  52. /* Fallback to buffered IO in case we cannot support DAX */
  53. return generic_file_read_iter(iocb, to);
  54. }
  55. ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
  56. inode_unlock_shared(inode);
  57. file_accessed(iocb->ki_filp);
  58. return ret;
  59. }
  60. #endif
  61. static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  62. {
  63. if (unlikely(ext4_forced_shutdown(EXT4_SB(file_inode(iocb->ki_filp)->i_sb))))
  64. return -EIO;
  65. if (!iov_iter_count(to))
  66. return 0; /* skip atime */
  67. #ifdef CONFIG_FS_DAX
  68. if (IS_DAX(file_inode(iocb->ki_filp)))
  69. return ext4_dax_read_iter(iocb, to);
  70. #endif
  71. return generic_file_read_iter(iocb, to);
  72. }
  73. /*
  74. * Called when an inode is released. Note that this is different
  75. * from ext4_file_open: open gets called at every open, but release
  76. * gets called only when /all/ the files are closed.
  77. */
  78. static int ext4_release_file(struct inode *inode, struct file *filp)
  79. {
  80. if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
  81. ext4_alloc_da_blocks(inode);
  82. ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  83. }
  84. /* if we are the last writer on the inode, drop the block reservation */
  85. if ((filp->f_mode & FMODE_WRITE) &&
  86. (atomic_read(&inode->i_writecount) == 1) &&
  87. !EXT4_I(inode)->i_reserved_data_blocks)
  88. {
  89. down_write(&EXT4_I(inode)->i_data_sem);
  90. ext4_discard_preallocations(inode);
  91. up_write(&EXT4_I(inode)->i_data_sem);
  92. }
  93. if (is_dx(inode) && filp->private_data)
  94. ext4_htree_free_dir_info(filp->private_data);
  95. return 0;
  96. }
  97. static void ext4_unwritten_wait(struct inode *inode)
  98. {
  99. wait_queue_head_t *wq = ext4_ioend_wq(inode);
  100. wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_unwritten) == 0));
  101. }
  102. /*
  103. * This tests whether the IO in question is block-aligned or not.
  104. * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
  105. * are converted to written only after the IO is complete. Until they are
  106. * mapped, these blocks appear as holes, so dio_zero_block() will assume that
  107. * it needs to zero out portions of the start and/or end block. If 2 AIO
  108. * threads are at work on the same unwritten block, they must be synchronized
  109. * or one thread will zero the other's data, causing corruption.
  110. */
  111. static int
  112. ext4_unaligned_aio(struct inode *inode, struct iov_iter *from, loff_t pos)
  113. {
  114. struct super_block *sb = inode->i_sb;
  115. int blockmask = sb->s_blocksize - 1;
  116. if (pos >= ALIGN(i_size_read(inode), sb->s_blocksize))
  117. return 0;
  118. if ((pos | iov_iter_alignment(from)) & blockmask)
  119. return 1;
  120. return 0;
  121. }
  122. /* Is IO overwriting allocated and initialized blocks? */
  123. static bool ext4_overwrite_io(struct inode *inode, loff_t pos, loff_t len)
  124. {
  125. struct ext4_map_blocks map;
  126. unsigned int blkbits = inode->i_blkbits;
  127. int err, blklen;
  128. if (pos + len > i_size_read(inode))
  129. return false;
  130. map.m_lblk = pos >> blkbits;
  131. map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
  132. blklen = map.m_len;
  133. err = ext4_map_blocks(NULL, inode, &map, 0);
  134. /*
  135. * 'err==len' means that all of the blocks have been preallocated,
  136. * regardless of whether they have been initialized or not. To exclude
  137. * unwritten extents, we need to check m_flags.
  138. */
  139. return err == blklen && (map.m_flags & EXT4_MAP_MAPPED);
  140. }
  141. static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
  142. {
  143. struct inode *inode = file_inode(iocb->ki_filp);
  144. ssize_t ret;
  145. ret = generic_write_checks(iocb, from);
  146. if (ret <= 0)
  147. return ret;
  148. if (unlikely(IS_IMMUTABLE(inode)))
  149. return -EPERM;
  150. /*
  151. * If we have encountered a bitmap-format file, the size limit
  152. * is smaller than s_maxbytes, which is for extent-mapped files.
  153. */
  154. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  155. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  156. if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
  157. return -EFBIG;
  158. iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
  159. }
  160. return iov_iter_count(from);
  161. }
  162. #ifdef CONFIG_FS_DAX
  163. static ssize_t
  164. ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
  165. {
  166. struct inode *inode = file_inode(iocb->ki_filp);
  167. ssize_t ret;
  168. if (iocb->ki_flags & IOCB_NOWAIT) {
  169. if (!inode_trylock(inode))
  170. return -EAGAIN;
  171. } else {
  172. inode_lock(inode);
  173. }
  174. ret = ext4_write_checks(iocb, from);
  175. if (ret <= 0)
  176. goto out;
  177. ret = file_remove_privs(iocb->ki_filp);
  178. if (ret)
  179. goto out;
  180. ret = file_update_time(iocb->ki_filp);
  181. if (ret)
  182. goto out;
  183. ret = dax_iomap_rw(iocb, from, &ext4_iomap_ops);
  184. out:
  185. inode_unlock(inode);
  186. if (ret > 0)
  187. ret = generic_write_sync(iocb, ret);
  188. return ret;
  189. }
  190. #endif
  191. static ssize_t
  192. ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  193. {
  194. struct inode *inode = file_inode(iocb->ki_filp);
  195. int o_direct = iocb->ki_flags & IOCB_DIRECT;
  196. int unaligned_aio = 0;
  197. int overwrite = 0;
  198. ssize_t ret;
  199. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  200. return -EIO;
  201. #ifdef CONFIG_FS_DAX
  202. if (IS_DAX(inode))
  203. return ext4_dax_write_iter(iocb, from);
  204. #endif
  205. if (!o_direct && (iocb->ki_flags & IOCB_NOWAIT))
  206. return -EOPNOTSUPP;
  207. if (!inode_trylock(inode)) {
  208. if (iocb->ki_flags & IOCB_NOWAIT)
  209. return -EAGAIN;
  210. inode_lock(inode);
  211. }
  212. ret = ext4_write_checks(iocb, from);
  213. if (ret <= 0)
  214. goto out;
  215. /*
  216. * Unaligned direct AIO must be serialized among each other as zeroing
  217. * of partial blocks of two competing unaligned AIOs can result in data
  218. * corruption.
  219. */
  220. if (o_direct && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
  221. !is_sync_kiocb(iocb) &&
  222. ext4_unaligned_aio(inode, from, iocb->ki_pos)) {
  223. unaligned_aio = 1;
  224. ext4_unwritten_wait(inode);
  225. }
  226. iocb->private = &overwrite;
  227. /* Check whether we do a DIO overwrite or not */
  228. if (o_direct && !unaligned_aio) {
  229. if (ext4_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from))) {
  230. if (ext4_should_dioread_nolock(inode))
  231. overwrite = 1;
  232. } else if (iocb->ki_flags & IOCB_NOWAIT) {
  233. ret = -EAGAIN;
  234. goto out;
  235. }
  236. }
  237. ret = __generic_file_write_iter(iocb, from);
  238. /*
  239. * Unaligned direct AIO must be the only IO in flight. Otherwise
  240. * overlapping aligned IO after unaligned might result in data
  241. * corruption.
  242. */
  243. if (ret == -EIOCBQUEUED && unaligned_aio)
  244. ext4_unwritten_wait(inode);
  245. inode_unlock(inode);
  246. if (ret > 0)
  247. ret = generic_write_sync(iocb, ret);
  248. return ret;
  249. out:
  250. inode_unlock(inode);
  251. return ret;
  252. }
  253. #ifdef CONFIG_FS_DAX
  254. static vm_fault_t ext4_dax_huge_fault(struct vm_fault *vmf,
  255. enum page_entry_size pe_size)
  256. {
  257. int error = 0;
  258. vm_fault_t result;
  259. int retries = 0;
  260. handle_t *handle = NULL;
  261. struct inode *inode = file_inode(vmf->vma->vm_file);
  262. struct super_block *sb = inode->i_sb;
  263. /*
  264. * We have to distinguish real writes from writes which will result in a
  265. * COW page; COW writes should *not* poke the journal (the file will not
  266. * be changed). Doing so would cause unintended failures when mounted
  267. * read-only.
  268. *
  269. * We check for VM_SHARED rather than vmf->cow_page since the latter is
  270. * unset for pe_size != PE_SIZE_PTE (i.e. only in do_cow_fault); for
  271. * other sizes, dax_iomap_fault will handle splitting / fallback so that
  272. * we eventually come back with a COW page.
  273. */
  274. bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
  275. (vmf->vma->vm_flags & VM_SHARED);
  276. pfn_t pfn;
  277. if (write) {
  278. sb_start_pagefault(sb);
  279. file_update_time(vmf->vma->vm_file);
  280. down_read(&EXT4_I(inode)->i_mmap_sem);
  281. retry:
  282. handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
  283. EXT4_DATA_TRANS_BLOCKS(sb));
  284. if (IS_ERR(handle)) {
  285. up_read(&EXT4_I(inode)->i_mmap_sem);
  286. sb_end_pagefault(sb);
  287. return VM_FAULT_SIGBUS;
  288. }
  289. } else {
  290. down_read(&EXT4_I(inode)->i_mmap_sem);
  291. }
  292. result = dax_iomap_fault(vmf, pe_size, &pfn, &error, &ext4_iomap_ops);
  293. if (write) {
  294. ext4_journal_stop(handle);
  295. if ((result & VM_FAULT_ERROR) && error == -ENOSPC &&
  296. ext4_should_retry_alloc(sb, &retries))
  297. goto retry;
  298. /* Handling synchronous page fault? */
  299. if (result & VM_FAULT_NEEDDSYNC)
  300. result = dax_finish_sync_fault(vmf, pe_size, pfn);
  301. up_read(&EXT4_I(inode)->i_mmap_sem);
  302. sb_end_pagefault(sb);
  303. } else {
  304. up_read(&EXT4_I(inode)->i_mmap_sem);
  305. }
  306. return result;
  307. }
  308. static vm_fault_t ext4_dax_fault(struct vm_fault *vmf)
  309. {
  310. return ext4_dax_huge_fault(vmf, PE_SIZE_PTE);
  311. }
  312. static const struct vm_operations_struct ext4_dax_vm_ops = {
  313. .fault = ext4_dax_fault,
  314. .huge_fault = ext4_dax_huge_fault,
  315. .page_mkwrite = ext4_dax_fault,
  316. .pfn_mkwrite = ext4_dax_fault,
  317. };
  318. #else
  319. #define ext4_dax_vm_ops ext4_file_vm_ops
  320. #endif
  321. static const struct vm_operations_struct ext4_file_vm_ops = {
  322. .fault = ext4_filemap_fault,
  323. .map_pages = filemap_map_pages,
  324. .page_mkwrite = ext4_page_mkwrite,
  325. };
  326. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  327. {
  328. struct inode *inode = file->f_mapping->host;
  329. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  330. return -EIO;
  331. /*
  332. * We don't support synchronous mappings for non-DAX files. At least
  333. * until someone comes with a sensible use case.
  334. */
  335. if (!IS_DAX(file_inode(file)) && (vma->vm_flags & VM_SYNC))
  336. return -EOPNOTSUPP;
  337. file_accessed(file);
  338. if (IS_DAX(file_inode(file))) {
  339. vma->vm_ops = &ext4_dax_vm_ops;
  340. vma->vm_flags |= VM_HUGEPAGE;
  341. } else {
  342. vma->vm_ops = &ext4_file_vm_ops;
  343. }
  344. return 0;
  345. }
  346. static int ext4_sample_last_mounted(struct super_block *sb,
  347. struct vfsmount *mnt)
  348. {
  349. struct ext4_sb_info *sbi = EXT4_SB(sb);
  350. struct path path;
  351. char buf[64], *cp;
  352. handle_t *handle;
  353. int err;
  354. if (likely(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED))
  355. return 0;
  356. if (sb_rdonly(sb) || !sb_start_intwrite_trylock(sb))
  357. return 0;
  358. sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
  359. /*
  360. * Sample where the filesystem has been mounted and
  361. * store it in the superblock for sysadmin convenience
  362. * when trying to sort through large numbers of block
  363. * devices or filesystem images.
  364. */
  365. memset(buf, 0, sizeof(buf));
  366. path.mnt = mnt;
  367. path.dentry = mnt->mnt_root;
  368. cp = d_path(&path, buf, sizeof(buf));
  369. err = 0;
  370. if (IS_ERR(cp))
  371. goto out;
  372. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  373. err = PTR_ERR(handle);
  374. if (IS_ERR(handle))
  375. goto out;
  376. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  377. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  378. if (err)
  379. goto out_journal;
  380. strlcpy(sbi->s_es->s_last_mounted, cp,
  381. sizeof(sbi->s_es->s_last_mounted));
  382. ext4_handle_dirty_super(handle, sb);
  383. out_journal:
  384. ext4_journal_stop(handle);
  385. out:
  386. sb_end_intwrite(sb);
  387. return err;
  388. }
  389. static int ext4_file_open(struct inode * inode, struct file * filp)
  390. {
  391. int ret;
  392. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  393. return -EIO;
  394. ret = ext4_sample_last_mounted(inode->i_sb, filp->f_path.mnt);
  395. if (ret)
  396. return ret;
  397. ret = fscrypt_file_open(inode, filp);
  398. if (ret)
  399. return ret;
  400. /*
  401. * Set up the jbd2_inode if we are opening the inode for
  402. * writing and the journal is present
  403. */
  404. if (filp->f_mode & FMODE_WRITE) {
  405. ret = ext4_inode_attach_jinode(inode);
  406. if (ret < 0)
  407. return ret;
  408. }
  409. filp->f_mode |= FMODE_NOWAIT;
  410. return dquot_file_open(inode, filp);
  411. }
  412. /*
  413. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  414. * by calling generic_file_llseek_size() with the appropriate maxbytes
  415. * value for each.
  416. */
  417. loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
  418. {
  419. struct inode *inode = file->f_mapping->host;
  420. loff_t maxbytes;
  421. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  422. maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  423. else
  424. maxbytes = inode->i_sb->s_maxbytes;
  425. switch (whence) {
  426. default:
  427. return generic_file_llseek_size(file, offset, whence,
  428. maxbytes, i_size_read(inode));
  429. case SEEK_HOLE:
  430. inode_lock_shared(inode);
  431. offset = iomap_seek_hole(inode, offset, &ext4_iomap_ops);
  432. inode_unlock_shared(inode);
  433. break;
  434. case SEEK_DATA:
  435. inode_lock_shared(inode);
  436. offset = iomap_seek_data(inode, offset, &ext4_iomap_ops);
  437. inode_unlock_shared(inode);
  438. break;
  439. }
  440. if (offset < 0)
  441. return offset;
  442. return vfs_setpos(file, offset, maxbytes);
  443. }
  444. const struct file_operations ext4_file_operations = {
  445. .llseek = ext4_llseek,
  446. .read_iter = ext4_file_read_iter,
  447. .write_iter = ext4_file_write_iter,
  448. .unlocked_ioctl = ext4_ioctl,
  449. #ifdef CONFIG_COMPAT
  450. .compat_ioctl = ext4_compat_ioctl,
  451. #endif
  452. .mmap = ext4_file_mmap,
  453. .mmap_supported_flags = MAP_SYNC,
  454. .open = ext4_file_open,
  455. .release = ext4_release_file,
  456. .fsync = ext4_sync_file,
  457. .get_unmapped_area = thp_get_unmapped_area,
  458. .splice_read = generic_file_splice_read,
  459. .splice_write = iter_file_splice_write,
  460. .fallocate = ext4_fallocate,
  461. };
  462. const struct inode_operations ext4_file_inode_operations = {
  463. .setattr = ext4_setattr,
  464. .getattr = ext4_file_getattr,
  465. .listxattr = ext4_listxattr,
  466. .get_acl = ext4_get_acl,
  467. .set_acl = ext4_set_acl,
  468. .fiemap = ext4_fiemap,
  469. };