1 /* 2 * Copyright (c) 1986, 1989, 1991 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)lfs_inode.c 7.55 (Berkeley) 01/19/92 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/mount.h> 13 #include <sys/proc.h> 14 #include <sys/file.h> 15 #include <sys/buf.h> 16 #include <sys/vnode.h> 17 #include <sys/kernel.h> 18 #include <sys/malloc.h> 19 20 #include <ufs/ufs/quota.h> 21 #include <ufs/ufs/inode.h> 22 #include <ufs/ufs/ufsmount.h> 23 #include <ufs/ufs/ufs_extern.h> 24 25 #include <ufs/lfs/lfs.h> 26 #include <ufs/lfs/lfs_extern.h> 27 28 int 29 lfs_init() 30 { 31 #ifdef VERBOSE 32 printf("lfs_init\n"); 33 #endif 34 return (ufs_init()); 35 } 36 37 /* 38 * Look up an LFS dinode number to find its incore vnode. If not already 39 * in core, read it in from the specified device. Return the inode locked. 40 * Detection and handling of mount points must be done by the calling routine. 41 */ 42 int 43 lfs_vget(mntp, ino, vpp) 44 struct mount *mntp; 45 ino_t ino; 46 struct vnode **vpp; 47 { 48 register struct lfs *fs; 49 register struct inode *ip; 50 struct buf *bp; 51 struct ifile *ifp; 52 struct vnode *vp; 53 struct ufsmount *ump; 54 daddr_t daddr; 55 dev_t dev; 56 int error; 57 58 #ifdef VERBOSE 59 printf("lfs_vget\n"); 60 #endif 61 ump = VFSTOUFS(mntp); 62 dev = ump->um_dev; 63 if ((*vpp = ufs_ihashget(dev, ino)) != NULL) 64 return (0); 65 66 /* Translate the inode number to a disk address. */ 67 fs = ump->um_lfs; 68 if (ino == LFS_IFILE_INUM) 69 daddr = fs->lfs_idaddr; 70 else { 71 LFS_IENTRY(ifp, fs, ino, bp); 72 daddr = ifp->if_daddr; 73 brelse(bp); 74 if (daddr == LFS_UNUSED_DADDR) 75 return (ENOENT); 76 } 77 78 /* Allocate new vnode/inode. */ 79 if (error = lfs_vcreate(mntp, ino, &vp)) { 80 *vpp = NULL; 81 return (error); 82 } 83 84 /* 85 * Put it onto its hash chain and lock it so that other requests for 86 * this inode will block if they arrive while we are sleeping waiting 87 * for old data structures to be purged or for the contents of the 88 * disk portion of this inode to be read. 89 */ 90 ip = VTOI(vp); 91 ufs_ihashins(ip); 92 93 /* 94 * XXX 95 * This may not need to be here, logically it should go down with 96 * the i_devvp initialization. 97 * Ask Kirk. 98 */ 99 ip->i_lfs = ump->um_lfs; 100 101 /* Read in the disk contents for the inode, copy into the inode. */ 102 if (error = 103 bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) { 104 /* 105 * The inode does not contain anything useful, so it 106 * would be misleading to leave it on its hash chain. 107 * Iput() will return it to the free list. 108 */ 109 remque(ip); 110 ip->i_forw = ip; 111 ip->i_back = ip; 112 113 /* Unlock and discard unneeded inode. */ 114 ufs_iput(ip); 115 brelse(bp); 116 *vpp = NULL; 117 return (error); 118 } 119 ip->i_din = *lfs_ifind(fs, ino, bp->b_un.b_dino); 120 brelse(bp); 121 122 /* 123 * Initialize the vnode from the inode, check for aliases. In all 124 * cases re-init ip, the underlying vnode/inode may have changed. 125 */ 126 if (error = ufs_vinit(mntp, &lfs_specops, LFS_FIFOOPS, &vp)) { 127 ufs_iput(ip); 128 *vpp = NULL; 129 return (error); 130 } 131 /* 132 * Finish inode initialization now that aliasing has been resolved. 133 */ 134 ip->i_devvp = ump->um_devvp; 135 VREF(ip->i_devvp); 136 *vpp = vp; 137 return (0); 138 } 139 140 int 141 lfs_update(vp, ta, tm, waitfor) 142 register struct vnode *vp; 143 struct timeval *ta, *tm; 144 int waitfor; 145 { 146 struct inode *ip; 147 148 #ifdef VERBOSE 149 printf("lfs_update\n"); 150 #endif 151 if (vp->v_mount->mnt_flag & MNT_RDONLY) 152 return (0); 153 ip = VTOI(vp); 154 if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 155 return (0); 156 if (ip->i_flag&IACC) 157 ip->i_atime = ta->tv_sec; 158 if (ip->i_flag&IUPD) { 159 ip->i_mtime = tm->tv_sec; 160 INCRQUAD((ip)->i_modrev); 161 } 162 if (ip->i_flag&ICHG) 163 ip->i_ctime = time.tv_sec; 164 ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 165 166 /* 167 * XXX 168 * I'm not real sure what to do here; once we have fsync and partial 169 * segments working in the LFS context, this must be fixed to be 170 * correct. The contents of the inode have to be pushed back to 171 * stable storage. 172 */ 173 return (0); 174 } 175 176 /* Update segment usage information when removing a block. */ 177 #define UPDATE_SEGUSE \ 178 if (lastseg != -1) { \ 179 LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \ 180 sup->su_nbytes -= fs->lfs_bsize * num; \ 181 LFS_UBWRITE(sup_bp); \ 182 blocksreleased += num; \ 183 } 184 185 #define SEGDEC { \ 186 if (daddr != UNASSIGNED) { \ 187 if (lastseg != (seg = datosn(fs, daddr))) { \ 188 UPDATE_SEGUSE; \ 189 num = 1; \ 190 lastseg = seg; \ 191 } else \ 192 ++num; \ 193 } \ 194 } 195 196 /* 197 * Truncate the inode ip to at most length size. Update segment usage 198 * table information. 199 */ 200 /* ARGSUSED */ 201 int 202 lfs_truncate(vp, length, flags) 203 struct vnode *vp; 204 u_long length; 205 int flags; 206 { 207 register INDIR *ap; 208 register int i; 209 register daddr_t *daddrp; 210 struct buf *bp, *sup_bp; 211 struct inode *ip; 212 struct lfs *fs; 213 INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 214 SEGUSE *sup; 215 daddr_t daddr, lastblock, lbn, olastblock; 216 off_t off; 217 long blocksreleased; 218 int error, depth, lastseg, num, offset, seg, size; 219 220 #ifdef VERBOSE 221 printf("lfs_truncate\n"); 222 #endif 223 vnode_pager_setsize(vp, length); 224 ip = VTOI(vp); 225 /* If length is larger than the file, just update the times. */ 226 if (ip->i_size <= length) { 227 ip->i_flag |= ICHG|IUPD; 228 return (lfs_update(vp, &time, &time, 1)); 229 } 230 /* 231 * Calculate index into inode's block list of last direct and indirect 232 * blocks (if any) which we want to keep. Lastblock is 0 when the 233 * file is truncated to 0. 234 */ 235 fs = ip->i_lfs; 236 lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 237 olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 238 239 /* 240 * Update the size of the file. If the file is not being truncated to 241 * a block boundry, the contents of the partial block following the end 242 * of the file must be zero'ed in case it ever become accessable again 243 * because of subsequent file growth. 244 */ 245 offset = blkoff(fs, length); 246 if (offset == 0) 247 ip->i_size = length; 248 else { 249 lbn = lblkno(fs, length); 250 #ifdef QUOTA 251 if (error = getinoquota(ip)) 252 return (error); 253 #endif 254 if (error = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 255 return (error); 256 ip->i_size = length; 257 size = blksize(fs); 258 (void)vnode_pager_uncache(vp); 259 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 260 allocbuf(bp, size); 261 LFS_UBWRITE(bp); 262 } 263 /* 264 * Modify sup->su_nbyte counters for each deleted block; keep track 265 * of number of blocks removed for ip->i_blocks. 266 */ 267 blocksreleased = 0; 268 num = 0; 269 lastseg = -1; 270 271 for (lbn = olastblock; lbn >= lastblock;) { 272 lfs_bmaparray(vp, lbn, &daddr, a, &depth); 273 if (lbn == olastblock) 274 for (i = NIADDR + 2; i--;) 275 a_end[i] = a[i]; 276 switch (depth) { 277 case 0: /* Direct block. */ 278 daddr = ip->i_db[lbn]; 279 SEGDEC; 280 ip->i_db[lbn] = 0; 281 --lbn; 282 break; 283 #ifdef DIAGNOSTIC 284 case 1: /* An indirect block. */ 285 panic("lfs_truncate: lfs_bmaparray returned depth 1"); 286 /* NOTREACHED */ 287 #endif 288 default: /* Chain of indirect blocks. */ 289 ap = a + --depth; 290 if (ap->in_off > 0 && lbn != lastblock) { 291 lbn -= ap->in_off < lbn - lastblock ? 292 ap->in_off : lbn - lastblock; 293 break; 294 } 295 for (; depth && (ap->in_off == 0 || lbn == lastblock); 296 --ap, --depth) { 297 /* 298 * XXX 299 * The indirect block may not yet exist, so 300 * bread will create one just so we can free 301 * it. 302 */ 303 if (bread(vp, 304 ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 305 panic("lfs_truncate: bread bno %d", 306 ap->in_lbn); 307 daddrp = bp->b_un.b_daddr + ap->in_off; 308 for (i = ap->in_off; 309 i++ <= a_end[depth].in_off;) { 310 daddr = *daddrp++; 311 SEGDEC; 312 } 313 a_end[depth].in_off=NINDIR(fs)-1; 314 if (ap->in_off > 0 && lbn == lastblock) { 315 bzero(bp->b_un.b_daddr + ap->in_off, 316 fs->lfs_bsize - 317 ap->in_off * sizeof(daddr_t)); 318 LFS_UBWRITE(bp); 319 } else 320 brelse (bp); 321 } 322 if (a[1].in_off == 0) { 323 off = a[0].in_off; 324 daddr = ip->i_ib[off]; 325 SEGDEC; 326 ip->i_ib[off] = 0; 327 } 328 if (lbn == lastblock) 329 --lbn; 330 else { 331 lbn -= NINDIR(fs); 332 if (lbn < lastblock) 333 lbn = lastblock; 334 } 335 } 336 } 337 UPDATE_SEGUSE; 338 ip->i_blocks -= blocksreleased; 339 /* 340 * XXX 341 * Currently, we don't know when we allocate an indirect block, so 342 * ip->i_blocks isn't getting incremented appropriately. As a result, 343 * when we delete any indirect blocks, we get a bad number here. 344 */ 345 if (ip->i_blocks < 0) 346 ip->i_blocks = 0; 347 ip->i_flag |= ICHG|IUPD; 348 (void)vinvalbuf(vp, length > 0); 349 error = lfs_update(vp, &time, &time, MNT_WAIT); 350 return (0); 351 } 352