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