]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <daniel.lezcano at free.fr> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | */ | |
23 | ||
24 | #define _GNU_SOURCE | |
25 | #include <errno.h> | |
26 | #include <fcntl.h> | |
27 | #include <grp.h> | |
28 | #include <libgen.h> | |
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | #include <unistd.h> | |
33 | #include <sys/ioctl.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/wait.h> | |
36 | #include <sys/types.h> | |
37 | #include <sys/vfs.h> | |
38 | ||
39 | #include "log.h" | |
40 | #include "btrfs.h" | |
41 | #include "rsync.h" | |
42 | #include "storage.h" | |
43 | #include "utils.h" | |
44 | ||
45 | #ifndef HAVE_STRLCPY | |
46 | #include "include/strlcpy.h" | |
47 | #endif | |
48 | ||
49 | #ifndef HAVE_STRLCAT | |
50 | #include "include/strlcat.h" | |
51 | #endif | |
52 | ||
53 | lxc_log_define(btrfs, lxc); | |
54 | ||
55 | /* | |
56 | * Return the full path of objid under dirid. Let's say dirid is | |
57 | * /lxc/c1/rootfs, and objid is /lxc/c1/rootfs/a/b/c. Then we will | |
58 | * return a/b/c. If instead objid is for /lxc/c1/rootfs/a, we will | |
59 | * simply return a. | |
60 | */ | |
61 | char *get_btrfs_subvol_path(int fd, u64 dir_id, u64 objid, char *name, | |
62 | int name_len) | |
63 | { | |
64 | struct btrfs_ioctl_ino_lookup_args args; | |
65 | int ret, e; | |
66 | size_t len, retlen; | |
67 | char *retpath; | |
68 | ||
69 | memset(&args, 0, sizeof(args)); | |
70 | args.treeid = dir_id; | |
71 | args.objectid = objid; | |
72 | ||
73 | ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); | |
74 | e = errno; | |
75 | if (ret) { | |
76 | ERROR("Failed to lookup path for %llu %llu %s - %s\n", | |
77 | (unsigned long long) dir_id, | |
78 | (unsigned long long) objid, | |
79 | name, strerror(e)); | |
80 | return NULL; | |
81 | } else | |
82 | INFO("Got path for %llu %llu - %s\n", | |
83 | (unsigned long long) objid, (unsigned long long) dir_id, | |
84 | name); | |
85 | ||
86 | if (args.name[0]) { | |
87 | /* | |
88 | * we're in a subdirectory of ref_tree, the kernel ioctl | |
89 | * puts a / in there for us | |
90 | */ | |
91 | len = strlen(args.name) + name_len + 2; | |
92 | retpath = malloc(len); | |
93 | if (!retpath) | |
94 | return NULL; | |
95 | ||
96 | (void)strlcpy(retpath, args.name, len); | |
97 | (void)strlcat(retpath, "/", len); | |
98 | ||
99 | retlen = strlcat(retpath, name, len); | |
100 | if (retlen >= len) { | |
101 | ERROR("Failed to append name - %s\n", name); | |
102 | free(retpath); | |
103 | return NULL; | |
104 | } | |
105 | } else { | |
106 | /* we're at the root of ref_tree */ | |
107 | len = name_len + 1; | |
108 | retpath = malloc(len); | |
109 | if (!retpath) | |
110 | return NULL; | |
111 | ||
112 | *retpath = '\0'; | |
113 | ||
114 | retlen = strlcat(retpath, name, len); | |
115 | if (retlen >= len) { | |
116 | ERROR("Failed to append name - %s\n", name); | |
117 | free(retpath); | |
118 | return NULL; | |
119 | } | |
120 | } | |
121 | ||
122 | return retpath; | |
123 | } | |
124 | ||
125 | int btrfs_list_get_path_rootid(int fd, u64 *treeid) | |
126 | { | |
127 | int ret; | |
128 | struct btrfs_ioctl_ino_lookup_args args; | |
129 | ||
130 | memset(&args, 0, sizeof(args)); | |
131 | args.objectid = BTRFS_FIRST_FREE_OBJECTID; | |
132 | ||
133 | ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); | |
134 | if (ret < 0) { | |
135 | SYSWARN("Warning: can't perform the search"); | |
136 | return ret; | |
137 | } | |
138 | *treeid = args.treeid; | |
139 | return 0; | |
140 | } | |
141 | ||
142 | bool is_btrfs_fs(const char *path) | |
143 | { | |
144 | int fd, ret; | |
145 | struct btrfs_ioctl_space_args sargs; | |
146 | ||
147 | /* Make sure this is a btrfs filesystem. */ | |
148 | fd = open(path, O_RDONLY); | |
149 | if (fd < 0) | |
150 | return false; | |
151 | sargs.space_slots = 0; | |
152 | sargs.total_spaces = 0; | |
153 | ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, &sargs); | |
154 | close(fd); | |
155 | if (ret < 0) | |
156 | return false; | |
157 | ||
158 | return true; | |
159 | } | |
160 | ||
161 | /* | |
162 | * Taken from btrfs toolsuite. Test if path is a subvolume. | |
163 | * return 0; path exists but it is not a subvolume | |
164 | * return 1; path exists and it is a subvolume | |
165 | * return < 0; error | |
166 | */ | |
167 | int is_btrfs_subvol(const char *path) | |
168 | { | |
169 | struct stat st; | |
170 | struct statfs stfs; | |
171 | int ret; | |
172 | ||
173 | ret = stat(path, &st); | |
174 | if (ret < 0) | |
175 | return -errno; | |
176 | ||
177 | if (st.st_ino != BTRFS_FIRST_FREE_OBJECTID || !S_ISDIR(st.st_mode)) | |
178 | return 0; | |
179 | ||
180 | ret = statfs(path, &stfs); | |
181 | if (ret < 0) | |
182 | return -errno; | |
183 | ||
184 | return stfs.f_type == BTRFS_SUPER_MAGIC; | |
185 | } | |
186 | ||
187 | bool btrfs_detect(const char *path) | |
188 | { | |
189 | struct stat st; | |
190 | int ret; | |
191 | ||
192 | if (!strncmp(path, "btrfs:", 6)) | |
193 | return true; | |
194 | ||
195 | if (!is_btrfs_fs(path)) | |
196 | return false; | |
197 | ||
198 | /* make sure it's a subvolume */ | |
199 | ret = stat(path, &st); | |
200 | if (ret < 0) | |
201 | return false; | |
202 | ||
203 | if (st.st_ino == 256 && S_ISDIR(st.st_mode)) | |
204 | return true; | |
205 | ||
206 | return false; | |
207 | } | |
208 | ||
209 | int btrfs_mount(struct lxc_storage *bdev) | |
210 | { | |
211 | unsigned long mntflags; | |
212 | char *mntdata; | |
213 | const char *src; | |
214 | int ret; | |
215 | ||
216 | if (strcmp(bdev->type, "btrfs")) | |
217 | return -22; | |
218 | ||
219 | if (!bdev->src || !bdev->dest) | |
220 | return -22; | |
221 | ||
222 | if (parse_mntopts(bdev->mntopts, &mntflags, &mntdata) < 0) { | |
223 | free(mntdata); | |
224 | return -22; | |
225 | } | |
226 | ||
227 | src = lxc_storage_get_path(bdev->src, "btrfs"); | |
228 | ||
229 | ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags, mntdata); | |
230 | free(mntdata); | |
231 | return ret; | |
232 | } | |
233 | ||
234 | int btrfs_umount(struct lxc_storage *bdev) | |
235 | { | |
236 | if (strcmp(bdev->type, "btrfs")) | |
237 | return -22; | |
238 | ||
239 | if (!bdev->src || !bdev->dest) | |
240 | return -22; | |
241 | ||
242 | return umount(bdev->dest); | |
243 | } | |
244 | ||
245 | static int btrfs_subvolume_create(const char *path) | |
246 | { | |
247 | int ret, saved_errno; | |
248 | size_t retlen; | |
249 | struct btrfs_ioctl_vol_args args; | |
250 | char *p, *newfull; | |
251 | int fd = -1; | |
252 | ||
253 | newfull = strdup(path); | |
254 | if (!newfull) { | |
255 | errno = ENOMEM; | |
256 | return -ENOMEM; | |
257 | } | |
258 | ||
259 | p = strrchr(newfull, '/'); | |
260 | if (!p) { | |
261 | free(newfull); | |
262 | errno = EINVAL; | |
263 | return -EINVAL; | |
264 | } | |
265 | *p = '\0'; | |
266 | ||
267 | fd = open(newfull, O_RDONLY); | |
268 | if (fd < 0) { | |
269 | free(newfull); | |
270 | return -errno; | |
271 | } | |
272 | ||
273 | memset(&args, 0, sizeof(args)); | |
274 | retlen = strlcpy(args.name, p + 1, BTRFS_SUBVOL_NAME_MAX); | |
275 | if (retlen >= BTRFS_SUBVOL_NAME_MAX) { | |
276 | free(newfull); | |
277 | close(fd); | |
278 | return -E2BIG; | |
279 | } | |
280 | ||
281 | ret = ioctl(fd, BTRFS_IOC_SUBVOL_CREATE, &args); | |
282 | saved_errno = errno; | |
283 | ||
284 | close(fd); | |
285 | free(newfull); | |
286 | errno = saved_errno; | |
287 | return ret; | |
288 | } | |
289 | ||
290 | int btrfs_same_fs(const char *orig, const char *new) | |
291 | { | |
292 | int fd_orig = -1, fd_new = -1, ret = -1; | |
293 | struct btrfs_ioctl_fs_info_args orig_args, new_args; | |
294 | ||
295 | fd_orig = open(orig, O_RDONLY); | |
296 | if (fd_orig < 0) { | |
297 | SYSERROR("Error opening original rootfs %s", orig); | |
298 | goto out; | |
299 | } | |
300 | ret = ioctl(fd_orig, BTRFS_IOC_FS_INFO, &orig_args); | |
301 | if (ret < 0) { | |
302 | SYSERROR("BTRFS_IOC_FS_INFO %s", orig); | |
303 | goto out; | |
304 | } | |
305 | ||
306 | fd_new = open(new, O_RDONLY); | |
307 | if (fd_new < 0) { | |
308 | SYSERROR("Error opening new container dir %s", new); | |
309 | ret = -1; | |
310 | goto out; | |
311 | } | |
312 | ret = ioctl(fd_new, BTRFS_IOC_FS_INFO, &new_args); | |
313 | if (ret < 0) { | |
314 | SYSERROR("BTRFS_IOC_FS_INFO %s", new); | |
315 | goto out; | |
316 | } | |
317 | ||
318 | if (strncmp(orig_args.fsid, new_args.fsid, BTRFS_FSID_SIZE) != 0) { | |
319 | ret = -1; | |
320 | goto out; | |
321 | } | |
322 | ret = 0; | |
323 | out: | |
324 | if (fd_new != -1) | |
325 | close(fd_new); | |
326 | if (fd_orig != -1) | |
327 | close(fd_orig); | |
328 | return ret; | |
329 | } | |
330 | ||
331 | int btrfs_snapshot(const char *orig, const char *new) | |
332 | { | |
333 | size_t retlen; | |
334 | struct btrfs_ioctl_vol_args_v2 args; | |
335 | char *newdir, *newname; | |
336 | char *newfull = NULL; | |
337 | int saved_errno = -1; | |
338 | int fd = -1, fddst = -1, ret = -1; | |
339 | ||
340 | newfull = strdup(new); | |
341 | if (!newfull) | |
342 | goto out; | |
343 | ||
344 | ret = rmdir(newfull); | |
345 | if (ret < 0 && errno != ENOENT) | |
346 | goto out; | |
347 | ||
348 | newname = basename(newfull); | |
349 | fd = open(orig, O_RDONLY); | |
350 | if (fd < 0) | |
351 | goto out; | |
352 | ||
353 | newdir = dirname(newfull); | |
354 | fddst = open(newdir, O_RDONLY); | |
355 | if (fddst < 0) | |
356 | goto out; | |
357 | ||
358 | memset(&args, 0, sizeof(args)); | |
359 | retlen = strlcpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX); | |
360 | if (retlen >= BTRFS_SUBVOL_NAME_MAX) | |
361 | goto out; | |
362 | ||
363 | ret = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args); | |
364 | saved_errno = errno; | |
365 | ||
366 | out: | |
367 | if (fddst != -1) | |
368 | close(fddst); | |
369 | if (fd != -1) | |
370 | close(fd); | |
371 | free(newfull); | |
372 | ||
373 | if (saved_errno >= 0) | |
374 | errno = saved_errno; | |
375 | return ret; | |
376 | } | |
377 | ||
378 | int btrfs_snapshot_wrapper(void *data) | |
379 | { | |
380 | const char *src; | |
381 | struct rsync_data_char *arg = data; | |
382 | ||
383 | if (setgid(0) < 0) { | |
384 | ERROR("Failed to setgid to 0"); | |
385 | return -1; | |
386 | } | |
387 | if (setgroups(0, NULL) < 0) | |
388 | WARN("Failed to clear groups"); | |
389 | ||
390 | if (setuid(0) < 0) { | |
391 | ERROR("Failed to setuid to 0"); | |
392 | return -1; | |
393 | } | |
394 | ||
395 | src = lxc_storage_get_path(arg->src, "btrfs"); | |
396 | return btrfs_snapshot(src, arg->dest); | |
397 | } | |
398 | ||
399 | int btrfs_clonepaths(struct lxc_storage *orig, struct lxc_storage *new, | |
400 | const char *oldname, const char *cname, | |
401 | const char *oldpath, const char *lxcpath, int snap, | |
402 | uint64_t newsize, struct lxc_conf *conf) | |
403 | { | |
404 | const char *src; | |
405 | ||
406 | if (!orig->dest || !orig->src) | |
407 | return -1; | |
408 | ||
409 | if (strcmp(orig->type, "btrfs") && snap) { | |
410 | ERROR("btrfs snapshot from %s backing store is not supported", | |
411 | orig->type); | |
412 | return -1; | |
413 | } | |
414 | ||
415 | new->src = lxc_string_join( | |
416 | "/", | |
417 | (const char *[]){"btrfs:", *lxcpath != '/' ? lxcpath : ++lxcpath, | |
418 | cname, "rootfs", NULL}, | |
419 | false); | |
420 | if (!new->src) { | |
421 | ERROR("Failed to create new rootfs path"); | |
422 | return -1; | |
423 | } | |
424 | TRACE("Constructed new rootfs path \"%s\"", new->src); | |
425 | ||
426 | src = lxc_storage_get_path(new->src, "btrfs"); | |
427 | new->dest = strdup(src); | |
428 | if (!new->dest) { | |
429 | ERROR("Failed to duplicate string \"%s\"", src); | |
430 | return -1; | |
431 | } | |
432 | ||
433 | if (orig->mntopts) { | |
434 | new->mntopts = strdup(orig->mntopts); | |
435 | if (!new->mntopts) { | |
436 | ERROR("Failed to duplicate string \"%s\"", | |
437 | orig->mntopts); | |
438 | return -1; | |
439 | } | |
440 | } | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
445 | bool btrfs_create_clone(struct lxc_conf *conf, struct lxc_storage *orig, | |
446 | struct lxc_storage *new, uint64_t newsize) | |
447 | { | |
448 | int ret; | |
449 | struct rsync_data data = {0, 0}; | |
450 | char cmd_output[MAXPATHLEN] = {0}; | |
451 | ||
452 | ret = rmdir(new->dest); | |
453 | if (ret < 0 && errno != ENOENT) | |
454 | return false; | |
455 | ||
456 | ret = btrfs_subvolume_create(new->dest); | |
457 | if (ret < 0) { | |
458 | SYSERROR("Failed to create btrfs subvolume \"%s\"", new->dest); | |
459 | return false; | |
460 | } | |
461 | ||
462 | /* rsync the contents from source to target */ | |
463 | data.orig = orig; | |
464 | data.new = new; | |
465 | if (am_guest_unpriv()) { | |
466 | ret = userns_exec_full(conf, lxc_storage_rsync_exec_wrapper, | |
467 | &data, "lxc_storage_rsync_exec_wrapper"); | |
468 | if (ret < 0) { | |
469 | ERROR("Failed to rsync from \"%s\" into \"%s\"", | |
470 | orig->dest, new->dest); | |
471 | return false; | |
472 | } | |
473 | ||
474 | return true; | |
475 | } | |
476 | ||
477 | ret = run_command(cmd_output, sizeof(cmd_output), | |
478 | lxc_storage_rsync_exec_wrapper, (void *)&data); | |
479 | if (ret < 0) { | |
480 | ERROR("Failed to rsync from \"%s\" into \"%s\": %s", orig->dest, | |
481 | new->dest, cmd_output); | |
482 | return false; | |
483 | } | |
484 | ||
485 | return true; | |
486 | } | |
487 | ||
488 | bool btrfs_create_snapshot(struct lxc_conf *conf, struct lxc_storage *orig, | |
489 | struct lxc_storage *new, uint64_t newsize) | |
490 | { | |
491 | int ret; | |
492 | ||
493 | ret = rmdir(new->dest); | |
494 | if (ret < 0 && errno != ENOENT) | |
495 | return false; | |
496 | ||
497 | if (am_guest_unpriv()) { | |
498 | struct rsync_data_char args; | |
499 | ||
500 | args.src = orig->src; | |
501 | args.dest = new->dest; | |
502 | ||
503 | ret = userns_exec_1(conf, btrfs_snapshot_wrapper, &args, | |
504 | "btrfs_snapshot_wrapper"); | |
505 | if (ret < 0) { | |
506 | ERROR("Failed to run \"btrfs_snapshot_wrapper\""); | |
507 | return false; | |
508 | } | |
509 | ||
510 | TRACE("Created btrfs snapshot \"%s\" from \"%s\"", new->dest, | |
511 | orig->dest); | |
512 | return true; | |
513 | } | |
514 | ||
515 | ret = btrfs_snapshot(orig->src, new->dest); | |
516 | if (ret < 0) { | |
517 | SYSERROR("Failed to create btrfs snapshot \"%s\" from \"%s\"", | |
518 | new->dest, orig->dest); | |
519 | return false; | |
520 | } | |
521 | ||
522 | TRACE("Created btrfs snapshot \"%s\" from \"%s\"", new->dest, orig->dest); | |
523 | return true; | |
524 | } | |
525 | ||
526 | static int btrfs_do_destroy_subvol(const char *path) | |
527 | { | |
528 | int ret, fd = -1; | |
529 | size_t retlen; | |
530 | struct btrfs_ioctl_vol_args args; | |
531 | char *p, *newfull = strdup(path); | |
532 | ||
533 | if (!newfull) { | |
534 | ERROR("Error: out of memory"); | |
535 | return -1; | |
536 | } | |
537 | ||
538 | p = strrchr(newfull, '/'); | |
539 | if (!p) { | |
540 | ERROR("bad path: %s", path); | |
541 | free(newfull); | |
542 | return -1; | |
543 | } | |
544 | *p = '\0'; | |
545 | ||
546 | fd = open(newfull, O_RDONLY); | |
547 | if (fd < 0) { | |
548 | SYSERROR("Error opening %s", newfull); | |
549 | free(newfull); | |
550 | return -1; | |
551 | } | |
552 | ||
553 | memset(&args, 0, sizeof(args)); | |
554 | retlen = strlcpy(args.name, p+1, BTRFS_SUBVOL_NAME_MAX); | |
555 | if (retlen >= BTRFS_SUBVOL_NAME_MAX) { | |
556 | free(newfull); | |
557 | close(fd); | |
558 | return -E2BIG; | |
559 | } | |
560 | ||
561 | ret = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args); | |
562 | INFO("btrfs: snapshot destroy ioctl returned %d for %s", ret, path); | |
563 | if (ret < 0 && errno == EPERM) | |
564 | ERROR("Is the rootfs mounted with -o user_subvol_rm_allowed?"); | |
565 | ||
566 | free(newfull); | |
567 | close(fd); | |
568 | return ret; | |
569 | } | |
570 | ||
571 | static int get_btrfs_tree_idx(struct my_btrfs_tree *tree, u64 id) | |
572 | { | |
573 | int i; | |
574 | if (!tree) | |
575 | return -1; | |
576 | for (i = 0; i < tree->num; i++) { | |
577 | if (tree->nodes[i].objid == id) | |
578 | return i; | |
579 | } | |
580 | return -1; | |
581 | } | |
582 | ||
583 | static struct my_btrfs_tree *create_my_btrfs_tree(u64 id, const char *path, | |
584 | int name_len) | |
585 | { | |
586 | struct my_btrfs_tree *tree; | |
587 | ||
588 | tree = malloc(sizeof(struct my_btrfs_tree)); | |
589 | if (!tree) | |
590 | return NULL; | |
591 | tree->nodes = malloc(sizeof(struct mytree_node)); | |
592 | if (!tree->nodes) { | |
593 | free(tree); | |
594 | return NULL; | |
595 | } | |
596 | tree->num = 1; | |
597 | tree->nodes[0].dirname = NULL; | |
598 | tree->nodes[0].name = strdup(path); | |
599 | if (!tree->nodes[0].name) { | |
600 | free(tree->nodes); | |
601 | free(tree); | |
602 | return NULL; | |
603 | } | |
604 | tree->nodes[0].parentid = 0; | |
605 | tree->nodes[0].objid = id; | |
606 | return tree; | |
607 | } | |
608 | ||
609 | static bool update_tree_node(struct mytree_node *n, u64 id, u64 parent, | |
610 | char *name, int name_len, char *dirname) | |
611 | { | |
612 | if (id) | |
613 | n->objid = id; | |
614 | ||
615 | if (parent) | |
616 | n->parentid = parent; | |
617 | ||
618 | if (name) { | |
619 | n->name = malloc(name_len + 1); | |
620 | if (!n->name) | |
621 | return false; | |
622 | ||
623 | (void)strlcpy(n->name, name, name_len + 1); | |
624 | } | |
625 | ||
626 | if (dirname) { | |
627 | size_t len; | |
628 | ||
629 | len = strlen(dirname); | |
630 | n->dirname = malloc(len + 1); | |
631 | if (!n->dirname) { | |
632 | free(n->name); | |
633 | return false; | |
634 | } | |
635 | ||
636 | (void)strlcpy(n->dirname, dirname, len + 1); | |
637 | } | |
638 | return true; | |
639 | } | |
640 | ||
641 | static bool add_btrfs_tree_node(struct my_btrfs_tree *tree, u64 id, u64 parent, | |
642 | char *name, int name_len, char *dirname) | |
643 | { | |
644 | struct mytree_node *tmp; | |
645 | ||
646 | int i = get_btrfs_tree_idx(tree, id); | |
647 | if (i != -1) | |
648 | return update_tree_node(&tree->nodes[i], id, parent, name, | |
649 | name_len, dirname); | |
650 | ||
651 | tmp = realloc(tree->nodes, (tree->num+1) * sizeof(struct mytree_node)); | |
652 | if (!tmp) | |
653 | return false; | |
654 | tree->nodes = tmp; | |
655 | memset(&tree->nodes[tree->num], 0, sizeof(struct mytree_node)); | |
656 | if (!update_tree_node(&tree->nodes[tree->num], id, parent, name, | |
657 | name_len, dirname)) | |
658 | return false; | |
659 | tree->num++; | |
660 | return true; | |
661 | } | |
662 | ||
663 | static void free_btrfs_tree(struct my_btrfs_tree *tree) | |
664 | { | |
665 | int i; | |
666 | if (!tree) | |
667 | return; | |
668 | for (i = 0; i < tree->num; i++) { | |
669 | free(tree->nodes[i].name); | |
670 | free(tree->nodes[i].dirname); | |
671 | } | |
672 | free(tree->nodes); | |
673 | free(tree); | |
674 | } | |
675 | ||
676 | /* | |
677 | * Given a @tree of subvolumes under @path, ask btrfs to remove each | |
678 | * subvolume | |
679 | */ | |
680 | static bool do_remove_btrfs_children(struct my_btrfs_tree *tree, u64 root_id, | |
681 | const char *path) | |
682 | { | |
683 | int i, ret; | |
684 | char *newpath; | |
685 | size_t len; | |
686 | ||
687 | for (i = 0; i < tree->num; i++) { | |
688 | if (tree->nodes[i].parentid == root_id) { | |
689 | if (!tree->nodes[i].dirname) { | |
690 | WARN("Odd condition: child objid with no name under %s\n", path); | |
691 | continue; | |
692 | } | |
693 | len = strlen(path) + strlen(tree->nodes[i].dirname) + 2; | |
694 | newpath = malloc(len); | |
695 | if (!newpath) { | |
696 | ERROR("Out of memory"); | |
697 | return false; | |
698 | } | |
699 | ret = snprintf(newpath, len, "%s/%s", path, tree->nodes[i].dirname); | |
700 | if (ret < 0 || ret >= len) { | |
701 | free(newpath); | |
702 | return false; | |
703 | } | |
704 | if (!do_remove_btrfs_children(tree, tree->nodes[i].objid, newpath)) { | |
705 | ERROR("Failed to prune %s\n", tree->nodes[i].name); | |
706 | free(newpath); | |
707 | return false; | |
708 | } | |
709 | if (btrfs_do_destroy_subvol(newpath) != 0) { | |
710 | ERROR("Failed to remove %s\n", newpath); | |
711 | free(newpath); | |
712 | return false; | |
713 | } | |
714 | free(newpath); | |
715 | } | |
716 | } | |
717 | return true; | |
718 | } | |
719 | ||
720 | static int btrfs_recursive_destroy(const char *path) | |
721 | { | |
722 | u64 root_id; | |
723 | int fd; | |
724 | struct btrfs_ioctl_search_args args; | |
725 | struct btrfs_ioctl_search_key *sk = &args.key; | |
726 | struct btrfs_ioctl_search_header sh; | |
727 | struct btrfs_root_ref *ref; | |
728 | struct my_btrfs_tree *tree; | |
729 | int ret, e, i; | |
730 | unsigned long off = 0; | |
731 | int name_len; | |
732 | char *name; | |
733 | char *tmppath; | |
734 | u64 dir_id; | |
735 | ||
736 | fd = open(path, O_RDONLY); | |
737 | if (fd < 0) { | |
738 | ERROR("Failed to open %s\n", path); | |
739 | return -1; | |
740 | } | |
741 | ||
742 | if (btrfs_list_get_path_rootid(fd, &root_id)) { | |
743 | e = errno; | |
744 | close(fd); | |
745 | if (e == EPERM || e == EACCES) { | |
746 | WARN("Will simply try removing"); | |
747 | goto ignore_search; | |
748 | } | |
749 | ||
750 | return -1; | |
751 | } | |
752 | ||
753 | tree = create_my_btrfs_tree(root_id, path, strlen(path)); | |
754 | if (!tree) { | |
755 | ERROR("Out of memory\n"); | |
756 | close(fd); | |
757 | return -1; | |
758 | } | |
759 | /* Walk all subvols looking for any under this id */ | |
760 | memset(&args, 0, sizeof(args)); | |
761 | ||
762 | /* search in the tree of tree roots */ | |
763 | sk->tree_id = 1; | |
764 | ||
765 | sk->max_type = BTRFS_ROOT_REF_KEY; | |
766 | sk->min_type = BTRFS_ROOT_ITEM_KEY; | |
767 | sk->min_objectid = 0; | |
768 | sk->max_objectid = (u64)-1; | |
769 | sk->max_offset = (u64)-1; | |
770 | sk->min_offset = 0; | |
771 | sk->max_transid = (u64)-1; | |
772 | sk->nr_items = 4096; | |
773 | ||
774 | while(1) { | |
775 | ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); | |
776 | e = errno; | |
777 | if (ret < 0) { | |
778 | close(fd); | |
779 | free_btrfs_tree(tree); | |
780 | if (e == EPERM || e == EACCES) { | |
781 | WARN("Warn: can't perform the search under %s. Will simply try removing", path); | |
782 | goto ignore_search; | |
783 | } | |
784 | ||
785 | ERROR("Error: can't perform the search under %s\n", path); | |
786 | return -1; | |
787 | } | |
788 | if (sk->nr_items == 0) | |
789 | break; | |
790 | ||
791 | off = 0; | |
792 | for (i = 0; i < sk->nr_items; i++) { | |
793 | memcpy(&sh, args.buf + off, sizeof(sh)); | |
794 | off += sizeof(sh); | |
795 | /* | |
796 | * A backref key with the name and dirid of the parent | |
797 | * comes followed by the reoot ref key which has the | |
798 | * name of the child subvol in question. | |
799 | */ | |
800 | if (sh.objectid != root_id && sh.type == BTRFS_ROOT_BACKREF_KEY) { | |
801 | ref = (struct btrfs_root_ref *)(args.buf + off); | |
802 | name_len = btrfs_stack_root_ref_name_len(ref); | |
803 | name = (char *)(ref + 1); | |
804 | dir_id = btrfs_stack_root_ref_dirid(ref); | |
805 | tmppath = get_btrfs_subvol_path(fd, sh.offset, | |
806 | dir_id, name, name_len); | |
807 | if (!add_btrfs_tree_node(tree, sh.objectid, | |
808 | sh.offset, name, | |
809 | name_len, tmppath)) { | |
810 | ERROR("Out of memory"); | |
811 | free_btrfs_tree(tree); | |
812 | free(tmppath); | |
813 | close(fd); | |
814 | return -1; | |
815 | } | |
816 | free(tmppath); | |
817 | } | |
818 | off += sh.len; | |
819 | ||
820 | /* | |
821 | * record the mins in sk so we can make sure the | |
822 | * next search doesn't repeat this root | |
823 | */ | |
824 | sk->min_objectid = sh.objectid; | |
825 | sk->min_type = sh.type; | |
826 | sk->min_offset = sh.offset; | |
827 | } | |
828 | sk->nr_items = 4096; | |
829 | sk->min_offset++; | |
830 | if (!sk->min_offset) | |
831 | sk->min_type++; | |
832 | else | |
833 | continue; | |
834 | ||
835 | if (sk->min_type > BTRFS_ROOT_BACKREF_KEY) { | |
836 | sk->min_type = BTRFS_ROOT_ITEM_KEY; | |
837 | sk->min_objectid++; | |
838 | } else | |
839 | continue; | |
840 | ||
841 | if (sk->min_objectid >= sk->max_objectid) | |
842 | break; | |
843 | } | |
844 | close(fd); | |
845 | ||
846 | /* now actually remove them */ | |
847 | ||
848 | if (!do_remove_btrfs_children(tree, root_id, path)) { | |
849 | free_btrfs_tree(tree); | |
850 | ERROR("failed pruning\n"); | |
851 | return -1; | |
852 | } | |
853 | ||
854 | free_btrfs_tree(tree); | |
855 | /* All child subvols have been removed, now remove this one */ | |
856 | ignore_search: | |
857 | return btrfs_do_destroy_subvol(path); | |
858 | } | |
859 | ||
860 | bool btrfs_try_remove_subvol(const char *path) | |
861 | { | |
862 | if (!btrfs_detect(path)) | |
863 | return false; | |
864 | ||
865 | return btrfs_recursive_destroy(path) == 0; | |
866 | } | |
867 | ||
868 | int btrfs_destroy(struct lxc_storage *orig) | |
869 | { | |
870 | const char *src; | |
871 | ||
872 | src = lxc_storage_get_path(orig->src, "btrfs"); | |
873 | ||
874 | return btrfs_recursive_destroy(src); | |
875 | } | |
876 | ||
877 | int btrfs_create(struct lxc_storage *bdev, const char *dest, const char *n, | |
878 | struct bdev_specs *specs) | |
879 | { | |
880 | int ret; | |
881 | size_t len; | |
882 | ||
883 | len = strlen(dest) + 1; | |
884 | /* strlen("btrfs:") */ | |
885 | len += 6; | |
886 | bdev->src = malloc(len); | |
887 | if (!bdev->src) { | |
888 | ERROR("Failed to allocate memory"); | |
889 | return -1; | |
890 | } | |
891 | ||
892 | ret = snprintf(bdev->src, len, "btrfs:%s", dest); | |
893 | if (ret < 0 || (size_t)ret >= len) { | |
894 | ERROR("Failed to create string"); | |
895 | return -1; | |
896 | } | |
897 | ||
898 | bdev->dest = strdup(dest); | |
899 | if (!bdev->dest) { | |
900 | ERROR("Failed to duplicate string \"%s\"", dest); | |
901 | return -1; | |
902 | } | |
903 | ||
904 | ret = btrfs_subvolume_create(bdev->dest); | |
905 | if (ret < 0) { | |
906 | SYSERROR("Failed to create btrfs subvolume \"%s\"", bdev->dest); | |
907 | } | |
908 | ||
909 | return ret; | |
910 | } |