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>
27 #include <linux/vs_cvirt.h>
29 static int set_task_ioprio(struct task_struct *task, int ioprio)
31 struct io_context *ioc;
33 if (task->uid != current->euid &&
34 task->uid != current->uid && !capable(CAP_SYS_NICE))
39 task->ioprio = ioprio;
41 ioc = task->io_context;
42 if (ioc && ioc->set_ioprio)
43 ioc->set_ioprio(ioc, ioprio);
49 asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
51 int class = IOPRIO_PRIO_CLASS(ioprio);
52 int data = IOPRIO_PRIO_DATA(ioprio);
53 struct task_struct *p, *g;
54 struct user_struct *user;
59 if (!capable(CAP_SYS_ADMIN))
61 /* fall through, rt has prio field too */
63 if (data >= IOPRIO_BE_NR || data < 0)
67 case IOPRIO_CLASS_IDLE:
68 if (!capable(CAP_SYS_ADMIN))
76 read_lock_irq(&tasklist_lock);
78 case IOPRIO_WHO_PROCESS:
82 p = find_task_by_pid(who);
84 ret = set_task_ioprio(p, ioprio);
88 who = process_group(current);
89 do_each_task_pid(who, PIDTYPE_PGID, p) {
90 ret = set_task_ioprio(p, ioprio);
93 } while_each_task_pid(who, PIDTYPE_PGID, p);
99 user = find_user(vx_current_xid(), who);
104 do_each_thread(g, p) {
107 ret = set_task_ioprio(p, ioprio);
110 } while_each_thread(g, p);
119 read_unlock_irq(&tasklist_lock);
123 asmlinkage long sys_ioprio_get(int which, int who)
125 struct task_struct *g, *p;
126 struct user_struct *user;
129 read_lock_irq(&tasklist_lock);
131 case IOPRIO_WHO_PROCESS:
135 p = find_task_by_pid(who);
139 case IOPRIO_WHO_PGRP:
141 who = process_group(current);
142 do_each_task_pid(who, PIDTYPE_PGID, p) {
146 ret = ioprio_best(ret, p->ioprio);
147 } while_each_task_pid(who, PIDTYPE_PGID, p);
149 case IOPRIO_WHO_USER:
151 user = current->user;
153 user = find_user(vx_current_xid(), who);
158 do_each_thread(g, p) {
159 if (p->uid != user->uid)
164 ret = ioprio_best(ret, p->ioprio);
165 } while_each_thread(g, p);
174 read_unlock_irq(&tasklist_lock);