sync.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_write_and_wait_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. out:
  208. return ret;
  209. }
  210. EXPORT_SYMBOL(vfs_fsync_range);
  211. /**
  212. * vfs_fsync - perform a fsync or fdatasync on a file
  213. * @file: file to sync
  214. * @dentry: dentry of @file
  215. * @datasync: only perform a fdatasync operation
  216. *
  217. * Write back data and metadata for @file to disk. If @datasync is
  218. * set only metadata needed to access modified file data is written.
  219. *
  220. * In case this function is called from nfsd @file may be %NULL and
  221. * only @dentry is set. This can only happen when the filesystem
  222. * implements the export_operations API.
  223. */
  224. int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  225. {
  226. return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync);
  227. }
  228. EXPORT_SYMBOL(vfs_fsync);
  229. static int do_fsync(unsigned int fd, int datasync)
  230. {
  231. struct file *file;
  232. int ret = -EBADF;
  233. file = fget(fd);
  234. if (file) {
  235. ret = vfs_fsync(file, file->f_path.dentry, datasync);
  236. fput(file);
  237. }
  238. return ret;
  239. }
  240. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  241. {
  242. return do_fsync(fd, 0);
  243. }
  244. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  245. {
  246. return do_fsync(fd, 1);
  247. }
  248. /**
  249. * generic_write_sync - perform syncing after a write if file / inode is sync
  250. * @file: file to which the write happened
  251. * @pos: offset where the write started
  252. * @count: length of the write
  253. *
  254. * This is just a simple wrapper about our general syncing function.
  255. */
  256. int generic_write_sync(struct file *file, loff_t pos, loff_t count)
  257. {
  258. if (!(file->f_flags & O_SYNC) && !IS_SYNC(file->f_mapping->host))
  259. return 0;
  260. return vfs_fsync_range(file, file->f_path.dentry, pos,
  261. pos + count - 1, 1);
  262. }
  263. EXPORT_SYMBOL(generic_write_sync);
  264. /*
  265. * sys_sync_file_range() permits finely controlled syncing over a segment of
  266. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  267. * zero then sys_sync_file_range() will operate from offset out to EOF.
  268. *
  269. * The flag bits are:
  270. *
  271. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  272. * before performing the write.
  273. *
  274. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  275. * range which are not presently under writeback. Note that this may block for
  276. * significant periods due to exhaustion of disk request structures.
  277. *
  278. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  279. * after performing the write.
  280. *
  281. * Useful combinations of the flag bits are:
  282. *
  283. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  284. * in the range which were dirty on entry to sys_sync_file_range() are placed
  285. * under writeout. This is a start-write-for-data-integrity operation.
  286. *
  287. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  288. * are not presently under writeout. This is an asynchronous flush-to-disk
  289. * operation. Not suitable for data integrity operations.
  290. *
  291. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  292. * completion of writeout of all pages in the range. This will be used after an
  293. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  294. * for that operation to complete and to return the result.
  295. *
  296. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  297. * a traditional sync() operation. This is a write-for-data-integrity operation
  298. * which will ensure that all pages in the range which were dirty on entry to
  299. * sys_sync_file_range() are committed to disk.
  300. *
  301. *
  302. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  303. * I/O errors or ENOSPC conditions and will return those to the caller, after
  304. * clearing the EIO and ENOSPC flags in the address_space.
  305. *
  306. * It should be noted that none of these operations write out the file's
  307. * metadata. So unless the application is strictly performing overwrites of
  308. * already-instantiated disk blocks, there are no guarantees here that the data
  309. * will be available after a crash.
  310. */
  311. SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
  312. unsigned int flags)
  313. {
  314. int ret;
  315. struct file *file;
  316. loff_t endbyte; /* inclusive */
  317. int fput_needed;
  318. umode_t i_mode;
  319. ret = -EINVAL;
  320. if (flags & ~VALID_FLAGS)
  321. goto out;
  322. endbyte = offset + nbytes;
  323. if ((s64)offset < 0)
  324. goto out;
  325. if ((s64)endbyte < 0)
  326. goto out;
  327. if (endbyte < offset)
  328. goto out;
  329. if (sizeof(pgoff_t) == 4) {
  330. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  331. /*
  332. * The range starts outside a 32 bit machine's
  333. * pagecache addressing capabilities. Let it "succeed"
  334. */
  335. ret = 0;
  336. goto out;
  337. }
  338. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  339. /*
  340. * Out to EOF
  341. */
  342. nbytes = 0;
  343. }
  344. }
  345. if (nbytes == 0)
  346. endbyte = LLONG_MAX;
  347. else
  348. endbyte--; /* inclusive */
  349. ret = -EBADF;
  350. file = fget_light(fd, &fput_needed);
  351. if (!file)
  352. goto out;
  353. i_mode = file->f_path.dentry->d_inode->i_mode;
  354. ret = -ESPIPE;
  355. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  356. !S_ISLNK(i_mode))
  357. goto out_put;
  358. ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
  359. out_put:
  360. fput_light(file, fput_needed);
  361. out:
  362. return ret;
  363. }
  364. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  365. asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
  366. long flags)
  367. {
  368. return SYSC_sync_file_range((int) fd, offset, nbytes,
  369. (unsigned int) flags);
  370. }
  371. SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
  372. #endif
  373. /* It would be nice if people remember that not all the world's an i386
  374. when they introduce new system calls */
  375. SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
  376. loff_t offset, loff_t nbytes)
  377. {
  378. return sys_sync_file_range(fd, offset, nbytes, flags);
  379. }
  380. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  381. asmlinkage long SyS_sync_file_range2(long fd, long flags,
  382. loff_t offset, loff_t nbytes)
  383. {
  384. return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
  385. offset, nbytes);
  386. }
  387. SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
  388. #endif
  389. /*
  390. * `endbyte' is inclusive
  391. */
  392. int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
  393. loff_t endbyte, unsigned int flags)
  394. {
  395. int ret;
  396. if (!mapping) {
  397. ret = -EINVAL;
  398. goto out;
  399. }
  400. ret = 0;
  401. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  402. ret = wait_on_page_writeback_range(mapping,
  403. offset >> PAGE_CACHE_SHIFT,
  404. endbyte >> PAGE_CACHE_SHIFT);
  405. if (ret < 0)
  406. goto out;
  407. }
  408. if (flags & SYNC_FILE_RANGE_WRITE) {
  409. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  410. WB_SYNC_ALL);
  411. if (ret < 0)
  412. goto out;
  413. }
  414. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  415. ret = wait_on_page_writeback_range(mapping,
  416. offset >> PAGE_CACHE_SHIFT,
  417. endbyte >> PAGE_CACHE_SHIFT);
  418. }
  419. out:
  420. return ret;
  421. }
  422. EXPORT_SYMBOL_GPL(do_sync_mapping_range);