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