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"
36 /* WARNING: Be careful with file system ioctls! When we get an fd, we
37 * need to make sure it either refers to only a regular file or
38 * directory, or that it is located on btrfs, before invoking any
39 * btrfs ioctls. The ioctl numbers are reused by some device drivers
40 * (such as DRM), and hence might have bad effects when invoked on
41 * device nodes (that reference drivers) rather than fds to normal
42 * files or directories. */
44 static int validate_subvolume_name(const char *name
) {
46 if (!filename_is_valid(name
))
49 if (strlen(name
) > BTRFS_SUBVOL_NAME_MAX
)
55 static int extract_subvolume_name(const char *path
, const char **subvolume
) {
64 r
= validate_subvolume_name(fn
);
72 int btrfs_is_subvol_fd(int fd
) {
77 /* On btrfs subvolumes always have the inode 256 */
79 if (fstat(fd
, &st
) < 0)
82 if (!btrfs_might_be_subvol(&st
))
85 return fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
88 int btrfs_is_subvol(const char *path
) {
89 _cleanup_close_
int fd
= -1;
93 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
97 return btrfs_is_subvol_fd(fd
);
100 int btrfs_subvol_make_fd(int fd
, const char *subvolume
) {
101 struct btrfs_ioctl_vol_args args
= {};
102 _cleanup_close_
int real_fd
= -1;
107 r
= validate_subvolume_name(subvolume
);
111 r
= fcntl(fd
, F_GETFL
);
114 if (FLAGS_SET(r
, O_PATH
)) {
115 /* An O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal with
118 real_fd
= fd_reopen(fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
125 strncpy(args
.name
, subvolume
, sizeof(args
.name
)-1);
127 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_SUBVOL_CREATE
, &args
));
130 int btrfs_subvol_make(const char *path
) {
131 _cleanup_close_
int fd
= -1;
132 const char *subvolume
;
137 r
= extract_subvolume_name(path
, &subvolume
);
141 fd
= open_parent(path
, O_CLOEXEC
, 0);
145 return btrfs_subvol_make_fd(fd
, subvolume
);
148 int btrfs_subvol_make_fallback(const char *path
, mode_t mode
) {
149 mode_t old
, combined
;
154 /* Let's work like mkdir(), i.e. take the specified mode, and mask it with the current umask. */
156 combined
= old
| ~mode
;
157 if (combined
!= ~mode
)
159 r
= btrfs_subvol_make(path
);
163 return 1; /* subvol worked */
167 if (mkdir(path
, mode
) < 0)
170 return 0; /* plain directory */
173 int btrfs_subvol_set_read_only_fd(int fd
, bool b
) {
174 uint64_t flags
, nflags
;
179 if (fstat(fd
, &st
) < 0)
182 if (!btrfs_might_be_subvol(&st
))
185 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
188 nflags
= UPDATE_FLAG(flags
, BTRFS_SUBVOL_RDONLY
, b
);
192 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &nflags
));
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 (!btrfs_might_be_subvol(&st
))
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 return RET_NERRNO(ioctl(outfd
, BTRFS_IOC_CLONE
, infd
));
238 int btrfs_clone_range(int infd
, uint64_t in_offset
, int outfd
, uint64_t out_offset
, uint64_t sz
) {
239 struct btrfs_ioctl_clone_range_args args
= {
241 .src_offset
= in_offset
,
243 .dest_offset
= out_offset
,
251 r
= fd_verify_regular(outfd
);
255 return RET_NERRNO(ioctl(outfd
, BTRFS_IOC_CLONE_RANGE
, &args
));
258 int btrfs_get_block_device_fd(int fd
, dev_t
*dev
) {
259 struct btrfs_ioctl_fs_info_args fsi
= {};
266 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
272 if (ioctl(fd
, BTRFS_IOC_FS_INFO
, &fsi
) < 0)
275 /* We won't do this for btrfs RAID */
276 if (fsi
.num_devices
!= 1) {
281 for (id
= 1; id
<= fsi
.max_id
; id
++) {
282 struct btrfs_ioctl_dev_info_args di
= {
287 if (ioctl(fd
, BTRFS_IOC_DEV_INFO
, &di
) < 0) {
294 /* For the root fs — when no initrd is involved — btrfs returns /dev/root on any kernels from
295 * the past few years. That sucks, as we have no API to determine the actual root then. let's
296 * return an recognizable error for this case, so that the caller can maybe print a nice
297 * message about this.
299 * https://bugzilla.kernel.org/show_bug.cgi?id=89721 */
300 if (path_equal((char*) di
.path
, "/dev/root"))
303 if (stat((char*) di
.path
, &st
) < 0)
306 if (!S_ISBLK(st
.st_mode
))
309 if (major(st
.st_rdev
) == 0)
319 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
320 _cleanup_close_
int fd
= -1;
325 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
329 return btrfs_get_block_device_fd(fd
, dev
);
332 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
333 struct btrfs_ioctl_ino_lookup_args args
= {
334 .objectid
= BTRFS_FIRST_FREE_OBJECTID
341 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
347 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
354 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
355 _cleanup_close_
int subvol_fd
= -1;
360 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
364 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
367 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
370 /* the objectid, type, offset together make up the btrfs key,
371 * which is considered a single 136byte integer when
372 * comparing. This call increases the counter by one, dealing
373 * with the overflow between the overflows */
375 if (args
->key
.min_offset
< UINT64_MAX
) {
376 args
->key
.min_offset
++;
380 if (args
->key
.min_type
< UINT8_MAX
) {
381 args
->key
.min_type
++;
382 args
->key
.min_offset
= 0;
386 if (args
->key
.min_objectid
< UINT64_MAX
) {
387 args
->key
.min_objectid
++;
388 args
->key
.min_offset
= 0;
389 args
->key
.min_type
= 0;
396 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
400 args
->key
.min_objectid
= h
->objectid
;
401 args
->key
.min_type
= h
->type
;
402 args
->key
.min_offset
= h
->offset
;
405 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
410 /* Compare min and max */
412 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
416 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
420 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
423 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
425 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
426 (i) < (args).key.nr_items; \
428 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
430 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
431 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
433 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
434 struct btrfs_ioctl_search_args args
= {
435 /* Tree of tree roots */
436 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
438 /* Look precisely for the subvolume items */
439 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
440 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
443 .key
.max_offset
= UINT64_MAX
,
445 /* No restrictions on the other components */
446 .key
.min_transid
= 0,
447 .key
.max_transid
= UINT64_MAX
,
456 if (subvol_id
== 0) {
457 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
461 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
468 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
470 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
471 const struct btrfs_ioctl_search_header
*sh
;
474 args
.key
.nr_items
= 256;
475 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
478 if (args
.key
.nr_items
<= 0)
481 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
483 const struct btrfs_root_item
*ri
;
485 /* Make sure we start the next search at least from this entry */
486 btrfs_ioctl_search_args_set(&args
, sh
);
488 if (sh
->objectid
!= subvol_id
)
490 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
493 /* Older versions of the struct lacked the otime setting */
494 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
497 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
499 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
500 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
502 ret
->subvol_id
= subvol_id
;
503 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
505 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
506 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
507 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
513 /* Increase search key by one, to read the next item, if we can. */
514 if (!btrfs_ioctl_search_args_inc(&args
))
525 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
527 struct btrfs_ioctl_search_args args
= {
528 /* Tree of quota items */
529 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
531 /* The object ID is always 0 */
532 .key
.min_objectid
= 0,
533 .key
.max_objectid
= 0,
535 /* Look precisely for the quota items */
536 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
537 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
539 /* No restrictions on the other components */
540 .key
.min_transid
= 0,
541 .key
.max_transid
= UINT64_MAX
,
544 bool found_info
= false, found_limit
= false;
551 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
555 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
562 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
564 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
565 const struct btrfs_ioctl_search_header
*sh
;
568 args
.key
.nr_items
= 256;
569 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
570 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
576 if (args
.key
.nr_items
<= 0)
579 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
581 /* Make sure we start the next search at least from this entry */
582 btrfs_ioctl_search_args_set(&args
, sh
);
584 if (sh
->objectid
!= 0)
586 if (sh
->offset
!= qgroupid
)
589 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
590 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
592 ret
->referenced
= le64toh(qii
->rfer
);
593 ret
->exclusive
= le64toh(qii
->excl
);
597 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
598 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
600 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
601 ret
->referenced_max
= le64toh(qli
->max_rfer
);
603 ret
->referenced_max
= UINT64_MAX
;
605 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
606 ret
->exclusive_max
= le64toh(qli
->max_excl
);
608 ret
->exclusive_max
= UINT64_MAX
;
613 if (found_info
&& found_limit
)
617 /* Increase search key by one, to read the next item, if we can. */
618 if (!btrfs_ioctl_search_args_inc(&args
))
623 if (!found_limit
&& !found_info
)
627 ret
->referenced
= UINT64_MAX
;
628 ret
->exclusive
= UINT64_MAX
;
632 ret
->referenced_max
= UINT64_MAX
;
633 ret
->exclusive_max
= UINT64_MAX
;
639 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
640 _cleanup_close_
int fd
= -1;
642 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
646 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
649 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
650 uint64_t level
, lowest
= UINT64_MAX
, lowest_qgroupid
= 0;
651 _cleanup_free_
uint64_t *qgroups
= NULL
;
657 /* This finds the "subtree" qgroup for a specific
658 * subvolume. This only works for subvolumes that have been
659 * prepared with btrfs_subvol_auto_qgroup_fd() with
660 * insert_intermediary_qgroup=true (or equivalent). For others
661 * it will return the leaf qgroup instead. The two cases may
662 * be distuingished via the return value, which is 1 in case
663 * an appropriate "subtree" qgroup was found, and 0
666 if (subvol_id
== 0) {
667 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
672 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
675 if (level
!= 0) /* Input must be a leaf qgroup */
678 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
682 for (int i
= 0; i
< n
; i
++) {
685 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
692 if (lowest
== UINT64_MAX
|| level
< lowest
) {
693 lowest_qgroupid
= qgroups
[i
];
698 if (lowest
== UINT64_MAX
) {
699 /* No suitable higher-level qgroup found, let's return
700 * the leaf qgroup instead, and indicate that with the
707 *ret
= lowest_qgroupid
;
711 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
718 /* This determines the quota data of the qgroup with the
719 * lowest level, that shares the id part with the specified
720 * subvolume. This is useful for determining the quota data
721 * for entire subvolume subtrees, as long as the subtrees have
722 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
725 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
729 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
732 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
733 _cleanup_close_
int fd
= -1;
735 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
739 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
742 int btrfs_defrag(const char *p
) {
743 _cleanup_close_
int fd
= -1;
745 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
749 return btrfs_defrag_fd(fd
);
752 int btrfs_quota_enable_fd(int fd
, bool b
) {
753 struct btrfs_ioctl_quota_ctl_args args
= {
754 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
760 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
766 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
));
769 int btrfs_quota_enable(const char *path
, bool b
) {
770 _cleanup_close_
int fd
= -1;
772 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
776 return btrfs_quota_enable_fd(fd
, b
);
779 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
781 struct btrfs_ioctl_qgroup_limit_args args
= {
782 .lim
.max_rfer
= referenced_max
,
783 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
790 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
794 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
801 args
.qgroupid
= qgroupid
;
803 for (unsigned c
= 0;; c
++) {
804 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
806 if (errno
== EBUSY
&& c
< 10) {
807 (void) btrfs_quota_scan_wait(fd
);
820 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, uint64_t referenced_max
) {
821 _cleanup_close_
int fd
= -1;
823 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
827 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
830 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
836 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
840 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
843 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
844 _cleanup_close_
int fd
= -1;
846 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
850 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
853 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
856 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
859 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
862 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
866 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
870 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
873 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
878 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
880 struct btrfs_ioctl_qgroup_create_args args
= {
882 .qgroupid
= qgroupid
,
886 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
892 for (unsigned c
= 0;; c
++) {
893 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
895 /* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
896 * ENOTCONN. Let's always convert this to ENOTCONN to make this recognizable
897 * everywhere the same way. */
899 if (IN_SET(errno
, EINVAL
, ENOTCONN
))
902 if (errno
== EBUSY
&& c
< 10) {
903 (void) btrfs_quota_scan_wait(fd
);
916 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
917 return qgroup_create_or_destroy(fd
, true, qgroupid
);
920 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
921 return qgroup_create_or_destroy(fd
, false, qgroupid
);
924 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
925 _cleanup_free_
uint64_t *qgroups
= NULL
;
929 /* Destroys the specified qgroup, but unassigns it from all
930 * its parents first. Also, it recursively destroys all
931 * qgroups it is assigned to that have the same id part of the
932 * qgroupid as the specified group. */
934 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
938 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
942 for (int i
= 0; i
< n
; i
++) {
945 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
949 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
956 /* The parent qgroupid shares the same id part with
957 * us? If so, destroy it too. */
959 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
962 return btrfs_qgroup_destroy(fd
, qgroupid
);
965 int btrfs_quota_scan_start(int fd
) {
966 struct btrfs_ioctl_quota_rescan_args args
= {};
970 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
));
973 int btrfs_quota_scan_wait(int fd
) {
976 return RET_NERRNO(ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
));
979 int btrfs_quota_scan_ongoing(int fd
) {
980 struct btrfs_ioctl_quota_rescan_args args
= {};
984 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
990 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
991 struct btrfs_ioctl_qgroup_assign_args args
= {
998 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1004 for (unsigned c
= 0;; c
++) {
1005 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1007 if (errno
== EBUSY
&& c
< 10) {
1008 (void) btrfs_quota_scan_wait(fd
);
1018 /* If the return value is > 0, we need to request a rescan */
1020 (void) btrfs_quota_scan_start(fd
);
1025 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1026 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1029 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1030 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1033 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1034 struct btrfs_ioctl_search_args args
= {
1035 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1037 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1038 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1040 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1041 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1043 .key
.min_transid
= 0,
1044 .key
.max_transid
= UINT64_MAX
,
1047 struct btrfs_ioctl_vol_args vol_args
= {};
1048 _cleanup_close_
int subvol_fd
= -1;
1050 bool made_writable
= false;
1056 if (fstat(fd
, &st
) < 0)
1059 if (!S_ISDIR(st
.st_mode
))
1062 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1066 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1067 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1068 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1069 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1070 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1071 * let's prefer ENOTTY over EPERM/EACCES though. */
1072 r
= btrfs_is_subvol_fd(subvol_fd
);
1075 if (r
== 0) /* Not a btrfs subvolume */
1078 if (subvol_id
== 0) {
1079 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1084 /* First, try to remove the subvolume. If it happens to be
1085 * already empty, this will just work. */
1086 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1087 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1088 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1091 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1094 /* OK, the subvolume is not empty, let's look for child
1095 * subvolumes, and remove them, first */
1097 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1099 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1100 const struct btrfs_ioctl_search_header
*sh
;
1103 args
.key
.nr_items
= 256;
1104 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1107 if (args
.key
.nr_items
<= 0)
1110 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1111 _cleanup_free_
char *p
= NULL
;
1112 const struct btrfs_root_ref
*ref
;
1114 btrfs_ioctl_search_args_set(&args
, sh
);
1116 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1118 if (sh
->offset
!= subvol_id
)
1121 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1123 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1127 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1128 .treeid
= subvol_id
,
1129 .objectid
= htole64(ref
->dirid
),
1132 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1135 if (!made_writable
) {
1136 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1140 made_writable
= true;
1143 if (isempty(ino_args
.name
))
1144 /* Subvolume is in the top-level
1145 * directory of the subvolume. */
1146 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1148 _cleanup_close_
int child_fd
= -1;
1150 /* Subvolume is somewhere further down,
1151 * hence we need to open the
1152 * containing directory first */
1154 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1158 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1164 /* Increase search key by one, to read the next item, if we can. */
1165 if (!btrfs_ioctl_search_args_inc(&args
))
1169 /* OK, the child subvolumes should all be gone now, let's try
1170 * again to remove the subvolume */
1171 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1174 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1178 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1179 _cleanup_close_
int fd
= -1;
1180 const char *subvolume
;
1185 r
= extract_subvolume_name(path
, &subvolume
);
1189 fd
= open_parent(path
, O_CLOEXEC
, 0);
1193 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1196 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1197 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1200 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1202 struct btrfs_ioctl_search_args args
= {
1203 /* Tree of quota items */
1204 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1206 /* The object ID is always 0 */
1207 .key
.min_objectid
= 0,
1208 .key
.max_objectid
= 0,
1210 /* Look precisely for the quota items */
1211 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1212 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1214 /* For our qgroup */
1215 .key
.min_offset
= old_qgroupid
,
1216 .key
.max_offset
= old_qgroupid
,
1218 /* No restrictions on the other components */
1219 .key
.min_transid
= 0,
1220 .key
.max_transid
= UINT64_MAX
,
1225 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1231 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1232 const struct btrfs_ioctl_search_header
*sh
;
1235 args
.key
.nr_items
= 256;
1236 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1237 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1243 if (args
.key
.nr_items
<= 0)
1246 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1247 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1248 struct btrfs_ioctl_qgroup_limit_args qargs
;
1251 /* Make sure we start the next search at least from this entry */
1252 btrfs_ioctl_search_args_set(&args
, sh
);
1254 if (sh
->objectid
!= 0)
1256 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1258 if (sh
->offset
!= old_qgroupid
)
1261 /* We found the entry, now copy things over. */
1263 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1264 .qgroupid
= new_qgroupid
,
1266 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1267 .lim
.max_excl
= le64toh(qli
->max_excl
),
1268 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1269 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1271 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1272 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1273 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1274 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1278 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1279 if (errno
== EBUSY
&& c
< 10) {
1280 (void) btrfs_quota_scan_wait(fd
);
1292 /* Increase search key by one, to read the next item, if we can. */
1293 if (!btrfs_ioctl_search_args_inc(&args
))
1300 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1301 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1302 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1303 int n_old_qgroups
, n_old_parent_qgroups
, r
;
1304 uint64_t old_parent_id
;
1308 /* Copies a reduced form of quota information from the old to
1309 * the new subvolume. */
1311 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1312 if (n_old_qgroups
<= 0) /* Nothing to copy */
1313 return n_old_qgroups
;
1315 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1317 /* We have no parent, hence nothing to copy. */
1318 n_old_parent_qgroups
= 0;
1322 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1323 if (n_old_parent_qgroups
< 0)
1324 return n_old_parent_qgroups
;
1327 for (int i
= 0; i
< n_old_qgroups
; i
++) {
1330 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1334 if (id
== old_subvol_id
) {
1335 /* The old subvolume was member of a qgroup
1336 * that had the same id, but a different level
1337 * as it self. Let's set up something similar
1338 * in the destination. */
1339 insert_intermediary_qgroup
= true;
1343 for (int j
= 0; j
< n_old_parent_qgroups
; j
++)
1344 if (old_parent_qgroups
[j
] == old_qgroups
[i
])
1345 /* The old subvolume shared a common
1346 * parent qgroup with its parent
1347 * subvolume. Let's set up something
1348 * similar in the destination. */
1349 copy_from_parent
= true;
1352 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1355 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1358 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1359 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1363 /* First copy the leaf limits */
1364 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1369 /* Then, try to copy the subtree limits, if there are any. */
1370 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1376 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1382 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1389 static int subvol_snapshot_children(
1392 const char *subvolume
,
1393 uint64_t old_subvol_id
,
1394 BtrfsSnapshotFlags flags
) {
1396 struct btrfs_ioctl_search_args args
= {
1397 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1399 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1400 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1402 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1403 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1405 .key
.min_transid
= 0,
1406 .key
.max_transid
= UINT64_MAX
,
1409 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1410 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1413 _cleanup_close_
int subvolume_fd
= -1;
1414 uint64_t new_subvol_id
;
1417 assert(old_fd
>= 0);
1418 assert(new_fd
>= 0);
1421 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1423 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1426 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1427 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1430 if (old_subvol_id
== 0) {
1431 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1436 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1440 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1441 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1443 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1445 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1446 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1451 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1453 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1454 const struct btrfs_ioctl_search_header
*sh
;
1457 args
.key
.nr_items
= 256;
1458 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1461 if (args
.key
.nr_items
<= 0)
1464 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1465 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1466 const struct btrfs_root_ref
*ref
;
1467 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1469 btrfs_ioctl_search_args_set(&args
, sh
);
1471 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1474 /* Avoid finding the source subvolume a second
1476 if (sh
->offset
!= old_subvol_id
)
1479 /* Avoid running into loops if the new
1480 * subvolume is below the old one. */
1481 if (sh
->objectid
== new_subvol_id
)
1484 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1485 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1489 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1490 .treeid
= old_subvol_id
,
1491 .objectid
= htole64(ref
->dirid
),
1494 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1497 c
= path_join(ino_args
.name
, p
);
1501 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1502 if (old_child_fd
< 0)
1505 np
= path_join(subvolume
, ino_args
.name
);
1509 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1510 if (new_child_fd
< 0)
1513 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1514 /* If the snapshot is read-only we
1515 * need to mark it writable
1516 * temporarily, to put the subsnapshot
1519 if (subvolume_fd
< 0) {
1520 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1521 if (subvolume_fd
< 0)
1525 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1530 /* When btrfs clones the subvolumes, child
1531 * subvolumes appear as empty directories. Remove
1532 * them, so that we can create a new snapshot
1534 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1537 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1538 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1543 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1545 /* Restore the readonly flag */
1546 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1549 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1550 if (r
>= 0 && k
< 0)
1558 /* Increase search key by one, to read the next item, if we can. */
1559 if (!btrfs_ioctl_search_args_inc(&args
))
1563 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1564 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1569 int btrfs_subvol_snapshot_fd_full(
1571 const char *new_path
,
1572 BtrfsSnapshotFlags flags
,
1573 copy_progress_path_t progress_path
,
1574 copy_progress_bytes_t progress_bytes
,
1577 _cleanup_close_
int new_fd
= -1;
1578 const char *subvolume
;
1581 assert(old_fd
>= 0);
1584 r
= btrfs_is_subvol_fd(old_fd
);
1588 bool plain_directory
= false;
1590 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1591 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1594 r
= btrfs_subvol_make(new_path
);
1595 if (ERRNO_IS_NOT_SUPPORTED(r
) && (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1596 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1597 if (mkdir(new_path
, 0755) < 0)
1600 plain_directory
= true;
1604 r
= copy_directory_fd_full(
1611 (FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGINT
) ? COPY_SIGINT
: 0)|
1612 (FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGTERM
) ? COPY_SIGTERM
: 0),
1619 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1621 if (plain_directory
) {
1622 /* Plain directories have no recursive read-only flag, but something pretty close to
1623 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1625 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1626 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1628 r
= btrfs_subvol_set_read_only(new_path
, true);
1637 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1641 r
= extract_subvolume_name(new_path
, &subvolume
);
1645 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1649 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1652 int btrfs_subvol_snapshot_full(
1653 const char *old_path
,
1654 const char *new_path
,
1655 BtrfsSnapshotFlags flags
,
1656 copy_progress_path_t progress_path
,
1657 copy_progress_bytes_t progress_bytes
,
1660 _cleanup_close_
int old_fd
= -1;
1665 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1669 return btrfs_subvol_snapshot_fd_full(old_fd
, new_path
, flags
, progress_path
, progress_bytes
, userdata
);
1672 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1674 struct btrfs_ioctl_search_args args
= {
1675 /* Tree of quota items */
1676 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1678 /* Look precisely for the quota relation items */
1679 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1680 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1682 /* No restrictions on the other components */
1683 .key
.min_offset
= 0,
1684 .key
.max_offset
= UINT64_MAX
,
1686 .key
.min_transid
= 0,
1687 .key
.max_transid
= UINT64_MAX
,
1690 _cleanup_free_
uint64_t *items
= NULL
;
1697 if (qgroupid
== 0) {
1698 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1702 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1709 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1711 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1712 const struct btrfs_ioctl_search_header
*sh
;
1715 args
.key
.nr_items
= 256;
1716 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1717 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1723 if (args
.key
.nr_items
<= 0)
1726 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1728 /* Make sure we start the next search at least from this entry */
1729 btrfs_ioctl_search_args_set(&args
, sh
);
1731 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1733 if (sh
->offset
< sh
->objectid
)
1735 if (sh
->objectid
!= qgroupid
)
1738 if (!GREEDY_REALLOC(items
, n_items
+1))
1741 items
[n_items
++] = sh
->offset
;
1744 /* Increase search key by one, to read the next item, if we can. */
1745 if (!btrfs_ioctl_search_args_inc(&args
))
1754 *ret
= TAKE_PTR(items
);
1756 return (int) n_items
;
1759 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1760 _cleanup_free_
uint64_t *qgroups
= NULL
;
1761 uint64_t parent_subvol
;
1762 bool changed
= false;
1768 * Sets up the specified subvolume's qgroup automatically in
1771 * If insert_intermediary_qgroup is false, the subvolume's
1772 * leaf qgroup will be assigned to the same parent qgroups as
1773 * the subvolume's parent subvolume.
1775 * If insert_intermediary_qgroup is true a new intermediary
1776 * higher-level qgroup is created, with a higher level number,
1777 * but reusing the id of the subvolume. The level number is
1778 * picked as one smaller than the lowest level qgroup the
1779 * parent subvolume is a member of. If the parent subvolume's
1780 * leaf qgroup is assigned to no higher-level qgroup a new
1781 * qgroup of level 255 is created instead. Either way, the new
1782 * qgroup is then assigned to the parent's higher-level
1783 * qgroup, and the subvolume itself is assigned to it.
1785 * If the subvolume is already assigned to a higher level
1786 * qgroup, no operation is executed.
1788 * Effectively this means: regardless if
1789 * insert_intermediary_qgroup is true or not, after this
1790 * function is invoked the subvolume will be accounted within
1791 * the same qgroups as the parent. However, if it is true, it
1792 * will also get its own higher-level qgroup, which may in
1793 * turn be used by subvolumes created beneath this subvolume
1796 * This hence defines a simple default qgroup setup for
1797 * subvolumes, as long as this function is invoked on each
1798 * created subvolume: each subvolume is always accounting
1799 * together with its immediate parents. Optionally, if
1800 * insert_intermediary_qgroup is true, it will also get a
1801 * qgroup that then includes all its own child subvolumes.
1804 if (subvol_id
== 0) {
1805 r
= btrfs_is_subvol_fd(fd
);
1811 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1816 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1819 if (n
> 0) /* already parent qgroups set up, let's bail */
1822 qgroups
= mfree(qgroups
);
1824 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1826 /* No parent, hence no qgroup memberships */
1831 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1836 if (insert_intermediary_qgroup
) {
1837 uint64_t lowest
= 256, new_qgroupid
;
1838 bool created
= false;
1840 /* Determine the lowest qgroup that the parent
1841 * subvolume is assigned to. */
1843 for (int i
= 0; i
< n
; i
++) {
1846 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, NULL
);
1854 if (lowest
<= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1857 r
= btrfs_qgroupid_make(lowest
- 1, subvol_id
, &new_qgroupid
);
1861 /* Create the new intermediary group, unless it already exists */
1862 r
= btrfs_qgroup_create(fd
, new_qgroupid
);
1863 if (r
< 0 && r
!= -EEXIST
)
1866 changed
= created
= true;
1868 for (int i
= 0; i
< n
; i
++) {
1869 r
= btrfs_qgroup_assign(fd
, new_qgroupid
, qgroups
[i
]);
1870 if (r
< 0 && r
!= -EEXIST
) {
1872 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1880 r
= btrfs_qgroup_assign(fd
, subvol_id
, new_qgroupid
);
1881 if (r
< 0 && r
!= -EEXIST
) {
1883 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1892 /* Assign our subvolume to all the same qgroups as the parent */
1894 for (i
= 0; i
< n
; i
++) {
1895 r
= btrfs_qgroup_assign(fd
, subvol_id
, qgroups
[i
]);
1896 if (r
< 0 && r
!= -EEXIST
)
1906 int btrfs_subvol_auto_qgroup(const char *path
, uint64_t subvol_id
, bool create_intermediary_qgroup
) {
1907 _cleanup_close_
int fd
= -1;
1909 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1913 return btrfs_subvol_auto_qgroup_fd(fd
, subvol_id
, create_intermediary_qgroup
);
1916 int btrfs_subvol_get_parent(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
1918 struct btrfs_ioctl_search_args args
= {
1919 /* Tree of tree roots */
1920 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1922 /* Look precisely for the subvolume items */
1923 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1924 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1926 /* No restrictions on the other components */
1927 .key
.min_offset
= 0,
1928 .key
.max_offset
= UINT64_MAX
,
1930 .key
.min_transid
= 0,
1931 .key
.max_transid
= UINT64_MAX
,
1938 if (subvol_id
== 0) {
1939 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1943 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
1950 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
1952 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1953 const struct btrfs_ioctl_search_header
*sh
;
1956 args
.key
.nr_items
= 256;
1957 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1958 return negative_errno();
1960 if (args
.key
.nr_items
<= 0)
1963 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1965 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1967 if (sh
->objectid
!= subvol_id
)