1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <linux/btrfs_tree.h>
8 #include <linux/loop.h>
9 #include <linux/magic.h>
13 #include <sys/ioctl.h>
14 #include <sys/sysmacros.h>
17 #include "alloc-util.h"
18 #include "blockdev-util.h"
19 #include "btrfs-util.h"
20 #include "chattr-util.h"
27 #include "path-util.h"
29 #include "smack-util.h"
30 #include "sparse-endian.h"
31 #include "stat-util.h"
32 #include "string-util.h"
33 #include "time-util.h"
35 /* WARNING: Be careful with file system ioctls! When we get an fd, we
36 * need to make sure it either refers to only a regular file or
37 * directory, or that it is located on btrfs, before invoking any
38 * btrfs ioctls. The ioctl numbers are reused by some device drivers
39 * (such as DRM), and hence might have bad effects when invoked on
40 * device nodes (that reference drivers) rather than fds to normal
41 * files or directories. */
43 static int validate_subvolume_name(const char *name
) {
45 if (!filename_is_valid(name
))
48 if (strlen(name
) > BTRFS_SUBVOL_NAME_MAX
)
54 static int extract_subvolume_name(const char *path
, const char **subvolume
) {
63 r
= validate_subvolume_name(fn
);
71 int btrfs_is_subvol_fd(int fd
) {
76 /* On btrfs subvolumes always have the inode 256 */
78 if (fstat(fd
, &st
) < 0)
81 if (!btrfs_might_be_subvol(&st
))
84 return fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
87 int btrfs_is_subvol(const char *path
) {
88 _cleanup_close_
int fd
= -1;
92 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
96 return btrfs_is_subvol_fd(fd
);
99 int btrfs_subvol_make_fd(int fd
, const char *subvolume
) {
100 struct btrfs_ioctl_vol_args args
= {};
101 _cleanup_close_
int real_fd
= -1;
106 r
= validate_subvolume_name(subvolume
);
110 r
= fcntl(fd
, F_GETFL
);
113 if (FLAGS_SET(r
, O_PATH
)) {
114 /* An O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal with
117 real_fd
= fd_reopen(fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
124 strncpy(args
.name
, subvolume
, sizeof(args
.name
)-1);
126 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_SUBVOL_CREATE
, &args
));
129 int btrfs_subvol_make(const char *path
) {
130 _cleanup_close_
int fd
= -1;
131 const char *subvolume
;
136 r
= extract_subvolume_name(path
, &subvolume
);
140 fd
= open_parent(path
, O_CLOEXEC
, 0);
144 return btrfs_subvol_make_fd(fd
, subvolume
);
147 int btrfs_subvol_make_fallback(const char *path
, mode_t mode
) {
148 mode_t old
, combined
;
153 /* Let's work like mkdir(), i.e. take the specified mode, and mask it with the current umask. */
155 combined
= old
| ~mode
;
156 if (combined
!= ~mode
)
158 r
= btrfs_subvol_make(path
);
162 return 1; /* subvol worked */
166 if (mkdir(path
, mode
) < 0)
169 return 0; /* plain directory */
172 int btrfs_subvol_set_read_only_fd(int fd
, bool b
) {
173 uint64_t flags
, nflags
;
178 if (fstat(fd
, &st
) < 0)
181 if (!btrfs_might_be_subvol(&st
))
184 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
187 nflags
= UPDATE_FLAG(flags
, BTRFS_SUBVOL_RDONLY
, b
);
191 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &nflags
));
194 int btrfs_subvol_set_read_only(const char *path
, bool b
) {
195 _cleanup_close_
int fd
= -1;
197 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
201 return btrfs_subvol_set_read_only_fd(fd
, b
);
204 int btrfs_subvol_get_read_only_fd(int fd
) {
210 if (fstat(fd
, &st
) < 0)
213 if (!btrfs_might_be_subvol(&st
))
216 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
219 return !!(flags
& BTRFS_SUBVOL_RDONLY
);
222 int btrfs_reflink(int infd
, int outfd
) {
228 /* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
230 r
= fd_verify_regular(outfd
);
234 return RET_NERRNO(ioctl(outfd
, BTRFS_IOC_CLONE
, infd
));
237 int btrfs_clone_range(int infd
, uint64_t in_offset
, int outfd
, uint64_t out_offset
, uint64_t sz
) {
238 struct btrfs_ioctl_clone_range_args args
= {
240 .src_offset
= in_offset
,
242 .dest_offset
= out_offset
,
250 r
= fd_verify_regular(outfd
);
254 return RET_NERRNO(ioctl(outfd
, BTRFS_IOC_CLONE_RANGE
, &args
));
257 int btrfs_get_block_device_fd(int fd
, dev_t
*dev
) {
258 struct btrfs_ioctl_fs_info_args fsi
= {};
265 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
271 if (ioctl(fd
, BTRFS_IOC_FS_INFO
, &fsi
) < 0)
274 /* We won't do this for btrfs RAID */
275 if (fsi
.num_devices
!= 1) {
280 for (id
= 1; id
<= fsi
.max_id
; id
++) {
281 struct btrfs_ioctl_dev_info_args di
= {
286 if (ioctl(fd
, BTRFS_IOC_DEV_INFO
, &di
) < 0) {
293 /* For the root fs — when no initrd is involved — btrfs returns /dev/root on any kernels from
294 * the past few years. That sucks, as we have no API to determine the actual root then. let's
295 * return an recognizable error for this case, so that the caller can maybe print a nice
296 * message about this.
298 * https://bugzilla.kernel.org/show_bug.cgi?id=89721 */
299 if (path_equal((char*) di
.path
, "/dev/root"))
302 if (stat((char*) di
.path
, &st
) < 0)
305 if (!S_ISBLK(st
.st_mode
))
308 if (major(st
.st_rdev
) == 0)
318 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
319 _cleanup_close_
int fd
= -1;
324 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
328 return btrfs_get_block_device_fd(fd
, dev
);
331 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
332 struct btrfs_ioctl_ino_lookup_args args
= {
333 .objectid
= BTRFS_FIRST_FREE_OBJECTID
340 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
346 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
353 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
354 _cleanup_close_
int subvol_fd
= -1;
359 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
363 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
366 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
369 /* the objectid, type, offset together make up the btrfs key,
370 * which is considered a single 136byte integer when
371 * comparing. This call increases the counter by one, dealing
372 * with the overflow between the overflows */
374 if (args
->key
.min_offset
< UINT64_MAX
) {
375 args
->key
.min_offset
++;
379 if (args
->key
.min_type
< UINT8_MAX
) {
380 args
->key
.min_type
++;
381 args
->key
.min_offset
= 0;
385 if (args
->key
.min_objectid
< UINT64_MAX
) {
386 args
->key
.min_objectid
++;
387 args
->key
.min_offset
= 0;
388 args
->key
.min_type
= 0;
395 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
399 args
->key
.min_objectid
= h
->objectid
;
400 args
->key
.min_type
= h
->type
;
401 args
->key
.min_offset
= h
->offset
;
404 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
409 /* Compare min and max */
411 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
415 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
419 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
422 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
424 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
425 (i) < (args).key.nr_items; \
427 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
429 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
430 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
432 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
433 struct btrfs_ioctl_search_args args
= {
434 /* Tree of tree roots */
435 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
437 /* Look precisely for the subvolume items */
438 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
439 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
442 .key
.max_offset
= UINT64_MAX
,
444 /* No restrictions on the other components */
445 .key
.min_transid
= 0,
446 .key
.max_transid
= UINT64_MAX
,
455 if (subvol_id
== 0) {
456 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
460 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
467 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
469 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
470 const struct btrfs_ioctl_search_header
*sh
;
473 args
.key
.nr_items
= 256;
474 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
477 if (args
.key
.nr_items
<= 0)
480 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
482 const struct btrfs_root_item
*ri
;
484 /* Make sure we start the next search at least from this entry */
485 btrfs_ioctl_search_args_set(&args
, sh
);
487 if (sh
->objectid
!= subvol_id
)
489 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
492 /* Older versions of the struct lacked the otime setting */
493 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
496 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
498 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
499 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
501 ret
->subvol_id
= subvol_id
;
502 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
504 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
505 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
506 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
512 /* Increase search key by one, to read the next item, if we can. */
513 if (!btrfs_ioctl_search_args_inc(&args
))
524 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
526 struct btrfs_ioctl_search_args args
= {
527 /* Tree of quota items */
528 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
530 /* The object ID is always 0 */
531 .key
.min_objectid
= 0,
532 .key
.max_objectid
= 0,
534 /* Look precisely for the quota items */
535 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
536 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
538 /* No restrictions on the other components */
539 .key
.min_transid
= 0,
540 .key
.max_transid
= UINT64_MAX
,
543 bool found_info
= false, found_limit
= false;
550 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
554 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
561 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
563 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
564 const struct btrfs_ioctl_search_header
*sh
;
567 args
.key
.nr_items
= 256;
568 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
569 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
575 if (args
.key
.nr_items
<= 0)
578 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
580 /* Make sure we start the next search at least from this entry */
581 btrfs_ioctl_search_args_set(&args
, sh
);
583 if (sh
->objectid
!= 0)
585 if (sh
->offset
!= qgroupid
)
588 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
589 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
591 ret
->referenced
= le64toh(qii
->rfer
);
592 ret
->exclusive
= le64toh(qii
->excl
);
596 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
597 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
599 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
600 ret
->referenced_max
= le64toh(qli
->max_rfer
);
602 ret
->referenced_max
= UINT64_MAX
;
604 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
605 ret
->exclusive_max
= le64toh(qli
->max_excl
);
607 ret
->exclusive_max
= UINT64_MAX
;
612 if (found_info
&& found_limit
)
616 /* Increase search key by one, to read the next item, if we can. */
617 if (!btrfs_ioctl_search_args_inc(&args
))
622 if (!found_limit
&& !found_info
)
626 ret
->referenced
= UINT64_MAX
;
627 ret
->exclusive
= UINT64_MAX
;
631 ret
->referenced_max
= UINT64_MAX
;
632 ret
->exclusive_max
= UINT64_MAX
;
638 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
639 _cleanup_close_
int fd
= -1;
641 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
645 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
648 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
649 uint64_t level
, lowest
= UINT64_MAX
, lowest_qgroupid
= 0;
650 _cleanup_free_
uint64_t *qgroups
= NULL
;
656 /* This finds the "subtree" qgroup for a specific
657 * subvolume. This only works for subvolumes that have been
658 * prepared with btrfs_subvol_auto_qgroup_fd() with
659 * insert_intermediary_qgroup=true (or equivalent). For others
660 * it will return the leaf qgroup instead. The two cases may
661 * be distinguished via the return value, which is 1 in case
662 * an appropriate "subtree" qgroup was found, and 0
665 if (subvol_id
== 0) {
666 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
671 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
674 if (level
!= 0) /* Input must be a leaf qgroup */
677 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
681 for (int i
= 0; i
< n
; i
++) {
684 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
691 if (lowest
== UINT64_MAX
|| level
< lowest
) {
692 lowest_qgroupid
= qgroups
[i
];
697 if (lowest
== UINT64_MAX
) {
698 /* No suitable higher-level qgroup found, let's return
699 * the leaf qgroup instead, and indicate that with the
706 *ret
= lowest_qgroupid
;
710 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
717 /* This determines the quota data of the qgroup with the
718 * lowest level, that shares the id part with the specified
719 * subvolume. This is useful for determining the quota data
720 * for entire subvolume subtrees, as long as the subtrees have
721 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
724 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
728 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
731 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
732 _cleanup_close_
int fd
= -1;
734 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
738 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
741 int btrfs_defrag_fd(int fd
) {
746 r
= fd_verify_regular(fd
);
750 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
));
753 int btrfs_defrag(const char *p
) {
754 _cleanup_close_
int fd
= -1;
756 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
760 return btrfs_defrag_fd(fd
);
763 int btrfs_quota_enable_fd(int fd
, bool b
) {
764 struct btrfs_ioctl_quota_ctl_args args
= {
765 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
771 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
777 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
));
780 int btrfs_quota_enable(const char *path
, bool b
) {
781 _cleanup_close_
int fd
= -1;
783 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
787 return btrfs_quota_enable_fd(fd
, b
);
790 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
792 struct btrfs_ioctl_qgroup_limit_args args
= {
793 .lim
.max_rfer
= referenced_max
,
794 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
801 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
805 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
812 args
.qgroupid
= qgroupid
;
814 for (unsigned c
= 0;; c
++) {
815 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
817 if (errno
== EBUSY
&& c
< 10) {
818 (void) btrfs_quota_scan_wait(fd
);
831 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, uint64_t referenced_max
) {
832 _cleanup_close_
int fd
= -1;
834 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
838 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
841 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
847 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
851 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
854 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
855 _cleanup_close_
int fd
= -1;
857 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
861 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
864 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
867 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
870 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
873 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
877 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
881 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
884 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
889 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
891 struct btrfs_ioctl_qgroup_create_args args
= {
893 .qgroupid
= qgroupid
,
897 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
903 for (unsigned c
= 0;; c
++) {
904 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
906 /* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
907 * ENOTCONN. Let's always convert this to ENOTCONN to make this recognizable
908 * everywhere the same way. */
910 if (IN_SET(errno
, EINVAL
, ENOTCONN
))
913 if (errno
== EBUSY
&& c
< 10) {
914 (void) btrfs_quota_scan_wait(fd
);
927 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
928 return qgroup_create_or_destroy(fd
, true, qgroupid
);
931 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
932 return qgroup_create_or_destroy(fd
, false, qgroupid
);
935 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
936 _cleanup_free_
uint64_t *qgroups
= NULL
;
940 /* Destroys the specified qgroup, but unassigns it from all
941 * its parents first. Also, it recursively destroys all
942 * qgroups it is assigned to that have the same id part of the
943 * qgroupid as the specified group. */
945 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
949 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
953 for (int i
= 0; i
< n
; i
++) {
956 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
960 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
967 /* The parent qgroupid shares the same id part with
968 * us? If so, destroy it too. */
970 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
973 return btrfs_qgroup_destroy(fd
, qgroupid
);
976 int btrfs_quota_scan_start(int fd
) {
977 struct btrfs_ioctl_quota_rescan_args args
= {};
981 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
));
984 int btrfs_quota_scan_wait(int fd
) {
987 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
));
990 int btrfs_quota_scan_ongoing(int fd
) {
991 struct btrfs_ioctl_quota_rescan_args args
= {};
995 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
1001 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
1002 struct btrfs_ioctl_qgroup_assign_args args
= {
1009 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1015 for (unsigned c
= 0;; c
++) {
1016 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1018 if (errno
== EBUSY
&& c
< 10) {
1019 (void) btrfs_quota_scan_wait(fd
);
1029 /* If the return value is > 0, we need to request a rescan */
1031 (void) btrfs_quota_scan_start(fd
);
1036 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1037 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1040 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1041 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1044 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1045 struct btrfs_ioctl_search_args args
= {
1046 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1048 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1049 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1051 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1052 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1054 .key
.min_transid
= 0,
1055 .key
.max_transid
= UINT64_MAX
,
1058 struct btrfs_ioctl_vol_args vol_args
= {};
1059 _cleanup_close_
int subvol_fd
= -1;
1061 bool made_writable
= false;
1067 if (fstat(fd
, &st
) < 0)
1070 if (!S_ISDIR(st
.st_mode
))
1073 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1077 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1078 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1079 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1080 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1081 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1082 * let's prefer ENOTTY over EPERM/EACCES though. */
1083 r
= btrfs_is_subvol_fd(subvol_fd
);
1086 if (r
== 0) /* Not a btrfs subvolume */
1089 if (subvol_id
== 0) {
1090 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1095 /* First, try to remove the subvolume. If it happens to be
1096 * already empty, this will just work. */
1097 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1098 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1099 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1102 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1105 /* OK, the subvolume is not empty, let's look for child
1106 * subvolumes, and remove them, first */
1108 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1110 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1111 const struct btrfs_ioctl_search_header
*sh
;
1114 args
.key
.nr_items
= 256;
1115 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1118 if (args
.key
.nr_items
<= 0)
1121 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1122 _cleanup_free_
char *p
= NULL
;
1123 const struct btrfs_root_ref
*ref
;
1125 btrfs_ioctl_search_args_set(&args
, sh
);
1127 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1129 if (sh
->offset
!= subvol_id
)
1132 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1134 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1138 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1139 .treeid
= subvol_id
,
1140 .objectid
= htole64(ref
->dirid
),
1143 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1146 if (!made_writable
) {
1147 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1151 made_writable
= true;
1154 if (isempty(ino_args
.name
))
1155 /* Subvolume is in the top-level
1156 * directory of the subvolume. */
1157 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1159 _cleanup_close_
int child_fd
= -1;
1161 /* Subvolume is somewhere further down,
1162 * hence we need to open the
1163 * containing directory first */
1165 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1169 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1175 /* Increase search key by one, to read the next item, if we can. */
1176 if (!btrfs_ioctl_search_args_inc(&args
))
1180 /* OK, the child subvolumes should all be gone now, let's try
1181 * again to remove the subvolume */
1182 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1185 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1189 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1190 _cleanup_close_
int fd
= -1;
1191 const char *subvolume
;
1196 r
= extract_subvolume_name(path
, &subvolume
);
1200 fd
= open_parent(path
, O_CLOEXEC
, 0);
1204 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1207 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1208 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1211 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1213 struct btrfs_ioctl_search_args args
= {
1214 /* Tree of quota items */
1215 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1217 /* The object ID is always 0 */
1218 .key
.min_objectid
= 0,
1219 .key
.max_objectid
= 0,
1221 /* Look precisely for the quota items */
1222 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1223 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1225 /* For our qgroup */
1226 .key
.min_offset
= old_qgroupid
,
1227 .key
.max_offset
= old_qgroupid
,
1229 /* No restrictions on the other components */
1230 .key
.min_transid
= 0,
1231 .key
.max_transid
= UINT64_MAX
,
1236 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1242 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1243 const struct btrfs_ioctl_search_header
*sh
;
1246 args
.key
.nr_items
= 256;
1247 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1248 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1254 if (args
.key
.nr_items
<= 0)
1257 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1258 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1259 struct btrfs_ioctl_qgroup_limit_args qargs
;
1262 /* Make sure we start the next search at least from this entry */
1263 btrfs_ioctl_search_args_set(&args
, sh
);
1265 if (sh
->objectid
!= 0)
1267 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1269 if (sh
->offset
!= old_qgroupid
)
1272 /* We found the entry, now copy things over. */
1274 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1275 .qgroupid
= new_qgroupid
,
1277 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1278 .lim
.max_excl
= le64toh(qli
->max_excl
),
1279 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1280 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1282 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1283 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1284 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1285 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1289 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1290 if (errno
== EBUSY
&& c
< 10) {
1291 (void) btrfs_quota_scan_wait(fd
);
1303 /* Increase search key by one, to read the next item, if we can. */
1304 if (!btrfs_ioctl_search_args_inc(&args
))
1311 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1312 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1313 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1314 int n_old_qgroups
, n_old_parent_qgroups
, r
;
1315 uint64_t old_parent_id
;
1319 /* Copies a reduced form of quota information from the old to
1320 * the new subvolume. */
1322 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1323 if (n_old_qgroups
<= 0) /* Nothing to copy */
1324 return n_old_qgroups
;
1326 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1328 /* We have no parent, hence nothing to copy. */
1329 n_old_parent_qgroups
= 0;
1333 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1334 if (n_old_parent_qgroups
< 0)
1335 return n_old_parent_qgroups
;
1338 for (int i
= 0; i
< n_old_qgroups
; i
++) {
1341 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1345 if (id
== old_subvol_id
) {
1346 /* The old subvolume was member of a qgroup
1347 * that had the same id, but a different level
1348 * as it self. Let's set up something similar
1349 * in the destination. */
1350 insert_intermediary_qgroup
= true;
1354 for (int j
= 0; j
< n_old_parent_qgroups
; j
++)
1355 if (old_parent_qgroups
[j
] == old_qgroups
[i
])
1356 /* The old subvolume shared a common
1357 * parent qgroup with its parent
1358 * subvolume. Let's set up something
1359 * similar in the destination. */
1360 copy_from_parent
= true;
1363 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1366 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1369 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1370 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1374 /* First copy the leaf limits */
1375 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1380 /* Then, try to copy the subtree limits, if there are any. */
1381 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1387 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1393 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1400 static int subvol_snapshot_children(
1403 const char *subvolume
,
1404 uint64_t old_subvol_id
,
1405 BtrfsSnapshotFlags flags
) {
1407 struct btrfs_ioctl_search_args args
= {
1408 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1410 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1411 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1413 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1414 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1416 .key
.min_transid
= 0,
1417 .key
.max_transid
= UINT64_MAX
,
1420 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1421 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1424 _cleanup_close_
int subvolume_fd
= -1;
1425 uint64_t new_subvol_id
;
1428 assert(old_fd
>= 0);
1429 assert(new_fd
>= 0);
1432 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1434 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1437 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1438 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1441 if (old_subvol_id
== 0) {
1442 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1447 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1451 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1452 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1454 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1456 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1457 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1462 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1464 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1465 const struct btrfs_ioctl_search_header
*sh
;
1468 args
.key
.nr_items
= 256;
1469 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1472 if (args
.key
.nr_items
<= 0)
1475 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1476 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1477 const struct btrfs_root_ref
*ref
;
1478 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1480 btrfs_ioctl_search_args_set(&args
, sh
);
1482 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1485 /* Avoid finding the source subvolume a second
1487 if (sh
->offset
!= old_subvol_id
)
1490 /* Avoid running into loops if the new
1491 * subvolume is below the old one. */
1492 if (sh
->objectid
== new_subvol_id
)
1495 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1496 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1500 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1501 .treeid
= old_subvol_id
,
1502 .objectid
= htole64(ref
->dirid
),
1505 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1508 c
= path_join(ino_args
.name
, p
);
1512 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1513 if (old_child_fd
< 0)
1516 np
= path_join(subvolume
, ino_args
.name
);
1520 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1521 if (new_child_fd
< 0)
1524 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1525 /* If the snapshot is read-only we
1526 * need to mark it writable
1527 * temporarily, to put the subsnapshot
1530 if (subvolume_fd
< 0) {
1531 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1532 if (subvolume_fd
< 0)
1536 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1541 /* When btrfs clones the subvolumes, child
1542 * subvolumes appear as empty directories. Remove
1543 * them, so that we can create a new snapshot
1545 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1548 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1549 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1554 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1556 /* Restore the readonly flag */
1557 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1560 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1561 if (r
>= 0 && k
< 0)
1569 /* Increase search key by one, to read the next item, if we can. */
1570 if (!btrfs_ioctl_search_args_inc(&args
))
1574 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1575 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1580 int btrfs_subvol_snapshot_fd_full(
1582 const char *new_path
,
1583 BtrfsSnapshotFlags flags
,
1584 copy_progress_path_t progress_path
,
1585 copy_progress_bytes_t progress_bytes
,
1588 _cleanup_close_
int new_fd
= -1;
1589 const char *subvolume
;
1592 assert(old_fd
>= 0);
1595 r
= btrfs_is_subvol_fd(old_fd
);
1599 bool plain_directory
= false;
1601 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1602 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1605 r
= btrfs_subvol_make(new_path
);
1606 if (ERRNO_IS_NOT_SUPPORTED(r
) && (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1607 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1608 if (mkdir(new_path
, 0755) < 0)
1611 plain_directory
= true;
1615 r
= copy_directory_fd_full(
1622 (FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGINT
) ? COPY_SIGINT
: 0)|
1623 (FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGTERM
) ? COPY_SIGTERM
: 0),
1630 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1632 if (plain_directory
) {
1633 /* Plain directories have no recursive read-only flag, but something pretty close to
1634 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1636 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1637 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1639 r
= btrfs_subvol_set_read_only(new_path
, true);
1648 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1652 r
= extract_subvolume_name(new_path
, &subvolume
);
1656 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1660 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1663 int btrfs_subvol_snapshot_full(
1664 const char *old_path
,
1665 const char *new_path
,
1666 BtrfsSnapshotFlags flags
,
1667 copy_progress_path_t progress_path
,
1668 copy_progress_bytes_t progress_bytes
,
1671 _cleanup_close_
int old_fd
= -1;
1676 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1680 return btrfs_subvol_snapshot_fd_full(old_fd
, new_path
, flags
, progress_path
, progress_bytes
, userdata
);
1683 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1685 struct btrfs_ioctl_search_args args
= {
1686 /* Tree of quota items */
1687 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1689 /* Look precisely for the quota relation items */
1690 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1691 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1693 /* No restrictions on the other components */
1694 .key
.min_offset
= 0,
1695 .key
.max_offset
= UINT64_MAX
,
1697 .key
.min_transid
= 0,
1698 .key
.max_transid
= UINT64_MAX
,
1701 _cleanup_free_
uint64_t *items
= NULL
;
1708 if (qgroupid
== 0) {
1709 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1713 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1720 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1722 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1723 const struct btrfs_ioctl_search_header
*sh
;
1726 args
.key
.nr_items
= 256;
1727 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1728 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1734 if (args
.key
.nr_items
<= 0)
1737 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1739 /* Make sure we start the next search at least from this entry */
1740 btrfs_ioctl_search_args_set(&args
, sh
);
1742 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1744 if (sh
->offset
< sh
->objectid
)
1746 if (sh
->objectid
!= qgroupid
)
1749 if (!GREEDY_REALLOC(items
, n_items
+1))
1752 items
[n_items
++] = sh
->offset
;
1755 /* Increase search key by one, to read the next item, if we can. */
1756 if (!btrfs_ioctl_search_args_inc(&args
))
1765 *ret
= TAKE_PTR(items
);
1767 return (int) n_items
;
1770 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1771 _cleanup_free_
uint64_t *qgroups
= NULL
;
1772 uint64_t parent_subvol
;
1773 bool changed
= false;
1779 * Sets up the specified subvolume's qgroup automatically in
1782 * If insert_intermediary_qgroup is false, the subvolume's
1783 * leaf qgroup will be assigned to the same parent qgroups as
1784 * the subvolume's parent subvolume.
1786 * If insert_intermediary_qgroup is true a new intermediary
1787 * higher-level qgroup is created, with a higher level number,
1788 * but reusing the id of the subvolume. The level number is
1789 * picked as one smaller than the lowest level qgroup the
1790 * parent subvolume is a member of. If the parent subvolume's
1791 * leaf qgroup is assigned to no higher-level qgroup a new
1792 * qgroup of level 255 is created instead. Either way, the new
1793 * qgroup is then assigned to the parent's higher-level
1794 * qgroup, and the subvolume itself is assigned to it.
1796 * If the subvolume is already assigned to a higher level
1797 * qgroup, no operation is executed.
1799 * Effectively this means: regardless if
1800 * insert_intermediary_qgroup is true or not, after this
1801 * function is invoked the subvolume will be accounted within
1802 * the same qgroups as the parent. However, if it is true, it
1803 * will also get its own higher-level qgroup, which may in
1804 * turn be used by subvolumes created beneath this subvolume
1807 * This hence defines a simple default qgroup setup for
1808 * subvolumes, as long as this function is invoked on each
1809 * created subvolume: each subvolume is always accounting
1810 * together with its immediate parents. Optionally, if
1811 * insert_intermediary_qgroup is true, it will also get a
1812 * qgroup that then includes all its own child subvolumes.
1815 if (subvol_id
== 0) {
1816 r
= btrfs_is_subvol_fd(fd
);
1822 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1827 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1830 if (n
> 0) /* already parent qgroups set up, let's bail */
1833 qgroups
= mfree(qgroups
);
1835 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1837 /* No parent, hence no qgroup memberships */
1842 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1847 if (insert_intermediary_qgroup
) {
1848 uint64_t lowest
= 256, new_qgroupid
;
1849 bool created
= false;
1851 /* Determine the lowest qgroup that the parent
1852 * subvolume is assigned to. */
1854 for (int i
= 0; i
< n
; i
++) {
1857 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, NULL
);
1865 if (lowest
<= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1868 r
= btrfs_qgroupid_make(lowest
- 1, subvol_id
, &new_qgroupid
);
1872 /* Create the new intermediary group, unless it already exists */
1873 r
= btrfs_qgroup_create(fd
, new_qgroupid
);
1874 if (r
< 0 && r
!= -EEXIST
)
1877 changed
= created
= true;
1879 for (int i
= 0; i
< n
; i
++) {
1880 r
= btrfs_qgroup_assign(fd
, new_qgroupid
, qgroups
[i
]);
1881 if (r
< 0 && r
!= -EEXIST
) {
1883 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1891 r
= btrfs_qgroup_assign(fd
, subvol_id
, new_qgroupid
);
1892 if (r
< 0 && r
!= -EEXIST
) {
1894 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1903 /* Assign our subvolume to all the same qgroups as the parent */
1905 for (i
= 0; i
< n
; i
++) {
1906 r
= btrfs_qgroup_assign(fd
, subvol_id
, qgroups
[i
]);
1907 if (r
< 0 && r
!= -EEXIST
)
1917 int btrfs_subvol_auto_qgroup(const char *path
, uint64_t subvol_id
, bool create_intermediary_qgroup
) {
1918 _cleanup_close_
int fd
= -1;
1920 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1924 return btrfs_subvol_auto_qgroup_fd(fd
, subvol_id
, create_intermediary_qgroup
);
1927 int btrfs_subvol_get_parent(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
1929 struct btrfs_ioctl_search_args args
= {
1930 /* Tree of tree roots */
1931 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1933 /* Look precisely for the subvolume items */
1934 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1935 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1937 /* No restrictions on the other components */
1938 .key
.min_offset
= 0,
1939 .key
.max_offset
= UINT64_MAX
,
1941 .key
.min_transid
= 0,
1942 .key
.max_transid
= UINT64_MAX
,
1949 if (subvol_id
== 0) {
1950 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1954 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1961 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
1963 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1964 const struct btrfs_ioctl_search_header
*sh
;
1967 args
.key
.nr_items
= 256;
1968 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1969 return negative_errno();
1971 if (args
.key
.nr_items
<= 0)
1974 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1976 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1978 if (sh
->objectid
!= subvol_id
)