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.80 (Berkeley) 09/02/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 int 31 lfs_init() 32 { 33 return (ufs_init()); 34 } 35 36 /* Search a block for a specific dinode. */ 37 struct dinode * 38 lfs_ifind(fs, ino, dip) 39 struct lfs *fs; 40 ino_t ino; 41 register struct dinode *dip; 42 { 43 register int cnt; 44 register struct dinode *ldip; 45 46 for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip) 47 if (ldip->di_inum == ino) 48 return (ldip); 49 50 panic("lfs_ifind: dinode %u not found", ino); 51 /* NOTREACHED */ 52 } 53 54 int 55 lfs_update(ap) 56 struct vop_update_args /* { 57 struct vnode *a_vp; 58 struct timeval *a_ta; 59 struct timeval *a_tm; 60 int a_waitfor; 61 } */ *ap; 62 { 63 struct vnode *vp = ap->a_vp; 64 struct inode *ip; 65 66 if (vp->v_mount->mnt_flag & MNT_RDONLY) 67 return (0); 68 ip = VTOI(vp); 69 if ((ip->i_flag & (IUPD | IACC | ICHG | IMOD)) == 0) 70 return (0); 71 if (ip->i_flag & IACC) 72 ip->i_atime.ts_sec = ap->a_ta->tv_sec; 73 if (ip->i_flag & IUPD) { 74 ip->i_mtime.ts_sec = ap->a_tm->tv_sec; 75 (ip)->i_modrev++; 76 } 77 if (ip->i_flag & ICHG) 78 ip->i_ctime.ts_sec = time.tv_sec; 79 ip->i_flag &= ~(IUPD|IACC|ICHG); 80 81 if (!(ip->i_flag & IMOD)) 82 ++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes); 83 ip->i_flag |= IMOD; 84 85 /* If sync, push back the vnode and any dirty blocks it may have. */ 86 return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0); 87 } 88 89 /* Update segment usage information when removing a block. */ 90 #define UPDATE_SEGUSE \ 91 if (lastseg != -1) { \ 92 LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \ 93 if ((num << fs->lfs_bshift) > sup->su_nbytes) \ 94 panic("lfs_truncate: negative bytes in segment %d\n", \ 95 lastseg); \ 96 sup->su_nbytes -= num << fs->lfs_bshift; \ 97 e1 = VOP_BWRITE(sup_bp); \ 98 blocksreleased += num; \ 99 } 100 101 #define SEGDEC { \ 102 if (daddr != 0) { \ 103 if (lastseg != (seg = datosn(fs, daddr))) { \ 104 UPDATE_SEGUSE; \ 105 num = 1; \ 106 lastseg = seg; \ 107 } else \ 108 ++num; \ 109 } \ 110 } 111 112 /* 113 * Truncate the inode ip to at most length size. Update segment usage 114 * table information. 115 */ 116 /* ARGSUSED */ 117 int 118 lfs_truncate(ap) 119 struct vop_truncate_args /* { 120 struct vnode *a_vp; 121 off_t a_length; 122 int a_flags; 123 struct ucred *a_cred; 124 struct proc *a_p; 125 } */ *ap; 126 { 127 register INDIR *inp; 128 register int i; 129 register daddr_t *daddrp; 130 register struct vnode *vp = ap->a_vp; 131 off_t length = ap->a_length; 132 struct buf *bp, *sup_bp; 133 struct timeval tv; 134 struct ifile *ifp; 135 struct inode *ip; 136 struct lfs *fs; 137 INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 138 SEGUSE *sup; 139 daddr_t daddr, lastblock, lbn, olastblock; 140 long off, a_released, blocksreleased, i_released; 141 int e1, e2, depth, lastseg, num, offset, seg, size; 142 143 ip = VTOI(vp); 144 tv = time; 145 if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) { 146 #ifdef DIAGNOSTIC 147 if (length != 0) 148 panic("lfs_truncate: partial truncate of symlink"); 149 #endif 150 bzero((char *)&ip->i_shortlink, (u_int)ip->i_size); 151 ip->i_size = 0; 152 ip->i_flag |= ICHG|IUPD; 153 return (VOP_UPDATE(vp, &tv, &tv, 0)); 154 } 155 vnode_pager_setsize(vp, (u_long)length); 156 157 fs = ip->i_lfs; 158 159 /* If length is larger than the file, just update the times. */ 160 if (ip->i_size <= length) { 161 ip->i_flag |= ICHG|IUPD; 162 return (VOP_UPDATE(vp, &tv, &tv, 0)); 163 } 164 165 /* 166 * Calculate index into inode's block list of last direct and indirect 167 * blocks (if any) which we want to keep. Lastblock is 0 when the 168 * file is truncated to 0. 169 */ 170 lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 171 olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 172 173 /* 174 * Update the size of the file. If the file is not being truncated to 175 * a block boundry, the contents of the partial block following the end 176 * of the file must be zero'ed in case it ever become accessable again 177 * because of subsequent file growth. 178 */ 179 offset = blkoff(fs, length); 180 if (offset == 0) 181 ip->i_size = length; 182 else { 183 lbn = lblkno(fs, length); 184 #ifdef QUOTA 185 if (e1 = getinoquota(ip)) 186 return (e1); 187 #endif 188 if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 189 return (e1); 190 ip->i_size = length; 191 size = blksize(fs); 192 (void)vnode_pager_uncache(vp); 193 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 194 allocbuf(bp, size); 195 if (e1 = VOP_BWRITE(bp)) 196 return (e1); 197 } 198 /* 199 * Modify sup->su_nbyte counters for each deleted block; keep track 200 * of number of blocks removed for ip->i_blocks. 201 */ 202 blocksreleased = 0; 203 num = 0; 204 lastseg = -1; 205 206 for (lbn = olastblock; lbn >= lastblock;) { 207 lfs_bmaparray(vp, lbn, &daddr, a, &depth); 208 if (lbn == olastblock) 209 for (i = NIADDR + 2; i--;) 210 a_end[i] = a[i]; 211 switch (depth) { 212 case 0: /* Direct block. */ 213 daddr = ip->i_db[lbn]; 214 SEGDEC; 215 ip->i_db[lbn] = 0; 216 --lbn; 217 break; 218 #ifdef DIAGNOSTIC 219 case 1: /* An indirect block. */ 220 panic("lfs_truncate: lfs_bmaparray returned depth 1"); 221 /* NOTREACHED */ 222 #endif 223 default: /* Chain of indirect blocks. */ 224 inp = a + --depth; 225 if (inp->in_off > 0 && lbn != lastblock) { 226 lbn -= inp->in_off < lbn - lastblock ? 227 inp->in_off : lbn - lastblock; 228 break; 229 } 230 for (; depth && (inp->in_off == 0 || lbn == lastblock); 231 --inp, --depth) { 232 if (bread(vp, 233 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 234 panic("lfs_truncate: bread bno %d", 235 inp->in_lbn); 236 daddrp = bp->b_un.b_daddr + inp->in_off; 237 for (i = inp->in_off; 238 i++ <= a_end[depth].in_off;) { 239 daddr = *daddrp++; 240 SEGDEC; 241 } 242 a_end[depth].in_off = NINDIR(fs) - 1; 243 if (inp->in_off == 0) 244 brelse (bp); 245 else { 246 bzero(bp->b_un.b_daddr + inp->in_off, 247 fs->lfs_bsize - 248 inp->in_off * sizeof(daddr_t)); 249 if (e1 = VOP_BWRITE(bp)) 250 return (e1); 251 } 252 } 253 if (depth == 0 && a[1].in_off == 0) { 254 off = a[0].in_off; 255 daddr = ip->i_ib[off]; 256 SEGDEC; 257 ip->i_ib[off] = 0; 258 } 259 if (lbn == lastblock || lbn <= NDADDR) 260 --lbn; 261 else { 262 lbn -= NINDIR(fs); 263 if (lbn < lastblock) 264 lbn = lastblock; 265 } 266 } 267 } 268 UPDATE_SEGUSE; 269 270 /* If truncating the file to 0, update the version number. */ 271 if (length == 0) { 272 LFS_IENTRY(ifp, fs, ip->i_number, bp); 273 ++ifp->if_version; 274 (void) VOP_BWRITE(bp); 275 } 276 277 #ifdef DIAGNOSTIC 278 if (ip->i_blocks < fsbtodb(fs, blocksreleased)) 279 panic("lfs_truncate: block count < 0"); 280 #endif 281 ip->i_blocks -= fsbtodb(fs, blocksreleased); 282 fs->lfs_bfree += fsbtodb(fs, blocksreleased); 283 ip->i_flag |= ICHG|IUPD; 284 /* 285 * Traverse dirty block list counting number of dirty buffers 286 * that are being deleted out of the cache, so that the lfs_avail 287 * field can be updated. 288 */ 289 a_released = 0; 290 i_released = 0; 291 for (bp = vp->v_dirtyblkhd; bp; bp = bp->b_blockf) 292 if (bp->b_flags & B_LOCKED) { 293 ++a_released; 294 /* 295 * XXX 296 * When buffers are created in the cache, their block 297 * number is set equal to their logical block number. 298 * If that is still true, we are assuming that the 299 * blocks are new (not yet on disk) and weren't 300 * counted above. However, there is a slight chance 301 * that a block's disk address is equal to its logical 302 * block number in which case, we'll get an overcounting 303 * here. 304 */ 305 if (bp->b_blkno == bp->b_lblkno) 306 ++i_released; 307 } 308 blocksreleased = fsbtodb(fs, i_released); 309 #ifdef DIAGNOSTIC 310 if (blocksreleased > ip->i_blocks) { 311 printf("lfs_inode: Warning! %s\n", 312 "more blocks released from inode than are in inode"); 313 blocksreleased = ip->i_blocks; 314 } 315 #endif 316 fs->lfs_bfree += blocksreleased; 317 ip->i_blocks -= blocksreleased; 318 #ifdef DIAGNOSTIC 319 if (length == 0 && ip->i_blocks != 0) 320 printf("lfs_inode: Warning! %s%d%s\n", 321 "Truncation to zero, but ", ip->i_blocks, 322 " blocks left on inode"); 323 #endif 324 fs->lfs_avail -= fsbtodb(fs, a_released); 325 e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 326 e2 = VOP_UPDATE(vp, &tv, &tv, 0); 327 return (e1 ? e1 : e2 ? e2 : 0); 328 } 329