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