4 * Copyright (C) 2004 Jens Axboe <axboe@suse.de>
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
13 * IOW, setting BE scheduling class with prio 2 is done ala:
15 * unsigned int prio = (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | 2;
17 * ioprio_set(PRIO_PROCESS, pid, prio);
19 * See also Documentation/block/ioprio.txt
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>
28 static int set_task_ioprio(struct task_struct *task, int ioprio)
30 struct io_context *ioc;
32 if (task->uid != current->euid &&
33 task->uid != current->uid && !capable(CAP_SYS_NICE))
38 task->ioprio = ioprio;
40 ioc = task->io_context;
41 if (ioc && ioc->set_ioprio)
42 ioc->set_ioprio(ioc, ioprio);
48 asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
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;
58 if (!capable(CAP_SYS_ADMIN))
60 /* fall through, rt has prio field too */
62 if (data >= IOPRIO_BE_NR || data < 0)
66 case IOPRIO_CLASS_IDLE:
67 if (!capable(CAP_SYS_ADMIN))
75 read_lock_irq(&tasklist_lock);
77 case IOPRIO_WHO_PROCESS:
81 p = find_task_by_pid(who);
83 ret = set_task_ioprio(p, ioprio);
87 who = process_group(current);
88 do_each_task_pid(who, PIDTYPE_PGID, p) {
89 ret = set_task_ioprio(p, ioprio);
92 } while_each_task_pid(who, PIDTYPE_PGID, p);
98 user = find_user(who);
103 do_each_thread(g, p) {
106 ret = set_task_ioprio(p, ioprio);
109 } while_each_thread(g, p);
118 read_unlock_irq(&tasklist_lock);
122 asmlinkage long sys_ioprio_get(int which, int who)
124 struct task_struct *g, *p;
125 struct user_struct *user;
128 read_lock_irq(&tasklist_lock);
130 case IOPRIO_WHO_PROCESS:
134 p = find_task_by_pid(who);
138 case IOPRIO_WHO_PGRP:
140 who = process_group(current);
141 do_each_task_pid(who, PIDTYPE_PGID, p) {
145 ret = ioprio_best(ret, p->ioprio);
146 } while_each_task_pid(who, PIDTYPE_PGID, p);
148 case IOPRIO_WHO_USER:
150 user = current->user;
152 user = find_user(who);
157 do_each_thread(g, p) {
158 if (p->uid != user->uid)
163 ret = ioprio_best(ret, p->ioprio);
164 } while_each_thread(g, p);
173 read_unlock_irq(&tasklist_lock);