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"
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 (!btrfs_might_be_subvol(&st
))
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 (!btrfs_might_be_subvol(&st
))
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 (!btrfs_might_be_subvol(&st
))
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 /* For the root fs — when no initrd is involved — btrfs returns /dev/root on any kernels from
319 * the past few years. That sucks, as we have no API to determine the actual root then. let's
320 * return an recognizable error for this case, so that the caller can maybe print a nice
321 * message about this.
323 * https://bugzilla.kernel.org/show_bug.cgi?id=89721 */
324 if (path_equal((char*) di
.path
, "/dev/root"))
327 if (stat((char*) di
.path
, &st
) < 0)
330 if (!S_ISBLK(st
.st_mode
))
333 if (major(st
.st_rdev
) == 0)
343 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
344 _cleanup_close_
int fd
= -1;
349 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
353 return btrfs_get_block_device_fd(fd
, dev
);
356 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
357 struct btrfs_ioctl_ino_lookup_args args
= {
358 .objectid
= BTRFS_FIRST_FREE_OBJECTID
365 r
= btrfs_is_filesystem(fd
);
371 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
378 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
379 _cleanup_close_
int subvol_fd
= -1;
384 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
388 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
391 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
394 /* the objectid, type, offset together make up the btrfs key,
395 * which is considered a single 136byte integer when
396 * comparing. This call increases the counter by one, dealing
397 * with the overflow between the overflows */
399 if (args
->key
.min_offset
< UINT64_MAX
) {
400 args
->key
.min_offset
++;
404 if (args
->key
.min_type
< UINT8_MAX
) {
405 args
->key
.min_type
++;
406 args
->key
.min_offset
= 0;
410 if (args
->key
.min_objectid
< UINT64_MAX
) {
411 args
->key
.min_objectid
++;
412 args
->key
.min_offset
= 0;
413 args
->key
.min_type
= 0;
420 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
424 args
->key
.min_objectid
= h
->objectid
;
425 args
->key
.min_type
= h
->type
;
426 args
->key
.min_offset
= h
->offset
;
429 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
434 /* Compare min and max */
436 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
440 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
444 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
447 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
449 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
450 (i) < (args).key.nr_items; \
452 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
454 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
455 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
457 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
458 struct btrfs_ioctl_search_args args
= {
459 /* Tree of tree roots */
460 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
462 /* Look precisely for the subvolume items */
463 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
464 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
467 .key
.max_offset
= UINT64_MAX
,
469 /* No restrictions on the other components */
470 .key
.min_transid
= 0,
471 .key
.max_transid
= UINT64_MAX
,
480 if (subvol_id
== 0) {
481 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
485 r
= btrfs_is_filesystem(fd
);
492 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
494 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
495 const struct btrfs_ioctl_search_header
*sh
;
498 args
.key
.nr_items
= 256;
499 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
502 if (args
.key
.nr_items
<= 0)
505 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
507 const struct btrfs_root_item
*ri
;
509 /* Make sure we start the next search at least from this entry */
510 btrfs_ioctl_search_args_set(&args
, sh
);
512 if (sh
->objectid
!= subvol_id
)
514 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
517 /* Older versions of the struct lacked the otime setting */
518 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
521 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
523 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
524 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
526 ret
->subvol_id
= subvol_id
;
527 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
529 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
530 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
531 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
537 /* Increase search key by one, to read the next item, if we can. */
538 if (!btrfs_ioctl_search_args_inc(&args
))
549 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
551 struct btrfs_ioctl_search_args args
= {
552 /* Tree of quota items */
553 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
555 /* The object ID is always 0 */
556 .key
.min_objectid
= 0,
557 .key
.max_objectid
= 0,
559 /* Look precisely for the quota items */
560 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
561 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
563 /* No restrictions on the other components */
564 .key
.min_transid
= 0,
565 .key
.max_transid
= UINT64_MAX
,
568 bool found_info
= false, found_limit
= false;
575 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
579 r
= btrfs_is_filesystem(fd
);
586 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
588 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
589 const struct btrfs_ioctl_search_header
*sh
;
592 args
.key
.nr_items
= 256;
593 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
594 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
600 if (args
.key
.nr_items
<= 0)
603 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
605 /* Make sure we start the next search at least from this entry */
606 btrfs_ioctl_search_args_set(&args
, sh
);
608 if (sh
->objectid
!= 0)
610 if (sh
->offset
!= qgroupid
)
613 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
614 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
616 ret
->referenced
= le64toh(qii
->rfer
);
617 ret
->exclusive
= le64toh(qii
->excl
);
621 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
622 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
624 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
625 ret
->referenced_max
= le64toh(qli
->max_rfer
);
627 ret
->referenced_max
= UINT64_MAX
;
629 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
630 ret
->exclusive_max
= le64toh(qli
->max_excl
);
632 ret
->exclusive_max
= UINT64_MAX
;
637 if (found_info
&& found_limit
)
641 /* Increase search key by one, to read the next item, if we can. */
642 if (!btrfs_ioctl_search_args_inc(&args
))
647 if (!found_limit
&& !found_info
)
651 ret
->referenced
= UINT64_MAX
;
652 ret
->exclusive
= UINT64_MAX
;
656 ret
->referenced_max
= UINT64_MAX
;
657 ret
->exclusive_max
= UINT64_MAX
;
663 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
664 _cleanup_close_
int fd
= -1;
666 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
670 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
673 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
674 uint64_t level
, lowest
= UINT64_MAX
, lowest_qgroupid
= 0;
675 _cleanup_free_
uint64_t *qgroups
= NULL
;
681 /* This finds the "subtree" qgroup for a specific
682 * subvolume. This only works for subvolumes that have been
683 * prepared with btrfs_subvol_auto_qgroup_fd() with
684 * insert_intermediary_qgroup=true (or equivalent). For others
685 * it will return the leaf qgroup instead. The two cases may
686 * be distuingished via the return value, which is 1 in case
687 * an appropriate "subtree" qgroup was found, and 0
690 if (subvol_id
== 0) {
691 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
696 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
699 if (level
!= 0) /* Input must be a leaf qgroup */
702 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
706 for (int i
= 0; i
< n
; i
++) {
709 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
716 if (lowest
== UINT64_MAX
|| level
< lowest
) {
717 lowest_qgroupid
= qgroups
[i
];
722 if (lowest
== UINT64_MAX
) {
723 /* No suitable higher-level qgroup found, let's return
724 * the leaf qgroup instead, and indicate that with the
731 *ret
= lowest_qgroupid
;
735 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
742 /* This determines the quota data of the qgroup with the
743 * lowest level, that shares the id part with the specified
744 * subvolume. This is useful for determining the quota data
745 * for entire subvolume subtrees, as long as the subtrees have
746 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
749 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
753 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
756 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
757 _cleanup_close_
int fd
= -1;
759 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
763 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
766 int btrfs_defrag_fd(int fd
) {
771 r
= fd_verify_regular(fd
);
775 if (ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
) < 0)
781 int btrfs_defrag(const char *p
) {
782 _cleanup_close_
int fd
= -1;
784 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
788 return btrfs_defrag_fd(fd
);
791 int btrfs_quota_enable_fd(int fd
, bool b
) {
792 struct btrfs_ioctl_quota_ctl_args args
= {
793 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
799 r
= btrfs_is_filesystem(fd
);
805 if (ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
) < 0)
811 int btrfs_quota_enable(const char *path
, bool b
) {
812 _cleanup_close_
int fd
= -1;
814 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
818 return btrfs_quota_enable_fd(fd
, b
);
821 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
823 struct btrfs_ioctl_qgroup_limit_args args
= {
824 .lim
.max_rfer
= referenced_max
,
825 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
832 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
836 r
= btrfs_is_filesystem(fd
);
843 args
.qgroupid
= qgroupid
;
845 for (unsigned c
= 0;; c
++) {
846 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
848 if (errno
== EBUSY
&& c
< 10) {
849 (void) btrfs_quota_scan_wait(fd
);
862 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, uint64_t referenced_max
) {
863 _cleanup_close_
int fd
= -1;
865 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
869 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
872 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
878 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
882 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
885 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
886 _cleanup_close_
int fd
= -1;
888 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
892 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
895 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
898 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
901 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
904 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
908 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
912 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
915 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
920 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
922 struct btrfs_ioctl_qgroup_create_args args
= {
924 .qgroupid
= qgroupid
,
928 r
= btrfs_is_filesystem(fd
);
934 for (unsigned c
= 0;; c
++) {
935 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
937 /* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
938 * ENOTCONN. Let's always convert this to ENOTCONN to make this recognizable
939 * everywhere the same way. */
941 if (IN_SET(errno
, EINVAL
, ENOTCONN
))
944 if (errno
== EBUSY
&& c
< 10) {
945 (void) btrfs_quota_scan_wait(fd
);
958 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
959 return qgroup_create_or_destroy(fd
, true, qgroupid
);
962 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
963 return qgroup_create_or_destroy(fd
, false, qgroupid
);
966 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
967 _cleanup_free_
uint64_t *qgroups
= NULL
;
971 /* Destroys the specified qgroup, but unassigns it from all
972 * its parents first. Also, it recursively destroys all
973 * qgroups it is assigned to that have the same id part of the
974 * qgroupid as the specified group. */
976 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
980 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
984 for (int i
= 0; i
< n
; i
++) {
987 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
991 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
998 /* The parent qgroupid shares the same id part with
999 * us? If so, destroy it too. */
1001 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
1004 return btrfs_qgroup_destroy(fd
, qgroupid
);
1007 int btrfs_quota_scan_start(int fd
) {
1008 struct btrfs_ioctl_quota_rescan_args args
= {};
1012 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
) < 0)
1018 int btrfs_quota_scan_wait(int fd
) {
1021 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
) < 0)
1027 int btrfs_quota_scan_ongoing(int fd
) {
1028 struct btrfs_ioctl_quota_rescan_args args
= {};
1032 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
1035 return !!args
.flags
;
1038 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
1039 struct btrfs_ioctl_qgroup_assign_args args
= {
1046 r
= btrfs_is_filesystem(fd
);
1052 for (unsigned c
= 0;; c
++) {
1053 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1055 if (errno
== EBUSY
&& c
< 10) {
1056 (void) btrfs_quota_scan_wait(fd
);
1066 /* If the return value is > 0, we need to request a rescan */
1068 (void) btrfs_quota_scan_start(fd
);
1073 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1074 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1077 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1078 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1081 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1082 struct btrfs_ioctl_search_args args
= {
1083 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1085 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1086 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1088 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1089 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1091 .key
.min_transid
= 0,
1092 .key
.max_transid
= UINT64_MAX
,
1095 struct btrfs_ioctl_vol_args vol_args
= {};
1096 _cleanup_close_
int subvol_fd
= -1;
1098 bool made_writable
= false;
1104 if (fstat(fd
, &st
) < 0)
1107 if (!S_ISDIR(st
.st_mode
))
1110 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1114 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1115 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1116 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1117 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1118 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1119 * let's prefer ENOTTY over EPERM/EACCES though. */
1120 r
= btrfs_is_subvol_fd(subvol_fd
);
1123 if (r
== 0) /* Not a btrfs subvolume */
1126 if (subvol_id
== 0) {
1127 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1132 /* First, try to remove the subvolume. If it happens to be
1133 * already empty, this will just work. */
1134 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1135 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1136 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1139 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1142 /* OK, the subvolume is not empty, let's look for child
1143 * subvolumes, and remove them, first */
1145 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1147 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1148 const struct btrfs_ioctl_search_header
*sh
;
1151 args
.key
.nr_items
= 256;
1152 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1155 if (args
.key
.nr_items
<= 0)
1158 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1159 _cleanup_free_
char *p
= NULL
;
1160 const struct btrfs_root_ref
*ref
;
1162 btrfs_ioctl_search_args_set(&args
, sh
);
1164 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1166 if (sh
->offset
!= subvol_id
)
1169 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1171 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1175 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1176 .treeid
= subvol_id
,
1177 .objectid
= htole64(ref
->dirid
),
1180 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1183 if (!made_writable
) {
1184 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1188 made_writable
= true;
1191 if (isempty(ino_args
.name
))
1192 /* Subvolume is in the top-level
1193 * directory of the subvolume. */
1194 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1196 _cleanup_close_
int child_fd
= -1;
1198 /* Subvolume is somewhere further down,
1199 * hence we need to open the
1200 * containing directory first */
1202 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1206 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1212 /* Increase search key by one, to read the next item, if we can. */
1213 if (!btrfs_ioctl_search_args_inc(&args
))
1217 /* OK, the child subvolumes should all be gone now, let's try
1218 * again to remove the subvolume */
1219 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1222 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1226 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1227 _cleanup_close_
int fd
= -1;
1228 const char *subvolume
;
1233 r
= extract_subvolume_name(path
, &subvolume
);
1237 fd
= open_parent(path
, O_CLOEXEC
, 0);
1241 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1244 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1245 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1248 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1250 struct btrfs_ioctl_search_args args
= {
1251 /* Tree of quota items */
1252 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1254 /* The object ID is always 0 */
1255 .key
.min_objectid
= 0,
1256 .key
.max_objectid
= 0,
1258 /* Look precisely for the quota items */
1259 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1260 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1262 /* For our qgroup */
1263 .key
.min_offset
= old_qgroupid
,
1264 .key
.max_offset
= old_qgroupid
,
1266 /* No restrictions on the other components */
1267 .key
.min_transid
= 0,
1268 .key
.max_transid
= UINT64_MAX
,
1273 r
= btrfs_is_filesystem(fd
);
1279 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1280 const struct btrfs_ioctl_search_header
*sh
;
1283 args
.key
.nr_items
= 256;
1284 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1285 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1291 if (args
.key
.nr_items
<= 0)
1294 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1295 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1296 struct btrfs_ioctl_qgroup_limit_args qargs
;
1299 /* Make sure we start the next search at least from this entry */
1300 btrfs_ioctl_search_args_set(&args
, sh
);
1302 if (sh
->objectid
!= 0)
1304 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1306 if (sh
->offset
!= old_qgroupid
)
1309 /* We found the entry, now copy things over. */
1311 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1312 .qgroupid
= new_qgroupid
,
1314 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1315 .lim
.max_excl
= le64toh(qli
->max_excl
),
1316 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1317 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1319 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1320 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1321 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1322 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1326 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1327 if (errno
== EBUSY
&& c
< 10) {
1328 (void) btrfs_quota_scan_wait(fd
);
1340 /* Increase search key by one, to read the next item, if we can. */
1341 if (!btrfs_ioctl_search_args_inc(&args
))
1348 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1349 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1350 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1351 int n_old_qgroups
, n_old_parent_qgroups
, r
;
1352 uint64_t old_parent_id
;
1356 /* Copies a reduced form of quota information from the old to
1357 * the new subvolume. */
1359 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1360 if (n_old_qgroups
<= 0) /* Nothing to copy */
1361 return n_old_qgroups
;
1363 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1365 /* We have no parent, hence nothing to copy. */
1366 n_old_parent_qgroups
= 0;
1370 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1371 if (n_old_parent_qgroups
< 0)
1372 return n_old_parent_qgroups
;
1375 for (int i
= 0; i
< n_old_qgroups
; i
++) {
1378 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1382 if (id
== old_subvol_id
) {
1383 /* The old subvolume was member of a qgroup
1384 * that had the same id, but a different level
1385 * as it self. Let's set up something similar
1386 * in the destination. */
1387 insert_intermediary_qgroup
= true;
1391 for (int j
= 0; j
< n_old_parent_qgroups
; j
++)
1392 if (old_parent_qgroups
[j
] == old_qgroups
[i
])
1393 /* The old subvolume shared a common
1394 * parent qgroup with its parent
1395 * subvolume. Let's set up something
1396 * similar in the destination. */
1397 copy_from_parent
= true;
1400 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1403 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1406 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1407 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1411 /* First copy the leaf limits */
1412 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1417 /* Then, try to copy the subtree limits, if there are any. */
1418 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1424 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1430 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1437 static int subvol_snapshot_children(
1440 const char *subvolume
,
1441 uint64_t old_subvol_id
,
1442 BtrfsSnapshotFlags flags
) {
1444 struct btrfs_ioctl_search_args args
= {
1445 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1447 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1448 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1450 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1451 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1453 .key
.min_transid
= 0,
1454 .key
.max_transid
= UINT64_MAX
,
1457 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1458 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1461 _cleanup_close_
int subvolume_fd
= -1;
1462 uint64_t new_subvol_id
;
1465 assert(old_fd
>= 0);
1466 assert(new_fd
>= 0);
1469 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1471 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1474 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1475 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1478 if (old_subvol_id
== 0) {
1479 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1484 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1488 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1489 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1491 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1493 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1494 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1499 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1501 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1502 const struct btrfs_ioctl_search_header
*sh
;
1505 args
.key
.nr_items
= 256;
1506 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1509 if (args
.key
.nr_items
<= 0)
1512 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1513 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1514 const struct btrfs_root_ref
*ref
;
1515 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1517 btrfs_ioctl_search_args_set(&args
, sh
);
1519 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1522 /* Avoid finding the source subvolume a second
1524 if (sh
->offset
!= old_subvol_id
)
1527 /* Avoid running into loops if the new
1528 * subvolume is below the old one. */
1529 if (sh
->objectid
== new_subvol_id
)
1532 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1533 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1537 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1538 .treeid
= old_subvol_id
,
1539 .objectid
= htole64(ref
->dirid
),
1542 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1545 c
= path_join(ino_args
.name
, p
);
1549 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1550 if (old_child_fd
< 0)
1553 np
= path_join(subvolume
, ino_args
.name
);
1557 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1558 if (new_child_fd
< 0)
1561 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1562 /* If the snapshot is read-only we
1563 * need to mark it writable
1564 * temporarily, to put the subsnapshot
1567 if (subvolume_fd
< 0) {
1568 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1569 if (subvolume_fd
< 0)
1573 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1578 /* When btrfs clones the subvolumes, child
1579 * subvolumes appear as empty directories. Remove
1580 * them, so that we can create a new snapshot
1582 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1585 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1586 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1591 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1593 /* Restore the readonly flag */
1594 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1597 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1598 if (r
>= 0 && k
< 0)
1606 /* Increase search key by one, to read the next item, if we can. */
1607 if (!btrfs_ioctl_search_args_inc(&args
))
1611 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1612 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1617 int btrfs_subvol_snapshot_fd_full(
1619 const char *new_path
,
1620 BtrfsSnapshotFlags flags
,
1621 copy_progress_path_t progress_path
,
1622 copy_progress_bytes_t progress_bytes
,
1625 _cleanup_close_
int new_fd
= -1;
1626 const char *subvolume
;
1629 assert(old_fd
>= 0);
1632 r
= btrfs_is_subvol_fd(old_fd
);
1636 bool plain_directory
= false;
1638 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1639 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1642 r
= btrfs_subvol_make(new_path
);
1643 if (r
== -ENOTTY
&& (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1644 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1645 if (mkdir(new_path
, 0755) < 0)
1648 plain_directory
= true;
1652 r
= copy_directory_fd_full(
1654 COPY_MERGE
|COPY_REFLINK
|COPY_SAME_MOUNT
|COPY_HARDLINKS
|(FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGINT
) ? COPY_SIGINT
: 0),
1655 progress_path
, progress_bytes
, userdata
);
1659 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1661 if (plain_directory
) {
1662 /* Plain directories have no recursive read-only flag, but something pretty close to
1663 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1665 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1666 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1668 r
= btrfs_subvol_set_read_only(new_path
, true);
1677 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1681 r
= extract_subvolume_name(new_path
, &subvolume
);
1685 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1689 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1692 int btrfs_subvol_snapshot_full(
1693 const char *old_path
,
1694 const char *new_path
,
1695 BtrfsSnapshotFlags flags
,
1696 copy_progress_path_t progress_path
,
1697 copy_progress_bytes_t progress_bytes
,
1700 _cleanup_close_
int old_fd
= -1;
1705 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1709 return btrfs_subvol_snapshot_fd_full(old_fd
, new_path
, flags
, progress_path
, progress_bytes
, userdata
);
1712 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1714 struct btrfs_ioctl_search_args args
= {
1715 /* Tree of quota items */
1716 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1718 /* Look precisely for the quota relation items */
1719 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1720 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1722 /* No restrictions on the other components */
1723 .key
.min_offset
= 0,
1724 .key
.max_offset
= UINT64_MAX
,
1726 .key
.min_transid
= 0,
1727 .key
.max_transid
= UINT64_MAX
,
1730 _cleanup_free_
uint64_t *items
= NULL
;
1737 if (qgroupid
== 0) {
1738 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1742 r
= btrfs_is_filesystem(fd
);
1749 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1751 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1752 const struct btrfs_ioctl_search_header
*sh
;
1755 args
.key
.nr_items
= 256;
1756 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1757 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1763 if (args
.key
.nr_items
<= 0)
1766 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1768 /* Make sure we start the next search at least from this entry */
1769 btrfs_ioctl_search_args_set(&args
, sh
);
1771 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1773 if (sh
->offset
< sh
->objectid
)
1775 if (sh
->objectid
!= qgroupid
)
1778 if (!GREEDY_REALLOC(items
, n_items
+1))
1781 items
[n_items
++] = sh
->offset
;
1784 /* Increase search key by one, to read the next item, if we can. */
1785 if (!btrfs_ioctl_search_args_inc(&args
))
1794 *ret
= TAKE_PTR(items
);
1796 return (int) n_items
;
1799 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1800 _cleanup_free_
uint64_t *qgroups
= NULL
;
1801 uint64_t parent_subvol
;
1802 bool changed
= false;
1808 * Sets up the specified subvolume's qgroup automatically in
1811 * If insert_intermediary_qgroup is false, the subvolume's
1812 * leaf qgroup will be assigned to the same parent qgroups as
1813 * the subvolume's parent subvolume.
1815 * If insert_intermediary_qgroup is true a new intermediary
1816 * higher-level qgroup is created, with a higher level number,
1817 * but reusing the id of the subvolume. The level number is
1818 * picked as one smaller than the lowest level qgroup the
1819 * parent subvolume is a member of. If the parent subvolume's
1820 * leaf qgroup is assigned to no higher-level qgroup a new
1821 * qgroup of level 255 is created instead. Either way, the new
1822 * qgroup is then assigned to the parent's higher-level
1823 * qgroup, and the subvolume itself is assigned to it.
1825 * If the subvolume is already assigned to a higher level
1826 * qgroup, no operation is executed.
1828 * Effectively this means: regardless if
1829 * insert_intermediary_qgroup is true or not, after this
1830 * function is invoked the subvolume will be accounted within
1831 * the same qgroups as the parent. However, if it is true, it
1832 * will also get its own higher-level qgroup, which may in
1833 * turn be used by subvolumes created beneath this subvolume
1836 * This hence defines a simple default qgroup setup for
1837 * subvolumes, as long as this function is invoked on each
1838 * created subvolume: each subvolume is always accounting
1839 * together with its immediate parents. Optionally, if
1840 * insert_intermediary_qgroup is true, it will also get a
1841 * qgroup that then includes all its own child subvolumes.
1844 if (subvol_id
== 0) {
1845 r
= btrfs_is_subvol_fd(fd
);
1851 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1856 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1859 if (n
> 0) /* already parent qgroups set up, let's bail */
1862 qgroups
= mfree(qgroups
);
1864 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1866 /* No parent, hence no qgroup memberships */
1871 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1876 if (insert_intermediary_qgroup
) {
1877 uint64_t lowest
= 256, new_qgroupid
;
1878 bool created
= false;
1880 /* Determine the lowest qgroup that the parent
1881 * subvolume is assigned to. */
1883 for (int i
= 0; i
< n
; i
++) {
1886 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, NULL
);
1894 if (lowest
<= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1897 r
= btrfs_qgroupid_make(lowest
- 1, subvol_id
, &new_qgroupid
);
1901 /* Create the new intermediary group, unless it already exists */
1902 r
= btrfs_qgroup_create(fd
, new_qgroupid
);
1903 if (r
< 0 && r
!= -EEXIST
)
1906 changed
= created
= true;
1908 for (int i
= 0; i
< n
; i
++) {
1909 r
= btrfs_qgroup_assign(fd
, new_qgroupid
, qgroups
[i
]);
1910 if (r
< 0 && r
!= -EEXIST
) {
1912 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1920 r
= btrfs_qgroup_assign(fd
, subvol_id
, new_qgroupid
);
1921 if (r
< 0 && r
!= -EEXIST
) {
1923 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1932 /* Assign our subvolume to all the same qgroups as the parent */
1934 for (i
= 0; i
< n
; i
++) {
1935 r
= btrfs_qgroup_assign(fd
, subvol_id
, qgroups
[i
]);
1936 if (r
< 0 && r
!= -EEXIST
)
1946 int btrfs_subvol_auto_qgroup(const char *path
, uint64_t subvol_id
, bool create_intermediary_qgroup
) {
1947 _cleanup_close_
int fd
= -1;
1949 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1953 return btrfs_subvol_auto_qgroup_fd(fd
, subvol_id
, create_intermediary_qgroup
);
1956 int btrfs_subvol_get_parent(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
1958 struct btrfs_ioctl_search_args args
= {
1959 /* Tree of tree roots */
1960 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1962 /* Look precisely for the subvolume items */
1963 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1964 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1966 /* No restrictions on the other components */
1967 .key
.min_offset
= 0,
1968 .key
.max_offset
= UINT64_MAX
,
1970 .key
.min_transid
= 0,
1971 .key
.max_transid
= UINT64_MAX
,
1978 if (subvol_id
== 0) {
1979 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1983 r
= btrfs_is_filesystem(fd
);
1990 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
1992 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1993 const struct btrfs_ioctl_search_header
*sh
;
1996 args
.key
.nr_items
= 256;
1997 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1998 return negative_errno();
2000 if (args
.key
.nr_items
<= 0)
2003 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
2005 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
2007 if (sh
->objectid
!= subvol_id
)