Merge tag 'v3.10.64' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / namespace.c
index 341d3f564082f2367a7f1538255e90bdfbfd8cd2..a2c0b962e5d282993faf3a849278581df46a27e0 100644 (file)
 #include <linux/fs_struct.h>   /* get_fs_root et.al. */
 #include <linux/fsnotify.h>    /* fsnotify_vfsmount_delete */
 #include <linux/uaccess.h>
-#include <linux/proc_fs.h>
+//#define UMOUNT_LOG  //enable kernel layer unmount log when unmount fail
+#ifdef UMOUNT_LOG
+#include <linux/types.h>
+#endif
+#include <linux/proc_ns.h>
+#include <linux/magic.h>
 #include "pnode.h"
 #include "internal.h"
 
@@ -36,6 +41,7 @@ static int mnt_id_start = 0;
 static int mnt_group_start = 1;
 
 static struct list_head *mount_hashtable __read_mostly;
+static struct list_head *mountpoint_hashtable __read_mostly;
 static struct kmem_cache *mnt_cache __read_mostly;
 static struct rw_semaphore namespace_sem;
 
@@ -130,10 +136,130 @@ void mnt_release_group_id(struct mount *mnt)
 /*
  * vfsmount lock must be held for read
  */
+#ifdef UMOUNT_LOG
+#define UMOUNT_Partition "/emmc@usrdata"
+struct  record_ref_count{ 
+  pid_t pid;
+  char name[TASK_COMM_LEN];
+  int count;
+  struct record_ref_count *next;
+};
+
+struct record_ref_count *ref_head    = NULL;
+struct record_ref_count *ref_current = NULL;
+struct record_ref_count *ref_prev    = NULL;
+int s_total_count = 0;
+#endif
 static inline void mnt_add_count(struct mount *mnt, int n)
 {
+#ifdef UMOUNT_LOG
+int print_link_list=0;
+#ifndef CONFIG_SMP
+       preempt_disable();
+#endif
+
+               if (strcmp(UMOUNT_Partition,mnt->mnt_devname)==0)
+                       {
+                               //if (strcmp("mobile_log_d",current->comm)!=0) 
+                                       {
+                                       //if (current->pid < 100)       //((current->pid < 70) && (current->pid > 60))  
+                                       {
+                                               //printk("Ahsin n=%d  current->pid=%d name=%s \n",n,current->pid,current->comm);
+                                               spin_lock(&mnt_id_lock);
+                                               if (ref_head == NULL) //linked list head (start)
+                                               {
+                                                       printk("Ahsin link list init mnt_get_count=%d \n",mnt_get_count(mnt));
+                                                       
+                                                       ref_current = kmalloc(sizeof(struct record_ref_count), GFP_KERNEL);
+                                               if (ref_current == NULL)
+                                                           printk("Ahsin can't allocate memory for ref_current /n");
+
+                                                       ref_current->next = NULL;
+                                                       ref_current->pid = current->pid;
+                                                       strncpy(ref_current->name, current->comm, TASK_COMM_LEN -1);                                    
+                                                       ref_current->name[TASK_COMM_LEN -1] = '\0';
+                                                       ref_current->count = n;
+                                                       s_total_count = s_total_count + n;
+                                                       ref_head = ref_current;
+                                                       
+                                                       printk("Ahsin ref_head == NULL pid=%d name=%s counter=%d n=%d \n",ref_current->pid,ref_current->name,ref_current->count,n);
+                                               }
+                                               else //check exist first and then add linked list or modify counter
+                                               {
+                                                       ref_prev = ref_head;
+                                                       while(ref_prev != NULL) 
+                                                       {
+                                                               //printk("Ahsin PID= %d, Name= %s, Count= %d n=%d  current->pid=%d \n", ref_prev->pid, ref_prev->name, ref_prev->count,n,current->pid);
+                                                               if (strcmp(ref_prev->name,current->comm)==0) //(ref_prev->pid==current->pid)//exist and find, modify counter
+                                                               {
+                                                                       ref_prev->count = ref_prev->count + n;
+                                                                       s_total_count = s_total_count + n;
+                                                                       //printk("Ahsin (ref_prev->name,current->comm) pid=%d name=%s counter=%d n=%d \n",ref_prev->pid,ref_prev->name,ref_prev->count,n);
+                                                                       break;
+                                                               }
+                                                               else 
+                                                               {
+                                                                       if (ref_prev->next != NULL) 
+                                                                               ref_prev = ref_prev->next;
+                                                                       else 
+                                                                       {       // end of link list
+                                                                               ref_current = kmalloc(sizeof(struct record_ref_count), GFP_KERNEL);
+                                                                               if (ref_current == NULL)
+                                                                                   printk("Ahsin can't allocate memory for ref_prev /n");
+
+                                                                               ref_current->next = NULL;
+                                                                               ref_current->pid = current->pid;
+                                                                               strncpy(ref_current->name, current->comm, TASK_COMM_LEN -1);                                    
+                                                                               ref_current->name[TASK_COMM_LEN -1] = '\0';
+                                                                               ref_current->count = n;
+                                                                               s_total_count = s_total_count + n;
+                                                                               ref_prev->next = ref_current;
+                                                                               //printk("Ahsin new node(end of link list) pid=%d name=%s counter=%d n=%d \n",ref_current->pid,ref_current->name,ref_current->count,n);
+                                                                               break;
+                                                                               
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                               spin_unlock(&mnt_id_lock);
+                                       }
+                                       }
+                       }
+
+#ifndef CONFIG_SMP
+       preempt_enable();
+#endif
+#endif
 #ifdef CONFIG_SMP
        this_cpu_add(mnt->mnt_pcp->mnt_count, n);
+#ifdef UMOUNT_LOG
+#if 0
+       if (strcmp(UMOUNT_Partition,mnt->mnt_devname)==0)
+       {
+               if (strcmp("mobile_log_d",current->comm)!=0) 
+                       {
+                               printk("Ahsin s_total_count=%d mnt_get_count=%d n=%d  current->pid=%d \n",s_total_count,mnt_get_count(mnt),n,current->pid);
+                       //if (current->pid < 100)
+                               {       
+                                       // print linked list
+                                       spin_lock(&mnt_id_lock);
+                                       ref_current = ref_head;
+                                       while(ref_current != NULL) 
+                                       {
+                                               if (ref_current->count)
+                                                       {
+                                                               print_link_list = print_link_list + ref_current->count;
+                                                               //printk("Ahsin PID= %d, Name = %s, Count= %d \n", ref_current->pid, ref_current->name, ref_current->count);
+                                                       }
+                                               ref_current = ref_current->next;
+                                       }
+                                       spin_unlock(&mnt_id_lock);
+                                       printk("Ahsin print_link_list=%d \n",print_link_list);
+                               }
+                       }
+       }
+#endif 
+#endif
 #else
        preempt_disable();
        mnt->mnt_count += n;
@@ -181,6 +307,12 @@ static struct mount *alloc_vfsmnt(const char *name)
                if (!mnt->mnt_pcp)
                        goto out_free_devname;
 
+#ifdef UMOUNT_LOG
+               if (strcmp(UMOUNT_Partition,mnt->mnt_devname)==0)
+               {
+                       printk("Ahsin alloc_vfsmnt current->pid=%d name=%s \n",current->pid,current->comm);     
+               }
+#endif         
                this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
 #else
                mnt->mnt_count = 1;
@@ -605,6 +737,51 @@ struct vfsmount *lookup_mnt(struct path *path)
        }
 }
 
+static struct mountpoint *new_mountpoint(struct dentry *dentry)
+{
+       struct list_head *chain = mountpoint_hashtable + hash(NULL, dentry);
+       struct mountpoint *mp;
+
+       list_for_each_entry(mp, chain, m_hash) {
+               if (mp->m_dentry == dentry) {
+                       /* might be worth a WARN_ON() */
+                       if (d_unlinked(dentry))
+                               return ERR_PTR(-ENOENT);
+                       mp->m_count++;
+                       return mp;
+               }
+       }
+
+       mp = kmalloc(sizeof(struct mountpoint), GFP_KERNEL);
+       if (!mp)
+               return ERR_PTR(-ENOMEM);
+
+       spin_lock(&dentry->d_lock);
+       if (d_unlinked(dentry)) {
+               spin_unlock(&dentry->d_lock);
+               kfree(mp);
+               return ERR_PTR(-ENOENT);
+       }
+       dentry->d_flags |= DCACHE_MOUNTED;
+       spin_unlock(&dentry->d_lock);
+       mp->m_dentry = dentry;
+       mp->m_count = 1;
+       list_add(&mp->m_hash, chain);
+       return mp;
+}
+
+static void put_mountpoint(struct mountpoint *mp)
+{
+       if (!--mp->m_count) {
+               struct dentry *dentry = mp->m_dentry;
+               spin_lock(&dentry->d_lock);
+               dentry->d_flags &= ~DCACHE_MOUNTED;
+               spin_unlock(&dentry->d_lock);
+               list_del(&mp->m_hash);
+               kfree(mp);
+       }
+}
+
 static inline int check_mnt(struct mount *mnt)
 {
        return mnt->mnt_ns == current->nsproxy->mnt_ns;
@@ -632,27 +809,6 @@ static void __touch_mnt_namespace(struct mnt_namespace *ns)
        }
 }
 
-/*
- * Clear dentry's mounted state if it has no remaining mounts.
- * vfsmount_lock must be held for write.
- */
-static void dentry_reset_mounted(struct dentry *dentry)
-{
-       unsigned u;
-
-       for (u = 0; u < HASH_SIZE; u++) {
-               struct mount *p;
-
-               list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
-                       if (p->mnt_mountpoint == dentry)
-                               return;
-               }
-       }
-       spin_lock(&dentry->d_lock);
-       dentry->d_flags &= ~DCACHE_MOUNTED;
-       spin_unlock(&dentry->d_lock);
-}
-
 /*
  * vfsmount lock must be held for write
  */
@@ -664,32 +820,35 @@ static void detach_mnt(struct mount *mnt, struct path *old_path)
        mnt->mnt_mountpoint = mnt->mnt.mnt_root;
        list_del_init(&mnt->mnt_child);
        list_del_init(&mnt->mnt_hash);
-       dentry_reset_mounted(old_path->dentry);
+       put_mountpoint(mnt->mnt_mp);
+       mnt->mnt_mp = NULL;
 }
 
 /*
  * vfsmount lock must be held for write
  */
-void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry,
+void mnt_set_mountpoint(struct mount *mnt,
+                       struct mountpoint *mp,
                        struct mount *child_mnt)
 {
+       mp->m_count++;
        mnt_add_count(mnt, 1);  /* essentially, that's mntget */
-       child_mnt->mnt_mountpoint = dget(dentry);
+       child_mnt->mnt_mountpoint = dget(mp->m_dentry);
        child_mnt->mnt_parent = mnt;
-       spin_lock(&dentry->d_lock);
-       dentry->d_flags |= DCACHE_MOUNTED;
-       spin_unlock(&dentry->d_lock);
+       child_mnt->mnt_mp = mp;
 }
 
 /*
  * vfsmount lock must be held for write
  */
-static void attach_mnt(struct mount *mnt, struct path *path)
+static void attach_mnt(struct mount *mnt,
+                       struct mount *parent,
+                       struct mountpoint *mp)
 {
-       mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt);
+       mnt_set_mountpoint(parent, mp, mnt);
        list_add_tail(&mnt->mnt_hash, mount_hashtable +
-                       hash(path->mnt, path->dentry));
-       list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts);
+                       hash(&parent->mnt, mp->m_dentry));
+       list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
 }
 
 /*
@@ -799,8 +958,21 @@ static struct mount *clone_mnt(struct mount *old, struct dentry *root,
 
        mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
        /* Don't allow unprivileged users to change mount flags */
-       if ((flag & CL_UNPRIVILEGED) && (mnt->mnt.mnt_flags & MNT_READONLY))
-               mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
+       if (flag & CL_UNPRIVILEGED) {
+               mnt->mnt.mnt_flags |= MNT_LOCK_ATIME;
+
+               if (mnt->mnt.mnt_flags & MNT_READONLY)
+                       mnt->mnt.mnt_flags |= MNT_LOCK_READONLY;
+
+               if (mnt->mnt.mnt_flags & MNT_NODEV)
+                       mnt->mnt.mnt_flags |= MNT_LOCK_NODEV;
+
+               if (mnt->mnt.mnt_flags & MNT_NOSUID)
+                       mnt->mnt.mnt_flags |= MNT_LOCK_NOSUID;
+
+               if (mnt->mnt.mnt_flags & MNT_NOEXEC)
+                       mnt->mnt.mnt_flags |= MNT_LOCK_NOEXEC;
+       }
 
        atomic_inc(&sb->s_active);
        mnt->mnt.mnt_sb = sb;
@@ -1095,11 +1267,23 @@ int may_umount(struct vfsmount *mnt)
 
 EXPORT_SYMBOL(may_umount);
 
-void release_mounts(struct list_head *head)
+static LIST_HEAD(unmounted);   /* protected by namespace_sem */
+
+static void namespace_unlock(void)
 {
        struct mount *mnt;
-       while (!list_empty(head)) {
-               mnt = list_first_entry(head, struct mount, mnt_hash);
+       LIST_HEAD(head);
+
+       if (likely(list_empty(&unmounted))) {
+               up_write(&namespace_sem);
+               return;
+       }
+
+       list_splice_init(&unmounted, &head);
+       up_write(&namespace_sem);
+
+       while (!list_empty(&head)) {
+               mnt = list_first_entry(&head, struct mount, mnt_hash);
                list_del_init(&mnt->mnt_hash);
                if (mnt_has_parent(mnt)) {
                        struct dentry *dentry;
@@ -1119,11 +1303,16 @@ void release_mounts(struct list_head *head)
        }
 }
 
+static inline void namespace_lock(void)
+{
+       down_write(&namespace_sem);
+}
+
 /*
  * vfsmount lock must be held for write
  * namespace_sem must be held for write
  */
-void umount_tree(struct mount *mnt, int propagate, struct list_head *kill)
+void umount_tree(struct mount *mnt, int propagate)
 {
        LIST_HEAD(tmp_list);
        struct mount *p;
@@ -1142,20 +1331,20 @@ void umount_tree(struct mount *mnt, int propagate, struct list_head *kill)
                list_del_init(&p->mnt_child);
                if (mnt_has_parent(p)) {
                        p->mnt_parent->mnt_ghosts++;
-                       dentry_reset_mounted(p->mnt_mountpoint);
+                       put_mountpoint(p->mnt_mp);
+                       p->mnt_mp = NULL;
                }
                change_mnt_propagation(p, MS_PRIVATE);
        }
-       list_splice(&tmp_list, kill);
+       list_splice(&tmp_list, &unmounted);
 }
 
-static void shrink_submounts(struct mount *mnt, struct list_head *umounts);
+static void shrink_submounts(struct mount *mnt);
 
 static int do_umount(struct mount *mnt, int flags)
 {
        struct super_block *sb = mnt->mnt.mnt_sb;
        int retval;
-       LIST_HEAD(umount_list);
 
        retval = security_sb_umount(&mnt->mnt, flags);
        if (retval)
@@ -1215,6 +1404,8 @@ static int do_umount(struct mount *mnt, int flags)
                 * Special case for "unmounting" root ...
                 * we just try to remount it readonly.
                 */
+               if (!capable(CAP_SYS_ADMIN))
+                       return -EPERM;
                down_write(&sb->s_umount);
                if (!(sb->s_flags & MS_RDONLY))
                        retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
@@ -1222,22 +1413,21 @@ static int do_umount(struct mount *mnt, int flags)
                return retval;
        }
 
-       down_write(&namespace_sem);
+       namespace_lock();
        br_write_lock(&vfsmount_lock);
        event++;
 
        if (!(flags & MNT_DETACH))
-               shrink_submounts(mnt, &umount_list);
+               shrink_submounts(mnt);
 
        retval = -EBUSY;
        if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
                if (!list_empty(&mnt->mnt_list))
-                       umount_tree(mnt, 1, &umount_list);
+                       umount_tree(mnt, 1);
                retval = 0;
        }
        br_write_unlock(&vfsmount_lock);
-       up_write(&namespace_sem);
-       release_mounts(&umount_list);
+       namespace_unlock();
        return retval;
 }
 
@@ -1263,7 +1453,9 @@ SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
        struct mount *mnt;
        int retval;
        int lookup_flags = 0;
-
+#ifdef UMOUNT_LOG      
+       int total_value =0;
+#endif
        if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
                return -EINVAL;
 
@@ -1282,8 +1474,37 @@ SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
                goto dput_and_out;
        if (!check_mnt(mnt))
                goto dput_and_out;
+       retval = -EPERM;
+       if (flags & MNT_FORCE && !capable(CAP_SYS_ADMIN))
+               goto dput_and_out;
 
        retval = do_umount(mnt, flags);
+#ifdef UMOUNT_LOG
+       {
+               printk("Ahsin do_umount retval=%d \n",retval);
+               //do_umount success: 0, do_umount busy: -16
+               //if do_umount fail, need to dump the link list here
+       
+               if(retval)
+                       printk("Ahsin do_umount fail;  mnt_get_count=%d   mnt->mnt_devname=%s\n",mnt_get_count(mnt),mnt->mnt_devname);          
+               else
+                       printk("Ahsin do_umount success;  mnt_get_count=%d   mnt->mnt_devname=%s\n",mnt_get_count(mnt),mnt->mnt_devname);               
+               
+               // print linked list
+               spin_lock(&mnt_id_lock);
+               ref_current = ref_head;
+               while(ref_current != NULL) 
+               {
+                       total_value = total_value + ref_current->count;
+                       
+                       if (ref_current->count)
+                               printk("Ahsin PID= %d, Name = %s, Count= %d \n", ref_current->pid, ref_current->name, ref_current->count);
+                       ref_current = ref_current->next;
+               }
+               spin_unlock(&mnt_id_lock);
+               printk("Ahsin total_value=%d \n",total_value);
+       }
+#endif
 dput_and_out:
        /* we mustn't call path_put() as that would clear mnt_expiry_mark */
        dput(path.dentry);
@@ -1310,13 +1531,13 @@ static bool mnt_ns_loop(struct path *path)
         * mount namespace loop?
         */
        struct inode *inode = path->dentry->d_inode;
-       struct proc_inode *ei;
+       struct proc_ns *ei;
        struct mnt_namespace *mnt_ns;
 
        if (!proc_ns_inode(inode))
                return false;
 
-       ei = PROC_I(inode);
+       ei = get_proc_ns(inode);
        if (ei->ns_ops != &mntns_operations)
                return false;
 
@@ -1327,8 +1548,7 @@ static bool mnt_ns_loop(struct path *path)
 struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
                                        int flag)
 {
-       struct mount *res, *p, *q, *r;
-       struct path path;
+       struct mount *res, *p, *q, *r, *parent;
 
        if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
                return ERR_PTR(-EINVAL);
@@ -1355,25 +1575,22 @@ struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
                                q = q->mnt_parent;
                        }
                        p = s;
-                       path.mnt = &q->mnt;
-                       path.dentry = p->mnt_mountpoint;
+                       parent = q;
                        q = clone_mnt(p, p->mnt.mnt_root, flag);
                        if (IS_ERR(q))
                                goto out;
                        br_write_lock(&vfsmount_lock);
                        list_add_tail(&q->mnt_list, &res->mnt_list);
-                       attach_mnt(q, &path);
+                       attach_mnt(q, parent, p->mnt_mp);
                        br_write_unlock(&vfsmount_lock);
                }
        }
        return res;
 out:
        if (res) {
-               LIST_HEAD(umount_list);
                br_write_lock(&vfsmount_lock);
-               umount_tree(res, 0, &umount_list);
+               umount_tree(res, 0);
                br_write_unlock(&vfsmount_lock);
-               release_mounts(&umount_list);
        }
        return q;
 }
@@ -1383,24 +1600,22 @@ out:
 struct vfsmount *collect_mounts(struct path *path)
 {
        struct mount *tree;
-       down_write(&namespace_sem);
+       namespace_lock();
        tree = copy_tree(real_mount(path->mnt), path->dentry,
                         CL_COPY_ALL | CL_PRIVATE);
-       up_write(&namespace_sem);
+       namespace_unlock();
        if (IS_ERR(tree))
-               return NULL;
+               return ERR_CAST(tree);
        return &tree->mnt;
 }
 
 void drop_collected_mounts(struct vfsmount *mnt)
 {
-       LIST_HEAD(umount_list);
-       down_write(&namespace_sem);
+       namespace_lock();
        br_write_lock(&vfsmount_lock);
-       umount_tree(real_mount(mnt), 0, &umount_list);
+       umount_tree(real_mount(mnt), 0);
        br_write_unlock(&vfsmount_lock);
-       up_write(&namespace_sem);
-       release_mounts(&umount_list);
+       namespace_unlock();
 }
 
 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
@@ -1509,11 +1724,11 @@ static int invent_group_ids(struct mount *mnt, bool recurse)
  * in allocations.
  */
 static int attach_recursive_mnt(struct mount *source_mnt,
-                       struct path *path, struct path *parent_path)
+                       struct mount *dest_mnt,
+                       struct mountpoint *dest_mp,
+                       struct path *parent_path)
 {
        LIST_HEAD(tree_list);
-       struct mount *dest_mnt = real_mount(path->mnt);
-       struct dentry *dest_dentry = path->dentry;
        struct mount *child, *p;
        int err;
 
@@ -1522,7 +1737,7 @@ static int attach_recursive_mnt(struct mount *source_mnt,
                if (err)
                        goto out;
        }
-       err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
+       err = propagate_mnt(dest_mnt, dest_mp, source_mnt, &tree_list);
        if (err)
                goto out_cleanup_ids;
 
@@ -1534,10 +1749,10 @@ static int attach_recursive_mnt(struct mount *source_mnt,
        }
        if (parent_path) {
                detach_mnt(source_mnt, parent_path);
-               attach_mnt(source_mnt, path);
+               attach_mnt(source_mnt, dest_mnt, dest_mp);
                touch_mnt_namespace(source_mnt->mnt_ns);
        } else {
-               mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
+               mnt_set_mountpoint(dest_mnt, dest_mp, source_mnt);
                commit_tree(source_mnt);
        }
 
@@ -1556,46 +1771,53 @@ static int attach_recursive_mnt(struct mount *source_mnt,
        return err;
 }
 
-static int lock_mount(struct path *path)
+static struct mountpoint *lock_mount(struct path *path)
 {
        struct vfsmount *mnt;
+       struct dentry *dentry = path->dentry;
 retry:
-       mutex_lock(&path->dentry->d_inode->i_mutex);
-       if (unlikely(cant_mount(path->dentry))) {
-               mutex_unlock(&path->dentry->d_inode->i_mutex);
-               return -ENOENT;
+       mutex_lock(&dentry->d_inode->i_mutex);
+       if (unlikely(cant_mount(dentry))) {
+               mutex_unlock(&dentry->d_inode->i_mutex);
+               return ERR_PTR(-ENOENT);
        }
-       down_write(&namespace_sem);
+       namespace_lock();
        mnt = lookup_mnt(path);
-       if (likely(!mnt))
-               return 0;
-       up_write(&namespace_sem);
+       if (likely(!mnt)) {
+               struct mountpoint *mp = new_mountpoint(dentry);
+               if (IS_ERR(mp)) {
+                       namespace_unlock();
+                       mutex_unlock(&dentry->d_inode->i_mutex);
+                       return mp;
+               }
+               return mp;
+       }
+       namespace_unlock();
        mutex_unlock(&path->dentry->d_inode->i_mutex);
        path_put(path);
        path->mnt = mnt;
-       path->dentry = dget(mnt->mnt_root);
+       dentry = path->dentry = dget(mnt->mnt_root);
        goto retry;
 }
 
-static void unlock_mount(struct path *path)
+static void unlock_mount(struct mountpoint *where)
 {
-       up_write(&namespace_sem);
-       mutex_unlock(&path->dentry->d_inode->i_mutex);
+       struct dentry *dentry = where->m_dentry;
+       put_mountpoint(where);
+       namespace_unlock();
+       mutex_unlock(&dentry->d_inode->i_mutex);
 }
 
-static int graft_tree(struct mount *mnt, struct path *path)
+static int graft_tree(struct mount *mnt, struct mount *p, struct mountpoint *mp)
 {
        if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
                return -EINVAL;
 
-       if (S_ISDIR(path->dentry->d_inode->i_mode) !=
+       if (S_ISDIR(mp->m_dentry->d_inode->i_mode) !=
              S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
                return -ENOTDIR;
 
-       if (d_unlinked(path->dentry))
-               return -ENOENT;
-
-       return attach_recursive_mnt(mnt, path, NULL);
+       return attach_recursive_mnt(mnt, p, mp, NULL);
 }
 
 /*
@@ -1633,7 +1855,7 @@ static int do_change_type(struct path *path, int flag)
        if (!type)
                return -EINVAL;
 
-       down_write(&namespace_sem);
+       namespace_lock();
        if (type == MS_SHARED) {
                err = invent_group_ids(mnt, recurse);
                if (err)
@@ -1646,7 +1868,7 @@ static int do_change_type(struct path *path, int flag)
        br_write_unlock(&vfsmount_lock);
 
  out_unlock:
-       up_write(&namespace_sem);
+       namespace_unlock();
        return err;
 }
 
@@ -1656,9 +1878,9 @@ static int do_change_type(struct path *path, int flag)
 static int do_loopback(struct path *path, const char *old_name,
                                int recurse)
 {
-       LIST_HEAD(umount_list);
        struct path old_path;
-       struct mount *mnt = NULL, *old;
+       struct mount *mnt = NULL, *old, *parent;
+       struct mountpoint *mp;
        int err;
        if (!old_name || !*old_name)
                return -EINVAL;
@@ -1670,17 +1892,19 @@ static int do_loopback(struct path *path, const char *old_name,
        if (mnt_ns_loop(&old_path))
                goto out; 
 
-       err = lock_mount(path);
-       if (err)
+       mp = lock_mount(path);
+       err = PTR_ERR(mp);
+       if (IS_ERR(mp))
                goto out;
 
        old = real_mount(old_path.mnt);
+       parent = real_mount(path->mnt);
 
        err = -EINVAL;
        if (IS_MNT_UNBINDABLE(old))
                goto out2;
 
-       if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old))
+       if (!check_mnt(parent) || !check_mnt(old))
                goto out2;
 
        if (recurse)
@@ -1693,15 +1917,14 @@ static int do_loopback(struct path *path, const char *old_name,
                goto out2;
        }
 
-       err = graft_tree(mnt, path);
+       err = graft_tree(mnt, parent, mp);
        if (err) {
                br_write_lock(&vfsmount_lock);
-               umount_tree(mnt, 0, &umount_list);
+               umount_tree(mnt, 0);
                br_write_unlock(&vfsmount_lock);
        }
 out2:
-       unlock_mount(path);
-       release_mounts(&umount_list);
+       unlock_mount(mp);
 out:
        path_put(&old_path);
        return err;
@@ -1717,9 +1940,6 @@ static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
        if (readonly_request == __mnt_is_readonly(mnt))
                return 0;
 
-       if (mnt->mnt_flags & MNT_LOCK_READONLY)
-               return -EPERM;
-
        if (readonly_request)
                error = mnt_make_readonly(real_mount(mnt));
        else
@@ -1745,6 +1965,39 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
        if (path->dentry != path->mnt->mnt_root)
                return -EINVAL;
 
+       /* Don't allow changing of locked mnt flags.
+        *
+        * No locks need to be held here while testing the various
+        * MNT_LOCK flags because those flags can never be cleared
+        * once they are set.
+        */
+       if ((mnt->mnt.mnt_flags & MNT_LOCK_READONLY) &&
+           !(mnt_flags & MNT_READONLY)) {
+               return -EPERM;
+       }
+       if ((mnt->mnt.mnt_flags & MNT_LOCK_NODEV) &&
+           !(mnt_flags & MNT_NODEV)) {
+               /* Was the nodev implicitly added in mount? */
+               if ((mnt->mnt_ns->user_ns != &init_user_ns) &&
+                   !(sb->s_type->fs_flags & FS_USERNS_DEV_MOUNT)) {
+                       mnt_flags |= MNT_NODEV;
+               } else {
+                       return -EPERM;
+               }
+       }
+       if ((mnt->mnt.mnt_flags & MNT_LOCK_NOSUID) &&
+           !(mnt_flags & MNT_NOSUID)) {
+               return -EPERM;
+       }
+       if ((mnt->mnt.mnt_flags & MNT_LOCK_NOEXEC) &&
+           !(mnt_flags & MNT_NOEXEC)) {
+               return -EPERM;
+       }
+       if ((mnt->mnt.mnt_flags & MNT_LOCK_ATIME) &&
+           ((mnt->mnt.mnt_flags & MNT_ATIME_MASK) != (mnt_flags & MNT_ATIME_MASK))) {
+               return -EPERM;
+       }
+
        err = security_sb_remount(sb, data);
        if (err)
                return err;
@@ -1758,7 +2011,7 @@ static int do_remount(struct path *path, int flags, int mnt_flags,
                err = do_remount_sb(sb, flags, data, 0);
        if (!err) {
                br_write_lock(&vfsmount_lock);
-               mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
+               mnt_flags |= mnt->mnt.mnt_flags & ~MNT_USER_SETTABLE_MASK;
                mnt->mnt.mnt_flags = mnt_flags;
                br_write_unlock(&vfsmount_lock);
        }
@@ -1786,6 +2039,7 @@ static int do_move_mount(struct path *path, const char *old_name)
        struct path old_path, parent_path;
        struct mount *p;
        struct mount *old;
+       struct mountpoint *mp;
        int err;
        if (!old_name || !*old_name)
                return -EINVAL;
@@ -1793,8 +2047,9 @@ static int do_move_mount(struct path *path, const char *old_name)
        if (err)
                return err;
 
-       err = lock_mount(path);
-       if (err < 0)
+       mp = lock_mount(path);
+       err = PTR_ERR(mp);
+       if (IS_ERR(mp))
                goto out;
 
        old = real_mount(old_path.mnt);
@@ -1804,9 +2059,6 @@ static int do_move_mount(struct path *path, const char *old_name)
        if (!check_mnt(p) || !check_mnt(old))
                goto out1;
 
-       if (d_unlinked(path->dentry))
-               goto out1;
-
        err = -EINVAL;
        if (old_path.dentry != old_path.mnt->mnt_root)
                goto out1;
@@ -1833,7 +2085,7 @@ static int do_move_mount(struct path *path, const char *old_name)
                if (p == old)
                        goto out1;
 
-       err = attach_recursive_mnt(old, path, &parent_path);
+       err = attach_recursive_mnt(old, real_mount(path->mnt), mp, &parent_path);
        if (err)
                goto out1;
 
@@ -1841,7 +2093,7 @@ static int do_move_mount(struct path *path, const char *old_name)
         * automatically */
        list_del_init(&old->mnt_expire);
 out1:
-       unlock_mount(path);
+       unlock_mount(mp);
 out:
        if (!err)
                path_put(&parent_path);
@@ -1877,21 +2129,24 @@ static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  */
 static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
 {
+       struct mountpoint *mp;
+       struct mount *parent;
        int err;
 
        mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
 
-       err = lock_mount(path);
-       if (err)
-               return err;
+       mp = lock_mount(path);
+       if (IS_ERR(mp))
+               return PTR_ERR(mp);
 
+       parent = real_mount(path->mnt);
        err = -EINVAL;
-       if (unlikely(!check_mnt(real_mount(path->mnt)))) {
+       if (unlikely(!check_mnt(parent))) {
                /* that's acceptable only for automounts done in private ns */
                if (!(mnt_flags & MNT_SHRINKABLE))
                        goto unlock;
                /* ... and for those we'd better have mountpoint still alive */
-               if (!real_mount(path->mnt)->mnt_ns)
+               if (!parent->mnt_ns)
                        goto unlock;
        }
 
@@ -1906,10 +2161,10 @@ static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
                goto unlock;
 
        newmnt->mnt.mnt_flags = mnt_flags;
-       err = graft_tree(newmnt, path);
+       err = graft_tree(newmnt, parent, mp);
 
 unlock:
-       unlock_mount(path);
+       unlock_mount(mp);
        return err;
 }
 
@@ -1942,7 +2197,7 @@ static int do_new_mount(struct path *path, const char *fstype, int flags,
                 */
                if (!(type->fs_flags & FS_USERNS_DEV_MOUNT)) {
                        flags |= MS_NODEV;
-                       mnt_flags |= MNT_NODEV;
+                       mnt_flags |= MNT_NODEV | MNT_LOCK_NODEV;
                }
        }
 
@@ -1982,11 +2237,11 @@ int finish_automount(struct vfsmount *m, struct path *path)
 fail:
        /* remove m from any expiration list it may be on */
        if (!list_empty(&mnt->mnt_expire)) {
-               down_write(&namespace_sem);
+               namespace_lock();
                br_write_lock(&vfsmount_lock);
                list_del_init(&mnt->mnt_expire);
                br_write_unlock(&vfsmount_lock);
-               up_write(&namespace_sem);
+               namespace_unlock();
        }
        mntput(m);
        mntput(m);
@@ -2000,13 +2255,13 @@ fail:
  */
 void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
 {
-       down_write(&namespace_sem);
+       namespace_lock();
        br_write_lock(&vfsmount_lock);
 
        list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
 
        br_write_unlock(&vfsmount_lock);
-       up_write(&namespace_sem);
+       namespace_unlock();
 }
 EXPORT_SYMBOL(mnt_set_expiry);
 
@@ -2019,12 +2274,11 @@ void mark_mounts_for_expiry(struct list_head *mounts)
 {
        struct mount *mnt, *next;
        LIST_HEAD(graveyard);
-       LIST_HEAD(umounts);
 
        if (list_empty(mounts))
                return;
 
-       down_write(&namespace_sem);
+       namespace_lock();
        br_write_lock(&vfsmount_lock);
 
        /* extract from the expiration list every vfsmount that matches the
@@ -2042,12 +2296,10 @@ void mark_mounts_for_expiry(struct list_head *mounts)
        while (!list_empty(&graveyard)) {
                mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
                touch_mnt_namespace(mnt->mnt_ns);
-               umount_tree(mnt, 1, &umounts);
+               umount_tree(mnt, 1);
        }
        br_write_unlock(&vfsmount_lock);
-       up_write(&namespace_sem);
-
-       release_mounts(&umounts);
+       namespace_unlock();
 }
 
 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
@@ -2104,7 +2356,7 @@ resume:
  *
  * vfsmount_lock must be held for write
  */
-static void shrink_submounts(struct mount *mnt, struct list_head *umounts)
+static void shrink_submounts(struct mount *mnt)
 {
        LIST_HEAD(graveyard);
        struct mount *m;
@@ -2115,7 +2367,7 @@ static void shrink_submounts(struct mount *mnt, struct list_head *umounts)
                        m = list_first_entry(&graveyard, struct mount,
                                                mnt_expire);
                        touch_mnt_namespace(m->mnt_ns);
-                       umount_tree(m, 1, umounts);
+                       umount_tree(m, 1);
                }
        }
 }
@@ -2238,12 +2490,11 @@ long do_mount(const char *dev_name, const char *dir_name,
 
        retval = security_sb_mount(dev_name, &path,
                                   type_page, flags, data_page);
+       if (!retval && !may_mount())
+               retval = -EPERM;
        if (retval)
                goto dput_out;
 
-       if (!may_mount())
-               return -EPERM;
-
        /* Default to relatime unless overriden */
        if (!(flags & MS_NOATIME))
                mnt_flags |= MNT_RELATIME;
@@ -2264,6 +2515,14 @@ long do_mount(const char *dev_name, const char *dir_name,
        if (flags & MS_RDONLY)
                mnt_flags |= MNT_READONLY;
 
+       /* The default atime for remount is preservation */
+       if ((flags & MS_REMOUNT) &&
+           ((flags & (MS_NOATIME | MS_NODIRATIME | MS_RELATIME |
+                      MS_STRICTATIME)) == 0)) {
+               mnt_flags &= ~MNT_ATIME_MASK;
+               mnt_flags |= path.mnt->mnt_flags & MNT_ATIME_MASK;
+       }
+
        flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
                   MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
                   MS_STRICTATIME);
@@ -2342,14 +2601,14 @@ static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
        if (IS_ERR(new_ns))
                return new_ns;
 
-       down_write(&namespace_sem);
+       namespace_lock();
        /* First pass: copy the tree topology */
        copy_flags = CL_COPY_ALL | CL_EXPIRE;
        if (user_ns != mnt_ns->user_ns)
                copy_flags |= CL_SHARED_TO_SLAVE | CL_UNPRIVILEGED;
        new = copy_tree(old, old->mnt.mnt_root, copy_flags);
        if (IS_ERR(new)) {
-               up_write(&namespace_sem);
+               namespace_unlock();
                free_mnt_ns(new_ns);
                return ERR_CAST(new);
        }
@@ -2380,7 +2639,7 @@ static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
                p = next_mnt(p, old);
                q = next_mnt(q, new);
        }
-       up_write(&namespace_sem);
+       namespace_unlock();
 
        if (rootmnt)
                mntput(rootmnt);
@@ -2418,7 +2677,7 @@ static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
                struct mount *mnt = real_mount(m);
                mnt->mnt_ns = new_ns;
                new_ns->root = mnt;
-               list_add(&new_ns->list, &mnt->mnt_list);
+               list_add(&mnt->mnt_list, &new_ns->list);
        } else {
                mntput(m);
        }
@@ -2550,7 +2809,8 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
                const char __user *, put_old)
 {
        struct path new, old, parent_path, root_parent, root;
-       struct mount *new_mnt, *root_mnt;
+       struct mount *new_mnt, *root_mnt, *old_mnt;
+       struct mountpoint *old_mp, *root_mp;
        int error;
 
        if (!may_mount())
@@ -2569,14 +2829,16 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
                goto out2;
 
        get_fs_root(current->fs, &root);
-       error = lock_mount(&old);
-       if (error)
+       old_mp = lock_mount(&old);
+       error = PTR_ERR(old_mp);
+       if (IS_ERR(old_mp))
                goto out3;
 
        error = -EINVAL;
        new_mnt = real_mount(new.mnt);
        root_mnt = real_mount(root.mnt);
-       if (IS_MNT_SHARED(real_mount(old.mnt)) ||
+       old_mnt = real_mount(old.mnt);
+       if (IS_MNT_SHARED(old_mnt) ||
                IS_MNT_SHARED(new_mnt->mnt_parent) ||
                IS_MNT_SHARED(root_mnt->mnt_parent))
                goto out4;
@@ -2585,37 +2847,40 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
        error = -ENOENT;
        if (d_unlinked(new.dentry))
                goto out4;
-       if (d_unlinked(old.dentry))
-               goto out4;
        error = -EBUSY;
-       if (new.mnt == root.mnt ||
-           old.mnt == root.mnt)
+       if (new_mnt == root_mnt || old_mnt == root_mnt)
                goto out4; /* loop, on the same file system  */
        error = -EINVAL;
        if (root.mnt->mnt_root != root.dentry)
                goto out4; /* not a mountpoint */
        if (!mnt_has_parent(root_mnt))
                goto out4; /* not attached */
+       root_mp = root_mnt->mnt_mp;
        if (new.mnt->mnt_root != new.dentry)
                goto out4; /* not a mountpoint */
        if (!mnt_has_parent(new_mnt))
                goto out4; /* not attached */
        /* make sure we can reach put_old from new_root */
-       if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new))
+       if (!is_path_reachable(old_mnt, old.dentry, &new))
+               goto out4;
+       /* make certain new is below the root */
+       if (!is_path_reachable(new_mnt, new.dentry, &root))
                goto out4;
+       root_mp->m_count++; /* pin it so it won't go away */
        br_write_lock(&vfsmount_lock);
        detach_mnt(new_mnt, &parent_path);
        detach_mnt(root_mnt, &root_parent);
        /* mount old root on put_old */
-       attach_mnt(root_mnt, &old);
+       attach_mnt(root_mnt, old_mnt, old_mp);
        /* mount new_root on / */
-       attach_mnt(new_mnt, &root_parent);
+       attach_mnt(new_mnt, real_mount(root_parent.mnt), root_mp);
        touch_mnt_namespace(current->nsproxy->mnt_ns);
        br_write_unlock(&vfsmount_lock);
        chroot_fs_refs(&root, &new);
+       put_mountpoint(root_mp);
        error = 0;
 out4:
-       unlock_mount(&old);
+       unlock_mount(old_mp);
        if (!error) {
                path_put(&root_parent);
                path_put(&parent_path);
@@ -2670,14 +2935,17 @@ void __init mnt_init(void)
                        0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
 
        mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
+       mountpoint_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
 
-       if (!mount_hashtable)
+       if (!mount_hashtable || !mountpoint_hashtable)
                panic("Failed to allocate mount hash table\n");
 
        printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
 
        for (u = 0; u < HASH_SIZE; u++)
                INIT_LIST_HEAD(&mount_hashtable[u]);
+       for (u = 0; u < HASH_SIZE; u++)
+               INIT_LIST_HEAD(&mountpoint_hashtable[u]);
 
        br_lock_init(&vfsmount_lock);
 
@@ -2694,16 +2962,13 @@ void __init mnt_init(void)
 
 void put_mnt_ns(struct mnt_namespace *ns)
 {
-       LIST_HEAD(umount_list);
-
        if (!atomic_dec_and_test(&ns->count))
                return;
-       down_write(&namespace_sem);
+       namespace_lock();
        br_write_lock(&vfsmount_lock);
-       umount_tree(ns->root, 0, &umount_list);
+       umount_tree(ns->root, 0);
        br_write_unlock(&vfsmount_lock);
-       up_write(&namespace_sem);
-       release_mounts(&umount_list);
+       namespace_unlock();
        free_mnt_ns(ns);
 }