File: sb.c

package info (click to toggle)
nilfs-tools 2.2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,852 kB
  • sloc: ansic: 15,594; sh: 4,374; perl: 4,174; makefile: 140
file content (257 lines) | stat: -rw-r--r-- 5,573 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * sb.c - NILFS super block access library
 *
 * Copyright (C) 2005-2012 Nippon Telegraph and Telephone Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * Written by Ryusuke Konishi <konishi.ryusuke@gmail.com>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif	/* HAVE_CONFIG_H */

#include <stdio.h>

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif	/* HAVE_STDLIB_H */

#if HAVE_STRING_H
#include <string.h>
#endif	/* HAVE_STRING_H */

#if HAVE_UNISTD_H
#include <unistd.h>
#endif	/* HAVE_UNISTD_H */

#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif	/* HAVE_SYS_TYPES_H */

#if HAVE_FCNTL_H
#include <fcntl.h>
#endif	/* HAVE_FCNTL_H */

#if HAVE_LIMITS_H
#include <limits.h>
#endif	/* HAVE_LIMITS_H */

#if HAVE_LINUX_TYPES_H
#include <linux/types.h>
#endif	/* HAVE_LINUX_TYPES_H */

#include <linux/fs.h>

#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif	/* HAVE_SYS_IOCTL_H */

#include <sys/stat.h>  /* fstat(), S_ISBLK(), S_ISREG(), etc */
#include <errno.h>
#include <assert.h>
#include "nilfs.h"
#include "util.h"
#include "compat.h"

#define NILFS_MAX_SB_SIZE	1024

extern __u32 crc32_le(__u32 seed, unsigned char const *data, size_t length);

static __u32 nilfs_sb_check_sum(struct nilfs_super_block *sbp)
{
	__u32 seed, crc;
	__le32 sum;

	seed = le32_to_cpu(sbp->s_crc_seed);
	sum = sbp->s_sum;
	sbp->s_sum = 0;
	crc = crc32_le(seed, (unsigned char *)sbp, le16_to_cpu(sbp->s_bytes));
	sbp->s_sum = sum;
	return crc;
}

static int nilfs_sb_is_valid(struct nilfs_super_block *sbp, int check_crc)
{
	__u32 crc;

	if (le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
		return 0;
	if (unlikely(le16_to_cpu(sbp->s_bytes) > NILFS_MAX_SB_SIZE))
		return 0;
	if (!check_crc)
		return 1;

	crc = nilfs_sb_check_sum(sbp);

	return crc == le32_to_cpu(sbp->s_sum);
}

static int nilfs_sb2_offset_is_too_small(struct nilfs_super_block *sbp,
					 __u64 sb2_offset)
{
	return sb2_offset < ((le64_to_cpu(sbp->s_nsegments) *
			      le32_to_cpu(sbp->s_blocks_per_segment)) <<
			     (le32_to_cpu(sbp->s_log_block_size) +
			      NILFS_SB_BLOCK_SIZE_SHIFT));
}

static int __nilfs_sb_read(int devfd, struct nilfs_super_block **sbp,
			   __u64 *offsets)
{
	__u64 devsize, sb2_offset;
	int invalid_fs = 0;
	int ret;
	struct stat stat;

	sbp[0] = malloc(NILFS_MAX_SB_SIZE);
	sbp[1] = malloc(NILFS_MAX_SB_SIZE);
	if (unlikely(sbp[0] == NULL || sbp[1] == NULL))
		goto failed;

	ret = fstat(devfd, &stat);
	if (unlikely(ret < 0))
		goto failed;

	if (S_ISBLK(stat.st_mode)) {
		ret = ioctl(devfd, BLKGETSIZE64, &devsize);
		if (unlikely(ret < 0))
			goto failed;
	} else if (S_ISREG(stat.st_mode)) {
		devsize = stat.st_size;
	} else {
		errno = EBADF;
		goto failed;
	}

	if (lseek(devfd, NILFS_SB_OFFSET_BYTES, SEEK_SET) >= 0 &&
	    read(devfd, sbp[0], NILFS_MAX_SB_SIZE) >= 0) {
		if (nilfs_sb_is_valid(sbp[0], 0))
			goto sb1_ok;
		invalid_fs = 1;
	}

	free(sbp[0]);
	sbp[0] = NULL;
sb1_ok:

	sb2_offset = NILFS_SB2_OFFSET_BYTES(devsize);
	if (offsets) {
		offsets[0] = NILFS_SB_OFFSET_BYTES;
		offsets[1] = sb2_offset;
	}

	if (lseek(devfd, sb2_offset, SEEK_SET) >= 0 &&
	    read(devfd, sbp[1], NILFS_MAX_SB_SIZE) >= 0) {
		if (nilfs_sb_is_valid(sbp[1], 0) &&
		    !nilfs_sb2_offset_is_too_small(sbp[1], sb2_offset))
			goto sb2_ok;
		invalid_fs = 1;
	}

	free(sbp[1]);
	sbp[1] = NULL;
sb2_ok:

	if (unlikely(!sbp[0] && !sbp[1])) {
		if (invalid_fs)
			errno = EINVAL;
		goto failed;
	}

	return 0;

 failed:
	free(sbp[0]);  /* free(NULL) is just ignored */
	free(sbp[1]);
	return -1;
}

struct nilfs_super_block *nilfs_sb_read(int devfd)
{
	struct nilfs_super_block *sbp[2];
	int ret;

	ret = __nilfs_sb_read(devfd, sbp, NULL);
	if (unlikely(ret < 0))
		return NULL;

	if (!sbp[0]) {
		sbp[0] = sbp[1];
		sbp[1] = NULL;
	}

	free(sbp[1]);

	return sbp[0];
}

int nilfs_sb_write(int devfd, struct nilfs_super_block *sbp, int mask)
{
	__u64 offsets[2];
	struct nilfs_super_block *sbps[2];
	ssize_t count;
	int i, ret;
	__u32 crc;

	assert(devfd >= 0);

	if (unlikely(sbp == NULL)) {
		errno = EINVAL;
		return -1;
	}

	ret = __nilfs_sb_read(devfd, sbps, offsets);
	if (unlikely(ret < 0))
		return -1;

	ret = 0;
	for (i = 0; i < 2; i++) {
		if (!sbps[i])
			continue;

		if (mask & NILFS_SB_LABEL)
			memcpy(sbps[i]->s_volume_name,
			       sbp->s_volume_name, sizeof(sbp->s_volume_name));

		if (mask & NILFS_SB_COMMIT_INTERVAL)
			sbps[i]->s_c_interval = sbp->s_c_interval;

		if (mask & NILFS_SB_BLOCK_MAX)
			sbps[i]->s_c_block_max = sbp->s_c_block_max;

		if (mask & NILFS_SB_UUID)
			memcpy(sbps[i]->s_uuid, sbp->s_uuid,
			       sizeof(sbp->s_uuid));

		if (mask & NILFS_SB_FEATURES) {
			sbps[i]->s_feature_compat = sbp->s_feature_compat;
			sbps[i]->s_feature_compat_ro = sbp->s_feature_compat_ro;
			sbps[i]->s_feature_incompat = sbp->s_feature_incompat;
		}

		crc = nilfs_sb_check_sum(sbps[i]);
		sbps[i]->s_sum = cpu_to_le32(crc);
		if (lseek(devfd, offsets[i], SEEK_SET) > 0) {
			count = write(devfd, sbps[i], NILFS_MAX_SB_SIZE);
			if (unlikely(count < NILFS_MAX_SB_SIZE)) {
				ret = -1;
				break;
			}
		}
	}

	free(sbps[0]);
	free(sbps[1]);

	return ret;
}