sync.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
  16. SYNC_FILE_RANGE_WAIT_AFTER)
  17. /*
  18. * sync everything. Start out by waking pdflush, because that writes back
  19. * all queues in parallel.
  20. */
  21. static void do_sync(unsigned long wait)
  22. {
  23. wakeup_pdflush(0);
  24. sync_inodes(0); /* All mappings, inodes and their blockdevs */
  25. vfs_dq_sync(NULL);
  26. sync_supers(); /* Write the superblocks */
  27. sync_filesystems(0); /* Start syncing the filesystems */
  28. sync_filesystems(wait); /* Waitingly sync the filesystems */
  29. sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
  30. if (!wait)
  31. printk("Emergency Sync complete\n");
  32. if (unlikely(laptop_mode))
  33. laptop_sync_completion();
  34. }
  35. SYSCALL_DEFINE0(sync)
  36. {
  37. do_sync(1);
  38. return 0;
  39. }
  40. static void do_sync_work(struct work_struct *work)
  41. {
  42. do_sync(0);
  43. kfree(work);
  44. }
  45. void emergency_sync(void)
  46. {
  47. struct work_struct *work;
  48. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  49. if (work) {
  50. INIT_WORK(work, do_sync_work);
  51. schedule_work(work);
  52. }
  53. }
  54. /*
  55. * Generic function to fsync a file.
  56. *
  57. * filp may be NULL if called via the msync of a vma.
  58. */
  59. int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
  60. {
  61. struct inode * inode = dentry->d_inode;
  62. struct super_block * sb;
  63. int ret, err;
  64. /* sync the inode to buffers */
  65. ret = write_inode_now(inode, 0);
  66. /* sync the superblock to buffers */
  67. sb = inode->i_sb;
  68. lock_super(sb);
  69. if (sb->s_dirt && sb->s_op->write_super)
  70. sb->s_op->write_super(sb);
  71. unlock_super(sb);
  72. /* .. finally sync the buffers to disk */
  73. err = sync_blockdev(sb->s_bdev);
  74. if (!ret)
  75. ret = err;
  76. return ret;
  77. }
  78. /**
  79. * vfs_fsync - perform a fsync or fdatasync on a file
  80. * @file: file to sync
  81. * @dentry: dentry of @file
  82. * @data: only perform a fdatasync operation
  83. *
  84. * Write back data and metadata for @file to disk. If @datasync is
  85. * set only metadata needed to access modified file data is written.
  86. *
  87. * In case this function is called from nfsd @file may be %NULL and
  88. * only @dentry is set. This can only happen when the filesystem
  89. * implements the export_operations API.
  90. */
  91. int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  92. {
  93. const struct file_operations *fop;
  94. struct address_space *mapping;
  95. int err, ret;
  96. /*
  97. * Get mapping and operations from the file in case we have
  98. * as file, or get the default values for them in case we
  99. * don't have a struct file available. Damn nfsd..
  100. */
  101. if (file) {
  102. mapping = file->f_mapping;
  103. fop = file->f_op;
  104. } else {
  105. mapping = dentry->d_inode->i_mapping;
  106. fop = dentry->d_inode->i_fop;
  107. }
  108. if (!fop || !fop->fsync) {
  109. ret = -EINVAL;
  110. goto out;
  111. }
  112. ret = filemap_fdatawrite(mapping);
  113. /*
  114. * We need to protect against concurrent writers, which could cause
  115. * livelocks in fsync_buffers_list().
  116. */
  117. mutex_lock(&mapping->host->i_mutex);
  118. err = fop->fsync(file, dentry, datasync);
  119. if (!ret)
  120. ret = err;
  121. mutex_unlock(&mapping->host->i_mutex);
  122. err = filemap_fdatawait(mapping);
  123. if (!ret)
  124. ret = err;
  125. out:
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(vfs_fsync);
  129. static int do_fsync(unsigned int fd, int datasync)
  130. {
  131. struct file *file;
  132. int ret = -EBADF;
  133. file = fget(fd);
  134. if (file) {
  135. ret = vfs_fsync(file, file->f_path.dentry, datasync);
  136. fput(file);
  137. }
  138. return ret;
  139. }
  140. SYSCALL_DEFINE1(fsync, unsigned int, fd)
  141. {
  142. return do_fsync(fd, 0);
  143. }
  144. SYSCALL_DEFINE1(fdatasync, unsigned int, fd)
  145. {
  146. return do_fsync(fd, 1);
  147. }
  148. /*
  149. * sys_sync_file_range() permits finely controlled syncing over a segment of
  150. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  151. * zero then sys_sync_file_range() will operate from offset out to EOF.
  152. *
  153. * The flag bits are:
  154. *
  155. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  156. * before performing the write.
  157. *
  158. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  159. * range which are not presently under writeback. Note that this may block for
  160. * significant periods due to exhaustion of disk request structures.
  161. *
  162. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  163. * after performing the write.
  164. *
  165. * Useful combinations of the flag bits are:
  166. *
  167. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  168. * in the range which were dirty on entry to sys_sync_file_range() are placed
  169. * under writeout. This is a start-write-for-data-integrity operation.
  170. *
  171. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  172. * are not presently under writeout. This is an asynchronous flush-to-disk
  173. * operation. Not suitable for data integrity operations.
  174. *
  175. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  176. * completion of writeout of all pages in the range. This will be used after an
  177. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  178. * for that operation to complete and to return the result.
  179. *
  180. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  181. * a traditional sync() operation. This is a write-for-data-integrity operation
  182. * which will ensure that all pages in the range which were dirty on entry to
  183. * sys_sync_file_range() are committed to disk.
  184. *
  185. *
  186. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  187. * I/O errors or ENOSPC conditions and will return those to the caller, after
  188. * clearing the EIO and ENOSPC flags in the address_space.
  189. *
  190. * It should be noted that none of these operations write out the file's
  191. * metadata. So unless the application is strictly performing overwrites of
  192. * already-instantiated disk blocks, there are no guarantees here that the data
  193. * will be available after a crash.
  194. */
  195. SYSCALL_DEFINE(sync_file_range)(int fd, loff_t offset, loff_t nbytes,
  196. unsigned int flags)
  197. {
  198. int ret;
  199. struct file *file;
  200. loff_t endbyte; /* inclusive */
  201. int fput_needed;
  202. umode_t i_mode;
  203. ret = -EINVAL;
  204. if (flags & ~VALID_FLAGS)
  205. goto out;
  206. endbyte = offset + nbytes;
  207. if ((s64)offset < 0)
  208. goto out;
  209. if ((s64)endbyte < 0)
  210. goto out;
  211. if (endbyte < offset)
  212. goto out;
  213. if (sizeof(pgoff_t) == 4) {
  214. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  215. /*
  216. * The range starts outside a 32 bit machine's
  217. * pagecache addressing capabilities. Let it "succeed"
  218. */
  219. ret = 0;
  220. goto out;
  221. }
  222. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  223. /*
  224. * Out to EOF
  225. */
  226. nbytes = 0;
  227. }
  228. }
  229. if (nbytes == 0)
  230. endbyte = LLONG_MAX;
  231. else
  232. endbyte--; /* inclusive */
  233. ret = -EBADF;
  234. file = fget_light(fd, &fput_needed);
  235. if (!file)
  236. goto out;
  237. i_mode = file->f_path.dentry->d_inode->i_mode;
  238. ret = -ESPIPE;
  239. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  240. !S_ISLNK(i_mode))
  241. goto out_put;
  242. ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
  243. out_put:
  244. fput_light(file, fput_needed);
  245. out:
  246. return ret;
  247. }
  248. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  249. asmlinkage long SyS_sync_file_range(long fd, loff_t offset, loff_t nbytes,
  250. long flags)
  251. {
  252. return SYSC_sync_file_range((int) fd, offset, nbytes,
  253. (unsigned int) flags);
  254. }
  255. SYSCALL_ALIAS(sys_sync_file_range, SyS_sync_file_range);
  256. #endif
  257. /* It would be nice if people remember that not all the world's an i386
  258. when they introduce new system calls */
  259. SYSCALL_DEFINE(sync_file_range2)(int fd, unsigned int flags,
  260. loff_t offset, loff_t nbytes)
  261. {
  262. return sys_sync_file_range(fd, offset, nbytes, flags);
  263. }
  264. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  265. asmlinkage long SyS_sync_file_range2(long fd, long flags,
  266. loff_t offset, loff_t nbytes)
  267. {
  268. return SYSC_sync_file_range2((int) fd, (unsigned int) flags,
  269. offset, nbytes);
  270. }
  271. SYSCALL_ALIAS(sys_sync_file_range2, SyS_sync_file_range2);
  272. #endif
  273. /*
  274. * `endbyte' is inclusive
  275. */
  276. int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
  277. loff_t endbyte, unsigned int flags)
  278. {
  279. int ret;
  280. if (!mapping) {
  281. ret = -EINVAL;
  282. goto out;
  283. }
  284. ret = 0;
  285. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  286. ret = wait_on_page_writeback_range(mapping,
  287. offset >> PAGE_CACHE_SHIFT,
  288. endbyte >> PAGE_CACHE_SHIFT);
  289. if (ret < 0)
  290. goto out;
  291. }
  292. if (flags & SYNC_FILE_RANGE_WRITE) {
  293. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  294. WB_SYNC_ALL);
  295. if (ret < 0)
  296. goto out;
  297. }
  298. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  299. ret = wait_on_page_writeback_range(mapping,
  300. offset >> PAGE_CACHE_SHIFT,
  301. endbyte >> PAGE_CACHE_SHIFT);
  302. }
  303. out:
  304. return ret;
  305. }
  306. EXPORT_SYMBOL_GPL(do_sync_mapping_range);