]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ioprio.c
jbd2: Submit writes to the journal using WRITE_SYNC
[net-next-2.6.git] / fs / ioprio.c
CommitLineData
22e2c507
JA
1/*
2 * fs/ioprio.c
3 *
0fe23479 4 * Copyright (C) 2004 Jens Axboe <axboe@kernel.dk>
22e2c507
JA
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>
16f7e0fe 25#include <linux/capability.h>
9abdc4cd 26#include <linux/syscalls.h>
03e68060 27#include <linux/security.h>
b488893a 28#include <linux/pid_namespace.h>
22e2c507
JA
29
30static int set_task_ioprio(struct task_struct *task, int ioprio)
31{
03e68060 32 int err;
22e2c507 33 struct io_context *ioc;
c69e8d9c 34 const struct cred *cred = current_cred(), *tcred;
22e2c507 35
c69e8d9c
DH
36 rcu_read_lock();
37 tcred = __task_cred(task);
38 if (tcred->uid != cred->euid &&
39 tcred->uid != cred->uid && !capable(CAP_SYS_NICE)) {
40 rcu_read_unlock();
22e2c507 41 return -EPERM;
c69e8d9c
DH
42 }
43 rcu_read_unlock();
22e2c507 44
03e68060
JM
45 err = security_task_setioprio(task, ioprio);
46 if (err)
47 return err;
48
22e2c507 49 task_lock(task);
fd0928df
JA
50 do {
51 ioc = task->io_context;
52 /* see wmb() in current_io_context() */
53 smp_read_barrier_depends();
54 if (ioc)
55 break;
22e2c507 56
fd0928df
JA
57 ioc = alloc_io_context(GFP_ATOMIC, -1);
58 if (!ioc) {
59 err = -ENOMEM;
60 break;
61 }
62 task->io_context = ioc;
fd0928df 63 } while (1);
9f83e45e 64
fd0928df
JA
65 if (!err) {
66 ioc->ioprio = ioprio;
fc46379d 67 ioc->ioprio_changed = 1;
fd0928df 68 }
22e2c507
JA
69
70 task_unlock(task);
fd0928df 71 return err;
22e2c507
JA
72}
73
cf366808 74asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
22e2c507
JA
75{
76 int class = IOPRIO_PRIO_CLASS(ioprio);
77 int data = IOPRIO_PRIO_DATA(ioprio);
78 struct task_struct *p, *g;
79 struct user_struct *user;
41487c65 80 struct pid *pgrp;
22e2c507
JA
81 int ret;
82
83 switch (class) {
84 case IOPRIO_CLASS_RT:
85 if (!capable(CAP_SYS_ADMIN))
86 return -EPERM;
87 /* fall through, rt has prio field too */
88 case IOPRIO_CLASS_BE:
89 if (data >= IOPRIO_BE_NR || data < 0)
90 return -EINVAL;
91
92 break;
93 case IOPRIO_CLASS_IDLE:
94 break;
8ec680e4
JA
95 case IOPRIO_CLASS_NONE:
96 if (data)
97 return -EINVAL;
98 break;
22e2c507
JA
99 default:
100 return -EINVAL;
101 }
102
103 ret = -ESRCH;
cf342e52
ON
104 /*
105 * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
106 * so we can't use rcu_read_lock(). See re-copy of ->ioprio
107 * in copy_process().
108 */
109 read_lock(&tasklist_lock);
22e2c507
JA
110 switch (which) {
111 case IOPRIO_WHO_PROCESS:
112 if (!who)
113 p = current;
114 else
228ebcbe 115 p = find_task_by_vpid(who);
22e2c507
JA
116 if (p)
117 ret = set_task_ioprio(p, ioprio);
118 break;
119 case IOPRIO_WHO_PGRP:
120 if (!who)
41487c65
EB
121 pgrp = task_pgrp(current);
122 else
b488893a 123 pgrp = find_vpid(who);
2d70b68d 124 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507
JA
125 ret = set_task_ioprio(p, ioprio);
126 if (ret)
127 break;
2d70b68d 128 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
129 break;
130 case IOPRIO_WHO_USER:
131 if (!who)
86a264ab 132 user = current_user();
22e2c507
JA
133 else
134 user = find_user(who);
135
136 if (!user)
137 break;
138
139 do_each_thread(g, p) {
c69e8d9c 140 if (__task_cred(p)->uid != who)
22e2c507
JA
141 continue;
142 ret = set_task_ioprio(p, ioprio);
143 if (ret)
78bd4d48 144 goto free_uid;
22e2c507 145 } while_each_thread(g, p);
78bd4d48 146free_uid:
22e2c507
JA
147 if (who)
148 free_uid(user);
149 break;
150 default:
151 ret = -EINVAL;
152 }
153
cf342e52 154 read_unlock(&tasklist_lock);
22e2c507
JA
155 return ret;
156}
157
a1836a42
DQ
158static int get_task_ioprio(struct task_struct *p)
159{
160 int ret;
161
162 ret = security_task_getioprio(p);
163 if (ret)
164 goto out;
fd0928df
JA
165 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
166 if (p->io_context)
167 ret = p->io_context->ioprio;
a1836a42
DQ
168out:
169 return ret;
170}
171
e014ff8d
ON
172int ioprio_best(unsigned short aprio, unsigned short bprio)
173{
174 unsigned short aclass = IOPRIO_PRIO_CLASS(aprio);
175 unsigned short bclass = IOPRIO_PRIO_CLASS(bprio);
176
e014ff8d
ON
177 if (aclass == IOPRIO_CLASS_NONE)
178 aclass = IOPRIO_CLASS_BE;
179 if (bclass == IOPRIO_CLASS_NONE)
180 bclass = IOPRIO_CLASS_BE;
181
182 if (aclass == bclass)
183 return min(aprio, bprio);
184 if (aclass > bclass)
185 return bprio;
186 else
187 return aprio;
188}
189
cf366808 190asmlinkage long sys_ioprio_get(int which, int who)
22e2c507
JA
191{
192 struct task_struct *g, *p;
193 struct user_struct *user;
41487c65 194 struct pid *pgrp;
22e2c507 195 int ret = -ESRCH;
a1836a42 196 int tmpio;
22e2c507 197
cf342e52 198 read_lock(&tasklist_lock);
22e2c507
JA
199 switch (which) {
200 case IOPRIO_WHO_PROCESS:
201 if (!who)
202 p = current;
203 else
228ebcbe 204 p = find_task_by_vpid(who);
22e2c507 205 if (p)
a1836a42 206 ret = get_task_ioprio(p);
22e2c507
JA
207 break;
208 case IOPRIO_WHO_PGRP:
209 if (!who)
41487c65
EB
210 pgrp = task_pgrp(current);
211 else
b488893a 212 pgrp = find_vpid(who);
2d70b68d 213 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
a1836a42
DQ
214 tmpio = get_task_ioprio(p);
215 if (tmpio < 0)
216 continue;
22e2c507 217 if (ret == -ESRCH)
a1836a42 218 ret = tmpio;
22e2c507 219 else
a1836a42 220 ret = ioprio_best(ret, tmpio);
2d70b68d 221 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
222 break;
223 case IOPRIO_WHO_USER:
224 if (!who)
86a264ab 225 user = current_user();
22e2c507
JA
226 else
227 user = find_user(who);
228
229 if (!user)
230 break;
231
232 do_each_thread(g, p) {
c69e8d9c 233 if (__task_cred(p)->uid != user->uid)
22e2c507 234 continue;
a1836a42
DQ
235 tmpio = get_task_ioprio(p);
236 if (tmpio < 0)
237 continue;
22e2c507 238 if (ret == -ESRCH)
a1836a42 239 ret = tmpio;
22e2c507 240 else
a1836a42 241 ret = ioprio_best(ret, tmpio);
22e2c507
JA
242 } while_each_thread(g, p);
243
244 if (who)
245 free_uid(user);
246 break;
247 default:
248 ret = -EINVAL;
249 }
250
cf342e52 251 read_unlock(&tasklist_lock);
22e2c507
JA
252 return ret;
253}
254