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