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.70 (Berkeley) 07/05/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 ifile *ifp; 136 struct inode *ip; 137 struct lfs *fs; 138 INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 139 SEGUSE *sup; 140 daddr_t daddr, lastblock, lbn, olastblock; 141 long off, blocksreleased; 142 int e1, e2, depth, lastseg, num, offset, seg, size; 143 144 #ifdef VERBOSE 145 printf("lfs_truncate\n"); 146 #endif 147 vnode_pager_setsize(vp, (u_long)length); 148 149 ip = VTOI(vp); 150 fs = ip->i_lfs; 151 152 /* If truncating the file to 0, update the version number. */ 153 if (length == 0) { 154 LFS_IENTRY(ifp, fs, ip->i_number, bp); 155 ++ifp->if_version; 156 LFS_UBWRITE(bp); 157 } 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, &time, &time, 1)); 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 LFS_UBWRITE(bp); 196 } 197 /* 198 * Modify sup->su_nbyte counters for each deleted block; keep track 199 * of number of blocks removed for ip->i_blocks. 200 */ 201 blocksreleased = 0; 202 num = 0; 203 lastseg = -1; 204 205 for (lbn = olastblock; lbn >= lastblock;) { 206 lfs_bmaparray(vp, lbn, &daddr, a, &depth); 207 if (lbn == olastblock) 208 for (i = NIADDR + 2; i--;) 209 a_end[i] = a[i]; 210 switch (depth) { 211 case 0: /* Direct block. */ 212 daddr = ip->i_db[lbn]; 213 SEGDEC; 214 ip->i_db[lbn] = 0; 215 --lbn; 216 break; 217 #ifdef DIAGNOSTIC 218 case 1: /* An indirect block. */ 219 panic("lfs_truncate: lfs_bmaparray returned depth 1"); 220 /* NOTREACHED */ 221 #endif 222 default: /* Chain of indirect blocks. */ 223 inp = a + --depth; 224 if (inp->in_off > 0 && lbn != lastblock) { 225 lbn -= inp->in_off < lbn - lastblock ? 226 inp->in_off : lbn - lastblock; 227 break; 228 } 229 for (; depth && (inp->in_off == 0 || lbn == lastblock); 230 --inp, --depth) { 231 /* 232 * XXX 233 * The indirect block may not yet exist, so 234 * bread will create one just so we can free 235 * it. 236 */ 237 if (bread(vp, 238 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 239 panic("lfs_truncate: bread bno %d", 240 inp->in_lbn); 241 daddrp = bp->b_un.b_daddr + inp->in_off; 242 for (i = inp->in_off; 243 i++ <= a_end[depth].in_off;) { 244 daddr = *daddrp++; 245 SEGDEC; 246 } 247 a_end[depth].in_off = NINDIR(fs) - 1; 248 if (inp->in_off == 0) 249 brelse (bp); 250 else { 251 bzero(bp->b_un.b_daddr + inp->in_off, 252 fs->lfs_bsize - 253 inp->in_off * sizeof(daddr_t)); 254 LFS_UBWRITE(bp); 255 } 256 } 257 if (depth == 0 && a[1].in_off == 0) { 258 off = a[0].in_off; 259 daddr = ip->i_ib[off]; 260 SEGDEC; 261 ip->i_ib[off] = 0; 262 } 263 if (lbn == lastblock || lbn <= NDADDR) 264 --lbn; 265 else { 266 lbn -= NINDIR(fs); 267 if (lbn < lastblock) 268 lbn = lastblock; 269 } 270 } 271 } 272 UPDATE_SEGUSE; 273 ip->i_blocks -= blocksreleased; 274 /* 275 * XXX 276 * Currently, we don't know when we allocate an indirect block, so 277 * ip->i_blocks isn't getting incremented appropriately. As a result, 278 * when we delete any indirect blocks, we get a bad number here. 279 */ 280 if (ip->i_blocks < 0) 281 ip->i_blocks = 0; 282 ip->i_flag |= ICHG|IUPD; 283 e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 284 e2 = VOP_UPDATE(vp, &time, &time, MNT_WAIT); 285 return (e1 ? e1 : e2 ? e2 : 0); 286 } 287