ioprio.c 3.6 KB

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