small asmconv cleanups.
[minix.git] / boot / rawfs.c
bloba143dc8ffa614bfc57514184ec86b8cb3eb4a5af
1 /* rawfs.c - Raw Minix file system support. Author: Kees J. Bot
2 * 23 Dec 1991
3 * Based on readfs by Paul Polderman
4 */
5 #define nil 0
6 #define _POSIX_SOURCE 1
7 #define _MINIX 1
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <stdlib.h>
11 #include <limits.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <minix/config.h>
15 #include <minix/const.h>
16 #include <minix/type.h>
17 #include <servers/mfs/const.h>
18 #include <servers/mfs/type.h>
19 #include <servers/mfs/buf.h>
20 #include <servers/mfs/super.h>
21 #include <servers/mfs/inode.h>
22 #include "rawfs.h"
24 void readblock(off_t blockno, char *buf, int);
26 /* The following code handles two file system types: Version 1 with small
27 * inodes and 16-bit disk addresses and Version 2 with big inodes and 32-bit
28 * disk addresses.
29 #ifdef FLEX
30 * To make matters worse, Minix-vmd knows about the normal Unix Version 7
31 * directories and directories with flexible entries.
32 #endif
35 /* File system parameters. */
36 static unsigned nr_dzones; /* Fill these in after reading superblock. */
37 static unsigned nr_indirects;
38 static unsigned inodes_per_block;
39 static int block_size;
40 #ifdef FLEX
41 #include <dirent.h>
42 #define direct _v7_direct
43 #else
44 #include <sys/dir.h>
45 #endif
47 #if __minix_vmd
48 static struct v12_super_block super; /* Superblock of file system */
49 #define s_log_zone_size s_dummy /* Zones are obsolete. */
50 #else
51 static struct super_block super; /* Superblock of file system */
52 #define SUPER_V1 SUPER_MAGIC /* V1 magic has a weird name. */
53 #endif
55 static struct inode curfil; /* Inode of file under examination */
56 static char indir[_MAX_BLOCK_SIZE]; /* Single indirect block. */
57 static char dindir[_MAX_BLOCK_SIZE]; /* Double indirect block. */
58 static char dirbuf[_MAX_BLOCK_SIZE]; /* Scratch/Directory block. */
59 #define scratch dirbuf
61 static block_t a_indir, a_dindir; /* Addresses of the indirects. */
62 static off_t dirpos; /* Reading pos in a dir. */
64 #define fsbuf(b) (* (union fsdata_u *) (b))
66 #define zone_shift (super.s_log_zone_size) /* zone to block ratio */
68 off_t r_super(int *bs)
69 /* Initialize variables, return size of file system in blocks,
70 * (zero on error).
73 /* Read superblock. (The superblock is always at 1kB offset,
74 * that's why we lie to readblock and say the block size is 1024
75 * and we want block number 1 (the 'second block', at offset 1kB).)
77 readblock(1, scratch, 1024);
79 memcpy(&super, scratch, sizeof(super));
81 /* Is it really a MINIX file system ? */
82 if (super.s_magic == SUPER_V2 || super.s_magic == SUPER_V3) {
83 if(super.s_magic == SUPER_V2)
84 super.s_block_size = 1024;
85 *bs = block_size = super.s_block_size;
86 if(block_size < _MIN_BLOCK_SIZE ||
87 block_size > _MAX_BLOCK_SIZE) {
88 return 0;
90 nr_dzones= V2_NR_DZONES;
91 nr_indirects= V2_INDIRECTS(block_size);
92 inodes_per_block= V2_INODES_PER_BLOCK(block_size);
93 return (off_t) super.s_zones << zone_shift;
94 } else
95 if (super.s_magic == SUPER_V1) {
96 *bs = block_size = 1024;
97 nr_dzones= V1_NR_DZONES;
98 nr_indirects= V1_INDIRECTS;
99 inodes_per_block= V1_INODES_PER_BLOCK;
100 return (off_t) super.s_nzones << zone_shift;
101 } else {
102 /* Filesystem not recognized as Minix. */
103 return 0;
107 void r_stat(Ino_t inum, struct stat *stp)
108 /* Return information about a file like stat(2) and remember it. */
110 block_t block;
111 block_t ino_block;
112 ino_t ino_offset;
113 union fsdata_u *blockbuf;
115 /* Calculate start of i-list */
116 block = START_BLOCK + super.s_imap_blocks + super.s_zmap_blocks;
118 /* Calculate block with inode inum */
119 ino_block = ((inum - 1) / inodes_per_block);
120 ino_offset = ((inum - 1) % inodes_per_block);
121 block += ino_block;
123 /* Fetch the block */
124 blockbuf = (union fsdata_u *) scratch;
125 readblock(block, (char *) blockbuf, block_size);
127 if (super.s_magic == SUPER_V2 || super.s_magic == SUPER_V3) {
128 d2_inode *dip;
129 int i;
131 dip= &blockbuf->b__v2_ino[(unsigned int) ino_offset];
133 curfil.i_mode= dip->d2_mode;
134 curfil.i_nlinks= dip->d2_nlinks;
135 curfil.i_uid= dip->d2_uid;
136 curfil.i_gid= dip->d2_gid;
137 curfil.i_size= dip->d2_size;
138 curfil.i_atime= dip->d2_atime;
139 curfil.i_mtime= dip->d2_mtime;
140 curfil.i_ctime= dip->d2_ctime;
141 for (i= 0; i < V2_NR_TZONES; i++)
142 curfil.i_zone[i]= dip->d2_zone[i];
143 } else {
144 d1_inode *dip;
145 int i;
147 dip= &blockbuf->b__v1_ino[(unsigned int) ino_offset];
149 curfil.i_mode= dip->d1_mode;
150 curfil.i_nlinks= dip->d1_nlinks;
151 curfil.i_uid= dip->d1_uid;
152 curfil.i_gid= dip->d1_gid;
153 curfil.i_size= dip->d1_size;
154 curfil.i_atime= dip->d1_mtime;
155 curfil.i_mtime= dip->d1_mtime;
156 curfil.i_ctime= dip->d1_mtime;
157 for (i= 0; i < V1_NR_TZONES; i++)
158 curfil.i_zone[i]= dip->d1_zone[i];
160 curfil.i_dev= -1; /* Can't fill this in alas. */
161 curfil.i_num= inum;
163 stp->st_dev= curfil.i_dev;
164 stp->st_ino= curfil.i_num;
165 stp->st_mode= curfil.i_mode;
166 stp->st_nlink= curfil.i_nlinks;
167 stp->st_uid= curfil.i_uid;
168 stp->st_gid= curfil.i_gid;
169 stp->st_rdev= (dev_t) curfil.i_zone[0];
170 stp->st_size= curfil.i_size;
171 stp->st_atime= curfil.i_atime;
172 stp->st_mtime= curfil.i_mtime;
173 stp->st_ctime= curfil.i_ctime;
175 a_indir= a_dindir= 0;
176 dirpos= 0;
179 ino_t r_readdir(char *name)
180 /* Read next directory entry at "dirpos" from file "curfil". */
182 ino_t inum= 0;
183 int blkpos;
184 struct direct *dp;
186 if (!S_ISDIR(curfil.i_mode)) { errno= ENOTDIR; return -1; }
188 if(!block_size) { errno = 0; return -1; }
190 while (inum == 0 && dirpos < curfil.i_size) {
191 if ((blkpos= (int) (dirpos % block_size)) == 0) {
192 /* Need to fetch a new directory block. */
194 readblock(r_vir2abs(dirpos / block_size), dirbuf, block_size);
196 #ifdef FLEX
197 if (super.s_flags & S_FLEX) {
198 struct _fl_direct *dp;
200 dp= (struct _fl_direct *) (dirbuf + blkpos);
201 if ((inum= dp->d_ino) != 0) strcpy(name, dp->d_name);
203 dirpos+= (1 + dp->d_extent) * FL_DIR_ENTRY_SIZE;
204 continue;
206 #endif
207 /* Let dp point to the next entry. */
208 dp= (struct direct *) (dirbuf + blkpos);
210 if ((inum= dp->d_ino) != 0) {
211 /* This entry is occupied, return name. */
212 strncpy(name, dp->d_name, sizeof(dp->d_name));
213 name[sizeof(dp->d_name)]= 0;
215 dirpos+= DIR_ENTRY_SIZE;
217 return inum;
220 off_t r_vir2abs(off_t virblk)
221 /* Translate a block number in a file to an absolute disk block number.
222 * Returns 0 for a hole and -1 if block is past end of file.
225 block_t b= virblk;
226 zone_t zone, ind_zone;
227 block_t z, zone_index;
228 int i;
230 if(!block_size) return -1;
232 /* Check if virblk within file. */
233 if (virblk * block_size >= curfil.i_size) return -1;
235 /* Calculate zone in which the datablock number is contained */
236 zone = (zone_t) (b >> zone_shift);
238 /* Calculate index of the block number in the zone */
239 zone_index = b - ((block_t) zone << zone_shift);
241 /* Go get the zone */
242 if (zone < (zone_t) nr_dzones) { /* direct block */
243 zone = curfil.i_zone[(int) zone];
244 z = ((block_t) zone << zone_shift) + zone_index;
245 return z;
248 /* The zone is not a direct one */
249 zone -= (zone_t) nr_dzones;
251 /* Is it single indirect ? */
252 if (zone < (zone_t) nr_indirects) { /* single indirect block */
253 ind_zone = curfil.i_zone[nr_dzones];
254 } else { /* double indirect block */
255 /* Fetch the double indirect block */
256 if ((ind_zone = curfil.i_zone[nr_dzones + 1]) == 0) return 0;
258 z = (block_t) ind_zone << zone_shift;
259 if (a_dindir != z) {
260 readblock(z, dindir, block_size);
261 a_dindir= z;
263 /* Extract the indirect zone number from it */
264 zone -= (zone_t) nr_indirects;
266 i = zone / (zone_t) nr_indirects;
267 ind_zone = (super.s_magic == SUPER_V2 || super.s_magic == SUPER_V3)
268 ? fsbuf(dindir).b__v2_ind[i]
269 : fsbuf(dindir).b__v1_ind[i];
270 zone %= (zone_t) nr_indirects;
272 if (ind_zone == 0) return 0;
274 /* Extract the datablock number from the indirect zone */
275 z = (block_t) ind_zone << zone_shift;
276 if (a_indir != z) {
277 readblock(z, indir, block_size);
278 a_indir= z;
280 zone = (super.s_magic == SUPER_V2 || super.s_magic == SUPER_V3)
281 ? fsbuf(indir).b__v2_ind[(int) zone]
282 : fsbuf(indir).b__v1_ind[(int) zone];
284 /* Calculate absolute datablock number */
285 z = ((block_t) zone << zone_shift) + zone_index;
286 return z;
289 ino_t r_lookup(Ino_t cwd, char *path)
290 /* Translates a pathname to an inode number. This is just a nice utility
291 * function, it only needs r_stat and r_readdir.
294 char name[NAME_MAX+1], r_name[NAME_MAX+1];
295 char *n;
296 struct stat st;
297 ino_t ino;
299 ino= path[0] == '/' ? ROOT_INO : cwd;
301 for (;;) {
302 if (ino == 0) {
303 errno= ENOENT;
304 return 0;
307 while (*path == '/') path++;
309 if (*path == 0) return ino;
311 r_stat(ino, &st);
313 if (!S_ISDIR(st.st_mode)) {
314 errno= ENOTDIR;
315 return 0;
318 n= name;
319 while (*path != 0 && *path != '/')
320 if (n < name + NAME_MAX) *n++ = *path++;
321 *n= 0;
323 while ((ino= r_readdir(r_name)) != 0
324 && strcmp(name, r_name) != 0) {
330 * $PchId: rawfs.c,v 1.8 1999/11/05 23:14:15 philip Exp $