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.54 (Berkeley) 01/18/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 LFS_SEGENTRY(sup, fs, lastseg, bp); \ 179 sup->su_nbytes -= fs->lfs_bsize * num; \ 180 LFS_UBWRITE(bp); \ 181 blocksreleased += num; \ 182 } 183 184 #define SEGDEC { \ 185 if (daddr != UNASSIGNED) { \ 186 if (lastseg != (seg = datosn(fs, daddr))) { \ 187 UPDATE_SEGUSE; \ 188 num = 1; \ 189 lastseg = seg; \ 190 } else \ 191 ++num; \ 192 } \ 193 } 194 195 /* 196 * Truncate the inode ip to at most length size. Update segment usage 197 * table information. 198 */ 199 /* ARGSUSED */ 200 int 201 lfs_truncate(vp, length, flags) 202 struct vnode *vp; 203 u_long length; 204 int flags; 205 { 206 register INDIR *ap; 207 register int i; 208 register daddr_t *daddrp; 209 struct buf *bp; 210 struct inode *ip; 211 struct lfs *fs; 212 INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 213 SEGUSE *sup; 214 daddr_t daddr, lastblock, lbn, olastblock; 215 off_t off; 216 long blocksreleased; 217 int error, depth, lastseg, num, offset, seg, size; 218 219 #ifdef VERBOSE 220 printf("lfs_truncate\n"); 221 #endif 222 vnode_pager_setsize(vp, length); 223 ip = VTOI(vp); 224 /* If length is larger than the file, just update the times. */ 225 if (ip->i_size <= length) { 226 ip->i_flag |= ICHG|IUPD; 227 return (lfs_update(vp, &time, &time, 1)); 228 } 229 /* 230 * Calculate index into inode's block list of last direct and indirect 231 * blocks (if any) which we want to keep. Lastblock is 0 when the 232 * file is truncated to 0. 233 */ 234 fs = ip->i_lfs; 235 lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 236 olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 237 238 /* 239 * Update the size of the file. If the file is not being truncated to 240 * a block boundry, the contents of the partial block following the end 241 * of the file must be zero'ed in case it ever become accessable again 242 * because of subsequent file growth. 243 */ 244 offset = blkoff(fs, length); 245 if (offset == 0) 246 ip->i_size = length; 247 else { 248 lbn = lblkno(fs, length); 249 #ifdef QUOTA 250 if (error = getinoquota(ip)) 251 return (error); 252 #endif 253 if (error = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 254 return (error); 255 ip->i_size = length; 256 size = blksize(fs); 257 (void)vnode_pager_uncache(vp); 258 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 259 allocbuf(bp, size); 260 LFS_UBWRITE(bp); 261 } 262 /* 263 * Modify sup->su_nbyte counters for each deleted block; keep track 264 * of number of blocks removed for ip->i_blocks. 265 */ 266 blocksreleased = 0; 267 num = 0; 268 lastseg = -1; 269 270 for (lbn = olastblock; lbn >= lastblock;) { 271 lfs_bmaparray(vp, lbn, &daddr, a, &depth); 272 if (lbn == olastblock) 273 for (i = NIADDR + 2; i--;) 274 a_end[i] = a[i]; 275 switch (depth) { 276 case 0: /* Direct block. */ 277 daddr = ip->i_db[lbn]; 278 SEGDEC; 279 ip->i_db[lbn] = 0; 280 --lbn; 281 break; 282 #ifdef DIAGNOSTIC 283 case 1: /* An indirect block. */ 284 panic("lfs_truncate: lfs_bmaparray returned depth 1"); 285 /* NOTREACHED */ 286 #endif 287 default: /* Chain of indirect blocks. */ 288 ap = a + --depth; 289 if (ap->in_off > 0 && lbn != lastblock) { 290 lbn -= ap->in_off < lbn - lastblock ? 291 ap->in_off : lbn - lastblock; 292 break; 293 } 294 for (; depth && (ap->in_off == 0 || lbn == lastblock); 295 --ap, --depth) { 296 /* 297 * XXX 298 * The indirect block may not yet exist, so 299 * bread will create one just so we can free 300 * it. 301 */ 302 if (bread(vp, 303 ap->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 304 panic("lfs_truncate: bread bno %d", 305 ap->in_lbn); 306 daddrp = bp->b_un.b_daddr + ap->in_off; 307 for (i = ap->in_off; 308 i++ <= a_end[depth].in_off;) { 309 daddr = *daddrp++; 310 SEGDEC; 311 } 312 a_end[depth].in_off=NINDIR(fs)-1; 313 if (ap->in_off > 0 && lbn == lastblock) { 314 bzero(bp->b_un.b_daddr + ap->in_off, 315 fs->lfs_bsize - 316 ap->in_off * sizeof(daddr_t)); 317 LFS_UBWRITE(bp); 318 } else 319 brelse (bp); 320 } 321 if (a[1].in_off == 0) { 322 off = a[0].in_off; 323 daddr = ip->i_ib[off]; 324 SEGDEC; 325 ip->i_ib[off] = 0; 326 } 327 if (lbn == lastblock) 328 --lbn; 329 else { 330 lbn -= NINDIR(fs); 331 if (lbn < lastblock) 332 lbn = lastblock; 333 } 334 } 335 } 336 if (lastseg != -1) 337 UPDATE_SEGUSE; 338 339 ip->i_blocks -= blocksreleased; 340 /* 341 * XXX 342 * Currently, we don't know when we allocate an indirect block, so 343 * ip->i_blocks isn't getting incremented appropriately. As a result, 344 * when we delete any indirect blocks, we get a bad number here. 345 */ 346 if (ip->i_blocks < 0) 347 ip->i_blocks = 0; 348 ip->i_flag |= ICHG|IUPD; 349 (void)vinvalbuf(vp, length > 0); 350 error = lfs_update(vp, &time, &time, MNT_WAIT); 351 return (0); 352 } 353