blob: 7ce90052faac0bca9feb02d98d5c29bf922ffb9d [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * linux/fs/namespace.c
3 *
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
6 *
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
10
11#include <linux/syscalls.h>
12#include <linux/export.h>
13#include <linux/capability.h>
14#include <linux/mnt_namespace.h>
15#include <linux/namei.h>
16#include <linux/security.h>
17#include <linux/idr.h>
18#include <linux/acct.h> /* acct_auto_close_mnt */
19#include <linux/ramfs.h> /* init_rootfs */
20#include <linux/fs_struct.h> /* get_fs_root et.al. */
21#include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
22#include <linux/uaccess.h>
23#include <linux/delay.h>
24#include "pnode.h"
25#include "internal.h"
26
27#define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
28#define HASH_SIZE (1UL << HASH_SHIFT)
29
30static int event;
31static DEFINE_IDA(mnt_id_ida);
32static DEFINE_IDA(mnt_group_ida);
33static DEFINE_SPINLOCK(mnt_id_lock);
34static int mnt_id_start = 0;
35static int mnt_group_start = 1;
36
37static struct list_head *mount_hashtable __read_mostly;
38static struct kmem_cache *mnt_cache __read_mostly;
39static struct rw_semaphore namespace_sem;
40
41/* /sys/fs */
42struct kobject *fs_kobj;
43EXPORT_SYMBOL_GPL(fs_kobj);
44
45/*
46 * vfsmount lock may be taken for read to prevent changes to the
47 * vfsmount hash, ie. during mountpoint lookups or walking back
48 * up the tree.
49 *
50 * It should be taken for write in all cases where the vfsmount
51 * tree or hash is modified or when a vfsmount structure is modified.
52 */
53DEFINE_BRLOCK(vfsmount_lock);
54
55static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
56{
57 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
58 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
59 tmp = tmp + (tmp >> HASH_SHIFT);
60 return tmp & (HASH_SIZE - 1);
61}
62
63#define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
64
65/*
66 * allocation is serialized by namespace_sem, but we need the spinlock to
67 * serialize with freeing.
68 */
69static int mnt_alloc_id(struct mount *mnt)
70{
71 int res;
72
73retry:
74 ida_pre_get(&mnt_id_ida, GFP_KERNEL);
75 spin_lock(&mnt_id_lock);
76 res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
77 if (!res)
78 mnt_id_start = mnt->mnt_id + 1;
79 spin_unlock(&mnt_id_lock);
80 if (res == -EAGAIN)
81 goto retry;
82
83 return res;
84}
85
86static void mnt_free_id(struct mount *mnt)
87{
88 int id = mnt->mnt_id;
89 spin_lock(&mnt_id_lock);
90 ida_remove(&mnt_id_ida, id);
91 if (mnt_id_start > id)
92 mnt_id_start = id;
93 spin_unlock(&mnt_id_lock);
94}
95
96/*
97 * Allocate a new peer group ID
98 *
99 * mnt_group_ida is protected by namespace_sem
100 */
101static int mnt_alloc_group_id(struct mount *mnt)
102{
103 int res;
104
105 if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
106 return -ENOMEM;
107
108 res = ida_get_new_above(&mnt_group_ida,
109 mnt_group_start,
110 &mnt->mnt_group_id);
111 if (!res)
112 mnt_group_start = mnt->mnt_group_id + 1;
113
114 return res;
115}
116
117/*
118 * Release a peer group ID
119 */
120void mnt_release_group_id(struct mount *mnt)
121{
122 int id = mnt->mnt_group_id;
123 ida_remove(&mnt_group_ida, id);
124 if (mnt_group_start > id)
125 mnt_group_start = id;
126 mnt->mnt_group_id = 0;
127}
128
129/*
130 * vfsmount lock must be held for read
131 */
132static inline void mnt_add_count(struct mount *mnt, int n)
133{
134#ifdef CONFIG_SMP
135 this_cpu_add(mnt->mnt_pcp->mnt_count, n);
136#else
137 preempt_disable();
138 mnt->mnt_count += n;
139 preempt_enable();
140#endif
141}
142
143/*
144 * vfsmount lock must be held for write
145 */
146unsigned int mnt_get_count(struct mount *mnt)
147{
148#ifdef CONFIG_SMP
149 unsigned int count = 0;
150 int cpu;
151
152 for_each_possible_cpu(cpu) {
153 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
154 }
155
156 return count;
157#else
158 return mnt->mnt_count;
159#endif
160}
161
162static struct mount *alloc_vfsmnt(const char *name)
163{
164 struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
165 if (mnt) {
166 int err;
167
168 err = mnt_alloc_id(mnt);
169 if (err)
170 goto out_free_cache;
171
172 if (name) {
173 mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
174 if (!mnt->mnt_devname)
175 goto out_free_id;
176 }
177
178#ifdef CONFIG_SMP
179 mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
180 if (!mnt->mnt_pcp)
181 goto out_free_devname;
182
183 this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
184#else
185 mnt->mnt_count = 1;
186 mnt->mnt_writers = 0;
187#endif
188
189 INIT_LIST_HEAD(&mnt->mnt_hash);
190 INIT_LIST_HEAD(&mnt->mnt_child);
191 INIT_LIST_HEAD(&mnt->mnt_mounts);
192 INIT_LIST_HEAD(&mnt->mnt_list);
193 INIT_LIST_HEAD(&mnt->mnt_expire);
194 INIT_LIST_HEAD(&mnt->mnt_share);
195 INIT_LIST_HEAD(&mnt->mnt_slave_list);
196 INIT_LIST_HEAD(&mnt->mnt_slave);
197#ifdef CONFIG_FSNOTIFY
198 INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
199#endif
200 }
201 return mnt;
202
203#ifdef CONFIG_SMP
204out_free_devname:
205 kfree(mnt->mnt_devname);
206#endif
207out_free_id:
208 mnt_free_id(mnt);
209out_free_cache:
210 kmem_cache_free(mnt_cache, mnt);
211 return NULL;
212}
213
214/*
215 * Most r/o checks on a fs are for operations that take
216 * discrete amounts of time, like a write() or unlink().
217 * We must keep track of when those operations start
218 * (for permission checks) and when they end, so that
219 * we can determine when writes are able to occur to
220 * a filesystem.
221 */
222/*
223 * __mnt_is_readonly: check whether a mount is read-only
224 * @mnt: the mount to check for its write status
225 *
226 * This shouldn't be used directly ouside of the VFS.
227 * It does not guarantee that the filesystem will stay
228 * r/w, just that it is right *now*. This can not and
229 * should not be used in place of IS_RDONLY(inode).
230 * mnt_want/drop_write() will _keep_ the filesystem
231 * r/w.
232 */
233int __mnt_is_readonly(struct vfsmount *mnt)
234{
235 if (mnt->mnt_flags & MNT_READONLY)
236 return 1;
237 if (mnt->mnt_sb->s_flags & MS_RDONLY)
238 return 1;
239 return 0;
240}
241EXPORT_SYMBOL_GPL(__mnt_is_readonly);
242
243static inline void mnt_inc_writers(struct mount *mnt)
244{
245#ifdef CONFIG_SMP
246 this_cpu_inc(mnt->mnt_pcp->mnt_writers);
247#else
248 mnt->mnt_writers++;
249#endif
250}
251
252static inline void mnt_dec_writers(struct mount *mnt)
253{
254#ifdef CONFIG_SMP
255 this_cpu_dec(mnt->mnt_pcp->mnt_writers);
256#else
257 mnt->mnt_writers--;
258#endif
259}
260
261static unsigned int mnt_get_writers(struct mount *mnt)
262{
263#ifdef CONFIG_SMP
264 unsigned int count = 0;
265 int cpu;
266
267 for_each_possible_cpu(cpu) {
268 count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
269 }
270
271 return count;
272#else
273 return mnt->mnt_writers;
274#endif
275}
276
277static int mnt_is_readonly(struct vfsmount *mnt)
278{
279 if (mnt->mnt_sb->s_readonly_remount)
280 return 1;
281 /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
282 smp_rmb();
283 return __mnt_is_readonly(mnt);
284}
285
286/*
287 * Most r/o checks on a fs are for operations that take
288 * discrete amounts of time, like a write() or unlink().
289 * We must keep track of when those operations start
290 * (for permission checks) and when they end, so that
291 * we can determine when writes are able to occur to
292 * a filesystem.
293 */
294/**
295 * mnt_want_write - get write access to a mount
296 * @m: the mount on which to take a write
297 *
298 * This tells the low-level filesystem that a write is
299 * about to be performed to it, and makes sure that
300 * writes are allowed before returning success. When
301 * the write operation is finished, mnt_drop_write()
302 * must be called. This is effectively a refcount.
303 */
304int mnt_want_write(struct vfsmount *m)
305{
306 struct mount *mnt = real_mount(m);
307 int ret = 0;
308
309 preempt_disable();
310 mnt_inc_writers(mnt);
311 /*
312 * The store to mnt_inc_writers must be visible before we pass
313 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
314 * incremented count after it has set MNT_WRITE_HOLD.
315 */
316 smp_mb();
317 while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD) {
318 preempt_enable();
319 cpu_chill();
320 preempt_disable();
321 }
322 /*
323 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
324 * be set to match its requirements. So we must not load that until
325 * MNT_WRITE_HOLD is cleared.
326 */
327 smp_rmb();
328 if (mnt_is_readonly(m)) {
329 mnt_dec_writers(mnt);
330 ret = -EROFS;
331 }
332 preempt_enable();
333 return ret;
334}
335EXPORT_SYMBOL_GPL(mnt_want_write);
336
337/**
338 * mnt_clone_write - get write access to a mount
339 * @mnt: the mount on which to take a write
340 *
341 * This is effectively like mnt_want_write, except
342 * it must only be used to take an extra write reference
343 * on a mountpoint that we already know has a write reference
344 * on it. This allows some optimisation.
345 *
346 * After finished, mnt_drop_write must be called as usual to
347 * drop the reference.
348 */
349int mnt_clone_write(struct vfsmount *mnt)
350{
351 /* superblock may be r/o */
352 if (__mnt_is_readonly(mnt))
353 return -EROFS;
354 preempt_disable();
355 mnt_inc_writers(real_mount(mnt));
356 preempt_enable();
357 return 0;
358}
359EXPORT_SYMBOL_GPL(mnt_clone_write);
360
361/**
362 * mnt_want_write_file - get write access to a file's mount
363 * @file: the file who's mount on which to take a write
364 *
365 * This is like mnt_want_write, but it takes a file and can
366 * do some optimisations if the file is open for write already
367 */
368int mnt_want_write_file(struct file *file)
369{
370 struct inode *inode = file->f_dentry->d_inode;
371 if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
372 return mnt_want_write(file->f_path.mnt);
373 else
374 return mnt_clone_write(file->f_path.mnt);
375}
376EXPORT_SYMBOL_GPL(mnt_want_write_file);
377
378/**
379 * mnt_drop_write - give up write access to a mount
380 * @mnt: the mount on which to give up write access
381 *
382 * Tells the low-level filesystem that we are done
383 * performing writes to it. Must be matched with
384 * mnt_want_write() call above.
385 */
386void mnt_drop_write(struct vfsmount *mnt)
387{
388 preempt_disable();
389 mnt_dec_writers(real_mount(mnt));
390 preempt_enable();
391}
392EXPORT_SYMBOL_GPL(mnt_drop_write);
393
394void mnt_drop_write_file(struct file *file)
395{
396 mnt_drop_write(file->f_path.mnt);
397}
398EXPORT_SYMBOL(mnt_drop_write_file);
399
400static int mnt_make_readonly(struct mount *mnt)
401{
402 int ret = 0;
403
404 br_write_lock(vfsmount_lock);
405 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
406 /*
407 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
408 * should be visible before we do.
409 */
410 smp_mb();
411
412 /*
413 * With writers on hold, if this value is zero, then there are
414 * definitely no active writers (although held writers may subsequently
415 * increment the count, they'll have to wait, and decrement it after
416 * seeing MNT_READONLY).
417 *
418 * It is OK to have counter incremented on one CPU and decremented on
419 * another: the sum will add up correctly. The danger would be when we
420 * sum up each counter, if we read a counter before it is incremented,
421 * but then read another CPU's count which it has been subsequently
422 * decremented from -- we would see more decrements than we should.
423 * MNT_WRITE_HOLD protects against this scenario, because
424 * mnt_want_write first increments count, then smp_mb, then spins on
425 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
426 * we're counting up here.
427 */
428 if (mnt_get_writers(mnt) > 0)
429 ret = -EBUSY;
430 else
431 mnt->mnt.mnt_flags |= MNT_READONLY;
432 /*
433 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
434 * that become unheld will see MNT_READONLY.
435 */
436 smp_wmb();
437 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
438 br_write_unlock(vfsmount_lock);
439 return ret;
440}
441
442static void __mnt_unmake_readonly(struct mount *mnt)
443{
444 br_write_lock(vfsmount_lock);
445 mnt->mnt.mnt_flags &= ~MNT_READONLY;
446 br_write_unlock(vfsmount_lock);
447}
448
449int sb_prepare_remount_readonly(struct super_block *sb)
450{
451 struct mount *mnt;
452 int err = 0;
453
454 /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
455 if (atomic_long_read(&sb->s_remove_count))
456 return -EBUSY;
457
458 br_write_lock(vfsmount_lock);
459 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
460 if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
461 mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
462 smp_mb();
463 if (mnt_get_writers(mnt) > 0) {
464 err = -EBUSY;
465 break;
466 }
467 }
468 }
469 if (!err && atomic_long_read(&sb->s_remove_count))
470 err = -EBUSY;
471
472 if (!err) {
473 sb->s_readonly_remount = 1;
474 smp_wmb();
475 }
476 list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
477 if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
478 mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
479 }
480 br_write_unlock(vfsmount_lock);
481
482 return err;
483}
484
485static void free_vfsmnt(struct mount *mnt)
486{
487 kfree(mnt->mnt_devname);
488 mnt_free_id(mnt);
489#ifdef CONFIG_SMP
490 free_percpu(mnt->mnt_pcp);
491#endif
492 kmem_cache_free(mnt_cache, mnt);
493}
494
495/*
496 * find the first or last mount at @dentry on vfsmount @mnt depending on
497 * @dir. If @dir is set return the first mount else return the last mount.
498 * vfsmount_lock must be held for read or write.
499 */
500struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
501 int dir)
502{
503 struct list_head *head = mount_hashtable + hash(mnt, dentry);
504 struct list_head *tmp = head;
505 struct mount *p, *found = NULL;
506
507 for (;;) {
508 tmp = dir ? tmp->next : tmp->prev;
509 p = NULL;
510 if (tmp == head)
511 break;
512 p = list_entry(tmp, struct mount, mnt_hash);
513 if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
514 found = p;
515 break;
516 }
517 }
518 return found;
519}
520
521/*
522 * lookup_mnt increments the ref count before returning
523 * the vfsmount struct.
524 */
525struct vfsmount *lookup_mnt(struct path *path)
526{
527 struct mount *child_mnt;
528
529 br_read_lock(vfsmount_lock);
530 child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
531 if (child_mnt) {
532 mnt_add_count(child_mnt, 1);
533 br_read_unlock(vfsmount_lock);
534 return &child_mnt->mnt;
535 } else {
536 br_read_unlock(vfsmount_lock);
537 return NULL;
538 }
539}
540
541static inline int check_mnt(struct mount *mnt)
542{
543 return mnt->mnt_ns == current->nsproxy->mnt_ns;
544}
545
546/*
547 * vfsmount lock must be held for write
548 */
549static void touch_mnt_namespace(struct mnt_namespace *ns)
550{
551 if (ns) {
552 ns->event = ++event;
553 wake_up_interruptible(&ns->poll);
554 }
555}
556
557/*
558 * vfsmount lock must be held for write
559 */
560static void __touch_mnt_namespace(struct mnt_namespace *ns)
561{
562 if (ns && ns->event != event) {
563 ns->event = event;
564 wake_up_interruptible(&ns->poll);
565 }
566}
567
568/*
569 * Clear dentry's mounted state if it has no remaining mounts.
570 * vfsmount_lock must be held for write.
571 */
572static void dentry_reset_mounted(struct dentry *dentry)
573{
574 unsigned u;
575
576 for (u = 0; u < HASH_SIZE; u++) {
577 struct mount *p;
578
579 list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
580 if (p->mnt_mountpoint == dentry)
581 return;
582 }
583 }
584 spin_lock(&dentry->d_lock);
585 dentry->d_flags &= ~DCACHE_MOUNTED;
586 spin_unlock(&dentry->d_lock);
587}
588
589/*
590 * vfsmount lock must be held for write
591 */
592static void detach_mnt(struct mount *mnt, struct path *old_path)
593{
594 old_path->dentry = mnt->mnt_mountpoint;
595 old_path->mnt = &mnt->mnt_parent->mnt;
596 mnt->mnt_parent = mnt;
597 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
598 list_del_init(&mnt->mnt_child);
599 list_del_init(&mnt->mnt_hash);
600 dentry_reset_mounted(old_path->dentry);
601}
602
603/*
604 * vfsmount lock must be held for write
605 */
606void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry,
607 struct mount *child_mnt)
608{
609 mnt_add_count(mnt, 1); /* essentially, that's mntget */
610 child_mnt->mnt_mountpoint = dget(dentry);
611 child_mnt->mnt_parent = mnt;
612 spin_lock(&dentry->d_lock);
613 dentry->d_flags |= DCACHE_MOUNTED;
614 spin_unlock(&dentry->d_lock);
615}
616
617/*
618 * vfsmount lock must be held for write
619 */
620static void attach_mnt(struct mount *mnt, struct path *path)
621{
622 mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt);
623 list_add_tail(&mnt->mnt_hash, mount_hashtable +
624 hash(path->mnt, path->dentry));
625 list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts);
626}
627
628static inline void __mnt_make_longterm(struct mount *mnt)
629{
630#ifdef CONFIG_SMP
631 atomic_inc(&mnt->mnt_longterm);
632#endif
633}
634
635/* needs vfsmount lock for write */
636static inline void __mnt_make_shortterm(struct mount *mnt)
637{
638#ifdef CONFIG_SMP
639 atomic_dec(&mnt->mnt_longterm);
640#endif
641}
642
643/*
644 * vfsmount lock must be held for write
645 */
646static void commit_tree(struct mount *mnt)
647{
648 struct mount *parent = mnt->mnt_parent;
649 struct mount *m;
650 LIST_HEAD(head);
651 struct mnt_namespace *n = parent->mnt_ns;
652
653 BUG_ON(parent == mnt);
654
655 list_add_tail(&head, &mnt->mnt_list);
656 list_for_each_entry(m, &head, mnt_list) {
657 m->mnt_ns = n;
658 __mnt_make_longterm(m);
659 }
660
661 list_splice(&head, n->list.prev);
662
663 list_add_tail(&mnt->mnt_hash, mount_hashtable +
664 hash(&parent->mnt, mnt->mnt_mountpoint));
665 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
666 touch_mnt_namespace(n);
667}
668
669static struct mount *next_mnt(struct mount *p, struct mount *root)
670{
671 struct list_head *next = p->mnt_mounts.next;
672 if (next == &p->mnt_mounts) {
673 while (1) {
674 if (p == root)
675 return NULL;
676 next = p->mnt_child.next;
677 if (next != &p->mnt_parent->mnt_mounts)
678 break;
679 p = p->mnt_parent;
680 }
681 }
682 return list_entry(next, struct mount, mnt_child);
683}
684
685static struct mount *skip_mnt_tree(struct mount *p)
686{
687 struct list_head *prev = p->mnt_mounts.prev;
688 while (prev != &p->mnt_mounts) {
689 p = list_entry(prev, struct mount, mnt_child);
690 prev = p->mnt_mounts.prev;
691 }
692 return p;
693}
694
695struct vfsmount *
696vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
697{
698 struct mount *mnt;
699 struct dentry *root;
700
701 if (!type)
702 return ERR_PTR(-ENODEV);
703
704 mnt = alloc_vfsmnt(name);
705 if (!mnt)
706 return ERR_PTR(-ENOMEM);
707
708 if (flags & MS_KERNMOUNT)
709 mnt->mnt.mnt_flags = MNT_INTERNAL;
710
711 root = mount_fs(type, flags, name, data);
712 if (IS_ERR(root)) {
713 free_vfsmnt(mnt);
714 return ERR_CAST(root);
715 }
716
717 mnt->mnt.mnt_root = root;
718 mnt->mnt.mnt_sb = root->d_sb;
719 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
720 mnt->mnt_parent = mnt;
721 br_write_lock(vfsmount_lock);
722 list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
723 br_write_unlock(vfsmount_lock);
724 return &mnt->mnt;
725}
726EXPORT_SYMBOL_GPL(vfs_kern_mount);
727
728static struct mount *clone_mnt(struct mount *old, struct dentry *root,
729 int flag)
730{
731 struct super_block *sb = old->mnt.mnt_sb;
732 struct mount *mnt = alloc_vfsmnt(old->mnt_devname);
733
734 if (mnt) {
735 if (flag & (CL_SLAVE | CL_PRIVATE))
736 mnt->mnt_group_id = 0; /* not a peer of original */
737 else
738 mnt->mnt_group_id = old->mnt_group_id;
739
740 if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
741 int err = mnt_alloc_group_id(mnt);
742 if (err)
743 goto out_free;
744 }
745
746 mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
747 atomic_inc(&sb->s_active);
748 mnt->mnt.mnt_sb = sb;
749 mnt->mnt.mnt_root = dget(root);
750 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
751 mnt->mnt_parent = mnt;
752 br_write_lock(vfsmount_lock);
753 list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
754 br_write_unlock(vfsmount_lock);
755
756 if (flag & CL_SLAVE) {
757 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
758 mnt->mnt_master = old;
759 CLEAR_MNT_SHARED(mnt);
760 } else if (!(flag & CL_PRIVATE)) {
761 if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
762 list_add(&mnt->mnt_share, &old->mnt_share);
763 if (IS_MNT_SLAVE(old))
764 list_add(&mnt->mnt_slave, &old->mnt_slave);
765 mnt->mnt_master = old->mnt_master;
766 }
767 if (flag & CL_MAKE_SHARED)
768 set_mnt_shared(mnt);
769
770 /* stick the duplicate mount on the same expiry list
771 * as the original if that was on one */
772 if (flag & CL_EXPIRE) {
773 if (!list_empty(&old->mnt_expire))
774 list_add(&mnt->mnt_expire, &old->mnt_expire);
775 }
776 }
777 return mnt;
778
779 out_free:
780 free_vfsmnt(mnt);
781 return NULL;
782}
783
784static inline void mntfree(struct mount *mnt)
785{
786 struct vfsmount *m = &mnt->mnt;
787 struct super_block *sb = m->mnt_sb;
788
789 /*
790 * This probably indicates that somebody messed
791 * up a mnt_want/drop_write() pair. If this
792 * happens, the filesystem was probably unable
793 * to make r/w->r/o transitions.
794 */
795 /*
796 * The locking used to deal with mnt_count decrement provides barriers,
797 * so mnt_get_writers() below is safe.
798 */
799 WARN_ON(mnt_get_writers(mnt));
800 fsnotify_vfsmount_delete(m);
801 dput(m->mnt_root);
802 free_vfsmnt(mnt);
803 deactivate_super(sb);
804}
805
806static void mntput_no_expire(struct mount *mnt)
807{
808put_again:
809#ifdef CONFIG_SMP
810 br_read_lock(vfsmount_lock);
811 if (likely(atomic_read(&mnt->mnt_longterm))) {
812 mnt_add_count(mnt, -1);
813 br_read_unlock(vfsmount_lock);
814 return;
815 }
816 br_read_unlock(vfsmount_lock);
817
818 br_write_lock(vfsmount_lock);
819 mnt_add_count(mnt, -1);
820 if (mnt_get_count(mnt)) {
821 br_write_unlock(vfsmount_lock);
822 return;
823 }
824#else
825 mnt_add_count(mnt, -1);
826 if (likely(mnt_get_count(mnt)))
827 return;
828 br_write_lock(vfsmount_lock);
829#endif
830 if (unlikely(mnt->mnt_pinned)) {
831 mnt_add_count(mnt, mnt->mnt_pinned + 1);
832 mnt->mnt_pinned = 0;
833 br_write_unlock(vfsmount_lock);
834 acct_auto_close_mnt(&mnt->mnt);
835 goto put_again;
836 }
837 list_del(&mnt->mnt_instance);
838 br_write_unlock(vfsmount_lock);
839 mntfree(mnt);
840}
841
842void mntput(struct vfsmount *mnt)
843{
844 if (mnt) {
845 struct mount *m = real_mount(mnt);
846 /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
847 if (unlikely(m->mnt_expiry_mark))
848 m->mnt_expiry_mark = 0;
849 mntput_no_expire(m);
850 }
851}
852EXPORT_SYMBOL(mntput);
853
854struct vfsmount *mntget(struct vfsmount *mnt)
855{
856 if (mnt)
857 mnt_add_count(real_mount(mnt), 1);
858 return mnt;
859}
860EXPORT_SYMBOL(mntget);
861
862void mnt_pin(struct vfsmount *mnt)
863{
864 br_write_lock(vfsmount_lock);
865 real_mount(mnt)->mnt_pinned++;
866 br_write_unlock(vfsmount_lock);
867}
868EXPORT_SYMBOL(mnt_pin);
869
870void mnt_unpin(struct vfsmount *m)
871{
872 struct mount *mnt = real_mount(m);
873 br_write_lock(vfsmount_lock);
874 if (mnt->mnt_pinned) {
875 mnt_add_count(mnt, 1);
876 mnt->mnt_pinned--;
877 }
878 br_write_unlock(vfsmount_lock);
879}
880EXPORT_SYMBOL(mnt_unpin);
881
882static inline void mangle(struct seq_file *m, const char *s)
883{
884 seq_escape(m, s, " \t\n\\");
885}
886
887/*
888 * Simple .show_options callback for filesystems which don't want to
889 * implement more complex mount option showing.
890 *
891 * See also save_mount_options().
892 */
893int generic_show_options(struct seq_file *m, struct dentry *root)
894{
895 const char *options;
896
897 rcu_read_lock();
898 options = rcu_dereference(root->d_sb->s_options);
899
900 if (options != NULL && options[0]) {
901 seq_putc(m, ',');
902 mangle(m, options);
903 }
904 rcu_read_unlock();
905
906 return 0;
907}
908EXPORT_SYMBOL(generic_show_options);
909
910/*
911 * If filesystem uses generic_show_options(), this function should be
912 * called from the fill_super() callback.
913 *
914 * The .remount_fs callback usually needs to be handled in a special
915 * way, to make sure, that previous options are not overwritten if the
916 * remount fails.
917 *
918 * Also note, that if the filesystem's .remount_fs function doesn't
919 * reset all options to their default value, but changes only newly
920 * given options, then the displayed options will not reflect reality
921 * any more.
922 */
923void save_mount_options(struct super_block *sb, char *options)
924{
925 BUG_ON(sb->s_options);
926 rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
927}
928EXPORT_SYMBOL(save_mount_options);
929
930void replace_mount_options(struct super_block *sb, char *options)
931{
932 char *old = sb->s_options;
933 rcu_assign_pointer(sb->s_options, options);
934 if (old) {
935 synchronize_rcu();
936 kfree(old);
937 }
938}
939EXPORT_SYMBOL(replace_mount_options);
940
941#ifdef CONFIG_PROC_FS
942/* iterator; we want it to have access to namespace_sem, thus here... */
943static void *m_start(struct seq_file *m, loff_t *pos)
944{
945 struct proc_mounts *p = container_of(m, struct proc_mounts, m);
946
947 down_read(&namespace_sem);
948 return seq_list_start(&p->ns->list, *pos);
949}
950
951static void *m_next(struct seq_file *m, void *v, loff_t *pos)
952{
953 struct proc_mounts *p = container_of(m, struct proc_mounts, m);
954
955 return seq_list_next(v, &p->ns->list, pos);
956}
957
958static void m_stop(struct seq_file *m, void *v)
959{
960 up_read(&namespace_sem);
961}
962
963static int m_show(struct seq_file *m, void *v)
964{
965 struct proc_mounts *p = container_of(m, struct proc_mounts, m);
966 struct mount *r = list_entry(v, struct mount, mnt_list);
967 return p->show(m, &r->mnt);
968}
969
970const struct seq_operations mounts_op = {
971 .start = m_start,
972 .next = m_next,
973 .stop = m_stop,
974 .show = m_show,
975};
976#endif /* CONFIG_PROC_FS */
977
978/**
979 * may_umount_tree - check if a mount tree is busy
980 * @mnt: root of mount tree
981 *
982 * This is called to check if a tree of mounts has any
983 * open files, pwds, chroots or sub mounts that are
984 * busy.
985 */
986int may_umount_tree(struct vfsmount *m)
987{
988 struct mount *mnt = real_mount(m);
989 int actual_refs = 0;
990 int minimum_refs = 0;
991 struct mount *p;
992 BUG_ON(!m);
993
994 /* write lock needed for mnt_get_count */
995 br_write_lock(vfsmount_lock);
996 for (p = mnt; p; p = next_mnt(p, mnt)) {
997 actual_refs += mnt_get_count(p);
998 minimum_refs += 2;
999 }
1000 br_write_unlock(vfsmount_lock);
1001
1002 if (actual_refs > minimum_refs)
1003 return 0;
1004
1005 return 1;
1006}
1007
1008EXPORT_SYMBOL(may_umount_tree);
1009
1010/**
1011 * may_umount - check if a mount point is busy
1012 * @mnt: root of mount
1013 *
1014 * This is called to check if a mount point has any
1015 * open files, pwds, chroots or sub mounts. If the
1016 * mount has sub mounts this will return busy
1017 * regardless of whether the sub mounts are busy.
1018 *
1019 * Doesn't take quota and stuff into account. IOW, in some cases it will
1020 * give false negatives. The main reason why it's here is that we need
1021 * a non-destructive way to look for easily umountable filesystems.
1022 */
1023int may_umount(struct vfsmount *mnt)
1024{
1025 int ret = 1;
1026 down_read(&namespace_sem);
1027 br_write_lock(vfsmount_lock);
1028 if (propagate_mount_busy(real_mount(mnt), 2))
1029 ret = 0;
1030 br_write_unlock(vfsmount_lock);
1031 up_read(&namespace_sem);
1032 return ret;
1033}
1034
1035EXPORT_SYMBOL(may_umount);
1036
1037void release_mounts(struct list_head *head)
1038{
1039 struct mount *mnt;
1040 while (!list_empty(head)) {
1041 mnt = list_first_entry(head, struct mount, mnt_hash);
1042 list_del_init(&mnt->mnt_hash);
1043 if (mnt_has_parent(mnt)) {
1044 struct dentry *dentry;
1045 struct mount *m;
1046
1047 br_write_lock(vfsmount_lock);
1048 dentry = mnt->mnt_mountpoint;
1049 m = mnt->mnt_parent;
1050 mnt->mnt_mountpoint = mnt->mnt.mnt_root;
1051 mnt->mnt_parent = mnt;
1052 m->mnt_ghosts--;
1053 br_write_unlock(vfsmount_lock);
1054 dput(dentry);
1055 mntput(&m->mnt);
1056 }
1057 mntput(&mnt->mnt);
1058 }
1059}
1060
1061/*
1062 * vfsmount lock must be held for write
1063 * namespace_sem must be held for write
1064 */
1065void umount_tree(struct mount *mnt, int propagate, struct list_head *kill)
1066{
1067 LIST_HEAD(tmp_list);
1068 struct mount *p;
1069
1070 for (p = mnt; p; p = next_mnt(p, mnt))
1071 list_move(&p->mnt_hash, &tmp_list);
1072
1073 list_for_each_entry(p, &tmp_list, mnt_hash)
1074 list_del_init(&p->mnt_child);
1075
1076 if (propagate)
1077 propagate_umount(&tmp_list);
1078
1079 list_for_each_entry(p, &tmp_list, mnt_hash) {
1080 list_del_init(&p->mnt_expire);
1081 list_del_init(&p->mnt_list);
1082 __touch_mnt_namespace(p->mnt_ns);
1083 if (p->mnt_ns)
1084 __mnt_make_shortterm(p);
1085 p->mnt_ns = NULL;
1086 if (mnt_has_parent(p)) {
1087 p->mnt_parent->mnt_ghosts++;
1088 dentry_reset_mounted(p->mnt_mountpoint);
1089 }
1090 change_mnt_propagation(p, MS_PRIVATE);
1091 }
1092 list_splice(&tmp_list, kill);
1093}
1094
1095static void shrink_submounts(struct mount *mnt, struct list_head *umounts);
1096
1097static int do_umount(struct mount *mnt, int flags)
1098{
1099 struct super_block *sb = mnt->mnt.mnt_sb;
1100 int retval;
1101 LIST_HEAD(umount_list);
1102
1103 retval = security_sb_umount(&mnt->mnt, flags);
1104 if (retval)
1105 return retval;
1106
1107 /*
1108 * Allow userspace to request a mountpoint be expired rather than
1109 * unmounting unconditionally. Unmount only happens if:
1110 * (1) the mark is already set (the mark is cleared by mntput())
1111 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1112 */
1113 if (flags & MNT_EXPIRE) {
1114 if (&mnt->mnt == current->fs->root.mnt ||
1115 flags & (MNT_FORCE | MNT_DETACH))
1116 return -EINVAL;
1117
1118 /*
1119 * probably don't strictly need the lock here if we examined
1120 * all race cases, but it's a slowpath.
1121 */
1122 br_write_lock(vfsmount_lock);
1123 if (mnt_get_count(mnt) != 2) {
1124 br_write_unlock(vfsmount_lock);
1125 return -EBUSY;
1126 }
1127 br_write_unlock(vfsmount_lock);
1128
1129 if (!xchg(&mnt->mnt_expiry_mark, 1))
1130 return -EAGAIN;
1131 }
1132
1133 /*
1134 * If we may have to abort operations to get out of this
1135 * mount, and they will themselves hold resources we must
1136 * allow the fs to do things. In the Unix tradition of
1137 * 'Gee thats tricky lets do it in userspace' the umount_begin
1138 * might fail to complete on the first run through as other tasks
1139 * must return, and the like. Thats for the mount program to worry
1140 * about for the moment.
1141 */
1142
1143 if (flags & MNT_FORCE && sb->s_op->umount_begin) {
1144 sb->s_op->umount_begin(sb);
1145 }
1146
1147 /*
1148 * No sense to grab the lock for this test, but test itself looks
1149 * somewhat bogus. Suggestions for better replacement?
1150 * Ho-hum... In principle, we might treat that as umount + switch
1151 * to rootfs. GC would eventually take care of the old vfsmount.
1152 * Actually it makes sense, especially if rootfs would contain a
1153 * /reboot - static binary that would close all descriptors and
1154 * call reboot(9). Then init(8) could umount root and exec /reboot.
1155 */
1156 if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
1157 /*
1158 * Special case for "unmounting" root ...
1159 * we just try to remount it readonly.
1160 */
1161 down_write(&sb->s_umount);
1162 if (!(sb->s_flags & MS_RDONLY))
1163 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
1164 up_write(&sb->s_umount);
1165 return retval;
1166 }
1167
1168 down_write(&namespace_sem);
1169 br_write_lock(vfsmount_lock);
1170 event++;
1171
1172 if (!(flags & MNT_DETACH))
1173 shrink_submounts(mnt, &umount_list);
1174
1175 retval = -EBUSY;
1176 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
1177 if (!list_empty(&mnt->mnt_list))
1178 umount_tree(mnt, 1, &umount_list);
1179 retval = 0;
1180 }
1181 br_write_unlock(vfsmount_lock);
1182 up_write(&namespace_sem);
1183 release_mounts(&umount_list);
1184 return retval;
1185}
1186
1187/*
1188 * Now umount can handle mount points as well as block devices.
1189 * This is important for filesystems which use unnamed block devices.
1190 *
1191 * We now support a flag for forced unmount like the other 'big iron'
1192 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1193 */
1194
1195SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
1196{
1197 struct path path;
1198 struct mount *mnt;
1199 int retval;
1200 int lookup_flags = 0;
1201
1202 if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
1203 return -EINVAL;
1204
1205 if (!(flags & UMOUNT_NOFOLLOW))
1206 lookup_flags |= LOOKUP_FOLLOW;
1207
1208 retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
1209 if (retval)
1210 goto out;
1211 mnt = real_mount(path.mnt);
1212 retval = -EINVAL;
1213 if (path.dentry != path.mnt->mnt_root)
1214 goto dput_and_out;
1215 if (!check_mnt(mnt))
1216 goto dput_and_out;
1217
1218 retval = -EPERM;
1219 if (!capable(CAP_SYS_ADMIN))
1220 goto dput_and_out;
1221
1222 retval = do_umount(mnt, flags);
1223dput_and_out:
1224 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
1225 dput(path.dentry);
1226 mntput_no_expire(mnt);
1227out:
1228 return retval;
1229}
1230
1231#ifdef __ARCH_WANT_SYS_OLDUMOUNT
1232
1233/*
1234 * The 2.0 compatible umount. No flags.
1235 */
1236SYSCALL_DEFINE1(oldumount, char __user *, name)
1237{
1238 return sys_umount(name, 0);
1239}
1240
1241#endif
1242
1243static int mount_is_safe(struct path *path)
1244{
1245 if (capable(CAP_SYS_ADMIN))
1246 return 0;
1247 return -EPERM;
1248#ifdef notyet
1249 if (S_ISLNK(path->dentry->d_inode->i_mode))
1250 return -EPERM;
1251 if (path->dentry->d_inode->i_mode & S_ISVTX) {
1252 if (current_uid() != path->dentry->d_inode->i_uid)
1253 return -EPERM;
1254 }
1255 if (inode_permission(path->dentry->d_inode, MAY_WRITE))
1256 return -EPERM;
1257 return 0;
1258#endif
1259}
1260
1261struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
1262 int flag)
1263{
1264 struct mount *res, *p, *q, *r;
1265 struct path path;
1266
1267 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
1268 return NULL;
1269
1270 res = q = clone_mnt(mnt, dentry, flag);
1271 if (!q)
1272 goto Enomem;
1273 q->mnt_mountpoint = mnt->mnt_mountpoint;
1274
1275 p = mnt;
1276 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1277 struct mount *s;
1278 if (!is_subdir(r->mnt_mountpoint, dentry))
1279 continue;
1280
1281 for (s = r; s; s = next_mnt(s, r)) {
1282 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
1283 s = skip_mnt_tree(s);
1284 continue;
1285 }
1286 while (p != s->mnt_parent) {
1287 p = p->mnt_parent;
1288 q = q->mnt_parent;
1289 }
1290 p = s;
1291 path.mnt = &q->mnt;
1292 path.dentry = p->mnt_mountpoint;
1293 q = clone_mnt(p, p->mnt.mnt_root, flag);
1294 if (!q)
1295 goto Enomem;
1296 br_write_lock(vfsmount_lock);
1297 list_add_tail(&q->mnt_list, &res->mnt_list);
1298 attach_mnt(q, &path);
1299 br_write_unlock(vfsmount_lock);
1300 }
1301 }
1302 return res;
1303Enomem:
1304 if (res) {
1305 LIST_HEAD(umount_list);
1306 br_write_lock(vfsmount_lock);
1307 umount_tree(res, 0, &umount_list);
1308 br_write_unlock(vfsmount_lock);
1309 release_mounts(&umount_list);
1310 }
1311 return NULL;
1312}
1313
1314struct vfsmount *collect_mounts(struct path *path)
1315{
1316 struct mount *tree;
1317 down_write(&namespace_sem);
1318 tree = copy_tree(real_mount(path->mnt), path->dentry,
1319 CL_COPY_ALL | CL_PRIVATE);
1320 up_write(&namespace_sem);
1321 return tree ? &tree->mnt : NULL;
1322}
1323
1324void drop_collected_mounts(struct vfsmount *mnt)
1325{
1326 LIST_HEAD(umount_list);
1327 down_write(&namespace_sem);
1328 br_write_lock(vfsmount_lock);
1329 umount_tree(real_mount(mnt), 0, &umount_list);
1330 br_write_unlock(vfsmount_lock);
1331 up_write(&namespace_sem);
1332 release_mounts(&umount_list);
1333}
1334
1335int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
1336 struct vfsmount *root)
1337{
1338 struct mount *mnt;
1339 int res = f(root, arg);
1340 if (res)
1341 return res;
1342 list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
1343 res = f(&mnt->mnt, arg);
1344 if (res)
1345 return res;
1346 }
1347 return 0;
1348}
1349
1350static void cleanup_group_ids(struct mount *mnt, struct mount *end)
1351{
1352 struct mount *p;
1353
1354 for (p = mnt; p != end; p = next_mnt(p, mnt)) {
1355 if (p->mnt_group_id && !IS_MNT_SHARED(p))
1356 mnt_release_group_id(p);
1357 }
1358}
1359
1360static int invent_group_ids(struct mount *mnt, bool recurse)
1361{
1362 struct mount *p;
1363
1364 for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
1365 if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
1366 int err = mnt_alloc_group_id(p);
1367 if (err) {
1368 cleanup_group_ids(mnt, p);
1369 return err;
1370 }
1371 }
1372 }
1373
1374 return 0;
1375}
1376
1377/*
1378 * @source_mnt : mount tree to be attached
1379 * @nd : place the mount tree @source_mnt is attached
1380 * @parent_nd : if non-null, detach the source_mnt from its parent and
1381 * store the parent mount and mountpoint dentry.
1382 * (done when source_mnt is moved)
1383 *
1384 * NOTE: in the table below explains the semantics when a source mount
1385 * of a given type is attached to a destination mount of a given type.
1386 * ---------------------------------------------------------------------------
1387 * | BIND MOUNT OPERATION |
1388 * |**************************************************************************
1389 * | source-->| shared | private | slave | unbindable |
1390 * | dest | | | | |
1391 * | | | | | | |
1392 * | v | | | | |
1393 * |**************************************************************************
1394 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1395 * | | | | | |
1396 * |non-shared| shared (+) | private | slave (*) | invalid |
1397 * ***************************************************************************
1398 * A bind operation clones the source mount and mounts the clone on the
1399 * destination mount.
1400 *
1401 * (++) the cloned mount is propagated to all the mounts in the propagation
1402 * tree of the destination mount and the cloned mount is added to
1403 * the peer group of the source mount.
1404 * (+) the cloned mount is created under the destination mount and is marked
1405 * as shared. The cloned mount is added to the peer group of the source
1406 * mount.
1407 * (+++) the mount is propagated to all the mounts in the propagation tree
1408 * of the destination mount and the cloned mount is made slave
1409 * of the same master as that of the source mount. The cloned mount
1410 * is marked as 'shared and slave'.
1411 * (*) the cloned mount is made a slave of the same master as that of the
1412 * source mount.
1413 *
1414 * ---------------------------------------------------------------------------
1415 * | MOVE MOUNT OPERATION |
1416 * |**************************************************************************
1417 * | source-->| shared | private | slave | unbindable |
1418 * | dest | | | | |
1419 * | | | | | | |
1420 * | v | | | | |
1421 * |**************************************************************************
1422 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1423 * | | | | | |
1424 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1425 * ***************************************************************************
1426 *
1427 * (+) the mount is moved to the destination. And is then propagated to
1428 * all the mounts in the propagation tree of the destination mount.
1429 * (+*) the mount is moved to the destination.
1430 * (+++) the mount is moved to the destination and is then propagated to
1431 * all the mounts belonging to the destination mount's propagation tree.
1432 * the mount is marked as 'shared and slave'.
1433 * (*) the mount continues to be a slave at the new location.
1434 *
1435 * if the source mount is a tree, the operations explained above is
1436 * applied to each mount in the tree.
1437 * Must be called without spinlocks held, since this function can sleep
1438 * in allocations.
1439 */
1440static int attach_recursive_mnt(struct mount *source_mnt,
1441 struct path *path, struct path *parent_path)
1442{
1443 LIST_HEAD(tree_list);
1444 struct mount *dest_mnt = real_mount(path->mnt);
1445 struct dentry *dest_dentry = path->dentry;
1446 struct mount *child, *p;
1447 int err;
1448
1449 if (IS_MNT_SHARED(dest_mnt)) {
1450 err = invent_group_ids(source_mnt, true);
1451 if (err)
1452 goto out;
1453 }
1454 err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
1455 if (err)
1456 goto out_cleanup_ids;
1457
1458 br_write_lock(vfsmount_lock);
1459
1460 if (IS_MNT_SHARED(dest_mnt)) {
1461 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
1462 set_mnt_shared(p);
1463 }
1464 if (parent_path) {
1465 detach_mnt(source_mnt, parent_path);
1466 attach_mnt(source_mnt, path);
1467 touch_mnt_namespace(source_mnt->mnt_ns);
1468 } else {
1469 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
1470 commit_tree(source_mnt);
1471 }
1472
1473 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
1474 list_del_init(&child->mnt_hash);
1475 commit_tree(child);
1476 }
1477 br_write_unlock(vfsmount_lock);
1478
1479 return 0;
1480
1481 out_cleanup_ids:
1482 if (IS_MNT_SHARED(dest_mnt))
1483 cleanup_group_ids(source_mnt, NULL);
1484 out:
1485 return err;
1486}
1487
1488static int lock_mount(struct path *path)
1489{
1490 struct vfsmount *mnt;
1491retry:
1492 mutex_lock(&path->dentry->d_inode->i_mutex);
1493 if (unlikely(cant_mount(path->dentry))) {
1494 mutex_unlock(&path->dentry->d_inode->i_mutex);
1495 return -ENOENT;
1496 }
1497 down_write(&namespace_sem);
1498 mnt = lookup_mnt(path);
1499 if (likely(!mnt))
1500 return 0;
1501 up_write(&namespace_sem);
1502 mutex_unlock(&path->dentry->d_inode->i_mutex);
1503 path_put(path);
1504 path->mnt = mnt;
1505 path->dentry = dget(mnt->mnt_root);
1506 goto retry;
1507}
1508
1509static void unlock_mount(struct path *path)
1510{
1511 up_write(&namespace_sem);
1512 mutex_unlock(&path->dentry->d_inode->i_mutex);
1513}
1514
1515static int graft_tree(struct mount *mnt, struct path *path)
1516{
1517 if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
1518 return -EINVAL;
1519
1520 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1521 S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
1522 return -ENOTDIR;
1523
1524 if (d_unlinked(path->dentry))
1525 return -ENOENT;
1526
1527 return attach_recursive_mnt(mnt, path, NULL);
1528}
1529
1530/*
1531 * Sanity check the flags to change_mnt_propagation.
1532 */
1533
1534static int flags_to_propagation_type(int flags)
1535{
1536 int type = flags & ~(MS_REC | MS_SILENT);
1537
1538 /* Fail if any non-propagation flags are set */
1539 if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1540 return 0;
1541 /* Only one propagation flag should be set */
1542 if (!is_power_of_2(type))
1543 return 0;
1544 return type;
1545}
1546
1547/*
1548 * recursively change the type of the mountpoint.
1549 */
1550static int do_change_type(struct path *path, int flag)
1551{
1552 struct mount *m;
1553 struct mount *mnt = real_mount(path->mnt);
1554 int recurse = flag & MS_REC;
1555 int type;
1556 int err = 0;
1557
1558 if (!capable(CAP_SYS_ADMIN))
1559 return -EPERM;
1560
1561 if (path->dentry != path->mnt->mnt_root)
1562 return -EINVAL;
1563
1564 type = flags_to_propagation_type(flag);
1565 if (!type)
1566 return -EINVAL;
1567
1568 down_write(&namespace_sem);
1569 if (type == MS_SHARED) {
1570 err = invent_group_ids(mnt, recurse);
1571 if (err)
1572 goto out_unlock;
1573 }
1574
1575 br_write_lock(vfsmount_lock);
1576 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
1577 change_mnt_propagation(m, type);
1578 br_write_unlock(vfsmount_lock);
1579
1580 out_unlock:
1581 up_write(&namespace_sem);
1582 return err;
1583}
1584
1585/*
1586 * do loopback mount.
1587 */
1588static int do_loopback(struct path *path, char *old_name,
1589 int recurse)
1590{
1591 LIST_HEAD(umount_list);
1592 struct path old_path;
1593 struct mount *mnt = NULL, *old;
1594 int err = mount_is_safe(path);
1595 if (err)
1596 return err;
1597 if (!old_name || !*old_name)
1598 return -EINVAL;
1599 err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
1600 if (err)
1601 return err;
1602
1603 err = lock_mount(path);
1604 if (err)
1605 goto out;
1606
1607 old = real_mount(old_path.mnt);
1608
1609 err = -EINVAL;
1610 if (IS_MNT_UNBINDABLE(old))
1611 goto out2;
1612
1613 if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old))
1614 goto out2;
1615
1616 err = -ENOMEM;
1617 if (recurse)
1618 mnt = copy_tree(old, old_path.dentry, 0);
1619 else
1620 mnt = clone_mnt(old, old_path.dentry, 0);
1621
1622 if (!mnt)
1623 goto out2;
1624
1625 err = graft_tree(mnt, path);
1626 if (err) {
1627 br_write_lock(vfsmount_lock);
1628 umount_tree(mnt, 0, &umount_list);
1629 br_write_unlock(vfsmount_lock);
1630 }
1631out2:
1632 unlock_mount(path);
1633 release_mounts(&umount_list);
1634out:
1635 path_put(&old_path);
1636 return err;
1637}
1638
1639static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
1640{
1641 int error = 0;
1642 int readonly_request = 0;
1643
1644 if (ms_flags & MS_RDONLY)
1645 readonly_request = 1;
1646 if (readonly_request == __mnt_is_readonly(mnt))
1647 return 0;
1648
1649 if (readonly_request)
1650 error = mnt_make_readonly(real_mount(mnt));
1651 else
1652 __mnt_unmake_readonly(real_mount(mnt));
1653 return error;
1654}
1655
1656/*
1657 * change filesystem flags. dir should be a physical root of filesystem.
1658 * If you've mounted a non-root directory somewhere and want to do remount
1659 * on it - tough luck.
1660 */
1661static int do_remount(struct path *path, int flags, int mnt_flags,
1662 void *data)
1663{
1664 int err;
1665 struct super_block *sb = path->mnt->mnt_sb;
1666 struct mount *mnt = real_mount(path->mnt);
1667
1668 if (!capable(CAP_SYS_ADMIN))
1669 return -EPERM;
1670
1671 if (!check_mnt(mnt))
1672 return -EINVAL;
1673
1674 if (path->dentry != path->mnt->mnt_root)
1675 return -EINVAL;
1676
1677 err = security_sb_remount(sb, data);
1678 if (err)
1679 return err;
1680
1681 down_write(&sb->s_umount);
1682 if (flags & MS_BIND)
1683 err = change_mount_flags(path->mnt, flags);
1684 else
1685 err = do_remount_sb(sb, flags, data, 0);
1686 if (!err) {
1687 br_write_lock(vfsmount_lock);
1688 mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
1689 mnt->mnt.mnt_flags = mnt_flags;
1690 br_write_unlock(vfsmount_lock);
1691 }
1692 up_write(&sb->s_umount);
1693 if (!err) {
1694 br_write_lock(vfsmount_lock);
1695 touch_mnt_namespace(mnt->mnt_ns);
1696 br_write_unlock(vfsmount_lock);
1697 }
1698 return err;
1699}
1700
1701static inline int tree_contains_unbindable(struct mount *mnt)
1702{
1703 struct mount *p;
1704 for (p = mnt; p; p = next_mnt(p, mnt)) {
1705 if (IS_MNT_UNBINDABLE(p))
1706 return 1;
1707 }
1708 return 0;
1709}
1710
1711static int do_move_mount(struct path *path, char *old_name)
1712{
1713 struct path old_path, parent_path;
1714 struct mount *p;
1715 struct mount *old;
1716 int err = 0;
1717 if (!capable(CAP_SYS_ADMIN))
1718 return -EPERM;
1719 if (!old_name || !*old_name)
1720 return -EINVAL;
1721 err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
1722 if (err)
1723 return err;
1724
1725 err = lock_mount(path);
1726 if (err < 0)
1727 goto out;
1728
1729 old = real_mount(old_path.mnt);
1730 p = real_mount(path->mnt);
1731
1732 err = -EINVAL;
1733 if (!check_mnt(p) || !check_mnt(old))
1734 goto out1;
1735
1736 if (d_unlinked(path->dentry))
1737 goto out1;
1738
1739 err = -EINVAL;
1740 if (old_path.dentry != old_path.mnt->mnt_root)
1741 goto out1;
1742
1743 if (!mnt_has_parent(old))
1744 goto out1;
1745
1746 if (S_ISDIR(path->dentry->d_inode->i_mode) !=
1747 S_ISDIR(old_path.dentry->d_inode->i_mode))
1748 goto out1;
1749 /*
1750 * Don't move a mount residing in a shared parent.
1751 */
1752 if (IS_MNT_SHARED(old->mnt_parent))
1753 goto out1;
1754 /*
1755 * Don't move a mount tree containing unbindable mounts to a destination
1756 * mount which is shared.
1757 */
1758 if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
1759 goto out1;
1760 err = -ELOOP;
1761 for (; mnt_has_parent(p); p = p->mnt_parent)
1762 if (p == old)
1763 goto out1;
1764
1765 err = attach_recursive_mnt(old, path, &parent_path);
1766 if (err)
1767 goto out1;
1768
1769 /* if the mount is moved, it should no longer be expire
1770 * automatically */
1771 list_del_init(&old->mnt_expire);
1772out1:
1773 unlock_mount(path);
1774out:
1775 if (!err)
1776 path_put(&parent_path);
1777 path_put(&old_path);
1778 return err;
1779}
1780
1781static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
1782{
1783 int err;
1784 const char *subtype = strchr(fstype, '.');
1785 if (subtype) {
1786 subtype++;
1787 err = -EINVAL;
1788 if (!subtype[0])
1789 goto err;
1790 } else
1791 subtype = "";
1792
1793 mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
1794 err = -ENOMEM;
1795 if (!mnt->mnt_sb->s_subtype)
1796 goto err;
1797 return mnt;
1798
1799 err:
1800 mntput(mnt);
1801 return ERR_PTR(err);
1802}
1803
1804static struct vfsmount *
1805do_kern_mount(const char *fstype, int flags, const char *name, void *data)
1806{
1807 struct file_system_type *type = get_fs_type(fstype);
1808 struct vfsmount *mnt;
1809 if (!type)
1810 return ERR_PTR(-ENODEV);
1811 mnt = vfs_kern_mount(type, flags, name, data);
1812 if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
1813 !mnt->mnt_sb->s_subtype)
1814 mnt = fs_set_subtype(mnt, fstype);
1815 put_filesystem(type);
1816 return mnt;
1817}
1818
1819/*
1820 * add a mount into a namespace's mount tree
1821 */
1822static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
1823{
1824 int err;
1825
1826 mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
1827
1828 err = lock_mount(path);
1829 if (err)
1830 return err;
1831
1832 err = -EINVAL;
1833 if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt)))
1834 goto unlock;
1835
1836 /* Refuse the same filesystem on the same mount point */
1837 err = -EBUSY;
1838 if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
1839 path->mnt->mnt_root == path->dentry)
1840 goto unlock;
1841
1842 err = -EINVAL;
1843 if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
1844 goto unlock;
1845
1846 newmnt->mnt.mnt_flags = mnt_flags;
1847 err = graft_tree(newmnt, path);
1848
1849unlock:
1850 unlock_mount(path);
1851 return err;
1852}
1853
1854/*
1855 * create a new mount for userspace and request it to be added into the
1856 * namespace's tree
1857 */
1858static int do_new_mount(struct path *path, char *type, int flags,
1859 int mnt_flags, char *name, void *data)
1860{
1861 struct vfsmount *mnt;
1862 int err;
1863
1864 if (!type)
1865 return -EINVAL;
1866
1867 /* we need capabilities... */
1868 if (!capable(CAP_SYS_ADMIN))
1869 return -EPERM;
1870
1871 mnt = do_kern_mount(type, flags, name, data);
1872 if (IS_ERR(mnt))
1873 return PTR_ERR(mnt);
1874
1875 err = do_add_mount(real_mount(mnt), path, mnt_flags);
1876 if (err)
1877 mntput(mnt);
1878 return err;
1879}
1880
1881int finish_automount(struct vfsmount *m, struct path *path)
1882{
1883 struct mount *mnt = real_mount(m);
1884 int err;
1885 /* The new mount record should have at least 2 refs to prevent it being
1886 * expired before we get a chance to add it
1887 */
1888 BUG_ON(mnt_get_count(mnt) < 2);
1889
1890 if (m->mnt_sb == path->mnt->mnt_sb &&
1891 m->mnt_root == path->dentry) {
1892 err = -ELOOP;
1893 goto fail;
1894 }
1895
1896 err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
1897 if (!err)
1898 return 0;
1899fail:
1900 /* remove m from any expiration list it may be on */
1901 if (!list_empty(&mnt->mnt_expire)) {
1902 down_write(&namespace_sem);
1903 br_write_lock(vfsmount_lock);
1904 list_del_init(&mnt->mnt_expire);
1905 br_write_unlock(vfsmount_lock);
1906 up_write(&namespace_sem);
1907 }
1908 mntput(m);
1909 mntput(m);
1910 return err;
1911}
1912
1913/**
1914 * mnt_set_expiry - Put a mount on an expiration list
1915 * @mnt: The mount to list.
1916 * @expiry_list: The list to add the mount to.
1917 */
1918void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
1919{
1920 down_write(&namespace_sem);
1921 br_write_lock(vfsmount_lock);
1922
1923 list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
1924
1925 br_write_unlock(vfsmount_lock);
1926 up_write(&namespace_sem);
1927}
1928EXPORT_SYMBOL(mnt_set_expiry);
1929
1930/*
1931 * process a list of expirable mountpoints with the intent of discarding any
1932 * mountpoints that aren't in use and haven't been touched since last we came
1933 * here
1934 */
1935void mark_mounts_for_expiry(struct list_head *mounts)
1936{
1937 struct mount *mnt, *next;
1938 LIST_HEAD(graveyard);
1939 LIST_HEAD(umounts);
1940
1941 if (list_empty(mounts))
1942 return;
1943
1944 down_write(&namespace_sem);
1945 br_write_lock(vfsmount_lock);
1946
1947 /* extract from the expiration list every vfsmount that matches the
1948 * following criteria:
1949 * - only referenced by its parent vfsmount
1950 * - still marked for expiry (marked on the last call here; marks are
1951 * cleared by mntput())
1952 */
1953 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1954 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1955 propagate_mount_busy(mnt, 1))
1956 continue;
1957 list_move(&mnt->mnt_expire, &graveyard);
1958 }
1959 while (!list_empty(&graveyard)) {
1960 mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
1961 touch_mnt_namespace(mnt->mnt_ns);
1962 umount_tree(mnt, 1, &umounts);
1963 }
1964 br_write_unlock(vfsmount_lock);
1965 up_write(&namespace_sem);
1966
1967 release_mounts(&umounts);
1968}
1969
1970EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1971
1972/*
1973 * Ripoff of 'select_parent()'
1974 *
1975 * search the list of submounts for a given mountpoint, and move any
1976 * shrinkable submounts to the 'graveyard' list.
1977 */
1978static int select_submounts(struct mount *parent, struct list_head *graveyard)
1979{
1980 struct mount *this_parent = parent;
1981 struct list_head *next;
1982 int found = 0;
1983
1984repeat:
1985 next = this_parent->mnt_mounts.next;
1986resume:
1987 while (next != &this_parent->mnt_mounts) {
1988 struct list_head *tmp = next;
1989 struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
1990
1991 next = tmp->next;
1992 if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
1993 continue;
1994 /*
1995 * Descend a level if the d_mounts list is non-empty.
1996 */
1997 if (!list_empty(&mnt->mnt_mounts)) {
1998 this_parent = mnt;
1999 goto repeat;
2000 }
2001
2002 if (!propagate_mount_busy(mnt, 1)) {
2003 list_move_tail(&mnt->mnt_expire, graveyard);
2004 found++;
2005 }
2006 }
2007 /*
2008 * All done at this level ... ascend and resume the search
2009 */
2010 if (this_parent != parent) {
2011 next = this_parent->mnt_child.next;
2012 this_parent = this_parent->mnt_parent;
2013 goto resume;
2014 }
2015 return found;
2016}
2017
2018/*
2019 * process a list of expirable mountpoints with the intent of discarding any
2020 * submounts of a specific parent mountpoint
2021 *
2022 * vfsmount_lock must be held for write
2023 */
2024static void shrink_submounts(struct mount *mnt, struct list_head *umounts)
2025{
2026 LIST_HEAD(graveyard);
2027 struct mount *m;
2028
2029 /* extract submounts of 'mountpoint' from the expiration list */
2030 while (select_submounts(mnt, &graveyard)) {
2031 while (!list_empty(&graveyard)) {
2032 m = list_first_entry(&graveyard, struct mount,
2033 mnt_expire);
2034 touch_mnt_namespace(m->mnt_ns);
2035 umount_tree(m, 1, umounts);
2036 }
2037 }
2038}
2039
2040/*
2041 * Some copy_from_user() implementations do not return the exact number of
2042 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
2043 * Note that this function differs from copy_from_user() in that it will oops
2044 * on bad values of `to', rather than returning a short copy.
2045 */
2046static long exact_copy_from_user(void *to, const void __user * from,
2047 unsigned long n)
2048{
2049 char *t = to;
2050 const char __user *f = from;
2051 char c;
2052
2053 if (!access_ok(VERIFY_READ, from, n))
2054 return n;
2055
2056 while (n) {
2057 if (__get_user(c, f)) {
2058 memset(t, 0, n);
2059 break;
2060 }
2061 *t++ = c;
2062 f++;
2063 n--;
2064 }
2065 return n;
2066}
2067
2068int copy_mount_options(const void __user * data, unsigned long *where)
2069{
2070 int i;
2071 unsigned long page;
2072 unsigned long size;
2073
2074 *where = 0;
2075 if (!data)
2076 return 0;
2077
2078 if (!(page = __get_free_page(GFP_KERNEL)))
2079 return -ENOMEM;
2080
2081 /* We only care that *some* data at the address the user
2082 * gave us is valid. Just in case, we'll zero
2083 * the remainder of the page.
2084 */
2085 /* copy_from_user cannot cross TASK_SIZE ! */
2086 size = TASK_SIZE - (unsigned long)data;
2087 if (size > PAGE_SIZE)
2088 size = PAGE_SIZE;
2089
2090 i = size - exact_copy_from_user((void *)page, data, size);
2091 if (!i) {
2092 free_page(page);
2093 return -EFAULT;
2094 }
2095 if (i != PAGE_SIZE)
2096 memset((char *)page + i, 0, PAGE_SIZE - i);
2097 *where = page;
2098 return 0;
2099}
2100
2101int copy_mount_string(const void __user *data, char **where)
2102{
2103 char *tmp;
2104
2105 if (!data) {
2106 *where = NULL;
2107 return 0;
2108 }
2109
2110 tmp = strndup_user(data, PAGE_SIZE);
2111 if (IS_ERR(tmp))
2112 return PTR_ERR(tmp);
2113
2114 *where = tmp;
2115 return 0;
2116}
2117
2118/*
2119 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2120 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2121 *
2122 * data is a (void *) that can point to any structure up to
2123 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2124 * information (or be NULL).
2125 *
2126 * Pre-0.97 versions of mount() didn't have a flags word.
2127 * When the flags word was introduced its top half was required
2128 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2129 * Therefore, if this magic number is present, it carries no information
2130 * and must be discarded.
2131 */
2132long do_mount(char *dev_name, char *dir_name, char *type_page,
2133 unsigned long flags, void *data_page)
2134{
2135 struct path path;
2136 int retval = 0;
2137 int mnt_flags = 0;
2138
2139 /* Discard magic */
2140 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
2141 flags &= ~MS_MGC_MSK;
2142
2143 /* Basic sanity checks */
2144
2145 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
2146 return -EINVAL;
2147
2148 if (data_page)
2149 ((char *)data_page)[PAGE_SIZE - 1] = 0;
2150
2151 /* ... and get the mountpoint */
2152 retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
2153 if (retval)
2154 return retval;
2155
2156 retval = security_sb_mount(dev_name, &path,
2157 type_page, flags, data_page);
2158 if (retval)
2159 goto dput_out;
2160
2161 /* Default to relatime unless overriden */
2162 if (!(flags & MS_NOATIME))
2163 mnt_flags |= MNT_RELATIME;
2164
2165 /* Separate the per-mountpoint flags */
2166 if (flags & MS_NOSUID)
2167 mnt_flags |= MNT_NOSUID;
2168 if (flags & MS_NODEV)
2169 mnt_flags |= MNT_NODEV;
2170 if (flags & MS_NOEXEC)
2171 mnt_flags |= MNT_NOEXEC;
2172 if (flags & MS_NOATIME)
2173 mnt_flags |= MNT_NOATIME;
2174 if (flags & MS_NODIRATIME)
2175 mnt_flags |= MNT_NODIRATIME;
2176 if (flags & MS_STRICTATIME)
2177 mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
2178 if (flags & MS_RDONLY)
2179 mnt_flags |= MNT_READONLY;
2180
2181 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
2182 MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
2183 MS_STRICTATIME);
2184
2185 if (flags & MS_REMOUNT)
2186 retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
2187 data_page);
2188 else if (flags & MS_BIND)
2189 retval = do_loopback(&path, dev_name, flags & MS_REC);
2190 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
2191 retval = do_change_type(&path, flags);
2192 else if (flags & MS_MOVE)
2193 retval = do_move_mount(&path, dev_name);
2194 else
2195 retval = do_new_mount(&path, type_page, flags, mnt_flags,
2196 dev_name, data_page);
2197dput_out:
2198 path_put(&path);
2199 return retval;
2200}
2201
2202static struct mnt_namespace *alloc_mnt_ns(void)
2203{
2204 struct mnt_namespace *new_ns;
2205
2206 new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
2207 if (!new_ns)
2208 return ERR_PTR(-ENOMEM);
2209 atomic_set(&new_ns->count, 1);
2210 new_ns->root = NULL;
2211 INIT_LIST_HEAD(&new_ns->list);
2212 init_waitqueue_head(&new_ns->poll);
2213 new_ns->event = 0;
2214 return new_ns;
2215}
2216
2217void mnt_make_longterm(struct vfsmount *mnt)
2218{
2219 __mnt_make_longterm(real_mount(mnt));
2220}
2221
2222void mnt_make_shortterm(struct vfsmount *m)
2223{
2224#ifdef CONFIG_SMP
2225 struct mount *mnt = real_mount(m);
2226 if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
2227 return;
2228 br_write_lock(vfsmount_lock);
2229 atomic_dec(&mnt->mnt_longterm);
2230 br_write_unlock(vfsmount_lock);
2231#endif
2232}
2233
2234/*
2235 * Allocate a new namespace structure and populate it with contents
2236 * copied from the namespace of the passed in task structure.
2237 */
2238static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
2239 struct fs_struct *fs)
2240{
2241 struct mnt_namespace *new_ns;
2242 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
2243 struct mount *p, *q;
2244 struct mount *old = mnt_ns->root;
2245 struct mount *new;
2246
2247 new_ns = alloc_mnt_ns();
2248 if (IS_ERR(new_ns))
2249 return new_ns;
2250
2251 down_write(&namespace_sem);
2252 /* First pass: copy the tree topology */
2253 new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE);
2254 if (!new) {
2255 up_write(&namespace_sem);
2256 kfree(new_ns);
2257 return ERR_PTR(-ENOMEM);
2258 }
2259 new_ns->root = new;
2260 br_write_lock(vfsmount_lock);
2261 list_add_tail(&new_ns->list, &new->mnt_list);
2262 br_write_unlock(vfsmount_lock);
2263
2264 /*
2265 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2266 * as belonging to new namespace. We have already acquired a private
2267 * fs_struct, so tsk->fs->lock is not needed.
2268 */
2269 p = old;
2270 q = new;
2271 while (p) {
2272 q->mnt_ns = new_ns;
2273 __mnt_make_longterm(q);
2274 if (fs) {
2275 if (&p->mnt == fs->root.mnt) {
2276 fs->root.mnt = mntget(&q->mnt);
2277 __mnt_make_longterm(q);
2278 mnt_make_shortterm(&p->mnt);
2279 rootmnt = &p->mnt;
2280 }
2281 if (&p->mnt == fs->pwd.mnt) {
2282 fs->pwd.mnt = mntget(&q->mnt);
2283 __mnt_make_longterm(q);
2284 mnt_make_shortterm(&p->mnt);
2285 pwdmnt = &p->mnt;
2286 }
2287 }
2288 p = next_mnt(p, old);
2289 q = next_mnt(q, new);
2290 }
2291 up_write(&namespace_sem);
2292
2293 if (rootmnt)
2294 mntput(rootmnt);
2295 if (pwdmnt)
2296 mntput(pwdmnt);
2297
2298 return new_ns;
2299}
2300
2301struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
2302 struct fs_struct *new_fs)
2303{
2304 struct mnt_namespace *new_ns;
2305
2306 BUG_ON(!ns);
2307 get_mnt_ns(ns);
2308
2309 if (!(flags & CLONE_NEWNS))
2310 return ns;
2311
2312 new_ns = dup_mnt_ns(ns, new_fs);
2313
2314 put_mnt_ns(ns);
2315 return new_ns;
2316}
2317
2318/**
2319 * create_mnt_ns - creates a private namespace and adds a root filesystem
2320 * @mnt: pointer to the new root filesystem mountpoint
2321 */
2322static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
2323{
2324 struct mnt_namespace *new_ns = alloc_mnt_ns();
2325 if (!IS_ERR(new_ns)) {
2326 struct mount *mnt = real_mount(m);
2327 mnt->mnt_ns = new_ns;
2328 __mnt_make_longterm(mnt);
2329 new_ns->root = mnt;
2330 list_add(&new_ns->list, &mnt->mnt_list);
2331 } else {
2332 mntput(m);
2333 }
2334 return new_ns;
2335}
2336
2337struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
2338{
2339 struct mnt_namespace *ns;
2340 struct super_block *s;
2341 struct path path;
2342 int err;
2343
2344 ns = create_mnt_ns(mnt);
2345 if (IS_ERR(ns))
2346 return ERR_CAST(ns);
2347
2348 err = vfs_path_lookup(mnt->mnt_root, mnt,
2349 name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
2350
2351 put_mnt_ns(ns);
2352
2353 if (err)
2354 return ERR_PTR(err);
2355
2356 /* trade a vfsmount reference for active sb one */
2357 s = path.mnt->mnt_sb;
2358 atomic_inc(&s->s_active);
2359 mntput(path.mnt);
2360 /* lock the sucker */
2361 down_write(&s->s_umount);
2362 /* ... and return the root of (sub)tree on it */
2363 return path.dentry;
2364}
2365EXPORT_SYMBOL(mount_subtree);
2366
2367SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
2368 char __user *, type, unsigned long, flags, void __user *, data)
2369{
2370 int ret;
2371 char *kernel_type;
2372 char *kernel_dir;
2373 char *kernel_dev;
2374 unsigned long data_page;
2375
2376 ret = copy_mount_string(type, &kernel_type);
2377 if (ret < 0)
2378 goto out_type;
2379
2380 kernel_dir = getname(dir_name);
2381 if (IS_ERR(kernel_dir)) {
2382 ret = PTR_ERR(kernel_dir);
2383 goto out_dir;
2384 }
2385
2386 ret = copy_mount_string(dev_name, &kernel_dev);
2387 if (ret < 0)
2388 goto out_dev;
2389
2390 ret = copy_mount_options(data, &data_page);
2391 if (ret < 0)
2392 goto out_data;
2393
2394 ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
2395 (void *) data_page);
2396
2397 free_page(data_page);
2398out_data:
2399 kfree(kernel_dev);
2400out_dev:
2401 putname(kernel_dir);
2402out_dir:
2403 kfree(kernel_type);
2404out_type:
2405 return ret;
2406}
2407
2408/*
2409 * Return true if path is reachable from root
2410 *
2411 * namespace_sem or vfsmount_lock is held
2412 */
2413bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
2414 const struct path *root)
2415{
2416 while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
2417 dentry = mnt->mnt_mountpoint;
2418 mnt = mnt->mnt_parent;
2419 }
2420 return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
2421}
2422
2423int path_is_under(struct path *path1, struct path *path2)
2424{
2425 int res;
2426 br_read_lock(vfsmount_lock);
2427 res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
2428 br_read_unlock(vfsmount_lock);
2429 return res;
2430}
2431EXPORT_SYMBOL(path_is_under);
2432
2433/*
2434 * pivot_root Semantics:
2435 * Moves the root file system of the current process to the directory put_old,
2436 * makes new_root as the new root file system of the current process, and sets
2437 * root/cwd of all processes which had them on the current root to new_root.
2438 *
2439 * Restrictions:
2440 * The new_root and put_old must be directories, and must not be on the
2441 * same file system as the current process root. The put_old must be
2442 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2443 * pointed to by put_old must yield the same directory as new_root. No other
2444 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2445 *
2446 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2447 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2448 * in this situation.
2449 *
2450 * Notes:
2451 * - we don't move root/cwd if they are not at the root (reason: if something
2452 * cared enough to change them, it's probably wrong to force them elsewhere)
2453 * - it's okay to pick a root that isn't the root of a file system, e.g.
2454 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2455 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2456 * first.
2457 */
2458SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
2459 const char __user *, put_old)
2460{
2461 struct path new, old, parent_path, root_parent, root;
2462 struct mount *new_mnt, *root_mnt;
2463 int error;
2464
2465 if (!capable(CAP_SYS_ADMIN))
2466 return -EPERM;
2467
2468 error = user_path_dir(new_root, &new);
2469 if (error)
2470 goto out0;
2471
2472 error = user_path_dir(put_old, &old);
2473 if (error)
2474 goto out1;
2475
2476 error = security_sb_pivotroot(&old, &new);
2477 if (error)
2478 goto out2;
2479
2480 get_fs_root(current->fs, &root);
2481 error = lock_mount(&old);
2482 if (error)
2483 goto out3;
2484
2485 error = -EINVAL;
2486 new_mnt = real_mount(new.mnt);
2487 root_mnt = real_mount(root.mnt);
2488 if (IS_MNT_SHARED(real_mount(old.mnt)) ||
2489 IS_MNT_SHARED(new_mnt->mnt_parent) ||
2490 IS_MNT_SHARED(root_mnt->mnt_parent))
2491 goto out4;
2492 if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
2493 goto out4;
2494 error = -ENOENT;
2495 if (d_unlinked(new.dentry))
2496 goto out4;
2497 if (d_unlinked(old.dentry))
2498 goto out4;
2499 error = -EBUSY;
2500 if (new.mnt == root.mnt ||
2501 old.mnt == root.mnt)
2502 goto out4; /* loop, on the same file system */
2503 error = -EINVAL;
2504 if (root.mnt->mnt_root != root.dentry)
2505 goto out4; /* not a mountpoint */
2506 if (!mnt_has_parent(root_mnt))
2507 goto out4; /* not attached */
2508 if (new.mnt->mnt_root != new.dentry)
2509 goto out4; /* not a mountpoint */
2510 if (!mnt_has_parent(new_mnt))
2511 goto out4; /* not attached */
2512 /* make sure we can reach put_old from new_root */
2513 if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new))
2514 goto out4;
2515 /* make certain new is below the root */
2516 if (!is_path_reachable(new_mnt, new.dentry, &root))
2517 goto out4;
2518 br_write_lock(vfsmount_lock);
2519 detach_mnt(new_mnt, &parent_path);
2520 detach_mnt(root_mnt, &root_parent);
2521 /* mount old root on put_old */
2522 attach_mnt(root_mnt, &old);
2523 /* mount new_root on / */
2524 attach_mnt(new_mnt, &root_parent);
2525 touch_mnt_namespace(current->nsproxy->mnt_ns);
2526 br_write_unlock(vfsmount_lock);
2527 chroot_fs_refs(&root, &new);
2528 error = 0;
2529out4:
2530 unlock_mount(&old);
2531 if (!error) {
2532 path_put(&root_parent);
2533 path_put(&parent_path);
2534 }
2535out3:
2536 path_put(&root);
2537out2:
2538 path_put(&old);
2539out1:
2540 path_put(&new);
2541out0:
2542 return error;
2543}
2544
2545static void __init init_mount_tree(void)
2546{
2547 struct vfsmount *mnt;
2548 struct mnt_namespace *ns;
2549 struct path root;
2550
2551 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
2552 if (IS_ERR(mnt))
2553 panic("Can't create rootfs");
2554
2555 ns = create_mnt_ns(mnt);
2556 if (IS_ERR(ns))
2557 panic("Can't allocate initial namespace");
2558
2559 init_task.nsproxy->mnt_ns = ns;
2560 get_mnt_ns(ns);
2561
2562 root.mnt = mnt;
2563 root.dentry = mnt->mnt_root;
2564
2565 set_fs_pwd(current->fs, &root);
2566 set_fs_root(current->fs, &root);
2567}
2568
2569void __init mnt_init(void)
2570{
2571 unsigned u;
2572 int err;
2573
2574 init_rwsem(&namespace_sem);
2575
2576 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
2577 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
2578
2579 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
2580
2581 if (!mount_hashtable)
2582 panic("Failed to allocate mount hash table\n");
2583
2584 printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
2585
2586 for (u = 0; u < HASH_SIZE; u++)
2587 INIT_LIST_HEAD(&mount_hashtable[u]);
2588
2589 br_lock_init(vfsmount_lock);
2590
2591 err = sysfs_init();
2592 if (err)
2593 printk(KERN_WARNING "%s: sysfs_init error: %d\n",
2594 __func__, err);
2595 fs_kobj = kobject_create_and_add("fs", NULL);
2596 if (!fs_kobj)
2597 printk(KERN_WARNING "%s: kobj create error\n", __func__);
2598 init_rootfs();
2599 init_mount_tree();
2600}
2601
2602void put_mnt_ns(struct mnt_namespace *ns)
2603{
2604 LIST_HEAD(umount_list);
2605
2606 if (!atomic_dec_and_test(&ns->count))
2607 return;
2608 down_write(&namespace_sem);
2609 br_write_lock(vfsmount_lock);
2610 umount_tree(ns->root, 0, &umount_list);
2611 br_write_unlock(vfsmount_lock);
2612 up_write(&namespace_sem);
2613 release_mounts(&umount_list);
2614 kfree(ns);
2615}
2616
2617struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
2618{
2619 struct vfsmount *mnt;
2620 mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
2621 if (!IS_ERR(mnt)) {
2622 /*
2623 * it is a longterm mount, don't release mnt until
2624 * we unmount before file sys is unregistered
2625 */
2626 mnt_make_longterm(mnt);
2627 }
2628 return mnt;
2629}
2630EXPORT_SYMBOL_GPL(kern_mount_data);
2631
2632void kern_unmount(struct vfsmount *mnt)
2633{
2634 /* release long term mount so mount point can be released */
2635 if (!IS_ERR_OR_NULL(mnt)) {
2636 mnt_make_shortterm(mnt);
2637 mntput(mnt);
2638 }
2639}
2640EXPORT_SYMBOL(kern_unmount);
2641
2642bool our_mnt(struct vfsmount *mnt)
2643{
2644 return check_mnt(real_mount(mnt));
2645}