Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3 */
  4
  5#include <linux/time.h>
  6#include <linux/reiserfs_fs.h>
  7
  8// this contains item handlers for old item types: sd, direct,
  9// indirect, directory
 
 
 10
 11/* and where are the comments? how about saying where we can find an
 12   explanation of each item handler method? -Hans */
 
 
 13
 14//////////////////////////////////////////////////////////////////////////////
 15// stat data functions
 16//
 17static int sd_bytes_number(struct item_head *ih, int block_size)
 18{
 19	return 0;
 20}
 21
 22static void sd_decrement_key(struct cpu_key *key)
 23{
 24	key->on_disk_key.k_objectid--;
 25	set_cpu_key_k_type(key, TYPE_ANY);
 26	set_cpu_key_k_offset(key, (loff_t)(~0ULL >> 1));
 27}
 28
 29static int sd_is_left_mergeable(struct reiserfs_key *key, unsigned long bsize)
 30{
 31	return 0;
 32}
 33
 34static char *print_time(time_t t)
 35{
 36	static char timebuf[256];
 37
 38	sprintf(timebuf, "%ld", t);
 39	return timebuf;
 40}
 41
 42static void sd_print_item(struct item_head *ih, char *item)
 43{
 44	printk("\tmode | size | nlinks | first direct | mtime\n");
 45	if (stat_data_v1(ih)) {
 46		struct stat_data_v1 *sd = (struct stat_data_v1 *)item;
 47
 48		printk("\t0%-6o | %6u | %2u | %d | %s\n", sd_v1_mode(sd),
 49		       sd_v1_size(sd), sd_v1_nlink(sd),
 50		       sd_v1_first_direct_byte(sd),
 51		       print_time(sd_v1_mtime(sd)));
 52	} else {
 53		struct stat_data *sd = (struct stat_data *)item;
 54
 55		printk("\t0%-6o | %6Lu | %2u | %d | %s\n", sd_v2_mode(sd),
 56		       (unsigned long long)sd_v2_size(sd), sd_v2_nlink(sd),
 57		       sd_v2_rdev(sd), print_time(sd_v2_mtime(sd)));
 58	}
 59}
 60
 61static void sd_check_item(struct item_head *ih, char *item)
 62{
 63	// FIXME: type something here!
 64}
 65
 66static int sd_create_vi(struct virtual_node *vn,
 67			struct virtual_item *vi,
 68			int is_affected, int insert_size)
 69{
 70	vi->vi_index = TYPE_STAT_DATA;
 71	//vi->vi_type |= VI_TYPE_STAT_DATA;// not needed?
 72	return 0;
 73}
 74
 75static int sd_check_left(struct virtual_item *vi, int free,
 76			 int start_skip, int end_skip)
 77{
 78	BUG_ON(start_skip || end_skip);
 79	return -1;
 80}
 81
 82static int sd_check_right(struct virtual_item *vi, int free)
 83{
 84	return -1;
 85}
 86
 87static int sd_part_size(struct virtual_item *vi, int first, int count)
 88{
 89	BUG_ON(count);
 90	return 0;
 91}
 92
 93static int sd_unit_num(struct virtual_item *vi)
 94{
 95	return vi->vi_item_len - IH_SIZE;
 96}
 97
 98static void sd_print_vi(struct virtual_item *vi)
 99{
100	reiserfs_warning(NULL, "reiserfs-16100",
101			 "STATDATA, index %d, type 0x%x, %h",
102			 vi->vi_index, vi->vi_type, vi->vi_ih);
103}
104
105static struct item_operations stat_data_ops = {
106	.bytes_number = sd_bytes_number,
107	.decrement_key = sd_decrement_key,
108	.is_left_mergeable = sd_is_left_mergeable,
109	.print_item = sd_print_item,
110	.check_item = sd_check_item,
111
112	.create_vi = sd_create_vi,
113	.check_left = sd_check_left,
114	.check_right = sd_check_right,
115	.part_size = sd_part_size,
116	.unit_num = sd_unit_num,
117	.print_vi = sd_print_vi
118};
119
120//////////////////////////////////////////////////////////////////////////////
121// direct item functions
122//
123static int direct_bytes_number(struct item_head *ih, int block_size)
124{
125	return ih_item_len(ih);
126}
127
128// FIXME: this should probably switch to indirect as well
129static void direct_decrement_key(struct cpu_key *key)
130{
131	cpu_key_k_offset_dec(key);
132	if (cpu_key_k_offset(key) == 0)
133		set_cpu_key_k_type(key, TYPE_STAT_DATA);
134}
135
136static int direct_is_left_mergeable(struct reiserfs_key *key,
137				    unsigned long bsize)
138{
139	int version = le_key_version(key);
140	return ((le_key_k_offset(version, key) & (bsize - 1)) != 1);
141}
142
143static void direct_print_item(struct item_head *ih, char *item)
144{
145	int j = 0;
146
147//    return;
148	printk("\"");
149	while (j < ih_item_len(ih))
150		printk("%c", item[j++]);
151	printk("\"\n");
152}
153
154static void direct_check_item(struct item_head *ih, char *item)
155{
156	// FIXME: type something here!
157}
158
159static int direct_create_vi(struct virtual_node *vn,
160			    struct virtual_item *vi,
161			    int is_affected, int insert_size)
162{
163	vi->vi_index = TYPE_DIRECT;
164	//vi->vi_type |= VI_TYPE_DIRECT;
165	return 0;
166}
167
168static int direct_check_left(struct virtual_item *vi, int free,
169			     int start_skip, int end_skip)
170{
171	int bytes;
172
173	bytes = free - free % 8;
174	return bytes ? : -1;
175}
176
177static int direct_check_right(struct virtual_item *vi, int free)
178{
179	return direct_check_left(vi, free, 0, 0);
180}
181
182static int direct_part_size(struct virtual_item *vi, int first, int count)
183{
184	return count;
185}
186
187static int direct_unit_num(struct virtual_item *vi)
188{
189	return vi->vi_item_len - IH_SIZE;
190}
191
192static void direct_print_vi(struct virtual_item *vi)
193{
194	reiserfs_warning(NULL, "reiserfs-16101",
195			 "DIRECT, index %d, type 0x%x, %h",
196			 vi->vi_index, vi->vi_type, vi->vi_ih);
197}
198
199static struct item_operations direct_ops = {
200	.bytes_number = direct_bytes_number,
201	.decrement_key = direct_decrement_key,
202	.is_left_mergeable = direct_is_left_mergeable,
203	.print_item = direct_print_item,
204	.check_item = direct_check_item,
205
206	.create_vi = direct_create_vi,
207	.check_left = direct_check_left,
208	.check_right = direct_check_right,
209	.part_size = direct_part_size,
210	.unit_num = direct_unit_num,
211	.print_vi = direct_print_vi
212};
213
214//////////////////////////////////////////////////////////////////////////////
215// indirect item functions
216//
217
218static int indirect_bytes_number(struct item_head *ih, int block_size)
219{
220	return ih_item_len(ih) / UNFM_P_SIZE * block_size;	//- get_ih_free_space (ih);
221}
222
223// decrease offset, if it becomes 0, change type to stat data
224static void indirect_decrement_key(struct cpu_key *key)
225{
226	cpu_key_k_offset_dec(key);
227	if (cpu_key_k_offset(key) == 0)
228		set_cpu_key_k_type(key, TYPE_STAT_DATA);
229}
230
231// if it is not first item of the body, then it is mergeable
232static int indirect_is_left_mergeable(struct reiserfs_key *key,
233				      unsigned long bsize)
234{
235	int version = le_key_version(key);
236	return (le_key_k_offset(version, key) != 1);
237}
238
239// printing of indirect item
240static void start_new_sequence(__u32 * start, int *len, __u32 new)
241{
242	*start = new;
243	*len = 1;
244}
245
246static int sequence_finished(__u32 start, int *len, __u32 new)
247{
248	if (start == INT_MAX)
249		return 1;
250
251	if (start == 0 && new == 0) {
252		(*len)++;
253		return 0;
254	}
255	if (start != 0 && (start + *len) == new) {
256		(*len)++;
257		return 0;
258	}
259	return 1;
260}
261
262static void print_sequence(__u32 start, int len)
263{
264	if (start == INT_MAX)
265		return;
266
267	if (len == 1)
268		printk(" %d", start);
269	else
270		printk(" %d(%d)", start, len);
271}
272
273static void indirect_print_item(struct item_head *ih, char *item)
274{
275	int j;
276	__le32 *unp;
277	__u32 prev = INT_MAX;
278	int num = 0;
279
280	unp = (__le32 *) item;
281
282	if (ih_item_len(ih) % UNFM_P_SIZE)
283		reiserfs_warning(NULL, "reiserfs-16102", "invalid item len");
284
285	printk("%d pointers\n[ ", (int)I_UNFM_NUM(ih));
286	for (j = 0; j < I_UNFM_NUM(ih); j++) {
287		if (sequence_finished(prev, &num, get_block_num(unp, j))) {
288			print_sequence(prev, num);
289			start_new_sequence(&prev, &num, get_block_num(unp, j));
290		}
291	}
292	print_sequence(prev, num);
293	printk("]\n");
294}
295
296static void indirect_check_item(struct item_head *ih, char *item)
297{
298	// FIXME: type something here!
299}
300
301static int indirect_create_vi(struct virtual_node *vn,
302			      struct virtual_item *vi,
303			      int is_affected, int insert_size)
304{
305	vi->vi_index = TYPE_INDIRECT;
306	//vi->vi_type |= VI_TYPE_INDIRECT;
307	return 0;
308}
309
310static int indirect_check_left(struct virtual_item *vi, int free,
311			       int start_skip, int end_skip)
312{
313	int bytes;
314
315	bytes = free - free % UNFM_P_SIZE;
316	return bytes ? : -1;
317}
318
319static int indirect_check_right(struct virtual_item *vi, int free)
320{
321	return indirect_check_left(vi, free, 0, 0);
322}
323
324// return size in bytes of 'units' units. If first == 0 - calculate from the head (left), otherwise - from tail (right)
 
 
 
325static int indirect_part_size(struct virtual_item *vi, int first, int units)
326{
327	// unit of indirect item is byte (yet)
328	return units;
329}
330
331static int indirect_unit_num(struct virtual_item *vi)
332{
333	// unit of indirect item is byte (yet)
334	return vi->vi_item_len - IH_SIZE;
335}
336
337static void indirect_print_vi(struct virtual_item *vi)
338{
339	reiserfs_warning(NULL, "reiserfs-16103",
340			 "INDIRECT, index %d, type 0x%x, %h",
341			 vi->vi_index, vi->vi_type, vi->vi_ih);
342}
343
344static struct item_operations indirect_ops = {
345	.bytes_number = indirect_bytes_number,
346	.decrement_key = indirect_decrement_key,
347	.is_left_mergeable = indirect_is_left_mergeable,
348	.print_item = indirect_print_item,
349	.check_item = indirect_check_item,
350
351	.create_vi = indirect_create_vi,
352	.check_left = indirect_check_left,
353	.check_right = indirect_check_right,
354	.part_size = indirect_part_size,
355	.unit_num = indirect_unit_num,
356	.print_vi = indirect_print_vi
357};
358
359//////////////////////////////////////////////////////////////////////////////
360// direntry functions
361//
362
363static int direntry_bytes_number(struct item_head *ih, int block_size)
364{
365	reiserfs_warning(NULL, "vs-16090",
366			 "bytes number is asked for direntry");
367	return 0;
368}
369
370static void direntry_decrement_key(struct cpu_key *key)
371{
372	cpu_key_k_offset_dec(key);
373	if (cpu_key_k_offset(key) == 0)
374		set_cpu_key_k_type(key, TYPE_STAT_DATA);
375}
376
377static int direntry_is_left_mergeable(struct reiserfs_key *key,
378				      unsigned long bsize)
379{
380	if (le32_to_cpu(key->u.k_offset_v1.k_offset) == DOT_OFFSET)
381		return 0;
382	return 1;
383
384}
385
386static void direntry_print_item(struct item_head *ih, char *item)
387{
388	int i;
389	int namelen;
390	struct reiserfs_de_head *deh;
391	char *name;
392	static char namebuf[80];
393
394	printk("\n # %-15s%-30s%-15s%-15s%-15s\n", "Name",
395	       "Key of pointed object", "Hash", "Gen number", "Status");
396
397	deh = (struct reiserfs_de_head *)item;
398
399	for (i = 0; i < I_ENTRY_COUNT(ih); i++, deh++) {
400		namelen =
401		    (i ? (deh_location(deh - 1)) : ih_item_len(ih)) -
402		    deh_location(deh);
403		name = item + deh_location(deh);
404		if (name[namelen - 1] == 0)
405			namelen = strlen(name);
406		namebuf[0] = '"';
407		if (namelen > sizeof(namebuf) - 3) {
408			strncpy(namebuf + 1, name, sizeof(namebuf) - 3);
409			namebuf[sizeof(namebuf) - 2] = '"';
410			namebuf[sizeof(namebuf) - 1] = 0;
411		} else {
412			memcpy(namebuf + 1, name, namelen);
413			namebuf[namelen + 1] = '"';
414			namebuf[namelen + 2] = 0;
415		}
416
417		printk("%d:  %-15s%-15d%-15d%-15Ld%-15Ld(%s)\n",
418		       i, namebuf,
419		       deh_dir_id(deh), deh_objectid(deh),
420		       GET_HASH_VALUE(deh_offset(deh)),
421		       GET_GENERATION_NUMBER((deh_offset(deh))),
422		       (de_hidden(deh)) ? "HIDDEN" : "VISIBLE");
423	}
424}
425
426static void direntry_check_item(struct item_head *ih, char *item)
427{
428	int i;
429	struct reiserfs_de_head *deh;
430
431	// FIXME: type something here!
432	deh = (struct reiserfs_de_head *)item;
433	for (i = 0; i < I_ENTRY_COUNT(ih); i++, deh++) {
434		;
435	}
436}
437
438#define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1
439
440/*
441 * function returns old entry number in directory item in real node
442 * using new entry number in virtual item in virtual node */
 
443static inline int old_entry_num(int is_affected, int virtual_entry_num,
444				int pos_in_item, int mode)
445{
446	if (mode == M_INSERT || mode == M_DELETE)
447		return virtual_entry_num;
448
449	if (!is_affected)
450		/* cut or paste is applied to another item */
451		return virtual_entry_num;
452
453	if (virtual_entry_num < pos_in_item)
454		return virtual_entry_num;
455
456	if (mode == M_CUT)
457		return virtual_entry_num + 1;
458
459	RFALSE(mode != M_PASTE || virtual_entry_num == 0,
460	       "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'",
461	       mode);
462
463	return virtual_entry_num - 1;
464}
465
466/* Create an array of sizes of directory entries for virtual
467   item. Return space used by an item. FIXME: no control over
468   consuming of space used by this item handler */
 
 
469static int direntry_create_vi(struct virtual_node *vn,
470			      struct virtual_item *vi,
471			      int is_affected, int insert_size)
472{
473	struct direntry_uarea *dir_u = vi->vi_uarea;
474	int i, j;
475	int size = sizeof(struct direntry_uarea);
476	struct reiserfs_de_head *deh;
477
478	vi->vi_index = TYPE_DIRENTRY;
479
480	BUG_ON(!(vi->vi_ih) || !vi->vi_item);
481
482	dir_u->flags = 0;
483	if (le_ih_k_offset(vi->vi_ih) == DOT_OFFSET)
484		dir_u->flags |= DIRENTRY_VI_FIRST_DIRENTRY_ITEM;
485
486	deh = (struct reiserfs_de_head *)(vi->vi_item);
487
488	/* virtual directory item have this amount of entry after */
489	dir_u->entry_count = ih_entry_count(vi->vi_ih) +
490	    ((is_affected) ? ((vn->vn_mode == M_CUT) ? -1 :
491			      (vn->vn_mode == M_PASTE ? 1 : 0)) : 0);
492
493	for (i = 0; i < dir_u->entry_count; i++) {
494		j = old_entry_num(is_affected, i, vn->vn_pos_in_item,
495				  vn->vn_mode);
496		dir_u->entry_sizes[i] =
497		    (j ? deh_location(&(deh[j - 1])) : ih_item_len(vi->vi_ih)) -
498		    deh_location(&(deh[j])) + DEH_SIZE;
499	}
500
501	size += (dir_u->entry_count * sizeof(short));
502
503	/* set size of pasted entry */
504	if (is_affected && vn->vn_mode == M_PASTE)
505		dir_u->entry_sizes[vn->vn_pos_in_item] = insert_size;
506
507#ifdef CONFIG_REISERFS_CHECK
508	/* compare total size of entries with item length */
509	{
510		int k, l;
511
512		l = 0;
513		for (k = 0; k < dir_u->entry_count; k++)
514			l += dir_u->entry_sizes[k];
515
516		if (l + IH_SIZE != vi->vi_item_len +
517		    ((is_affected
518		      && (vn->vn_mode == M_PASTE
519			  || vn->vn_mode == M_CUT)) ? insert_size : 0)) {
520			reiserfs_panic(NULL, "vs-8025", "(mode==%c, "
521				       "insert_size==%d), invalid length of "
522				       "directory item",
523				       vn->vn_mode, insert_size);
524		}
525	}
526#endif
527
528	return size;
529
530}
531
532//
533// return number of entries which may fit into specified amount of
534// free space, or -1 if free space is not enough even for 1 entry
535//
536static int direntry_check_left(struct virtual_item *vi, int free,
537			       int start_skip, int end_skip)
538{
539	int i;
540	int entries = 0;
541	struct direntry_uarea *dir_u = vi->vi_uarea;
542
543	for (i = start_skip; i < dir_u->entry_count - end_skip; i++) {
 
544		if (dir_u->entry_sizes[i] > free)
545			/* i-th entry doesn't fit into the remaining free space */
546			break;
547
548		free -= dir_u->entry_sizes[i];
549		entries++;
550	}
551
552	if (entries == dir_u->entry_count) {
553		reiserfs_panic(NULL, "item_ops-1",
554			       "free space %d, entry_count %d", free,
555			       dir_u->entry_count);
556	}
557
558	/* "." and ".." can not be separated from each other */
559	if (start_skip == 0 && (dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM)
560	    && entries < 2)
561		entries = 0;
562
563	return entries ? : -1;
564}
565
566static int direntry_check_right(struct virtual_item *vi, int free)
567{
568	int i;
569	int entries = 0;
570	struct direntry_uarea *dir_u = vi->vi_uarea;
571
572	for (i = dir_u->entry_count - 1; i >= 0; i--) {
 
573		if (dir_u->entry_sizes[i] > free)
574			/* i-th entry doesn't fit into the remaining free space */
575			break;
576
577		free -= dir_u->entry_sizes[i];
578		entries++;
579	}
580	BUG_ON(entries == dir_u->entry_count);
581
582	/* "." and ".." can not be separated from each other */
583	if ((dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM)
584	    && entries > dir_u->entry_count - 2)
585		entries = dir_u->entry_count - 2;
586
587	return entries ? : -1;
588}
589
590/* sum of entry sizes between from-th and to-th entries including both edges */
591static int direntry_part_size(struct virtual_item *vi, int first, int count)
592{
593	int i, retval;
594	int from, to;
595	struct direntry_uarea *dir_u = vi->vi_uarea;
596
597	retval = 0;
598	if (first == 0)
599		from = 0;
600	else
601		from = dir_u->entry_count - count;
602	to = from + count - 1;
603
604	for (i = from; i <= to; i++)
605		retval += dir_u->entry_sizes[i];
606
607	return retval;
608}
609
610static int direntry_unit_num(struct virtual_item *vi)
611{
612	struct direntry_uarea *dir_u = vi->vi_uarea;
613
614	return dir_u->entry_count;
615}
616
617static void direntry_print_vi(struct virtual_item *vi)
618{
619	int i;
620	struct direntry_uarea *dir_u = vi->vi_uarea;
621
622	reiserfs_warning(NULL, "reiserfs-16104",
623			 "DIRENTRY, index %d, type 0x%x, %h, flags 0x%x",
624			 vi->vi_index, vi->vi_type, vi->vi_ih, dir_u->flags);
625	printk("%d entries: ", dir_u->entry_count);
626	for (i = 0; i < dir_u->entry_count; i++)
627		printk("%d ", dir_u->entry_sizes[i]);
628	printk("\n");
629}
630
631static struct item_operations direntry_ops = {
632	.bytes_number = direntry_bytes_number,
633	.decrement_key = direntry_decrement_key,
634	.is_left_mergeable = direntry_is_left_mergeable,
635	.print_item = direntry_print_item,
636	.check_item = direntry_check_item,
637
638	.create_vi = direntry_create_vi,
639	.check_left = direntry_check_left,
640	.check_right = direntry_check_right,
641	.part_size = direntry_part_size,
642	.unit_num = direntry_unit_num,
643	.print_vi = direntry_print_vi
644};
645
646//////////////////////////////////////////////////////////////////////////////
647// Error catching functions to catch errors caused by incorrect item types.
648//
649static int errcatch_bytes_number(struct item_head *ih, int block_size)
650{
651	reiserfs_warning(NULL, "green-16001",
652			 "Invalid item type observed, run fsck ASAP");
653	return 0;
654}
655
656static void errcatch_decrement_key(struct cpu_key *key)
657{
658	reiserfs_warning(NULL, "green-16002",
659			 "Invalid item type observed, run fsck ASAP");
660}
661
662static int errcatch_is_left_mergeable(struct reiserfs_key *key,
663				      unsigned long bsize)
664{
665	reiserfs_warning(NULL, "green-16003",
666			 "Invalid item type observed, run fsck ASAP");
667	return 0;
668}
669
670static void errcatch_print_item(struct item_head *ih, char *item)
671{
672	reiserfs_warning(NULL, "green-16004",
673			 "Invalid item type observed, run fsck ASAP");
674}
675
676static void errcatch_check_item(struct item_head *ih, char *item)
677{
678	reiserfs_warning(NULL, "green-16005",
679			 "Invalid item type observed, run fsck ASAP");
680}
681
682static int errcatch_create_vi(struct virtual_node *vn,
683			      struct virtual_item *vi,
684			      int is_affected, int insert_size)
685{
686	reiserfs_warning(NULL, "green-16006",
687			 "Invalid item type observed, run fsck ASAP");
688	return 0;		// We might return -1 here as well, but it won't help as create_virtual_node() from where
689	// this operation is called from is of return type void.
 
 
 
 
690}
691
692static int errcatch_check_left(struct virtual_item *vi, int free,
693			       int start_skip, int end_skip)
694{
695	reiserfs_warning(NULL, "green-16007",
696			 "Invalid item type observed, run fsck ASAP");
697	return -1;
698}
699
700static int errcatch_check_right(struct virtual_item *vi, int free)
701{
702	reiserfs_warning(NULL, "green-16008",
703			 "Invalid item type observed, run fsck ASAP");
704	return -1;
705}
706
707static int errcatch_part_size(struct virtual_item *vi, int first, int count)
708{
709	reiserfs_warning(NULL, "green-16009",
710			 "Invalid item type observed, run fsck ASAP");
711	return 0;
712}
713
714static int errcatch_unit_num(struct virtual_item *vi)
715{
716	reiserfs_warning(NULL, "green-16010",
717			 "Invalid item type observed, run fsck ASAP");
718	return 0;
719}
720
721static void errcatch_print_vi(struct virtual_item *vi)
722{
723	reiserfs_warning(NULL, "green-16011",
724			 "Invalid item type observed, run fsck ASAP");
725}
726
727static struct item_operations errcatch_ops = {
728	errcatch_bytes_number,
729	errcatch_decrement_key,
730	errcatch_is_left_mergeable,
731	errcatch_print_item,
732	errcatch_check_item,
733
734	errcatch_create_vi,
735	errcatch_check_left,
736	errcatch_check_right,
737	errcatch_part_size,
738	errcatch_unit_num,
739	errcatch_print_vi
740};
741
742//////////////////////////////////////////////////////////////////////////////
743//
744//
745#if ! (TYPE_STAT_DATA == 0 && TYPE_INDIRECT == 1 && TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3)
746#error Item types must use disk-format assigned values.
747#endif
748
749struct item_operations *item_ops[TYPE_ANY + 1] = {
750	&stat_data_ops,
751	&indirect_ops,
752	&direct_ops,
753	&direntry_ops,
754	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
755	&errcatch_ops		/* This is to catch errors with invalid type (15th entry for TYPE_ANY) */
756};
v4.6
  1/*
  2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3 */
  4
  5#include <linux/time.h>
  6#include "reiserfs.h"
  7
  8/*
  9 * this contains item handlers for old item types: sd, direct,
 10 * indirect, directory
 11 */
 12
 13/*
 14 * and where are the comments? how about saying where we can find an
 15 * explanation of each item handler method? -Hans
 16 */
 17
 18/* stat data functions */
 
 
 19static int sd_bytes_number(struct item_head *ih, int block_size)
 20{
 21	return 0;
 22}
 23
 24static void sd_decrement_key(struct cpu_key *key)
 25{
 26	key->on_disk_key.k_objectid--;
 27	set_cpu_key_k_type(key, TYPE_ANY);
 28	set_cpu_key_k_offset(key, (loff_t)(~0ULL >> 1));
 29}
 30
 31static int sd_is_left_mergeable(struct reiserfs_key *key, unsigned long bsize)
 32{
 33	return 0;
 34}
 35
 36static char *print_time(time_t t)
 37{
 38	static char timebuf[256];
 39
 40	sprintf(timebuf, "%ld", t);
 41	return timebuf;
 42}
 43
 44static void sd_print_item(struct item_head *ih, char *item)
 45{
 46	printk("\tmode | size | nlinks | first direct | mtime\n");
 47	if (stat_data_v1(ih)) {
 48		struct stat_data_v1 *sd = (struct stat_data_v1 *)item;
 49
 50		printk("\t0%-6o | %6u | %2u | %d | %s\n", sd_v1_mode(sd),
 51		       sd_v1_size(sd), sd_v1_nlink(sd),
 52		       sd_v1_first_direct_byte(sd),
 53		       print_time(sd_v1_mtime(sd)));
 54	} else {
 55		struct stat_data *sd = (struct stat_data *)item;
 56
 57		printk("\t0%-6o | %6llu | %2u | %d | %s\n", sd_v2_mode(sd),
 58		       (unsigned long long)sd_v2_size(sd), sd_v2_nlink(sd),
 59		       sd_v2_rdev(sd), print_time(sd_v2_mtime(sd)));
 60	}
 61}
 62
 63static void sd_check_item(struct item_head *ih, char *item)
 64{
 65	/* unused */
 66}
 67
 68static int sd_create_vi(struct virtual_node *vn,
 69			struct virtual_item *vi,
 70			int is_affected, int insert_size)
 71{
 72	vi->vi_index = TYPE_STAT_DATA;
 
 73	return 0;
 74}
 75
 76static int sd_check_left(struct virtual_item *vi, int free,
 77			 int start_skip, int end_skip)
 78{
 79	BUG_ON(start_skip || end_skip);
 80	return -1;
 81}
 82
 83static int sd_check_right(struct virtual_item *vi, int free)
 84{
 85	return -1;
 86}
 87
 88static int sd_part_size(struct virtual_item *vi, int first, int count)
 89{
 90	BUG_ON(count);
 91	return 0;
 92}
 93
 94static int sd_unit_num(struct virtual_item *vi)
 95{
 96	return vi->vi_item_len - IH_SIZE;
 97}
 98
 99static void sd_print_vi(struct virtual_item *vi)
100{
101	reiserfs_warning(NULL, "reiserfs-16100",
102			 "STATDATA, index %d, type 0x%x, %h",
103			 vi->vi_index, vi->vi_type, vi->vi_ih);
104}
105
106static struct item_operations stat_data_ops = {
107	.bytes_number = sd_bytes_number,
108	.decrement_key = sd_decrement_key,
109	.is_left_mergeable = sd_is_left_mergeable,
110	.print_item = sd_print_item,
111	.check_item = sd_check_item,
112
113	.create_vi = sd_create_vi,
114	.check_left = sd_check_left,
115	.check_right = sd_check_right,
116	.part_size = sd_part_size,
117	.unit_num = sd_unit_num,
118	.print_vi = sd_print_vi
119};
120
121/* direct item functions */
 
 
122static int direct_bytes_number(struct item_head *ih, int block_size)
123{
124	return ih_item_len(ih);
125}
126
127/* FIXME: this should probably switch to indirect as well */
128static void direct_decrement_key(struct cpu_key *key)
129{
130	cpu_key_k_offset_dec(key);
131	if (cpu_key_k_offset(key) == 0)
132		set_cpu_key_k_type(key, TYPE_STAT_DATA);
133}
134
135static int direct_is_left_mergeable(struct reiserfs_key *key,
136				    unsigned long bsize)
137{
138	int version = le_key_version(key);
139	return ((le_key_k_offset(version, key) & (bsize - 1)) != 1);
140}
141
142static void direct_print_item(struct item_head *ih, char *item)
143{
144	int j = 0;
145
146/*    return; */
147	printk("\"");
148	while (j < ih_item_len(ih))
149		printk("%c", item[j++]);
150	printk("\"\n");
151}
152
153static void direct_check_item(struct item_head *ih, char *item)
154{
155	/* unused */
156}
157
158static int direct_create_vi(struct virtual_node *vn,
159			    struct virtual_item *vi,
160			    int is_affected, int insert_size)
161{
162	vi->vi_index = TYPE_DIRECT;
 
163	return 0;
164}
165
166static int direct_check_left(struct virtual_item *vi, int free,
167			     int start_skip, int end_skip)
168{
169	int bytes;
170
171	bytes = free - free % 8;
172	return bytes ? : -1;
173}
174
175static int direct_check_right(struct virtual_item *vi, int free)
176{
177	return direct_check_left(vi, free, 0, 0);
178}
179
180static int direct_part_size(struct virtual_item *vi, int first, int count)
181{
182	return count;
183}
184
185static int direct_unit_num(struct virtual_item *vi)
186{
187	return vi->vi_item_len - IH_SIZE;
188}
189
190static void direct_print_vi(struct virtual_item *vi)
191{
192	reiserfs_warning(NULL, "reiserfs-16101",
193			 "DIRECT, index %d, type 0x%x, %h",
194			 vi->vi_index, vi->vi_type, vi->vi_ih);
195}
196
197static struct item_operations direct_ops = {
198	.bytes_number = direct_bytes_number,
199	.decrement_key = direct_decrement_key,
200	.is_left_mergeable = direct_is_left_mergeable,
201	.print_item = direct_print_item,
202	.check_item = direct_check_item,
203
204	.create_vi = direct_create_vi,
205	.check_left = direct_check_left,
206	.check_right = direct_check_right,
207	.part_size = direct_part_size,
208	.unit_num = direct_unit_num,
209	.print_vi = direct_print_vi
210};
211
212/* indirect item functions */
 
 
 
213static int indirect_bytes_number(struct item_head *ih, int block_size)
214{
215	return ih_item_len(ih) / UNFM_P_SIZE * block_size;
216}
217
218/* decrease offset, if it becomes 0, change type to stat data */
219static void indirect_decrement_key(struct cpu_key *key)
220{
221	cpu_key_k_offset_dec(key);
222	if (cpu_key_k_offset(key) == 0)
223		set_cpu_key_k_type(key, TYPE_STAT_DATA);
224}
225
226/* if it is not first item of the body, then it is mergeable */
227static int indirect_is_left_mergeable(struct reiserfs_key *key,
228				      unsigned long bsize)
229{
230	int version = le_key_version(key);
231	return (le_key_k_offset(version, key) != 1);
232}
233
234/* printing of indirect item */
235static void start_new_sequence(__u32 * start, int *len, __u32 new)
236{
237	*start = new;
238	*len = 1;
239}
240
241static int sequence_finished(__u32 start, int *len, __u32 new)
242{
243	if (start == INT_MAX)
244		return 1;
245
246	if (start == 0 && new == 0) {
247		(*len)++;
248		return 0;
249	}
250	if (start != 0 && (start + *len) == new) {
251		(*len)++;
252		return 0;
253	}
254	return 1;
255}
256
257static void print_sequence(__u32 start, int len)
258{
259	if (start == INT_MAX)
260		return;
261
262	if (len == 1)
263		printk(" %d", start);
264	else
265		printk(" %d(%d)", start, len);
266}
267
268static void indirect_print_item(struct item_head *ih, char *item)
269{
270	int j;
271	__le32 *unp;
272	__u32 prev = INT_MAX;
273	int num = 0;
274
275	unp = (__le32 *) item;
276
277	if (ih_item_len(ih) % UNFM_P_SIZE)
278		reiserfs_warning(NULL, "reiserfs-16102", "invalid item len");
279
280	printk("%d pointers\n[ ", (int)I_UNFM_NUM(ih));
281	for (j = 0; j < I_UNFM_NUM(ih); j++) {
282		if (sequence_finished(prev, &num, get_block_num(unp, j))) {
283			print_sequence(prev, num);
284			start_new_sequence(&prev, &num, get_block_num(unp, j));
285		}
286	}
287	print_sequence(prev, num);
288	printk("]\n");
289}
290
291static void indirect_check_item(struct item_head *ih, char *item)
292{
293	/* unused */
294}
295
296static int indirect_create_vi(struct virtual_node *vn,
297			      struct virtual_item *vi,
298			      int is_affected, int insert_size)
299{
300	vi->vi_index = TYPE_INDIRECT;
 
301	return 0;
302}
303
304static int indirect_check_left(struct virtual_item *vi, int free,
305			       int start_skip, int end_skip)
306{
307	int bytes;
308
309	bytes = free - free % UNFM_P_SIZE;
310	return bytes ? : -1;
311}
312
313static int indirect_check_right(struct virtual_item *vi, int free)
314{
315	return indirect_check_left(vi, free, 0, 0);
316}
317
318/*
319 * return size in bytes of 'units' units. If first == 0 - calculate
320 * from the head (left), otherwise - from tail (right)
321 */
322static int indirect_part_size(struct virtual_item *vi, int first, int units)
323{
324	/* unit of indirect item is byte (yet) */
325	return units;
326}
327
328static int indirect_unit_num(struct virtual_item *vi)
329{
330	/* unit of indirect item is byte (yet) */
331	return vi->vi_item_len - IH_SIZE;
332}
333
334static void indirect_print_vi(struct virtual_item *vi)
335{
336	reiserfs_warning(NULL, "reiserfs-16103",
337			 "INDIRECT, index %d, type 0x%x, %h",
338			 vi->vi_index, vi->vi_type, vi->vi_ih);
339}
340
341static struct item_operations indirect_ops = {
342	.bytes_number = indirect_bytes_number,
343	.decrement_key = indirect_decrement_key,
344	.is_left_mergeable = indirect_is_left_mergeable,
345	.print_item = indirect_print_item,
346	.check_item = indirect_check_item,
347
348	.create_vi = indirect_create_vi,
349	.check_left = indirect_check_left,
350	.check_right = indirect_check_right,
351	.part_size = indirect_part_size,
352	.unit_num = indirect_unit_num,
353	.print_vi = indirect_print_vi
354};
355
356/* direntry functions */
 
 
 
357static int direntry_bytes_number(struct item_head *ih, int block_size)
358{
359	reiserfs_warning(NULL, "vs-16090",
360			 "bytes number is asked for direntry");
361	return 0;
362}
363
364static void direntry_decrement_key(struct cpu_key *key)
365{
366	cpu_key_k_offset_dec(key);
367	if (cpu_key_k_offset(key) == 0)
368		set_cpu_key_k_type(key, TYPE_STAT_DATA);
369}
370
371static int direntry_is_left_mergeable(struct reiserfs_key *key,
372				      unsigned long bsize)
373{
374	if (le32_to_cpu(key->u.k_offset_v1.k_offset) == DOT_OFFSET)
375		return 0;
376	return 1;
377
378}
379
380static void direntry_print_item(struct item_head *ih, char *item)
381{
382	int i;
383	int namelen;
384	struct reiserfs_de_head *deh;
385	char *name;
386	static char namebuf[80];
387
388	printk("\n # %-15s%-30s%-15s%-15s%-15s\n", "Name",
389	       "Key of pointed object", "Hash", "Gen number", "Status");
390
391	deh = (struct reiserfs_de_head *)item;
392
393	for (i = 0; i < ih_entry_count(ih); i++, deh++) {
394		namelen =
395		    (i ? (deh_location(deh - 1)) : ih_item_len(ih)) -
396		    deh_location(deh);
397		name = item + deh_location(deh);
398		if (name[namelen - 1] == 0)
399			namelen = strlen(name);
400		namebuf[0] = '"';
401		if (namelen > sizeof(namebuf) - 3) {
402			strncpy(namebuf + 1, name, sizeof(namebuf) - 3);
403			namebuf[sizeof(namebuf) - 2] = '"';
404			namebuf[sizeof(namebuf) - 1] = 0;
405		} else {
406			memcpy(namebuf + 1, name, namelen);
407			namebuf[namelen + 1] = '"';
408			namebuf[namelen + 2] = 0;
409		}
410
411		printk("%d:  %-15s%-15d%-15d%-15lld%-15lld(%s)\n",
412		       i, namebuf,
413		       deh_dir_id(deh), deh_objectid(deh),
414		       GET_HASH_VALUE(deh_offset(deh)),
415		       GET_GENERATION_NUMBER((deh_offset(deh))),
416		       (de_hidden(deh)) ? "HIDDEN" : "VISIBLE");
417	}
418}
419
420static void direntry_check_item(struct item_head *ih, char *item)
421{
422	int i;
423	struct reiserfs_de_head *deh;
424
425	/* unused */
426	deh = (struct reiserfs_de_head *)item;
427	for (i = 0; i < ih_entry_count(ih); i++, deh++) {
428		;
429	}
430}
431
432#define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1
433
434/*
435 * function returns old entry number in directory item in real node
436 * using new entry number in virtual item in virtual node
437 */
438static inline int old_entry_num(int is_affected, int virtual_entry_num,
439				int pos_in_item, int mode)
440{
441	if (mode == M_INSERT || mode == M_DELETE)
442		return virtual_entry_num;
443
444	if (!is_affected)
445		/* cut or paste is applied to another item */
446		return virtual_entry_num;
447
448	if (virtual_entry_num < pos_in_item)
449		return virtual_entry_num;
450
451	if (mode == M_CUT)
452		return virtual_entry_num + 1;
453
454	RFALSE(mode != M_PASTE || virtual_entry_num == 0,
455	       "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'",
456	       mode);
457
458	return virtual_entry_num - 1;
459}
460
461/*
462 * Create an array of sizes of directory entries for virtual
463 * item. Return space used by an item. FIXME: no control over
464 * consuming of space used by this item handler
465 */
466static int direntry_create_vi(struct virtual_node *vn,
467			      struct virtual_item *vi,
468			      int is_affected, int insert_size)
469{
470	struct direntry_uarea *dir_u = vi->vi_uarea;
471	int i, j;
472	int size = sizeof(struct direntry_uarea);
473	struct reiserfs_de_head *deh;
474
475	vi->vi_index = TYPE_DIRENTRY;
476
477	BUG_ON(!(vi->vi_ih) || !vi->vi_item);
478
479	dir_u->flags = 0;
480	if (le_ih_k_offset(vi->vi_ih) == DOT_OFFSET)
481		dir_u->flags |= DIRENTRY_VI_FIRST_DIRENTRY_ITEM;
482
483	deh = (struct reiserfs_de_head *)(vi->vi_item);
484
485	/* virtual directory item have this amount of entry after */
486	dir_u->entry_count = ih_entry_count(vi->vi_ih) +
487	    ((is_affected) ? ((vn->vn_mode == M_CUT) ? -1 :
488			      (vn->vn_mode == M_PASTE ? 1 : 0)) : 0);
489
490	for (i = 0; i < dir_u->entry_count; i++) {
491		j = old_entry_num(is_affected, i, vn->vn_pos_in_item,
492				  vn->vn_mode);
493		dir_u->entry_sizes[i] =
494		    (j ? deh_location(&deh[j - 1]) : ih_item_len(vi->vi_ih)) -
495		    deh_location(&deh[j]) + DEH_SIZE;
496	}
497
498	size += (dir_u->entry_count * sizeof(short));
499
500	/* set size of pasted entry */
501	if (is_affected && vn->vn_mode == M_PASTE)
502		dir_u->entry_sizes[vn->vn_pos_in_item] = insert_size;
503
504#ifdef CONFIG_REISERFS_CHECK
505	/* compare total size of entries with item length */
506	{
507		int k, l;
508
509		l = 0;
510		for (k = 0; k < dir_u->entry_count; k++)
511			l += dir_u->entry_sizes[k];
512
513		if (l + IH_SIZE != vi->vi_item_len +
514		    ((is_affected
515		      && (vn->vn_mode == M_PASTE
516			  || vn->vn_mode == M_CUT)) ? insert_size : 0)) {
517			reiserfs_panic(NULL, "vs-8025", "(mode==%c, "
518				       "insert_size==%d), invalid length of "
519				       "directory item",
520				       vn->vn_mode, insert_size);
521		}
522	}
523#endif
524
525	return size;
526
527}
528
529/*
530 * return number of entries which may fit into specified amount of
531 * free space, or -1 if free space is not enough even for 1 entry
532 */
533static int direntry_check_left(struct virtual_item *vi, int free,
534			       int start_skip, int end_skip)
535{
536	int i;
537	int entries = 0;
538	struct direntry_uarea *dir_u = vi->vi_uarea;
539
540	for (i = start_skip; i < dir_u->entry_count - end_skip; i++) {
541		/* i-th entry doesn't fit into the remaining free space */
542		if (dir_u->entry_sizes[i] > free)
 
543			break;
544
545		free -= dir_u->entry_sizes[i];
546		entries++;
547	}
548
549	if (entries == dir_u->entry_count) {
550		reiserfs_panic(NULL, "item_ops-1",
551			       "free space %d, entry_count %d", free,
552			       dir_u->entry_count);
553	}
554
555	/* "." and ".." can not be separated from each other */
556	if (start_skip == 0 && (dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM)
557	    && entries < 2)
558		entries = 0;
559
560	return entries ? : -1;
561}
562
563static int direntry_check_right(struct virtual_item *vi, int free)
564{
565	int i;
566	int entries = 0;
567	struct direntry_uarea *dir_u = vi->vi_uarea;
568
569	for (i = dir_u->entry_count - 1; i >= 0; i--) {
570		/* i-th entry doesn't fit into the remaining free space */
571		if (dir_u->entry_sizes[i] > free)
 
572			break;
573
574		free -= dir_u->entry_sizes[i];
575		entries++;
576	}
577	BUG_ON(entries == dir_u->entry_count);
578
579	/* "." and ".." can not be separated from each other */
580	if ((dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM)
581	    && entries > dir_u->entry_count - 2)
582		entries = dir_u->entry_count - 2;
583
584	return entries ? : -1;
585}
586
587/* sum of entry sizes between from-th and to-th entries including both edges */
588static int direntry_part_size(struct virtual_item *vi, int first, int count)
589{
590	int i, retval;
591	int from, to;
592	struct direntry_uarea *dir_u = vi->vi_uarea;
593
594	retval = 0;
595	if (first == 0)
596		from = 0;
597	else
598		from = dir_u->entry_count - count;
599	to = from + count - 1;
600
601	for (i = from; i <= to; i++)
602		retval += dir_u->entry_sizes[i];
603
604	return retval;
605}
606
607static int direntry_unit_num(struct virtual_item *vi)
608{
609	struct direntry_uarea *dir_u = vi->vi_uarea;
610
611	return dir_u->entry_count;
612}
613
614static void direntry_print_vi(struct virtual_item *vi)
615{
616	int i;
617	struct direntry_uarea *dir_u = vi->vi_uarea;
618
619	reiserfs_warning(NULL, "reiserfs-16104",
620			 "DIRENTRY, index %d, type 0x%x, %h, flags 0x%x",
621			 vi->vi_index, vi->vi_type, vi->vi_ih, dir_u->flags);
622	printk("%d entries: ", dir_u->entry_count);
623	for (i = 0; i < dir_u->entry_count; i++)
624		printk("%d ", dir_u->entry_sizes[i]);
625	printk("\n");
626}
627
628static struct item_operations direntry_ops = {
629	.bytes_number = direntry_bytes_number,
630	.decrement_key = direntry_decrement_key,
631	.is_left_mergeable = direntry_is_left_mergeable,
632	.print_item = direntry_print_item,
633	.check_item = direntry_check_item,
634
635	.create_vi = direntry_create_vi,
636	.check_left = direntry_check_left,
637	.check_right = direntry_check_right,
638	.part_size = direntry_part_size,
639	.unit_num = direntry_unit_num,
640	.print_vi = direntry_print_vi
641};
642
643/* Error catching functions to catch errors caused by incorrect item types. */
 
 
644static int errcatch_bytes_number(struct item_head *ih, int block_size)
645{
646	reiserfs_warning(NULL, "green-16001",
647			 "Invalid item type observed, run fsck ASAP");
648	return 0;
649}
650
651static void errcatch_decrement_key(struct cpu_key *key)
652{
653	reiserfs_warning(NULL, "green-16002",
654			 "Invalid item type observed, run fsck ASAP");
655}
656
657static int errcatch_is_left_mergeable(struct reiserfs_key *key,
658				      unsigned long bsize)
659{
660	reiserfs_warning(NULL, "green-16003",
661			 "Invalid item type observed, run fsck ASAP");
662	return 0;
663}
664
665static void errcatch_print_item(struct item_head *ih, char *item)
666{
667	reiserfs_warning(NULL, "green-16004",
668			 "Invalid item type observed, run fsck ASAP");
669}
670
671static void errcatch_check_item(struct item_head *ih, char *item)
672{
673	reiserfs_warning(NULL, "green-16005",
674			 "Invalid item type observed, run fsck ASAP");
675}
676
677static int errcatch_create_vi(struct virtual_node *vn,
678			      struct virtual_item *vi,
679			      int is_affected, int insert_size)
680{
681	reiserfs_warning(NULL, "green-16006",
682			 "Invalid item type observed, run fsck ASAP");
683	/*
684	 * We might return -1 here as well, but it won't help as
685	 * create_virtual_node() from where this operation is called
686	 * from is of return type void.
687	 */
688	return 0;
689}
690
691static int errcatch_check_left(struct virtual_item *vi, int free,
692			       int start_skip, int end_skip)
693{
694	reiserfs_warning(NULL, "green-16007",
695			 "Invalid item type observed, run fsck ASAP");
696	return -1;
697}
698
699static int errcatch_check_right(struct virtual_item *vi, int free)
700{
701	reiserfs_warning(NULL, "green-16008",
702			 "Invalid item type observed, run fsck ASAP");
703	return -1;
704}
705
706static int errcatch_part_size(struct virtual_item *vi, int first, int count)
707{
708	reiserfs_warning(NULL, "green-16009",
709			 "Invalid item type observed, run fsck ASAP");
710	return 0;
711}
712
713static int errcatch_unit_num(struct virtual_item *vi)
714{
715	reiserfs_warning(NULL, "green-16010",
716			 "Invalid item type observed, run fsck ASAP");
717	return 0;
718}
719
720static void errcatch_print_vi(struct virtual_item *vi)
721{
722	reiserfs_warning(NULL, "green-16011",
723			 "Invalid item type observed, run fsck ASAP");
724}
725
726static struct item_operations errcatch_ops = {
727	errcatch_bytes_number,
728	errcatch_decrement_key,
729	errcatch_is_left_mergeable,
730	errcatch_print_item,
731	errcatch_check_item,
732
733	errcatch_create_vi,
734	errcatch_check_left,
735	errcatch_check_right,
736	errcatch_part_size,
737	errcatch_unit_num,
738	errcatch_print_vi
739};
740
 
 
 
741#if ! (TYPE_STAT_DATA == 0 && TYPE_INDIRECT == 1 && TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3)
742#error Item types must use disk-format assigned values.
743#endif
744
745struct item_operations *item_ops[TYPE_ANY + 1] = {
746	&stat_data_ops,
747	&indirect_ops,
748	&direct_ops,
749	&direntry_ops,
750	NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
751	&errcatch_ops		/* This is to catch errors with invalid type (15th entry for TYPE_ANY) */
752};