1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include <linux/btrfs_tree.h>
8 #include <linux/loop.h>
9 #include <linux/magic.h>
13 #include <sys/ioctl.h>
14 #include <sys/sysmacros.h>
17 #include "alloc-util.h"
18 #include "blockdev-util.h"
19 #include "btrfs-util.h"
20 #include "chattr-util.h"
22 #include "device-nodes.h"
28 #include "path-util.h"
30 #include "smack-util.h"
31 #include "sparse-endian.h"
32 #include "stat-util.h"
33 #include "string-util.h"
34 #include "time-util.h"
37 /* WARNING: Be careful with file system ioctls! When we get an fd, we
38 * need to make sure it either refers to only a regular file or
39 * directory, or that it is located on btrfs, before invoking any
40 * btrfs ioctls. The ioctl numbers are reused by some device drivers
41 * (such as DRM), and hence might have bad effects when invoked on
42 * device nodes (that reference drivers) rather than fds to normal
43 * files or directories. */
45 static int validate_subvolume_name(const char *name
) {
47 if (!filename_is_valid(name
))
50 if (strlen(name
) > BTRFS_SUBVOL_NAME_MAX
)
56 static int extract_subvolume_name(const char *path
, const char **subvolume
) {
65 r
= validate_subvolume_name(fn
);
73 int btrfs_is_filesystem(int fd
) {
78 if (fstatfs(fd
, &sfs
) < 0)
81 return F_TYPE_EQUAL(sfs
.f_type
, BTRFS_SUPER_MAGIC
);
84 int btrfs_is_subvol_fd(int fd
) {
89 /* On btrfs subvolumes always have the inode 256 */
91 if (fstat(fd
, &st
) < 0)
94 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
97 return btrfs_is_filesystem(fd
);
100 int btrfs_is_subvol(const char *path
) {
101 _cleanup_close_
int fd
= -1;
105 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
109 return btrfs_is_subvol_fd(fd
);
112 int btrfs_subvol_make_fd(int fd
, const char *subvolume
) {
113 struct btrfs_ioctl_vol_args args
= {};
114 _cleanup_close_
int real_fd
= -1;
119 r
= validate_subvolume_name(subvolume
);
123 r
= fcntl(fd
, F_GETFL
);
126 if (FLAGS_SET(r
, O_PATH
)) {
127 /* An O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal with
130 real_fd
= fd_reopen(fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
137 strncpy(args
.name
, subvolume
, sizeof(args
.name
)-1);
139 if (ioctl(fd
, BTRFS_IOC_SUBVOL_CREATE
, &args
) < 0)
145 int btrfs_subvol_make(const char *path
) {
146 _cleanup_close_
int fd
= -1;
147 const char *subvolume
;
152 r
= extract_subvolume_name(path
, &subvolume
);
156 fd
= open_parent(path
, O_CLOEXEC
, 0);
160 return btrfs_subvol_make_fd(fd
, subvolume
);
163 int btrfs_subvol_make_fallback(const char *path
, mode_t mode
) {
164 mode_t old
, combined
;
169 /* Let's work like mkdir(), i.e. take the specified mode, and mask it with the current umask. */
171 combined
= old
| ~mode
;
172 if (combined
!= ~mode
)
174 r
= btrfs_subvol_make(path
);
178 return 1; /* subvol worked */
182 if (mkdir(path
, mode
) < 0)
185 return 0; /* plain directory */
188 int btrfs_subvol_set_read_only_fd(int fd
, bool b
) {
189 uint64_t flags
, nflags
;
194 if (fstat(fd
, &st
) < 0)
197 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
200 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
203 nflags
= UPDATE_FLAG(flags
, BTRFS_SUBVOL_RDONLY
, b
);
207 if (ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &nflags
) < 0)
213 int btrfs_subvol_set_read_only(const char *path
, bool b
) {
214 _cleanup_close_
int fd
= -1;
216 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
220 return btrfs_subvol_set_read_only_fd(fd
, b
);
223 int btrfs_subvol_get_read_only_fd(int fd
) {
229 if (fstat(fd
, &st
) < 0)
232 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
235 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
238 return !!(flags
& BTRFS_SUBVOL_RDONLY
);
241 int btrfs_reflink(int infd
, int outfd
) {
247 /* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
249 r
= fd_verify_regular(outfd
);
253 if (ioctl(outfd
, BTRFS_IOC_CLONE
, infd
) < 0)
259 int btrfs_clone_range(int infd
, uint64_t in_offset
, int outfd
, uint64_t out_offset
, uint64_t sz
) {
260 struct btrfs_ioctl_clone_range_args args
= {
262 .src_offset
= in_offset
,
264 .dest_offset
= out_offset
,
272 r
= fd_verify_regular(outfd
);
276 if (ioctl(outfd
, BTRFS_IOC_CLONE_RANGE
, &args
) < 0)
282 int btrfs_get_block_device_fd(int fd
, dev_t
*dev
) {
283 struct btrfs_ioctl_fs_info_args fsi
= {};
290 r
= btrfs_is_filesystem(fd
);
296 if (ioctl(fd
, BTRFS_IOC_FS_INFO
, &fsi
) < 0)
299 /* We won't do this for btrfs RAID */
300 if (fsi
.num_devices
!= 1) {
305 for (id
= 1; id
<= fsi
.max_id
; id
++) {
306 struct btrfs_ioctl_dev_info_args di
= {
311 if (ioctl(fd
, BTRFS_IOC_DEV_INFO
, &di
) < 0) {
318 if (stat((char*) di
.path
, &st
) < 0)
321 if (!S_ISBLK(st
.st_mode
))
324 if (major(st
.st_rdev
) == 0)
334 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
335 _cleanup_close_
int fd
= -1;
340 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
344 return btrfs_get_block_device_fd(fd
, dev
);
347 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
348 struct btrfs_ioctl_ino_lookup_args args
= {
349 .objectid
= BTRFS_FIRST_FREE_OBJECTID
356 r
= btrfs_is_filesystem(fd
);
362 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
369 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
370 _cleanup_close_
int subvol_fd
= -1;
375 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
379 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
382 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
385 /* the objectid, type, offset together make up the btrfs key,
386 * which is considered a single 136byte integer when
387 * comparing. This call increases the counter by one, dealing
388 * with the overflow between the overflows */
390 if (args
->key
.min_offset
< (uint64_t) -1) {
391 args
->key
.min_offset
++;
395 if (args
->key
.min_type
< (uint8_t) -1) {
396 args
->key
.min_type
++;
397 args
->key
.min_offset
= 0;
401 if (args
->key
.min_objectid
< (uint64_t) -1) {
402 args
->key
.min_objectid
++;
403 args
->key
.min_offset
= 0;
404 args
->key
.min_type
= 0;
411 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
415 args
->key
.min_objectid
= h
->objectid
;
416 args
->key
.min_type
= h
->type
;
417 args
->key
.min_offset
= h
->offset
;
420 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
425 /* Compare min and max */
427 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
431 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
435 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
438 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
440 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
441 (i) < (args).key.nr_items; \
443 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
445 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
446 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
448 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
449 struct btrfs_ioctl_search_args args
= {
450 /* Tree of tree roots */
451 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
453 /* Look precisely for the subvolume items */
454 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
455 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
458 .key
.max_offset
= (uint64_t) -1,
460 /* No restrictions on the other components */
461 .key
.min_transid
= 0,
462 .key
.max_transid
= (uint64_t) -1,
471 if (subvol_id
== 0) {
472 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
476 r
= btrfs_is_filesystem(fd
);
483 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
485 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
486 const struct btrfs_ioctl_search_header
*sh
;
489 args
.key
.nr_items
= 256;
490 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
493 if (args
.key
.nr_items
<= 0)
496 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
498 const struct btrfs_root_item
*ri
;
500 /* Make sure we start the next search at least from this entry */
501 btrfs_ioctl_search_args_set(&args
, sh
);
503 if (sh
->objectid
!= subvol_id
)
505 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
508 /* Older versions of the struct lacked the otime setting */
509 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
512 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
514 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
515 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
517 ret
->subvol_id
= subvol_id
;
518 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
520 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
521 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
522 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
528 /* Increase search key by one, to read the next item, if we can. */
529 if (!btrfs_ioctl_search_args_inc(&args
))
540 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
542 struct btrfs_ioctl_search_args args
= {
543 /* Tree of quota items */
544 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
546 /* The object ID is always 0 */
547 .key
.min_objectid
= 0,
548 .key
.max_objectid
= 0,
550 /* Look precisely for the quota items */
551 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
552 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
554 /* No restrictions on the other components */
555 .key
.min_transid
= 0,
556 .key
.max_transid
= (uint64_t) -1,
559 bool found_info
= false, found_limit
= false;
566 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
570 r
= btrfs_is_filesystem(fd
);
577 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
579 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
580 const struct btrfs_ioctl_search_header
*sh
;
583 args
.key
.nr_items
= 256;
584 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
585 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
591 if (args
.key
.nr_items
<= 0)
594 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
596 /* Make sure we start the next search at least from this entry */
597 btrfs_ioctl_search_args_set(&args
, sh
);
599 if (sh
->objectid
!= 0)
601 if (sh
->offset
!= qgroupid
)
604 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
605 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
607 ret
->referenced
= le64toh(qii
->rfer
);
608 ret
->exclusive
= le64toh(qii
->excl
);
612 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
613 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
615 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
616 ret
->referenced_max
= le64toh(qli
->max_rfer
);
618 ret
->referenced_max
= (uint64_t) -1;
620 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
621 ret
->exclusive_max
= le64toh(qli
->max_excl
);
623 ret
->exclusive_max
= (uint64_t) -1;
628 if (found_info
&& found_limit
)
632 /* Increase search key by one, to read the next item, if we can. */
633 if (!btrfs_ioctl_search_args_inc(&args
))
638 if (!found_limit
&& !found_info
)
642 ret
->referenced
= (uint64_t) -1;
643 ret
->exclusive
= (uint64_t) -1;
647 ret
->referenced_max
= (uint64_t) -1;
648 ret
->exclusive_max
= (uint64_t) -1;
654 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
655 _cleanup_close_
int fd
= -1;
657 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
661 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
664 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
665 uint64_t level
, lowest
= (uint64_t) -1, lowest_qgroupid
= 0;
666 _cleanup_free_
uint64_t *qgroups
= NULL
;
672 /* This finds the "subtree" qgroup for a specific
673 * subvolume. This only works for subvolumes that have been
674 * prepared with btrfs_subvol_auto_qgroup_fd() with
675 * insert_intermediary_qgroup=true (or equivalent). For others
676 * it will return the leaf qgroup instead. The two cases may
677 * be distuingished via the return value, which is 1 in case
678 * an appropriate "subtree" qgroup was found, and 0
681 if (subvol_id
== 0) {
682 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
687 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
690 if (level
!= 0) /* Input must be a leaf qgroup */
693 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
697 for (i
= 0; i
< n
; i
++) {
700 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
707 if (lowest
== (uint64_t) -1 || level
< lowest
) {
708 lowest_qgroupid
= qgroups
[i
];
713 if (lowest
== (uint64_t) -1) {
714 /* No suitable higher-level qgroup found, let's return
715 * the leaf qgroup instead, and indicate that with the
722 *ret
= lowest_qgroupid
;
726 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
733 /* This determines the quota data of the qgroup with the
734 * lowest level, that shares the id part with the specified
735 * subvolume. This is useful for determining the quota data
736 * for entire subvolume subtrees, as long as the subtrees have
737 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
740 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
744 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
747 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
748 _cleanup_close_
int fd
= -1;
750 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
754 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
757 int btrfs_defrag_fd(int fd
) {
762 r
= fd_verify_regular(fd
);
766 if (ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
) < 0)
772 int btrfs_defrag(const char *p
) {
773 _cleanup_close_
int fd
= -1;
775 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
779 return btrfs_defrag_fd(fd
);
782 int btrfs_quota_enable_fd(int fd
, bool b
) {
783 struct btrfs_ioctl_quota_ctl_args args
= {
784 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
790 r
= btrfs_is_filesystem(fd
);
796 if (ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
) < 0)
802 int btrfs_quota_enable(const char *path
, bool b
) {
803 _cleanup_close_
int fd
= -1;
805 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
809 return btrfs_quota_enable_fd(fd
, b
);
812 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
814 struct btrfs_ioctl_qgroup_limit_args args
= {
815 .lim
.max_rfer
= referenced_max
,
816 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
824 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
828 r
= btrfs_is_filesystem(fd
);
835 args
.qgroupid
= qgroupid
;
838 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
840 if (errno
== EBUSY
&& c
< 10) {
841 (void) btrfs_quota_scan_wait(fd
);
854 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, uint64_t referenced_max
) {
855 _cleanup_close_
int fd
= -1;
857 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
861 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
864 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
870 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
874 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
877 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
878 _cleanup_close_
int fd
= -1;
880 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
884 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
887 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
890 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
893 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
896 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
900 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
904 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
907 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
912 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
914 struct btrfs_ioctl_qgroup_create_args args
= {
916 .qgroupid
= qgroupid
,
921 r
= btrfs_is_filesystem(fd
);
928 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
930 /* If quota is not enabled, we get EINVAL. Turn this into a recognizable error */
934 if (errno
== EBUSY
&& c
< 10) {
935 (void) btrfs_quota_scan_wait(fd
);
948 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
949 return qgroup_create_or_destroy(fd
, true, qgroupid
);
952 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
953 return qgroup_create_or_destroy(fd
, false, qgroupid
);
956 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
957 _cleanup_free_
uint64_t *qgroups
= NULL
;
961 /* Destroys the specified qgroup, but unassigns it from all
962 * its parents first. Also, it recursively destroys all
963 * qgroups it is assigned to that have the same id part of the
964 * qgroupid as the specified group. */
966 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
970 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
974 for (i
= 0; i
< n
; i
++) {
977 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
981 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
988 /* The parent qgroupid shares the same id part with
989 * us? If so, destroy it too. */
991 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
994 return btrfs_qgroup_destroy(fd
, qgroupid
);
997 int btrfs_quota_scan_start(int fd
) {
998 struct btrfs_ioctl_quota_rescan_args args
= {};
1002 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
) < 0)
1008 int btrfs_quota_scan_wait(int fd
) {
1011 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
) < 0)
1017 int btrfs_quota_scan_ongoing(int fd
) {
1018 struct btrfs_ioctl_quota_rescan_args args
= {};
1022 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
1025 return !!args
.flags
;
1028 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
1029 struct btrfs_ioctl_qgroup_assign_args args
= {
1037 r
= btrfs_is_filesystem(fd
);
1044 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1046 if (errno
== EBUSY
&& c
< 10) {
1047 (void) btrfs_quota_scan_wait(fd
);
1057 /* If the return value is > 0, we need to request a rescan */
1059 (void) btrfs_quota_scan_start(fd
);
1064 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1065 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1068 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1069 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1072 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1073 struct btrfs_ioctl_search_args args
= {
1074 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1076 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1077 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1079 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1080 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1082 .key
.min_transid
= 0,
1083 .key
.max_transid
= (uint64_t) -1,
1086 struct btrfs_ioctl_vol_args vol_args
= {};
1087 _cleanup_close_
int subvol_fd
= -1;
1089 bool made_writable
= false;
1095 if (fstat(fd
, &st
) < 0)
1098 if (!S_ISDIR(st
.st_mode
))
1101 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1105 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1106 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1107 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1108 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1109 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1110 * let's prefer ENOTTY over EPERM/EACCES though. */
1111 r
= btrfs_is_subvol_fd(subvol_fd
);
1114 if (r
== 0) /* Not a btrfs subvolume */
1117 if (subvol_id
== 0) {
1118 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1123 /* First, try to remove the subvolume. If it happens to be
1124 * already empty, this will just work. */
1125 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1126 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1127 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1130 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1133 /* OK, the subvolume is not empty, let's look for child
1134 * subvolumes, and remove them, first */
1136 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1138 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1139 const struct btrfs_ioctl_search_header
*sh
;
1142 args
.key
.nr_items
= 256;
1143 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1146 if (args
.key
.nr_items
<= 0)
1149 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1150 _cleanup_free_
char *p
= NULL
;
1151 const struct btrfs_root_ref
*ref
;
1153 btrfs_ioctl_search_args_set(&args
, sh
);
1155 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1157 if (sh
->offset
!= subvol_id
)
1160 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1162 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1166 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1167 .treeid
= subvol_id
,
1168 .objectid
= htole64(ref
->dirid
),
1171 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1174 if (!made_writable
) {
1175 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1179 made_writable
= true;
1182 if (isempty(ino_args
.name
))
1183 /* Subvolume is in the top-level
1184 * directory of the subvolume. */
1185 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1187 _cleanup_close_
int child_fd
= -1;
1189 /* Subvolume is somewhere further down,
1190 * hence we need to open the
1191 * containing directory first */
1193 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1197 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1203 /* Increase search key by one, to read the next item, if we can. */
1204 if (!btrfs_ioctl_search_args_inc(&args
))
1208 /* OK, the child subvolumes should all be gone now, let's try
1209 * again to remove the subvolume */
1210 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1213 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1217 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1218 _cleanup_close_
int fd
= -1;
1219 const char *subvolume
;
1224 r
= extract_subvolume_name(path
, &subvolume
);
1228 fd
= open_parent(path
, O_CLOEXEC
, 0);
1232 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1235 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1236 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1239 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1241 struct btrfs_ioctl_search_args args
= {
1242 /* Tree of quota items */
1243 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1245 /* The object ID is always 0 */
1246 .key
.min_objectid
= 0,
1247 .key
.max_objectid
= 0,
1249 /* Look precisely for the quota items */
1250 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1251 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1253 /* For our qgroup */
1254 .key
.min_offset
= old_qgroupid
,
1255 .key
.max_offset
= old_qgroupid
,
1257 /* No restrictions on the other components */
1258 .key
.min_transid
= 0,
1259 .key
.max_transid
= (uint64_t) -1,
1264 r
= btrfs_is_filesystem(fd
);
1270 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1271 const struct btrfs_ioctl_search_header
*sh
;
1274 args
.key
.nr_items
= 256;
1275 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1276 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1282 if (args
.key
.nr_items
<= 0)
1285 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1286 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1287 struct btrfs_ioctl_qgroup_limit_args qargs
;
1290 /* Make sure we start the next search at least from this entry */
1291 btrfs_ioctl_search_args_set(&args
, sh
);
1293 if (sh
->objectid
!= 0)
1295 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1297 if (sh
->offset
!= old_qgroupid
)
1300 /* We found the entry, now copy things over. */
1302 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1303 .qgroupid
= new_qgroupid
,
1305 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1306 .lim
.max_excl
= le64toh(qli
->max_excl
),
1307 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1308 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1310 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1311 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1312 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1313 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1317 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1318 if (errno
== EBUSY
&& c
< 10) {
1319 (void) btrfs_quota_scan_wait(fd
);
1331 /* Increase search key by one, to read the next item, if we can. */
1332 if (!btrfs_ioctl_search_args_inc(&args
))
1339 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1340 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1341 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1342 int n_old_qgroups
, n_old_parent_qgroups
, r
, i
;
1343 uint64_t old_parent_id
;
1347 /* Copies a reduced form of quota information from the old to
1348 * the new subvolume. */
1350 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1351 if (n_old_qgroups
<= 0) /* Nothing to copy */
1352 return n_old_qgroups
;
1354 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1356 /* We have no parent, hence nothing to copy. */
1357 n_old_parent_qgroups
= 0;
1361 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1362 if (n_old_parent_qgroups
< 0)
1363 return n_old_parent_qgroups
;
1366 for (i
= 0; i
< n_old_qgroups
; i
++) {
1370 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1374 if (id
== old_subvol_id
) {
1375 /* The old subvolume was member of a qgroup
1376 * that had the same id, but a different level
1377 * as it self. Let's set up something similar
1378 * in the destination. */
1379 insert_intermediary_qgroup
= true;
1383 for (j
= 0; j
< n_old_parent_qgroups
; j
++)
1384 if (old_parent_qgroups
[j
] == old_qgroups
[i
]) {
1385 /* The old subvolume shared a common
1386 * parent qgroup with its parent
1387 * subvolume. Let's set up something
1388 * similar in the destination. */
1389 copy_from_parent
= true;
1393 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1396 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1399 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1400 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1404 /* First copy the leaf limits */
1405 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1410 /* Then, try to copy the subtree limits, if there are any. */
1411 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1417 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1423 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1430 static int subvol_snapshot_children(
1433 const char *subvolume
,
1434 uint64_t old_subvol_id
,
1435 BtrfsSnapshotFlags flags
) {
1437 struct btrfs_ioctl_search_args args
= {
1438 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1440 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1441 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1443 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1444 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1446 .key
.min_transid
= 0,
1447 .key
.max_transid
= (uint64_t) -1,
1450 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1451 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1454 _cleanup_close_
int subvolume_fd
= -1;
1455 uint64_t new_subvol_id
;
1458 assert(old_fd
>= 0);
1459 assert(new_fd
>= 0);
1462 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1464 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1467 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1468 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1471 if (old_subvol_id
== 0) {
1472 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1477 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1481 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1482 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1484 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1486 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1487 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1492 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1494 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1495 const struct btrfs_ioctl_search_header
*sh
;
1498 args
.key
.nr_items
= 256;
1499 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1502 if (args
.key
.nr_items
<= 0)
1505 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1506 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1507 const struct btrfs_root_ref
*ref
;
1508 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1510 btrfs_ioctl_search_args_set(&args
, sh
);
1512 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1515 /* Avoid finding the source subvolume a second
1517 if (sh
->offset
!= old_subvol_id
)
1520 /* Avoid running into loops if the new
1521 * subvolume is below the old one. */
1522 if (sh
->objectid
== new_subvol_id
)
1525 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1526 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1530 struct btrfs_ioctl_ino_lookup_args ino_args
= {
1531 .treeid
= old_subvol_id
,
1532 .objectid
= htole64(ref
->dirid
),
1535 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1538 c
= path_join(ino_args
.name
, p
);
1542 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1543 if (old_child_fd
< 0)
1546 np
= path_join(subvolume
, ino_args
.name
);
1550 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1551 if (new_child_fd
< 0)
1554 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1555 /* If the snapshot is read-only we
1556 * need to mark it writable
1557 * temporarily, to put the subsnapshot
1560 if (subvolume_fd
< 0) {
1561 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1562 if (subvolume_fd
< 0)
1566 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1571 /* When btrfs clones the subvolumes, child
1572 * subvolumes appear as empty directories. Remove
1573 * them, so that we can create a new snapshot
1575 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1578 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1579 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1584 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1586 /* Restore the readonly flag */
1587 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1590 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1591 if (r
>= 0 && k
< 0)
1599 /* Increase search key by one, to read the next item, if we can. */
1600 if (!btrfs_ioctl_search_args_inc(&args
))
1604 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1605 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1610 int btrfs_subvol_snapshot_fd_full(
1612 const char *new_path
,
1613 BtrfsSnapshotFlags flags
,
1614 copy_progress_path_t progress_path
,
1615 copy_progress_bytes_t progress_bytes
,
1618 _cleanup_close_
int new_fd
= -1;
1619 const char *subvolume
;
1622 assert(old_fd
>= 0);
1625 r
= btrfs_is_subvol_fd(old_fd
);
1629 bool plain_directory
= false;
1631 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1632 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1635 r
= btrfs_subvol_make(new_path
);
1636 if (r
== -ENOTTY
&& (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1637 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1638 if (mkdir(new_path
, 0755) < 0)
1641 plain_directory
= true;
1645 r
= copy_directory_fd_full(old_fd
, new_path
, COPY_MERGE
|COPY_REFLINK
|COPY_SAME_MOUNT
|(FLAGS_SET(flags
, BTRFS_SNAPSHOT_SIGINT
) ? COPY_SIGINT
: 0), progress_path
, progress_bytes
, userdata
);
1649 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1651 if (plain_directory
) {
1652 /* Plain directories have no recursive read-only flag, but something pretty close to
1653 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1655 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1656 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1658 r
= btrfs_subvol_set_read_only(new_path
, true);
1667 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1671 r
= extract_subvolume_name(new_path
, &subvolume
);
1675 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1679 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1682 int btrfs_subvol_snapshot_full(
1683 const char *old_path
,
1684 const char *new_path
,
1685 BtrfsSnapshotFlags flags
,
1686 copy_progress_path_t progress_path
,
1687 copy_progress_bytes_t progress_bytes
,
1690 _cleanup_close_
int old_fd
= -1;
1695 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1699 return btrfs_subvol_snapshot_fd_full(old_fd
, new_path
, flags
, progress_path
, progress_bytes
, userdata
);
1702 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1704 struct btrfs_ioctl_search_args args
= {
1705 /* Tree of quota items */
1706 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1708 /* Look precisely for the quota relation items */
1709 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1710 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1712 /* No restrictions on the other components */
1713 .key
.min_offset
= 0,
1714 .key
.max_offset
= (uint64_t) -1,
1716 .key
.min_transid
= 0,
1717 .key
.max_transid
= (uint64_t) -1,
1720 _cleanup_free_
uint64_t *items
= NULL
;
1721 size_t n_items
= 0, n_allocated
= 0;
1727 if (qgroupid
== 0) {
1728 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1732 r
= btrfs_is_filesystem(fd
);
1739 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1741 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1742 const struct btrfs_ioctl_search_header
*sh
;
1745 args
.key
.nr_items
= 256;
1746 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1747 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1753 if (args
.key
.nr_items
<= 0)
1756 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1758 /* Make sure we start the next search at least from this entry */
1759 btrfs_ioctl_search_args_set(&args
, sh
);
1761 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1763 if (sh
->offset
< sh
->objectid
)
1765 if (sh
->objectid
!= qgroupid
)
1768 if (!GREEDY_REALLOC(items
, n_allocated
, n_items
+1))
1771 items
[n_items
++] = sh
->offset
;
1774 /* Increase search key by one, to read the next item, if we can. */
1775 if (!btrfs_ioctl_search_args_inc(&args
))
1784 *ret
= TAKE_PTR(items
);
1786 return (int) n_items
;
1789 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1790 _cleanup_free_
uint64_t *qgroups
= NULL
;
1791 uint64_t parent_subvol
;
1792 bool changed
= false;
1798 * Sets up the specified subvolume's qgroup automatically in
1801 * If insert_intermediary_qgroup is false, the subvolume's
1802 * leaf qgroup will be assigned to the same parent qgroups as
1803 * the subvolume's parent subvolume.
1805 * If insert_intermediary_qgroup is true a new intermediary
1806 * higher-level qgroup is created, with a higher level number,
1807 * but reusing the id of the subvolume. The level number is
1808 * picked as one smaller than the lowest level qgroup the
1809 * parent subvolume is a member of. If the parent subvolume's
1810 * leaf qgroup is assigned to no higher-level qgroup a new
1811 * qgroup of level 255 is created instead. Either way, the new
1812 * qgroup is then assigned to the parent's higher-level
1813 * qgroup, and the subvolume itself is assigned to it.
1815 * If the subvolume is already assigned to a higher level
1816 * qgroup, no operation is executed.
1818 * Effectively this means: regardless if
1819 * insert_intermediary_qgroup is true or not, after this
1820 * function is invoked the subvolume will be accounted within
1821 * the same qgroups as the parent. However, if it is true, it
1822 * will also get its own higher-level qgroup, which may in
1823 * turn be used by subvolumes created beneath this subvolume
1826 * This hence defines a simple default qgroup setup for
1827 * subvolumes, as long as this function is invoked on each
1828 * created subvolume: each subvolume is always accounting
1829 * together with its immediate parents. Optionally, if
1830 * insert_intermediary_qgroup is true, it will also get a
1831 * qgroup that then includes all its own child subvolumes.
1834 if (subvol_id
== 0) {
1835 r
= btrfs_is_subvol_fd(fd
);
1841 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1846 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1849 if (n
> 0) /* already parent qgroups set up, let's bail */
1852 qgroups
= mfree(qgroups
);
1854 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1856 /* No parent, hence no qgroup memberships */
1861 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1866 if (insert_intermediary_qgroup
) {
1867 uint64_t lowest
= 256, new_qgroupid
;
1868 bool created
= false;
1871 /* Determine the lowest qgroup that the parent
1872 * subvolume is assigned to. */
1874 for (i
= 0; i
< n
; i
++) {
1877 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, NULL
);
1885 if (lowest
<= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1888 r
= btrfs_qgroupid_make(lowest
- 1, subvol_id
, &new_qgroupid
);
1892 /* Create the new intermediary group, unless it already exists */
1893 r
= btrfs_qgroup_create(fd
, new_qgroupid
);
1894 if (r
< 0 && r
!= -EEXIST
)
1897 changed
= created
= true;
1899 for (i
= 0; i
< n
; i
++) {
1900 r
= btrfs_qgroup_assign(fd
, new_qgroupid
, qgroups
[i
]);
1901 if (r
< 0 && r
!= -EEXIST
) {
1903 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1911 r
= btrfs_qgroup_assign(fd
, subvol_id
, new_qgroupid
);
1912 if (r
< 0 && r
!= -EEXIST
) {
1914 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1923 /* Assign our subvolume to all the same qgroups as the parent */
1925 for (i
= 0; i
< n
; i
++) {
1926 r
= btrfs_qgroup_assign(fd
, subvol_id
, qgroups
[i
]);
1927 if (r
< 0 && r
!= -EEXIST
)
1937 int btrfs_subvol_auto_qgroup(const char *path
, uint64_t subvol_id
, bool create_intermediary_qgroup
) {
1938 _cleanup_close_
int fd
= -1;
1940 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1944 return btrfs_subvol_auto_qgroup_fd(fd
, subvol_id
, create_intermediary_qgroup
);
1947 int btrfs_subvol_get_parent(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
1949 struct btrfs_ioctl_search_args args
= {
1950 /* Tree of tree roots */
1951 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1953 /* Look precisely for the subvolume items */
1954 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1955 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1957 /* No restrictions on the other components */
1958 .key
.min_offset
= 0,
1959 .key
.max_offset
= (uint64_t) -1,
1961 .key
.min_transid
= 0,
1962 .key
.max_transid
= (uint64_t) -1,
1969 if (subvol_id
== 0) {
1970 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1974 r
= btrfs_is_filesystem(fd
);
1981 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
1983 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1984 const struct btrfs_ioctl_search_header
*sh
;
1987 args
.key
.nr_items
= 256;
1988 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1989 return negative_errno();
1991 if (args
.key
.nr_items
<= 0)
1994 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1996 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1998 if (sh
->objectid
!= subvol_id
)