Remove building with NOCRYPTO option
[minix.git] / usr.sbin / makefs / v7fs / v7fs_estimate.c
blob3e09d47e18a0b7f7c7853f1473809927e5472738
1 /* $NetBSD: v7fs_estimate.c,v 1.3 2012/04/19 17:28:26 christos Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(__lint)
38 __RCSID("$NetBSD: v7fs_estimate.c,v 1.3 2012/04/19 17:28:26 christos Exp $");
39 #endif /* !__lint */
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <errno.h>
46 #if !HAVE_NBTOOL_CONFIG_H
47 #include <sys/mount.h> /*MAXPATHLEN */
48 #endif
50 #include "makefs.h"
51 #include "v7fs.h"
52 #include "v7fs_impl.h"
53 #include "v7fs_inode.h"
54 #include "v7fs_datablock.h"
55 #include "v7fs_makefs.h"
57 struct v7fs_geometry {
58 v7fs_daddr_t ndatablock;
59 v7fs_ino_t ninode;
60 v7fs_daddr_t npuredatablk;
63 #define VPRINTF(fmt, args...) { if (v7fs_newfs_verbose) printf(fmt, ##args); }
65 static int
66 v7fs_datablock_size(off_t sz, v7fs_daddr_t *nblk)
68 struct v7fs_daddr_map map;
69 int error = 0;
71 if (sz == 0) {
72 *nblk = 0;
73 return 0;
76 if ((error = v7fs_datablock_addr(sz, &map))) {
77 return error;
79 switch (map.level) {
80 case 0: /* Direct */
81 *nblk = map.index[0] + 1;
82 break;
83 case 1:
84 *nblk = V7FS_NADDR_DIRECT + /*direct */
85 1/*addr[V7FS_NADDR_INDEX1]*/ + map.index[0] + 1;
86 break;
87 case 2:
88 *nblk = V7FS_NADDR_DIRECT + /*direct */
89 1/*addr[V7FS_NADDR_INDEX1]*/ +
90 V7FS_DADDR_PER_BLOCK +/*idx1 */
91 1/*addr[V7FS_NADDR_INDEX2]*/ +
92 map.index[0] + /* # of idx2 index block(filled) */
93 map.index[0] * V7FS_DADDR_PER_BLOCK + /* of its datablocks*/
94 1 + /*current idx2 indexblock */
95 map.index[1] + 1;
96 break;
97 case 3:
98 *nblk = V7FS_NADDR_DIRECT + /*direct */
99 1/*addr[V7FS_NADDR_INDEX1]*/ +
100 V7FS_DADDR_PER_BLOCK +/*idx1 */
101 1/*addr[V7FS_NADDR_INDEX2]*/ +
102 V7FS_DADDR_PER_BLOCK + /* # of idx2 index block */
103 V7FS_DADDR_PER_BLOCK * V7FS_DADDR_PER_BLOCK +
104 /*idx2 datablk */
105 1/*addr[v7FS_NADDR_INDEX3*/ +
106 map.index[0] + /* # of lv1 index block(filled) */
107 map.index[0] * V7FS_DADDR_PER_BLOCK * V7FS_DADDR_PER_BLOCK +
108 1 + /*lv1 */
109 map.index[1] + /* #of lv2 index block(filled) */
110 map.index[1] * V7FS_DADDR_PER_BLOCK + /*lv2 datablock */
111 1 + /* current lv2 index block */
112 map.index[2] + 1; /*filled datablock */
113 break;
114 default:
115 *nblk = 0;
116 error = EINVAL;
117 break;
120 return error;
123 static int
124 estimate_size_walk(fsnode *root, char *dir, struct v7fs_geometry *geom)
126 fsnode *cur;
127 int nentries;
128 size_t pathlen = strlen(dir);
129 char *mydir = dir + pathlen;
130 fsinode *fnode;
131 v7fs_daddr_t nblk;
132 int n;
133 off_t sz;
135 #if defined(__minix)
136 /* LSC: -Werror=maybe-uninitialized, when compiling with -O3. */
137 nblk = 0;
138 #endif /* defined(__minix) */
139 for (cur = root, nentries = 0; cur != NULL; cur = cur->next,
140 nentries++, geom->ninode++) {
141 switch (cur->type & S_IFMT) {
142 default:
143 break;
144 case S_IFDIR:
145 if (!cur->child)
146 break;
147 mydir[0] = '/';
148 strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
149 n = estimate_size_walk(cur->child, dir, geom);
150 sz = (n + 1/*..*/) * sizeof(struct v7fs_dirent);
151 v7fs_datablock_size(sz, &nblk);
152 mydir[0] = '\0';
153 geom->ndatablock += nblk;
154 geom->npuredatablk +=
155 V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT;
156 break;
157 case S_IFREG:
158 fnode = cur->inode;
159 if (!(fnode->flags & FI_SIZED)) { /*Skip hard-link */
160 sz = fnode->st.st_size;
161 v7fs_datablock_size(sz, &nblk);
162 geom->ndatablock += nblk;
163 geom->npuredatablk +=
164 V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT;
165 fnode->flags |= FI_SIZED;
168 break;
169 case S_IFLNK:
170 nblk = V7FSBSD_MAXSYMLINKLEN >> V7FS_BSHIFT;
171 geom->ndatablock += nblk;
172 geom->npuredatablk += nblk;
173 break;
177 return nentries;
180 static v7fs_daddr_t
181 calculate_fs_size(const struct v7fs_geometry *geom)
183 v7fs_daddr_t fs_blk, ilist_blk;
185 ilist_blk = V7FS_ROUND_BSIZE(geom->ninode *
186 sizeof(struct v7fs_inode_diskimage)) >> V7FS_BSHIFT;
187 fs_blk = geom->ndatablock + ilist_blk + V7FS_ILIST_SECTOR;
189 VPRINTF("datablock:%d ilistblock:%d total:%d\n", geom->ndatablock,
190 ilist_blk, fs_blk);
192 return fs_blk;
195 static void
196 determine_fs_size(fsinfo_t *fsopts, struct v7fs_geometry *geom)
198 v7fs_daddr_t nblk = geom->ndatablock;
199 v7fs_daddr_t fsblk, n;
200 int32_t nfiles = geom->ninode;
202 VPRINTF("size=%lld inodes=%lld fd=%d superb=%p onlyspec=%d\n",
203 (long long)fsopts->size, (long long)fsopts->inodes, fsopts->fd,
204 fsopts->superblock, fsopts->onlyspec);
205 VPRINTF("minsize=%lld maxsize=%lld freefiles=%lld freefilepc=%d "
206 "freeblocks=%lld freeblockpc=%d sectorseize=%d\n",
207 (long long)fsopts->minsize, (long long)fsopts->maxsize,
208 (long long)fsopts->freefiles, fsopts->freefilepc,
209 (long long)fsopts->freeblocks, fsopts->freeblockpc,
210 fsopts->sectorsize);
212 if ((fsopts->sectorsize > 0) && (fsopts->sectorsize != V7FS_BSIZE))
213 warnx("v7fs sector size is 512byte only. '-S %d' is ignored.",
214 fsopts->sectorsize);
216 /* Free inode */
217 if (fsopts->freefiles) {
218 nfiles += fsopts->freefiles;
219 } else if ((n = fsopts->freefilepc)) {
220 nfiles += (nfiles * n) / (100 - n);
222 if (nfiles >= V7FS_INODE_MAX) {
223 errx(EXIT_FAILURE, "# of files(%d) over v7fs limit(%d).",
224 nfiles, V7FS_INODE_MAX);
227 /* Free datablock */
228 if (fsopts->freeblocks) {
229 nblk += fsopts->freeblocks;
230 } else if ((n = fsopts->freeblockpc)) {
231 nblk += (nblk * n) / (100 - n);
234 /* Total size */
235 geom->ndatablock = nblk;
236 geom->ninode = nfiles;
237 fsblk = calculate_fs_size(geom);
239 if (fsblk >= V7FS_DADDR_MAX) {
240 errx(EXIT_FAILURE, "filesystem size(%d) over v7fs limit(%d).",
241 fsblk, V7FS_DADDR_MAX);
244 n = fsopts->minsize >> V7FS_BSHIFT;
245 if (fsblk < n)
246 geom->ndatablock += (n - fsblk);
248 n = fsopts->maxsize >> V7FS_BSHIFT;
249 if (fsopts->maxsize > 0 && fsblk > n) {
250 errx(EXIT_FAILURE, "# of datablocks %d > %d", fsblk, n);
254 void
255 v7fs_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts)
257 v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
258 char path[MAXPATHLEN + 1];
259 int ndir;
260 off_t sz;
261 v7fs_daddr_t nblk;
262 struct v7fs_geometry geom;
264 #if defined(__minix)
265 /* LSC: -Werror=maybe-uninitialized, when compiling with -O3. */
266 nblk = 0;
267 #endif /* defined(__minix) */
268 memset(&geom , 0, sizeof(geom));
269 strncpy(path, dir, sizeof(path));
271 /* Calculate strict size. */
272 ndir = estimate_size_walk(root, path, &geom);
273 sz = (ndir + 1/*..*/) * sizeof(struct v7fs_dirent);
274 v7fs_datablock_size(sz, &nblk);
275 geom.ndatablock += nblk;
277 /* Consider options. */
278 determine_fs_size(fsopts, &geom);
280 fsopts->size = calculate_fs_size(&geom) << V7FS_BSHIFT;
281 fsopts->inodes = geom.ninode;
282 v7fs_opts->npuredatablk = geom.npuredatablk; /* for progress bar */