sync.c 9.8 KB

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