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