File: uobjectid.c

package info (click to toggle)
reiserfsprogs 1%3A3.6.27-3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,148 kB
  • sloc: ansic: 27,337; sh: 4,143; makefile: 87
file content (368 lines) | stat: -rw-r--r-- 8,457 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright 1996-2004 by Hans Reiser, licensing governed by
 * reiserfsprogs/README
 */

#include "fsck.h"

/* when --check fsck builds a map of objectids of files it finds in the tree
   when --rebuild-tree - fsck builds map of objectids it inserts into tree */

#define ALLOC_SIZE			1024
#define MAX_ID				(~(__u32)0)

/* 2 bytes for the counter */
#define BM_SIZE				(ALLOC_SIZE - sizeof(__u16))
#define BM_INTERVAL			(BM_SIZE * 8)
#define INDEX_COUNT			((MAX_ID / BM_INTERVAL) + 1)

#define id_map_interval(map, id)	(map->index + (id / BM_INTERVAL))

#define id_map_local_count(interval)	(interval + BM_SIZE)

/*
typedef struct sb_id_map {
    __u32 * m_begin;
    __u32 m_size, m_used_slot_count;
} sb_id_map_t;
*/

id_map_t *id_map_init()
{
	id_map_t *map;
	__u32 i;

	map = getmem(sizeof(id_map_t));
	map->index = mem_alloc(INDEX_COUNT * sizeof(void *));

	for (i = 0; i < INDEX_COUNT; i++) {
		if (map->index[i] != (void *)0)
			map->index[i] = (void *)0;
	}

	id_map_mark(map, 0);
	id_map_mark(map, 1);

	/* id == 0 should not be there, just for convinient usage */
	map->count--;

	return map;
}

void id_map_free(id_map_t *map)
{
	__u32 i;

	for (i = 0; i < INDEX_COUNT; i++) {
		if (map->index[i] != (void *)0 && map->index[i] != (void *)1)
			freemem(map->index[i]);
	}

	freemem(map->index);
	freemem(map);
}

int id_map_test(id_map_t *map, __u32 id)
{
	void **interval = id_map_interval(map, id);

	if (*interval == (void *)0)
		return 0;

	if (*interval == (void *)1)
		return 1;

	return misc_test_bit(id % BM_INTERVAL, *interval);
}

int id_map_mark(id_map_t *map, __u32 id)
{
	void **interval = id_map_interval(map, id);

	if (*interval == (void *)0)
		*interval = getmem(ALLOC_SIZE);

	if (*interval == (void *)1)
		return 1;

	if (misc_test_bit(id % BM_INTERVAL, *interval))
		return 1;

	misc_set_bit(id % BM_INTERVAL, *interval);

	(*(__u16 *) id_map_local_count(*interval))++;
	map->count++;

	if ((*(__u16 *) id_map_local_count(*interval)) == BM_INTERVAL) {
		/* Dealloc fully used bitmap */
		freemem(*interval);
		*interval = (void *)1;
	}

	if (map->last_used < (id / BM_INTERVAL))
		map->last_used = id / BM_INTERVAL;

	return 0;
}

/* call this for proper_id_map only!! */
__u32 id_map_alloc(id_map_t *map)
{
	__u32 i, zero_count;
	__u32 id = 0, first = ~(__u32) 0;

	for (i = 0, zero_count = 0; zero_count < 10 && i < INDEX_COUNT - 1; i++) {
		if (map->index[i] == (void *)0) {
			if (zero_count == 0)
				first = i;

			zero_count++;
		} else if (map->index[i] != (void *)1)
			break;
	}

	if (map->index[i] != (void *)1 && map->index[i] != (void *)0) {
		id = misc_find_first_zero_bit(map->index[i], BM_INTERVAL);
		if (id >= BM_INTERVAL)
			die("Id is out of interval size, interval looks corrupted.");

		id += i * BM_INTERVAL;
	} else if (first != ~(__u32) 0) {
		id = first * BM_INTERVAL;
		if (id == 0)
			id = 2;
	} else
		die("%s: No more free objectid is available.", __FUNCTION__);

	id_map_mark(map, id);

	return id;
}

/* this could be used if some more sofisticated flushing will be needed. */
/*
static void sb_id_map_pack(sb_id_map_t *map) {
    map->m_begin[1] = map->m_begin[map->m_used_slot_count - 1];
    memset(map->m_begin + 2, 0, map->m_used_slot_count - 2);
    map->m_used_slot_count = 2;
}*/

static __u32 id_map_next_bound(id_map_t *map, __u32 start)
{
	__u32 index = start / BM_INTERVAL;
	__u32 offset = start % BM_INTERVAL;
	int look_for;

	if (map->index[index] == (void *)0)
		look_for = 1;
	else if (map->index[index] == (void *)1)
		look_for = 0;
	else
		look_for = !misc_test_bit(offset, map->index[index]);

	offset++;

start_again:

	if (look_for) {
		while (index < INDEX_COUNT && map->index[index] == (void *)0)
			index++;

		if (index == INDEX_COUNT)
			return 0;

		if (map->index[index] == (void *)1)
			return index * BM_INTERVAL;

		offset =
		    misc_find_next_set_bit(map->index[index], BM_INTERVAL,
					   offset);

		if (offset >= BM_INTERVAL) {
			offset = 0;
			index++;
			goto start_again;
		}

		return index * BM_INTERVAL + offset;
	} else {
		while (index < INDEX_COUNT && map->index[index] == (void *)1)
			index++;

		if (index == INDEX_COUNT)
			return 0;

		if (map->index[index] == (void *)0)
			return index * BM_INTERVAL;

		offset =
		    misc_find_next_zero_bit(map->index[index], BM_INTERVAL,
					    offset);

		if (offset >= BM_INTERVAL) {
			offset = 0;
			index++;
			goto start_again;
		}

		return index * BM_INTERVAL + offset;
	}
}

void id_map_flush(struct id_map *map, reiserfs_filsys_t fs)
{
	int size, max, i;
	__u32 id, prev_id;
	__le32 *sb_objectid_map;

	size = reiserfs_super_block_size(fs->fs_ondisk_sb);
	sb_objectid_map = (__le32 *) ((char *)(fs->fs_ondisk_sb) + size);

	max = ((fs->fs_blocksize - size) >> 3 << 1);
	set_sb_oid_maxsize(fs->fs_ondisk_sb, max);

	id = 1;
	sb_objectid_map[0] = cpu_to_le32(1);

	for (i = 1; i < max - 1; i++) {
		id = id_map_next_bound(map, id);
		sb_objectid_map[i] = cpu_to_le32(id);
		if (id == 0) {
			if (i % 2)
				die("%s: Used interval is not closed on flushing.", __FUNCTION__);
			break;
		}
	}

	if (map->index[map->last_used] == (void *)0)
		die("Object id map looks corrupted - last used interval cannot be zeroed.");

	i++;

	if (i == max) {
		if (id == 0)
			die("Objectid interval does not contain any set bit what is expected.");

		if (map->index[map->last_used] == (void *)1) {
			prev_id = BM_INTERVAL - 1;
		} else {
			prev_id = ~(__u32) 0;

			if (id < map->last_used * BM_INTERVAL)
				id = 0;
			else
				id %= BM_INTERVAL;

			if (misc_test_bit(id, map->index[map->last_used]))
				prev_id = id;

			while ((id =
				misc_find_next_set_bit(map->
						       index[map->last_used],
						       BM_INTERVAL,
						       (id + 1))) !=
			       BM_INTERVAL) {
				prev_id = id;
			}

			if (prev_id == ~(__u32) 0)
				die("Objectid interval does not contain any set bit what is expected.");

			prev_id++;
		}

		sb_objectid_map[max - 1] =
		    cpu_to_le32(prev_id + map->last_used * BM_INTERVAL);
	} else {
		i--;
		memset(sb_objectid_map + i, 0, (max - i) * sizeof(__u32));
	}

	set_sb_oid_cursize(fs->fs_ondisk_sb, i);
}

/* FIXME: these 3 methods must be implemented also.

void fetch_objectid_map (struct id_map * map, reiserfs_filsys_t fs)
{
    int sb_size;
    __le32 * sb_objectid_map;

    sb_size = reiserfs_super_block_size (fs->fs_ondisk_sb);
    sb_objectid_map = (__le32 *)((char *)(fs->fs_ondisk_sb) + sb_size);

    if (map->m_page_count != 1)
	die ("fetch_objectid_map: can not fetch long map");

    make_id_space(map, 0);
    memcpy (map->m_begin, sb_objectid_map, get_sb_oid_cursize (fs->fs_ondisk_sb) * sizeof (__u32));
    map->m_used_slot_count = get_sb_oid_cursize (fs->fs_ondisk_sb);
}

#define OBJMAP_START_MAGIC 375331
#define OBJMAP_END_MAGIC 7700472

void reiserfs_objectid_map_save (FILE * fp, struct id_map * id_map)
{
    __u32 v;
    int i;

    v = OBJMAP_START_MAGIC;
    fwrite (&v, 4, 1, fp);

    v = id_map->m_used_slot_count;
    fwrite (&v, 4, 1, fp);

    for (i = 0; i < id_map->m_page_count - 1; i++) {
	fwrite ((char *)id_map->m_begin + MAP_SIZE * i, 4, 1, fp);
    }

    for (i = 0; i < id_map->m_used_slot_count * sizeof(__u32) - (id_map->m_page_count - 1) * MAP_SIZE; i++) {
	fwrite ((char *)id_map->m_begin + MAP_SIZE * (id_map->m_page_count - 1) + i, 4, 1, fp);
    }

    v = OBJMAP_END_MAGIC;
    fwrite (&v, 4, 1, fp);
}

struct id_map * reiserfs_objectid_map_load (FILE * fp)
{
    __u32 v;
    int i;
    struct id_map * id_map;

    fread (&v, 4, 1, fp);
    if (v != OBJMAP_START_MAGIC) {
	reiserfs_warning (stderr, "reiserfs_objectid_map_load: no objectid map begin magic found");
	return 0;
    }
	
    // read bit size of objectid map
    fread (&v, 4, 1, fp);

    id_map = init_id_map (MAP_NOT_PACKED);

    id_map->m_used_slot_count = v;
    id_map->m_page_count = v / MAP_SIZE + 1;

    id_map->m_begin = expandmem (id_map->m_begin, 0, id_map->m_page_count * MAP_SIZE);

    for (i = 0; i < id_map->m_page_count - 1; i++) {
	fread ((char *)id_map->m_begin + MAP_SIZE * i, 4, 1, fp);
    }

    for (i = 0; i < id_map->m_used_slot_count * sizeof(__u32) - (id_map->m_page_count - 1) * MAP_SIZE; i++) {
	fread ((char *)id_map->m_begin + MAP_SIZE * (id_map->m_page_count - 1) + i, 4, 1, fp);
    }

    fread (&v, 4, 1, fp);

    if (v != OBJMAP_END_MAGIC) {
	reiserfs_warning (stderr, "reiserfs_objectid_map_load: no objectid map end magic found");
	return 0;
    }

    fflush (stderr);
    return id_map;
}
*/