projectquota.go 8.6 KB

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