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