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.71 (Berkeley) 07/07/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_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 vnode_pager_setsize(vp, (u_long)length); 149 150 ip = VTOI(vp); 151 fs = ip->i_lfs; 152 153 /* If truncating the file to 0, update the version number. */ 154 if (length == 0) { 155 LFS_IENTRY(ifp, fs, ip->i_number, bp); 156 ++ifp->if_version; 157 LFS_UBWRITE(bp); 158 } 159 160 /* If length is larger than the file, just update the times. */ 161 tv = time; 162 if (ip->i_size <= length) { 163 ip->i_flag |= ICHG|IUPD; 164 return (VOP_UPDATE(vp, &tv, &tv, 1)); 165 } 166 167 /* 168 * Calculate index into inode's block list of last direct and indirect 169 * blocks (if any) which we want to keep. Lastblock is 0 when the 170 * file is truncated to 0. 171 */ 172 lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 173 olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 174 175 /* 176 * Update the size of the file. If the file is not being truncated to 177 * a block boundry, the contents of the partial block following the end 178 * of the file must be zero'ed in case it ever become accessable again 179 * because of subsequent file growth. 180 */ 181 offset = blkoff(fs, length); 182 if (offset == 0) 183 ip->i_size = length; 184 else { 185 lbn = lblkno(fs, length); 186 #ifdef QUOTA 187 if (e1 = getinoquota(ip)) 188 return (e1); 189 #endif 190 if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 191 return (e1); 192 ip->i_size = length; 193 size = blksize(fs); 194 (void)vnode_pager_uncache(vp); 195 bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 196 allocbuf(bp, size); 197 LFS_UBWRITE(bp); 198 } 199 /* 200 * Modify sup->su_nbyte counters for each deleted block; keep track 201 * of number of blocks removed for ip->i_blocks. 202 */ 203 blocksreleased = 0; 204 num = 0; 205 lastseg = -1; 206 207 for (lbn = olastblock; lbn >= lastblock;) { 208 lfs_bmaparray(vp, lbn, &daddr, a, &depth); 209 if (lbn == olastblock) 210 for (i = NIADDR + 2; i--;) 211 a_end[i] = a[i]; 212 switch (depth) { 213 case 0: /* Direct block. */ 214 daddr = ip->i_db[lbn]; 215 SEGDEC; 216 ip->i_db[lbn] = 0; 217 --lbn; 218 break; 219 #ifdef DIAGNOSTIC 220 case 1: /* An indirect block. */ 221 panic("lfs_truncate: lfs_bmaparray returned depth 1"); 222 /* NOTREACHED */ 223 #endif 224 default: /* Chain of indirect blocks. */ 225 inp = a + --depth; 226 if (inp->in_off > 0 && lbn != lastblock) { 227 lbn -= inp->in_off < lbn - lastblock ? 228 inp->in_off : lbn - lastblock; 229 break; 230 } 231 for (; depth && (inp->in_off == 0 || lbn == lastblock); 232 --inp, --depth) { 233 /* 234 * XXX 235 * The indirect block may not yet exist, so 236 * bread will create one just so we can free 237 * it. 238 */ 239 if (bread(vp, 240 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 241 panic("lfs_truncate: bread bno %d", 242 inp->in_lbn); 243 daddrp = bp->b_un.b_daddr + inp->in_off; 244 for (i = inp->in_off; 245 i++ <= a_end[depth].in_off;) { 246 daddr = *daddrp++; 247 SEGDEC; 248 } 249 a_end[depth].in_off = NINDIR(fs) - 1; 250 if (inp->in_off == 0) 251 brelse (bp); 252 else { 253 bzero(bp->b_un.b_daddr + inp->in_off, 254 fs->lfs_bsize - 255 inp->in_off * sizeof(daddr_t)); 256 LFS_UBWRITE(bp); 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 ip->i_blocks -= blocksreleased; 276 /* 277 * XXX 278 * Currently, we don't know when we allocate an indirect block, so 279 * ip->i_blocks isn't getting incremented appropriately. As a result, 280 * when we delete any indirect blocks, we get a bad number here. 281 */ 282 if (ip->i_blocks < 0) 283 ip->i_blocks = 0; 284 ip->i_flag |= ICHG|IUPD; 285 e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 286 e2 = VOP_UPDATE(vp, &tv, &tv, MNT_WAIT); 287 return (e1 ? e1 : e2 ? e2 : 0); 288 } 289