sync.c 9.7 KB

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