4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
11 #include <linux/syscalls.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include <linux/spinlock.h>
15 #include <linux/percpu.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/acct.h>
19 #include <linux/capability.h>
20 #include <linux/cpumask.h>
21 #include <linux/module.h>
22 #include <linux/sysfs.h>
23 #include <linux/seq_file.h>
24 #include <linux/mnt_namespace.h>
25 #include <linux/namei.h>
26 #include <linux/nsproxy.h>
27 #include <linux/security.h>
28 #include <linux/mount.h>
29 #include <linux/ramfs.h>
30 #include <linux/log2.h>
31 #include <linux/idr.h>
32 #include <linux/fs_struct.h>
33 #include <linux/fsnotify.h>
34 #include <asm/uaccess.h>
35 #include <asm/unistd.h>
39 #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
40 #define HASH_SIZE (1UL << HASH_SHIFT)
43 static DEFINE_IDA(mnt_id_ida
);
44 static DEFINE_IDA(mnt_group_ida
);
45 static DEFINE_SPINLOCK(mnt_id_lock
);
46 static int mnt_id_start
= 0;
47 static int mnt_group_start
= 1;
49 static struct list_head
*mount_hashtable __read_mostly
;
50 static struct kmem_cache
*mnt_cache __read_mostly
;
51 static struct rw_semaphore namespace_sem
;
54 struct kobject
*fs_kobj
;
55 EXPORT_SYMBOL_GPL(fs_kobj
);
58 * vfsmount lock may be taken for read to prevent changes to the
59 * vfsmount hash, ie. during mountpoint lookups or walking back
62 * It should be taken for write in all cases where the vfsmount
63 * tree or hash is modified or when a vfsmount structure is modified.
65 DEFINE_BRLOCK(vfsmount_lock
);
67 static inline unsigned long hash(struct vfsmount
*mnt
, struct dentry
*dentry
)
69 unsigned long tmp
= ((unsigned long)mnt
/ L1_CACHE_BYTES
);
70 tmp
+= ((unsigned long)dentry
/ L1_CACHE_BYTES
);
71 tmp
= tmp
+ (tmp
>> HASH_SHIFT
);
72 return tmp
& (HASH_SIZE
- 1);
75 #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
78 * allocation is serialized by namespace_sem, but we need the spinlock to
79 * serialize with freeing.
81 static int mnt_alloc_id(struct vfsmount
*mnt
)
86 ida_pre_get(&mnt_id_ida
, GFP_KERNEL
);
87 spin_lock(&mnt_id_lock
);
88 res
= ida_get_new_above(&mnt_id_ida
, mnt_id_start
, &mnt
->mnt_id
);
90 mnt_id_start
= mnt
->mnt_id
+ 1;
91 spin_unlock(&mnt_id_lock
);
98 static void mnt_free_id(struct vfsmount
*mnt
)
100 int id
= mnt
->mnt_id
;
101 spin_lock(&mnt_id_lock
);
102 ida_remove(&mnt_id_ida
, id
);
103 if (mnt_id_start
> id
)
105 spin_unlock(&mnt_id_lock
);
109 * Allocate a new peer group ID
111 * mnt_group_ida is protected by namespace_sem
113 static int mnt_alloc_group_id(struct vfsmount
*mnt
)
117 if (!ida_pre_get(&mnt_group_ida
, GFP_KERNEL
))
120 res
= ida_get_new_above(&mnt_group_ida
,
124 mnt_group_start
= mnt
->mnt_group_id
+ 1;
130 * Release a peer group ID
132 void mnt_release_group_id(struct vfsmount
*mnt
)
134 int id
= mnt
->mnt_group_id
;
135 ida_remove(&mnt_group_ida
, id
);
136 if (mnt_group_start
> id
)
137 mnt_group_start
= id
;
138 mnt
->mnt_group_id
= 0;
141 struct vfsmount
*alloc_vfsmnt(const char *name
)
143 struct vfsmount
*mnt
= kmem_cache_zalloc(mnt_cache
, GFP_KERNEL
);
147 err
= mnt_alloc_id(mnt
);
152 mnt
->mnt_devname
= kstrdup(name
, GFP_KERNEL
);
153 if (!mnt
->mnt_devname
)
157 atomic_set(&mnt
->mnt_count
, 1);
158 INIT_LIST_HEAD(&mnt
->mnt_hash
);
159 INIT_LIST_HEAD(&mnt
->mnt_child
);
160 INIT_LIST_HEAD(&mnt
->mnt_mounts
);
161 INIT_LIST_HEAD(&mnt
->mnt_list
);
162 INIT_LIST_HEAD(&mnt
->mnt_expire
);
163 INIT_LIST_HEAD(&mnt
->mnt_share
);
164 INIT_LIST_HEAD(&mnt
->mnt_slave_list
);
165 INIT_LIST_HEAD(&mnt
->mnt_slave
);
166 #ifdef CONFIG_FSNOTIFY
167 INIT_HLIST_HEAD(&mnt
->mnt_fsnotify_marks
);
170 mnt
->mnt_writers
= alloc_percpu(int);
171 if (!mnt
->mnt_writers
)
172 goto out_free_devname
;
174 mnt
->mnt_writers
= 0;
181 kfree(mnt
->mnt_devname
);
186 kmem_cache_free(mnt_cache
, mnt
);
191 * Most r/o checks on a fs are for operations that take
192 * discrete amounts of time, like a write() or unlink().
193 * We must keep track of when those operations start
194 * (for permission checks) and when they end, so that
195 * we can determine when writes are able to occur to
199 * __mnt_is_readonly: check whether a mount is read-only
200 * @mnt: the mount to check for its write status
202 * This shouldn't be used directly ouside of the VFS.
203 * It does not guarantee that the filesystem will stay
204 * r/w, just that it is right *now*. This can not and
205 * should not be used in place of IS_RDONLY(inode).
206 * mnt_want/drop_write() will _keep_ the filesystem
209 int __mnt_is_readonly(struct vfsmount
*mnt
)
211 if (mnt
->mnt_flags
& MNT_READONLY
)
213 if (mnt
->mnt_sb
->s_flags
& MS_RDONLY
)
217 EXPORT_SYMBOL_GPL(__mnt_is_readonly
);
219 static inline void inc_mnt_writers(struct vfsmount
*mnt
)
222 (*per_cpu_ptr(mnt
->mnt_writers
, smp_processor_id()))++;
228 static inline void dec_mnt_writers(struct vfsmount
*mnt
)
231 (*per_cpu_ptr(mnt
->mnt_writers
, smp_processor_id()))--;
237 static unsigned int count_mnt_writers(struct vfsmount
*mnt
)
240 unsigned int count
= 0;
243 for_each_possible_cpu(cpu
) {
244 count
+= *per_cpu_ptr(mnt
->mnt_writers
, cpu
);
249 return mnt
->mnt_writers
;
254 * Most r/o checks on a fs are for operations that take
255 * discrete amounts of time, like a write() or unlink().
256 * We must keep track of when those operations start
257 * (for permission checks) and when they end, so that
258 * we can determine when writes are able to occur to
262 * mnt_want_write - get write access to a mount
263 * @mnt: the mount on which to take a write
265 * This tells the low-level filesystem that a write is
266 * about to be performed to it, and makes sure that
267 * writes are allowed before returning success. When
268 * the write operation is finished, mnt_drop_write()
269 * must be called. This is effectively a refcount.
271 int mnt_want_write(struct vfsmount
*mnt
)
276 inc_mnt_writers(mnt
);
278 * The store to inc_mnt_writers must be visible before we pass
279 * MNT_WRITE_HOLD loop below, so that the slowpath can see our
280 * incremented count after it has set MNT_WRITE_HOLD.
283 while (mnt
->mnt_flags
& MNT_WRITE_HOLD
)
286 * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
287 * be set to match its requirements. So we must not load that until
288 * MNT_WRITE_HOLD is cleared.
291 if (__mnt_is_readonly(mnt
)) {
292 dec_mnt_writers(mnt
);
300 EXPORT_SYMBOL_GPL(mnt_want_write
);
303 * mnt_clone_write - get write access to a mount
304 * @mnt: the mount on which to take a write
306 * This is effectively like mnt_want_write, except
307 * it must only be used to take an extra write reference
308 * on a mountpoint that we already know has a write reference
309 * on it. This allows some optimisation.
311 * After finished, mnt_drop_write must be called as usual to
312 * drop the reference.
314 int mnt_clone_write(struct vfsmount
*mnt
)
316 /* superblock may be r/o */
317 if (__mnt_is_readonly(mnt
))
320 inc_mnt_writers(mnt
);
324 EXPORT_SYMBOL_GPL(mnt_clone_write
);
327 * mnt_want_write_file - get write access to a file's mount
328 * @file: the file who's mount on which to take a write
330 * This is like mnt_want_write, but it takes a file and can
331 * do some optimisations if the file is open for write already
333 int mnt_want_write_file(struct file
*file
)
335 struct inode
*inode
= file
->f_dentry
->d_inode
;
336 if (!(file
->f_mode
& FMODE_WRITE
) || special_file(inode
->i_mode
))
337 return mnt_want_write(file
->f_path
.mnt
);
339 return mnt_clone_write(file
->f_path
.mnt
);
341 EXPORT_SYMBOL_GPL(mnt_want_write_file
);
344 * mnt_drop_write - give up write access to a mount
345 * @mnt: the mount on which to give up write access
347 * Tells the low-level filesystem that we are done
348 * performing writes to it. Must be matched with
349 * mnt_want_write() call above.
351 void mnt_drop_write(struct vfsmount
*mnt
)
354 dec_mnt_writers(mnt
);
357 EXPORT_SYMBOL_GPL(mnt_drop_write
);
359 static int mnt_make_readonly(struct vfsmount
*mnt
)
363 br_write_lock(vfsmount_lock
);
364 mnt
->mnt_flags
|= MNT_WRITE_HOLD
;
366 * After storing MNT_WRITE_HOLD, we'll read the counters. This store
367 * should be visible before we do.
372 * With writers on hold, if this value is zero, then there are
373 * definitely no active writers (although held writers may subsequently
374 * increment the count, they'll have to wait, and decrement it after
375 * seeing MNT_READONLY).
377 * It is OK to have counter incremented on one CPU and decremented on
378 * another: the sum will add up correctly. The danger would be when we
379 * sum up each counter, if we read a counter before it is incremented,
380 * but then read another CPU's count which it has been subsequently
381 * decremented from -- we would see more decrements than we should.
382 * MNT_WRITE_HOLD protects against this scenario, because
383 * mnt_want_write first increments count, then smp_mb, then spins on
384 * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
385 * we're counting up here.
387 if (count_mnt_writers(mnt
) > 0)
390 mnt
->mnt_flags
|= MNT_READONLY
;
392 * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
393 * that become unheld will see MNT_READONLY.
396 mnt
->mnt_flags
&= ~MNT_WRITE_HOLD
;
397 br_write_unlock(vfsmount_lock
);
401 static void __mnt_unmake_readonly(struct vfsmount
*mnt
)
403 br_write_lock(vfsmount_lock
);
404 mnt
->mnt_flags
&= ~MNT_READONLY
;
405 br_write_unlock(vfsmount_lock
);
408 void simple_set_mnt(struct vfsmount
*mnt
, struct super_block
*sb
)
411 mnt
->mnt_root
= dget(sb
->s_root
);
414 EXPORT_SYMBOL(simple_set_mnt
);
416 void free_vfsmnt(struct vfsmount
*mnt
)
418 kfree(mnt
->mnt_devname
);
421 free_percpu(mnt
->mnt_writers
);
423 kmem_cache_free(mnt_cache
, mnt
);
427 * find the first or last mount at @dentry on vfsmount @mnt depending on
428 * @dir. If @dir is set return the first mount else return the last mount.
429 * vfsmount_lock must be held for read or write.
431 struct vfsmount
*__lookup_mnt(struct vfsmount
*mnt
, struct dentry
*dentry
,
434 struct list_head
*head
= mount_hashtable
+ hash(mnt
, dentry
);
435 struct list_head
*tmp
= head
;
436 struct vfsmount
*p
, *found
= NULL
;
439 tmp
= dir
? tmp
->next
: tmp
->prev
;
443 p
= list_entry(tmp
, struct vfsmount
, mnt_hash
);
444 if (p
->mnt_parent
== mnt
&& p
->mnt_mountpoint
== dentry
) {
453 * lookup_mnt increments the ref count before returning
454 * the vfsmount struct.
456 struct vfsmount
*lookup_mnt(struct path
*path
)
458 struct vfsmount
*child_mnt
;
460 br_read_lock(vfsmount_lock
);
461 if ((child_mnt
= __lookup_mnt(path
->mnt
, path
->dentry
, 1)))
463 br_read_unlock(vfsmount_lock
);
467 static inline int check_mnt(struct vfsmount
*mnt
)
469 return mnt
->mnt_ns
== current
->nsproxy
->mnt_ns
;
473 * vfsmount lock must be held for write
475 static void touch_mnt_namespace(struct mnt_namespace
*ns
)
479 wake_up_interruptible(&ns
->poll
);
484 * vfsmount lock must be held for write
486 static void __touch_mnt_namespace(struct mnt_namespace
*ns
)
488 if (ns
&& ns
->event
!= event
) {
490 wake_up_interruptible(&ns
->poll
);
495 * Clear dentry's mounted state if it has no remaining mounts.
496 * vfsmount_lock must be held for write.
498 static void dentry_reset_mounted(struct vfsmount
*mnt
, struct dentry
*dentry
)
502 for (u
= 0; u
< HASH_SIZE
; u
++) {
505 list_for_each_entry(p
, &mount_hashtable
[u
], mnt_hash
) {
506 if (p
->mnt_mountpoint
== dentry
)
510 spin_lock(&dentry
->d_lock
);
511 dentry
->d_flags
&= ~DCACHE_MOUNTED
;
512 spin_unlock(&dentry
->d_lock
);
516 * vfsmount lock must be held for write
518 static void detach_mnt(struct vfsmount
*mnt
, struct path
*old_path
)
520 old_path
->dentry
= mnt
->mnt_mountpoint
;
521 old_path
->mnt
= mnt
->mnt_parent
;
522 mnt
->mnt_parent
= mnt
;
523 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
524 list_del_init(&mnt
->mnt_child
);
525 list_del_init(&mnt
->mnt_hash
);
526 dentry_reset_mounted(old_path
->mnt
, old_path
->dentry
);
530 * vfsmount lock must be held for write
532 void mnt_set_mountpoint(struct vfsmount
*mnt
, struct dentry
*dentry
,
533 struct vfsmount
*child_mnt
)
535 child_mnt
->mnt_parent
= mntget(mnt
);
536 child_mnt
->mnt_mountpoint
= dget(dentry
);
537 spin_lock(&dentry
->d_lock
);
538 dentry
->d_flags
|= DCACHE_MOUNTED
;
539 spin_unlock(&dentry
->d_lock
);
543 * vfsmount lock must be held for write
545 static void attach_mnt(struct vfsmount
*mnt
, struct path
*path
)
547 mnt_set_mountpoint(path
->mnt
, path
->dentry
, mnt
);
548 list_add_tail(&mnt
->mnt_hash
, mount_hashtable
+
549 hash(path
->mnt
, path
->dentry
));
550 list_add_tail(&mnt
->mnt_child
, &path
->mnt
->mnt_mounts
);
554 * vfsmount lock must be held for write
556 static void commit_tree(struct vfsmount
*mnt
)
558 struct vfsmount
*parent
= mnt
->mnt_parent
;
561 struct mnt_namespace
*n
= parent
->mnt_ns
;
563 BUG_ON(parent
== mnt
);
565 list_add_tail(&head
, &mnt
->mnt_list
);
566 list_for_each_entry(m
, &head
, mnt_list
)
568 list_splice(&head
, n
->list
.prev
);
570 list_add_tail(&mnt
->mnt_hash
, mount_hashtable
+
571 hash(parent
, mnt
->mnt_mountpoint
));
572 list_add_tail(&mnt
->mnt_child
, &parent
->mnt_mounts
);
573 touch_mnt_namespace(n
);
576 static struct vfsmount
*next_mnt(struct vfsmount
*p
, struct vfsmount
*root
)
578 struct list_head
*next
= p
->mnt_mounts
.next
;
579 if (next
== &p
->mnt_mounts
) {
583 next
= p
->mnt_child
.next
;
584 if (next
!= &p
->mnt_parent
->mnt_mounts
)
589 return list_entry(next
, struct vfsmount
, mnt_child
);
592 static struct vfsmount
*skip_mnt_tree(struct vfsmount
*p
)
594 struct list_head
*prev
= p
->mnt_mounts
.prev
;
595 while (prev
!= &p
->mnt_mounts
) {
596 p
= list_entry(prev
, struct vfsmount
, mnt_child
);
597 prev
= p
->mnt_mounts
.prev
;
602 static struct vfsmount
*clone_mnt(struct vfsmount
*old
, struct dentry
*root
,
605 struct super_block
*sb
= old
->mnt_sb
;
606 struct vfsmount
*mnt
= alloc_vfsmnt(old
->mnt_devname
);
609 if (flag
& (CL_SLAVE
| CL_PRIVATE
))
610 mnt
->mnt_group_id
= 0; /* not a peer of original */
612 mnt
->mnt_group_id
= old
->mnt_group_id
;
614 if ((flag
& CL_MAKE_SHARED
) && !mnt
->mnt_group_id
) {
615 int err
= mnt_alloc_group_id(mnt
);
620 mnt
->mnt_flags
= old
->mnt_flags
& ~MNT_WRITE_HOLD
;
621 atomic_inc(&sb
->s_active
);
623 mnt
->mnt_root
= dget(root
);
624 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
625 mnt
->mnt_parent
= mnt
;
627 if (flag
& CL_SLAVE
) {
628 list_add(&mnt
->mnt_slave
, &old
->mnt_slave_list
);
629 mnt
->mnt_master
= old
;
630 CLEAR_MNT_SHARED(mnt
);
631 } else if (!(flag
& CL_PRIVATE
)) {
632 if ((flag
& CL_MAKE_SHARED
) || IS_MNT_SHARED(old
))
633 list_add(&mnt
->mnt_share
, &old
->mnt_share
);
634 if (IS_MNT_SLAVE(old
))
635 list_add(&mnt
->mnt_slave
, &old
->mnt_slave
);
636 mnt
->mnt_master
= old
->mnt_master
;
638 if (flag
& CL_MAKE_SHARED
)
641 /* stick the duplicate mount on the same expiry list
642 * as the original if that was on one */
643 if (flag
& CL_EXPIRE
) {
644 if (!list_empty(&old
->mnt_expire
))
645 list_add(&mnt
->mnt_expire
, &old
->mnt_expire
);
655 static inline void __mntput(struct vfsmount
*mnt
)
657 struct super_block
*sb
= mnt
->mnt_sb
;
659 * This probably indicates that somebody messed
660 * up a mnt_want/drop_write() pair. If this
661 * happens, the filesystem was probably unable
662 * to make r/w->r/o transitions.
665 * atomic_dec_and_lock() used to deal with ->mnt_count decrements
666 * provides barriers, so count_mnt_writers() below is safe. AV
668 WARN_ON(count_mnt_writers(mnt
));
669 fsnotify_vfsmount_delete(mnt
);
672 deactivate_super(sb
);
675 void mntput_no_expire(struct vfsmount
*mnt
)
678 if (atomic_add_unless(&mnt
->mnt_count
, -1, 1))
680 br_write_lock(vfsmount_lock
);
681 if (!atomic_dec_and_test(&mnt
->mnt_count
)) {
682 br_write_unlock(vfsmount_lock
);
685 if (likely(!mnt
->mnt_pinned
)) {
686 br_write_unlock(vfsmount_lock
);
690 atomic_add(mnt
->mnt_pinned
+ 1, &mnt
->mnt_count
);
692 br_write_unlock(vfsmount_lock
);
693 acct_auto_close_mnt(mnt
);
696 EXPORT_SYMBOL(mntput_no_expire
);
698 void mnt_pin(struct vfsmount
*mnt
)
700 br_write_lock(vfsmount_lock
);
702 br_write_unlock(vfsmount_lock
);
705 EXPORT_SYMBOL(mnt_pin
);
707 void mnt_unpin(struct vfsmount
*mnt
)
709 br_write_lock(vfsmount_lock
);
710 if (mnt
->mnt_pinned
) {
711 atomic_inc(&mnt
->mnt_count
);
714 br_write_unlock(vfsmount_lock
);
717 EXPORT_SYMBOL(mnt_unpin
);
719 static inline void mangle(struct seq_file
*m
, const char *s
)
721 seq_escape(m
, s
, " \t\n\\");
725 * Simple .show_options callback for filesystems which don't want to
726 * implement more complex mount option showing.
728 * See also save_mount_options().
730 int generic_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
735 options
= rcu_dereference(mnt
->mnt_sb
->s_options
);
737 if (options
!= NULL
&& options
[0]) {
745 EXPORT_SYMBOL(generic_show_options
);
748 * If filesystem uses generic_show_options(), this function should be
749 * called from the fill_super() callback.
751 * The .remount_fs callback usually needs to be handled in a special
752 * way, to make sure, that previous options are not overwritten if the
755 * Also note, that if the filesystem's .remount_fs function doesn't
756 * reset all options to their default value, but changes only newly
757 * given options, then the displayed options will not reflect reality
760 void save_mount_options(struct super_block
*sb
, char *options
)
762 BUG_ON(sb
->s_options
);
763 rcu_assign_pointer(sb
->s_options
, kstrdup(options
, GFP_KERNEL
));
765 EXPORT_SYMBOL(save_mount_options
);
767 void replace_mount_options(struct super_block
*sb
, char *options
)
769 char *old
= sb
->s_options
;
770 rcu_assign_pointer(sb
->s_options
, options
);
776 EXPORT_SYMBOL(replace_mount_options
);
778 #ifdef CONFIG_PROC_FS
780 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
782 struct proc_mounts
*p
= m
->private;
784 down_read(&namespace_sem
);
785 return seq_list_start(&p
->ns
->list
, *pos
);
788 static void *m_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
790 struct proc_mounts
*p
= m
->private;
792 return seq_list_next(v
, &p
->ns
->list
, pos
);
795 static void m_stop(struct seq_file
*m
, void *v
)
797 up_read(&namespace_sem
);
800 int mnt_had_events(struct proc_mounts
*p
)
802 struct mnt_namespace
*ns
= p
->ns
;
805 br_read_lock(vfsmount_lock
);
806 if (p
->event
!= ns
->event
) {
807 p
->event
= ns
->event
;
810 br_read_unlock(vfsmount_lock
);
815 struct proc_fs_info
{
820 static int show_sb_opts(struct seq_file
*m
, struct super_block
*sb
)
822 static const struct proc_fs_info fs_info
[] = {
823 { MS_SYNCHRONOUS
, ",sync" },
824 { MS_DIRSYNC
, ",dirsync" },
825 { MS_MANDLOCK
, ",mand" },
828 const struct proc_fs_info
*fs_infop
;
830 for (fs_infop
= fs_info
; fs_infop
->flag
; fs_infop
++) {
831 if (sb
->s_flags
& fs_infop
->flag
)
832 seq_puts(m
, fs_infop
->str
);
835 return security_sb_show_options(m
, sb
);
838 static void show_mnt_opts(struct seq_file
*m
, struct vfsmount
*mnt
)
840 static const struct proc_fs_info mnt_info
[] = {
841 { MNT_NOSUID
, ",nosuid" },
842 { MNT_NODEV
, ",nodev" },
843 { MNT_NOEXEC
, ",noexec" },
844 { MNT_NOATIME
, ",noatime" },
845 { MNT_NODIRATIME
, ",nodiratime" },
846 { MNT_RELATIME
, ",relatime" },
849 const struct proc_fs_info
*fs_infop
;
851 for (fs_infop
= mnt_info
; fs_infop
->flag
; fs_infop
++) {
852 if (mnt
->mnt_flags
& fs_infop
->flag
)
853 seq_puts(m
, fs_infop
->str
);
857 static void show_type(struct seq_file
*m
, struct super_block
*sb
)
859 mangle(m
, sb
->s_type
->name
);
860 if (sb
->s_subtype
&& sb
->s_subtype
[0]) {
862 mangle(m
, sb
->s_subtype
);
866 static int show_vfsmnt(struct seq_file
*m
, void *v
)
868 struct vfsmount
*mnt
= list_entry(v
, struct vfsmount
, mnt_list
);
870 struct path mnt_path
= { .dentry
= mnt
->mnt_root
, .mnt
= mnt
};
872 mangle(m
, mnt
->mnt_devname
? mnt
->mnt_devname
: "none");
874 seq_path(m
, &mnt_path
, " \t\n\\");
876 show_type(m
, mnt
->mnt_sb
);
877 seq_puts(m
, __mnt_is_readonly(mnt
) ? " ro" : " rw");
878 err
= show_sb_opts(m
, mnt
->mnt_sb
);
881 show_mnt_opts(m
, mnt
);
882 if (mnt
->mnt_sb
->s_op
->show_options
)
883 err
= mnt
->mnt_sb
->s_op
->show_options(m
, mnt
);
884 seq_puts(m
, " 0 0\n");
889 const struct seq_operations mounts_op
= {
896 static int show_mountinfo(struct seq_file
*m
, void *v
)
898 struct proc_mounts
*p
= m
->private;
899 struct vfsmount
*mnt
= list_entry(v
, struct vfsmount
, mnt_list
);
900 struct super_block
*sb
= mnt
->mnt_sb
;
901 struct path mnt_path
= { .dentry
= mnt
->mnt_root
, .mnt
= mnt
};
902 struct path root
= p
->root
;
905 seq_printf(m
, "%i %i %u:%u ", mnt
->mnt_id
, mnt
->mnt_parent
->mnt_id
,
906 MAJOR(sb
->s_dev
), MINOR(sb
->s_dev
));
907 seq_dentry(m
, mnt
->mnt_root
, " \t\n\\");
909 seq_path_root(m
, &mnt_path
, &root
, " \t\n\\");
910 if (root
.mnt
!= p
->root
.mnt
|| root
.dentry
!= p
->root
.dentry
) {
912 * Mountpoint is outside root, discard that one. Ugly,
913 * but less so than trying to do that in iterator in a
914 * race-free way (due to renames).
918 seq_puts(m
, mnt
->mnt_flags
& MNT_READONLY
? " ro" : " rw");
919 show_mnt_opts(m
, mnt
);
921 /* Tagged fields ("foo:X" or "bar") */
922 if (IS_MNT_SHARED(mnt
))
923 seq_printf(m
, " shared:%i", mnt
->mnt_group_id
);
924 if (IS_MNT_SLAVE(mnt
)) {
925 int master
= mnt
->mnt_master
->mnt_group_id
;
926 int dom
= get_dominating_id(mnt
, &p
->root
);
927 seq_printf(m
, " master:%i", master
);
928 if (dom
&& dom
!= master
)
929 seq_printf(m
, " propagate_from:%i", dom
);
931 if (IS_MNT_UNBINDABLE(mnt
))
932 seq_puts(m
, " unbindable");
934 /* Filesystem specific data */
938 mangle(m
, mnt
->mnt_devname
? mnt
->mnt_devname
: "none");
939 seq_puts(m
, sb
->s_flags
& MS_RDONLY
? " ro" : " rw");
940 err
= show_sb_opts(m
, sb
);
943 if (sb
->s_op
->show_options
)
944 err
= sb
->s_op
->show_options(m
, mnt
);
950 const struct seq_operations mountinfo_op
= {
954 .show
= show_mountinfo
,
957 static int show_vfsstat(struct seq_file
*m
, void *v
)
959 struct vfsmount
*mnt
= list_entry(v
, struct vfsmount
, mnt_list
);
960 struct path mnt_path
= { .dentry
= mnt
->mnt_root
, .mnt
= mnt
};
964 if (mnt
->mnt_devname
) {
965 seq_puts(m
, "device ");
966 mangle(m
, mnt
->mnt_devname
);
968 seq_puts(m
, "no device");
971 seq_puts(m
, " mounted on ");
972 seq_path(m
, &mnt_path
, " \t\n\\");
975 /* file system type */
976 seq_puts(m
, "with fstype ");
977 show_type(m
, mnt
->mnt_sb
);
979 /* optional statistics */
980 if (mnt
->mnt_sb
->s_op
->show_stats
) {
982 err
= mnt
->mnt_sb
->s_op
->show_stats(m
, mnt
);
989 const struct seq_operations mountstats_op
= {
993 .show
= show_vfsstat
,
995 #endif /* CONFIG_PROC_FS */
998 * may_umount_tree - check if a mount tree is busy
999 * @mnt: root of mount tree
1001 * This is called to check if a tree of mounts has any
1002 * open files, pwds, chroots or sub mounts that are
1005 int may_umount_tree(struct vfsmount
*mnt
)
1007 int actual_refs
= 0;
1008 int minimum_refs
= 0;
1011 br_read_lock(vfsmount_lock
);
1012 for (p
= mnt
; p
; p
= next_mnt(p
, mnt
)) {
1013 actual_refs
+= atomic_read(&p
->mnt_count
);
1016 br_read_unlock(vfsmount_lock
);
1018 if (actual_refs
> minimum_refs
)
1024 EXPORT_SYMBOL(may_umount_tree
);
1027 * may_umount - check if a mount point is busy
1028 * @mnt: root of mount
1030 * This is called to check if a mount point has any
1031 * open files, pwds, chroots or sub mounts. If the
1032 * mount has sub mounts this will return busy
1033 * regardless of whether the sub mounts are busy.
1035 * Doesn't take quota and stuff into account. IOW, in some cases it will
1036 * give false negatives. The main reason why it's here is that we need
1037 * a non-destructive way to look for easily umountable filesystems.
1039 int may_umount(struct vfsmount
*mnt
)
1042 down_read(&namespace_sem
);
1043 br_read_lock(vfsmount_lock
);
1044 if (propagate_mount_busy(mnt
, 2))
1046 br_read_unlock(vfsmount_lock
);
1047 up_read(&namespace_sem
);
1051 EXPORT_SYMBOL(may_umount
);
1053 void release_mounts(struct list_head
*head
)
1055 struct vfsmount
*mnt
;
1056 while (!list_empty(head
)) {
1057 mnt
= list_first_entry(head
, struct vfsmount
, mnt_hash
);
1058 list_del_init(&mnt
->mnt_hash
);
1059 if (mnt
->mnt_parent
!= mnt
) {
1060 struct dentry
*dentry
;
1063 br_write_lock(vfsmount_lock
);
1064 dentry
= mnt
->mnt_mountpoint
;
1065 m
= mnt
->mnt_parent
;
1066 mnt
->mnt_mountpoint
= mnt
->mnt_root
;
1067 mnt
->mnt_parent
= mnt
;
1069 br_write_unlock(vfsmount_lock
);
1078 * vfsmount lock must be held for write
1079 * namespace_sem must be held for write
1081 void umount_tree(struct vfsmount
*mnt
, int propagate
, struct list_head
*kill
)
1085 for (p
= mnt
; p
; p
= next_mnt(p
, mnt
))
1086 list_move(&p
->mnt_hash
, kill
);
1089 propagate_umount(kill
);
1091 list_for_each_entry(p
, kill
, mnt_hash
) {
1092 list_del_init(&p
->mnt_expire
);
1093 list_del_init(&p
->mnt_list
);
1094 __touch_mnt_namespace(p
->mnt_ns
);
1096 list_del_init(&p
->mnt_child
);
1097 if (p
->mnt_parent
!= p
) {
1098 p
->mnt_parent
->mnt_ghosts
++;
1099 dentry_reset_mounted(p
->mnt_parent
, p
->mnt_mountpoint
);
1101 change_mnt_propagation(p
, MS_PRIVATE
);
1105 static void shrink_submounts(struct vfsmount
*mnt
, struct list_head
*umounts
);
1107 static int do_umount(struct vfsmount
*mnt
, int flags
)
1109 struct super_block
*sb
= mnt
->mnt_sb
;
1111 LIST_HEAD(umount_list
);
1113 retval
= security_sb_umount(mnt
, flags
);
1118 * Allow userspace to request a mountpoint be expired rather than
1119 * unmounting unconditionally. Unmount only happens if:
1120 * (1) the mark is already set (the mark is cleared by mntput())
1121 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
1123 if (flags
& MNT_EXPIRE
) {
1124 if (mnt
== current
->fs
->root
.mnt
||
1125 flags
& (MNT_FORCE
| MNT_DETACH
))
1128 if (atomic_read(&mnt
->mnt_count
) != 2)
1131 if (!xchg(&mnt
->mnt_expiry_mark
, 1))
1136 * If we may have to abort operations to get out of this
1137 * mount, and they will themselves hold resources we must
1138 * allow the fs to do things. In the Unix tradition of
1139 * 'Gee thats tricky lets do it in userspace' the umount_begin
1140 * might fail to complete on the first run through as other tasks
1141 * must return, and the like. Thats for the mount program to worry
1142 * about for the moment.
1145 if (flags
& MNT_FORCE
&& sb
->s_op
->umount_begin
) {
1146 sb
->s_op
->umount_begin(sb
);
1150 * No sense to grab the lock for this test, but test itself looks
1151 * somewhat bogus. Suggestions for better replacement?
1152 * Ho-hum... In principle, we might treat that as umount + switch
1153 * to rootfs. GC would eventually take care of the old vfsmount.
1154 * Actually it makes sense, especially if rootfs would contain a
1155 * /reboot - static binary that would close all descriptors and
1156 * call reboot(9). Then init(8) could umount root and exec /reboot.
1158 if (mnt
== current
->fs
->root
.mnt
&& !(flags
& MNT_DETACH
)) {
1160 * Special case for "unmounting" root ...
1161 * we just try to remount it readonly.
1163 down_write(&sb
->s_umount
);
1164 if (!(sb
->s_flags
& MS_RDONLY
))
1165 retval
= do_remount_sb(sb
, MS_RDONLY
, NULL
, 0);
1166 up_write(&sb
->s_umount
);
1170 down_write(&namespace_sem
);
1171 br_write_lock(vfsmount_lock
);
1174 if (!(flags
& MNT_DETACH
))
1175 shrink_submounts(mnt
, &umount_list
);
1178 if (flags
& MNT_DETACH
|| !propagate_mount_busy(mnt
, 2)) {
1179 if (!list_empty(&mnt
->mnt_list
))
1180 umount_tree(mnt
, 1, &umount_list
);
1183 br_write_unlock(vfsmount_lock
);
1184 up_write(&namespace_sem
);
1185 release_mounts(&umount_list
);
1190 * Now umount can handle mount points as well as block devices.
1191 * This is important for filesystems which use unnamed block devices.
1193 * We now support a flag for forced unmount like the other 'big iron'
1194 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
1197 SYSCALL_DEFINE2(umount
, char __user
*, name
, int, flags
)
1201 int lookup_flags
= 0;
1203 if (flags
& ~(MNT_FORCE
| MNT_DETACH
| MNT_EXPIRE
| UMOUNT_NOFOLLOW
))
1206 if (!(flags
& UMOUNT_NOFOLLOW
))
1207 lookup_flags
|= LOOKUP_FOLLOW
;
1209 retval
= user_path_at(AT_FDCWD
, name
, lookup_flags
, &path
);
1213 if (path
.dentry
!= path
.mnt
->mnt_root
)
1215 if (!check_mnt(path
.mnt
))
1219 if (!capable(CAP_SYS_ADMIN
))
1222 retval
= do_umount(path
.mnt
, flags
);
1224 /* we mustn't call path_put() as that would clear mnt_expiry_mark */
1226 mntput_no_expire(path
.mnt
);
1231 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
1234 * The 2.0 compatible umount. No flags.
1236 SYSCALL_DEFINE1(oldumount
, char __user
*, name
)
1238 return sys_umount(name
, 0);
1243 static int mount_is_safe(struct path
*path
)
1245 if (capable(CAP_SYS_ADMIN
))
1249 if (S_ISLNK(path
->dentry
->d_inode
->i_mode
))
1251 if (path
->dentry
->d_inode
->i_mode
& S_ISVTX
) {
1252 if (current_uid() != path
->dentry
->d_inode
->i_uid
)
1255 if (inode_permission(path
->dentry
->d_inode
, MAY_WRITE
))
1261 struct vfsmount
*copy_tree(struct vfsmount
*mnt
, struct dentry
*dentry
,
1264 struct vfsmount
*res
, *p
, *q
, *r
, *s
;
1267 if (!(flag
& CL_COPY_ALL
) && IS_MNT_UNBINDABLE(mnt
))
1270 res
= q
= clone_mnt(mnt
, dentry
, flag
);
1273 q
->mnt_mountpoint
= mnt
->mnt_mountpoint
;
1276 list_for_each_entry(r
, &mnt
->mnt_mounts
, mnt_child
) {
1277 if (!is_subdir(r
->mnt_mountpoint
, dentry
))
1280 for (s
= r
; s
; s
= next_mnt(s
, r
)) {
1281 if (!(flag
& CL_COPY_ALL
) && IS_MNT_UNBINDABLE(s
)) {
1282 s
= skip_mnt_tree(s
);
1285 while (p
!= s
->mnt_parent
) {
1291 path
.dentry
= p
->mnt_mountpoint
;
1292 q
= clone_mnt(p
, p
->mnt_root
, flag
);
1295 br_write_lock(vfsmount_lock
);
1296 list_add_tail(&q
->mnt_list
, &res
->mnt_list
);
1297 attach_mnt(q
, &path
);
1298 br_write_unlock(vfsmount_lock
);
1304 LIST_HEAD(umount_list
);
1305 br_write_lock(vfsmount_lock
);
1306 umount_tree(res
, 0, &umount_list
);
1307 br_write_unlock(vfsmount_lock
);
1308 release_mounts(&umount_list
);
1313 struct vfsmount
*collect_mounts(struct path
*path
)
1315 struct vfsmount
*tree
;
1316 down_write(&namespace_sem
);
1317 tree
= copy_tree(path
->mnt
, path
->dentry
, CL_COPY_ALL
| CL_PRIVATE
);
1318 up_write(&namespace_sem
);
1322 void drop_collected_mounts(struct vfsmount
*mnt
)
1324 LIST_HEAD(umount_list
);
1325 down_write(&namespace_sem
);
1326 br_write_lock(vfsmount_lock
);
1327 umount_tree(mnt
, 0, &umount_list
);
1328 br_write_unlock(vfsmount_lock
);
1329 up_write(&namespace_sem
);
1330 release_mounts(&umount_list
);
1333 int iterate_mounts(int (*f
)(struct vfsmount
*, void *), void *arg
,
1334 struct vfsmount
*root
)
1336 struct vfsmount
*mnt
;
1337 int res
= f(root
, arg
);
1340 list_for_each_entry(mnt
, &root
->mnt_list
, mnt_list
) {
1348 static void cleanup_group_ids(struct vfsmount
*mnt
, struct vfsmount
*end
)
1352 for (p
= mnt
; p
!= end
; p
= next_mnt(p
, mnt
)) {
1353 if (p
->mnt_group_id
&& !IS_MNT_SHARED(p
))
1354 mnt_release_group_id(p
);
1358 static int invent_group_ids(struct vfsmount
*mnt
, bool recurse
)
1362 for (p
= mnt
; p
; p
= recurse
? next_mnt(p
, mnt
) : NULL
) {
1363 if (!p
->mnt_group_id
&& !IS_MNT_SHARED(p
)) {
1364 int err
= mnt_alloc_group_id(p
);
1366 cleanup_group_ids(mnt
, p
);
1376 * @source_mnt : mount tree to be attached
1377 * @nd : place the mount tree @source_mnt is attached
1378 * @parent_nd : if non-null, detach the source_mnt from its parent and
1379 * store the parent mount and mountpoint dentry.
1380 * (done when source_mnt is moved)
1382 * NOTE: in the table below explains the semantics when a source mount
1383 * of a given type is attached to a destination mount of a given type.
1384 * ---------------------------------------------------------------------------
1385 * | BIND MOUNT OPERATION |
1386 * |**************************************************************************
1387 * | source-->| shared | private | slave | unbindable |
1391 * |**************************************************************************
1392 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
1394 * |non-shared| shared (+) | private | slave (*) | invalid |
1395 * ***************************************************************************
1396 * A bind operation clones the source mount and mounts the clone on the
1397 * destination mount.
1399 * (++) the cloned mount is propagated to all the mounts in the propagation
1400 * tree of the destination mount and the cloned mount is added to
1401 * the peer group of the source mount.
1402 * (+) the cloned mount is created under the destination mount and is marked
1403 * as shared. The cloned mount is added to the peer group of the source
1405 * (+++) the mount is propagated to all the mounts in the propagation tree
1406 * of the destination mount and the cloned mount is made slave
1407 * of the same master as that of the source mount. The cloned mount
1408 * is marked as 'shared and slave'.
1409 * (*) the cloned mount is made a slave of the same master as that of the
1412 * ---------------------------------------------------------------------------
1413 * | MOVE MOUNT OPERATION |
1414 * |**************************************************************************
1415 * | source-->| shared | private | slave | unbindable |
1419 * |**************************************************************************
1420 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
1422 * |non-shared| shared (+*) | private | slave (*) | unbindable |
1423 * ***************************************************************************
1425 * (+) the mount is moved to the destination. And is then propagated to
1426 * all the mounts in the propagation tree of the destination mount.
1427 * (+*) the mount is moved to the destination.
1428 * (+++) the mount is moved to the destination and is then propagated to
1429 * all the mounts belonging to the destination mount's propagation tree.
1430 * the mount is marked as 'shared and slave'.
1431 * (*) the mount continues to be a slave at the new location.
1433 * if the source mount is a tree, the operations explained above is
1434 * applied to each mount in the tree.
1435 * Must be called without spinlocks held, since this function can sleep
1438 static int attach_recursive_mnt(struct vfsmount
*source_mnt
,
1439 struct path
*path
, struct path
*parent_path
)
1441 LIST_HEAD(tree_list
);
1442 struct vfsmount
*dest_mnt
= path
->mnt
;
1443 struct dentry
*dest_dentry
= path
->dentry
;
1444 struct vfsmount
*child
, *p
;
1447 if (IS_MNT_SHARED(dest_mnt
)) {
1448 err
= invent_group_ids(source_mnt
, true);
1452 err
= propagate_mnt(dest_mnt
, dest_dentry
, source_mnt
, &tree_list
);
1454 goto out_cleanup_ids
;
1456 br_write_lock(vfsmount_lock
);
1458 if (IS_MNT_SHARED(dest_mnt
)) {
1459 for (p
= source_mnt
; p
; p
= next_mnt(p
, source_mnt
))
1463 detach_mnt(source_mnt
, parent_path
);
1464 attach_mnt(source_mnt
, path
);
1465 touch_mnt_namespace(parent_path
->mnt
->mnt_ns
);
1467 mnt_set_mountpoint(dest_mnt
, dest_dentry
, source_mnt
);
1468 commit_tree(source_mnt
);
1471 list_for_each_entry_safe(child
, p
, &tree_list
, mnt_hash
) {
1472 list_del_init(&child
->mnt_hash
);
1475 br_write_unlock(vfsmount_lock
);
1480 if (IS_MNT_SHARED(dest_mnt
))
1481 cleanup_group_ids(source_mnt
, NULL
);
1486 static int graft_tree(struct vfsmount
*mnt
, struct path
*path
)
1489 if (mnt
->mnt_sb
->s_flags
& MS_NOUSER
)
1492 if (S_ISDIR(path
->dentry
->d_inode
->i_mode
) !=
1493 S_ISDIR(mnt
->mnt_root
->d_inode
->i_mode
))
1497 mutex_lock(&path
->dentry
->d_inode
->i_mutex
);
1498 if (cant_mount(path
->dentry
))
1501 if (!d_unlinked(path
->dentry
))
1502 err
= attach_recursive_mnt(mnt
, path
, NULL
);
1504 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
1509 * Sanity check the flags to change_mnt_propagation.
1512 static int flags_to_propagation_type(int flags
)
1514 int type
= flags
& ~MS_REC
;
1516 /* Fail if any non-propagation flags are set */
1517 if (type
& ~(MS_SHARED
| MS_PRIVATE
| MS_SLAVE
| MS_UNBINDABLE
))
1519 /* Only one propagation flag should be set */
1520 if (!is_power_of_2(type
))
1526 * recursively change the type of the mountpoint.
1528 static int do_change_type(struct path
*path
, int flag
)
1530 struct vfsmount
*m
, *mnt
= path
->mnt
;
1531 int recurse
= flag
& MS_REC
;
1535 if (!capable(CAP_SYS_ADMIN
))
1538 if (path
->dentry
!= path
->mnt
->mnt_root
)
1541 type
= flags_to_propagation_type(flag
);
1545 down_write(&namespace_sem
);
1546 if (type
== MS_SHARED
) {
1547 err
= invent_group_ids(mnt
, recurse
);
1552 br_write_lock(vfsmount_lock
);
1553 for (m
= mnt
; m
; m
= (recurse
? next_mnt(m
, mnt
) : NULL
))
1554 change_mnt_propagation(m
, type
);
1555 br_write_unlock(vfsmount_lock
);
1558 up_write(&namespace_sem
);
1563 * do loopback mount.
1565 static int do_loopback(struct path
*path
, char *old_name
,
1568 struct path old_path
;
1569 struct vfsmount
*mnt
= NULL
;
1570 int err
= mount_is_safe(path
);
1573 if (!old_name
|| !*old_name
)
1575 err
= kern_path(old_name
, LOOKUP_FOLLOW
, &old_path
);
1579 down_write(&namespace_sem
);
1581 if (IS_MNT_UNBINDABLE(old_path
.mnt
))
1584 if (!check_mnt(path
->mnt
) || !check_mnt(old_path
.mnt
))
1589 mnt
= copy_tree(old_path
.mnt
, old_path
.dentry
, 0);
1591 mnt
= clone_mnt(old_path
.mnt
, old_path
.dentry
, 0);
1596 err
= graft_tree(mnt
, path
);
1598 LIST_HEAD(umount_list
);
1600 br_write_lock(vfsmount_lock
);
1601 umount_tree(mnt
, 0, &umount_list
);
1602 br_write_unlock(vfsmount_lock
);
1603 release_mounts(&umount_list
);
1607 up_write(&namespace_sem
);
1608 path_put(&old_path
);
1612 static int change_mount_flags(struct vfsmount
*mnt
, int ms_flags
)
1615 int readonly_request
= 0;
1617 if (ms_flags
& MS_RDONLY
)
1618 readonly_request
= 1;
1619 if (readonly_request
== __mnt_is_readonly(mnt
))
1622 if (readonly_request
)
1623 error
= mnt_make_readonly(mnt
);
1625 __mnt_unmake_readonly(mnt
);
1630 * change filesystem flags. dir should be a physical root of filesystem.
1631 * If you've mounted a non-root directory somewhere and want to do remount
1632 * on it - tough luck.
1634 static int do_remount(struct path
*path
, int flags
, int mnt_flags
,
1638 struct super_block
*sb
= path
->mnt
->mnt_sb
;
1640 if (!capable(CAP_SYS_ADMIN
))
1643 if (!check_mnt(path
->mnt
))
1646 if (path
->dentry
!= path
->mnt
->mnt_root
)
1649 down_write(&sb
->s_umount
);
1650 if (flags
& MS_BIND
)
1651 err
= change_mount_flags(path
->mnt
, flags
);
1653 err
= do_remount_sb(sb
, flags
, data
, 0);
1655 br_write_lock(vfsmount_lock
);
1656 mnt_flags
|= path
->mnt
->mnt_flags
& MNT_PROPAGATION_MASK
;
1657 path
->mnt
->mnt_flags
= mnt_flags
;
1658 br_write_unlock(vfsmount_lock
);
1660 up_write(&sb
->s_umount
);
1662 br_write_lock(vfsmount_lock
);
1663 touch_mnt_namespace(path
->mnt
->mnt_ns
);
1664 br_write_unlock(vfsmount_lock
);
1669 static inline int tree_contains_unbindable(struct vfsmount
*mnt
)
1672 for (p
= mnt
; p
; p
= next_mnt(p
, mnt
)) {
1673 if (IS_MNT_UNBINDABLE(p
))
1679 static int do_move_mount(struct path
*path
, char *old_name
)
1681 struct path old_path
, parent_path
;
1684 if (!capable(CAP_SYS_ADMIN
))
1686 if (!old_name
|| !*old_name
)
1688 err
= kern_path(old_name
, LOOKUP_FOLLOW
, &old_path
);
1692 down_write(&namespace_sem
);
1693 while (d_mountpoint(path
->dentry
) &&
1697 if (!check_mnt(path
->mnt
) || !check_mnt(old_path
.mnt
))
1701 mutex_lock(&path
->dentry
->d_inode
->i_mutex
);
1702 if (cant_mount(path
->dentry
))
1705 if (d_unlinked(path
->dentry
))
1709 if (old_path
.dentry
!= old_path
.mnt
->mnt_root
)
1712 if (old_path
.mnt
== old_path
.mnt
->mnt_parent
)
1715 if (S_ISDIR(path
->dentry
->d_inode
->i_mode
) !=
1716 S_ISDIR(old_path
.dentry
->d_inode
->i_mode
))
1719 * Don't move a mount residing in a shared parent.
1721 if (old_path
.mnt
->mnt_parent
&&
1722 IS_MNT_SHARED(old_path
.mnt
->mnt_parent
))
1725 * Don't move a mount tree containing unbindable mounts to a destination
1726 * mount which is shared.
1728 if (IS_MNT_SHARED(path
->mnt
) &&
1729 tree_contains_unbindable(old_path
.mnt
))
1732 for (p
= path
->mnt
; p
->mnt_parent
!= p
; p
= p
->mnt_parent
)
1733 if (p
== old_path
.mnt
)
1736 err
= attach_recursive_mnt(old_path
.mnt
, path
, &parent_path
);
1740 /* if the mount is moved, it should no longer be expire
1742 list_del_init(&old_path
.mnt
->mnt_expire
);
1744 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
1746 up_write(&namespace_sem
);
1748 path_put(&parent_path
);
1749 path_put(&old_path
);
1754 * create a new mount for userspace and request it to be added into the
1757 static int do_new_mount(struct path
*path
, char *type
, int flags
,
1758 int mnt_flags
, char *name
, void *data
)
1760 struct vfsmount
*mnt
;
1765 /* we need capabilities... */
1766 if (!capable(CAP_SYS_ADMIN
))
1769 mnt
= do_kern_mount(type
, flags
, name
, data
);
1771 return PTR_ERR(mnt
);
1773 return do_add_mount(mnt
, path
, mnt_flags
, NULL
);
1777 * add a mount into a namespace's mount tree
1778 * - provide the option of adding the new mount to an expiration list
1780 int do_add_mount(struct vfsmount
*newmnt
, struct path
*path
,
1781 int mnt_flags
, struct list_head
*fslist
)
1785 mnt_flags
&= ~(MNT_SHARED
| MNT_WRITE_HOLD
| MNT_INTERNAL
);
1787 down_write(&namespace_sem
);
1788 /* Something was mounted here while we slept */
1789 while (d_mountpoint(path
->dentry
) &&
1793 if (!(mnt_flags
& MNT_SHRINKABLE
) && !check_mnt(path
->mnt
))
1796 /* Refuse the same filesystem on the same mount point */
1798 if (path
->mnt
->mnt_sb
== newmnt
->mnt_sb
&&
1799 path
->mnt
->mnt_root
== path
->dentry
)
1803 if (S_ISLNK(newmnt
->mnt_root
->d_inode
->i_mode
))
1806 newmnt
->mnt_flags
= mnt_flags
;
1807 if ((err
= graft_tree(newmnt
, path
)))
1810 if (fslist
) /* add to the specified expiration list */
1811 list_add_tail(&newmnt
->mnt_expire
, fslist
);
1813 up_write(&namespace_sem
);
1817 up_write(&namespace_sem
);
1822 EXPORT_SYMBOL_GPL(do_add_mount
);
1825 * process a list of expirable mountpoints with the intent of discarding any
1826 * mountpoints that aren't in use and haven't been touched since last we came
1829 void mark_mounts_for_expiry(struct list_head
*mounts
)
1831 struct vfsmount
*mnt
, *next
;
1832 LIST_HEAD(graveyard
);
1835 if (list_empty(mounts
))
1838 down_write(&namespace_sem
);
1839 br_write_lock(vfsmount_lock
);
1841 /* extract from the expiration list every vfsmount that matches the
1842 * following criteria:
1843 * - only referenced by its parent vfsmount
1844 * - still marked for expiry (marked on the last call here; marks are
1845 * cleared by mntput())
1847 list_for_each_entry_safe(mnt
, next
, mounts
, mnt_expire
) {
1848 if (!xchg(&mnt
->mnt_expiry_mark
, 1) ||
1849 propagate_mount_busy(mnt
, 1))
1851 list_move(&mnt
->mnt_expire
, &graveyard
);
1853 while (!list_empty(&graveyard
)) {
1854 mnt
= list_first_entry(&graveyard
, struct vfsmount
, mnt_expire
);
1855 touch_mnt_namespace(mnt
->mnt_ns
);
1856 umount_tree(mnt
, 1, &umounts
);
1858 br_write_unlock(vfsmount_lock
);
1859 up_write(&namespace_sem
);
1861 release_mounts(&umounts
);
1864 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry
);
1867 * Ripoff of 'select_parent()'
1869 * search the list of submounts for a given mountpoint, and move any
1870 * shrinkable submounts to the 'graveyard' list.
1872 static int select_submounts(struct vfsmount
*parent
, struct list_head
*graveyard
)
1874 struct vfsmount
*this_parent
= parent
;
1875 struct list_head
*next
;
1879 next
= this_parent
->mnt_mounts
.next
;
1881 while (next
!= &this_parent
->mnt_mounts
) {
1882 struct list_head
*tmp
= next
;
1883 struct vfsmount
*mnt
= list_entry(tmp
, struct vfsmount
, mnt_child
);
1886 if (!(mnt
->mnt_flags
& MNT_SHRINKABLE
))
1889 * Descend a level if the d_mounts list is non-empty.
1891 if (!list_empty(&mnt
->mnt_mounts
)) {
1896 if (!propagate_mount_busy(mnt
, 1)) {
1897 list_move_tail(&mnt
->mnt_expire
, graveyard
);
1902 * All done at this level ... ascend and resume the search
1904 if (this_parent
!= parent
) {
1905 next
= this_parent
->mnt_child
.next
;
1906 this_parent
= this_parent
->mnt_parent
;
1913 * process a list of expirable mountpoints with the intent of discarding any
1914 * submounts of a specific parent mountpoint
1916 * vfsmount_lock must be held for write
1918 static void shrink_submounts(struct vfsmount
*mnt
, struct list_head
*umounts
)
1920 LIST_HEAD(graveyard
);
1923 /* extract submounts of 'mountpoint' from the expiration list */
1924 while (select_submounts(mnt
, &graveyard
)) {
1925 while (!list_empty(&graveyard
)) {
1926 m
= list_first_entry(&graveyard
, struct vfsmount
,
1928 touch_mnt_namespace(m
->mnt_ns
);
1929 umount_tree(m
, 1, umounts
);
1935 * Some copy_from_user() implementations do not return the exact number of
1936 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1937 * Note that this function differs from copy_from_user() in that it will oops
1938 * on bad values of `to', rather than returning a short copy.
1940 static long exact_copy_from_user(void *to
, const void __user
* from
,
1944 const char __user
*f
= from
;
1947 if (!access_ok(VERIFY_READ
, from
, n
))
1951 if (__get_user(c
, f
)) {
1962 int copy_mount_options(const void __user
* data
, unsigned long *where
)
1972 if (!(page
= __get_free_page(GFP_KERNEL
)))
1975 /* We only care that *some* data at the address the user
1976 * gave us is valid. Just in case, we'll zero
1977 * the remainder of the page.
1979 /* copy_from_user cannot cross TASK_SIZE ! */
1980 size
= TASK_SIZE
- (unsigned long)data
;
1981 if (size
> PAGE_SIZE
)
1984 i
= size
- exact_copy_from_user((void *)page
, data
, size
);
1990 memset((char *)page
+ i
, 0, PAGE_SIZE
- i
);
1995 int copy_mount_string(const void __user
*data
, char **where
)
2004 tmp
= strndup_user(data
, PAGE_SIZE
);
2006 return PTR_ERR(tmp
);
2013 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
2014 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
2016 * data is a (void *) that can point to any structure up to
2017 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
2018 * information (or be NULL).
2020 * Pre-0.97 versions of mount() didn't have a flags word.
2021 * When the flags word was introduced its top half was required
2022 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
2023 * Therefore, if this magic number is present, it carries no information
2024 * and must be discarded.
2026 long do_mount(char *dev_name
, char *dir_name
, char *type_page
,
2027 unsigned long flags
, void *data_page
)
2034 if ((flags
& MS_MGC_MSK
) == MS_MGC_VAL
)
2035 flags
&= ~MS_MGC_MSK
;
2037 /* Basic sanity checks */
2039 if (!dir_name
|| !*dir_name
|| !memchr(dir_name
, 0, PAGE_SIZE
))
2043 ((char *)data_page
)[PAGE_SIZE
- 1] = 0;
2045 /* ... and get the mountpoint */
2046 retval
= kern_path(dir_name
, LOOKUP_FOLLOW
, &path
);
2050 retval
= security_sb_mount(dev_name
, &path
,
2051 type_page
, flags
, data_page
);
2055 /* Default to relatime unless overriden */
2056 if (!(flags
& MS_NOATIME
))
2057 mnt_flags
|= MNT_RELATIME
;
2059 /* Separate the per-mountpoint flags */
2060 if (flags
& MS_NOSUID
)
2061 mnt_flags
|= MNT_NOSUID
;
2062 if (flags
& MS_NODEV
)
2063 mnt_flags
|= MNT_NODEV
;
2064 if (flags
& MS_NOEXEC
)
2065 mnt_flags
|= MNT_NOEXEC
;
2066 if (flags
& MS_NOATIME
)
2067 mnt_flags
|= MNT_NOATIME
;
2068 if (flags
& MS_NODIRATIME
)
2069 mnt_flags
|= MNT_NODIRATIME
;
2070 if (flags
& MS_STRICTATIME
)
2071 mnt_flags
&= ~(MNT_RELATIME
| MNT_NOATIME
);
2072 if (flags
& MS_RDONLY
)
2073 mnt_flags
|= MNT_READONLY
;
2075 flags
&= ~(MS_NOSUID
| MS_NOEXEC
| MS_NODEV
| MS_ACTIVE
| MS_BORN
|
2076 MS_NOATIME
| MS_NODIRATIME
| MS_RELATIME
| MS_KERNMOUNT
|
2079 if (flags
& MS_REMOUNT
)
2080 retval
= do_remount(&path
, flags
& ~MS_REMOUNT
, mnt_flags
,
2082 else if (flags
& MS_BIND
)
2083 retval
= do_loopback(&path
, dev_name
, flags
& MS_REC
);
2084 else if (flags
& (MS_SHARED
| MS_PRIVATE
| MS_SLAVE
| MS_UNBINDABLE
))
2085 retval
= do_change_type(&path
, flags
);
2086 else if (flags
& MS_MOVE
)
2087 retval
= do_move_mount(&path
, dev_name
);
2089 retval
= do_new_mount(&path
, type_page
, flags
, mnt_flags
,
2090 dev_name
, data_page
);
2096 static struct mnt_namespace
*alloc_mnt_ns(void)
2098 struct mnt_namespace
*new_ns
;
2100 new_ns
= kmalloc(sizeof(struct mnt_namespace
), GFP_KERNEL
);
2102 return ERR_PTR(-ENOMEM
);
2103 atomic_set(&new_ns
->count
, 1);
2104 new_ns
->root
= NULL
;
2105 INIT_LIST_HEAD(&new_ns
->list
);
2106 init_waitqueue_head(&new_ns
->poll
);
2112 * Allocate a new namespace structure and populate it with contents
2113 * copied from the namespace of the passed in task structure.
2115 static struct mnt_namespace
*dup_mnt_ns(struct mnt_namespace
*mnt_ns
,
2116 struct fs_struct
*fs
)
2118 struct mnt_namespace
*new_ns
;
2119 struct vfsmount
*rootmnt
= NULL
, *pwdmnt
= NULL
;
2120 struct vfsmount
*p
, *q
;
2122 new_ns
= alloc_mnt_ns();
2126 down_write(&namespace_sem
);
2127 /* First pass: copy the tree topology */
2128 new_ns
->root
= copy_tree(mnt_ns
->root
, mnt_ns
->root
->mnt_root
,
2129 CL_COPY_ALL
| CL_EXPIRE
);
2130 if (!new_ns
->root
) {
2131 up_write(&namespace_sem
);
2133 return ERR_PTR(-ENOMEM
);
2135 br_write_lock(vfsmount_lock
);
2136 list_add_tail(&new_ns
->list
, &new_ns
->root
->mnt_list
);
2137 br_write_unlock(vfsmount_lock
);
2140 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
2141 * as belonging to new namespace. We have already acquired a private
2142 * fs_struct, so tsk->fs->lock is not needed.
2149 if (p
== fs
->root
.mnt
) {
2151 fs
->root
.mnt
= mntget(q
);
2153 if (p
== fs
->pwd
.mnt
) {
2155 fs
->pwd
.mnt
= mntget(q
);
2158 p
= next_mnt(p
, mnt_ns
->root
);
2159 q
= next_mnt(q
, new_ns
->root
);
2161 up_write(&namespace_sem
);
2171 struct mnt_namespace
*copy_mnt_ns(unsigned long flags
, struct mnt_namespace
*ns
,
2172 struct fs_struct
*new_fs
)
2174 struct mnt_namespace
*new_ns
;
2179 if (!(flags
& CLONE_NEWNS
))
2182 new_ns
= dup_mnt_ns(ns
, new_fs
);
2189 * create_mnt_ns - creates a private namespace and adds a root filesystem
2190 * @mnt: pointer to the new root filesystem mountpoint
2192 struct mnt_namespace
*create_mnt_ns(struct vfsmount
*mnt
)
2194 struct mnt_namespace
*new_ns
;
2196 new_ns
= alloc_mnt_ns();
2197 if (!IS_ERR(new_ns
)) {
2198 mnt
->mnt_ns
= new_ns
;
2200 list_add(&new_ns
->list
, &new_ns
->root
->mnt_list
);
2204 EXPORT_SYMBOL(create_mnt_ns
);
2206 SYSCALL_DEFINE5(mount
, char __user
*, dev_name
, char __user
*, dir_name
,
2207 char __user
*, type
, unsigned long, flags
, void __user
*, data
)
2213 unsigned long data_page
;
2215 ret
= copy_mount_string(type
, &kernel_type
);
2219 kernel_dir
= getname(dir_name
);
2220 if (IS_ERR(kernel_dir
)) {
2221 ret
= PTR_ERR(kernel_dir
);
2225 ret
= copy_mount_string(dev_name
, &kernel_dev
);
2229 ret
= copy_mount_options(data
, &data_page
);
2233 ret
= do_mount(kernel_dev
, kernel_dir
, kernel_type
, flags
,
2234 (void *) data_page
);
2236 free_page(data_page
);
2240 putname(kernel_dir
);
2248 * pivot_root Semantics:
2249 * Moves the root file system of the current process to the directory put_old,
2250 * makes new_root as the new root file system of the current process, and sets
2251 * root/cwd of all processes which had them on the current root to new_root.
2254 * The new_root and put_old must be directories, and must not be on the
2255 * same file system as the current process root. The put_old must be
2256 * underneath new_root, i.e. adding a non-zero number of /.. to the string
2257 * pointed to by put_old must yield the same directory as new_root. No other
2258 * file system may be mounted on put_old. After all, new_root is a mountpoint.
2260 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
2261 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
2262 * in this situation.
2265 * - we don't move root/cwd if they are not at the root (reason: if something
2266 * cared enough to change them, it's probably wrong to force them elsewhere)
2267 * - it's okay to pick a root that isn't the root of a file system, e.g.
2268 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
2269 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
2272 SYSCALL_DEFINE2(pivot_root
, const char __user
*, new_root
,
2273 const char __user
*, put_old
)
2275 struct vfsmount
*tmp
;
2276 struct path
new, old
, parent_path
, root_parent
, root
;
2279 if (!capable(CAP_SYS_ADMIN
))
2282 error
= user_path_dir(new_root
, &new);
2286 if (!check_mnt(new.mnt
))
2289 error
= user_path_dir(put_old
, &old
);
2293 error
= security_sb_pivotroot(&old
, &new);
2299 get_fs_root(current
->fs
, &root
);
2300 down_write(&namespace_sem
);
2301 mutex_lock(&old
.dentry
->d_inode
->i_mutex
);
2303 if (IS_MNT_SHARED(old
.mnt
) ||
2304 IS_MNT_SHARED(new.mnt
->mnt_parent
) ||
2305 IS_MNT_SHARED(root
.mnt
->mnt_parent
))
2307 if (!check_mnt(root
.mnt
))
2310 if (cant_mount(old
.dentry
))
2312 if (d_unlinked(new.dentry
))
2314 if (d_unlinked(old
.dentry
))
2317 if (new.mnt
== root
.mnt
||
2318 old
.mnt
== root
.mnt
)
2319 goto out2
; /* loop, on the same file system */
2321 if (root
.mnt
->mnt_root
!= root
.dentry
)
2322 goto out2
; /* not a mountpoint */
2323 if (root
.mnt
->mnt_parent
== root
.mnt
)
2324 goto out2
; /* not attached */
2325 if (new.mnt
->mnt_root
!= new.dentry
)
2326 goto out2
; /* not a mountpoint */
2327 if (new.mnt
->mnt_parent
== new.mnt
)
2328 goto out2
; /* not attached */
2329 /* make sure we can reach put_old from new_root */
2331 br_write_lock(vfsmount_lock
);
2332 if (tmp
!= new.mnt
) {
2334 if (tmp
->mnt_parent
== tmp
)
2335 goto out3
; /* already mounted on put_old */
2336 if (tmp
->mnt_parent
== new.mnt
)
2338 tmp
= tmp
->mnt_parent
;
2340 if (!is_subdir(tmp
->mnt_mountpoint
, new.dentry
))
2342 } else if (!is_subdir(old
.dentry
, new.dentry
))
2344 detach_mnt(new.mnt
, &parent_path
);
2345 detach_mnt(root
.mnt
, &root_parent
);
2346 /* mount old root on put_old */
2347 attach_mnt(root
.mnt
, &old
);
2348 /* mount new_root on / */
2349 attach_mnt(new.mnt
, &root_parent
);
2350 touch_mnt_namespace(current
->nsproxy
->mnt_ns
);
2351 br_write_unlock(vfsmount_lock
);
2352 chroot_fs_refs(&root
, &new);
2354 path_put(&root_parent
);
2355 path_put(&parent_path
);
2357 mutex_unlock(&old
.dentry
->d_inode
->i_mutex
);
2358 up_write(&namespace_sem
);
2366 br_write_unlock(vfsmount_lock
);
2370 static void __init
init_mount_tree(void)
2372 struct vfsmount
*mnt
;
2373 struct mnt_namespace
*ns
;
2376 mnt
= do_kern_mount("rootfs", 0, "rootfs", NULL
);
2378 panic("Can't create rootfs");
2379 ns
= create_mnt_ns(mnt
);
2381 panic("Can't allocate initial namespace");
2383 init_task
.nsproxy
->mnt_ns
= ns
;
2386 root
.mnt
= ns
->root
;
2387 root
.dentry
= ns
->root
->mnt_root
;
2389 set_fs_pwd(current
->fs
, &root
);
2390 set_fs_root(current
->fs
, &root
);
2393 void __init
mnt_init(void)
2398 init_rwsem(&namespace_sem
);
2400 mnt_cache
= kmem_cache_create("mnt_cache", sizeof(struct vfsmount
),
2401 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
, NULL
);
2403 mount_hashtable
= (struct list_head
*)__get_free_page(GFP_ATOMIC
);
2405 if (!mount_hashtable
)
2406 panic("Failed to allocate mount hash table\n");
2408 printk("Mount-cache hash table entries: %lu\n", HASH_SIZE
);
2410 for (u
= 0; u
< HASH_SIZE
; u
++)
2411 INIT_LIST_HEAD(&mount_hashtable
[u
]);
2413 br_lock_init(vfsmount_lock
);
2417 printk(KERN_WARNING
"%s: sysfs_init error: %d\n",
2419 fs_kobj
= kobject_create_and_add("fs", NULL
);
2421 printk(KERN_WARNING
"%s: kobj create error\n", __func__
);
2426 void put_mnt_ns(struct mnt_namespace
*ns
)
2428 LIST_HEAD(umount_list
);
2430 if (!atomic_dec_and_test(&ns
->count
))
2432 down_write(&namespace_sem
);
2433 br_write_lock(vfsmount_lock
);
2434 umount_tree(ns
->root
, 0, &umount_list
);
2435 br_write_unlock(vfsmount_lock
);
2436 up_write(&namespace_sem
);
2437 release_mounts(&umount_list
);
2440 EXPORT_SYMBOL(put_mnt_ns
);