]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/btrfs-util.c
Merge pull request #25437 from YHNdnzj/systemctl-disable-warn-statically-enabled...
[thirdparty/systemd.git] / src / shared / btrfs-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <inttypes.h>
6 #include <linux/btrfs_tree.h>
7 #include <linux/fs.h>
8 #include <linux/loop.h>
9 #include <linux/magic.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/ioctl.h>
14 #include <sys/sysmacros.h>
15 #include <unistd.h>
16
17 #include "alloc-util.h"
18 #include "blockdev-util.h"
19 #include "btrfs-util.h"
20 #include "chattr-util.h"
21 #include "copy.h"
22 #include "fd-util.h"
23 #include "fileio.h"
24 #include "fs-util.h"
25 #include "io-util.h"
26 #include "macro.h"
27 #include "path-util.h"
28 #include "rm-rf.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"
34
35 /* WARNING: Be careful with file system ioctls! When we get an fd, we
36 * need to make sure it either refers to only a regular file or
37 * directory, or that it is located on btrfs, before invoking any
38 * btrfs ioctls. The ioctl numbers are reused by some device drivers
39 * (such as DRM), and hence might have bad effects when invoked on
40 * device nodes (that reference drivers) rather than fds to normal
41 * files or directories. */
42
43 static int validate_subvolume_name(const char *name) {
44
45 if (!filename_is_valid(name))
46 return -EINVAL;
47
48 if (strlen(name) > BTRFS_SUBVOL_NAME_MAX)
49 return -E2BIG;
50
51 return 0;
52 }
53
54 static int extract_subvolume_name(const char *path, const char **subvolume) {
55 const char *fn;
56 int r;
57
58 assert(path);
59 assert(subvolume);
60
61 fn = basename(path);
62
63 r = validate_subvolume_name(fn);
64 if (r < 0)
65 return r;
66
67 *subvolume = fn;
68 return 0;
69 }
70
71 int btrfs_is_subvol_fd(int fd) {
72 struct stat st;
73
74 assert(fd >= 0);
75
76 /* On btrfs subvolumes always have the inode 256 */
77
78 if (fstat(fd, &st) < 0)
79 return -errno;
80
81 if (!btrfs_might_be_subvol(&st))
82 return 0;
83
84 return fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
85 }
86
87 int btrfs_is_subvol(const char *path) {
88 _cleanup_close_ int fd = -1;
89
90 assert(path);
91
92 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
93 if (fd < 0)
94 return -errno;
95
96 return btrfs_is_subvol_fd(fd);
97 }
98
99 int btrfs_subvol_make_fd(int fd, const char *subvolume) {
100 struct btrfs_ioctl_vol_args args = {};
101 _cleanup_close_ int real_fd = -1;
102 int r;
103
104 assert(subvolume);
105
106 r = validate_subvolume_name(subvolume);
107 if (r < 0)
108 return r;
109
110 /* If an O_PATH fd was specified, let's convert here to a proper one, as btrfs ioctl's can't deal
111 * with O_PATH. */
112 fd = fd_reopen_condition(fd, O_RDONLY|O_CLOEXEC|O_DIRECTORY, O_PATH|O_DIRECTORY, &real_fd);
113 if (fd < 0)
114 return fd;
115
116 strncpy(args.name, subvolume, sizeof(args.name)-1);
117
118 return RET_NERRNO(ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args));
119 }
120
121 int btrfs_subvol_make(const char *path) {
122 _cleanup_close_ int fd = -1;
123 const char *subvolume;
124 int r;
125
126 assert(path);
127
128 r = extract_subvolume_name(path, &subvolume);
129 if (r < 0)
130 return r;
131
132 fd = open_parent(path, O_CLOEXEC, 0);
133 if (fd < 0)
134 return fd;
135
136 return btrfs_subvol_make_fd(fd, subvolume);
137 }
138
139 int btrfs_subvol_make_fallback(const char *path, mode_t mode) {
140 mode_t old, combined;
141 int r;
142
143 assert(path);
144
145 /* Let's work like mkdir(), i.e. take the specified mode, and mask it with the current umask. */
146 old = umask(~mode);
147 combined = old | ~mode;
148 if (combined != ~mode)
149 umask(combined);
150 r = btrfs_subvol_make(path);
151 umask(old);
152
153 if (r >= 0)
154 return 1; /* subvol worked */
155 if (r != -ENOTTY)
156 return r;
157
158 if (mkdir(path, mode) < 0)
159 return -errno;
160
161 return 0; /* plain directory */
162 }
163
164 int btrfs_subvol_set_read_only_fd(int fd, bool b) {
165 uint64_t flags, nflags;
166 struct stat st;
167
168 assert(fd >= 0);
169
170 if (fstat(fd, &st) < 0)
171 return -errno;
172
173 if (!btrfs_might_be_subvol(&st))
174 return -EINVAL;
175
176 if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
177 return -errno;
178
179 nflags = UPDATE_FLAG(flags, BTRFS_SUBVOL_RDONLY, b);
180 if (flags == nflags)
181 return 0;
182
183 return RET_NERRNO(ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &nflags));
184 }
185
186 int btrfs_subvol_set_read_only(const char *path, bool b) {
187 _cleanup_close_ int fd = -1;
188
189 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
190 if (fd < 0)
191 return -errno;
192
193 return btrfs_subvol_set_read_only_fd(fd, b);
194 }
195
196 int btrfs_subvol_get_read_only_fd(int fd) {
197 uint64_t flags;
198 struct stat st;
199
200 assert(fd >= 0);
201
202 if (fstat(fd, &st) < 0)
203 return -errno;
204
205 if (!btrfs_might_be_subvol(&st))
206 return -EINVAL;
207
208 if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
209 return -errno;
210
211 return !!(flags & BTRFS_SUBVOL_RDONLY);
212 }
213
214 int btrfs_reflink(int infd, int outfd) {
215 int r;
216
217 assert(infd >= 0);
218 assert(outfd >= 0);
219
220 /* Make sure we invoke the ioctl on a regular file, so that no device driver accidentally gets it. */
221
222 r = fd_verify_regular(outfd);
223 if (r < 0)
224 return r;
225
226 return RET_NERRNO(ioctl(outfd, BTRFS_IOC_CLONE, infd));
227 }
228
229 int btrfs_clone_range(int infd, uint64_t in_offset, int outfd, uint64_t out_offset, uint64_t sz) {
230 struct btrfs_ioctl_clone_range_args args = {
231 .src_fd = infd,
232 .src_offset = in_offset,
233 .src_length = sz,
234 .dest_offset = out_offset,
235 };
236 int r;
237
238 assert(infd >= 0);
239 assert(outfd >= 0);
240
241 r = fd_verify_regular(outfd);
242 if (r < 0)
243 return r;
244
245 return RET_NERRNO(ioctl(outfd, BTRFS_IOC_CLONE_RANGE, &args));
246 }
247
248 int btrfs_get_block_device_fd(int fd, dev_t *dev) {
249 struct btrfs_ioctl_fs_info_args fsi = {};
250 _cleanup_close_ int regfd = -1;
251 uint64_t id;
252 int r;
253
254 assert(fd >= 0);
255 assert(dev);
256
257 fd = fd_reopen_condition(fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY, O_PATH, &regfd);
258 if (fd < 0)
259 return fd;
260
261 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
262 if (r < 0)
263 return r;
264 if (!r)
265 return -ENOTTY;
266
267 if (ioctl(fd, BTRFS_IOC_FS_INFO, &fsi) < 0)
268 return -errno;
269
270 /* We won't do this for btrfs RAID */
271 if (fsi.num_devices != 1) {
272 *dev = 0;
273 return 0;
274 }
275
276 for (id = 1; id <= fsi.max_id; id++) {
277 struct btrfs_ioctl_dev_info_args di = {
278 .devid = id,
279 };
280 struct stat st;
281
282 if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di) < 0) {
283 if (errno == ENODEV)
284 continue;
285
286 return -errno;
287 }
288
289 /* For the root fs — when no initrd is involved — btrfs returns /dev/root on any kernels from
290 * the past few years. That sucks, as we have no API to determine the actual root then. let's
291 * return an recognizable error for this case, so that the caller can maybe print a nice
292 * message about this.
293 *
294 * https://bugzilla.kernel.org/show_bug.cgi?id=89721 */
295 if (path_equal((char*) di.path, "/dev/root"))
296 return -EUCLEAN;
297
298 if (stat((char*) di.path, &st) < 0)
299 return -errno;
300
301 if (!S_ISBLK(st.st_mode))
302 return -ENOTBLK;
303
304 if (major(st.st_rdev) == 0)
305 return -ENODEV;
306
307 *dev = st.st_rdev;
308 return 1;
309 }
310
311 return -ENODEV;
312 }
313
314 int btrfs_get_block_device(const char *path, dev_t *dev) {
315 _cleanup_close_ int fd = -1;
316
317 assert(path);
318 assert(dev);
319
320 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
321 if (fd < 0)
322 return -errno;
323
324 return btrfs_get_block_device_fd(fd, dev);
325 }
326
327 int btrfs_subvol_get_id_fd(int fd, uint64_t *ret) {
328 struct btrfs_ioctl_ino_lookup_args args = {
329 .objectid = BTRFS_FIRST_FREE_OBJECTID
330 };
331 int r;
332
333 assert(fd >= 0);
334 assert(ret);
335
336 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
337 if (r < 0)
338 return r;
339 if (!r)
340 return -ENOTTY;
341
342 if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
343 return -errno;
344
345 *ret = args.treeid;
346 return 0;
347 }
348
349 int btrfs_subvol_get_id(int fd, const char *subvol, uint64_t *ret) {
350 _cleanup_close_ int subvol_fd = -1;
351
352 assert(fd >= 0);
353 assert(ret);
354
355 subvol_fd = openat(fd, subvol, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
356 if (subvol_fd < 0)
357 return -errno;
358
359 return btrfs_subvol_get_id_fd(subvol_fd, ret);
360 }
361
362 static bool btrfs_ioctl_search_args_inc(struct btrfs_ioctl_search_args *args) {
363 assert(args);
364
365 /* the objectid, type, offset together make up the btrfs key,
366 * which is considered a single 136byte integer when
367 * comparing. This call increases the counter by one, dealing
368 * with the overflow between the overflows */
369
370 if (args->key.min_offset < UINT64_MAX) {
371 args->key.min_offset++;
372 return true;
373 }
374
375 if (args->key.min_type < UINT8_MAX) {
376 args->key.min_type++;
377 args->key.min_offset = 0;
378 return true;
379 }
380
381 if (args->key.min_objectid < UINT64_MAX) {
382 args->key.min_objectid++;
383 args->key.min_offset = 0;
384 args->key.min_type = 0;
385 return true;
386 }
387
388 return 0;
389 }
390
391 static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args *args, const struct btrfs_ioctl_search_header *h) {
392 assert(args);
393 assert(h);
394
395 args->key.min_objectid = h->objectid;
396 args->key.min_type = h->type;
397 args->key.min_offset = h->offset;
398 }
399
400 static int btrfs_ioctl_search_args_compare(const struct btrfs_ioctl_search_args *args) {
401 int r;
402
403 assert(args);
404
405 /* Compare min and max */
406
407 r = CMP(args->key.min_objectid, args->key.max_objectid);
408 if (r != 0)
409 return r;
410
411 r = CMP(args->key.min_type, args->key.max_type);
412 if (r != 0)
413 return r;
414
415 return CMP(args->key.min_offset, args->key.max_offset);
416 }
417
418 #define FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) \
419 for ((i) = 0, \
420 (sh) = (const struct btrfs_ioctl_search_header*) (args).buf; \
421 (i) < (args).key.nr_items; \
422 (i)++, \
423 (sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
424
425 #define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
426 ((void*) ((uint8_t*) sh + sizeof(struct btrfs_ioctl_search_header)))
427
428 int btrfs_subvol_get_info_fd(int fd, uint64_t subvol_id, BtrfsSubvolInfo *ret) {
429 struct btrfs_ioctl_search_args args = {
430 /* Tree of tree roots */
431 .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
432
433 /* Look precisely for the subvolume items */
434 .key.min_type = BTRFS_ROOT_ITEM_KEY,
435 .key.max_type = BTRFS_ROOT_ITEM_KEY,
436
437 .key.min_offset = 0,
438 .key.max_offset = UINT64_MAX,
439
440 /* No restrictions on the other components */
441 .key.min_transid = 0,
442 .key.max_transid = UINT64_MAX,
443 };
444
445 bool found = false;
446 int r;
447
448 assert(fd >= 0);
449 assert(ret);
450
451 if (subvol_id == 0) {
452 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
453 if (r < 0)
454 return r;
455 } else {
456 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
457 if (r < 0)
458 return r;
459 if (!r)
460 return -ENOTTY;
461 }
462
463 args.key.min_objectid = args.key.max_objectid = subvol_id;
464
465 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
466 const struct btrfs_ioctl_search_header *sh;
467 unsigned i;
468
469 args.key.nr_items = 256;
470 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
471 return -errno;
472
473 if (args.key.nr_items <= 0)
474 break;
475
476 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
477
478 const struct btrfs_root_item *ri;
479
480 /* Make sure we start the next search at least from this entry */
481 btrfs_ioctl_search_args_set(&args, sh);
482
483 if (sh->objectid != subvol_id)
484 continue;
485 if (sh->type != BTRFS_ROOT_ITEM_KEY)
486 continue;
487
488 /* Older versions of the struct lacked the otime setting */
489 if (sh->len < offsetof(struct btrfs_root_item, otime) + sizeof(struct btrfs_timespec))
490 continue;
491
492 ri = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
493
494 ret->otime = (usec_t) le64toh(ri->otime.sec) * USEC_PER_SEC +
495 (usec_t) le32toh(ri->otime.nsec) / NSEC_PER_USEC;
496
497 ret->subvol_id = subvol_id;
498 ret->read_only = le64toh(ri->flags) & BTRFS_ROOT_SUBVOL_RDONLY;
499
500 assert_cc(sizeof(ri->uuid) == sizeof(ret->uuid));
501 memcpy(&ret->uuid, ri->uuid, sizeof(ret->uuid));
502 memcpy(&ret->parent_uuid, ri->parent_uuid, sizeof(ret->parent_uuid));
503
504 found = true;
505 goto finish;
506 }
507
508 /* Increase search key by one, to read the next item, if we can. */
509 if (!btrfs_ioctl_search_args_inc(&args))
510 break;
511 }
512
513 finish:
514 if (!found)
515 return -ENODATA;
516
517 return 0;
518 }
519
520 int btrfs_qgroup_get_quota_fd(int fd, uint64_t qgroupid, BtrfsQuotaInfo *ret) {
521
522 struct btrfs_ioctl_search_args args = {
523 /* Tree of quota items */
524 .key.tree_id = BTRFS_QUOTA_TREE_OBJECTID,
525
526 /* The object ID is always 0 */
527 .key.min_objectid = 0,
528 .key.max_objectid = 0,
529
530 /* Look precisely for the quota items */
531 .key.min_type = BTRFS_QGROUP_STATUS_KEY,
532 .key.max_type = BTRFS_QGROUP_LIMIT_KEY,
533
534 /* No restrictions on the other components */
535 .key.min_transid = 0,
536 .key.max_transid = UINT64_MAX,
537 };
538
539 bool found_info = false, found_limit = false;
540 int r;
541
542 assert(fd >= 0);
543 assert(ret);
544
545 if (qgroupid == 0) {
546 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
547 if (r < 0)
548 return r;
549 } else {
550 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
551 if (r < 0)
552 return r;
553 if (!r)
554 return -ENOTTY;
555 }
556
557 args.key.min_offset = args.key.max_offset = qgroupid;
558
559 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
560 const struct btrfs_ioctl_search_header *sh;
561 unsigned i;
562
563 args.key.nr_items = 256;
564 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
565 if (errno == ENOENT) /* quota tree is missing: quota disabled */
566 break;
567
568 return -errno;
569 }
570
571 if (args.key.nr_items <= 0)
572 break;
573
574 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
575
576 /* Make sure we start the next search at least from this entry */
577 btrfs_ioctl_search_args_set(&args, sh);
578
579 if (sh->objectid != 0)
580 continue;
581 if (sh->offset != qgroupid)
582 continue;
583
584 if (sh->type == BTRFS_QGROUP_INFO_KEY) {
585 const struct btrfs_qgroup_info_item *qii = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
586
587 ret->referenced = le64toh(qii->rfer);
588 ret->exclusive = le64toh(qii->excl);
589
590 found_info = true;
591
592 } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
593 const struct btrfs_qgroup_limit_item *qli = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
594
595 if (le64toh(qli->flags) & BTRFS_QGROUP_LIMIT_MAX_RFER)
596 ret->referenced_max = le64toh(qli->max_rfer);
597 else
598 ret->referenced_max = UINT64_MAX;
599
600 if (le64toh(qli->flags) & BTRFS_QGROUP_LIMIT_MAX_EXCL)
601 ret->exclusive_max = le64toh(qli->max_excl);
602 else
603 ret->exclusive_max = UINT64_MAX;
604
605 found_limit = true;
606 }
607
608 if (found_info && found_limit)
609 goto finish;
610 }
611
612 /* Increase search key by one, to read the next item, if we can. */
613 if (!btrfs_ioctl_search_args_inc(&args))
614 break;
615 }
616
617 finish:
618 if (!found_limit && !found_info)
619 return -ENODATA;
620
621 if (!found_info) {
622 ret->referenced = UINT64_MAX;
623 ret->exclusive = UINT64_MAX;
624 }
625
626 if (!found_limit) {
627 ret->referenced_max = UINT64_MAX;
628 ret->exclusive_max = UINT64_MAX;
629 }
630
631 return 0;
632 }
633
634 int btrfs_qgroup_get_quota(const char *path, uint64_t qgroupid, BtrfsQuotaInfo *ret) {
635 _cleanup_close_ int fd = -1;
636
637 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
638 if (fd < 0)
639 return -errno;
640
641 return btrfs_qgroup_get_quota_fd(fd, qgroupid, ret);
642 }
643
644 int btrfs_subvol_find_subtree_qgroup(int fd, uint64_t subvol_id, uint64_t *ret) {
645 uint64_t level, lowest = UINT64_MAX, lowest_qgroupid = 0;
646 _cleanup_free_ uint64_t *qgroups = NULL;
647 int r, n;
648
649 assert(fd >= 0);
650 assert(ret);
651
652 /* This finds the "subtree" qgroup for a specific
653 * subvolume. This only works for subvolumes that have been
654 * prepared with btrfs_subvol_auto_qgroup_fd() with
655 * insert_intermediary_qgroup=true (or equivalent). For others
656 * it will return the leaf qgroup instead. The two cases may
657 * be distinguished via the return value, which is 1 in case
658 * an appropriate "subtree" qgroup was found, and 0
659 * otherwise. */
660
661 if (subvol_id == 0) {
662 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
663 if (r < 0)
664 return r;
665 }
666
667 r = btrfs_qgroupid_split(subvol_id, &level, NULL);
668 if (r < 0)
669 return r;
670 if (level != 0) /* Input must be a leaf qgroup */
671 return -EINVAL;
672
673 n = btrfs_qgroup_find_parents(fd, subvol_id, &qgroups);
674 if (n < 0)
675 return n;
676
677 for (int i = 0; i < n; i++) {
678 uint64_t id;
679
680 r = btrfs_qgroupid_split(qgroups[i], &level, &id);
681 if (r < 0)
682 return r;
683
684 if (id != subvol_id)
685 continue;
686
687 if (lowest == UINT64_MAX || level < lowest) {
688 lowest_qgroupid = qgroups[i];
689 lowest = level;
690 }
691 }
692
693 if (lowest == UINT64_MAX) {
694 /* No suitable higher-level qgroup found, let's return
695 * the leaf qgroup instead, and indicate that with the
696 * return value. */
697
698 *ret = subvol_id;
699 return 0;
700 }
701
702 *ret = lowest_qgroupid;
703 return 1;
704 }
705
706 int btrfs_subvol_get_subtree_quota_fd(int fd, uint64_t subvol_id, BtrfsQuotaInfo *ret) {
707 uint64_t qgroupid;
708 int r;
709
710 assert(fd >= 0);
711 assert(ret);
712
713 /* This determines the quota data of the qgroup with the
714 * lowest level, that shares the id part with the specified
715 * subvolume. This is useful for determining the quota data
716 * for entire subvolume subtrees, as long as the subtrees have
717 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
718 * compatible way */
719
720 r = btrfs_subvol_find_subtree_qgroup(fd, subvol_id, &qgroupid);
721 if (r < 0)
722 return r;
723
724 return btrfs_qgroup_get_quota_fd(fd, qgroupid, ret);
725 }
726
727 int btrfs_subvol_get_subtree_quota(const char *path, uint64_t subvol_id, BtrfsQuotaInfo *ret) {
728 _cleanup_close_ int fd = -1;
729
730 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
731 if (fd < 0)
732 return -errno;
733
734 return btrfs_subvol_get_subtree_quota_fd(fd, subvol_id, ret);
735 }
736
737 int btrfs_defrag_fd(int fd) {
738 int r;
739
740 assert(fd >= 0);
741
742 r = fd_verify_regular(fd);
743 if (r < 0)
744 return r;
745
746 return RET_NERRNO(ioctl(fd, BTRFS_IOC_DEFRAG, NULL));
747 }
748
749 int btrfs_defrag(const char *p) {
750 _cleanup_close_ int fd = -1;
751
752 fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
753 if (fd < 0)
754 return -errno;
755
756 return btrfs_defrag_fd(fd);
757 }
758
759 int btrfs_quota_enable_fd(int fd, bool b) {
760 struct btrfs_ioctl_quota_ctl_args args = {
761 .cmd = b ? BTRFS_QUOTA_CTL_ENABLE : BTRFS_QUOTA_CTL_DISABLE,
762 };
763 int r;
764
765 assert(fd >= 0);
766
767 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
768 if (r < 0)
769 return r;
770 if (!r)
771 return -ENOTTY;
772
773 return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args));
774 }
775
776 int btrfs_quota_enable(const char *path, bool b) {
777 _cleanup_close_ int fd = -1;
778
779 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
780 if (fd < 0)
781 return -errno;
782
783 return btrfs_quota_enable_fd(fd, b);
784 }
785
786 int btrfs_qgroup_set_limit_fd(int fd, uint64_t qgroupid, uint64_t referenced_max) {
787
788 struct btrfs_ioctl_qgroup_limit_args args = {
789 .lim.max_rfer = referenced_max,
790 .lim.flags = BTRFS_QGROUP_LIMIT_MAX_RFER,
791 };
792 int r;
793
794 assert(fd >= 0);
795
796 if (qgroupid == 0) {
797 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
798 if (r < 0)
799 return r;
800 } else {
801 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
802 if (r < 0)
803 return r;
804 if (!r)
805 return -ENOTTY;
806 }
807
808 args.qgroupid = qgroupid;
809
810 for (unsigned c = 0;; c++) {
811 if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args) < 0) {
812
813 if (errno == EBUSY && c < 10) {
814 (void) btrfs_quota_scan_wait(fd);
815 continue;
816 }
817
818 return -errno;
819 }
820
821 break;
822 }
823
824 return 0;
825 }
826
827 int btrfs_qgroup_set_limit(const char *path, uint64_t qgroupid, uint64_t referenced_max) {
828 _cleanup_close_ int fd = -1;
829
830 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
831 if (fd < 0)
832 return -errno;
833
834 return btrfs_qgroup_set_limit_fd(fd, qgroupid, referenced_max);
835 }
836
837 int btrfs_subvol_set_subtree_quota_limit_fd(int fd, uint64_t subvol_id, uint64_t referenced_max) {
838 uint64_t qgroupid;
839 int r;
840
841 assert(fd >= 0);
842
843 r = btrfs_subvol_find_subtree_qgroup(fd, subvol_id, &qgroupid);
844 if (r < 0)
845 return r;
846
847 return btrfs_qgroup_set_limit_fd(fd, qgroupid, referenced_max);
848 }
849
850 int btrfs_subvol_set_subtree_quota_limit(const char *path, uint64_t subvol_id, uint64_t referenced_max) {
851 _cleanup_close_ int fd = -1;
852
853 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
854 if (fd < 0)
855 return -errno;
856
857 return btrfs_subvol_set_subtree_quota_limit_fd(fd, subvol_id, referenced_max);
858 }
859
860 int btrfs_qgroupid_make(uint64_t level, uint64_t id, uint64_t *ret) {
861 assert(ret);
862
863 if (level >= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT)))
864 return -EINVAL;
865
866 if (id >= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT))
867 return -EINVAL;
868
869 *ret = (level << BTRFS_QGROUP_LEVEL_SHIFT) | id;
870 return 0;
871 }
872
873 int btrfs_qgroupid_split(uint64_t qgroupid, uint64_t *level, uint64_t *id) {
874 assert(level || id);
875
876 if (level)
877 *level = qgroupid >> BTRFS_QGROUP_LEVEL_SHIFT;
878
879 if (id)
880 *id = qgroupid & ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT) - 1);
881
882 return 0;
883 }
884
885 static int qgroup_create_or_destroy(int fd, bool b, uint64_t qgroupid) {
886
887 struct btrfs_ioctl_qgroup_create_args args = {
888 .create = b,
889 .qgroupid = qgroupid,
890 };
891 int r;
892
893 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
894 if (r < 0)
895 return r;
896 if (r == 0)
897 return -ENOTTY;
898
899 for (unsigned c = 0;; c++) {
900 if (ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args) < 0) {
901
902 /* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
903 * ENOTCONN. Let's always convert this to ENOTCONN to make this recognizable
904 * everywhere the same way. */
905
906 if (IN_SET(errno, EINVAL, ENOTCONN))
907 return -ENOTCONN;
908
909 if (errno == EBUSY && c < 10) {
910 (void) btrfs_quota_scan_wait(fd);
911 continue;
912 }
913
914 return -errno;
915 }
916
917 break;
918 }
919
920 return 0;
921 }
922
923 int btrfs_qgroup_create(int fd, uint64_t qgroupid) {
924 return qgroup_create_or_destroy(fd, true, qgroupid);
925 }
926
927 int btrfs_qgroup_destroy(int fd, uint64_t qgroupid) {
928 return qgroup_create_or_destroy(fd, false, qgroupid);
929 }
930
931 int btrfs_qgroup_destroy_recursive(int fd, uint64_t qgroupid) {
932 _cleanup_free_ uint64_t *qgroups = NULL;
933 uint64_t subvol_id;
934 int n, r;
935
936 /* Destroys the specified qgroup, but unassigns it from all
937 * its parents first. Also, it recursively destroys all
938 * qgroups it is assigned to that have the same id part of the
939 * qgroupid as the specified group. */
940
941 r = btrfs_qgroupid_split(qgroupid, NULL, &subvol_id);
942 if (r < 0)
943 return r;
944
945 n = btrfs_qgroup_find_parents(fd, qgroupid, &qgroups);
946 if (n < 0)
947 return n;
948
949 for (int i = 0; i < n; i++) {
950 uint64_t id;
951
952 r = btrfs_qgroupid_split(qgroups[i], NULL, &id);
953 if (r < 0)
954 return r;
955
956 r = btrfs_qgroup_unassign(fd, qgroupid, qgroups[i]);
957 if (r < 0)
958 return r;
959
960 if (id != subvol_id)
961 continue;
962
963 /* The parent qgroupid shares the same id part with
964 * us? If so, destroy it too. */
965
966 (void) btrfs_qgroup_destroy_recursive(fd, qgroups[i]);
967 }
968
969 return btrfs_qgroup_destroy(fd, qgroupid);
970 }
971
972 int btrfs_quota_scan_start(int fd) {
973 struct btrfs_ioctl_quota_rescan_args args = {};
974
975 assert(fd >= 0);
976
977 return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args));
978 }
979
980 int btrfs_quota_scan_wait(int fd) {
981 assert(fd >= 0);
982
983 return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT));
984 }
985
986 int btrfs_quota_scan_ongoing(int fd) {
987 struct btrfs_ioctl_quota_rescan_args args = {};
988
989 assert(fd >= 0);
990
991 if (ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_STATUS, &args) < 0)
992 return -errno;
993
994 return !!args.flags;
995 }
996
997 static int qgroup_assign_or_unassign(int fd, bool b, uint64_t child, uint64_t parent) {
998 struct btrfs_ioctl_qgroup_assign_args args = {
999 .assign = b,
1000 .src = child,
1001 .dst = parent,
1002 };
1003 int r;
1004
1005 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1006 if (r < 0)
1007 return r;
1008 if (r == 0)
1009 return -ENOTTY;
1010
1011 for (unsigned c = 0;; c++) {
1012 r = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
1013 if (r < 0) {
1014 if (errno == EBUSY && c < 10) {
1015 (void) btrfs_quota_scan_wait(fd);
1016 continue;
1017 }
1018
1019 return -errno;
1020 }
1021
1022 if (r == 0)
1023 return 0;
1024
1025 /* If the return value is > 0, we need to request a rescan */
1026
1027 (void) btrfs_quota_scan_start(fd);
1028 return 1;
1029 }
1030 }
1031
1032 int btrfs_qgroup_assign(int fd, uint64_t child, uint64_t parent) {
1033 return qgroup_assign_or_unassign(fd, true, child, parent);
1034 }
1035
1036 int btrfs_qgroup_unassign(int fd, uint64_t child, uint64_t parent) {
1037 return qgroup_assign_or_unassign(fd, false, child, parent);
1038 }
1039
1040 static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol_id, BtrfsRemoveFlags flags) {
1041 struct btrfs_ioctl_search_args args = {
1042 .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
1043
1044 .key.min_objectid = BTRFS_FIRST_FREE_OBJECTID,
1045 .key.max_objectid = BTRFS_LAST_FREE_OBJECTID,
1046
1047 .key.min_type = BTRFS_ROOT_BACKREF_KEY,
1048 .key.max_type = BTRFS_ROOT_BACKREF_KEY,
1049
1050 .key.min_transid = 0,
1051 .key.max_transid = UINT64_MAX,
1052 };
1053
1054 struct btrfs_ioctl_vol_args vol_args = {};
1055 _cleanup_close_ int subvol_fd = -1;
1056 struct stat st;
1057 bool made_writable = false;
1058 int r;
1059
1060 assert(fd >= 0);
1061 assert(subvolume);
1062
1063 if (fstat(fd, &st) < 0)
1064 return -errno;
1065
1066 if (!S_ISDIR(st.st_mode))
1067 return -EINVAL;
1068
1069 subvol_fd = openat(fd, subvolume, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1070 if (subvol_fd < 0)
1071 return -errno;
1072
1073 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1074 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1075 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1076 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1077 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1078 * let's prefer ENOTTY over EPERM/EACCES though. */
1079 r = btrfs_is_subvol_fd(subvol_fd);
1080 if (r < 0)
1081 return r;
1082 if (r == 0) /* Not a btrfs subvolume */
1083 return -ENOTTY;
1084
1085 if (subvol_id == 0) {
1086 r = btrfs_subvol_get_id_fd(subvol_fd, &subvol_id);
1087 if (r < 0)
1088 return r;
1089 }
1090
1091 /* First, try to remove the subvolume. If it happens to be
1092 * already empty, this will just work. */
1093 strncpy(vol_args.name, subvolume, sizeof(vol_args.name)-1);
1094 if (ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &vol_args) >= 0) {
1095 (void) btrfs_qgroup_destroy_recursive(fd, subvol_id); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1096 return 0;
1097 }
1098 if (!(flags & BTRFS_REMOVE_RECURSIVE) || errno != ENOTEMPTY)
1099 return -errno;
1100
1101 /* OK, the subvolume is not empty, let's look for child
1102 * subvolumes, and remove them, first */
1103
1104 args.key.min_offset = args.key.max_offset = subvol_id;
1105
1106 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1107 const struct btrfs_ioctl_search_header *sh;
1108 unsigned i;
1109
1110 args.key.nr_items = 256;
1111 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
1112 return -errno;
1113
1114 if (args.key.nr_items <= 0)
1115 break;
1116
1117 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1118 _cleanup_free_ char *p = NULL;
1119 const struct btrfs_root_ref *ref;
1120
1121 btrfs_ioctl_search_args_set(&args, sh);
1122
1123 if (sh->type != BTRFS_ROOT_BACKREF_KEY)
1124 continue;
1125 if (sh->offset != subvol_id)
1126 continue;
1127
1128 ref = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
1129
1130 p = strndup((char*) ref + sizeof(struct btrfs_root_ref), le64toh(ref->name_len));
1131 if (!p)
1132 return -ENOMEM;
1133
1134 struct btrfs_ioctl_ino_lookup_args ino_args = {
1135 .treeid = subvol_id,
1136 .objectid = htole64(ref->dirid),
1137 };
1138
1139 if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args) < 0)
1140 return -errno;
1141
1142 if (!made_writable) {
1143 r = btrfs_subvol_set_read_only_fd(subvol_fd, false);
1144 if (r < 0)
1145 return r;
1146
1147 made_writable = true;
1148 }
1149
1150 if (isempty(ino_args.name))
1151 /* Subvolume is in the top-level
1152 * directory of the subvolume. */
1153 r = subvol_remove_children(subvol_fd, p, sh->objectid, flags);
1154 else {
1155 _cleanup_close_ int child_fd = -1;
1156
1157 /* Subvolume is somewhere further down,
1158 * hence we need to open the
1159 * containing directory first */
1160
1161 child_fd = openat(subvol_fd, ino_args.name, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1162 if (child_fd < 0)
1163 return -errno;
1164
1165 r = subvol_remove_children(child_fd, p, sh->objectid, flags);
1166 }
1167 if (r < 0)
1168 return r;
1169 }
1170
1171 /* Increase search key by one, to read the next item, if we can. */
1172 if (!btrfs_ioctl_search_args_inc(&args))
1173 break;
1174 }
1175
1176 /* OK, the child subvolumes should all be gone now, let's try
1177 * again to remove the subvolume */
1178 if (ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &vol_args) < 0)
1179 return -errno;
1180
1181 (void) btrfs_qgroup_destroy_recursive(fd, subvol_id);
1182 return 0;
1183 }
1184
1185 int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags) {
1186 _cleanup_close_ int fd = -1;
1187 const char *subvolume;
1188 int r;
1189
1190 assert(path);
1191
1192 r = extract_subvolume_name(path, &subvolume);
1193 if (r < 0)
1194 return r;
1195
1196 fd = open_parent(path, O_CLOEXEC, 0);
1197 if (fd < 0)
1198 return fd;
1199
1200 return subvol_remove_children(fd, subvolume, 0, flags);
1201 }
1202
1203 int btrfs_subvol_remove_fd(int fd, const char *subvolume, BtrfsRemoveFlags flags) {
1204 return subvol_remove_children(fd, subvolume, 0, flags);
1205 }
1206
1207 int btrfs_qgroup_copy_limits(int fd, uint64_t old_qgroupid, uint64_t new_qgroupid) {
1208
1209 struct btrfs_ioctl_search_args args = {
1210 /* Tree of quota items */
1211 .key.tree_id = BTRFS_QUOTA_TREE_OBJECTID,
1212
1213 /* The object ID is always 0 */
1214 .key.min_objectid = 0,
1215 .key.max_objectid = 0,
1216
1217 /* Look precisely for the quota items */
1218 .key.min_type = BTRFS_QGROUP_LIMIT_KEY,
1219 .key.max_type = BTRFS_QGROUP_LIMIT_KEY,
1220
1221 /* For our qgroup */
1222 .key.min_offset = old_qgroupid,
1223 .key.max_offset = old_qgroupid,
1224
1225 /* No restrictions on the other components */
1226 .key.min_transid = 0,
1227 .key.max_transid = UINT64_MAX,
1228 };
1229
1230 int r;
1231
1232 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1233 if (r < 0)
1234 return r;
1235 if (!r)
1236 return -ENOTTY;
1237
1238 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1239 const struct btrfs_ioctl_search_header *sh;
1240 unsigned i;
1241
1242 args.key.nr_items = 256;
1243 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
1244 if (errno == ENOENT) /* quota tree missing: quota is not enabled, hence nothing to copy */
1245 break;
1246
1247 return -errno;
1248 }
1249
1250 if (args.key.nr_items <= 0)
1251 break;
1252
1253 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1254 const struct btrfs_qgroup_limit_item *qli = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
1255 struct btrfs_ioctl_qgroup_limit_args qargs;
1256 unsigned c;
1257
1258 /* Make sure we start the next search at least from this entry */
1259 btrfs_ioctl_search_args_set(&args, sh);
1260
1261 if (sh->objectid != 0)
1262 continue;
1263 if (sh->type != BTRFS_QGROUP_LIMIT_KEY)
1264 continue;
1265 if (sh->offset != old_qgroupid)
1266 continue;
1267
1268 /* We found the entry, now copy things over. */
1269
1270 qargs = (struct btrfs_ioctl_qgroup_limit_args) {
1271 .qgroupid = new_qgroupid,
1272
1273 .lim.max_rfer = le64toh(qli->max_rfer),
1274 .lim.max_excl = le64toh(qli->max_excl),
1275 .lim.rsv_rfer = le64toh(qli->rsv_rfer),
1276 .lim.rsv_excl = le64toh(qli->rsv_excl),
1277
1278 .lim.flags = le64toh(qli->flags) & (BTRFS_QGROUP_LIMIT_MAX_RFER|
1279 BTRFS_QGROUP_LIMIT_MAX_EXCL|
1280 BTRFS_QGROUP_LIMIT_RSV_RFER|
1281 BTRFS_QGROUP_LIMIT_RSV_EXCL),
1282 };
1283
1284 for (c = 0;; c++) {
1285 if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &qargs) < 0) {
1286 if (errno == EBUSY && c < 10) {
1287 (void) btrfs_quota_scan_wait(fd);
1288 continue;
1289 }
1290 return -errno;
1291 }
1292
1293 break;
1294 }
1295
1296 return 1;
1297 }
1298
1299 /* Increase search key by one, to read the next item, if we can. */
1300 if (!btrfs_ioctl_search_args_inc(&args))
1301 break;
1302 }
1303
1304 return 0;
1305 }
1306
1307 static int copy_quota_hierarchy(int fd, uint64_t old_subvol_id, uint64_t new_subvol_id) {
1308 _cleanup_free_ uint64_t *old_qgroups = NULL, *old_parent_qgroups = NULL;
1309 bool copy_from_parent = false, insert_intermediary_qgroup = false;
1310 int n_old_qgroups, n_old_parent_qgroups, r;
1311 uint64_t old_parent_id;
1312
1313 assert(fd >= 0);
1314
1315 /* Copies a reduced form of quota information from the old to
1316 * the new subvolume. */
1317
1318 n_old_qgroups = btrfs_qgroup_find_parents(fd, old_subvol_id, &old_qgroups);
1319 if (n_old_qgroups <= 0) /* Nothing to copy */
1320 return n_old_qgroups;
1321
1322 r = btrfs_subvol_get_parent(fd, old_subvol_id, &old_parent_id);
1323 if (r == -ENXIO)
1324 /* We have no parent, hence nothing to copy. */
1325 n_old_parent_qgroups = 0;
1326 else if (r < 0)
1327 return r;
1328 else {
1329 n_old_parent_qgroups = btrfs_qgroup_find_parents(fd, old_parent_id, &old_parent_qgroups);
1330 if (n_old_parent_qgroups < 0)
1331 return n_old_parent_qgroups;
1332 }
1333
1334 for (int i = 0; i < n_old_qgroups; i++) {
1335 uint64_t id;
1336
1337 r = btrfs_qgroupid_split(old_qgroups[i], NULL, &id);
1338 if (r < 0)
1339 return r;
1340
1341 if (id == old_subvol_id) {
1342 /* The old subvolume was member of a qgroup
1343 * that had the same id, but a different level
1344 * as it self. Let's set up something similar
1345 * in the destination. */
1346 insert_intermediary_qgroup = true;
1347 break;
1348 }
1349
1350 for (int j = 0; j < n_old_parent_qgroups; j++)
1351 if (old_parent_qgroups[j] == old_qgroups[i])
1352 /* The old subvolume shared a common
1353 * parent qgroup with its parent
1354 * subvolume. Let's set up something
1355 * similar in the destination. */
1356 copy_from_parent = true;
1357 }
1358
1359 if (!insert_intermediary_qgroup && !copy_from_parent)
1360 return 0;
1361
1362 return btrfs_subvol_auto_qgroup_fd(fd, new_subvol_id, insert_intermediary_qgroup);
1363 }
1364
1365 static int copy_subtree_quota_limits(int fd, uint64_t old_subvol, uint64_t new_subvol) {
1366 uint64_t old_subtree_qgroup, new_subtree_qgroup;
1367 bool changed;
1368 int r;
1369
1370 /* First copy the leaf limits */
1371 r = btrfs_qgroup_copy_limits(fd, old_subvol, new_subvol);
1372 if (r < 0)
1373 return r;
1374 changed = r > 0;
1375
1376 /* Then, try to copy the subtree limits, if there are any. */
1377 r = btrfs_subvol_find_subtree_qgroup(fd, old_subvol, &old_subtree_qgroup);
1378 if (r < 0)
1379 return r;
1380 if (r == 0)
1381 return changed;
1382
1383 r = btrfs_subvol_find_subtree_qgroup(fd, new_subvol, &new_subtree_qgroup);
1384 if (r < 0)
1385 return r;
1386 if (r == 0)
1387 return changed;
1388
1389 r = btrfs_qgroup_copy_limits(fd, old_subtree_qgroup, new_subtree_qgroup);
1390 if (r != 0)
1391 return r;
1392
1393 return changed;
1394 }
1395
1396 static int subvol_snapshot_children(
1397 int old_fd,
1398 int new_fd,
1399 const char *subvolume,
1400 uint64_t old_subvol_id,
1401 BtrfsSnapshotFlags flags) {
1402
1403 struct btrfs_ioctl_search_args args = {
1404 .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
1405
1406 .key.min_objectid = BTRFS_FIRST_FREE_OBJECTID,
1407 .key.max_objectid = BTRFS_LAST_FREE_OBJECTID,
1408
1409 .key.min_type = BTRFS_ROOT_BACKREF_KEY,
1410 .key.max_type = BTRFS_ROOT_BACKREF_KEY,
1411
1412 .key.min_transid = 0,
1413 .key.max_transid = UINT64_MAX,
1414 };
1415
1416 struct btrfs_ioctl_vol_args_v2 vol_args = {
1417 .flags = flags & BTRFS_SNAPSHOT_READ_ONLY ? BTRFS_SUBVOL_RDONLY : 0,
1418 .fd = old_fd,
1419 };
1420 _cleanup_close_ int subvolume_fd = -1;
1421 uint64_t new_subvol_id;
1422 int r;
1423
1424 assert(old_fd >= 0);
1425 assert(new_fd >= 0);
1426 assert(subvolume);
1427
1428 strncpy(vol_args.name, subvolume, sizeof(vol_args.name)-1);
1429
1430 if (ioctl(new_fd, BTRFS_IOC_SNAP_CREATE_V2, &vol_args) < 0)
1431 return -errno;
1432
1433 if (!(flags & BTRFS_SNAPSHOT_RECURSIVE) &&
1434 !(flags & BTRFS_SNAPSHOT_QUOTA))
1435 return 0;
1436
1437 if (old_subvol_id == 0) {
1438 r = btrfs_subvol_get_id_fd(old_fd, &old_subvol_id);
1439 if (r < 0)
1440 return r;
1441 }
1442
1443 r = btrfs_subvol_get_id(new_fd, vol_args.name, &new_subvol_id);
1444 if (r < 0)
1445 return r;
1446
1447 if (flags & BTRFS_SNAPSHOT_QUOTA)
1448 (void) copy_quota_hierarchy(new_fd, old_subvol_id, new_subvol_id);
1449
1450 if (!(flags & BTRFS_SNAPSHOT_RECURSIVE)) {
1451
1452 if (flags & BTRFS_SNAPSHOT_QUOTA)
1453 (void) copy_subtree_quota_limits(new_fd, old_subvol_id, new_subvol_id);
1454
1455 return 0;
1456 }
1457
1458 args.key.min_offset = args.key.max_offset = old_subvol_id;
1459
1460 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1461 const struct btrfs_ioctl_search_header *sh;
1462 unsigned i;
1463
1464 args.key.nr_items = 256;
1465 if (ioctl(old_fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
1466 return -errno;
1467
1468 if (args.key.nr_items <= 0)
1469 break;
1470
1471 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1472 _cleanup_free_ char *p = NULL, *c = NULL, *np = NULL;
1473 const struct btrfs_root_ref *ref;
1474 _cleanup_close_ int old_child_fd = -1, new_child_fd = -1;
1475
1476 btrfs_ioctl_search_args_set(&args, sh);
1477
1478 if (sh->type != BTRFS_ROOT_BACKREF_KEY)
1479 continue;
1480
1481 /* Avoid finding the source subvolume a second
1482 * time */
1483 if (sh->offset != old_subvol_id)
1484 continue;
1485
1486 /* Avoid running into loops if the new
1487 * subvolume is below the old one. */
1488 if (sh->objectid == new_subvol_id)
1489 continue;
1490
1491 ref = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
1492 p = strndup((char*) ref + sizeof(struct btrfs_root_ref), le64toh(ref->name_len));
1493 if (!p)
1494 return -ENOMEM;
1495
1496 struct btrfs_ioctl_ino_lookup_args ino_args = {
1497 .treeid = old_subvol_id,
1498 .objectid = htole64(ref->dirid),
1499 };
1500
1501 if (ioctl(old_fd, BTRFS_IOC_INO_LOOKUP, &ino_args) < 0)
1502 return -errno;
1503
1504 c = path_join(ino_args.name, p);
1505 if (!c)
1506 return -ENOMEM;
1507
1508 old_child_fd = openat(old_fd, c, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1509 if (old_child_fd < 0)
1510 return -errno;
1511
1512 np = path_join(subvolume, ino_args.name);
1513 if (!np)
1514 return -ENOMEM;
1515
1516 new_child_fd = openat(new_fd, np, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1517 if (new_child_fd < 0)
1518 return -errno;
1519
1520 if (flags & BTRFS_SNAPSHOT_READ_ONLY) {
1521 /* If the snapshot is read-only we
1522 * need to mark it writable
1523 * temporarily, to put the subsnapshot
1524 * into place. */
1525
1526 if (subvolume_fd < 0) {
1527 subvolume_fd = openat(new_fd, subvolume, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1528 if (subvolume_fd < 0)
1529 return -errno;
1530 }
1531
1532 r = btrfs_subvol_set_read_only_fd(subvolume_fd, false);
1533 if (r < 0)
1534 return r;
1535 }
1536
1537 /* When btrfs clones the subvolumes, child
1538 * subvolumes appear as empty directories. Remove
1539 * them, so that we can create a new snapshot
1540 * in their place */
1541 if (unlinkat(new_child_fd, p, AT_REMOVEDIR) < 0) {
1542 int k = -errno;
1543
1544 if (flags & BTRFS_SNAPSHOT_READ_ONLY)
1545 (void) btrfs_subvol_set_read_only_fd(subvolume_fd, true);
1546
1547 return k;
1548 }
1549
1550 r = subvol_snapshot_children(old_child_fd, new_child_fd, p, sh->objectid, flags & ~BTRFS_SNAPSHOT_FALLBACK_COPY);
1551
1552 /* Restore the readonly flag */
1553 if (flags & BTRFS_SNAPSHOT_READ_ONLY) {
1554 int k;
1555
1556 k = btrfs_subvol_set_read_only_fd(subvolume_fd, true);
1557 if (r >= 0 && k < 0)
1558 return k;
1559 }
1560
1561 if (r < 0)
1562 return r;
1563 }
1564
1565 /* Increase search key by one, to read the next item, if we can. */
1566 if (!btrfs_ioctl_search_args_inc(&args))
1567 break;
1568 }
1569
1570 if (flags & BTRFS_SNAPSHOT_QUOTA)
1571 (void) copy_subtree_quota_limits(new_fd, old_subvol_id, new_subvol_id);
1572
1573 return 0;
1574 }
1575
1576 int btrfs_subvol_snapshot_fd_full(
1577 int old_fd,
1578 const char *new_path,
1579 BtrfsSnapshotFlags flags,
1580 copy_progress_path_t progress_path,
1581 copy_progress_bytes_t progress_bytes,
1582 void *userdata) {
1583
1584 _cleanup_close_ int new_fd = -1;
1585 const char *subvolume;
1586 int r;
1587
1588 assert(old_fd >= 0);
1589 assert(new_path);
1590
1591 r = btrfs_is_subvol_fd(old_fd);
1592 if (r < 0)
1593 return r;
1594 if (r == 0) {
1595 bool plain_directory = false;
1596
1597 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1598 if (!(flags & BTRFS_SNAPSHOT_FALLBACK_COPY))
1599 return -EISDIR;
1600
1601 r = btrfs_subvol_make(new_path);
1602 if (ERRNO_IS_NOT_SUPPORTED(r) && (flags & BTRFS_SNAPSHOT_FALLBACK_DIRECTORY)) {
1603 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1604 if (mkdir(new_path, 0755) < 0)
1605 return -errno;
1606
1607 plain_directory = true;
1608 } else if (r < 0)
1609 return r;
1610
1611 r = copy_directory_fd_full(
1612 old_fd, new_path,
1613 COPY_MERGE_EMPTY|
1614 COPY_REFLINK|
1615 COPY_SAME_MOUNT|
1616 COPY_HARDLINKS|
1617 COPY_ALL_XATTRS|
1618 (FLAGS_SET(flags, BTRFS_SNAPSHOT_SIGINT) ? COPY_SIGINT : 0)|
1619 (FLAGS_SET(flags, BTRFS_SNAPSHOT_SIGTERM) ? COPY_SIGTERM : 0),
1620 progress_path,
1621 progress_bytes,
1622 userdata);
1623 if (r < 0)
1624 goto fallback_fail;
1625
1626 if (flags & BTRFS_SNAPSHOT_READ_ONLY) {
1627
1628 if (plain_directory) {
1629 /* Plain directories have no recursive read-only flag, but something pretty close to
1630 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1631
1632 if (flags & BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE)
1633 (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL, NULL);
1634 } else {
1635 r = btrfs_subvol_set_read_only(new_path, true);
1636 if (r < 0)
1637 goto fallback_fail;
1638 }
1639 }
1640
1641 return 0;
1642
1643 fallback_fail:
1644 (void) rm_rf(new_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
1645 return r;
1646 }
1647
1648 r = extract_subvolume_name(new_path, &subvolume);
1649 if (r < 0)
1650 return r;
1651
1652 new_fd = open_parent(new_path, O_CLOEXEC, 0);
1653 if (new_fd < 0)
1654 return new_fd;
1655
1656 return subvol_snapshot_children(old_fd, new_fd, subvolume, 0, flags);
1657 }
1658
1659 int btrfs_subvol_snapshot_full(
1660 const char *old_path,
1661 const char *new_path,
1662 BtrfsSnapshotFlags flags,
1663 copy_progress_path_t progress_path,
1664 copy_progress_bytes_t progress_bytes,
1665 void *userdata) {
1666
1667 _cleanup_close_ int old_fd = -1;
1668
1669 assert(old_path);
1670 assert(new_path);
1671
1672 old_fd = open(old_path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1673 if (old_fd < 0)
1674 return -errno;
1675
1676 return btrfs_subvol_snapshot_fd_full(old_fd, new_path, flags, progress_path, progress_bytes, userdata);
1677 }
1678
1679 int btrfs_qgroup_find_parents(int fd, uint64_t qgroupid, uint64_t **ret) {
1680
1681 struct btrfs_ioctl_search_args args = {
1682 /* Tree of quota items */
1683 .key.tree_id = BTRFS_QUOTA_TREE_OBJECTID,
1684
1685 /* Look precisely for the quota relation items */
1686 .key.min_type = BTRFS_QGROUP_RELATION_KEY,
1687 .key.max_type = BTRFS_QGROUP_RELATION_KEY,
1688
1689 /* No restrictions on the other components */
1690 .key.min_offset = 0,
1691 .key.max_offset = UINT64_MAX,
1692
1693 .key.min_transid = 0,
1694 .key.max_transid = UINT64_MAX,
1695 };
1696
1697 _cleanup_free_ uint64_t *items = NULL;
1698 size_t n_items = 0;
1699 int r;
1700
1701 assert(fd >= 0);
1702 assert(ret);
1703
1704 if (qgroupid == 0) {
1705 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
1706 if (r < 0)
1707 return r;
1708 } else {
1709 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1710 if (r < 0)
1711 return r;
1712 if (!r)
1713 return -ENOTTY;
1714 }
1715
1716 args.key.min_objectid = args.key.max_objectid = qgroupid;
1717
1718 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1719 const struct btrfs_ioctl_search_header *sh;
1720 unsigned i;
1721
1722 args.key.nr_items = 256;
1723 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
1724 if (errno == ENOENT) /* quota tree missing: quota is disabled */
1725 break;
1726
1727 return -errno;
1728 }
1729
1730 if (args.key.nr_items <= 0)
1731 break;
1732
1733 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1734
1735 /* Make sure we start the next search at least from this entry */
1736 btrfs_ioctl_search_args_set(&args, sh);
1737
1738 if (sh->type != BTRFS_QGROUP_RELATION_KEY)
1739 continue;
1740 if (sh->offset < sh->objectid)
1741 continue;
1742 if (sh->objectid != qgroupid)
1743 continue;
1744
1745 if (!GREEDY_REALLOC(items, n_items+1))
1746 return -ENOMEM;
1747
1748 items[n_items++] = sh->offset;
1749 }
1750
1751 /* Increase search key by one, to read the next item, if we can. */
1752 if (!btrfs_ioctl_search_args_inc(&args))
1753 break;
1754 }
1755
1756 if (n_items <= 0) {
1757 *ret = NULL;
1758 return 0;
1759 }
1760
1761 *ret = TAKE_PTR(items);
1762
1763 return (int) n_items;
1764 }
1765
1766 int btrfs_subvol_auto_qgroup_fd(int fd, uint64_t subvol_id, bool insert_intermediary_qgroup) {
1767 _cleanup_free_ uint64_t *qgroups = NULL;
1768 _cleanup_close_ int real_fd = -1;
1769 uint64_t parent_subvol;
1770 bool changed = false;
1771 int n = 0, r;
1772
1773 assert(fd >= 0);
1774
1775 /*
1776 * Sets up the specified subvolume's qgroup automatically in
1777 * one of two ways:
1778 *
1779 * If insert_intermediary_qgroup is false, the subvolume's
1780 * leaf qgroup will be assigned to the same parent qgroups as
1781 * the subvolume's parent subvolume.
1782 *
1783 * If insert_intermediary_qgroup is true a new intermediary
1784 * higher-level qgroup is created, with a higher level number,
1785 * but reusing the id of the subvolume. The level number is
1786 * picked as one smaller than the lowest level qgroup the
1787 * parent subvolume is a member of. If the parent subvolume's
1788 * leaf qgroup is assigned to no higher-level qgroup a new
1789 * qgroup of level 255 is created instead. Either way, the new
1790 * qgroup is then assigned to the parent's higher-level
1791 * qgroup, and the subvolume itself is assigned to it.
1792 *
1793 * If the subvolume is already assigned to a higher level
1794 * qgroup, no operation is executed.
1795 *
1796 * Effectively this means: regardless if
1797 * insert_intermediary_qgroup is true or not, after this
1798 * function is invoked the subvolume will be accounted within
1799 * the same qgroups as the parent. However, if it is true, it
1800 * will also get its own higher-level qgroup, which may in
1801 * turn be used by subvolumes created beneath this subvolume
1802 * later on.
1803 *
1804 * This hence defines a simple default qgroup setup for
1805 * subvolumes, as long as this function is invoked on each
1806 * created subvolume: each subvolume is always accounting
1807 * together with its immediate parents. Optionally, if
1808 * insert_intermediary_qgroup is true, it will also get a
1809 * qgroup that then includes all its own child subvolumes.
1810 */
1811
1812 /* Turn this into a proper fd, if it is currently O_PATH */
1813 fd = fd_reopen_condition(fd, O_RDONLY|O_CLOEXEC, O_PATH, &real_fd);
1814 if (fd < 0)
1815 return fd;
1816
1817 if (subvol_id == 0) {
1818 r = btrfs_is_subvol_fd(fd);
1819 if (r < 0)
1820 return r;
1821 if (!r)
1822 return -ENOTTY;
1823
1824 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
1825 if (r < 0)
1826 return r;
1827 }
1828
1829 n = btrfs_qgroup_find_parents(fd, subvol_id, &qgroups);
1830 if (n < 0)
1831 return n;
1832 if (n > 0) /* already parent qgroups set up, let's bail */
1833 return 0;
1834
1835 qgroups = mfree(qgroups);
1836
1837 r = btrfs_subvol_get_parent(fd, subvol_id, &parent_subvol);
1838 if (r == -ENXIO)
1839 /* No parent, hence no qgroup memberships */
1840 n = 0;
1841 else if (r < 0)
1842 return r;
1843 else {
1844 n = btrfs_qgroup_find_parents(fd, parent_subvol, &qgroups);
1845 if (n < 0)
1846 return n;
1847 }
1848
1849 if (insert_intermediary_qgroup) {
1850 uint64_t lowest = 256, new_qgroupid;
1851 bool created = false;
1852
1853 /* Determine the lowest qgroup that the parent
1854 * subvolume is assigned to. */
1855
1856 for (int i = 0; i < n; i++) {
1857 uint64_t level;
1858
1859 r = btrfs_qgroupid_split(qgroups[i], &level, NULL);
1860 if (r < 0)
1861 return r;
1862
1863 if (level < lowest)
1864 lowest = level;
1865 }
1866
1867 if (lowest <= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1868 return -EBUSY;
1869
1870 r = btrfs_qgroupid_make(lowest - 1, subvol_id, &new_qgroupid);
1871 if (r < 0)
1872 return r;
1873
1874 /* Create the new intermediary group, unless it already exists */
1875 r = btrfs_qgroup_create(fd, new_qgroupid);
1876 if (r < 0 && r != -EEXIST)
1877 return r;
1878 if (r >= 0)
1879 changed = created = true;
1880
1881 for (int i = 0; i < n; i++) {
1882 r = btrfs_qgroup_assign(fd, new_qgroupid, qgroups[i]);
1883 if (r < 0 && r != -EEXIST) {
1884 if (created)
1885 (void) btrfs_qgroup_destroy_recursive(fd, new_qgroupid);
1886
1887 return r;
1888 }
1889 if (r >= 0)
1890 changed = true;
1891 }
1892
1893 r = btrfs_qgroup_assign(fd, subvol_id, new_qgroupid);
1894 if (r < 0 && r != -EEXIST) {
1895 if (created)
1896 (void) btrfs_qgroup_destroy_recursive(fd, new_qgroupid);
1897 return r;
1898 }
1899 if (r >= 0)
1900 changed = true;
1901
1902 } else {
1903 int i;
1904
1905 /* Assign our subvolume to all the same qgroups as the parent */
1906
1907 for (i = 0; i < n; i++) {
1908 r = btrfs_qgroup_assign(fd, subvol_id, qgroups[i]);
1909 if (r < 0 && r != -EEXIST)
1910 return r;
1911 if (r >= 0)
1912 changed = true;
1913 }
1914 }
1915
1916 return changed;
1917 }
1918
1919 int btrfs_subvol_auto_qgroup(const char *path, uint64_t subvol_id, bool create_intermediary_qgroup) {
1920 _cleanup_close_ int fd = -1;
1921
1922 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1923 if (fd < 0)
1924 return -errno;
1925
1926 return btrfs_subvol_auto_qgroup_fd(fd, subvol_id, create_intermediary_qgroup);
1927 }
1928
1929 int btrfs_subvol_get_parent(int fd, uint64_t subvol_id, uint64_t *ret) {
1930
1931 struct btrfs_ioctl_search_args args = {
1932 /* Tree of tree roots */
1933 .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
1934
1935 /* Look precisely for the subvolume items */
1936 .key.min_type = BTRFS_ROOT_BACKREF_KEY,
1937 .key.max_type = BTRFS_ROOT_BACKREF_KEY,
1938
1939 /* No restrictions on the other components */
1940 .key.min_offset = 0,
1941 .key.max_offset = UINT64_MAX,
1942
1943 .key.min_transid = 0,
1944 .key.max_transid = UINT64_MAX,
1945 };
1946 int r;
1947
1948 assert(fd >= 0);
1949 assert(ret);
1950
1951 if (subvol_id == 0) {
1952 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
1953 if (r < 0)
1954 return r;
1955 } else {
1956 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1957 if (r < 0)
1958 return r;
1959 if (!r)
1960 return -ENOTTY;
1961 }
1962
1963 args.key.min_objectid = args.key.max_objectid = subvol_id;
1964
1965 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1966 const struct btrfs_ioctl_search_header *sh;
1967 unsigned i;
1968
1969 args.key.nr_items = 256;
1970 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
1971 return negative_errno();
1972
1973 if (args.key.nr_items <= 0)
1974 break;
1975
1976 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1977
1978 if (sh->type != BTRFS_ROOT_BACKREF_KEY)
1979 continue;
1980 if (sh->objectid != subvol_id)
1981 continue;
1982
1983 *ret = sh->offset;
1984 return 0;
1985 }
1986 }
1987
1988 return -ENXIO;
1989 }