sync.c 12 KB

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