aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Kara <jack@suse.cz>2009-08-17 13:52:36 -0400
committerJan Kara <jack@suse.cz>2009-09-14 11:08:15 -0400
commit148f948ba877f4d3cdef036b1ff6d9f68986706a (patch)
treec07963f08bf8c2119ec00df64e4293e2e60acaa1
parenteef99380679e20e7edc096aa4d8a98b875404d79 (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>
-rw-r--r--fs/splice.c22
-rw-r--r--fs/sync.c55
-rw-r--r--include/linux/fs.h3
-rw-r--r--mm/filemap.c11
4 files changed, 61 insertions, 30 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
diff --git a/fs/sync.c b/fs/sync.c
index 103cc7fdd3df..4e15da01923c 100644
--- a/fs/sync.c
+++ b/fs/sync.c
@@ -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 */
193int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) 196int 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;
231out: 236out:
232 return ret; 237 return ret;
233} 238}
239EXPORT_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 */
254int vfs_fsync(struct file *file, struct dentry *dentry, int datasync)
255{
256 return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync);
257}
234EXPORT_SYMBOL(vfs_fsync); 258EXPORT_SYMBOL(vfs_fsync);
235 259
236static int do_fsync(unsigned int fd, int datasync) 260static 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 */
291int 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}
298EXPORT_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
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 6c1be3a4edea..e2c7f5167662 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2098,7 +2098,10 @@ extern int __filemap_fdatawrite_range(struct address_space *mapping,
2098extern int filemap_fdatawrite_range(struct address_space *mapping, 2098extern int filemap_fdatawrite_range(struct address_space *mapping,
2099 loff_t start, loff_t end); 2099 loff_t start, loff_t end);
2100 2100
2101extern int vfs_fsync_range(struct file *file, struct dentry *dentry,
2102 loff_t start, loff_t end, int datasync);
2101extern int vfs_fsync(struct file *file, struct dentry *dentry, int datasync); 2103extern int vfs_fsync(struct file *file, struct dentry *dentry, int datasync);
2104extern int generic_write_sync(struct file *file, loff_t pos, loff_t count);
2102extern void sync_supers(void); 2105extern void sync_supers(void);
2103extern void emergency_sync(void); 2106extern void emergency_sync(void);
2104extern void emergency_remount(void); 2107extern void emergency_remount(void);
diff --git a/mm/filemap.c b/mm/filemap.c
index 3587554f45ef..849293c4f418 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -39,11 +39,10 @@
39/* 39/*
40 * FIXME: remove all knowledge of the buffer layer from the core VM 40 * FIXME: remove all knowledge of the buffer layer from the core VM
41 */ 41 */
42#include <linux/buffer_head.h> /* for generic_osync_inode */ 42#include <linux/buffer_head.h> /* for try_to_free_buffers */
43 43
44#include <asm/mman.h> 44#include <asm/mman.h>
45 45
46
47/* 46/*
48 * Shared mappings implemented 30.11.1994. It's not fully working yet, 47 * Shared mappings implemented 30.11.1994. It's not fully working yet,
49 * though. 48 * though.
@@ -2477,8 +2476,7 @@ ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2477 unsigned long nr_segs, loff_t pos) 2476 unsigned long nr_segs, loff_t pos)
2478{ 2477{
2479 struct file *file = iocb->ki_filp; 2478 struct file *file = iocb->ki_filp;
2480 struct address_space *mapping = file->f_mapping; 2479 struct inode *inode = file->f_mapping->host;
2481 struct inode *inode = mapping->host;
2482 ssize_t ret; 2480 ssize_t ret;
2483 2481
2484 BUG_ON(iocb->ki_pos != pos); 2482 BUG_ON(iocb->ki_pos != pos);
@@ -2487,11 +2485,10 @@ ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
2487 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); 2485 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
2488 mutex_unlock(&inode->i_mutex); 2486 mutex_unlock(&inode->i_mutex);
2489 2487
2490 if ((ret > 0 || ret == -EIOCBQUEUED) && 2488 if (ret > 0 || ret == -EIOCBQUEUED) {
2491 ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
2492 ssize_t err; 2489 ssize_t err;
2493 2490
2494 err = sync_page_range(inode, mapping, pos, ret); 2491 err = generic_write_sync(file, pos, ret);
2495 if (err < 0 && ret > 0) 2492 if (err < 0 && ret > 0)
2496 ret = err; 2493 ret = err;
2497 } 2494 }