sync.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 - perform a fsync or fdatasync on a file
  161. * @file: file to sync
  162. * @dentry: dentry of @file
  163. * @data: only perform a fdatasync operation
  164. *
  165. * Write back data and metadata for @file to disk. If @datasync is
  166. * set only metadata needed to access modified file data is written.
  167. *
  168. * In case this function is called from nfsd @file may be %NULL and
  169. * only @dentry is set. This can only happen when the filesystem
  170. * implements the export_operations API.
  171. */
  172. int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  173. {
  174. const struct file_operations *fop;
  175. struct address_space *mapping;
  176. int err, ret;
  177. /*
  178. * Get mapping and operations from the file in case we have
  179. * as file, or get the default values for them in case we
  180. * don't have a struct file available. Damn nfsd..
  181. */
  182. if (file) {
  183. mapping = file->f_mapping;
  184. fop = file->f_op;
  185. } else {
  186. mapping = dentry->d_inode->i_mapping;
  187. fop = dentry->d_inode->i_fop;
  188. }
  189. if (!fop || !fop->fsync) {
  190. ret = -EINVAL;
  191. goto out;
  192. }
  193. ret = filemap_fdatawrite(mapping);
  194. /*
  195. * We need to protect against concurrent writers, which could cause
  196. * livelocks in fsync_buffers_list().
  197. */
  198. mutex_lock(&mapping->host->i_mutex);
  199. err = fop->fsync(file, dentry, datasync);
  200. if (!ret)
  201. ret = err;
  202. mutex_unlock(&mapping->host->i_mutex);
  203. err = filemap_fdatawait(mapping);
  204. if (!ret)
  205. ret = err;
  206. out:
  207. return ret;
  208. }
  209. EXPORT_SYMBOL(vfs_fsync);
  210. static int do_fsync(unsigned int fd, int datasync)
  211. {
  212. struct file *file;
  213. int ret = -EBADF;
  214. file = fget(fd);
  215. if (file) {
  216. ret = vfs_fsync(file, file->f_path.dentry, datasync);
  217. fput(file);
  218. }
  219. return ret;
  220. }
  221. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  222. {
  223. return do_fsync(fd, 0);
  224. }
  225. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  226. {
  227. return do_fsync(fd, 1);
  228. }
  229. /*
  230. * sys_sync_file_range() permits finely controlled syncing over a segment of
  231. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  232. * zero then sys_sync_file_range() will operate from offset out to EOF.
  233. *
  234. * The flag bits are:
  235. *
  236. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  237. * before performing the write.
  238. *
  239. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  240. * range which are not presently under writeback. Note that this may block for
  241. * significant periods due to exhaustion of disk request structures.
  242. *
  243. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  244. * after performing the write.
  245. *
  246. * Useful combinations of the flag bits are:
  247. *
  248. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  249. * in the range which were dirty on entry to sys_sync_file_range() are placed
  250. * under writeout. This is a start-write-for-data-integrity operation.
  251. *
  252. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  253. * are not presently under writeout. This is an asynchronous flush-to-disk
  254. * operation. Not suitable for data integrity operations.
  255. *
  256. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  257. * completion of writeout of all pages in the range. This will be used after an
  258. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  259. * for that operation to complete and to return the result.
  260. *
  261. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  262. * a traditional sync() operation. This is a write-for-data-integrity operation
  263. * which will ensure that all pages in the range which were dirty on entry to
  264. * sys_sync_file_range() are committed to disk.
  265. *
  266. *
  267. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  268. * I/O errors or ENOSPC conditions and will return those to the caller, after
  269. * clearing the EIO and ENOSPC flags in the address_space.
  270. *
  271. * It should be noted that none of these operations write out the file's
  272. * metadata. So unless the application is strictly performing overwrites of
  273. * already-instantiated disk blocks, there are no guarantees here that the data
  274. * will be available after a crash.
  275. */
  276. SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
  277. unsigned int flags)
  278. {
  279. int ret;
  280. struct file *file;
  281. loff_t endbyte; /* inclusive */
  282. int fput_needed;
  283. umode_t i_mode;
  284. ret = -EINVAL;
  285. if (flags & ~VALID_FLAGS)
  286. goto out;
  287. endbyte = offset + nbytes;
  288. if ((s64)offset < 0)
  289. goto out;
  290. if ((s64)endbyte < 0)
  291. goto out;
  292. if (endbyte < offset)
  293. goto out;
  294. if (sizeof(pgoff_t) == 4) {
  295. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  296. /*
  297. * The range starts outside a 32 bit machine's
  298. * pagecache addressing capabilities. Let it "succeed"
  299. */
  300. ret = 0;
  301. goto out;
  302. }
  303. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  304. /*
  305. * Out to EOF
  306. */
  307. nbytes = 0;
  308. }
  309. }
  310. if (nbytes == 0)
  311. endbyte = LLONG_MAX;
  312. else
  313. endbyte--; /* inclusive */
  314. ret = -EBADF;
  315. file = fget_light(fd, &fput_needed);
  316. if (!file)
  317. goto out;
  318. i_mode = file->f_path.dentry->d_inode->i_mode;
  319. ret = -ESPIPE;
  320. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  321. !S_ISLNK(i_mode))
  322. goto out_put;
  323. ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
  324. out_put:
  325. fput_light(file, fput_needed);
  326. out:
  327. return ret;
  328. }
  329. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  330. asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
  331. long flags)
  332. {
  333. return SYSC_sync_file_range((int) fd, offset, nbytes,
  334. (unsigned int) flags);
  335. }
  336. SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
  337. #endif
  338. /* It would be nice if people remember that not all the world's an i386
  339. when they introduce new system calls */
  340. SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
  341. loff_t offset, loff_t nbytes)
  342. {
  343. return sys_sync_file_range(fd, offset, nbytes, flags);
  344. }
  345. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  346. asmlinkage long SyS_sync_file_range2(long fd, long flags,
  347. loff_t offset, loff_t nbytes)
  348. {
  349. return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
  350. offset, nbytes);
  351. }
  352. SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
  353. #endif
  354. /*
  355. * `endbyte' is inclusive
  356. */
  357. int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
  358. loff_t endbyte, unsigned int flags)
  359. {
  360. int ret;
  361. if (!mapping) {
  362. ret = -EINVAL;
  363. goto out;
  364. }
  365. ret = 0;
  366. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  367. ret = wait_on_page_writeback_range(mapping,
  368. offset >> PAGE_CACHE_SHIFT,
  369. endbyte >> PAGE_CACHE_SHIFT);
  370. if (ret < 0)
  371. goto out;
  372. }
  373. if (flags & SYNC_FILE_RANGE_WRITE) {
  374. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  375. WB_SYNC_ALL);
  376. if (ret < 0)
  377. goto out;
  378. }
  379. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  380. ret = wait_on_page_writeback_range(mapping,
  381. offset >> PAGE_CACHE_SHIFT,
  382. endbyte >> PAGE_CACHE_SHIFT);
  383. }
  384. out:
  385. return ret;
  386. }
  387. EXPORT_SYMBOL_GPL(do_sync_mapping_range);