sync.c 8.4 KB

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