1 /* $NetBSD: lfs_inode.c,v 1.2 1994/06/29 06:46:57 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)lfs_inode.c 8.5 (Berkeley) 12/30/93 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/mount.h> 41 #include <sys/proc.h> 42 #include <sys/file.h> 43 #include <sys/buf.h> 44 #include <sys/vnode.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 48 #include <vm/vm.h> 49 50 #include <ufs/ufs/quota.h> 51 #include <ufs/ufs/inode.h> 52 #include <ufs/ufs/ufsmount.h> 53 #include <ufs/ufs/ufs_extern.h> 54 55 #include <ufs/lfs/lfs.h> 56 #include <ufs/lfs/lfs_extern.h> 57 58 int 59 lfs_init() 60 { 61 return (ufs_init()); 62 } 63 64 /* Search a block for a specific dinode. */ 65 struct dinode * 66 lfs_ifind(fs, ino, dip) 67 struct lfs *fs; 68 ino_t ino; 69 register struct dinode *dip; 70 { 71 register int cnt; 72 register struct dinode *ldip; 73 74 for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip) 75 if (ldip->di_inumber == ino) 76 return (ldip); 77 78 panic("lfs_ifind: dinode %u not found", ino); 79 /* NOTREACHED */ 80 } 81 82 int 83 lfs_update(ap) 84 struct vop_update_args /* { 85 struct vnode *a_vp; 86 struct timeval *a_access; 87 struct timeval *a_modify; 88 int a_waitfor; 89 } */ *ap; 90 { 91 struct vnode *vp = ap->a_vp; 92 struct inode *ip; 93 94 if (vp->v_mount->mnt_flag & MNT_RDONLY) 95 return (0); 96 ip = VTOI(vp); 97 if ((ip->i_flag & 98 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0) 99 return (0); 100 if (ip->i_flag & IN_ACCESS) 101 ip->i_atime.ts_sec = ap->a_access->tv_sec; 102 if (ip->i_flag & IN_UPDATE) { 103 ip->i_mtime.ts_sec = ap->a_modify->tv_sec; 104 (ip)->i_modrev++; 105 } 106 if (ip->i_flag & IN_CHANGE) 107 ip->i_ctime.ts_sec = time.tv_sec; 108 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE); 109 110 if (!(ip->i_flag & IN_MODIFIED)) 111 ++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes); 112 ip->i_flag |= IN_MODIFIED; 113 114 /* If sync, push back the vnode and any dirty blocks it may have. */ 115 return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0); 116 } 117 118 /* Update segment usage information when removing a block. */ 119 #define UPDATE_SEGUSE \ 120 if (lastseg != -1) { \ 121 LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \ 122 if ((num << fs->lfs_bshift) > sup->su_nbytes) \ 123 panic("lfs_truncate: negative bytes in segment %d\n", \ 124 lastseg); \ 125 sup->su_nbytes -= num << fs->lfs_bshift; \ 126 e1 = VOP_BWRITE(sup_bp); \ 127 blocksreleased += num; \ 128 } 129 130 #define SEGDEC { \ 131 if (daddr != 0) { \ 132 if (lastseg != (seg = datosn(fs, daddr))) { \ 133 UPDATE_SEGUSE; \ 134 num = 1; \ 135 lastseg = seg; \ 136 } else \ 137 ++num; \ 138 } \ 139 } 140 141 /* 142 * Truncate the inode ip to at most length size. Update segment usage 143 * table information. 144 */ 145 /* ARGSUSED */ 146 int 147 lfs_truncate(ap) 148 struct vop_truncate_args /* { 149 struct vnode *a_vp; 150 off_t a_length; 151 int a_flags; 152 struct ucred *a_cred; 153 struct proc *a_p; 154 } */ *ap; 155 { 156 register struct indir *inp; 157 register int i; 158 register daddr_t *daddrp; 159 register struct vnode *vp = ap->a_vp; 160 off_t length = ap->a_length; 161 struct buf *bp, *sup_bp; 162 struct timeval tv; 163 struct ifile *ifp; 164 struct inode *ip; 165 struct lfs *fs; 166 struct indir a[NIADDR + 2], a_end[NIADDR + 2]; 167 SEGUSE *sup; 168 daddr_t daddr, lastblock, lbn, olastblock; 169 long off, a_released, blocksreleased, i_released; 170 int e1, e2, depth, lastseg, num, offset, seg, size; 171 172 ip = VTOI(vp); 173 tv = time; 174 if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) { 175 #ifdef DIAGNOSTIC 176 if (length != 0) 177 panic("lfs_truncate: partial truncate of symlink"); 178 #endif 179 bzero((char *)&ip->i_shortlink, (u_int)ip->i_size); 180 ip->i_size = 0; 181 ip->i_flag |= IN_CHANGE | IN_UPDATE; 182 return (VOP_UPDATE(vp, &tv, &tv, 0)); 183 } 184 vnode_pager_setsize(vp, (u_long)length); 185 186 fs = ip->i_lfs; 187 188 /* If length is larger than the file, just update the times. */ 189 if (ip->i_size <= length) { 190 ip->i_flag |= IN_CHANGE | IN_UPDATE; 191 return (VOP_UPDATE(vp, &tv, &tv, 0)); 192 } 193 194 /* 195 * Calculate index into inode's block list of last direct and indirect 196 * blocks (if any) which we want to keep. Lastblock is 0 when the 197 * file is truncated to 0. 198 */ 199 lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 200 olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 201 202 /* 203 * Update the size of the file. If the file is not being truncated to 204 * a block boundry, the contents of the partial block following the end 205 * of the file must be zero'ed in case it ever become accessable again 206 * because of subsequent file growth. 207 */ 208 offset = blkoff(fs, length); 209 if (offset == 0) 210 ip->i_size = length; 211 else { 212 lbn = lblkno(fs, length); 213 #ifdef QUOTA 214 if (e1 = getinoquota(ip)) 215 return (e1); 216 #endif 217 if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 218 return (e1); 219 ip->i_size = length; 220 size = blksize(fs); 221 (void)vnode_pager_uncache(vp); 222 bzero((char *)bp->b_data + offset, (u_int)(size - offset)); 223 allocbuf(bp, size); 224 if (e1 = VOP_BWRITE(bp)) 225 return (e1); 226 } 227 /* 228 * Modify sup->su_nbyte counters for each deleted block; keep track 229 * of number of blocks removed for ip->i_blocks. 230 */ 231 blocksreleased = 0; 232 num = 0; 233 lastseg = -1; 234 235 for (lbn = olastblock; lbn >= lastblock;) { 236 /* XXX use run length from bmap array to make this faster */ 237 ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL); 238 if (lbn == olastblock) 239 for (i = NIADDR + 2; i--;) 240 a_end[i] = a[i]; 241 switch (depth) { 242 case 0: /* Direct block. */ 243 daddr = ip->i_db[lbn]; 244 SEGDEC; 245 ip->i_db[lbn] = 0; 246 --lbn; 247 break; 248 #ifdef DIAGNOSTIC 249 case 1: /* An indirect block. */ 250 panic("lfs_truncate: ufs_bmaparray returned depth 1"); 251 /* NOTREACHED */ 252 #endif 253 default: /* Chain of indirect blocks. */ 254 inp = a + --depth; 255 if (inp->in_off > 0 && lbn != lastblock) { 256 lbn -= inp->in_off < lbn - lastblock ? 257 inp->in_off : lbn - lastblock; 258 break; 259 } 260 for (; depth && (inp->in_off == 0 || lbn == lastblock); 261 --inp, --depth) { 262 if (bread(vp, 263 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 264 panic("lfs_truncate: bread bno %d", 265 inp->in_lbn); 266 daddrp = (daddr_t *)bp->b_data + inp->in_off; 267 for (i = inp->in_off; 268 i++ <= a_end[depth].in_off;) { 269 daddr = *daddrp++; 270 SEGDEC; 271 } 272 a_end[depth].in_off = NINDIR(fs) - 1; 273 if (inp->in_off == 0) 274 brelse (bp); 275 else { 276 bzero((daddr_t *)bp->b_data + 277 inp->in_off, fs->lfs_bsize - 278 inp->in_off * sizeof(daddr_t)); 279 if (e1 = VOP_BWRITE(bp)) 280 return (e1); 281 } 282 } 283 if (depth == 0 && a[1].in_off == 0) { 284 off = a[0].in_off; 285 daddr = ip->i_ib[off]; 286 SEGDEC; 287 ip->i_ib[off] = 0; 288 } 289 if (lbn == lastblock || lbn <= NDADDR) 290 --lbn; 291 else { 292 lbn -= NINDIR(fs); 293 if (lbn < lastblock) 294 lbn = lastblock; 295 } 296 } 297 } 298 UPDATE_SEGUSE; 299 300 /* If truncating the file to 0, update the version number. */ 301 if (length == 0) { 302 LFS_IENTRY(ifp, fs, ip->i_number, bp); 303 ++ifp->if_version; 304 (void) VOP_BWRITE(bp); 305 } 306 307 #ifdef DIAGNOSTIC 308 if (ip->i_blocks < fsbtodb(fs, blocksreleased)) { 309 printf("lfs_truncate: block count < 0\n"); 310 blocksreleased = ip->i_blocks; 311 } 312 #endif 313 ip->i_blocks -= fsbtodb(fs, blocksreleased); 314 fs->lfs_bfree += fsbtodb(fs, blocksreleased); 315 ip->i_flag |= IN_CHANGE | IN_UPDATE; 316 /* 317 * Traverse dirty block list counting number of dirty buffers 318 * that are being deleted out of the cache, so that the lfs_avail 319 * field can be updated. 320 */ 321 a_released = 0; 322 i_released = 0; 323 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) 324 if (bp->b_flags & B_LOCKED) { 325 ++a_released; 326 /* 327 * XXX 328 * When buffers are created in the cache, their block 329 * number is set equal to their logical block number. 330 * If that is still true, we are assuming that the 331 * blocks are new (not yet on disk) and weren't 332 * counted above. However, there is a slight chance 333 * that a block's disk address is equal to its logical 334 * block number in which case, we'll get an overcounting 335 * here. 336 */ 337 if (bp->b_blkno == bp->b_lblkno) 338 ++i_released; 339 } 340 blocksreleased = fsbtodb(fs, i_released); 341 #ifdef DIAGNOSTIC 342 if (blocksreleased > ip->i_blocks) { 343 printf("lfs_inode: Warning! %s\n", 344 "more blocks released from inode than are in inode"); 345 blocksreleased = ip->i_blocks; 346 } 347 #endif 348 fs->lfs_bfree += blocksreleased; 349 ip->i_blocks -= blocksreleased; 350 #ifdef DIAGNOSTIC 351 if (length == 0 && ip->i_blocks != 0) 352 printf("lfs_inode: Warning! %s%d%s\n", 353 "Truncation to zero, but ", ip->i_blocks, 354 " blocks left on inode"); 355 #endif 356 fs->lfs_avail += fsbtodb(fs, a_released); 357 e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p, 358 0, 0); 359 e2 = VOP_UPDATE(vp, &tv, &tv, 0); 360 return (e1 ? e1 : e2 ? e2 : 0); 361 } 362