blob: 93aa5715f224cd5163e070473dc04e6d76409667 [file] [log] [blame]
Jens Axboe22e2c502005-06-27 10:55:12 +02001/*
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>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080025#include <linux/capability.h>
Adrian Bunk9abdc4c2005-11-08 16:57:02 +010026#include <linux/syscalls.h>
James Morris03e68062006-06-23 02:03:58 -070027#include <linux/security.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020028
29static int set_task_ioprio(struct task_struct *task, int ioprio)
30{
James Morris03e68062006-06-23 02:03:58 -070031 int err;
Jens Axboe22e2c502005-06-27 10:55:12 +020032 struct io_context *ioc;
33
34 if (task->uid != current->euid &&
35 task->uid != current->uid && !capable(CAP_SYS_NICE))
36 return -EPERM;
37
James Morris03e68062006-06-23 02:03:58 -070038 err = security_task_setioprio(task, ioprio);
39 if (err)
40 return err;
41
Jens Axboe22e2c502005-06-27 10:55:12 +020042 task_lock(task);
43
44 task->ioprio = ioprio;
45
46 ioc = task->io_context;
47 if (ioc && ioc->set_ioprio)
48 ioc->set_ioprio(ioc, ioprio);
49
50 task_unlock(task);
51 return 0;
52}
53
Anton Blanchardcf366802005-07-07 17:56:13 -070054asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
Jens Axboe22e2c502005-06-27 10:55:12 +020055{
56 int class = IOPRIO_PRIO_CLASS(ioprio);
57 int data = IOPRIO_PRIO_DATA(ioprio);
58 struct task_struct *p, *g;
59 struct user_struct *user;
60 int ret;
61
62 switch (class) {
63 case IOPRIO_CLASS_RT:
64 if (!capable(CAP_SYS_ADMIN))
65 return -EPERM;
66 /* fall through, rt has prio field too */
67 case IOPRIO_CLASS_BE:
68 if (data >= IOPRIO_BE_NR || data < 0)
69 return -EINVAL;
70
71 break;
72 case IOPRIO_CLASS_IDLE:
Linus Torvaldsf6fdd7d2005-08-20 18:51:29 -070073 if (!capable(CAP_SYS_ADMIN))
74 return -EPERM;
Jens Axboe22e2c502005-06-27 10:55:12 +020075 break;
76 default:
77 return -EINVAL;
78 }
79
80 ret = -ESRCH;
81 read_lock_irq(&tasklist_lock);
82 switch (which) {
83 case IOPRIO_WHO_PROCESS:
84 if (!who)
85 p = current;
86 else
87 p = find_task_by_pid(who);
88 if (p)
89 ret = set_task_ioprio(p, ioprio);
90 break;
91 case IOPRIO_WHO_PGRP:
92 if (!who)
93 who = process_group(current);
94 do_each_task_pid(who, PIDTYPE_PGID, p) {
95 ret = set_task_ioprio(p, ioprio);
96 if (ret)
97 break;
98 } while_each_task_pid(who, PIDTYPE_PGID, p);
99 break;
100 case IOPRIO_WHO_USER:
101 if (!who)
102 user = current->user;
103 else
104 user = find_user(who);
105
106 if (!user)
107 break;
108
109 do_each_thread(g, p) {
110 if (p->uid != who)
111 continue;
112 ret = set_task_ioprio(p, ioprio);
113 if (ret)
114 break;
115 } while_each_thread(g, p);
116
117 if (who)
118 free_uid(user);
119 break;
120 default:
121 ret = -EINVAL;
122 }
123
124 read_unlock_irq(&tasklist_lock);
125 return ret;
126}
127
David Quigleya1836a42006-06-30 01:55:49 -0700128static int get_task_ioprio(struct task_struct *p)
129{
130 int ret;
131
132 ret = security_task_getioprio(p);
133 if (ret)
134 goto out;
135 ret = p->ioprio;
136out:
137 return ret;
138}
139
Anton Blanchardcf366802005-07-07 17:56:13 -0700140asmlinkage long sys_ioprio_get(int which, int who)
Jens Axboe22e2c502005-06-27 10:55:12 +0200141{
142 struct task_struct *g, *p;
143 struct user_struct *user;
144 int ret = -ESRCH;
David Quigleya1836a42006-06-30 01:55:49 -0700145 int tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200146
147 read_lock_irq(&tasklist_lock);
148 switch (which) {
149 case IOPRIO_WHO_PROCESS:
150 if (!who)
151 p = current;
152 else
153 p = find_task_by_pid(who);
154 if (p)
David Quigleya1836a42006-06-30 01:55:49 -0700155 ret = get_task_ioprio(p);
Jens Axboe22e2c502005-06-27 10:55:12 +0200156 break;
157 case IOPRIO_WHO_PGRP:
158 if (!who)
159 who = process_group(current);
160 do_each_task_pid(who, PIDTYPE_PGID, p) {
David Quigleya1836a42006-06-30 01:55:49 -0700161 tmpio = get_task_ioprio(p);
162 if (tmpio < 0)
163 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200164 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700165 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200166 else
David Quigleya1836a42006-06-30 01:55:49 -0700167 ret = ioprio_best(ret, tmpio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200168 } while_each_task_pid(who, PIDTYPE_PGID, p);
169 break;
170 case IOPRIO_WHO_USER:
171 if (!who)
172 user = current->user;
173 else
174 user = find_user(who);
175
176 if (!user)
177 break;
178
179 do_each_thread(g, p) {
180 if (p->uid != user->uid)
181 continue;
David Quigleya1836a42006-06-30 01:55:49 -0700182 tmpio = get_task_ioprio(p);
183 if (tmpio < 0)
184 continue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200185 if (ret == -ESRCH)
David Quigleya1836a42006-06-30 01:55:49 -0700186 ret = tmpio;
Jens Axboe22e2c502005-06-27 10:55:12 +0200187 else
David Quigleya1836a42006-06-30 01:55:49 -0700188 ret = ioprio_best(ret, tmpio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200189 } while_each_thread(g, p);
190
191 if (who)
192 free_uid(user);
193 break;
194 default:
195 ret = -EINVAL;
196 }
197
198 read_unlock_irq(&tasklist_lock);
199 return ret;
200}
201