sync.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include <linux/quotaops.h>
  13. #include <linux/buffer_head.h>
  14. #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
  15. SYNC_FILE_RANGE_WAIT_AFTER)
  16. /*
  17. * sync everything. Start out by waking pdflush, because that writes back
  18. * all queues in parallel.
  19. */
  20. static void do_sync(unsigned long wait)
  21. {
  22. wakeup_pdflush(0);
  23. sync_inodes(0); /* All mappings, inodes and their blockdevs */
  24. DQUOT_SYNC(NULL);
  25. sync_supers(); /* Write the superblocks */
  26. sync_filesystems(0); /* Start syncing the filesystems */
  27. sync_filesystems(wait); /* Waitingly sync the filesystems */
  28. sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
  29. if (!wait)
  30. printk("Emergency Sync complete\n");
  31. if (unlikely(laptop_mode))
  32. laptop_sync_completion();
  33. }
  34. asmlinkage long sys_sync(void)
  35. {
  36. do_sync(1);
  37. return 0;
  38. }
  39. void emergency_sync(void)
  40. {
  41. pdflush_operation(do_sync, 0);
  42. }
  43. /*
  44. * Generic function to fsync a file.
  45. *
  46. * filp may be NULL if called via the msync of a vma.
  47. */
  48. int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
  49. {
  50. struct inode * inode = dentry->d_inode;
  51. struct super_block * sb;
  52. int ret, err;
  53. /* sync the inode to buffers */
  54. ret = write_inode_now(inode, 0);
  55. /* sync the superblock to buffers */
  56. sb = inode->i_sb;
  57. lock_super(sb);
  58. if (sb->s_op->write_super)
  59. sb->s_op->write_super(sb);
  60. unlock_super(sb);
  61. /* .. finally sync the buffers to disk */
  62. err = sync_blockdev(sb->s_bdev);
  63. if (!ret)
  64. ret = err;
  65. return ret;
  66. }
  67. long do_fsync(struct file *file, int datasync)
  68. {
  69. int ret;
  70. int err;
  71. struct address_space *mapping = file->f_mapping;
  72. if (!file->f_op || !file->f_op->fsync) {
  73. /* Why? We can still call filemap_fdatawrite */
  74. ret = -EINVAL;
  75. goto out;
  76. }
  77. ret = filemap_fdatawrite(mapping);
  78. /*
  79. * We need to protect against concurrent writers, which could cause
  80. * livelocks in fsync_buffers_list().
  81. */
  82. mutex_lock(&mapping->host->i_mutex);
  83. err = file->f_op->fsync(file, file->f_dentry, datasync);
  84. if (!ret)
  85. ret = err;
  86. mutex_unlock(&mapping->host->i_mutex);
  87. err = filemap_fdatawait(mapping);
  88. if (!ret)
  89. ret = err;
  90. out:
  91. return ret;
  92. }
  93. static long __do_fsync(unsigned int fd, int datasync)
  94. {
  95. struct file *file;
  96. int ret = -EBADF;
  97. file = fget(fd);
  98. if (file) {
  99. ret = do_fsync(file, datasync);
  100. fput(file);
  101. }
  102. return ret;
  103. }
  104. asmlinkage long sys_fsync(unsigned int fd)
  105. {
  106. return __do_fsync(fd, 0);
  107. }
  108. asmlinkage long sys_fdatasync(unsigned int fd)
  109. {
  110. return __do_fsync(fd, 1);
  111. }
  112. /*
  113. * sys_sync_file_range() permits finely controlled syncing over a segment of
  114. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  115. * zero then sys_sync_file_range() will operate from offset out to EOF.
  116. *
  117. * The flag bits are:
  118. *
  119. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  120. * before performing the write.
  121. *
  122. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  123. * range which are not presently under writeback.
  124. *
  125. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  126. * after performing the write.
  127. *
  128. * Useful combinations of the flag bits are:
  129. *
  130. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  131. * in the range which were dirty on entry to sys_sync_file_range() are placed
  132. * under writeout. This is a start-write-for-data-integrity operation.
  133. *
  134. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  135. * are not presently under writeout. This is an asynchronous flush-to-disk
  136. * operation. Not suitable for data integrity operations.
  137. *
  138. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  139. * completion of writeout of all pages in the range. This will be used after an
  140. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  141. * for that operation to complete and to return the result.
  142. *
  143. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  144. * a traditional sync() operation. This is a write-for-data-integrity operation
  145. * which will ensure that all pages in the range which were dirty on entry to
  146. * sys_sync_file_range() are committed to disk.
  147. *
  148. *
  149. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  150. * I/O errors or ENOSPC conditions and will return those to the caller, after
  151. * clearing the EIO and ENOSPC flags in the address_space.
  152. *
  153. * It should be noted that none of these operations write out the file's
  154. * metadata. So unless the application is strictly performing overwrites of
  155. * already-instantiated disk blocks, there are no guarantees here that the data
  156. * will be available after a crash.
  157. */
  158. asmlinkage long sys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
  159. unsigned int flags)
  160. {
  161. int ret;
  162. struct file *file;
  163. loff_t endbyte; /* inclusive */
  164. int fput_needed;
  165. umode_t i_mode;
  166. ret = -EINVAL;
  167. if (flags & ~VALID_FLAGS)
  168. goto out;
  169. endbyte = offset + nbytes;
  170. if ((s64)offset < 0)
  171. goto out;
  172. if ((s64)endbyte < 0)
  173. goto out;
  174. if (endbyte < offset)
  175. goto out;
  176. if (sizeof(pgoff_t) == 4) {
  177. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  178. /*
  179. * The range starts outside a 32 bit machine's
  180. * pagecache addressing capabilities. Let it "succeed"
  181. */
  182. ret = 0;
  183. goto out;
  184. }
  185. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  186. /*
  187. * Out to EOF
  188. */
  189. nbytes = 0;
  190. }
  191. }
  192. if (nbytes == 0)
  193. endbyte = LLONG_MAX;
  194. else
  195. endbyte--; /* inclusive */
  196. ret = -EBADF;
  197. file = fget_light(fd, &fput_needed);
  198. if (!file)
  199. goto out;
  200. i_mode = file->f_dentry->d_inode->i_mode;
  201. ret = -ESPIPE;
  202. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  203. !S_ISLNK(i_mode))
  204. goto out_put;
  205. ret = do_sync_file_range(file, offset, endbyte, flags);
  206. out_put:
  207. fput_light(file, fput_needed);
  208. out:
  209. return ret;
  210. }
  211. /*
  212. * `endbyte' is inclusive
  213. */
  214. int do_sync_file_range(struct file *file, loff_t offset, loff_t endbyte,
  215. unsigned int flags)
  216. {
  217. int ret;
  218. struct address_space *mapping;
  219. mapping = file->f_mapping;
  220. if (!mapping) {
  221. ret = -EINVAL;
  222. goto out;
  223. }
  224. ret = 0;
  225. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  226. ret = wait_on_page_writeback_range(mapping,
  227. offset >> PAGE_CACHE_SHIFT,
  228. endbyte >> PAGE_CACHE_SHIFT);
  229. if (ret < 0)
  230. goto out;
  231. }
  232. if (flags & SYNC_FILE_RANGE_WRITE) {
  233. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  234. WB_SYNC_NONE);
  235. if (ret < 0)
  236. goto out;
  237. }
  238. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  239. ret = wait_on_page_writeback_range(mapping,
  240. offset >> PAGE_CACHE_SHIFT,
  241. endbyte >> PAGE_CACHE_SHIFT);
  242. }
  243. out:
  244. return ret;
  245. }
  246. EXPORT_SYMBOL_GPL(do_sync_file_range);