1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
22 #include "device-nodes.h"
28 #include "path-util.h"
30 #include "smack-util.h"
31 #include "sparse-endian.h"
32 #include "stat-util.h"
33 #include "string-util.h"
34 #include "time-util.h"
37 /* WARNING: Be careful with file system ioctls! When we get an fd, we
38 * need to make sure it either refers to only a regular file or
39 * directory, or that it is located on btrfs, before invoking any
40 * btrfs ioctls. The ioctl numbers are reused by some device drivers
41 * (such as DRM), and hence might have bad effects when invoked on
42 * device nodes (that reference drivers) rather than fds to normal
43 * files or directories. */
45 static int validate_subvolume_name(const char *name
) {
47 if (!filename_is_valid(name
))
50 if (strlen(name
) > BTRFS_SUBVOL_NAME_MAX
)
56 static int extract_subvolume_name(const char *path
, const char **subvolume
) {
65 r
= validate_subvolume_name(fn
);
73 int btrfs_is_filesystem(int fd
) {
78 if (fstatfs(fd
, &sfs
) < 0)
81 return F_TYPE_EQUAL(sfs
.f_type
, BTRFS_SUPER_MAGIC
);
84 int btrfs_is_subvol_fd(int fd
) {
89 /* On btrfs subvolumes always have the inode 256 */
91 if (fstat(fd
, &st
) < 0)
94 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
97 return btrfs_is_filesystem(fd
);
100 int btrfs_is_subvol(const char *path
) {
101 _cleanup_close_
int fd
= -1;
105 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
109 return btrfs_is_subvol_fd(fd
);
112 int btrfs_subvol_make_fd(int fd
, const char *subvolume
) {
113 struct btrfs_ioctl_vol_args args
= {};
114 _cleanup_close_
int real_fd
= -1;
119 r
= validate_subvolume_name(subvolume
);
123 r
= fcntl(fd
, F_GETFL
);
126 if (FLAGS_SET(r
, O_PATH
)) {
127 /* An O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal with
130 real_fd
= fd_reopen(fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
137 strncpy(args
.name
, subvolume
, sizeof(args
.name
)-1);
139 if (ioctl(fd
, BTRFS_IOC_SUBVOL_CREATE
, &args
) < 0)
145 int btrfs_subvol_make(const char *path
) {
146 _cleanup_close_
int fd
= -1;
147 const char *subvolume
;
152 r
= extract_subvolume_name(path
, &subvolume
);
156 fd
= open_parent(path
, O_CLOEXEC
, 0);
160 return btrfs_subvol_make_fd(fd
, subvolume
);
163 int btrfs_subvol_make_fallback(const char *path
, mode_t mode
) {
164 mode_t old
, combined
;
169 /* Let's work like mkdir(), i.e. take the specified mode, and mask it with the current umask. */
171 combined
= old
| ~mode
;
172 if (combined
!= ~mode
)
174 r
= btrfs_subvol_make(path
);
178 return 1; /* subvol worked */
182 if (mkdir(path
, mode
) < 0)
185 return 0; /* plain directory */
188 int btrfs_subvol_set_read_only_fd(int fd
, bool b
) {
189 uint64_t flags
, nflags
;
194 if (fstat(fd
, &st
) < 0)
197 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
200 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
203 nflags
= UPDATE_FLAG(flags
, BTRFS_SUBVOL_RDONLY
, b
);
207 if (ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &nflags
) < 0)
213 int btrfs_subvol_set_read_only(const char *path
, bool b
) {
214 _cleanup_close_
int fd
= -1;
216 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
220 return btrfs_subvol_set_read_only_fd(fd
, b
);
223 int btrfs_subvol_get_read_only_fd(int fd
) {
229 if (fstat(fd
, &st
) < 0)
232 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
235 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
238 return !!(flags
& BTRFS_SUBVOL_RDONLY
);
241 int btrfs_reflink(int infd
, int outfd
) {
247 /* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
249 r
= fd_verify_regular(outfd
);
253 if (ioctl(outfd
, BTRFS_IOC_CLONE
, infd
) < 0)
259 int btrfs_clone_range(int infd
, uint64_t in_offset
, int outfd
, uint64_t out_offset
, uint64_t sz
) {
260 struct btrfs_ioctl_clone_range_args args
= {
262 .src_offset
= in_offset
,
264 .dest_offset
= out_offset
,
272 r
= fd_verify_regular(outfd
);
276 if (ioctl(outfd
, BTRFS_IOC_CLONE_RANGE
, &args
) < 0)
282 int btrfs_get_block_device_fd(int fd
, dev_t
*dev
) {
283 struct btrfs_ioctl_fs_info_args fsi
= {};
290 r
= btrfs_is_filesystem(fd
);
296 if (ioctl(fd
, BTRFS_IOC_FS_INFO
, &fsi
) < 0)
299 /* We won't do this for btrfs RAID */
300 if (fsi
.num_devices
!= 1) {
305 for (id
= 1; id
<= fsi
.max_id
; id
++) {
306 struct btrfs_ioctl_dev_info_args di
= {
311 if (ioctl(fd
, BTRFS_IOC_DEV_INFO
, &di
) < 0) {
318 if (stat((char*) di
.path
, &st
) < 0)
321 if (!S_ISBLK(st
.st_mode
))
324 if (major(st
.st_rdev
) == 0)
334 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
335 _cleanup_close_
int fd
= -1;
340 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
344 return btrfs_get_block_device_fd(fd
, dev
);
347 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
348 struct btrfs_ioctl_ino_lookup_args args
= {
349 .objectid
= BTRFS_FIRST_FREE_OBJECTID
356 r
= btrfs_is_filesystem(fd
);
362 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
369 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
370 _cleanup_close_
int subvol_fd
= -1;
375 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
379 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
382 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
385 /* the objectid, type, offset together make up the btrfs key,
386 * which is considered a single 136byte integer when
387 * comparing. This call increases the counter by one, dealing
388 * with the overflow between the overflows */
390 if (args
->key
.min_offset
< (uint64_t) -1) {
391 args
->key
.min_offset
++;
395 if (args
->key
.min_type
< (uint8_t) -1) {
396 args
->key
.min_type
++;
397 args
->key
.min_offset
= 0;
401 if (args
->key
.min_objectid
< (uint64_t) -1) {
402 args
->key
.min_objectid
++;
403 args
->key
.min_offset
= 0;
404 args
->key
.min_type
= 0;
411 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
415 args
->key
.min_objectid
= h
->objectid
;
416 args
->key
.min_type
= h
->type
;
417 args
->key
.min_offset
= h
->offset
;
420 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
425 /* Compare min and max */
427 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
431 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
435 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
438 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
440 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
441 (i) < (args).key.nr_items; \
443 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
445 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
446 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
448 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
449 struct btrfs_ioctl_search_args args
= {
450 /* Tree of tree roots */
451 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
453 /* Look precisely for the subvolume items */
454 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
455 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
458 .key
.max_offset
= (uint64_t) -1,
460 /* No restrictions on the other components */
461 .key
.min_transid
= 0,
462 .key
.max_transid
= (uint64_t) -1,
471 if (subvol_id
== 0) {
472 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
476 r
= btrfs_is_filesystem(fd
);
483 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
485 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
486 const struct btrfs_ioctl_search_header
*sh
;
489 args
.key
.nr_items
= 256;
490 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
493 if (args
.key
.nr_items
<= 0)
496 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
498 const struct btrfs_root_item
*ri
;
500 /* Make sure we start the next search at least from this entry */
501 btrfs_ioctl_search_args_set(&args
, sh
);
503 if (sh
->objectid
!= subvol_id
)
505 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
508 /* Older versions of the struct lacked the otime setting */
509 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
512 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
514 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
515 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
517 ret
->subvol_id
= subvol_id
;
518 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
520 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
521 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
522 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
528 /* Increase search key by one, to read the next item, if we can. */
529 if (!btrfs_ioctl_search_args_inc(&args
))
540 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
542 struct btrfs_ioctl_search_args args
= {
543 /* Tree of quota items */
544 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
546 /* The object ID is always 0 */
547 .key
.min_objectid
= 0,
548 .key
.max_objectid
= 0,
550 /* Look precisely for the quota items */
551 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
552 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
554 /* No restrictions on the other components */
555 .key
.min_transid
= 0,
556 .key
.max_transid
= (uint64_t) -1,
559 bool found_info
= false, found_limit
= false;
566 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
570 r
= btrfs_is_filesystem(fd
);
577 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
579 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
580 const struct btrfs_ioctl_search_header
*sh
;
583 args
.key
.nr_items
= 256;
584 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
585 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
591 if (args
.key
.nr_items
<= 0)
594 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
596 /* Make sure we start the next search at least from this entry */
597 btrfs_ioctl_search_args_set(&args
, sh
);
599 if (sh
->objectid
!= 0)
601 if (sh
->offset
!= qgroupid
)
604 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
605 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
607 ret
->referenced
= le64toh(qii
->rfer
);
608 ret
->exclusive
= le64toh(qii
->excl
);
612 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
613 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
615 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
616 ret
->referenced_max
= le64toh(qli
->max_rfer
);
618 ret
->referenced_max
= (uint64_t) -1;
620 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
621 ret
->exclusive_max
= le64toh(qli
->max_excl
);
623 ret
->exclusive_max
= (uint64_t) -1;
628 if (found_info
&& found_limit
)
632 /* Increase search key by one, to read the next item, if we can. */
633 if (!btrfs_ioctl_search_args_inc(&args
))
638 if (!found_limit
&& !found_info
)
642 ret
->referenced
= (uint64_t) -1;
643 ret
->exclusive
= (uint64_t) -1;
647 ret
->referenced_max
= (uint64_t) -1;
648 ret
->exclusive_max
= (uint64_t) -1;
654 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
655 _cleanup_close_
int fd
= -1;
657 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
661 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
664 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
665 uint64_t level
, lowest
= (uint64_t) -1, lowest_qgroupid
= 0;
666 _cleanup_free_
uint64_t *qgroups
= NULL
;
672 /* This finds the "subtree" qgroup for a specific
673 * subvolume. This only works for subvolumes that have been
674 * prepared with btrfs_subvol_auto_qgroup_fd() with
675 * insert_intermediary_qgroup=true (or equivalent). For others
676 * it will return the leaf qgroup instead. The two cases may
677 * be distuingished via the return value, which is 1 in case
678 * an appropriate "subtree" qgroup was found, and 0
681 if (subvol_id
== 0) {
682 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
687 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
690 if (level
!= 0) /* Input must be a leaf qgroup */
693 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
697 for (i
= 0; i
< n
; i
++) {
700 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
707 if (lowest
== (uint64_t) -1 || level
< lowest
) {
708 lowest_qgroupid
= qgroups
[i
];
713 if (lowest
== (uint64_t) -1) {
714 /* No suitable higher-level qgroup found, let's return
715 * the leaf qgroup instead, and indicate that with the
722 *ret
= lowest_qgroupid
;
726 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
733 /* This determines the quota data of the qgroup with the
734 * lowest level, that shares the id part with the specified
735 * subvolume. This is useful for determining the quota data
736 * for entire subvolume subtrees, as long as the subtrees have
737 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
740 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
744 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
747 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
748 _cleanup_close_
int fd
= -1;
750 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
754 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
757 int btrfs_defrag_fd(int fd
) {
762 r
= fd_verify_regular(fd
);
766 if (ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
) < 0)
772 int btrfs_defrag(const char *p
) {
773 _cleanup_close_
int fd
= -1;
775 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
779 return btrfs_defrag_fd(fd
);
782 int btrfs_quota_enable_fd(int fd
, bool b
) {
783 struct btrfs_ioctl_quota_ctl_args args
= {
784 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
790 r
= btrfs_is_filesystem(fd
);
796 if (ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
) < 0)
802 int btrfs_quota_enable(const char *path
, bool b
) {
803 _cleanup_close_
int fd
= -1;
805 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
809 return btrfs_quota_enable_fd(fd
, b
);
812 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
814 struct btrfs_ioctl_qgroup_limit_args args
= {
815 .lim
.max_rfer
= referenced_max
,
816 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
824 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
828 r
= btrfs_is_filesystem(fd
);
835 args
.qgroupid
= qgroupid
;
838 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
840 if (errno
== EBUSY
&& c
< 10) {
841 (void) btrfs_quota_scan_wait(fd
);
854 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, 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_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
864 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
870 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
874 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
877 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
878 _cleanup_close_
int fd
= -1;
880 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
884 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
887 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
890 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
893 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
896 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
900 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
904 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
907 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
912 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
914 struct btrfs_ioctl_qgroup_create_args args
= {
916 .qgroupid
= qgroupid
,
921 r
= btrfs_is_filesystem(fd
);
928 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
930 /* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
931 * ENOTCONN. Let's always convert this to ENOTCONN to make this recognizable
932 * everywhere the same way. */
934 if (IN_SET(errno
, EINVAL
, ENOTCONN
))
937 if (errno
== EBUSY
&& c
< 10) {
938 (void) btrfs_quota_scan_wait(fd
);
951 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
952 return qgroup_create_or_destroy(fd
, true, qgroupid
);
955 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
956 return qgroup_create_or_destroy(fd
, false, qgroupid
);
959 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
960 _cleanup_free_
uint64_t *qgroups
= NULL
;
964 /* Destroys the specified qgroup, but unassigns it from all
965 * its parents first. Also, it recursively destroys all
966 * qgroups it is assigned to that have the same id part of the
967 * qgroupid as the specified group. */
969 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
973 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
977 for (i
= 0; i
< n
; i
++) {
980 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
984 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
991 /* The parent qgroupid shares the same id part with
992 * us? If so, destroy it too. */
994 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
997 return btrfs_qgroup_destroy(fd
, qgroupid
);
1000 int btrfs_quota_scan_start(int fd
) {
1001 struct btrfs_ioctl_quota_rescan_args args
= {};
1005 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
) < 0)
1011 int btrfs_quota_scan_wait(int fd
) {
1014 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
) < 0)
1020 int btrfs_quota_scan_ongoing(int fd
) {
1021 struct btrfs_ioctl_quota_rescan_args args
= {};
1025 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
1028 return !!args
.flags
;
1031 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
1032 struct btrfs_ioctl_qgroup_assign_args args
= {
1040 r
= btrfs_is_filesystem(fd
);
1047 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1049 if (errno
== EBUSY
&& c
< 10) {
1050 (void) btrfs_quota_scan_wait(fd
);
1060 /* If the return value is > 0, we need to request a rescan */
1062 (void) btrfs_quota_scan_start(fd
);
1067 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1068 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1071 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1072 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1075 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1076 struct btrfs_ioctl_search_args args
= {
1077 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1079 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1080 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1082 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1083 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1085 .key
.min_transid
= 0,
1086 .key
.max_transid
= (uint64_t) -1,
1089 struct btrfs_ioctl_vol_args vol_args
= {};
1090 _cleanup_close_
int subvol_fd
= -1;
1092 bool made_writable
= false;
1098 if (fstat(fd
, &st
) < 0)
1101 if (!S_ISDIR(st
.st_mode
))
1104 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1108 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1109 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1110 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1111 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1112 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1113 * let's prefer ENOTTY over EPERM/EACCES though. */
1114 r
= btrfs_is_subvol_fd(subvol_fd
);
1117 if (r
== 0) /* Not a btrfs subvolume */
1120 if (subvol_id
== 0) {
1121 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1126 /* First, try to remove the subvolume. If it happens to be
1127 * already empty, this will just work. */
1128 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1129 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1130 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1133 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1136 /* OK, the subvolume is not empty, let's look for child
1137 * subvolumes, and remove them, first */
1139 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1141 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1142 const struct btrfs_ioctl_search_header
*sh
;
1145 args
.key
.nr_items
= 256;
1146 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1149 if (args
.key
.nr_items
<= 0)
1152 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1153 _cleanup_free_
char *p
= NULL
;
1154 const struct btrfs_root_ref
*ref
;
1156 btrfs_ioctl_search_args_set(&args
, sh
);
1158 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1160 if (sh
->offset
!= subvol_id
)
1163 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1165 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1169 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1170 .treeid
= subvol_id
,
1171 .objectid
= htole64(ref
->dirid
),
1174 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1177 if (!made_writable
) {
1178 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1182 made_writable
= true;
1185 if (isempty(ino_args
.name
))
1186 /* Subvolume is in the top-level
1187 * directory of the subvolume. */
1188 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1190 _cleanup_close_
int child_fd
= -1;
1192 /* Subvolume is somewhere further down,
1193 * hence we need to open the
1194 * containing directory first */
1196 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1200 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1206 /* Increase search key by one, to read the next item, if we can. */
1207 if (!btrfs_ioctl_search_args_inc(&args
))
1211 /* OK, the child subvolumes should all be gone now, let's try
1212 * again to remove the subvolume */
1213 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1216 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1220 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1221 _cleanup_close_
int fd
= -1;
1222 const char *subvolume
;
1227 r
= extract_subvolume_name(path
, &subvolume
);
1231 fd
= open_parent(path
, O_CLOEXEC
, 0);
1235 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1238 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1239 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1242 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1244 struct btrfs_ioctl_search_args args
= {
1245 /* Tree of quota items */
1246 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1248 /* The object ID is always 0 */
1249 .key
.min_objectid
= 0,
1250 .key
.max_objectid
= 0,
1252 /* Look precisely for the quota items */
1253 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1254 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1256 /* For our qgroup */
1257 .key
.min_offset
= old_qgroupid
,
1258 .key
.max_offset
= old_qgroupid
,
1260 /* No restrictions on the other components */
1261 .key
.min_transid
= 0,
1262 .key
.max_transid
= (uint64_t) -1,
1267 r
= btrfs_is_filesystem(fd
);
1273 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1274 const struct btrfs_ioctl_search_header
*sh
;
1277 args
.key
.nr_items
= 256;
1278 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1279 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1285 if (args
.key
.nr_items
<= 0)
1288 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1289 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1290 struct btrfs_ioctl_qgroup_limit_args qargs
;
1293 /* Make sure we start the next search at least from this entry */
1294 btrfs_ioctl_search_args_set(&args
, sh
);
1296 if (sh
->objectid
!= 0)
1298 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1300 if (sh
->offset
!= old_qgroupid
)
1303 /* We found the entry, now copy things over. */
1305 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1306 .qgroupid
= new_qgroupid
,
1308 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1309 .lim
.max_excl
= le64toh(qli
->max_excl
),
1310 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1311 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1313 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1314 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1315 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1316 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1320 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1321 if (errno
== EBUSY
&& c
< 10) {
1322 (void) btrfs_quota_scan_wait(fd
);
1334 /* Increase search key by one, to read the next item, if we can. */
1335 if (!btrfs_ioctl_search_args_inc(&args
))
1342 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1343 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1344 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1345 int n_old_qgroups
, n_old_parent_qgroups
, r
, i
;
1346 uint64_t old_parent_id
;
1350 /* Copies a reduced form of quota information from the old to
1351 * the new subvolume. */
1353 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1354 if (n_old_qgroups
<= 0) /* Nothing to copy */
1355 return n_old_qgroups
;
1357 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1359 /* We have no parent, hence nothing to copy. */
1360 n_old_parent_qgroups
= 0;
1364 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1365 if (n_old_parent_qgroups
< 0)
1366 return n_old_parent_qgroups
;
1369 for (i
= 0; i
< n_old_qgroups
; i
++) {
1373 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1377 if (id
== old_subvol_id
) {
1378 /* The old subvolume was member of a qgroup
1379 * that had the same id, but a different level
1380 * as it self. Let's set up something similar
1381 * in the destination. */
1382 insert_intermediary_qgroup
= true;
1386 for (j
= 0; j
< n_old_parent_qgroups
; j
++)
1387 if (old_parent_qgroups
[j
] == old_qgroups
[i
]) {
1388 /* The old subvolume shared a common
1389 * parent qgroup with its parent
1390 * subvolume. Let's set up something
1391 * similar in the destination. */
1392 copy_from_parent
= true;
1396 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1399 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1402 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1403 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1407 /* First copy the leaf limits */
1408 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1413 /* Then, try to copy the subtree limits, if there are any. */
1414 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1420 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1426 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1433 static int subvol_snapshot_children(
1436 const char *subvolume
,
1437 uint64_t old_subvol_id
,
1438 BtrfsSnapshotFlags flags
) {
1440 struct btrfs_ioctl_search_args args
= {
1441 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1443 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1444 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1446 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1447 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1449 .key
.min_transid
= 0,
1450 .key
.max_transid
= (uint64_t) -1,
1453 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1454 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1457 _cleanup_close_
int subvolume_fd
= -1;
1458 uint64_t new_subvol_id
;
1461 assert(old_fd
>= 0);
1462 assert(new_fd
>= 0);
1465 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1467 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1470 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1471 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1474 if (old_subvol_id
== 0) {
1475 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1480 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1484 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1485 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1487 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1489 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1490 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1495 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1497 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1498 const struct btrfs_ioctl_search_header
*sh
;
1501 args
.key
.nr_items
= 256;
1502 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1505 if (args
.key
.nr_items
<= 0)
1508 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1509 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1510 const struct btrfs_root_ref
*ref
;
1511 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1513 btrfs_ioctl_search_args_set(&args
, sh
);
1515 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1518 /* Avoid finding the source subvolume a second
1520 if (sh
->offset
!= old_subvol_id
)
1523 /* Avoid running into loops if the new
1524 * subvolume is below the old one. */
1525 if (sh
->objectid
== new_subvol_id
)
1528 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1529 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1533 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1534 .treeid
= old_subvol_id
,
1535 .objectid
= htole64(ref
->dirid
),
1538 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1541 c
= path_join(ino_args
.name
, p
);
1545 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1546 if (old_child_fd
< 0)
1549 np
= path_join(subvolume
, ino_args
.name
);
1553 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1554 if (new_child_fd
< 0)
1557 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1558 /* If the snapshot is read-only we
1559 * need to mark it writable
1560 * temporarily, to put the subsnapshot
1563 if (subvolume_fd
< 0) {
1564 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1565 if (subvolume_fd
< 0)
1569 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1574 /* When btrfs clones the subvolumes, child
1575 * subvolumes appear as empty directories. Remove
1576 * them, so that we can create a new snapshot
1578 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1581 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1582 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1587 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1589 /* Restore the readonly flag */
1590 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1593 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1594 if (r
>= 0 && k
< 0)
1602 /* Increase search key by one, to read the next item, if we can. */
1603 if (!btrfs_ioctl_search_args_inc(&args
))
1607 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1608 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1613 int btrfs_subvol_snapshot_fd_full(
1615 const char *new_path
,
1616 BtrfsSnapshotFlags flags
,
1617 copy_progress_path_t progress_path
,
1618 copy_progress_bytes_t progress_bytes
,
1621 _cleanup_close_
int new_fd
= -1;
1622 const char *subvolume
;
1625 assert(old_fd
>= 0);
1628 r
= btrfs_is_subvol_fd(old_fd
);
1632 bool plain_directory
= false;
1634 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1635 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1638 r
= btrfs_subvol_make(new_path
);
1639 if (r
== -ENOTTY
&& (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1640 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1641 if (mkdir(new_path
, 0755) < 0)
1644 plain_directory
= true;
1648 r
= copy_directory_fd_full(old_fd
, new_path
, COPY_MERGE
|COPY_REFLINK
|COPY_SAME_MOUNT
|(FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGINT
) ? COPY_SIGINT
: 0), progress_path
, progress_bytes
, userdata
);
1652 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1654 if (plain_directory
) {
1655 /* Plain directories have no recursive read-only flag, but something pretty close to
1656 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1658 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1659 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1661 r
= btrfs_subvol_set_read_only(new_path
, true);
1670 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1674 r
= extract_subvolume_name(new_path
, &subvolume
);
1678 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1682 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1685 int btrfs_subvol_snapshot_full(
1686 const char *old_path
,
1687 const char *new_path
,
1688 BtrfsSnapshotFlags flags
,
1689 copy_progress_path_t progress_path
,
1690 copy_progress_bytes_t progress_bytes
,
1693 _cleanup_close_
int old_fd
= -1;
1698 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1702 return btrfs_subvol_snapshot_fd_full(old_fd
, new_path
, flags
, progress_path
, progress_bytes
, userdata
);
1705 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1707 struct btrfs_ioctl_search_args args
= {
1708 /* Tree of quota items */
1709 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1711 /* Look precisely for the quota relation items */
1712 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1713 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1715 /* No restrictions on the other components */
1716 .key
.min_offset
= 0,
1717 .key
.max_offset
= (uint64_t) -1,
1719 .key
.min_transid
= 0,
1720 .key
.max_transid
= (uint64_t) -1,
1723 _cleanup_free_
uint64_t *items
= NULL
;
1724 size_t n_items
= 0, n_allocated
= 0;
1730 if (qgroupid
== 0) {
1731 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1735 r
= btrfs_is_filesystem(fd
);
1742 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1744 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1745 const struct btrfs_ioctl_search_header
*sh
;
1748 args
.key
.nr_items
= 256;
1749 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1750 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1756 if (args
.key
.nr_items
<= 0)
1759 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1761 /* Make sure we start the next search at least from this entry */
1762 btrfs_ioctl_search_args_set(&args
, sh
);
1764 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1766 if (sh
->offset
< sh
->objectid
)
1768 if (sh
->objectid
!= qgroupid
)
1771 if (!GREEDY_REALLOC(items
, n_allocated
, n_items
+1))
1774 items
[n_items
++] = sh
->offset
;
1777 /* Increase search key by one, to read the next item, if we can. */
1778 if (!btrfs_ioctl_search_args_inc(&args
))
1787 *ret
= TAKE_PTR(items
);
1789 return (int) n_items
;
1792 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1793 _cleanup_free_
uint64_t *qgroups
= NULL
;
1794 uint64_t parent_subvol
;
1795 bool changed
= false;
1801 * Sets up the specified subvolume's qgroup automatically in
1804 * If insert_intermediary_qgroup is false, the subvolume's
1805 * leaf qgroup will be assigned to the same parent qgroups as
1806 * the subvolume's parent subvolume.
1808 * If insert_intermediary_qgroup is true a new intermediary
1809 * higher-level qgroup is created, with a higher level number,
1810 * but reusing the id of the subvolume. The level number is
1811 * picked as one smaller than the lowest level qgroup the
1812 * parent subvolume is a member of. If the parent subvolume's
1813 * leaf qgroup is assigned to no higher-level qgroup a new
1814 * qgroup of level 255 is created instead. Either way, the new
1815 * qgroup is then assigned to the parent's higher-level
1816 * qgroup, and the subvolume itself is assigned to it.
1818 * If the subvolume is already assigned to a higher level
1819 * qgroup, no operation is executed.
1821 * Effectively this means: regardless if
1822 * insert_intermediary_qgroup is true or not, after this
1823 * function is invoked the subvolume will be accounted within
1824 * the same qgroups as the parent. However, if it is true, it
1825 * will also get its own higher-level qgroup, which may in
1826 * turn be used by subvolumes created beneath this subvolume
1829 * This hence defines a simple default qgroup setup for
1830 * subvolumes, as long as this function is invoked on each
1831 * created subvolume: each subvolume is always accounting
1832 * together with its immediate parents. Optionally, if
1833 * insert_intermediary_qgroup is true, it will also get a
1834 * qgroup that then includes all its own child subvolumes.
1837 if (subvol_id
== 0) {
1838 r
= btrfs_is_subvol_fd(fd
);
1844 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1849 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1852 if (n
> 0) /* already parent qgroups set up, let's bail */
1855 qgroups
= mfree(qgroups
);
1857 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1859 /* No parent, hence no qgroup memberships */
1864 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1869 if (insert_intermediary_qgroup
) {
1870 uint64_t lowest
= 256, new_qgroupid
;
1871 bool created
= false;
1874 /* Determine the lowest qgroup that the parent
1875 * subvolume is assigned to. */
1877 for (i
= 0; i
< n
; i
++) {
1880 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, NULL
);
1888 if (lowest
<= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1891 r
= btrfs_qgroupid_make(lowest
- 1, subvol_id
, &new_qgroupid
);
1895 /* Create the new intermediary group, unless it already exists */
1896 r
= btrfs_qgroup_create(fd
, new_qgroupid
);
1897 if (r
< 0 && r
!= -EEXIST
)
1900 changed
= created
= true;
1902 for (i
= 0; i
< n
; i
++) {
1903 r
= btrfs_qgroup_assign(fd
, new_qgroupid
, qgroups
[i
]);
1904 if (r
< 0 && r
!= -EEXIST
) {
1906 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1914 r
= btrfs_qgroup_assign(fd
, subvol_id
, new_qgroupid
);
1915 if (r
< 0 && r
!= -EEXIST
) {
1917 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1926 /* Assign our subvolume to all the same qgroups as the parent */
1928 for (i
= 0; i
< n
; i
++) {
1929 r
= btrfs_qgroup_assign(fd
, subvol_id
, qgroups
[i
]);
1930 if (r
< 0 && r
!= -EEXIST
)
1940 int btrfs_subvol_auto_qgroup(const char *path
, uint64_t subvol_id
, bool create_intermediary_qgroup
) {
1941 _cleanup_close_
int fd
= -1;
1943 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1947 return btrfs_subvol_auto_qgroup_fd(fd
, subvol_id
, create_intermediary_qgroup
);
1950 int btrfs_subvol_get_parent(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
1952 struct btrfs_ioctl_search_args args
= {
1953 /* Tree of tree roots */
1954 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1956 /* Look precisely for the subvolume items */
1957 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1958 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1960 /* No restrictions on the other components */
1961 .key
.min_offset
= 0,
1962 .key
.max_offset
= (uint64_t) -1,
1964 .key
.min_transid
= 0,
1965 .key
.max_transid
= (uint64_t) -1,
1972 if (subvol_id
== 0) {
1973 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1977 r
= btrfs_is_filesystem(fd
);
1984 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
1986 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1987 const struct btrfs_ioctl_search_header
*sh
;
1990 args
.key
.nr_items
= 256;
1991 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1992 return negative_errno();
1994 if (args
.key
.nr_items
<= 0)
1997 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1999 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
2001 if (sh
->objectid
!= subvol_id
)