Commit e257039f authored by Al Viro's avatar Al Viro

mount_setattr(): clean the control flow and calling conventions

separate the "cleanup" and "apply" codepaths (they have almost no overlap),
fold the "cleanup" into "prepare" (which eliminates the need of ->revert)
and make loops more idiomatic.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 87bb5b60
...@@ -82,7 +82,6 @@ struct mount_kattr { ...@@ -82,7 +82,6 @@ struct mount_kattr {
unsigned int lookup_flags; unsigned int lookup_flags;
bool recurse; bool recurse;
struct user_namespace *mnt_userns; struct user_namespace *mnt_userns;
struct mount *revert;
}; };
/* /sys/fs */ /* /sys/fs */
...@@ -4014,32 +4013,39 @@ static inline bool mnt_allow_writers(const struct mount_kattr *kattr, ...@@ -4014,32 +4013,39 @@ static inline bool mnt_allow_writers(const struct mount_kattr *kattr,
static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt) static int mount_setattr_prepare(struct mount_kattr *kattr, struct mount *mnt)
{ {
struct mount *m = mnt; struct mount *m;
int err;
do {
int err = -EPERM;
unsigned int flags;
kattr->revert = m;
flags = recalc_flags(kattr, m); for (m = mnt; m; m = next_mnt(m, mnt)) {
if (!can_change_locked_flags(m, flags)) if (!can_change_locked_flags(m, recalc_flags(kattr, m))) {
return err; err = -EPERM;
break;
}
err = can_idmap_mount(kattr, m); err = can_idmap_mount(kattr, m);
if (err) if (err)
return err; break;
if (mnt_allow_writers(kattr, m))
continue;
if (!mnt_allow_writers(kattr, m)) {
err = mnt_hold_writers(m); err = mnt_hold_writers(m);
if (err) if (err)
return err; break;
} while (kattr->recurse && (m = next_mnt(m, mnt))); }
kattr->revert = NULL; if (!kattr->recurse)
return 0; return 0;
}
if (err) {
struct mount *p;
for (p = mnt; p != m; p = next_mnt(p, mnt)) {
/* If we had to hold writers unblock them. */
if (p->mnt.mnt_flags & MNT_WRITE_HOLD)
mnt_unhold_writers(p);
}
}
return err;
} }
static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
...@@ -4067,35 +4073,26 @@ static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt) ...@@ -4067,35 +4073,26 @@ static void do_idmap_mount(const struct mount_kattr *kattr, struct mount *mnt)
put_user_ns(old_mnt_userns); put_user_ns(old_mnt_userns);
} }
static void mount_setattr_finish(struct mount_kattr *kattr, struct mount *mnt) static void mount_setattr_commit(struct mount_kattr *kattr, struct mount *mnt)
{ {
struct mount *m = mnt; struct mount *m;
do { for (m = mnt; m; m = next_mnt(m, mnt)) {
if (!kattr->revert) {
unsigned int flags; unsigned int flags;
do_idmap_mount(kattr, m); do_idmap_mount(kattr, m);
flags = recalc_flags(kattr, m); flags = recalc_flags(kattr, m);
WRITE_ONCE(m->mnt.mnt_flags, flags); WRITE_ONCE(m->mnt.mnt_flags, flags);
}
/* If we had to hold writers unblock them. */ /* If we had to hold writers unblock them. */
if (m->mnt.mnt_flags & MNT_WRITE_HOLD) if (m->mnt.mnt_flags & MNT_WRITE_HOLD)
mnt_unhold_writers(m); mnt_unhold_writers(m);
if (!kattr->revert && kattr->propagation) if (kattr->propagation)
change_mnt_propagation(m, kattr->propagation); change_mnt_propagation(m, kattr->propagation);
if (!kattr->recurse)
/* break;
* On failure, only cleanup until we found the first mount }
* we failed to handle.
*/
if (kattr->revert == m)
return;
} while (kattr->recurse && (m = next_mnt(m, mnt)));
if (!kattr->revert)
touch_mnt_namespace(mnt->mnt_ns); touch_mnt_namespace(mnt->mnt_ns);
} }
...@@ -4144,7 +4141,8 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr) ...@@ -4144,7 +4141,8 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
* changes and if we failed we clean up. * changes and if we failed we clean up.
*/ */
err = mount_setattr_prepare(kattr, mnt); err = mount_setattr_prepare(kattr, mnt);
mount_setattr_finish(kattr, mnt); if (!err)
mount_setattr_commit(kattr, mnt);
out: out:
unlock_mount_hash(); unlock_mount_hash();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment