Marek BehĂșn | 2938754 | 2017-09-03 17:00:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * BTRFS filesystem implementation for U-Boot |
| 3 | * |
| 4 | * 2017 Marek Behun, CZ.NIC, marek.behun@nic.cz |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include "btrfs.h" |
| 10 | #include <malloc.h> |
| 11 | |
| 12 | static int get_subvol_name(u64 subvolid, char *name, int max_len) |
| 13 | { |
| 14 | struct btrfs_root_ref rref; |
| 15 | struct btrfs_inode_ref iref; |
| 16 | struct btrfs_root root; |
| 17 | u64 dir; |
| 18 | char tmp[max(BTRFS_VOL_NAME_MAX, BTRFS_NAME_MAX)]; |
| 19 | char *ptr; |
| 20 | |
| 21 | ptr = name + max_len - 1; |
| 22 | *ptr = '\0'; |
| 23 | |
| 24 | while (subvolid != BTRFS_FS_TREE_OBJECTID) { |
| 25 | subvolid = btrfs_lookup_root_ref(subvolid, &rref, tmp); |
| 26 | |
| 27 | if (subvolid == -1ULL) |
| 28 | return -1; |
| 29 | |
| 30 | ptr -= rref.name_len + 1; |
| 31 | if (ptr < name) |
| 32 | goto too_long; |
| 33 | |
| 34 | memcpy(ptr + 1, tmp, rref.name_len); |
| 35 | *ptr = '/'; |
| 36 | |
| 37 | if (btrfs_find_root(subvolid, &root, NULL)) |
| 38 | return -1; |
| 39 | |
| 40 | dir = rref.dirid; |
| 41 | |
| 42 | while (dir != BTRFS_FIRST_FREE_OBJECTID) { |
| 43 | dir = btrfs_lookup_inode_ref(&root, dir, &iref, tmp); |
| 44 | |
| 45 | if (dir == -1ULL) |
| 46 | return -1; |
| 47 | |
| 48 | ptr -= iref.name_len + 1; |
| 49 | if (ptr < name) |
| 50 | goto too_long; |
| 51 | |
| 52 | memcpy(ptr + 1, tmp, iref.name_len); |
| 53 | *ptr = '/'; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (ptr == name + max_len - 1) { |
| 58 | name[0] = '/'; |
| 59 | name[1] = '\0'; |
| 60 | } else { |
| 61 | memmove(name, ptr, name + max_len - ptr); |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | |
| 66 | too_long: |
| 67 | printf("%s: subvolume name too long\n", __func__); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | u64 btrfs_get_default_subvol_objectid(void) |
| 72 | { |
| 73 | struct btrfs_dir_item item; |
| 74 | |
| 75 | if (btrfs_lookup_dir_item(&btrfs_info.tree_root, |
| 76 | btrfs_info.sb.root_dir_objectid, "default", 7, |
| 77 | &item)) |
| 78 | return BTRFS_FS_TREE_OBJECTID; |
| 79 | return item.location.objectid; |
| 80 | } |
| 81 | |
| 82 | static void list_subvols(u64 tree, char *nameptr, int max_name_len, int level) |
| 83 | { |
| 84 | struct btrfs_key key, *found_key; |
| 85 | struct btrfs_path path; |
| 86 | struct btrfs_root_ref *ref; |
| 87 | int res; |
| 88 | |
| 89 | key.objectid = tree; |
| 90 | key.type = BTRFS_ROOT_REF_KEY; |
| 91 | key.offset = 0; |
| 92 | |
| 93 | if (btrfs_search_tree(&btrfs_info.tree_root, &key, &path)) |
| 94 | return; |
| 95 | |
| 96 | do { |
| 97 | found_key = btrfs_path_leaf_key(&path); |
| 98 | if (btrfs_comp_keys_type(&key, found_key)) |
| 99 | break; |
| 100 | |
| 101 | ref = btrfs_path_item_ptr(&path, struct btrfs_root_ref); |
| 102 | btrfs_root_ref_to_cpu(ref); |
| 103 | |
| 104 | printf("ID %llu parent %llu name ", found_key->offset, tree); |
| 105 | if (nameptr && !get_subvol_name(found_key->offset, nameptr, |
| 106 | max_name_len)) |
| 107 | printf("%s\n", nameptr); |
| 108 | else |
| 109 | printf("%.*s\n", (int) ref->name_len, |
| 110 | (const char *) (ref + 1)); |
| 111 | |
| 112 | if (level > 0) |
| 113 | list_subvols(found_key->offset, nameptr, max_name_len, |
| 114 | level - 1); |
| 115 | else |
| 116 | printf("%s: Too much recursion, maybe skipping some " |
| 117 | "subvolumes\n", __func__); |
| 118 | } while (!(res = btrfs_next_slot(&path))); |
| 119 | |
| 120 | btrfs_free_path(&path); |
| 121 | } |
| 122 | |
| 123 | void btrfs_list_subvols(void) |
| 124 | { |
| 125 | char *nameptr = malloc(4096); |
| 126 | |
| 127 | list_subvols(BTRFS_FS_TREE_OBJECTID, nameptr, nameptr ? 4096 : 0, 40); |
| 128 | |
| 129 | if (nameptr) |
| 130 | free(nameptr); |
| 131 | } |