btrfs.go 18 KB

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