Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
/*
 * fs/dcache.c
 *
 * Complete reimplementation
 * (C) 1997 Thomas Schoebel-Theuer
 */

/* The new dcache is exclusively called from the VFS, not from
 * the specific fs'es any more. Despite having the same name as in the
 * old code, it has less to do with it.
 *
 * It serves many purposes:
 *
 *  1) Any inode that has been retrieved with lookup() and is in use
 *     (i_count>0), has access to its full absolute path name, by going
 *     to inode->i_dentry and then recursively following the entry->d_parent
 *     chain. Use d_path() as predefined method for that.
 *     You may find out the corresponding inode belonging to
 *     a dentry by calling d_inode(). This can be used as an easy way for
 *     determining .. and its absolute pathname, an old UNIX problem that
 *     deserved a solution for a long time.
 *     Note that hardlinked inodes may have multiple dentries assigned to
 *     (via the d_next chain), reflecting multiple alias pathnames.
 *
 *  2) If not disabled by filesystem types specifying FS_NO_DCACHE,
 *     the dentries of unused (aged) inodes are retained for speeding up
 *     lookup()s, by allowing hashed inquiry starting from the dentry of
 *     the parent directory.
 *
 *  3) It can remeber so-called "negative entries", that is dentries for
 *     pathnames that are known to *not* exist, so unneccessary repeated
 *     lookup()s for non-existant names can be saved.
 *
 *  4) It provides a means for keeping deleted files (inode->i_nlink==0)
 *     accessible in the so-called *basket*. Inodes in the basket have been
 *     removed with unlink() while being in use (i_count>0), so they would
 *     normally use up space on the disk and be accessile through their
 *     filedescriptor, but would not be accessible for lookup() any more.
 *     The basket simply keeps such files in the dcache (for potential
 *     dcache lookup) until they are either eventually removed completely,
 *     or transferred to the second-level basket, the so-called *ibasket*.
 *     The ibasket is implemented in the new inode code, on request of
 *     filesystem types that have the flag FS_IBASKET set, and proliferates
 *     the unlinked files when i_count has gone to zero, at least as long
 *     as there is space on the disk and enough inodes remain available
 *     and no umount() has started.
 *
 *  5) Preliminary dentries can be added by readdir(). While normal dentries
 *     directly point to the inode via u.d_inode only the inode number is
 *     known from readdir(), but not more. They can be converted to
 *     normal dentries by using d_inode().
 */

/*
 * Notes on the allocation strategy:
 *
 * The dcache is a full slave cache of the inodes. Whenever an inode
 * is cleared, all the dentries associated with it will recursively
 * disappear. dentries have no own reference counting; this has to
 * be obeyed for SMP.
 * If directories could go out of inode cache while
 * successors are alive, this would interrupt the d_parent chain of
 * the live successors. To prevent this without using zombies, all
 * directories are thus prevented from __iput() as long as successors
 * are alive.
 */

#include <linux/config.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/dalloc.h>
#include <linux/dlists.h>

/* this should be removed after the beta phase */
/* #define DEBUG */
/*#undef DEBUG*/
/* #define DEBUG_DDIR_COUNT */

#define D_HASHSIZE   64

/* local flags for d_flag */
#define D_DIR          32
#define D_HASHED       64
#define D_ZOMBIE      128
#define D_PRELIMINARY 256
#define D_INC_DDIR    512

/* local flags for d_del() */
#define D_RECURSIVE 4
#define D_NO_FREE   8

/* adjust these constants if you know a probability distribution ... */
#define D_SMALL 16
#define D_MEDIUM 64
#define D_LARGE 256
#define D_HUGE D_MAXLEN

#define BASE_DHEADER(x) (struct dheader*)((unsigned long)(x) & ~(PAGE_SIZE-1))
#define BYTE_ADD(x,n) (void*)((char*)(x) + (n))
#define BYTE_SUB(x,n) (void*)((char*)(x) - (n))

/* This is for global allocation of dentries.  Remove this when
 * converting to SLAB.
 */
struct dheader {
	struct dentry * emptylist;
	short free, maxfree;
	struct dheader * next;
	struct dheader * prev;
};

struct anchors {
	struct dheader * free; /* each contains at least 1 empty dentry */
	struct dheader * full; /* all the used up ones */
	struct dheader * dir_free; 
	struct dheader * dir_full; 
};

/* This is only used for directory dentries. Think of it as an extension
 * of the dentry.
 * It is defined as separate struct, so it uses up space only
 * where necessary.
 */
struct ddir {
	struct dentry * dd_hashtable[D_HASHSIZE];
	struct dentry * dd_neglist;
	struct dentry * dd_basketlist;
	struct dentry * dd_zombielist;
	unsigned short dd_alloced; /* # d_alloc()ed, but not yet d_add()ed */
	unsigned short dd_hashed;  /* # of entries in hashtable */
	unsigned short dd_true_hashed; /* # non-preliminaries in hashtable */
	unsigned short dd_negs; /* # of negative entries */
};

DEF_INSERT(header,struct dheader,next,prev)
DEF_REMOVE(header,struct dheader,next,prev)

DEF_INSERT(alias,struct dentry,d_next,d_prev)
DEF_REMOVE(alias,struct dentry,d_next,d_prev)

DEF_INSERT(hash,struct dentry,d_hash_next,d_hash_prev)
DEF_REMOVE(hash,struct dentry,d_hash_next,d_hash_prev)

DEF_INSERT(basket,struct dentry,d_basket_next,d_basket_prev)
DEF_REMOVE(basket,struct dentry,d_basket_next,d_basket_prev)

static struct anchors anchors[4];

struct dentry * the_root = NULL;

unsigned long name_cache_init(unsigned long mem_start, unsigned long mem_end)
{
	memset(anchors, 0, sizeof(anchors));
	return mem_start;
}

#ifdef DEBUG
/* throw this away after the beta phase */
/*************************************************************************/
extern void xcheck(char * txt, struct inode * p);

static int x_alloc = 0;
static int x_freed = 0;
static int x_free = 0;

static void * tst[20000];
static int cnt = 0;

static void ins(void* ptr)
{
	extern int inodes_stat;
	tst[cnt++] = ptr;
        if(cnt % 1000 == 0)
		printk("------%d allocated: %d: %d %d %d\n", inodes_stat, cnt,
		       x_alloc, x_freed, x_free);
	if(cnt>=20000) panic("stop");
}

#if 0
static inline int search(void* ptr)
{
	int i;
	for(i = cnt-1; i>=0; i--)
		if(tst[i] == ptr)
			return i;
	return -1;
}

#define TST(n,x) if(search(x)<0) printk("%s bad ptr %p line %d\n", n, x, __LINE__)
#else
#define TST(n,x) /*nothing*/
#endif

void LOG(char * txt, struct dentry * entry)
{
	static int count = 0;
	if(entry) {
		TST(txt,entry);
	}
	if(count) {
		count--;
		printk("%s: entry=%p\n", txt, entry);
	}
}

#ifdef DEBUG_DDIR_COUNT
static struct ddir * d_dir(struct dentry * entry);
void recursive_test(struct dentry * entry)
{
	int i;
	struct ddir * ddir = d_dir(entry);
	int sons = 0;

	if(ddir->dd_zombielist)
		sons++;
	for(i=0; i < D_HASHSIZE; i++) {
		struct dentry ** base = &ddir->dd_hashtable[i];
		struct dentry * tmp = *base;
		if(tmp) do {
			TST("__clear",tmp);
			if(!(tmp->d_flag & D_HASHED)) {
				printk("VFS: dcache entry not hashed!\n");
				printpath(*base); printk("\n");
				printpath(tmp);
			}
			if(!(tmp->d_flag & D_PRELIMINARY))
				sons++;
			if(tmp->d_flag & D_DIR)
				recursive_test(tmp);
			tmp = tmp->d_hash_next;
		} while(tmp && tmp != *base);
	}
	if(!sons && !(entry->d_flag & D_PRELIMINARY) && entry->u.d_inode) {
		struct inode * inode = entry->u.d_inode;
		if(!atomic_read(&inode->i_count)) {
			if(!(inode->i_status & 1/*ST_AGED*/)) {
				printpath(entry);
				printk(" is not aged!\n");
			}
			if(inode->i_ddir_count) {
				printpath(entry);
				printk(" has ddir_count blockage!\n");
			}
		}
	}
}
#else
#define recursive_test(e) /*nothing*/
#endif
#else
#define TST(n,x) /*nothing*/
#define LOG(n,x) /*nothing*/
#define xcheck(t,i) /*nothing*/
#define recursive_test(e) /*nothing*/
/*****************************************************************************/
#endif

void printpath(struct dentry * entry)
{
	if(!IS_ROOT(entry))
		printpath(entry->d_parent);
	printk("/%s", entry->d_name);
}

static inline long has_sons(struct ddir * ddir)
{
	return ((ddir->dd_alloced | ddir->dd_hashed)	||
		ddir->dd_neglist			||
		ddir->dd_basketlist			||
		ddir->dd_zombielist);
}

static inline int has_true_sons(struct ddir * ddir)
{
	return (ddir->dd_alloced | ddir->dd_true_hashed);
}

/* Only hold the i_ddir_count pseudo refcount when neccessary (i.e. when
 * they have true_sons), to prevent keeping too much dir inodes in use.
 */
static inline void inc_ddir(struct dentry * entry, struct inode * inode)
{
	if(!(entry->d_flag & D_INC_DDIR)) {
		entry->d_flag |= D_INC_DDIR;
#ifdef DEBUG
		if(inode->i_ddir_count) {
			printpath(entry);
			printk(" ddir_count=%d\n", inode->i_ddir_count);
		}
#endif
		inode->i_ddir_count++;
		_get_inode(inode);
	}
}

static inline blocking void dec_ddir(struct dentry * entry, struct inode * inode)
{
	if(entry->d_flag & D_INC_DDIR) {
		entry->d_flag &= ~D_INC_DDIR;
		inode->i_ddir_count--;
		if(!inode->i_ddir_count)
			__iput(inode);
	}
}

/* Do not inline this many times. */
static void d_panic(void)
{
	panic("VFS: dcache directory corruption");
}

static inline struct ddir * d_dir(struct dentry * entry)
{
	struct ddir * res = BYTE_SUB(entry, sizeof(struct ddir));

	if(!(entry->d_flag & D_DIR))
		d_panic();
#ifdef DEBUG
	if(!entry)
		panic("entry NULL!");
	if(BASE_DHEADER(res) != BASE_DHEADER(entry))
		printk("Scheisse!!!\n");
#endif
	return res;
}

static /*inline*/ struct dheader * dinit(int isdir, int size)
{
	struct dheader * res = (struct dheader*)__get_free_page(GFP_KERNEL);
	int restlen = PAGE_SIZE - sizeof(struct dheader);
	struct dentry * ptr = BYTE_ADD(res, sizeof(struct dheader));

	if(!res)
		return NULL;
        memset(res, 0, sizeof(struct dheader));
	if(isdir) {
		ptr = BYTE_ADD(ptr, sizeof(struct ddir));
		size += sizeof(struct ddir);
	}
	if(BASE_DHEADER(ptr) != res) 
		panic("Bad kernel page alignment");
	size += sizeof(struct dentry) - D_MAXLEN;
	res->emptylist = NULL;
	res->free = 0;
	while(restlen >= size) {
#ifdef DEBUG
		ins(ptr);
		if(BASE_DHEADER(ptr) != res)
			panic("Wrong dinit!");
#endif
		ptr->d_next = res->emptylist;
		res->emptylist = ptr;
		ptr = BYTE_ADD(ptr, size);
		res->free++;
		restlen -= size;
	}
	res->maxfree = res->free;
	return res;
}

static /*inline*/ struct dentry * __dalloc(struct anchors * anchor,
					   struct dentry * parent, int isdir,
					   int len, int size)
{
	struct dheader ** free = isdir ? &anchor->dir_free : &anchor->free;
	struct dheader ** full = isdir ? &anchor->dir_full : &anchor->full;
	struct dheader * base = *free;
	struct dentry * res;

	if(!base) {
		base = dinit(isdir, size);
		if(!base)
			return NULL;
		insert_header(free, base);
	}
	base->free--;
	res = base->emptylist;
	if(!(base->emptylist = res->d_next)) {
		remove_header(free, base);
		insert_header(full, base);
	}
	memset(res, 0, sizeof(struct dentry) - D_MAXLEN);
	if(isdir) {
		res->d_flag = D_DIR;
		memset(d_dir(res), 0, sizeof(struct ddir));
	}
	res->d_len = len;
	res->d_parent = parent;
	if(parent) {
		struct ddir * pdir = d_dir(parent);
#ifdef DEBUG
		if(pdir->dd_alloced > 1 && !IS_ROOT(parent)) {
			printpath(parent);
			printk(" dd_alloced=%d\n", pdir->dd_alloced);
		}
#endif
		pdir->dd_alloced++;
	}
#ifdef DEBUG
	x_alloc++;
#endif
	return res;
}

struct dentry * d_alloc(struct dentry * parent, int len, int isdir)
{
	int i, size;

#ifdef DEBUG
        if(the_root)
	        recursive_test(the_root);
	LOG("d_alloc", parent);
#endif
	if(len >= D_MEDIUM) {
		if(len >= D_LARGE) {
			i = 3;
			size = D_HUGE;
		} else {
			i = 2;
			size = D_LARGE;
		} 
	} else if(len >= D_SMALL) {
		i = 1;
		size = D_MEDIUM;
	} else {
		i = 0;
		size = D_SMALL;
	}
	return __dalloc(&anchors[i], parent, isdir, len, size);
}

extern blocking struct dentry * d_alloc_root(struct inode * root_inode)
{
	struct dentry * res = the_root;

	if(res) {
		d_del(res, D_NO_CLEAR_INODE); /* invalidate everything beyond */
	} else {
		struct ddir * ddir;

		the_root = res = d_alloc(NULL, 0, 1);
		LOG("d_alloc_root", res);
		res->d_parent = res;
		res->d_name[0]='\0';
		ddir = d_dir(res);
		ddir->dd_alloced = 999; /* protect from deletion */
	}
	insert_alias(&root_inode->i_dentry, res);
	root_inode->i_dent_count++;
	root_inode->i_ddir_count++;
	res->u.d_inode = root_inode;
	return res;
}

static inline unsigned long d_hash(char first, char last)
{
	return ((unsigned long)first ^ ((unsigned long)last << 4)) & (D_HASHSIZE-1);
}

static inline struct dentry ** d_base_entry(struct ddir * pdir, struct dentry * entry)
{
	return &pdir->dd_hashtable[d_hash(entry->d_name[0],
					  entry->d_name[entry->d_len-1])];
}

static inline struct dentry ** d_base_qstr(struct ddir * pdir,
					   struct qstr * s1,
					   struct qstr * s2)
{
	unsigned long hash;

	if(s2 && s2->len) {
		hash = d_hash(s1->name[0], s2->name[s2->len-1]);
	} else {
		hash = d_hash(s1->name[0], s1->name[s1->len-1]);
	}
	return &pdir->dd_hashtable[hash];
}


static /*inline*/ blocking void recursive_clear(struct ddir * ddir, int flags)
{
	int i, retry = 0;

	flags = (flags | D_RECURSIVE) & ~D_NO_CLEAR_INODE;
again:
	/* Clear those separately that are not in the hashtable. */
	while(ddir->dd_zombielist)
		d_del(ddir->dd_zombielist, flags);

	if(!ddir->dd_hashed)
		return; /* shortcut */

	for(i=0; i < D_HASHSIZE; i++) {
		struct dentry ** base = &ddir->dd_hashtable[i];
		struct dentry * tmp;

		while((tmp = *base)) {
#ifdef DEBUG
			TST("__clear",tmp);
			if(!(tmp->d_flag & D_HASHED)) {
				printk("VFS: dcache entry not hashed!\n");
				printpath(*base); printk("\n");
				printpath(tmp);
			}
#endif
			/* printk("["); */
			d_del(tmp, flags);
			/* printk("]"); */
		}
	}

	/* New entries may have been added during blocking. */
	if(ddir->dd_hashed && ++retry < 10)
		goto again;

#ifdef DEBUG
	if(ddir->dd_hashed || ddir->dd_true_hashed) {
		printk("remained %d/%d in hashtable\n",
		       ddir->dd_true_hashed, ddir->dd_hashed);
	}
#endif
}

static /*inline*/ blocking void _d_remove_from_parent(struct dentry * entry,
						      struct ddir * pdir,
						      struct inode * inode,
						      int flags)
{
	if(entry->d_flag & D_HASHED) {
		struct dentry ** base = d_base_entry(pdir, entry);

		remove_hash(base, entry);
		entry->d_flag &= ~D_HASHED;
		pdir->dd_hashed--;
		if(!(entry->d_flag & D_PRELIMINARY)) {
			pdir->dd_true_hashed--;
			if(!inode) {
#ifdef DEBUG
				if(!entry->d_next || !entry->d_prev) {
					printpath(entry);
					printk(" flags=%x d_flag=%x negs=%d "
					       "hashed=%d\n", flags, entry->d_flag,
					       pdir->dd_negs, pdir->dd_hashed);
				}
#endif
				remove_alias(&pdir->dd_neglist, entry);
				pdir->dd_negs--;
			}
		}
	} else if(!(entry->d_flag & D_ZOMBIE)) {
#ifdef DEBUG
		if(!pdir->dd_alloced) printk("dd_alloced is 0!\n");
#endif
		pdir->dd_alloced--;
	}
	if(entry->d_flag & D_BASKET) {
		remove_basket(&pdir->dd_basketlist, entry);
		entry->d_flag &= ~D_BASKET;
	}
}

/* Theoretically, zombies should never or extremely seldom appear,
 * so this code is nearly superfluous.
 * A way to get zombies is while using inodes (i_count>0), unlink()
 * them as well as rmdir() the parent dir => the parent dir becomes a zombie.
 * Zombies are *not* in the hashtable, because somebody could re-creat()
 * that filename in it's parent dir again.
 * Besides coding errors during beta phase, when forcing an umount()
 * (e.g. at shutdown time), inodes could be in use such that the parent
 * dir is cleared, resulting also in zombies.
 */
static /*inline*/ void _d_handle_zombie(struct dentry * entry,
					struct ddir * ddir,
					struct ddir * pdir)
{
	if(entry->d_flag & D_DIR) {
		if(entry->d_flag & D_ZOMBIE) {
			if(!has_sons(ddir)) {
				entry->d_flag &= ~D_ZOMBIE;
				remove_hash(&pdir->dd_zombielist, entry);
				if(!pdir->dd_zombielist &&
				   (entry->d_parent->d_flag & D_ZOMBIE)) {
					d_del(entry->d_parent, D_NORMAL);
				}
			}
		} else if(has_sons(ddir)) {
			entry->d_flag |= D_ZOMBIE;
			insert_hash(&pdir->dd_zombielist, entry);

			/* Not sure when this message should show up... */
			if(!IS_ROOT(entry)) {
				printk("VFS: clearing dcache directory "
				       "with successors\n");
#ifdef DEBUG
				printpath(entry);
				printk(" d_flag=%x alloced=%d negs=%d hashed=%d "
				       "basket=%p zombies=%p\n",
				       entry->d_flag, ddir->dd_alloced,
				       ddir->dd_negs, ddir->dd_hashed,
				       ddir->dd_basketlist, ddir->dd_zombielist);
#endif
			}
		}
	}
}

static /*inline*/ blocking void _d_del(struct dentry * entry,
				       struct anchors * anchor,
				       int flags)
{
	struct dheader ** free;
	struct dheader ** full;
	struct dheader * base = BASE_DHEADER(entry);
	struct ddir * ddir = NULL;
	struct ddir * pdir;
	struct inode * inode = entry->d_flag & D_PRELIMINARY ? NULL : entry->u.d_inode;

#ifdef DEBUG
	if(inode)
		xcheck("_d_del", inode);
#endif
	if(!entry->d_parent) {
		printk("VFS: dcache parent is NULL\n");
		return;
	}
	if(entry->d_flag & D_DIR) {
		free = &anchor->dir_free;
		full = &anchor->dir_full;
	} else {
		free = &anchor->free;
		full = &anchor->full;
	}
	pdir = d_dir(entry->d_parent);
	if(!IS_ROOT(entry))
		_d_remove_from_parent(entry, pdir, inode, flags);

	/* This may block, be careful! _d_remove_from_parent() is
	 * thus called before.
	 *
	 * And there is also another problem, for FS_NO_DCACHE filesystems,
	 * when a recursive_clear hits a directory and performs a dec_ddir()
	 * on that parent (what 'entry' is right now) this could call __iput().
	 * This is ok for the most part, but FS_NO_DCACHE will cause a
	 * redundant call to d_del() for what is now 'entry'.  We do not want
	 * this because this disturbs our state.  So we use a special flag
	 * to notify __iput() of this situation.  -DaveM
	 */
	if(entry->d_flag & D_DIR) {
		entry->d_flag |= D_DDELIP;
		ddir = d_dir(entry);
		recursive_clear(ddir, flags);
		entry->d_flag &= ~D_DDELIP;
	}
	if(IS_ROOT(entry))
		return;

	if(!has_true_sons(pdir))
		dec_ddir(entry->d_parent, entry->d_parent->u.d_inode);

	if(flags & D_NO_FREE) {
		/* Make it re-d_add()able */
		pdir->dd_alloced++;
		entry->d_flag &= D_DIR;
	} else
		_d_handle_zombie(entry, ddir, pdir);
	entry->u.d_inode = NULL;
	if(inode) {
		remove_alias(&inode->i_dentry, entry);
		inode->i_dent_count--;
		if (entry->d_flag & D_DIR)
			dec_ddir(entry, inode);

		if(!(flags & D_NO_CLEAR_INODE) &&
		   !(atomic_read(&inode->i_count) +
		     inode->i_ddir_count +
		     inode->i_dent_count)) {
#ifdef DEBUG
			printk("#");
#endif
			/* This may block also. */
			_clear_inode(inode, 0, 0);
		}
	}
	if(!(flags & D_NO_FREE) && !(entry->d_flag & D_ZOMBIE)) {
		base->free++;
		if(base->free == base->maxfree) {
#ifndef DEBUG
			remove_header(free, base);
			free_page((unsigned long)base);
			goto done;
#endif
		}
		entry->d_next = base->emptylist;
		base->emptylist = entry;
		if(!entry->d_next) {
			remove_header(full, base);
			insert_header(free, base);
		}
#ifdef DEBUG
		x_freed++;
#endif
	}
#ifndef DEBUG
done:
#else
	x_free++;
#endif
}

blocking void d_del(struct dentry * entry, int flags)
{
	int i;

	if(!entry)
		return;
	LOG("d_clear", entry);
	if(entry->d_len >= D_MEDIUM) {
		if(entry->d_len >= D_LARGE) {
			i = 3;
		} else {
			i = 2;
		} 
	} else if(entry->d_len >= D_SMALL) {
		i = 1;
	} else {
		i = 0;
	}
	_d_del(entry, &anchors[i], flags);
}

static inline struct dentry * __dlookup(struct dentry ** base,
					struct qstr * name,
					struct qstr * appendix)
{
	struct dentry * tmp = *base;

	if(tmp && name->len) {
		int totallen = name->len;

		if(appendix)
			totallen += appendix->len;
		do {
			if(tmp->d_len == totallen			&&
			   !(tmp->d_flag & D_DUPLICATE)			&&
			   !strncmp(tmp->d_name, name->name, name->len)	&&
			   (!appendix || !strncmp(tmp->d_name+name->len,
						  appendix->name, appendix->len)))
				return tmp;
			tmp = tmp->d_hash_next;
		} while(tmp != *base);
	}
	return NULL;
}

struct dentry * d_lookup(struct inode * dir,
			 struct qstr * name,
			 struct qstr * appendix)
{
	if(dir->i_dentry) {
		struct ddir * ddir = d_dir(dir->i_dentry);
		struct dentry ** base = d_base_qstr(ddir, name, appendix);

		return __dlookup(base, name, appendix);
	}
	return NULL;
}

static /*inline*/ blocking void _d_insert_to_parent(struct dentry * entry,
						    struct ddir * pdir,
						    struct inode * inode,
						    struct qstr * ininame,
						    int flags)
{
	struct dentry ** base;
	struct dentry * parent = entry->d_parent;

#ifdef DEBUG
	if(!pdir->dd_alloced)
		printk("dd_alloced is 0!\n");
#endif
	base = d_base_qstr(pdir, ininame, NULL);
	if(!(flags & (D_NOCHECKDUP|D_DUPLICATE)) &&
	   __dlookup(base, ininame, NULL)) {
		d_del(entry, D_NO_CLEAR_INODE);
		return;
	}
	if(entry->d_flag & D_HASHED) {
		printk("VFS: dcache entry is already hashed\n");
		return;
	}
	if(!(flags & D_PRELIMINARY))
		pdir->dd_true_hashed++;
	pdir->dd_hashed++;
	insert_hash(base, entry);
	entry->d_flag |= D_HASHED;
	pdir->dd_alloced--;
	if(flags & D_BASKET)
		insert_basket(&pdir->dd_basketlist, entry);

#ifdef DEBUG
	if(inode && inode->i_dentry && (entry->d_flag & D_DIR)) {
		struct dentry * tmp = inode->i_dentry;
		printk("Auweia inode=%p entry=%p (%p %p %s)\n",
		       inode, entry, parent->u.d_inode, parent, parent->d_name);
		printk("entry path="); printpath(entry); printk("\n");
		do {
			TST("auweia",tmp);
			printk("alias path="); printpath(tmp); printk("\n");
			tmp = tmp->d_next;
		} while(tmp != inode->i_dentry);
		printk("\n");
	}
#endif
	if(has_true_sons(pdir))
		inc_ddir(parent, parent->u.d_inode);
	if(!inode && !(flags & D_PRELIMINARY)) {
		insert_alias(&pdir->dd_neglist, entry);
		pdir->dd_negs++;

		/* Don't allow the negative list to grow too much ... */

		/* XXX There is a bad bug here.  We remove the dentry from
		 * XXX the alias list, but later on recursive_clear() and
		 * XXX d_del() have no way of knowing this, they thus try
		 * XXX to remove it again from the alias linked list and we
		 * XXX get a crash.  -DaveM
		 */
#if 0 /* FIXME */
		while(pdir->dd_negs > (pdir->dd_true_hashed >> 1) + 5) {
			struct dentry *removal = pdir->dd_neglist->d_prev;

			remove_alias(&pdir->dd_neglist, removal);
			pdir->dd_negs--;
		}
#endif
	}
}

blocking void d_add(struct dentry * entry, struct inode * inode,
		    struct qstr * ininame, int flags)
{
	struct dentry * parent = entry->d_parent;
	struct qstr dummy;
	struct ddir * pdir;

#ifdef DEBUG
	if(inode)
		xcheck("d_add", inode);
	if(IS_ROOT(entry)) {
		printk("VFS: d_add for root dentry ");
		printpath(entry);
		printk(" -> ");
		if(ininame)
			printk("%s", ininame->name);
		printk("\n");
		return;
	}
	if(!parent)
		panic("d_add with parent==NULL");
	LOG("d_add", entry);
#endif
	if(ininame) {
		if(ininame->len != entry->d_len) {
			printk("VFS: d_add with wrong string length");
			entry->d_len = ininame->len; /* kludge */
		}
		memcpy(entry->d_name, ininame->name, ininame->len);
		entry->d_name[ininame->len] = '\0';
	} else {
		dummy.name = entry->d_name;
		dummy.len = entry->d_len;
		ininame = &dummy;
	}
        if(entry->d_flag & D_HASHED)
		printk("VFS: d_add of already added dcache entry\n");

	pdir = d_dir(parent);
	_d_insert_to_parent(entry, pdir, inode, ininame, flags);
	entry->d_flag |= flags;
	if(inode && !(flags & D_PRELIMINARY)) {
		if(entry->d_flag & D_DIR) {
			if(inode->i_dentry) {
				printk("VFS: creating dcache directory alias\n");
				return;
			}
		}
		insert_alias(&inode->i_dentry, entry);
		inode->i_dent_count++;
	}
	entry->u.d_inode = inode;
}

blocking struct dentry * d_entry(struct dentry * parent,
				 struct qstr * name,
				 struct inode * inode)
{
	struct ddir * pdir = d_dir(parent);
	struct dentry ** base = d_base_qstr(pdir, name, NULL);
	struct dentry * found = __dlookup(base, name, NULL);

	if(!found) {
		int isdir = (inode && S_ISDIR(inode->i_mode));

		found = d_alloc(parent, name->len, isdir);
		if(found) {
			d_add(found, inode, name,
			      isdir ? (D_DIR|D_NOCHECKDUP) : D_NOCHECKDUP);
		} else
			printk("VFS: problem with d_alloc\n");
	}
	return found;
}

blocking void d_entry_preliminary(struct dentry * parent,
				  struct qstr * name,
				  unsigned long ino)
{
	struct ddir * pdir = d_dir(parent);
	struct dentry ** base = d_base_qstr(pdir, name, NULL);
	struct dentry * found = __dlookup(base, name, NULL);

	if(!found && ino) {
		struct dentry * new = d_alloc(parent, name->len, 0);

		if(new) {
			d_add(new, NULL, name, D_PRELIMINARY|D_NOCHECKDUP);
			new->u.d_ino = ino;
		} else
			printk("VFS: problem with d_alloc\n");
	}
}

blocking void d_move(struct dentry * entry, struct inode * newdir, 
		     struct qstr * newname, struct qstr * newapp)
{
	struct ddir tmp;
	struct dentry * new;
	struct inode * inode;
	int len;
	int flags;

	if(!entry)
		return;
	inode = entry->u.d_inode;
	flags = entry->d_flag;
	if((flags & D_PRELIMINARY) || !inode) {
		if(!(flags & D_PRELIMINARY))
			printk("VFS: trying to move negative dcache entry\n");
		d_del(entry, D_NO_CLEAR_INODE);
		return;
	}
#if 0
printk("d_move %p '%s' -> '%s%s' dent_count=%d\n", inode, entry->d_name,
       newname->name, newapp ? newapp->name : "", inode->i_dent_count);
#endif
	if(flags & D_ZOMBIE) {
		printk("VFS: moving zombie entry\n");
	}
	if(flags & D_DIR) {
		struct ddir * ddir = d_dir(entry);

		memcpy(&tmp, ddir, sizeof(struct ddir));

                /* Simulate empty dir for d_del(). */
		memset(ddir, 0, sizeof(struct ddir));
	}
	len = newname->len;
	if(newapp) {
		len += newapp->len;
		flags |= D_BASKET;
	} else
		flags &= ~D_BASKET;
	new = d_alloc(newdir->i_dentry, len, flags & D_DIR);
	memcpy(new->d_name, newname->name, newname->len);
	if(newapp)
		memcpy(new->d_name+newname->len, newapp->name, newapp->len);
	new->d_name[len] = '\0';
	d_del(entry, D_NO_CLEAR_INODE);
	d_add(new, inode, NULL, flags & (D_DIR|D_BASKET));
	if(flags & D_DIR) {
		struct ddir * ddir = d_dir(new);

		memcpy(ddir, &tmp, sizeof(struct ddir));
       	}
}

int d_path(struct dentry * entry, struct inode * chroot, char * buf)
{
	if(IS_ROOT(entry) || (chroot && entry->u.d_inode == chroot &&
			      !(entry->d_flag & D_PRELIMINARY))) {
		*buf = '/';
		return 1;
	} else {
		int len = d_path(entry->d_parent, chroot, buf);

		buf += len;
		if(len > 1) {
			*buf++ = '/';
			len++;
		}
		memcpy(buf, entry->d_name, entry->d_len);
		return len + entry->d_len;
	}
}

struct dentry * d_basket(struct dentry * dir_entry)
{
	if(dir_entry && (dir_entry->d_flag & D_DIR)) {
		struct ddir * ddir = d_dir(dir_entry);

		return ddir->dd_basketlist;
	} else
		return NULL;
}

int d_isbasket(struct dentry * entry)
{
	return entry->d_flag & D_BASKET;
}

blocking struct inode * d_inode(struct dentry ** changing_entry)
{
	struct dentry * entry = *changing_entry;
	struct inode * inode;

#ifdef CONFIG_DCACHE_PRELOAD
	if(entry->d_flag & D_PRELIMINARY) {
		struct qstr name = { entry->d_name, entry->d_len };
		struct ddir * pdir = d_dir(entry->d_parent);
		struct dentry ** base = d_base_qstr(pdir, &name, NULL);
		struct dentry * found;
		unsigned long ino;
		struct inode * dir = entry->d_parent->u.d_inode;
		TST("d_inode",entry);
		ino = entry->u.d_ino;
		if(!dir)
			d_panic();

		/* Prevent concurrent d_lookup()s or d_inode()s before
		 * giving up vfs_lock. This just removes from the parent,
		 * but does not deallocate it.
		 */

		/* !!!!!!! Aiee, here is an unresolved race if somebody
		 * unlink()s the inode during the iget(). The problem is
		 * that we need to synchronize externally. Proposed solution:
		 * put a rw_lock (read-mode) on the parent dir for each
		 * iget(), lookup() and so on, and a write-mode lock for
		 * everything that changes the dir (e.g. unlink()), and do
		 * this consistently everywhere in the generic VFS (not in
		 * the concrete filesystems). This should kill similar
		 * races everywhere, with a single clean concept.
		 * Later, the synchronization stuff can be cleaned out
		 * of the concrete fs'es.
		 */
		d_del(entry, D_NO_CLEAR_INODE|D_NO_FREE);
		vfs_unlock();

		/* This circumvents the normal lookup() of pathnames.
		 * Therefore,  preliminary entries must not be used
		 * (see FS_NO_DCACHE and FS_NO_PRELIM) if the fs does not
		 * permit fetching *valid* inodes with plain iget().
		 */
		inode = __iget(dir->i_sb, ino, 0);
		vfs_lock();
		if(!inode) {
			printk("VFS: preliminary dcache entry was invalid\n");
			*changing_entry = NULL;
			return NULL;
		}
		xcheck("d_inode iget()", inode);
		if((found = __dlookup(base, &name, NULL))) {
			d_del(entry, D_NO_CLEAR_INODE);
			*changing_entry = found;
		} else if(S_ISDIR(inode->i_mode)) {
			struct dentry * new = d_alloc(entry->d_parent, entry->d_len, 1);
			if(new)
				d_add(new, inode, &name, D_DIR);
			*changing_entry = new;

			/* Finally deallocate old entry. */
			d_del(entry, D_NO_CLEAR_INODE);
		} else {
			/* Re-insert to the parent, but now as normal dentry. */
			d_add(entry, inode, NULL, 0);
		}
		return inode;
	}
#endif
	inode = entry->u.d_inode;
	if(inode) {
#ifdef DEBUG
		xcheck("d_inode", inode);
#endif
		iinc_zero(inode);
	}
	return inode;
}