xref: /illumos-gate/usr/src/cmd/svr4pkg/libinst/nblk.c (revision 5c51f124)
1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland  * CDDL HEADER START
3*5c51f124SMoriah Waterland  *
4*5c51f124SMoriah Waterland  * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland  * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland  * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland  *
8*5c51f124SMoriah Waterland  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland  * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland  * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland  * and limitations under the License.
12*5c51f124SMoriah Waterland  *
13*5c51f124SMoriah Waterland  * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland  * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland  * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland  *
19*5c51f124SMoriah Waterland  * CDDL HEADER END
20*5c51f124SMoriah Waterland  */
21*5c51f124SMoriah Waterland 
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*5c51f124SMoriah Waterland  * Use is subject to license terms.
25*5c51f124SMoriah Waterland  */
26*5c51f124SMoriah Waterland 
27*5c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*5c51f124SMoriah Waterland /* All Rights Reserved */
29*5c51f124SMoriah Waterland 
30*5c51f124SMoriah Waterland 
31*5c51f124SMoriah Waterland #include <sys/types.h>
32*5c51f124SMoriah Waterland #include <sys/param.h>
33*5c51f124SMoriah Waterland #include <sys/sysmacros.h>
34*5c51f124SMoriah Waterland 
35*5c51f124SMoriah Waterland /*
36*5c51f124SMoriah Waterland  * This should not be a constant, but for ufs it is 12, not 10 like for s5.
37*5c51f124SMoriah Waterland  */
38*5c51f124SMoriah Waterland #define	DIRECT 12	/* Number of logical blocks before indirection */
39*5c51f124SMoriah Waterland 
40*5c51f124SMoriah Waterland fsblkcnt_t
nblk(fsblkcnt_t size,ulong_t bsize,ulong_t frsize)41*5c51f124SMoriah Waterland nblk(fsblkcnt_t size, ulong_t bsize, ulong_t frsize)
42*5c51f124SMoriah Waterland {
43*5c51f124SMoriah Waterland 	fsblkcnt_t tot, count, count1, d_indirect, t_indirect, ind;
44*5c51f124SMoriah Waterland 	fsblkcnt_t frags = 0;
45*5c51f124SMoriah Waterland 
46*5c51f124SMoriah Waterland 	if (size == 0 || bsize == 0)
47*5c51f124SMoriah Waterland 		return (1);
48*5c51f124SMoriah Waterland 
49*5c51f124SMoriah Waterland 	/*
50*5c51f124SMoriah Waterland 	 * Need to keep track of indirect blocks.
51*5c51f124SMoriah Waterland 	 */
52*5c51f124SMoriah Waterland 
53*5c51f124SMoriah Waterland 	ind = howmany(bsize, sizeof (daddr_t));
54*5c51f124SMoriah Waterland 	d_indirect = ind + DIRECT; 			/* double indirection */
55*5c51f124SMoriah Waterland 	t_indirect = ind * (ind + 1) + DIRECT; 		/* triple indirection */
56*5c51f124SMoriah Waterland 
57*5c51f124SMoriah Waterland 	tot = howmany(size, bsize);
58*5c51f124SMoriah Waterland 
59*5c51f124SMoriah Waterland 	if (tot > t_indirect) {
60*5c51f124SMoriah Waterland 		count1 = (tot - ind * ind - (DIRECT + 1)) / ind;
61*5c51f124SMoriah Waterland 		count = count1 + count1 / ind + ind + 3;
62*5c51f124SMoriah Waterland 	} else if (tot > d_indirect) {
63*5c51f124SMoriah Waterland 		count = (tot - (DIRECT + 1)) / ind + 2;
64*5c51f124SMoriah Waterland 	} else if (tot > DIRECT) {
65*5c51f124SMoriah Waterland 		count = 1;
66*5c51f124SMoriah Waterland 	} else {
67*5c51f124SMoriah Waterland 		count = 0;
68*5c51f124SMoriah Waterland 		frags = (frsize > 0) ?
69*5c51f124SMoriah Waterland 		    roundup(size, frsize) :
70*5c51f124SMoriah Waterland 		    roundup(size, bsize);
71*5c51f124SMoriah Waterland 	}
72*5c51f124SMoriah Waterland 
73*5c51f124SMoriah Waterland 	/* Accounting for the indirect blocks, the total becomes */
74*5c51f124SMoriah Waterland 	tot += count;
75*5c51f124SMoriah Waterland 
76*5c51f124SMoriah Waterland 	/*
77*5c51f124SMoriah Waterland 	 * calculate number of 512 byte blocks, for frag or full block cases.
78*5c51f124SMoriah Waterland 	 */
79*5c51f124SMoriah Waterland 	if (!frags)
80*5c51f124SMoriah Waterland 		tot *= howmany(bsize, DEV_BSIZE);
81*5c51f124SMoriah Waterland 	else
82*5c51f124SMoriah Waterland 		tot = howmany(frags, DEV_BSIZE);
83*5c51f124SMoriah Waterland 	return (tot);
84*5c51f124SMoriah Waterland }
85