sync.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. DQUOT_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. asmlinkage long sys_sync(void)
  36. {
  37. do_sync(1);
  38. return 0;
  39. }
  40. void emergency_sync(void)
  41. {
  42. pdflush_operation(do_sync, 0);
  43. }
  44. /*
  45. * Generic function to fsync a file.
  46. *
  47. * filp may be NULL if called via the msync of a vma.
  48. */
  49. int file_fsync(struct file *filp, struct dentry *dentry, int datasync)
  50. {
  51. struct inode * inode = dentry->d_inode;
  52. struct super_block * sb;
  53. int ret, err;
  54. /* sync the inode to buffers */
  55. ret = write_inode_now(inode, 0);
  56. /* sync the superblock to buffers */
  57. sb = inode->i_sb;
  58. lock_super(sb);
  59. if (sb->s_dirt && sb->s_op->write_super)
  60. sb->s_op->write_super(sb);
  61. unlock_super(sb);
  62. /* .. finally sync the buffers to disk */
  63. err = sync_blockdev(sb->s_bdev);
  64. if (!ret)
  65. ret = err;
  66. return ret;
  67. }
  68. long do_fsync(struct file *file, int datasync)
  69. {
  70. int ret;
  71. int err;
  72. struct address_space *mapping = file->f_mapping;
  73. if (!file->f_op || !file->f_op->fsync) {
  74. /* Why? We can still call filemap_fdatawrite */
  75. ret = -EINVAL;
  76. goto out;
  77. }
  78. ret = filemap_fdatawrite(mapping);
  79. /*
  80. * We need to protect against concurrent writers, which could cause
  81. * livelocks in fsync_buffers_list().
  82. */
  83. mutex_lock(&mapping->host->i_mutex);
  84. err = file->f_op->fsync(file, file->f_path.dentry, datasync);
  85. if (!ret)
  86. ret = err;
  87. mutex_unlock(&mapping->host->i_mutex);
  88. err = filemap_fdatawait(mapping);
  89. if (!ret)
  90. ret = err;
  91. out:
  92. return ret;
  93. }
  94. static long __do_fsync(unsigned int fd, int datasync)
  95. {
  96. struct file *file;
  97. int ret = -EBADF;
  98. file = fget(fd);
  99. if (file) {
  100. ret = do_fsync(file, datasync);
  101. fput(file);
  102. }
  103. return ret;
  104. }
  105. asmlinkage long sys_fsync(unsigned int fd)
  106. {
  107. return __do_fsync(fd, 0);
  108. }
  109. asmlinkage long sys_fdatasync(unsigned int fd)
  110. {
  111. return __do_fsync(fd, 1);
  112. }
  113. /*
  114. * sys_sync_file_range() permits finely controlled syncing over a segment of
  115. * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is
  116. * zero then sys_sync_file_range() will operate from offset out to EOF.
  117. *
  118. * The flag bits are:
  119. *
  120. * SYNC_FILE_RANGE_WAIT_BEFORE: wait upon writeout of all pages in the range
  121. * before performing the write.
  122. *
  123. * SYNC_FILE_RANGE_WRITE: initiate writeout of all those dirty pages in the
  124. * range which are not presently under writeback. Note that this may block for
  125. * significant periods due to exhaustion of disk request structures.
  126. *
  127. * SYNC_FILE_RANGE_WAIT_AFTER: wait upon writeout of all pages in the range
  128. * after performing the write.
  129. *
  130. * Useful combinations of the flag bits are:
  131. *
  132. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE: ensures that all pages
  133. * in the range which were dirty on entry to sys_sync_file_range() are placed
  134. * under writeout. This is a start-write-for-data-integrity operation.
  135. *
  136. * SYNC_FILE_RANGE_WRITE: start writeout of all dirty pages in the range which
  137. * are not presently under writeout. This is an asynchronous flush-to-disk
  138. * operation. Not suitable for data integrity operations.
  139. *
  140. * SYNC_FILE_RANGE_WAIT_BEFORE (or SYNC_FILE_RANGE_WAIT_AFTER): wait for
  141. * completion of writeout of all pages in the range. This will be used after an
  142. * earlier SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE operation to wait
  143. * for that operation to complete and to return the result.
  144. *
  145. * SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE|SYNC_FILE_RANGE_WAIT_AFTER:
  146. * a traditional sync() operation. This is a write-for-data-integrity operation
  147. * which will ensure that all pages in the range which were dirty on entry to
  148. * sys_sync_file_range() are committed to disk.
  149. *
  150. *
  151. * SYNC_FILE_RANGE_WAIT_BEFORE and SYNC_FILE_RANGE_WAIT_AFTER will detect any
  152. * I/O errors or ENOSPC conditions and will return those to the caller, after
  153. * clearing the EIO and ENOSPC flags in the address_space.
  154. *
  155. * It should be noted that none of these operations write out the file's
  156. * metadata. So unless the application is strictly performing overwrites of
  157. * already-instantiated disk blocks, there are no guarantees here that the data
  158. * will be available after a crash.
  159. */
  160. asmlinkage long sys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
  161. unsigned int flags)
  162. {
  163. int ret;
  164. struct file *file;
  165. loff_t endbyte; /* inclusive */
  166. int fput_needed;
  167. umode_t i_mode;
  168. ret = -EINVAL;
  169. if (flags & ~VALID_FLAGS)
  170. goto out;
  171. endbyte = offset + nbytes;
  172. if ((s64)offset < 0)
  173. goto out;
  174. if ((s64)endbyte < 0)
  175. goto out;
  176. if (endbyte < offset)
  177. goto out;
  178. if (sizeof(pgoff_t) == 4) {
  179. if (offset >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  180. /*
  181. * The range starts outside a 32 bit machine's
  182. * pagecache addressing capabilities. Let it "succeed"
  183. */
  184. ret = 0;
  185. goto out;
  186. }
  187. if (endbyte >= (0x100000000ULL << PAGE_CACHE_SHIFT)) {
  188. /*
  189. * Out to EOF
  190. */
  191. nbytes = 0;
  192. }
  193. }
  194. if (nbytes == 0)
  195. endbyte = LLONG_MAX;
  196. else
  197. endbyte--; /* inclusive */
  198. ret = -EBADF;
  199. file = fget_light(fd, &fput_needed);
  200. if (!file)
  201. goto out;
  202. i_mode = file->f_path.dentry->d_inode->i_mode;
  203. ret = -ESPIPE;
  204. if (!S_ISREG(i_mode) && !S_ISBLK(i_mode) && !S_ISDIR(i_mode) &&
  205. !S_ISLNK(i_mode))
  206. goto out_put;
  207. ret = do_sync_mapping_range(file->f_mapping, offset, endbyte, flags);
  208. out_put:
  209. fput_light(file, fput_needed);
  210. out:
  211. return ret;
  212. }
  213. /* It would be nice if people remember that not all the world's an i386
  214. when they introduce new system calls */
  215. asmlinkage long sys_sync_file_range2(int fd, unsigned int flags,
  216. loff_t offset, loff_t nbytes)
  217. {
  218. return sys_sync_file_range(fd, offset, nbytes, flags);
  219. }
  220. /*
  221. * `endbyte' is inclusive
  222. */
  223. int do_sync_mapping_range(struct address_space *mapping, loff_t offset,
  224. loff_t endbyte, unsigned int flags)
  225. {
  226. int ret;
  227. if (!mapping) {
  228. ret = -EINVAL;
  229. goto out;
  230. }
  231. ret = 0;
  232. if (flags & SYNC_FILE_RANGE_WAIT_BEFORE) {
  233. ret = wait_on_page_writeback_range(mapping,
  234. offset >> PAGE_CACHE_SHIFT,
  235. endbyte >> PAGE_CACHE_SHIFT);
  236. if (ret < 0)
  237. goto out;
  238. }
  239. if (flags & SYNC_FILE_RANGE_WRITE) {
  240. ret = __filemap_fdatawrite_range(mapping, offset, endbyte,
  241. WB_SYNC_NONE);
  242. if (ret < 0)
  243. goto out;
  244. }
  245. if (flags & SYNC_FILE_RANGE_WAIT_AFTER) {
  246. ret = wait_on_page_writeback_range(mapping,
  247. offset >> PAGE_CACHE_SHIFT,
  248. endbyte >> PAGE_CACHE_SHIFT);
  249. }
  250. out:
  251. return ret;
  252. }
  253. EXPORT_SYMBOL_GPL(do_sync_mapping_range);