plan 9 kernel history: overview | file list | diff list

1996/0123/port/devtinyfs.c (diff list | history)

1996/0122/sys/src/9/port/devtinyfs.c:13,201996/0123/sys/src/9/port/devtinyfs.c:13,23 (short | long | prev | next)
1996/0116    
 
enum{ 
	Qdir, 
	Nfile=	32, 
1996/0123    
	Nfile=		32, 
1996/0120    
	Qmedium, 
1996/0123    
 
	Magic=		0xfeedbeef, 
	Superlen=	64, 
1996/0116    
}; 
 
1996/0122    
typedef struct FS FS; 
1996/0122/sys/src/9/port/devtinyfs.c:25,331996/0123/sys/src/9/port/devtinyfs.c:28,36
1996/0122    
	int	dev; 
	FS	*next; 
1996/0116    
	Chan	*c; 
1996/0122    
	Dirtab	*file; 
1996/0116    
	int	nfile; 
1996/0122    
	int	maxfile; 
1996/0123    
	uchar	*fat; 
	ulong	nclust; 
	ulong	clustsize; 
1996/0122    
}; 
 
struct { 
1996/0122/sys/src/9/port/devtinyfs.c:39,521996/0123/sys/src/9/port/devtinyfs.c:42,47
1996/0116    
void 
tinyfsreset(void) 
{ 
	Dirtab *d; 
                 
	d = tinyfs.file; 
	memmove(d->name, "medium"); 
	d->qid.vers = 0; 
1996/0120    
	d->qid.path = Qmedium; 
1996/0116    
	d->perm = 0666; 
1996/0120    
	tinyfs.nfile = 1; 
1996/0116    
} 
 
void 
1996/0122/sys/src/9/port/devtinyfs.c:54,591996/0123/sys/src/9/port/devtinyfs.c:49,123
1996/0116    
{ 
} 
 
1996/0123    
#define GETS(x) ((x)[0]|((x)[1]<<8)) 
#define PUTS(x, v) {(x)[0] = (v);(x)[1] = ((v)>>8);} 
 
#define GETL(x) (GETS(x)|(GETS(x+2)<<16)) 
#define PUTL(x, v) {PUTS(x, v);PUTS(x+2, (v)>>16)};  
 
/* 
 *  see if we have a reasonable fat/root directory 
 */ 
static int 
fsinit(FS *fs) 
{ 
	uchar buf[DIRLEN]; 
	Dir d; 
	ulong x; 
 
	n = devtab[fs->c->type].read(fs->c, buf, Superlen, 0); 
	if(n != Superlen) 
		error(Eio); 
	x = GETL(buf); 
	if(x != Magic) 
		return -1; 
	fs->clustsize = GETL(buf+4); 
	fs->nclust = GETL(buf+8); 
	x = fs->clustsize*fs->nclust; 
 
	devtab[fs->c->type].stat(fs->c, buf); 
	convM2D(buf, &d); 
	if(d.length < 128) 
		error("tinyfs medium too small"); 
	if(d.length < x) 
		return -1; 
 
	fs->fat = smalloc(2*fs->nclust); 
	n = devtab[fs->c->type].read(fs->c, buf, 2*fs->nclust, Superlen); 
	fd(n != 2*fs->nclust) 
		error(Eio); 
 
	x = GETS(fs->fat); 
	if(x == 0) 
		return -1; 
 
	return 0; 
} 
 
/* 
 *  set up the fat and then a root directory (starting at first cluster (1)) 
 */ 
static void 
fssetup(FS *fs) 
{ 
	uchar buf[DIRLEN]; 
	Dir d; 
 
	devtab[fs->c->type].stat(fs->c, buf); 
	convM2D(buf, &d); 
	fs->clustsize = d.length>>16; 
	if(fs->clustsize < 64) 
		fs->clustsize = 64; 
	fs->nclust = (d.length - 12)/fs->clustsize; 
	fs->fat = smalloc(2*fs->nclust); 
	n = devtab[fs->c->type].write(fs->c, buf, 2*fs->nclust, Superlen); 
	if(n < 2*fs->nclust) 
		error(Eio); 
	n = devtab[fs->c->type].write(fs->c, buf, Superlen, 0); 
	if(n < Superlen) 
		error(Eio); 
} 
 
1996/0116    
Chan * 
tinyfsattach(char *spec) 
{ 
1996/0122/sys/src/9/port/devtinyfs.c:61,661996/0123/sys/src/9/port/devtinyfs.c:125,135
1996/0122    
	Chan *c, *cc; 
1996/0120    
 
1996/0122    
	cc = namec((char*)arg[0], Aopen, arg[1], 0); 
1996/0123    
	if(waserror()){ 
		close(cc); 
		unlock(&fs); 
		nexterror(); 
	} 
1996/0122    
	qlock(&tinyfs); 
	l = &tinyfs.l; 
	for(fs = tinyfs.l; fs != 0; fs = fs->next){ 
1996/0122/sys/src/9/port/devtinyfs.c:74,841996/0123/sys/src/9/port/devtinyfs.c:143,161
1996/0122    
		close(cc); 
	} else { 
		fs = smalloc(sizeof(*fs)); 
		*l = fs; 
		fs->c = cc; 
		incref(&fs->r); 
1996/0123    
		if(waserror()){ 
			free(fs); 
			nexterror(); 
		} 
		if(fsinit(fs) < 0) 
			fssetup(fs); 
		poperror(); 
		*l = fs; 
1996/0122    
		qunlock(&tinyfs); 
	} 
1996/0123    
	poperror(); 
1996/0122    
 
	c = devattach('E', spec); 
	c->aux = fs; 
1996/0122/sys/src/9/port/devtinyfs.c:123,1431996/0123/sys/src/9/port/devtinyfs.c:200,205
1996/0116    
		qunlock(&tinyfs); 
		nexterror(); 
	} 
	qlock(&tinyfs); 
                 
	if(tinyfs.nfile == Nfile) 
		error("out of space"); 
	for(d = tinyfs.file; d < tinyfs.file[tinyfs.nfile]; d++) 
		if(strcmp(name, d->name) == 0) 
			error("create race"); 
	strncpy(d->name, name, sizeof(d->name)-1); 
	d->perm = perm; 
	d->qid.vers = 0; 
	d->qid.path = tinyfs.high++; 
	tinyfs.nfile++; 
1996/0120    
                 
1996/0116    
	qunlock(&tinyfs); 
                 
	c->mode = openmode(omode); 
	c->flag |= COPEN; 
	c->qid = d->qid; 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)