Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.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/vs_cvirt.h>
28
29 static int set_task_ioprio(struct task_struct *task, int ioprio)
30 {
31         struct io_context *ioc;
32
33         if (task->uid != current->euid &&
34             task->uid != current->uid && !capable(CAP_SYS_NICE))
35                 return -EPERM;
36
37         task_lock(task);
38
39         task->ioprio = ioprio;
40
41         ioc = task->io_context;
42         if (ioc && ioc->set_ioprio)
43                 ioc->set_ioprio(ioc, ioprio);
44
45         task_unlock(task);
46         return 0;
47 }
48
49 asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
50 {
51         int class = IOPRIO_PRIO_CLASS(ioprio);
52         int data = IOPRIO_PRIO_DATA(ioprio);
53         struct task_struct *p, *g;
54         struct user_struct *user;
55         int ret;
56
57         switch (class) {
58                 case IOPRIO_CLASS_RT:
59                         if (!capable(CAP_SYS_ADMIN))
60                                 return -EPERM;
61                         /* fall through, rt has prio field too */
62                 case IOPRIO_CLASS_BE:
63                         if (data >= IOPRIO_BE_NR || data < 0)
64                                 return -EINVAL;
65
66                         break;
67                 case IOPRIO_CLASS_IDLE:
68                         if (!capable(CAP_SYS_ADMIN))
69                                 return -EPERM;
70                         break;
71                 default:
72                         return -EINVAL;
73         }
74
75         ret = -ESRCH;
76         read_lock_irq(&tasklist_lock);
77         switch (which) {
78                 case IOPRIO_WHO_PROCESS:
79                         if (!who)
80                                 p = current;
81                         else
82                                 p = find_task_by_pid(who);
83                         if (p)
84                                 ret = set_task_ioprio(p, ioprio);
85                         break;
86                 case IOPRIO_WHO_PGRP:
87                         if (!who)
88                                 who = process_group(current);
89                         do_each_task_pid(who, PIDTYPE_PGID, p) {
90                                 ret = set_task_ioprio(p, ioprio);
91                                 if (ret)
92                                         break;
93                         } while_each_task_pid(who, PIDTYPE_PGID, p);
94                         break;
95                 case IOPRIO_WHO_USER:
96                         if (!who)
97                                 user = current->user;
98                         else
99                                 user = find_user(vx_current_xid(), who);
100
101                         if (!user)
102                                 break;
103
104                         do_each_thread(g, p) {
105                                 if (p->uid != who)
106                                         continue;
107                                 ret = set_task_ioprio(p, ioprio);
108                                 if (ret)
109                                         break;
110                         } while_each_thread(g, p);
111
112                         if (who)
113                                 free_uid(user);
114                         break;
115                 default:
116                         ret = -EINVAL;
117         }
118
119         read_unlock_irq(&tasklist_lock);
120         return ret;
121 }
122
123 asmlinkage long sys_ioprio_get(int which, int who)
124 {
125         struct task_struct *g, *p;
126         struct user_struct *user;
127         int ret = -ESRCH;
128
129         read_lock_irq(&tasklist_lock);
130         switch (which) {
131                 case IOPRIO_WHO_PROCESS:
132                         if (!who)
133                                 p = current;
134                         else
135                                 p = find_task_by_pid(who);
136                         if (p)
137                                 ret = p->ioprio;
138                         break;
139                 case IOPRIO_WHO_PGRP:
140                         if (!who)
141                                 who = process_group(current);
142                         do_each_task_pid(who, PIDTYPE_PGID, p) {
143                                 if (ret == -ESRCH)
144                                         ret = p->ioprio;
145                                 else
146                                         ret = ioprio_best(ret, p->ioprio);
147                         } while_each_task_pid(who, PIDTYPE_PGID, p);
148                         break;
149                 case IOPRIO_WHO_USER:
150                         if (!who)
151                                 user = current->user;
152                         else
153                                 user = find_user(vx_current_xid(), who);
154
155                         if (!user)
156                                 break;
157
158                         do_each_thread(g, p) {
159                                 if (p->uid != user->uid)
160                                         continue;
161                                 if (ret == -ESRCH)
162                                         ret = p->ioprio;
163                                 else
164                                         ret = ioprio_best(ret, p->ioprio);
165                         } while_each_thread(g, p);
166
167                         if (who)
168                                 free_uid(user);
169                         break;
170                 default:
171                         ret = -EINVAL;
172         }
173
174         read_unlock_irq(&tasklist_lock);
175         return ret;
176 }
177