ioprio.c 3.6 KB

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