xref: /minix3/usr.sbin/makefs/v7fs/v7fs_estimate.c (revision e1cdaee10649323af446eb1a74571984b2ab3181)
19f988b79SJean-Baptiste Boric /*	$NetBSD: v7fs_estimate.c,v 1.3 2012/04/19 17:28:26 christos Exp $	*/
29f988b79SJean-Baptiste Boric 
39f988b79SJean-Baptiste Boric /*-
49f988b79SJean-Baptiste Boric  * Copyright (c) 2011 The NetBSD Foundation, Inc.
59f988b79SJean-Baptiste Boric  * All rights reserved.
69f988b79SJean-Baptiste Boric  *
79f988b79SJean-Baptiste Boric  * This code is derived from software contributed to The NetBSD Foundation
89f988b79SJean-Baptiste Boric  * by UCHIYAMA Yasushi.
99f988b79SJean-Baptiste Boric  *
109f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
119f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
129f988b79SJean-Baptiste Boric  * are met:
139f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
149f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
159f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
169f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
179f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
189f988b79SJean-Baptiste Boric  *
199f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209f988b79SJean-Baptiste Boric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219f988b79SJean-Baptiste Boric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229f988b79SJean-Baptiste Boric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239f988b79SJean-Baptiste Boric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249f988b79SJean-Baptiste Boric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259f988b79SJean-Baptiste Boric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269f988b79SJean-Baptiste Boric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279f988b79SJean-Baptiste Boric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289f988b79SJean-Baptiste Boric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299f988b79SJean-Baptiste Boric  * POSSIBILITY OF SUCH DAMAGE.
309f988b79SJean-Baptiste Boric  */
319f988b79SJean-Baptiste Boric 
329f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
339f988b79SJean-Baptiste Boric #include "nbtool_config.h"
349f988b79SJean-Baptiste Boric #endif
359f988b79SJean-Baptiste Boric 
369f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
379f988b79SJean-Baptiste Boric #if defined(__RCSID) && !defined(__lint)
389f988b79SJean-Baptiste Boric __RCSID("$NetBSD: v7fs_estimate.c,v 1.3 2012/04/19 17:28:26 christos Exp $");
399f988b79SJean-Baptiste Boric #endif	/* !__lint */
409f988b79SJean-Baptiste Boric 
419f988b79SJean-Baptiste Boric #include <stdio.h>
429f988b79SJean-Baptiste Boric #include <string.h>
439f988b79SJean-Baptiste Boric #include <stdlib.h>
449f988b79SJean-Baptiste Boric #include <errno.h>
459f988b79SJean-Baptiste Boric 
469f988b79SJean-Baptiste Boric #if !HAVE_NBTOOL_CONFIG_H
479f988b79SJean-Baptiste Boric #include <sys/mount.h>	/*MAXPATHLEN */
489f988b79SJean-Baptiste Boric #endif
499f988b79SJean-Baptiste Boric 
509f988b79SJean-Baptiste Boric #include "makefs.h"
519f988b79SJean-Baptiste Boric #include "v7fs.h"
529f988b79SJean-Baptiste Boric #include "v7fs_impl.h"
539f988b79SJean-Baptiste Boric #include "v7fs_inode.h"
549f988b79SJean-Baptiste Boric #include "v7fs_datablock.h"
559f988b79SJean-Baptiste Boric #include "v7fs_makefs.h"
569f988b79SJean-Baptiste Boric 
579f988b79SJean-Baptiste Boric struct v7fs_geometry {
589f988b79SJean-Baptiste Boric 	v7fs_daddr_t ndatablock;
599f988b79SJean-Baptiste Boric 	v7fs_ino_t ninode;
609f988b79SJean-Baptiste Boric 	v7fs_daddr_t npuredatablk;
619f988b79SJean-Baptiste Boric };
629f988b79SJean-Baptiste Boric 
639f988b79SJean-Baptiste Boric #define	VPRINTF(fmt, args...)	{ if (v7fs_newfs_verbose) printf(fmt, ##args); }
649f988b79SJean-Baptiste Boric 
659f988b79SJean-Baptiste Boric static int
v7fs_datablock_size(off_t sz,v7fs_daddr_t * nblk)669f988b79SJean-Baptiste Boric v7fs_datablock_size(off_t sz, v7fs_daddr_t *nblk)
679f988b79SJean-Baptiste Boric {
689f988b79SJean-Baptiste Boric 	struct v7fs_daddr_map map;
699f988b79SJean-Baptiste Boric 	int error = 0;
709f988b79SJean-Baptiste Boric 
719f988b79SJean-Baptiste Boric 	if (sz == 0) {
729f988b79SJean-Baptiste Boric 		*nblk = 0;
739f988b79SJean-Baptiste Boric 		return 0;
749f988b79SJean-Baptiste Boric 	}
759f988b79SJean-Baptiste Boric 
769f988b79SJean-Baptiste Boric 	if ((error = v7fs_datablock_addr(sz, &map))) {
779f988b79SJean-Baptiste Boric 		return error;
789f988b79SJean-Baptiste Boric 	}
799f988b79SJean-Baptiste Boric 	switch (map.level) {
809f988b79SJean-Baptiste Boric 	case 0:	/* Direct */
819f988b79SJean-Baptiste Boric 		*nblk = map.index[0] + 1;
829f988b79SJean-Baptiste Boric 		break;
839f988b79SJean-Baptiste Boric 	case 1:
849f988b79SJean-Baptiste Boric 		*nblk = V7FS_NADDR_DIRECT +	/*direct */
859f988b79SJean-Baptiste Boric 		    1/*addr[V7FS_NADDR_INDEX1]*/ + map.index[0] + 1;
869f988b79SJean-Baptiste Boric 		break;
879f988b79SJean-Baptiste Boric 	case 2:
889f988b79SJean-Baptiste Boric 		*nblk = V7FS_NADDR_DIRECT +	/*direct */
899f988b79SJean-Baptiste Boric 		    1/*addr[V7FS_NADDR_INDEX1]*/ +
909f988b79SJean-Baptiste Boric 		    V7FS_DADDR_PER_BLOCK +/*idx1 */
919f988b79SJean-Baptiste Boric 		    1/*addr[V7FS_NADDR_INDEX2]*/ +
929f988b79SJean-Baptiste Boric 		    map.index[0] + /* # of idx2 index block(filled) */
939f988b79SJean-Baptiste Boric 		    map.index[0] * V7FS_DADDR_PER_BLOCK + /* of its datablocks*/
949f988b79SJean-Baptiste Boric 		    1 + /*current idx2 indexblock */
959f988b79SJean-Baptiste Boric 		    map.index[1] + 1;
969f988b79SJean-Baptiste Boric 		break;
979f988b79SJean-Baptiste Boric 	case 3:
989f988b79SJean-Baptiste Boric 		*nblk = V7FS_NADDR_DIRECT +	/*direct */
999f988b79SJean-Baptiste Boric 		    1/*addr[V7FS_NADDR_INDEX1]*/ +
1009f988b79SJean-Baptiste Boric 		    V7FS_DADDR_PER_BLOCK +/*idx1 */
1019f988b79SJean-Baptiste Boric 		    1/*addr[V7FS_NADDR_INDEX2]*/ +
1029f988b79SJean-Baptiste Boric 		    V7FS_DADDR_PER_BLOCK + /* # of idx2 index block */
1039f988b79SJean-Baptiste Boric 		    V7FS_DADDR_PER_BLOCK * V7FS_DADDR_PER_BLOCK +
1049f988b79SJean-Baptiste Boric 		    /*idx2 datablk */
1059f988b79SJean-Baptiste Boric 		    1/*addr[v7FS_NADDR_INDEX3*/ +
1069f988b79SJean-Baptiste Boric 		    map.index[0] + /* # of lv1 index block(filled) */
1079f988b79SJean-Baptiste Boric 		    map.index[0] * V7FS_DADDR_PER_BLOCK * V7FS_DADDR_PER_BLOCK +
1089f988b79SJean-Baptiste Boric 		    1 +	/*lv1 */
1099f988b79SJean-Baptiste Boric 		    map.index[1] + /* #of lv2 index block(filled) */
1109f988b79SJean-Baptiste Boric 		    map.index[1] * V7FS_DADDR_PER_BLOCK + /*lv2 datablock */
1119f988b79SJean-Baptiste Boric 		    1 + /* current lv2 index block */
1129f988b79SJean-Baptiste Boric 		    map.index[2] + 1; /*filled datablock */
1139f988b79SJean-Baptiste Boric 		break;
1149f988b79SJean-Baptiste Boric 	default:
1159f988b79SJean-Baptiste Boric 		*nblk = 0;
1169f988b79SJean-Baptiste Boric 		error = EINVAL;
1179f988b79SJean-Baptiste Boric 		break;
1189f988b79SJean-Baptiste Boric 	}
1199f988b79SJean-Baptiste Boric 
1209f988b79SJean-Baptiste Boric 	return error;
1219f988b79SJean-Baptiste Boric }
1229f988b79SJean-Baptiste Boric 
1239f988b79SJean-Baptiste Boric static int
estimate_size_walk(fsnode * root,char * dir,struct v7fs_geometry * geom)1249f988b79SJean-Baptiste Boric estimate_size_walk(fsnode *root, char *dir, struct v7fs_geometry *geom)
1259f988b79SJean-Baptiste Boric {
1269f988b79SJean-Baptiste Boric 	fsnode *cur;
1279f988b79SJean-Baptiste Boric 	int nentries;
1289f988b79SJean-Baptiste Boric 	size_t pathlen = strlen(dir);
1299f988b79SJean-Baptiste Boric 	char *mydir = dir + pathlen;
1309f988b79SJean-Baptiste Boric 	fsinode *fnode;
1319f988b79SJean-Baptiste Boric 	v7fs_daddr_t nblk;
1329f988b79SJean-Baptiste Boric 	int n;
1339f988b79SJean-Baptiste Boric 	off_t sz;
1349f988b79SJean-Baptiste Boric 
135*e1cdaee1SLionel Sambuc #if defined(__minix)
136*e1cdaee1SLionel Sambuc 	/* LSC: -Werror=maybe-uninitialized, when compiling with -O3. */
137*e1cdaee1SLionel Sambuc 	nblk = 0;
138*e1cdaee1SLionel Sambuc #endif /* defined(__minix) */
1399f988b79SJean-Baptiste Boric 	for (cur = root, nentries = 0; cur != NULL; cur = cur->next,
1409f988b79SJean-Baptiste Boric 		 nentries++, geom->ninode++) {
1419f988b79SJean-Baptiste Boric 		switch (cur->type & S_IFMT) {
1429f988b79SJean-Baptiste Boric 		default:
1439f988b79SJean-Baptiste Boric 			break;
1449f988b79SJean-Baptiste Boric 		case S_IFDIR:
1459f988b79SJean-Baptiste Boric 			if (!cur->child)
1469f988b79SJean-Baptiste Boric 				break;
1479f988b79SJean-Baptiste Boric 			mydir[0] = '/';
1489f988b79SJean-Baptiste Boric 			strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
1499f988b79SJean-Baptiste Boric 			n = estimate_size_walk(cur->child, dir, geom);
1509f988b79SJean-Baptiste Boric 			sz = (n + 1/*..*/) * sizeof(struct v7fs_dirent);
1519f988b79SJean-Baptiste Boric 			v7fs_datablock_size(sz, &nblk);
1529f988b79SJean-Baptiste Boric 			mydir[0] = '\0';
1539f988b79SJean-Baptiste Boric 			geom->ndatablock += nblk;
1549f988b79SJean-Baptiste Boric 			geom->npuredatablk +=
1559f988b79SJean-Baptiste Boric 			    V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT;
1569f988b79SJean-Baptiste Boric 			break;
1579f988b79SJean-Baptiste Boric 		case S_IFREG:
1589f988b79SJean-Baptiste Boric 			fnode = cur->inode;
1599f988b79SJean-Baptiste Boric 			if (!(fnode->flags & FI_SIZED)) { /*Skip hard-link */
1609f988b79SJean-Baptiste Boric 				sz = fnode->st.st_size;
1619f988b79SJean-Baptiste Boric 				v7fs_datablock_size(sz, &nblk);
1629f988b79SJean-Baptiste Boric 				geom->ndatablock += nblk;
1639f988b79SJean-Baptiste Boric 				geom->npuredatablk +=
1649f988b79SJean-Baptiste Boric 				    V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT;
1659f988b79SJean-Baptiste Boric 				fnode->flags |= FI_SIZED;
1669f988b79SJean-Baptiste Boric 			}
1679f988b79SJean-Baptiste Boric 
1689f988b79SJean-Baptiste Boric 			break;
1699f988b79SJean-Baptiste Boric 		case S_IFLNK:
1709f988b79SJean-Baptiste Boric 			nblk = V7FSBSD_MAXSYMLINKLEN >> V7FS_BSHIFT;
1719f988b79SJean-Baptiste Boric 			geom->ndatablock += nblk;
1729f988b79SJean-Baptiste Boric 			geom->npuredatablk += nblk;
1739f988b79SJean-Baptiste Boric 			break;
1749f988b79SJean-Baptiste Boric 		}
1759f988b79SJean-Baptiste Boric 	}
1769f988b79SJean-Baptiste Boric 
1779f988b79SJean-Baptiste Boric 	return nentries;
1789f988b79SJean-Baptiste Boric }
1799f988b79SJean-Baptiste Boric 
1809f988b79SJean-Baptiste Boric static v7fs_daddr_t
calculate_fs_size(const struct v7fs_geometry * geom)1819f988b79SJean-Baptiste Boric calculate_fs_size(const struct v7fs_geometry *geom)
1829f988b79SJean-Baptiste Boric {
1839f988b79SJean-Baptiste Boric 	v7fs_daddr_t fs_blk, ilist_blk;
1849f988b79SJean-Baptiste Boric 
1859f988b79SJean-Baptiste Boric 	ilist_blk = V7FS_ROUND_BSIZE(geom->ninode *
1869f988b79SJean-Baptiste Boric 	    sizeof(struct v7fs_inode_diskimage)) >> V7FS_BSHIFT;
1879f988b79SJean-Baptiste Boric 	fs_blk = geom->ndatablock + ilist_blk + V7FS_ILIST_SECTOR;
1889f988b79SJean-Baptiste Boric 
1899f988b79SJean-Baptiste Boric 	VPRINTF("datablock:%d ilistblock:%d total:%d\n", geom->ndatablock,
1909f988b79SJean-Baptiste Boric 	    ilist_blk, fs_blk);
1919f988b79SJean-Baptiste Boric 
1929f988b79SJean-Baptiste Boric 	return fs_blk;
1939f988b79SJean-Baptiste Boric }
1949f988b79SJean-Baptiste Boric 
1959f988b79SJean-Baptiste Boric static void
determine_fs_size(fsinfo_t * fsopts,struct v7fs_geometry * geom)1969f988b79SJean-Baptiste Boric determine_fs_size(fsinfo_t *fsopts, struct v7fs_geometry *geom)
1979f988b79SJean-Baptiste Boric {
1989f988b79SJean-Baptiste Boric 	v7fs_daddr_t nblk = geom->ndatablock;
1999f988b79SJean-Baptiste Boric 	v7fs_daddr_t fsblk, n;
2009f988b79SJean-Baptiste Boric 	int32_t	nfiles = geom->ninode;
2019f988b79SJean-Baptiste Boric 
2029f988b79SJean-Baptiste Boric 	VPRINTF("size=%lld inodes=%lld fd=%d superb=%p onlyspec=%d\n",
2039f988b79SJean-Baptiste Boric 	    (long long)fsopts->size, (long long)fsopts->inodes, fsopts->fd,
2049f988b79SJean-Baptiste Boric 	    fsopts->superblock, fsopts->onlyspec);
2059f988b79SJean-Baptiste Boric 	VPRINTF("minsize=%lld maxsize=%lld freefiles=%lld freefilepc=%d "
2069f988b79SJean-Baptiste Boric 	    "freeblocks=%lld freeblockpc=%d sectorseize=%d\n",
2079f988b79SJean-Baptiste Boric 	    (long long)fsopts->minsize, (long long)fsopts->maxsize,
2089f988b79SJean-Baptiste Boric 	    (long long)fsopts->freefiles, fsopts->freefilepc,
2099f988b79SJean-Baptiste Boric 	    (long long)fsopts->freeblocks,  fsopts->freeblockpc,
2109f988b79SJean-Baptiste Boric 	    fsopts->sectorsize);
2119f988b79SJean-Baptiste Boric 
2129f988b79SJean-Baptiste Boric 	if ((fsopts->sectorsize > 0) && (fsopts->sectorsize != V7FS_BSIZE))
2139f988b79SJean-Baptiste Boric 		warnx("v7fs sector size is 512byte only. '-S %d' is ignored.",
2149f988b79SJean-Baptiste Boric 		    fsopts->sectorsize);
2159f988b79SJean-Baptiste Boric 
2169f988b79SJean-Baptiste Boric 	/* Free inode */
2179f988b79SJean-Baptiste Boric 	if (fsopts->freefiles) {
2189f988b79SJean-Baptiste Boric 		nfiles += fsopts->freefiles;
2199f988b79SJean-Baptiste Boric 	} else if ((n = fsopts->freefilepc)) {
2209f988b79SJean-Baptiste Boric 		nfiles += (nfiles * n) / (100 - n);
2219f988b79SJean-Baptiste Boric 	}
2229f988b79SJean-Baptiste Boric 	if (nfiles >= V7FS_INODE_MAX) {
2239f988b79SJean-Baptiste Boric 		errx(EXIT_FAILURE, "# of files(%d) over v7fs limit(%d).",
2249f988b79SJean-Baptiste Boric 		    nfiles, V7FS_INODE_MAX);
2259f988b79SJean-Baptiste Boric 	}
2269f988b79SJean-Baptiste Boric 
2279f988b79SJean-Baptiste Boric 	/* Free datablock */
2289f988b79SJean-Baptiste Boric 	if (fsopts->freeblocks) {
2299f988b79SJean-Baptiste Boric 		nblk += fsopts->freeblocks;
2309f988b79SJean-Baptiste Boric 	} else if ((n = fsopts->freeblockpc)) {
2319f988b79SJean-Baptiste Boric 		nblk += (nblk * n) / (100 - n);
2329f988b79SJean-Baptiste Boric 	}
2339f988b79SJean-Baptiste Boric 
2349f988b79SJean-Baptiste Boric 	/* Total size */
2359f988b79SJean-Baptiste Boric 	geom->ndatablock = nblk;
2369f988b79SJean-Baptiste Boric 	geom->ninode = nfiles;
2379f988b79SJean-Baptiste Boric 	fsblk = calculate_fs_size(geom);
2389f988b79SJean-Baptiste Boric 
2399f988b79SJean-Baptiste Boric 	if (fsblk >= V7FS_DADDR_MAX) {
2409f988b79SJean-Baptiste Boric 		errx(EXIT_FAILURE, "filesystem size(%d) over v7fs limit(%d).",
2419f988b79SJean-Baptiste Boric 		    fsblk, V7FS_DADDR_MAX);
2429f988b79SJean-Baptiste Boric 	}
2439f988b79SJean-Baptiste Boric 
2449f988b79SJean-Baptiste Boric 	n = fsopts->minsize >> V7FS_BSHIFT;
2459f988b79SJean-Baptiste Boric 	if (fsblk < n)
2469f988b79SJean-Baptiste Boric 		geom->ndatablock += (n - fsblk);
2479f988b79SJean-Baptiste Boric 
2489f988b79SJean-Baptiste Boric 	n = fsopts->maxsize >> V7FS_BSHIFT;
2499f988b79SJean-Baptiste Boric 	if (fsopts->maxsize > 0 && fsblk > n) {
2509f988b79SJean-Baptiste Boric 		errx(EXIT_FAILURE, "# of datablocks %d > %d", fsblk, n);
2519f988b79SJean-Baptiste Boric 	}
2529f988b79SJean-Baptiste Boric }
2539f988b79SJean-Baptiste Boric 
2549f988b79SJean-Baptiste Boric void
v7fs_estimate(const char * dir,fsnode * root,fsinfo_t * fsopts)2559f988b79SJean-Baptiste Boric v7fs_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts)
2569f988b79SJean-Baptiste Boric {
2579f988b79SJean-Baptiste Boric 	v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
2589f988b79SJean-Baptiste Boric 	char path[MAXPATHLEN + 1];
2599f988b79SJean-Baptiste Boric 	int ndir;
2609f988b79SJean-Baptiste Boric 	off_t sz;
2619f988b79SJean-Baptiste Boric 	v7fs_daddr_t nblk;
2629f988b79SJean-Baptiste Boric 	struct v7fs_geometry geom;
2639f988b79SJean-Baptiste Boric 
264*e1cdaee1SLionel Sambuc #if defined(__minix)
265*e1cdaee1SLionel Sambuc 	/* LSC: -Werror=maybe-uninitialized, when compiling with -O3. */
266*e1cdaee1SLionel Sambuc 	nblk = 0;
267*e1cdaee1SLionel Sambuc #endif /* defined(__minix) */
2689f988b79SJean-Baptiste Boric 	memset(&geom , 0, sizeof(geom));
2699f988b79SJean-Baptiste Boric 	strncpy(path, dir, sizeof(path));
2709f988b79SJean-Baptiste Boric 
2719f988b79SJean-Baptiste Boric 	/* Calculate strict size. */
2729f988b79SJean-Baptiste Boric 	ndir = estimate_size_walk(root, path, &geom);
2739f988b79SJean-Baptiste Boric 	sz = (ndir + 1/*..*/) * sizeof(struct v7fs_dirent);
2749f988b79SJean-Baptiste Boric 	v7fs_datablock_size(sz, &nblk);
2759f988b79SJean-Baptiste Boric 	geom.ndatablock += nblk;
2769f988b79SJean-Baptiste Boric 
2779f988b79SJean-Baptiste Boric 	/* Consider options. */
2789f988b79SJean-Baptiste Boric 	determine_fs_size(fsopts, &geom);
2799f988b79SJean-Baptiste Boric 
2809f988b79SJean-Baptiste Boric 	fsopts->size = calculate_fs_size(&geom) << V7FS_BSHIFT;
2819f988b79SJean-Baptiste Boric 	fsopts->inodes = geom.ninode;
2829f988b79SJean-Baptiste Boric 	v7fs_opts->npuredatablk = geom.npuredatablk; /* for progress bar */
2839f988b79SJean-Baptiste Boric }
284