sync.c 11 KB

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