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