sync.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * High-level sync()-related operations
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/kernel.h>
  7. #include <linux/file.h>
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/export.h>
  11. #include <linux/namei.h>
  12. #include <linux/sched.h>
  13. #include <linux/writeback.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/linkage.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/quotaops.h>
  18. #include <linux/backing-dev.h>
  19. #include "internal.h"
  20. #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
  21. SYNC_FILE_RANGE_WAIT_AFTER)
  22. /*
  23. * Write out and wait upon all dirty data associated with this
  24. * superblock. Filesystem data as well as the underlying block
  25. * device. Takes the superblock lock.
  26. */
  27. int sync_filesystem(struct super_block *sb)
  28. {
  29. int ret = 0;
  30. /*
  31. * We need to be protected against the filesystem going from
  32. * r/o to r/w or vice versa.
  33. */
  34. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  35. /*
  36. * No point in syncing out anything if the filesystem is read-only.
  37. */
  38. if (sb_rdonly(sb))
  39. return 0;
  40. /*
  41. * Do the filesystem syncing work. For simple filesystems
  42. * writeback_inodes_sb(sb) just dirties buffers with inodes so we have
  43. * to submit I/O for these buffers via sync_blockdev(). This also
  44. * speeds up the wait == 1 case since in that case write_inode()
  45. * methods call sync_dirty_buffer() and thus effectively write one block
  46. * at a time.
  47. */
  48. writeback_inodes_sb(sb, WB_REASON_SYNC);
  49. if (sb->s_op->sync_fs) {
  50. ret = sb->s_op->sync_fs(sb, 0);
  51. if (ret)
  52. return ret;
  53. }
  54. ret = sync_blockdev_nowait(sb->s_bdev);
  55. if (ret)
  56. return ret;
  57. sync_inodes_sb(sb);
  58. if (sb->s_op->sync_fs) {
  59. ret = sb->s_op->sync_fs(sb, 1);
  60. if (ret)
  61. return ret;
  62. }
  63. return sync_blockdev(sb->s_bdev);
  64. }
  65. EXPORT_SYMBOL(sync_filesystem);
  66. static void sync_inodes_one_sb(struct super_block *sb, void *arg)
  67. {
  68. if (!sb_rdonly(sb))
  69. sync_inodes_sb(sb);
  70. }
  71. static void sync_fs_one_sb(struct super_block *sb, void *arg)
  72. {
  73. if (!sb_rdonly(sb) && !(sb->s_iflags & SB_I_SKIP_SYNC) &&
  74. sb->s_op->sync_fs)
  75. sb->s_op->sync_fs(sb, *(int *)arg);
  76. }
  77. /*
  78. * Sync everything. We start by waking flusher threads so that most of
  79. * writeback runs on all devices in parallel. Then we sync all inodes reliably
  80. * which effectively also waits for all flusher threads to finish doing
  81. * writeback. At this point all data is on disk so metadata should be stable
  82. * and we tell filesystems to sync their metadata via ->sync_fs() calls.
  83. * Finally, we writeout all block devices because some filesystems (e.g. ext2)
  84. * just write metadata (such as inodes or bitmaps) to block device page cache
  85. * and do not sync it on their own in ->sync_fs().
  86. */
  87. void ksys_sync(void)
  88. {
  89. int nowait = 0, wait = 1;
  90. wakeup_flusher_threads(WB_REASON_SYNC);
  91. iterate_supers(sync_inodes_one_sb, NULL);
  92. iterate_supers(sync_fs_one_sb, &nowait);
  93. iterate_supers(sync_fs_one_sb, &wait);
  94. sync_bdevs(false);
  95. sync_bdevs(true);
  96. }
  97. SYSCALL_DEFINE0(sync)
  98. {
  99. ksys_sync();
  100. return 0;
  101. }
  102. static void do_sync_work(struct work_struct *work)
  103. {
  104. int nowait = 0;
  105. int wait = 1;
  106. /*
  107. * Sync twice to reduce the possibility we skipped some inodes / pages
  108. * because they were temporarily locked
  109. */
  110. iterate_supers(sync_inodes_one_sb, NULL);
  111. iterate_supers(sync_fs_one_sb, &nowait);
  112. sync_bdevs(false);
  113. iterate_supers(sync_inodes_one_sb, NULL);
  114. iterate_supers(sync_fs_one_sb, &wait);
  115. sync_bdevs(false);
  116. printk("Emergency Sync complete\n");
  117. kfree(work);
  118. }
  119. void emergency_sync(void)
  120. {
  121. struct work_struct *work;
  122. work = kmalloc_obj(*work, GFP_ATOMIC);
  123. if (work) {
  124. INIT_WORK(work, do_sync_work);
  125. schedule_work(work);
  126. }
  127. }
  128. /*
  129. * sync a single super
  130. */
  131. SYSCALL_DEFINE1(syncfs, int, fd)
  132. {
  133. CLASS(fd, f)(fd);
  134. struct super_block *sb;
  135. int ret, ret2;
  136. if (fd_empty(f))
  137. return -EBADF;
  138. sb = fd_file(f)->f_path.dentry->d_sb;
  139. down_read(&sb->s_umount);
  140. ret = sync_filesystem(sb);
  141. up_read(&sb->s_umount);
  142. ret2 = errseq_check_and_advance(&sb->s_wb_err, &fd_file(f)->f_sb_err);
  143. return ret ? ret : ret2;
  144. }
  145. /**
  146. * vfs_fsync_range - helper to sync a range of data & metadata to disk
  147. * @file: file to sync
  148. * @start: offset in bytes of the beginning of data range to sync
  149. * @end: offset in bytes of the end of data range (inclusive)
  150. * @datasync: perform only datasync
  151. *
  152. * Write back data in range @start..@end and metadata for @file to disk. If
  153. * @datasync is set only metadata needed to access modified file data is
  154. * written.
  155. */
  156. int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
  157. {
  158. struct inode *inode = file->f_mapping->host;
  159. if (!file->f_op->fsync)
  160. return -EINVAL;
  161. if (!datasync)
  162. sync_lazytime(inode);
  163. return file->f_op->fsync(file, start, end, datasync);
  164. }
  165. EXPORT_SYMBOL(vfs_fsync_range);
  166. /**
  167. * vfs_fsync - perform a fsync or fdatasync on a file
  168. * @file: file to sync
  169. * @datasync: only perform a fdatasync operation
  170. *
  171. * Write back data and metadata for @file to disk. If @datasync is
  172. * set only metadata needed to access modified file data is written.
  173. */
  174. int vfs_fsync(struct file *file, int datasync)
  175. {
  176. return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
  177. }
  178. EXPORT_SYMBOL(vfs_fsync);
  179. static int do_fsync(unsigned int fd, int datasync)
  180. {
  181. CLASS(fd, f)(fd);
  182. if (fd_empty(f))
  183. return -EBADF;
  184. return vfs_fsync(fd_file(f), datasync);
  185. }
  186. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  187. {
  188. return do_fsync(fd, 0);
  189. }
  190. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  191. {
  192. return do_fsync(fd, 1);
  193. }
  194. int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
  195. unsigned int flags)
  196. {
  197. int ret;
  198. struct address_space *mapping;
  199. loff_t endbyte; /* inclusive */
  200. umode_t i_mode;
  201. ret = -EINVAL;
  202. if (flags & ~VALID_FLAGS)
  203. goto out;
  204. endbyte = offset + nbytes;
  205. if ((s64)offset < 0)
  206. goto out;
  207. if ((s64)endbyte < 0)
  208. goto out;
  209. if (endbyte < offset)
  210. goto out;
  211. if (sizeof(pgoff_t) == 4) {
  212. if (offset >= (0x100000000ULL << PAGE_SHIFT)) {
  213. /*
  214. * The range starts outside a 32 bit machine's
  215. * pagecache addressing capabilities. Let it "succeed"
  216. */
  217. ret = 0;
  218. goto out;
  219. }
  220. if (endbyte >= (0x100000000ULL << PAGE_SHIFT)) {
  221. /*
  222. * Out to EOF
  223. */
  224. nbytes = 0;
  225. }
  226. }
  227. if (nbytes == 0)
  228. endbyte = LLONG_MAX;
  229. else
  230. endbyte--; /* inclusive */
  231. i_mode = file_inode(file)->i_mode;
  232. ret = -ESPIPE;
  233. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  234. !S_ISLNK(i_mode))
  235. goto out;
  236. mapping = file->f_mapping;
  237. ret = 0;
  238. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  239. ret = file_fdatawait_range(file, offset, endbyte);
  240. if (ret < 0)
  241. goto out;
  242. }
  243. if (flags & SYNC_FILE_RANGE_WRITE) {
  244. if ((flags & SYNC_FILE_RANGE_WRITE_AND_WAIT) ==
  245. SYNC_FILE_RANGE_WRITE_AND_WAIT)
  246. ret = filemap_fdatawrite_range(mapping, offset,
  247. endbyte);
  248. else
  249. ret = filemap_flush_range(mapping, offset, endbyte);
  250. if (ret < 0)
  251. goto out;
  252. }
  253. if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
  254. ret = file_fdatawait_range(file, offset, endbyte);
  255. out:
  256. return ret;
  257. }
  258. /*
  259. * ksys_sync_file_range() permits finely controlled syncing over a segment of
  260. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  261. * zero then ksys_sync_file_range() will operate from offset out to EOF.
  262. *
  263. * The flag bits are:
  264. *
  265. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  266. * before performing the write.
  267. *
  268. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  269. * range which are not presently under writeback. Note that this may block for
  270. * significant periods due to exhaustion of disk request structures.
  271. *
  272. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  273. * after performing the write.
  274. *
  275. * Useful combinations of the flag bits are:
  276. *
  277. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  278. * in the range which were dirty on entry to ksys_sync_file_range() are placed
  279. * under writeout. This is a start-write-for-data-integrity operation.
  280. *
  281. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  282. * are not presently under writeout. This is an asynchronous flush-to-disk
  283. * operation. Not suitable for data integrity operations.
  284. *
  285. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  286. * completion of writeout of all pages in the range. This will be used after an
  287. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  288. * for that operation to complete and to return the result.
  289. *
  290. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER
  291. * (a.k.a. SYNC_FILE_RANGE_WRITE_AND_WAIT):
  292. * a traditional sync() operation. This is a write-for-data-integrity operation
  293. * which will ensure that all pages in the range which were dirty on entry to
  294. * ksys_sync_file_range() are written to disk. It should be noted that disk
  295. * caches are not flushed by this call, so there are no guarantees here that the
  296. * data will be available on disk after a crash.
  297. *
  298. *
  299. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  300. * I/O errors or ENOSPC conditions and will return those to the caller, after
  301. * clearing the EIO and ENOSPC flags in the address_space.
  302. *
  303. * It should be noted that none of these operations write out the file's
  304. * metadata. So unless the application is strictly performing overwrites of
  305. * already-instantiated disk blocks, there are no guarantees here that the data
  306. * will be available after a crash.
  307. */
  308. int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
  309. unsigned int flags)
  310. {
  311. CLASS(fd, f)(fd);
  312. if (fd_empty(f))
  313. return -EBADF;
  314. return sync_file_range(fd_file(f), offset, nbytes, flags);
  315. }
  316. SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
  317. unsigned int, flags)
  318. {
  319. return ksys_sync_file_range(fd, offset, nbytes, flags);
  320. }
  321. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_SYNC_FILE_RANGE)
  322. COMPAT_SYSCALL_DEFINE6(sync_file_range, int, fd, compat_arg_u64_dual(offset),
  323. compat_arg_u64_dual(nbytes), unsigned int, flags)
  324. {
  325. return ksys_sync_file_range(fd, compat_arg_u64_glue(offset),
  326. compat_arg_u64_glue(nbytes), flags);
  327. }
  328. #endif
  329. /* It would be nice if people remember that not all the world's an i386
  330. when they introduce new system calls */
  331. SYSCALL_DEFINE4(sync_file_range2, int, fd, unsigned int, flags,
  332. loff_t, offset, loff_t, nbytes)
  333. {
  334. return ksys_sync_file_range(fd, offset, nbytes, flags);
  335. }