Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1 | /* |
| 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 Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 25 | #include <linux/capability.h> |
Adrian Bunk | 9abdc4c | 2005-11-08 16:57:02 +0100 | [diff] [blame] | 26 | #include <linux/syscalls.h> |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 27 | #include <linux/security.h> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 28 | |
| 29 | static int set_task_ioprio(struct task_struct *task, int ioprio) |
| 30 | { |
James Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 31 | int err; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 32 | 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 Morris | 03e6806 | 2006-06-23 02:03:58 -0700 | [diff] [blame] | 38 | err = security_task_setioprio(task, ioprio); |
| 39 | if (err) |
| 40 | return err; |
| 41 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 42 | 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 Blanchard | cf36680 | 2005-07-07 17:56:13 -0700 | [diff] [blame] | 54 | asmlinkage long sys_ioprio_set(int which, int who, int ioprio) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 55 | { |
| 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 Torvalds | f6fdd7d | 2005-08-20 18:51:29 -0700 | [diff] [blame] | 73 | if (!capable(CAP_SYS_ADMIN)) |
| 74 | return -EPERM; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 75 | 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 Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 128 | static 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; |
| 136 | out: |
| 137 | return ret; |
| 138 | } |
| 139 | |
Anton Blanchard | cf36680 | 2005-07-07 17:56:13 -0700 | [diff] [blame] | 140 | asmlinkage long sys_ioprio_get(int which, int who) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 141 | { |
| 142 | struct task_struct *g, *p; |
| 143 | struct user_struct *user; |
| 144 | int ret = -ESRCH; |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 145 | int tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 146 | |
| 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 Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 155 | ret = get_task_ioprio(p); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 156 | break; |
| 157 | case IOPRIO_WHO_PGRP: |
| 158 | if (!who) |
| 159 | who = process_group(current); |
| 160 | do_each_task_pid(who, PIDTYPE_PGID, p) { |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 161 | tmpio = get_task_ioprio(p); |
| 162 | if (tmpio < 0) |
| 163 | continue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 164 | if (ret == -ESRCH) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 165 | ret = tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 166 | else |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 167 | ret = ioprio_best(ret, tmpio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 168 | } 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 Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 182 | tmpio = get_task_ioprio(p); |
| 183 | if (tmpio < 0) |
| 184 | continue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 185 | if (ret == -ESRCH) |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 186 | ret = tmpio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 187 | else |
David Quigley | a1836a4 | 2006-06-30 01:55:49 -0700 | [diff] [blame^] | 188 | ret = ioprio_best(ret, tmpio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 189 | } 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 | |