Home | History | Annotate | Line # | Download | only in v7fs
v7fs_estimate.c revision 1.3
      1 /*	$NetBSD: v7fs_estimate.c,v 1.3 2012/04/19 17:28:26 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     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.
     18  *
     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.
     30  */
     31 
     32 #if HAVE_NBTOOL_CONFIG_H
     33 #include "nbtool_config.h"
     34 #endif
     35 
     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 */
     40 
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include <stdlib.h>
     44 #include <errno.h>
     45 
     46 #if !HAVE_NBTOOL_CONFIG_H
     47 #include <sys/mount.h>	/*MAXPATHLEN */
     48 #endif
     49 
     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"
     56 
     57 struct v7fs_geometry {
     58 	v7fs_daddr_t ndatablock;
     59 	v7fs_ino_t ninode;
     60 	v7fs_daddr_t npuredatablk;
     61 };
     62 
     63 #define	VPRINTF(fmt, args...)	{ if (v7fs_newfs_verbose) printf(fmt, ##args); }
     64 
     65 static int
     66 v7fs_datablock_size(off_t sz, v7fs_daddr_t *nblk)
     67 {
     68 	struct v7fs_daddr_map map;
     69 	int error = 0;
     70 
     71 	if (sz == 0) {
     72 		*nblk = 0;
     73 		return 0;
     74 	}
     75 
     76 	if ((error = v7fs_datablock_addr(sz, &map))) {
     77 		return error;
     78 	}
     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;
    118 	}
    119 
    120 	return error;
    121 }
    122 
    123 static int
    124 estimate_size_walk(fsnode *root, char *dir, struct v7fs_geometry *geom)
    125 {
    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;
    134 
    135 	for (cur = root, nentries = 0; cur != NULL; cur = cur->next,
    136 		 nentries++, geom->ninode++) {
    137 		switch (cur->type & S_IFMT) {
    138 		default:
    139 			break;
    140 		case S_IFDIR:
    141 			if (!cur->child)
    142 				break;
    143 			mydir[0] = '/';
    144 			strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
    145 			n = estimate_size_walk(cur->child, dir, geom);
    146 			sz = (n + 1/*..*/) * sizeof(struct v7fs_dirent);
    147 			v7fs_datablock_size(sz, &nblk);
    148 			mydir[0] = '\0';
    149 			geom->ndatablock += nblk;
    150 			geom->npuredatablk +=
    151 			    V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT;
    152 			break;
    153 		case S_IFREG:
    154 			fnode = cur->inode;
    155 			if (!(fnode->flags & FI_SIZED)) { /*Skip hard-link */
    156 				sz = fnode->st.st_size;
    157 				v7fs_datablock_size(sz, &nblk);
    158 				geom->ndatablock += nblk;
    159 				geom->npuredatablk +=
    160 				    V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT;
    161 				fnode->flags |= FI_SIZED;
    162 			}
    163 
    164 			break;
    165 		case S_IFLNK:
    166 			nblk = V7FSBSD_MAXSYMLINKLEN >> V7FS_BSHIFT;
    167 			geom->ndatablock += nblk;
    168 			geom->npuredatablk += nblk;
    169 			break;
    170 		}
    171 	}
    172 
    173 	return nentries;
    174 }
    175 
    176 static v7fs_daddr_t
    177 calculate_fs_size(const struct v7fs_geometry *geom)
    178 {
    179 	v7fs_daddr_t fs_blk, ilist_blk;
    180 
    181 	ilist_blk = V7FS_ROUND_BSIZE(geom->ninode *
    182 	    sizeof(struct v7fs_inode_diskimage)) >> V7FS_BSHIFT;
    183 	fs_blk = geom->ndatablock + ilist_blk + V7FS_ILIST_SECTOR;
    184 
    185 	VPRINTF("datablock:%d ilistblock:%d total:%d\n", geom->ndatablock,
    186 	    ilist_blk, fs_blk);
    187 
    188 	return fs_blk;
    189 }
    190 
    191 static void
    192 determine_fs_size(fsinfo_t *fsopts, struct v7fs_geometry *geom)
    193 {
    194 	v7fs_daddr_t nblk = geom->ndatablock;
    195 	v7fs_daddr_t fsblk, n;
    196 	int32_t	nfiles = geom->ninode;
    197 
    198 	VPRINTF("size=%lld inodes=%lld fd=%d superb=%p onlyspec=%d\n",
    199 	    (long long)fsopts->size, (long long)fsopts->inodes, fsopts->fd,
    200 	    fsopts->superblock, fsopts->onlyspec);
    201 	VPRINTF("minsize=%lld maxsize=%lld freefiles=%lld freefilepc=%d "
    202 	    "freeblocks=%lld freeblockpc=%d sectorseize=%d\n",
    203 	    (long long)fsopts->minsize, (long long)fsopts->maxsize,
    204 	    (long long)fsopts->freefiles, fsopts->freefilepc,
    205 	    (long long)fsopts->freeblocks,  fsopts->freeblockpc,
    206 	    fsopts->sectorsize);
    207 
    208 	if ((fsopts->sectorsize > 0) && (fsopts->sectorsize != V7FS_BSIZE))
    209 		warnx("v7fs sector size is 512byte only. '-S %d' is ignored.",
    210 		    fsopts->sectorsize);
    211 
    212 	/* Free inode */
    213 	if (fsopts->freefiles) {
    214 		nfiles += fsopts->freefiles;
    215 	} else if ((n = fsopts->freefilepc)) {
    216 		nfiles += (nfiles * n) / (100 - n);
    217 	}
    218 	if (nfiles >= V7FS_INODE_MAX) {
    219 		errx(EXIT_FAILURE, "# of files(%d) over v7fs limit(%d).",
    220 		    nfiles, V7FS_INODE_MAX);
    221 	}
    222 
    223 	/* Free datablock */
    224 	if (fsopts->freeblocks) {
    225 		nblk += fsopts->freeblocks;
    226 	} else if ((n = fsopts->freeblockpc)) {
    227 		nblk += (nblk * n) / (100 - n);
    228 	}
    229 
    230 	/* Total size */
    231 	geom->ndatablock = nblk;
    232 	geom->ninode = nfiles;
    233 	fsblk = calculate_fs_size(geom);
    234 
    235 	if (fsblk >= V7FS_DADDR_MAX) {
    236 		errx(EXIT_FAILURE, "filesystem size(%d) over v7fs limit(%d).",
    237 		    fsblk, V7FS_DADDR_MAX);
    238 	}
    239 
    240 	n = fsopts->minsize >> V7FS_BSHIFT;
    241 	if (fsblk < n)
    242 		geom->ndatablock += (n - fsblk);
    243 
    244 	n = fsopts->maxsize >> V7FS_BSHIFT;
    245 	if (fsopts->maxsize > 0 && fsblk > n) {
    246 		errx(EXIT_FAILURE, "# of datablocks %d > %d", fsblk, n);
    247 	}
    248 }
    249 
    250 void
    251 v7fs_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts)
    252 {
    253 	v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
    254 	char path[MAXPATHLEN + 1];
    255 	int ndir;
    256 	off_t sz;
    257 	v7fs_daddr_t nblk;
    258 	struct v7fs_geometry geom;
    259 
    260 	memset(&geom , 0, sizeof(geom));
    261 	strncpy(path, dir, sizeof(path));
    262 
    263 	/* Calculate strict size. */
    264 	ndir = estimate_size_walk(root, path, &geom);
    265 	sz = (ndir + 1/*..*/) * sizeof(struct v7fs_dirent);
    266 	v7fs_datablock_size(sz, &nblk);
    267 	geom.ndatablock += nblk;
    268 
    269 	/* Consider options. */
    270 	determine_fs_size(fsopts, &geom);
    271 
    272 	fsopts->size = calculate_fs_size(&geom) << V7FS_BSHIFT;
    273 	fsopts->inodes = geom.ninode;
    274 	v7fs_opts->npuredatablk = geom.npuredatablk; /* for progress bar */
    275 }
    276