xref: /linux/fs/pnode.c (revision 01cbebb420709af722a769dddac26b30270ae4b2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/pnode.c
4  *
5  * (C) Copyright IBM Corporation 2005.
6  *	Author : Ram Pai (linuxram@us.ibm.com)
7  */
8 #include <linux/mnt_namespace.h>
9 #include <linux/mount.h>
10 #include <linux/fs.h>
11 #include <linux/nsproxy.h>
12 #include <uapi/linux/mount.h>
13 #include "internal.h"
14 #include "pnode.h"
15 
16 /* return the next shared peer mount of @p */
17 static inline struct mount *next_peer(struct mount *p)
18 {
19 	return list_entry(p->mnt_share.next, struct mount, mnt_share);
20 }
21 
22 static inline struct mount *first_slave(struct mount *p)
23 {
24 	return hlist_entry(p->mnt_slave_list.first, struct mount, mnt_slave);
25 }
26 
27 static inline struct mount *next_slave(struct mount *p)
28 {
29 	return hlist_entry(p->mnt_slave.next, struct mount, mnt_slave);
30 }
31 
32 /* locks: namespace_shared && is_mounted(mnt) */
33 static struct mount *get_peer_under_root(struct mount *mnt,
34 					 struct mnt_namespace *ns,
35 					 const struct path *root)
36 {
37 	struct mount *m = mnt;
38 
39 	do {
40 		/* Check the namespace first for optimization */
41 		if (m->mnt_ns == ns && is_path_reachable(m, m->mnt.mnt_root, root))
42 			return m;
43 
44 		m = next_peer(m);
45 	} while (m != mnt);
46 
47 	return NULL;
48 }
49 
50 /*
51  * Get ID of closest dominating peer group having a representative
52  * under the given root.
53  *
54  * locks: namespace_shared
55  */
56 int get_dominating_id(struct mount *mnt, const struct path *root)
57 {
58 	struct mount *m;
59 
60 	for (m = mnt->mnt_master; m != NULL; m = m->mnt_master) {
61 		struct mount *d = get_peer_under_root(m, mnt->mnt_ns, root);
62 		if (d)
63 			return d->mnt_group_id;
64 	}
65 
66 	return 0;
67 }
68 
69 static inline bool will_be_unmounted(struct mount *m)
70 {
71 	return m->mnt.mnt_flags & MNT_UMOUNT;
72 }
73 
74 static void transfer_propagation(struct mount *mnt, struct mount *to)
75 {
76 	struct hlist_node *p = NULL, *n;
77 	struct mount *m;
78 
79 	hlist_for_each_entry_safe(m, n, &mnt->mnt_slave_list, mnt_slave) {
80 		m->mnt_master = to;
81 		if (!to)
82 			hlist_del_init(&m->mnt_slave);
83 		else
84 			p = &m->mnt_slave;
85 	}
86 	if (p)
87 		hlist_splice_init(&mnt->mnt_slave_list, p, &to->mnt_slave_list);
88 }
89 
90 /*
91  * EXCL[namespace_sem]
92  */
93 void change_mnt_propagation(struct mount *mnt, int type)
94 {
95 	struct mount *m = mnt->mnt_master;
96 
97 	if (type == MS_SHARED) {
98 		set_mnt_shared(mnt);
99 		return;
100 	}
101 	if (IS_MNT_SHARED(mnt)) {
102 		if (list_empty(&mnt->mnt_share)) {
103 			mnt_release_group_id(mnt);
104 		} else {
105 			m = next_peer(mnt);
106 			list_del_init(&mnt->mnt_share);
107 			mnt->mnt_group_id = 0;
108 		}
109 		CLEAR_MNT_SHARED(mnt);
110 		transfer_propagation(mnt, m);
111 	}
112 	hlist_del_init(&mnt->mnt_slave);
113 	if (type == MS_SLAVE) {
114 		mnt->mnt_master = m;
115 		if (m)
116 			hlist_add_head(&mnt->mnt_slave, &m->mnt_slave_list);
117 	} else {
118 		mnt->mnt_master = NULL;
119 		if (type == MS_UNBINDABLE)
120 			mnt->mnt_t_flags |= T_UNBINDABLE;
121 		else
122 			mnt->mnt_t_flags &= ~T_UNBINDABLE;
123 	}
124 }
125 
126 static struct mount *trace_transfers(struct mount *m)
127 {
128 	while (1) {
129 		struct mount *next = next_peer(m);
130 
131 		if (next != m) {
132 			list_del_init(&m->mnt_share);
133 			m->mnt_group_id = 0;
134 			m->mnt_master = next;
135 		} else {
136 			if (IS_MNT_SHARED(m))
137 				mnt_release_group_id(m);
138 			next = m->mnt_master;
139 		}
140 		hlist_del_init(&m->mnt_slave);
141 		CLEAR_MNT_SHARED(m);
142 		SET_MNT_MARK(m);
143 
144 		if (!next || !will_be_unmounted(next))
145 			return next;
146 		if (IS_MNT_MARKED(next))
147 			return next->mnt_master;
148 		m = next;
149 	}
150 }
151 
152 static void set_destinations(struct mount *m, struct mount *master)
153 {
154 	struct mount *next;
155 
156 	while ((next = m->mnt_master) != master) {
157 		m->mnt_master = master;
158 		m = next;
159 	}
160 }
161 
162 void bulk_make_private(struct list_head *set)
163 {
164 	struct mount *m;
165 
166 	list_for_each_entry(m, set, mnt_list)
167 		if (!IS_MNT_MARKED(m))
168 			set_destinations(m, trace_transfers(m));
169 
170 	list_for_each_entry(m, set, mnt_list) {
171 		transfer_propagation(m, m->mnt_master);
172 		m->mnt_master = NULL;
173 		CLEAR_MNT_MARK(m);
174 	}
175 }
176 
177 static struct mount *__propagation_next(struct mount *m,
178 					 struct mount *origin)
179 {
180 	while (1) {
181 		struct mount *master = m->mnt_master;
182 
183 		if (master == origin->mnt_master) {
184 			struct mount *next = next_peer(m);
185 			return (next == origin) ? NULL : next;
186 		} else if (m->mnt_slave.next)
187 			return next_slave(m);
188 
189 		/* back at master */
190 		m = master;
191 	}
192 }
193 
194 /*
195  * get the next mount in the propagation tree.
196  * @m: the mount seen last
197  * @origin: the original mount from where the tree walk initiated
198  *
199  * Note that peer groups form contiguous segments of slave lists.
200  * We rely on that in get_source() to be able to find out if
201  * vfsmount found while iterating with propagation_next() is
202  * a peer of one we'd found earlier.
203  */
204 static struct mount *propagation_next(struct mount *m,
205 					 struct mount *origin)
206 {
207 	/* are there any slaves of this mount? */
208 	if (!IS_MNT_NEW(m) && !hlist_empty(&m->mnt_slave_list))
209 		return first_slave(m);
210 
211 	return __propagation_next(m, origin);
212 }
213 
214 static struct mount *skip_propagation_subtree(struct mount *m,
215 						struct mount *origin)
216 {
217 	/*
218 	 * Advance m past everything that gets propagation from it.
219 	 */
220 	struct mount *p = __propagation_next(m, origin);
221 
222 	while (p && peers(m, p))
223 		p = __propagation_next(p, origin);
224 
225 	return p;
226 }
227 
228 static struct mount *next_group(struct mount *m, struct mount *origin)
229 {
230 	while (1) {
231 		while (1) {
232 			struct mount *next;
233 			if (!IS_MNT_NEW(m) && !hlist_empty(&m->mnt_slave_list))
234 				return first_slave(m);
235 			next = next_peer(m);
236 			if (m->mnt_group_id == origin->mnt_group_id) {
237 				if (next == origin)
238 					return NULL;
239 			} else if (m->mnt_slave.next != &next->mnt_slave)
240 				break;
241 			m = next;
242 		}
243 		/* m is the last peer */
244 		while (1) {
245 			struct mount *master = m->mnt_master;
246 			if (m->mnt_slave.next)
247 				return next_slave(m);
248 			m = next_peer(master);
249 			if (master->mnt_group_id == origin->mnt_group_id)
250 				break;
251 			if (master->mnt_slave.next == &m->mnt_slave)
252 				break;
253 			m = master;
254 		}
255 		if (m == origin)
256 			return NULL;
257 	}
258 }
259 
260 static bool need_secondary(struct mount *m, struct mountpoint *dest_mp)
261 {
262 	/* skip ones added by this propagate_mnt() */
263 	if (IS_MNT_NEW(m))
264 		return false;
265 	/* skip if mountpoint isn't visible in m */
266 	if (!is_subdir(dest_mp->m_dentry, m->mnt.mnt_root))
267 		return false;
268 	/* skip if m is in the anon_ns */
269 	if (is_anon_ns(m->mnt_ns))
270 		return false;
271 	return true;
272 }
273 
274 static struct mount *find_master(struct mount *m,
275 				struct mount *last_copy,
276 				struct mount *original)
277 {
278 	struct mount *p;
279 
280 	// ascend until there's a copy for something with the same master
281 	for (;;) {
282 		p = m->mnt_master;
283 		if (!p || IS_MNT_MARKED(p))
284 			break;
285 		m = p;
286 	}
287 	while (!peers(last_copy, original)) {
288 		struct mount *parent = last_copy->mnt_parent;
289 		if (parent->mnt_master == p) {
290 			if (!peers(parent, m))
291 				last_copy = last_copy->mnt_master;
292 			break;
293 		}
294 		last_copy = last_copy->mnt_master;
295 	}
296 	return last_copy;
297 }
298 
299 /**
300  * propagate_mnt() - create secondary copies for tree attachment
301  * @dest_mnt:    destination mount.
302  * @dest_mp:     destination mountpoint.
303  * @source_mnt:  source mount.
304  * @tree_list:   list of secondaries to be attached.
305  *
306  * Create secondary copies for attaching a tree with root @source_mnt
307  * at mount @dest_mnt with mountpoint @dest_mp.  Link all new mounts
308  * into a propagation graph.  Set mountpoints for all secondaries,
309  * link their roots into @tree_list via ->mnt_hash.
310  */
311 int propagate_mnt(struct mount *dest_mnt, struct mountpoint *dest_mp,
312 		  struct mount *source_mnt, struct hlist_head *tree_list)
313 {
314 	struct mount *m, *n, *copy, *this;
315 	int err = 0, type;
316 
317 	if (dest_mnt->mnt_master)
318 		SET_MNT_MARK(dest_mnt->mnt_master);
319 
320 	/* iterate over peer groups, depth first */
321 	for (m = dest_mnt; m && !err; m = next_group(m, dest_mnt)) {
322 		if (m == dest_mnt) { // have one for dest_mnt itself
323 			copy = source_mnt;
324 			type = CL_MAKE_SHARED;
325 			n = next_peer(m);
326 			if (n == m)
327 				continue;
328 		} else {
329 			type = CL_SLAVE;
330 			/* beginning of peer group among the slaves? */
331 			if (IS_MNT_SHARED(m))
332 				type |= CL_MAKE_SHARED;
333 			n = m;
334 		}
335 		do {
336 			if (!need_secondary(n, dest_mp))
337 				continue;
338 			if (type & CL_SLAVE) // first in this peer group
339 				copy = find_master(n, copy, source_mnt);
340 			this = copy_tree(copy, copy->mnt.mnt_root, type);
341 			if (IS_ERR(this)) {
342 				err = PTR_ERR(this);
343 				break;
344 			}
345 			scoped_guard(mount_locked_reader)
346 				mnt_set_mountpoint(n, dest_mp, this);
347 			if (n->mnt_master)
348 				SET_MNT_MARK(n->mnt_master);
349 			copy = this;
350 			hlist_add_head(&this->mnt_hash, tree_list);
351 			err = count_mounts(n->mnt_ns, this);
352 			if (err)
353 				break;
354 			type = CL_MAKE_SHARED;
355 		} while ((n = next_peer(n)) != m);
356 	}
357 
358 	hlist_for_each_entry(n, tree_list, mnt_hash) {
359 		m = n->mnt_parent;
360 		if (m->mnt_master)
361 			CLEAR_MNT_MARK(m->mnt_master);
362 	}
363 	if (dest_mnt->mnt_master)
364 		CLEAR_MNT_MARK(dest_mnt->mnt_master);
365 	return err;
366 }
367 
368 /*
369  * return true if the refcount is greater than count
370  */
371 static inline int do_refcount_check(struct mount *mnt, int count)
372 {
373 	return mnt_get_count(mnt) > count;
374 }
375 
376 /**
377  * propagation_would_overmount - check whether propagation from @from
378  *                               would overmount @to
379  * @from: shared mount
380  * @to:   mount to check
381  * @mp:   future mountpoint of @to on @from
382  *
383  * If @from propagates mounts to @to, @from and @to must either be peers
384  * or one of the masters in the hierarchy of masters of @to must be a
385  * peer of @from.
386  *
387  * If the root of the @to mount is equal to the future mountpoint @mp of
388  * the @to mount on @from then @to will be overmounted by whatever is
389  * propagated to it.
390  *
391  * Context: This function expects namespace_lock() to be held and that
392  *          @mp is stable.
393  * Return: If @from overmounts @to, true is returned, false if not.
394  */
395 bool propagation_would_overmount(const struct mount *from,
396 				 const struct mount *to,
397 				 const struct mountpoint *mp)
398 {
399 	if (!IS_MNT_SHARED(from))
400 		return false;
401 
402 	if (to->mnt.mnt_root != mp->m_dentry)
403 		return false;
404 
405 	for (const struct mount *m = to; m; m = m->mnt_master) {
406 		if (peers(from, m))
407 			return true;
408 	}
409 
410 	return false;
411 }
412 
413 /* Does @m receive propagation from @parent? */
414 static bool receives_from(struct mount *m, struct mount *parent)
415 {
416 	if (m == parent)
417 		return false;
418 	for (; m; m = m->mnt_master)
419 		if (m == parent || peers(m, parent))
420 			return true;
421 	return false;
422 }
423 
424 /*
425  * Does @m receive propagation from the victim's parent as well? If so, then
426  * the mount at the victim's mountpoint inside of @m is a umount candidate as
427  * well. So it's the next candidate in the chain. Otherwise the chain ends at
428  * @m.
429  */
430 static struct mount *next_candidate(struct mount *m, struct mount *victim)
431 {
432 	if (!receives_from(m, victim->mnt_parent))
433 		return NULL;
434 	return __lookup_mnt(&m->mnt, victim->mnt_mountpoint);
435 }
436 
437 /*
438  * Would propagate_umount() pull out a mount of the chain of candidates that
439  * starts at @c, and does that mount have references beyond its own?
440  *
441  * This mirrors how trim_one(), trim_ancestors() and handle_locked() handle a
442  * synchronous umount:
443  *
444  * - single victim
445  * - without children
446  * - with MNT_LOCKED already cleared on every candidate by propagate_mount_unlock()
447  *
448  * A copy of the victim gets unmounted when each of its children is
449  * the next candidate in the chain or its overmount, unless the next
450  * unmount candidate is not its overmount and some unmount candidate further
451  * down has a child outside the chain.  Keep this in sync with
452  * Documentation/filesystems/propagate_umount.txt.
453  */
454 static bool chain_busy(struct mount *c, struct mount *victim)
455 {
456 	struct mount *m, *n, *next, *deepest = NULL;
457 	bool above;
458 
459 	/* the deepest candidate with a child outside the chain */
460 	for (m = c; m; m = next) {
461 		next = next_candidate(m, victim);
462 		list_for_each_entry(n, &m->mnt_mounts, mnt_child) {
463 			if (n != next && n != victim) {
464 				deepest = m;
465 				break;
466 			}
467 		}
468 	}
469 
470 	above = deepest != NULL;	/* @deepest is at or below @m */
471 	for (m = c; m; m = next) {
472 		bool goes = true;
473 
474 		next = next_candidate(m, victim);
475 		list_for_each_entry(n, &m->mnt_mounts, mnt_child) {
476 			if (n != next && n != m->overmount && n != victim) {
477 				goes = false;
478 				break;
479 			}
480 		}
481 		if (goes && next && next != m->overmount && above && m != deepest)
482 			goes = false;
483 		if (m == deepest)
484 			above = false;
485 		if (goes && do_refcount_check(m, 1))
486 			return true;
487 	}
488 	return false;
489 }
490 
491 /*
492  * check if the mount 'mnt' can be unmounted successfully.
493  * @mnt: the mount to be checked for unmount
494  * NOTE: unmounting 'mnt' would naturally propagate to all
495  * other mounts its parent propagates to.
496  * Check if any of the mounts that propagate_umount() would pull out
497  * along with it have more references than their own. If so return busy.
498  *
499  * vfsmount lock must be held for write
500  */
501 int propagate_mount_busy(struct mount *mnt, int refcnt)
502 {
503 	struct mount *parent = mnt->mnt_parent;
504 	struct dentry *mp = mnt->mnt_mountpoint;
505 	struct mount *m;
506 
507 	/*
508 	 * quickly check if the current mount can be unmounted.
509 	 * If not, we don't have to go checking for all other
510 	 * mounts
511 	 */
512 	if (!list_empty(&mnt->mnt_mounts) || do_refcount_check(mnt, refcnt))
513 		return 1;
514 
515 	if (mnt == parent)
516 		return 0;
517 
518 	/* the candidates are the mounts at @mp below the receivers */
519 	for (m = propagation_next(parent, parent); m;
520 	     m = propagation_next(m, parent)) {
521 		struct mount *c = __lookup_mnt(&m->mnt, mp);
522 
523 		/* each chain once, from its top: skip receivers that are candidates */
524 		if (!c || (mnt_has_parent(m) && m->mnt_mountpoint == mp &&
525 			   receives_from(m->mnt_parent, parent)))
526 			continue;
527 		if (chain_busy(c, mnt))
528 			return 1;
529 	}
530 	return 0;
531 }
532 
533 /*
534  * Clear MNT_LOCKED when it can be shown to be safe.
535  *
536  * mount_lock lock must be held for write
537  */
538 void propagate_mount_unlock(struct mount *mnt)
539 {
540 	struct mount *parent = mnt->mnt_parent;
541 	struct mount *m, *child;
542 
543 	BUG_ON(parent == mnt);
544 
545 	for (m = propagation_next(parent, parent); m;
546 			m = propagation_next(m, parent)) {
547 		child = __lookup_mnt(&m->mnt, mnt->mnt_mountpoint);
548 		if (child)
549 			child->mnt.mnt_flags &= ~MNT_LOCKED;
550 	}
551 }
552 
553 static inline bool is_candidate(struct mount *m)
554 {
555 	return m->mnt_t_flags & T_UMOUNT_CANDIDATE;
556 }
557 
558 static void umount_one(struct mount *m, struct list_head *to_umount)
559 {
560 	m->mnt.mnt_flags |= MNT_UMOUNT;
561 	list_del_init(&m->mnt_child);
562 	move_from_ns(m);
563 	list_add_tail(&m->mnt_list, to_umount);
564 }
565 
566 static void remove_from_candidate_list(struct mount *m)
567 {
568 	m->mnt_t_flags &= ~(T_MARKED | T_UMOUNT_CANDIDATE);
569 	list_del_init(&m->mnt_list);
570 }
571 
572 static void gather_candidates(struct list_head *set,
573 			      struct list_head *candidates)
574 {
575 	struct mount *m, *p, *q;
576 
577 	list_for_each_entry(m, set, mnt_list) {
578 		if (is_candidate(m))
579 			continue;
580 		m->mnt_t_flags |= T_UMOUNT_CANDIDATE;
581 		p = m->mnt_parent;
582 		q = propagation_next(p, p);
583 		while (q) {
584 			struct mount *child = __lookup_mnt(&q->mnt,
585 							   m->mnt_mountpoint);
586 			if (child) {
587 				/*
588 				 * We might've already run into this one.  That
589 				 * must've happened on earlier iteration of the
590 				 * outer loop; in that case we can skip those
591 				 * parents that get propagation from q - there
592 				 * will be nothing new on those as well.
593 				 */
594 				if (is_candidate(child)) {
595 					q = skip_propagation_subtree(q, p);
596 					continue;
597 				}
598 				child->mnt_t_flags |= T_UMOUNT_CANDIDATE;
599 				if (!will_be_unmounted(child))
600 					list_add(&child->mnt_list, candidates);
601 			}
602 			q = propagation_next(q, p);
603 		}
604 	}
605 	list_for_each_entry(m, set, mnt_list)
606 		m->mnt_t_flags &= ~T_UMOUNT_CANDIDATE;
607 }
608 
609 /*
610  * We know that some child of @m can't be unmounted.  In all places where the
611  * chain of descent of @m has child not overmounting the root of parent,
612  * the parent can't be unmounted either.
613  */
614 static void trim_ancestors(struct mount *m)
615 {
616 	struct mount *p;
617 
618 	for (p = m->mnt_parent; is_candidate(p); m = p, p = p->mnt_parent) {
619 		if (IS_MNT_MARKED(m))	// all candidates beneath are overmounts
620 			return;
621 		SET_MNT_MARK(m);
622 		if (m != p->overmount)
623 			p->mnt_t_flags &= ~T_UMOUNT_CANDIDATE;
624 	}
625 }
626 
627 /*
628  * Find and exclude all umount candidates forbidden by @m
629  * (see Documentation/filesystems/propagate_umount.txt)
630  * If we can immediately tell that @m is OK to unmount (unlocked
631  * and all children are already committed to unmounting) commit
632  * to unmounting it.
633  * Only @m itself might be taken from the candidates list;
634  * anything found by trim_ancestors() is marked non-candidate
635  * and left on the list.
636  */
637 static void trim_one(struct mount *m, struct list_head *to_umount)
638 {
639 	bool remove_this = false, found = false, umount_this = false;
640 	struct mount *n;
641 
642 	if (!is_candidate(m)) { // trim_ancestors() left it on list
643 		remove_from_candidate_list(m);
644 		return;
645 	}
646 
647 	list_for_each_entry(n, &m->mnt_mounts, mnt_child) {
648 		if (!is_candidate(n)) {
649 			found = true;
650 			if (n != m->overmount) {
651 				remove_this = true;
652 				break;
653 			}
654 		}
655 	}
656 	if (found) {
657 		trim_ancestors(m);
658 	} else if (!IS_MNT_LOCKED(m) && list_empty(&m->mnt_mounts)) {
659 		remove_this = true;
660 		umount_this = true;
661 	}
662 	if (remove_this) {
663 		remove_from_candidate_list(m);
664 		if (umount_this)
665 			umount_one(m, to_umount);
666 	}
667 }
668 
669 static void handle_locked(struct mount *m, struct list_head *to_umount)
670 {
671 	struct mount *cutoff = m, *p;
672 
673 	if (!is_candidate(m)) { // trim_ancestors() left it on list
674 		remove_from_candidate_list(m);
675 		return;
676 	}
677 	for (p = m; is_candidate(p); p = p->mnt_parent) {
678 		remove_from_candidate_list(p);
679 		if (!IS_MNT_LOCKED(p))
680 			cutoff = p->mnt_parent;
681 	}
682 	if (will_be_unmounted(p))
683 		cutoff = p;
684 	while (m != cutoff) {
685 		umount_one(m, to_umount);
686 		m = m->mnt_parent;
687 	}
688 }
689 
690 /*
691  * @m is not to going away, and it overmounts the top of a stack of mounts
692  * that are going away.  We know that all of those are fully overmounted
693  * by the one above (@m being the topmost of the chain), so @m can be slid
694  * in place where the bottom of the stack is attached.
695  *
696  * NOTE: here we temporarily violate a constraint - two mounts end up with
697  * the same parent and mountpoint; that will be remedied as soon as we
698  * return from propagate_umount() - its caller (umount_tree()) will detach
699  * the stack from the parent it (and now @m) is attached to.  umount_tree()
700  * detaches every unmounted mount from its parent, whether that parent
701  * remains in the tree or not.
702  */
703 static void reparent(struct mount *m)
704 {
705 	struct mount *p = m;
706 	struct mountpoint *mp;
707 
708 	do {
709 		mp = p->mnt_mp;
710 		p = p->mnt_parent;
711 	} while (will_be_unmounted(p));
712 
713 	mnt_change_mountpoint(p, mp, m);
714 	mnt_notify_add(m);
715 }
716 
717 /**
718  * propagate_umount - apply propagation rules to the set of mounts for umount()
719  * @set: the list of mounts to be unmounted.
720  *
721  * Collect all mounts that receive propagation from the mount in @set and have
722  * no obstacles to being unmounted.  Add these additional mounts to the set.
723  *
724  * See Documentation/filesystems/propagate_umount.txt if you do anything in
725  * this area.
726  *
727  * Locks held:
728  * mount_lock (write_seqlock), namespace_sem (exclusive).
729  */
730 void propagate_umount(struct list_head *set)
731 {
732 	struct mount *m, *p;
733 	LIST_HEAD(to_umount);	// committed to unmounting
734 	LIST_HEAD(candidates);	// undecided umount candidates
735 
736 	// collect all candidates
737 	gather_candidates(set, &candidates);
738 
739 	// reduce the set until it's non-shifting
740 	list_for_each_entry_safe(m, p, &candidates, mnt_list)
741 		trim_one(m, &to_umount);
742 
743 	// ... and non-revealing
744 	while (!list_empty(&candidates)) {
745 		m = list_first_entry(&candidates,struct mount, mnt_list);
746 		handle_locked(m, &to_umount);
747 	}
748 
749 	// now to_umount consists of all acceptable candidates
750 	// deal with reparenting of surviving overmounts on those
751 	list_for_each_entry(m, &to_umount, mnt_list) {
752 		struct mount *over = m->overmount;
753 		if (over && !will_be_unmounted(over))
754 			reparent(over);
755 	}
756 
757 	// and fold them into the set
758 	list_splice_tail_init(&to_umount, set);
759 }
760