]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/btrfs-util.c
tree-wide: use -EBADF for fd initialization
[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 = -EBADF;
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 = -EBADF;
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 = -EBADF;
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 = -EBADF;
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 = -EBADF;
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 = -EBADF;
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 = -EBADF;
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 return found ? 0 : -ENODATA;
515 }
516
517 int btrfs_qgroup_get_quota_fd(int fd, uint64_t qgroupid, BtrfsQuotaInfo *ret) {
518
519 struct btrfs_ioctl_search_args args = {
520 /* Tree of quota items */
521 .key.tree_id = BTRFS_QUOTA_TREE_OBJECTID,
522
523 /* The object ID is always 0 */
524 .key.min_objectid = 0,
525 .key.max_objectid = 0,
526
527 /* Look precisely for the quota items */
528 .key.min_type = BTRFS_QGROUP_STATUS_KEY,
529 .key.max_type = BTRFS_QGROUP_LIMIT_KEY,
530
531 /* No restrictions on the other components */
532 .key.min_transid = 0,
533 .key.max_transid = UINT64_MAX,
534 };
535
536 bool found_info = false, found_limit = false;
537 int r;
538
539 assert(fd >= 0);
540 assert(ret);
541
542 if (qgroupid == 0) {
543 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
544 if (r < 0)
545 return r;
546 } else {
547 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
548 if (r < 0)
549 return r;
550 if (!r)
551 return -ENOTTY;
552 }
553
554 args.key.min_offset = args.key.max_offset = qgroupid;
555
556 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
557 const struct btrfs_ioctl_search_header *sh;
558 unsigned i;
559
560 args.key.nr_items = 256;
561 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
562 if (errno == ENOENT) /* quota tree is missing: quota disabled */
563 break;
564
565 return -errno;
566 }
567
568 if (args.key.nr_items <= 0)
569 break;
570
571 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
572
573 /* Make sure we start the next search at least from this entry */
574 btrfs_ioctl_search_args_set(&args, sh);
575
576 if (sh->objectid != 0)
577 continue;
578 if (sh->offset != qgroupid)
579 continue;
580
581 if (sh->type == BTRFS_QGROUP_INFO_KEY) {
582 const struct btrfs_qgroup_info_item *qii = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
583
584 ret->referenced = le64toh(qii->rfer);
585 ret->exclusive = le64toh(qii->excl);
586
587 found_info = true;
588
589 } else if (sh->type == BTRFS_QGROUP_LIMIT_KEY) {
590 const struct btrfs_qgroup_limit_item *qli = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
591
592 if (le64toh(qli->flags) & BTRFS_QGROUP_LIMIT_MAX_RFER)
593 ret->referenced_max = le64toh(qli->max_rfer);
594 else
595 ret->referenced_max = UINT64_MAX;
596
597 if (le64toh(qli->flags) & BTRFS_QGROUP_LIMIT_MAX_EXCL)
598 ret->exclusive_max = le64toh(qli->max_excl);
599 else
600 ret->exclusive_max = UINT64_MAX;
601
602 found_limit = true;
603 }
604
605 if (found_info && found_limit)
606 goto finish;
607 }
608
609 /* Increase search key by one, to read the next item, if we can. */
610 if (!btrfs_ioctl_search_args_inc(&args))
611 break;
612 }
613
614 finish:
615 if (!found_limit && !found_info)
616 return -ENODATA;
617
618 if (!found_info) {
619 ret->referenced = UINT64_MAX;
620 ret->exclusive = UINT64_MAX;
621 }
622
623 if (!found_limit) {
624 ret->referenced_max = UINT64_MAX;
625 ret->exclusive_max = UINT64_MAX;
626 }
627
628 return 0;
629 }
630
631 int btrfs_qgroup_get_quota(const char *path, uint64_t qgroupid, BtrfsQuotaInfo *ret) {
632 _cleanup_close_ int fd = -EBADF;
633
634 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
635 if (fd < 0)
636 return -errno;
637
638 return btrfs_qgroup_get_quota_fd(fd, qgroupid, ret);
639 }
640
641 int btrfs_subvol_find_subtree_qgroup(int fd, uint64_t subvol_id, uint64_t *ret) {
642 uint64_t level, lowest = UINT64_MAX, lowest_qgroupid = 0;
643 _cleanup_free_ uint64_t *qgroups = NULL;
644 int r, n;
645
646 assert(fd >= 0);
647 assert(ret);
648
649 /* This finds the "subtree" qgroup for a specific
650 * subvolume. This only works for subvolumes that have been
651 * prepared with btrfs_subvol_auto_qgroup_fd() with
652 * insert_intermediary_qgroup=true (or equivalent). For others
653 * it will return the leaf qgroup instead. The two cases may
654 * be distinguished via the return value, which is 1 in case
655 * an appropriate "subtree" qgroup was found, and 0
656 * otherwise. */
657
658 if (subvol_id == 0) {
659 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
660 if (r < 0)
661 return r;
662 }
663
664 r = btrfs_qgroupid_split(subvol_id, &level, NULL);
665 if (r < 0)
666 return r;
667 if (level != 0) /* Input must be a leaf qgroup */
668 return -EINVAL;
669
670 n = btrfs_qgroup_find_parents(fd, subvol_id, &qgroups);
671 if (n < 0)
672 return n;
673
674 for (int i = 0; i < n; i++) {
675 uint64_t id;
676
677 r = btrfs_qgroupid_split(qgroups[i], &level, &id);
678 if (r < 0)
679 return r;
680
681 if (id != subvol_id)
682 continue;
683
684 if (lowest == UINT64_MAX || level < lowest) {
685 lowest_qgroupid = qgroups[i];
686 lowest = level;
687 }
688 }
689
690 if (lowest == UINT64_MAX) {
691 /* No suitable higher-level qgroup found, let's return
692 * the leaf qgroup instead, and indicate that with the
693 * return value. */
694
695 *ret = subvol_id;
696 return 0;
697 }
698
699 *ret = lowest_qgroupid;
700 return 1;
701 }
702
703 int btrfs_subvol_get_subtree_quota_fd(int fd, uint64_t subvol_id, BtrfsQuotaInfo *ret) {
704 uint64_t qgroupid;
705 int r;
706
707 assert(fd >= 0);
708 assert(ret);
709
710 /* This determines the quota data of the qgroup with the
711 * lowest level, that shares the id part with the specified
712 * subvolume. This is useful for determining the quota data
713 * for entire subvolume subtrees, as long as the subtrees have
714 * been set up with btrfs_qgroup_subvol_auto_fd() or in a
715 * compatible way */
716
717 r = btrfs_subvol_find_subtree_qgroup(fd, subvol_id, &qgroupid);
718 if (r < 0)
719 return r;
720
721 return btrfs_qgroup_get_quota_fd(fd, qgroupid, ret);
722 }
723
724 int btrfs_subvol_get_subtree_quota(const char *path, uint64_t subvol_id, BtrfsQuotaInfo *ret) {
725 _cleanup_close_ int fd = -EBADF;
726
727 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
728 if (fd < 0)
729 return -errno;
730
731 return btrfs_subvol_get_subtree_quota_fd(fd, subvol_id, ret);
732 }
733
734 int btrfs_defrag_fd(int fd) {
735 int r;
736
737 assert(fd >= 0);
738
739 r = fd_verify_regular(fd);
740 if (r < 0)
741 return r;
742
743 return RET_NERRNO(ioctl(fd, BTRFS_IOC_DEFRAG, NULL));
744 }
745
746 int btrfs_defrag(const char *p) {
747 _cleanup_close_ int fd = -EBADF;
748
749 fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
750 if (fd < 0)
751 return -errno;
752
753 return btrfs_defrag_fd(fd);
754 }
755
756 int btrfs_quota_enable_fd(int fd, bool b) {
757 struct btrfs_ioctl_quota_ctl_args args = {
758 .cmd = b ? BTRFS_QUOTA_CTL_ENABLE : BTRFS_QUOTA_CTL_DISABLE,
759 };
760 int r;
761
762 assert(fd >= 0);
763
764 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
765 if (r < 0)
766 return r;
767 if (!r)
768 return -ENOTTY;
769
770 return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_CTL, &args));
771 }
772
773 int btrfs_quota_enable(const char *path, bool b) {
774 _cleanup_close_ int fd = -EBADF;
775
776 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
777 if (fd < 0)
778 return -errno;
779
780 return btrfs_quota_enable_fd(fd, b);
781 }
782
783 int btrfs_qgroup_set_limit_fd(int fd, uint64_t qgroupid, uint64_t referenced_max) {
784
785 struct btrfs_ioctl_qgroup_limit_args args = {
786 .lim.max_rfer = referenced_max,
787 .lim.flags = BTRFS_QGROUP_LIMIT_MAX_RFER,
788 };
789 int r;
790
791 assert(fd >= 0);
792
793 if (qgroupid == 0) {
794 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
795 if (r < 0)
796 return r;
797 } else {
798 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
799 if (r < 0)
800 return r;
801 if (!r)
802 return -ENOTTY;
803 }
804
805 args.qgroupid = qgroupid;
806
807 for (unsigned c = 0;; c++) {
808 if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &args) < 0) {
809
810 if (errno == EBUSY && c < 10) {
811 (void) btrfs_quota_scan_wait(fd);
812 continue;
813 }
814
815 return -errno;
816 }
817
818 break;
819 }
820
821 return 0;
822 }
823
824 int btrfs_qgroup_set_limit(const char *path, uint64_t qgroupid, uint64_t referenced_max) {
825 _cleanup_close_ int fd = -EBADF;
826
827 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
828 if (fd < 0)
829 return -errno;
830
831 return btrfs_qgroup_set_limit_fd(fd, qgroupid, referenced_max);
832 }
833
834 int btrfs_subvol_set_subtree_quota_limit_fd(int fd, uint64_t subvol_id, uint64_t referenced_max) {
835 uint64_t qgroupid;
836 int r;
837
838 assert(fd >= 0);
839
840 r = btrfs_subvol_find_subtree_qgroup(fd, subvol_id, &qgroupid);
841 if (r < 0)
842 return r;
843
844 return btrfs_qgroup_set_limit_fd(fd, qgroupid, referenced_max);
845 }
846
847 int btrfs_subvol_set_subtree_quota_limit(const char *path, uint64_t subvol_id, uint64_t referenced_max) {
848 _cleanup_close_ int fd = -EBADF;
849
850 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
851 if (fd < 0)
852 return -errno;
853
854 return btrfs_subvol_set_subtree_quota_limit_fd(fd, subvol_id, referenced_max);
855 }
856
857 int btrfs_qgroupid_make(uint64_t level, uint64_t id, uint64_t *ret) {
858 assert(ret);
859
860 if (level >= (UINT64_C(1) << (64 - BTRFS_QGROUP_LEVEL_SHIFT)))
861 return -EINVAL;
862
863 if (id >= (UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT))
864 return -EINVAL;
865
866 *ret = (level << BTRFS_QGROUP_LEVEL_SHIFT) | id;
867 return 0;
868 }
869
870 int btrfs_qgroupid_split(uint64_t qgroupid, uint64_t *level, uint64_t *id) {
871 assert(level || id);
872
873 if (level)
874 *level = qgroupid >> BTRFS_QGROUP_LEVEL_SHIFT;
875
876 if (id)
877 *id = qgroupid & ((UINT64_C(1) << BTRFS_QGROUP_LEVEL_SHIFT) - 1);
878
879 return 0;
880 }
881
882 static int qgroup_create_or_destroy(int fd, bool b, uint64_t qgroupid) {
883
884 struct btrfs_ioctl_qgroup_create_args args = {
885 .create = b,
886 .qgroupid = qgroupid,
887 };
888 int r;
889
890 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
891 if (r < 0)
892 return r;
893 if (r == 0)
894 return -ENOTTY;
895
896 for (unsigned c = 0;; c++) {
897 if (ioctl(fd, BTRFS_IOC_QGROUP_CREATE, &args) < 0) {
898
899 /* On old kernels if quota is not enabled, we get EINVAL. On newer kernels we get
900 * ENOTCONN. Let's always convert this to ENOTCONN to make this recognizable
901 * everywhere the same way. */
902
903 if (IN_SET(errno, EINVAL, ENOTCONN))
904 return -ENOTCONN;
905
906 if (errno == EBUSY && c < 10) {
907 (void) btrfs_quota_scan_wait(fd);
908 continue;
909 }
910
911 return -errno;
912 }
913
914 break;
915 }
916
917 return 0;
918 }
919
920 int btrfs_qgroup_create(int fd, uint64_t qgroupid) {
921 return qgroup_create_or_destroy(fd, true, qgroupid);
922 }
923
924 int btrfs_qgroup_destroy(int fd, uint64_t qgroupid) {
925 return qgroup_create_or_destroy(fd, false, qgroupid);
926 }
927
928 int btrfs_qgroup_destroy_recursive(int fd, uint64_t qgroupid) {
929 _cleanup_free_ uint64_t *qgroups = NULL;
930 uint64_t subvol_id;
931 int n, r;
932
933 /* Destroys the specified qgroup, but unassigns it from all
934 * its parents first. Also, it recursively destroys all
935 * qgroups it is assigned to that have the same id part of the
936 * qgroupid as the specified group. */
937
938 r = btrfs_qgroupid_split(qgroupid, NULL, &subvol_id);
939 if (r < 0)
940 return r;
941
942 n = btrfs_qgroup_find_parents(fd, qgroupid, &qgroups);
943 if (n < 0)
944 return n;
945
946 for (int i = 0; i < n; i++) {
947 uint64_t id;
948
949 r = btrfs_qgroupid_split(qgroups[i], NULL, &id);
950 if (r < 0)
951 return r;
952
953 r = btrfs_qgroup_unassign(fd, qgroupid, qgroups[i]);
954 if (r < 0)
955 return r;
956
957 if (id != subvol_id)
958 continue;
959
960 /* The parent qgroupid shares the same id part with
961 * us? If so, destroy it too. */
962
963 (void) btrfs_qgroup_destroy_recursive(fd, qgroups[i]);
964 }
965
966 return btrfs_qgroup_destroy(fd, qgroupid);
967 }
968
969 int btrfs_quota_scan_start(int fd) {
970 struct btrfs_ioctl_quota_rescan_args args = {};
971
972 assert(fd >= 0);
973
974 return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_RESCAN, &args));
975 }
976
977 int btrfs_quota_scan_wait(int fd) {
978 assert(fd >= 0);
979
980 return RET_NERRNO(ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_WAIT));
981 }
982
983 int btrfs_quota_scan_ongoing(int fd) {
984 struct btrfs_ioctl_quota_rescan_args args = {};
985
986 assert(fd >= 0);
987
988 if (ioctl(fd, BTRFS_IOC_QUOTA_RESCAN_STATUS, &args) < 0)
989 return -errno;
990
991 return !!args.flags;
992 }
993
994 static int qgroup_assign_or_unassign(int fd, bool b, uint64_t child, uint64_t parent) {
995 struct btrfs_ioctl_qgroup_assign_args args = {
996 .assign = b,
997 .src = child,
998 .dst = parent,
999 };
1000 int r;
1001
1002 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1003 if (r < 0)
1004 return r;
1005 if (r == 0)
1006 return -ENOTTY;
1007
1008 for (unsigned c = 0;; c++) {
1009 r = ioctl(fd, BTRFS_IOC_QGROUP_ASSIGN, &args);
1010 if (r < 0) {
1011 if (errno == EBUSY && c < 10) {
1012 (void) btrfs_quota_scan_wait(fd);
1013 continue;
1014 }
1015
1016 return -errno;
1017 }
1018
1019 if (r == 0)
1020 return 0;
1021
1022 /* If the return value is > 0, we need to request a rescan */
1023
1024 (void) btrfs_quota_scan_start(fd);
1025 return 1;
1026 }
1027 }
1028
1029 int btrfs_qgroup_assign(int fd, uint64_t child, uint64_t parent) {
1030 return qgroup_assign_or_unassign(fd, true, child, parent);
1031 }
1032
1033 int btrfs_qgroup_unassign(int fd, uint64_t child, uint64_t parent) {
1034 return qgroup_assign_or_unassign(fd, false, child, parent);
1035 }
1036
1037 static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol_id, BtrfsRemoveFlags flags) {
1038 struct btrfs_ioctl_search_args args = {
1039 .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
1040
1041 .key.min_objectid = BTRFS_FIRST_FREE_OBJECTID,
1042 .key.max_objectid = BTRFS_LAST_FREE_OBJECTID,
1043
1044 .key.min_type = BTRFS_ROOT_BACKREF_KEY,
1045 .key.max_type = BTRFS_ROOT_BACKREF_KEY,
1046
1047 .key.min_transid = 0,
1048 .key.max_transid = UINT64_MAX,
1049 };
1050
1051 struct btrfs_ioctl_vol_args vol_args = {};
1052 _cleanup_close_ int subvol_fd = -EBADF;
1053 struct stat st;
1054 bool made_writable = false;
1055 int r;
1056
1057 assert(fd >= 0);
1058 assert(subvolume);
1059
1060 if (fstat(fd, &st) < 0)
1061 return -errno;
1062
1063 if (!S_ISDIR(st.st_mode))
1064 return -EINVAL;
1065
1066 subvol_fd = openat(fd, subvolume, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1067 if (subvol_fd < 0)
1068 return -errno;
1069
1070 /* Let's check if this is actually a subvolume. Note that this is mostly redundant, as BTRFS_IOC_SNAP_DESTROY
1071 * would fail anyway if it is not. However, it's a good thing to check this ahead of time so that we can return
1072 * ENOTTY unconditionally in this case. This is different from the ioctl() which will return EPERM/EACCES if we
1073 * don't have the privileges to remove subvolumes, regardless if the specified directory is actually a
1074 * subvolume or not. In order to make it easy for callers to cover the "this is not a btrfs subvolume" case
1075 * let's prefer ENOTTY over EPERM/EACCES though. */
1076 r = btrfs_is_subvol_fd(subvol_fd);
1077 if (r < 0)
1078 return r;
1079 if (r == 0) /* Not a btrfs subvolume */
1080 return -ENOTTY;
1081
1082 if (subvol_id == 0) {
1083 r = btrfs_subvol_get_id_fd(subvol_fd, &subvol_id);
1084 if (r < 0)
1085 return r;
1086 }
1087
1088 /* First, try to remove the subvolume. If it happens to be
1089 * already empty, this will just work. */
1090 strncpy(vol_args.name, subvolume, sizeof(vol_args.name)-1);
1091 if (ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &vol_args) >= 0) {
1092 (void) btrfs_qgroup_destroy_recursive(fd, subvol_id); /* for the leaf subvolumes, the qgroup id is identical to the subvol id */
1093 return 0;
1094 }
1095 if (!(flags & BTRFS_REMOVE_RECURSIVE) || errno != ENOTEMPTY)
1096 return -errno;
1097
1098 /* OK, the subvolume is not empty, let's look for child
1099 * subvolumes, and remove them, first */
1100
1101 args.key.min_offset = args.key.max_offset = subvol_id;
1102
1103 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1104 const struct btrfs_ioctl_search_header *sh;
1105 unsigned i;
1106
1107 args.key.nr_items = 256;
1108 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
1109 return -errno;
1110
1111 if (args.key.nr_items <= 0)
1112 break;
1113
1114 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1115 _cleanup_free_ char *p = NULL;
1116 const struct btrfs_root_ref *ref;
1117
1118 btrfs_ioctl_search_args_set(&args, sh);
1119
1120 if (sh->type != BTRFS_ROOT_BACKREF_KEY)
1121 continue;
1122 if (sh->offset != subvol_id)
1123 continue;
1124
1125 ref = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
1126
1127 p = strndup((char*) ref + sizeof(struct btrfs_root_ref), le64toh(ref->name_len));
1128 if (!p)
1129 return -ENOMEM;
1130
1131 struct btrfs_ioctl_ino_lookup_args ino_args = {
1132 .treeid = subvol_id,
1133 .objectid = htole64(ref->dirid),
1134 };
1135
1136 if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args) < 0)
1137 return -errno;
1138
1139 if (!made_writable) {
1140 r = btrfs_subvol_set_read_only_fd(subvol_fd, false);
1141 if (r < 0)
1142 return r;
1143
1144 made_writable = true;
1145 }
1146
1147 if (isempty(ino_args.name))
1148 /* Subvolume is in the top-level
1149 * directory of the subvolume. */
1150 r = subvol_remove_children(subvol_fd, p, sh->objectid, flags);
1151 else {
1152 _cleanup_close_ int child_fd = -EBADF;
1153
1154 /* Subvolume is somewhere further down,
1155 * hence we need to open the
1156 * containing directory first */
1157
1158 child_fd = openat(subvol_fd, ino_args.name, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1159 if (child_fd < 0)
1160 return -errno;
1161
1162 r = subvol_remove_children(child_fd, p, sh->objectid, flags);
1163 }
1164 if (r < 0)
1165 return r;
1166 }
1167
1168 /* Increase search key by one, to read the next item, if we can. */
1169 if (!btrfs_ioctl_search_args_inc(&args))
1170 break;
1171 }
1172
1173 /* OK, the child subvolumes should all be gone now, let's try
1174 * again to remove the subvolume */
1175 if (ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &vol_args) < 0)
1176 return -errno;
1177
1178 (void) btrfs_qgroup_destroy_recursive(fd, subvol_id);
1179 return 0;
1180 }
1181
1182 int btrfs_subvol_remove(const char *path, BtrfsRemoveFlags flags) {
1183 _cleanup_close_ int fd = -EBADF;
1184 const char *subvolume;
1185 int r;
1186
1187 assert(path);
1188
1189 r = extract_subvolume_name(path, &subvolume);
1190 if (r < 0)
1191 return r;
1192
1193 fd = open_parent(path, O_CLOEXEC, 0);
1194 if (fd < 0)
1195 return fd;
1196
1197 return subvol_remove_children(fd, subvolume, 0, flags);
1198 }
1199
1200 int btrfs_subvol_remove_fd(int fd, const char *subvolume, BtrfsRemoveFlags flags) {
1201 return subvol_remove_children(fd, subvolume, 0, flags);
1202 }
1203
1204 int btrfs_qgroup_copy_limits(int fd, uint64_t old_qgroupid, uint64_t new_qgroupid) {
1205
1206 struct btrfs_ioctl_search_args args = {
1207 /* Tree of quota items */
1208 .key.tree_id = BTRFS_QUOTA_TREE_OBJECTID,
1209
1210 /* The object ID is always 0 */
1211 .key.min_objectid = 0,
1212 .key.max_objectid = 0,
1213
1214 /* Look precisely for the quota items */
1215 .key.min_type = BTRFS_QGROUP_LIMIT_KEY,
1216 .key.max_type = BTRFS_QGROUP_LIMIT_KEY,
1217
1218 /* For our qgroup */
1219 .key.min_offset = old_qgroupid,
1220 .key.max_offset = old_qgroupid,
1221
1222 /* No restrictions on the other components */
1223 .key.min_transid = 0,
1224 .key.max_transid = UINT64_MAX,
1225 };
1226
1227 int r;
1228
1229 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1230 if (r < 0)
1231 return r;
1232 if (!r)
1233 return -ENOTTY;
1234
1235 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1236 const struct btrfs_ioctl_search_header *sh;
1237 unsigned i;
1238
1239 args.key.nr_items = 256;
1240 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
1241 if (errno == ENOENT) /* quota tree missing: quota is not enabled, hence nothing to copy */
1242 break;
1243
1244 return -errno;
1245 }
1246
1247 if (args.key.nr_items <= 0)
1248 break;
1249
1250 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1251 const struct btrfs_qgroup_limit_item *qli = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
1252 struct btrfs_ioctl_qgroup_limit_args qargs;
1253 unsigned c;
1254
1255 /* Make sure we start the next search at least from this entry */
1256 btrfs_ioctl_search_args_set(&args, sh);
1257
1258 if (sh->objectid != 0)
1259 continue;
1260 if (sh->type != BTRFS_QGROUP_LIMIT_KEY)
1261 continue;
1262 if (sh->offset != old_qgroupid)
1263 continue;
1264
1265 /* We found the entry, now copy things over. */
1266
1267 qargs = (struct btrfs_ioctl_qgroup_limit_args) {
1268 .qgroupid = new_qgroupid,
1269
1270 .lim.max_rfer = le64toh(qli->max_rfer),
1271 .lim.max_excl = le64toh(qli->max_excl),
1272 .lim.rsv_rfer = le64toh(qli->rsv_rfer),
1273 .lim.rsv_excl = le64toh(qli->rsv_excl),
1274
1275 .lim.flags = le64toh(qli->flags) & (BTRFS_QGROUP_LIMIT_MAX_RFER|
1276 BTRFS_QGROUP_LIMIT_MAX_EXCL|
1277 BTRFS_QGROUP_LIMIT_RSV_RFER|
1278 BTRFS_QGROUP_LIMIT_RSV_EXCL),
1279 };
1280
1281 for (c = 0;; c++) {
1282 if (ioctl(fd, BTRFS_IOC_QGROUP_LIMIT, &qargs) < 0) {
1283 if (errno == EBUSY && c < 10) {
1284 (void) btrfs_quota_scan_wait(fd);
1285 continue;
1286 }
1287 return -errno;
1288 }
1289
1290 break;
1291 }
1292
1293 return 1;
1294 }
1295
1296 /* Increase search key by one, to read the next item, if we can. */
1297 if (!btrfs_ioctl_search_args_inc(&args))
1298 break;
1299 }
1300
1301 return 0;
1302 }
1303
1304 static int copy_quota_hierarchy(int fd, uint64_t old_subvol_id, uint64_t new_subvol_id) {
1305 _cleanup_free_ uint64_t *old_qgroups = NULL, *old_parent_qgroups = NULL;
1306 bool copy_from_parent = false, insert_intermediary_qgroup = false;
1307 int n_old_qgroups, n_old_parent_qgroups, r;
1308 uint64_t old_parent_id;
1309
1310 assert(fd >= 0);
1311
1312 /* Copies a reduced form of quota information from the old to
1313 * the new subvolume. */
1314
1315 n_old_qgroups = btrfs_qgroup_find_parents(fd, old_subvol_id, &old_qgroups);
1316 if (n_old_qgroups <= 0) /* Nothing to copy */
1317 return n_old_qgroups;
1318
1319 r = btrfs_subvol_get_parent(fd, old_subvol_id, &old_parent_id);
1320 if (r == -ENXIO)
1321 /* We have no parent, hence nothing to copy. */
1322 n_old_parent_qgroups = 0;
1323 else if (r < 0)
1324 return r;
1325 else {
1326 n_old_parent_qgroups = btrfs_qgroup_find_parents(fd, old_parent_id, &old_parent_qgroups);
1327 if (n_old_parent_qgroups < 0)
1328 return n_old_parent_qgroups;
1329 }
1330
1331 for (int i = 0; i < n_old_qgroups; i++) {
1332 uint64_t id;
1333
1334 r = btrfs_qgroupid_split(old_qgroups[i], NULL, &id);
1335 if (r < 0)
1336 return r;
1337
1338 if (id == old_subvol_id) {
1339 /* The old subvolume was member of a qgroup
1340 * that had the same id, but a different level
1341 * as it self. Let's set up something similar
1342 * in the destination. */
1343 insert_intermediary_qgroup = true;
1344 break;
1345 }
1346
1347 for (int j = 0; j < n_old_parent_qgroups; j++)
1348 if (old_parent_qgroups[j] == old_qgroups[i])
1349 /* The old subvolume shared a common
1350 * parent qgroup with its parent
1351 * subvolume. Let's set up something
1352 * similar in the destination. */
1353 copy_from_parent = true;
1354 }
1355
1356 if (!insert_intermediary_qgroup && !copy_from_parent)
1357 return 0;
1358
1359 return btrfs_subvol_auto_qgroup_fd(fd, new_subvol_id, insert_intermediary_qgroup);
1360 }
1361
1362 static int copy_subtree_quota_limits(int fd, uint64_t old_subvol, uint64_t new_subvol) {
1363 uint64_t old_subtree_qgroup, new_subtree_qgroup;
1364 bool changed;
1365 int r;
1366
1367 /* First copy the leaf limits */
1368 r = btrfs_qgroup_copy_limits(fd, old_subvol, new_subvol);
1369 if (r < 0)
1370 return r;
1371 changed = r > 0;
1372
1373 /* Then, try to copy the subtree limits, if there are any. */
1374 r = btrfs_subvol_find_subtree_qgroup(fd, old_subvol, &old_subtree_qgroup);
1375 if (r < 0)
1376 return r;
1377 if (r == 0)
1378 return changed;
1379
1380 r = btrfs_subvol_find_subtree_qgroup(fd, new_subvol, &new_subtree_qgroup);
1381 if (r < 0)
1382 return r;
1383 if (r == 0)
1384 return changed;
1385
1386 r = btrfs_qgroup_copy_limits(fd, old_subtree_qgroup, new_subtree_qgroup);
1387 if (r != 0)
1388 return r;
1389
1390 return changed;
1391 }
1392
1393 static int subvol_snapshot_children(
1394 int old_fd,
1395 int new_fd,
1396 const char *subvolume,
1397 uint64_t old_subvol_id,
1398 BtrfsSnapshotFlags flags) {
1399
1400 struct btrfs_ioctl_search_args args = {
1401 .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
1402
1403 .key.min_objectid = BTRFS_FIRST_FREE_OBJECTID,
1404 .key.max_objectid = BTRFS_LAST_FREE_OBJECTID,
1405
1406 .key.min_type = BTRFS_ROOT_BACKREF_KEY,
1407 .key.max_type = BTRFS_ROOT_BACKREF_KEY,
1408
1409 .key.min_transid = 0,
1410 .key.max_transid = UINT64_MAX,
1411 };
1412
1413 struct btrfs_ioctl_vol_args_v2 vol_args = {
1414 .flags = flags & BTRFS_SNAPSHOT_READ_ONLY ? BTRFS_SUBVOL_RDONLY : 0,
1415 .fd = old_fd,
1416 };
1417 _cleanup_close_ int subvolume_fd = -EBADF;
1418 uint64_t new_subvol_id;
1419 int r;
1420
1421 assert(old_fd >= 0);
1422 assert(new_fd >= 0);
1423 assert(subvolume);
1424
1425 strncpy(vol_args.name, subvolume, sizeof(vol_args.name)-1);
1426
1427 if (ioctl(new_fd, BTRFS_IOC_SNAP_CREATE_V2, &vol_args) < 0)
1428 return -errno;
1429
1430 if (!(flags & BTRFS_SNAPSHOT_RECURSIVE) &&
1431 !(flags & BTRFS_SNAPSHOT_QUOTA))
1432 return 0;
1433
1434 if (old_subvol_id == 0) {
1435 r = btrfs_subvol_get_id_fd(old_fd, &old_subvol_id);
1436 if (r < 0)
1437 return r;
1438 }
1439
1440 r = btrfs_subvol_get_id(new_fd, vol_args.name, &new_subvol_id);
1441 if (r < 0)
1442 return r;
1443
1444 if (flags & BTRFS_SNAPSHOT_QUOTA)
1445 (void) copy_quota_hierarchy(new_fd, old_subvol_id, new_subvol_id);
1446
1447 if (!(flags & BTRFS_SNAPSHOT_RECURSIVE)) {
1448
1449 if (flags & BTRFS_SNAPSHOT_QUOTA)
1450 (void) copy_subtree_quota_limits(new_fd, old_subvol_id, new_subvol_id);
1451
1452 return 0;
1453 }
1454
1455 args.key.min_offset = args.key.max_offset = old_subvol_id;
1456
1457 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1458 const struct btrfs_ioctl_search_header *sh;
1459 unsigned i;
1460
1461 args.key.nr_items = 256;
1462 if (ioctl(old_fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
1463 return -errno;
1464
1465 if (args.key.nr_items <= 0)
1466 break;
1467
1468 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1469 _cleanup_free_ char *p = NULL, *c = NULL, *np = NULL;
1470 const struct btrfs_root_ref *ref;
1471 _cleanup_close_ int old_child_fd = -EBADF, new_child_fd = -EBADF;
1472
1473 btrfs_ioctl_search_args_set(&args, sh);
1474
1475 if (sh->type != BTRFS_ROOT_BACKREF_KEY)
1476 continue;
1477
1478 /* Avoid finding the source subvolume a second
1479 * time */
1480 if (sh->offset != old_subvol_id)
1481 continue;
1482
1483 /* Avoid running into loops if the new
1484 * subvolume is below the old one. */
1485 if (sh->objectid == new_subvol_id)
1486 continue;
1487
1488 ref = BTRFS_IOCTL_SEARCH_HEADER_BODY(sh);
1489 p = strndup((char*) ref + sizeof(struct btrfs_root_ref), le64toh(ref->name_len));
1490 if (!p)
1491 return -ENOMEM;
1492
1493 struct btrfs_ioctl_ino_lookup_args ino_args = {
1494 .treeid = old_subvol_id,
1495 .objectid = htole64(ref->dirid),
1496 };
1497
1498 if (ioctl(old_fd, BTRFS_IOC_INO_LOOKUP, &ino_args) < 0)
1499 return -errno;
1500
1501 c = path_join(ino_args.name, p);
1502 if (!c)
1503 return -ENOMEM;
1504
1505 old_child_fd = openat(old_fd, c, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1506 if (old_child_fd < 0)
1507 return -errno;
1508
1509 np = path_join(subvolume, ino_args.name);
1510 if (!np)
1511 return -ENOMEM;
1512
1513 new_child_fd = openat(new_fd, np, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1514 if (new_child_fd < 0)
1515 return -errno;
1516
1517 if (flags & BTRFS_SNAPSHOT_READ_ONLY) {
1518 /* If the snapshot is read-only we
1519 * need to mark it writable
1520 * temporarily, to put the subsnapshot
1521 * into place. */
1522
1523 if (subvolume_fd < 0) {
1524 subvolume_fd = openat(new_fd, subvolume, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY|O_NOFOLLOW);
1525 if (subvolume_fd < 0)
1526 return -errno;
1527 }
1528
1529 r = btrfs_subvol_set_read_only_fd(subvolume_fd, false);
1530 if (r < 0)
1531 return r;
1532 }
1533
1534 /* When btrfs clones the subvolumes, child
1535 * subvolumes appear as empty directories. Remove
1536 * them, so that we can create a new snapshot
1537 * in their place */
1538 if (unlinkat(new_child_fd, p, AT_REMOVEDIR) < 0) {
1539 int k = -errno;
1540
1541 if (flags & BTRFS_SNAPSHOT_READ_ONLY)
1542 (void) btrfs_subvol_set_read_only_fd(subvolume_fd, true);
1543
1544 return k;
1545 }
1546
1547 r = subvol_snapshot_children(old_child_fd, new_child_fd, p, sh->objectid, flags & ~BTRFS_SNAPSHOT_FALLBACK_COPY);
1548
1549 /* Restore the readonly flag */
1550 if (flags & BTRFS_SNAPSHOT_READ_ONLY) {
1551 int k;
1552
1553 k = btrfs_subvol_set_read_only_fd(subvolume_fd, true);
1554 if (r >= 0 && k < 0)
1555 return k;
1556 }
1557
1558 if (r < 0)
1559 return r;
1560 }
1561
1562 /* Increase search key by one, to read the next item, if we can. */
1563 if (!btrfs_ioctl_search_args_inc(&args))
1564 break;
1565 }
1566
1567 if (flags & BTRFS_SNAPSHOT_QUOTA)
1568 (void) copy_subtree_quota_limits(new_fd, old_subvol_id, new_subvol_id);
1569
1570 return 0;
1571 }
1572
1573 int btrfs_subvol_snapshot_fd_full(
1574 int old_fd,
1575 const char *new_path,
1576 BtrfsSnapshotFlags flags,
1577 copy_progress_path_t progress_path,
1578 copy_progress_bytes_t progress_bytes,
1579 void *userdata) {
1580
1581 _cleanup_close_ int new_fd = -EBADF;
1582 const char *subvolume;
1583 int r;
1584
1585 assert(old_fd >= 0);
1586 assert(new_path);
1587
1588 r = btrfs_is_subvol_fd(old_fd);
1589 if (r < 0)
1590 return r;
1591 if (r == 0) {
1592 bool plain_directory = false;
1593
1594 /* If the source isn't a proper subvolume, fail unless fallback is requested */
1595 if (!(flags & BTRFS_SNAPSHOT_FALLBACK_COPY))
1596 return -EISDIR;
1597
1598 r = btrfs_subvol_make(new_path);
1599 if (ERRNO_IS_NOT_SUPPORTED(r) && (flags & BTRFS_SNAPSHOT_FALLBACK_DIRECTORY)) {
1600 /* If the destination doesn't support subvolumes, then use a plain directory, if that's requested. */
1601 if (mkdir(new_path, 0755) < 0)
1602 return -errno;
1603
1604 plain_directory = true;
1605 } else if (r < 0)
1606 return r;
1607
1608 r = copy_directory_fd_full(
1609 old_fd, new_path,
1610 COPY_MERGE_EMPTY|
1611 COPY_REFLINK|
1612 COPY_SAME_MOUNT|
1613 COPY_HARDLINKS|
1614 COPY_ALL_XATTRS|
1615 (FLAGS_SET(flags, BTRFS_SNAPSHOT_SIGINT) ? COPY_SIGINT : 0)|
1616 (FLAGS_SET(flags, BTRFS_SNAPSHOT_SIGTERM) ? COPY_SIGTERM : 0),
1617 progress_path,
1618 progress_bytes,
1619 userdata);
1620 if (r < 0)
1621 goto fallback_fail;
1622
1623 if (flags & BTRFS_SNAPSHOT_READ_ONLY) {
1624
1625 if (plain_directory) {
1626 /* Plain directories have no recursive read-only flag, but something pretty close to
1627 * it: the IMMUTABLE bit. Let's use this here, if this is requested. */
1628
1629 if (flags & BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE)
1630 (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL, NULL);
1631 } else {
1632 r = btrfs_subvol_set_read_only(new_path, true);
1633 if (r < 0)
1634 goto fallback_fail;
1635 }
1636 }
1637
1638 return 0;
1639
1640 fallback_fail:
1641 (void) rm_rf(new_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
1642 return r;
1643 }
1644
1645 r = extract_subvolume_name(new_path, &subvolume);
1646 if (r < 0)
1647 return r;
1648
1649 new_fd = open_parent(new_path, O_CLOEXEC, 0);
1650 if (new_fd < 0)
1651 return new_fd;
1652
1653 return subvol_snapshot_children(old_fd, new_fd, subvolume, 0, flags);
1654 }
1655
1656 int btrfs_subvol_snapshot_full(
1657 const char *old_path,
1658 const char *new_path,
1659 BtrfsSnapshotFlags flags,
1660 copy_progress_path_t progress_path,
1661 copy_progress_bytes_t progress_bytes,
1662 void *userdata) {
1663
1664 _cleanup_close_ int old_fd = -EBADF;
1665
1666 assert(old_path);
1667 assert(new_path);
1668
1669 old_fd = open(old_path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1670 if (old_fd < 0)
1671 return -errno;
1672
1673 return btrfs_subvol_snapshot_fd_full(old_fd, new_path, flags, progress_path, progress_bytes, userdata);
1674 }
1675
1676 int btrfs_qgroup_find_parents(int fd, uint64_t qgroupid, uint64_t **ret) {
1677
1678 struct btrfs_ioctl_search_args args = {
1679 /* Tree of quota items */
1680 .key.tree_id = BTRFS_QUOTA_TREE_OBJECTID,
1681
1682 /* Look precisely for the quota relation items */
1683 .key.min_type = BTRFS_QGROUP_RELATION_KEY,
1684 .key.max_type = BTRFS_QGROUP_RELATION_KEY,
1685
1686 /* No restrictions on the other components */
1687 .key.min_offset = 0,
1688 .key.max_offset = UINT64_MAX,
1689
1690 .key.min_transid = 0,
1691 .key.max_transid = UINT64_MAX,
1692 };
1693
1694 _cleanup_free_ uint64_t *items = NULL;
1695 size_t n_items = 0;
1696 int r;
1697
1698 assert(fd >= 0);
1699 assert(ret);
1700
1701 if (qgroupid == 0) {
1702 r = btrfs_subvol_get_id_fd(fd, &qgroupid);
1703 if (r < 0)
1704 return r;
1705 } else {
1706 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1707 if (r < 0)
1708 return r;
1709 if (!r)
1710 return -ENOTTY;
1711 }
1712
1713 args.key.min_objectid = args.key.max_objectid = qgroupid;
1714
1715 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1716 const struct btrfs_ioctl_search_header *sh;
1717 unsigned i;
1718
1719 args.key.nr_items = 256;
1720 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0) {
1721 if (errno == ENOENT) /* quota tree missing: quota is disabled */
1722 break;
1723
1724 return -errno;
1725 }
1726
1727 if (args.key.nr_items <= 0)
1728 break;
1729
1730 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1731
1732 /* Make sure we start the next search at least from this entry */
1733 btrfs_ioctl_search_args_set(&args, sh);
1734
1735 if (sh->type != BTRFS_QGROUP_RELATION_KEY)
1736 continue;
1737 if (sh->offset < sh->objectid)
1738 continue;
1739 if (sh->objectid != qgroupid)
1740 continue;
1741
1742 if (!GREEDY_REALLOC(items, n_items+1))
1743 return -ENOMEM;
1744
1745 items[n_items++] = sh->offset;
1746 }
1747
1748 /* Increase search key by one, to read the next item, if we can. */
1749 if (!btrfs_ioctl_search_args_inc(&args))
1750 break;
1751 }
1752
1753 if (n_items <= 0) {
1754 *ret = NULL;
1755 return 0;
1756 }
1757
1758 *ret = TAKE_PTR(items);
1759
1760 return (int) n_items;
1761 }
1762
1763 int btrfs_subvol_auto_qgroup_fd(int fd, uint64_t subvol_id, bool insert_intermediary_qgroup) {
1764 _cleanup_free_ uint64_t *qgroups = NULL;
1765 _cleanup_close_ int real_fd = -EBADF;
1766 uint64_t parent_subvol;
1767 bool changed = false;
1768 int n = 0, r;
1769
1770 assert(fd >= 0);
1771
1772 /*
1773 * Sets up the specified subvolume's qgroup automatically in
1774 * one of two ways:
1775 *
1776 * If insert_intermediary_qgroup is false, the subvolume's
1777 * leaf qgroup will be assigned to the same parent qgroups as
1778 * the subvolume's parent subvolume.
1779 *
1780 * If insert_intermediary_qgroup is true a new intermediary
1781 * higher-level qgroup is created, with a higher level number,
1782 * but reusing the id of the subvolume. The level number is
1783 * picked as one smaller than the lowest level qgroup the
1784 * parent subvolume is a member of. If the parent subvolume's
1785 * leaf qgroup is assigned to no higher-level qgroup a new
1786 * qgroup of level 255 is created instead. Either way, the new
1787 * qgroup is then assigned to the parent's higher-level
1788 * qgroup, and the subvolume itself is assigned to it.
1789 *
1790 * If the subvolume is already assigned to a higher level
1791 * qgroup, no operation is executed.
1792 *
1793 * Effectively this means: regardless if
1794 * insert_intermediary_qgroup is true or not, after this
1795 * function is invoked the subvolume will be accounted within
1796 * the same qgroups as the parent. However, if it is true, it
1797 * will also get its own higher-level qgroup, which may in
1798 * turn be used by subvolumes created beneath this subvolume
1799 * later on.
1800 *
1801 * This hence defines a simple default qgroup setup for
1802 * subvolumes, as long as this function is invoked on each
1803 * created subvolume: each subvolume is always accounting
1804 * together with its immediate parents. Optionally, if
1805 * insert_intermediary_qgroup is true, it will also get a
1806 * qgroup that then includes all its own child subvolumes.
1807 */
1808
1809 /* Turn this into a proper fd, if it is currently O_PATH */
1810 fd = fd_reopen_condition(fd, O_RDONLY|O_CLOEXEC, O_PATH, &real_fd);
1811 if (fd < 0)
1812 return fd;
1813
1814 if (subvol_id == 0) {
1815 r = btrfs_is_subvol_fd(fd);
1816 if (r < 0)
1817 return r;
1818 if (!r)
1819 return -ENOTTY;
1820
1821 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
1822 if (r < 0)
1823 return r;
1824 }
1825
1826 n = btrfs_qgroup_find_parents(fd, subvol_id, &qgroups);
1827 if (n < 0)
1828 return n;
1829 if (n > 0) /* already parent qgroups set up, let's bail */
1830 return 0;
1831
1832 qgroups = mfree(qgroups);
1833
1834 r = btrfs_subvol_get_parent(fd, subvol_id, &parent_subvol);
1835 if (r == -ENXIO)
1836 /* No parent, hence no qgroup memberships */
1837 n = 0;
1838 else if (r < 0)
1839 return r;
1840 else {
1841 n = btrfs_qgroup_find_parents(fd, parent_subvol, &qgroups);
1842 if (n < 0)
1843 return n;
1844 }
1845
1846 if (insert_intermediary_qgroup) {
1847 uint64_t lowest = 256, new_qgroupid;
1848 bool created = false;
1849
1850 /* Determine the lowest qgroup that the parent
1851 * subvolume is assigned to. */
1852
1853 for (int i = 0; i < n; i++) {
1854 uint64_t level;
1855
1856 r = btrfs_qgroupid_split(qgroups[i], &level, NULL);
1857 if (r < 0)
1858 return r;
1859
1860 if (level < lowest)
1861 lowest = level;
1862 }
1863
1864 if (lowest <= 1) /* There are no levels left we could use insert an intermediary qgroup at */
1865 return -EBUSY;
1866
1867 r = btrfs_qgroupid_make(lowest - 1, subvol_id, &new_qgroupid);
1868 if (r < 0)
1869 return r;
1870
1871 /* Create the new intermediary group, unless it already exists */
1872 r = btrfs_qgroup_create(fd, new_qgroupid);
1873 if (r < 0 && r != -EEXIST)
1874 return r;
1875 if (r >= 0)
1876 changed = created = true;
1877
1878 for (int i = 0; i < n; i++) {
1879 r = btrfs_qgroup_assign(fd, new_qgroupid, qgroups[i]);
1880 if (r < 0 && r != -EEXIST) {
1881 if (created)
1882 (void) btrfs_qgroup_destroy_recursive(fd, new_qgroupid);
1883
1884 return r;
1885 }
1886 if (r >= 0)
1887 changed = true;
1888 }
1889
1890 r = btrfs_qgroup_assign(fd, subvol_id, new_qgroupid);
1891 if (r < 0 && r != -EEXIST) {
1892 if (created)
1893 (void) btrfs_qgroup_destroy_recursive(fd, new_qgroupid);
1894 return r;
1895 }
1896 if (r >= 0)
1897 changed = true;
1898
1899 } else {
1900 int i;
1901
1902 /* Assign our subvolume to all the same qgroups as the parent */
1903
1904 for (i = 0; i < n; i++) {
1905 r = btrfs_qgroup_assign(fd, subvol_id, qgroups[i]);
1906 if (r < 0 && r != -EEXIST)
1907 return r;
1908 if (r >= 0)
1909 changed = true;
1910 }
1911 }
1912
1913 return changed;
1914 }
1915
1916 int btrfs_subvol_auto_qgroup(const char *path, uint64_t subvol_id, bool create_intermediary_qgroup) {
1917 _cleanup_close_ int fd = -EBADF;
1918
1919 fd = open(path, O_RDONLY|O_NOCTTY|O_CLOEXEC|O_DIRECTORY);
1920 if (fd < 0)
1921 return -errno;
1922
1923 return btrfs_subvol_auto_qgroup_fd(fd, subvol_id, create_intermediary_qgroup);
1924 }
1925
1926 int btrfs_subvol_get_parent(int fd, uint64_t subvol_id, uint64_t *ret) {
1927
1928 struct btrfs_ioctl_search_args args = {
1929 /* Tree of tree roots */
1930 .key.tree_id = BTRFS_ROOT_TREE_OBJECTID,
1931
1932 /* Look precisely for the subvolume items */
1933 .key.min_type = BTRFS_ROOT_BACKREF_KEY,
1934 .key.max_type = BTRFS_ROOT_BACKREF_KEY,
1935
1936 /* No restrictions on the other components */
1937 .key.min_offset = 0,
1938 .key.max_offset = UINT64_MAX,
1939
1940 .key.min_transid = 0,
1941 .key.max_transid = UINT64_MAX,
1942 };
1943 int r;
1944
1945 assert(fd >= 0);
1946 assert(ret);
1947
1948 if (subvol_id == 0) {
1949 r = btrfs_subvol_get_id_fd(fd, &subvol_id);
1950 if (r < 0)
1951 return r;
1952 } else {
1953 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
1954 if (r < 0)
1955 return r;
1956 if (!r)
1957 return -ENOTTY;
1958 }
1959
1960 args.key.min_objectid = args.key.max_objectid = subvol_id;
1961
1962 while (btrfs_ioctl_search_args_compare(&args) <= 0) {
1963 const struct btrfs_ioctl_search_header *sh;
1964 unsigned i;
1965
1966 args.key.nr_items = 256;
1967 if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
1968 return negative_errno();
1969
1970 if (args.key.nr_items <= 0)
1971 break;
1972
1973 FOREACH_BTRFS_IOCTL_SEARCH_HEADER(i, sh, args) {
1974
1975 if (sh->type != BTRFS_ROOT_BACKREF_KEY)
1976 continue;
1977 if (sh->objectid != subvol_id)
1978 continue;
1979
1980 *ret = sh->offset;
1981 return 0;
1982 }
1983 }
1984
1985 return -ENXIO;
1986 }