main.c revision 1.8 1 /* $NetBSD: main.c,v 1.8 2011/08/09 09:12:07 uch Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: main.c,v 1.8 2011/08/09 09:12:07 uch Exp $");
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <time.h>
46 #include <err.h>
47
48 #include "v7fs.h"
49 #include "v7fs_impl.h"
50 #include "v7fs_endian.h"
51 #include "v7fs_superblock.h"
52 #include "v7fs_inode.h"
53 #include "v7fs_datablock.h" /*v7fs_datablock_expand/last */
54 #include "newfs_v7fs.h"
55 #include "progress.h" /*../sbin/fsck */
56
57 #define VPRINTF(fmt, args...) { if (verbose) printf(fmt, ##args); }
58
59 static v7fs_daddr_t
60 determine_ilist_size(v7fs_daddr_t volume_size, int32_t files)
61 {
62 v7fs_daddr_t ilist_size;
63
64 if (files)
65 ilist_size = howmany(files, V7FS_INODE_PER_BLOCK);
66 else
67 ilist_size = volume_size / 25; /* 4% */
68 if (ilist_size > (v7fs_daddr_t)V7FS_ILISTBLK_MAX)
69 ilist_size = V7FS_ILISTBLK_MAX;
70
71 return ilist_size;
72 }
73
74 static int
75 partition_check(struct v7fs_self *fs)
76 {
77 struct v7fs_superblock *sb = &fs->superblock;
78 int error;
79
80 if ((error = v7fs_superblock_load(fs))) {
81 warnx("Can't read superblock sector.");
82 }
83 sb->modified = 1;
84 if ((error = v7fs_superblock_writeback(fs))) {
85 if (errno == EROFS) {
86 warnx("Overwriting disk label? ");
87 }
88 warnx("Can't write superblock sector.");
89 }
90
91 return error;
92 }
93
94 static int
95 make_root(struct v7fs_self *fs)
96 {
97 struct v7fs_inode inode;
98 struct v7fs_dirent *dir;
99 int error;
100
101 /* INO 1 badblk (don't used) */
102 memset(&inode, 0, sizeof(inode));
103 inode.inode_number = 1;
104 inode.mode = V7FS_IFREG; /* V7 manner */
105 v7fs_inode_writeback(fs, &inode);
106
107 /* INO 2 root */
108 v7fs_ino_t ino;
109 if ((error = v7fs_inode_allocate(fs, &ino))) {
110 errno = error;
111 warn("Can't allocate / inode");
112 return error;
113 }
114
115 memset(&inode, 0, sizeof(inode));
116 inode.inode_number = ino;
117 inode.mode = 0777 | V7FS_IFDIR;
118 inode.uid = 0;
119 inode.gid = 0;
120 inode.nlink = 2; /* . + .. */
121 inode.atime = inode.mtime = inode.ctime = time(0);
122
123 /* root dirent. */
124 v7fs_datablock_expand(fs, &inode, sizeof(*dir) * 2);
125 v7fs_daddr_t blk = inode.addr[0];
126 void *buf;
127 if (!(buf = scratch_read(fs, blk))) {
128 v7fs_inode_deallocate(fs, ino);
129 errno = error = EIO;
130 warn("Can't read / dirent.");
131 return error;
132 }
133 dir = (struct v7fs_dirent *)buf; /*disk endian */
134
135 strcpy(dir[0].name, ".");
136 dir[0].inode_number = V7FS_VAL16(fs, ino);
137 strcpy(dir[1].name, "..");
138 dir[1].inode_number = V7FS_VAL16(fs, ino);
139 if (!fs->io.write(fs->io.cookie, buf, blk)) {/*writeback */
140 scratch_free(fs, buf);
141 errno = error = EIO;
142 warn("Can't write / dirent.");
143 return error;
144 }
145 scratch_free(fs, buf);
146 v7fs_inode_writeback(fs, &inode);
147 if ((error = v7fs_superblock_writeback(fs))) {
148 errno = error;
149 warnx("Can't write superblock.");
150 }
151
152 return error;
153 }
154
155 static v7fs_daddr_t
156 make_freeblocklist(struct v7fs_self *fs, v7fs_daddr_t listblk, uint8_t *buf)
157 {
158 uint32_t (*val32)(uint32_t) = fs->val.conv32;
159 uint16_t (*val16)(uint16_t) = fs->val.conv16;
160 struct v7fs_freeblock *fb = (struct v7fs_freeblock *)buf;
161 int i, j, k;
162
163 memset(buf, 0, V7FS_BSIZE);
164
165 for (i = V7FS_MAX_FREEBLOCK - 1, j = listblk + 1, k = 0; i >= 0;
166 i--, j++, k++) {
167 progress(0);
168 if (j == (int32_t)fs->superblock.volume_size)
169 {
170 VPRINTF("\nlast freeblock #%d\n",
171 (*val32)(fb->freeblock[i + 1]));
172
173 memmove(fb->freeblock + 1, fb->freeblock + i + 1, k *
174 sizeof(v7fs_daddr_t));
175 fb->freeblock[0] = 0; /* Terminate link; */
176 fb->nfreeblock = (*val16)(k + 1);
177 VPRINTF("last freeblock contains #%d\n",
178 (*val16)(fb->nfreeblock));
179 fs->io.write(fs->io.cookie, buf, listblk);
180 return 0;
181 }
182 fb->freeblock[i] = (*val32)(j);
183 }
184 fb->nfreeblock = (*val16)(k);
185
186 if (!fs->io.write(fs->io.cookie, buf, listblk)) {
187 errno = EIO;
188 warn("blk=%ld", (long)listblk);
189 return 0;
190 }
191
192 /* Return next link block */
193 return (*val32)(fb->freeblock[0]);
194 }
195
196 static int
197 make_filesystem(struct v7fs_self *fs, v7fs_daddr_t volume_size,
198 v7fs_daddr_t ilist_size)
199 {
200 struct v7fs_superblock *sb;
201 v7fs_daddr_t blk;
202 uint8_t buf[V7FS_BSIZE];
203 int error = 0;
204 int32_t i, j;
205
206 /* Setup ilist. (ilist must be zero filled. becuase of they are free) */
207 VPRINTF("Zero clear ilist.\n");
208 progress(&(struct progress_arg){ .label = "zero ilist", .tick =
209 ilist_size / PROGRESS_BAR_GRANULE });
210 memset(buf, 0, sizeof buf);
211 for (i = V7FS_ILIST_SECTOR; i < (int32_t)ilist_size; i++) {
212 fs->io.write(fs->io.cookie, buf, i);
213 progress(0);
214 }
215 #ifndef HAVE_NBTOOL_CONFIG_H
216 progress_done();
217 #endif
218 VPRINTF("\n");
219
220 /* Construct superblock */
221 sb = &fs->superblock;
222 sb->volume_size = volume_size;
223 sb->datablock_start_sector = ilist_size + V7FS_ILIST_SECTOR;
224 sb->update_time = time(NULL);
225
226 /* fill free inode cache. */
227 VPRINTF("Setup inode cache.\n");
228 sb->nfreeinode = V7FS_MAX_FREEINODE;
229 for (i = V7FS_MAX_FREEINODE - 1, j = V7FS_ROOT_INODE; i >= 0; i--, j++)
230 sb->freeinode[i] = j;
231 sb->total_freeinode = ilist_size * V7FS_INODE_PER_BLOCK - 1;
232
233 /* fill free block cache. */
234 VPRINTF("Setup free block cache.\n");
235 sb->nfreeblock = V7FS_MAX_FREEBLOCK;
236 for (i = V7FS_MAX_FREEBLOCK - 1, j = sb->datablock_start_sector; i >= 0;
237 i--, j++)
238 sb->freeblock[i] = j;
239
240 sb->total_freeblock = volume_size - sb->datablock_start_sector;
241
242 /* Write superblock. */
243 sb->modified = 1;
244 if ((error = v7fs_superblock_writeback(fs))) {
245 errno = error;
246 warn("Can't write back superblock.");
247 return error;
248 }
249
250 /* Construct freeblock list */
251 VPRINTF("Setup whole freeblock list.\n");
252 progress(&(struct progress_arg){ .label = "freeblock list", .tick =
253 (volume_size - sb->datablock_start_sector) / PROGRESS_BAR_GRANULE});
254 blk = sb->freeblock[0];
255 while ((blk = make_freeblocklist(fs, blk, buf)))
256 continue;
257 #ifndef HAVE_NBTOOL_CONFIG_H
258 progress_done();
259 #endif
260
261 VPRINTF("done.\n");
262
263 return 0;
264 }
265
266 int
267 v7fs_newfs(const struct v7fs_mount_device *mount, int32_t maxfile)
268 {
269 struct v7fs_self *fs;
270 v7fs_daddr_t ilist_size;
271 int error;
272 v7fs_daddr_t volume_size = mount->sectors;
273
274 /* Check and determine ilistblock, datablock size. */
275 if (volume_size > V7FS_DADDR_MAX + 1) {
276 warnx("volume size %d over v7fs limit %d. truncated.",
277 volume_size, V7FS_DADDR_MAX + 1);
278 volume_size = V7FS_DADDR_MAX + 1;
279 }
280
281 ilist_size = determine_ilist_size(volume_size, maxfile);
282
283 VPRINTF("volume size=%d, ilist size=%d, endian=%d, NAME_MAX=%d\n",
284 volume_size, ilist_size, mount->endian, V7FS_NAME_MAX);
285
286 /* Setup I/O ops. */
287 if ((error = v7fs_io_init(&fs, mount, V7FS_BSIZE))) {
288 errno = error;
289 warn("I/O setup failed.");
290 return error;
291 }
292 fs->endian = mount->endian;
293 v7fs_endian_init(fs);
294
295 if ((error = partition_check(fs))) {
296 return error;
297 }
298
299 /* Construct filesystem. */
300 if ((error = make_filesystem(fs, volume_size, ilist_size))) {
301 return error;
302 }
303
304 /* Setup root. */
305 if ((error = make_root(fs))) {
306 return error;
307 }
308
309 v7fs_io_fini(fs);
310
311 return 0;
312 }
313