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