ioprio.c 3.5 KB

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