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 | |
Anton Blanchard | cf36680 | 2005-07-07 17:56:13 -0700 | [diff] [blame] | 128 | asmlinkage long sys_ioprio_get(int which, int who) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 129 | { |
| 130 | struct task_struct *g, *p; |
| 131 | struct user_struct *user; |
| 132 | int ret = -ESRCH; |
| 133 | |
| 134 | read_lock_irq(&tasklist_lock); |
| 135 | switch (which) { |
| 136 | case IOPRIO_WHO_PROCESS: |
| 137 | if (!who) |
| 138 | p = current; |
| 139 | else |
| 140 | p = find_task_by_pid(who); |
| 141 | if (p) |
| 142 | ret = p->ioprio; |
| 143 | break; |
| 144 | case IOPRIO_WHO_PGRP: |
| 145 | if (!who) |
| 146 | who = process_group(current); |
| 147 | do_each_task_pid(who, PIDTYPE_PGID, p) { |
| 148 | if (ret == -ESRCH) |
| 149 | ret = p->ioprio; |
| 150 | else |
| 151 | ret = ioprio_best(ret, p->ioprio); |
| 152 | } while_each_task_pid(who, PIDTYPE_PGID, p); |
| 153 | break; |
| 154 | case IOPRIO_WHO_USER: |
| 155 | if (!who) |
| 156 | user = current->user; |
| 157 | else |
| 158 | user = find_user(who); |
| 159 | |
| 160 | if (!user) |
| 161 | break; |
| 162 | |
| 163 | do_each_thread(g, p) { |
| 164 | if (p->uid != user->uid) |
| 165 | continue; |
| 166 | if (ret == -ESRCH) |
| 167 | ret = p->ioprio; |
| 168 | else |
| 169 | ret = ioprio_best(ret, p->ioprio); |
| 170 | } while_each_thread(g, p); |
| 171 | |
| 172 | if (who) |
| 173 | free_uid(user); |
| 174 | break; |
| 175 | default: |
| 176 | ret = -EINVAL; |
| 177 | } |
| 178 | |
| 179 | read_unlock_irq(&tasklist_lock); |
| 180 | return ret; |
| 181 | } |
| 182 | |