1/*	$NetBSD: v7fs_estimate.c,v 1.1 2011/07/18 08:58:39 uch 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.1 2011/07/18 08:58:39 uch 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
57struct 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
65static int
66v7fs_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
123static int
124estimate_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
176static v7fs_daddr_t
177calculate_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
191static void
192determine_fs_size(fsinfo_t *fsopts, struct v7fs_geometry *geom)
193{
194	v7fs_daddr_t nblk = geom->ndatablock;
195	v7fs_daddr_t fsblk;
196	int32_t	nfiles = geom->ninode;
197	int n;
198
199	VPRINTF("size=%lld inodes=%lld fd=%d superb=%p onlyspec=%d\n",
200	    (long long)fsopts->size, (long long)fsopts->inodes, fsopts->fd,
201	    fsopts->superblock, fsopts->onlyspec);
202	VPRINTF("minsize=%lld maxsize=%lld freefiles=%lld freefilepc=%d "
203	    "freeblocks=%lld freeblockpc=%d sectorseize=%d\n",
204	    (long long)fsopts->minsize, (long long)fsopts->maxsize,
205	    (long long)fsopts->freefiles, fsopts->freefilepc,
206	    (long long)fsopts->freeblocks,  fsopts->freeblockpc,
207	    fsopts->sectorsize);
208
209	if ((fsopts->sectorsize > 0) && (fsopts->sectorsize != V7FS_BSIZE))
210		warnx("v7fs sector size is 512byte only. '-S %d' is ignored.",
211		    fsopts->sectorsize);
212
213	/* Free inode */
214	if (fsopts->freefiles) {
215		nfiles += fsopts->freefiles;
216	} else if ((n = fsopts->freefilepc)) {
217		nfiles += (nfiles * n) / (100 - n);
218	}
219	if (nfiles >= V7FS_INODE_MAX) {
220		errx(EXIT_FAILURE, "# of files(%d) over v7fs limit(%d).",
221		    nfiles, V7FS_INODE_MAX);
222	}
223
224	/* Free datablock */
225	if (fsopts->freeblocks) {
226		nblk += fsopts->freeblocks;
227	} else if ((n = fsopts->freeblockpc)) {
228		nblk += (nblk * n) / (100 - n);
229	}
230
231	/* Total size */
232	geom->ndatablock = nblk;
233	geom->ninode = nfiles;
234	fsblk = calculate_fs_size(geom);
235
236	if (fsblk >= V7FS_DADDR_MAX) {
237		errx(EXIT_FAILURE, "filesystem size(%d) over v7fs limit(%d).",
238		    fsblk, V7FS_DADDR_MAX);
239	}
240
241	n = fsopts->minsize >> V7FS_BSHIFT;
242	if (fsblk < n)
243		geom->ndatablock += (n - fsblk);
244
245	n = fsopts->maxsize >> V7FS_BSHIFT;
246	if (fsopts->maxsize > 0 && fsblk > n) {
247		errx(EXIT_FAILURE, "# of datablocks %d > %d", fsblk, n);
248	}
249}
250
251void
252v7fs_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts)
253{
254	v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
255	char path[MAXPATHLEN + 1];
256	int ndir;
257	off_t sz;
258	v7fs_daddr_t nblk;
259	struct v7fs_geometry geom;
260
261	memset(&geom , 0, sizeof(geom));
262	strncpy(path, dir, sizeof(path));
263
264	/* Calculate strict size. */
265	ndir = estimate_size_walk(root, path, &geom);
266	sz = (ndir + 1/*..*/) * sizeof(struct v7fs_dirent);
267	v7fs_datablock_size(sz, &nblk);
268	geom.ndatablock += nblk;
269
270	/* Consider options. */
271	determine_fs_size(fsopts, &geom);
272
273	fsopts->size = calculate_fs_size(&geom) << V7FS_BSHIFT;
274	fsopts->inodes = geom.ninode;
275	v7fs_opts->npuredatablk = geom.npuredatablk; /* for progress bar */
276}
277