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/syscalls.h>
27 static int set_task_ioprio(struct task_struct
*task
, int ioprio
)
29 struct io_context
*ioc
;
31 if (task
->uid
!= current
->euid
&&
32 task
->uid
!= current
->uid
&& !capable(CAP_SYS_NICE
))
37 task
->ioprio
= ioprio
;
39 ioc
= task
->io_context
;
40 if (ioc
&& ioc
->set_ioprio
)
41 ioc
->set_ioprio(ioc
, ioprio
);
47 asmlinkage
long sys_ioprio_set(int which
, int who
, int ioprio
)
49 int class = IOPRIO_PRIO_CLASS(ioprio
);
50 int data
= IOPRIO_PRIO_DATA(ioprio
);
51 struct task_struct
*p
, *g
;
52 struct user_struct
*user
;
57 if (!capable(CAP_SYS_ADMIN
))
59 /* fall through, rt has prio field too */
61 if (data
>= IOPRIO_BE_NR
|| data
< 0)
65 case IOPRIO_CLASS_IDLE
:
66 if (!capable(CAP_SYS_ADMIN
))
74 read_lock_irq(&tasklist_lock
);
76 case IOPRIO_WHO_PROCESS
:
80 p
= find_task_by_pid(who
);
82 ret
= set_task_ioprio(p
, ioprio
);
86 who
= process_group(current
);
87 do_each_task_pid(who
, PIDTYPE_PGID
, p
) {
88 ret
= set_task_ioprio(p
, ioprio
);
91 } while_each_task_pid(who
, PIDTYPE_PGID
, p
);
97 user
= find_user(who
);
102 do_each_thread(g
, p
) {
105 ret
= set_task_ioprio(p
, ioprio
);
108 } while_each_thread(g
, p
);
117 read_unlock_irq(&tasklist_lock
);
121 asmlinkage
long sys_ioprio_get(int which
, int who
)
123 struct task_struct
*g
, *p
;
124 struct user_struct
*user
;
127 read_lock_irq(&tasklist_lock
);
129 case IOPRIO_WHO_PROCESS
:
133 p
= find_task_by_pid(who
);
137 case IOPRIO_WHO_PGRP
:
139 who
= process_group(current
);
140 do_each_task_pid(who
, PIDTYPE_PGID
, p
) {
144 ret
= ioprio_best(ret
, p
->ioprio
);
145 } while_each_task_pid(who
, PIDTYPE_PGID
, p
);
147 case IOPRIO_WHO_USER
:
149 user
= current
->user
;
151 user
= find_user(who
);
156 do_each_thread(g
, p
) {
157 if (p
->uid
!= user
->uid
)
162 ret
= ioprio_best(ret
, p
->ioprio
);
163 } while_each_thread(g
, p
);
172 read_unlock_irq(&tasklist_lock
);