sync.c 6.8 KB

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