file.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NILFS regular file handling primitives including fsync().
  4. *
  5. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Written by Amagai Yoshiji and Ryusuke Konishi.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/writeback.h>
  12. #include "nilfs.h"
  13. #include "segment.h"
  14. int nilfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  15. {
  16. /*
  17. * Called from fsync() system call
  18. * This is the only entry point that can catch write and synch
  19. * timing for both data blocks and intermediate blocks.
  20. *
  21. * This function should be implemented when the writeback function
  22. * will be implemented.
  23. */
  24. struct the_nilfs *nilfs;
  25. struct inode *inode = file->f_mapping->host;
  26. int err = 0;
  27. if (nilfs_inode_dirty(inode)) {
  28. if (datasync)
  29. err = nilfs_construct_dsync_segment(inode->i_sb, inode,
  30. start, end);
  31. else
  32. err = nilfs_construct_segment(inode->i_sb);
  33. }
  34. nilfs = inode->i_sb->s_fs_info;
  35. if (!err)
  36. err = nilfs_flush_device(nilfs);
  37. return err;
  38. }
  39. static vm_fault_t nilfs_page_mkwrite(struct vm_fault *vmf)
  40. {
  41. struct vm_area_struct *vma = vmf->vma;
  42. struct folio *folio = page_folio(vmf->page);
  43. struct inode *inode = file_inode(vma->vm_file);
  44. struct nilfs_transaction_info ti;
  45. struct buffer_head *bh, *head;
  46. int ret = 0;
  47. if (unlikely(nilfs_near_disk_full(inode->i_sb->s_fs_info)))
  48. return VM_FAULT_SIGBUS; /* -ENOSPC */
  49. sb_start_pagefault(inode->i_sb);
  50. folio_lock(folio);
  51. if (folio->mapping != inode->i_mapping ||
  52. folio_pos(folio) >= i_size_read(inode) ||
  53. !folio_test_uptodate(folio)) {
  54. folio_unlock(folio);
  55. ret = -EFAULT; /* make the VM retry the fault */
  56. goto out;
  57. }
  58. /*
  59. * check to see if the folio is mapped already (no holes)
  60. */
  61. if (folio_test_mappedtodisk(folio))
  62. goto mapped;
  63. head = folio_buffers(folio);
  64. if (head) {
  65. int fully_mapped = 1;
  66. bh = head;
  67. do {
  68. if (!buffer_mapped(bh)) {
  69. fully_mapped = 0;
  70. break;
  71. }
  72. } while (bh = bh->b_this_page, bh != head);
  73. if (fully_mapped) {
  74. folio_set_mappedtodisk(folio);
  75. goto mapped;
  76. }
  77. }
  78. folio_unlock(folio);
  79. /*
  80. * fill hole blocks
  81. */
  82. ret = nilfs_transaction_begin(inode->i_sb, &ti, 1);
  83. /* never returns -ENOMEM, but may return -ENOSPC */
  84. if (unlikely(ret))
  85. goto out;
  86. file_update_time(vma->vm_file);
  87. ret = block_page_mkwrite(vma, vmf, nilfs_get_block);
  88. if (ret) {
  89. nilfs_transaction_abort(inode->i_sb);
  90. goto out;
  91. }
  92. nilfs_set_file_dirty(inode, 1 << (PAGE_SHIFT - inode->i_blkbits));
  93. nilfs_transaction_commit(inode->i_sb);
  94. mapped:
  95. /*
  96. * Since checksumming including data blocks is performed to determine
  97. * the validity of the log to be written and used for recovery, it is
  98. * necessary to wait for writeback to finish here, regardless of the
  99. * stable write requirement of the backing device.
  100. */
  101. folio_wait_writeback(folio);
  102. out:
  103. sb_end_pagefault(inode->i_sb);
  104. return vmf_fs_error(ret);
  105. }
  106. static const struct vm_operations_struct nilfs_file_vm_ops = {
  107. .fault = filemap_fault,
  108. .map_pages = filemap_map_pages,
  109. .page_mkwrite = nilfs_page_mkwrite,
  110. };
  111. static int nilfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  112. {
  113. file_accessed(file);
  114. vma->vm_ops = &nilfs_file_vm_ops;
  115. return 0;
  116. }
  117. /*
  118. * We have mostly NULL's here: the current defaults are ok for
  119. * the nilfs filesystem.
  120. */
  121. const struct file_operations nilfs_file_operations = {
  122. .llseek = generic_file_llseek,
  123. .read_iter = generic_file_read_iter,
  124. .write_iter = generic_file_write_iter,
  125. .unlocked_ioctl = nilfs_ioctl,
  126. #ifdef CONFIG_COMPAT
  127. .compat_ioctl = nilfs_compat_ioctl,
  128. #endif /* CONFIG_COMPAT */
  129. .mmap = nilfs_file_mmap,
  130. .open = generic_file_open,
  131. /* .release = nilfs_release_file, */
  132. .fsync = nilfs_sync_file,
  133. .splice_read = filemap_splice_read,
  134. .splice_write = iter_file_splice_write,
  135. };
  136. const struct inode_operations nilfs_file_inode_operations = {
  137. .setattr = nilfs_setattr,
  138. .permission = nilfs_permission,
  139. .fiemap = nilfs_fiemap,
  140. .fileattr_get = nilfs_fileattr_get,
  141. .fileattr_set = nilfs_fileattr_set,
  142. };
  143. /* end of file */