File: format.c

package info (click to toggle)
reiser4progs 1.0.9-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,608 kB
  • ctags: 3,844
  • sloc: ansic: 33,541; sh: 10,679; makefile: 1,010
file content (330 lines) | stat: -rw-r--r-- 8,866 bytes parent folder | download | duplicates (5)
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
/* Copyright 2001-2005 by Hans Reiser, licensing governed by 
   reiser4progs/COPYING.
   
   libprogs/format.c - methods are needed for handle the fs format. */

#include <repair/librepair.h>

static int cb_check_plugname(char *name, void *data) {
	reiser4_plug_t **plug = (reiser4_plug_t **)data;
	
	if (!name) return 0;
	
	*plug = reiser4_factory_nfind(name);
	if ((*plug)->id.type != KEY_PLUG_TYPE)
		*plug = NULL;
	
	return *plug ? 1 : 0;
}

count_t repair_format_len_old(aal_device_t *device, uint32_t blksize) {
	return (aal_device_len(device) * device->blksize / blksize);
}

/* This is the copy of the repair_formaat_check_len, however it calculates
   the amount of allowable blocks differently, actually not correctly, but
   how it was created in old progs versions, to not force users to build-sb
   and ossibly lost thier data. */
errno_t repair_format_check_len_old(aal_device_t *device, 
				    uint32_t blksize, 
				    count_t blocks) 
{
	count_t dev_len;

	aal_assert("vpf-1564", device != NULL);
	
	dev_len = repair_format_len_old(device, blksize);
	
	if (blocks > dev_len) {
		aal_error("Device %s is too small (%llu) for filesystem %llu "
			  "blocks long.", device->name, dev_len, blocks);
		return -EINVAL;
	}

	if (blocks < REISER4_FS_MIN_SIZE(blksize)) {
		aal_error("Requested filesystem size (%llu) is too small. "
			  "Reiser4 required minimal size %u blocks long.",
			  blocks, REISER4_FS_MIN_SIZE(blksize));
		return -EINVAL;
	}

	return 0;
}

static int cb_check_count(int64_t val, void *data) {
	reiser4_fs_t *fs = (reiser4_fs_t *)data;
	uint32_t blksize;
	
	if (val < 0) 
		return 0;
	
	blksize = reiser4_master_get_blksize(fs->master);
	return reiser4_format_check_len(fs->device, blksize, val) ? 0 : 1;
}

/* Try to open format if not yet and check it. */
errno_t repair_format_check_struct(reiser4_fs_t *fs, 
				   uint8_t mode, 
				   uint32_t options) 
{
	reiser4_format_ent_t *format;
	reiser4_plug_t *plug; 
	format_hint_t hint;
	count_t blocks;
	bool_t over;
	errno_t res;
	rid_t pid;
	
	aal_assert("vpf-165", fs != NULL);
	aal_assert("vpf-171", fs->device != NULL);
	aal_assert("vpf-834", fs->master != NULL);
	
	pid = reiser4_master_get_format(fs->master);

	/* If format is not opened but the master has been changed, try
	 to open format again -- probably the master format plug id has 
	 been changed. */
	if (!fs->format && pid < FORMAT_LAST_ID && 
	    reiser4_master_isdirty(fs->master))
	{
		fs->format = reiser4_format_open(fs);
	}

	/* If format is still not opened, return the error not in 
	   the BUILD mode. */
	if (!fs->format && mode != RM_BUILD)
		return RE_FATAL;
	
	/* Prepare the format hint for the futher checks. */
	aal_memset(&hint, 0, sizeof(hint));

	/* If the policy/key/etc plugin is overridden in the profile or there 
	   is no opened backup not format, policy is taken from the profile. 
	   Otherwise from the backup if opened or from the format. */
	over = reiser4_profile_overridden(PROF_POLICY);
	plug = reiser4_profile_plug(PROF_POLICY);
	hint.policy = ((reiser4_policy_plug_t *)plug)->p.id.id;
	hint.mask |= over ? (1 << PM_POLICY) : 0;

	over = reiser4_profile_overridden(PROF_KEY);
	plug = reiser4_profile_plug(PROF_KEY);
	
	if (over) {
		hint.mask |= (1 << PM_KEY);
	} else if (!fs->backup && mode == RM_BUILD) {
		char buff[256];

		plug = reiser4_profile_plug(PROF_KEY);
		aal_memset(buff, 0, sizeof(buff));
		aal_memcpy(buff, plug->label, 
			   aal_strlen(plug->label));

		if (!(options & (1 << REPAIR_YES))) {
			aal_ui_get_alpha(buff, cb_check_plugname, &plug, 
					 "Enter the key plugin name");
		}
		
		hint.mask |= (1 << PM_KEY);
	}
	
	hint.key = plug->id.id;
	
	hint.blksize = reiser4_master_get_blksize(fs->master);
	hint.blocks = repair_format_len_old(fs->device, hint.blksize);

	/* Check the block count if the backup is not opened. */
	if (!fs->backup) {
		if (fs->format) {
			blocks = reiser4_format_get_len(fs->format);

			if (repair_format_check_len_old(fs->device, 
							hint.blksize, blocks))
			{
				/* FS length is not valid. */
				if (mode != RM_BUILD)
					return RE_FATAL;

				blocks = MAX_UINT64;
			} 
			
			if (blocks == reiser4_format_len(fs->device, 
							 hint.blksize)) 
			{
				hint.blocks = reiser4_format_len(fs->device, 
								 hint.blksize);
			}
			
			if (blocks != MAX_UINT64 && blocks != hint.blocks) {
				aal_warn("Number of blocks found in the super "
					 "block (%llu) is not equal to the size"
					 " of the partition (%llu).%s", blocks,
					 hint.blocks, mode != RM_BUILD ? 
					 " Assuming this is correct.": "");
			}
		} else {
			blocks = MAX_UINT64;
		}

		if (blocks != hint.blocks && mode == RM_BUILD) {
			/* Confirm that size is correct. */
			blocks = blocks != MAX_UINT64 ? blocks :
				reiser4_format_len(fs->device, hint.blksize);
			
			if (!(options & (1 << REPAIR_YES))) {
				blocks = aal_ui_get_numeric(blocks, 
							    cb_check_count, fs, 
							    "Enter the correct "
							    "block count please");
			}
			
			hint.blocks = blocks;
		}
	}

	/* If there still is no format opened, create a new one or regenerate 
	   it from the backup if exists. */
	if (!fs->format) {
		if (!(plug = reiser4_factory_ifind(FORMAT_PLUG_TYPE, pid))) {
			aal_fatal("Failed to find a format plugin "
				  "by its on-disk id (%u).", pid);
			return -EINVAL;
		}
		
		if (fs->backup) {
			format = plugcall((reiser4_format_plug_t *)plug, 
					  regenerate, fs->device, 
					  &fs->backup->hint);
		} else {
			format = plugcall((reiser4_format_plug_t *)plug, 
					  create, fs->device, &hint);
		}

		if (!format) {
			aal_error("Failed to %s the format '%s' on '%s'.",
				  fs->backup ? "regenerate" : "create", 
				  plug->label, fs->device->name);
			return -EINVAL;
		} else {
			aal_warn("The format '%s' is %s on '%s'.", plug->label,
				 fs->backup ? "regenerated from backup" : 
				 "created", fs->device->name);
		}
	} else {
		format = fs->format->ent;
	}
	
	/* Check the format structure. If there is no backup and format, then 
	   @fent has been just created, nothing to check anymore. */
	if (fs->backup || fs->format) {
		res = plugcall(format->plug, check_struct, format,
			       fs->backup ? &fs->backup->hint : NULL,
			       &hint, mode);
	} else {
		res = 0;
	}
	
	if (!fs->format) {
		if (!(fs->format = aal_calloc(sizeof(reiser4_format_t), 0))) {
			aal_error("Can't allocate the format.");
			plugcall((reiser4_format_plug_t *)plug, close, format);
			return -ENOMEM;
		}

		fs->format->fs = fs;
		fs->format->ent = format;
	}
	
	return res;
}

errno_t repair_format_update(reiser4_format_t *format) {
	aal_assert("vpf-829", format != NULL);

	if (format->ent->plug->update == NULL)
		return 0;
    
	return format->ent->plug->update(format->ent);
}

/* Fetches format data to @stream. */
errno_t repair_format_pack(reiser4_format_t *format, aal_stream_t *stream) {
	aal_assert("umka-2604", format != NULL);

	return reiser4call(format, pack, stream);
}

/* Prints @format to passed @stream */
void repair_format_print(reiser4_format_t *format, aal_stream_t *stream) {
	aal_assert("umka-1560", format != NULL);

	reiser4call(format, print, stream, 0);
}

/* Loads format data from @stream to format entity. */
reiser4_format_t *repair_format_unpack(reiser4_fs_t *fs, aal_stream_t *stream) {
	rid_t pid;
	uint32_t blksize;
	reiser4_plug_t *plug;
	reiser4_format_t *format;
	
	aal_assert("umka-2606", fs != NULL);
	aal_assert("umka-2607", stream != NULL);

	if (aal_stream_read(stream, &pid, sizeof(pid)) != sizeof(pid)) {
		aal_error("Can't unpack disk format. Stream is over?");
		return NULL;
	}

	/* Getting needed plugin from plugin factory */
	if (!(plug = reiser4_factory_ifind(FORMAT_PLUG_TYPE, pid)))  {
		aal_error("Can't find disk-format plugin by "
			  "its id 0x%x.", pid);
		return NULL;
	}
    
	/* Allocating memory for format instance. */
	if (!(format = aal_calloc(sizeof(*format), 0)))
		return NULL;

	format->fs = fs;
	format->fs->format = format;

	blksize = reiser4_master_get_blksize(fs->master);
	
	if (!(format->ent = plugcall((reiser4_format_plug_t *)plug, unpack,
				      fs->device, blksize, stream)))
	{
		aal_error("Can't unpack disk-format.");
		goto error_free_format;
	}

	return format;

 error_free_format:
	aal_free(format);
	return NULL;
}

errno_t repair_format_check_backup(aal_device_t *device, backup_hint_t *hint) {
	reiser4_master_sb_t *master;
	reiser4_plug_t *plug;
	errno_t res;

	aal_assert("vpf-1732", hint != NULL);
	
	master = (reiser4_master_sb_t *)
		(hint->block.data + hint->off[BK_MASTER]);
	
	if (!(plug = reiser4_factory_ifind(FORMAT_PLUG_TYPE, 
					   get_ms_format(master))))
	{
		return RE_FATAL;
	}

	if ((res = plugcall((reiser4_format_plug_t *)plug, check_backup, hint)))
		return res;
	
	return (repair_format_check_len_old(device, get_ms_blksize(master), 
					    hint->blocks)) ? RE_FATAL : 0;
}