ioprio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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>
  25. #include <linux/capability.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/security.h>
  28. static int set_task_ioprio(struct task_struct *task, int ioprio)
  29. {
  30. int err;
  31. struct io_context *ioc;
  32. if (task->uid != current->euid &&
  33. task->uid != current->uid && !capable(CAP_SYS_NICE))
  34. return -EPERM;
  35. err = security_task_setioprio(task, ioprio);
  36. if (err)
  37. return err;
  38. task_lock(task);
  39. task->ioprio = ioprio;
  40. ioc = task->io_context;
  41. if (ioc && ioc->set_ioprio)
  42. ioc->set_ioprio(ioc, ioprio);
  43. task_unlock(task);
  44. return 0;
  45. }
  46. asmlinkage long sys_ioprio_set(int which, int who, int ioprio)
  47. {
  48. int class = IOPRIO_PRIO_CLASS(ioprio);
  49. int data = IOPRIO_PRIO_DATA(ioprio);
  50. struct task_struct *p, *g;
  51. struct user_struct *user;
  52. int ret;
  53. switch (class) {
  54. case IOPRIO_CLASS_RT:
  55. if (!capable(CAP_SYS_ADMIN))
  56. return -EPERM;
  57. /* fall through, rt has prio field too */
  58. case IOPRIO_CLASS_BE:
  59. if (data >= IOPRIO_BE_NR || data < 0)
  60. return -EINVAL;
  61. break;
  62. case IOPRIO_CLASS_IDLE:
  63. if (!capable(CAP_SYS_ADMIN))
  64. return -EPERM;
  65. break;
  66. default:
  67. return -EINVAL;
  68. }
  69. ret = -ESRCH;
  70. read_lock_irq(&tasklist_lock);
  71. switch (which) {
  72. case IOPRIO_WHO_PROCESS:
  73. if (!who)
  74. p = current;
  75. else
  76. p = find_task_by_pid(who);
  77. if (p)
  78. ret = set_task_ioprio(p, ioprio);
  79. break;
  80. case IOPRIO_WHO_PGRP:
  81. if (!who)
  82. who = process_group(current);
  83. do_each_task_pid(who, PIDTYPE_PGID, p) {
  84. ret = set_task_ioprio(p, ioprio);
  85. if (ret)
  86. break;
  87. } while_each_task_pid(who, PIDTYPE_PGID, p);
  88. break;
  89. case IOPRIO_WHO_USER:
  90. if (!who)
  91. user = current->user;
  92. else
  93. user = find_user(who);
  94. if (!user)
  95. break;
  96. do_each_thread(g, p) {
  97. if (p->uid != who)
  98. continue;
  99. ret = set_task_ioprio(p, ioprio);
  100. if (ret)
  101. break;
  102. } while_each_thread(g, p);
  103. if (who)
  104. free_uid(user);
  105. break;
  106. default:
  107. ret = -EINVAL;
  108. }
  109. read_unlock_irq(&tasklist_lock);
  110. return ret;
  111. }
  112. static int get_task_ioprio(struct task_struct *p)
  113. {
  114. int ret;
  115. ret = security_task_getioprio(p);
  116. if (ret)
  117. goto out;
  118. ret = p->ioprio;
  119. out:
  120. return ret;
  121. }
  122. asmlinkage long sys_ioprio_get(int which, int who)
  123. {
  124. struct task_struct *g, *p;
  125. struct user_struct *user;
  126. int ret = -ESRCH;
  127. int tmpio;
  128. read_lock_irq(&tasklist_lock);
  129. switch (which) {
  130. case IOPRIO_WHO_PROCESS:
  131. if (!who)
  132. p = current;
  133. else
  134. p = find_task_by_pid(who);
  135. if (p)
  136. ret = get_task_ioprio(p);
  137. break;
  138. case IOPRIO_WHO_PGRP:
  139. if (!who)
  140. who = process_group(current);
  141. do_each_task_pid(who, PIDTYPE_PGID, p) {
  142. tmpio = get_task_ioprio(p);
  143. if (tmpio < 0)
  144. continue;
  145. if (ret == -ESRCH)
  146. ret = tmpio;
  147. else
  148. ret = ioprio_best(ret, tmpio);
  149. } while_each_task_pid(who, PIDTYPE_PGID, p);
  150. break;
  151. case IOPRIO_WHO_USER:
  152. if (!who)
  153. user = current->user;
  154. else
  155. user = find_user(who);
  156. if (!user)
  157. break;
  158. do_each_thread(g, p) {
  159. if (p->uid != user->uid)
  160. continue;
  161. tmpio = get_task_ioprio(p);
  162. if (tmpio < 0)
  163. continue;
  164. if (ret == -ESRCH)
  165. ret = tmpio;
  166. else
  167. ret = ioprio_best(ret, tmpio);
  168. } while_each_thread(g, p);
  169. if (who)
  170. free_uid(user);
  171. break;
  172. default:
  173. ret = -EINVAL;
  174. }
  175. read_unlock_irq(&tasklist_lock);
  176. return ret;
  177. }