projectquota.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // +build linux
  2. //
  3. // projectquota.go - implements XFS project quota controls
  4. // for setting quota limits on a newly created directory.
  5. // It currently supports the legacy XFS specific ioctls.
  6. //
  7. // TODO: use generic quota control ioctl FS_IOC_FS{GET,SET}XATTR
  8. // for both xfs/ext4 for kernel version >= v4.5
  9. //
  10. package quota
  11. /*
  12. #include <stdlib.h>
  13. #include <dirent.h>
  14. #include <linux/fs.h>
  15. #include <linux/quota.h>
  16. #include <linux/dqblk_xfs.h>
  17. #ifndef FS_XFLAG_PROJINHERIT
  18. struct fsxattr {
  19. __u32 fsx_xflags;
  20. __u32 fsx_extsize;
  21. __u32 fsx_nextents;
  22. __u32 fsx_projid;
  23. unsigned char fsx_pad[12];
  24. };
  25. #define FS_XFLAG_PROJINHERIT 0x00000200
  26. #endif
  27. #ifndef FS_IOC_FSGETXATTR
  28. #define FS_IOC_FSGETXATTR _IOR ('X', 31, struct fsxattr)
  29. #endif
  30. #ifndef FS_IOC_FSSETXATTR
  31. #define FS_IOC_FSSETXATTR _IOW ('X', 32, struct fsxattr)
  32. #endif
  33. #ifndef PRJQUOTA
  34. #define PRJQUOTA 2
  35. #endif
  36. #ifndef XFS_PROJ_QUOTA
  37. #define XFS_PROJ_QUOTA 2
  38. #endif
  39. #ifndef Q_XSETPQLIM
  40. #define Q_XSETPQLIM QCMD(Q_XSETQLIM, PRJQUOTA)
  41. #endif
  42. #ifndef Q_XGETPQUOTA
  43. #define Q_XGETPQUOTA QCMD(Q_XGETQUOTA, PRJQUOTA)
  44. #endif
  45. */
  46. import "C"
  47. import (
  48. "fmt"
  49. "io/ioutil"
  50. "path"
  51. "path/filepath"
  52. "unsafe"
  53. "github.com/sirupsen/logrus"
  54. "golang.org/x/sys/unix"
  55. )
  56. // Quota limit params - currently we only control blocks hard limit
  57. type Quota struct {
  58. Size uint64
  59. }
  60. // Control - Context to be used by storage driver (e.g. overlay)
  61. // who wants to apply project quotas to container dirs
  62. type Control struct {
  63. backingFsBlockDev string
  64. nextProjectID uint32
  65. quotas map[string]uint32
  66. }
  67. // NewControl - initialize project quota support.
  68. // Test to make sure that quota can be set on a test dir and find
  69. // the first project id to be used for the next container create.
  70. //
  71. // Returns nil (and error) if project quota is not supported.
  72. //
  73. // First get the project id of the home directory.
  74. // This test will fail if the backing fs is not xfs.
  75. //
  76. // xfs_quota tool can be used to assign a project id to the driver home directory, e.g.:
  77. // echo 999:/var/lib/docker/overlay2 >> /etc/projects
  78. // echo docker:999 >> /etc/projid
  79. // xfs_quota -x -c 'project -s docker' /<xfs mount point>
  80. //
  81. // In that case, the home directory project id will be used as a "start offset"
  82. // and all containers will be assigned larger project ids (e.g. >= 1000).
  83. // This is a way to prevent xfs_quota management from conflicting with docker.
  84. //
  85. // Then try to create a test directory with the next project id and set a quota
  86. // on it. If that works, continue to scan existing containers to map allocated
  87. // project ids.
  88. //
  89. func NewControl(basePath string) (*Control, error) {
  90. //
  91. // Get project id of parent dir as minimal id to be used by driver
  92. //
  93. minProjectID, err := getProjectID(basePath)
  94. if err != nil {
  95. return nil, err
  96. }
  97. minProjectID++
  98. //
  99. // create backing filesystem device node
  100. //
  101. backingFsBlockDev, err := makeBackingFsDev(basePath)
  102. if err != nil {
  103. return nil, err
  104. }
  105. //
  106. // Test if filesystem supports project quotas by trying to set
  107. // a quota on the first available project id
  108. //
  109. quota := Quota{
  110. Size: 0,
  111. }
  112. if err := setProjectQuota(backingFsBlockDev, minProjectID, quota); err != nil {
  113. return nil, err
  114. }
  115. q := Control{
  116. backingFsBlockDev: backingFsBlockDev,
  117. nextProjectID: minProjectID + 1,
  118. quotas: make(map[string]uint32),
  119. }
  120. //
  121. // get first project id to be used for next container
  122. //
  123. err = q.findNextProjectID(basePath)
  124. if err != nil {
  125. return nil, err
  126. }
  127. logrus.Debugf("NewControl(%s): nextProjectID = %d", basePath, q.nextProjectID)
  128. return &q, nil
  129. }
  130. // SetQuota - assign a unique project id to directory and set the quota limits
  131. // for that project id
  132. func (q *Control) SetQuota(targetPath string, quota Quota) error {
  133. projectID, ok := q.quotas[targetPath]
  134. if !ok {
  135. projectID = q.nextProjectID
  136. //
  137. // assign project id to new container directory
  138. //
  139. err := setProjectID(targetPath, projectID)
  140. if err != nil {
  141. return err
  142. }
  143. q.quotas[targetPath] = projectID
  144. q.nextProjectID++
  145. }
  146. //
  147. // set the quota limit for the container's project id
  148. //
  149. logrus.Debugf("SetQuota(%s, %d): projectID=%d", targetPath, quota.Size, projectID)
  150. return setProjectQuota(q.backingFsBlockDev, projectID, quota)
  151. }
  152. // setProjectQuota - set the quota for project id on xfs block device
  153. func setProjectQuota(backingFsBlockDev string, projectID uint32, quota Quota) error {
  154. var d C.fs_disk_quota_t
  155. d.d_version = C.FS_DQUOT_VERSION
  156. d.d_id = C.__u32(projectID)
  157. d.d_flags = C.XFS_PROJ_QUOTA
  158. d.d_fieldmask = C.FS_DQ_BHARD | C.FS_DQ_BSOFT
  159. d.d_blk_hardlimit = C.__u64(quota.Size / 512)
  160. d.d_blk_softlimit = d.d_blk_hardlimit
  161. var cs = C.CString(backingFsBlockDev)
  162. defer C.free(unsafe.Pointer(cs))
  163. _, _, errno := unix.Syscall6(unix.SYS_QUOTACTL, C.Q_XSETPQLIM,
  164. uintptr(unsafe.Pointer(cs)), uintptr(d.d_id),
  165. uintptr(unsafe.Pointer(&d)), 0, 0)
  166. if errno != 0 {
  167. return fmt.Errorf("Failed to set quota limit for projid %d on %s: %v",
  168. projectID, backingFsBlockDev, errno.Error())
  169. }
  170. return nil
  171. }
  172. // GetQuota - get the quota limits of a directory that was configured with SetQuota
  173. func (q *Control) GetQuota(targetPath string, quota *Quota) error {
  174. projectID, ok := q.quotas[targetPath]
  175. if !ok {
  176. return fmt.Errorf("quota not found for path : %s", targetPath)
  177. }
  178. //
  179. // get the quota limit for the container's project id
  180. //
  181. var d C.fs_disk_quota_t
  182. var cs = C.CString(q.backingFsBlockDev)
  183. defer C.free(unsafe.Pointer(cs))
  184. _, _, errno := unix.Syscall6(unix.SYS_QUOTACTL, C.Q_XGETPQUOTA,
  185. uintptr(unsafe.Pointer(cs)), uintptr(C.__u32(projectID)),
  186. uintptr(unsafe.Pointer(&d)), 0, 0)
  187. if errno != 0 {
  188. return fmt.Errorf("Failed to get quota limit for projid %d on %s: %v",
  189. projectID, q.backingFsBlockDev, errno.Error())
  190. }
  191. quota.Size = uint64(d.d_blk_hardlimit) * 512
  192. return nil
  193. }
  194. // getProjectID - get the project id of path on xfs
  195. func getProjectID(targetPath string) (uint32, error) {
  196. dir, err := openDir(targetPath)
  197. if err != nil {
  198. return 0, err
  199. }
  200. defer closeDir(dir)
  201. var fsx C.struct_fsxattr
  202. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.FS_IOC_FSGETXATTR,
  203. uintptr(unsafe.Pointer(&fsx)))
  204. if errno != 0 {
  205. return 0, fmt.Errorf("Failed to get projid for %s: %v", targetPath, errno.Error())
  206. }
  207. return uint32(fsx.fsx_projid), nil
  208. }
  209. // setProjectID - set the project id of path on xfs
  210. func setProjectID(targetPath string, projectID uint32) error {
  211. dir, err := openDir(targetPath)
  212. if err != nil {
  213. return err
  214. }
  215. defer closeDir(dir)
  216. var fsx C.struct_fsxattr
  217. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.FS_IOC_FSGETXATTR,
  218. uintptr(unsafe.Pointer(&fsx)))
  219. if errno != 0 {
  220. return fmt.Errorf("Failed to get projid for %s: %v", targetPath, errno.Error())
  221. }
  222. fsx.fsx_projid = C.__u32(projectID)
  223. fsx.fsx_xflags |= C.FS_XFLAG_PROJINHERIT
  224. _, _, errno = unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.FS_IOC_FSSETXATTR,
  225. uintptr(unsafe.Pointer(&fsx)))
  226. if errno != 0 {
  227. return fmt.Errorf("Failed to set projid for %s: %v", targetPath, errno.Error())
  228. }
  229. return nil
  230. }
  231. // findNextProjectID - find the next project id to be used for containers
  232. // by scanning driver home directory to find used project ids
  233. func (q *Control) findNextProjectID(home string) error {
  234. files, err := ioutil.ReadDir(home)
  235. if err != nil {
  236. return fmt.Errorf("read directory failed : %s", home)
  237. }
  238. for _, file := range files {
  239. if !file.IsDir() {
  240. continue
  241. }
  242. path := filepath.Join(home, file.Name())
  243. projid, err := getProjectID(path)
  244. if err != nil {
  245. return err
  246. }
  247. if projid > 0 {
  248. q.quotas[path] = projid
  249. }
  250. if q.nextProjectID <= projid {
  251. q.nextProjectID = projid + 1
  252. }
  253. }
  254. return nil
  255. }
  256. func free(p *C.char) {
  257. C.free(unsafe.Pointer(p))
  258. }
  259. func openDir(path string) (*C.DIR, error) {
  260. Cpath := C.CString(path)
  261. defer free(Cpath)
  262. dir := C.opendir(Cpath)
  263. if dir == nil {
  264. return nil, fmt.Errorf("Can't open dir")
  265. }
  266. return dir, nil
  267. }
  268. func closeDir(dir *C.DIR) {
  269. if dir != nil {
  270. C.closedir(dir)
  271. }
  272. }
  273. func getDirFd(dir *C.DIR) uintptr {
  274. return uintptr(C.dirfd(dir))
  275. }
  276. // Get the backing block device of the driver home directory
  277. // and create a block device node under the home directory
  278. // to be used by quotactl commands
  279. func makeBackingFsDev(home string) (string, error) {
  280. var stat unix.Stat_t
  281. if err := unix.Stat(home, &stat); err != nil {
  282. return "", err
  283. }
  284. backingFsBlockDev := path.Join(home, "backingFsBlockDev")
  285. // Re-create just in case someone copied the home directory over to a new device
  286. unix.Unlink(backingFsBlockDev)
  287. if err := unix.Mknod(backingFsBlockDev, unix.S_IFBLK|0600, int(stat.Dev)); err != nil {
  288. return "", fmt.Errorf("Failed to mknod %s: %v", backingFsBlockDev, err)
  289. }
  290. return backingFsBlockDev, nil
  291. }