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