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