sync.c 11 KB

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