quota_linux_common.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // +build linux
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package common
  15. import (
  16. "regexp"
  17. )
  18. // QuotaID is generic quota identifier.
  19. // Data type based on quotactl(2).
  20. type QuotaID int32
  21. const (
  22. // UnknownQuotaID -- cannot determine whether a quota is in force
  23. UnknownQuotaID QuotaID = -1
  24. // BadQuotaID -- Invalid quota
  25. BadQuotaID QuotaID = 0
  26. )
  27. const (
  28. acct = iota
  29. enforcing = iota
  30. )
  31. // QuotaType -- type of quota to be applied
  32. type QuotaType int
  33. const (
  34. // FSQuotaAccounting for quotas for accounting only
  35. FSQuotaAccounting QuotaType = 1 << iota
  36. // FSQuotaEnforcing for quotas for enforcement
  37. FSQuotaEnforcing QuotaType = 1 << iota
  38. )
  39. // FirstQuota is the quota ID we start with.
  40. // XXXXXXX Need a better way of doing this...
  41. var FirstQuota QuotaID = 1048577
  42. // MountsFile is the location of the system mount data
  43. var MountsFile = "/proc/self/mounts"
  44. // MountParseRegexp parses out /proc/sys/self/mounts
  45. var MountParseRegexp = regexp.MustCompilePOSIX("^([^ ]*)[ \t]*([^ ]*)[ \t]*([^ ]*)") // Ignore options etc.
  46. // LinuxVolumeQuotaProvider returns an appropriate quota applier
  47. // object if we can support quotas on this device
  48. type LinuxVolumeQuotaProvider interface {
  49. // GetQuotaApplier retrieves an object that can apply
  50. // quotas (or nil if this provider cannot support quotas
  51. // on the device)
  52. GetQuotaApplier(mountpoint string, backingDev string) LinuxVolumeQuotaApplier
  53. }
  54. // LinuxVolumeQuotaApplier is a generic interface to any quota
  55. // mechanism supported by Linux
  56. type LinuxVolumeQuotaApplier interface {
  57. // GetQuotaOnDir gets the quota ID (if any) that applies to
  58. // this directory
  59. GetQuotaOnDir(path string) (QuotaID, error)
  60. // SetQuotaOnDir applies the specified quota ID to a directory.
  61. // Negative value for bytes means that a non-enforcing quota
  62. // should be applied (perhaps by setting a quota too large to
  63. // be hit)
  64. SetQuotaOnDir(path string, id QuotaID, bytes int64) error
  65. // QuotaIDIsInUse determines whether the quota ID is in use.
  66. // Implementations should not check /etc/project or /etc/projid,
  67. // only whether their underlying mechanism already has the ID in
  68. // use.
  69. // Return value of false with no error means that the ID is not
  70. // in use; true means that it is already in use. An error
  71. // return means that any quota ID will fail.
  72. QuotaIDIsInUse(id QuotaID) (bool, error)
  73. // GetConsumption returns the consumption (in bytes) of the
  74. // directory, determined by the implementation's quota-based
  75. // mechanism. If it is unable to do so using that mechanism,
  76. // it should return an error and allow higher layers to
  77. // enumerate the directory.
  78. GetConsumption(path string, id QuotaID) (int64, error)
  79. // GetInodes returns the number of inodes used by the
  80. // directory, determined by the implementation's quota-based
  81. // mechanism. If it is unable to do so using that mechanism,
  82. // it should return an error and allow higher layers to
  83. // enumerate the directory.
  84. GetInodes(path string, id QuotaID) (int64, error)
  85. }