blob: a3ddf97825f0f8a646d22c8d980fa6d59f49a0e9 [file] [log] [blame]
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +01001/*
2 * volume_id - reads filesystem label and uuid
3 *
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5 * Copyright (C) 2009 Vladimir Dronnikov <dronnikov@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020021//config:config FEATURE_VOLUMEID_BTRFS
22//config: bool "btrfs filesystem"
23//config: default y
24//config: depends on VOLUMEID
Sven-Göran Bergh15d0a862013-07-31 15:57:59 +020025
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020026//kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_BTRFS) += btrfs.o
27
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010028#include "volume_id_internal.h"
29
30#define BTRFS_UUID_SIZE 16
31#define BTRFS_LABEL_SIZE 256
32#define BTRFS_CSUM_SIZE 32
33#define BTRFS_FSID_SIZE 16
34
35#define BTRFS_MAGIC "_BHRfS_M"
36
37struct btrfs_dev_item {
38 uint64_t devid;
39 uint64_t total_bytes;
40 uint64_t bytes_used;
41 uint32_t io_align;
42 uint32_t io_width;
43 uint32_t sector_size;
44 uint64_t type;
45 uint64_t generation;
46 uint64_t start_offset;
47 uint32_t dev_group;
48 uint8_t seek_speed;
49 uint8_t bandwidth;
50 uint8_t uuid[BTRFS_UUID_SIZE];
51 uint8_t fsid[BTRFS_UUID_SIZE];
52} PACKED;
53
54struct btrfs_super_block {
55 uint8_t csum[BTRFS_CSUM_SIZE];
56 uint8_t fsid[BTRFS_FSID_SIZE]; // UUID
57 uint64_t bytenr;
58 uint64_t flags;
59 uint8_t magic[8];
60 uint64_t generation;
61 uint64_t root;
62 uint64_t chunk_root;
63 uint64_t log_root;
64 uint64_t log_root_transid;
65 uint64_t total_bytes;
66 uint64_t bytes_used;
67 uint64_t root_dir_objectid;
68 uint64_t num_devices;
69 uint32_t sectorsize;
70 uint32_t nodesize;
71 uint32_t leafsize;
72 uint32_t stripesize;
73 uint32_t sys_chunk_array_size;
74 uint64_t chunk_root_generation;
75 uint64_t compat_flags;
76 uint64_t compat_ro_flags;
77 uint64_t incompat_flags;
78 uint16_t csum_type;
79 uint8_t root_level;
80 uint8_t chunk_root_level;
81 uint8_t log_root_level;
82 struct btrfs_dev_item dev_item;
83 uint8_t label[BTRFS_LABEL_SIZE]; // LABEL
84 // ...
85} PACKED;
86
Denys Vlasenko89300962009-11-01 23:07:18 +010087int FAST_FUNC volume_id_probe_btrfs(struct volume_id *id /*,uint64_t off*/)
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010088{
Denys Vlasenkod784b652009-11-02 00:12:35 +010089 // btrfs has superblocks at 64K, 64M and 256G
90 // minimum btrfs size is 256M
91 // so we never step out the device if we analyze
92 // the first and the second superblocks
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010093 struct btrfs_super_block *sb;
Denys Vlasenkod784b652009-11-02 00:12:35 +010094 unsigned off = 64;
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010095
Denys Vlasenkod784b652009-11-02 00:12:35 +010096 while (off < 64*1024*1024) {
97 off *= 1024;
98 dbg("btrfs: probing at offset 0x%x", off);
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +010099
Denys Vlasenkod784b652009-11-02 00:12:35 +0100100 sb = volume_id_get_buffer(id, off, sizeof(*sb));
101 if (sb == NULL)
102 return -1;
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +0100103
Denys Vlasenkod784b652009-11-02 00:12:35 +0100104 if (memcmp(sb->magic, BTRFS_MAGIC, 8) != 0)
105 return -1;
106 }
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +0100107
Denys Vlasenkod784b652009-11-02 00:12:35 +0100108 // N.B.: btrfs natively supports 256 (>VOLUME_ID_LABEL_SIZE) size labels
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +0100109 volume_id_set_label_string(id, sb->label, VOLUME_ID_LABEL_SIZE);
110 volume_id_set_uuid(id, sb->fsid, UUID_DCE);
Sven-Göran Bergh11f2c0d2012-09-02 14:56:25 +0200111 IF_FEATURE_BLKID_TYPE(id->type = "btrfs";)
Vladimir Dronnikov662e8b72009-11-01 23:05:09 +0100112
113 return 0;
114}