btrfs.go 18 KB

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