btrfs.go 18 KB

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