~kris/9p

9hist

ref: ed9c354533985ee042cd567157da3dd3e240a6c8 9hist/port/devroot.c -rw-r--r-- 2.9 KiB
ed9c3545 — David du Colombier Plan 9 from Bell Labs 2000-01-25 26 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

enum{
	Qdir=	0,

	Nfiles=32,	/* max root files */
};

extern ulong	bootlen;
extern uchar	bootcode[];

Dirtab rootdir[Nfiles];

static uchar	*rootdata[Nfiles];
static int	nroot = 0;

typedef struct Recover Recover;
struct Recover
{
	int	len;
	char	*req;
	Recover	*next;
};

struct
{
	Lock;
	QLock;
	Rendez;
	Recover	*q;
}reclist;

/*
 *  add a root file
 */
static void
addroot(char *name, uchar *contents, ulong len, int perm)
{
	Dirtab *d;

	if(nroot >= Nfiles)
		panic("too many root files");
	rootdata[nroot] = contents;
	d = &rootdir[nroot];
	strcpy(d->name, name);
	d->length = len;
	d->perm = perm;
	d->qid.path = nroot+1;
	if(perm & CHDIR)
		d->qid.path |= CHDIR;
	nroot++;
}

/*
 *  add a root file
 */
void
addrootfile(char *name, uchar *contents, ulong len)
{
	addroot(name, contents, len, 0555);
}

/*
 *  add a root file
 */
static void
addrootdir(char *name)
{
	addroot(name, nil, 0, CHDIR|0555);
}

static void
rootreset(void)
{
	addrootdir("bin");
	addrootdir("dev");
	addrootdir("env");
	addrootdir("proc");
	addrootdir("net");
	addrootdir("net.alt");
	addrootdir("root");
	addrootdir("srv");
	addrootdir("xsrv");

	addrootfile("boot", bootcode, bootlen);	/* always have a boot file */
}

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

static 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, devgen);
}

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

static Chan*
rootopen(Chan *c, int omode)
{
	switch(c->qid.path & ~CHDIR) {
	default:
		break;
	}

	return devopen(c, omode, rootdir, nroot, devgen);
}

/*
 * sysremove() knows this is a nop
 */
static void
rootclose(Chan *c)
{
	switch(c->qid.path) {
	default:
		break;
	}
}

static int
rdrdy(void*)
{
	return reclist.q != 0;
}

static long
rootread(Chan *c, void *buf, long n, vlong off)
{
	ulong t;
	Dirtab *d;
	uchar *data;
	ulong offset = off;

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

	d = &rootdir[t-1];
	data = rootdata[t-1];
	if(offset >= d->length)
		return 0;
	if(offset+n > d->length)
		n = d->length - offset;
	memmove(buf, data+offset, n);
	return n;
}

static long
rootwrite(Chan *c, void*, long, vlong)
{
	switch(c->qid.path & ~CHDIR){
	default:
		error(Egreg);
	}
	return 0;
}

static void
rootcreate(Chan *c, char *name, int mode, ulong perm)
{
	if(!iseve() || c->qid.path != (CHDIR|Qdir) ||
	   (perm & CHDIR) == 0 || mode != OREAD)
		error(Eperm);
	addrootdir(name);
	c->flag |= COPEN;
	c->mode = OREAD;
}

Dev rootdevtab = {
	'/',
	"root",

	rootreset,
	devinit,
	rootattach,
	devclone,
	rootwalk,
	rootstat,
	rootopen,
	rootcreate,
	rootclose,
	rootread,
	devbread,
	rootwrite,
	devbwrite,
	devremove,
	devwstat,
};