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

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

1996/0123/sys/src/9/port/devtinyfs.c:13,271996/0131/sys/src/9/port/devtinyfs.c:13,28 (short | long | prev | next)
1996/0116    
 
enum{ 
	Qdir, 
1996/0123    
	Nfile=		32, 
1996/0120    
	Qmedium, 
1996/0123    
 
	Magic=		0xfeedbeef, 
	Superlen=	64, 
1996/0131    
	Blen=	48, 
 
	Tdir=	0, 
	Tdata, 
	Tend, 
1996/0116    
}; 
 
1996/0122    
typedef struct FS FS; 
1996/0116    
                 
1996/0122    
struct FS { 
1996/0116    
	QLock; 
1996/0122    
	Ref	r; 
1996/0123/sys/src/9/port/devtinyfs.c:28,361996/0131/sys/src/9/port/devtinyfs.c:29,36
1996/0122    
	int	dev; 
	FS	*next; 
1996/0116    
	Chan	*c; 
1996/0123    
	uchar	*fat; 
	ulong	nclust; 
	ulong	clustsize; 
1996/0131    
	uchar	*map; 
	int	nblocks; 
1996/0122    
}; 
 
struct { 
1996/0123/sys/src/9/port/devtinyfs.c:39,441996/0131/sys/src/9/port/devtinyfs.c:39,50
1996/0122    
	int	hidev; 
1996/0116    
} tinyfs; 
 
1996/0131    
#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)}; 
 
1996/0116    
void 
tinyfsreset(void) 
{ 
1996/0123/sys/src/9/port/devtinyfs.c:49,601996/0131/sys/src/9/port/devtinyfs.c:55,106
1996/0116    
{ 
} 
 
1996/0123    
#define GETS(x) ((x)[0]|((x)[1]<<8)) 
#define PUTS(x, v) {(x)[0] = (v);(x)[1] = ((v)>>8);} 
1996/0131    
static uchar 
checksum(uchar *p) 
{ 
	uchar *e; 
	uchar s; 
1996/0123    
 
#define GETL(x) (GETS(x)|(GETS(x+2)<<16)) 
#define PUTL(x, v) {PUTS(x, v);PUTS(x+2, (v)>>16)};  
1996/0131    
	s = 0; 
	for(e = p + Blen; p < e; p++) 
		s += *p; 
} 
1996/0123    
 
1996/0131    
static void 
mapclr(FS *fs, int bno) 
{ 
	fs->map[bno>>3] &= ~(1<<(bno&7)); 
} 
 
static void 
mapset(FS *fs, int bno) 
{ 
	fs->map[bno>>3] |= 1<<(bno&7); 
} 
 
static int 
mapalloc(FS *fs) 
{ 
	int i, j, lim; 
	uchar x; 
 
	qlock(fs); 
	lim = (fs->nblocks + 8 - 1)/8; 
	for(i = 0; i < lim; i++){ 
		x = fs->map[i]; 
		if(x == 0xff) 
			continue; 
		for(j = 0; j < 8; j++) 
			if((x & (1<<j)) == 0){ 
				fs->map[i] = x|(1<<j); 
				qunlock(fs); 
				return i*8 + j; 
			} 
	} 
	qunlock(fs); 
	return -1; 
} 
 
1996/0123    
/* 
 *  see if we have a reasonable fat/root directory 
 */ 
1996/0123/sys/src/9/port/devtinyfs.c:61,971996/0131/sys/src/9/port/devtinyfs.c:107,141
1996/0123    
static int 
fsinit(FS *fs) 
{ 
	uchar buf[DIRLEN]; 
1996/0131    
	uchar buf[Blen+DIRLEN]; 
1996/0123    
	Dir d; 
	ulong x; 
1996/0131    
	ulong x, bno; 
1996/0123    
 
	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) 
1996/0131    
	fs->nblocks = d.length/Blen; 
	if(fs->nblocks < 3) 
1996/0123    
		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); 
1996/0131    
	/* bitmap for block usage */ 
	x = (fs->nblocks + 8 - 1)/8; 
	fs->map = malloc(x); 
	memset(fs->map, 0x0, x); 
	for(bno = fs->nblocks; bno < x*8; bno++) 
		mapset(fs, bno); 
1996/0123    
 
	x = GETS(fs->fat); 
	if(x == 0) 
		return -1; 
                 
	return 0; 
1996/0131    
	for(bno = 0; bno < fs->nblocks; bno++){ 
		n = devtab[fs->c->type].read(fs->c, buf, Blen, Blen*bno); 
		if(n != Blen) 
			break; 
		if(checksum(buf) != 0) 
			continue; 
		switch(buf[0]){ 
		case Tdir: 
			mapset(fs, bno); 
			break; 
		} 
	} 
1996/0123    
} 
 
/* 


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