Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/fs/pnode.c
0003  *
0004  * (C) Copyright IBM Corporation 2005.
0005  *  Released under GPL v2.
0006  *  Author : Ram Pai (linuxram@us.ibm.com)
0007  *
0008  */
0009 #include <linux/mnt_namespace.h>
0010 #include <linux/mount.h>
0011 #include <linux/fs.h>
0012 #include <linux/nsproxy.h>
0013 #include "internal.h"
0014 #include "pnode.h"
0015 
0016 /* return the next shared peer mount of @p */
0017 static inline struct mount *next_peer(struct mount *p)
0018 {
0019     return list_entry(p->mnt_share.next, struct mount, mnt_share);
0020 }
0021 
0022 static inline struct mount *first_slave(struct mount *p)
0023 {
0024     return list_entry(p->mnt_slave_list.next, struct mount, mnt_slave);
0025 }
0026 
0027 static inline struct mount *next_slave(struct mount *p)
0028 {
0029     return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
0030 }
0031 
0032 static struct mount *get_peer_under_root(struct mount *mnt,
0033                      struct mnt_namespace *ns,
0034                      const struct path *root)
0035 {
0036     struct mount *m = mnt;
0037 
0038     do {
0039         /* Check the namespace first for optimization */
0040         if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
0041             return m;
0042 
0043         m = next_peer(m);
0044     } while (m != mnt);
0045 
0046     return NULL;
0047 }
0048 
0049 /*
0050  * Get ID of closest dominating peer group having a representative
0051  * under the given root.
0052  *
0053  * Caller must hold namespace_sem
0054  */
0055 int get_dominating_id(struct mount *mnt, const struct path *root)
0056 {
0057     struct mount *m;
0058 
0059     for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
0060         struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
0061         if (d)
0062             return d->mnt_group_id;
0063     }
0064 
0065     return 0;
0066 }
0067 
0068 static int do_make_slave(struct mount *mnt)
0069 {
0070     struct mount *master, *slave_mnt;
0071 
0072     if (list_empty(&mnt->mnt_share)) {
0073         if (IS_MNT_SHARED(mnt)) {
0074             mnt_release_group_id(mnt);
0075             CLEAR_MNT_SHARED(mnt);
0076         }
0077         master = mnt->mnt_master;
0078         if (!master) {
0079             struct list_head *p = &mnt->mnt_slave_list;
0080             while (!list_empty(p)) {
0081                 slave_mnt = list_first_entry(p,
0082                         struct mount, mnt_slave);
0083                 list_del_init(&slave_mnt->mnt_slave);
0084                 slave_mnt->mnt_master = NULL;
0085             }
0086             return 0;
0087         }
0088     } else {
0089         struct mount *m;
0090         /*
0091          * slave 'mnt' to a peer mount that has the
0092          * same root dentry. If none is available then
0093          * slave it to anything that is available.
0094          */
0095         for (m = master = next_peer(mnt); m != mnt; m = next_peer(m)) {
0096             if (m->mnt.mnt_root == mnt->mnt.mnt_root) {
0097                 master = m;
0098                 break;
0099             }
0100         }
0101         list_del_init(&mnt->mnt_share);
0102         mnt->mnt_group_id = 0;
0103         CLEAR_MNT_SHARED(mnt);
0104     }
0105     list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
0106         slave_mnt->mnt_master = master;
0107     list_move(&mnt->mnt_slave, &master->mnt_slave_list);
0108     list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
0109     INIT_LIST_HEAD(&mnt->mnt_slave_list);
0110     mnt->mnt_master = master;
0111     return 0;
0112 }
0113 
0114 /*
0115  * vfsmount lock must be held for write
0116  */
0117 void change_mnt_propagation(struct mount *mnt, int type)
0118 {
0119     if (type == MS_SHARED) {
0120         set_mnt_shared(mnt);
0121         return;
0122     }
0123     do_make_slave(mnt);
0124     if (type != MS_SLAVE) {
0125         list_del_init(&mnt->mnt_slave);
0126         mnt->mnt_master = NULL;
0127         if (type == MS_UNBINDABLE)
0128             mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
0129         else
0130             mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
0131     }
0132 }
0133 
0134 /*
0135  * get the next mount in the propagation tree.
0136  * @m: the mount seen last
0137  * @origin: the original mount from where the tree walk initiated
0138  *
0139  * Note that peer groups form contiguous segments of slave lists.
0140  * We rely on that in get_source() to be able to find out if
0141  * vfsmount found while iterating with propagation_next() is
0142  * a peer of one we'd found earlier.
0143  */
0144 static struct mount *propagation_next(struct mount *m,
0145                      struct mount *origin)
0146 {
0147     /* are there any slaves of this mount? */
0148     if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
0149         return first_slave(m);
0150 
0151     while (1) {
0152         struct mount *master = m->mnt_master;
0153 
0154         if (master == origin->mnt_master) {
0155             struct mount *next = next_peer(m);
0156             return (next == origin) ? NULL : next;
0157         } else if (m->mnt_slave.next != &master->mnt_slave_list)
0158             return next_slave(m);
0159 
0160         /* back at master */
0161         m = master;
0162     }
0163 }
0164 
0165 static struct mount *next_group(struct mount *m, struct mount *origin)
0166 {
0167     while (1) {
0168         while (1) {
0169             struct mount *next;
0170             if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
0171                 return first_slave(m);
0172             next = next_peer(m);
0173             if (m->mnt_group_id == origin->mnt_group_id) {
0174                 if (next == origin)
0175                     return NULL;
0176             } else if (m->mnt_slave.next != &next->mnt_slave)
0177                 break;
0178             m = next;
0179         }
0180         /* m is the last peer */
0181         while (1) {
0182             struct mount *master = m->mnt_master;
0183             if (m->mnt_slave.next != &master->mnt_slave_list)
0184                 return next_slave(m);
0185             m = next_peer(master);
0186             if (master->mnt_group_id == origin->mnt_group_id)
0187                 break;
0188             if (master->mnt_slave.next == &m->mnt_slave)
0189                 break;
0190             m = master;
0191         }
0192         if (m == origin)
0193             return NULL;
0194     }
0195 }
0196 
0197 /* all accesses are serialized by namespace_sem */
0198 static struct user_namespace *user_ns;
0199 static struct mount *last_dest, *first_source, *last_source, *dest_master;
0200 static struct mountpoint *mp;
0201 static struct hlist_head *list;
0202 
0203 static inline bool peers(struct mount *m1, struct mount *m2)
0204 {
0205     return m1->mnt_group_id == m2->mnt_group_id && m1->mnt_group_id;
0206 }
0207 
0208 static int propagate_one(struct mount *m)
0209 {
0210     struct mount *child;
0211     int type;
0212     /* skip ones added by this propagate_mnt() */
0213     if (IS_MNT_NEW(m))
0214         return 0;
0215     /* skip if mountpoint isn't covered by it */
0216     if (!is_subdir(mp->m_dentry, m->mnt.mnt_root))
0217         return 0;
0218     if (peers(m, last_dest)) {
0219         type = CL_MAKE_SHARED;
0220     } else {
0221         struct mount *n, *p;
0222         bool done;
0223         for (n = m; ; n = p) {
0224             p = n->mnt_master;
0225             if (p == dest_master || IS_MNT_MARKED(p))
0226                 break;
0227         }
0228         do {
0229             struct mount *parent = last_source->mnt_parent;
0230             if (last_source == first_source)
0231                 break;
0232             done = parent->mnt_master == p;
0233             if (done && peers(n, parent))
0234                 break;
0235             last_source = last_source->mnt_master;
0236         } while (!done);
0237 
0238         type = CL_SLAVE;
0239         /* beginning of peer group among the slaves? */
0240         if (IS_MNT_SHARED(m))
0241             type |= CL_MAKE_SHARED;
0242     }
0243         
0244     /* Notice when we are propagating across user namespaces */
0245     if (m->mnt_ns->user_ns != user_ns)
0246         type |= CL_UNPRIVILEGED;
0247     child = copy_tree(last_source, last_source->mnt.mnt_root, type);
0248     if (IS_ERR(child))
0249         return PTR_ERR(child);
0250     child->mnt.mnt_flags &= ~MNT_LOCKED;
0251     mnt_set_mountpoint(m, mp, child);
0252     last_dest = m;
0253     last_source = child;
0254     if (m->mnt_master != dest_master) {
0255         read_seqlock_excl(&mount_lock);
0256         SET_MNT_MARK(m->mnt_master);
0257         read_sequnlock_excl(&mount_lock);
0258     }
0259     hlist_add_head(&child->mnt_hash, list);
0260     return count_mounts(m->mnt_ns, child);
0261 }
0262 
0263 /*
0264  * mount 'source_mnt' under the destination 'dest_mnt' at
0265  * dentry 'dest_dentry'. And propagate that mount to
0266  * all the peer and slave mounts of 'dest_mnt'.
0267  * Link all the new mounts into a propagation tree headed at
0268  * source_mnt. Also link all the new mounts using ->mnt_list
0269  * headed at source_mnt's ->mnt_list
0270  *
0271  * @dest_mnt: destination mount.
0272  * @dest_dentry: destination dentry.
0273  * @source_mnt: source mount.
0274  * @tree_list : list of heads of trees to be attached.
0275  */
0276 int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
0277             struct mount *source_mnt, struct hlist_head *tree_list)
0278 {
0279     struct mount *m, *n;
0280     int ret = 0;
0281 
0282     /*
0283      * we don't want to bother passing tons of arguments to
0284      * propagate_one(); everything is serialized by namespace_sem,
0285      * so globals will do just fine.
0286      */
0287     user_ns = current->nsproxy->mnt_ns->user_ns;
0288     last_dest = dest_mnt;
0289     first_source = source_mnt;
0290     last_source = source_mnt;
0291     mp = dest_mp;
0292     list = tree_list;
0293     dest_master = dest_mnt->mnt_master;
0294 
0295     /* all peers of dest_mnt, except dest_mnt itself */
0296     for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
0297         ret = propagate_one(n);
0298         if (ret)
0299             goto out;
0300     }
0301 
0302     /* all slave groups */
0303     for (m = next_group(dest_mnt, dest_mnt); m;
0304             m = next_group(m, dest_mnt)) {
0305         /* everything in that slave group */
0306         n = m;
0307         do {
0308             ret = propagate_one(n);
0309             if (ret)
0310                 goto out;
0311             n = next_peer(n);
0312         } while (n != m);
0313     }
0314 out:
0315     read_seqlock_excl(&mount_lock);
0316     hlist_for_each_entry(n, tree_list, mnt_hash) {
0317         m = n->mnt_parent;
0318         if (m->mnt_master != dest_mnt->mnt_master)
0319             CLEAR_MNT_MARK(m->mnt_master);
0320     }
0321     read_sequnlock_excl(&mount_lock);
0322     return ret;
0323 }
0324 
0325 /*
0326  * return true if the refcount is greater than count
0327  */
0328 static inline int do_refcount_check(struct mount *mnt, int count)
0329 {
0330     return mnt_get_count(mnt) > count;
0331 }
0332 
0333 /*
0334  * check if the mount 'mnt' can be unmounted successfully.
0335  * @mnt: the mount to be checked for unmount
0336  * NOTE: unmounting 'mnt' would naturally propagate to all
0337  * other mounts its parent propagates to.
0338  * Check if any of these mounts that **do not have submounts**
0339  * have more references than 'refcnt'. If so return busy.
0340  *
0341  * vfsmount lock must be held for write
0342  */
0343 int propagate_mount_busy(struct mount *mnt, int refcnt)
0344 {
0345     struct mount *m, *child;
0346     struct mount *parent = mnt->mnt_parent;
0347     int ret = 0;
0348 
0349     if (mnt == parent)
0350         return do_refcount_check(mnt, refcnt);
0351 
0352     /*
0353      * quickly check if the current mount can be unmounted.
0354      * If not, we don't have to go checking for all other
0355      * mounts
0356      */
0357     if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
0358         return 1;
0359 
0360     for (m = propagation_next(parent, parent); m;
0361                 m = propagation_next(m, parent)) {
0362         child = __lookup_mnt_last(&m->mnt, mnt->mnt_mountpoint);
0363         if (child && list_empty(&child->mnt_mounts) &&
0364             (ret = do_refcount_check(child, 1)))
0365             break;
0366     }
0367     return ret;
0368 }
0369 
0370 /*
0371  * Clear MNT_LOCKED when it can be shown to be safe.
0372  *
0373  * mount_lock lock must be held for write
0374  */
0375 void propagate_mount_unlock(struct mount *mnt)
0376 {
0377     struct mount *parent = mnt->mnt_parent;
0378     struct mount *m, *child;
0379 
0380     BUG_ON(parent == mnt);
0381 
0382     for (m = propagation_next(parent, parent); m;
0383             m = propagation_next(m, parent)) {
0384         child = __lookup_mnt_last(&m->mnt, mnt->mnt_mountpoint);
0385         if (child)
0386             child->mnt.mnt_flags &= ~MNT_LOCKED;
0387     }
0388 }
0389 
0390 /*
0391  * Mark all mounts that the MNT_LOCKED logic will allow to be unmounted.
0392  */
0393 static void mark_umount_candidates(struct mount *mnt)
0394 {
0395     struct mount *parent = mnt->mnt_parent;
0396     struct mount *m;
0397 
0398     BUG_ON(parent == mnt);
0399 
0400     for (m = propagation_next(parent, parent); m;
0401             m = propagation_next(m, parent)) {
0402         struct mount *child = __lookup_mnt_last(&m->mnt,
0403                         mnt->mnt_mountpoint);
0404         if (child && (!IS_MNT_LOCKED(child) || IS_MNT_MARKED(m))) {
0405             SET_MNT_MARK(child);
0406         }
0407     }
0408 }
0409 
0410 /*
0411  * NOTE: unmounting 'mnt' naturally propagates to all other mounts its
0412  * parent propagates to.
0413  */
0414 static void __propagate_umount(struct mount *mnt)
0415 {
0416     struct mount *parent = mnt->mnt_parent;
0417     struct mount *m;
0418 
0419     BUG_ON(parent == mnt);
0420 
0421     for (m = propagation_next(parent, parent); m;
0422             m = propagation_next(m, parent)) {
0423 
0424         struct mount *child = __lookup_mnt_last(&m->mnt,
0425                         mnt->mnt_mountpoint);
0426         /*
0427          * umount the child only if the child has no children
0428          * and the child is marked safe to unmount.
0429          */
0430         if (!child || !IS_MNT_MARKED(child))
0431             continue;
0432         CLEAR_MNT_MARK(child);
0433         if (list_empty(&child->mnt_mounts)) {
0434             list_del_init(&child->mnt_child);
0435             child->mnt.mnt_flags |= MNT_UMOUNT;
0436             list_move_tail(&child->mnt_list, &mnt->mnt_list);
0437         }
0438     }
0439 }
0440 
0441 /*
0442  * collect all mounts that receive propagation from the mount in @list,
0443  * and return these additional mounts in the same list.
0444  * @list: the list of mounts to be unmounted.
0445  *
0446  * vfsmount lock must be held for write
0447  */
0448 int propagate_umount(struct list_head *list)
0449 {
0450     struct mount *mnt;
0451 
0452     list_for_each_entry_reverse(mnt, list, mnt_list)
0453         mark_umount_candidates(mnt);
0454 
0455     list_for_each_entry(mnt, list, mnt_list)
0456         __propagate_umount(mnt);
0457     return 0;
0458 }