blob: 1f8f3a69fc83cdc38e0824a2ef489e74adb7bbe0 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * Copyright (C) 2010 by Jiro SEKIBA <jir@unicus.jp>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License
6 */
7#include <stddef.h>
8#include <string.h>
9
10#include "superblocks.h"
11#include "crc32.h"
12
13struct nilfs_super_block {
14 uint32_t s_rev_level;
15 uint16_t s_minor_rev_level;
16 uint16_t s_magic;
17
18 uint16_t s_bytes;
19
20 uint16_t s_flags;
21 uint32_t s_crc_seed;
22 uint32_t s_sum;
23
24 uint32_t s_log_block_size;
25
26 uint64_t s_nsegments;
27 uint64_t s_dev_size;
28 uint64_t s_first_data_block;
29 uint32_t s_blocks_per_segment;
30 uint32_t s_r_segments_percentage;
31
32 uint64_t s_last_cno;
33 uint64_t s_last_pseg;
34 uint64_t s_last_seq;
35 uint64_t s_free_blocks_count;
36
37 uint64_t s_ctime;
38
39 uint64_t s_mtime;
40 uint64_t s_wtime;
41 uint16_t s_mnt_count;
42 uint16_t s_max_mnt_count;
43 uint16_t s_state;
44 uint16_t s_errors;
45 uint64_t s_lastcheck;
46
47 uint32_t s_checkinterval;
48 uint32_t s_creator_os;
49 uint16_t s_def_resuid;
50 uint16_t s_def_resgid;
51 uint32_t s_first_ino;
52
53 uint16_t s_inode_size;
54 uint16_t s_dat_entry_size;
55 uint16_t s_checkpoint_size;
56 uint16_t s_segment_usage_size;
57
58 uint8_t s_uuid[16];
59 char s_volume_name[80];
60
61 uint32_t s_c_interval;
62 uint32_t s_c_block_max;
63 uint32_t s_reserved[192];
64};
65
66/* nilfs2 magic string */
67#define NILFS_SB_MAGIC "\x34\x34"
68/* nilfs2 super block offset */
69#define NILFS_SB_OFF 0x400
70/* nilfs2 super block offset in kB */
71#define NILFS_SB_KBOFF (NILFS_SB_OFF >> 10)
72/* nilfs2 magic string offset within super block */
73#define NILFS_MAG_OFF 6
74
75static int probe_nilfs2(blkid_probe pr, const struct blkid_idmag *mag)
76{
77 struct nilfs_super_block *sb;
78 static unsigned char sum[4];
79 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
80 size_t bytes;
81 uint32_t crc;
82
83 sb = blkid_probe_get_sb(pr, mag, struct nilfs_super_block);
84 if (!sb)
85 return -1;
86
87 bytes = le16_to_cpu(sb->s_bytes);
88 crc = crc32(le32_to_cpu(sb->s_crc_seed), (unsigned char *)sb, sumoff);
89 crc = crc32(crc, sum, 4);
90 crc = crc32(crc, (unsigned char *)sb + sumoff + 4, bytes - sumoff - 4);
91
92 if (crc != le32_to_cpu(sb->s_sum))
93 return -1;
94
95 if (strlen(sb->s_volume_name))
96 blkid_probe_set_label(pr, (unsigned char *) sb->s_volume_name,
97 sizeof(sb->s_volume_name));
98
99 blkid_probe_set_uuid(pr, sb->s_uuid);
100 blkid_probe_sprintf_version(pr, "%u", le32_to_cpu(sb->s_rev_level));
101
102 return 0;
103}
104
105const struct blkid_idinfo nilfs2_idinfo =
106{
107 .name = "nilfs2",
108 .usage = BLKID_USAGE_FILESYSTEM,
109 .probefunc = probe_nilfs2,
110 .magics =
111 {
112 {
113 .magic = NILFS_SB_MAGIC,
114 .len = 2,
115 .kboff = NILFS_SB_KBOFF,
116 .sboff = NILFS_MAG_OFF
117 },
118 { NULL }
119 }
120};