kqid.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fs.h>
  3. #include <linux/quota.h>
  4. #include <linux/export.h>
  5. /**
  6. * qid_eq - Test to see if to kquid values are the same
  7. * @left: A qid value
  8. * @right: Another quid value
  9. *
  10. * Return true if the two qid values are equal and false otherwise.
  11. */
  12. bool qid_eq(struct kqid left, struct kqid right)
  13. {
  14. if (left.type != right.type)
  15. return false;
  16. switch(left.type) {
  17. case USRQUOTA:
  18. return uid_eq(left.uid, right.uid);
  19. case GRPQUOTA:
  20. return gid_eq(left.gid, right.gid);
  21. case PRJQUOTA:
  22. return projid_eq(left.projid, right.projid);
  23. default:
  24. BUG();
  25. }
  26. }
  27. EXPORT_SYMBOL(qid_eq);
  28. /**
  29. * qid_lt - Test to see if one qid value is less than another
  30. * @left: The possibly lesser qid value
  31. * @right: The possibly greater qid value
  32. *
  33. * Return true if left is less than right and false otherwise.
  34. */
  35. bool qid_lt(struct kqid left, struct kqid right)
  36. {
  37. if (left.type < right.type)
  38. return true;
  39. if (left.type > right.type)
  40. return false;
  41. switch (left.type) {
  42. case USRQUOTA:
  43. return uid_lt(left.uid, right.uid);
  44. case GRPQUOTA:
  45. return gid_lt(left.gid, right.gid);
  46. case PRJQUOTA:
  47. return projid_lt(left.projid, right.projid);
  48. default:
  49. BUG();
  50. }
  51. }
  52. EXPORT_SYMBOL(qid_lt);
  53. /**
  54. * from_kqid - Create a qid from a kqid user-namespace pair.
  55. * @targ: The user namespace we want a qid in.
  56. * @kqid: The kernel internal quota identifier to start with.
  57. *
  58. * Map @kqid into the user-namespace specified by @targ and
  59. * return the resulting qid.
  60. *
  61. * There is always a mapping into the initial user_namespace.
  62. *
  63. * If @kqid has no mapping in @targ (qid_t)-1 is returned.
  64. */
  65. qid_t from_kqid(struct user_namespace *targ, struct kqid kqid)
  66. {
  67. switch (kqid.type) {
  68. case USRQUOTA:
  69. return from_kuid(targ, kqid.uid);
  70. case GRPQUOTA:
  71. return from_kgid(targ, kqid.gid);
  72. case PRJQUOTA:
  73. return from_kprojid(targ, kqid.projid);
  74. default:
  75. BUG();
  76. }
  77. }
  78. EXPORT_SYMBOL(from_kqid);
  79. /**
  80. * from_kqid_munged - Create a qid from a kqid user-namespace pair.
  81. * @targ: The user namespace we want a qid in.
  82. * @kqid: The kernel internal quota identifier to start with.
  83. *
  84. * Map @kqid into the user-namespace specified by @targ and
  85. * return the resulting qid.
  86. *
  87. * There is always a mapping into the initial user_namespace.
  88. *
  89. * Unlike from_kqid from_kqid_munged never fails and always
  90. * returns a valid projid. This makes from_kqid_munged
  91. * appropriate for use in places where failing to provide
  92. * a qid_t is not a good option.
  93. *
  94. * If @kqid has no mapping in @targ the kqid.type specific
  95. * overflow identifier is returned.
  96. */
  97. qid_t from_kqid_munged(struct user_namespace *targ, struct kqid kqid)
  98. {
  99. switch (kqid.type) {
  100. case USRQUOTA:
  101. return from_kuid_munged(targ, kqid.uid);
  102. case GRPQUOTA:
  103. return from_kgid_munged(targ, kqid.gid);
  104. case PRJQUOTA:
  105. return from_kprojid_munged(targ, kqid.projid);
  106. default:
  107. BUG();
  108. }
  109. }
  110. EXPORT_SYMBOL(from_kqid_munged);
  111. /**
  112. * qid_valid - Report if a valid value is stored in a kqid.
  113. * @qid: The kernel internal quota identifier to test.
  114. */
  115. bool qid_valid(struct kqid qid)
  116. {
  117. switch (qid.type) {
  118. case USRQUOTA:
  119. return uid_valid(qid.uid);
  120. case GRPQUOTA:
  121. return gid_valid(qid.gid);
  122. case PRJQUOTA:
  123. return projid_valid(qid.projid);
  124. default:
  125. BUG();
  126. }
  127. }
  128. EXPORT_SYMBOL(qid_valid);