aboutsummaryrefslogtreecommitdiffstats
path: root/fs/sync.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/sync.c')
-rw-r--r--fs/sync.c94
1 files changed, 17 insertions, 77 deletions
diff --git a/fs/sync.c b/fs/sync.c
index 92b228176f7c..c9f83f480ec5 100644
--- a/fs/sync.c
+++ b/fs/sync.c
@@ -42,7 +42,7 @@ static int __sync_filesystem(struct super_block *sb, int wait)
42 if (wait) 42 if (wait)
43 sync_inodes_sb(sb); 43 sync_inodes_sb(sb);
44 else 44 else
45 writeback_inodes_sb(sb); 45 writeback_inodes_sb_locked(sb);
46 46
47 if (sb->s_op->sync_fs) 47 if (sb->s_op->sync_fs)
48 sb->s_op->sync_fs(sb, wait); 48 sb->s_op->sync_fs(sb, wait);
@@ -77,50 +77,18 @@ int sync_filesystem(struct super_block *sb)
77} 77}
78EXPORT_SYMBOL_GPL(sync_filesystem); 78EXPORT_SYMBOL_GPL(sync_filesystem);
79 79
80static void sync_one_sb(struct super_block *sb, void *arg)
81{
82 if (!(sb->s_flags & MS_RDONLY) && sb->s_bdi)
83 __sync_filesystem(sb, *(int *)arg);
84}
80/* 85/*
81 * Sync all the data for all the filesystems (called by sys_sync() and 86 * Sync all the data for all the filesystems (called by sys_sync() and
82 * emergency sync) 87 * emergency sync)
83 *
84 * This operation is careful to avoid the livelock which could easily happen
85 * if two or more filesystems are being continuously dirtied. s_need_sync
86 * is used only here. We set it against all filesystems and then clear it as
87 * we sync them. So redirtied filesystems are skipped.
88 *
89 * But if process A is currently running sync_filesystems and then process B
90 * calls sync_filesystems as well, process B will set all the s_need_sync
91 * flags again, which will cause process A to resync everything. Fix that with
92 * a local mutex.
93 */ 88 */
94static void sync_filesystems(int wait) 89static void sync_filesystems(int wait)
95{ 90{
96 struct super_block *sb; 91 iterate_supers(sync_one_sb, &wait);
97 static DEFINE_MUTEX(mutex);
98
99 mutex_lock(&mutex); /* Could be down_interruptible */
100 spin_lock(&sb_lock);
101 list_for_each_entry(sb, &super_blocks, s_list)
102 sb->s_need_sync = 1;
103
104restart:
105 list_for_each_entry(sb, &super_blocks, s_list) {
106 if (!sb->s_need_sync)
107 continue;
108 sb->s_need_sync = 0;
109 sb->s_count++;
110 spin_unlock(&sb_lock);
111
112 down_read(&sb->s_umount);
113 if (!(sb->s_flags & MS_RDONLY) && sb->s_root && sb->s_bdi)
114 __sync_filesystem(sb, wait);
115 up_read(&sb->s_umount);
116
117 /* restart only when sb is no longer on the list */
118 spin_lock(&sb_lock);
119 if (__put_super_and_need_restart(sb))
120 goto restart;
121 }
122 spin_unlock(&sb_lock);
123 mutex_unlock(&mutex);
124} 92}
125 93
126/* 94/*
@@ -162,12 +130,10 @@ void emergency_sync(void)
162 130
163/* 131/*
164 * Generic function to fsync a file. 132 * Generic function to fsync a file.
165 *
166 * filp may be NULL if called via the msync of a vma.
167 */ 133 */
168int file_fsync(struct file *filp, struct dentry *dentry, int datasync) 134int file_fsync(struct file *filp, int datasync)
169{ 135{
170 struct inode * inode = dentry->d_inode; 136 struct inode *inode = filp->f_mapping->host;
171 struct super_block * sb; 137 struct super_block * sb;
172 int ret, err; 138 int ret, err;
173 139
@@ -190,7 +156,6 @@ EXPORT_SYMBOL(file_fsync);
190/** 156/**
191 * vfs_fsync_range - helper to sync a range of data & metadata to disk 157 * vfs_fsync_range - helper to sync a range of data & metadata to disk
192 * @file: file to sync 158 * @file: file to sync
193 * @dentry: dentry of @file
194 * @start: offset in bytes of the beginning of data range to sync 159 * @start: offset in bytes of the beginning of data range to sync
195 * @end: offset in bytes of the end of data range (inclusive) 160 * @end: offset in bytes of the end of data range (inclusive)
196 * @datasync: perform only datasync 161 * @datasync: perform only datasync
@@ -198,32 +163,13 @@ EXPORT_SYMBOL(file_fsync);
198 * Write back data in range @start..@end and metadata for @file to disk. If 163 * Write back data in range @start..@end and metadata for @file to disk. If
199 * @datasync is set only metadata needed to access modified file data is 164 * @datasync is set only metadata needed to access modified file data is
200 * written. 165 * written.
201 *
202 * In case this function is called from nfsd @file may be %NULL and
203 * only @dentry is set. This can only happen when the filesystem
204 * implements the export_operations API.
205 */ 166 */
206int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start, 167int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
207 loff_t end, int datasync)
208{ 168{
209 const struct file_operations *fop; 169 struct address_space *mapping = file->f_mapping;
210 struct address_space *mapping;
211 int err, ret; 170 int err, ret;
212 171
213 /* 172 if (!file->f_op || !file->f_op->fsync) {
214 * Get mapping and operations from the file in case we have
215 * as file, or get the default values for them in case we
216 * don't have a struct file available. Damn nfsd..
217 */
218 if (file) {
219 mapping = file->f_mapping;
220 fop = file->f_op;
221 } else {
222 mapping = dentry->d_inode->i_mapping;
223 fop = dentry->d_inode->i_fop;
224 }
225
226 if (!fop || !fop->fsync) {
227 ret = -EINVAL; 173 ret = -EINVAL;
228 goto out; 174 goto out;
229 } 175 }
@@ -235,7 +181,7 @@ int vfs_fsync_range(struct file *file, struct dentry *dentry, loff_t start,
235 * livelocks in fsync_buffers_list(). 181 * livelocks in fsync_buffers_list().
236 */ 182 */
237 mutex_lock(&mapping->host->i_mutex); 183 mutex_lock(&mapping->host->i_mutex);
238 err = fop->fsync(file, dentry, datasync); 184 err = file->f_op->fsync(file, datasync);
239 if (!ret) 185 if (!ret)
240 ret = err; 186 ret = err;
241 mutex_unlock(&mapping->host->i_mutex); 187 mutex_unlock(&mapping->host->i_mutex);
@@ -248,19 +194,14 @@ EXPORT_SYMBOL(vfs_fsync_range);
248/** 194/**
249 * vfs_fsync - perform a fsync or fdatasync on a file 195 * vfs_fsync - perform a fsync or fdatasync on a file
250 * @file: file to sync 196 * @file: file to sync
251 * @dentry: dentry of @file
252 * @datasync: only perform a fdatasync operation 197 * @datasync: only perform a fdatasync operation
253 * 198 *
254 * Write back data and metadata for @file to disk. If @datasync is 199 * Write back data and metadata for @file to disk. If @datasync is
255 * set only metadata needed to access modified file data is written. 200 * set only metadata needed to access modified file data is written.
256 *
257 * In case this function is called from nfsd @file may be %NULL and
258 * only @dentry is set. This can only happen when the filesystem
259 * implements the export_operations API.
260 */ 201 */
261int vfs_fsync(struct file *file, struct dentry *dentry, int datasync) 202int vfs_fsync(struct file *file, int datasync)
262{ 203{
263 return vfs_fsync_range(file, dentry, 0, LLONG_MAX, datasync); 204 return vfs_fsync_range(file, 0, LLONG_MAX, datasync);
264} 205}
265EXPORT_SYMBOL(vfs_fsync); 206EXPORT_SYMBOL(vfs_fsync);
266 207
@@ -271,7 +212,7 @@ static int do_fsync(unsigned int fd, int datasync)
271 212
272 file = fget(fd); 213 file = fget(fd);
273 if (file) { 214 if (file) {
274 ret = vfs_fsync(file, file->f_path.dentry, datasync); 215 ret = vfs_fsync(file, datasync);
275 fput(file); 216 fput(file);
276 } 217 }
277 return ret; 218 return ret;
@@ -299,8 +240,7 @@ int generic_write_sync(struct file *file, loff_t pos, loff_t count)
299{ 240{
300 if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host)) 241 if (!(file->f_flags & O_DSYNC) && !IS_SYNC(file->f_mapping->host))
301 return 0; 242 return 0;
302 return vfs_fsync_range(file, file->f_path.dentry, pos, 243 return vfs_fsync_range(file, pos, pos + count - 1,
303 pos + count - 1,
304 (file->f_flags & __O_SYNC) ? 0 : 1); 244 (file->f_flags & __O_SYNC) ? 0 : 1);
305} 245}
306EXPORT_SYMBOL(generic_write_sync); 246EXPORT_SYMBOL(generic_write_sync);