1 /* $NetBSD: v7fs_estimate.c,v 1.3 2012/04/19 17:28:26 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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"
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 $");
46 #if !HAVE_NBTOOL_CONFIG_H
47 #include <sys/mount.h> /*MAXPATHLEN */
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
;
60 v7fs_daddr_t npuredatablk
;
63 #define VPRINTF(fmt, args...) { if (v7fs_newfs_verbose) printf(fmt, ##args); }
66 v7fs_datablock_size(off_t sz
, v7fs_daddr_t
*nblk
)
68 struct v7fs_daddr_map map
;
76 if ((error
= v7fs_datablock_addr(sz
, &map
))) {
81 *nblk
= map
.index
[0] + 1;
84 *nblk
= V7FS_NADDR_DIRECT
+ /*direct */
85 1/*addr[V7FS_NADDR_INDEX1]*/ + map
.index
[0] + 1;
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 */
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
+
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
+
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 */
124 estimate_size_walk(fsnode
*root
, char *dir
, struct v7fs_geometry
*geom
)
128 size_t pathlen
= strlen(dir
);
129 char *mydir
= dir
+ pathlen
;
136 /* LSC: -Werror=maybe-uninitialized, when compiling with -O3. */
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
) {
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
);
153 geom
->ndatablock
+= nblk
;
154 geom
->npuredatablk
+=
155 V7FS_ROUND_BSIZE(sz
) >> V7FS_BSHIFT
;
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
;
170 nblk
= V7FSBSD_MAXSYMLINKLEN
>> V7FS_BSHIFT
;
171 geom
->ndatablock
+= nblk
;
172 geom
->npuredatablk
+= nblk
;
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
,
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
,
212 if ((fsopts
->sectorsize
> 0) && (fsopts
->sectorsize
!= V7FS_BSIZE
))
213 warnx("v7fs sector size is 512byte only. '-S %d' is ignored.",
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
);
228 if (fsopts
->freeblocks
) {
229 nblk
+= fsopts
->freeblocks
;
230 } else if ((n
= fsopts
->freeblockpc
)) {
231 nblk
+= (nblk
* n
) / (100 - n
);
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
;
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
);
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];
262 struct v7fs_geometry geom
;
265 /* LSC: -Werror=maybe-uninitialized, when compiling with -O3. */
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 */