Skip to content

Commit 5cee581

Browse files
jankaraAl Viro
authored andcommitted
vfs: Make sys_sync() use fsync_super() (version 4)
It is unnecessarily fragile to have two places (fsync_super() and do_sync()) doing data integrity sync of the filesystem. Alter __fsync_super() to accommodate needs of both callers and use it. So after this patch __fsync_super() is the only place where we gather all the calls needed to properly send all data on a filesystem to disk. Nice bonus is that we get a complete livelock avoidance and write_supers() is now only used for periodic writeback of superblocks. sync_blockdevs() introduced a couple of patches ago is gone now. [build fixes folded] Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 429479f commit 5cee581

7 files changed

Lines changed: 51 additions & 135 deletions

File tree

‎fs/block_dev.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,22 @@ blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
176176
iov, offset, nr_segs, blkdev_get_blocks, NULL);
177177
}
178178

179+
int __sync_blockdev(struct block_device *bdev, int wait)
180+
{
181+
if (!bdev)
182+
return 0;
183+
if (!wait)
184+
return filemap_flush(bdev->bd_inode->i_mapping);
185+
return filemap_write_and_wait(bdev->bd_inode->i_mapping);
186+
}
187+
179188
/*
180189
* Write out and wait upon all the dirty data associated with a block
181190
* device via its mapping. Does not take the superblock lock.
182191
*/
183192
int sync_blockdev(struct block_device *bdev)
184193
{
185-
int ret = 0;
186-
187-
if (bdev)
188-
ret = filemap_write_and_wait(bdev->bd_inode->i_mapping);
189-
return ret;
194+
return __sync_blockdev(bdev, 1);
190195
}
191196
EXPORT_SYMBOL(sync_blockdev);
192197

‎fs/fs-writeback.c‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -678,55 +678,6 @@ void sync_inodes_sb(struct super_block *sb, int wait)
678678
sync_sb_inodes(sb, &wbc);
679679
}
680680

681-
/**
682-
* sync_inodes - writes all inodes to disk
683-
* @wait: wait for completion
684-
*
685-
* sync_inodes() goes through each super block's dirty inode list, writes the
686-
* inodes out, waits on the writeout and puts the inodes back on the normal
687-
* list.
688-
*
689-
* This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle
690-
* part of the sync functions is that the blockdev "superblock" is processed
691-
* last. This is because the write_inode() function of a typical fs will
692-
* perform no I/O, but will mark buffers in the blockdev mapping as dirty.
693-
* What we want to do is to perform all that dirtying first, and then write
694-
* back all those inode blocks via the blockdev mapping in one sweep. So the
695-
* additional (somewhat redundant) sync_blockdev() calls here are to make
696-
* sure that really happens. Because if we call sync_inodes_sb(wait=1) with
697-
* outstanding dirty inodes, the writeback goes block-at-a-time within the
698-
* filesystem's write_inode(). This is extremely slow.
699-
*/
700-
static void __sync_inodes(int wait)
701-
{
702-
struct super_block *sb;
703-
704-
spin_lock(&sb_lock);
705-
restart:
706-
list_for_each_entry(sb, &super_blocks, s_list) {
707-
sb->s_count++;
708-
spin_unlock(&sb_lock);
709-
down_read(&sb->s_umount);
710-
if (sb->s_root) {
711-
sync_inodes_sb(sb, wait);
712-
sync_blockdev(sb->s_bdev);
713-
}
714-
up_read(&sb->s_umount);
715-
spin_lock(&sb_lock);
716-
if (__put_super_and_need_restart(sb))
717-
goto restart;
718-
}
719-
spin_unlock(&sb_lock);
720-
}
721-
722-
void sync_inodes(int wait)
723-
{
724-
__sync_inodes(0);
725-
726-
if (wait)
727-
__sync_inodes(1);
728-
}
729-
730681
/**
731682
* write_inode_now - write an inode to disk
732683
* @inode: inode to write to disk

‎fs/internal.h‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ static inline int sb_is_blkdev_sb(struct super_block *sb)
2525
return sb == blockdev_superblock;
2626
}
2727

28+
extern int __sync_blockdev(struct block_device *bdev, int wait);
29+
2830
#else
2931
static inline void bdev_cache_init(void)
3032
{
@@ -34,6 +36,11 @@ static inline int sb_is_blkdev_sb(struct super_block *sb)
3436
{
3537
return 0;
3638
}
39+
40+
static inline int __sync_blockdev(struct block_device *bdev, int wait)
41+
{
42+
return 0;
43+
}
3744
#endif
3845

3946
/*
@@ -71,12 +78,3 @@ extern void chroot_fs_refs(struct path *, struct path *);
7178
* file_table.c
7279
*/
7380
extern void mark_files_ro(struct super_block *);
74-
75-
/*
76-
* super.c
77-
*/
78-
#ifdef CONFIG_BLOCK
79-
extern void sync_blockdevs(void);
80-
#else
81-
static inline void sync_blockdevs(void) { }
82-
#endif

‎fs/super.c‎

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -284,23 +284,23 @@ EXPORT_SYMBOL(lock_super);
284284
EXPORT_SYMBOL(unlock_super);
285285

286286
/*
287-
* Write out and wait upon all dirty data associated with this
288-
* superblock. Filesystem data as well as the underlying block
289-
* device. Takes the superblock lock. Requires a second blkdev
290-
* flush by the caller to complete the operation.
287+
* Do the filesystem syncing work. For simple filesystems sync_inodes_sb(sb, 0)
288+
* just dirties buffers with inodes so we have to submit IO for these buffers
289+
* via __sync_blockdev(). This also speeds up the wait == 1 case since in that
290+
* case write_inode() functions do sync_dirty_buffer() and thus effectively
291+
* write one block at a time.
291292
*/
292-
static int __fsync_super(struct super_block *sb)
293+
static int __fsync_super(struct super_block *sb, int wait)
293294
{
294-
sync_inodes_sb(sb, 0);
295295
vfs_dq_sync(sb);
296-
sync_inodes_sb(sb, 1);
296+
sync_inodes_sb(sb, wait);
297297
lock_super(sb);
298298
if (sb->s_dirt && sb->s_op->write_super)
299299
sb->s_op->write_super(sb);
300300
unlock_super(sb);
301301
if (sb->s_op->sync_fs)
302-
sb->s_op->sync_fs(sb, 1);
303-
return sync_blockdev(sb->s_bdev);
302+
sb->s_op->sync_fs(sb, wait);
303+
return __sync_blockdev(sb->s_bdev, wait);
304304
}
305305

306306
/*
@@ -310,7 +310,12 @@ static int __fsync_super(struct super_block *sb)
310310
*/
311311
int fsync_super(struct super_block *sb)
312312
{
313-
return __fsync_super(sb);
313+
int ret;
314+
315+
ret = __fsync_super(sb, 0);
316+
if (ret < 0)
317+
return ret;
318+
return __fsync_super(sb, 1);
314319
}
315320
EXPORT_SYMBOL_GPL(fsync_super);
316321

@@ -469,20 +474,18 @@ void sync_supers(void)
469474
}
470475

471476
/*
472-
* Call the ->sync_fs super_op against all filesystems which are r/w and
473-
* which implement it.
477+
* Sync all the data for all the filesystems (called by sys_sync() and
478+
* emergency sync)
474479
*
475480
* This operation is careful to avoid the livelock which could easily happen
476-
* if two or more filesystems are being continuously dirtied. s_need_sync_fs
481+
* if two or more filesystems are being continuously dirtied. s_need_sync
477482
* is used only here. We set it against all filesystems and then clear it as
478483
* we sync them. So redirtied filesystems are skipped.
479484
*
480485
* But if process A is currently running sync_filesystems and then process B
481-
* calls sync_filesystems as well, process B will set all the s_need_sync_fs
486+
* calls sync_filesystems as well, process B will set all the s_need_sync
482487
* flags again, which will cause process A to resync everything. Fix that with
483488
* a local mutex.
484-
*
485-
* (Fabian) Avoid sync_fs with clean fs & wait mode 0
486489
*/
487490
void sync_filesystems(int wait)
488491
{
@@ -492,25 +495,23 @@ void sync_filesystems(int wait)
492495
mutex_lock(&mutex); /* Could be down_interruptible */
493496
spin_lock(&sb_lock);
494497
list_for_each_entry(sb, &super_blocks, s_list) {
495-
if (!sb->s_op->sync_fs)
496-
continue;
497498
if (sb->s_flags & MS_RDONLY)
498499
continue;
499-
sb->s_need_sync_fs = 1;
500+
sb->s_need_sync = 1;
500501
}
501502

502503
restart:
503504
list_for_each_entry(sb, &super_blocks, s_list) {
504-
if (!sb->s_need_sync_fs)
505+
if (!sb->s_need_sync)
505506
continue;
506-
sb->s_need_sync_fs = 0;
507+
sb->s_need_sync = 0;
507508
if (sb->s_flags & MS_RDONLY)
508509
continue; /* hm. Was remounted r/o meanwhile */
509510
sb->s_count++;
510511
spin_unlock(&sb_lock);
511512
down_read(&sb->s_umount);
512513
if (sb->s_root)
513-
sb->s_op->sync_fs(sb, wait);
514+
__fsync_super(sb, wait);
514515
up_read(&sb->s_umount);
515516
/* restart only when sb is no longer on the list */
516517
spin_lock(&sb_lock);
@@ -521,33 +522,6 @@ void sync_filesystems(int wait)
521522
mutex_unlock(&mutex);
522523
}
523524

524-
#ifdef CONFIG_BLOCK
525-
/*
526-
* Sync all block devices underlying some superblock
527-
*/
528-
void sync_blockdevs(void)
529-
{
530-
struct super_block *sb;
531-
532-
spin_lock(&sb_lock);
533-
restart:
534-
list_for_each_entry(sb, &super_blocks, s_list) {
535-
if (!sb->s_bdev)
536-
continue;
537-
sb->s_count++;
538-
spin_unlock(&sb_lock);
539-
down_read(&sb->s_umount);
540-
if (sb->s_root)
541-
sync_blockdev(sb->s_bdev);
542-
up_read(&sb->s_umount);
543-
spin_lock(&sb_lock);
544-
if (__put_super_and_need_restart(sb))
545-
goto restart;
546-
}
547-
spin_unlock(&sb_lock);
548-
}
549-
#endif
550-
551525
/**
552526
* get_super - get the superblock of a device
553527
* @bdev: device to get the superblock for

‎fs/sync.c‎

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,24 @@
1818
#define VALID_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE| \
1919
SYNC_FILE_RANGE_WAIT_AFTER)
2020

21-
/*
22-
* sync everything. Start out by waking pdflush, because that writes back
23-
* all queues in parallel.
24-
*/
25-
static void do_sync(unsigned long wait)
21+
SYSCALL_DEFINE0(sync)
2622
{
27-
wakeup_pdflush(0);
28-
sync_inodes(0); /* All mappings, inodes and their blockdevs */
29-
vfs_dq_sync(NULL);
30-
sync_inodes(wait); /* Mappings, inodes and blockdevs, again. */
31-
sync_supers(); /* Write the superblocks */
32-
sync_filesystems(0); /* Start syncing the filesystems */
33-
sync_filesystems(wait); /* Waitingly sync the filesystems */
34-
sync_blockdevs();
35-
if (!wait)
36-
printk("Emergency Sync complete\n");
23+
sync_filesystems(0);
24+
sync_filesystems(1);
3725
if (unlikely(laptop_mode))
3826
laptop_sync_completion();
39-
}
40-
41-
SYSCALL_DEFINE0(sync)
42-
{
43-
do_sync(1);
4427
return 0;
4528
}
4629

4730
static void do_sync_work(struct work_struct *work)
4831
{
49-
do_sync(0);
32+
/*
33+
* Sync twice to reduce the possibility we skipped some inodes / pages
34+
* because they were temporarily locked
35+
*/
36+
sync_filesystems(0);
37+
sync_filesystems(0);
38+
printk("Emergency Sync complete\n");
5039
kfree(work);
5140
}
5241

‎include/linux/fs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ struct super_block {
13211321
struct rw_semaphore s_umount;
13221322
struct mutex s_lock;
13231323
int s_count;
1324-
int s_need_sync_fs;
1324+
int s_need_sync;
13251325
atomic_t s_active;
13261326
#ifdef CONFIG_SECURITY
13271327
void *s_security;

‎include/linux/writeback.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ struct writeback_control {
7979
void writeback_inodes(struct writeback_control *wbc);
8080
int inode_wait(void *);
8181
void sync_inodes_sb(struct super_block *, int wait);
82-
void sync_inodes(int wait);
8382

8483
/* writeback.h requires fs.h; it, too, is not included from here. */
8584
static inline void wait_on_inode(struct inode *inode)

0 commit comments

Comments
 (0)