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