int mnt_want_write(struct vfsmount *m)获得一个mount点的写权限
其源码分析如下:
int mnt_want_write(struct vfsmount *m)
{
int ret;
#标志开始对super block开始写
sb_start_write(m->mnt_sb);
#检查mnt是否持有MNT_WRITE_HOLD 或者是否只读,如果可以获得mnt的写权限的话ret返回零
ret = __mnt_want_write(m);
#可以获得写权限的话ret为零,不会执行sb_end_write
if (ret)
sb_end_write(m->mnt_sb);
return ret;
}
__mnt_want_write的实现如下:
int __mnt_want_write(struct vfsmount *m)
{
#获得实际的mount点
struct mount *mnt = real_mount(m);
int ret = 0;
#禁止抢占
preempt_disable();
#增加mnt的引用计数
mnt_inc_writers(mnt);
/*
* The store to mnt_inc_writers must be visible before we pass
* MNT_WRITE_HOLD loop below, so that the slowpath can see our
* incremented count after it has set MNT_WRITE_HOLD.
*/
smp_mb();
#检查mnt是否持有MNT_WRITE_HOLD,如果持有的话,则通过cpu_relax 进行忙等待
while (READ_ONCE(mnt->mnt.mnt_flags) & MNT_WRITE_HOLD)
cpu_relax();
/*
* After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
* be set to match its requirements. So we must not load that until
* MNT_WRITE_HOLD is cleared.
*/
smp_rmb();
#检查mnt是否是只读的
if (mnt_is_readonly(m)) {
mnt_dec_writers(mnt);
ret = -EROFS;
}
#使能抢占
preempt_enable();
#如果mnt是可以写的,则返回0
return ret;
}
内核文件系统API之mnt_want_write
最新推荐文章于 2023-08-12 15:18:14 发布