sys_sync简介

在我们RTFSC的时候,我们经常会看到一些关于sys_sync的地方,这是一种系统同步的机制。这个函数实际上是一个系统调用的过程。具体的代码在sync.c文件中:

/*
 * sync everything.  Start out by waking pdflush, because that writes back
 * all queues in parallel.
 */
SYSCALL_DEFINE0(sync)
{
wakeup_flusher_threads(0);
sync_filesystems(0);
sync_filesystems(1);
if (unlikely(laptop_mode))
laptop_sync_completion();
return 0;
}

其实在这个系统调用中,主要的工作就是sync_filesystem,为什么这个地方会调用两次?我们接着往下看:

/*
 * Sync all the data for all the filesystems (called by sys_sync() and
 * emergency sync)
 */
static void sync_filesystems(int wait)
{
iterate_supers(sync_one_sb, &wait);
}

主要的工作是sync_one_sb中,

static void sync_one_sb(struct super_block *sb, void *arg)
{
if (!(sb->s_flags & MS_RDONLY))
__sync_filesystem(sb, *(int *)arg);
}

但是这个函数真正完成的任务也不是在每次的iterate_supers函数中都要执行到,从代码中我们能够看到需要满足的条件是怎样的。

/*
 * Do the filesystem syncing work. For simple filesystems
 * writeback_inodes_sb(sb) just dirties buffers with inodes so we have to
 * submit IO for these buffers via __sync_blockdev(). This also speeds up the
 * wait == 1 case since in that case write_inode() functions do
 * sync_dirty_buffer() and thus effectively write one block at a time.
 */
static int __sync_filesystem(struct super_block *sb, int wait)
{
/*
* This should be safe, as we require bdi backing to actually
* write out data in the first place
*/
if (sb->s_bdi == &noop_backing_dev_info)
return 0;


if (sb->s_qcop && sb->s_qcop->quota_sync)
sb->s_qcop->quota_sync(sb, -1, wait);


if (wait)
sync_inodes_sb(sb);
else
writeback_inodes_sb(sb);


if (sb->s_op->sync_fs)
sb->s_op->sync_fs(sb, wait);
return __sync_blockdev(sb->s_bdev, wait);
}

这个函数完成的任务的主题就是if-else语句,也是前面在sync_filesystem中为什么传递0和1这两个参数的原因所在。

你可能感兴趣的:(sys_sync简介)