~kris/9p

9hist

ref: 3dc4b63048cdc4a84aa83bfb9b09fd751fb9d077 9hist/port/devroot.c -rw-r--r-- 2.8 KiB
3dc4b630 — David du Colombier Plan 9 from Bell Labs 1992-07-27 34 years ago
                                                                                
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
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"devtab.h"

enum{
	Qdir,
	Qbin,
	Qdev,
	Qenv,
	Qproc,
	Qnet,

	Qboot,
	Qcfs,
	Qkfs,
};

extern long	cfslen;
extern ulong	cfscode[];
extern long	kfslen;
extern ulong	kfscode[];
extern ulong	bootlen;
extern ulong	bootcode[];

Dirtab rootdir[]={
	"bin",		{Qbin|CHDIR},	0,			0777,
	"boot",		{Qboot},	0,			0777,
	"dev",		{Qdev|CHDIR},	0,			0777,
	"env",		{Qenv|CHDIR},	0,			0777,
	"proc",		{Qproc|CHDIR},	0,			0777,
	"net",		{Qnet|CHDIR},	0,			0777,
};
#define	NROOT	(sizeof rootdir/sizeof(Dirtab))
Dirtab rootpdir[]={
	"cfs",		{Qcfs},		0,			0777,
	"kfs",		{Qkfs},		0,			0777,
};
Dirtab *rootmap[sizeof rootpdir/sizeof(Dirtab)];
int	nroot;

int
rootgen(Chan *c, Dirtab *tab, int ntab, int i, Dir *dp)
{
	USED(ntab);

	if(i >= nroot)
		return -1;

	if(i < NROOT)
		tab = &rootdir[i];
	else
		tab = rootmap[i - NROOT];

	devdir(c, tab->qid, tab->name, tab->length, eve, tab->perm, dp);
	return 1;
}

void
rootreset(void)
{
	int i;

	i = 0;
	if(cfslen)
		rootmap[i++] = &rootpdir[0];
	if(kfslen)
		rootmap[i++] = &rootpdir[1];
	nroot = NROOT + i;
}

void
rootinit(void)
{
}

Chan*
rootattach(char *spec)
{
	return devattach('/', spec);
}

Chan*
rootclone(Chan *c, Chan *nc)
{
	return devclone(c, nc);
}

int	 
rootwalk(Chan *c, char *name)
{
	if(strcmp(name, "..") == 0) {
		c->qid.path = Qdir|CHDIR;
		return 1;
	}
	if((c->qid.path & ~CHDIR) != Qdir)
		return 0;
	return devwalk(c, name, rootdir, nroot, rootgen);
}

void	 
rootstat(Chan *c, char *dp)
{
	devstat(c, dp, rootdir, nroot, rootgen);
}

Chan*
rootopen(Chan *c, int omode)
{
	return devopen(c, omode, rootdir, nroot, rootgen);
}

void	 
rootcreate(Chan *c, char *name, int omode, ulong perm)
{
	USED(c, name, omode, perm);
	error(Eperm);
}

/*
 * sysremove() knows this is a nop
 */
void	 
rootclose(Chan *c)
{
	USED(c);
}

long	 
rootread(Chan *c, void *buf, long n, ulong offset)
{

	switch(c->qid.path & ~CHDIR){
	case Qdir:
		return devdirread(c, buf, n, rootdir, nroot, rootgen);

	case Qboot:		/* boot */
		if(offset >= bootlen)
			return 0;
		if(offset+n > bootlen)
			n = bootlen - offset;
		memmove(buf, ((char*)bootcode)+offset, n);
		return n;

	case Qcfs:		/* cfs */
		if(offset >= cfslen)
			return 0;
		if(offset+n > cfslen)
			n = cfslen - offset;
		memmove(buf, ((char*)cfscode)+offset, n);
		return n;

	case Qkfs:		/* kfs */
		if(offset >= kfslen)
			return 0;
		if(offset+n > kfslen)
			n = kfslen - offset;
		memmove(buf, ((char*)kfscode)+offset, n);
		return n;

	case Qdev:
		return 0;
	}
	return 0;
}

long	 
rootwrite(Chan *c, void *buf, long n, ulong offset)
{
	USED(c, buf, n, offset);
	error(Egreg);
	return 0;	/* not reached */
}

void	 
rootremove(Chan *c)
{
	USED(c);
	error(Eperm);
}

void	 
rootwstat(Chan *c, char *dp)
{
	USED(c, dp);
	error(Eperm);
}