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