wake up s_wait_unfrozen when ->freeze_fs fails
[linux-2.6.git] / block / blk-cgroup.c
index e41cc6f..fa8f263 100644 (file)
@@ -31,9 +31,9 @@ EXPORT_SYMBOL_GPL(blkio_root_cgroup);
 static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
                                                  struct cgroup *);
 static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
-                             struct task_struct *, bool);
+                             struct cgroup_taskset *);
 static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
-                          struct cgroup *, struct task_struct *, bool);
+                          struct cgroup_taskset *);
 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
 
@@ -441,15 +441,27 @@ void blkiocg_update_completion_stats(struct blkio_group *blkg,
 }
 EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
 
+/*  Merged stats are per cpu.  */
 void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
                                        bool sync)
 {
+       struct blkio_group_stats_cpu *stats_cpu;
        unsigned long flags;
 
-       spin_lock_irqsave(&blkg->stats_lock, flags);
-       blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
-                       sync);
-       spin_unlock_irqrestore(&blkg->stats_lock, flags);
+       /*
+        * Disabling interrupts to provide mutual exclusion between two
+        * writes on same cpu. It probably is not needed for 64bit. Not
+        * optimizing that case yet.
+        */
+       local_irq_save(flags);
+
+       stats_cpu = this_cpu_ptr(blkg->stats_cpu);
+
+       u64_stats_update_begin(&stats_cpu->syncp);
+       blkio_add_stat(stats_cpu->stat_arr_cpu[BLKIO_STAT_CPU_MERGED], 1,
+                               direction, sync);
+       u64_stats_update_end(&stats_cpu->syncp);
+       local_irq_restore(flags);
 }
 EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
 
@@ -758,27 +770,16 @@ static uint64_t blkio_get_stat(struct blkio_group *blkg,
        return disk_total;
 }
 
-static int blkio_check_dev_num(dev_t dev)
-{
-       int part = 0;
-       struct gendisk *disk;
-
-       disk = get_gendisk(dev, &part);
-       if (!disk || part)
-               return -ENODEV;
-
-       return 0;
-}
-
 static int blkio_policy_parse_and_set(char *buf,
        struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
 {
+       struct gendisk *disk = NULL;
        char *s[4], *p, *major_s = NULL, *minor_s = NULL;
-       int ret;
-       unsigned long major, minor, temp;
-       int i = 0;
+       unsigned long major, minor;
+       int i = 0, ret = -EINVAL;
+       int part;
        dev_t dev;
-       u64 bps, iops;
+       u64 temp;
 
        memset(s, 0, sizeof(s));
 
@@ -794,43 +795,45 @@ static int blkio_policy_parse_and_set(char *buf,
        }
 
        if (i != 2)
-               return -EINVAL;
+               goto out;
 
        p = strsep(&s[0], ":");
        if (p != NULL)
                major_s = p;
        else
-               return -EINVAL;
+               goto out;
 
        minor_s = s[0];
        if (!minor_s)
-               return -EINVAL;
+               goto out;
 
-       ret = strict_strtoul(major_s, 10, &major);
-       if (ret)
-               return -EINVAL;
+       if (strict_strtoul(major_s, 10, &major))
+               goto out;
 
-       ret = strict_strtoul(minor_s, 10, &minor);
-       if (ret)
-               return -EINVAL;
+       if (strict_strtoul(minor_s, 10, &minor))
+               goto out;
 
        dev = MKDEV(major, minor);
 
-       ret = blkio_check_dev_num(dev);
-       if (ret)
-               return ret;
+       if (strict_strtoull(s[1], 10, &temp))
+               goto out;
 
-       newpn->dev = dev;
+       /* For rule removal, do not check for device presence. */
+       if (temp) {
+               disk = get_gendisk(dev, &part);
+               if (!disk || part) {
+                       ret = -ENODEV;
+                       goto out;
+               }
+       }
 
-       if (s[1] == NULL)
-               return -EINVAL;
+       newpn->dev = dev;
 
        switch (plid) {
        case BLKIO_POLICY_PROP:
-               ret = strict_strtoul(s[1], 10, &temp);
-               if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
-                       temp > BLKIO_WEIGHT_MAX)
-                       return -EINVAL;
+               if ((temp < BLKIO_WEIGHT_MIN && temp > 0) ||
+                    temp > BLKIO_WEIGHT_MAX)
+                       goto out;
 
                newpn->plid = plid;
                newpn->fileid = fileid;
@@ -840,94 +843,114 @@ static int blkio_policy_parse_and_set(char *buf,
                switch(fileid) {
                case BLKIO_THROTL_read_bps_device:
                case BLKIO_THROTL_write_bps_device:
-                       ret = strict_strtoull(s[1], 10, &bps);
-                       if (ret)
-                               return -EINVAL;
-
                        newpn->plid = plid;
                        newpn->fileid = fileid;
-                       newpn->val.bps = bps;
+                       newpn->val.bps = temp;
                        break;
                case BLKIO_THROTL_read_iops_device:
                case BLKIO_THROTL_write_iops_device:
-                       ret = strict_strtoull(s[1], 10, &iops);
-                       if (ret)
-                               return -EINVAL;
-
-                       if (iops > THROTL_IOPS_MAX)
-                               return -EINVAL;
+                       if (temp > THROTL_IOPS_MAX)
+                               goto out;
 
                        newpn->plid = plid;
                        newpn->fileid = fileid;
-                       newpn->val.iops = (unsigned int)iops;
+                       newpn->val.iops = (unsigned int)temp;
                        break;
                }
                break;
        default:
                BUG();
        }
-
-       return 0;
+       ret = 0;
+out:
+       put_disk(disk);
+       return ret;
 }
 
 unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
                              dev_t dev)
 {
        struct blkio_policy_node *pn;
+       unsigned long flags;
+       unsigned int weight;
+
+       spin_lock_irqsave(&blkcg->lock, flags);
 
        pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
                                BLKIO_PROP_weight_device);
        if (pn)
-               return pn->val.weight;
+               weight = pn->val.weight;
        else
-               return blkcg->weight;
+               weight = blkcg->weight;
+
+       spin_unlock_irqrestore(&blkcg->lock, flags);
+
+       return weight;
 }
 EXPORT_SYMBOL_GPL(blkcg_get_weight);
 
 uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
 {
        struct blkio_policy_node *pn;
+       unsigned long flags;
+       uint64_t bps = -1;
 
+       spin_lock_irqsave(&blkcg->lock, flags);
        pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
                                BLKIO_THROTL_read_bps_device);
        if (pn)
-               return pn->val.bps;
-       else
-               return -1;
+               bps = pn->val.bps;
+       spin_unlock_irqrestore(&blkcg->lock, flags);
+
+       return bps;
 }
 
 uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
 {
        struct blkio_policy_node *pn;
+       unsigned long flags;
+       uint64_t bps = -1;
+
+       spin_lock_irqsave(&blkcg->lock, flags);
        pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
                                BLKIO_THROTL_write_bps_device);
        if (pn)
-               return pn->val.bps;
-       else
-               return -1;
+               bps = pn->val.bps;
+       spin_unlock_irqrestore(&blkcg->lock, flags);
+
+       return bps;
 }
 
 unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
 {
        struct blkio_policy_node *pn;
+       unsigned long flags;
+       unsigned int iops = -1;
 
+       spin_lock_irqsave(&blkcg->lock, flags);
        pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
                                BLKIO_THROTL_read_iops_device);
        if (pn)
-               return pn->val.iops;
-       else
-               return -1;
+               iops = pn->val.iops;
+       spin_unlock_irqrestore(&blkcg->lock, flags);
+
+       return iops;
 }
 
 unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
 {
        struct blkio_policy_node *pn;
+       unsigned long flags;
+       unsigned int iops = -1;
+
+       spin_lock_irqsave(&blkcg->lock, flags);
        pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
                                BLKIO_THROTL_write_iops_device);
        if (pn)
-               return pn->val.iops;
-       else
-               return -1;
+               iops = pn->val.iops;
+       spin_unlock_irqrestore(&blkcg->lock, flags);
+
+       return iops;
 }
 
 /* Checks whether user asked for deleting a policy rule */
@@ -1080,6 +1103,7 @@ static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
 
        if (blkio_delete_rule_command(newpn)) {
                blkio_policy_delete_node(pn);
+               kfree(pn);
                spin_unlock_irq(&blkcg->lock);
                goto update_io_group;
        }
@@ -1244,7 +1268,7 @@ static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
                                                BLKIO_STAT_WAIT_TIME, 1, 0);
                case BLKIO_PROP_io_merged:
                        return blkio_read_blkg_stats(blkcg, cft, cb,
-                                               BLKIO_STAT_MERGED, 1, 0);
+                                               BLKIO_STAT_CPU_MERGED, 1, 1);
                case BLKIO_PROP_io_queued:
                        return blkio_read_blkg_stats(blkcg, cft, cb,
                                                BLKIO_STAT_QUEUED, 1, 0);
@@ -1604,34 +1628,40 @@ done:
  * of the main cic data structures.  For now we allow a task to change
  * its cgroup only if it's the only owner of its ioc.
  */
-static int blkiocg_can_attach(struct cgroup_subsys *subsys,
-                               struct cgroup *cgroup, struct task_struct *tsk,
-                               bool threadgroup)
+static int blkiocg_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
+                             struct cgroup_taskset *tset)
 {
+       struct task_struct *task;
        struct io_context *ioc;
        int ret = 0;
 
        /* task_lock() is needed to avoid races with exit_io_context() */
-       task_lock(tsk);
-       ioc = tsk->io_context;
-       if (ioc && atomic_read(&ioc->nr_tasks) > 1)
-               ret = -EINVAL;
-       task_unlock(tsk);
-
+       cgroup_taskset_for_each(task, cgrp, tset) {
+               task_lock(task);
+               ioc = task->io_context;
+               if (ioc && atomic_read(&ioc->nr_tasks) > 1)
+                       ret = -EINVAL;
+               task_unlock(task);
+               if (ret)
+                       break;
+       }
        return ret;
 }
 
-static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
-                               struct cgroup *prev, struct task_struct *tsk,
-                               bool threadgroup)
+static void blkiocg_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
+                          struct cgroup_taskset *tset)
 {
+       struct task_struct *task;
        struct io_context *ioc;
 
-       task_lock(tsk);
-       ioc = tsk->io_context;
-       if (ioc)
-               ioc->cgroup_changed = 1;
-       task_unlock(tsk);
+       cgroup_taskset_for_each(task, cgrp, tset) {
+               /* we don't lose anything even if ioc allocation fails */
+               ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
+               if (ioc) {
+                       ioc_cgroup_changed(ioc);
+                       put_io_context(ioc, NULL);
+               }
+       }
 }
 
 void blkio_policy_register(struct blkio_policy_type *blkiop)