blkio: deletion of a cgroup was causes oops
[pandora-kernel.git] / block / blk-cgroup.c
1 /*
2  * Common Block IO controller cgroup interface
3  *
4  * Based on ideas and code from CFQ, CFS and BFQ:
5  * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6  *
7  * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8  *                    Paolo Valente <paolo.valente@unimore.it>
9  *
10  * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
11  *                    Nauman Rafique <nauman@google.com>
12  */
13 #include <linux/ioprio.h>
14 #include <linux/seq_file.h>
15 #include <linux/kdev_t.h>
16 #include <linux/module.h>
17 #include <linux/err.h>
18 #include <linux/blkdev.h>
19 #include <linux/slab.h>
20 #include "blk-cgroup.h"
21 #include <linux/genhd.h>
22
23 #define MAX_KEY_LEN 100
24
25 static DEFINE_SPINLOCK(blkio_list_lock);
26 static LIST_HEAD(blkio_list);
27
28 struct blkio_cgroup blkio_root_cgroup = { .weight = 2*BLKIO_WEIGHT_DEFAULT };
29 EXPORT_SYMBOL_GPL(blkio_root_cgroup);
30
31 static struct cgroup_subsys_state *blkiocg_create(struct cgroup_subsys *,
32                                                   struct cgroup *);
33 static int blkiocg_can_attach(struct cgroup_subsys *, struct cgroup *,
34                               struct task_struct *, bool);
35 static void blkiocg_attach(struct cgroup_subsys *, struct cgroup *,
36                            struct cgroup *, struct task_struct *, bool);
37 static void blkiocg_destroy(struct cgroup_subsys *, struct cgroup *);
38 static int blkiocg_populate(struct cgroup_subsys *, struct cgroup *);
39
40 /* for encoding cft->private value on file */
41 #define BLKIOFILE_PRIVATE(x, val)       (((x) << 16) | (val))
42 /* What policy owns the file, proportional or throttle */
43 #define BLKIOFILE_POLICY(val)           (((val) >> 16) & 0xffff)
44 #define BLKIOFILE_ATTR(val)             ((val) & 0xffff)
45
46 struct cgroup_subsys blkio_subsys = {
47         .name = "blkio",
48         .create = blkiocg_create,
49         .can_attach = blkiocg_can_attach,
50         .attach = blkiocg_attach,
51         .destroy = blkiocg_destroy,
52         .populate = blkiocg_populate,
53 #ifdef CONFIG_BLK_CGROUP
54         /* note: blkio_subsys_id is otherwise defined in blk-cgroup.h */
55         .subsys_id = blkio_subsys_id,
56 #endif
57         .use_id = 1,
58         .module = THIS_MODULE,
59 };
60 EXPORT_SYMBOL_GPL(blkio_subsys);
61
62 static inline void blkio_policy_insert_node(struct blkio_cgroup *blkcg,
63                                             struct blkio_policy_node *pn)
64 {
65         list_add(&pn->node, &blkcg->policy_list);
66 }
67
68 static inline bool cftype_blkg_same_policy(struct cftype *cft,
69                         struct blkio_group *blkg)
70 {
71         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
72
73         if (blkg->plid == plid)
74                 return 1;
75
76         return 0;
77 }
78
79 /* Determines if policy node matches cgroup file being accessed */
80 static inline bool pn_matches_cftype(struct cftype *cft,
81                         struct blkio_policy_node *pn)
82 {
83         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
84         int fileid = BLKIOFILE_ATTR(cft->private);
85
86         return (plid == pn->plid && fileid == pn->fileid);
87 }
88
89 /* Must be called with blkcg->lock held */
90 static inline void blkio_policy_delete_node(struct blkio_policy_node *pn)
91 {
92         list_del(&pn->node);
93 }
94
95 /* Must be called with blkcg->lock held */
96 static struct blkio_policy_node *
97 blkio_policy_search_node(const struct blkio_cgroup *blkcg, dev_t dev,
98                 enum blkio_policy_id plid, int fileid)
99 {
100         struct blkio_policy_node *pn;
101
102         list_for_each_entry(pn, &blkcg->policy_list, node) {
103                 if (pn->dev == dev && pn->plid == plid && pn->fileid == fileid)
104                         return pn;
105         }
106
107         return NULL;
108 }
109
110 struct blkio_cgroup *cgroup_to_blkio_cgroup(struct cgroup *cgroup)
111 {
112         return container_of(cgroup_subsys_state(cgroup, blkio_subsys_id),
113                             struct blkio_cgroup, css);
114 }
115 EXPORT_SYMBOL_GPL(cgroup_to_blkio_cgroup);
116
117 static inline void
118 blkio_update_group_weight(struct blkio_group *blkg, unsigned int weight)
119 {
120         struct blkio_policy_type *blkiop;
121
122         list_for_each_entry(blkiop, &blkio_list, list) {
123                 /* If this policy does not own the blkg, do not send updates */
124                 if (blkiop->plid != blkg->plid)
125                         continue;
126                 if (blkiop->ops.blkio_update_group_weight_fn)
127                         blkiop->ops.blkio_update_group_weight_fn(blkg, weight);
128         }
129 }
130
131 static inline void blkio_update_group_bps(struct blkio_group *blkg, u64 bps,
132                                 int fileid)
133 {
134         struct blkio_policy_type *blkiop;
135
136         list_for_each_entry(blkiop, &blkio_list, list) {
137
138                 /* If this policy does not own the blkg, do not send updates */
139                 if (blkiop->plid != blkg->plid)
140                         continue;
141
142                 if (fileid == BLKIO_THROTL_read_bps_device
143                     && blkiop->ops.blkio_update_group_read_bps_fn)
144                         blkiop->ops.blkio_update_group_read_bps_fn(blkg, bps);
145
146                 if (fileid == BLKIO_THROTL_write_bps_device
147                     && blkiop->ops.blkio_update_group_write_bps_fn)
148                         blkiop->ops.blkio_update_group_write_bps_fn(blkg, bps);
149         }
150 }
151
152 static inline void blkio_update_group_iops(struct blkio_group *blkg,
153                         unsigned int iops, int fileid)
154 {
155         struct blkio_policy_type *blkiop;
156
157         list_for_each_entry(blkiop, &blkio_list, list) {
158
159                 /* If this policy does not own the blkg, do not send updates */
160                 if (blkiop->plid != blkg->plid)
161                         continue;
162
163                 if (fileid == BLKIO_THROTL_read_iops_device
164                     && blkiop->ops.blkio_update_group_read_iops_fn)
165                         blkiop->ops.blkio_update_group_read_iops_fn(blkg, iops);
166
167                 if (fileid == BLKIO_THROTL_write_iops_device
168                     && blkiop->ops.blkio_update_group_write_iops_fn)
169                         blkiop->ops.blkio_update_group_write_iops_fn(blkg,iops);
170         }
171 }
172
173 /*
174  * Add to the appropriate stat variable depending on the request type.
175  * This should be called with the blkg->stats_lock held.
176  */
177 static void blkio_add_stat(uint64_t *stat, uint64_t add, bool direction,
178                                 bool sync)
179 {
180         if (direction)
181                 stat[BLKIO_STAT_WRITE] += add;
182         else
183                 stat[BLKIO_STAT_READ] += add;
184         if (sync)
185                 stat[BLKIO_STAT_SYNC] += add;
186         else
187                 stat[BLKIO_STAT_ASYNC] += add;
188 }
189
190 /*
191  * Decrements the appropriate stat variable if non-zero depending on the
192  * request type. Panics on value being zero.
193  * This should be called with the blkg->stats_lock held.
194  */
195 static void blkio_check_and_dec_stat(uint64_t *stat, bool direction, bool sync)
196 {
197         if (direction) {
198                 BUG_ON(stat[BLKIO_STAT_WRITE] == 0);
199                 stat[BLKIO_STAT_WRITE]--;
200         } else {
201                 BUG_ON(stat[BLKIO_STAT_READ] == 0);
202                 stat[BLKIO_STAT_READ]--;
203         }
204         if (sync) {
205                 BUG_ON(stat[BLKIO_STAT_SYNC] == 0);
206                 stat[BLKIO_STAT_SYNC]--;
207         } else {
208                 BUG_ON(stat[BLKIO_STAT_ASYNC] == 0);
209                 stat[BLKIO_STAT_ASYNC]--;
210         }
211 }
212
213 #ifdef CONFIG_DEBUG_BLK_CGROUP
214 /* This should be called with the blkg->stats_lock held. */
215 static void blkio_set_start_group_wait_time(struct blkio_group *blkg,
216                                                 struct blkio_group *curr_blkg)
217 {
218         if (blkio_blkg_waiting(&blkg->stats))
219                 return;
220         if (blkg == curr_blkg)
221                 return;
222         blkg->stats.start_group_wait_time = sched_clock();
223         blkio_mark_blkg_waiting(&blkg->stats);
224 }
225
226 /* This should be called with the blkg->stats_lock held. */
227 static void blkio_update_group_wait_time(struct blkio_group_stats *stats)
228 {
229         unsigned long long now;
230
231         if (!blkio_blkg_waiting(stats))
232                 return;
233
234         now = sched_clock();
235         if (time_after64(now, stats->start_group_wait_time))
236                 stats->group_wait_time += now - stats->start_group_wait_time;
237         blkio_clear_blkg_waiting(stats);
238 }
239
240 /* This should be called with the blkg->stats_lock held. */
241 static void blkio_end_empty_time(struct blkio_group_stats *stats)
242 {
243         unsigned long long now;
244
245         if (!blkio_blkg_empty(stats))
246                 return;
247
248         now = sched_clock();
249         if (time_after64(now, stats->start_empty_time))
250                 stats->empty_time += now - stats->start_empty_time;
251         blkio_clear_blkg_empty(stats);
252 }
253
254 void blkiocg_update_set_idle_time_stats(struct blkio_group *blkg)
255 {
256         unsigned long flags;
257
258         spin_lock_irqsave(&blkg->stats_lock, flags);
259         BUG_ON(blkio_blkg_idling(&blkg->stats));
260         blkg->stats.start_idle_time = sched_clock();
261         blkio_mark_blkg_idling(&blkg->stats);
262         spin_unlock_irqrestore(&blkg->stats_lock, flags);
263 }
264 EXPORT_SYMBOL_GPL(blkiocg_update_set_idle_time_stats);
265
266 void blkiocg_update_idle_time_stats(struct blkio_group *blkg)
267 {
268         unsigned long flags;
269         unsigned long long now;
270         struct blkio_group_stats *stats;
271
272         spin_lock_irqsave(&blkg->stats_lock, flags);
273         stats = &blkg->stats;
274         if (blkio_blkg_idling(stats)) {
275                 now = sched_clock();
276                 if (time_after64(now, stats->start_idle_time))
277                         stats->idle_time += now - stats->start_idle_time;
278                 blkio_clear_blkg_idling(stats);
279         }
280         spin_unlock_irqrestore(&blkg->stats_lock, flags);
281 }
282 EXPORT_SYMBOL_GPL(blkiocg_update_idle_time_stats);
283
284 void blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg)
285 {
286         unsigned long flags;
287         struct blkio_group_stats *stats;
288
289         spin_lock_irqsave(&blkg->stats_lock, flags);
290         stats = &blkg->stats;
291         stats->avg_queue_size_sum +=
292                         stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] +
293                         stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE];
294         stats->avg_queue_size_samples++;
295         blkio_update_group_wait_time(stats);
296         spin_unlock_irqrestore(&blkg->stats_lock, flags);
297 }
298 EXPORT_SYMBOL_GPL(blkiocg_update_avg_queue_size_stats);
299
300 void blkiocg_set_start_empty_time(struct blkio_group *blkg)
301 {
302         unsigned long flags;
303         struct blkio_group_stats *stats;
304
305         spin_lock_irqsave(&blkg->stats_lock, flags);
306         stats = &blkg->stats;
307
308         if (stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_READ] ||
309                         stats->stat_arr[BLKIO_STAT_QUEUED][BLKIO_STAT_WRITE]) {
310                 spin_unlock_irqrestore(&blkg->stats_lock, flags);
311                 return;
312         }
313
314         /*
315          * group is already marked empty. This can happen if cfqq got new
316          * request in parent group and moved to this group while being added
317          * to service tree. Just ignore the event and move on.
318          */
319         if(blkio_blkg_empty(stats)) {
320                 spin_unlock_irqrestore(&blkg->stats_lock, flags);
321                 return;
322         }
323
324         stats->start_empty_time = sched_clock();
325         blkio_mark_blkg_empty(stats);
326         spin_unlock_irqrestore(&blkg->stats_lock, flags);
327 }
328 EXPORT_SYMBOL_GPL(blkiocg_set_start_empty_time);
329
330 void blkiocg_update_dequeue_stats(struct blkio_group *blkg,
331                         unsigned long dequeue)
332 {
333         blkg->stats.dequeue += dequeue;
334 }
335 EXPORT_SYMBOL_GPL(blkiocg_update_dequeue_stats);
336 #else
337 static inline void blkio_set_start_group_wait_time(struct blkio_group *blkg,
338                                         struct blkio_group *curr_blkg) {}
339 static inline void blkio_end_empty_time(struct blkio_group_stats *stats) {}
340 #endif
341
342 void blkiocg_update_io_add_stats(struct blkio_group *blkg,
343                         struct blkio_group *curr_blkg, bool direction,
344                         bool sync)
345 {
346         unsigned long flags;
347
348         spin_lock_irqsave(&blkg->stats_lock, flags);
349         blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED], 1, direction,
350                         sync);
351         blkio_end_empty_time(&blkg->stats);
352         blkio_set_start_group_wait_time(blkg, curr_blkg);
353         spin_unlock_irqrestore(&blkg->stats_lock, flags);
354 }
355 EXPORT_SYMBOL_GPL(blkiocg_update_io_add_stats);
356
357 void blkiocg_update_io_remove_stats(struct blkio_group *blkg,
358                                                 bool direction, bool sync)
359 {
360         unsigned long flags;
361
362         spin_lock_irqsave(&blkg->stats_lock, flags);
363         blkio_check_and_dec_stat(blkg->stats.stat_arr[BLKIO_STAT_QUEUED],
364                                         direction, sync);
365         spin_unlock_irqrestore(&blkg->stats_lock, flags);
366 }
367 EXPORT_SYMBOL_GPL(blkiocg_update_io_remove_stats);
368
369 void blkiocg_update_timeslice_used(struct blkio_group *blkg, unsigned long time)
370 {
371         unsigned long flags;
372
373         spin_lock_irqsave(&blkg->stats_lock, flags);
374         blkg->stats.time += time;
375         spin_unlock_irqrestore(&blkg->stats_lock, flags);
376 }
377 EXPORT_SYMBOL_GPL(blkiocg_update_timeslice_used);
378
379 void blkiocg_update_dispatch_stats(struct blkio_group *blkg,
380                                 uint64_t bytes, bool direction, bool sync)
381 {
382         struct blkio_group_stats *stats;
383         unsigned long flags;
384
385         spin_lock_irqsave(&blkg->stats_lock, flags);
386         stats = &blkg->stats;
387         stats->sectors += bytes >> 9;
388         blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICED], 1, direction,
389                         sync);
390         blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_BYTES], bytes,
391                         direction, sync);
392         spin_unlock_irqrestore(&blkg->stats_lock, flags);
393 }
394 EXPORT_SYMBOL_GPL(blkiocg_update_dispatch_stats);
395
396 void blkiocg_update_completion_stats(struct blkio_group *blkg,
397         uint64_t start_time, uint64_t io_start_time, bool direction, bool sync)
398 {
399         struct blkio_group_stats *stats;
400         unsigned long flags;
401         unsigned long long now = sched_clock();
402
403         spin_lock_irqsave(&blkg->stats_lock, flags);
404         stats = &blkg->stats;
405         if (time_after64(now, io_start_time))
406                 blkio_add_stat(stats->stat_arr[BLKIO_STAT_SERVICE_TIME],
407                                 now - io_start_time, direction, sync);
408         if (time_after64(io_start_time, start_time))
409                 blkio_add_stat(stats->stat_arr[BLKIO_STAT_WAIT_TIME],
410                                 io_start_time - start_time, direction, sync);
411         spin_unlock_irqrestore(&blkg->stats_lock, flags);
412 }
413 EXPORT_SYMBOL_GPL(blkiocg_update_completion_stats);
414
415 void blkiocg_update_io_merged_stats(struct blkio_group *blkg, bool direction,
416                                         bool sync)
417 {
418         unsigned long flags;
419
420         spin_lock_irqsave(&blkg->stats_lock, flags);
421         blkio_add_stat(blkg->stats.stat_arr[BLKIO_STAT_MERGED], 1, direction,
422                         sync);
423         spin_unlock_irqrestore(&blkg->stats_lock, flags);
424 }
425 EXPORT_SYMBOL_GPL(blkiocg_update_io_merged_stats);
426
427 void blkiocg_add_blkio_group(struct blkio_cgroup *blkcg,
428                 struct blkio_group *blkg, void *key, dev_t dev,
429                 enum blkio_policy_id plid)
430 {
431         unsigned long flags;
432
433         spin_lock_irqsave(&blkcg->lock, flags);
434         spin_lock_init(&blkg->stats_lock);
435         rcu_assign_pointer(blkg->key, key);
436         blkg->blkcg_id = css_id(&blkcg->css);
437         hlist_add_head_rcu(&blkg->blkcg_node, &blkcg->blkg_list);
438         blkg->plid = plid;
439         spin_unlock_irqrestore(&blkcg->lock, flags);
440         /* Need to take css reference ? */
441         cgroup_path(blkcg->css.cgroup, blkg->path, sizeof(blkg->path));
442         blkg->dev = dev;
443 }
444 EXPORT_SYMBOL_GPL(blkiocg_add_blkio_group);
445
446 static void __blkiocg_del_blkio_group(struct blkio_group *blkg)
447 {
448         hlist_del_init_rcu(&blkg->blkcg_node);
449         blkg->blkcg_id = 0;
450 }
451
452 /*
453  * returns 0 if blkio_group was still on cgroup list. Otherwise returns 1
454  * indicating that blk_group was unhashed by the time we got to it.
455  */
456 int blkiocg_del_blkio_group(struct blkio_group *blkg)
457 {
458         struct blkio_cgroup *blkcg;
459         unsigned long flags;
460         struct cgroup_subsys_state *css;
461         int ret = 1;
462
463         rcu_read_lock();
464         css = css_lookup(&blkio_subsys, blkg->blkcg_id);
465         if (css) {
466                 blkcg = container_of(css, struct blkio_cgroup, css);
467                 spin_lock_irqsave(&blkcg->lock, flags);
468                 if (!hlist_unhashed(&blkg->blkcg_node)) {
469                         __blkiocg_del_blkio_group(blkg);
470                         ret = 0;
471                 }
472                 spin_unlock_irqrestore(&blkcg->lock, flags);
473         }
474
475         rcu_read_unlock();
476         return ret;
477 }
478 EXPORT_SYMBOL_GPL(blkiocg_del_blkio_group);
479
480 /* called under rcu_read_lock(). */
481 struct blkio_group *blkiocg_lookup_group(struct blkio_cgroup *blkcg, void *key)
482 {
483         struct blkio_group *blkg;
484         struct hlist_node *n;
485         void *__key;
486
487         hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
488                 __key = blkg->key;
489                 if (__key == key)
490                         return blkg;
491         }
492
493         return NULL;
494 }
495 EXPORT_SYMBOL_GPL(blkiocg_lookup_group);
496
497 static int
498 blkiocg_reset_stats(struct cgroup *cgroup, struct cftype *cftype, u64 val)
499 {
500         struct blkio_cgroup *blkcg;
501         struct blkio_group *blkg;
502         struct blkio_group_stats *stats;
503         struct hlist_node *n;
504         uint64_t queued[BLKIO_STAT_TOTAL];
505         int i;
506 #ifdef CONFIG_DEBUG_BLK_CGROUP
507         bool idling, waiting, empty;
508         unsigned long long now = sched_clock();
509 #endif
510
511         blkcg = cgroup_to_blkio_cgroup(cgroup);
512         spin_lock_irq(&blkcg->lock);
513         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
514                 spin_lock(&blkg->stats_lock);
515                 stats = &blkg->stats;
516 #ifdef CONFIG_DEBUG_BLK_CGROUP
517                 idling = blkio_blkg_idling(stats);
518                 waiting = blkio_blkg_waiting(stats);
519                 empty = blkio_blkg_empty(stats);
520 #endif
521                 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
522                         queued[i] = stats->stat_arr[BLKIO_STAT_QUEUED][i];
523                 memset(stats, 0, sizeof(struct blkio_group_stats));
524                 for (i = 0; i < BLKIO_STAT_TOTAL; i++)
525                         stats->stat_arr[BLKIO_STAT_QUEUED][i] = queued[i];
526 #ifdef CONFIG_DEBUG_BLK_CGROUP
527                 if (idling) {
528                         blkio_mark_blkg_idling(stats);
529                         stats->start_idle_time = now;
530                 }
531                 if (waiting) {
532                         blkio_mark_blkg_waiting(stats);
533                         stats->start_group_wait_time = now;
534                 }
535                 if (empty) {
536                         blkio_mark_blkg_empty(stats);
537                         stats->start_empty_time = now;
538                 }
539 #endif
540                 spin_unlock(&blkg->stats_lock);
541         }
542         spin_unlock_irq(&blkcg->lock);
543         return 0;
544 }
545
546 static void blkio_get_key_name(enum stat_sub_type type, dev_t dev, char *str,
547                                 int chars_left, bool diskname_only)
548 {
549         snprintf(str, chars_left, "%d:%d", MAJOR(dev), MINOR(dev));
550         chars_left -= strlen(str);
551         if (chars_left <= 0) {
552                 printk(KERN_WARNING
553                         "Possibly incorrect cgroup stat display format");
554                 return;
555         }
556         if (diskname_only)
557                 return;
558         switch (type) {
559         case BLKIO_STAT_READ:
560                 strlcat(str, " Read", chars_left);
561                 break;
562         case BLKIO_STAT_WRITE:
563                 strlcat(str, " Write", chars_left);
564                 break;
565         case BLKIO_STAT_SYNC:
566                 strlcat(str, " Sync", chars_left);
567                 break;
568         case BLKIO_STAT_ASYNC:
569                 strlcat(str, " Async", chars_left);
570                 break;
571         case BLKIO_STAT_TOTAL:
572                 strlcat(str, " Total", chars_left);
573                 break;
574         default:
575                 strlcat(str, " Invalid", chars_left);
576         }
577 }
578
579 static uint64_t blkio_fill_stat(char *str, int chars_left, uint64_t val,
580                                 struct cgroup_map_cb *cb, dev_t dev)
581 {
582         blkio_get_key_name(0, dev, str, chars_left, true);
583         cb->fill(cb, str, val);
584         return val;
585 }
586
587 /* This should be called with blkg->stats_lock held */
588 static uint64_t blkio_get_stat(struct blkio_group *blkg,
589                 struct cgroup_map_cb *cb, dev_t dev, enum stat_type type)
590 {
591         uint64_t disk_total;
592         char key_str[MAX_KEY_LEN];
593         enum stat_sub_type sub_type;
594
595         if (type == BLKIO_STAT_TIME)
596                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
597                                         blkg->stats.time, cb, dev);
598         if (type == BLKIO_STAT_SECTORS)
599                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
600                                         blkg->stats.sectors, cb, dev);
601 #ifdef CONFIG_DEBUG_BLK_CGROUP
602         if (type == BLKIO_STAT_AVG_QUEUE_SIZE) {
603                 uint64_t sum = blkg->stats.avg_queue_size_sum;
604                 uint64_t samples = blkg->stats.avg_queue_size_samples;
605                 if (samples)
606                         do_div(sum, samples);
607                 else
608                         sum = 0;
609                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1, sum, cb, dev);
610         }
611         if (type == BLKIO_STAT_GROUP_WAIT_TIME)
612                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
613                                         blkg->stats.group_wait_time, cb, dev);
614         if (type == BLKIO_STAT_IDLE_TIME)
615                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
616                                         blkg->stats.idle_time, cb, dev);
617         if (type == BLKIO_STAT_EMPTY_TIME)
618                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
619                                         blkg->stats.empty_time, cb, dev);
620         if (type == BLKIO_STAT_DEQUEUE)
621                 return blkio_fill_stat(key_str, MAX_KEY_LEN - 1,
622                                         blkg->stats.dequeue, cb, dev);
623 #endif
624
625         for (sub_type = BLKIO_STAT_READ; sub_type < BLKIO_STAT_TOTAL;
626                         sub_type++) {
627                 blkio_get_key_name(sub_type, dev, key_str, MAX_KEY_LEN, false);
628                 cb->fill(cb, key_str, blkg->stats.stat_arr[type][sub_type]);
629         }
630         disk_total = blkg->stats.stat_arr[type][BLKIO_STAT_READ] +
631                         blkg->stats.stat_arr[type][BLKIO_STAT_WRITE];
632         blkio_get_key_name(BLKIO_STAT_TOTAL, dev, key_str, MAX_KEY_LEN, false);
633         cb->fill(cb, key_str, disk_total);
634         return disk_total;
635 }
636
637 static int blkio_check_dev_num(dev_t dev)
638 {
639         int part = 0;
640         struct gendisk *disk;
641
642         disk = get_gendisk(dev, &part);
643         if (!disk || part)
644                 return -ENODEV;
645
646         return 0;
647 }
648
649 static int blkio_policy_parse_and_set(char *buf,
650         struct blkio_policy_node *newpn, enum blkio_policy_id plid, int fileid)
651 {
652         char *s[4], *p, *major_s = NULL, *minor_s = NULL;
653         int ret;
654         unsigned long major, minor, temp, iops;
655         int i = 0;
656         dev_t dev;
657         u64 bps;
658
659         memset(s, 0, sizeof(s));
660
661         while ((p = strsep(&buf, " ")) != NULL) {
662                 if (!*p)
663                         continue;
664
665                 s[i++] = p;
666
667                 /* Prevent from inputing too many things */
668                 if (i == 3)
669                         break;
670         }
671
672         if (i != 2)
673                 return -EINVAL;
674
675         p = strsep(&s[0], ":");
676         if (p != NULL)
677                 major_s = p;
678         else
679                 return -EINVAL;
680
681         minor_s = s[0];
682         if (!minor_s)
683                 return -EINVAL;
684
685         ret = strict_strtoul(major_s, 10, &major);
686         if (ret)
687                 return -EINVAL;
688
689         ret = strict_strtoul(minor_s, 10, &minor);
690         if (ret)
691                 return -EINVAL;
692
693         dev = MKDEV(major, minor);
694
695         ret = blkio_check_dev_num(dev);
696         if (ret)
697                 return ret;
698
699         newpn->dev = dev;
700
701         if (s[1] == NULL)
702                 return -EINVAL;
703
704         switch (plid) {
705         case BLKIO_POLICY_PROP:
706                 ret = strict_strtoul(s[1], 10, &temp);
707                 if (ret || (temp < BLKIO_WEIGHT_MIN && temp > 0) ||
708                         temp > BLKIO_WEIGHT_MAX)
709                         return -EINVAL;
710
711                 newpn->plid = plid;
712                 newpn->fileid = fileid;
713                 newpn->val.weight = temp;
714                 break;
715         case BLKIO_POLICY_THROTL:
716                 switch(fileid) {
717                 case BLKIO_THROTL_read_bps_device:
718                 case BLKIO_THROTL_write_bps_device:
719                         ret = strict_strtoull(s[1], 10, &bps);
720                         if (ret)
721                                 return -EINVAL;
722
723                         newpn->plid = plid;
724                         newpn->fileid = fileid;
725                         newpn->val.bps = bps;
726                         break;
727                 case BLKIO_THROTL_read_iops_device:
728                 case BLKIO_THROTL_write_iops_device:
729                         ret = strict_strtoul(s[1], 10, &iops);
730                         if (ret)
731                                 return -EINVAL;
732
733                         newpn->plid = plid;
734                         newpn->fileid = fileid;
735                         newpn->val.iops = iops;
736                         break;
737                 }
738                 break;
739         default:
740                 BUG();
741         }
742
743         return 0;
744 }
745
746 unsigned int blkcg_get_weight(struct blkio_cgroup *blkcg,
747                               dev_t dev)
748 {
749         struct blkio_policy_node *pn;
750
751         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_PROP,
752                                 BLKIO_PROP_weight_device);
753         if (pn)
754                 return pn->val.weight;
755         else
756                 return blkcg->weight;
757 }
758 EXPORT_SYMBOL_GPL(blkcg_get_weight);
759
760 uint64_t blkcg_get_read_bps(struct blkio_cgroup *blkcg, dev_t dev)
761 {
762         struct blkio_policy_node *pn;
763
764         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
765                                 BLKIO_THROTL_read_bps_device);
766         if (pn)
767                 return pn->val.bps;
768         else
769                 return -1;
770 }
771
772 uint64_t blkcg_get_write_bps(struct blkio_cgroup *blkcg, dev_t dev)
773 {
774         struct blkio_policy_node *pn;
775         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
776                                 BLKIO_THROTL_write_bps_device);
777         if (pn)
778                 return pn->val.bps;
779         else
780                 return -1;
781 }
782
783 unsigned int blkcg_get_read_iops(struct blkio_cgroup *blkcg, dev_t dev)
784 {
785         struct blkio_policy_node *pn;
786
787         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
788                                 BLKIO_THROTL_read_iops_device);
789         if (pn)
790                 return pn->val.iops;
791         else
792                 return -1;
793 }
794
795 unsigned int blkcg_get_write_iops(struct blkio_cgroup *blkcg, dev_t dev)
796 {
797         struct blkio_policy_node *pn;
798         pn = blkio_policy_search_node(blkcg, dev, BLKIO_POLICY_THROTL,
799                                 BLKIO_THROTL_write_iops_device);
800         if (pn)
801                 return pn->val.iops;
802         else
803                 return -1;
804 }
805
806 /* Checks whether user asked for deleting a policy rule */
807 static bool blkio_delete_rule_command(struct blkio_policy_node *pn)
808 {
809         switch(pn->plid) {
810         case BLKIO_POLICY_PROP:
811                 if (pn->val.weight == 0)
812                         return 1;
813                 break;
814         case BLKIO_POLICY_THROTL:
815                 switch(pn->fileid) {
816                 case BLKIO_THROTL_read_bps_device:
817                 case BLKIO_THROTL_write_bps_device:
818                         if (pn->val.bps == 0)
819                                 return 1;
820                         break;
821                 case BLKIO_THROTL_read_iops_device:
822                 case BLKIO_THROTL_write_iops_device:
823                         if (pn->val.iops == 0)
824                                 return 1;
825                 }
826                 break;
827         default:
828                 BUG();
829         }
830
831         return 0;
832 }
833
834 static void blkio_update_policy_rule(struct blkio_policy_node *oldpn,
835                                         struct blkio_policy_node *newpn)
836 {
837         switch(oldpn->plid) {
838         case BLKIO_POLICY_PROP:
839                 oldpn->val.weight = newpn->val.weight;
840                 break;
841         case BLKIO_POLICY_THROTL:
842                 switch(newpn->fileid) {
843                 case BLKIO_THROTL_read_bps_device:
844                 case BLKIO_THROTL_write_bps_device:
845                         oldpn->val.bps = newpn->val.bps;
846                         break;
847                 case BLKIO_THROTL_read_iops_device:
848                 case BLKIO_THROTL_write_iops_device:
849                         oldpn->val.iops = newpn->val.iops;
850                 }
851                 break;
852         default:
853                 BUG();
854         }
855 }
856
857 /*
858  * Some rules/values in blkg have changed. Propogate those to respective
859  * policies.
860  */
861 static void blkio_update_blkg_policy(struct blkio_cgroup *blkcg,
862                 struct blkio_group *blkg, struct blkio_policy_node *pn)
863 {
864         unsigned int weight, iops;
865         u64 bps;
866
867         switch(pn->plid) {
868         case BLKIO_POLICY_PROP:
869                 weight = pn->val.weight ? pn->val.weight :
870                                 blkcg->weight;
871                 blkio_update_group_weight(blkg, weight);
872                 break;
873         case BLKIO_POLICY_THROTL:
874                 switch(pn->fileid) {
875                 case BLKIO_THROTL_read_bps_device:
876                 case BLKIO_THROTL_write_bps_device:
877                         bps = pn->val.bps ? pn->val.bps : (-1);
878                         blkio_update_group_bps(blkg, bps, pn->fileid);
879                         break;
880                 case BLKIO_THROTL_read_iops_device:
881                 case BLKIO_THROTL_write_iops_device:
882                         iops = pn->val.iops ? pn->val.iops : (-1);
883                         blkio_update_group_iops(blkg, iops, pn->fileid);
884                         break;
885                 }
886                 break;
887         default:
888                 BUG();
889         }
890 }
891
892 /*
893  * A policy node rule has been updated. Propogate this update to all the
894  * block groups which might be affected by this update.
895  */
896 static void blkio_update_policy_node_blkg(struct blkio_cgroup *blkcg,
897                                 struct blkio_policy_node *pn)
898 {
899         struct blkio_group *blkg;
900         struct hlist_node *n;
901
902         spin_lock(&blkio_list_lock);
903         spin_lock_irq(&blkcg->lock);
904
905         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
906                 if (pn->dev != blkg->dev || pn->plid != blkg->plid)
907                         continue;
908                 blkio_update_blkg_policy(blkcg, blkg, pn);
909         }
910
911         spin_unlock_irq(&blkcg->lock);
912         spin_unlock(&blkio_list_lock);
913 }
914
915 static int blkiocg_file_write(struct cgroup *cgrp, struct cftype *cft,
916                                        const char *buffer)
917 {
918         int ret = 0;
919         char *buf;
920         struct blkio_policy_node *newpn, *pn;
921         struct blkio_cgroup *blkcg;
922         int keep_newpn = 0;
923         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
924         int fileid = BLKIOFILE_ATTR(cft->private);
925
926         buf = kstrdup(buffer, GFP_KERNEL);
927         if (!buf)
928                 return -ENOMEM;
929
930         newpn = kzalloc(sizeof(*newpn), GFP_KERNEL);
931         if (!newpn) {
932                 ret = -ENOMEM;
933                 goto free_buf;
934         }
935
936         ret = blkio_policy_parse_and_set(buf, newpn, plid, fileid);
937         if (ret)
938                 goto free_newpn;
939
940         blkcg = cgroup_to_blkio_cgroup(cgrp);
941
942         spin_lock_irq(&blkcg->lock);
943
944         pn = blkio_policy_search_node(blkcg, newpn->dev, plid, fileid);
945         if (!pn) {
946                 if (!blkio_delete_rule_command(newpn)) {
947                         blkio_policy_insert_node(blkcg, newpn);
948                         keep_newpn = 1;
949                 }
950                 spin_unlock_irq(&blkcg->lock);
951                 goto update_io_group;
952         }
953
954         if (blkio_delete_rule_command(newpn)) {
955                 blkio_policy_delete_node(pn);
956                 spin_unlock_irq(&blkcg->lock);
957                 goto update_io_group;
958         }
959         spin_unlock_irq(&blkcg->lock);
960
961         blkio_update_policy_rule(pn, newpn);
962
963 update_io_group:
964         blkio_update_policy_node_blkg(blkcg, newpn);
965
966 free_newpn:
967         if (!keep_newpn)
968                 kfree(newpn);
969 free_buf:
970         kfree(buf);
971         return ret;
972 }
973
974 static void
975 blkio_print_policy_node(struct seq_file *m, struct blkio_policy_node *pn)
976 {
977         switch(pn->plid) {
978                 case BLKIO_POLICY_PROP:
979                         if (pn->fileid == BLKIO_PROP_weight_device)
980                                 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
981                                         MINOR(pn->dev), pn->val.weight);
982                         break;
983                 case BLKIO_POLICY_THROTL:
984                         switch(pn->fileid) {
985                         case BLKIO_THROTL_read_bps_device:
986                         case BLKIO_THROTL_write_bps_device:
987                                 seq_printf(m, "%u:%u\t%llu\n", MAJOR(pn->dev),
988                                         MINOR(pn->dev), pn->val.bps);
989                                 break;
990                         case BLKIO_THROTL_read_iops_device:
991                         case BLKIO_THROTL_write_iops_device:
992                                 seq_printf(m, "%u:%u\t%u\n", MAJOR(pn->dev),
993                                         MINOR(pn->dev), pn->val.iops);
994                                 break;
995                         }
996                         break;
997                 default:
998                         BUG();
999         }
1000 }
1001
1002 /* cgroup files which read their data from policy nodes end up here */
1003 static void blkio_read_policy_node_files(struct cftype *cft,
1004                         struct blkio_cgroup *blkcg, struct seq_file *m)
1005 {
1006         struct blkio_policy_node *pn;
1007
1008         if (!list_empty(&blkcg->policy_list)) {
1009                 spin_lock_irq(&blkcg->lock);
1010                 list_for_each_entry(pn, &blkcg->policy_list, node) {
1011                         if (!pn_matches_cftype(cft, pn))
1012                                 continue;
1013                         blkio_print_policy_node(m, pn);
1014                 }
1015                 spin_unlock_irq(&blkcg->lock);
1016         }
1017 }
1018
1019 static int blkiocg_file_read(struct cgroup *cgrp, struct cftype *cft,
1020                                 struct seq_file *m)
1021 {
1022         struct blkio_cgroup *blkcg;
1023         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1024         int name = BLKIOFILE_ATTR(cft->private);
1025
1026         blkcg = cgroup_to_blkio_cgroup(cgrp);
1027
1028         switch(plid) {
1029         case BLKIO_POLICY_PROP:
1030                 switch(name) {
1031                 case BLKIO_PROP_weight_device:
1032                         blkio_read_policy_node_files(cft, blkcg, m);
1033                         return 0;
1034                 default:
1035                         BUG();
1036                 }
1037                 break;
1038         case BLKIO_POLICY_THROTL:
1039                 switch(name){
1040                 case BLKIO_THROTL_read_bps_device:
1041                 case BLKIO_THROTL_write_bps_device:
1042                 case BLKIO_THROTL_read_iops_device:
1043                 case BLKIO_THROTL_write_iops_device:
1044                         blkio_read_policy_node_files(cft, blkcg, m);
1045                         return 0;
1046                 default:
1047                         BUG();
1048                 }
1049                 break;
1050         default:
1051                 BUG();
1052         }
1053
1054         return 0;
1055 }
1056
1057 static int blkio_read_blkg_stats(struct blkio_cgroup *blkcg,
1058                 struct cftype *cft, struct cgroup_map_cb *cb, enum stat_type type,
1059                 bool show_total)
1060 {
1061         struct blkio_group *blkg;
1062         struct hlist_node *n;
1063         uint64_t cgroup_total = 0;
1064
1065         rcu_read_lock();
1066         hlist_for_each_entry_rcu(blkg, n, &blkcg->blkg_list, blkcg_node) {
1067                 if (blkg->dev) {
1068                         if (!cftype_blkg_same_policy(cft, blkg))
1069                                 continue;
1070                         spin_lock_irq(&blkg->stats_lock);
1071                         cgroup_total += blkio_get_stat(blkg, cb, blkg->dev,
1072                                                 type);
1073                         spin_unlock_irq(&blkg->stats_lock);
1074                 }
1075         }
1076         if (show_total)
1077                 cb->fill(cb, "Total", cgroup_total);
1078         rcu_read_unlock();
1079         return 0;
1080 }
1081
1082 /* All map kind of cgroup file get serviced by this function */
1083 static int blkiocg_file_read_map(struct cgroup *cgrp, struct cftype *cft,
1084                                 struct cgroup_map_cb *cb)
1085 {
1086         struct blkio_cgroup *blkcg;
1087         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1088         int name = BLKIOFILE_ATTR(cft->private);
1089
1090         blkcg = cgroup_to_blkio_cgroup(cgrp);
1091
1092         switch(plid) {
1093         case BLKIO_POLICY_PROP:
1094                 switch(name) {
1095                 case BLKIO_PROP_time:
1096                         return blkio_read_blkg_stats(blkcg, cft, cb,
1097                                                 BLKIO_STAT_TIME, 0);
1098                 case BLKIO_PROP_sectors:
1099                         return blkio_read_blkg_stats(blkcg, cft, cb,
1100                                                 BLKIO_STAT_SECTORS, 0);
1101                 case BLKIO_PROP_io_service_bytes:
1102                         return blkio_read_blkg_stats(blkcg, cft, cb,
1103                                                 BLKIO_STAT_SERVICE_BYTES, 1);
1104                 case BLKIO_PROP_io_serviced:
1105                         return blkio_read_blkg_stats(blkcg, cft, cb,
1106                                                 BLKIO_STAT_SERVICED, 1);
1107                 case BLKIO_PROP_io_service_time:
1108                         return blkio_read_blkg_stats(blkcg, cft, cb,
1109                                                 BLKIO_STAT_SERVICE_TIME, 1);
1110                 case BLKIO_PROP_io_wait_time:
1111                         return blkio_read_blkg_stats(blkcg, cft, cb,
1112                                                 BLKIO_STAT_WAIT_TIME, 1);
1113                 case BLKIO_PROP_io_merged:
1114                         return blkio_read_blkg_stats(blkcg, cft, cb,
1115                                                 BLKIO_STAT_MERGED, 1);
1116                 case BLKIO_PROP_io_queued:
1117                         return blkio_read_blkg_stats(blkcg, cft, cb,
1118                                                 BLKIO_STAT_QUEUED, 1);
1119 #ifdef CONFIG_DEBUG_BLK_CGROUP
1120                 case BLKIO_PROP_dequeue:
1121                         return blkio_read_blkg_stats(blkcg, cft, cb,
1122                                                 BLKIO_STAT_DEQUEUE, 0);
1123                 case BLKIO_PROP_avg_queue_size:
1124                         return blkio_read_blkg_stats(blkcg, cft, cb,
1125                                                 BLKIO_STAT_AVG_QUEUE_SIZE, 0);
1126                 case BLKIO_PROP_group_wait_time:
1127                         return blkio_read_blkg_stats(blkcg, cft, cb,
1128                                                 BLKIO_STAT_GROUP_WAIT_TIME, 0);
1129                 case BLKIO_PROP_idle_time:
1130                         return blkio_read_blkg_stats(blkcg, cft, cb,
1131                                                 BLKIO_STAT_IDLE_TIME, 0);
1132                 case BLKIO_PROP_empty_time:
1133                         return blkio_read_blkg_stats(blkcg, cft, cb,
1134                                                 BLKIO_STAT_EMPTY_TIME, 0);
1135 #endif
1136                 default:
1137                         BUG();
1138                 }
1139                 break;
1140         case BLKIO_POLICY_THROTL:
1141                 switch(name){
1142                 case BLKIO_THROTL_io_service_bytes:
1143                         return blkio_read_blkg_stats(blkcg, cft, cb,
1144                                                 BLKIO_STAT_SERVICE_BYTES, 1);
1145                 case BLKIO_THROTL_io_serviced:
1146                         return blkio_read_blkg_stats(blkcg, cft, cb,
1147                                                 BLKIO_STAT_SERVICED, 1);
1148                 default:
1149                         BUG();
1150                 }
1151                 break;
1152         default:
1153                 BUG();
1154         }
1155
1156         return 0;
1157 }
1158
1159 static int blkio_weight_write(struct blkio_cgroup *blkcg, u64 val)
1160 {
1161         struct blkio_group *blkg;
1162         struct hlist_node *n;
1163         struct blkio_policy_node *pn;
1164
1165         if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1166                 return -EINVAL;
1167
1168         spin_lock(&blkio_list_lock);
1169         spin_lock_irq(&blkcg->lock);
1170         blkcg->weight = (unsigned int)val;
1171
1172         hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1173                 pn = blkio_policy_search_node(blkcg, blkg->dev,
1174                                 BLKIO_POLICY_PROP, BLKIO_PROP_weight_device);
1175                 if (pn)
1176                         continue;
1177
1178                 blkio_update_group_weight(blkg, blkcg->weight);
1179         }
1180         spin_unlock_irq(&blkcg->lock);
1181         spin_unlock(&blkio_list_lock);
1182         return 0;
1183 }
1184
1185 static u64 blkiocg_file_read_u64 (struct cgroup *cgrp, struct cftype *cft) {
1186         struct blkio_cgroup *blkcg;
1187         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1188         int name = BLKIOFILE_ATTR(cft->private);
1189
1190         blkcg = cgroup_to_blkio_cgroup(cgrp);
1191
1192         switch(plid) {
1193         case BLKIO_POLICY_PROP:
1194                 switch(name) {
1195                 case BLKIO_PROP_weight:
1196                         return (u64)blkcg->weight;
1197                 }
1198                 break;
1199         default:
1200                 BUG();
1201         }
1202         return 0;
1203 }
1204
1205 static int
1206 blkiocg_file_write_u64(struct cgroup *cgrp, struct cftype *cft, u64 val)
1207 {
1208         struct blkio_cgroup *blkcg;
1209         enum blkio_policy_id plid = BLKIOFILE_POLICY(cft->private);
1210         int name = BLKIOFILE_ATTR(cft->private);
1211
1212         blkcg = cgroup_to_blkio_cgroup(cgrp);
1213
1214         switch(plid) {
1215         case BLKIO_POLICY_PROP:
1216                 switch(name) {
1217                 case BLKIO_PROP_weight:
1218                         return blkio_weight_write(blkcg, val);
1219                 }
1220                 break;
1221         default:
1222                 BUG();
1223         }
1224
1225         return 0;
1226 }
1227
1228 struct cftype blkio_files[] = {
1229         {
1230                 .name = "weight_device",
1231                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1232                                 BLKIO_PROP_weight_device),
1233                 .read_seq_string = blkiocg_file_read,
1234                 .write_string = blkiocg_file_write,
1235                 .max_write_len = 256,
1236         },
1237         {
1238                 .name = "weight",
1239                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1240                                 BLKIO_PROP_weight),
1241                 .read_u64 = blkiocg_file_read_u64,
1242                 .write_u64 = blkiocg_file_write_u64,
1243         },
1244         {
1245                 .name = "time",
1246                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1247                                 BLKIO_PROP_time),
1248                 .read_map = blkiocg_file_read_map,
1249         },
1250         {
1251                 .name = "sectors",
1252                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1253                                 BLKIO_PROP_sectors),
1254                 .read_map = blkiocg_file_read_map,
1255         },
1256         {
1257                 .name = "io_service_bytes",
1258                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1259                                 BLKIO_PROP_io_service_bytes),
1260                 .read_map = blkiocg_file_read_map,
1261         },
1262         {
1263                 .name = "io_serviced",
1264                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1265                                 BLKIO_PROP_io_serviced),
1266                 .read_map = blkiocg_file_read_map,
1267         },
1268         {
1269                 .name = "io_service_time",
1270                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1271                                 BLKIO_PROP_io_service_time),
1272                 .read_map = blkiocg_file_read_map,
1273         },
1274         {
1275                 .name = "io_wait_time",
1276                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1277                                 BLKIO_PROP_io_wait_time),
1278                 .read_map = blkiocg_file_read_map,
1279         },
1280         {
1281                 .name = "io_merged",
1282                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1283                                 BLKIO_PROP_io_merged),
1284                 .read_map = blkiocg_file_read_map,
1285         },
1286         {
1287                 .name = "io_queued",
1288                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1289                                 BLKIO_PROP_io_queued),
1290                 .read_map = blkiocg_file_read_map,
1291         },
1292         {
1293                 .name = "reset_stats",
1294                 .write_u64 = blkiocg_reset_stats,
1295         },
1296 #ifdef CONFIG_BLK_DEV_THROTTLING
1297         {
1298                 .name = "throttle.read_bps_device",
1299                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1300                                 BLKIO_THROTL_read_bps_device),
1301                 .read_seq_string = blkiocg_file_read,
1302                 .write_string = blkiocg_file_write,
1303                 .max_write_len = 256,
1304         },
1305
1306         {
1307                 .name = "throttle.write_bps_device",
1308                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1309                                 BLKIO_THROTL_write_bps_device),
1310                 .read_seq_string = blkiocg_file_read,
1311                 .write_string = blkiocg_file_write,
1312                 .max_write_len = 256,
1313         },
1314
1315         {
1316                 .name = "throttle.read_iops_device",
1317                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1318                                 BLKIO_THROTL_read_iops_device),
1319                 .read_seq_string = blkiocg_file_read,
1320                 .write_string = blkiocg_file_write,
1321                 .max_write_len = 256,
1322         },
1323
1324         {
1325                 .name = "throttle.write_iops_device",
1326                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1327                                 BLKIO_THROTL_write_iops_device),
1328                 .read_seq_string = blkiocg_file_read,
1329                 .write_string = blkiocg_file_write,
1330                 .max_write_len = 256,
1331         },
1332         {
1333                 .name = "throttle.io_service_bytes",
1334                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1335                                 BLKIO_THROTL_io_service_bytes),
1336                 .read_map = blkiocg_file_read_map,
1337         },
1338         {
1339                 .name = "throttle.io_serviced",
1340                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_THROTL,
1341                                 BLKIO_THROTL_io_serviced),
1342                 .read_map = blkiocg_file_read_map,
1343         },
1344 #endif /* CONFIG_BLK_DEV_THROTTLING */
1345
1346 #ifdef CONFIG_DEBUG_BLK_CGROUP
1347         {
1348                 .name = "avg_queue_size",
1349                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1350                                 BLKIO_PROP_avg_queue_size),
1351                 .read_map = blkiocg_file_read_map,
1352         },
1353         {
1354                 .name = "group_wait_time",
1355                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1356                                 BLKIO_PROP_group_wait_time),
1357                 .read_map = blkiocg_file_read_map,
1358         },
1359         {
1360                 .name = "idle_time",
1361                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1362                                 BLKIO_PROP_idle_time),
1363                 .read_map = blkiocg_file_read_map,
1364         },
1365         {
1366                 .name = "empty_time",
1367                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1368                                 BLKIO_PROP_empty_time),
1369                 .read_map = blkiocg_file_read_map,
1370         },
1371         {
1372                 .name = "dequeue",
1373                 .private = BLKIOFILE_PRIVATE(BLKIO_POLICY_PROP,
1374                                 BLKIO_PROP_dequeue),
1375                 .read_map = blkiocg_file_read_map,
1376         },
1377 #endif
1378 };
1379
1380 static int blkiocg_populate(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1381 {
1382         return cgroup_add_files(cgroup, subsys, blkio_files,
1383                                 ARRAY_SIZE(blkio_files));
1384 }
1385
1386 static void blkiocg_destroy(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1387 {
1388         struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
1389         unsigned long flags;
1390         struct blkio_group *blkg;
1391         void *key;
1392         struct blkio_policy_type *blkiop;
1393         struct blkio_policy_node *pn, *pntmp;
1394
1395         rcu_read_lock();
1396         do {
1397                 spin_lock_irqsave(&blkcg->lock, flags);
1398
1399                 if (hlist_empty(&blkcg->blkg_list)) {
1400                         spin_unlock_irqrestore(&blkcg->lock, flags);
1401                         break;
1402                 }
1403
1404                 blkg = hlist_entry(blkcg->blkg_list.first, struct blkio_group,
1405                                         blkcg_node);
1406                 key = rcu_dereference(blkg->key);
1407                 __blkiocg_del_blkio_group(blkg);
1408
1409                 spin_unlock_irqrestore(&blkcg->lock, flags);
1410
1411                 /*
1412                  * This blkio_group is being unlinked as associated cgroup is
1413                  * going away. Let all the IO controlling policies know about
1414                  * this event.
1415                  */
1416                 spin_lock(&blkio_list_lock);
1417                 list_for_each_entry(blkiop, &blkio_list, list) {
1418                         if (blkiop->plid != blkg->plid)
1419                                 continue;
1420                         blkiop->ops.blkio_unlink_group_fn(key, blkg);
1421                 }
1422                 spin_unlock(&blkio_list_lock);
1423         } while (1);
1424
1425         list_for_each_entry_safe(pn, pntmp, &blkcg->policy_list, node) {
1426                 blkio_policy_delete_node(pn);
1427                 kfree(pn);
1428         }
1429
1430         free_css_id(&blkio_subsys, &blkcg->css);
1431         rcu_read_unlock();
1432         if (blkcg != &blkio_root_cgroup)
1433                 kfree(blkcg);
1434 }
1435
1436 static struct cgroup_subsys_state *
1437 blkiocg_create(struct cgroup_subsys *subsys, struct cgroup *cgroup)
1438 {
1439         struct blkio_cgroup *blkcg;
1440         struct cgroup *parent = cgroup->parent;
1441
1442         if (!parent) {
1443                 blkcg = &blkio_root_cgroup;
1444                 goto done;
1445         }
1446
1447         /* Currently we do not support hierarchy deeper than two level (0,1) */
1448         if (parent != cgroup->top_cgroup)
1449                 return ERR_PTR(-EINVAL);
1450
1451         blkcg = kzalloc(sizeof(*blkcg), GFP_KERNEL);
1452         if (!blkcg)
1453                 return ERR_PTR(-ENOMEM);
1454
1455         blkcg->weight = BLKIO_WEIGHT_DEFAULT;
1456 done:
1457         spin_lock_init(&blkcg->lock);
1458         INIT_HLIST_HEAD(&blkcg->blkg_list);
1459
1460         INIT_LIST_HEAD(&blkcg->policy_list);
1461         return &blkcg->css;
1462 }
1463
1464 /*
1465  * We cannot support shared io contexts, as we have no mean to support
1466  * two tasks with the same ioc in two different groups without major rework
1467  * of the main cic data structures.  For now we allow a task to change
1468  * its cgroup only if it's the only owner of its ioc.
1469  */
1470 static int blkiocg_can_attach(struct cgroup_subsys *subsys,
1471                                 struct cgroup *cgroup, struct task_struct *tsk,
1472                                 bool threadgroup)
1473 {
1474         struct io_context *ioc;
1475         int ret = 0;
1476
1477         /* task_lock() is needed to avoid races with exit_io_context() */
1478         task_lock(tsk);
1479         ioc = tsk->io_context;
1480         if (ioc && atomic_read(&ioc->nr_tasks) > 1)
1481                 ret = -EINVAL;
1482         task_unlock(tsk);
1483
1484         return ret;
1485 }
1486
1487 static void blkiocg_attach(struct cgroup_subsys *subsys, struct cgroup *cgroup,
1488                                 struct cgroup *prev, struct task_struct *tsk,
1489                                 bool threadgroup)
1490 {
1491         struct io_context *ioc;
1492
1493         task_lock(tsk);
1494         ioc = tsk->io_context;
1495         if (ioc)
1496                 ioc->cgroup_changed = 1;
1497         task_unlock(tsk);
1498 }
1499
1500 void blkio_policy_register(struct blkio_policy_type *blkiop)
1501 {
1502         spin_lock(&blkio_list_lock);
1503         list_add_tail(&blkiop->list, &blkio_list);
1504         spin_unlock(&blkio_list_lock);
1505 }
1506 EXPORT_SYMBOL_GPL(blkio_policy_register);
1507
1508 void blkio_policy_unregister(struct blkio_policy_type *blkiop)
1509 {
1510         spin_lock(&blkio_list_lock);
1511         list_del_init(&blkiop->list);
1512         spin_unlock(&blkio_list_lock);
1513 }
1514 EXPORT_SYMBOL_GPL(blkio_policy_unregister);
1515
1516 static int __init init_cgroup_blkio(void)
1517 {
1518         return cgroup_load_subsys(&blkio_subsys);
1519 }
1520
1521 static void __exit exit_cgroup_blkio(void)
1522 {
1523         cgroup_unload_subsys(&blkio_subsys);
1524 }
1525
1526 module_init(init_cgroup_blkio);
1527 module_exit(exit_cgroup_blkio);
1528 MODULE_LICENSE("GPL");