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 57 struct v7fs_geometry { 58 v7fs_daddr_t ndatablock; 59 v7fs_ino_t ninode; 60 v7fs_daddr_t npuredatablk; 61 }; 62 63 extern bool verbose; 64 #define VPRINTF(fmt, args...) { if (verbose) printf(fmt, ##args); } 65 66 static int 67 v7fs_datablock_size(off_t sz, v7fs_daddr_t *nblk) 68 { 69 struct v7fs_daddr_map map; 70 int error = 0; 71 72 if (sz == 0) { 73 *nblk = 0; 74 return 0; 75 } 76 77 if ((error = v7fs_datablock_addr(sz, &map))) { 78 return error; 79 } 80 switch (map.level) { 81 case 0: /* Direct */ 82 *nblk = map.index[0] + 1; 83 break; 84 case 1: 85 *nblk = V7FS_NADDR_DIRECT + /*direct */ 86 1/*addr[V7FS_NADDR_INDEX1]*/ + map.index[0] + 1; 87 break; 88 case 2: 89 *nblk = V7FS_NADDR_DIRECT + /*direct */ 90 1/*addr[V7FS_NADDR_INDEX1]*/ + 91 V7FS_DADDR_PER_BLOCK +/*idx1 */ 92 1/*addr[V7FS_NADDR_INDEX2]*/ + 93 map.index[0] + /* # of idx2 index block(filled) */ 94 map.index[0] * V7FS_DADDR_PER_BLOCK + /* of its datablocks*/ 95 1 + /*current idx2 indexblock */ 96 map.index[1] + 1; 97 break; 98 case 3: 99 *nblk = V7FS_NADDR_DIRECT + /*direct */ 100 1/*addr[V7FS_NADDR_INDEX1]*/ + 101 V7FS_DADDR_PER_BLOCK +/*idx1 */ 102 1/*addr[V7FS_NADDR_INDEX2]*/ + 103 V7FS_DADDR_PER_BLOCK + /* # of idx2 index block */ 104 V7FS_DADDR_PER_BLOCK * V7FS_DADDR_PER_BLOCK + 105 /*idx2 datablk */ 106 1/*addr[v7FS_NADDR_INDEX3*/ + 107 map.index[0] + /* # of lv1 index block(filled) */ 108 map.index[0] * V7FS_DADDR_PER_BLOCK * V7FS_DADDR_PER_BLOCK + 109 1 + /*lv1 */ 110 map.index[1] + /* #of lv2 index block(filled) */ 111 map.index[1] * V7FS_DADDR_PER_BLOCK + /*lv2 datablock */ 112 1 + /* current lv2 index block */ 113 map.index[2] + 1; /*filled datablock */ 114 break; 115 default: 116 *nblk = 0; 117 error = EINVAL; 118 break; 119 } 120 121 return error; 122 } 123 124 static int 125 estimate_size_walk(fsnode *root, char *dir, struct v7fs_geometry *geom) 126 { 127 fsnode *cur; 128 int nentries; 129 size_t pathlen = strlen(dir); 130 char *mydir = dir + pathlen; 131 fsinode *fnode; 132 v7fs_daddr_t nblk; 133 int n; 134 off_t sz; 135 136 for (cur = root, nentries = 0; cur != NULL; cur = cur->next, 137 nentries++, geom->ninode++) { 138 switch (cur->type & S_IFMT) { 139 default: 140 break; 141 case S_IFDIR: 142 if (!cur->child) 143 break; 144 mydir[0] = '/'; 145 strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen); 146 n = estimate_size_walk(cur->child, dir, geom); 147 sz = (n + 1/*..*/) * sizeof(struct v7fs_dirent); 148 v7fs_datablock_size(sz, &nblk); 149 mydir[0] = '\0'; 150 geom->ndatablock += nblk; 151 geom->npuredatablk += 152 V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT; 153 break; 154 case S_IFREG: 155 fnode = cur->inode; 156 if (!(fnode->flags & FI_SIZED)) { /*Skip hard-link */ 157 sz = fnode->st.st_size; 158 v7fs_datablock_size(sz, &nblk); 159 geom->ndatablock += nblk; 160 geom->npuredatablk += 161 V7FS_ROUND_BSIZE(sz) >> V7FS_BSHIFT; 162 fnode->flags |= FI_SIZED; 163 } 164 165 break; 166 case S_IFLNK: 167 nblk = V7FSBSD_MAXSYMLINKLEN >> V7FS_BSHIFT; 168 geom->ndatablock += nblk; 169 geom->npuredatablk += nblk; 170 break; 171 } 172 } 173 174 return nentries; 175 } 176 177 static v7fs_daddr_t 178 calculate_fs_size(const struct v7fs_geometry *geom) 179 { 180 v7fs_daddr_t fs_blk, ilist_blk; 181 182 ilist_blk = V7FS_ROUND_BSIZE(geom->ninode * 183 sizeof(struct v7fs_inode_diskimage)) >> V7FS_BSHIFT; 184 fs_blk = geom->ndatablock + ilist_blk + V7FS_ILIST_SECTOR; 185 186 VPRINTF("datablock:%d ilistblock:%d total:%d\n", geom->ndatablock, 187 ilist_blk, fs_blk); 188 189 return fs_blk; 190 } 191 192 static void 193 determine_fs_size(fsinfo_t *fsopts, struct v7fs_geometry *geom) 194 { 195 v7fs_daddr_t nblk = geom->ndatablock; 196 v7fs_daddr_t fsblk; 197 int32_t nfiles = geom->ninode; 198 int n; 199 200 VPRINTF("size=%lld inodes=%lld fd=%d superb=%p onlyspec=%d\n", 201 (long long)fsopts->size, (long long)fsopts->inodes, fsopts->fd, 202 fsopts->superblock, fsopts->onlyspec); 203 VPRINTF("minsize=%lld maxsize=%lld freefiles=%lld freefilepc=%d " 204 "freeblocks=%lld freeblockpc=%d sectorseize=%d\n", 205 (long long)fsopts->minsize, (long long)fsopts->maxsize, 206 (long long)fsopts->freefiles, fsopts->freefilepc, 207 (long long)fsopts->freeblocks, fsopts->freeblockpc, 208 fsopts->sectorsize); 209 210 if ((fsopts->sectorsize > 0) && (fsopts->sectorsize != V7FS_BSIZE)) 211 warnx("v7fs sector size is 512byte only. '-S %d' is ignored.", 212 fsopts->sectorsize); 213 214 /* Free inode */ 215 if (fsopts->freefiles) { 216 nfiles += fsopts->freefiles; 217 } else if ((n = fsopts->freefilepc)) { 218 nfiles += (nfiles * n) / (100 - n); 219 } 220 if (nfiles >= V7FS_INODE_MAX) { 221 errx(EXIT_FAILURE, "# of files(%d) over v7fs limit(%d).", 222 nfiles, V7FS_INODE_MAX); 223 } 224 225 /* Free datablock */ 226 if (fsopts->freeblocks) { 227 nblk += fsopts->freeblocks; 228 } else if ((n = fsopts->freeblockpc)) { 229 nblk += (nblk * n) / (100 - n); 230 } 231 232 /* Total size */ 233 geom->ndatablock = nblk; 234 geom->ninode = nfiles; 235 fsblk = calculate_fs_size(geom); 236 237 if (fsblk >= V7FS_DADDR_MAX) { 238 errx(EXIT_FAILURE, "filesystem size(%d) over v7fs limit(%d).", 239 fsblk, V7FS_DADDR_MAX); 240 } 241 242 n = fsopts->minsize >> V7FS_BSHIFT; 243 if (fsblk < n) 244 geom->ndatablock += (n - fsblk); 245 246 n = fsopts->maxsize >> V7FS_BSHIFT; 247 if (fsopts->maxsize > 0 && fsblk > n) { 248 errx(EXIT_FAILURE, "# of datablocks %d > %d", fsblk, n); 249 } 250 } 251 252 void 253 v7fs_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts) 254 { 255 v7fs_opt_t *v7fs_opts = fsopts->fs_specific; 256 char path[MAXPATHLEN + 1]; 257 int ndir; 258 off_t sz; 259 v7fs_daddr_t nblk; 260 struct v7fs_geometry geom; 261 262 memset(&geom , 0, sizeof(geom)); 263 strncpy(path, dir, sizeof(path)); 264 265 /* Calculate strict size. */ 266 ndir = estimate_size_walk(root, path, &geom); 267 sz = (ndir + 1/*..*/) * sizeof(struct v7fs_dirent); 268 v7fs_datablock_size(sz, &nblk); 269 geom.ndatablock += nblk; 270 271 /* Consider options. */ 272 determine_fs_size(fsopts, &geom); 273 274 fsopts->size = calculate_fs_size(&geom) << V7FS_BSHIFT; 275 fsopts->inodes = geom.ninode; 276 v7fs_opts->npuredatablk = geom.npuredatablk; /* for progress bar */ 277 } 278