ioprio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 int 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. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. ret = -ESRCH;
  61. read_lock_irq(&tasklist_lock);
  62. switch (which) {
  63. case IOPRIO_WHO_PROCESS:
  64. if (!who)
  65. p = current;
  66. else
  67. p = find_task_by_pid(who);
  68. if (p)
  69. ret = set_task_ioprio(p, ioprio);
  70. break;
  71. case IOPRIO_WHO_PGRP:
  72. if (!who)
  73. who = process_group(current);
  74. do_each_task_pid(who, PIDTYPE_PGID, p) {
  75. ret = set_task_ioprio(p, ioprio);
  76. if (ret)
  77. break;
  78. } while_each_task_pid(who, PIDTYPE_PGID, p);
  79. break;
  80. case IOPRIO_WHO_USER:
  81. if (!who)
  82. user = current->user;
  83. else
  84. user = find_user(who);
  85. if (!user)
  86. break;
  87. do_each_thread(g, p) {
  88. if (p->uid != who)
  89. continue;
  90. ret = set_task_ioprio(p, ioprio);
  91. if (ret)
  92. break;
  93. } while_each_thread(g, p);
  94. if (who)
  95. free_uid(user);
  96. break;
  97. default:
  98. ret = -EINVAL;
  99. }
  100. read_unlock_irq(&tasklist_lock);
  101. return ret;
  102. }
  103. asmlinkage int sys_ioprio_get(int which, int who)
  104. {
  105. struct task_struct *g, *p;
  106. struct user_struct *user;
  107. int ret = -ESRCH;
  108. read_lock_irq(&tasklist_lock);
  109. switch (which) {
  110. case IOPRIO_WHO_PROCESS:
  111. if (!who)
  112. p = current;
  113. else
  114. p = find_task_by_pid(who);
  115. if (p)
  116. ret = p->ioprio;
  117. break;
  118. case IOPRIO_WHO_PGRP:
  119. if (!who)
  120. who = process_group(current);
  121. do_each_task_pid(who, PIDTYPE_PGID, p) {
  122. if (ret == -ESRCH)
  123. ret = p->ioprio;
  124. else
  125. ret = ioprio_best(ret, p->ioprio);
  126. } while_each_task_pid(who, PIDTYPE_PGID, p);
  127. break;
  128. case IOPRIO_WHO_USER:
  129. if (!who)
  130. user = current->user;
  131. else
  132. user = find_user(who);
  133. if (!user)
  134. break;
  135. do_each_thread(g, p) {
  136. if (p->uid != user->uid)
  137. continue;
  138. if (ret == -ESRCH)
  139. ret = p->ioprio;
  140. else
  141. ret = ioprio_best(ret, p->ioprio);
  142. } while_each_thread(g, p);
  143. if (who)
  144. free_uid(user);
  145. break;
  146. default:
  147. ret = -EINVAL;
  148. }
  149. read_unlock_irq(&tasklist_lock);
  150. return ret;
  151. }