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.35 (Berkeley) 08/01/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/trace.h> 17 18 #include <miscfs/specfs/specdev.h> 19 20 #include <ufs/ufs/quota.h> 21 #include <ufs/ufs/inode.h> 22 #include <ufs/ufs/ufsmount.h> 23 24 #include <ufs/lfs/lfs.h> 25 #include <ufs/lfs/lfs_extern.h> 26 27 int lfs_getlbns __P((struct vnode *, daddr_t, INDIR *, int *)); 28 29 /* 30 * Bmap converts a the logical block number of a file to its physical block 31 * number on the disk. The conversion is done by using the logical block 32 * number to index into the array of block pointers described by the dinode. 33 */ 34 int 35 lfs_bmap(ap) 36 struct vop_bmap_args /* { 37 struct vnode *a_vp; 38 daddr_t a_bn; 39 struct vnode **a_vpp; 40 daddr_t *a_bnp; 41 } */ *ap; 42 { 43 /* 44 * Check for underlying vnode requests and ensure that logical 45 * to physical mapping is requested. 46 */ 47 if (ap->a_vpp != NULL) 48 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp; 49 if (ap->a_bnp == NULL) 50 return (0); 51 52 return (lfs_bmaparray(ap->a_vp, ap->a_bn, ap->a_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 struct vop_strategy_args vop_strategy_a; 89 90 ip = VTOI(vp); 91 #ifdef DIAGNOSTIC 92 if (ap != NULL && nump == NULL || ap == NULL && nump != NULL) 93 panic("lfs_bmaparray: invalid arguments"); 94 #endif 95 96 xap = ap == NULL ? a : ap; 97 if (!nump) 98 nump = # 99 if (error = lfs_getlbns(vp, bn, xap, nump)) 100 return (error); 101 102 num = *nump; 103 if (num == 0) { 104 *bnp = ip->i_db[bn]; 105 if (*bnp == 0) 106 *bnp = UNASSIGNED; 107 return (0); 108 } 109 110 111 /* Get disk address out of indirect block array */ 112 daddr = ip->i_ib[xap->in_off]; 113 114 /* Fetch through the indirect blocks. */ 115 fs = ip->i_lfs; 116 devvp = VFSTOUFS(vp->v_mount)->um_devvp; 117 118 for (bp = NULL, ++xap; daddr && --num; ++xap) { 119 /* If looking for a meta-block, break out when we find it. */ 120 metalbn = xap->in_lbn; 121 if (metalbn == bn) 122 break; 123 124 /* 125 * Read in the appropriate indirect block. LFS can't do a 126 * bread because bread knows that FFS will hand it the device 127 * vnode, not the file vnode, so the b_dev and b_blkno would 128 * be wrong. 129 * 130 * XXX 131 * This REALLY needs to be fixed, at the very least it needs 132 * to be rethought when the buffer cache goes away. When it's 133 * fixed, change lfs_bmaparray and lfs_getlbns to take an ip, 134 * not a vp. 135 */ 136 if (bp) 137 brelse(bp); 138 bp = getblk(vp, metalbn, fs->lfs_bsize); 139 if (bp->b_flags & (B_DONE | B_DELWRI)) { 140 trace(TR_BREADHIT, pack(vp, size), metalbn); 141 } else { 142 trace(TR_BREADMISS, pack(vp, size), metalbn); 143 bp->b_blkno = daddr; 144 bp->b_flags |= B_READ; 145 bp->b_dev = devvp->v_rdev; 146 /* 147 * Call a strategy VOP by hand. 148 */ 149 vop_strategy_a.a_desc = VDESC(vop_strategy); 150 vop_strategy_a.a_bp=bp; 151 VOCALL(devvp->v_op, VOFFSET(vop_strategy), \ 152 &vop_strategy_a); 153 curproc->p_stats->p_ru.ru_inblock++; /* XXX */ 154 if (error = biowait(bp)) { 155 brelse(bp); 156 return (error); 157 } 158 } 159 daddr = bp->b_un.b_daddr[xap->in_off]; 160 } 161 if (bp) 162 brelse(bp); 163 164 *bnp = daddr == 0 ? UNASSIGNED : daddr; 165 return (0); 166 } 167 168 /* 169 * Create an array of logical block number/offset pairs which represent the 170 * path of indirect blocks required to access a data block. The first "pair" 171 * contains the logical block number of the appropriate single, double or 172 * triple indirect block and the offset into the inode indirect block array. 173 * Note, the logical block number of the inode single/double/triple indirect 174 * block appears twice in the array, once with the offset into the i_ib and 175 * once with the offset into the page itself. 176 */ 177 int 178 lfs_getlbns(vp, bn, ap, nump) 179 struct vnode *vp; 180 register daddr_t bn; 181 INDIR *ap; 182 int *nump; 183 { 184 struct lfs *fs; 185 long metalbn, realbn; 186 int j, numlevels, off, sh; 187 188 if (nump) 189 *nump = 0; 190 numlevels = 0; 191 realbn = bn; 192 if ((long)bn < 0) 193 bn = -(long)bn; 194 195 /* The first NDADDR blocks are direct blocks. */ 196 if (bn < NDADDR) 197 return (0); 198 199 /* 200 * Determine the number of levels of indirection. After this loop 201 * is done, sh indicates the number of data blocks possible at the 202 * given level of indirection, and NIADDR - j is the number of levels 203 * of indirection needed to locate the requested block. 204 */ 205 bn -= NDADDR; 206 fs = VTOI(vp)->i_lfs; 207 sh = 1; 208 for (j = NIADDR; j > 0; j--) { 209 sh *= NINDIR(fs); 210 if (bn < sh) 211 break; 212 bn -= sh; 213 } 214 if (j == 0) 215 return (EFBIG); 216 217 /* Calculate the address of the first meta-block. */ 218 if (realbn >= 0) 219 metalbn = -(realbn - bn + NIADDR - j); 220 else 221 metalbn = -(-realbn - bn + NIADDR - j); 222 223 /* 224 * At each iteration, off is the offset into the bap array which is 225 * an array of disk addresses at the current level of indirection. 226 * The logical block number and the offset in that block are stored 227 * into the argument array. 228 */ 229 ++numlevels; 230 ap->in_lbn = metalbn; 231 ap->in_off = off = NIADDR - j; 232 ap++; 233 for (; j <= NIADDR; j++) { 234 /* If searching for a meta-data block, quit when found. */ 235 if (metalbn == realbn) 236 break; 237 238 sh /= NINDIR(fs); 239 off = (bn / sh) % NINDIR(fs); 240 241 ++numlevels; 242 ap->in_lbn = metalbn; 243 ap->in_off = off; 244 ++ap; 245 246 metalbn -= -1 + off * sh; 247 } 248 if (nump) 249 *nump = numlevels; 250 return (0); 251 } 252 253 int 254 lfs_balloc(vp, iosize, lbn, bpp) 255 struct vnode *vp; 256 u_long iosize; 257 daddr_t lbn; 258 struct buf **bpp; 259 { 260 struct buf *bp; 261 struct inode *ip; 262 struct lfs *fs; 263 daddr_t daddr; 264 int error, newblock; 265 266 ip = VTOI(vp); 267 fs = ip->i_lfs; 268 269 /* 270 * Three cases: it's a block beyond the end of file, it's a block in 271 * the file that may or may not have been assigned a disk address or 272 * we're writing an entire block. Note, if the daddr is unassigned, 273 * the block might still have existed in the cache. If it did, make 274 * sure we don't count it as a new block or zero out its contents. 275 */ 276 newblock = ip->i_size <= lbn << fs->lfs_bshift; 277 if (!newblock && (error = VOP_BMAP(vp, lbn, NULL, &daddr))) 278 return (error); 279 280 if (newblock || daddr == UNASSIGNED || iosize == fs->lfs_bsize) { 281 *bpp = bp = getblk(vp, lbn, fs->lfs_bsize); 282 if (newblock || 283 daddr == UNASSIGNED && !(bp->b_flags & B_CACHE)) { 284 ip->i_blocks += btodb(fs->lfs_bsize); 285 fs->lfs_bfree -= btodb(fs->lfs_bsize); 286 if (iosize != fs->lfs_bsize) 287 clrbuf(bp); 288 } 289 return (0); 290 } 291 return (bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp)); 292 293 } 294