0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/mnt_namespace.h>
0009 #include <linux/mount.h>
0010 #include <linux/fs.h>
0011 #include <linux/nsproxy.h>
0012 #include <uapi/linux/mount.h>
0013 #include "internal.h"
0014 #include "pnode.h"
0015
0016
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 *last_slave(struct mount *p)
0028 {
0029 return list_entry(p->mnt_slave_list.prev, struct mount, mnt_slave);
0030 }
0031
0032 static inline struct mount *next_slave(struct mount *p)
0033 {
0034 return list_entry(p->mnt_slave.next, struct mount, mnt_slave);
0035 }
0036
0037 static struct mount *get_peer_under_root(struct mount *mnt,
0038 struct mnt_namespace *ns,
0039 const struct path *root)
0040 {
0041 struct mount *m = mnt;
0042
0043 do {
0044
0045 if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
0046 return m;
0047
0048 m = next_peer(m);
0049 } while (m != mnt);
0050
0051 return NULL;
0052 }
0053
0054
0055
0056
0057
0058
0059
0060 int get_dominating_id(struct mount *mnt, const struct path *root)
0061 {
0062 struct mount *m;
0063
0064 for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
0065 struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
0066 if (d)
0067 return d->mnt_group_id;
0068 }
0069
0070 return 0;
0071 }
0072
0073 static int do_make_slave(struct mount *mnt)
0074 {
0075 struct mount *master, *slave_mnt;
0076
0077 if (list_empty(&mnt->mnt_share)) {
0078 if (IS_MNT_SHARED(mnt)) {
0079 mnt_release_group_id(mnt);
0080 CLEAR_MNT_SHARED(mnt);
0081 }
0082 master = mnt->mnt_master;
0083 if (!master) {
0084 struct list_head *p = &mnt->mnt_slave_list;
0085 while (!list_empty(p)) {
0086 slave_mnt = list_first_entry(p,
0087 struct mount, mnt_slave);
0088 list_del_init(&slave_mnt->mnt_slave);
0089 slave_mnt->mnt_master = NULL;
0090 }
0091 return 0;
0092 }
0093 } else {
0094 struct mount *m;
0095
0096
0097
0098
0099
0100 for (m = master = next_peer(mnt); m != mnt; m = next_peer(m)) {
0101 if (m->mnt.mnt_root == mnt->mnt.mnt_root) {
0102 master = m;
0103 break;
0104 }
0105 }
0106 list_del_init(&mnt->mnt_share);
0107 mnt->mnt_group_id = 0;
0108 CLEAR_MNT_SHARED(mnt);
0109 }
0110 list_for_each_entry(slave_mnt, &mnt->mnt_slave_list, mnt_slave)
0111 slave_mnt->mnt_master = master;
0112 list_move(&mnt->mnt_slave, &master->mnt_slave_list);
0113 list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
0114 INIT_LIST_HEAD(&mnt->mnt_slave_list);
0115 mnt->mnt_master = master;
0116 return 0;
0117 }
0118
0119
0120
0121
0122 void change_mnt_propagation(struct mount *mnt, int type)
0123 {
0124 if (type == MS_SHARED) {
0125 set_mnt_shared(mnt);
0126 return;
0127 }
0128 do_make_slave(mnt);
0129 if (type != MS_SLAVE) {
0130 list_del_init(&mnt->mnt_slave);
0131 mnt->mnt_master = NULL;
0132 if (type == MS_UNBINDABLE)
0133 mnt->mnt.mnt_flags |= MNT_UNBINDABLE;
0134 else
0135 mnt->mnt.mnt_flags &= ~MNT_UNBINDABLE;
0136 }
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 static struct mount *propagation_next(struct mount *m,
0150 struct mount *origin)
0151 {
0152
0153 if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
0154 return first_slave(m);
0155
0156 while (1) {
0157 struct mount *master = m->mnt_master;
0158
0159 if (master == origin->mnt_master) {
0160 struct mount *next = next_peer(m);
0161 return (next == origin) ? NULL : next;
0162 } else if (m->mnt_slave.next != &master->mnt_slave_list)
0163 return next_slave(m);
0164
0165
0166 m = master;
0167 }
0168 }
0169
0170 static struct mount *skip_propagation_subtree(struct mount *m,
0171 struct mount *origin)
0172 {
0173
0174
0175
0176
0177 if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
0178 m = last_slave(m);
0179
0180 return m;
0181 }
0182
0183 static struct mount *next_group(struct mount *m, struct mount *origin)
0184 {
0185 while (1) {
0186 while (1) {
0187 struct mount *next;
0188 if (!IS_MNT_NEW(m) && !list_empty(&m->mnt_slave_list))
0189 return first_slave(m);
0190 next = next_peer(m);
0191 if (m->mnt_group_id == origin->mnt_group_id) {
0192 if (next == origin)
0193 return NULL;
0194 } else if (m->mnt_slave.next != &next->mnt_slave)
0195 break;
0196 m = next;
0197 }
0198
0199 while (1) {
0200 struct mount *master = m->mnt_master;
0201 if (m->mnt_slave.next != &master->mnt_slave_list)
0202 return next_slave(m);
0203 m = next_peer(master);
0204 if (master->mnt_group_id == origin->mnt_group_id)
0205 break;
0206 if (master->mnt_slave.next == &m->mnt_slave)
0207 break;
0208 m = master;
0209 }
0210 if (m == origin)
0211 return NULL;
0212 }
0213 }
0214
0215
0216 static struct mount *last_dest, *first_source, *last_source, *dest_master;
0217 static struct mountpoint *mp;
0218 static struct hlist_head *list;
0219
0220 static inline bool peers(struct mount *m1, struct mount *m2)
0221 {
0222 return m1->mnt_group_id == m2->mnt_group_id && m1->mnt_group_id;
0223 }
0224
0225 static int propagate_one(struct mount *m)
0226 {
0227 struct mount *child;
0228 int type;
0229
0230 if (IS_MNT_NEW(m))
0231 return 0;
0232
0233 if (!is_subdir(mp->m_dentry, m->mnt.mnt_root))
0234 return 0;
0235 if (peers(m, last_dest)) {
0236 type = CL_MAKE_SHARED;
0237 } else {
0238 struct mount *n, *p;
0239 bool done;
0240 for (n = m; ; n = p) {
0241 p = n->mnt_master;
0242 if (p == dest_master || IS_MNT_MARKED(p))
0243 break;
0244 }
0245 do {
0246 struct mount *parent = last_source->mnt_parent;
0247 if (last_source == first_source)
0248 break;
0249 done = parent->mnt_master == p;
0250 if (done && peers(n, parent))
0251 break;
0252 last_source = last_source->mnt_master;
0253 } while (!done);
0254
0255 type = CL_SLAVE;
0256
0257 if (IS_MNT_SHARED(m))
0258 type |= CL_MAKE_SHARED;
0259 }
0260
0261 child = copy_tree(last_source, last_source->mnt.mnt_root, type);
0262 if (IS_ERR(child))
0263 return PTR_ERR(child);
0264 read_seqlock_excl(&mount_lock);
0265 mnt_set_mountpoint(m, mp, child);
0266 if (m->mnt_master != dest_master)
0267 SET_MNT_MARK(m->mnt_master);
0268 read_sequnlock_excl(&mount_lock);
0269 last_dest = m;
0270 last_source = child;
0271 hlist_add_head(&child->mnt_hash, list);
0272 return count_mounts(m->mnt_ns, child);
0273 }
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
0289 struct mount *source_mnt, struct hlist_head *tree_list)
0290 {
0291 struct mount *m, *n;
0292 int ret = 0;
0293
0294
0295
0296
0297
0298
0299 last_dest = dest_mnt;
0300 first_source = source_mnt;
0301 last_source = source_mnt;
0302 mp = dest_mp;
0303 list = tree_list;
0304 dest_master = dest_mnt->mnt_master;
0305
0306
0307 for (n = next_peer(dest_mnt); n != dest_mnt; n = next_peer(n)) {
0308 ret = propagate_one(n);
0309 if (ret)
0310 goto out;
0311 }
0312
0313
0314 for (m = next_group(dest_mnt, dest_mnt); m;
0315 m = next_group(m, dest_mnt)) {
0316
0317 n = m;
0318 do {
0319 ret = propagate_one(n);
0320 if (ret)
0321 goto out;
0322 n = next_peer(n);
0323 } while (n != m);
0324 }
0325 out:
0326 read_seqlock_excl(&mount_lock);
0327 hlist_for_each_entry(n, tree_list, mnt_hash) {
0328 m = n->mnt_parent;
0329 if (m->mnt_master != dest_mnt->mnt_master)
0330 CLEAR_MNT_MARK(m->mnt_master);
0331 }
0332 read_sequnlock_excl(&mount_lock);
0333 return ret;
0334 }
0335
0336 static struct mount *find_topper(struct mount *mnt)
0337 {
0338
0339 struct mount *child;
0340
0341 if (!list_is_singular(&mnt->mnt_mounts))
0342 return NULL;
0343
0344 child = list_first_entry(&mnt->mnt_mounts, struct mount, mnt_child);
0345 if (child->mnt_mountpoint != mnt->mnt.mnt_root)
0346 return NULL;
0347
0348 return child;
0349 }
0350
0351
0352
0353
0354 static inline int do_refcount_check(struct mount *mnt, int count)
0355 {
0356 return mnt_get_count(mnt) > count;
0357 }
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369 int propagate_mount_busy(struct mount *mnt, int refcnt)
0370 {
0371 struct mount *m, *child, *topper;
0372 struct mount *parent = mnt->mnt_parent;
0373
0374 if (mnt == parent)
0375 return do_refcount_check(mnt, refcnt);
0376
0377
0378
0379
0380
0381
0382 if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
0383 return 1;
0384
0385 for (m = propagation_next(parent, parent); m;
0386 m = propagation_next(m, parent)) {
0387 int count = 1;
0388 child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
0389 if (!child)
0390 continue;
0391
0392
0393
0394
0395 topper = find_topper(child);
0396 if (topper)
0397 count += 1;
0398 else if (!list_empty(&child->mnt_mounts))
0399 continue;
0400
0401 if (do_refcount_check(child, count))
0402 return 1;
0403 }
0404 return 0;
0405 }
0406
0407
0408
0409
0410
0411
0412 void propagate_mount_unlock(struct mount *mnt)
0413 {
0414 struct mount *parent = mnt->mnt_parent;
0415 struct mount *m, *child;
0416
0417 BUG_ON(parent == mnt);
0418
0419 for (m = propagation_next(parent, parent); m;
0420 m = propagation_next(m, parent)) {
0421 child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
0422 if (child)
0423 child->mnt.mnt_flags &= ~MNT_LOCKED;
0424 }
0425 }
0426
0427 static void umount_one(struct mount *mnt, struct list_head *to_umount)
0428 {
0429 CLEAR_MNT_MARK(mnt);
0430 mnt->mnt.mnt_flags |= MNT_UMOUNT;
0431 list_del_init(&mnt->mnt_child);
0432 list_del_init(&mnt->mnt_umounting);
0433 list_move_tail(&mnt->mnt_list, to_umount);
0434 }
0435
0436
0437
0438
0439
0440 static bool __propagate_umount(struct mount *mnt,
0441 struct list_head *to_umount,
0442 struct list_head *to_restore)
0443 {
0444 bool progress = false;
0445 struct mount *child;
0446
0447
0448
0449
0450
0451 if (mnt->mnt.mnt_flags & (MNT_UMOUNT | MNT_MARKED))
0452 goto out;
0453
0454
0455
0456
0457 list_for_each_entry(child, &mnt->mnt_mounts, mnt_child) {
0458 if (child->mnt_mountpoint == mnt->mnt.mnt_root)
0459 continue;
0460 if (!list_empty(&child->mnt_umounting) && IS_MNT_MARKED(child))
0461 continue;
0462
0463 goto children;
0464 }
0465
0466
0467 SET_MNT_MARK(mnt);
0468 progress = true;
0469
0470
0471 if (!IS_MNT_LOCKED(mnt)) {
0472 umount_one(mnt, to_umount);
0473 } else {
0474 children:
0475 list_move_tail(&mnt->mnt_umounting, to_restore);
0476 }
0477 out:
0478 return progress;
0479 }
0480
0481 static void umount_list(struct list_head *to_umount,
0482 struct list_head *to_restore)
0483 {
0484 struct mount *mnt, *child, *tmp;
0485 list_for_each_entry(mnt, to_umount, mnt_list) {
0486 list_for_each_entry_safe(child, tmp, &mnt->mnt_mounts, mnt_child) {
0487
0488 if (child->mnt_mountpoint == mnt->mnt.mnt_root)
0489 list_move_tail(&child->mnt_umounting, to_restore);
0490 else
0491 umount_one(child, to_umount);
0492 }
0493 }
0494 }
0495
0496 static void restore_mounts(struct list_head *to_restore)
0497 {
0498
0499 while (!list_empty(to_restore)) {
0500 struct mount *mnt, *parent;
0501 struct mountpoint *mp;
0502
0503 mnt = list_first_entry(to_restore, struct mount, mnt_umounting);
0504 CLEAR_MNT_MARK(mnt);
0505 list_del_init(&mnt->mnt_umounting);
0506
0507
0508 mp = mnt->mnt_mp;
0509 parent = mnt->mnt_parent;
0510 while (parent->mnt.mnt_flags & MNT_UMOUNT) {
0511 mp = parent->mnt_mp;
0512 parent = parent->mnt_parent;
0513 }
0514 if (parent != mnt->mnt_parent)
0515 mnt_change_mountpoint(parent, mp, mnt);
0516 }
0517 }
0518
0519 static void cleanup_umount_visitations(struct list_head *visited)
0520 {
0521 while (!list_empty(visited)) {
0522 struct mount *mnt =
0523 list_first_entry(visited, struct mount, mnt_umounting);
0524 list_del_init(&mnt->mnt_umounting);
0525 }
0526 }
0527
0528
0529
0530
0531
0532
0533
0534
0535 int propagate_umount(struct list_head *list)
0536 {
0537 struct mount *mnt;
0538 LIST_HEAD(to_restore);
0539 LIST_HEAD(to_umount);
0540 LIST_HEAD(visited);
0541
0542
0543 list_for_each_entry_reverse(mnt, list, mnt_list) {
0544 struct mount *parent = mnt->mnt_parent;
0545 struct mount *m;
0546
0547
0548
0549
0550
0551
0552
0553 if (!list_empty(&mnt->mnt_umounting))
0554 continue;
0555
0556 list_add_tail(&mnt->mnt_umounting, &visited);
0557 for (m = propagation_next(parent, parent); m;
0558 m = propagation_next(m, parent)) {
0559 struct mount *child = __lookup_mnt(&m->mnt,
0560 mnt->mnt_mountpoint);
0561 if (!child)
0562 continue;
0563
0564 if (!list_empty(&child->mnt_umounting)) {
0565
0566
0567
0568
0569
0570
0571
0572 m = skip_propagation_subtree(m, parent);
0573 continue;
0574 } else if (child->mnt.mnt_flags & MNT_UMOUNT) {
0575
0576
0577
0578
0579
0580
0581 list_add_tail(&child->mnt_umounting, &visited);
0582 continue;
0583 }
0584
0585
0586 while (__propagate_umount(child,
0587 &to_umount, &to_restore)) {
0588
0589 child = child->mnt_parent;
0590 if (list_empty(&child->mnt_umounting))
0591 break;
0592 }
0593 }
0594 }
0595
0596 umount_list(&to_umount, &to_restore);
0597 restore_mounts(&to_restore);
0598 cleanup_umount_visitations(&visited);
0599 list_splice_tail(&to_umount, list);
0600
0601 return 0;
0602 }