sync.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. static void sync_one_sb(struct super_block *sb, void *arg)
  70. {
  71. if (!(sb->s_flags & MS_RDONLY) && sb->s_bdi)
  72. __sync_filesystem(sb, *(int *)arg);
  73. }
  74. /*
  75. * Sync all the data for all the filesystems (called by sys_sync() and
  76. * emergency sync)
  77. */
  78. static void sync_filesystems(int wait)
  79. {
  80. iterate_supers(sync_one_sb, &wait);
  81. }
  82. /*
  83. * sync everything. Start out by waking pdflush, because that writes back
  84. * all queues in parallel.
  85. */
  86. SYSCALL_DEFINE0(sync)
  87. {
  88. wakeup_flusher_threads(0);
  89. sync_filesystems(0);
  90. sync_filesystems(1);
  91. if (unlikely(laptop_mode))
  92. laptop_sync_completion();
  93. return 0;
  94. }
  95. static void do_sync_work(struct work_struct *work)
  96. {
  97. /*
  98. * Sync twice to reduce the possibility we skipped some inodes / pages
  99. * because they were temporarily locked
  100. */
  101. sync_filesystems(0);
  102. sync_filesystems(0);
  103. printk("Emergency Sync complete\n");
  104. kfree(work);
  105. }
  106. void emergency_sync(void)
  107. {
  108. struct work_struct *work;
  109. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  110. if (work) {
  111. INIT_WORK(work, do_sync_work);
  112. schedule_work(work);
  113. }
  114. }
  115. /*
  116. * Generic function to fsync a file.
  117. */
  118. int file_fsync(struct file *filp, int datasync)
  119. {
  120. struct inode *inode = filp->f_mapping->host;
  121. struct super_block * sb;
  122. int ret, err;
  123. /* sync the inode to buffers */
  124. ret = write_inode_now(inode, 0);
  125. /* sync the superblock to buffers */
  126. sb = inode->i_sb;
  127. if (sb->s_dirt && sb->s_op->write_super)
  128. sb->s_op->write_super(sb);
  129. /* .. finally sync the buffers to disk */
  130. err = sync_blockdev(sb->s_bdev);
  131. if (!ret)
  132. ret = err;
  133. return ret;
  134. }
  135. EXPORT_SYMBOL(file_fsync);
  136. /**
  137. * vfs_fsync_range - helper to sync a range of data & metadata to disk
  138. * @file: file to sync
  139. * @start: offset in bytes of the beginning of data range to sync
  140. * @end: offset in bytes of the end of data range (inclusive)
  141. * @datasync: perform only datasync
  142. *
  143. * Write back data in range @start..@end and metadata for @file to disk. If
  144. * @datasync is set only metadata needed to access modified file data is
  145. * written.
  146. */
  147. int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
  148. {
  149. struct address_space *mapping = file->f_mapping;
  150. int err, ret;
  151. if (!file->f_op || !file->f_op->fsync) {
  152. ret = -EINVAL;
  153. goto out;
  154. }
  155. ret = filemap_write_and_wait_range(mapping, start, end);
  156. /*
  157. * We need to protect against concurrent writers, which could cause
  158. * livelocks in fsync_buffers_list().
  159. */
  160. mutex_lock(&mapping->host->i_mutex);
  161. err = file->f_op->fsync(file, datasync);
  162. if (!ret)
  163. ret = err;
  164. mutex_unlock(&mapping->host->i_mutex);
  165. out:
  166. return ret;
  167. }
  168. EXPORT_SYMBOL(vfs_fsync_range);
  169. /**
  170. * vfs_fsync - perform a fsync or fdatasync on a file
  171. * @file: file to sync
  172. * @datasync: only perform a fdatasync operation
  173. *
  174. * Write back data and metadata for @file to disk. If @datasync is
  175. * set only metadata needed to access modified file data is written.
  176. */
  177. int vfs_fsync(struct file *file, int datasync)
  178. {
  179. return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
  180. }
  181. EXPORT_SYMBOL(vfs_fsync);
  182. static int do_fsync(unsigned int fd, int datasync)
  183. {
  184. struct file *file;
  185. int ret = -EBADF;
  186. file = fget(fd);
  187. if (file) {
  188. ret = vfs_fsync(file, datasync);
  189. fput(file);
  190. }
  191. return ret;
  192. }
  193. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  194. {
  195. return do_fsync(fd, 0);
  196. }
  197. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  198. {
  199. return do_fsync(fd, 1);
  200. }
  201. /**
  202. * generic_write_sync - perform syncing after a write if file / inode is sync
  203. * @file: file to which the write happened
  204. * @pos: offset where the write started
  205. * @count: length of the write
  206. *
  207. * This is just a simple wrapper about our general syncing function.
  208. */
  209. int generic_write_sync(struct file *file, loff_t pos, loff_t count)
  210. {
  211. if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host))
  212. return 0;
  213. return vfs_fsync_range(file, pos, pos + count - 1,
  214. (file->f_flags & __O_SYNC) ? 0 : 1);
  215. }
  216. EXPORT_SYMBOL(generic_write_sync);
  217. /*
  218. * sys_sync_file_range() permits finely controlled syncing over a segment of
  219. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  220. * zero then sys_sync_file_range() will operate from offset out to EOF.
  221. *
  222. * The flag bits are:
  223. *
  224. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  225. * before performing the write.
  226. *
  227. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  228. * range which are not presently under writeback. Note that this may block for
  229. * significant periods due to exhaustion of disk request structures.
  230. *
  231. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  232. * after performing the write.
  233. *
  234. * Useful combinations of the flag bits are:
  235. *
  236. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  237. * in the range which were dirty on entry to sys_sync_file_range() are placed
  238. * under writeout. This is a start-write-for-data-integrity operation.
  239. *
  240. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  241. * are not presently under writeout. This is an asynchronous flush-to-disk
  242. * operation. Not suitable for data integrity operations.
  243. *
  244. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  245. * completion of writeout of all pages in the range. This will be used after an
  246. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  247. * for that operation to complete and to return the result.
  248. *
  249. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  250. * a traditional sync() operation. This is a write-for-data-integrity operation
  251. * which will ensure that all pages in the range which were dirty on entry to
  252. * sys_sync_file_range() are committed to disk.
  253. *
  254. *
  255. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  256. * I/O errors or ENOSPC conditions and will return those to the caller, after
  257. * clearing the EIO and ENOSPC flags in the address_space.
  258. *
  259. * It should be noted that none of these operations write out the file's
  260. * metadata. So unless the application is strictly performing overwrites of
  261. * already-instantiated disk blocks, there are no guarantees here that the data
  262. * will be available after a crash.
  263. */
  264. SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
  265. unsigned int flags)
  266. {
  267. int ret;
  268. struct file *file;
  269. struct address_space *mapping;
  270. loff_t endbyte; /* inclusive */
  271. int fput_needed;
  272. umode_t i_mode;
  273. ret = -EINVAL;
  274. if (flags & ~VALID_FLAGS)
  275. goto out;
  276. endbyte = offset + nbytes;
  277. if ((s64)offset < 0)
  278. goto out;
  279. if ((s64)endbyte < 0)
  280. goto out;
  281. if (endbyte < offset)
  282. goto out;
  283. if (sizeof(pgoff_t) == 4) {
  284. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  285. /*
  286. * The range starts outside a 32 bit machine's
  287. * pagecache addressing capabilities. Let it "succeed"
  288. */
  289. ret = 0;
  290. goto out;
  291. }
  292. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  293. /*
  294. * Out to EOF
  295. */
  296. nbytes = 0;
  297. }
  298. }
  299. if (nbytes == 0)
  300. endbyte = LLONG_MAX;
  301. else
  302. endbyte--; /* inclusive */
  303. ret = -EBADF;
  304. file = fget_light(fd, &fput_needed);
  305. if (!file)
  306. goto out;
  307. i_mode = file->f_path.dentry->d_inode->i_mode;
  308. ret = -ESPIPE;
  309. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  310. !S_ISLNK(i_mode))
  311. goto out_put;
  312. mapping = file->f_mapping;
  313. if (!mapping) {
  314. ret = -EINVAL;
  315. goto out_put;
  316. }
  317. ret = 0;
  318. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  319. ret = filemap_fdatawait_range(mapping, offset, endbyte);
  320. if (ret < 0)
  321. goto out_put;
  322. }
  323. if (flags & SYNC_FILE_RANGE_WRITE) {
  324. ret = filemap_fdatawrite_range(mapping, offset, endbyte);
  325. if (ret < 0)
  326. goto out_put;
  327. }
  328. if (flags & SYNC_FILE_RANGE_WAIT_AFTER)
  329. ret = filemap_fdatawait_range(mapping, offset, endbyte);
  330. out_put:
  331. fput_light(file, fput_needed);
  332. out:
  333. return ret;
  334. }
  335. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  336. asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
  337. long flags)
  338. {
  339. return SYSC_sync_file_range((int) fd, offset, nbytes,
  340. (unsigned int) flags);
  341. }
  342. SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
  343. #endif
  344. /* It would be nice if people remember that not all the world's an i386
  345. when they introduce new system calls */
  346. SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
  347. loff_t offset, loff_t nbytes)
  348. {
  349. return sys_sync_file_range(fd, offset, nbytes, flags);
  350. }
  351. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  352. asmlinkage long SyS_sync_file_range2(long fd, long flags,
  353. loff_t offset, loff_t nbytes)
  354. {
  355. return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
  356. offset, nbytes);
  357. }
  358. SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
  359. #endif