File: dir40.c

package info (click to toggle)
reiser4progs 1.2.2-1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 5,904 kB
  • sloc: ansic: 34,331; sh: 4,251; makefile: 994
file content (652 lines) | stat: -rw-r--r-- 16,469 bytes parent folder | download | duplicates (8)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
   reiser4progs/COPYING.
   
   dir40.c -- reiser4 directory object plugin. */

#ifndef ENABLE_MINIMAL
#  include <unistd.h>
#endif

#include "dir40.h"
#include "dir40_repair.h"

/* Return current position in directory into passed @offset. */
static errno_t dir40_telldir(reiser4_object_t *dir,
			     reiser4_key_t *position)
{
	aal_assert("umka-1985", dir != NULL);
	aal_assert("umka-1986", position != NULL);

	/* Getting current dir key and adjust. */
	aal_memcpy(position, &dir->position, sizeof(*position));

#ifndef ENABLE_MINIMAL
	/* Adjust is offset inside collided keys arrays and needed for
	   positioning right in such a case. In normal case it is zero. */
	position->adjust = dir->position.adjust;
#endif
	
	return 0;
}

/* Positioning inside directory by passed @position key. Normally, user should use
   key got from telldir() function. But, this is possible to generate directory
   key by himself and pass here. */
static errno_t dir40_seekdir(reiser4_object_t *dir,
			     reiser4_key_t *position)
{
	aal_assert("umka-1983", dir != NULL);
	aal_assert("umka-1984", position != NULL);

	/* Set directory position to the given one. */
	aal_memcpy(&dir->position, position, sizeof(*position));
	return 0;
}

/* Resets current direntry position to zero. */
errno_t dir40_reset(reiser4_object_t *dir) {
	aal_assert("umka-864", dir != NULL);
	
	/* Preparing key of the first entry in directory and set directory
	   adjust to zero. */
#ifndef ENABLE_MINIMAL
	dir->position.adjust = 0;
#endif

	/* Building key itself. */
	plugcall(dir->info.object.plug, build_hashed, &dir->position,
		  reiser4_pshash(dir), reiser4_psfibre(dir), 
		  objcall(&dir->info.object, get_locality),
		  objcall(&dir->info.object, get_objectid), ".");

	return 0;
}

/* Fetches current unit to passed @entry */
errno_t dir40_fetch(reiser4_object_t *dir, entry_hint_t *entry) {
	trans_hint_t hint;

	aal_memset(&hint, 0, sizeof(hint));
	hint.count = 1;
	hint.specific = entry;
	hint.shift_flags = SF_DEFAULT;

	/* Reading entry to passed @entry */
	if (objcall(&dir->body, object->fetch_units, &hint) != 1)
		return -EIO;
	
	/* Copying entry place. */
	aal_memcpy(&entry->place, &dir->body,
		   sizeof(reiser4_place_t));

	return 0;
}

#ifndef ENABLE_MINIMAL
static void dir40_entry_type(entry_hint_t *entry) {
	entry->type = ET_NAME;

	if (aal_strlen(entry->name) == 1 &&
	    !aal_strncmp(entry->name, ".", 1))
	{
		entry->type = ET_SPCL;
	}

	if (aal_strlen(entry->name) == 2 &&
	    !aal_strncmp(entry->name, "..",2))
	{
		entry->type = ET_SPCL;
	}
}
#else
#define dir40_entry_type(entry) do{;} while(0)
#endif

errno_t dir40_entry_comp(reiser4_object_t *dir, void *data) {
	entry_hint_t entry;
	reiser4_key_t *key;

	aal_assert("vpf-1834", dir != NULL);

	if (!dir->body.plug)
		return -EINVAL;
#ifndef EINVAL	
	if (dir->body.plug->id.group != DIR_ITEM)
		return -ESTRUCT;
#endif
	if (dir40_fetch(dir, &entry))
		return -EIO;

	if (!data) {
		key = &dir->position;
	} else {
		key = (reiser4_key_t *)data;
	}
	
	/* If greater key is reached, return PRESENT. */
	return objcall(&entry.offset, compfull, key) ? 1 : 0;
}

/* Reads one current directory entry to passed @entry hint. Returns count of
   read entries, zero for the case directory is over and nagtive values fopr
   errors. */
static int32_t dir40_readdir(reiser4_object_t *dir, 
			     entry_hint_t *entry)
{
	uint32_t units;
	errno_t res;

	aal_assert("umka-845", entry != NULL);
	aal_assert("umka-844", dir != NULL);

	/* Getting place of current unit */
	if ((res = obj40_update_body(dir, dir40_entry_comp)) != PRESENT)
		return res == ABSENT ? 0 : res;

	/* Reading next entry */
	if ((res = dir40_fetch(dir, entry)))
		return res;

	/* Setting up the entry type. It is essential for fsck to know what is
	   the NAME -- that needs to be traversed semantically to be recovered
	   completely -- and what is not -- that needs some other special
	   actions, e.g. check_attach for ".." (and even "..." if it is needed
	   one day), etc. */
	dir40_entry_type(entry);

	units = objcall(&dir->body, balance->units);

	/* Getting next entry in odrer to set up @dir->position correctly. */
	if (++dir->body.pos.unit >= units) {
		/* Switching to the next directory item */
		if ((res = obj40_next_item(dir)) < 0)
			return res;

		if (res == ABSENT) {
			uint64_t offset;
			
			/* Set offset to non-existent value. */
			offset = objcall(&dir->position, get_offset);
			objcall(&dir->position, set_offset, offset + 1);
		}
	} else {
		/* There is no need to switch */
		res = 1;
	}

	if (res == 1) {
		entry_hint_t temp;
		
		if ((res = dir40_fetch(dir, &temp)))
			return res;

#ifndef ENABLE_MINIMAL
		/* Taking care about adjust */
		if (!objcall(&temp.offset, compfull, &dir->position))
			temp.offset.adjust = dir->position.adjust + 1;
		else
			temp.offset.adjust = 0;
#endif
		dir40_seekdir(dir, &temp.offset);
	}
	
	return 1;
}

/* Makes lookup inside directory. This is needed to be used in add_entry() for
   two reasons: for make sure, that passed entry does not exists and to use
   lookup result for consequent insert. */
static lookup_t dir40_search(reiser4_object_t *dir, char *name,
			     lookup_bias_t bias, entry_hint_t *entry)
{
	lookup_t res;
#ifndef ENABLE_MINIMAL
	coll_hint_t hint;
	coll_func_t func;
#endif

	aal_assert("umka-1118", name != NULL);
	aal_assert("umka-1117", dir != NULL);

	/* Preparing key to be used for lookup. It is generating from the
	   directory oid, locality and name by menas of using hash plugin. */
	plugcall(dir->info.object.plug, build_hashed, &dir->body.key,
		 reiser4_pshash(dir), reiser4_psfibre(dir),
		 objcall(&dir->info.object, get_locality),
		 objcall(&dir->info.object, get_objectid), name);

#ifndef ENABLE_MINIMAL
	hint.specific = name;
	hint.type = DIR_ITEM;
	func = obj40_core->tree_ops.collision;
#endif
	
	if ((res = obj40_find_item(dir, &dir->body.key, bias, 
#ifndef ENABLE_MINIMAL
				   func, &hint,
#else
				   NULL, NULL,
#endif
				   &dir->body)) < 0)
	{
		return res;
	}

	if (entry) {
		aal_memset(entry, 0, sizeof(*entry));
		
		aal_memcpy(&entry->place, &dir->body,
			   sizeof(reiser4_place_t));

		aal_memcpy(&entry->offset, &dir->body.key,
			   sizeof(reiser4_key_t));

		if (res == PRESENT) {
			if (dir40_fetch(dir, entry))
				return -EIO;

			dir40_entry_type(entry);
		}
	}

	return res;
}

/* Makes lookup inside @dir by passed @name. Saves found entry in passed
   @entry hint. */
lookup_t dir40_lookup(reiser4_object_t *dir,
		      char *name, entry_hint_t *entry) 
{
	return dir40_search(dir, name, FIND_EXACT, entry);
}

#ifndef ENABLE_MINIMAL
/* Creates dir40 instance. Creates its stat data item, and body item with one
   "." unit. Yet another unit ".." will be inserted latter, then directiry will
   be attached to a parent object. */
static errno_t dir40_create(reiser4_object_t *dir, object_hint_t *hint) {
	trans_hint_t body_hint;
	entry_hint_t entry;
	reiser4_key_t *key;
	uint32_t mode;
	errno_t res;
    
	aal_assert("vpf-1816",  dir != NULL);
	aal_assert("vpf-1095",  dir->info.tree != NULL);

	aal_memset(&body_hint, 0, sizeof(body_hint));
	
	/* Initializing direntry item hint. This should be done before the stat
	   data item hint, because we will need size of direntry item during
	   stat data initialization. */
   	body_hint.count = 1;
	body_hint.plug = reiser4_psdiren(dir);
	
	key = &dir->info.object;
	plugcall(key->plug, build_hashed, &body_hint.offset, 
		 reiser4_pshash(dir), reiser4_psfibre(dir),
		 objcall(&dir->info.object, get_locality),
		 objcall(&dir->info.object, get_objectid), ".");

	/* Preparing hint for the empty directory. It consists only "." for
	   unlinked directories. */
	aal_strncpy(entry.name, ".", 1);

	/* Initializing entry stat data key. */
	aal_memcpy(&entry.object, key, sizeof(*key));

	/* Initializing entry hash key. */
	aal_memcpy(&entry.offset, &body_hint.offset, sizeof(entry.offset));

	body_hint.specific = &entry;
	body_hint.shift_flags = SF_DEFAULT;
	
	dir40_reset(dir);
	
        /* Looking for place to insert directory body */
	switch (obj40_find_item(dir, &body_hint.offset,
				FIND_CONV, NULL, NULL, &dir->body))
	{
	case ABSENT:
		/* Inserting the direntry body item into the tree. */
		if ((res = obj40_insert(dir, &dir->body,
					&body_hint, LEAF_LEVEL)) < 0)
		{
			return res;
		}
		
		break;
	default:
		return -EIO;
	}
	
	mode = (hint ? hint->mode : 0) | S_IFDIR | 0755;
	
	/* Create stat data item. */
	if ((res = obj40_create_stat(dir, 1, body_hint.len, 
				     0, 1, mode, NULL))) 
	{
	
		/* Removing body item. */	
		if (obj40_update_body(dir, dir40_entry_comp) == PRESENT) {
			body_hint.count = 1;
			body_hint.place_func = NULL;
			body_hint.region_func = NULL;
			dir->body.pos.unit = MAX_UINT32;
			
			obj40_remove(dir, &dir->body, &body_hint);
		}
		
		return res;
	}
	
	return 0;
}

/* Removes all directory body items. */
static errno_t dir40_mkempty(reiser4_object_t *dir, uint64_t n) {
	errno_t res;
	reiser4_key_t key;

	aal_assert("umka-1925", dir != NULL);

	/* Making sure, that dir->body points to correct item */
	if ((res = obj40_update_body(dir, dir40_entry_comp)) != PRESENT)
		return res  == ABSENT ? 0 : res;

	/* Creating maximal possible key in order to find last directory item
	   and remove it from the tree. Thanks to Nikita for this idea. */
	aal_memcpy(&key, &dir->body.key, sizeof(key));
	objcall(&key, set_offset, MAX_UINT64);

	while (1) {
		trans_hint_t hint;

		/* Looking for the last directory item. 
		   FIXME: the possible speedup here is to get rid of 
		   lookup */
		if ((res = obj40_find_item(dir, &key, FIND_EXACT, NULL, 
					   NULL, &dir->body)) < 0)
		{
			return res;
		}

		/* Checking if found item belongs this directory */
		if (obj40_belong(&dir->body, &key) == ABSENT)
			return 0;

		aal_memset(&hint, 0, sizeof(hint));
		
		hint.count = 1;
		hint.shift_flags = SF_DEFAULT;
		dir->body.pos.unit = MAX_UINT32;
		
		/* Removing item from the tree */
		if ((res = obj40_remove(dir, &dir->body, &hint)))
			return res;
	}
	
	return 0;
}

/* Removes directory body and stat data from the tree. */
static errno_t dir40_clobber(reiser4_object_t *dir) {
	uint32_t nlink;
	errno_t res;
		
	aal_assert("umka-2298", dir != NULL);
	
	/* Check that truncate is allowed -- i.e. nlink == 2. */
	if ((nlink = obj40_get_nlink(dir, 1)) != 2) {
		aal_error("Can't detach the object "
			  "with nlink (%d).", nlink);
		return -EINVAL;
	}

	dir40_reset(dir);
	
	/* Truncates directory body. */
	if ((res = dir40_mkempty(dir, 0)))
		return res;

	/* Cloberring stat data. */
	return obj40_clobber(dir);
}

/* Return number of hard links. */
static bool_t dir40_linked(reiser4_object_t *dir) {
	return obj40_get_nlink(dir, 1) != 1;
}

/* Helper function. Builds @entry->offset key by @entry->name. */
static errno_t dir40_build_entry(reiser4_object_t *dir, 
				 entry_hint_t *entry)
{
	aal_assert("umka-2528", entry != NULL);
	aal_assert("umka-2527", dir != NULL);
	
	plugcall(dir->info.object.plug, build_hashed, &entry->offset, 
		 reiser4_pshash(dir), reiser4_psfibre(dir),
		 objcall(&dir->info.object, get_locality),
		 objcall(&dir->info.object, get_objectid), entry->name);

	return 0;
}

/* Add new entry to directory. */
static errno_t dir40_add_entry(reiser4_object_t *dir, 
			       entry_hint_t *entry)
{
	errno_t res;
	
	entry_hint_t temp;
	trans_hint_t hint;

	aal_assert("umka-844", dir != NULL);
	aal_assert("umka-845", entry != NULL);

	aal_memset(&hint, 0, sizeof(hint));
	
	/* Getting place new entry will be inserted at. */
	switch (dir40_search(dir, entry->name,
			     FIND_EXACT, &temp))
	{
	case ABSENT:
		if (!obj40_valid_item(&temp.place))
			return -EINVAL;
		
		if ((res = obj40_fetch_item(&temp.place)))
			return res;
		
		break;
	case PRESENT:
		aal_error("Entry \"%s\" already exists.",
			  entry->name);
	default:
		return -EINVAL;
	}

	/* Prepare trans hint. */
	hint.count = 1;
	hint.region_func = NULL;
	hint.place_func = entry->place_func;
	hint.data = entry->data;
	
	hint.plug = temp.place.plug;
	hint.specific = (void *)entry;
	hint.shift_flags = SF_DEFAULT;

	/* Building key of the new entry and hint's one */
	dir40_build_entry(dir, entry);

	/* Copying key to @hint */
	aal_memcpy(&hint.offset, &entry->offset, sizeof(hint.offset));

	/* Inserting entry described by @hint to tree at @temp.place */
	if ((res = obj40_insert(dir, &temp.place,
				&hint, LEAF_LEVEL)) < 0)
	{
		return res;
	}

	entry->len = hint.len;
	
	/* Updating stat data fields. */
	return obj40_touch(dir, 1, hint.bytes);
}

/* Removing entry from the directory */
static errno_t dir40_rem_entry(reiser4_object_t *dir,
			       entry_hint_t *entry)
{
	errno_t res;
	entry_hint_t temp;
	trans_hint_t hint;
	
	aal_assert("umka-1923", entry != NULL);
	aal_assert("umka-1922", dir != NULL);
	aal_assert("umka-2390", entry->name != NULL);

	/* Looking for place to insert directory entry */
	if (dir40_search(dir, entry->name, FIND_EXACT, &temp) != PRESENT)
		return -EINVAL;
	
	aal_memset(&hint, 0, sizeof(hint));
	
	hint.count = 1;
	hint.shift_flags = SF_DEFAULT;
	
	/* Removing one unit from directory */
	if ((res = obj40_remove(dir, &dir->body, &hint)))
		return res;
	
	if (!objcall(&dir->position, compfull, &temp.offset)) {
		if (entry->offset.adjust < dir->position.adjust)
			dir->position.adjust--;
	}

	entry->len = hint.len;
	
	/* Updating stat data fields */
	return obj40_touch(dir, -1, -hint.bytes);
}

/* Attaches the given directory @dir to @parent object. */
static errno_t dir40_attach(reiser4_object_t *dir,
			    reiser4_object_t *parent)
{
	errno_t res;
	entry_hint_t entry;
	
	aal_assert("umka-2289", dir != NULL);
	aal_assert("umka-2359", parent != NULL);

	aal_memset(&entry, 0, sizeof(entry));
	
	aal_strncpy(entry.name, "..", sizeof(entry.name));

	/* Adding ".." pointing to parent to @dir object. */
	aal_memcpy(&entry.object, &parent->info.object, sizeof(entry.object));

	if ((res = dir40_add_entry(dir, &entry)))
		return res;

	/* Increasing parent's @nlink by one */
	return plugcall(reiser4_psobj(parent), link, parent);
}

/* Detaches @dir from @parent. */
static errno_t dir40_detach(reiser4_object_t *dir,
			    reiser4_object_t *parent)
{
	entry_hint_t entry;
	errno_t res;

	aal_assert("umka-2291", dir != NULL);

	/* Removing ".." from child if it is found */
	if (dir40_lookup(dir, "..", &entry) == PRESENT) {
		if ((res = dir40_rem_entry(dir, &entry)))
			return res;
	}
	
	if (!parent) 
		return 0;
	
	/* Decreasing parent's @nlink by one */
	return plugcall(reiser4_psobj(parent), unlink, parent);
}

/* This fucntion implements hashed directory enumerator function. It is used for
   calculating fargmentation, prining. */
static errno_t dir40_layout(reiser4_object_t *dir,
			    region_func_t func,
			    void *data)
{
	dir40_reset(dir);
	return obj40_layout(dir, func, dir40_entry_comp, data);
}

/* This fucntion implements hashed directory metadata enumerator function. This
   is needed for getting directory metadata for pack them, etc. */
static errno_t dir40_metadata(reiser4_object_t *dir,
			      place_func_t place_func,
			      void *data)
{
	dir40_reset(dir);
	return obj40_traverse(dir, place_func, dir40_entry_comp, data);
}
#endif

/* Directory object operations. */
reiser4_object_plug_t dir40_plug = {
	.p = {
		.id    = {OBJECT_DIR40_ID, DIR_OBJECT, OBJECT_PLUG_TYPE},
#ifndef ENABLE_MINIMAL
		.label = "dir40",
		.desc  = "Directory file plugin.",
#endif
	},

#ifndef ENABLE_MINIMAL
	.inherit	= obj40_inherit,
	.create		= dir40_create,
	.layout		= dir40_layout,
	.metadata	= dir40_metadata,
	.link		= obj40_link,
	.unlink		= obj40_unlink,
	.linked		= dir40_linked,
	.truncate	= NULL,
	.add_entry	= dir40_add_entry,
	.rem_entry	= dir40_rem_entry,
	.build_entry    = dir40_build_entry,
	.attach		= dir40_attach,
	.detach		= dir40_detach,
	.clobber	= dir40_clobber,
	.recognize	= obj40_recognize,
	.fake		= dir40_fake,
	.check_struct	= dir40_check_struct,
	.check_attach	= dir40_check_attach,

	.seek		= NULL,
	.write		= NULL,
	.convert        = NULL,
#endif
	.follow		= NULL,
	.read		= NULL,
	.offset		= NULL,
	
	.stat           = obj40_load_stat,
	.open		= obj40_open,
	.close		= NULL,
	.reset		= dir40_reset,
	.lookup		= dir40_lookup,
	.seekdir	= dir40_seekdir,
	.readdir	= dir40_readdir,
	.telldir	= dir40_telldir,

#ifndef ENABLE_MINIMAL
	.sdext_mandatory = (1 << SDEXT_LW_ID),
	.sdext_unknown   = (1 << SDEXT_SYMLINK_ID)
#endif
};