summaryrefslogtreecommitdiffstats
path: root/sys/miscfs/devfs/devfs_tree.c
blob: ae6b2d0acad78bdf748a1210de9453ecc846a2dd (plain)
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

/*
 *  Written by Julian Elischer (julian@DIALix.oz.au)
 *
 *	$Header: /home/ncvs/src/sys/miscfs/devfs/devfs_tree.c,v 1.5 1995/09/08 04:46:14 julian Exp $
 */

#include "param.h"
#include "systm.h"
#include "types.h"
#include "kernel.h"
#include "file.h"		/* define FWRITE ... */
#include "conf.h"
#include "stat.h"
#include "mount.h"
#include "vnode.h"
#include "malloc.h"
#include "dir.h"		/* defines dirent structure		*/
#include "devfsdefs.h"
#include "sys/devfsext.h"

SYSINIT(devfs, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_sinit, NULL)

devnm_p	dev_root;		/* root of the backing tree */
struct mount *devfs_hidden_mount;
int devfs_up_and_going; 

/*
 * Set up the root directory node in the backing plane
 * This is happenning before the vfs system has been
 * set up yet, so be careful about what we reference..
 * Notice that the ops are by indirection.. as they haven't
 * been set up yet!
 */
void  devfs_sinit(caddr_t junk) /*proto*/
{
	int retval; /* we will discard this */
	devnm_p new;
	/*
	 * call the right routine at the right time with the right args....
	 */
	retval = dev_add_entry("root", NULL, DEV_DIR, NULL, &dev_root);
#ifdef PARANOID
	if(retval) panic("devfs_sinit: dev_add_entry failed ");
#endif
	devfs_hidden_mount = (struct mount *)malloc(sizeof(struct mount),
							M_MOUNT,M_NOWAIT);
#ifdef PARANOID
	if(!devfs_hidden_mount) panic("devfs_sinit: malloc failed");
#endif
	bzero(devfs_hidden_mount,sizeof(struct mount));
	devfs_mount(devfs_hidden_mount,"dummy",NULL,NULL,NULL);
	dev_root->dnp->dvm = (struct devfsmount *)devfs_hidden_mount->mnt_data;
	devfs_up_and_going = 1;
	printf("DEVFS: ready for devices\n");
}

/***********************************************************************\
*************************************************************************
*	Routines used to find our way to a point in the tree		*
*************************************************************************
\***********************************************************************/


/***************************************************************\
* Search down the linked list off a dir to find "name"		*
* return the dn_p for that node.
\***************************************************************/
devnm_p dev_findname(dn_p dir,char *name) /*proto*/
{
	devnm_p newfp;
	DBPRINT(("	dev_findname(%s)\n",name));
	if(dir->type != DEV_DIR) return 0;/*XXX*/ /* printf?*/

	if(name[0] == '.')
	{
		if(name[1] == 0)
		{
			return dir->by.Dir.myname;
		}
		if((name[1] == '.') && (name[2] == 0))
		{
			/* for root, .. == . */
			return dir->by.Dir.parent->by.Dir.myname;
		}
	}
	newfp = dir->by.Dir.dirlist;
	while(newfp)
	{
		if(!(strcmp(name,newfp->name)))
			return newfp;
		newfp = newfp->next;
	}
	return NULL;
}

/***********************************************************************\
* Given a starting node (0 for root) and a pathname, return the node	*
* for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE'	*
* option is true, then create any missing nodes in the path and create	*
* and return the final node as well.					*
* This is used to set up a directory, before making nodes in it..	*
*									*
* Warning: This function is RECURSIVE.					*
*	char	*path,		 find this dir (err if not dir)		*
*	dn_p	dirnode,	 starting point  (0 = root)	 	*
*	int	create,		 create path if not found 		*
*	dn_p	*dn_pp)		 where to return the node of the dir	*
\***********************************************************************/
int	dev_finddir(char *orig_path, dn_p dirnode, int create, dn_p *dn_pp) /*proto*/
{
	devnm_p	devnmp;
	dn_p	dnp;
	char	pathbuf[DEVMAXPATHSIZE];
	char	*path;
	char	*name;
	register char *cp;
	int	retval;


	DBPRINT(("dev_finddir\n"));
	/***************************************\
	* If no parent directory is given	*
	* then start at the root of the tree	*
	\***************************************/
	if(!dirnode) dirnode = dev_root->dnp;

	/***************************************\
	* Sanity Checks				*
	\***************************************/
	if(dirnode->type != DEV_DIR) return ENOTDIR;
	if(strlen(orig_path) > (DEVMAXPATHSIZE - 1)) return ENAMETOOLONG;


	path = pathbuf;
	strcpy(path,orig_path);
	while(*path == '/') path++;	/* always absolute, skip leading / */
	/***************************************\
	* find the next segment of the name	*
	\***************************************/
	cp = name = path;
	while((*cp != '/') && (*cp != 0))
	{
		cp++;
	}
	/***********************************************\
	* Check to see if it's the last component	*
	\***********************************************/
	if(*cp)
	{
		path = cp + 1;	/* path refers to the rest */
		*cp = 0; 	/* name is now a separate string */
		if(!(*path))
		{
			path = (char *)0; /* was trailing slash */
		}
	}
	else
	{
		path = (char *)0;	/* no more to do */
	}

	/***************************************\
	* Start scanning along the linked list	*
	\***************************************/
	devnmp = dev_findname(dirnode,name);
	if(devnmp)
	{	/* check it's a directory */
		dnp = devnmp->dnp;
		if(dnp->type != DEV_DIR) return ENOTDIR;
	}
	else
	{
		/***************************************\
		* The required element does not exist	*
		* So we will add it if asked to.	*
		\***************************************/
		if(!create) return ENOENT;

		if(retval = dev_add_entry(name, dirnode, DEV_DIR,
					NULL, &devnmp))
		{
			return retval;
		}
		dnp = devnmp->dnp;
	}
	if(path)	/* decide whether to recurse more or return */
	{
		return (dev_finddir(path,dnp,create,dn_pp));
	}
	else
	{
		*dn_pp = dnp;
		return 0;
	}
}


/***********************************************************************\
* Add a new element to the devfs backing structure. 			*
* If we're creating a root node, then dirname is NULL			*
*									*
* Creates a name node, and links it to the supplied node		*
\***********************************************************************/
int	dev_add_name(char *name, dn_p dirnode, devnm_p back, dn_p dnp, devnm_p *devnm_pp) /*proto*/
{
	devnm_p devnmp;
	devnm_p realthing;	/* needed to create an alias */
	int	retval;

	DBPRINT(("dev_add_name\n"));
	if(dirnode ) {
		if(dirnode->type != DEV_DIR) return(ENOTDIR);
	
		if( dev_findname(dirnode,name))
			return(EEXIST);
	}
	/*
	 * make sure the name is legal
	 * slightly misleading in the case of NULL
	 */
	if( !name || (strlen(name) > (DEVMAXNAMESIZE - 1)))
			return (ENAMETOOLONG);
	/*
	 * Allocate and fill out a new directory entry 
	 */
	if(!(devnmp = (devnm_p)malloc(sizeof(devnm_t),
				M_DEVFSNAME, M_NOWAIT)))
	{
		return ENOMEM;
	}
	bzero(devnmp,sizeof(devnm_t));

	/* inherrit our parent's mount info */ /*XXX*/
	/* a kludge but.... */
	if(dirnode && ( dnp->dvm == NULL)) {
		dnp->dvm = dirnode->dvm;
		if(!dnp->dvm) printf("parent had null dvm ");
	}

	/*
	 * Link the two together
	 * include the implicit link in the count of links to the devnode..
	 * this stops it from being accidentally freed later.
	 */
	devnmp->dnp = dnp;
	dnp->links++ ; /* implicit from our own name-node */
	if(dnp->type == DEV_DIR) {
		dnp->by.Dir.myname = devnmp;
		/*
		 * If we are unlinking from an old dir, decrement it's links
		 * as we point our '..' elsewhere
		 */
		if(dnp->by.Dir.parent) {
			dnp->by.Dir.parent->links--;
		}
	 	if(dirnode) {
			dnp->by.Dir.parent = dirnode;
		} else {
			dnp->by.Dir.parent = dnp;
		}
		dnp->by.Dir.parent->links++; /* account for the new '..' */
	}

	/*
	 * put in it's name
	 */
	strcpy(devnmp->name,name);


	/*
	 * And set up the 'clones' list (empty if new node)
	 */
	if(back) {
		/*******************************************************\
		* Put it in the appropriate back/front list too.	*
		\*******************************************************/
		devnmp->next_front = *back->prev_frontp;
		devnmp->prev_frontp = back->prev_frontp;
		*back->prev_frontp = devnmp;
		back->prev_frontp = &(devnmp->next_front);
		devnmp->as.front.realthing = back;
		
	} else {
		devnmp->prev_frontp = &(devnmp->next_front);
		devnmp->next_front = NULL;
	}


	/*******************************************
	 * Check if we are not making a root node..
	 * (i.e. have parent)
	 */
	if(dirnode) {
		/*
	 	 * Put it on the END of the linked list of directory entries
	 	 */
		devnmp->parent = dirnode; /* null for root */
		devnmp->prevp = dirnode->by.Dir.dirlast;
		devnmp->next = *(devnmp->prevp); /* should be NULL */ /*right?*/
		*(devnmp->prevp) = devnmp;
		dirnode->by.Dir.dirlast = &(devnmp->next);
		dirnode->by.Dir.entrycount++;
		dirnode->len += strlen(name) + 8;/*ok, ok?*/
	}

	/*
	 * If we have a parent, then maybe we should duplicate
	 * ourselves onto any plane that the parent is on...
	 * Though this may be better handled elsewhere as
	 * it stops this routine from being used for front nodes
	 */
	if(dirnode && !back) {
		if(retval = devfs_add_fronts(dirnode->by.Dir.myname,devnmp))
        	{
			/*XXX*//* no idea what to do if it fails... */
			return retval;
		}
	}

	*devnm_pp = devnmp;
	return 0 ;
}


/***********************************************************************\
* Add a new element to the devfs backing structure. 			*
*									*
* Creates a new dev_node to go with it					*
* 'by' gives us info to make our node					*
* note the 'links' count is 0 (except if a dir)				*
* but it is only cleared on a transition				*
* so this is ok till we link it to something				*
\***********************************************************************/
int	dev_add_node(int entrytype, union typeinfo *by, dn_p proto, dn_p *dn_pp) /*proto*/
{
	dn_p	dnp;
	int	retval;

	DBPRINT(("dev_add_node\n"));
	if(!(dnp = (dn_p)malloc(sizeof(devnode_t),
			M_DEVFSNODE, M_NOWAIT)))
	{
		return ENOMEM;
	}
	if(proto) {
		/*  XXX should check that we are NOT copying a device node */
		bcopy(proto, dnp, sizeof(devnode_t));
		/* some things you DON'T copy */
		dnp->links = 0;
		dnp->dvm = NULL;
		dnp->vn = NULL;
		dnp->len = 0;
	} else {
		bzero(dnp,sizeof(devnode_t));
		dnp->type = entrytype;
		TIMEVAL_TO_TIMESPEC(&time,&(dnp->ctime))
		dnp->mtime = dnp->ctime;
		dnp->atime = dnp->ctime;
	}
	/*
	 * fill out the dev node according to type
	 */
	switch(entrytype) {
	case DEV_DIR:
		/*
		 * As it's a directory, make sure
		 * it has a null entries list
		 */
		dnp->by.Dir.dirlast = &(dnp->by.Dir.dirlist);
		dnp->by.Dir.dirlist = (devnm_p)0;
		dnp->by.Dir.entrycount = 0;
		/*  until we know better, it has a null parent pointer*/
		dnp->by.Dir.parent = NULL;
		dnp->links++; /* for .*/
		dnp->by.Dir.myname = NULL;
		/*
		 * make sure that the ops associated with it are the ops
		 * that we use (by default) for directories
		 */
		dnp->ops = &devfs_vnodeop_p;
		dnp->mode |= 0555;	/* default perms */
		break;
	/*******************************************************\
	* The rest of these can't happen except in the back plane*
	\*******************************************************/
	case DEV_BDEV:
		/*
		 * Make sure it has DEVICE type ops
		 * and device specific fields are correct
		 */
		dnp->ops = &dev_spec_vnodeop_p;
		dnp->by.Bdev.bdevsw = by->Bdev.bdevsw;
		dnp->by.Bdev.dev = by->Bdev.dev;
		break;
	case DEV_CDEV:
		/*
		 * Make sure it has DEVICE type ops
		 * and device specific fields are correct
		 */
		dnp->ops = &dev_spec_vnodeop_p;
		dnp->by.Cdev.cdevsw = by->Cdev.cdevsw;
		dnp->by.Cdev.dev = by->Cdev.dev;
		break;
	case DEV_DDEV:
		/*
		 * store the address of (the address of) the ops
		 * and the magic cookie to use with them
		 */
		dnp->by.Ddev.arg = by->Ddev.arg;
		dnp->ops = by->Ddev.ops;
		break;
	default:
		return EINVAL;
	}

	*dn_pp = dnp;
	return 0 ;
}


/***************************************************************\
* DEV_NODE reference count manipulations.. when a ref count	*
* reaches 0, the node is to be deleted				*
\***************************************************************/
int	dev_touch(devnm_p key)		/* update the node for this dev */ /*proto*/
{
	DBPRINT(("dev_touch\n"));
	TIMEVAL_TO_TIMESPEC(&time,&(key->dnp->mtime))
	return 0; /*XXX*/
}

void	devfs_dn_free(dn_p dnp) /*proto*/
{
	if(--dnp->links <= 0 ) /* can be -1 for initial free, on error */
	{
		/*probably need to do other cleanups XXX */
	        devfs_dropvnode(dnp);
	        free (dnp, M_DEVFSNODE);
	}
}

/***********************************************************************\
*	Front Node Operations						* 
*	Add or delete a chain of front nodes				*
\***********************************************************************/

/***********************************************************************\
* Given a directory backing node, and a child backing node, add the	*
* appropriate front nodes to the front nodes of the directory to	*
* represent the child node to the user					*
*									*
* on failure, front nodes will either be correct or not exist for each	*
* front dir, however dirs completed will not be stripped of completed	*
* frontnodes on failure of a later frontnode				*
*									*
* This allows a new node to be propogated through all mounted planes	*
*									*
\***********************************************************************/
int devfs_add_fronts(devnm_p parent,devnm_p child) /*proto*/
{
	devnm_p newnmp;
	devnm_p  falias;
	dn_p	dnp = child->dnp;
	int type = child->dnp->type;

	DBPRINT(("	devfs_add_fronts\n"));
	/***********************************************\
	* Find the frontnodes of the parent node	*
	\***********************************************/
	for (falias = parent->next_front; falias; falias = falias->next_front)
	{
		/*
		 * If a Dir (XXX symlink too one day)
		 * Make the node, using the original as a prototype)
		 */
		if(type == DEV_DIR) {
			if (dev_add_node(type, NULL, dnp, &dnp))
			{
				printf("Device %s: node allocation failed\n",
					child->name);
				continue;
			}
		}
		if (dev_add_name(child->name,parent->dnp,child, dnp,&newnmp)) {
			if( type == DEV_DIR) {
				devfs_dn_free(dnp); /* 1->0 */
			}
			printf("Device %s: allocation failed\n", child->name);
			continue;
		}

	}
	return 0;	/* for now always succeed */
}

/***********************************************************************
 * remove all instances of this devicename [for backing nodes..]
 * note.. if there is another link to the node (non dir nodes only)
 * then the devfs_node will still exist as the ref count will be non-0
 * removing a directory node will remove all sup-nodes on all planes (ZAP)
 *
 * Used be device drivers to remove nodes that are no longer relevant
 ***********************************************************************/
void	dev_remove_dev(devnm_p devnmp) /*proto*/
{
	DBPRINT(("dev_remove_dev\n"));
	/*
	 * Keep removing the next front node till no more exist
	 */
	while(devnmp->next_front)
	{
		dev_free_name(devnmp->next_front);
	}
	/*
	 * then free the main node
	 */
	dev_free_name(devnmp);
	return ;
}

/***************************************************************
 * duplicate the backing tree into a tree of nodes hung off the
 * mount point given as the argument. Do this by
 * calling dev_dup_entry which recurses all the way
 * up the tree..
 * If we are the first plane, just return the base root 
 **************************************************************/
int dev_dup_plane(struct devfsmount *devfs_mp_p) /*proto*/
{
	devnm_p	new;
	int	error = 0;

	DBPRINT(("	dev_dup_plane\n"));
	if(devfs_up_and_going) {
		if(error = dev_dup_entry(NULL, dev_root, &new, devfs_mp_p)) {
			return error;
		}
	} else { /* we are doing the dummy mount during initialisation.. */
		new = dev_root;
	}
	devfs_mp_p->plane_root = new;

	return error;
}



/***************************************************************\
* Free a whole plane
\***************************************************************/
void  devfs_free_plane(struct devfsmount *devfs_mp_p) /*proto*/
{
	devnm_p devnmp;

	DBPRINT(("	devfs_free_plane\n"));
	devnmp = devfs_mp_p->plane_root;
	if(devnmp) dev_free_name(devnmp);
	devfs_mp_p->plane_root = NULL;
}

/***************************************************************\
* Create and link in a new front element.. 			*
* Parent can be 0 for a root node				*
* Not presently usable to make a symlink XXX			*
* recursively will create subnodes corresponding to equivalent	*
* child nodes in the base level					*
\***************************************************************/
int dev_dup_entry(dn_p parent, devnm_p back, devnm_p *dnm_pp, struct devfsmount *dvm) /*proto*/
{
	devnm_p	newnmp;
	struct	devfsmount *dmt;
	devnm_p	newback;
	devnm_p	newfront;
	int	error;
	dn_p	dnp = back->dnp;
	int type = back->dnp->type;

	DBPRINT(("	dev_dup_entry\n"));
	/*
	 * go get the node made (if we need to)
	 * use the back one as a prototype
	 */
	if ( type == DEV_DIR) {
		error = dev_add_node( dnp->type, NULL, dnp, &dnp);
		if(error) {
			printf("dev_dup_entry: node alloc failed\n");
			return error;
		}
	} 
	error = dev_add_name(back->name,parent,back,dnp,&newnmp);
	if ( error ) {
		if ( type == DEV_DIR) {
			devfs_dn_free(dnp); /* 1->0 */
		}
		return error;
	}

	/*
	 * If we have just made the root, then insert the pointer to the
	 * mount information
	 */
	if(dvm) {
		newnmp->dnp->dvm = dvm;
	}

	/*
	 * If it is a directory, then recurse down all the other
	 * subnodes in it....
	 * note that this time we don't pass on the mount info..
	 */
	if ( newnmp->dnp->type == DEV_DIR)
	{
		for(newback = back->dnp->by.Dir.dirlist;
				newback; newback = newback->next)
		{
			if(error = dev_dup_entry(newnmp->dnp,
						newback, &newfront, NULL))
			{
				break; /* back out with an error */
			}
		}
	}
	*dnm_pp = newnmp;
	return error;
}

/***************************************************************\
* Free a name node (and any below it of it's a directory node)	*
* remember that if there are other names pointing to the	*
* dev_node then it may not get freed yet			*
* can handle if there is no dnp 				*
\***************************************************************/
void dev_free_name(devnm_p devnmp) /*proto*/
{
	dn_p	parent = devnmp->parent;
	dn_p	dnp = devnmp->dnp;
	devnm_p	back;

	DBPRINT(("	dev_free_name\n"));
	if(dnp) {
		if(dnp->type == DEV_DIR)
		{
			while(dnp->by.Dir.dirlist)
			{
				dev_free_name(dnp->by.Dir.dirlist);
			}
			/*
			 * drop the reference counts on our and our parent's
			 * nodes for "." and ".." (root has ".." -> "." )
			 */
			devfs_dn_free(dnp);	/* account for '.' */
			devfs_dn_free(dnp->by.Dir.parent); /* '..' */
			/* should only have one reference left
				(from name element) */
		}
		devfs_dn_free(dnp);
	}
	/*
	 * unlink ourselves from the directory on this plane
	 */
	if(parent) /* if not fs root */
	{
		if( *devnmp->prevp = devnmp->next)/* yes, assign */
		{
			devnmp->next->prevp = devnmp->prevp;
		}
		else
		{
			parent->by.Dir.dirlast
				= devnmp->prevp;
		}
		parent->by.Dir.entrycount--;
		parent->len -= strlen(devnmp->name) + 8;
	}

	/*
	 * If the node has a backing pointer we need to free ourselves
	 * from that..
	 * Remember that we may not HAVE a backing node.
	 */
	if (back = devnmp->as.front.realthing) /* yes an assign */
	{
		if( *devnmp->prev_frontp = devnmp->next_front)/* yes, assign */
		{
			devnmp->next_front->prev_frontp = devnmp->prev_frontp;
		}
		else
		{
			back->prev_frontp = devnmp->prev_frontp;
		}
	}
	/***************************************************************\
	* If the front node has it's own devnode structure,		*
	* then free it.							*
	\***************************************************************/
	free(devnmp,M_DEVFSNAME);
	return;
}

/*******************************************************\
*********************************************************
* ROUTINES to control the connection between devfs	*
* nodes and the system's vnodes				*
*********************************************************
\*******************************************************/

/*******************************************************\
* Theoretically this could be called for any kind of 	*
* vnode, however in practice it must be a DEVFS vnode	*
\*******************************************************/
int devfs_vntodn(struct vnode *vn_p, dn_p *dn_pp) /*proto*/
{

DBPRINT(("	vntodn "));
	if(vn_p->v_tag != VT_DEVFS)
	{
		printf("bad-tag ");
		Debugger("bad-tag ");
		return(EINVAL);
	}
	if(vn_p->v_usecount == 0)
	{
		printf("not locked! ");
	}
	if((vn_p->v_type == VBAD) || (vn_p->v_type == VNON))
	{
		printf("bad-type ");
		return(EINVAL);
	}
	*dn_pp = (dn_p)vn_p->v_data;

	return(0);
}

/***************************************************************\
* given a dev_node, find the appropriate vnode if one is already*
* associated, or get a new one an associate it with the dev_node*
* need to check about vnode references.. should we increment it?*
\***************************************************************/
int devfs_dntovn(dn_p dnp, struct vnode **vn_pp) /*proto*/
{
	struct vnode *vn_p, *nvp;
	int error = 0;

	vn_p = dnp->vn;
DBPRINT(("dntovn "));
	if( vn_p)
	{
		if(vn_p->v_id != dnp->vn_id)
		{
			printf("bad-id ");
			goto skip;
		}
		if(vn_p->v_tag != VT_DEVFS)
		{
			printf("bad-tag ");
			goto skip;
		}
		if(vn_p->v_op != *(dnp->ops))
		{
			printf("bad-ops ");
			goto skip;
		}
		if((dn_p)(vn_p->v_data) != dnp)
		{
			printf("bad-rev_link ");
			goto skip;
		}
		if(vn_p->v_type != VNON)
		{
			vget(vn_p,0/*lockflag ?*/); /*XXX*/
			*vn_pp = vn_p;
			return(0);
		}
		else
		{
			printf("bad-type");
		}
skip:
		vn_p = (struct vnode *) 0;
	}
	if(!(error = getnewvnode(VT_DEVFS,
			dnp->dvm->mount,
			*(dnp->ops),
			&vn_p)))
	{
		dnp->vn = vn_p;
		dnp->vn_id = vn_p->v_id;
		*vn_pp = vn_p;
DBPRINT(("(New vnode)"));
		switch(dnp->type)
		{
		case	DEV_SLNK:
			break;
		case	DEV_DIR:
			if(dnp->by.Dir.parent == dnp)
			{
				vn_p->v_flag |= VROOT;
			}
			vn_p->v_type = VDIR;
			break;
		case	DEV_BDEV:
			vn_p->v_type = VBLK;
			if (nvp = checkalias(vn_p,
			   dnp->by.Bdev.dev,
			  (struct mount *)0))
			{
				vput(vn_p);
				vn_p = nvp;
			}
			break;
		case	DEV_CDEV:
			vn_p->v_type = VCHR;
			if (nvp = checkalias(vn_p,
			   dnp->by.Cdev.dev,
			  (struct mount *)0))
			{
				vput(vn_p);
				vn_p = nvp;
			}
			break;
		case	DEV_DDEV:
			break;
		}
		if ( vn_p)
		{
			vn_p->v_mount  = dnp->dvm->mount;/* XXX Duplicated */
			*vn_pp = vn_p;
			vn_p->v_data = (void *)dnp;
		}
		else
		{
			error = EINVAL;
		}
	}
	return error;
}

/***********************************************************************\
* UTILITY routine:							*
* Return the major number for the cdevsw entry containing the given	*
* address.								*
\***********************************************************************/
int get_cdev_major_num(caddr_t addr)	/*proto*/
{
	int	index = 0;

	DBPRINT(("get_cdev_major_num\n"));
	while (index < nchrdev)
	{
		if(((caddr_t)(cdevsw[index].d_open) == addr)
		 ||((caddr_t)(cdevsw[index].d_read) == addr)
		 ||((caddr_t)(cdevsw[index].d_ioctl) == addr))
		{
			return index;
		}
		index++;
	}
	return -1;
}

int get_bdev_major_num(caddr_t addr)	/*proto*/
{
	int	index = 0;

	DBPRINT(("get_bdev_major_num\n"));
	while (index < nblkdev)
	{
		if(((caddr_t)(bdevsw[index].d_open) == addr)
		 ||((caddr_t)(bdevsw[index].d_strategy) == addr)
		 ||((caddr_t)(bdevsw[index].d_ioctl) == addr))
		{
			return index;
		}
		index++;
	}
	return -1;
}

/***********************************************************************\
* add a whole device, with not prototype.. make name element and node	*
\***********************************************************************/
int dev_add_entry(char *name, dn_p parent, int type, union typeinfo *by, devnm_p *nm_pp) /*proto*/ 
{
	dn_p	dnp;
	int	error = 0;

	DBPRINT(("	devfs_add_entry\n"));
	if (error = dev_add_node(type, by, NULL, &dnp))
	{
		printf("Device %s: base node allocation failed\n", name);
		return error;
	}
	if ( error = dev_add_name(name ,parent ,NULL, dnp, nm_pp))
	{
		devfs_dn_free(dnp); /* 1->0 for dir, 0->(-1) for other */
		printf("Device %s: name slot allocation failed\n",
			name);
		
	}
	return error;
}

/***********************************************************************\
* Add the named device entry into the given directory, and make it 	*
* The appropriate type... (called (sometimes indirectly) by drivers..)	*
\***********************************************************************/
void *dev_add(char *path,
		char *name,
		void *funct,
		int minor,
		int chrblk,
		uid_t uid,
		gid_t gid,
		int perms)
{
	devnm_p	new_dev;
	dn_p	dnp;	/* devnode for parent directory */
	int	retval;
	int major ;
	union	typeinfo by;

	DBPRINT(("dev_add\n"));
	retval = dev_finddir(path,NULL,1,&dnp);
	if (retval) return 0;
	switch(chrblk)
	{
	case	DV_CHR:
		major = get_cdev_major_num(funct);
		by.Cdev.cdevsw = cdevsw + major;
		by.Cdev.dev = makedev(major, minor);
		if( dev_add_entry(name, dnp, DEV_CDEV, &by,&new_dev))
			return NULL;
		break;
	case	DV_BLK:
		major = get_bdev_major_num(funct);
		by.Bdev.bdevsw = bdevsw + major;
		by.Bdev.dev = makedev(major, minor);
		if( dev_add_entry(name, dnp, DEV_BDEV, &by, &new_dev))
			return NULL;
		break;
	default:
		return NULL;
	}
	new_dev->dnp->gid = gid;
	new_dev->dnp->uid = uid;
	new_dev->dnp->mode |= perms;
	return new_dev;
}

/***********************************************************************\
* Add the named device entry into the given directory, and make it 	*
*  a link to the already created device given as an arg..		*
\***********************************************************************/
void *dev_link(char *path, char *name, void *original)
{
	devnm_p	new_dev;
	devnm_p	orig = (devnm_p) original;
	dn_p	dirnode;	/* devnode for parent directory */
	int	retval;
	int major ;
	union	typeinfo by;

	DBPRINT(("dev_add\n"));
	retval = dev_finddir(path,NULL,1,&dirnode);
	if (retval) return 0;
	/*
	 *  The DEV_CDEV below is not used other than it must NOT be DEV_DIR
	 * the correctness of original shuold be checked..
	 */
	if( dev_add_name(name, dirnode, NULL, orig->dnp, &new_dev))
		return NULL;
	return new_dev;
}

OpenPOWER on IntegriCloud