sync.c 9.9 KB

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