sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. /*
  28. * This should be safe, as we require bdi backing to actually
  29. * write out data in the first place
  30. */
  31. if (!sb->s_bdi)
  32. return 0;
  33. /* Avoid doing twice syncing and cache pruning for quota sync */
  34. if (!wait) {
  35. writeout_quota_sb(sb, -1);
  36. writeback_inodes_sb(sb);
  37. } else {
  38. sync_quota_sb(sb, -1);
  39. sync_inodes_sb(sb);
  40. }
  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. /*
  70. * Sync all the data for all the filesystems (called by sys_sync() and
  71. * emergency sync)
  72. *
  73. * This operation is careful to avoid the livelock which could easily happen
  74. * if two or more filesystems are being continuously dirtied. s_need_sync
  75. * is used only here. We set it against all filesystems and then clear it as
  76. * we sync them. So redirtied filesystems are skipped.
  77. *
  78. * But if process A is currently running sync_filesystems and then process B
  79. * calls sync_filesystems as well, process B will set all the s_need_sync
  80. * flags again, which will cause process A to resync everything. Fix that with
  81. * a local mutex.
  82. */
  83. static void sync_filesystems(int wait)
  84. {
  85. struct super_block *sb;
  86. static DEFINE_MUTEX(mutex);
  87. mutex_lock(&mutex); /* Could be down_interruptible */
  88. spin_lock(&sb_lock);
  89. list_for_each_entry(sb, &super_blocks, s_list)
  90. sb->s_need_sync = 1;
  91. restart:
  92. list_for_each_entry(sb, &super_blocks, s_list) {
  93. if (!sb->s_need_sync)
  94. continue;
  95. sb->s_need_sync = 0;
  96. sb->s_count++;
  97. spin_unlock(&sb_lock);
  98. down_read(&sb->s_umount);
  99. if (!(sb->s_flags & MS_RDONLY) && sb->s_root && sb->s_bdi)
  100. __sync_filesystem(sb, wait);
  101. up_read(&sb->s_umount);
  102. /* restart only when sb is no longer on the list */
  103. spin_lock(&sb_lock);
  104. if (__put_super_and_need_restart(sb))
  105. goto restart;
  106. }
  107. spin_unlock(&sb_lock);
  108. mutex_unlock(&mutex);
  109. }
  110. /*
  111. * sync everything. Start out by waking pdflush, because that writes back
  112. * all queues in parallel.
  113. */
  114. SYSCALL_DEFINE0(sync)
  115. {
  116. wakeup_flusher_threads(0);
  117. sync_filesystems(0);
  118. sync_filesystems(1);
  119. if (unlikely(laptop_mode))
  120. laptop_sync_completion();
  121. return 0;
  122. }
  123. static void do_sync_work(struct work_struct *work)
  124. {
  125. /*
  126. * Sync twice to reduce the possibility we skipped some inodes / pages
  127. * because they were temporarily locked
  128. */
  129. sync_filesystems(0);
  130. sync_filesystems(0);
  131. printk("Emergency Sync complete\n");
  132. kfree(work);
  133. }
  134. void emergency_sync(void)
  135. {
  136. struct work_struct *work;
  137. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  138. if (work) {
  139. INIT_WORK(work, do_sync_work);
  140. schedule_work(work);
  141. }
  142. }
  143. /*
  144. * Generic function to fsync a file.
  145. *
  146. * filp may be NULL if called via the msync of a vma.
  147. */
  148. int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
  149. {
  150. struct inode * inode = dentry->d_inode;
  151. struct super_block * sb;
  152. int ret, err;
  153. /* sync the inode to buffers */
  154. ret = write_inode_now(inode, 0);
  155. /* sync the superblock to buffers */
  156. sb = inode->i_sb;
  157. if (sb->s_dirt && sb->s_op->write_super)
  158. sb->s_op->write_super(sb);
  159. /* .. finally sync the buffers to disk */
  160. err = sync_blockdev(sb->s_bdev);
  161. if (!ret)
  162. ret = err;
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(file_fsync);
  166. /**
  167. * vfs_fsync_range - helper to sync a range of data & metadata to disk
  168. * @file: file to sync
  169. * @dentry: dentry of @file
  170. * @start: offset in bytes of the beginning of data range to sync
  171. * @end: offset in bytes of the end of data range (inclusive)
  172. * @datasync: perform only datasync
  173. *
  174. * Write back data in range @start..@end and metadata for @file to disk. If
  175. * @datasync is set only metadata needed to access modified file data is
  176. * written.
  177. *
  178. * In case this function is called from nfsd @file may be %NULL and
  179. * only @dentry is set. This can only happen when the filesystem
  180. * implements the export_operations API.
  181. */
  182. int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start,
  183. loff_t end, int datasync)
  184. {
  185. const struct file_operations *fop;
  186. struct address_space *mapping;
  187. int err, ret;
  188. /*
  189. * Get mapping and operations from the file in case we have
  190. * as file, or get the default values for them in case we
  191. * don't have a struct file available. Damn nfsd..
  192. */
  193. if (file) {
  194. mapping = file->f_mapping;
  195. fop = file->f_op;
  196. } else {
  197. mapping = dentry->d_inode->i_mapping;
  198. fop = dentry->d_inode->i_fop;
  199. }
  200. if (!fop || !fop->fsync) {
  201. ret = -EINVAL;
  202. goto out;
  203. }
  204. ret = filemap_write_and_wait_range(mapping, start, end);
  205. /*
  206. * We need to protect against concurrent writers, which could cause
  207. * livelocks in fsync_buffers_list().
  208. */
  209. mutex_lock(&mapping->host->i_mutex);
  210. err = fop->fsync(file, dentry, datasync);
  211. if (!ret)
  212. ret = err;
  213. mutex_unlock(&mapping->host->i_mutex);
  214. out:
  215. return ret;
  216. }
  217. EXPORT_SYMBOL(vfs_fsync_range);
  218. /**
  219. * vfs_fsync - perform a fsync or fdatasync on a file
  220. * @file: file to sync
  221. * @dentry: dentry of @file
  222. * @datasync: only perform a fdatasync operation
  223. *
  224. * Write back data and metadata for @file to disk. If @datasync is
  225. * set only metadata needed to access modified file data is written.
  226. *
  227. * In case this function is called from nfsd @file may be %NULL and
  228. * only @dentry is set. This can only happen when the filesystem
  229. * implements the export_operations API.
  230. */
  231. int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  232. {
  233. return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync);
  234. }
  235. EXPORT_SYMBOL(vfs_fsync);
  236. static int do_fsync(unsigned int fd, int datasync)
  237. {
  238. struct file *file;
  239. int ret = -EBADF;
  240. file = fget(fd);
  241. if (file) {
  242. ret = vfs_fsync(file, file->f_path.dentry, datasync);
  243. fput(file);
  244. }
  245. return ret;
  246. }
  247. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  248. {
  249. return do_fsync(fd, 0);
  250. }
  251. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  252. {
  253. return do_fsync(fd, 1);
  254. }
  255. /**
  256. * generic_write_sync - perform syncing after a write if file / inode is sync
  257. * @file: file to which the write happened
  258. * @pos: offset where the write started
  259. * @count: length of the write
  260. *
  261. * This is just a simple wrapper about our general syncing function.
  262. */
  263. int generic_write_sync(struct file *file, loff_t pos, loff_t count)
  264. {
  265. if (!(file->f_flags & O_SYNC) && !IS_SYNC(file->f_mapping->host))
  266. return 0;
  267. return vfs_fsync_range(file, file->f_path.dentry, pos,
  268. pos + count - 1, 1);
  269. }
  270. EXPORT_SYMBOL(generic_write_sync);
  271. /*
  272. * sys_sync_file_range() permits finely controlled syncing over a segment of
  273. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  274. * zero then sys_sync_file_range() will operate from offset out to EOF.
  275. *
  276. * The flag bits are:
  277. *
  278. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  279. * before performing the write.
  280. *
  281. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  282. * range which are not presently under writeback. Note that this may block for
  283. * significant periods due to exhaustion of disk request structures.
  284. *
  285. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  286. * after performing the write.
  287. *
  288. * Useful combinations of the flag bits are:
  289. *
  290. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  291. * in the range which were dirty on entry to sys_sync_file_range() are placed
  292. * under writeout. This is a start-write-for-data-integrity operation.
  293. *
  294. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  295. * are not presently under writeout. This is an asynchronous flush-to-disk
  296. * operation. Not suitable for data integrity operations.
  297. *
  298. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  299. * completion of writeout of all pages in the range. This will be used after an
  300. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  301. * for that operation to complete and to return the result.
  302. *
  303. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  304. * a traditional sync() operation. This is a write-for-data-integrity operation
  305. * which will ensure that all pages in the range which were dirty on entry to
  306. * sys_sync_file_range() are committed to disk.
  307. *
  308. *
  309. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  310. * I/O errors or ENOSPC conditions and will return those to the caller, after
  311. * clearing the EIO and ENOSPC flags in the address_space.
  312. *
  313. * It should be noted that none of these operations write out the file's
  314. * metadata. So unless the application is strictly performing overwrites of
  315. * already-instantiated disk blocks, there are no guarantees here that the data
  316. * will be available after a crash.
  317. */
  318. SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
  319. unsigned int flags)
  320. {
  321. int ret;
  322. struct file *file;
  323. loff_t endbyte; /* inclusive */
  324. int fput_needed;
  325. umode_t i_mode;
  326. ret = -EINVAL;
  327. if (flags & ~VALID_FLAGS)
  328. goto out;
  329. endbyte = offset + nbytes;
  330. if ((s64)offset < 0)
  331. goto out;
  332. if ((s64)endbyte < 0)
  333. goto out;
  334. if (endbyte < offset)
  335. goto out;
  336. if (sizeof(pgoff_t) == 4) {
  337. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  338. /*
  339. * The range starts outside a 32 bit machine's
  340. * pagecache addressing capabilities. Let it "succeed"
  341. */
  342. ret = 0;
  343. goto out;
  344. }
  345. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  346. /*
  347. * Out to EOF
  348. */
  349. nbytes = 0;
  350. }
  351. }
  352. if (nbytes == 0)
  353. endbyte = LLONG_MAX;
  354. else
  355. endbyte--; /* inclusive */
  356. ret = -EBADF;
  357. file = fget_light(fd, &fput_needed);
  358. if (!file)
  359. goto out;
  360. i_mode = file->f_path.dentry->d_inode->i_mode;
  361. ret = -ESPIPE;
  362. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  363. !S_ISLNK(i_mode))
  364. goto out_put;
  365. ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
  366. out_put:
  367. fput_light(file, fput_needed);
  368. out:
  369. return ret;
  370. }
  371. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  372. asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
  373. long flags)
  374. {
  375. return SYSC_sync_file_range((int) fd, offset, nbytes,
  376. (unsigned int) flags);
  377. }
  378. SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
  379. #endif
  380. /* It would be nice if people remember that not all the world's an i386
  381. when they introduce new system calls */
  382. SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
  383. loff_t offset, loff_t nbytes)
  384. {
  385. return sys_sync_file_range(fd, offset, nbytes, flags);
  386. }
  387. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  388. asmlinkage long SyS_sync_file_range2(long fd, long flags,
  389. loff_t offset, loff_t nbytes)
  390. {
  391. return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
  392. offset, nbytes);
  393. }
  394. SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
  395. #endif
  396. /*
  397. * `endbyte' is inclusive
  398. */
  399. int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
  400. loff_t endbyte, unsigned int flags)
  401. {
  402. int ret;
  403. if (!mapping) {
  404. ret = -EINVAL;
  405. goto out;
  406. }
  407. ret = 0;
  408. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  409. ret = wait_on_page_writeback_range(mapping,
  410. offset >> PAGE_CACHE_SHIFT,
  411. endbyte >> PAGE_CACHE_SHIFT);
  412. if (ret < 0)
  413. goto out;
  414. }
  415. if (flags & SYNC_FILE_RANGE_WRITE) {
  416. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  417. WB_SYNC_ALL);
  418. if (ret < 0)
  419. goto out;
  420. }
  421. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  422. ret = wait_on_page_writeback_range(mapping,
  423. offset >> PAGE_CACHE_SHIFT,
  424. endbyte >> PAGE_CACHE_SHIFT);
  425. }
  426. out:
  427. return ret;
  428. }
  429. EXPORT_SYMBOL_GPL(do_sync_mapping_range);