sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/writeback.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/linkage.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/quotaops.h>
  14. #include <linux/buffer_head.h>
  15. #include "internal.h"
  16. #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
  17. SYNC_FILE_RANGE_WAIT_AFTER)
  18. /*
  19. * Do the filesystem syncing work. For simple filesystems
  20. * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
  21. * submit IO for these buffers via __sync_blockdev(). This also speeds up the
  22. * wait == 1 case since in that case write_inode() functions do
  23. * sync_dirty_buffer() and thus effectively write one block at a time.
  24. */
  25. static int __sync_filesystem(struct super_block *sb, int wait)
  26. {
  27. /* Avoid doing twice syncing and cache pruning for quota sync */
  28. if (!wait) {
  29. writeout_quota_sb(sb, -1);
  30. writeback_inodes_sb(sb);
  31. } else {
  32. sync_quota_sb(sb, -1);
  33. sync_inodes_sb(sb);
  34. }
  35. if (sb->s_op->sync_fs)
  36. sb->s_op->sync_fs(sb, wait);
  37. return __sync_blockdev(sb->s_bdev, wait);
  38. }
  39. /*
  40. * Write out and wait upon all dirty data associated with this
  41. * superblock. Filesystem data as well as the underlying block
  42. * device. Takes the superblock lock.
  43. */
  44. int sync_filesystem(struct super_block *sb)
  45. {
  46. int ret;
  47. /*
  48. * We need to be protected against the filesystem going from
  49. * r/o to r/w or vice versa.
  50. */
  51. WARN_ON(!rwsem_is_locked(&sb->s_umount));
  52. /*
  53. * No point in syncing out anything if the filesystem is read-only.
  54. */
  55. if (sb->s_flags & MS_RDONLY)
  56. return 0;
  57. ret = __sync_filesystem(sb, 0);
  58. if (ret < 0)
  59. return ret;
  60. return __sync_filesystem(sb, 1);
  61. }
  62. EXPORT_SYMBOL_GPL(sync_filesystem);
  63. /*
  64. * Sync all the data for all the filesystems (called by sys_sync() and
  65. * emergency sync)
  66. *
  67. * This operation is careful to avoid the livelock which could easily happen
  68. * if two or more filesystems are being continuously dirtied. s_need_sync
  69. * is used only here. We set it against all filesystems and then clear it as
  70. * we sync them. So redirtied filesystems are skipped.
  71. *
  72. * But if process A is currently running sync_filesystems and then process B
  73. * calls sync_filesystems as well, process B will set all the s_need_sync
  74. * flags again, which will cause process A to resync everything. Fix that with
  75. * a local mutex.
  76. */
  77. static void sync_filesystems(int wait)
  78. {
  79. struct super_block *sb;
  80. static DEFINE_MUTEX(mutex);
  81. mutex_lock(&mutex); /* Could be down_interruptible */
  82. spin_lock(&sb_lock);
  83. list_for_each_entry(sb, &super_blocks, s_list)
  84. sb->s_need_sync = 1;
  85. restart:
  86. list_for_each_entry(sb, &super_blocks, s_list) {
  87. if (!sb->s_need_sync)
  88. continue;
  89. sb->s_need_sync = 0;
  90. sb->s_count++;
  91. spin_unlock(&sb_lock);
  92. down_read(&sb->s_umount);
  93. if (!(sb->s_flags & MS_RDONLY) && sb->s_root)
  94. __sync_filesystem(sb, wait);
  95. up_read(&sb->s_umount);
  96. /* restart only when sb is no longer on the list */
  97. spin_lock(&sb_lock);
  98. if (__put_super_and_need_restart(sb))
  99. goto restart;
  100. }
  101. spin_unlock(&sb_lock);
  102. mutex_unlock(&mutex);
  103. }
  104. /*
  105. * sync everything. Start out by waking pdflush, because that writes back
  106. * all queues in parallel.
  107. */
  108. SYSCALL_DEFINE0(sync)
  109. {
  110. wakeup_flusher_threads(0);
  111. sync_filesystems(0);
  112. sync_filesystems(1);
  113. if (unlikely(laptop_mode))
  114. laptop_sync_completion();
  115. return 0;
  116. }
  117. static void do_sync_work(struct work_struct *work)
  118. {
  119. /*
  120. * Sync twice to reduce the possibility we skipped some inodes / pages
  121. * because they were temporarily locked
  122. */
  123. sync_filesystems(0);
  124. sync_filesystems(0);
  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. * Generic function to fsync a file.
  139. *
  140. * filp may be NULL if called via the msync of a vma.
  141. */
  142. int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
  143. {
  144. struct inode * inode = dentry->d_inode;
  145. struct super_block * sb;
  146. int ret, err;
  147. /* sync the inode to buffers */
  148. ret = write_inode_now(inode, 0);
  149. /* sync the superblock to buffers */
  150. sb = inode->i_sb;
  151. if (sb->s_dirt && sb->s_op->write_super)
  152. sb->s_op->write_super(sb);
  153. /* .. finally sync the buffers to disk */
  154. err = sync_blockdev(sb->s_bdev);
  155. if (!ret)
  156. ret = err;
  157. return ret;
  158. }
  159. /**
  160. * vfs_fsync_range - helper to sync a range of data & metadata to disk
  161. * @file: file to sync
  162. * @dentry: dentry of @file
  163. * @start: offset in bytes of the beginning of data range to sync
  164. * @end: offset in bytes of the end of data range (inclusive)
  165. * @datasync: perform only datasync
  166. *
  167. * Write back data in range @start..@end and metadata for @file to disk. If
  168. * @datasync is set only metadata needed to access modified file data is
  169. * written.
  170. *
  171. * In case this function is called from nfsd @file may be %NULL and
  172. * only @dentry is set. This can only happen when the filesystem
  173. * implements the export_operations API.
  174. */
  175. int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start,
  176. loff_t end, int datasync)
  177. {
  178. const struct file_operations *fop;
  179. struct address_space *mapping;
  180. int err, ret;
  181. /*
  182. * Get mapping and operations from the file in case we have
  183. * as file, or get the default values for them in case we
  184. * don't have a struct file available. Damn nfsd..
  185. */
  186. if (file) {
  187. mapping = file->f_mapping;
  188. fop = file->f_op;
  189. } else {
  190. mapping = dentry->d_inode->i_mapping;
  191. fop = dentry->d_inode->i_fop;
  192. }
  193. if (!fop || !fop->fsync) {
  194. ret = -EINVAL;
  195. goto out;
  196. }
  197. ret = filemap_fdatawrite_range(mapping, start, end);
  198. /*
  199. * We need to protect against concurrent writers, which could cause
  200. * livelocks in fsync_buffers_list().
  201. */
  202. mutex_lock(&mapping->host->i_mutex);
  203. err = fop->fsync(file, dentry, datasync);
  204. if (!ret)
  205. ret = err;
  206. mutex_unlock(&mapping->host->i_mutex);
  207. err = filemap_fdatawait_range(mapping, start, end);
  208. if (!ret)
  209. ret = err;
  210. out:
  211. return ret;
  212. }
  213. EXPORT_SYMBOL(vfs_fsync_range);
  214. /**
  215. * vfs_fsync - perform a fsync or fdatasync on a file
  216. * @file: file to sync
  217. * @dentry: dentry of @file
  218. * @datasync: only perform a fdatasync operation
  219. *
  220. * Write back data and metadata for @file to disk. If @datasync is
  221. * set only metadata needed to access modified file data is written.
  222. *
  223. * In case this function is called from nfsd @file may be %NULL and
  224. * only @dentry is set. This can only happen when the filesystem
  225. * implements the export_operations API.
  226. */
  227. int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  228. {
  229. return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync);
  230. }
  231. EXPORT_SYMBOL(vfs_fsync);
  232. static int do_fsync(unsigned int fd, int datasync)
  233. {
  234. struct file *file;
  235. int ret = -EBADF;
  236. file = fget(fd);
  237. if (file) {
  238. ret = vfs_fsync(file, file->f_path.dentry, datasync);
  239. fput(file);
  240. }
  241. return ret;
  242. }
  243. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  244. {
  245. return do_fsync(fd, 0);
  246. }
  247. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  248. {
  249. return do_fsync(fd, 1);
  250. }
  251. /**
  252. * generic_write_sync - perform syncing after a write if file / inode is sync
  253. * @file: file to which the write happened
  254. * @pos: offset where the write started
  255. * @count: length of the write
  256. *
  257. * This is just a simple wrapper about our general syncing function.
  258. */
  259. int generic_write_sync(struct file *file, loff_t pos, loff_t count)
  260. {
  261. if (!(file->f_flags & O_SYNC) && !IS_SYNC(file->f_mapping->host))
  262. return 0;
  263. return vfs_fsync_range(file, file->f_path.dentry, pos,
  264. pos + count - 1, 1);
  265. }
  266. EXPORT_SYMBOL(generic_write_sync);
  267. /*
  268. * sys_sync_file_range() permits finely controlled syncing over a segment of
  269. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  270. * zero then sys_sync_file_range() will operate from offset out to EOF.
  271. *
  272. * The flag bits are:
  273. *
  274. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  275. * before performing the write.
  276. *
  277. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  278. * range which are not presently under writeback. Note that this may block for
  279. * significant periods due to exhaustion of disk request structures.
  280. *
  281. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  282. * after performing the write.
  283. *
  284. * Useful combinations of the flag bits are:
  285. *
  286. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  287. * in the range which were dirty on entry to sys_sync_file_range() are placed
  288. * under writeout. This is a start-write-for-data-integrity operation.
  289. *
  290. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  291. * are not presently under writeout. This is an asynchronous flush-to-disk
  292. * operation. Not suitable for data integrity operations.
  293. *
  294. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  295. * completion of writeout of all pages in the range. This will be used after an
  296. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  297. * for that operation to complete and to return the result.
  298. *
  299. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  300. * a traditional sync() operation. This is a write-for-data-integrity operation
  301. * which will ensure that all pages in the range which were dirty on entry to
  302. * sys_sync_file_range() are committed to disk.
  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. SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
  315. unsigned int flags)
  316. {
  317. int ret;
  318. struct file *file;
  319. loff_t endbyte; /* inclusive */
  320. int fput_needed;
  321. umode_t i_mode;
  322. ret = -EINVAL;
  323. if (flags & ~VALID_FLAGS)
  324. goto out;
  325. endbyte = offset + nbytes;
  326. if ((s64)offset < 0)
  327. goto out;
  328. if ((s64)endbyte < 0)
  329. goto out;
  330. if (endbyte < offset)
  331. goto out;
  332. if (sizeof(pgoff_t) == 4) {
  333. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  334. /*
  335. * The range starts outside a 32 bit machine's
  336. * pagecache addressing capabilities. Let it "succeed"
  337. */
  338. ret = 0;
  339. goto out;
  340. }
  341. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  342. /*
  343. * Out to EOF
  344. */
  345. nbytes = 0;
  346. }
  347. }
  348. if (nbytes == 0)
  349. endbyte = LLONG_MAX;
  350. else
  351. endbyte--; /* inclusive */
  352. ret = -EBADF;
  353. file = fget_light(fd, &fput_needed);
  354. if (!file)
  355. goto out;
  356. i_mode = file->f_path.dentry->d_inode->i_mode;
  357. ret = -ESPIPE;
  358. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  359. !S_ISLNK(i_mode))
  360. goto out_put;
  361. ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
  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
  392. /*
  393. * `endbyte' is inclusive
  394. */
  395. int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
  396. loff_t endbyte, unsigned int flags)
  397. {
  398. int ret;
  399. if (!mapping) {
  400. ret = -EINVAL;
  401. goto out;
  402. }
  403. ret = 0;
  404. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  405. ret = wait_on_page_writeback_range(mapping,
  406. offset >> PAGE_CACHE_SHIFT,
  407. endbyte >> PAGE_CACHE_SHIFT);
  408. if (ret < 0)
  409. goto out;
  410. }
  411. if (flags & SYNC_FILE_RANGE_WRITE) {
  412. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  413. WB_SYNC_ALL);
  414. if (ret < 0)
  415. goto out;
  416. }
  417. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  418. ret = wait_on_page_writeback_range(mapping,
  419. offset >> PAGE_CACHE_SHIFT,
  420. endbyte >> PAGE_CACHE_SHIFT);
  421. }
  422. out:
  423. return ret;
  424. }
  425. EXPORT_SYMBOL_GPL(do_sync_mapping_range);