~kris/9p

9hist

ref: f21be7f3245257e4b558e2ed8617f8a19bb1471d 9hist/port/devroot.c -rw-r--r-- 3.8 KiB
f21be7f3 — David du Colombier Plan 9 from Bell Labs 1996-02-02 30 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
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
#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=	0,
	Qbin,
	Qdev,
	Qenv,
	Qproc,
	Qnet,
	Qnetalt,
	Qrecover,
	Qboot,		/* readable files */

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

extern ulong	bootlen;
extern uchar	bootcode[];

Dirtab rootdir[Nfiles]={
	"bin",		{Qbin|CHDIR},	0,	0777,
	"dev",		{Qdev|CHDIR},	0,	0777,
	"env",		{Qenv|CHDIR},	0,	0777,
	"proc",		{Qproc|CHDIR},	0,	0777,
	"net",		{Qnet|CHDIR},	0,	0777,
	"net.alt",	{Qnetalt|CHDIR},	0,	0777,
	"recover",	{Qrecover},	0,	0777,
};

static uchar	*rootdata[Nfiles];
static int	nroot = Qboot - 1;
static int	recovbusy;

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

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

/*
 *  add a root file
 */
void
addrootfile(char *name, uchar *contents, ulong len)
{
	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 = 0555;
	d->qid.path = nroot+1;
	nroot++;
}

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

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

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

Chan*
rootopen(Chan *c, int omode)
{
	switch(c->qid.path & ~CHDIR) {
	default:
		break;
	case Qrecover:
		if(recovbusy)
			error(Einuse);		
		if(strcmp(up->user, eve) != 0)
			error(Eperm);
		recovbusy = 1;
		break;
	}

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

void	 
rootcreate(Chan*, char*, int, ulong)
{
	error(Eperm);
}

/*
 * sysremove() knows this is a nop
 */
void	 
rootclose(Chan *c)
{
	switch(c->qid.path) {
	default:
		break;
	case Qrecover:
		if(c->flag&COPEN)
			recovbusy = 0;
		break;
	}
}

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

long	 
rootread(Chan *c, void *buf, long n, ulong offset)
{
	ulong t;
	Dirtab *d;
	uchar *data;
	Recover *r;

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

		sleep(&reclist, rdrdy, 0);

		lock(&reclist);
		r = reclist.q;
		reclist.q = r->next;
		unlock(&reclist);

		qunlock(&reclist);

		poperror();
		if(n < r->len)
			n = r->len;
		memmove(buf, r->req, n);
		free(r->req);
		free(r);
		return n;
	}

	if(t < Qboot)
		return 0;

	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;
}

Block*
rootbread(Chan *c, long n, ulong offset)
{
	return devbread(c, n, offset);
}

long	 
rootwrite(Chan *c, void *buf, long n, ulong)
{
	char tmp[256];

	switch(c->qid.path & ~CHDIR){
	default:
		error(Egreg);
	case Qrecover:
		if(n > sizeof(tmp)-1)
			error(Etoosmall);
		/* Nul terminate */
		memmove(tmp, buf, n);
		tmp[n] = '\0';
		mntrepl(tmp);
		return n;
	}
	return 0;
}

long
rootbwrite(Chan *c, Block *bp, ulong offset)
{
	return devbwrite(c, bp, offset);
}

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

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

void
rootrecover(Path *p, char *mntname)
{
	int i;
	Recover *r;
	char buf[256];

	r = malloc(sizeof(Recover));
	i = ptpath(p, buf, sizeof(buf));
	r->req = smalloc(i+strlen(mntname)+2);
	sprint(r->req, "%s %s", buf, mntname);
	lock(&reclist);
	r->next = reclist.q;
	reclist.q = r;
	unlock(&reclist);
	wakeup(&reclist);
}