1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <linux/loop.h>
12 #include <sys/ioctl.h>
14 #include <sys/statfs.h>
15 #include <sys/sysmacros.h>
18 #if HAVE_LINUX_BTRFS_H
19 #include <linux/btrfs.h>
22 #include "alloc-util.h"
23 #include "blockdev-util.h"
24 #include "btrfs-ctree.h"
25 #include "btrfs-util.h"
26 #include "chattr-util.h"
28 #include "device-nodes.h"
35 #include "path-util.h"
37 #include "smack-util.h"
38 #include "sparse-endian.h"
39 #include "stat-util.h"
40 #include "string-util.h"
41 #include "time-util.h"
44 /* WARNING: Be careful with file system ioctls! When we get an fd, we
45 * need to make sure it either refers to only a regular file or
46 * directory, or that it is located on btrfs, before invoking any
47 * btrfs ioctls. The ioctl numbers are reused by some device drivers
48 * (such as DRM), and hence might have bad effects when invoked on
49 * device nodes (that reference drivers) rather than fds to normal
50 * files or directories. */
52 static int validate_subvolume_name(const char *name
) {
54 if (!filename_is_valid(name
))
57 if (strlen(name
) > BTRFS_SUBVOL_NAME_MAX
)
63 static int extract_subvolume_name(const char *path
, const char **subvolume
) {
72 r
= validate_subvolume_name(fn
);
80 int btrfs_is_filesystem(int fd
) {
85 if (fstatfs(fd
, &sfs
) < 0)
88 return F_TYPE_EQUAL(sfs
.f_type
, BTRFS_SUPER_MAGIC
);
91 int btrfs_is_subvol_fd(int fd
) {
96 /* On btrfs subvolumes always have the inode 256 */
98 if (fstat(fd
, &st
) < 0)
101 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
104 return btrfs_is_filesystem(fd
);
107 int btrfs_is_subvol(const char *path
) {
108 _cleanup_close_
int fd
= -1;
112 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
116 return btrfs_is_subvol_fd(fd
);
119 int btrfs_subvol_make_fd(int fd
, const char *subvolume
) {
120 struct btrfs_ioctl_vol_args args
= {};
121 _cleanup_close_
int real_fd
= -1;
126 r
= validate_subvolume_name(subvolume
);
130 r
= fcntl(fd
, F_GETFL
);
133 if (FLAGS_SET(r
, O_PATH
)) {
134 /* An O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal with
137 real_fd
= fd_reopen(fd
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
144 strncpy(args
.name
, subvolume
, sizeof(args
.name
)-1);
146 if (ioctl(fd
, BTRFS_IOC_SUBVOL_CREATE
, &args
) < 0)
152 int btrfs_subvol_make(const char *path
) {
153 _cleanup_close_
int fd
= -1;
154 const char *subvolume
;
159 r
= extract_subvolume_name(path
, &subvolume
);
163 fd
= open_parent(path
, O_CLOEXEC
, 0);
167 return btrfs_subvol_make_fd(fd
, subvolume
);
170 int btrfs_subvol_set_read_only_fd(int fd
, bool b
) {
171 uint64_t flags
, nflags
;
176 if (fstat(fd
, &st
) < 0)
179 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
182 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
186 nflags
= flags
| BTRFS_SUBVOL_RDONLY
;
188 nflags
= flags
& ~BTRFS_SUBVOL_RDONLY
;
193 if (ioctl(fd
, BTRFS_IOC_SUBVOL_SETFLAGS
, &nflags
) < 0)
199 int btrfs_subvol_set_read_only(const char *path
, bool b
) {
200 _cleanup_close_
int fd
= -1;
202 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
206 return btrfs_subvol_set_read_only_fd(fd
, b
);
209 int btrfs_subvol_get_read_only_fd(int fd
) {
215 if (fstat(fd
, &st
) < 0)
218 if (!S_ISDIR(st
.st_mode
) || st
.st_ino
!= 256)
221 if (ioctl(fd
, BTRFS_IOC_SUBVOL_GETFLAGS
, &flags
) < 0)
224 return !!(flags
& BTRFS_SUBVOL_RDONLY
);
227 int btrfs_reflink(int infd
, int outfd
) {
233 /* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
235 r
= fd_verify_regular(outfd
);
239 if (ioctl(outfd
, BTRFS_IOC_CLONE
, infd
) < 0)
245 int btrfs_clone_range(int infd
, uint64_t in_offset
, int outfd
, uint64_t out_offset
, uint64_t sz
) {
246 struct btrfs_ioctl_clone_range_args args
= {
248 .src_offset
= in_offset
,
250 .dest_offset
= out_offset
,
258 r
= fd_verify_regular(outfd
);
262 if (ioctl(outfd
, BTRFS_IOC_CLONE_RANGE
, &args
) < 0)
268 int btrfs_get_block_device_fd(int fd
, dev_t
*dev
) {
269 struct btrfs_ioctl_fs_info_args fsi
= {};
276 r
= btrfs_is_filesystem(fd
);
282 if (ioctl(fd
, BTRFS_IOC_FS_INFO
, &fsi
) < 0)
285 /* We won't do this for btrfs RAID */
286 if (fsi
.num_devices
!= 1) {
291 for (id
= 1; id
<= fsi
.max_id
; id
++) {
292 struct btrfs_ioctl_dev_info_args di
= {
297 if (ioctl(fd
, BTRFS_IOC_DEV_INFO
, &di
) < 0) {
304 if (stat((char*) di
.path
, &st
) < 0)
307 if (!S_ISBLK(st
.st_mode
))
310 if (major(st
.st_rdev
) == 0)
320 int btrfs_get_block_device(const char *path
, dev_t
*dev
) {
321 _cleanup_close_
int fd
= -1;
326 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
330 return btrfs_get_block_device_fd(fd
, dev
);
333 int btrfs_subvol_get_id_fd(int fd
, uint64_t *ret
) {
334 struct btrfs_ioctl_ino_lookup_args args
= {
335 .objectid
= BTRFS_FIRST_FREE_OBJECTID
342 r
= btrfs_is_filesystem(fd
);
348 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &args
) < 0)
355 int btrfs_subvol_get_id(int fd
, const char *subvol
, uint64_t *ret
) {
356 _cleanup_close_
int subvol_fd
= -1;
361 subvol_fd
= openat(fd
, subvol
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
365 return btrfs_subvol_get_id_fd(subvol_fd
, ret
);
368 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args
*args
) {
371 /* the objectid, type, offset together make up the btrfs key,
372 * which is considered a single 136byte integer when
373 * comparing. This call increases the counter by one, dealing
374 * with the overflow between the overflows */
376 if (args
->key
.min_offset
< (uint64_t) -1) {
377 args
->key
.min_offset
++;
381 if (args
->key
.min_type
< (uint8_t) -1) {
382 args
->key
.min_type
++;
383 args
->key
.min_offset
= 0;
387 if (args
->key
.min_objectid
< (uint64_t) -1) {
388 args
->key
.min_objectid
++;
389 args
->key
.min_offset
= 0;
390 args
->key
.min_type
= 0;
397 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args
*args
, const struct btrfs_ioctl_search_header
*h
) {
401 args
->key
.min_objectid
= h
->objectid
;
402 args
->key
.min_type
= h
->type
;
403 args
->key
.min_offset
= h
->offset
;
406 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args
*args
) {
411 /* Compare min and max */
413 r
= CMP(args
->key
.min_objectid
, args
->key
.max_objectid
);
417 r
= CMP(args
->key
.min_type
, args
->key
.max_type
);
421 return CMP(args
->key
.min_offset
, args
->key
.max_offset
);
424 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
426 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
427 (i) < (args).key.nr_items; \
429 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
431 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
432 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
434 int btrfs_subvol_get_info_fd(int fd
, uint64_t subvol_id
, BtrfsSubvolInfo
*ret
) {
435 struct btrfs_ioctl_search_args args
= {
436 /* Tree of tree roots */
437 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
439 /* Look precisely for the subvolume items */
440 .key
.min_type
= BTRFS_ROOT_ITEM_KEY
,
441 .key
.max_type
= BTRFS_ROOT_ITEM_KEY
,
444 .key
.max_offset
= (uint64_t) -1,
446 /* No restrictions on the other components */
447 .key
.min_transid
= 0,
448 .key
.max_transid
= (uint64_t) -1,
457 if (subvol_id
== 0) {
458 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
462 r
= btrfs_is_filesystem(fd
);
469 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
471 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
472 const struct btrfs_ioctl_search_header
*sh
;
475 args
.key
.nr_items
= 256;
476 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
479 if (args
.key
.nr_items
<= 0)
482 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
484 const struct btrfs_root_item
*ri
;
486 /* Make sure we start the next search at least from this entry */
487 btrfs_ioctl_search_args_set(&args
, sh
);
489 if (sh
->objectid
!= subvol_id
)
491 if (sh
->type
!= BTRFS_ROOT_ITEM_KEY
)
494 /* Older versions of the struct lacked the otime setting */
495 if (sh
->len
< offsetof(struct btrfs_root_item
, otime
) + sizeof(struct btrfs_timespec
))
498 ri
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
500 ret
->otime
= (usec_t
) le64toh(ri
->otime
.sec
) * USEC_PER_SEC
+
501 (usec_t
) le32toh(ri
->otime
.nsec
) / NSEC_PER_USEC
;
503 ret
->subvol_id
= subvol_id
;
504 ret
->read_only
= le64toh(ri
->flags
) & BTRFS_ROOT_SUBVOL_RDONLY
;
506 assert_cc(sizeof(ri
->uuid
) == sizeof(ret
->uuid
));
507 memcpy(&ret
->uuid
, ri
->uuid
, sizeof(ret
->uuid
));
508 memcpy(&ret
->parent_uuid
, ri
->parent_uuid
, sizeof(ret
->parent_uuid
));
514 /* Increase search key by one, to read the next item, if we can. */
515 if (!btrfs_ioctl_search_args_inc(&args
))
526 int btrfs_qgroup_get_quota_fd(int fd
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
528 struct btrfs_ioctl_search_args args
= {
529 /* Tree of quota items */
530 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
532 /* The object ID is always 0 */
533 .key
.min_objectid
= 0,
534 .key
.max_objectid
= 0,
536 /* Look precisely for the quota items */
537 .key
.min_type
= BTRFS_QGROUP_STATUS_KEY
,
538 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
540 /* No restrictions on the other components */
541 .key
.min_transid
= 0,
542 .key
.max_transid
= (uint64_t) -1,
545 bool found_info
= false, found_limit
= false;
552 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
556 r
= btrfs_is_filesystem(fd
);
563 args
.key
.min_offset
= args
.key
.max_offset
= qgroupid
;
565 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
566 const struct btrfs_ioctl_search_header
*sh
;
569 args
.key
.nr_items
= 256;
570 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
571 if (errno
== ENOENT
) /* quota tree is missing: quota disabled */
577 if (args
.key
.nr_items
<= 0)
580 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
582 /* Make sure we start the next search at least from this entry */
583 btrfs_ioctl_search_args_set(&args
, sh
);
585 if (sh
->objectid
!= 0)
587 if (sh
->offset
!= qgroupid
)
590 if (sh
->type
== BTRFS_QGROUP_INFO_KEY
) {
591 const struct btrfs_qgroup_info_item
*qii
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
593 ret
->referenced
= le64toh(qii
->rfer
);
594 ret
->exclusive
= le64toh(qii
->excl
);
598 } else if (sh
->type
== BTRFS_QGROUP_LIMIT_KEY
) {
599 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
601 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_RFER
)
602 ret
->referenced_max
= le64toh(qli
->max_rfer
);
604 ret
->referenced_max
= (uint64_t) -1;
606 if (le64toh(qli
->flags
) & BTRFS_QGROUP_LIMIT_MAX_EXCL
)
607 ret
->exclusive_max
= le64toh(qli
->max_excl
);
609 ret
->exclusive_max
= (uint64_t) -1;
614 if (found_info
&& found_limit
)
618 /* Increase search key by one, to read the next item, if we can. */
619 if (!btrfs_ioctl_search_args_inc(&args
))
624 if (!found_limit
&& !found_info
)
628 ret
->referenced
= (uint64_t) -1;
629 ret
->exclusive
= (uint64_t) -1;
633 ret
->referenced_max
= (uint64_t) -1;
634 ret
->exclusive_max
= (uint64_t) -1;
640 int btrfs_qgroup_get_quota(const char *path
, uint64_t qgroupid
, BtrfsQuotaInfo
*ret
) {
641 _cleanup_close_
int fd
= -1;
643 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
647 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
650 int btrfs_subvol_find_subtree_qgroup(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
651 uint64_t level
, lowest
= (uint64_t) -1, lowest_qgroupid
= 0;
652 _cleanup_free_
uint64_t *qgroups
= NULL
;
658 /* This finds the "subtree" qgroup for a specific
659 * subvolume. This only works for subvolumes that have been
660 * prepared with btrfs_subvol_auto_qgroup_fd() with
661 * insert_intermediary_qgroup=true (or equivalent). For others
662 * it will return the leaf qgroup instead. The two cases may
663 * be distuingished via the return value, which is 1 in case
664 * an appropriate "subtree" qgroup was found, and 0
667 if (subvol_id
== 0) {
668 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
673 r
= btrfs_qgroupid_split(subvol_id
, &level
, NULL
);
676 if (level
!= 0) /* Input must be a leaf qgroup */
679 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
683 for (i
= 0; i
< n
; i
++) {
686 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, &id
);
693 if (lowest
== (uint64_t) -1 || level
< lowest
) {
694 lowest_qgroupid
= qgroups
[i
];
699 if (lowest
== (uint64_t) -1) {
700 /* No suitable higher-level qgroup found, let's return
701 * the leaf qgroup instead, and indicate that with the
708 *ret
= lowest_qgroupid
;
712 int btrfs_subvol_get_subtree_quota_fd(int fd
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
719 /* This determines the quota data of the qgroup with the
720 * lowest level, that shares the id part with the specified
721 * subvolume. This is useful for determining the quota data
722 * for entire subvolume subtrees, as long as the subtrees have
723 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
726 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
730 return btrfs_qgroup_get_quota_fd(fd
, qgroupid
, ret
);
733 int btrfs_subvol_get_subtree_quota(const char *path
, uint64_t subvol_id
, BtrfsQuotaInfo
*ret
) {
734 _cleanup_close_
int fd
= -1;
736 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
740 return btrfs_subvol_get_subtree_quota_fd(fd
, subvol_id
, ret
);
743 int btrfs_defrag_fd(int fd
) {
748 r
= fd_verify_regular(fd
);
752 if (ioctl(fd
, BTRFS_IOC_DEFRAG
, NULL
) < 0)
758 int btrfs_defrag(const char *p
) {
759 _cleanup_close_
int fd
= -1;
761 fd
= open(p
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
765 return btrfs_defrag_fd(fd
);
768 int btrfs_quota_enable_fd(int fd
, bool b
) {
769 struct btrfs_ioctl_quota_ctl_args args
= {
770 .cmd
= b
? BTRFS_QUOTA_CTL_ENABLE
: BTRFS_QUOTA_CTL_DISABLE
,
776 r
= btrfs_is_filesystem(fd
);
782 if (ioctl(fd
, BTRFS_IOC_QUOTA_CTL
, &args
) < 0)
788 int btrfs_quota_enable(const char *path
, bool b
) {
789 _cleanup_close_
int fd
= -1;
791 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
795 return btrfs_quota_enable_fd(fd
, b
);
798 int btrfs_qgroup_set_limit_fd(int fd
, uint64_t qgroupid
, uint64_t referenced_max
) {
800 struct btrfs_ioctl_qgroup_limit_args args
= {
801 .lim
.max_rfer
= referenced_max
,
802 .lim
.flags
= BTRFS_QGROUP_LIMIT_MAX_RFER
,
810 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
814 r
= btrfs_is_filesystem(fd
);
821 args
.qgroupid
= qgroupid
;
824 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &args
) < 0) {
826 if (errno
== EBUSY
&& c
< 10) {
827 (void) btrfs_quota_scan_wait(fd
);
840 int btrfs_qgroup_set_limit(const char *path
, uint64_t qgroupid
, uint64_t referenced_max
) {
841 _cleanup_close_
int fd
= -1;
843 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
847 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
850 int btrfs_subvol_set_subtree_quota_limit_fd(int fd
, uint64_t subvol_id
, uint64_t referenced_max
) {
856 r
= btrfs_subvol_find_subtree_qgroup(fd
, subvol_id
, &qgroupid
);
860 return btrfs_qgroup_set_limit_fd(fd
, qgroupid
, referenced_max
);
863 int btrfs_subvol_set_subtree_quota_limit(const char *path
, uint64_t subvol_id
, uint64_t referenced_max
) {
864 _cleanup_close_
int fd
= -1;
866 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
);
870 return btrfs_subvol_set_subtree_quota_limit_fd(fd
, subvol_id
, referenced_max
);
873 int btrfs_resize_loopback_fd(int fd
, uint64_t new_size
, bool grow_only
) {
874 struct btrfs_ioctl_vol_args args
= {};
875 char p
[SYS_BLOCK_PATH_MAX("/loop/backing_file")], q
[DEV_NUM_PATH_MAX
];
876 _cleanup_free_
char *backing
= NULL
;
877 _cleanup_close_
int loop_fd
= -1, backing_fd
= -1;
882 /* In contrast to btrfs quota ioctls ftruncate() cannot make sense of "infinity" or file sizes > 2^31 */
883 if (!FILE_SIZE_VALID(new_size
))
886 /* btrfs cannot handle file systems < 16M, hence use this as minimum */
887 if (new_size
< 16*1024*1024)
888 new_size
= 16*1024*1024;
890 r
= btrfs_get_block_device_fd(fd
, &dev
);
896 xsprintf_sys_block_path(p
, "/loop/backing_file", dev
);
897 r
= read_one_line_file(p
, &backing
);
902 if (isempty(backing
) || !path_is_absolute(backing
))
905 backing_fd
= open(backing
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
);
909 if (fstat(backing_fd
, &st
) < 0)
911 if (!S_ISREG(st
.st_mode
))
914 if (new_size
== (uint64_t) st
.st_size
)
917 if (grow_only
&& new_size
< (uint64_t) st
.st_size
)
920 xsprintf_dev_num_path(q
, "block", dev
);
921 loop_fd
= open(q
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
);
925 if (snprintf(args
.name
, sizeof(args
.name
), "%" PRIu64
, new_size
) >= (int) sizeof(args
.name
))
928 if (new_size
< (uint64_t) st
.st_size
) {
929 /* Decrease size: first decrease btrfs size, then shorten loopback */
930 if (ioctl(fd
, BTRFS_IOC_RESIZE
, &args
) < 0)
934 if (ftruncate(backing_fd
, new_size
) < 0)
937 if (ioctl(loop_fd
, LOOP_SET_CAPACITY
, 0) < 0)
940 if (new_size
> (uint64_t) st
.st_size
) {
941 /* Increase size: first enlarge loopback, then increase btrfs size */
942 if (ioctl(fd
, BTRFS_IOC_RESIZE
, &args
) < 0)
946 /* Make sure the free disk space is correctly updated for both file systems */
948 (void) fsync(backing_fd
);
953 int btrfs_resize_loopback(const char *p
, uint64_t new_size
, bool grow_only
) {
954 _cleanup_close_
int fd
= -1;
956 fd
= open(p
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
960 return btrfs_resize_loopback_fd(fd
, new_size
, grow_only
);
963 int btrfs_qgroupid_make(uint64_t level
, uint64_t id
, uint64_t *ret
) {
966 if (level
>= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT
)))
969 if (id
>= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
))
972 *ret
= (level
<< BTRFS_QGROUP_LEVEL_SHIFT
) | id
;
976 int btrfs_qgroupid_split(uint64_t qgroupid
, uint64_t *level
, uint64_t *id
) {
980 *level
= qgroupid
>> BTRFS_QGROUP_LEVEL_SHIFT
;
983 *id
= qgroupid
& ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT
) - 1);
988 static int qgroup_create_or_destroy(int fd
, bool b
, uint64_t qgroupid
) {
990 struct btrfs_ioctl_qgroup_create_args args
= {
992 .qgroupid
= qgroupid
,
997 r
= btrfs_is_filesystem(fd
);
1004 if (ioctl(fd
, BTRFS_IOC_QGROUP_CREATE
, &args
) < 0) {
1006 /* If quota is not enabled, we get EINVAL. Turn this into a recognizable error */
1007 if (errno
== EINVAL
)
1008 return -ENOPROTOOPT
;
1010 if (errno
== EBUSY
&& c
< 10) {
1011 (void) btrfs_quota_scan_wait(fd
);
1024 int btrfs_qgroup_create(int fd
, uint64_t qgroupid
) {
1025 return qgroup_create_or_destroy(fd
, true, qgroupid
);
1028 int btrfs_qgroup_destroy(int fd
, uint64_t qgroupid
) {
1029 return qgroup_create_or_destroy(fd
, false, qgroupid
);
1032 int btrfs_qgroup_destroy_recursive(int fd
, uint64_t qgroupid
) {
1033 _cleanup_free_
uint64_t *qgroups
= NULL
;
1037 /* Destroys the specified qgroup, but unassigns it from all
1038 * its parents first. Also, it recursively destroys all
1039 * qgroups it is assgined to that have the same id part of the
1040 * qgroupid as the specified group. */
1042 r
= btrfs_qgroupid_split(qgroupid
, NULL
, &subvol_id
);
1046 n
= btrfs_qgroup_find_parents(fd
, qgroupid
, &qgroups
);
1050 for (i
= 0; i
< n
; i
++) {
1053 r
= btrfs_qgroupid_split(qgroups
[i
], NULL
, &id
);
1057 r
= btrfs_qgroup_unassign(fd
, qgroupid
, qgroups
[i
]);
1061 if (id
!= subvol_id
)
1064 /* The parent qgroupid shares the same id part with
1065 * us? If so, destroy it too. */
1067 (void) btrfs_qgroup_destroy_recursive(fd
, qgroups
[i
]);
1070 return btrfs_qgroup_destroy(fd
, qgroupid
);
1073 int btrfs_quota_scan_start(int fd
) {
1074 struct btrfs_ioctl_quota_rescan_args args
= {};
1078 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN
, &args
) < 0)
1084 int btrfs_quota_scan_wait(int fd
) {
1087 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_WAIT
) < 0)
1093 int btrfs_quota_scan_ongoing(int fd
) {
1094 struct btrfs_ioctl_quota_rescan_args args
= {};
1098 if (ioctl(fd
, BTRFS_IOC_QUOTA_RESCAN_STATUS
, &args
) < 0)
1101 return !!args
.flags
;
1104 static int qgroup_assign_or_unassign(int fd
, bool b
, uint64_t child
, uint64_t parent
) {
1105 struct btrfs_ioctl_qgroup_assign_args args
= {
1113 r
= btrfs_is_filesystem(fd
);
1120 r
= ioctl(fd
, BTRFS_IOC_QGROUP_ASSIGN
, &args
);
1122 if (errno
== EBUSY
&& c
< 10) {
1123 (void) btrfs_quota_scan_wait(fd
);
1133 /* If the return value is > 0, we need to request a rescan */
1135 (void) btrfs_quota_scan_start(fd
);
1140 int btrfs_qgroup_assign(int fd
, uint64_t child
, uint64_t parent
) {
1141 return qgroup_assign_or_unassign(fd
, true, child
, parent
);
1144 int btrfs_qgroup_unassign(int fd
, uint64_t child
, uint64_t parent
) {
1145 return qgroup_assign_or_unassign(fd
, false, child
, parent
);
1148 static int subvol_remove_children(int fd
, const char *subvolume
, uint64_t subvol_id
, BtrfsRemoveFlags flags
) {
1149 struct btrfs_ioctl_search_args args
= {
1150 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1152 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1153 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1155 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1156 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1158 .key
.min_transid
= 0,
1159 .key
.max_transid
= (uint64_t) -1,
1162 struct btrfs_ioctl_vol_args vol_args
= {};
1163 _cleanup_close_
int subvol_fd
= -1;
1165 bool made_writable
= false;
1171 if (fstat(fd
, &st
) < 0)
1174 if (!S_ISDIR(st
.st_mode
))
1177 subvol_fd
= openat(fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1181 if (subvol_id
== 0) {
1182 r
= btrfs_subvol_get_id_fd(subvol_fd
, &subvol_id
);
1187 /* First, try to remove the subvolume. If it happens to be
1188 * already empty, this will just work. */
1189 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1190 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) >= 0) {
1191 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1194 if (!(flags
& BTRFS_REMOVE_RECURSIVE
) || errno
!= ENOTEMPTY
)
1197 /* OK, the subvolume is not empty, let's look for child
1198 * subvolumes, and remove them, first */
1200 args
.key
.min_offset
= args
.key
.max_offset
= subvol_id
;
1202 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1203 const struct btrfs_ioctl_search_header
*sh
;
1206 args
.key
.nr_items
= 256;
1207 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1210 if (args
.key
.nr_items
<= 0)
1213 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1214 _cleanup_free_
char *p
= NULL
;
1215 const struct btrfs_root_ref
*ref
;
1216 struct btrfs_ioctl_ino_lookup_args ino_args
;
1218 btrfs_ioctl_search_args_set(&args
, sh
);
1220 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1222 if (sh
->offset
!= subvol_id
)
1225 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1227 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1232 ino_args
.treeid
= subvol_id
;
1233 ino_args
.objectid
= htole64(ref
->dirid
);
1235 if (ioctl(fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1238 if (!made_writable
) {
1239 r
= btrfs_subvol_set_read_only_fd(subvol_fd
, false);
1243 made_writable
= true;
1246 if (isempty(ino_args
.name
))
1247 /* Subvolume is in the top-level
1248 * directory of the subvolume. */
1249 r
= subvol_remove_children(subvol_fd
, p
, sh
->objectid
, flags
);
1251 _cleanup_close_
int child_fd
= -1;
1253 /* Subvolume is somewhere further down,
1254 * hence we need to open the
1255 * containing directory first */
1257 child_fd
= openat(subvol_fd
, ino_args
.name
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1261 r
= subvol_remove_children(child_fd
, p
, sh
->objectid
, flags
);
1267 /* Increase search key by one, to read the next item, if we can. */
1268 if (!btrfs_ioctl_search_args_inc(&args
))
1272 /* OK, the child subvolumes should all be gone now, let's try
1273 * again to remove the subvolume */
1274 if (ioctl(fd
, BTRFS_IOC_SNAP_DESTROY
, &vol_args
) < 0)
1277 (void) btrfs_qgroup_destroy_recursive(fd
, subvol_id
);
1281 int btrfs_subvol_remove(const char *path
, BtrfsRemoveFlags flags
) {
1282 _cleanup_close_
int fd
= -1;
1283 const char *subvolume
;
1288 r
= extract_subvolume_name(path
, &subvolume
);
1292 fd
= open_parent(path
, O_CLOEXEC
, 0);
1296 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1299 int btrfs_subvol_remove_fd(int fd
, const char *subvolume
, BtrfsRemoveFlags flags
) {
1300 return subvol_remove_children(fd
, subvolume
, 0, flags
);
1303 int btrfs_qgroup_copy_limits(int fd
, uint64_t old_qgroupid
, uint64_t new_qgroupid
) {
1305 struct btrfs_ioctl_search_args args
= {
1306 /* Tree of quota items */
1307 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1309 /* The object ID is always 0 */
1310 .key
.min_objectid
= 0,
1311 .key
.max_objectid
= 0,
1313 /* Look precisely for the quota items */
1314 .key
.min_type
= BTRFS_QGROUP_LIMIT_KEY
,
1315 .key
.max_type
= BTRFS_QGROUP_LIMIT_KEY
,
1317 /* For our qgroup */
1318 .key
.min_offset
= old_qgroupid
,
1319 .key
.max_offset
= old_qgroupid
,
1321 /* No restrictions on the other components */
1322 .key
.min_transid
= 0,
1323 .key
.max_transid
= (uint64_t) -1,
1328 r
= btrfs_is_filesystem(fd
);
1334 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1335 const struct btrfs_ioctl_search_header
*sh
;
1338 args
.key
.nr_items
= 256;
1339 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1340 if (errno
== ENOENT
) /* quota tree missing: quota is not enabled, hence nothing to copy */
1346 if (args
.key
.nr_items
<= 0)
1349 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1350 const struct btrfs_qgroup_limit_item
*qli
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1351 struct btrfs_ioctl_qgroup_limit_args qargs
;
1354 /* Make sure we start the next search at least from this entry */
1355 btrfs_ioctl_search_args_set(&args
, sh
);
1357 if (sh
->objectid
!= 0)
1359 if (sh
->type
!= BTRFS_QGROUP_LIMIT_KEY
)
1361 if (sh
->offset
!= old_qgroupid
)
1364 /* We found the entry, now copy things over. */
1366 qargs
= (struct btrfs_ioctl_qgroup_limit_args
) {
1367 .qgroupid
= new_qgroupid
,
1369 .lim
.max_rfer
= le64toh(qli
->max_rfer
),
1370 .lim
.max_excl
= le64toh(qli
->max_excl
),
1371 .lim
.rsv_rfer
= le64toh(qli
->rsv_rfer
),
1372 .lim
.rsv_excl
= le64toh(qli
->rsv_excl
),
1374 .lim
.flags
= le64toh(qli
->flags
) & (BTRFS_QGROUP_LIMIT_MAX_RFER
|
1375 BTRFS_QGROUP_LIMIT_MAX_EXCL
|
1376 BTRFS_QGROUP_LIMIT_RSV_RFER
|
1377 BTRFS_QGROUP_LIMIT_RSV_EXCL
),
1381 if (ioctl(fd
, BTRFS_IOC_QGROUP_LIMIT
, &qargs
) < 0) {
1382 if (errno
== EBUSY
&& c
< 10) {
1383 (void) btrfs_quota_scan_wait(fd
);
1395 /* Increase search key by one, to read the next item, if we can. */
1396 if (!btrfs_ioctl_search_args_inc(&args
))
1403 static int copy_quota_hierarchy(int fd
, uint64_t old_subvol_id
, uint64_t new_subvol_id
) {
1404 _cleanup_free_
uint64_t *old_qgroups
= NULL
, *old_parent_qgroups
= NULL
;
1405 bool copy_from_parent
= false, insert_intermediary_qgroup
= false;
1406 int n_old_qgroups
, n_old_parent_qgroups
, r
, i
;
1407 uint64_t old_parent_id
;
1411 /* Copies a reduced form of quota information from the old to
1412 * the new subvolume. */
1414 n_old_qgroups
= btrfs_qgroup_find_parents(fd
, old_subvol_id
, &old_qgroups
);
1415 if (n_old_qgroups
<= 0) /* Nothing to copy */
1416 return n_old_qgroups
;
1418 r
= btrfs_subvol_get_parent(fd
, old_subvol_id
, &old_parent_id
);
1420 /* We have no parent, hence nothing to copy. */
1421 n_old_parent_qgroups
= 0;
1425 n_old_parent_qgroups
= btrfs_qgroup_find_parents(fd
, old_parent_id
, &old_parent_qgroups
);
1426 if (n_old_parent_qgroups
< 0)
1427 return n_old_parent_qgroups
;
1430 for (i
= 0; i
< n_old_qgroups
; i
++) {
1434 r
= btrfs_qgroupid_split(old_qgroups
[i
], NULL
, &id
);
1438 if (id
== old_subvol_id
) {
1439 /* The old subvolume was member of a qgroup
1440 * that had the same id, but a different level
1441 * as it self. Let's set up something similar
1442 * in the destination. */
1443 insert_intermediary_qgroup
= true;
1447 for (j
= 0; j
< n_old_parent_qgroups
; j
++)
1448 if (old_parent_qgroups
[j
] == old_qgroups
[i
]) {
1449 /* The old subvolume shared a common
1450 * parent qgroup with its parent
1451 * subvolume. Let's set up something
1452 * similar in the destination. */
1453 copy_from_parent
= true;
1457 if (!insert_intermediary_qgroup
&& !copy_from_parent
)
1460 return btrfs_subvol_auto_qgroup_fd(fd
, new_subvol_id
, insert_intermediary_qgroup
);
1463 static int copy_subtree_quota_limits(int fd
, uint64_t old_subvol
, uint64_t new_subvol
) {
1464 uint64_t old_subtree_qgroup
, new_subtree_qgroup
;
1468 /* First copy the leaf limits */
1469 r
= btrfs_qgroup_copy_limits(fd
, old_subvol
, new_subvol
);
1474 /* Then, try to copy the subtree limits, if there are any. */
1475 r
= btrfs_subvol_find_subtree_qgroup(fd
, old_subvol
, &old_subtree_qgroup
);
1481 r
= btrfs_subvol_find_subtree_qgroup(fd
, new_subvol
, &new_subtree_qgroup
);
1487 r
= btrfs_qgroup_copy_limits(fd
, old_subtree_qgroup
, new_subtree_qgroup
);
1494 static int subvol_snapshot_children(int old_fd
, int new_fd
, const char *subvolume
, uint64_t old_subvol_id
, BtrfsSnapshotFlags flags
) {
1496 struct btrfs_ioctl_search_args args
= {
1497 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
1499 .key
.min_objectid
= BTRFS_FIRST_FREE_OBJECTID
,
1500 .key
.max_objectid
= BTRFS_LAST_FREE_OBJECTID
,
1502 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
1503 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
1505 .key
.min_transid
= 0,
1506 .key
.max_transid
= (uint64_t) -1,
1509 struct btrfs_ioctl_vol_args_v2 vol_args
= {
1510 .flags
= flags
& BTRFS_SNAPSHOT_READ_ONLY
? BTRFS_SUBVOL_RDONLY
: 0,
1513 _cleanup_close_
int subvolume_fd
= -1;
1514 uint64_t new_subvol_id
;
1517 assert(old_fd
>= 0);
1518 assert(new_fd
>= 0);
1521 strncpy(vol_args
.name
, subvolume
, sizeof(vol_args
.name
)-1);
1523 if (ioctl(new_fd
, BTRFS_IOC_SNAP_CREATE_V2
, &vol_args
) < 0)
1526 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
) &&
1527 !(flags
& BTRFS_SNAPSHOT_QUOTA
))
1530 if (old_subvol_id
== 0) {
1531 r
= btrfs_subvol_get_id_fd(old_fd
, &old_subvol_id
);
1536 r
= btrfs_subvol_get_id(new_fd
, vol_args
.name
, &new_subvol_id
);
1540 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1541 (void) copy_quota_hierarchy(new_fd
, old_subvol_id
, new_subvol_id
);
1543 if (!(flags
& BTRFS_SNAPSHOT_RECURSIVE
)) {
1545 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1546 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1551 args
.key
.min_offset
= args
.key
.max_offset
= old_subvol_id
;
1553 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1554 const struct btrfs_ioctl_search_header
*sh
;
1557 args
.key
.nr_items
= 256;
1558 if (ioctl(old_fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
1561 if (args
.key
.nr_items
<= 0)
1564 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1565 _cleanup_free_
char *p
= NULL
, *c
= NULL
, *np
= NULL
;
1566 struct btrfs_ioctl_ino_lookup_args ino_args
;
1567 const struct btrfs_root_ref
*ref
;
1568 _cleanup_close_
int old_child_fd
= -1, new_child_fd
= -1;
1570 btrfs_ioctl_search_args_set(&args
, sh
);
1572 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
1575 /* Avoid finding the source subvolume a second
1577 if (sh
->offset
!= old_subvol_id
)
1580 /* Avoid running into loops if the new
1581 * subvolume is below the old one. */
1582 if (sh
->objectid
== new_subvol_id
)
1585 ref
= BTRFS_IOCTL_SEARCH_HEADER_BODY(sh
);
1586 p
= strndup((char*) ref
+ sizeof(struct btrfs_root_ref
), le64toh(ref
->name_len
));
1591 ino_args
.treeid
= old_subvol_id
;
1592 ino_args
.objectid
= htole64(ref
->dirid
);
1594 if (ioctl(old_fd
, BTRFS_IOC_INO_LOOKUP
, &ino_args
) < 0)
1597 /* The kernel returns an empty name if the
1598 * subvolume is in the top-level directory,
1599 * and otherwise appends a slash, so that we
1600 * can just concatenate easily here, without
1601 * adding a slash. */
1602 c
= strappend(ino_args
.name
, p
);
1606 old_child_fd
= openat(old_fd
, c
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1607 if (old_child_fd
< 0)
1610 np
= strjoin(subvolume
, "/", ino_args
.name
);
1614 new_child_fd
= openat(new_fd
, np
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1615 if (new_child_fd
< 0)
1618 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1619 /* If the snapshot is read-only we
1620 * need to mark it writable
1621 * temporarily, to put the subsnapshot
1624 if (subvolume_fd
< 0) {
1625 subvolume_fd
= openat(new_fd
, subvolume
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
1626 if (subvolume_fd
< 0)
1630 r
= btrfs_subvol_set_read_only_fd(subvolume_fd
, false);
1635 /* When btrfs clones the subvolumes, child
1636 * subvolumes appear as empty directories. Remove
1637 * them, so that we can create a new snapshot
1639 if (unlinkat(new_child_fd
, p
, AT_REMOVEDIR
) < 0) {
1642 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
)
1643 (void) btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1648 r
= subvol_snapshot_children(old_child_fd
, new_child_fd
, p
, sh
->objectid
, flags
& ~BTRFS_SNAPSHOT_FALLBACK_COPY
);
1650 /* Restore the readonly flag */
1651 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1654 k
= btrfs_subvol_set_read_only_fd(subvolume_fd
, true);
1655 if (r
>= 0 && k
< 0)
1663 /* Increase search key by one, to read the next item, if we can. */
1664 if (!btrfs_ioctl_search_args_inc(&args
))
1668 if (flags
& BTRFS_SNAPSHOT_QUOTA
)
1669 (void) copy_subtree_quota_limits(new_fd
, old_subvol_id
, new_subvol_id
);
1674 int btrfs_subvol_snapshot_fd(int old_fd
, const char *new_path
, BtrfsSnapshotFlags flags
) {
1675 _cleanup_close_
int new_fd
= -1;
1676 const char *subvolume
;
1679 assert(old_fd
>= 0);
1682 r
= btrfs_is_subvol_fd(old_fd
);
1686 bool plain_directory
= false;
1688 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1689 if (!(flags
& BTRFS_SNAPSHOT_FALLBACK_COPY
))
1692 r
= btrfs_subvol_make(new_path
);
1693 if (r
== -ENOTTY
&& (flags
& BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
)) {
1694 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1695 if (mkdir(new_path
, 0755) < 0)
1698 plain_directory
= true;
1702 r
= copy_directory_fd(old_fd
, new_path
, COPY_MERGE
|COPY_REFLINK
);
1706 if (flags
& BTRFS_SNAPSHOT_READ_ONLY
) {
1708 if (plain_directory
) {
1709 /* Plain directories have no recursive read-only flag, but something pretty close to
1710 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1712 if (flags
& BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
)
1713 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
1715 r
= btrfs_subvol_set_read_only(new_path
, true);
1724 (void) rm_rf(new_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
1728 r
= extract_subvolume_name(new_path
, &subvolume
);
1732 new_fd
= open_parent(new_path
, O_CLOEXEC
, 0);
1736 return subvol_snapshot_children(old_fd
, new_fd
, subvolume
, 0, flags
);
1739 int btrfs_subvol_snapshot(const char *old_path
, const char *new_path
, BtrfsSnapshotFlags flags
) {
1740 _cleanup_close_
int old_fd
= -1;
1745 old_fd
= open(old_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1749 return btrfs_subvol_snapshot_fd(old_fd
, new_path
, flags
);
1752 int btrfs_qgroup_find_parents(int fd
, uint64_t qgroupid
, uint64_t **ret
) {
1754 struct btrfs_ioctl_search_args args
= {
1755 /* Tree of quota items */
1756 .key
.tree_id
= BTRFS_QUOTA_TREE_OBJECTID
,
1758 /* Look precisely for the quota relation items */
1759 .key
.min_type
= BTRFS_QGROUP_RELATION_KEY
,
1760 .key
.max_type
= BTRFS_QGROUP_RELATION_KEY
,
1762 /* No restrictions on the other components */
1763 .key
.min_offset
= 0,
1764 .key
.max_offset
= (uint64_t) -1,
1766 .key
.min_transid
= 0,
1767 .key
.max_transid
= (uint64_t) -1,
1770 _cleanup_free_
uint64_t *items
= NULL
;
1771 size_t n_items
= 0, n_allocated
= 0;
1777 if (qgroupid
== 0) {
1778 r
= btrfs_subvol_get_id_fd(fd
, &qgroupid
);
1782 r
= btrfs_is_filesystem(fd
);
1789 args
.key
.min_objectid
= args
.key
.max_objectid
= qgroupid
;
1791 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
1792 const struct btrfs_ioctl_search_header
*sh
;
1795 args
.key
.nr_items
= 256;
1796 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0) {
1797 if (errno
== ENOENT
) /* quota tree missing: quota is disabled */
1803 if (args
.key
.nr_items
<= 0)
1806 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
1808 /* Make sure we start the next search at least from this entry */
1809 btrfs_ioctl_search_args_set(&args
, sh
);
1811 if (sh
->type
!= BTRFS_QGROUP_RELATION_KEY
)
1813 if (sh
->offset
< sh
->objectid
)
1815 if (sh
->objectid
!= qgroupid
)
1818 if (!GREEDY_REALLOC(items
, n_allocated
, n_items
+1))
1821 items
[n_items
++] = sh
->offset
;
1824 /* Increase search key by one, to read the next item, if we can. */
1825 if (!btrfs_ioctl_search_args_inc(&args
))
1834 *ret
= TAKE_PTR(items
);
1836 return (int) n_items
;
1839 int btrfs_subvol_auto_qgroup_fd(int fd
, uint64_t subvol_id
, bool insert_intermediary_qgroup
) {
1840 _cleanup_free_
uint64_t *qgroups
= NULL
;
1841 uint64_t parent_subvol
;
1842 bool changed
= false;
1848 * Sets up the specified subvolume's qgroup automatically in
1851 * If insert_intermediary_qgroup is false, the subvolume's
1852 * leaf qgroup will be assigned to the same parent qgroups as
1853 * the subvolume's parent subvolume.
1855 * If insert_intermediary_qgroup is true a new intermediary
1856 * higher-level qgroup is created, with a higher level number,
1857 * but reusing the id of the subvolume. The level number is
1858 * picked as one smaller than the lowest level qgroup the
1859 * parent subvolume is a member of. If the parent subvolume's
1860 * leaf qgroup is assigned to no higher-level qgroup a new
1861 * qgroup of level 255 is created instead. Either way, the new
1862 * qgroup is then assigned to the parent's higher-level
1863 * qgroup, and the subvolume itself is assigned to it.
1865 * If the subvolume is already assigned to a higher level
1866 * qgroup, no operation is executed.
1868 * Effectively this means: regardless if
1869 * insert_intermediary_qgroup is true or not, after this
1870 * function is invoked the subvolume will be accounted within
1871 * the same qgroups as the parent. However, if it is true, it
1872 * will also get its own higher-level qgroup, which may in
1873 * turn be used by subvolumes created beneath this subvolume
1876 * This hence defines a simple default qgroup setup for
1877 * subvolumes, as long as this function is invoked on each
1878 * created subvolume: each subvolume is always accounting
1879 * together with its immediate parents. Optionally, if
1880 * insert_intermediary_qgroup is true, it will also get a
1881 * qgroup that then includes all its own child subvolumes.
1884 if (subvol_id
== 0) {
1885 r
= btrfs_is_subvol_fd(fd
);
1891 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
1896 n
= btrfs_qgroup_find_parents(fd
, subvol_id
, &qgroups
);
1899 if (n
> 0) /* already parent qgroups set up, let's bail */
1902 qgroups
= mfree(qgroups
);
1904 r
= btrfs_subvol_get_parent(fd
, subvol_id
, &parent_subvol
);
1906 /* No parent, hence no qgroup memberships */
1911 n
= btrfs_qgroup_find_parents(fd
, parent_subvol
, &qgroups
);
1916 if (insert_intermediary_qgroup
) {
1917 uint64_t lowest
= 256, new_qgroupid
;
1918 bool created
= false;
1921 /* Determine the lowest qgroup that the parent
1922 * subvolume is assigned to. */
1924 for (i
= 0; i
< n
; i
++) {
1927 r
= btrfs_qgroupid_split(qgroups
[i
], &level
, NULL
);
1935 if (lowest
<= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1938 r
= btrfs_qgroupid_make(lowest
- 1, subvol_id
, &new_qgroupid
);
1942 /* Create the new intermediary group, unless it already exists */
1943 r
= btrfs_qgroup_create(fd
, new_qgroupid
);
1944 if (r
< 0 && r
!= -EEXIST
)
1947 changed
= created
= true;
1949 for (i
= 0; i
< n
; i
++) {
1950 r
= btrfs_qgroup_assign(fd
, new_qgroupid
, qgroups
[i
]);
1951 if (r
< 0 && r
!= -EEXIST
) {
1953 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1961 r
= btrfs_qgroup_assign(fd
, subvol_id
, new_qgroupid
);
1962 if (r
< 0 && r
!= -EEXIST
) {
1964 (void) btrfs_qgroup_destroy_recursive(fd
, new_qgroupid
);
1973 /* Assign our subvolume to all the same qgroups as the parent */
1975 for (i
= 0; i
< n
; i
++) {
1976 r
= btrfs_qgroup_assign(fd
, subvol_id
, qgroups
[i
]);
1977 if (r
< 0 && r
!= -EEXIST
)
1987 int btrfs_subvol_auto_qgroup(const char *path
, uint64_t subvol_id
, bool create_intermediary_qgroup
) {
1988 _cleanup_close_
int fd
= -1;
1990 fd
= open(path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
|O_DIRECTORY
);
1994 return btrfs_subvol_auto_qgroup_fd(fd
, subvol_id
, create_intermediary_qgroup
);
1997 int btrfs_subvol_get_parent(int fd
, uint64_t subvol_id
, uint64_t *ret
) {
1999 struct btrfs_ioctl_search_args args
= {
2000 /* Tree of tree roots */
2001 .key
.tree_id
= BTRFS_ROOT_TREE_OBJECTID
,
2003 /* Look precisely for the subvolume items */
2004 .key
.min_type
= BTRFS_ROOT_BACKREF_KEY
,
2005 .key
.max_type
= BTRFS_ROOT_BACKREF_KEY
,
2007 /* No restrictions on the other components */
2008 .key
.min_offset
= 0,
2009 .key
.max_offset
= (uint64_t) -1,
2011 .key
.min_transid
= 0,
2012 .key
.max_transid
= (uint64_t) -1,
2019 if (subvol_id
== 0) {
2020 r
= btrfs_subvol_get_id_fd(fd
, &subvol_id
);
2024 r
= btrfs_is_filesystem(fd
);
2031 args
.key
.min_objectid
= args
.key
.max_objectid
= subvol_id
;
2033 while (btrfs_ioctl_search_args_compare(&args
) <= 0) {
2034 const struct btrfs_ioctl_search_header
*sh
;
2037 args
.key
.nr_items
= 256;
2038 if (ioctl(fd
, BTRFS_IOC_TREE_SEARCH
, &args
) < 0)
2039 return negative_errno();
2041 if (args
.key
.nr_items
<= 0)
2044 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i
, sh
, args
) {
2046 if (sh
->type
!= BTRFS_ROOT_BACKREF_KEY
)
2048 if (sh
->objectid
!= subvol_id
)