file.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext2/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. * ext2 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/pagemap.h>
  23. #include <linux/dax.h>
  24. #include <linux/quotaops.h>
  25. #include <linux/iomap.h>
  26. #include <linux/uio.h>
  27. #include "ext2.h"
  28. #include "xattr.h"
  29. #include "acl.h"
  30. #ifdef CONFIG_FS_DAX
  31. static ssize_t ext2_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
  32. {
  33. struct inode *inode = iocb->ki_filp->f_mapping->host;
  34. ssize_t ret;
  35. if (!iov_iter_count(to))
  36. return 0; /* skip atime */
  37. inode_lock_shared(inode);
  38. ret = dax_iomap_rw(iocb, to, &ext2_iomap_ops);
  39. inode_unlock_shared(inode);
  40. file_accessed(iocb->ki_filp);
  41. return ret;
  42. }
  43. static ssize_t ext2_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
  44. {
  45. struct file *file = iocb->ki_filp;
  46. struct inode *inode = file->f_mapping->host;
  47. ssize_t ret;
  48. inode_lock(inode);
  49. ret = generic_write_checks(iocb, from);
  50. if (ret <= 0)
  51. goto out_unlock;
  52. ret = file_remove_privs(file);
  53. if (ret)
  54. goto out_unlock;
  55. ret = file_update_time(file);
  56. if (ret)
  57. goto out_unlock;
  58. ret = dax_iomap_rw(iocb, from, &ext2_iomap_ops);
  59. if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
  60. i_size_write(inode, iocb->ki_pos);
  61. mark_inode_dirty(inode);
  62. }
  63. out_unlock:
  64. inode_unlock(inode);
  65. if (ret > 0)
  66. ret = generic_write_sync(iocb, ret);
  67. return ret;
  68. }
  69. /*
  70. * The lock ordering for ext2 DAX fault paths is:
  71. *
  72. * mmap_sem (MM)
  73. * sb_start_pagefault (vfs, freeze)
  74. * ext2_inode_info->dax_sem
  75. * address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX)
  76. * ext2_inode_info->truncate_mutex
  77. *
  78. * The default page_lock and i_size verification done by non-DAX fault paths
  79. * is sufficient because ext2 doesn't support hole punching.
  80. */
  81. static vm_fault_t ext2_dax_fault(struct vm_fault *vmf)
  82. {
  83. struct inode *inode = file_inode(vmf->vma->vm_file);
  84. struct ext2_inode_info *ei = EXT2_I(inode);
  85. vm_fault_t ret;
  86. if (vmf->flags & FAULT_FLAG_WRITE) {
  87. sb_start_pagefault(inode->i_sb);
  88. file_update_time(vmf->vma->vm_file);
  89. }
  90. down_read(&ei->dax_sem);
  91. ret = dax_iomap_fault(vmf, PE_SIZE_PTE, NULL, NULL, &ext2_iomap_ops);
  92. up_read(&ei->dax_sem);
  93. if (vmf->flags & FAULT_FLAG_WRITE)
  94. sb_end_pagefault(inode->i_sb);
  95. return ret;
  96. }
  97. static const struct vm_operations_struct ext2_dax_vm_ops = {
  98. .fault = ext2_dax_fault,
  99. /*
  100. * .huge_fault is not supported for DAX because allocation in ext2
  101. * cannot be reliably aligned to huge page sizes and so pmd faults
  102. * will always fail and fail back to regular faults.
  103. */
  104. .page_mkwrite = ext2_dax_fault,
  105. .pfn_mkwrite = ext2_dax_fault,
  106. };
  107. static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma)
  108. {
  109. if (!IS_DAX(file_inode(file)))
  110. return generic_file_mmap(file, vma);
  111. file_accessed(file);
  112. vma->vm_ops = &ext2_dax_vm_ops;
  113. return 0;
  114. }
  115. #else
  116. #define ext2_file_mmap generic_file_mmap
  117. #endif
  118. /*
  119. * Called when filp is released. This happens when all file descriptors
  120. * for a single struct file are closed. Note that different open() calls
  121. * for the same file yield different struct file structures.
  122. */
  123. static int ext2_release_file (struct inode * inode, struct file * filp)
  124. {
  125. if (filp->f_mode & FMODE_WRITE) {
  126. mutex_lock(&EXT2_I(inode)->truncate_mutex);
  127. ext2_discard_reservation(inode);
  128. mutex_unlock(&EXT2_I(inode)->truncate_mutex);
  129. }
  130. return 0;
  131. }
  132. int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  133. {
  134. int ret;
  135. struct super_block *sb = file->f_mapping->host->i_sb;
  136. ret = generic_file_fsync(file, start, end, datasync);
  137. if (ret == -EIO)
  138. /* We don't really know where the IO error happened... */
  139. ext2_error(sb, __func__,
  140. "detected IO error when writing metadata buffers");
  141. return ret;
  142. }
  143. static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  144. {
  145. #ifdef CONFIG_FS_DAX
  146. if (IS_DAX(iocb->ki_filp->f_mapping->host))
  147. return ext2_dax_read_iter(iocb, to);
  148. #endif
  149. return generic_file_read_iter(iocb, to);
  150. }
  151. static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  152. {
  153. #ifdef CONFIG_FS_DAX
  154. if (IS_DAX(iocb->ki_filp->f_mapping->host))
  155. return ext2_dax_write_iter(iocb, from);
  156. #endif
  157. return generic_file_write_iter(iocb, from);
  158. }
  159. const struct file_operations ext2_file_operations = {
  160. .llseek = generic_file_llseek,
  161. .read_iter = ext2_file_read_iter,
  162. .write_iter = ext2_file_write_iter,
  163. .unlocked_ioctl = ext2_ioctl,
  164. #ifdef CONFIG_COMPAT
  165. .compat_ioctl = ext2_compat_ioctl,
  166. #endif
  167. .mmap = ext2_file_mmap,
  168. .open = dquot_file_open,
  169. .release = ext2_release_file,
  170. .fsync = ext2_fsync,
  171. .get_unmapped_area = thp_get_unmapped_area,
  172. .splice_read = generic_file_splice_read,
  173. .splice_write = iter_file_splice_write,
  174. };
  175. const struct inode_operations ext2_file_inode_operations = {
  176. #ifdef CONFIG_EXT2_FS_XATTR
  177. .listxattr = ext2_listxattr,
  178. #endif
  179. .setattr = ext2_setattr,
  180. .get_acl = ext2_get_acl,
  181. .set_acl = ext2_set_acl,
  182. .fiemap = ext2_fiemap,
  183. };