file.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * linux/fs/ext2/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. * ext2 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/pagemap.h>
  22. #include <linux/dax.h>
  23. #include <linux/quotaops.h>
  24. #include "ext2.h"
  25. #include "xattr.h"
  26. #include "acl.h"
  27. #ifdef CONFIG_FS_DAX
  28. /*
  29. * The lock ordering for ext2 DAX fault paths is:
  30. *
  31. * mmap_sem (MM)
  32. * sb_start_pagefault (vfs, freeze)
  33. * ext2_inode_info->dax_sem
  34. * address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX)
  35. * ext2_inode_info->truncate_mutex
  36. *
  37. * The default page_lock and i_size verification done by non-DAX fault paths
  38. * is sufficient because ext2 doesn't support hole punching.
  39. */
  40. static int ext2_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  41. {
  42. struct inode *inode = file_inode(vma->vm_file);
  43. struct ext2_inode_info *ei = EXT2_I(inode);
  44. int ret;
  45. if (vmf->flags & FAULT_FLAG_WRITE) {
  46. sb_start_pagefault(inode->i_sb);
  47. file_update_time(vma->vm_file);
  48. }
  49. down_read(&ei->dax_sem);
  50. ret = __dax_fault(vma, vmf, ext2_get_block, NULL);
  51. up_read(&ei->dax_sem);
  52. if (vmf->flags & FAULT_FLAG_WRITE)
  53. sb_end_pagefault(inode->i_sb);
  54. return ret;
  55. }
  56. static int ext2_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
  57. pmd_t *pmd, unsigned int flags)
  58. {
  59. struct inode *inode = file_inode(vma->vm_file);
  60. struct ext2_inode_info *ei = EXT2_I(inode);
  61. int ret;
  62. if (flags & FAULT_FLAG_WRITE) {
  63. sb_start_pagefault(inode->i_sb);
  64. file_update_time(vma->vm_file);
  65. }
  66. down_read(&ei->dax_sem);
  67. ret = __dax_pmd_fault(vma, addr, pmd, flags, ext2_get_block, NULL);
  68. up_read(&ei->dax_sem);
  69. if (flags & FAULT_FLAG_WRITE)
  70. sb_end_pagefault(inode->i_sb);
  71. return ret;
  72. }
  73. static int ext2_dax_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  74. {
  75. struct inode *inode = file_inode(vma->vm_file);
  76. struct ext2_inode_info *ei = EXT2_I(inode);
  77. int ret;
  78. sb_start_pagefault(inode->i_sb);
  79. file_update_time(vma->vm_file);
  80. down_read(&ei->dax_sem);
  81. ret = __dax_mkwrite(vma, vmf, ext2_get_block, NULL);
  82. up_read(&ei->dax_sem);
  83. sb_end_pagefault(inode->i_sb);
  84. return ret;
  85. }
  86. static int ext2_dax_pfn_mkwrite(struct vm_area_struct *vma,
  87. struct vm_fault *vmf)
  88. {
  89. struct inode *inode = file_inode(vma->vm_file);
  90. struct ext2_inode_info *ei = EXT2_I(inode);
  91. loff_t size;
  92. int ret;
  93. sb_start_pagefault(inode->i_sb);
  94. file_update_time(vma->vm_file);
  95. down_read(&ei->dax_sem);
  96. /* check that the faulting page hasn't raced with truncate */
  97. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  98. if (vmf->pgoff >= size)
  99. ret = VM_FAULT_SIGBUS;
  100. else
  101. ret = dax_pfn_mkwrite(vma, vmf);
  102. up_read(&ei->dax_sem);
  103. sb_end_pagefault(inode->i_sb);
  104. return ret;
  105. }
  106. static const struct vm_operations_struct ext2_dax_vm_ops = {
  107. .fault = ext2_dax_fault,
  108. .pmd_fault = ext2_dax_pmd_fault,
  109. .page_mkwrite = ext2_dax_mkwrite,
  110. .pfn_mkwrite = ext2_dax_pfn_mkwrite,
  111. };
  112. static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma)
  113. {
  114. if (!IS_DAX(file_inode(file)))
  115. return generic_file_mmap(file, vma);
  116. file_accessed(file);
  117. vma->vm_ops = &ext2_dax_vm_ops;
  118. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  119. return 0;
  120. }
  121. #else
  122. #define ext2_file_mmap generic_file_mmap
  123. #endif
  124. /*
  125. * Called when filp is released. This happens when all file descriptors
  126. * for a single struct file are closed. Note that different open() calls
  127. * for the same file yield different struct file structures.
  128. */
  129. static int ext2_release_file (struct inode * inode, struct file * filp)
  130. {
  131. if (filp->f_mode & FMODE_WRITE) {
  132. mutex_lock(&EXT2_I(inode)->truncate_mutex);
  133. ext2_discard_reservation(inode);
  134. mutex_unlock(&EXT2_I(inode)->truncate_mutex);
  135. }
  136. return 0;
  137. }
  138. int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  139. {
  140. int ret;
  141. struct super_block *sb = file->f_mapping->host->i_sb;
  142. struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
  143. ret = generic_file_fsync(file, start, end, datasync);
  144. if (ret == -EIO || test_and_clear_bit(AS_EIO, &mapping->flags)) {
  145. /* We don't really know where the IO error happened... */
  146. ext2_error(sb, __func__,
  147. "detected IO error when writing metadata buffers");
  148. ret = -EIO;
  149. }
  150. return ret;
  151. }
  152. /*
  153. * We have mostly NULL's here: the current defaults are ok for
  154. * the ext2 filesystem.
  155. */
  156. const struct file_operations ext2_file_operations = {
  157. .llseek = generic_file_llseek,
  158. .read_iter = generic_file_read_iter,
  159. .write_iter = generic_file_write_iter,
  160. .unlocked_ioctl = ext2_ioctl,
  161. #ifdef CONFIG_COMPAT
  162. .compat_ioctl = ext2_compat_ioctl,
  163. #endif
  164. .mmap = ext2_file_mmap,
  165. .open = dquot_file_open,
  166. .release = ext2_release_file,
  167. .fsync = ext2_fsync,
  168. .splice_read = generic_file_splice_read,
  169. .splice_write = iter_file_splice_write,
  170. };
  171. const struct inode_operations ext2_file_inode_operations = {
  172. #ifdef CONFIG_EXT2_FS_XATTR
  173. .setxattr = generic_setxattr,
  174. .getxattr = generic_getxattr,
  175. .listxattr = ext2_listxattr,
  176. .removexattr = generic_removexattr,
  177. #endif
  178. .setattr = ext2_setattr,
  179. .get_acl = ext2_get_acl,
  180. .set_acl = ext2_set_acl,
  181. .fiemap = ext2_fiemap,
  182. };