btrfs.go 17 KB

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