File: super.c

package info (click to toggle)
jfsutils 1.1.15-7
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,076 kB
  • sloc: ansic: 35,080; sh: 1,048; makefile: 81
file content (335 lines) | stat: -rw-r--r-- 7,152 bytes parent folder | download | duplicates (10)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 *   Copyright (c) International Business Machines Corp., 2000-2002
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>

#include "jfs_endian.h"
#include "jfs_types.h"
#include "jfs_filsys.h"
#include "jfs_superblock.h"
#include "libjufs.h"
#include "devices.h"
#include "jfs_dmap.h"
#include "utilsubs.h"

/*
 * NAME: inrange
 *
 * FUNCTION: Checks to see that <num> is a power-of-2 multiple of <low> that is
 *      less than or equal to <high>.
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *
 * NOTES:
 *
 * DATA STRUCTURES:
 *
 * RETURNS: If it is, it returns 1, else 0
 */
int inrange(uint32_t num, uint32_t low, uint32_t high)
{
	if (low) {
		for (; low <= high; low <<= 1) {
			if (low == num) {
				return 1;
			}
		}
	}
	return 0;
}

/*
 * NAME: validate_sizes
 *
 * FUNCTION: Ensure that all configurable sizes fall within their respective
 *      version specific limits.
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *      sb      - superblock to check sizes
 *
 * NOTES:
 *
 * RECOVERY OPERATION:
 *
 * DATA STRUCTURES:
 *
 * RETURNS:
 *      success: 0
 *      failure:  any other value
 */
static int validate_sizes(struct superblock *sb)
{
	/*
	 * Make sure AG size is at least 32M
	 */
	if (sb->s_agsize >= (1 << L2BPERDMAP)) {
		return 0;
	}

	return EINVAL;
}

/*
 * NAME: ujfs_validate_super
 *
 * FUNCTION: Check if superblock is valid
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *      sb      - superblock to validate
 *
 * NOTES:
 *
 * RECOVERY OPERATION:
 *
 * DATA STRUCTURES:
 *
 * RETURNS:
 *      success: 0
 *      failure: LIBFS_CORRUPTSUPER, LIBFS_BADVERSION, LIBFS_BADMAGIC
 */
int ujfs_validate_super(struct superblock *sb)
{
	if (memcmp(sb->s_magic, JFS_MAGIC, sizeof (sb->s_magic)) == 0) {
		if (sb->s_version > JFS_VERSION)
			return LIBFS_BADVERSION;
		if (validate_sizes(sb) == EINVAL)
			return LIBFS_CORRUPTSUPER;
	} else {
		return LIBFS_BADMAGIC;
	}
	return 0;
}

/*
 * NAME: ujfs_put_superblk
 *
 * FUNCTION: Write primary or secondary aggregate superblock
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *      fp              - open port for device to write superblock to
 *      sb              - pointer to struct superblock to be written
 *      is_primary      - 0 value means we are putting the secondary superblock;
 *                        non-zero value means we are putting the primary
 *                        superblock.
 *
 * NOTES: The sizeof(struct superblock) is less than the amount of disk space
 *      being allowed for the superblock (SIZE_OF_SUPER).  This function will
 *      write 0's to the space following the actual superblock structure to fill
 *      the entire allocated disk space.
 *
 * RECOVERY OPERATION:
 *
 * DATA STRUCTURES:
 *
 * RETURNS:
 *      success: 0
 *      failure: any other value
 */
int ujfs_put_superblk(FILE *fp, struct superblock *sb, int16_t is_primary)
{
	char buf[SIZE_OF_SUPER];
	int rc;

	memset(buf, 0, SIZE_OF_SUPER);
	memcpy(buf, sb, sizeof (*sb));

	/* swap if on big endian machine */
	ujfs_swap_superblock((struct superblock *) buf);

	rc = ujfs_rw_diskblocks(fp, (is_primary ? SUPER1_OFF : SUPER2_OFF),
				SIZE_OF_SUPER, buf, PUT);

	return rc;
}

/*
 * NAME: ujfs_get_superblk
 *
 * FUNCTION: read either the primary or secondary superblock from the specified
 *      device
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *      fp              - open port for device to read superblock from
 *      sb              - pointer to struct superblock to be filled in on return
 *      is_primary      - 0 indicates to retrieve secondary superblock,
 *                        otherwise retrieve primary superblock
 *
 * NOTES:
 *
 * RECOVERY OPERATION:
 *
 * DATA STRUCTURES:
 *
 * RETURNS:
 *      success: 0
 *      failure: any other value
 */
int ujfs_get_superblk(FILE *fp, struct superblock *sb, int32_t is_primary)
{
	int rc;
	char buf[SIZE_OF_SUPER];
	struct superblock *sblk = (struct superblock *) buf;

	rc = ujfs_rw_diskblocks(fp, (is_primary ? SUPER1_OFF : SUPER2_OFF),
				SIZE_OF_SUPER, sblk, GET);

	ujfs_swap_superblock(sblk);

	if (rc != 0)
		return rc;

	memcpy(sb, sblk, sizeof (*sb));
	return 0;
}

/*
 * NAME: ujfs_validate_logsuper
 *
 * FUNCTION: Check if log superblock is valid
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *      logsup - log superblock to validate
 *
 * NOTES:
 *
 * RECOVERY OPERATION:
 *
 * DATA STRUCTURES:
 *
 * RETURNS:
 *      success: 0
 *      failure: LIBFS_BADVERSION, LIBFS_BADMAGIC
 */
int ujfs_validate_logsuper(struct logsuper *logsup)
{
	/* see if superblock is JFS log superblock */
	if (logsup->magic == LOGMAGIC) {
		if (logsup->version != LOGVERSION) {
			return LIBFS_BADVERSION;
		}
	} else {
		return LIBFS_BADMAGIC;
	}

	return 0;
}

/*
 * NAME: ujfs_put_logsuper
 *
 * FUNCTION: Write log superblock to disk
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *      fp      - open port for device to write superblock to
 *      logsup  - pointer to log superblock to be written
 *
 * NOTES:
 *
 * RECOVERY OPERATION:
 *
 * DATA STRUCTURES:
 *
 * RETURNS:
 *      success: 0
 *      failure: any other value
 */
int ujfs_put_logsuper(FILE *fp, struct logsuper *logsup)
{
	char buf[sizeof (struct logsuper)];
	int rc = 0;

	memcpy(buf, logsup, sizeof (struct logsuper));

	/* swap if on big endian machine */
	ujfs_swap_logsuper((struct logsuper *) buf);

	rc = ujfs_rw_diskblocks(fp, LOGPSIZE, sizeof (struct logsuper), buf, PUT);

	return rc;
}

/*
 * NAME: ujfs_get_logsuper
 *
 * FUNCTION: read the log superblock from the specified device
 *
 * PRE CONDITIONS:
 *
 * POST CONDITIONS:
 *
 * PARAMETERS:
 *      fp      - open port for device to read superblock from
 *      logsup  - pointer to log superblock to be filled in on return
 *
 * NOTES:
 *
 * RECOVERY OPERATION:
 *
 * DATA STRUCTURES:
 *
 * RETURNS:
 *      success: 0
 *      failure: any other value
 */
int ujfs_get_logsuper(FILE *fp, struct logsuper *logsup)
{
	int rc = 0;
	char buf[sizeof (struct logsuper)];
	struct logsuper *logsup_buf = (struct logsuper *) buf;

	rc = ujfs_rw_diskblocks(fp, LOGPSIZE, sizeof (struct logsuper), logsup_buf, GET);

	if (!rc) {
		ujfs_swap_logsuper(logsup_buf);
		memcpy(logsup, logsup_buf, sizeof (struct logsuper));
	}

	return rc;
}