sync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. /*
  30. * This should be safe, as we require bdi backing to actually
  31. * write out data in the first place
  32. */
  33. if (sb->s_bdi == &noop_backing_dev_info)
  34. return 0;
  35. if (sb->s_qcop && sb->s_qcop->quota_sync)
  36. sb->s_qcop->quota_sync(sb, -1, wait);
  37. if (wait)
  38. sync_inodes_sb(sb);
  39. else
  40. writeback_inodes_sb(sb, WB_REASON_SYNC);
  41. if (sb->s_op->sync_fs)
  42. sb->s_op->sync_fs(sb, wait);
  43. return __sync_blockdev(sb->s_bdev, wait);
  44. }
  45. /*
  46. * Write out and wait upon all dirty data associated with this
  47. * superblock. Filesystem data as well as the underlying block
  48. * device. Takes the superblock lock.
  49. */
  50. int sync_filesystem(struct super_block *sb)
  51. {
  52. int ret;
  53. /*
  54. * We need to be protected against the filesystem going from
  55. * r/o to r/w or vice versa.
  56. */
  57. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  58. /*
  59. * No point in syncing out anything if the filesystem is read-only.
  60. */
  61. if (sb->s_flags & MS_RDONLY)
  62. return 0;
  63. ret = __sync_filesystem(sb, 0);
  64. if (ret < 0)
  65. return ret;
  66. return __sync_filesystem(sb, 1);
  67. }
  68. EXPORT_SYMBOL_GPL(sync_filesystem);
  69. static void sync_one_sb(struct super_block *sb, void *arg)
  70. {
  71. if (!(sb->s_flags & MS_RDONLY))
  72. __sync_filesystem(sb, *(int *)arg);
  73. }
  74. /*
  75. * Sync all the data for all the filesystems (called by sys_sync() and
  76. * emergency sync)
  77. */
  78. static void sync_filesystems(int wait)
  79. {
  80. iterate_supers(sync_one_sb, &wait);
  81. }
  82. /*
  83. * sync everything. Start out by waking pdflush, because that writes back
  84. * all queues in parallel.
  85. */
  86. static void do_sync(void)
  87. {
  88. wakeup_flusher_threads(0, WB_REASON_SYNC);
  89. sync_filesystems(0);
  90. sync_filesystems(1);
  91. if (unlikely(laptop_mode))
  92. laptop_sync_completion();
  93. return;
  94. }
  95. static DEFINE_MUTEX(sync_mutex); /* One do_sync() at a time. */
  96. static unsigned long sync_seq; /* Many sync()s from one do_sync(). */
  97. /* Overflow harmless, extra wait. */
  98. /*
  99. * Only allow one task to do sync() at a time, and further allow
  100. * concurrent sync() calls to be satisfied by a single do_sync()
  101. * invocation.
  102. */
  103. SYSCALL_DEFINE0(sync)
  104. {
  105. unsigned long snap;
  106. unsigned long snap_done;
  107. snap = ACCESS_ONCE(sync_seq);
  108. smp_mb(); /* Prevent above from bleeding into critical section. */
  109. mutex_lock(&sync_mutex);
  110. snap_done = sync_seq;
  111. /*
  112. * If the value in snap is odd, we need to wait for the current
  113. * do_sync() to complete, then wait for the next one, in other
  114. * words, we need the value of snap_done to be three larger than
  115. * the value of snap. On the other hand, if the value in snap is
  116. * even, we only have to wait for the next request to complete,
  117. * in other words, we need the value of snap_done to be only two
  118. * greater than the value of snap. The "(snap + 3) & 0x1" computes
  119. * this for us (thank you, Linus!).
  120. */
  121. if (ULONG_CMP_GE(snap_done, (snap + 3) & ~0x1)) {
  122. /*
  123. * A full do_sync() executed between our two fetches from
  124. * sync_seq, so our work is done!
  125. */
  126. smp_mb(); /* Order test with caller's subsequent code. */
  127. mutex_unlock(&sync_mutex);
  128. return 0;
  129. }
  130. /* Record the start of do_sync(). */
  131. ACCESS_ONCE(sync_seq)++;
  132. WARN_ON_ONCE((sync_seq & 0x1) != 1);
  133. smp_mb(); /* Keep prior increment out of do_sync(). */
  134. do_sync();
  135. /* Record the end of do_sync(). */
  136. smp_mb(); /* Keep subsequent increment out of do_sync(). */
  137. ACCESS_ONCE(sync_seq)++;
  138. WARN_ON_ONCE((sync_seq & 0x1) != 0);
  139. mutex_unlock(&sync_mutex);
  140. return 0;
  141. }
  142. static void do_sync_work(struct work_struct *work)
  143. {
  144. /*
  145. * Sync twice to reduce the possibility we skipped some inodes / pages
  146. * because they were temporarily locked
  147. */
  148. sync_filesystems(0);
  149. sync_filesystems(0);
  150. printk("Emergency Sync complete\n");
  151. kfree(work);
  152. }
  153. void emergency_sync(void)
  154. {
  155. struct work_struct *work;
  156. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  157. if (work) {
  158. INIT_WORK(work, do_sync_work);
  159. schedule_work(work);
  160. }
  161. }
  162. /*
  163. * sync a single super
  164. */
  165. SYSCALL_DEFINE1(syncfs, int, fd)
  166. {
  167. struct file *file;
  168. struct super_block *sb;
  169. int ret;
  170. int fput_needed;
  171. file = fget_light(fd, &fput_needed);
  172. if (!file)
  173. return -EBADF;
  174. sb = file->f_dentry->d_sb;
  175. down_read(&sb->s_umount);
  176. ret = sync_filesystem(sb);
  177. up_read(&sb->s_umount);
  178. fput_light(file, fput_needed);
  179. return ret;
  180. }
  181. /**
  182. * vfs_fsync_range - helper to sync a range of data & metadata to disk
  183. * @file: file to sync
  184. * @start: offset in bytes of the beginning of data range to sync
  185. * @end: offset in bytes of the end of data range (inclusive)
  186. * @datasync: perform only datasync
  187. *
  188. * Write back data in range @start..@end and metadata for @file to disk. If
  189. * @datasync is set only metadata needed to access modified file data is
  190. * written.
  191. */
  192. int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
  193. {
  194. if (!file->f_op || !file->f_op->fsync)
  195. return -EINVAL;
  196. return file->f_op->fsync(file, start, end, datasync);
  197. }
  198. EXPORT_SYMBOL(vfs_fsync_range);
  199. /**
  200. * vfs_fsync - perform a fsync or fdatasync on a file
  201. * @file: file to sync
  202. * @datasync: only perform a fdatasync operation
  203. *
  204. * Write back data and metadata for @file to disk. If @datasync is
  205. * set only metadata needed to access modified file data is written.
  206. */
  207. int vfs_fsync(struct file *file, int datasync)
  208. {
  209. return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
  210. }
  211. EXPORT_SYMBOL(vfs_fsync);
  212. static int do_fsync(unsigned int fd, int datasync)
  213. {
  214. struct file *file;
  215. int ret = -EBADF;
  216. int fput_needed;
  217. file = fget_light(fd, &fput_needed);
  218. if (file) {
  219. ret = vfs_fsync(file, datasync);
  220. fput_light(file, fput_needed);
  221. }
  222. return ret;
  223. }
  224. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  225. {
  226. return do_fsync(fd, 0);
  227. }
  228. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  229. {
  230. return do_fsync(fd, 1);
  231. }
  232. /**
  233. * generic_write_sync - perform syncing after a write if file / inode is sync
  234. * @file: file to which the write happened
  235. * @pos: offset where the write started
  236. * @count: length of the write
  237. *
  238. * This is just a simple wrapper about our general syncing function.
  239. */
  240. int generic_write_sync(struct file *file, loff_t pos, loff_t count)
  241. {
  242. if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host))
  243. return 0;
  244. return vfs_fsync_range(file, pos, pos + count - 1,
  245. (file->f_flags & __O_SYNC) ? 0 : 1);
  246. }
  247. EXPORT_SYMBOL(generic_write_sync);
  248. /*
  249. * sys_sync_file_range() permits finely controlled syncing over a segment of
  250. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  251. * zero then sys_sync_file_range() will operate from offset out to EOF.
  252. *
  253. * The flag bits are:
  254. *
  255. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  256. * before performing the write.
  257. *
  258. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  259. * range which are not presently under writeback. Note that this may block for
  260. * significant periods due to exhaustion of disk request structures.
  261. *
  262. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  263. * after performing the write.
  264. *
  265. * Useful combinations of the flag bits are:
  266. *
  267. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  268. * in the range which were dirty on entry to sys_sync_file_range() are placed
  269. * under writeout. This is a start-write-for-data-integrity operation.
  270. *
  271. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  272. * are not presently under writeout. This is an asynchronous flush-to-disk
  273. * operation. Not suitable for data integrity operations.
  274. *
  275. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  276. * completion of writeout of all pages in the range. This will be used after an
  277. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  278. * for that operation to complete and to return the result.
  279. *
  280. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  281. * a traditional sync() operation. This is a write-for-data-integrity operation
  282. * which will ensure that all pages in the range which were dirty on entry to
  283. * sys_sync_file_range() are committed to disk.
  284. *
  285. *
  286. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  287. * I/O errors or ENOSPC conditions and will return those to the caller, after
  288. * clearing the EIO and ENOSPC flags in the address_space.
  289. *
  290. * It should be noted that none of these operations write out the file's
  291. * metadata. So unless the application is strictly performing overwrites of
  292. * already-instantiated disk blocks, there are no guarantees here that the data
  293. * will be available after a crash.
  294. */
  295. SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
  296. unsigned int flags)
  297. {
  298. int ret;
  299. struct file *file;
  300. struct address_space *mapping;
  301. loff_t endbyte; /* inclusive */
  302. int fput_needed;
  303. umode_t i_mode;
  304. ret = -EINVAL;
  305. if (flags & ~VALID_FLAGS)
  306. goto out;
  307. endbyte = offset + nbytes;
  308. if ((s64)offset < 0)
  309. goto out;
  310. if ((s64)endbyte < 0)
  311. goto out;
  312. if (endbyte < offset)
  313. goto out;
  314. if (sizeof(pgoff_t) == 4) {
  315. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  316. /*
  317. * The range starts outside a 32 bit machine's
  318. * pagecache addressing capabilities. Let it "succeed"
  319. */
  320. ret = 0;
  321. goto out;
  322. }
  323. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  324. /*
  325. * Out to EOF
  326. */
  327. nbytes = 0;
  328. }
  329. }
  330. if (nbytes == 0)
  331. endbyte = LLONG_MAX;
  332. else
  333. endbyte--; /* inclusive */
  334. ret = -EBADF;
  335. file = fget_light(fd, &fput_needed);
  336. if (!file)
  337. goto out;
  338. i_mode = file->f_path.dentry->d_inode->i_mode;
  339. ret = -ESPIPE;
  340. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  341. !S_ISLNK(i_mode))
  342. goto out_put;
  343. mapping = file->f_mapping;
  344. if (!mapping) {
  345. ret = -EINVAL;
  346. goto out_put;
  347. }
  348. ret = 0;
  349. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  350. ret = filemap_fdatawait_range(mapping, offset, endbyte);
  351. if (ret < 0)
  352. goto out_put;
  353. }
  354. if (flags & SYNC_FILE_RANGE_WRITE) {
  355. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  356. WB_SYNC_NONE);
  357. if (ret < 0)
  358. goto out_put;
  359. }
  360. if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
  361. ret = filemap_fdatawait_range(mapping, offset, endbyte);
  362. out_put:
  363. fput_light(file, fput_needed);
  364. out:
  365. return ret;
  366. }
  367. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  368. asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
  369. long flags)
  370. {
  371. return SYSC_sync_file_range((int) fd, offset, nbytes,
  372. (unsigned int) flags);
  373. }
  374. SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
  375. #endif
  376. /* It would be nice if people remember that not all the world's an i386
  377. when they introduce new system calls */
  378. SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
  379. loff_t offset, loff_t nbytes)
  380. {
  381. return sys_sync_file_range(fd, offset, nbytes, flags);
  382. }
  383. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  384. asmlinkage long SyS_sync_file_range2(long fd, long flags,
  385. loff_t offset, loff_t nbytes)
  386. {
  387. return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
  388. offset, nbytes);
  389. }
  390. SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
  391. #endif