Merge branch 'upstream-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[pandora-kernel.git] / fs / ioprio.c
1 /*
2  * fs/ioprio.c
3  *
4  * Copyright (C) 2004 Jens Axboe <axboe@suse.de>
5  *
6  * Helper functions for setting/querying io priorities of processes. The
7  * system calls closely mimmick getpriority/setpriority, see the man page for
8  * those. The prio argument is a composite of prio class and prio data, where
9  * the data argument has meaning within that class. The standard scheduling
10  * classes have 8 distinct prio levels, with 0 being the highest prio and 7
11  * being the lowest.
12  *
13  * IOW, setting BE scheduling class with prio 2 is done ala:
14  *
15  * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
16  *
17  * ioprio_set(PRIO_PROCESS, pid, prio);
18  *
19  * See also Documentation/block/ioprio.txt
20  *
21  */
22 #include <linux/kernel.h>
23 #include <linux/ioprio.h>
24 #include <linux/blkdev.h>
25 #include <linux/capability.h>
26 #include <linux/syscalls.h>
27 #include <linux/security.h>
28
29 static int set_task_ioprio(struct task_struct *task, int ioprio)
30 {
31         int err;
32         struct io_context *ioc;
33
34         if (task->uid != current->euid &&
35             task->uid != current->uid && !capable(CAP_SYS_NICE))
36                 return -EPERM;
37
38         err = security_task_setioprio(task, ioprio);
39         if (err)
40                 return err;
41
42         task_lock(task);
43
44         task->ioprio = ioprio;
45
46         ioc = task->io_context;
47         if (ioc && ioc->set_ioprio)
48                 ioc->set_ioprio(ioc, ioprio);
49
50         task_unlock(task);
51         return 0;
52 }
53
54 asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
55 {
56         int class = IOPRIO_PRIO_CLASS(ioprio);
57         int data = IOPRIO_PRIO_DATA(ioprio);
58         struct task_struct *p, *g;
59         struct user_struct *user;
60         int ret;
61
62         switch (class) {
63                 case IOPRIO_CLASS_RT:
64                         if (!capable(CAP_SYS_ADMIN))
65                                 return -EPERM;
66                         /* fall through, rt has prio field too */
67                 case IOPRIO_CLASS_BE:
68                         if (data >= IOPRIO_BE_NR || data < 0)
69                                 return -EINVAL;
70
71                         break;
72                 case IOPRIO_CLASS_IDLE:
73                         if (!capable(CAP_SYS_ADMIN))
74                                 return -EPERM;
75                         break;
76                 default:
77                         return -EINVAL;
78         }
79
80         ret = -ESRCH;
81         read_lock_irq(&tasklist_lock);
82         switch (which) {
83                 case IOPRIO_WHO_PROCESS:
84                         if (!who)
85                                 p = current;
86                         else
87                                 p = find_task_by_pid(who);
88                         if (p)
89                                 ret = set_task_ioprio(p, ioprio);
90                         break;
91                 case IOPRIO_WHO_PGRP:
92                         if (!who)
93                                 who = process_group(current);
94                         do_each_task_pid(who, PIDTYPE_PGID, p) {
95                                 ret = set_task_ioprio(p, ioprio);
96                                 if (ret)
97                                         break;
98                         } while_each_task_pid(who, PIDTYPE_PGID, p);
99                         break;
100                 case IOPRIO_WHO_USER:
101                         if (!who)
102                                 user = current->user;
103                         else
104                                 user = find_user(who);
105
106                         if (!user)
107                                 break;
108
109                         do_each_thread(g, p) {
110                                 if (p->uid != who)
111                                         continue;
112                                 ret = set_task_ioprio(p, ioprio);
113                                 if (ret)
114                                         break;
115                         } while_each_thread(g, p);
116
117                         if (who)
118                                 free_uid(user);
119                         break;
120                 default:
121                         ret = -EINVAL;
122         }
123
124         read_unlock_irq(&tasklist_lock);
125         return ret;
126 }
127
128 static int get_task_ioprio(struct task_struct *p)
129 {
130         int ret;
131
132         ret = security_task_getioprio(p);
133         if (ret)
134                 goto out;
135         ret = p->ioprio;
136 out:
137         return ret;
138 }
139
140 asmlinkage long sys_ioprio_get(int which, int who)
141 {
142         struct task_struct *g, *p;
143         struct user_struct *user;
144         int ret = -ESRCH;
145         int tmpio;
146
147         read_lock_irq(&tasklist_lock);
148         switch (which) {
149                 case IOPRIO_WHO_PROCESS:
150                         if (!who)
151                                 p = current;
152                         else
153                                 p = find_task_by_pid(who);
154                         if (p)
155                                 ret = get_task_ioprio(p);
156                         break;
157                 case IOPRIO_WHO_PGRP:
158                         if (!who)
159                                 who = process_group(current);
160                         do_each_task_pid(who, PIDTYPE_PGID, p) {
161                                 tmpio = get_task_ioprio(p);
162                                 if (tmpio < 0)
163                                         continue;
164                                 if (ret == -ESRCH)
165                                         ret = tmpio;
166                                 else
167                                         ret = ioprio_best(ret, tmpio);
168                         } while_each_task_pid(who, PIDTYPE_PGID, p);
169                         break;
170                 case IOPRIO_WHO_USER:
171                         if (!who)
172                                 user = current->user;
173                         else
174                                 user = find_user(who);
175
176                         if (!user)
177                                 break;
178
179                         do_each_thread(g, p) {
180                                 if (p->uid != user->uid)
181                                         continue;
182                                 tmpio = get_task_ioprio(p);
183                                 if (tmpio < 0)
184                                         continue;
185                                 if (ret == -ESRCH)
186                                         ret = tmpio;
187                                 else
188                                         ret = ioprio_best(ret, tmpio);
189                         } while_each_thread(g, p);
190
191                         if (who)
192                                 free_uid(user);
193                         break;
194                 default:
195                         ret = -EINVAL;
196         }
197
198         read_unlock_irq(&tasklist_lock);
199         return ret;
200 }
201