file.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * file.c - NILFS regular file handling primitives including fsync().
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Written by Amagai Yoshiji and Ryusuke Konishi.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/mm.h>
  20. #include <linux/writeback.h>
  21. #include "nilfs.h"
  22. #include "segment.h"
  23. int nilfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
  24. {
  25. /*
  26. * Called from fsync() system call
  27. * This is the only entry point that can catch write and synch
  28. * timing for both data blocks and intermediate blocks.
  29. *
  30. * This function should be implemented when the writeback function
  31. * will be implemented.
  32. */
  33. struct the_nilfs *nilfs;
  34. struct inode *inode = file->f_mapping->host;
  35. int err = 0;
  36. if (nilfs_inode_dirty(inode)) {
  37. if (datasync)
  38. err = nilfs_construct_dsync_segment(inode->i_sb, inode,
  39. start, end);
  40. else
  41. err = nilfs_construct_segment(inode->i_sb);
  42. }
  43. nilfs = inode->i_sb->s_fs_info;
  44. if (!err)
  45. err = nilfs_flush_device(nilfs);
  46. return err;
  47. }
  48. static int nilfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  49. {
  50. struct page *page = vmf->page;
  51. struct inode *inode = file_inode(vma->vm_file);
  52. struct nilfs_transaction_info ti;
  53. int ret = 0;
  54. if (unlikely(nilfs_near_disk_full(inode->i_sb->s_fs_info)))
  55. return VM_FAULT_SIGBUS; /* -ENOSPC */
  56. sb_start_pagefault(inode->i_sb);
  57. lock_page(page);
  58. if (page->mapping != inode->i_mapping ||
  59. page_offset(page) >= i_size_read(inode) || !PageUptodate(page)) {
  60. unlock_page(page);
  61. ret = -EFAULT; /* make the VM retry the fault */
  62. goto out;
  63. }
  64. /*
  65. * check to see if the page is mapped already (no holes)
  66. */
  67. if (PageMappedToDisk(page))
  68. goto mapped;
  69. if (page_has_buffers(page)) {
  70. struct buffer_head *bh, *head;
  71. int fully_mapped = 1;
  72. bh = head = page_buffers(page);
  73. do {
  74. if (!buffer_mapped(bh)) {
  75. fully_mapped = 0;
  76. break;
  77. }
  78. } while (bh = bh->b_this_page, bh != head);
  79. if (fully_mapped) {
  80. SetPageMappedToDisk(page);
  81. goto mapped;
  82. }
  83. }
  84. unlock_page(page);
  85. /*
  86. * fill hole blocks
  87. */
  88. ret = nilfs_transaction_begin(inode->i_sb, &ti, 1);
  89. /* never returns -ENOMEM, but may return -ENOSPC */
  90. if (unlikely(ret))
  91. goto out;
  92. file_update_time(vma->vm_file);
  93. ret = block_page_mkwrite(vma, vmf, nilfs_get_block);
  94. if (ret) {
  95. nilfs_transaction_abort(inode->i_sb);
  96. goto out;
  97. }
  98. nilfs_set_file_dirty(inode, 1 << (PAGE_SHIFT - inode->i_blkbits));
  99. nilfs_transaction_commit(inode->i_sb);
  100. mapped:
  101. wait_for_stable_page(page);
  102. out:
  103. sb_end_pagefault(inode->i_sb);
  104. return block_page_mkwrite_return(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 = generic_file_splice_read,
  134. };
  135. const struct inode_operations nilfs_file_inode_operations = {
  136. .setattr = nilfs_setattr,
  137. .permission = nilfs_permission,
  138. .fiemap = nilfs_fiemap,
  139. };
  140. /* end of file */