chiark / gitweb /
udevadm: test - remove --force option
[elogind.git] / extras / volume_id / lib / btrfs.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <byteswap.h>
31
32 #include "libvolume_id.h"
33 #include "libvolume_id-private.h"
34
35 struct btrfs_super_block {
36         uint8_t csum[32];
37         uint8_t fsid[16];
38         uint64_t bytenr;
39         uint64_t flags;
40         uint8_t magic[8];
41         uint64_t generation;
42         uint64_t root;
43         uint64_t chunk_root;
44         uint64_t log_root;
45         uint64_t log_root_transid;
46         uint64_t total_bytes;
47         uint64_t bytes_used;
48         uint64_t root_dir_objectid;
49         uint64_t num_devices;
50         uint32_t sectorsize;
51         uint32_t nodesize;
52         uint32_t leafsize;
53         uint32_t stripesize;
54         uint32_t sys_chunk_array_size;
55         uint64_t chunk_root_generation;
56         uint64_t compat_flags;
57         uint64_t compat_ro_flags;
58         uint64_t incompat_flags;
59         uint16_t csum_type;
60         uint8_t root_level;
61         uint8_t chunk_root_level;
62         uint8_t log_root_level;
63         struct btrfs_dev_item {
64                 uint64_t devid;
65                 uint64_t total_bytes;
66                 uint64_t bytes_used;
67                 uint32_t io_align;
68                 uint32_t io_width;
69                 uint32_t sector_size;
70                 uint64_t type;
71                 uint64_t generation;
72                 uint64_t start_offset;
73                 uint32_t dev_group;
74                 uint8_t seek_speed;
75                 uint8_t bandwidth;
76                 uint8_t uuid[16];
77                 uint8_t fsid[16];
78         } PACKED dev_item;
79         uint8_t label[256];
80 } PACKED;
81
82 int volume_id_probe_btrfs(struct volume_id *id, uint64_t off, uint64_t size)
83 {
84         const uint8_t *buf;
85         struct btrfs_super_block *bfs;
86
87         info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
88
89         buf = volume_id_get_buffer(id, off + 0x10000, 0x200);
90         if (buf == NULL)
91                 return -1;
92         bfs = (struct btrfs_super_block *)buf;
93         if (memcmp(bfs->magic, "_BHRfS_M", 8) != 0)
94                 return -1;
95         volume_id_set_uuid(id, bfs->fsid, 0, UUID_DCE);
96         volume_id_set_uuid_sub(id, bfs->dev_item.uuid, 0, UUID_DCE);
97         volume_id_set_label_raw(id, bfs->label, 256);
98         volume_id_set_label_string(id, bfs->label, 256);
99         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
100         id->type = "btrfs";
101         return 0;
102 }