main.c revision 1.10 1 1.10 uch /* $NetBSD: main.c,v 1.10 2011/08/10 11:31:49 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.10 uch __RCSID("$NetBSD: main.c,v 1.10 2011/08/10 11:31:49 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.10 uch #define VPRINTF(lv, fmt, args...) { if (v7fs_newfs_verbose >= lv) \
58 1.10 uch printf(fmt, ##args); }
59 1.1 uch
60 1.1 uch static v7fs_daddr_t
61 1.1 uch determine_ilist_size(v7fs_daddr_t volume_size, int32_t files)
62 1.1 uch {
63 1.1 uch v7fs_daddr_t ilist_size;
64 1.1 uch
65 1.1 uch if (files)
66 1.6 tron ilist_size = howmany(files, V7FS_INODE_PER_BLOCK);
67 1.1 uch else
68 1.1 uch ilist_size = volume_size / 25; /* 4% */
69 1.1 uch if (ilist_size > (v7fs_daddr_t)V7FS_ILISTBLK_MAX)
70 1.1 uch ilist_size = V7FS_ILISTBLK_MAX;
71 1.1 uch
72 1.1 uch return ilist_size;
73 1.1 uch }
74 1.1 uch
75 1.1 uch static int
76 1.8 uch partition_check(struct v7fs_self *fs)
77 1.8 uch {
78 1.8 uch struct v7fs_superblock *sb = &fs->superblock;
79 1.8 uch int error;
80 1.8 uch
81 1.8 uch if ((error = v7fs_superblock_load(fs))) {
82 1.9 uch if (error != EINVAL) {
83 1.9 uch /* Invalid superblock information is OK. */
84 1.9 uch warnx("Can't read superblock sector.");
85 1.9 uch }
86 1.8 uch }
87 1.8 uch sb->modified = 1;
88 1.8 uch if ((error = v7fs_superblock_writeback(fs))) {
89 1.8 uch if (errno == EROFS) {
90 1.8 uch warnx("Overwriting disk label? ");
91 1.8 uch }
92 1.8 uch warnx("Can't write superblock sector.");
93 1.8 uch }
94 1.8 uch
95 1.8 uch return error;
96 1.8 uch }
97 1.8 uch
98 1.8 uch static int
99 1.1 uch make_root(struct v7fs_self *fs)
100 1.1 uch {
101 1.1 uch struct v7fs_inode inode;
102 1.1 uch struct v7fs_dirent *dir;
103 1.1 uch int error;
104 1.1 uch
105 1.1 uch /* INO 1 badblk (don't used) */
106 1.1 uch memset(&inode, 0, sizeof(inode));
107 1.1 uch inode.inode_number = 1;
108 1.1 uch inode.mode = V7FS_IFREG; /* V7 manner */
109 1.1 uch v7fs_inode_writeback(fs, &inode);
110 1.1 uch
111 1.1 uch /* INO 2 root */
112 1.1 uch v7fs_ino_t ino;
113 1.1 uch if ((error = v7fs_inode_allocate(fs, &ino))) {
114 1.1 uch errno = error;
115 1.1 uch warn("Can't allocate / inode");
116 1.1 uch return error;
117 1.1 uch }
118 1.1 uch
119 1.1 uch memset(&inode, 0, sizeof(inode));
120 1.1 uch inode.inode_number = ino;
121 1.1 uch inode.mode = 0777 | V7FS_IFDIR;
122 1.1 uch inode.uid = 0;
123 1.1 uch inode.gid = 0;
124 1.1 uch inode.nlink = 2; /* . + .. */
125 1.1 uch inode.atime = inode.mtime = inode.ctime = time(0);
126 1.1 uch
127 1.1 uch /* root dirent. */
128 1.1 uch v7fs_datablock_expand(fs, &inode, sizeof(*dir) * 2);
129 1.1 uch v7fs_daddr_t blk = inode.addr[0];
130 1.1 uch void *buf;
131 1.1 uch if (!(buf = scratch_read(fs, blk))) {
132 1.1 uch v7fs_inode_deallocate(fs, ino);
133 1.1 uch errno = error = EIO;
134 1.1 uch warn("Can't read / dirent.");
135 1.1 uch return error;
136 1.1 uch }
137 1.1 uch dir = (struct v7fs_dirent *)buf; /*disk endian */
138 1.1 uch
139 1.1 uch strcpy(dir[0].name, ".");
140 1.1 uch dir[0].inode_number = V7FS_VAL16(fs, ino);
141 1.1 uch strcpy(dir[1].name, "..");
142 1.1 uch dir[1].inode_number = V7FS_VAL16(fs, ino);
143 1.1 uch if (!fs->io.write(fs->io.cookie, buf, blk)) {/*writeback */
144 1.1 uch scratch_free(fs, buf);
145 1.1 uch errno = error = EIO;
146 1.1 uch warn("Can't write / dirent.");
147 1.1 uch return error;
148 1.1 uch }
149 1.1 uch scratch_free(fs, buf);
150 1.1 uch v7fs_inode_writeback(fs, &inode);
151 1.1 uch if ((error = v7fs_superblock_writeback(fs))) {
152 1.1 uch errno = error;
153 1.8 uch warnx("Can't write superblock.");
154 1.1 uch }
155 1.1 uch
156 1.1 uch return error;
157 1.1 uch }
158 1.1 uch
159 1.1 uch static v7fs_daddr_t
160 1.1 uch make_freeblocklist(struct v7fs_self *fs, v7fs_daddr_t listblk, uint8_t *buf)
161 1.1 uch {
162 1.1 uch uint32_t (*val32)(uint32_t) = fs->val.conv32;
163 1.1 uch uint16_t (*val16)(uint16_t) = fs->val.conv16;
164 1.1 uch struct v7fs_freeblock *fb = (struct v7fs_freeblock *)buf;
165 1.1 uch int i, j, k;
166 1.1 uch
167 1.1 uch memset(buf, 0, V7FS_BSIZE);
168 1.1 uch
169 1.1 uch for (i = V7FS_MAX_FREEBLOCK - 1, j = listblk + 1, k = 0; i >= 0;
170 1.1 uch i--, j++, k++) {
171 1.1 uch progress(0);
172 1.1 uch if (j == (int32_t)fs->superblock.volume_size)
173 1.1 uch {
174 1.10 uch VPRINTF(4, "\nlast freeblock #%d\n",
175 1.1 uch (*val32)(fb->freeblock[i + 1]));
176 1.1 uch
177 1.1 uch memmove(fb->freeblock + 1, fb->freeblock + i + 1, k *
178 1.1 uch sizeof(v7fs_daddr_t));
179 1.1 uch fb->freeblock[0] = 0; /* Terminate link; */
180 1.2 uch fb->nfreeblock = (*val16)(k + 1);
181 1.10 uch VPRINTF(4, "last freeblock contains #%d\n",
182 1.1 uch (*val16)(fb->nfreeblock));
183 1.1 uch fs->io.write(fs->io.cookie, buf, listblk);
184 1.1 uch return 0;
185 1.1 uch }
186 1.1 uch fb->freeblock[i] = (*val32)(j);
187 1.1 uch }
188 1.1 uch fb->nfreeblock = (*val16)(k);
189 1.1 uch
190 1.1 uch if (!fs->io.write(fs->io.cookie, buf, listblk)) {
191 1.1 uch errno = EIO;
192 1.1 uch warn("blk=%ld", (long)listblk);
193 1.1 uch return 0;
194 1.1 uch }
195 1.1 uch
196 1.1 uch /* Return next link block */
197 1.1 uch return (*val32)(fb->freeblock[0]);
198 1.1 uch }
199 1.1 uch
200 1.1 uch static int
201 1.1 uch make_filesystem(struct v7fs_self *fs, v7fs_daddr_t volume_size,
202 1.1 uch v7fs_daddr_t ilist_size)
203 1.1 uch {
204 1.1 uch struct v7fs_superblock *sb;
205 1.1 uch v7fs_daddr_t blk;
206 1.1 uch uint8_t buf[V7FS_BSIZE];
207 1.1 uch int error = 0;
208 1.1 uch int32_t i, j;
209 1.1 uch
210 1.1 uch /* Setup ilist. (ilist must be zero filled. becuase of they are free) */
211 1.10 uch VPRINTF(4, "Zero clear ilist.\n");
212 1.1 uch progress(&(struct progress_arg){ .label = "zero ilist", .tick =
213 1.1 uch ilist_size / PROGRESS_BAR_GRANULE });
214 1.1 uch memset(buf, 0, sizeof buf);
215 1.1 uch for (i = V7FS_ILIST_SECTOR; i < (int32_t)ilist_size; i++) {
216 1.1 uch fs->io.write(fs->io.cookie, buf, i);
217 1.1 uch progress(0);
218 1.1 uch }
219 1.5 joerg #ifndef HAVE_NBTOOL_CONFIG_H
220 1.1 uch progress_done();
221 1.5 joerg #endif
222 1.10 uch VPRINTF(4, "\n");
223 1.1 uch
224 1.1 uch /* Construct superblock */
225 1.1 uch sb = &fs->superblock;
226 1.1 uch sb->volume_size = volume_size;
227 1.1 uch sb->datablock_start_sector = ilist_size + V7FS_ILIST_SECTOR;
228 1.1 uch sb->update_time = time(NULL);
229 1.1 uch
230 1.1 uch /* fill free inode cache. */
231 1.10 uch VPRINTF(4, "Setup inode cache.\n");
232 1.1 uch sb->nfreeinode = V7FS_MAX_FREEINODE;
233 1.1 uch for (i = V7FS_MAX_FREEINODE - 1, j = V7FS_ROOT_INODE; i >= 0; i--, j++)
234 1.1 uch sb->freeinode[i] = j;
235 1.1 uch sb->total_freeinode = ilist_size * V7FS_INODE_PER_BLOCK - 1;
236 1.1 uch
237 1.1 uch /* fill free block cache. */
238 1.10 uch VPRINTF(4, "Setup free block cache.\n");
239 1.1 uch sb->nfreeblock = V7FS_MAX_FREEBLOCK;
240 1.1 uch for (i = V7FS_MAX_FREEBLOCK - 1, j = sb->datablock_start_sector; i >= 0;
241 1.1 uch i--, j++)
242 1.1 uch sb->freeblock[i] = j;
243 1.2 uch
244 1.2 uch sb->total_freeblock = volume_size - sb->datablock_start_sector;
245 1.1 uch
246 1.1 uch /* Write superblock. */
247 1.1 uch sb->modified = 1;
248 1.1 uch if ((error = v7fs_superblock_writeback(fs))) {
249 1.1 uch errno = error;
250 1.1 uch warn("Can't write back superblock.");
251 1.1 uch return error;
252 1.1 uch }
253 1.1 uch
254 1.1 uch /* Construct freeblock list */
255 1.10 uch VPRINTF(4, "Setup whole freeblock list.\n");
256 1.1 uch progress(&(struct progress_arg){ .label = "freeblock list", .tick =
257 1.1 uch (volume_size - sb->datablock_start_sector) / PROGRESS_BAR_GRANULE});
258 1.1 uch blk = sb->freeblock[0];
259 1.1 uch while ((blk = make_freeblocklist(fs, blk, buf)))
260 1.1 uch continue;
261 1.5 joerg #ifndef HAVE_NBTOOL_CONFIG_H
262 1.1 uch progress_done();
263 1.5 joerg #endif
264 1.1 uch
265 1.10 uch VPRINTF(4, "done.\n");
266 1.1 uch
267 1.1 uch return 0;
268 1.1 uch }
269 1.1 uch
270 1.1 uch int
271 1.1 uch v7fs_newfs(const struct v7fs_mount_device *mount, int32_t maxfile)
272 1.1 uch {
273 1.1 uch struct v7fs_self *fs;
274 1.1 uch v7fs_daddr_t ilist_size;
275 1.1 uch int error;
276 1.1 uch v7fs_daddr_t volume_size = mount->sectors;
277 1.1 uch
278 1.1 uch /* Check and determine ilistblock, datablock size. */
279 1.7 uch if (volume_size > V7FS_DADDR_MAX + 1) {
280 1.7 uch warnx("volume size %d over v7fs limit %d. truncated.",
281 1.7 uch volume_size, V7FS_DADDR_MAX + 1);
282 1.7 uch volume_size = V7FS_DADDR_MAX + 1;
283 1.7 uch }
284 1.1 uch
285 1.1 uch ilist_size = determine_ilist_size(volume_size, maxfile);
286 1.1 uch
287 1.10 uch VPRINTF(1, "volume size=%d, ilist size=%d, endian=%d, NAME_MAX=%d\n",
288 1.1 uch volume_size, ilist_size, mount->endian, V7FS_NAME_MAX);
289 1.1 uch
290 1.1 uch /* Setup I/O ops. */
291 1.1 uch if ((error = v7fs_io_init(&fs, mount, V7FS_BSIZE))) {
292 1.1 uch errno = error;
293 1.1 uch warn("I/O setup failed.");
294 1.1 uch return error;
295 1.1 uch }
296 1.1 uch fs->endian = mount->endian;
297 1.1 uch v7fs_endian_init(fs);
298 1.1 uch
299 1.8 uch if ((error = partition_check(fs))) {
300 1.8 uch return error;
301 1.8 uch }
302 1.8 uch
303 1.1 uch /* Construct filesystem. */
304 1.1 uch if ((error = make_filesystem(fs, volume_size, ilist_size))) {
305 1.1 uch return error;
306 1.1 uch }
307 1.1 uch
308 1.1 uch /* Setup root. */
309 1.1 uch if ((error = make_root(fs))) {
310 1.1 uch return error;
311 1.1 uch }
312 1.1 uch
313 1.1 uch v7fs_io_fini(fs);
314 1.1 uch
315 1.1 uch return 0;
316 1.1 uch }
317