ioprio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. asmlinkage long sys_ioprio_get(int which, int who)
  113. {
  114. struct task_struct *g, *p;
  115. struct user_struct *user;
  116. int ret = -ESRCH;
  117. read_lock_irq(&tasklist_lock);
  118. switch (which) {
  119. case IOPRIO_WHO_PROCESS:
  120. if (!who)
  121. p = current;
  122. else
  123. p = find_task_by_pid(who);
  124. if (p)
  125. ret = p->ioprio;
  126. break;
  127. case IOPRIO_WHO_PGRP:
  128. if (!who)
  129. who = process_group(current);
  130. do_each_task_pid(who, PIDTYPE_PGID, p) {
  131. if (ret == -ESRCH)
  132. ret = p->ioprio;
  133. else
  134. ret = ioprio_best(ret, p->ioprio);
  135. } while_each_task_pid(who, PIDTYPE_PGID, p);
  136. break;
  137. case IOPRIO_WHO_USER:
  138. if (!who)
  139. user = current->user;
  140. else
  141. user = find_user(who);
  142. if (!user)
  143. break;
  144. do_each_thread(g, p) {
  145. if (p->uid != user->uid)
  146. continue;
  147. if (ret == -ESRCH)
  148. ret = p->ioprio;
  149. else
  150. ret = ioprio_best(ret, p->ioprio);
  151. } while_each_thread(g, p);
  152. if (who)
  153. free_uid(user);
  154. break;
  155. default:
  156. ret = -EINVAL;
  157. }
  158. read_unlock_irq(&tasklist_lock);
  159. return ret;
  160. }