ath10k: add testmode
[cascardo/linux.git] / kernel / acct.c
index d9ebc96..b4c667d 100644 (file)
@@ -59,7 +59,7 @@
 #include <asm/div64.h>
 #include <linux/blkdev.h> /* sector_div */
 #include <linux/pid_namespace.h>
-#include <../fs/mount.h>       /* will go away when we refactor */
+#include <linux/fs_pin.h>
 
 /*
  * These constants control the amount of freespace that suspend and
@@ -79,14 +79,7 @@ int acct_parm[3] = {4, 2, 30};
 static void do_acct_process(struct bsd_acct_struct *acct);
 
 struct bsd_acct_struct {
-       atomic_long_t           count;
-       union {
-               struct {
-                       struct hlist_node       s_list;
-                       struct hlist_node       m_list;
-               };
-               struct rcu_head rcu;
-       };
+       struct fs_pin           pin;
        struct mutex            lock;
        int                     active;
        unsigned long           needcheck;
@@ -96,13 +89,6 @@ struct bsd_acct_struct {
        struct completion       done;
 };
 
-static void acct_free_rcu(struct rcu_head *head)
-{
-       kfree(container_of(head, struct bsd_acct_struct, rcu));
-}
-
-static DEFINE_SPINLOCK(acct_lock);
-
 /*
  * Check the amount of free space and suspend/resume accordingly.
  */
@@ -122,14 +108,14 @@ static int check_free_space(struct bsd_acct_struct *acct)
                do_div(suspend, 100);
                if (sbuf.f_bavail <= suspend) {
                        acct->active = 0;
-                       printk(KERN_INFO "Process accounting paused\n");
+                       pr_info("Process accounting paused\n");
                }
        } else {
                u64 resume = sbuf.f_blocks * RESUME;
                do_div(resume, 100);
                if (sbuf.f_bavail >= resume) {
                        acct->active = 1;
-                       printk(KERN_INFO "Process accounting resumed\n");
+                       pr_info("Process accounting resumed\n");
                }
        }
 
@@ -138,29 +124,6 @@ out:
        return acct->active;
 }
 
-static void acct_put(struct bsd_acct_struct *p)
-{
-       if (atomic_long_dec_and_test(&p->count))
-               call_rcu(&p->rcu, acct_free_rcu);
-}
-
-static struct bsd_acct_struct *__acct_get(struct bsd_acct_struct *res)
-{
-       if (!atomic_long_inc_not_zero(&res->count)) {
-               rcu_read_unlock();
-               cpu_relax();
-               return NULL;
-       }
-       rcu_read_unlock();
-       mutex_lock(&res->lock);
-       if (!res->ns) {
-               mutex_unlock(&res->lock);
-               acct_put(res);
-               return NULL;
-       }
-       return res;
-}
-
 static struct bsd_acct_struct *acct_get(struct pid_namespace *ns)
 {
        struct bsd_acct_struct *res;
@@ -172,9 +135,18 @@ again:
                rcu_read_unlock();
                return NULL;
        }
-       res = __acct_get(res);
-       if (!res)
+       if (!atomic_long_inc_not_zero(&res->pin.count)) {
+               rcu_read_unlock();
+               cpu_relax();
+               goto again;
+       }
+       rcu_read_unlock();
+       mutex_lock(&res->lock);
+       if (!res->ns) {
+               mutex_unlock(&res->lock);
+               pin_put(&res->pin);
                goto again;
+       }
        return res;
 }
 
@@ -182,7 +154,6 @@ static void close_work(struct work_struct *work)
 {
        struct bsd_acct_struct *acct = container_of(work, struct bsd_acct_struct, work);
        struct file *file = acct->file;
-       mnt_unpin(file->f_path.mnt);
        if (file->f_op->flush)
                file->f_op->flush(file, NULL);
        __fput_sync(file);
@@ -199,33 +170,35 @@ static void acct_kill(struct bsd_acct_struct *acct,
                init_completion(&acct->done);
                schedule_work(&acct->work);
                wait_for_completion(&acct->done);
-               spin_lock(&acct_lock);
-               hlist_del(&acct->m_list);
-               hlist_del(&acct->s_list);
-               spin_unlock(&acct_lock);
+               pin_remove(&acct->pin);
                ns->bacct = new;
-               if (new) {
-                       struct vfsmount *m = new->file->f_path.mnt;
-                       mnt_pin(m);
-                       spin_lock(&acct_lock);
-                       hlist_add_head(&new->s_list, &m->mnt_sb->s_pins);
-                       hlist_add_head(&new->m_list, &real_mount(m)->mnt_pins);
-                       spin_unlock(&acct_lock);
-                       mutex_unlock(&new->lock);
-               }
                acct->ns = NULL;
-               atomic_long_dec(&acct->count);
+               atomic_long_dec(&acct->pin.count);
+               mutex_unlock(&acct->lock);
+               pin_put(&acct->pin);
+       }
+}
+
+static void acct_pin_kill(struct fs_pin *pin)
+{
+       struct bsd_acct_struct *acct;
+       acct = container_of(pin, struct bsd_acct_struct, pin);
+       mutex_lock(&acct->lock);
+       if (!acct->ns) {
                mutex_unlock(&acct->lock);
-               acct_put(acct);
+               pin_put(pin);
+               acct = NULL;
        }
+       acct_kill(acct, NULL);
 }
 
 static int acct_on(struct filename *pathname)
 {
        struct file *file;
-       struct vfsmount *mnt;
+       struct vfsmount *mnt, *internal;
        struct pid_namespace *ns = task_active_pid_ns(current);
        struct bsd_acct_struct *acct, *old;
+       int err;
 
        acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
        if (!acct)
@@ -249,28 +222,39 @@ static int acct_on(struct filename *pathname)
                filp_close(file, NULL);
                return -EIO;
        }
+       internal = mnt_clone_internal(&file->f_path);
+       if (IS_ERR(internal)) {
+               kfree(acct);
+               filp_close(file, NULL);
+               return PTR_ERR(internal);
+       }
+       err = mnt_want_write(internal);
+       if (err) {
+               mntput(internal);
+               kfree(acct);
+               filp_close(file, NULL);
+               return err;
+       }
+       mnt = file->f_path.mnt;
+       file->f_path.mnt = internal;
 
-       atomic_long_set(&acct->count, 1);
+       atomic_long_set(&acct->pin.count, 1);
+       acct->pin.kill = acct_pin_kill;
        acct->file = file;
        acct->needcheck = jiffies;
        acct->ns = ns;
        mutex_init(&acct->lock);
-       mnt = file->f_path.mnt;
+       mutex_lock_nested(&acct->lock, 1);      /* nobody has seen it yet */
+       pin_insert(&acct->pin, mnt);
 
        old = acct_get(ns);
-       mutex_lock_nested(&acct->lock, 1);      /* nobody has seen it yet */
-       if (old) {
+       if (old)
                acct_kill(old, acct);
-       } else {
+       else
                ns->bacct = acct;
-               spin_lock(&acct_lock);
-               mnt_pin(mnt);
-               hlist_add_head(&acct->s_list, &mnt->mnt_sb->s_pins);
-               hlist_add_head(&acct->m_list, &real_mount(mnt)->mnt_pins);
-               spin_unlock(&acct_lock);
-               mutex_unlock(&acct->lock);
-       }
-       mntput(mnt); /* it's pinned, now give up active reference */
+       mutex_unlock(&acct->lock);
+       mnt_drop_write(mnt);
+       mntput(mnt);
        return 0;
 }
 
@@ -296,6 +280,7 @@ SYSCALL_DEFINE1(acct, const char __user *, name)
 
        if (name) {
                struct filename *tmp = getname(name);
+
                if (IS_ERR(tmp))
                        return PTR_ERR(tmp);
                mutex_lock(&acct_on_mutex);
@@ -309,36 +294,6 @@ SYSCALL_DEFINE1(acct, const char __user *, name)
        return error;
 }
 
-void acct_auto_close_mnt(struct hlist_head *list)
-{
-       rcu_read_lock();
-       while (1) {
-               struct hlist_node *p = ACCESS_ONCE(list->first);
-               if (!p)
-                       break;
-               acct_kill(__acct_get(hlist_entry(p,
-                                                struct bsd_acct_struct,
-                                                m_list)), NULL);
-               rcu_read_lock();
-       }
-       rcu_read_unlock();
-}
-
-void acct_auto_close(struct hlist_head *list)
-{
-       rcu_read_lock();
-       while (1) {
-               struct hlist_node *p = ACCESS_ONCE(list->first);
-               if (!p)
-                       break;
-               acct_kill(__acct_get(hlist_entry(p,
-                                                struct bsd_acct_struct,
-                                                s_list)), NULL);
-               rcu_read_lock();
-       }
-       rcu_read_unlock();
-}
-
 void acct_exit_ns(struct pid_namespace *ns)
 {
        acct_kill(acct_get(ns), NULL);
@@ -383,7 +338,7 @@ static comp_t encode_comp_t(unsigned long value)
        return exp;
 }
 
-#if ACCT_VERSION==1 || ACCT_VERSION==2
+#if ACCT_VERSION == 1 || ACCT_VERSION == 2
 /*
  * encode an u64 into a comp2_t (24 bits)
  *
@@ -396,7 +351,7 @@ static comp_t encode_comp_t(unsigned long value)
 #define MANTSIZE2       20                      /* 20 bit mantissa. */
 #define EXPSIZE2        5                       /* 5 bit base 2 exponent. */
 #define MAXFRACT2       ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */
-#define MAXEXP2         ((1 <<EXPSIZE2) - 1)    /* Maximum exponent. */
+#define MAXEXP2         ((1 << EXPSIZE2) - 1)    /* Maximum exponent. */
 
 static comp2_t encode_comp2_t(u64 value)
 {
@@ -427,7 +382,7 @@ static comp2_t encode_comp2_t(u64 value)
 }
 #endif
 
-#if ACCT_VERSION==3
+#if ACCT_VERSION == 3
 /*
  * encode an u64 into a 32 bit IEEE float
  */
@@ -436,8 +391,9 @@ static u32 encode_float(u64 value)
        unsigned exp = 190;
        unsigned u;
 
-       if (value==0) return 0;
-       while ((s64)value > 0){
+       if (value == 0)
+               return 0;
+       while ((s64)value > 0) {
                value <<= 1;
                exp--;
        }
@@ -475,16 +431,17 @@ static void fill_ac(acct_t *ac)
        run_time -= current->group_leader->start_time;
        /* convert nsec -> AHZ */
        elapsed = nsec_to_AHZ(run_time);
-#if ACCT_VERSION==3
+#if ACCT_VERSION == 3
        ac->ac_etime = encode_float(elapsed);
 #else
        ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
-                              (unsigned long) elapsed : (unsigned long) -1l);
+                               (unsigned long) elapsed : (unsigned long) -1l);
 #endif
-#if ACCT_VERSION==1 || ACCT_VERSION==2
+#if ACCT_VERSION == 1 || ACCT_VERSION == 2
        {
                /* new enlarged etime field */
                comp2_t etime = encode_comp2_t(elapsed);
+
                ac->ac_etime_hi = etime >> 16;
                ac->ac_etime_lo = (u16) etime;
        }
@@ -537,12 +494,12 @@ static void do_acct_process(struct bsd_acct_struct *acct)
        /* we really need to bite the bullet and change layout */
        ac.ac_uid = from_kuid_munged(file->f_cred->user_ns, orig_cred->uid);
        ac.ac_gid = from_kgid_munged(file->f_cred->user_ns, orig_cred->gid);
-#if ACCT_VERSION==1 || ACCT_VERSION==2
+#if ACCT_VERSION == 1 || ACCT_VERSION == 2
        /* backward-compatible 16 bit fields */
        ac.ac_uid16 = ac.ac_uid;
        ac.ac_gid16 = ac.ac_gid;
 #endif
-#if ACCT_VERSION==3
+#if ACCT_VERSION == 3
        ac.ac_pid = task_tgid_nr_ns(current, ns);
        rcu_read_lock();
        ac.ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns);
@@ -576,6 +533,7 @@ void acct_collect(long exitcode, int group_dead)
 
        if (group_dead && current->mm) {
                struct vm_area_struct *vma;
+
                down_read(&current->mm->mmap_sem);
                vma = current->mm->mmap;
                while (vma) {
@@ -614,7 +572,7 @@ static void slow_acct_process(struct pid_namespace *ns)
                if (acct) {
                        do_acct_process(acct);
                        mutex_unlock(&acct->lock);
-                       acct_put(acct);
+                       pin_put(&acct->pin);
                }
        }
 }