sync.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/xacct.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. if (unlikely(laptop_mode))
  97. laptop_sync_completion();
  98. }
  99. SYSCALL_DEFINE0(sync)
  100. {
  101. ksys_sync();
  102. return 0;
  103. }
  104. static void do_sync_work(struct work_struct *work)
  105. {
  106. int nowait = 0;
  107. /*
  108. * Sync twice to reduce the possibility we skipped some inodes / pages
  109. * because they were temporarily locked
  110. */
  111. iterate_supers(sync_inodes_one_sb, &nowait);
  112. iterate_supers(sync_fs_one_sb, &nowait);
  113. sync_bdevs(false);
  114. iterate_supers(sync_inodes_one_sb, &nowait);
  115. iterate_supers(sync_fs_one_sb, &nowait);
  116. sync_bdevs(false);
  117. printk("Emergency Sync complete\n");
  118. kfree(work);
  119. }
  120. void emergency_sync(void)
  121. {
  122. struct work_struct *work;
  123. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  124. if (work) {
  125. INIT_WORK(work, do_sync_work);
  126. schedule_work(work);
  127. }
  128. }
  129. /*
  130. * sync a single super
  131. */
  132. SYSCALL_DEFINE1(syncfs, int, fd)
  133. {
  134. struct fd f = fdget(fd);
  135. struct super_block *sb;
  136. int ret, ret2;
  137. if (!f.file)
  138. return -EBADF;
  139. sb = f.file->f_path.dentry->d_sb;
  140. down_read(&sb->s_umount);
  141. ret = sync_filesystem(sb);
  142. up_read(&sb->s_umount);
  143. ret2 = errseq_check_and_advance(&sb->s_wb_err, &f.file->f_sb_err);
  144. fdput(f);
  145. return ret ? ret : ret2;
  146. }
  147. /**
  148. * vfs_fsync_range - helper to sync a range of data & metadata to disk
  149. * @file: file to sync
  150. * @start: offset in bytes of the beginning of data range to sync
  151. * @end: offset in bytes of the end of data range (inclusive)
  152. * @datasync: perform only datasync
  153. *
  154. * Write back data in range @start..@end and metadata for @file to disk. If
  155. * @datasync is set only metadata needed to access modified file data is
  156. * written.
  157. */
  158. int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
  159. {
  160. struct inode *inode = file->f_mapping->host;
  161. if (!file->f_op->fsync)
  162. return -EINVAL;
  163. if (!datasync && (inode->i_state & I_DIRTY_TIME))
  164. mark_inode_dirty_sync(inode);
  165. return file->f_op->fsync(file, start, end, datasync);
  166. }
  167. EXPORT_SYMBOL(vfs_fsync_range);
  168. /**
  169. * vfs_fsync - perform a fsync or fdatasync on a file
  170. * @file: file to sync
  171. * @datasync: only perform a fdatasync operation
  172. *
  173. * Write back data and metadata for @file to disk. If @datasync is
  174. * set only metadata needed to access modified file data is written.
  175. */
  176. int vfs_fsync(struct file *file, int datasync)
  177. {
  178. return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
  179. }
  180. EXPORT_SYMBOL(vfs_fsync);
  181. static int do_fsync(unsigned int fd, int datasync)
  182. {
  183. struct fd f = fdget(fd);
  184. int ret = -EBADF;
  185. if (f.file) {
  186. ret = vfs_fsync(f.file, datasync);
  187. fdput(f);
  188. inc_syscfs(current);
  189. }
  190. return ret;
  191. }
  192. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  193. {
  194. return do_fsync(fd, 0);
  195. }
  196. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  197. {
  198. return do_fsync(fd, 1);
  199. }
  200. int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
  201. unsigned int flags)
  202. {
  203. int ret;
  204. struct address_space *mapping;
  205. loff_t endbyte; /* inclusive */
  206. umode_t i_mode;
  207. ret = -EINVAL;
  208. if (flags & ~VALID_FLAGS)
  209. goto out;
  210. endbyte = offset + nbytes;
  211. if ((s64)offset < 0)
  212. goto out;
  213. if ((s64)endbyte < 0)
  214. goto out;
  215. if (endbyte < offset)
  216. goto out;
  217. if (sizeof(pgoff_t) == 4) {
  218. if (offset >= (0x100000000ULL << PAGE_SHIFT)) {
  219. /*
  220. * The range starts outside a 32 bit machine's
  221. * pagecache addressing capabilities. Let it "succeed"
  222. */
  223. ret = 0;
  224. goto out;
  225. }
  226. if (endbyte >= (0x100000000ULL << PAGE_SHIFT)) {
  227. /*
  228. * Out to EOF
  229. */
  230. nbytes = 0;
  231. }
  232. }
  233. if (nbytes == 0)
  234. endbyte = LLONG_MAX;
  235. else
  236. endbyte--; /* inclusive */
  237. i_mode = file_inode(file)->i_mode;
  238. ret = -ESPIPE;
  239. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  240. !S_ISLNK(i_mode))
  241. goto out;
  242. mapping = file->f_mapping;
  243. ret = 0;
  244. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  245. ret = file_fdatawait_range(file, offset, endbyte);
  246. if (ret < 0)
  247. goto out;
  248. }
  249. if (flags & SYNC_FILE_RANGE_WRITE) {
  250. int sync_mode = WB_SYNC_NONE;
  251. if ((flags & SYNC_FILE_RANGE_WRITE_AND_WAIT) ==
  252. SYNC_FILE_RANGE_WRITE_AND_WAIT)
  253. sync_mode = WB_SYNC_ALL;
  254. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  255. sync_mode);
  256. if (ret < 0)
  257. goto out;
  258. }
  259. if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
  260. ret = file_fdatawait_range(file, offset, endbyte);
  261. out:
  262. return ret;
  263. }
  264. /*
  265. * ksys_sync_file_range() permits finely controlled syncing over a segment of
  266. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  267. * zero then ksys_sync_file_range() will operate from offset out to EOF.
  268. *
  269. * The flag bits are:
  270. *
  271. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  272. * before performing the write.
  273. *
  274. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  275. * range which are not presently under writeback. Note that this may block for
  276. * significant periods due to exhaustion of disk request structures.
  277. *
  278. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  279. * after performing the write.
  280. *
  281. * Useful combinations of the flag bits are:
  282. *
  283. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  284. * in the range which were dirty on entry to ksys_sync_file_range() are placed
  285. * under writeout. This is a start-write-for-data-integrity operation.
  286. *
  287. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  288. * are not presently under writeout. This is an asynchronous flush-to-disk
  289. * operation. Not suitable for data integrity operations.
  290. *
  291. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  292. * completion of writeout of all pages in the range. This will be used after an
  293. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  294. * for that operation to complete and to return the result.
  295. *
  296. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER
  297. * (a.k.a. SYNC_FILE_RANGE_WRITE_AND_WAIT):
  298. * a traditional sync() operation. This is a write-for-data-integrity operation
  299. * which will ensure that all pages in the range which were dirty on entry to
  300. * ksys_sync_file_range() are written to disk. It should be noted that disk
  301. * caches are not flushed by this call, so there are no guarantees here that the
  302. * data will be available on disk after a crash.
  303. *
  304. *
  305. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  306. * I/O errors or ENOSPC conditions and will return those to the caller, after
  307. * clearing the EIO and ENOSPC flags in the address_space.
  308. *
  309. * It should be noted that none of these operations write out the file's
  310. * metadata. So unless the application is strictly performing overwrites of
  311. * already-instantiated disk blocks, there are no guarantees here that the data
  312. * will be available after a crash.
  313. */
  314. int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
  315. unsigned int flags)
  316. {
  317. int ret;
  318. struct fd f;
  319. ret = -EBADF;
  320. f = fdget(fd);
  321. if (f.file)
  322. ret = sync_file_range(f.file, offset, nbytes, flags);
  323. fdput(f);
  324. return ret;
  325. }
  326. SYSCALL_DEFINE4(sync_file_range, int, fd, loff_t, offset, loff_t, nbytes,
  327. unsigned int, flags)
  328. {
  329. return ksys_sync_file_range(fd, offset, nbytes, flags);
  330. }
  331. #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_SYNC_FILE_RANGE)
  332. COMPAT_SYSCALL_DEFINE6(sync_file_range, int, fd, compat_arg_u64_dual(offset),
  333. compat_arg_u64_dual(nbytes), unsigned int, flags)
  334. {
  335. return ksys_sync_file_range(fd, compat_arg_u64_glue(offset),
  336. compat_arg_u64_glue(nbytes), flags);
  337. }
  338. #endif
  339. /* It would be nice if people remember that not all the world's an i386
  340. when they introduce new system calls */
  341. SYSCALL_DEFINE4(sync_file_range2, int, fd, unsigned int, flags,
  342. loff_t, offset, loff_t, nbytes)
  343. {
  344. return ksys_sync_file_range(fd, offset, nbytes, flags);
  345. }