sync.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * High-level sync()-related operations
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/file.h>
  6. #include <linux/fs.h>
  7. #include <linux/module.h>
  8. #include <linux/writeback.h>
  9. #include <linux/syscalls.h>
  10. #include <linux/linkage.h>
  11. #include <linux/pagemap.h>
  12. #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
  13. SYNC_FILE_RANGE_WAIT_AFTER)
  14. /*
  15. * sys_sync_file_range() permits finely controlled syncing over a segment of
  16. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  17. * zero then sys_sync_file_range() will operate from offset out to EOF.
  18. *
  19. * The flag bits are:
  20. *
  21. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  22. * before performing the write.
  23. *
  24. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  25. * range which are not presently under writeback.
  26. *
  27. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  28. * after performing the write.
  29. *
  30. * Useful combinations of the flag bits are:
  31. *
  32. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  33. * in the range which were dirty on entry to sys_sync_file_range() are placed
  34. * under writeout. This is a start-write-for-data-integrity operation.
  35. *
  36. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  37. * are not presently under writeout. This is an asynchronous flush-to-disk
  38. * operation. Not suitable for data integrity operations.
  39. *
  40. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  41. * completion of writeout of all pages in the range. This will be used after an
  42. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  43. * for that operation to complete and to return the result.
  44. *
  45. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  46. * a traditional sync() operation. This is a write-for-data-integrity operation
  47. * which will ensure that all pages in the range which were dirty on entry to
  48. * sys_sync_file_range() are committed to disk.
  49. *
  50. *
  51. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  52. * I/O errors or ENOSPC conditions and will return those to the caller, after
  53. * clearing the EIO and ENOSPC flags in the address_space.
  54. *
  55. * It should be noted that none of these operations write out the file's
  56. * metadata. So unless the application is strictly performing overwrites of
  57. * already-instantiated disk blocks, there are no guarantees here that the data
  58. * will be available after a crash.
  59. */
  60. asmlinkage long sys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
  61. unsigned int flags)
  62. {
  63. int ret;
  64. struct file *file;
  65. loff_t endbyte; /* inclusive */
  66. int fput_needed;
  67. umode_t i_mode;
  68. ret = -EINVAL;
  69. if (flags & ~VALID_FLAGS)
  70. goto out;
  71. endbyte = offset + nbytes;
  72. if ((s64)offset < 0)
  73. goto out;
  74. if ((s64)endbyte < 0)
  75. goto out;
  76. if (endbyte < offset)
  77. goto out;
  78. if (sizeof(pgoff_t) == 4) {
  79. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  80. /*
  81. * The range starts outside a 32 bit machine's
  82. * pagecache addressing capabilities. Let it "succeed"
  83. */
  84. ret = 0;
  85. goto out;
  86. }
  87. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  88. /*
  89. * Out to EOF
  90. */
  91. nbytes = 0;
  92. }
  93. }
  94. if (nbytes == 0)
  95. endbyte = LLONG_MAX;
  96. else
  97. endbyte--; /* inclusive */
  98. ret = -EBADF;
  99. file = fget_light(fd, &fput_needed);
  100. if (!file)
  101. goto out;
  102. i_mode = file->f_dentry->d_inode->i_mode;
  103. ret = -ESPIPE;
  104. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  105. !S_ISLNK(i_mode))
  106. goto out_put;
  107. ret = do_sync_file_range(file, offset, endbyte, flags);
  108. out_put:
  109. fput_light(file, fput_needed);
  110. out:
  111. return ret;
  112. }
  113. /*
  114. * `endbyte' is inclusive
  115. */
  116. int do_sync_file_range(struct file *file, loff_t offset, loff_t endbyte,
  117. unsigned int flags)
  118. {
  119. int ret;
  120. struct address_space *mapping;
  121. mapping = file->f_mapping;
  122. if (!mapping) {
  123. ret = -EINVAL;
  124. goto out;
  125. }
  126. ret = 0;
  127. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  128. ret = wait_on_page_writeback_range(mapping,
  129. offset >> PAGE_CACHE_SHIFT,
  130. endbyte >> PAGE_CACHE_SHIFT);
  131. if (ret < 0)
  132. goto out;
  133. }
  134. if (flags & SYNC_FILE_RANGE_WRITE) {
  135. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  136. WB_SYNC_NONE);
  137. if (ret < 0)
  138. goto out;
  139. }
  140. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  141. ret = wait_on_page_writeback_range(mapping,
  142. offset >> PAGE_CACHE_SHIFT,
  143. endbyte >> PAGE_CACHE_SHIFT);
  144. }
  145. out:
  146. return ret;
  147. }
  148. EXPORT_SYMBOL_GPL(do_sync_file_range);