File: object.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 (244 lines) | stat: -rw-r--r-- 6,085 bytes parent folder | download | duplicates (7)
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
/*  Copyright 2001-2005 by Hans Reiser, licensing governed by 
    reiser4progs/COPYING.
    
    librepair/object.c - Object consystency recovery code. */

#include <repair/object.h>
#include <repair/item.h>


/* Check the semantic structure of the object. Mark all items as CHECKED. */
errno_t repair_object_check_struct(reiser4_object_t *object,
				   place_func_t place_func,
				   uint8_t mode, void *data) 
{
	errno_t res;
	
	aal_assert("vpf-1044", object != NULL);
	
	if ((res = plugcall(reiser4_psobj(object), check_struct, 
			    object, place_func, data, mode)) < 0)
	{
		return res;
	}
	
	aal_assert("vpf-1195", mode != RM_BUILD ||
			      !(res & RE_FATAL));
	
	aal_memcpy(&object->info.object, 
		   &object_start(object)->key,
		   sizeof(object->info.object));

	return res;
}

/* Create the fake object--needed for "/" and "lost+found" recovery when SD 
   is corrupted and not directory plugin gets realized. */
reiser4_object_t *repair_object_fake(reiser4_tree_t *tree, 
				     reiser4_object_t *parent,
				     reiser4_key_t *key,
				     reiser4_plug_t *plug) 
{
	reiser4_object_t *object;

	aal_assert("vpf-1247", tree != NULL);
	aal_assert("vpf-1248", key != NULL);
	aal_assert("vpf-1640", plug != NULL);

	if (!(object = aal_calloc(sizeof(*object), 0)))
		return INVAL_PTR;

	/* Initializing info */
	aal_memcpy(&object->info.object, key, sizeof(*key));
	
	object->info.tree = (tree_entity_t *)tree;
	object->info.pset.plug[PSET_OBJ] = plug;
	object->info.pset.plug_mask |= (1 << PSET_OBJ);

	if (parent) {
		aal_memcpy(&object->info.parent, 
			   &parent->info.object, 
			   sizeof(object->info.parent));
		
		/* Inherit from the parent. */
		if (plugcall(reiser4_psobj(object), inherit, 
			     &object->info, &parent->info))
		{
			return NULL;
		}
	} else {
		reiser4_pset_root(&object->info);
	}
	
	/* Create the fake object. */
	if (plugcall(reiser4_psobj(object), fake, object)) {
		aal_free(object);
		return NULL;
	}
	
	return object;
}

reiser4_object_t *repair_object_open(reiser4_tree_t *tree, 
				     reiser4_object_t *parent,
				     reiser4_place_t *place) 
{
	reiser4_object_t *object;
	errno_t res;
	
	aal_assert("vpf-1622", place != NULL);
	
	if (!(object = reiser4_object_prep(tree, parent, 
					   &place->key, place)))
	{
		return INVAL_PTR;
	}
	
	if ((res = plugcall(reiser4_psobj(object), recognize, object))) {
		aal_free(object);
		return res < 0 ? INVAL_PTR : NULL;
	}
	
	return object;
}

/* Open the object on the base of given start @key */
reiser4_object_t *repair_object_obtain(reiser4_tree_t *tree,
				       reiser4_object_t *parent,
				       reiser4_key_t *key)
{
	reiser4_object_t *object;
	reiser4_place_t place;
	lookup_hint_t hint;
	errno_t res;

	aal_assert("vpf-1132", tree != NULL);
	aal_assert("vpf-1134", key != NULL);

	hint.key = key;
	hint.level = LEAF_LEVEL;
	hint.collision = NULL;
	
	if (reiser4_tree_lookup(tree, &hint, FIND_EXACT, &place) != PRESENT)
		return NULL;
	
	if (!(object = reiser4_object_prep(tree, parent, key, &place))) {
		/* FIXME: object_init fails to initialize if found item 
		   is not SD, but this is not fatal error. */
		return NULL;
	}
	
	if ((res = plugcall(reiser4_psobj(object), recognize, object))) {
		aal_free(object);
		return res < 0 ? INVAL_PTR : NULL;
	}
	
	return object;
}

/* Checks the attach between @parent and @object */
errno_t repair_object_check_attach(reiser4_object_t *parent, 
				   reiser4_object_t *object, 
				   place_func_t place_func, 
				   void *data, uint8_t mode)
{
	aal_assert("vpf-1188", object != NULL);
	aal_assert("vpf-1099", parent != NULL);
	
	if (!reiser4_psobj(object)->check_attach)
		return 0;
	
	return plugcall(reiser4_psobj(object), check_attach, 
			object, parent, place_func, data, mode);
}

errno_t repair_object_mark(reiser4_object_t *object, uint16_t flag) {
	errno_t res;
	
	aal_assert("vpf-1270", object != NULL);
	
	/* Get the start place. */
	if ((res = reiser4_object_refresh(object))) {
		aal_error("Update of the object [%s] failed.",
			  reiser4_print_inode(&object->info.object));
		return res;
	}
	
	reiser4_item_set_flag(object_start(object), flag);
	
	return 0;
}

int repair_object_test(reiser4_object_t *object, uint16_t flag) {
	errno_t res;
	
	aal_assert("vpf-1273", object != NULL);
	
	/* Get the start place. */
	if ((res = reiser4_object_refresh(object))) {
		aal_error("Update of the object [%s] failed.",
			  reiser4_print_inode(&object->info.object));
		return res;
	}
	
	return reiser4_item_test_flag(object_start(object), flag);
}

errno_t repair_object_clear(reiser4_object_t *object, uint16_t flag) {
	errno_t res;
	
	aal_assert("vpf-1272", object != NULL);
	
	/* Get the start place. */
	if ((res = reiser4_object_refresh(object))) {
		aal_error("Update of the object [%s] failed.",
			  reiser4_print_inode(&object->info.object));
		return res;
	}
	
	reiser4_item_clear_flag(object_start(object), flag);
	
	return 0;
}

/* This method is intended to refresh all info needed for the futher work 
   with the object. For now it is only updating the parent pointer. 
   Should be called after repair_object_obtain, repair_object_check_sytict. */
errno_t repair_object_refresh(reiser4_object_t *object) {
	entry_hint_t entry;
	
	aal_assert("vpf-1271", object != NULL);

	if (!reiser4_psobj(object)->lookup)
		return 0;

	switch (plugcall(reiser4_psobj(object), lookup, object, "..", &entry))
	{
	case ABSENT:
		aal_memset(&object->info.parent, 0, 
			   sizeof(object->info.parent));
		break;
	case PRESENT:
		aal_memcpy(&object->info.parent, 
			   &entry.object, sizeof(entry.object));
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

/* Helper function for printing passed @place into @stream. */
static errno_t cb_print_place(reiser4_place_t *place, void *data) {
	aal_stream_t *stream = (aal_stream_t *)data;
	
	repair_item_print(place, stream);
	aal_stream_write(stream, "\n", 1);
	return 0;
}

/* Prints object items into passed stream */
void repair_object_print(reiser4_object_t *object, aal_stream_t *stream) {
	reiser4_object_metadata(object, cb_print_place, stream);
}