File: lost%2Bfound.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 (422 lines) | stat: -rw-r--r-- 11,235 bytes parent folder | download | duplicates (4)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright 2000-2004 by Hans Reiser, licensing governed by
 * reiserfsprogs/README
 */

#include "fsck.h"

void erase_name(int len);
void print_name(char *name, int len);

void modify_item(struct item_head *ih, void *item);

/* fixme: search_by_key is not needed after any add_entry */
static __u64 _look_for_lost(reiserfs_filsys_t fs, int link_lost_dirs)
{
	struct reiserfs_key key, prev_key, *rdkey;
	INITIALIZE_REISERFS_PATH(path);
	int item_pos;
	struct buffer_head *bh;
	struct item_head *ih;
	unsigned long leaves;
	int is_it_dir;
	static int lost_files = 0;	/* looking for lost dirs we calculate amount of
					   lost files, so that when we will look for
					   lost files we will be able to stop when
					   there are no lost files anymore */
	int retval;
	__u64 size;

	key = root_dir_key;

	if (!link_lost_dirs && !lost_files) {
		/* we have to look for lost files but we know already that there are
		   no any */
		return 0;
	}

	fsck_progress("Looking for lost %s:\n",
		      link_lost_dirs ? "directories" : "files");
	leaves = 0;

	/* total size of added entries */
	size = 0;
	while (1) {
		retval = reiserfs_search_by_key_4(fs, &key, &path);
		/* fixme: we assume path ends up with a leaf */
		bh = get_bh(&path);
		item_pos = get_item_pos(&path);
		if (retval != ITEM_FOUND) {
			if (item_pos == get_blkh_nr_items(B_BLK_HEAD(bh))) {
				rdkey = uget_rkey(&path);
				if (!rdkey) {
					pathrelse(&path);
					break;
				}
				key = *rdkey;
				pathrelse(&path);
				continue;
			}
			/* we are on the item in the buffer */
		}

		/* print ~ how many leaves were scanned and how fast it was */
		if (!fsck_quiet(fs))
			print_how_fast(leaves++, 0, 50, 0);

		for (ih = tp_item_head(&path);
		     item_pos < get_blkh_nr_items(B_BLK_HEAD(bh));
		     item_pos++, ih++, PATH_LAST_POSITION(&path)++) {
			if (is_item_reachable(ih))
				continue;

			/* found item which can not be reached */
			if (!is_direntry_ih(ih) && !is_stat_data_ih(ih)) {
				continue;
			}

			if (is_direntry_ih(ih)) {
				/* if this directory has no stat data - try to recover it */
				struct reiserfs_key sd;
				struct reiserfs_path tmp;

				sd = ih->ih_key;
				set_type_and_offset(KEY_FORMAT_1, &sd,
						    SD_OFFSET, TYPE_STAT_DATA);
				if (reiserfs_search_by_key_4(fs, &sd, &tmp) ==
				    ITEM_FOUND) {
					/* should not happen - because if there were a stat data -
					   we would have done with the whole directory */
					pathrelse(&tmp);
					continue;
				}
				lost_found_pass_stat(fs)->dir_recovered++;
				create_dir_sd(fs, &tmp, &sd, modify_item);
				id_map_mark(proper_id_map(fs),
					    get_key_objectid(&sd));
				key = sd;
				pathrelse(&path);
				goto cont;
			}

			/* stat data marked "not having name" found */
			if (is_stat_data_ih(tp_item_head(&path)))
				fix_obviously_wrong_sd_mode(&path);

			is_it_dir =
			    ((not_a_directory(ih_item_body(bh, ih))) ? 0 : 1);

			if (is_it_dir) {
				struct reiserfs_key tmp_key;
				INITIALIZE_REISERFS_PATH(tmp_path);
				struct item_head *tmp_ih;

				/* there is no need to link empty lost directories into /lost+found */
				tmp_key = ih->ih_key;
				set_type_and_offset(KEY_FORMAT_1, &tmp_key,
						    0xffffffff, TYPE_DIRENTRY);
				reiserfs_search_by_key_4(fs, &tmp_key,
							 &tmp_path);
				tmp_ih = tp_item_head(&tmp_path);
				tmp_ih--;
				if (not_of_one_file(&tmp_key, tmp_ih))
					reiserfs_panic("not directory found");
				if (!is_direntry_ih(tmp_ih) ||
				    (get_deh_offset
				     (B_I_DEH(get_bh(&tmp_path), tmp_ih) +
				      get_ih_entry_count(tmp_ih) - 1) ==
				     DOT_DOT_OFFSET)) {
					/* last directory item is either stat data or empty
					   directory item - do not link this dir into lost+found */
					lost_found_pass_stat(fs)->
					    empty_lost_dirs++;
					pathrelse(&tmp_path);
					continue;
				}
				pathrelse(&tmp_path);
			}

			if (link_lost_dirs && !is_it_dir) {
				/* we are looking for directories and it is not a dir */
				lost_files++;
				continue;
			}

			lost_found_pass_stat(fs)->lost_found++;

			{
				struct reiserfs_key obj_key =
				    { 0, 0, {{0, 0},} };
				char *lost_name;
				struct item_head tmp_ih;

				/* key to continue */
				key = ih->ih_key;
				set_key_objectid(&key,
						 get_key_objectid(&key) + 1);

				tmp_ih = *ih;
				if (id_map_test
				    (semantic_id_map(fs),
				     get_key_objectid(&ih->ih_key))) {
					/* objectid is used, relocate an object */
					lost_found_pass_stat(fs)->oid_sharing++;

					if (is_it_dir) {
						relocate_dir(&tmp_ih, 1);
						lost_found_pass_stat(fs)->
						    oid_sharing_dirs_relocated++;
					} else {
						rewrite_file(&tmp_ih, 1, 1);
						lost_found_pass_stat(fs)->
						    oid_sharing_files_relocated++;
					}

					linked_already(&tmp_ih.ih_key);
				} else {
					if (!is_it_dir)
						id_map_mark(semantic_id_map(fs),
							    get_key_objectid
							    (&ih->ih_key));
				}

				asprintf(&lost_name, "%u_%u",
					 get_key_dirid(&tmp_ih.ih_key),
					 get_key_objectid(&tmp_ih.ih_key));

				/* entry in lost+found directory will point to this key */
				set_key_dirid(&obj_key,
					      get_key_dirid(&tmp_ih.ih_key));
				set_key_objectid(&obj_key,
						 get_key_objectid(&tmp_ih.
								  ih_key));

				pathrelse(&path);

				/* 0 does not mean anyting - item with "." and ".." already
				   exists and reached, so only name will be added */
				size +=
				    reiserfs_add_entry(fs, &lost_found_dir_key,
						       lost_name,
						       name_length(lost_name,
								   lost_found_dir_format),
						       &obj_key,
						       0 /*fsck_need */ );

				if (is_it_dir) {
					/* fixme: we hope that if we will try to pull all the
					   directory right now - then there will be less
					   lost_found things */
					print_name(lost_name,
						   strlen(lost_name));
					/*fsck_progress ("\tChecking lost dir \"%s\":", lost_name); */
					rebuild_semantic_pass(&obj_key,
							      &lost_found_dir_key,
							      /*dot_dot */ 0,
							      /*reloc_ih */ NULL);
					erase_name(strlen(lost_name));
					/*fsck_progress ("finished\n"); */

					lost_found_pass_stat(fs)->
					    lost_found_dirs++;
				} else {
					if (reiserfs_search_by_key_4
					    (fs, &obj_key, &path) != ITEM_FOUND)
						reiserfs_panic
						    ("look_for_lost: lost file stat data %K not found",
						     &obj_key);

					/* check_regular_file does not mark stat data reachable */
					mark_item_reachable(tp_item_head(&path),
							    get_bh(&path));

					rebuild_check_regular_file(&path,
								   tp_item_body
								   (&path),
								   NULL
								   /*reloc_ih */
								   );
					pathrelse(&path);

					lost_found_pass_stat(fs)->
					    lost_found_files++;
					lost_files--;
				}

				free(lost_name);
				goto cont;
			}
		}		/* for */

		prev_key = key;

		PATH_LAST_POSITION(&path) = item_pos - 1;
		rdkey = reiserfs_next_key(&path);
		if (rdkey)
			key = *rdkey;
		else
			break;

		if (comp_keys(&prev_key, &key) != -1)
			reiserfs_panic
			    ("pass_3a: key must grow 2: prev=%k next=%k",
			     &prev_key, &key);
		pathrelse(&path);

cont:
		if (!link_lost_dirs && !lost_files) {
			break;
		}
	}

	pathrelse(&path);

#if 0
	/* check names added we just have added to/lost+found. Those names are
	   marked DEH_Lost_found flag */
	fsck_progress("Checking lost+found directory..");
	fflush(stdout);
	check_semantic_tree(&lost_found_dir_key, &root_dir_key, 0,
			    1 /* lost+found */ );
	fsck_progress("finished\n");
#endif

	return size;

}

static void save_lost_found_result(reiserfs_filsys_t fs)
{
	FILE *file;
	int retval;

	/* save bitmaps with which we will be able start reiserfs from
	   pass 1 */
	file = open_file("temp_fsck_file.deleteme", "w+");
	if (!file)
		return;

	reiserfs_begin_stage_info_save(file, LOST_FOUND_DONE);
	reiserfs_end_stage_info_save(file);
	close_file(file);

	retval = rename("temp_fsck_file.deleteme", state_dump_file(fs));
	if (retval != 0)
		fsck_progress
		    ("pass 0: Could not rename the temporary file temp_fsck_file.deleteme to %s",
		     state_dump_file(fs));
}

/* we have nothing to load from a state file, but we have to fetch
   on-disk bitmap, copy it to allocable bitmap, and fetch objectid
   map */
void load_lost_found_result(reiserfs_filsys_t fs)
{
	fsck_new_bitmap(fs) =
	    reiserfs_create_bitmap(get_sb_block_count(fs->fs_ondisk_sb));
	reiserfs_bitmap_copy(fsck_new_bitmap(fs), fs->fs_bitmap2);

	fsck_allocable_bitmap(fs) =
	    reiserfs_create_bitmap(get_sb_block_count(fs->fs_ondisk_sb));
	reiserfs_bitmap_copy(fsck_allocable_bitmap(fs), fs->fs_bitmap2);

	fs->block_allocator = reiserfsck_reiserfs_new_blocknrs;
	fs->block_deallocator = reiserfsck_reiserfs_free_block;

	/* we need objectid map on semantic pass to be able to relocate files */
	proper_id_map(fs) = id_map_init();
	/* Not implemented yet.
	   fetch_objectid_map (proper_id_map (fs), fs);
	 */
}

static void after_lost_found(reiserfs_filsys_t fs)
{
	int ret;
	/* update super block: objectid map, fsck state */
	set_sb_fs_state(fs->fs_ondisk_sb, LOST_FOUND_DONE);
	mark_buffer_dirty(fs->fs_super_bh);

	/* write all dirty blocks */
	fsck_progress("Flushing..");
	id_map_flush(proper_id_map(fs), fs);
	fs->fs_dirt = 1;
	ret = reiserfs_flush_to_ondisk_bitmap(fsck_new_bitmap(fs), fs);
	if (ret < 0)
		reiserfs_exit(1, "Exiting after unrecoverable error.");
	reiserfs_flush(fs);
	fsck_progress("finished\n");

	stage_report(0x3a, fs);

	if (!fsck_run_one_step(fs)) {
		if (fsck_user_confirmed(fs, "Continue? (Yes):", "Yes\n", 1))
			/* reiserfsck continues */
			return;
	}

	save_lost_found_result(fs);

	id_map_free(proper_id_map(fs));
	proper_id_map(fs) = NULL;

	fs->fs_dirt = 1;
	reiserfs_close(fs);
	exit(EXIT_OK);
}

void pass_3a_look_for_lost(reiserfs_filsys_t fs)
{
	INITIALIZE_REISERFS_PATH(path);
	struct item_head *ih;
	void *sd;
	__u64 size, sd_size;
	__u32 blocks;
	__u16 mode;
	__u32 objectid;
	unsigned int gen_counter;
	fsck_progress("Pass 3a (looking for lost dir/files):\n");

	/* when warnings go not to stderr - separate them in the log */
	if (fsck_log_file(fs) != stderr)
		fsck_log("####### Pass 3a (lost+found pass) #########\n");

	/* look for lost dirs first */
	size = _look_for_lost(fs, 1);

	/* link files which are still lost */
	size += _look_for_lost(fs, 0);

	/* update /lost+found sd_size and sd_blocks (nlink is correct already) */

	objectid = reiserfs_find_entry(fs, &root_dir_key, "lost+found",
				       &gen_counter, &lost_found_dir_key);

	if (!objectid) {
		reiserfs_panic
		    ("look_for_lost: The entry 'lost+found' could not be found in the root directory.");
	}

	if (reiserfs_search_by_key_4(fs, &lost_found_dir_key, &path) !=
	    ITEM_FOUND)
		reiserfs_panic
		    ("look_for_lost: The StatData of the 'lost+found' directory %K could not be found",
		     &lost_found_dir_key);
	ih = tp_item_head(&path);
	sd = tp_item_body(&path);
	get_sd_size(ih, sd, &sd_size);
	size += sd_size;
	blocks = dir_size2st_blocks(size);

	set_sd_size(ih, sd, &size);
	set_sd_blocks(ih, sd, &blocks);

	/* make lost+found to be drwx------ */
	mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
	set_sd_mode(ih, sd, &mode);

	mark_buffer_dirty(get_bh(&path));
	pathrelse(&path);

	after_lost_found(fs);
}