xref: /original-bsd/sys/ufs/lfs/lfs_balloc.c (revision 753853ba)
1 /*
2  * Copyright (c) 1989, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_balloc.c	7.27 (Berkeley) 03/18/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/buf.h>
12 #include <sys/proc.h>
13 #include <sys/vnode.h>
14 #include <sys/mount.h>
15 #include <sys/resourcevar.h>
16 #include <sys/specdev.h>
17 #include <sys/trace.h>
18 
19 #include <ufs/ufs/quota.h>
20 #include <ufs/ufs/inode.h>
21 #include <ufs/ufs/ufsmount.h>
22 
23 #include <ufs/lfs/lfs.h>
24 #include <ufs/lfs/lfs_extern.h>
25 
26 int lfs_getlbns __P((struct vnode *, daddr_t, INDIR *, int *));
27 
28 /*
29  * Bmap converts a the logical block number of a file to its physical block
30  * number on the disk. The conversion is done by using the logical block
31  * number to index into the array of block pointers described by the dinode.
32  */
33 int
34 lfs_bmap(vp, bn, vpp, bnp)
35 	struct vnode *vp;
36 	register daddr_t bn;
37 	struct vnode **vpp;
38 	daddr_t *bnp;
39 {
40 #ifdef VERBOSE
41 	printf("lfs_bmap\n");
42 #endif
43 	/*
44 	 * Check for underlying vnode requests and ensure that logical
45 	 * to physical mapping is requested.
46 	 */
47 	if (vpp != NULL)
48 		*vpp = VTOI(vp)->i_devvp;
49 	if (bnp == NULL)
50 		return (0);
51 
52 	return (lfs_bmaparray(vp, bn, bnp, NULL, NULL));
53 }
54 
55 /*
56  * LFS has a different version of bmap from FFS because of a naming conflict.
57  * In FFS, meta blocks are given real disk addresses at allocation time, and
58  * are linked into the device vnode, using a logical block number which is
59  * the same as the physical block number.  This can't be done by LFS because
60  * blocks aren't given disk addresses until they're written, so there's no
61  * way to distinguish the meta-data blocks for one file from any other file.
62  * This means that meta-data blocks have to be on the vnode for the file so
63  * they can be found, and have to have "names" different from the standard
64  * data blocks.  To do this, we divide the name space into positive and
65  * negative block numbers, and give the meta-data blocks negative logical
66  * numbers.  Indirect blocks are addressed by the negative address of the
67  * first data block to which they point.  Double indirect blocks are addressed
68  * by one less than the address of the first indirect block to which they
69  * point.  Triple indirect blocks are addressed by one less than the address
70  * of the first double indirect block to which they point.
71  */
72 int
73 lfs_bmaparray(vp, bn, bnp, ap, nump)
74 	struct vnode *vp;
75 	register daddr_t bn;
76 	daddr_t *bnp;
77 	INDIR *ap;
78 	int *nump;
79 {
80 	register struct inode *ip;
81 	struct buf *bp;
82 	struct lfs *fs;
83 	struct vnode *devvp;
84 	INDIR a[NIADDR], *xap;
85 	daddr_t *bap, daddr;
86 	long metalbn;
87 	int error, num, off;
88 
89 
90 	ip = VTOI(vp);
91 #ifdef VERBOSE
92 	printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
93 #endif
94 #ifdef DIAGNOSTIC
95 	if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
96 		panic("lfs_bmaparray: invalid arguments");
97 #endif
98 
99 	xap = ap == NULL ? a : ap;
100 	if (error = lfs_getlbns(vp, bn, xap, nump))
101 		return (error);
102 
103 	num = *nump;
104 
105 	if (num == 0) {
106 		*bnp = ip->i_db[bn];
107 		if (*bnp == 0)
108 			*bnp = UNASSIGNED;
109 		return (0);
110 	}
111 
112 
113 	/* Get disk address out of indirect block array */
114 	daddr = ip->i_ib[xap->in_off];
115 
116 	/* Fetch through the indirect blocks. */
117 	fs = ip->i_lfs;
118 	devvp = VFSTOUFS(vp->v_mount)->um_devvp;
119 
120 	for (bp = NULL, ++xap; daddr && --num; ++xap) {
121 		/* If looking for a meta-block, break out when we find it. */
122 		metalbn = xap->in_lbn;
123 		if (metalbn == bn)
124 			break;
125 
126 		/*
127 		 * Read in the appropriate indirect block.  LFS can't do a
128 		 * bread because bread knows that FFS will hand it the device
129 		 * vnode, not the file vnode, so the b_dev and b_blkno would
130 		 * be wrong.
131 		 *
132 		 * XXX
133 		 * This REALLY needs to be fixed, at the very least it needs
134 		 * to be rethought when the buffer cache goes away.  When it's
135 		 * fixed, change lfs_bmaparray and lfs_getlbns to take an ip,
136 		 * not a vp.
137 		 */
138 		if (bp)
139 			brelse(bp);
140 		bp = getblk(vp, metalbn, fs->lfs_bsize);
141 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
142 			trace(TR_BREADHIT, pack(vp, size), metalbn);
143 		} else {
144 			trace(TR_BREADMISS, pack(vp, size), metalbn);
145 			bp->b_blkno = daddr;
146 			bp->b_flags |= B_READ;
147 			bp->b_dev = devvp->v_rdev;
148 			(devvp->v_op->vop_strategy)(bp);
149 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
150 			if (error = biowait(bp)) {
151 				brelse(bp);
152 				return (error);
153 			}
154 		}
155 		daddr = bp->b_un.b_daddr[xap->in_off];
156 	}
157 	if (bp)
158 		brelse(bp);
159 
160 	*bnp = daddr == 0 ? UNASSIGNED : daddr;
161 	return (0);
162 }
163 
164 /*
165  * Create an array of logical block number/offset pairs which represent the
166  * path of indirect blocks required to access a data block.  The first "pair"
167  * contains the logical block number of the appropriate single, double or
168  * triple indirect block and the offset into the inode indirect block array.
169  * Note, the logical block number of the inode single/double/triple indirect
170  * block appears twice in the array, once with the offset into the i_ib and
171  * once with the offset into the page itself.
172  */
173 int
174 lfs_getlbns(vp, bn, ap, nump)
175 	struct vnode *vp;
176 	register daddr_t bn;
177 	INDIR *ap;
178 	int *nump;
179 {
180 	struct lfs *fs;
181 	long metalbn, realbn;
182 	int j, off, sh;
183 
184 #ifdef VERBOSE
185 	printf("lfs_getlbns: bn %d, inode %d\n", bn, VTOI(vp)->i_number);
186 #endif
187 	*nump = 0;
188 	realbn = bn;
189 	if ((long)bn < 0)
190 		bn = -(long)bn;
191 
192 	/* The first NDADDR blocks are direct blocks. */
193 	if (bn < NDADDR)
194 		return (0);
195 
196 	/*
197 	 * Determine the number of levels of indirection.  After this loop
198 	 * is done, sh indicates the number of data blocks possible at the
199 	 * given level of indirection, and NIADDR - j is the number of levels
200 	 * of indirection needed to locate the requested block.
201 	 */
202 	bn -= NDADDR;
203 	fs = VTOI(vp)->i_lfs;
204 	sh = 1;
205 	for (j = NIADDR; j > 0; j--) {
206 		sh *= NINDIR(fs);
207 		if (bn < sh)
208 			break;
209 		bn -= sh;
210 	}
211 	if (j == 0)
212 		return (EFBIG);
213 
214 	/* Calculate the address of the first meta-block. */
215 	if (realbn >= 0)
216 		metalbn = -(realbn - bn + NIADDR - j);
217 	else
218 		metalbn = -(-realbn - bn + NIADDR - j);
219 
220 	/*
221 	 * At each iteration, off is the offset into the bap array which is
222 	 * an array of disk addresses at the current level of indirection.
223 	 * The logical block number and the offset in that block are stored
224 	 * into the argument array.
225 	 */
226 	++*nump;
227 	ap->in_lbn = metalbn;
228 	ap->in_off = off = NIADDR - j;
229 	ap++;
230 	for (; j <= NIADDR; j++) {
231 		/* If searching for a meta-data block, quit when found. */
232 		if (metalbn == realbn)
233 			break;
234 
235 		sh /= NINDIR(fs);
236 		off = (bn / sh) % NINDIR(fs);
237 
238 		++*nump;
239 		ap->in_lbn = metalbn;
240 		ap->in_off = off;
241 		++ap;
242 
243 		metalbn -= -1 + off * sh;
244 	}
245 	return (0);
246 }
247 
248 int
249 lfs_balloc(vp, iosize, lbn, bpp)
250 	struct vnode *vp;
251 	u_long iosize;
252 	daddr_t lbn;
253 	struct buf **bpp;
254 {
255 	struct buf *bp;
256 	struct inode *ip;
257 	struct lfs *fs;
258 	daddr_t daddr;
259 	int error, newblock;
260 
261 	ip = VTOI(vp);
262 	fs = ip->i_lfs;
263 
264 	/*
265 	 * Three cases: it's a block beyond the end of file, it's a block in
266 	 * the file that may or may not have been assigned a disk address or
267 	 * we're writing an entire block.  Note, if the daddr is unassigned,
268 	 * the block might still have existed in the cache.  If it did, make
269 	 * sure we don't count it as a new block or zero out its contents.
270 	 */
271 	newblock = ip->i_size <= lbn << fs->lfs_bshift;
272 	if (!newblock && (error = lfs_bmap(vp, lbn, NULL, &daddr)))
273 		return (error);
274 
275 	if (newblock || daddr == UNASSIGNED || iosize == fs->lfs_bsize) {
276 		*bpp = bp = getblk(vp, lbn, fs->lfs_bsize);
277 		if (newblock ||
278 		    daddr == UNASSIGNED && !(bp->b_flags & B_CACHE)) {
279 			++ip->i_blocks;
280 			if (iosize != fs->lfs_bsize)
281 				clrbuf(bp);
282 		}
283 		return (0);
284 	}
285 	return (bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp));
286 
287 }
288