diff options
author | Jan Kara <jack@suse.cz> | 2009-08-17 13:52:36 -0400 |
---|---|---|
committer | Jan Kara <jack@suse.cz> | 2009-09-14 11:08:15 -0400 |
commit | 148f948ba877f4d3cdef036b1ff6d9f68986706a (patch) | |
tree | c07963f08bf8c2119ec00df64e4293e2e60acaa1 /fs | |
parent | eef99380679e20e7edc096aa4d8a98b875404d79 (diff) |
vfs: Introduce new helpers for syncing after writing to O_SYNC file or IS_SYNC inode
Introduce new function for generic inode syncing (vfs_fsync_range) and use
it from fsync() path. Introduce also new helper for syncing after a sync
write (generic_write_sync) using the generic function.
Use these new helpers for syncing from generic VFS functions. This makes
O_SYNC writes to block devices acquire i_mutex for syncing. If we really
care about this, we can make block_fsync() drop the i_mutex and reacquire
it before it returns.
CC: Evgeniy Polyakov <zbr@ioremap.net>
CC: ocfs2-devel@oss.oracle.com
CC: Joel Becker <joel.becker@oracle.com>
CC: Felix Blyakher <felixb@sgi.com>
CC: xfs@oss.sgi.com
CC: Anton Altaparmakov <aia21@cantab.net>
CC: linux-ntfs-dev@lists.sourceforge.net
CC: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
CC: linux-ext4@vger.kernel.org
CC: tytso@mit.edu
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jan Kara <jack@suse.cz>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/splice.c | 22 | ||||
-rw-r--r-- | fs/sync.c | 55 |
2 files changed, 54 insertions, 23 deletions
diff --git a/fs/splice.c b/fs/splice.c index 73766d24f97b..819023733f8e 100644 --- a/fs/splice.c +++ b/fs/splice.c | |||
@@ -976,25 +976,15 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, | |||
976 | 976 | ||
977 | if (ret > 0) { | 977 | if (ret > 0) { |
978 | unsigned long nr_pages; | 978 | unsigned long nr_pages; |
979 | int err; | ||
979 | 980 | ||
980 | *ppos += ret; | ||
981 | nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | 981 | nr_pages = (ret + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
982 | 982 | ||
983 | /* | 983 | err = generic_write_sync(out, *ppos, ret); |
984 | * If file or inode is SYNC and we actually wrote some data, | 984 | if (err) |
985 | * sync it. | 985 | ret = err; |
986 | */ | 986 | else |
987 | if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { | 987 | *ppos += ret; |
988 | int err; | ||
989 | |||
990 | mutex_lock(&inode->i_mutex); | ||
991 | err = generic_osync_inode(inode, mapping, | ||
992 | OSYNC_METADATA|OSYNC_DATA); | ||
993 | mutex_unlock(&inode->i_mutex); | ||
994 | |||
995 | if (err) | ||
996 | ret = err; | ||
997 | } | ||
998 | balance_dirty_pages_ratelimited_nr(mapping, nr_pages); | 988 | balance_dirty_pages_ratelimited_nr(mapping, nr_pages); |
999 | } | 989 | } |
1000 | 990 | ||
@@ -178,19 +178,23 @@ int file_fsync(struct file *filp, struct dentry *dentry, int datasync) | |||
178 | } | 178 | } |
179 | 179 | ||
180 | /** | 180 | /** |
181 | * vfs_fsync - perform a fsync or fdatasync on a file | 181 | * vfs_fsync_range - helper to sync a range of data & metadata to disk |
182 | * @file: file to sync | 182 | * @file: file to sync |
183 | * @dentry: dentry of @file | 183 | * @dentry: dentry of @file |
184 | * @data: only perform a fdatasync operation | 184 | * @start: offset in bytes of the beginning of data range to sync |
185 | * @end: offset in bytes of the end of data range (inclusive) | ||
186 | * @datasync: perform only datasync | ||
185 | * | 187 | * |
186 | * Write back data and metadata for @file to disk. If @datasync is | 188 | * Write back data in range @start..@end and metadata for @file to disk. If |
187 | * set only metadata needed to access modified file data is written. | 189 | * @datasync is set only metadata needed to access modified file data is |
190 | * written. | ||
188 | * | 191 | * |
189 | * In case this function is called from nfsd @file may be %NULL and | 192 | * In case this function is called from nfsd @file may be %NULL and |
190 | * only @dentry is set. This can only happen when the filesystem | 193 | * only @dentry is set. This can only happen when the filesystem |
191 | * implements the export_operations API. | 194 | * implements the export_operations API. |
192 | */ | 195 | */ |
193 | int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) | 196 | int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start, |
197 | loff_t end, int datasync) | ||
194 | { | 198 | { |
195 | const struct file_operations *fop; | 199 | const struct file_operations *fop; |
196 | struct address_space *mapping; | 200 | struct address_space *mapping; |
@@ -214,7 +218,7 @@ int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) | |||
214 | goto out; | 218 | goto out; |
215 | } | 219 | } |
216 | 220 | ||
217 | ret = filemap_fdatawrite(mapping); | 221 | ret = filemap_fdatawrite_range(mapping, start, end); |
218 | 222 | ||
219 | /* | 223 | /* |
220 | * We need to protect against concurrent writers, which could cause | 224 | * We need to protect against concurrent writers, which could cause |
@@ -225,12 +229,32 @@ int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) | |||
225 | if (!ret) | 229 | if (!ret) |
226 | ret = err; | 230 | ret = err; |
227 | mutex_unlock(&mapping->host->i_mutex); | 231 | mutex_unlock(&mapping->host->i_mutex); |
228 | err = filemap_fdatawait(mapping); | 232 | |
233 | err = filemap_fdatawait_range(mapping, start, end); | ||
229 | if (!ret) | 234 | if (!ret) |
230 | ret = err; | 235 | ret = err; |
231 | out: | 236 | out: |
232 | return ret; | 237 | return ret; |
233 | } | 238 | } |
239 | EXPORT_SYMBOL(vfs_fsync_range); | ||
240 | |||
241 | /** | ||
242 | * vfs_fsync - perform a fsync or fdatasync on a file | ||
243 | * @file: file to sync | ||
244 | * @dentry: dentry of @file | ||
245 | * @datasync: only perform a fdatasync operation | ||
246 | * | ||
247 | * Write back data and metadata for @file to disk. If @datasync is | ||
248 | * set only metadata needed to access modified file data is written. | ||
249 | * | ||
250 | * In case this function is called from nfsd @file may be %NULL and | ||
251 | * only @dentry is set. This can only happen when the filesystem | ||
252 | * implements the export_operations API. | ||
253 | */ | ||
254 | int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) | ||
255 | { | ||
256 | return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync); | ||
257 | } | ||
234 | EXPORT_SYMBOL(vfs_fsync); | 258 | EXPORT_SYMBOL(vfs_fsync); |
235 | 259 | ||
236 | static int do_fsync(unsigned int fd, int datasync) | 260 | static int do_fsync(unsigned int fd, int datasync) |
@@ -256,6 +280,23 @@ SYSCALL_DEFINE1(fdatasync, unsigned int, fd) | |||
256 | return do_fsync(fd, 1); | 280 | return do_fsync(fd, 1); |
257 | } | 281 | } |
258 | 282 | ||
283 | /** | ||
284 | * generic_write_sync - perform syncing after a write if file / inode is sync | ||
285 | * @file: file to which the write happened | ||
286 | * @pos: offset where the write started | ||
287 | * @count: length of the write | ||
288 | * | ||
289 | * This is just a simple wrapper about our general syncing function. | ||
290 | */ | ||
291 | int generic_write_sync(struct file *file, loff_t pos, loff_t count) | ||
292 | { | ||
293 | if (!(file->f_flags & O_SYNC) && !IS_SYNC(file->f_mapping->host)) | ||
294 | return 0; | ||
295 | return vfs_fsync_range(file, file->f_path.dentry, pos, | ||
296 | pos + count - 1, 1); | ||
297 | } | ||
298 | EXPORT_SYMBOL(generic_write_sync); | ||
299 | |||
259 | /* | 300 | /* |
260 | * sys_sync_file_range() permits finely controlled syncing over a segment of | 301 | * sys_sync_file_range() permits finely controlled syncing over a segment of |
261 | * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is | 302 | * a file in the range offset .. (offset+nbytes-1) inclusive. If nbytes is |