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.79 (Berkeley) 08/25/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, blocksreleased; 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 /* 233 * XXX 234 * The indirect block may not yet exist, so 235 * bread will create one just so we can free 236 * it. 237 */ 238 if (bread(vp, 239 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 240 panic("lfs_truncate: bread bno %d", 241 inp->in_lbn); 242 daddrp = bp->b_un.b_daddr + inp->in_off; 243 for (i = inp->in_off; 244 i++ <= a_end[depth].in_off;) { 245 daddr = *daddrp++; 246 SEGDEC; 247 } 248 a_end[depth].in_off = NINDIR(fs) - 1; 249 if (inp->in_off == 0) 250 brelse (bp); 251 else { 252 bzero(bp->b_un.b_daddr + inp->in_off, 253 fs->lfs_bsize - 254 inp->in_off * sizeof(daddr_t)); 255 if (e1 = VOP_BWRITE(bp)) 256 return (e1); 257 } 258 } 259 if (depth == 0 && a[1].in_off == 0) { 260 off = a[0].in_off; 261 daddr = ip->i_ib[off]; 262 SEGDEC; 263 ip->i_ib[off] = 0; 264 } 265 if (lbn == lastblock || lbn <= NDADDR) 266 --lbn; 267 else { 268 lbn -= NINDIR(fs); 269 if (lbn < lastblock) 270 lbn = lastblock; 271 } 272 } 273 } 274 UPDATE_SEGUSE; 275 276 /* If truncating the file to 0, update the version number. */ 277 if (length == 0) { 278 LFS_IENTRY(ifp, fs, ip->i_number, bp); 279 ++ifp->if_version; 280 (void) VOP_BWRITE(bp); 281 } 282 283 #ifdef DIAGNOSTIC 284 if (ip->i_blocks < fsbtodb(fs, blocksreleased)) 285 panic("lfs_truncate: block count < 0"); 286 #endif 287 ip->i_blocks -= fsbtodb(fs, blocksreleased); 288 fs->lfs_bfree += fsbtodb(fs, blocksreleased); 289 ip->i_flag |= ICHG|IUPD; 290 /* 291 * Traverse dirty block list counting number of dirty buffers 292 * that are being deleted out of the cache, so that the lfs_avail 293 * field can be updated. 294 */ 295 for (bp = vp->v_dirtyblkhd; bp; bp = bp->b_blockf) 296 if (bp->b_flags & B_LOCKED) 297 fs->lfs_avail -= fsbtodb(fs, 1); 298 e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 299 e2 = VOP_UPDATE(vp, &tv, &tv, 0); 300 return (e1 ? e1 : e2 ? e2 : 0); 301 } 302