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