1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <linux/btrfs_tree.h>
8 #include <linux/loop.h>
9 #include <linux/magic.h>
14 #include <sys/ioctl.h>
16 #include <sys/statfs.h>
17 #include <sys/sysmacros.h>
20 #include "alloc-util.h"
21 #include "blockdev-util.h"
22 #include "btrfs-util.h"
23 #include "chattr-util.h"
25 #include "device-nodes.h"
31 #include "path-util.h"
33 #include "smack-util.h"
34 #include "sparse-endian.h"
35 #include "stat-util.h"
36 #include "string-util.h"
37 #include "time-util.h"
40 /* WARNING: Be careful with file system ioctls! When we get an fd, we
41 * need to make sure it either refers to only a regular file or
42 * directory, or that it is located on btrfs, before invoking any
43 * btrfs ioctls. The ioctl numbers are reused by some device drivers
44 * (such as DRM), and hence might have bad effects when invoked on
45 * device nodes (that reference drivers) rather than fds to normal
46 * files or directories. */
48 static int validate_subvolume_name(const char *name
) {
50 if (!filename_is_valid(name
))
53 if (strlen(name
) > BTRFS_SUBVOL_NAME_MAX
)
59 static int extract_subvolume_name(const char *path
, const char **subvolume
) {
68 r
= validate_subvolume_name(fn
);
76 int btrfs_is_filesystem(int fd
) {
81 if (fstatfs(fd
, &sfs
) < 0)
84 return F_TYPE_EQUAL(sfs
.f_type
, BTRFS_SUPER_MAGIC
);
87 int btrfs_is_subvol_fd(int fd
) {
92 /* On btrfs subvolumes always have the inode 256 */
94 if (fstat(fd
, &st
) < 0)
97 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
100 return btrfs_is_filesystem(fd
);
103 int btrfs_is_subvol(const char *path
) {
104 _cleanup_close_
int fd
= -1;
108 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
112 return btrfs_is_subvol_fd(fd
);
115 int btrfs_subvol_make_fd(int fd
, const char *subvolume
) {
116 struct btrfs_ioctl_vol_args args
= {};
117 _cleanup_close_
int real_fd
= -1;
122 r
= validate_subvolume_name(subvolume
);
126 r
= fcntl(fd
, F_GETFL
);
129 if (FLAGS_SET(r
, O_PATH
)) {
130 /* An O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal with
133 real_fd
= fd_reopen(fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
140 strncpy(args
.name
, subvolume
, sizeof(args
.name
)-1);
142 if (ioctl(fd
, BTRFS_IOC_SUBVOL_CREATE
, &args
) < 0)
148 int btrfs_subvol_make(const char *path
) {
149 _cleanup_close_
int fd
= -1;
150 const char *subvolume
;
155 r
= extract_subvolume_name(path
, &subvolume
);
159 fd
= open_parent(path
, O_CLOEXEC
, 0);
163 return btrfs_subvol_make_fd(fd
, subvolume
);
166 int btrfs_subvol_set_read_only_fd(int fd
, bool b
) {
167 uint64_t flags
, nflags
;
172 if (fstat(fd
, &st
) < 0)
175 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
178 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
182 nflags
= flags
| BTRFS_SUBVOL_RDONLY
;
184 nflags
= flags
& ~BTRFS_SUBVOL_RDONLY
;
189 if (ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &nflags
) < 0)
195 int btrfs_subvol_set_read_only(const char *path
, bool b
) {
196 _cleanup_close_
int fd
= -1;
198 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
202 return btrfs_subvol_set_read_only_fd(fd
, b
);
205 int btrfs_subvol_get_read_only_fd(int fd
) {
211 if (fstat(fd
, &st
) < 0)
214 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
217 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
220 return !!(flags
& BTRFS_SUBVOL_RDONLY
);
223 int btrfs_reflink(int infd
, int outfd
) {
229 /* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
231 r
= fd_verify_regular(outfd
);
235 if (ioctl(outfd
, BTRFS_IOC_CLONE
, infd
) < 0)
241 int btrfs_clone_range(int infd
, uint64_t in_offset
, int outfd
, uint64_t out_offset
, uint64_t sz
) {
242 struct btrfs_ioctl_clone_range_args args
= {
244 .src_offset
= in_offset
,
246 .dest_offset
= out_offset
,
254 r
= fd_verify_regular(outfd
);
258 if (ioctl(outfd
, BTRFS_IOC_CLONE_RANGE
, &args
) < 0)
264 int btrfs_get_block_device_fd(int fd
, dev_t
*dev
) {
265 struct btrfs_ioctl_fs_info_args fsi
= {};
272 r
= btrfs_is_filesystem(fd
);
278 if (ioctl(fd
, BTRFS_IOC_FS_INFO
, &fsi
) < 0)
281 /* We won't do this for btrfs RAID */
282 if (fsi
.num_devices
!= 1) {
287 for (id
= 1; id
<= fsi
.max_id
; id
++) {
288 struct btrfs_ioctl_dev_info_args di
= {
293 if (ioctl(fd
, BTRFS_IOC_DEV_INFO
, &di
) < 0) {
300 if (stat((char*) di
.path
, &st
) < 0)
303 if (!S_ISBLK(st
.st_mode
))
306 if (major(st
.st_rdev
) == 0)
316 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
317 _cleanup_close_
int fd
= -1;
322 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
326 return btrfs_get_block_device_fd(fd
, dev
);
329 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
330 struct btrfs_ioctl_ino_lookup_args args
= {
331 .objectid
= BTRFS_FIRST_FREE_OBJECTID
338 r
= btrfs_is_filesystem(fd
);
344 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
351 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
352 _cleanup_close_
int subvol_fd
= -1;
357 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
361 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
364 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
367 /* the objectid, type, offset together make up the btrfs key,
368 * which is considered a single 136byte integer when
369 * comparing. This call increases the counter by one, dealing
370 * with the overflow between the overflows */
372 if (args
->key
.min_offset
< (uint64_t) -1) {
373 args
->key
.min_offset
++;
377 if (args
->key
.min_type
< (uint8_t) -1) {
378 args
->key
.min_type
++;
379 args
->key
.min_offset
= 0;
383 if (args
->key
.min_objectid
< (uint64_t) -1) {
384 args
->key
.min_objectid
++;
385 args
->key
.min_offset
= 0;
386 args
->key
.min_type
= 0;
393 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
397 args
->key
.min_objectid
= h
->objectid
;
398 args
->key
.min_type
= h
->type
;
399 args
->key
.min_offset
= h
->offset
;
402 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
407 /* Compare min and max */
409 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
413 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
417 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
420 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
422 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
423 (i) < (args).key.nr_items; \
425 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
427 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
428 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
430 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
431 struct btrfs_ioctl_search_args args
= {
432 /* Tree of tree roots */
433 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
435 /* Look precisely for the subvolume items */
436 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
437 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
440 .key
.max_offset
= (uint64_t) -1,
442 /* No restrictions on the other components */
443 .key
.min_transid
= 0,
444 .key
.max_transid
= (uint64_t) -1,
453 if (subvol_id
== 0) {
454 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
458 r
= btrfs_is_filesystem(fd
);
465 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
467 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
468 const struct btrfs_ioctl_search_header
*sh
;
471 args
.key
.nr_items
= 256;
472 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
475 if (args
.key
.nr_items
<= 0)
478 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
480 const struct btrfs_root_item
*ri
;
482 /* Make sure we start the next search at least from this entry */
483 btrfs_ioctl_search_args_set(&args
, sh
);
485 if (sh
->objectid
!= subvol_id
)
487 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
490 /* Older versions of the struct lacked the otime setting */
491 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
494 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
496 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
497 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
499 ret
->subvol_id
= subvol_id
;
500 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
502 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
503 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
504 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
510 /* Increase search key by one, to read the next item, if we can. */
511 if (!btrfs_ioctl_search_args_inc(&args
))
522 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
524 struct btrfs_ioctl_search_args args
= {
525 /* Tree of quota items */
526 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
528 /* The object ID is always 0 */
529 .key
.min_objectid
= 0,
530 .key
.max_objectid
= 0,
532 /* Look precisely for the quota items */
533 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
534 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
536 /* No restrictions on the other components */
537 .key
.min_transid
= 0,
538 .key
.max_transid
= (uint64_t) -1,
541 bool found_info
= false, found_limit
= false;
548 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
552 r
= btrfs_is_filesystem(fd
);
559 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
561 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
562 const struct btrfs_ioctl_search_header
*sh
;
565 args
.key
.nr_items
= 256;
566 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
567 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
573 if (args
.key
.nr_items
<= 0)
576 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
578 /* Make sure we start the next search at least from this entry */
579 btrfs_ioctl_search_args_set(&args
, sh
);
581 if (sh
->objectid
!= 0)
583 if (sh
->offset
!= qgroupid
)
586 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
587 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
589 ret
->referenced
= le64toh(qii
->rfer
);
590 ret
->exclusive
= le64toh(qii
->excl
);
594 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
595 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
597 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
598 ret
->referenced_max
= le64toh(qli
->max_rfer
);
600 ret
->referenced_max
= (uint64_t) -1;
602 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
603 ret
->exclusive_max
= le64toh(qli
->max_excl
);
605 ret
->exclusive_max
= (uint64_t) -1;
610 if (found_info
&& found_limit
)
614 /* Increase search key by one, to read the next item, if we can. */
615 if (!btrfs_ioctl_search_args_inc(&args
))
620 if (!found_limit
&& !found_info
)
624 ret
->referenced
= (uint64_t) -1;
625 ret
->exclusive
= (uint64_t) -1;
629 ret
->referenced_max
= (uint64_t) -1;
630 ret
->exclusive_max
= (uint64_t) -1;
636 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
637 _cleanup_close_
int fd
= -1;
639 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
643 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
646 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
647 uint64_t level
, lowest
= (uint64_t) -1, lowest_qgroupid
= 0;
648 _cleanup_free_
uint64_t *qgroups
= NULL
;
654 /* This finds the "subtree" qgroup for a specific
655 * subvolume. This only works for subvolumes that have been
656 * prepared with btrfs_subvol_auto_qgroup_fd() with
657 * insert_intermediary_qgroup=true (or equivalent). For others
658 * it will return the leaf qgroup instead. The two cases may
659 * be distuingished via the return value, which is 1 in case
660 * an appropriate "subtree" qgroup was found, and 0
663 if (subvol_id
== 0) {
664 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
669 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
672 if (level
!= 0) /* Input must be a leaf qgroup */
675 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
679 for (i
= 0; i
< n
; i
++) {
682 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
689 if (lowest
== (uint64_t) -1 || level
< lowest
) {
690 lowest_qgroupid
= qgroups
[i
];
695 if (lowest
== (uint64_t) -1) {
696 /* No suitable higher-level qgroup found, let's return
697 * the leaf qgroup instead, and indicate that with the
704 *ret
= lowest_qgroupid
;
708 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
715 /* This determines the quota data of the qgroup with the
716 * lowest level, that shares the id part with the specified
717 * subvolume. This is useful for determining the quota data
718 * for entire subvolume subtrees, as long as the subtrees have
719 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
722 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
726 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
729 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
730 _cleanup_close_
int fd
= -1;
732 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
736 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
739 int btrfs_defrag_fd(int fd
) {
744 r
= fd_verify_regular(fd
);
748 if (ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
) < 0)
754 int btrfs_defrag(const char *p
) {
755 _cleanup_close_
int fd
= -1;
757 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
761 return btrfs_defrag_fd(fd
);
764 int btrfs_quota_enable_fd(int fd
, bool b
) {
765 struct btrfs_ioctl_quota_ctl_args args
= {
766 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
772 r
= btrfs_is_filesystem(fd
);
778 if (ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
) < 0)
784 int btrfs_quota_enable(const char *path
, bool b
) {
785 _cleanup_close_
int fd
= -1;
787 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
791 return btrfs_quota_enable_fd(fd
, b
);
794 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
796 struct btrfs_ioctl_qgroup_limit_args args
= {
797 .lim
.max_rfer
= referenced_max
,
798 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
806 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
810 r
= btrfs_is_filesystem(fd
);
817 args
.qgroupid
= qgroupid
;
820 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
822 if (errno
== EBUSY
&& c
< 10) {
823 (void) btrfs_quota_scan_wait(fd
);
836 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, uint64_t referenced_max
) {
837 _cleanup_close_
int fd
= -1;
839 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
843 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
846 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
852 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
856 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
859 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
860 _cleanup_close_
int fd
= -1;
862 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
866 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
869 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
872 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
875 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
878 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
882 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
886 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
889 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
894 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
896 struct btrfs_ioctl_qgroup_create_args args
= {
898 .qgroupid
= qgroupid
,
903 r
= btrfs_is_filesystem(fd
);
910 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
912 /* If quota is not enabled, we get EINVAL. Turn this into a recognizable error */
916 if (errno
== EBUSY
&& c
< 10) {
917 (void) btrfs_quota_scan_wait(fd
);
930 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
931 return qgroup_create_or_destroy(fd
, true, qgroupid
);
934 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
935 return qgroup_create_or_destroy(fd
, false, qgroupid
);
938 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
939 _cleanup_free_
uint64_t *qgroups
= NULL
;
943 /* Destroys the specified qgroup, but unassigns it from all
944 * its parents first. Also, it recursively destroys all
945 * qgroups it is assigned to that have the same id part of the
946 * qgroupid as the specified group. */
948 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
952 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
956 for (i
= 0; i
< n
; i
++) {
959 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
963 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
970 /* The parent qgroupid shares the same id part with
971 * us? If so, destroy it too. */
973 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
976 return btrfs_qgroup_destroy(fd
, qgroupid
);
979 int btrfs_quota_scan_start(int fd
) {
980 struct btrfs_ioctl_quota_rescan_args args
= {};
984 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
) < 0)
990 int btrfs_quota_scan_wait(int fd
) {
993 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
) < 0)
999 int btrfs_quota_scan_ongoing(int fd
) {
1000 struct btrfs_ioctl_quota_rescan_args args
= {};
1004 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
1007 return !!args
.flags
;
1010 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
1011 struct btrfs_ioctl_qgroup_assign_args args
= {
1019 r
= btrfs_is_filesystem(fd
);
1026 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1028 if (errno
== EBUSY
&& c
< 10) {
1029 (void) btrfs_quota_scan_wait(fd
);
1039 /* If the return value is > 0, we need to request a rescan */
1041 (void) btrfs_quota_scan_start(fd
);
1046 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1047 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1050 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1051 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1054 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1055 struct btrfs_ioctl_search_args args
= {
1056 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1058 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1059 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1061 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1062 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1064 .key
.min_transid
= 0,
1065 .key
.max_transid
= (uint64_t) -1,
1068 struct btrfs_ioctl_vol_args vol_args
= {};
1069 _cleanup_close_
int subvol_fd
= -1;
1071 bool made_writable
= false;
1077 if (fstat(fd
, &st
) < 0)
1080 if (!S_ISDIR(st
.st_mode
))
1083 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1087 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1088 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1089 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1090 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1091 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1092 * let's prefer ENOTTY over EPERM/EACCES though. */
1093 r
= btrfs_is_subvol_fd(subvol_fd
);
1096 if (r
== 0) /* Not a btrfs subvolume */
1099 if (subvol_id
== 0) {
1100 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1105 /* First, try to remove the subvolume. If it happens to be
1106 * already empty, this will just work. */
1107 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1108 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1109 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1112 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1115 /* OK, the subvolume is not empty, let's look for child
1116 * subvolumes, and remove them, first */
1118 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1120 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1121 const struct btrfs_ioctl_search_header
*sh
;
1124 args
.key
.nr_items
= 256;
1125 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1128 if (args
.key
.nr_items
<= 0)
1131 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1132 _cleanup_free_
char *p
= NULL
;
1133 const struct btrfs_root_ref
*ref
;
1134 struct btrfs_ioctl_ino_lookup_args ino_args
;
1136 btrfs_ioctl_search_args_set(&args
, sh
);
1138 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1140 if (sh
->offset
!= subvol_id
)
1143 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1145 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1150 ino_args
.treeid
= subvol_id
;
1151 ino_args
.objectid
= htole64(ref
->dirid
);
1153 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1156 if (!made_writable
) {
1157 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1161 made_writable
= true;
1164 if (isempty(ino_args
.name
))
1165 /* Subvolume is in the top-level
1166 * directory of the subvolume. */
1167 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1169 _cleanup_close_
int child_fd
= -1;
1171 /* Subvolume is somewhere further down,
1172 * hence we need to open the
1173 * containing directory first */
1175 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1179 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1185 /* Increase search key by one, to read the next item, if we can. */
1186 if (!btrfs_ioctl_search_args_inc(&args
))
1190 /* OK, the child subvolumes should all be gone now, let's try
1191 * again to remove the subvolume */
1192 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1195 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1199 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1200 _cleanup_close_
int fd
= -1;
1201 const char *subvolume
;
1206 r
= extract_subvolume_name(path
, &subvolume
);
1210 fd
= open_parent(path
, O_CLOEXEC
, 0);
1214 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1217 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1218 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1221 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1223 struct btrfs_ioctl_search_args args
= {
1224 /* Tree of quota items */
1225 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1227 /* The object ID is always 0 */
1228 .key
.min_objectid
= 0,
1229 .key
.max_objectid
= 0,
1231 /* Look precisely for the quota items */
1232 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1233 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1235 /* For our qgroup */
1236 .key
.min_offset
= old_qgroupid
,
1237 .key
.max_offset
= old_qgroupid
,
1239 /* No restrictions on the other components */
1240 .key
.min_transid
= 0,
1241 .key
.max_transid
= (uint64_t) -1,
1246 r
= btrfs_is_filesystem(fd
);
1252 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1253 const struct btrfs_ioctl_search_header
*sh
;
1256 args
.key
.nr_items
= 256;
1257 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1258 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1264 if (args
.key
.nr_items
<= 0)
1267 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1268 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1269 struct btrfs_ioctl_qgroup_limit_args qargs
;
1272 /* Make sure we start the next search at least from this entry */
1273 btrfs_ioctl_search_args_set(&args
, sh
);
1275 if (sh
->objectid
!= 0)
1277 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1279 if (sh
->offset
!= old_qgroupid
)
1282 /* We found the entry, now copy things over. */
1284 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1285 .qgroupid
= new_qgroupid
,
1287 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1288 .lim
.max_excl
= le64toh(qli
->max_excl
),
1289 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1290 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1292 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1293 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1294 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1295 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1299 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1300 if (errno
== EBUSY
&& c
< 10) {
1301 (void) btrfs_quota_scan_wait(fd
);
1313 /* Increase search key by one, to read the next item, if we can. */
1314 if (!btrfs_ioctl_search_args_inc(&args
))
1321 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1322 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1323 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1324 int n_old_qgroups
, n_old_parent_qgroups
, r
, i
;
1325 uint64_t old_parent_id
;
1329 /* Copies a reduced form of quota information from the old to
1330 * the new subvolume. */
1332 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1333 if (n_old_qgroups
<= 0) /* Nothing to copy */
1334 return n_old_qgroups
;
1336 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1338 /* We have no parent, hence nothing to copy. */
1339 n_old_parent_qgroups
= 0;
1343 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1344 if (n_old_parent_qgroups
< 0)
1345 return n_old_parent_qgroups
;
1348 for (i
= 0; i
< n_old_qgroups
; i
++) {
1352 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1356 if (id
== old_subvol_id
) {
1357 /* The old subvolume was member of a qgroup
1358 * that had the same id, but a different level
1359 * as it self. Let's set up something similar
1360 * in the destination. */
1361 insert_intermediary_qgroup
= true;
1365 for (j
= 0; j
< n_old_parent_qgroups
; j
++)
1366 if (old_parent_qgroups
[j
] == old_qgroups
[i
]) {
1367 /* The old subvolume shared a common
1368 * parent qgroup with its parent
1369 * subvolume. Let's set up something
1370 * similar in the destination. */
1371 copy_from_parent
= true;
1375 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1378 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1381 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1382 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1386 /* First copy the leaf limits */
1387 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1392 /* Then, try to copy the subtree limits, if there are any. */
1393 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1399 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1405 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1412 static int subvol_snapshot_children(
1415 const char *subvolume
,
1416 uint64_t old_subvol_id
,
1417 BtrfsSnapshotFlags flags
) {
1419 struct btrfs_ioctl_search_args args
= {
1420 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1422 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1423 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1425 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1426 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1428 .key
.min_transid
= 0,
1429 .key
.max_transid
= (uint64_t) -1,
1432 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1433 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1436 _cleanup_close_
int subvolume_fd
= -1;
1437 uint64_t new_subvol_id
;
1440 assert(old_fd
>= 0);
1441 assert(new_fd
>= 0);
1444 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1446 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1449 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1450 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1453 if (old_subvol_id
== 0) {
1454 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1459 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1463 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1464 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1466 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1468 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1469 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1474 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1476 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1477 const struct btrfs_ioctl_search_header
*sh
;
1480 args
.key
.nr_items
= 256;
1481 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1484 if (args
.key
.nr_items
<= 0)
1487 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1488 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1489 struct btrfs_ioctl_ino_lookup_args ino_args
;
1490 const struct btrfs_root_ref
*ref
;
1491 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1493 btrfs_ioctl_search_args_set(&args
, sh
);
1495 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1498 /* Avoid finding the source subvolume a second
1500 if (sh
->offset
!= old_subvol_id
)
1503 /* Avoid running into loops if the new
1504 * subvolume is below the old one. */
1505 if (sh
->objectid
== new_subvol_id
)
1508 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1509 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1514 ino_args
.treeid
= old_subvol_id
;
1515 ino_args
.objectid
= htole64(ref
->dirid
);
1517 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1520 c
= path_join(ino_args
.name
, p
);
1524 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1525 if (old_child_fd
< 0)
1528 np
= path_join(subvolume
, ino_args
.name
);
1532 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1533 if (new_child_fd
< 0)
1536 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1537 /* If the snapshot is read-only we
1538 * need to mark it writable
1539 * temporarily, to put the subsnapshot
1542 if (subvolume_fd
< 0) {
1543 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1544 if (subvolume_fd
< 0)
1548 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1553 /* When btrfs clones the subvolumes, child
1554 * subvolumes appear as empty directories. Remove
1555 * them, so that we can create a new snapshot
1557 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1560 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1561 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1566 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1568 /* Restore the readonly flag */
1569 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1572 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1573 if (r
>= 0 && k
< 0)
1581 /* Increase search key by one, to read the next item, if we can. */
1582 if (!btrfs_ioctl_search_args_inc(&args
))
1586 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1587 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1592 int btrfs_subvol_snapshot_fd_full(
1594 const char *new_path
,
1595 BtrfsSnapshotFlags flags
,
1596 copy_progress_path_t progress_path
,
1597 copy_progress_bytes_t progress_bytes
,
1600 _cleanup_close_
int new_fd
= -1;
1601 const char *subvolume
;
1604 assert(old_fd
>= 0);
1607 r
= btrfs_is_subvol_fd(old_fd
);
1611 bool plain_directory
= false;
1613 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1614 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1617 r
= btrfs_subvol_make(new_path
);
1618 if (r
== -ENOTTY
&& (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1619 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1620 if (mkdir(new_path
, 0755) < 0)
1623 plain_directory
= true;
1627 r
= copy_directory_fd_full(old_fd
, new_path
, COPY_MERGE
|COPY_REFLINK
|COPY_SAME_MOUNT
, progress_path
, progress_bytes
, userdata
);
1631 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1633 if (plain_directory
) {
1634 /* Plain directories have no recursive read-only flag, but something pretty close to
1635 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1637 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1638 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1640 r
= btrfs_subvol_set_read_only(new_path
, true);
1649 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1653 r
= extract_subvolume_name(new_path
, &subvolume
);
1657 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1661 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1664 int btrfs_subvol_snapshot_full(
1665 const char *old_path
,
1666 const char *new_path
,
1667 BtrfsSnapshotFlags flags
,
1668 copy_progress_path_t progress_path
,
1669 copy_progress_bytes_t progress_bytes
,
1672 _cleanup_close_
int old_fd
= -1;
1677 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1681 return btrfs_subvol_snapshot_fd_full(old_fd
, new_path
, flags
, progress_path
, progress_bytes
, userdata
);
1684 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1686 struct btrfs_ioctl_search_args args
= {
1687 /* Tree of quota items */
1688 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1690 /* Look precisely for the quota relation items */
1691 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1692 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1694 /* No restrictions on the other components */
1695 .key
.min_offset
= 0,
1696 .key
.max_offset
= (uint64_t) -1,
1698 .key
.min_transid
= 0,
1699 .key
.max_transid
= (uint64_t) -1,
1702 _cleanup_free_
uint64_t *items
= NULL
;
1703 size_t n_items
= 0, n_allocated
= 0;
1709 if (qgroupid
== 0) {
1710 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1714 r
= btrfs_is_filesystem(fd
);
1721 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1723 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1724 const struct btrfs_ioctl_search_header
*sh
;
1727 args
.key
.nr_items
= 256;
1728 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1729 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1735 if (args
.key
.nr_items
<= 0)
1738 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1740 /* Make sure we start the next search at least from this entry */
1741 btrfs_ioctl_search_args_set(&args
, sh
);
1743 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1745 if (sh
->offset
< sh
->objectid
)
1747 if (sh
->objectid
!= qgroupid
)
1750 if (!GREEDY_REALLOC(items
, n_allocated
, n_items
+1))
1753 items
[n_items
++] = sh
->offset
;
1756 /* Increase search key by one, to read the next item, if we can. */
1757 if (!btrfs_ioctl_search_args_inc(&args
))
1766 *ret
= TAKE_PTR(items
);
1768 return (int) n_items
;
1771 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1772 _cleanup_free_
uint64_t *qgroups
= NULL
;
1773 uint64_t parent_subvol
;
1774 bool changed
= false;
1780 * Sets up the specified subvolume's qgroup automatically in
1783 * If insert_intermediary_qgroup is false, the subvolume's
1784 * leaf qgroup will be assigned to the same parent qgroups as
1785 * the subvolume's parent subvolume.
1787 * If insert_intermediary_qgroup is true a new intermediary
1788 * higher-level qgroup is created, with a higher level number,
1789 * but reusing the id of the subvolume. The level number is
1790 * picked as one smaller than the lowest level qgroup the
1791 * parent subvolume is a member of. If the parent subvolume's
1792 * leaf qgroup is assigned to no higher-level qgroup a new
1793 * qgroup of level 255 is created instead. Either way, the new
1794 * qgroup is then assigned to the parent's higher-level
1795 * qgroup, and the subvolume itself is assigned to it.
1797 * If the subvolume is already assigned to a higher level
1798 * qgroup, no operation is executed.
1800 * Effectively this means: regardless if
1801 * insert_intermediary_qgroup is true or not, after this
1802 * function is invoked the subvolume will be accounted within
1803 * the same qgroups as the parent. However, if it is true, it
1804 * will also get its own higher-level qgroup, which may in
1805 * turn be used by subvolumes created beneath this subvolume
1808 * This hence defines a simple default qgroup setup for
1809 * subvolumes, as long as this function is invoked on each
1810 * created subvolume: each subvolume is always accounting
1811 * together with its immediate parents. Optionally, if
1812 * insert_intermediary_qgroup is true, it will also get a
1813 * qgroup that then includes all its own child subvolumes.
1816 if (subvol_id
== 0) {
1817 r
= btrfs_is_subvol_fd(fd
);
1823 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1828 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1831 if (n
> 0) /* already parent qgroups set up, let's bail */
1834 qgroups
= mfree(qgroups
);
1836 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1838 /* No parent, hence no qgroup memberships */
1843 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1848 if (insert_intermediary_qgroup
) {
1849 uint64_t lowest
= 256, new_qgroupid
;
1850 bool created
= false;
1853 /* Determine the lowest qgroup that the parent
1854 * subvolume is assigned to. */
1856 for (i
= 0; i
< n
; i
++) {
1859 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, NULL
);
1867 if (lowest
<= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1870 r
= btrfs_qgroupid_make(lowest
- 1, subvol_id
, &new_qgroupid
);
1874 /* Create the new intermediary group, unless it already exists */
1875 r
= btrfs_qgroup_create(fd
, new_qgroupid
);
1876 if (r
< 0 && r
!= -EEXIST
)
1879 changed
= created
= true;
1881 for (i
= 0; i
< n
; i
++) {
1882 r
= btrfs_qgroup_assign(fd
, new_qgroupid
, qgroups
[i
]);
1883 if (r
< 0 && r
!= -EEXIST
) {
1885 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1893 r
= btrfs_qgroup_assign(fd
, subvol_id
, new_qgroupid
);
1894 if (r
< 0 && r
!= -EEXIST
) {
1896 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1905 /* Assign our subvolume to all the same qgroups as the parent */
1907 for (i
= 0; i
< n
; i
++) {
1908 r
= btrfs_qgroup_assign(fd
, subvol_id
, qgroups
[i
]);
1909 if (r
< 0 && r
!= -EEXIST
)
1919 int btrfs_subvol_auto_qgroup(const char *path
, uint64_t subvol_id
, bool create_intermediary_qgroup
) {
1920 _cleanup_close_
int fd
= -1;
1922 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1926 return btrfs_subvol_auto_qgroup_fd(fd
, subvol_id
, create_intermediary_qgroup
);
1929 int btrfs_subvol_get_parent(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
1931 struct btrfs_ioctl_search_args args
= {
1932 /* Tree of tree roots */
1933 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1935 /* Look precisely for the subvolume items */
1936 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1937 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1939 /* No restrictions on the other components */
1940 .key
.min_offset
= 0,
1941 .key
.max_offset
= (uint64_t) -1,
1943 .key
.min_transid
= 0,
1944 .key
.max_transid
= (uint64_t) -1,
1951 if (subvol_id
== 0) {
1952 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1956 r
= btrfs_is_filesystem(fd
);
1963 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
1965 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1966 const struct btrfs_ioctl_search_header
*sh
;
1969 args
.key
.nr_items
= 256;
1970 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1971 return negative_errno();
1973 if (args
.key
.nr_items
<= 0)
1976 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1978 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1980 if (sh
->objectid
!= subvol_id
)