1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <linux/btrfs_tree.h>
8 #include <linux/loop.h>
9 #include <linux/magic.h>
13 #include <sys/ioctl.h>
14 #include <sys/sysmacros.h>
17 #include "alloc-util.h"
18 #include "blockdev-util.h"
19 #include "btrfs-util.h"
20 #include "chattr-util.h"
27 #include "path-util.h"
29 #include "smack-util.h"
30 #include "sparse-endian.h"
31 #include "stat-util.h"
32 #include "string-util.h"
33 #include "time-util.h"
35 /* WARNING: Be careful with file system ioctls! When we get an fd, we
36 * need to make sure it either refers to only a regular file or
37 * directory, or that it is located on btrfs, before invoking any
38 * btrfs ioctls. The ioctl numbers are reused by some device drivers
39 * (such as DRM), and hence might have bad effects when invoked on
40 * device nodes (that reference drivers) rather than fds to normal
41 * files or directories. */
43 static int validate_subvolume_name(const char *name
) {
45 if (!filename_is_valid(name
))
48 if (strlen(name
) > BTRFS_SUBVOL_NAME_MAX
)
54 static int extract_subvolume_name(const char *path
, const char **subvolume
) {
63 r
= validate_subvolume_name(fn
);
71 int btrfs_is_subvol_fd(int fd
) {
76 /* On btrfs subvolumes always have the inode 256 */
78 if (fstat(fd
, &st
) < 0)
81 if (!btrfs_might_be_subvol(&st
))
84 return fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
87 int btrfs_is_subvol(const char *path
) {
88 _cleanup_close_
int fd
= -1;
92 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
96 return btrfs_is_subvol_fd(fd
);
99 int btrfs_subvol_make_fd(int fd
, const char *subvolume
) {
100 struct btrfs_ioctl_vol_args args
= {};
101 _cleanup_close_
int real_fd
= -1;
106 r
= validate_subvolume_name(subvolume
);
110 /* If an O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal
112 fd
= fd_reopen_condition(fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
, O_PATH
|O_DIRECTORY
, &real_fd
);
116 strncpy(args
.name
, subvolume
, sizeof(args
.name
)-1);
118 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_SUBVOL_CREATE
, &args
));
121 int btrfs_subvol_make(const char *path
) {
122 _cleanup_close_
int fd
= -1;
123 const char *subvolume
;
128 r
= extract_subvolume_name(path
, &subvolume
);
132 fd
= open_parent(path
, O_CLOEXEC
, 0);
136 return btrfs_subvol_make_fd(fd
, subvolume
);
139 int btrfs_subvol_make_fallback(const char *path
, mode_t mode
) {
140 mode_t old
, combined
;
145 /* Let's work like mkdir(), i.e. take the specified mode, and mask it with the current umask. */
147 combined
= old
| ~mode
;
148 if (combined
!= ~mode
)
150 r
= btrfs_subvol_make(path
);
154 return 1; /* subvol worked */
158 if (mkdir(path
, mode
) < 0)
161 return 0; /* plain directory */
164 int btrfs_subvol_set_read_only_fd(int fd
, bool b
) {
165 uint64_t flags
, nflags
;
170 if (fstat(fd
, &st
) < 0)
173 if (!btrfs_might_be_subvol(&st
))
176 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
179 nflags
= UPDATE_FLAG(flags
, BTRFS_SUBVOL_RDONLY
, b
);
183 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &nflags
));
186 int btrfs_subvol_set_read_only(const char *path
, bool b
) {
187 _cleanup_close_
int fd
= -1;
189 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
193 return btrfs_subvol_set_read_only_fd(fd
, b
);
196 int btrfs_subvol_get_read_only_fd(int fd
) {
202 if (fstat(fd
, &st
) < 0)
205 if (!btrfs_might_be_subvol(&st
))
208 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
211 return !!(flags
& BTRFS_SUBVOL_RDONLY
);
214 int btrfs_reflink(int infd
, int outfd
) {
220 /* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
222 r
= fd_verify_regular(outfd
);
226 return RET_NERRNO(ioctl(outfd
, BTRFS_IOC_CLONE
, infd
));
229 int btrfs_clone_range(int infd
, uint64_t in_offset
, int outfd
, uint64_t out_offset
, uint64_t sz
) {
230 struct btrfs_ioctl_clone_range_args args
= {
232 .src_offset
= in_offset
,
234 .dest_offset
= out_offset
,
241 r
= fd_verify_regular(outfd
);
245 return RET_NERRNO(ioctl(outfd
, BTRFS_IOC_CLONE_RANGE
, &args
));
248 int btrfs_get_block_device_fd(int fd
, dev_t
*dev
) {
249 struct btrfs_ioctl_fs_info_args fsi
= {};
250 _cleanup_close_
int regfd
= -1;
257 fd
= fd_reopen_condition(fd
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
, O_PATH
, ®fd
);
261 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
267 if (ioctl(fd
, BTRFS_IOC_FS_INFO
, &fsi
) < 0)
270 /* We won't do this for btrfs RAID */
271 if (fsi
.num_devices
!= 1) {
276 for (id
= 1; id
<= fsi
.max_id
; id
++) {
277 struct btrfs_ioctl_dev_info_args di
= {
282 if (ioctl(fd
, BTRFS_IOC_DEV_INFO
, &di
) < 0) {
289 /* For the root fs — when no initrd is involved — btrfs returns /dev/root on any kernels from
290 * the past few years. That sucks, as we have no API to determine the actual root then. let's
291 * return an recognizable error for this case, so that the caller can maybe print a nice
292 * message about this.
294 * https://bugzilla.kernel.org/show_bug.cgi?id=89721 */
295 if (path_equal((char*) di
.path
, "/dev/root"))
298 if (stat((char*) di
.path
, &st
) < 0)
301 if (!S_ISBLK(st
.st_mode
))
304 if (major(st
.st_rdev
) == 0)
314 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
315 _cleanup_close_
int fd
= -1;
320 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
324 return btrfs_get_block_device_fd(fd
, dev
);
327 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
328 struct btrfs_ioctl_ino_lookup_args args
= {
329 .objectid
= BTRFS_FIRST_FREE_OBJECTID
336 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
342 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
349 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
350 _cleanup_close_
int subvol_fd
= -1;
355 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
359 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
362 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
365 /* the objectid, type, offset together make up the btrfs key,
366 * which is considered a single 136byte integer when
367 * comparing. This call increases the counter by one, dealing
368 * with the overflow between the overflows */
370 if (args
->key
.min_offset
< UINT64_MAX
) {
371 args
->key
.min_offset
++;
375 if (args
->key
.min_type
< UINT8_MAX
) {
376 args
->key
.min_type
++;
377 args
->key
.min_offset
= 0;
381 if (args
->key
.min_objectid
< UINT64_MAX
) {
382 args
->key
.min_objectid
++;
383 args
->key
.min_offset
= 0;
384 args
->key
.min_type
= 0;
391 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
395 args
->key
.min_objectid
= h
->objectid
;
396 args
->key
.min_type
= h
->type
;
397 args
->key
.min_offset
= h
->offset
;
400 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
405 /* Compare min and max */
407 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
411 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
415 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
418 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
420 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
421 (i) < (args).key.nr_items; \
423 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
425 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
426 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
428 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
429 struct btrfs_ioctl_search_args args
= {
430 /* Tree of tree roots */
431 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
433 /* Look precisely for the subvolume items */
434 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
435 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
438 .key
.max_offset
= UINT64_MAX
,
440 /* No restrictions on the other components */
441 .key
.min_transid
= 0,
442 .key
.max_transid
= UINT64_MAX
,
451 if (subvol_id
== 0) {
452 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
456 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
463 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
465 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
466 const struct btrfs_ioctl_search_header
*sh
;
469 args
.key
.nr_items
= 256;
470 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
473 if (args
.key
.nr_items
<= 0)
476 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
478 const struct btrfs_root_item
*ri
;
480 /* Make sure we start the next search at least from this entry */
481 btrfs_ioctl_search_args_set(&args
, sh
);
483 if (sh
->objectid
!= subvol_id
)
485 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
488 /* Older versions of the struct lacked the otime setting */
489 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
492 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
494 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
495 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
497 ret
->subvol_id
= subvol_id
;
498 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
500 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
501 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
502 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
508 /* Increase search key by one, to read the next item, if we can. */
509 if (!btrfs_ioctl_search_args_inc(&args
))
520 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
522 struct btrfs_ioctl_search_args args
= {
523 /* Tree of quota items */
524 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
526 /* The object ID is always 0 */
527 .key
.min_objectid
= 0,
528 .key
.max_objectid
= 0,
530 /* Look precisely for the quota items */
531 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
532 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
534 /* No restrictions on the other components */
535 .key
.min_transid
= 0,
536 .key
.max_transid
= UINT64_MAX
,
539 bool found_info
= false, found_limit
= false;
546 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
550 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
557 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
559 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
560 const struct btrfs_ioctl_search_header
*sh
;
563 args
.key
.nr_items
= 256;
564 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
565 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
571 if (args
.key
.nr_items
<= 0)
574 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
576 /* Make sure we start the next search at least from this entry */
577 btrfs_ioctl_search_args_set(&args
, sh
);
579 if (sh
->objectid
!= 0)
581 if (sh
->offset
!= qgroupid
)
584 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
585 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
587 ret
->referenced
= le64toh(qii
->rfer
);
588 ret
->exclusive
= le64toh(qii
->excl
);
592 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
593 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
595 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
596 ret
->referenced_max
= le64toh(qli
->max_rfer
);
598 ret
->referenced_max
= UINT64_MAX
;
600 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
601 ret
->exclusive_max
= le64toh(qli
->max_excl
);
603 ret
->exclusive_max
= UINT64_MAX
;
608 if (found_info
&& found_limit
)
612 /* Increase search key by one, to read the next item, if we can. */
613 if (!btrfs_ioctl_search_args_inc(&args
))
618 if (!found_limit
&& !found_info
)
622 ret
->referenced
= UINT64_MAX
;
623 ret
->exclusive
= UINT64_MAX
;
627 ret
->referenced_max
= UINT64_MAX
;
628 ret
->exclusive_max
= UINT64_MAX
;
634 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
635 _cleanup_close_
int fd
= -1;
637 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
641 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
644 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
645 uint64_t level
, lowest
= UINT64_MAX
, lowest_qgroupid
= 0;
646 _cleanup_free_
uint64_t *qgroups
= NULL
;
652 /* This finds the "subtree" qgroup for a specific
653 * subvolume. This only works for subvolumes that have been
654 * prepared with btrfs_subvol_auto_qgroup_fd() with
655 * insert_intermediary_qgroup=true (or equivalent). For others
656 * it will return the leaf qgroup instead. The two cases may
657 * be distinguished via the return value, which is 1 in case
658 * an appropriate "subtree" qgroup was found, and 0
661 if (subvol_id
== 0) {
662 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
667 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
670 if (level
!= 0) /* Input must be a leaf qgroup */
673 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
677 for (int i
= 0; i
< n
; i
++) {
680 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
687 if (lowest
== UINT64_MAX
|| level
< lowest
) {
688 lowest_qgroupid
= qgroups
[i
];
693 if (lowest
== UINT64_MAX
) {
694 /* No suitable higher-level qgroup found, let's return
695 * the leaf qgroup instead, and indicate that with the
702 *ret
= lowest_qgroupid
;
706 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
713 /* This determines the quota data of the qgroup with the
714 * lowest level, that shares the id part with the specified
715 * subvolume. This is useful for determining the quota data
716 * for entire subvolume subtrees, as long as the subtrees have
717 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
720 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
724 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
727 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
728 _cleanup_close_
int fd
= -1;
730 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
734 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
737 int btrfs_defrag_fd(int fd
) {
742 r
= fd_verify_regular(fd
);
746 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
));
749 int btrfs_defrag(const char *p
) {
750 _cleanup_close_
int fd
= -1;
752 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
756 return btrfs_defrag_fd(fd
);
759 int btrfs_quota_enable_fd(int fd
, bool b
) {
760 struct btrfs_ioctl_quota_ctl_args args
= {
761 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
767 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
773 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
));
776 int btrfs_quota_enable(const char *path
, bool b
) {
777 _cleanup_close_
int fd
= -1;
779 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
783 return btrfs_quota_enable_fd(fd
, b
);
786 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
788 struct btrfs_ioctl_qgroup_limit_args args
= {
789 .lim
.max_rfer
= referenced_max
,
790 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
797 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
801 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
808 args
.qgroupid
= qgroupid
;
810 for (unsigned c
= 0;; c
++) {
811 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
813 if (errno
== EBUSY
&& c
< 10) {
814 (void) btrfs_quota_scan_wait(fd
);
827 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, uint64_t referenced_max
) {
828 _cleanup_close_
int fd
= -1;
830 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
834 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
837 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
843 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
847 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
850 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
851 _cleanup_close_
int fd
= -1;
853 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
857 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
860 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
863 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
866 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
869 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
873 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
877 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
880 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
885 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
887 struct btrfs_ioctl_qgroup_create_args args
= {
889 .qgroupid
= qgroupid
,
893 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
899 for (unsigned c
= 0;; c
++) {
900 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
902 /* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
903 * ENOTCONN. Let's always convert this to ENOTCONN to make this recognizable
904 * everywhere the same way. */
906 if (IN_SET(errno
, EINVAL
, ENOTCONN
))
909 if (errno
== EBUSY
&& c
< 10) {
910 (void) btrfs_quota_scan_wait(fd
);
923 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
924 return qgroup_create_or_destroy(fd
, true, qgroupid
);
927 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
928 return qgroup_create_or_destroy(fd
, false, qgroupid
);
931 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
932 _cleanup_free_
uint64_t *qgroups
= NULL
;
936 /* Destroys the specified qgroup, but unassigns it from all
937 * its parents first. Also, it recursively destroys all
938 * qgroups it is assigned to that have the same id part of the
939 * qgroupid as the specified group. */
941 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
945 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
949 for (int i
= 0; i
< n
; i
++) {
952 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
956 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
963 /* The parent qgroupid shares the same id part with
964 * us? If so, destroy it too. */
966 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
969 return btrfs_qgroup_destroy(fd
, qgroupid
);
972 int btrfs_quota_scan_start(int fd
) {
973 struct btrfs_ioctl_quota_rescan_args args
= {};
977 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
));
980 int btrfs_quota_scan_wait(int fd
) {
983 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
));
986 int btrfs_quota_scan_ongoing(int fd
) {
987 struct btrfs_ioctl_quota_rescan_args args
= {};
991 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
997 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
998 struct btrfs_ioctl_qgroup_assign_args args
= {
1005 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1011 for (unsigned c
= 0;; c
++) {
1012 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1014 if (errno
== EBUSY
&& c
< 10) {
1015 (void) btrfs_quota_scan_wait(fd
);
1025 /* If the return value is > 0, we need to request a rescan */
1027 (void) btrfs_quota_scan_start(fd
);
1032 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1033 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1036 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1037 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1040 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1041 struct btrfs_ioctl_search_args args
= {
1042 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1044 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1045 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1047 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1048 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1050 .key
.min_transid
= 0,
1051 .key
.max_transid
= UINT64_MAX
,
1054 struct btrfs_ioctl_vol_args vol_args
= {};
1055 _cleanup_close_
int subvol_fd
= -1;
1057 bool made_writable
= false;
1063 if (fstat(fd
, &st
) < 0)
1066 if (!S_ISDIR(st
.st_mode
))
1069 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1073 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1074 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1075 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1076 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1077 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1078 * let's prefer ENOTTY over EPERM/EACCES though. */
1079 r
= btrfs_is_subvol_fd(subvol_fd
);
1082 if (r
== 0) /* Not a btrfs subvolume */
1085 if (subvol_id
== 0) {
1086 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1091 /* First, try to remove the subvolume. If it happens to be
1092 * already empty, this will just work. */
1093 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1094 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1095 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1098 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1101 /* OK, the subvolume is not empty, let's look for child
1102 * subvolumes, and remove them, first */
1104 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1106 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1107 const struct btrfs_ioctl_search_header
*sh
;
1110 args
.key
.nr_items
= 256;
1111 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1114 if (args
.key
.nr_items
<= 0)
1117 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1118 _cleanup_free_
char *p
= NULL
;
1119 const struct btrfs_root_ref
*ref
;
1121 btrfs_ioctl_search_args_set(&args
, sh
);
1123 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1125 if (sh
->offset
!= subvol_id
)
1128 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1130 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1134 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1135 .treeid
= subvol_id
,
1136 .objectid
= htole64(ref
->dirid
),
1139 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1142 if (!made_writable
) {
1143 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1147 made_writable
= true;
1150 if (isempty(ino_args
.name
))
1151 /* Subvolume is in the top-level
1152 * directory of the subvolume. */
1153 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1155 _cleanup_close_
int child_fd
= -1;
1157 /* Subvolume is somewhere further down,
1158 * hence we need to open the
1159 * containing directory first */
1161 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1165 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1171 /* Increase search key by one, to read the next item, if we can. */
1172 if (!btrfs_ioctl_search_args_inc(&args
))
1176 /* OK, the child subvolumes should all be gone now, let's try
1177 * again to remove the subvolume */
1178 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1181 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1185 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1186 _cleanup_close_
int fd
= -1;
1187 const char *subvolume
;
1192 r
= extract_subvolume_name(path
, &subvolume
);
1196 fd
= open_parent(path
, O_CLOEXEC
, 0);
1200 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1203 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1204 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1207 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1209 struct btrfs_ioctl_search_args args
= {
1210 /* Tree of quota items */
1211 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1213 /* The object ID is always 0 */
1214 .key
.min_objectid
= 0,
1215 .key
.max_objectid
= 0,
1217 /* Look precisely for the quota items */
1218 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1219 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1221 /* For our qgroup */
1222 .key
.min_offset
= old_qgroupid
,
1223 .key
.max_offset
= old_qgroupid
,
1225 /* No restrictions on the other components */
1226 .key
.min_transid
= 0,
1227 .key
.max_transid
= UINT64_MAX
,
1232 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1238 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1239 const struct btrfs_ioctl_search_header
*sh
;
1242 args
.key
.nr_items
= 256;
1243 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1244 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1250 if (args
.key
.nr_items
<= 0)
1253 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1254 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1255 struct btrfs_ioctl_qgroup_limit_args qargs
;
1258 /* Make sure we start the next search at least from this entry */
1259 btrfs_ioctl_search_args_set(&args
, sh
);
1261 if (sh
->objectid
!= 0)
1263 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1265 if (sh
->offset
!= old_qgroupid
)
1268 /* We found the entry, now copy things over. */
1270 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1271 .qgroupid
= new_qgroupid
,
1273 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1274 .lim
.max_excl
= le64toh(qli
->max_excl
),
1275 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1276 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1278 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1279 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1280 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1281 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1285 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1286 if (errno
== EBUSY
&& c
< 10) {
1287 (void) btrfs_quota_scan_wait(fd
);
1299 /* Increase search key by one, to read the next item, if we can. */
1300 if (!btrfs_ioctl_search_args_inc(&args
))
1307 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1308 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1309 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1310 int n_old_qgroups
, n_old_parent_qgroups
, r
;
1311 uint64_t old_parent_id
;
1315 /* Copies a reduced form of quota information from the old to
1316 * the new subvolume. */
1318 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1319 if (n_old_qgroups
<= 0) /* Nothing to copy */
1320 return n_old_qgroups
;
1322 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1324 /* We have no parent, hence nothing to copy. */
1325 n_old_parent_qgroups
= 0;
1329 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1330 if (n_old_parent_qgroups
< 0)
1331 return n_old_parent_qgroups
;
1334 for (int i
= 0; i
< n_old_qgroups
; i
++) {
1337 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1341 if (id
== old_subvol_id
) {
1342 /* The old subvolume was member of a qgroup
1343 * that had the same id, but a different level
1344 * as it self. Let's set up something similar
1345 * in the destination. */
1346 insert_intermediary_qgroup
= true;
1350 for (int j
= 0; j
< n_old_parent_qgroups
; j
++)
1351 if (old_parent_qgroups
[j
] == old_qgroups
[i
])
1352 /* The old subvolume shared a common
1353 * parent qgroup with its parent
1354 * subvolume. Let's set up something
1355 * similar in the destination. */
1356 copy_from_parent
= true;
1359 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1362 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1365 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1366 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1370 /* First copy the leaf limits */
1371 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1376 /* Then, try to copy the subtree limits, if there are any. */
1377 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1383 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1389 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1396 static int subvol_snapshot_children(
1399 const char *subvolume
,
1400 uint64_t old_subvol_id
,
1401 BtrfsSnapshotFlags flags
) {
1403 struct btrfs_ioctl_search_args args
= {
1404 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1406 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1407 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1409 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1410 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1412 .key
.min_transid
= 0,
1413 .key
.max_transid
= UINT64_MAX
,
1416 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1417 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1420 _cleanup_close_
int subvolume_fd
= -1;
1421 uint64_t new_subvol_id
;
1424 assert(old_fd
>= 0);
1425 assert(new_fd
>= 0);
1428 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1430 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1433 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1434 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1437 if (old_subvol_id
== 0) {
1438 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1443 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1447 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1448 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1450 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1452 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1453 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1458 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1460 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1461 const struct btrfs_ioctl_search_header
*sh
;
1464 args
.key
.nr_items
= 256;
1465 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1468 if (args
.key
.nr_items
<= 0)
1471 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1472 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1473 const struct btrfs_root_ref
*ref
;
1474 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1476 btrfs_ioctl_search_args_set(&args
, sh
);
1478 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1481 /* Avoid finding the source subvolume a second
1483 if (sh
->offset
!= old_subvol_id
)
1486 /* Avoid running into loops if the new
1487 * subvolume is below the old one. */
1488 if (sh
->objectid
== new_subvol_id
)
1491 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1492 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1496 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1497 .treeid
= old_subvol_id
,
1498 .objectid
= htole64(ref
->dirid
),
1501 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1504 c
= path_join(ino_args
.name
, p
);
1508 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1509 if (old_child_fd
< 0)
1512 np
= path_join(subvolume
, ino_args
.name
);
1516 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1517 if (new_child_fd
< 0)
1520 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1521 /* If the snapshot is read-only we
1522 * need to mark it writable
1523 * temporarily, to put the subsnapshot
1526 if (subvolume_fd
< 0) {
1527 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1528 if (subvolume_fd
< 0)
1532 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1537 /* When btrfs clones the subvolumes, child
1538 * subvolumes appear as empty directories. Remove
1539 * them, so that we can create a new snapshot
1541 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1544 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1545 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1550 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1552 /* Restore the readonly flag */
1553 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1556 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1557 if (r
>= 0 && k
< 0)
1565 /* Increase search key by one, to read the next item, if we can. */
1566 if (!btrfs_ioctl_search_args_inc(&args
))
1570 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1571 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1576 int btrfs_subvol_snapshot_fd_full(
1578 const char *new_path
,
1579 BtrfsSnapshotFlags flags
,
1580 copy_progress_path_t progress_path
,
1581 copy_progress_bytes_t progress_bytes
,
1584 _cleanup_close_
int new_fd
= -1;
1585 const char *subvolume
;
1588 assert(old_fd
>= 0);
1591 r
= btrfs_is_subvol_fd(old_fd
);
1595 bool plain_directory
= false;
1597 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1598 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1601 r
= btrfs_subvol_make(new_path
);
1602 if (ERRNO_IS_NOT_SUPPORTED(r
) && (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1603 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1604 if (mkdir(new_path
, 0755) < 0)
1607 plain_directory
= true;
1611 r
= copy_directory_fd_full(
1618 (FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGINT
) ? COPY_SIGINT
: 0)|
1619 (FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGTERM
) ? COPY_SIGTERM
: 0),
1626 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1628 if (plain_directory
) {
1629 /* Plain directories have no recursive read-only flag, but something pretty close to
1630 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1632 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1633 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1635 r
= btrfs_subvol_set_read_only(new_path
, true);
1644 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1648 r
= extract_subvolume_name(new_path
, &subvolume
);
1652 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1656 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1659 int btrfs_subvol_snapshot_full(
1660 const char *old_path
,
1661 const char *new_path
,
1662 BtrfsSnapshotFlags flags
,
1663 copy_progress_path_t progress_path
,
1664 copy_progress_bytes_t progress_bytes
,
1667 _cleanup_close_
int old_fd
= -1;
1672 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1676 return btrfs_subvol_snapshot_fd_full(old_fd
, new_path
, flags
, progress_path
, progress_bytes
, userdata
);
1679 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1681 struct btrfs_ioctl_search_args args
= {
1682 /* Tree of quota items */
1683 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1685 /* Look precisely for the quota relation items */
1686 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1687 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1689 /* No restrictions on the other components */
1690 .key
.min_offset
= 0,
1691 .key
.max_offset
= UINT64_MAX
,
1693 .key
.min_transid
= 0,
1694 .key
.max_transid
= UINT64_MAX
,
1697 _cleanup_free_
uint64_t *items
= NULL
;
1704 if (qgroupid
== 0) {
1705 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1709 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1716 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1718 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1719 const struct btrfs_ioctl_search_header
*sh
;
1722 args
.key
.nr_items
= 256;
1723 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1724 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1730 if (args
.key
.nr_items
<= 0)
1733 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1735 /* Make sure we start the next search at least from this entry */
1736 btrfs_ioctl_search_args_set(&args
, sh
);
1738 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1740 if (sh
->offset
< sh
->objectid
)
1742 if (sh
->objectid
!= qgroupid
)
1745 if (!GREEDY_REALLOC(items
, n_items
+1))
1748 items
[n_items
++] = sh
->offset
;
1751 /* Increase search key by one, to read the next item, if we can. */
1752 if (!btrfs_ioctl_search_args_inc(&args
))
1761 *ret
= TAKE_PTR(items
);
1763 return (int) n_items
;
1766 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1767 _cleanup_free_
uint64_t *qgroups
= NULL
;
1768 _cleanup_close_
int real_fd
= -1;
1769 uint64_t parent_subvol
;
1770 bool changed
= false;
1776 * Sets up the specified subvolume's qgroup automatically in
1779 * If insert_intermediary_qgroup is false, the subvolume's
1780 * leaf qgroup will be assigned to the same parent qgroups as
1781 * the subvolume's parent subvolume.
1783 * If insert_intermediary_qgroup is true a new intermediary
1784 * higher-level qgroup is created, with a higher level number,
1785 * but reusing the id of the subvolume. The level number is
1786 * picked as one smaller than the lowest level qgroup the
1787 * parent subvolume is a member of. If the parent subvolume's
1788 * leaf qgroup is assigned to no higher-level qgroup a new
1789 * qgroup of level 255 is created instead. Either way, the new
1790 * qgroup is then assigned to the parent's higher-level
1791 * qgroup, and the subvolume itself is assigned to it.
1793 * If the subvolume is already assigned to a higher level
1794 * qgroup, no operation is executed.
1796 * Effectively this means: regardless if
1797 * insert_intermediary_qgroup is true or not, after this
1798 * function is invoked the subvolume will be accounted within
1799 * the same qgroups as the parent. However, if it is true, it
1800 * will also get its own higher-level qgroup, which may in
1801 * turn be used by subvolumes created beneath this subvolume
1804 * This hence defines a simple default qgroup setup for
1805 * subvolumes, as long as this function is invoked on each
1806 * created subvolume: each subvolume is always accounting
1807 * together with its immediate parents. Optionally, if
1808 * insert_intermediary_qgroup is true, it will also get a
1809 * qgroup that then includes all its own child subvolumes.
1812 /* Turn this into a proper fd, if it is currently O_PATH */
1813 fd
= fd_reopen_condition(fd
, O_RDONLY
|O_CLOEXEC
, O_PATH
, &real_fd
);
1817 if (subvol_id
== 0) {
1818 r
= btrfs_is_subvol_fd(fd
);
1824 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1829 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1832 if (n
> 0) /* already parent qgroups set up, let's bail */
1835 qgroups
= mfree(qgroups
);
1837 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1839 /* No parent, hence no qgroup memberships */
1844 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1849 if (insert_intermediary_qgroup
) {
1850 uint64_t lowest
= 256, new_qgroupid
;
1851 bool created
= false;
1853 /* Determine the lowest qgroup that the parent
1854 * subvolume is assigned to. */
1856 for (int 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 (int 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_MAX
,
1943 .key
.min_transid
= 0,
1944 .key
.max_transid
= UINT64_MAX
,
1951 if (subvol_id
== 0) {
1952 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1956 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
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
)