btrfs.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. //go:build linux
  2. package btrfs // import "github.com/docker/docker/daemon/graphdriver/btrfs"
  3. /*
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <dirent.h>
  7. #include <linux/version.h>
  8. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0)
  9. #error "Headers from kernel >= 4.12 are required to build with Btrfs support."
  10. #error "HINT: Set 'DOCKER_BUILDTAGS=exclude_graphdriver_btrfs' to build without Btrfs."
  11. #endif
  12. #include <linux/btrfs.h>
  13. #include <linux/btrfs_tree.h>
  14. static void set_name_btrfs_ioctl_vol_args_v2(struct btrfs_ioctl_vol_args_v2* btrfs_struct, const char* value) {
  15. snprintf(btrfs_struct->name, BTRFS_SUBVOL_NAME_MAX, "%s", value);
  16. }
  17. */
  18. import "C"
  19. import (
  20. "context"
  21. "fmt"
  22. "math"
  23. "os"
  24. "path"
  25. "path/filepath"
  26. "strconv"
  27. "strings"
  28. "sync"
  29. "unsafe"
  30. "github.com/containerd/containerd/pkg/userns"
  31. "github.com/containerd/log"
  32. "github.com/docker/docker/daemon/graphdriver"
  33. "github.com/docker/docker/pkg/containerfs"
  34. "github.com/docker/docker/pkg/idtools"
  35. "github.com/docker/docker/pkg/parsers"
  36. units "github.com/docker/go-units"
  37. "github.com/moby/sys/mount"
  38. "github.com/opencontainers/selinux/go-selinux/label"
  39. "github.com/pkg/errors"
  40. "golang.org/x/sys/unix"
  41. )
  42. func init() {
  43. graphdriver.Register("btrfs", Init)
  44. }
  45. type btrfsOptions struct {
  46. minSpace uint64
  47. size uint64
  48. }
  49. // Init returns a new BTRFS driver.
  50. // An error is returned if BTRFS is not supported.
  51. func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdriver.Driver, error) {
  52. // Perform feature detection on /var/lib/docker/btrfs if it's an existing directory.
  53. // This covers situations where /var/lib/docker/btrfs is a mount, and on a different
  54. // filesystem than /var/lib/docker.
  55. // If the path does not exist, fall back to using /var/lib/docker for feature detection.
  56. testdir := home
  57. if _, err := os.Stat(testdir); os.IsNotExist(err) {
  58. testdir = filepath.Dir(testdir)
  59. }
  60. fsMagic, err := graphdriver.GetFSMagic(testdir)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if fsMagic != graphdriver.FsMagicBtrfs {
  65. return nil, graphdriver.ErrPrerequisites
  66. }
  67. currentID := idtools.CurrentIdentity()
  68. dirID := idtools.Identity{
  69. UID: currentID.UID,
  70. GID: idMap.RootPair().GID,
  71. }
  72. if err := idtools.MkdirAllAndChown(home, 0o710, dirID); err != nil {
  73. return nil, err
  74. }
  75. opt, userDiskQuota, err := parseOptions(options)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // For some reason shared mount propagation between a container
  80. // and the host does not work for btrfs, and a remedy is to bind
  81. // mount graphdriver home to itself (even without changing the
  82. // propagation mode).
  83. err = mount.MakeMount(home)
  84. if err != nil {
  85. return nil, errors.Wrapf(err, "failed to make %s a mount", home)
  86. }
  87. driver := &Driver{
  88. home: home,
  89. idMap: idMap,
  90. options: opt,
  91. }
  92. if userDiskQuota {
  93. if err := driver.enableQuota(); err != nil {
  94. return nil, err
  95. }
  96. }
  97. return graphdriver.NewNaiveDiffDriver(driver, driver.idMap), nil
  98. }
  99. func parseOptions(opt []string) (btrfsOptions, bool, error) {
  100. var options btrfsOptions
  101. userDiskQuota := false
  102. for _, option := range opt {
  103. key, val, err := parsers.ParseKeyValueOpt(option)
  104. if err != nil {
  105. return options, userDiskQuota, err
  106. }
  107. key = strings.ToLower(key)
  108. switch key {
  109. case "btrfs.min_space":
  110. minSpace, err := units.RAMInBytes(val)
  111. if err != nil {
  112. return options, userDiskQuota, err
  113. }
  114. userDiskQuota = true
  115. options.minSpace = uint64(minSpace)
  116. default:
  117. return options, userDiskQuota, fmt.Errorf("Unknown option %s", key)
  118. }
  119. }
  120. return options, userDiskQuota, nil
  121. }
  122. // Driver contains information about the filesystem mounted.
  123. type Driver struct {
  124. // root of the file system
  125. home string
  126. idMap idtools.IdentityMapping
  127. options btrfsOptions
  128. quotaEnabled bool
  129. once sync.Once
  130. }
  131. // String prints the name of the driver (btrfs).
  132. func (d *Driver) String() string {
  133. return "btrfs"
  134. }
  135. // Status returns the status of the driver.
  136. func (d *Driver) Status() [][2]string {
  137. return [][2]string{
  138. {"Btrfs", ""},
  139. }
  140. }
  141. // GetMetadata returns empty metadata for this driver.
  142. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  143. return nil, nil
  144. }
  145. // Cleanup unmounts the home directory.
  146. func (d *Driver) Cleanup() error {
  147. if err := mount.Unmount(d.home); err != nil {
  148. return err
  149. }
  150. return nil
  151. }
  152. func free(p *C.char) {
  153. C.free(unsafe.Pointer(p))
  154. }
  155. func openDir(path string) (*C.DIR, error) {
  156. Cpath := C.CString(path)
  157. defer free(Cpath)
  158. dir := C.opendir(Cpath)
  159. if dir == nil {
  160. return nil, fmt.Errorf("Can't open dir")
  161. }
  162. return dir, nil
  163. }
  164. func closeDir(dir *C.DIR) {
  165. if dir != nil {
  166. C.closedir(dir)
  167. }
  168. }
  169. func getDirFd(dir *C.DIR) uintptr {
  170. return uintptr(C.dirfd(dir))
  171. }
  172. func subvolCreate(path, name string) error {
  173. dir, err := openDir(path)
  174. if err != nil {
  175. return err
  176. }
  177. defer closeDir(dir)
  178. var args C.struct_btrfs_ioctl_vol_args
  179. for i, c := range []byte(name) {
  180. args.name[i] = C.char(c)
  181. }
  182. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
  183. uintptr(unsafe.Pointer(&args)))
  184. if errno != 0 {
  185. return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
  186. }
  187. return nil
  188. }
  189. func subvolSnapshot(src, dest, name string) error {
  190. srcDir, err := openDir(src)
  191. if err != nil {
  192. return err
  193. }
  194. defer closeDir(srcDir)
  195. destDir, err := openDir(dest)
  196. if err != nil {
  197. return err
  198. }
  199. defer closeDir(destDir)
  200. var args C.struct_btrfs_ioctl_vol_args_v2
  201. args.fd = C.__s64(getDirFd(srcDir))
  202. cs := C.CString(name)
  203. C.set_name_btrfs_ioctl_vol_args_v2(&args, cs)
  204. free(cs)
  205. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
  206. uintptr(unsafe.Pointer(&args)))
  207. if errno != 0 {
  208. return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
  209. }
  210. return nil
  211. }
  212. func isSubvolume(p string) (bool, error) {
  213. var bufStat unix.Stat_t
  214. if err := unix.Lstat(p, &bufStat); err != nil {
  215. return false, err
  216. }
  217. // return true if it is a btrfs subvolume
  218. return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
  219. }
  220. func subvolDelete(dirpath, name string, quotaEnabled bool) error {
  221. dir, err := openDir(dirpath)
  222. if err != nil {
  223. return err
  224. }
  225. defer closeDir(dir)
  226. fullPath := path.Join(dirpath, name)
  227. var args C.struct_btrfs_ioctl_vol_args
  228. // walk the btrfs subvolumes
  229. walkSubVolumes := func(p string, f os.DirEntry, err error) error {
  230. if err != nil {
  231. if os.IsNotExist(err) && p != fullPath {
  232. // missing most likely because the path was a subvolume that got removed in the previous iteration
  233. // since it's gone anyway, we don't care
  234. return nil
  235. }
  236. return fmt.Errorf("error walking subvolumes: %v", err)
  237. }
  238. // we want to check children only so skip itself
  239. // it will be removed after the filepath walk anyways
  240. if f.IsDir() && p != fullPath {
  241. sv, err := isSubvolume(p)
  242. if err != nil {
  243. return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
  244. }
  245. if sv {
  246. if err := subvolDelete(path.Dir(p), f.Name(), quotaEnabled); err != nil {
  247. return fmt.Errorf("Failed to destroy btrfs child subvolume (%s) of parent (%s): %v", p, dirpath, err)
  248. }
  249. }
  250. }
  251. return nil
  252. }
  253. if err := filepath.WalkDir(path.Join(dirpath, name), walkSubVolumes); err != nil {
  254. return fmt.Errorf("Recursively walking subvolumes for %s failed: %v", dirpath, err)
  255. }
  256. if quotaEnabled {
  257. if qgroupid, err := subvolLookupQgroup(fullPath); err == nil {
  258. var args C.struct_btrfs_ioctl_qgroup_create_args
  259. args.qgroupid = C.__u64(qgroupid)
  260. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_CREATE,
  261. uintptr(unsafe.Pointer(&args)))
  262. if errno != 0 {
  263. log.G(context.TODO()).WithField("storage-driver", "btrfs").Errorf("Failed to delete btrfs qgroup %v for %s: %v", qgroupid, fullPath, errno.Error())
  264. }
  265. } else {
  266. log.G(context.TODO()).WithField("storage-driver", "btrfs").Errorf("Failed to lookup btrfs qgroup for %s: %v", fullPath, err.Error())
  267. }
  268. }
  269. // all subvolumes have been removed
  270. // now remove the one originally passed in
  271. for i, c := range []byte(name) {
  272. args.name[i] = C.char(c)
  273. }
  274. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
  275. uintptr(unsafe.Pointer(&args)))
  276. if errno != 0 {
  277. return fmt.Errorf("Failed to destroy btrfs snapshot %s for %s: %v", dirpath, name, errno.Error())
  278. }
  279. return nil
  280. }
  281. func (d *Driver) updateQuotaStatus() {
  282. d.once.Do(func() {
  283. if !d.quotaEnabled {
  284. // In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
  285. if err := qgroupStatus(d.home); err != nil {
  286. // quota is still not enabled
  287. return
  288. }
  289. d.quotaEnabled = true
  290. }
  291. })
  292. }
  293. func (d *Driver) enableQuota() error {
  294. d.updateQuotaStatus()
  295. if d.quotaEnabled {
  296. return nil
  297. }
  298. dir, err := openDir(d.home)
  299. if err != nil {
  300. return err
  301. }
  302. defer closeDir(dir)
  303. var args C.struct_btrfs_ioctl_quota_ctl_args
  304. args.cmd = C.BTRFS_QUOTA_CTL_ENABLE
  305. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
  306. uintptr(unsafe.Pointer(&args)))
  307. if errno != 0 {
  308. return fmt.Errorf("Failed to enable btrfs quota for %s: %v", dir, errno.Error())
  309. }
  310. d.quotaEnabled = true
  311. return nil
  312. }
  313. func (d *Driver) subvolRescanQuota() error {
  314. d.updateQuotaStatus()
  315. if !d.quotaEnabled {
  316. return nil
  317. }
  318. dir, err := openDir(d.home)
  319. if err != nil {
  320. return err
  321. }
  322. defer closeDir(dir)
  323. var args C.struct_btrfs_ioctl_quota_rescan_args
  324. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_RESCAN_WAIT,
  325. uintptr(unsafe.Pointer(&args)))
  326. if errno != 0 {
  327. return fmt.Errorf("Failed to rescan btrfs quota for %s: %v", dir, errno.Error())
  328. }
  329. return nil
  330. }
  331. func subvolLimitQgroup(path string, size uint64) error {
  332. dir, err := openDir(path)
  333. if err != nil {
  334. return err
  335. }
  336. defer closeDir(dir)
  337. var args C.struct_btrfs_ioctl_qgroup_limit_args
  338. args.lim.max_rfer = C.__u64(size)
  339. args.lim.flags = C.BTRFS_QGROUP_LIMIT_MAX_RFER
  340. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_LIMIT,
  341. uintptr(unsafe.Pointer(&args)))
  342. if errno != 0 {
  343. return fmt.Errorf("Failed to limit qgroup for %s: %v", dir, errno.Error())
  344. }
  345. return nil
  346. }
  347. // qgroupStatus performs a BTRFS_IOC_TREE_SEARCH on the root path
  348. // with search key of BTRFS_QGROUP_STATUS_KEY.
  349. // In case qgroup is enabled, the retuned key type will match BTRFS_QGROUP_STATUS_KEY.
  350. // For more details please see https://github.com/kdave/btrfs-progs/blob/v4.9/qgroup.c#L1035
  351. func qgroupStatus(path string) error {
  352. dir, err := openDir(path)
  353. if err != nil {
  354. return err
  355. }
  356. defer closeDir(dir)
  357. var args C.struct_btrfs_ioctl_search_args
  358. args.key.tree_id = C.BTRFS_QUOTA_TREE_OBJECTID
  359. args.key.min_type = C.BTRFS_QGROUP_STATUS_KEY
  360. args.key.max_type = C.BTRFS_QGROUP_STATUS_KEY
  361. args.key.max_objectid = C.__u64(math.MaxUint64)
  362. args.key.max_offset = C.__u64(math.MaxUint64)
  363. args.key.max_transid = C.__u64(math.MaxUint64)
  364. args.key.nr_items = 4096
  365. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_TREE_SEARCH,
  366. uintptr(unsafe.Pointer(&args)))
  367. if errno != 0 {
  368. return fmt.Errorf("Failed to search qgroup for %s: %v", path, errno.Error())
  369. }
  370. sh := (*C.struct_btrfs_ioctl_search_header)(unsafe.Pointer(&args.buf))
  371. if sh._type != C.BTRFS_QGROUP_STATUS_KEY {
  372. return fmt.Errorf("Invalid qgroup search header type for %s: %v", path, sh._type)
  373. }
  374. return nil
  375. }
  376. func subvolLookupQgroup(path string) (uint64, error) {
  377. dir, err := openDir(path)
  378. if err != nil {
  379. return 0, err
  380. }
  381. defer closeDir(dir)
  382. var args C.struct_btrfs_ioctl_ino_lookup_args
  383. args.objectid = C.BTRFS_FIRST_FREE_OBJECTID
  384. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_INO_LOOKUP,
  385. uintptr(unsafe.Pointer(&args)))
  386. if errno != 0 {
  387. return 0, fmt.Errorf("Failed to lookup qgroup for %s: %v", dir, errno.Error())
  388. }
  389. if args.treeid == 0 {
  390. return 0, fmt.Errorf("Invalid qgroup id for %s: 0", dir)
  391. }
  392. return uint64(args.treeid), nil
  393. }
  394. func (d *Driver) subvolumesDir() string {
  395. return path.Join(d.home, "subvolumes")
  396. }
  397. func (d *Driver) subvolumesDirID(id string) string {
  398. return path.Join(d.subvolumesDir(), id)
  399. }
  400. func (d *Driver) quotasDir() string {
  401. return path.Join(d.home, "quotas")
  402. }
  403. func (d *Driver) quotasDirID(id string) string {
  404. return path.Join(d.quotasDir(), id)
  405. }
  406. // CreateReadWrite creates a layer that is writable for use as a container
  407. // file system.
  408. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  409. return d.Create(id, parent, opts)
  410. }
  411. // Create the filesystem with given id.
  412. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  413. quotas := path.Join(d.home, "quotas")
  414. subvolumes := path.Join(d.home, "subvolumes")
  415. root := d.idMap.RootPair()
  416. currentID := idtools.CurrentIdentity()
  417. dirID := idtools.Identity{
  418. UID: currentID.UID,
  419. GID: root.GID,
  420. }
  421. if err := idtools.MkdirAllAndChown(subvolumes, 0o710, dirID); err != nil {
  422. return err
  423. }
  424. if parent == "" {
  425. if err := subvolCreate(subvolumes, id); err != nil {
  426. return err
  427. }
  428. } else {
  429. parentDir := d.subvolumesDirID(parent)
  430. st, err := os.Stat(parentDir)
  431. if err != nil {
  432. return err
  433. }
  434. if !st.IsDir() {
  435. return fmt.Errorf("%s: not a directory", parentDir)
  436. }
  437. if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
  438. return err
  439. }
  440. }
  441. var storageOpt map[string]string
  442. if opts != nil {
  443. storageOpt = opts.StorageOpt
  444. }
  445. if _, ok := storageOpt["size"]; ok {
  446. driver := &Driver{}
  447. if err := d.parseStorageOpt(storageOpt, driver); err != nil {
  448. return err
  449. }
  450. if err := d.setStorageSize(path.Join(subvolumes, id), driver); err != nil {
  451. return err
  452. }
  453. if err := idtools.MkdirAllAndChown(quotas, 0o700, idtools.CurrentIdentity()); err != nil {
  454. return err
  455. }
  456. if err := os.WriteFile(path.Join(quotas, id), []byte(fmt.Sprint(driver.options.size)), 0o644); err != nil {
  457. return err
  458. }
  459. }
  460. // if we have a remapped root (user namespaces enabled), change the created snapshot
  461. // dir ownership to match
  462. if root.UID != 0 || root.GID != 0 {
  463. if err := root.Chown(path.Join(subvolumes, id)); err != nil {
  464. return err
  465. }
  466. }
  467. mountLabel := ""
  468. if opts != nil {
  469. mountLabel = opts.MountLabel
  470. }
  471. return label.Relabel(path.Join(subvolumes, id), mountLabel, false)
  472. }
  473. // Parse btrfs storage options
  474. func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
  475. // Read size to change the subvolume disk quota per container
  476. for key, val := range storageOpt {
  477. key := strings.ToLower(key)
  478. switch key {
  479. case "size":
  480. size, err := units.RAMInBytes(val)
  481. if err != nil {
  482. return err
  483. }
  484. driver.options.size = uint64(size)
  485. default:
  486. return fmt.Errorf("Unknown option %s", key)
  487. }
  488. }
  489. return nil
  490. }
  491. // Set btrfs storage size
  492. func (d *Driver) setStorageSize(dir string, driver *Driver) error {
  493. if driver.options.size == 0 {
  494. return fmt.Errorf("btrfs: invalid storage size: %s", units.HumanSize(float64(driver.options.size)))
  495. }
  496. if d.options.minSpace > 0 && driver.options.size < d.options.minSpace {
  497. return fmt.Errorf("btrfs: storage size cannot be less than %s", units.HumanSize(float64(d.options.minSpace)))
  498. }
  499. if err := d.enableQuota(); err != nil {
  500. return err
  501. }
  502. return subvolLimitQgroup(dir, driver.options.size)
  503. }
  504. // Remove the filesystem with given id.
  505. func (d *Driver) Remove(id string) error {
  506. dir := d.subvolumesDirID(id)
  507. if _, err := os.Stat(dir); err != nil {
  508. return err
  509. }
  510. quotasDir := d.quotasDirID(id)
  511. if _, err := os.Stat(quotasDir); err == nil {
  512. if err := os.Remove(quotasDir); err != nil {
  513. return err
  514. }
  515. } else if !os.IsNotExist(err) {
  516. return err
  517. }
  518. // Call updateQuotaStatus() to invoke status update
  519. d.updateQuotaStatus()
  520. if err := subvolDelete(d.subvolumesDir(), id, d.quotaEnabled); err != nil {
  521. if d.quotaEnabled {
  522. // use strings.Contains() rather than errors.Is(), because subvolDelete() does not use %w yet
  523. if userns.RunningInUserNS() && strings.Contains(err.Error(), "operation not permitted") {
  524. err = errors.Wrap(err, `failed to delete subvolume without root (hint: remount btrfs on "user_subvol_rm_allowed" option, or update the kernel to >= 4.18, or change the storage driver to "fuse-overlayfs")`)
  525. }
  526. return err
  527. }
  528. // If quota is not enabled, fallback to rmdir syscall to delete subvolumes.
  529. // This would allow unprivileged user to delete their owned subvolumes
  530. // in kernel >= 4.18 without user_subvol_rm_allowed mount option.
  531. //
  532. // From https://github.com/containers/storage/pull/508/commits/831e32b6bdcb530acc4c1cb9059d3c6dba14208c
  533. }
  534. if err := containerfs.EnsureRemoveAll(dir); err != nil {
  535. return err
  536. }
  537. return d.subvolRescanQuota()
  538. }
  539. // Get the requested filesystem id.
  540. func (d *Driver) Get(id, mountLabel string) (string, error) {
  541. dir := d.subvolumesDirID(id)
  542. st, err := os.Stat(dir)
  543. if err != nil {
  544. return "", err
  545. }
  546. if !st.IsDir() {
  547. return "", fmt.Errorf("%s: not a directory", dir)
  548. }
  549. if quota, err := os.ReadFile(d.quotasDirID(id)); err == nil {
  550. if size, err := strconv.ParseUint(string(quota), 10, 64); err == nil && size >= d.options.minSpace {
  551. if err := d.enableQuota(); err != nil {
  552. return "", err
  553. }
  554. if err := subvolLimitQgroup(dir, size); err != nil {
  555. return "", err
  556. }
  557. }
  558. }
  559. return dir, nil
  560. }
  561. // Put is not implemented for BTRFS as there is no cleanup required for the id.
  562. func (d *Driver) Put(id string) error {
  563. // Get() creates no runtime resources (like e.g. mounts)
  564. // so this doesn't need to do anything.
  565. return nil
  566. }
  567. // Exists checks if the id exists in the filesystem.
  568. func (d *Driver) Exists(id string) bool {
  569. dir := d.subvolumesDirID(id)
  570. _, err := os.Stat(dir)
  571. return err == nil
  572. }