btrfs.go 18 KB

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