Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/*
2
 * 10th edition 4K file system
3
 */
4
#include <u.h>
5
#include <libc.h>
6
#include <auth.h>
7
#include <fcall.h>
8
#include "tapefs.h"
9
 
10
/*
11
 * v10 disk inode
12
 */
13
#define	VNADDR	13
14
#define	VFMT	0160000
15
#define	VIFREG	0100000
16
#define	VIFDIR	0040000
17
#define	VIFCHR	0120000
18
#define	VIFBLK	0160000
19
#define	VMODE	0777
20
#define	VSUPERB	1
21
#define	VROOT		2	/* root inode */
22
#define	VNAMELEN	14
23
#define	BLSIZE	4096
24
#define	LINOPB	(BLSIZE/sizeof(struct v10dinode))
25
#define	LNINDIR	(BLSIZE/sizeof(unsigned long))
26
 
27
struct v10dinode {
28
	unsigned char flags[2];
29
	unsigned char nlinks[2];
30
	unsigned char uid[2];
31
	unsigned char gid[2];
32
	unsigned char size[4];
33
	unsigned char addr[40];
34
	unsigned char atime[4];
35
	unsigned char mtime[4];
36
	unsigned char ctime[4];
37
};
38
 
39
struct	v10dir {
40
	uchar	ino[2];
41
	char	name[VNAMELEN];
42
};
43
 
44
int	tapefile;
45
vlong	tapelen;
46
Fileinf	iget(int ino);
47
long	bmap(Ram *r, long bno);
48
void	getblk(Ram *r, long bno, char *buf);
49
 
50
void
51
populate(char *name)
52
{
53
	Fileinf f;
54
	Dir *d;
55
 
56
	replete = 0;
57
	tapefile = open(name, OREAD);
58
	if (tapefile<0)
59
		error("Can't open argument file");
60
	if ((d=dirfstat(tapefile)) == nil)
61
		error("dirfstat");
62
	tapelen = d->length;
63
	free(d);
64
	f = iget(VROOT);
65
	ram->perm = f.mode;
66
	ram->mtime = f.mdate;
67
	ram->addr = f.addr;
68
	ram->data = f.data;
69
	ram->ndata = f.size;
70
}
71
 
72
void
73
popdir(Ram *r)
74
{
75
	int i, ino;
76
	char *cp;
77
	struct v10dir *dp;
78
	Fileinf f;
79
	char name[VNAMELEN+1];
80
 
81
	cp = 0;
82
	for (i=0; i<r->ndata; i+=sizeof(struct v10dir)) {
83
		if (i%BLSIZE==0)
84
			cp = doread(r, i, BLSIZE);
85
		dp = (struct v10dir *)(cp+i%BLSIZE);
86
		ino = g2byte(dp->ino);
87
		if (strcmp(dp->name, ".")==0 || strcmp(dp->name, "..")==0)
88
			continue;
89
		if (ino==0)
90
			continue;
91
		f = iget(ino);
92
		strncpy(name, dp->name, VNAMELEN);
93
		name[VNAMELEN] = '\0';
94
		f.name = name;
95
		popfile(r, f);
96
	}
97
	r->replete = 1;
98
}
99
 
100
void
101
dotrunc(Ram *r)
102
{
103
	USED(r);
104
}
105
 
106
void
107
docreate(Ram *r)
108
{
109
	USED(r);
110
}
111
 
112
char *
113
doread(Ram *r, vlong off, long cnt)
114
{
115
	static char buf[Maxbuf+BLSIZE];
116
	int bno, i;
117
 
118
	bno = off/BLSIZE;
119
	off -= bno*BLSIZE;
120
	if (cnt>Maxbuf)
121
		error("count too large");
122
	if (off)
123
		cnt += off;
124
	i = 0;
125
	while (cnt>0) {
126
		getblk(r, bno, &buf[i*BLSIZE]);
127
		cnt -= BLSIZE;
128
		bno++;
129
		i++;
130
	}
131
	return buf+off;
132
}
133
 
134
void
135
dowrite(Ram *r, char *buf, long off, long cnt)
136
{
137
	USED(r); USED(buf); USED(off); USED(cnt);
138
}
139
 
140
int
141
dopermw(Ram *r)
142
{
143
	USED(r);
144
	return 0;
145
}
146
 
147
/*
148
 * fetch an i-node
149
 * -- no sanity check for now
150
 * -- magic inode-to-disk-block stuff here
151
 */
152
 
153
Fileinf
154
iget(int ino)
155
{
156
	char buf[BLSIZE];
157
	struct v10dinode *dp;
158
	long flags, i;
159
	Fileinf f;
160
 
161
	seek(tapefile, BLSIZE*((ino-1)/LINOPB + VSUPERB + 1), 0);
162
	if (read(tapefile, buf, BLSIZE) != BLSIZE)
163
		error("Can't read inode");
164
	dp = ((struct v10dinode *)buf) + ((ino-1)%LINOPB);
165
	flags = g2byte(dp->flags);
166
	f.size = g4byte(dp->size);
167
	if ((flags&VFMT)==VIFCHR || (flags&VFMT)==VIFBLK)
168
		f.size = 0;
169
	f.data = emalloc(VNADDR*sizeof(long));
170
	for (i = 0; i < VNADDR; i++)
171
		((long*)f.data)[i] = g3byte(dp->addr+3*i);
172
	f.mode = flags & VMODE;
173
	if ((flags&VFMT)==VIFDIR)
174
		f.mode |= DMDIR;
175
	f.uid = g2byte(dp->uid);
176
	f.gid = g2byte(dp->gid);	
177
	f.mdate = g4byte(dp->mtime);
178
	return f;
179
}
180
 
181
void
182
getblk(Ram *r, long bno, char *buf)
183
{
184
	long dbno;
185
 
186
	if ((dbno = bmap(r, bno)) == 0) {
187
		memset(buf, 0, BLSIZE);
188
		return;
189
	}
190
	if ((vlong)(dbno+1)*BLSIZE > tapelen) {
191
		fprint(2, "read past end of tape: %lld\n", (vlong)dbno*BLSIZE);
192
		memset(buf, 0, BLSIZE);
193
		return;
194
	}
195
	seek(tapefile, dbno*BLSIZE, 0);
196
	if (readn(tapefile, buf, BLSIZE) != BLSIZE){
197
		fprint(2, "readn at %lld: %r\n", (vlong)dbno*BLSIZE);
198
		error("bad read");
199
	}
200
}
201
 
202
/*
203
 * logical to physical block
204
 * only singly-indirect files for now
205
 */
206
 
207
long
208
bmap(Ram *r, long bno)
209
{
210
	unsigned char indbuf[LNINDIR][sizeof(long)];
211
 
212
	if (bno < VNADDR-3)
213
		return ((long*)r->data)[bno];
214
	if (bno < VNADDR*LNINDIR) {
215
		seek(tapefile, ((long *)r->data)[(bno-(VNADDR-3))/LNINDIR+(VNADDR-3)]*BLSIZE, 0);
216
		if (read(tapefile, (char *)indbuf, BLSIZE) != BLSIZE)
217
			return 0;
218
		return ((indbuf[(bno-(VNADDR-3))%LNINDIR][2]<<16) + (indbuf[(bno-(VNADDR-3))%LNINDIR][1]<<8)
219
			+ indbuf[(bno-(VNADDR-3))%LNINDIR][0]);
220
	}
221
	return 0;
222
}