btrfs.go 18 KB

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