sync.c 11 KB

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