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