]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - block/ioprio.c
btrfs: use new block error code
[thirdparty/kernel/stable.git] / block / 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 */
5a0e3ad6 22#include <linux/gfp.h>
22e2c507 23#include <linux/kernel.h>
afeacc8c 24#include <linux/export.h>
22e2c507 25#include <linux/ioprio.h>
5b825c3a 26#include <linux/cred.h>
22e2c507 27#include <linux/blkdev.h>
16f7e0fe 28#include <linux/capability.h>
8703e8a4 29#include <linux/sched/user.h>
f719ff9b 30#include <linux/sched/task.h>
9abdc4cd 31#include <linux/syscalls.h>
03e68060 32#include <linux/security.h>
b488893a 33#include <linux/pid_namespace.h>
22e2c507 34
b3881f74 35int set_task_ioprio(struct task_struct *task, int ioprio)
22e2c507 36{
03e68060 37 int err;
22e2c507 38 struct io_context *ioc;
c69e8d9c 39 const struct cred *cred = current_cred(), *tcred;
22e2c507 40
c69e8d9c
DH
41 rcu_read_lock();
42 tcred = __task_cred(task);
8e96e3b7
EB
43 if (!uid_eq(tcred->uid, cred->euid) &&
44 !uid_eq(tcred->uid, cred->uid) && !capable(CAP_SYS_NICE)) {
c69e8d9c 45 rcu_read_unlock();
22e2c507 46 return -EPERM;
c69e8d9c
DH
47 }
48 rcu_read_unlock();
22e2c507 49
03e68060
JM
50 err = security_task_setioprio(task, ioprio);
51 if (err)
52 return err;
53
6e736be7
TH
54 ioc = get_task_io_context(task, GFP_ATOMIC, NUMA_NO_NODE);
55 if (ioc) {
2b566fa5 56 ioc->ioprio = ioprio;
11a3122f 57 put_io_context(ioc);
fd0928df 58 }
22e2c507 59
fd0928df 60 return err;
22e2c507 61}
b3881f74 62EXPORT_SYMBOL_GPL(set_task_ioprio);
22e2c507 63
938bb9f5 64SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
22e2c507
JA
65{
66 int class = IOPRIO_PRIO_CLASS(ioprio);
67 int data = IOPRIO_PRIO_DATA(ioprio);
68 struct task_struct *p, *g;
69 struct user_struct *user;
41487c65 70 struct pid *pgrp;
7b44ab97 71 kuid_t uid;
22e2c507
JA
72 int ret;
73
74 switch (class) {
75 case IOPRIO_CLASS_RT:
76 if (!capable(CAP_SYS_ADMIN))
77 return -EPERM;
78 /* fall through, rt has prio field too */
79 case IOPRIO_CLASS_BE:
80 if (data >= IOPRIO_BE_NR || data < 0)
81 return -EINVAL;
82
83 break;
84 case IOPRIO_CLASS_IDLE:
85 break;
8ec680e4
JA
86 case IOPRIO_CLASS_NONE:
87 if (data)
88 return -EINVAL;
89 break;
22e2c507
JA
90 default:
91 return -EINVAL;
92 }
93
94 ret = -ESRCH;
d69b78ba 95 rcu_read_lock();
22e2c507
JA
96 switch (which) {
97 case IOPRIO_WHO_PROCESS:
98 if (!who)
99 p = current;
100 else
228ebcbe 101 p = find_task_by_vpid(who);
22e2c507
JA
102 if (p)
103 ret = set_task_ioprio(p, ioprio);
104 break;
105 case IOPRIO_WHO_PGRP:
106 if (!who)
41487c65
EB
107 pgrp = task_pgrp(current);
108 else
b488893a 109 pgrp = find_vpid(who);
2d70b68d 110 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
22e2c507
JA
111 ret = set_task_ioprio(p, ioprio);
112 if (ret)
113 break;
2d70b68d 114 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
115 break;
116 case IOPRIO_WHO_USER:
7b44ab97
EB
117 uid = make_kuid(current_user_ns(), who);
118 if (!uid_valid(uid))
119 break;
22e2c507 120 if (!who)
86a264ab 121 user = current_user();
22e2c507 122 else
7b44ab97 123 user = find_user(uid);
22e2c507
JA
124
125 if (!user)
126 break;
127
612dafab 128 for_each_process_thread(g, p) {
8639b461
BS
129 if (!uid_eq(task_uid(p), uid) ||
130 !task_pid_vnr(p))
22e2c507
JA
131 continue;
132 ret = set_task_ioprio(p, ioprio);
133 if (ret)
78bd4d48 134 goto free_uid;
612dafab 135 }
78bd4d48 136free_uid:
22e2c507
JA
137 if (who)
138 free_uid(user);
139 break;
140 default:
141 ret = -EINVAL;
142 }
143
d69b78ba 144 rcu_read_unlock();
22e2c507
JA
145 return ret;
146}
147
a1836a42
DQ
148static int get_task_ioprio(struct task_struct *p)
149{
150 int ret;
151
152 ret = security_task_getioprio(p);
153 if (ret)
154 goto out;
fd0928df 155 ret = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, IOPRIO_NORM);
8ba86821 156 task_lock(p);
fd0928df
JA
157 if (p->io_context)
158 ret = p->io_context->ioprio;
8ba86821 159 task_unlock(p);
a1836a42
DQ
160out:
161 return ret;
162}
163
e014ff8d
ON
164int ioprio_best(unsigned short aprio, unsigned short bprio)
165{
ece9c72a
JK
166 if (!ioprio_valid(aprio))
167 aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
168 if (!ioprio_valid(bprio))
169 bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
e014ff8d 170
9a87182c 171 return min(aprio, bprio);
e014ff8d
ON
172}
173
938bb9f5 174SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
22e2c507
JA
175{
176 struct task_struct *g, *p;
177 struct user_struct *user;
41487c65 178 struct pid *pgrp;
7b44ab97 179 kuid_t uid;
22e2c507 180 int ret = -ESRCH;
a1836a42 181 int tmpio;
22e2c507 182
d69b78ba 183 rcu_read_lock();
22e2c507
JA
184 switch (which) {
185 case IOPRIO_WHO_PROCESS:
186 if (!who)
187 p = current;
188 else
228ebcbe 189 p = find_task_by_vpid(who);
22e2c507 190 if (p)
a1836a42 191 ret = get_task_ioprio(p);
22e2c507
JA
192 break;
193 case IOPRIO_WHO_PGRP:
194 if (!who)
41487c65
EB
195 pgrp = task_pgrp(current);
196 else
b488893a 197 pgrp = find_vpid(who);
2d70b68d 198 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
a1836a42
DQ
199 tmpio = get_task_ioprio(p);
200 if (tmpio < 0)
201 continue;
22e2c507 202 if (ret == -ESRCH)
a1836a42 203 ret = tmpio;
22e2c507 204 else
a1836a42 205 ret = ioprio_best(ret, tmpio);
2d70b68d 206 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
22e2c507
JA
207 break;
208 case IOPRIO_WHO_USER:
7b44ab97 209 uid = make_kuid(current_user_ns(), who);
22e2c507 210 if (!who)
86a264ab 211 user = current_user();
22e2c507 212 else
7b44ab97 213 user = find_user(uid);
22e2c507
JA
214
215 if (!user)
216 break;
217
612dafab 218 for_each_process_thread(g, p) {
8639b461
BS
219 if (!uid_eq(task_uid(p), user->uid) ||
220 !task_pid_vnr(p))
22e2c507 221 continue;
a1836a42
DQ
222 tmpio = get_task_ioprio(p);
223 if (tmpio < 0)
224 continue;
22e2c507 225 if (ret == -ESRCH)
a1836a42 226 ret = tmpio;
22e2c507 227 else
a1836a42 228 ret = ioprio_best(ret, tmpio);
612dafab 229 }
22e2c507
JA
230
231 if (who)
232 free_uid(user);
233 break;
234 default:
235 ret = -EINVAL;
236 }
237
d69b78ba 238 rcu_read_unlock();
22e2c507
JA
239 return ret;
240}