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