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