daemon/graphdriver/btrfs/btrfs.go
89ec17d1
 // +build linux
e51af36a
 
 package btrfs
 
 /*
 #include <stdlib.h>
 #include <dirent.h>
6922f1be
 #include <btrfs/ioctl.h>
dea78fc2
 #include <btrfs/ctree.h>
a038cccf
 
 static void set_name_btrfs_ioctl_vol_args_v2(struct btrfs_ioctl_vol_args_v2* btrfs_struct, const char* value) {
     snprintf(btrfs_struct->name, BTRFS_SUBVOL_NAME_MAX, "%s", value);
 }
e51af36a
 */
 import "C"
6922f1be
 
e51af36a
 import (
 	"fmt"
16328cc2
 	"io/ioutil"
e907c641
 	"math"
e51af36a
 	"os"
 	"path"
dea78fc2
 	"path/filepath"
16328cc2
 	"strconv"
401c8d17
 	"strings"
e907c641
 	"sync"
e51af36a
 	"unsafe"
3609b051
 
e907c641
 	"github.com/Sirupsen/logrus"
b3ee9ac7
 	"github.com/docker/docker/daemon/graphdriver"
442b4562
 	"github.com/docker/docker/pkg/idtools"
b3ee9ac7
 	"github.com/docker/docker/pkg/mount"
401c8d17
 	"github.com/docker/docker/pkg/parsers"
54dcbab2
 	"github.com/docker/docker/pkg/system"
401c8d17
 	"github.com/docker/go-units"
abbbf914
 	"github.com/opencontainers/selinux/go-selinux/label"
069fdc8a
 	"golang.org/x/sys/unix"
e51af36a
 )
 
 func init() {
 	graphdriver.Register("btrfs", Init)
 }
 
401c8d17
 type btrfsOptions struct {
 	minSpace uint64
 	size     uint64
 }
 
17c19f39
 // Init returns a new BTRFS driver.
 // An error is returned if BTRFS is not supported.
442b4562
 func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
e51af36a
 
feda5d76
 	fsMagic, err := graphdriver.GetFSMagic(home)
 	if err != nil {
e51af36a
 		return nil, err
 	}
 
feda5d76
 	if fsMagic != graphdriver.FsMagicBtrfs {
75754e69
 		return nil, graphdriver.ErrPrerequisites
e51af36a
 	}
 
442b4562
 	rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
 	if err != nil {
 		return nil, err
 	}
 	if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil {
3609b051
 		return nil, err
 	}
 
930a756a
 	if err := mount.MakePrivate(home); err != nil {
3609b051
 		return nil, err
 	}
 
b36e613d
 	opt, userDiskQuota, err := parseOptions(options)
401c8d17
 	if err != nil {
 		return nil, err
 	}
 
dee6b481
 	driver := &Driver{
442b4562
 		home:    home,
 		uidMaps: uidMaps,
 		gidMaps: gidMaps,
401c8d17
 		options: opt,
dee6b481
 	}
 
b36e613d
 	if userDiskQuota {
 		if err := driver.subvolEnableQuota(); err != nil {
 			return nil, err
 		}
 	}
 
442b4562
 	return graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps), nil
e51af36a
 }
 
b36e613d
 func parseOptions(opt []string) (btrfsOptions, bool, error) {
401c8d17
 	var options btrfsOptions
b36e613d
 	userDiskQuota := false
401c8d17
 	for _, option := range opt {
 		key, val, err := parsers.ParseKeyValueOpt(option)
 		if err != nil {
b36e613d
 			return options, userDiskQuota, err
401c8d17
 		}
 		key = strings.ToLower(key)
 		switch key {
 		case "btrfs.min_space":
 			minSpace, err := units.RAMInBytes(val)
 			if err != nil {
b36e613d
 				return options, userDiskQuota, err
401c8d17
 			}
 			userDiskQuota = true
 			options.minSpace = uint64(minSpace)
 		default:
b36e613d
 			return options, userDiskQuota, fmt.Errorf("Unknown option %s", key)
401c8d17
 		}
 	}
b36e613d
 	return options, userDiskQuota, nil
401c8d17
 }
 
17c19f39
 // Driver contains information about the filesystem mounted.
e51af36a
 type Driver struct {
17c19f39
 	//root of the file system
b36e613d
 	home         string
 	uidMaps      []idtools.IDMap
 	gidMaps      []idtools.IDMap
 	options      btrfsOptions
 	quotaEnabled bool
e907c641
 	once         sync.Once
e51af36a
 }
 
17c19f39
 // String prints the name of the driver (btrfs).
e51af36a
 func (d *Driver) String() string {
 	return "btrfs"
 }
 
17c19f39
 // Status returns current driver information in a two dimensional string array.
 // Output contains "Build Version" and "Library Version" of the btrfs libraries used.
 // Version information can be used to check compatibility with your kernel.
e51af36a
 func (d *Driver) Status() [][2]string {
25154682
 	status := [][2]string{}
17c19f39
 	if bv := btrfsBuildVersion(); bv != "-" {
b76e300b
 		status = append(status, [2]string{"Build Version", bv})
 	}
17c19f39
 	if lv := btrfsLibVersion(); lv != -1 {
25154682
 		status = append(status, [2]string{"Library Version", fmt.Sprintf("%d", lv)})
 	}
 	return status
e51af36a
 }
 
17c19f39
 // GetMetadata returns empty metadata for this driver.
407a626b
 func (d *Driver) GetMetadata(id string) (map[string]string, error) {
 	return nil, nil
 }
 
17c19f39
 // Cleanup unmounts the home directory.
e51af36a
 func (d *Driver) Cleanup() error {
b36e613d
 	if err := d.subvolDisableQuota(); err != nil {
 		return err
401c8d17
 	}
 
3609b051
 	return mount.Unmount(d.home)
e51af36a
 }
 
 func free(p *C.char) {
 	C.free(unsafe.Pointer(p))
 }
 
 func openDir(path string) (*C.DIR, error) {
 	Cpath := C.CString(path)
 	defer free(Cpath)
 
 	dir := C.opendir(Cpath)
 	if dir == nil {
 		return nil, fmt.Errorf("Can't open dir")
 	}
 	return dir, nil
 }
 
 func closeDir(dir *C.DIR) {
 	if dir != nil {
 		C.closedir(dir)
 	}
 }
 
 func getDirFd(dir *C.DIR) uintptr {
 	return uintptr(C.dirfd(dir))
 }
 
f0e6e135
 func subvolCreate(path, name string) error {
e51af36a
 	dir, err := openDir(path)
 	if err != nil {
 		return err
 	}
 	defer closeDir(dir)
 
 	var args C.struct_btrfs_ioctl_vol_args
 	for i, c := range []byte(name) {
 		args.name[i] = C.char(c)
 	}
 
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
e51af36a
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
f7f8e3c2
 		return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
e51af36a
 	}
 	return nil
 }
 
 func subvolSnapshot(src, dest, name string) error {
 	srcDir, err := openDir(src)
 	if err != nil {
 		return err
 	}
 	defer closeDir(srcDir)
 
 	destDir, err := openDir(dest)
 	if err != nil {
 		return err
 	}
 	defer closeDir(destDir)
 
 	var args C.struct_btrfs_ioctl_vol_args_v2
 	args.fd = C.__s64(getDirFd(srcDir))
a038cccf
 
 	var cs = C.CString(name)
 	C.set_name_btrfs_ioctl_vol_args_v2(&args, cs)
 	C.free(unsafe.Pointer(cs))
e51af36a
 
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
e51af36a
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
f7f8e3c2
 		return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
e51af36a
 	}
 	return nil
 }
 
bd06432b
 func isSubvolume(p string) (bool, error) {
069fdc8a
 	var bufStat unix.Stat_t
 	if err := unix.Lstat(p, &bufStat); err != nil {
bd06432b
 		return false, err
 	}
 
 	// return true if it is a btrfs subvolume
 	return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
 }
 
e907c641
 func subvolDelete(dirpath, name string, quotaEnabled bool) error {
dea78fc2
 	dir, err := openDir(dirpath)
e51af36a
 	if err != nil {
 		return err
 	}
 	defer closeDir(dir)
f9befce2
 	fullPath := path.Join(dirpath, name)
e51af36a
 
 	var args C.struct_btrfs_ioctl_vol_args
dea78fc2
 
bd06432b
 	// walk the btrfs subvolumes
 	walkSubvolumes := func(p string, f os.FileInfo, err error) error {
f9befce2
 		if err != nil {
 			if os.IsNotExist(err) && p != fullPath {
 				// missing most likely because the path was a subvolume that got removed in the previous iteration
 				// since it's gone anyway, we don't care
 				return nil
 			}
 			return fmt.Errorf("error walking subvolumes: %v", err)
 		}
bd06432b
 		// we want to check children only so skip itself
 		// it will be removed after the filepath walk anyways
f9befce2
 		if f.IsDir() && p != fullPath {
bd06432b
 			sv, err := isSubvolume(p)
 			if err != nil {
 				return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
 			}
 			if sv {
e907c641
 				if err := subvolDelete(path.Dir(p), f.Name(), quotaEnabled); err != nil {
bd06432b
 					return fmt.Errorf("Failed to destroy btrfs child subvolume (%s) of parent (%s): %v", p, dirpath, err)
dea78fc2
 				}
 			}
bd06432b
 		}
 		return nil
 	}
 	if err := filepath.Walk(path.Join(dirpath, name), walkSubvolumes); err != nil {
 		return fmt.Errorf("Recursively walking subvolumes for %s failed: %v", dirpath, err)
 	}
dea78fc2
 
e907c641
 	if quotaEnabled {
 		if qgroupid, err := subvolLookupQgroup(fullPath); err == nil {
 			var args C.struct_btrfs_ioctl_qgroup_create_args
 			args.qgroupid = C.__u64(qgroupid)
 
069fdc8a
 			_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_CREATE,
e907c641
 				uintptr(unsafe.Pointer(&args)))
 			if errno != 0 {
 				logrus.Errorf("Failed to delete btrfs qgroup %v for %s: %v", qgroupid, fullPath, errno.Error())
 			}
 		} else {
 			logrus.Errorf("Failed to lookup btrfs qgroup for %s: %v", fullPath, err.Error())
 		}
 	}
 
bd06432b
 	// all subvolumes have been removed
 	// now remove the one originally passed in
e51af36a
 	for i, c := range []byte(name) {
 		args.name[i] = C.char(c)
 	}
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
e51af36a
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
bd06432b
 		return fmt.Errorf("Failed to destroy btrfs snapshot %s for %s: %v", dirpath, name, errno.Error())
e51af36a
 	}
 	return nil
 }
 
e907c641
 func (d *Driver) updateQuotaStatus() {
 	d.once.Do(func() {
 		if !d.quotaEnabled {
 			// In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
 			if err := subvolQgroupStatus(d.home); err != nil {
 				// quota is still not enabled
 				return
 			}
 			d.quotaEnabled = true
 		}
 	})
 }
 
b36e613d
 func (d *Driver) subvolEnableQuota() error {
e907c641
 	d.updateQuotaStatus()
 
b36e613d
 	if d.quotaEnabled {
 		return nil
 	}
 
 	dir, err := openDir(d.home)
401c8d17
 	if err != nil {
 		return err
 	}
 	defer closeDir(dir)
 
 	var args C.struct_btrfs_ioctl_quota_ctl_args
 	args.cmd = C.BTRFS_QUOTA_CTL_ENABLE
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
401c8d17
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
 		return fmt.Errorf("Failed to enable btrfs quota for %s: %v", dir, errno.Error())
 	}
 
b36e613d
 	d.quotaEnabled = true
 
401c8d17
 	return nil
 }
 
b36e613d
 func (d *Driver) subvolDisableQuota() error {
e907c641
 	d.updateQuotaStatus()
 
b36e613d
 	if !d.quotaEnabled {
e907c641
 		return nil
b36e613d
 	}
 
 	dir, err := openDir(d.home)
401c8d17
 	if err != nil {
 		return err
 	}
 	defer closeDir(dir)
 
 	var args C.struct_btrfs_ioctl_quota_ctl_args
 	args.cmd = C.BTRFS_QUOTA_CTL_DISABLE
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
401c8d17
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
 		return fmt.Errorf("Failed to disable btrfs quota for %s: %v", dir, errno.Error())
 	}
 
b36e613d
 	d.quotaEnabled = false
 
401c8d17
 	return nil
 }
 
b36e613d
 func (d *Driver) subvolRescanQuota() error {
e907c641
 	d.updateQuotaStatus()
 
b36e613d
 	if !d.quotaEnabled {
e907c641
 		return nil
b36e613d
 	}
 
 	dir, err := openDir(d.home)
401c8d17
 	if err != nil {
 		return err
 	}
 	defer closeDir(dir)
 
 	var args C.struct_btrfs_ioctl_quota_rescan_args
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_RESCAN_WAIT,
401c8d17
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
 		return fmt.Errorf("Failed to rescan btrfs quota for %s: %v", dir, errno.Error())
 	}
 
 	return nil
 }
 
 func subvolLimitQgroup(path string, size uint64) error {
 	dir, err := openDir(path)
 	if err != nil {
 		return err
 	}
 	defer closeDir(dir)
 
 	var args C.struct_btrfs_ioctl_qgroup_limit_args
 	args.lim.max_referenced = C.__u64(size)
 	args.lim.flags = C.BTRFS_QGROUP_LIMIT_MAX_RFER
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_LIMIT,
401c8d17
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
 		return fmt.Errorf("Failed to limit qgroup for %s: %v", dir, errno.Error())
 	}
 
 	return nil
 }
 
e907c641
 // subvolQgroupStatus performs a BTRFS_IOC_TREE_SEARCH on the root path
 // with search key of BTRFS_QGROUP_STATUS_KEY.
 // In case qgroup is enabled, the retuned key type will match BTRFS_QGROUP_STATUS_KEY.
 // For more details please see https://github.com/kdave/btrfs-progs/blob/v4.9/qgroup.c#L1035
 func subvolQgroupStatus(path string) error {
 	dir, err := openDir(path)
 	if err != nil {
 		return err
 	}
 	defer closeDir(dir)
 
 	var args C.struct_btrfs_ioctl_search_args
 	args.key.tree_id = C.BTRFS_QUOTA_TREE_OBJECTID
 	args.key.min_type = C.BTRFS_QGROUP_STATUS_KEY
 	args.key.max_type = C.BTRFS_QGROUP_STATUS_KEY
 	args.key.max_objectid = C.__u64(math.MaxUint64)
 	args.key.max_offset = C.__u64(math.MaxUint64)
 	args.key.max_transid = C.__u64(math.MaxUint64)
 	args.key.nr_items = 4096
 
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_TREE_SEARCH,
e907c641
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
 		return fmt.Errorf("Failed to search qgroup for %s: %v", path, errno.Error())
 	}
 	sh := (*C.struct_btrfs_ioctl_search_header)(unsafe.Pointer(&args.buf))
 	if sh._type != C.BTRFS_QGROUP_STATUS_KEY {
 		return fmt.Errorf("Invalid qgroup search header type for %s: %v", path, sh._type)
 	}
 	return nil
 }
 
b36e613d
 func subvolLookupQgroup(path string) (uint64, error) {
 	dir, err := openDir(path)
 	if err != nil {
 		return 0, err
 	}
 	defer closeDir(dir)
 
 	var args C.struct_btrfs_ioctl_ino_lookup_args
 	args.objectid = C.BTRFS_FIRST_FREE_OBJECTID
 
069fdc8a
 	_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_INO_LOOKUP,
b36e613d
 		uintptr(unsafe.Pointer(&args)))
 	if errno != 0 {
 		return 0, fmt.Errorf("Failed to lookup qgroup for %s: %v", dir, errno.Error())
 	}
 	if args.treeid == 0 {
 		return 0, fmt.Errorf("Invalid qgroup id for %s: 0", dir)
 	}
 
 	return uint64(args.treeid), nil
 }
 
e51af36a
 func (d *Driver) subvolumesDir() string {
 	return path.Join(d.home, "subvolumes")
 }
 
17c19f39
 func (d *Driver) subvolumesDirID(id string) string {
e51af36a
 	return path.Join(d.subvolumesDir(), id)
 }
 
16328cc2
 func (d *Driver) quotasDir() string {
 	return path.Join(d.home, "quotas")
 }
 
 func (d *Driver) quotasDirID(id string) string {
 	return path.Join(d.quotasDir(), id)
 }
 
ef5bfad3
 // CreateReadWrite creates a layer that is writable for use as a container
 // file system.
b937aa8e
 func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
 	return d.Create(id, parent, opts)
ef5bfad3
 }
 
17c19f39
 // Create the filesystem with given id.
b937aa8e
 func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
16328cc2
 	quotas := path.Join(d.home, "quotas")
e51af36a
 	subvolumes := path.Join(d.home, "subvolumes")
442b4562
 	rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
 	if err != nil {
 		return err
 	}
 	if err := idtools.MkdirAllAs(subvolumes, 0700, rootUID, rootGID); err != nil {
e51af36a
 		return err
 	}
 	if parent == "" {
f0e6e135
 		if err := subvolCreate(subvolumes, id); err != nil {
e51af36a
 			return err
 		}
 	} else {
b2e27fee
 		parentDir := d.subvolumesDirID(parent)
 		st, err := os.Stat(parentDir)
e51af36a
 		if err != nil {
 			return err
 		}
b2e27fee
 		if !st.IsDir() {
c33cdf9e
 			return fmt.Errorf("%s: not a directory", parentDir)
b2e27fee
 		}
e51af36a
 		if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
 			return err
 		}
 	}
1716d497
 
b937aa8e
 	var storageOpt map[string]string
 	if opts != nil {
 		storageOpt = opts.StorageOpt
 	}
 
401c8d17
 	if _, ok := storageOpt["size"]; ok {
 		driver := &Driver{}
 		if err := d.parseStorageOpt(storageOpt, driver); err != nil {
 			return err
 		}
16328cc2
 
401c8d17
 		if err := d.setStorageSize(path.Join(subvolumes, id), driver); err != nil {
 			return err
 		}
16328cc2
 		if err := idtools.MkdirAllAs(quotas, 0700, rootUID, rootGID); err != nil {
 			return err
 		}
 		if err := ioutil.WriteFile(path.Join(quotas, id), []byte(fmt.Sprint(driver.options.size)), 0644); err != nil {
 			return err
 		}
401c8d17
 	}
 
72e65e87
 	// if we have a remapped root (user namespaces enabled), change the created snapshot
 	// dir ownership to match
 	if rootUID != 0 || rootGID != 0 {
 		if err := os.Chown(path.Join(subvolumes, id), rootUID, rootGID); err != nil {
 			return err
 		}
 	}
 
b937aa8e
 	mountLabel := ""
 	if opts != nil {
 		mountLabel = opts.MountLabel
 	}
 
1716d497
 	return label.Relabel(path.Join(subvolumes, id), mountLabel, false)
e51af36a
 }
 
401c8d17
 // Parse btrfs storage options
 func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
 	// Read size to change the subvolume disk quota per container
 	for key, val := range storageOpt {
 		key := strings.ToLower(key)
 		switch key {
 		case "size":
 			size, err := units.RAMInBytes(val)
 			if err != nil {
 				return err
 			}
 			driver.options.size = uint64(size)
 		default:
 			return fmt.Errorf("Unknown option %s", key)
 		}
 	}
 
 	return nil
 }
 
 // Set btrfs storage size
 func (d *Driver) setStorageSize(dir string, driver *Driver) error {
 	if driver.options.size <= 0 {
 		return fmt.Errorf("btrfs: invalid storage size: %s", units.HumanSize(float64(driver.options.size)))
 	}
 	if d.options.minSpace > 0 && driver.options.size < d.options.minSpace {
 		return fmt.Errorf("btrfs: storage size cannot be less than %s", units.HumanSize(float64(d.options.minSpace)))
 	}
 
b36e613d
 	if err := d.subvolEnableQuota(); err != nil {
 		return err
401c8d17
 	}
 
 	if err := subvolLimitQgroup(dir, driver.options.size); err != nil {
 		return err
 	}
 
 	return nil
 }
 
17c19f39
 // Remove the filesystem with given id.
e51af36a
 func (d *Driver) Remove(id string) error {
17c19f39
 	dir := d.subvolumesDirID(id)
e51af36a
 	if _, err := os.Stat(dir); err != nil {
 		return err
 	}
16328cc2
 	quotasDir := d.quotasDirID(id)
 	if _, err := os.Stat(quotasDir); err == nil {
 		if err := os.Remove(quotasDir); err != nil {
 			return err
 		}
 	} else if !os.IsNotExist(err) {
 		return err
 	}
e907c641
 
 	// Call updateQuotaStatus() to invoke status update
 	d.updateQuotaStatus()
 
 	if err := subvolDelete(d.subvolumesDir(), id, d.quotaEnabled); err != nil {
e51af36a
 		return err
 	}
54dcbab2
 	if err := system.EnsureRemoveAll(dir); err != nil {
de7f6cf1
 		return err
 	}
b36e613d
 	if err := d.subvolRescanQuota(); err != nil {
401c8d17
 		return err
 	}
de7f6cf1
 	return nil
e51af36a
 }
 
17c19f39
 // Get the requested filesystem id.
f0e6e135
 func (d *Driver) Get(id, mountLabel string) (string, error) {
17c19f39
 	dir := d.subvolumesDirID(id)
e51af36a
 	st, err := os.Stat(dir)
 	if err != nil {
 		return "", err
 	}
 
 	if !st.IsDir() {
 		return "", fmt.Errorf("%s: not a directory", dir)
 	}
 
16328cc2
 	if quota, err := ioutil.ReadFile(d.quotasDirID(id)); err == nil {
 		if size, err := strconv.ParseUint(string(quota), 10, 64); err == nil && size >= d.options.minSpace {
 			if err := d.subvolEnableQuota(); err != nil {
 				return "", err
 			}
 			if err := subvolLimitQgroup(dir, size); err != nil {
 				return "", err
 			}
 		}
 	}
 
e51af36a
 	return dir, nil
 }
 
17c19f39
 // Put is not implemented for BTRFS as there is no cleanup required for the id.
00fd63e5
 func (d *Driver) Put(id string) error {
a1851a6d
 	// Get() creates no runtime resources (like e.g. mounts)
 	// so this doesn't need to do anything.
00fd63e5
 	return nil
e51af36a
 }
 
17c19f39
 // Exists checks if the id exists in the filesystem.
e51af36a
 func (d *Driver) Exists(id string) bool {
17c19f39
 	dir := d.subvolumesDirID(id)
e51af36a
 	_, err := os.Stat(dir)
 	return err == nil
 }