-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper.c
222 lines (199 loc) · 6.69 KB
/
super.c
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
/*
* Copyright (C) 2005 - Alejandro Liu Ly <alejandro_liu@hotmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* @project mfstool
* @module super
* @section 3
* @doc Superblock functions
*/
#include "minix_fs.h"
#include "protos.h"
#include "bitops.h"
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
/**
* Find a free bit in map
* @param bmap - bitmap to scan
* @param bsize - bitmap size in blocks
* @return the bit number of the found block
*/
unsigned long get_free_bit(u8 *bmap,int bsize) {
int i;
for (i = 0; i < bsize * BLOCK_SIZE; i++) {
if (bmap[i] != 0xff) {
int j;
for (j = 0; j < 8 ; j++) {
if ((bmap[i] & (1<<j)) == 0) return (i<<3) + j;
}
die("Internal error!");
}
}
fatalmsg("No free slots in bitmap found");
return -1;
}
/**
* Initializes a new filesystem
* @param fn - file name for new filesystem
* @param magic - superblock magic number
* @param fsize - size of filesystem in blocks
* @param inodes - number of inodes to allocate (0 for auto)
* @return pointer to a minix_fs_dat structure
* @effect the file will be created or truncated.
*/
struct minix_fs_dat *new_fs(const char *fn,int magic,unsigned long fsize,int inodes,int keepdata) {
struct minix_fs_dat *fs = domalloc(sizeof(struct minix_fs_dat),0);
u32 rootblkp;
char root_block[BLOCK_SIZE];
int i;
if (magic != MINIX_SUPER_MAGIC && magic != MINIX_SUPER_MAGIC2 &&
magic != MINIX2_SUPER_MAGIC && magic != MINIX2_SUPER_MAGIC2) {
fatalmsg("invalid magic fs-type %x",magic);
}
fs->msb.s_magic = magic;
if (VERSION_2(fs)) {
fs->msb.s_zones = fsize;
} else {
fs->msb.s_nzones = fsize;
}
fs->msb.s_state = MINIX_VALID_FS;
fs->msb.s_max_size = VERSION_2(fs) ? 0x7fffffff : (7+512+512*512) * 1024;
/* Manage inodes */
if (!inodes) inodes = fsize / 3; /* Default number inodes to 1/3 blocks */
if (fsize == 1440) inodes = 512;
if (fsize == 1200) inodes = 384;
if (fsize == 720) inodes = 256;
if (fsize == 360) inodes = 128;
/* Round up inode count */
if (VERSION_2(fs))
inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
~(MINIX2_INODES_PER_BLOCK - 1));
else
inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
~(MINIX_INODES_PER_BLOCK - 1));
if (inodes > 65535) inodes = 65535;
INODES(fs) = inodes;
if (INODE_BLOCKS(fs) > fsize * 9 / 10 + 5)
fatalmsg("Too many inodes requested");
/*
* Initialise bitmaps
*/
IMAPS(fs) =UPPER(inodes + 1,BITS_PER_BLOCK);
ZMAPS(fs)=UPPER(fsize-(1+fs->msb.s_imap_blocks+INODE_BLOCKS(fs)),
BITS_PER_BLOCK+1);
FIRSTZONE(fs) = NORM_FIRSTZONE(fs);
fs->inode_bmap = domalloc(IMAPS(fs) * BLOCK_SIZE,0xff);
fs->zone_bmap = domalloc(ZMAPS(fs) * BLOCK_SIZE,0xff);
for (i = FIRSTZONE(fs) ; i < fsize ; i++) unmark_zone(fs,i);
for (i = MINIX_ROOT_INO; i<=INODES(fs) ; i++) unmark_inode(fs,i);
/*
* Initialize inode tables
*/
fs->ino.v1 = domalloc(INODE_BUFFER_SIZE(fs),0);
mark_inode(fs,MINIX_ROOT_INO);
set_inode(fs,MINIX_ROOT_INO,S_IFDIR | 0755, 2,
opt_squash ? 0 : getuid(), opt_squash ? 0 : getgid(),
2 * DIRSIZE(fs),NOW,NOW,NOW,0);
rootblkp = get_free_block(fs);
if (VERSION_2(fs)) {
INODE2(fs,MINIX_ROOT_INO)->i_zone[0] = rootblkp;
} else {
INODE(fs,MINIX_ROOT_INO)->i_zone[0] = rootblkp;
}
mark_zone(fs,rootblkp);
/*
* Initialise file
*/
fs->fp = fopen(fn,keepdata ? "r+b" : "w+b");
if (!fs->fp) die(fn);
if (fseek(fs->fp,fsize * BLOCK_SIZE-1,SEEK_SET)) die("fseek");
putc(0,fs->fp);
fflush(fs->fp);
/*
* Create a root block
*/
memset(root_block,0,sizeof(root_block));
*((short *)root_block) =MINIX_ROOT_INO;
strcpy(root_block+2,".");
*((short *)(root_block+DIRSIZE(fs))) = MINIX_ROOT_INO;
strcpy(root_block+2+DIRSIZE(fs),"..");
// printf("WRITE TO : %d %08x\n",rootblkp,rootblkp * BLOCK_SIZE);
// printf("%3d) FIRSTZONE: %d\n",__LINE__,FIRSTZONE(fs));
dofwrite(goto_blk(fs->fp,rootblkp),root_block,sizeof(root_block));
return fs;
}
/**
* Initializes a new filesystem
* @param fn - file name for new filesystem
* @param chk - stop if filesystem is not clean
* @return pointer to a minix_fs_dat structure
*/
struct minix_fs_dat *open_fs(const char *fn,int chk) {
struct minix_fs_dat *fs = domalloc(sizeof(struct minix_fs_dat),0);
fs->fp = fopen(fn,"r+b");
if (!fs->fp) die(fn);
/*
* Read super block
*/
goto_blk(fs->fp,MINIX_SUPER_BLOCK);
dofread(fs->fp,&(fs->msb),sizeof(struct minix_super_block));
/*
* Sanity checks ...
*/
if (FSMAGIC(fs) != MINIX_SUPER_MAGIC && FSMAGIC(fs) != MINIX_SUPER_MAGIC2 &&
FSMAGIC(fs) != MINIX2_SUPER_MAGIC && FSMAGIC(fs) != MINIX2_SUPER_MAGIC2) {
fatalmsg("invalid magic fs-type %x in %s",FSMAGIC(fs), fn);
}
if (MINIX_VALID_FS != fs->msb.s_state) {
if (chk) die("Filesystem in an unknown state");
fprintf(stderr,"Warning: %s in an unknown state\n",fn);
}
/*
* Read tables...
*/
fs->inode_bmap = domalloc(IMAPS(fs) * BLOCK_SIZE,-1);
fs->zone_bmap = domalloc(ZMAPS(fs) * BLOCK_SIZE,-1);
fs->ino.v1 = domalloc(INODE_BUFFER_SIZE(fs),-1);
dofread(goto_blk(fs->fp,MINIX_SUPER_BLOCK+1),
fs->inode_bmap,IMAPS(fs) * BLOCK_SIZE);
dofread(fs->fp,fs->zone_bmap,ZMAPS(fs) * BLOCK_SIZE);
dofread(fs->fp,fs->ino.v1,INODE_BUFFER_SIZE(fs));
return fs;
}
/**
* Closes filesystem
* @param fs - pointer to filesystem structure
* @return NULL
*/
struct minix_fs_dat *close_fs(struct minix_fs_dat *fs) {
goto_blk(fs->fp,MINIX_SUPER_BLOCK);
dofwrite(goto_blk(fs->fp,MINIX_SUPER_BLOCK),
&fs->msb,sizeof(struct minix_super_block));
dofwrite(goto_blk(fs->fp,MINIX_SUPER_BLOCK+1),
fs->inode_bmap,IMAPS(fs) * BLOCK_SIZE);
// printf("ZMAPSIZE=%d\n",ZMAPS(fs));
// printf("%4d) FTELL=%d - %x\n",__LINE__,ftell(fs->fp),ftell(fs->fp));
dofwrite(fs->fp,fs->zone_bmap,ZMAPS(fs) * BLOCK_SIZE);
// printf("%4d) FTELL=%d - %x\n",__LINE__,ftell(fs->fp),ftell(fs->fp));
if (VERSION_2(fs))
dofwrite(fs->fp,fs->ino.v2,INODE_BUFFER_SIZE(fs));
else
dofwrite(fs->fp,fs->ino.v1,INODE_BUFFER_SIZE(fs));
return 0;
}