summaryrefslogtreecommitdiffstats
path: root/sys/miscfs/devfs/devfs_back.c
blob: 0a15e403625fa7c89be5335a6d4442b925765f79 (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

/*
 *  Written by Julian Elischer (julian@DIALix.oz.au)
 *
 *	$Header: /home/ncvs/src/sys/miscfs/devfs/devfs_back.c,v 1.3 1995/05/30 08:06:49 rgrimes 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 */

/*
 * 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() /*proto*/
{

	devnm_p devbp;
	dn_p dnp;

	/*
 	 * Allocate and fill out a new backing node
 	 */
	if(!(devbp = (devnm_p)malloc(sizeof(devnm_t),
				M_DEVFSBACK, M_NOWAIT)))
	{
		return ;
	}
	bzero(devbp,sizeof(devnm_t));
	/*
	 * And the devnode associated with it
	 */
	if(!(dnp = (dn_p)malloc(sizeof(devnode_t),
				M_DEVFSNODE, M_NOWAIT)))
	{
		free(devbp,M_DEVFSBACK);
		return ;
	}
	bzero(dnp,sizeof(devnode_t));
	/*
	 * Link the two together
	 */
	devbp->dnp = dnp;
	dnp->links = 1;
	/*
	 * set up the directory node for the root
	 * and put in all the usual entries for a directory node
	 */
	dnp->type = DEV_DIR;
	dnp->links++; /* for .*/
	/* root loops to self */
	dnp->by.Dir.parent = dnp;
	dnp->links++; /* for ..*/
	/*
	 * set up the list of children (none so far)
	 */
	dnp->by.Dir.dirlist = (devnm_p)0;
	dnp->by.Dir.dirlast =
			&dnp->by.Dir.dirlist;
	dnp->by.Dir.myname = devbp;
	/*
	 * set up a pointer to directory type ops
	 */
	dnp->ops = &devfs_vnodeop_p;
	dnp->mode |= 0555;	/* default perms */
	/*
	 * note creation times etc, as now (boot time)
	 */
	TIMEVAL_TO_TIMESPEC(&time,&(dnp->ctime))
	dnp->mtime = dnp->ctime;
	dnp->atime = dnp->ctime;

	/*
	 * and the list of layers
	 */
	devbp->next_front = NULL;
	devbp->prev_frontp = &(devbp->next_front);


	/*
	 * next time, we don't need to do all this
	 */
	dev_root = devbp;
	printf("DEVFS: ready for devices\n");
}

/***********************************************************************\
* 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.					*
* Generally, this MUST be the first function called by any module	*
* as it also calls the initial setup code, in case it has never been	*
* done yet.								*
* 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	devbp;
	char	pathbuf[DEVMAXPATHSIZE];
	char	*path;
	char	*name;
	register char *cp;
	int	retval;


	DBPRINT(("dev_finddir\n"));
	if(!dirnode) dirnode = dev_root->dnp;
	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	*
	\***************************************/
	devbp = dirnode->by.Dir.dirlist;
	while(devbp && strcmp(devbp->name,name))
	{
		devbp = devbp->next;
	}
	if(devbp)
	{	/* check it's a directory */
		if(devbp->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_node(name, dirnode ,DEV_DIR,
					NULL, &devbp))
		{
			return retval;
		}
	}
	if(path)	/* decide whether to recurse more or return */
	{
		return (dev_finddir(path,devbp->dnp,create,dn_pp));
	}
	else
	{
		*dn_pp = devbp->dnp;
		return 0;
	}
}

/***********************************************************************\
* Add a new element to the devfs backing structure. 			*
\***********************************************************************/
int	dev_add_node(char *name, dn_p dirnode, int entrytype, union typeinfo *by, devnm_p *devnm_pp) /*proto*/
{
	devnm_p devbp;
	devnm_p realthing;	/* needed to create an alias */
	dn_p	dnp;
	int	retval;

	DBPRINT(("dev_add_node\n"));
	if(dirnode->type != DEV_DIR) return(ENOTDIR);
	if(strlen(name) > (DEVMAXNAMESIZE - 1)) return (ENAMETOOLONG);

	retval = dev_finddir(name,dirnode,0,&dnp); /*don't create!*/
	dnp = NULL; /*just want the return code..*/
	if(retval != ENOENT) /* only acceptable answer */
		return(EEXIST);
	/*
	 * Allocate and fill out a new backing node
	 */
	if(!(devbp = (devnm_p)malloc(sizeof(devnm_t),
				M_DEVFSBACK, M_NOWAIT)))
	{
		return ENOMEM;
	}
	bzero(devbp,sizeof(devnm_t));
	if(!(dnp = (dn_p)malloc(sizeof(devnode_t),
				M_DEVFSNODE, M_NOWAIT)))
	{
		free(devbp,M_DEVFSBACK);
		return ENOMEM;
	}
	bzero(dnp,sizeof(devnode_t));
	devbp->dnp = dnp;
	dnp->links = 1; /* implicit from our own name-node */

	/*
	 * note the node type we are adding
	 * and set the creation times to NOW
	 * put in it's name
	 * include the implicit link in the count of links to the devnode..
	 * this stops it from being accidentally freed later.
	 */
	strcpy(devbp->name,name);
	dnp->type = entrytype;
	TIMEVAL_TO_TIMESPEC(&time,&(dnp->ctime))
	dnp->mtime = dnp->ctime;
	dnp->atime = dnp->ctime;

	/*
	 * And set up a new 'clones' list (empty)
	 */
	devbp->prev_frontp = &(devbp->next_front);

	/*
	 * Put it on the END of the linked list of directory entries
	 */
	devbp->parent = dirnode;
	devbp->prevp = dirnode->by.Dir.dirlast;
	devbp->next = *(devbp->prevp); /* should be NULL */ /*right?*/
	*(devbp->prevp) = devbp;
	dirnode->by.Dir.dirlast = &(devbp->next);
	dirnode->by.Dir.entrycount++;

	/*
	 * return the answer
	 */
	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.parent = (dn_p)dirnode;
		dnp->by.Dir.myname = devbp;
		/*
		 * 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;
	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;


	case DEV_ALIAS:
		/*
		 * point to the node we want to shadow
		 * Also store the fact we exist so that aliases
		 * can be deleted accuratly when the original node
		 * is deleted.. (i.e. when device is removed)
		 */
		realthing = by->Alias.realthing;
		dnp->by.Alias.realthing = realthing;
		dnp->by.Alias.next = realthing->as.back.aliases;
		realthing->as.back.aliases = devbp;
		realthing->as.back.alias_count++;
		break;
	}

	if(retval = devfs_add_fronts(dirnode->by.Dir.myname/*XXX*/,devbp))
        {
		/*XXX*//* no idea what to do if it fails... */
		return retval;
	}

	*devnm_pp = devbp;
	return 0 ;
}

/***********************************************************************
 * remove all fronts to this dev and also it's aliases,
 * Then remove this node.
 * For now only allow DEVICE nodes to go.. XXX
 * directory nodes are more complicated and may need more work..
 */
int	dev_remove(devnm_p devbp) /*proto*/
{
	devnm_p alias;

	DBPRINT(("dev_remove\n"));
	/*
	 * Check the type of the node.. for now don't allow dirs
	 */
	switch(devbp->dnp->type)
	{
	case DEV_BDEV:
	case DEV_CDEV:
	case DEV_DDEV:
	case DEV_ALIAS:
	case DEV_SLNK:
		break;
	case DEV_DIR:
	default:
		return(EINVAL);
	}
	/*
	 * Free each alias
	 */
	while ( devbp->as.back.alias_count)
	{
		alias = devbp->as.back.aliases;
		devbp->as.back.aliases = alias->dnp->by.Alias.next;
		devbp->as.back.alias_count--;
		devfs_dn_free(alias->dnp);
		free (alias, M_DEVFSBACK);
	}
	/*
	 * Now remove front items of the Main node itself
	 */
	devfs_remove_fronts(devbp);

	/*
	 * now we should free the main node
	 */
	devfs_dn_free(devbp->dnp);
	free (devbp, M_DEVFSBACK);
	return 0;
}

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)
	{
		printf("devfs node reference count bogus\n");
		Debugger("devfs_dn_free");
		return;
	}
	if(--dnp->links == 0 )
	{
	        devfs_dropvnode(dnp);
	        free (dnp, M_DEVFSNODE);
	}
}
/***********************************************************************\
* 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 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_node(name, dnp, DEV_CDEV,
				&by,&new_dev))
			return 0;
		break;
	case	DV_BLK:
		major = get_bdev_major_num(funct);
		by.Bdev.bdevsw = bdevsw + major;
		by.Bdev.dev = makedev(major, minor);
		if( dev_add_node(name, dnp, DEV_BDEV,
				&by, &new_dev))
			return 0;
		break;
	default:
		return(0);
	}
	new_dev->dnp->gid = gid;
	new_dev->dnp->uid = uid;
	new_dev->dnp->mode |= perms;
	return new_dev;
}



OpenPOWER on IntegriCloud