123399Smckusick /* 251498Sbostic * Copyright (c) 1986, 1989, 1991 Regents of the University of California. 337736Smckusick * All rights reserved. 423399Smckusick * 544537Sbostic * %sccs.include.redist.c% 637736Smckusick * 7*55938Sbostic * @(#)lfs_inode.c 7.78 (Berkeley) 08/21/92 823399Smckusick */ 924Sbill 1051484Sbostic #include <sys/param.h> 1151484Sbostic #include <sys/systm.h> 1251484Sbostic #include <sys/mount.h> 1351484Sbostic #include <sys/proc.h> 1451484Sbostic #include <sys/file.h> 1551484Sbostic #include <sys/buf.h> 1651484Sbostic #include <sys/vnode.h> 1751484Sbostic #include <sys/kernel.h> 1851484Sbostic #include <sys/malloc.h> 1924Sbill 2053477Smckusick #include <vm/vm.h> 2153477Smckusick 2251498Sbostic #include <ufs/ufs/quota.h> 2351498Sbostic #include <ufs/ufs/inode.h> 2451498Sbostic #include <ufs/ufs/ufsmount.h> 2551498Sbostic #include <ufs/ufs/ufs_extern.h> 2647571Skarels 2751498Sbostic #include <ufs/lfs/lfs.h> 2851498Sbostic #include <ufs/lfs/lfs_extern.h> 2924Sbill 3051346Sbostic int 3151155Sbostic lfs_init() 3224Sbill { 3351484Sbostic return (ufs_init()); 3424Sbill } 3524Sbill 3652834Sbostic /* Search a block for a specific dinode. */ 3754694Sbostic struct dinode * 3852834Sbostic lfs_ifind(fs, ino, dip) 3952834Sbostic struct lfs *fs; 4052834Sbostic ino_t ino; 4152834Sbostic register struct dinode *dip; 4252834Sbostic { 4352834Sbostic register int cnt; 4454264Sbostic register struct dinode *ldip; 4552834Sbostic 4654264Sbostic for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip) 4754264Sbostic if (ldip->di_inum == ino) 4854264Sbostic return (ldip); 4952834Sbostic 5052834Sbostic panic("lfs_ifind: dinode %u not found", ino); 5152834Sbostic /* NOTREACHED */ 5252834Sbostic } 5352834Sbostic 5451346Sbostic int 5554694Sbostic lfs_update(ap) 5654694Sbostic struct vop_update_args /* { 5754694Sbostic struct vnode *a_vp; 5854694Sbostic struct timeval *a_ta; 5954694Sbostic struct timeval *a_tm; 6054694Sbostic int a_waitfor; 6154694Sbostic } */ *ap; 627118Smckusick { 6353867Sheideman struct vnode *vp = ap->a_vp; 6451562Smckusick struct inode *ip; 6551562Smckusick 6653867Sheideman if (vp->v_mount->mnt_flag & MNT_RDONLY) 6751562Smckusick return (0); 6853867Sheideman ip = VTOI(vp); 69*55938Sbostic if ((ip->i_flag & (IUPD | IACC | ICHG | IMOD)) == 0) 7051562Smckusick return (0); 71*55938Sbostic if (ip->i_flag & IACC) 7254103Smckusick ip->i_atime.ts_sec = ap->a_ta->tv_sec; 73*55938Sbostic if (ip->i_flag & IUPD) { 7454103Smckusick ip->i_mtime.ts_sec = ap->a_tm->tv_sec; 7554128Smckusick (ip)->i_modrev++; 7652018Smckusick } 77*55938Sbostic if (ip->i_flag & ICHG) 7854103Smckusick ip->i_ctime.ts_sec = time.tv_sec; 79*55938Sbostic ip->i_flag &= ~(IUPD|IACC|ICHG); 8051562Smckusick 81*55938Sbostic if (!(ip->i_flag & IMOD)) 82*55938Sbostic ++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes); 83*55938Sbostic ip->i_flag |= IMOD; 84*55938Sbostic 85*55938Sbostic /* If sync, push back the vnode and any dirty blocks it may have. */ 8655549Sbostic return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0); 8724Sbill } 8824Sbill 8952222Sbostic /* Update segment usage information when removing a block. */ 9052225Sbostic #define UPDATE_SEGUSE \ 9152225Sbostic if (lastseg != -1) { \ 9252225Sbostic LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \ 9355786Sbostic if ((num << fs->lfs_bshift) > sup->su_nbytes) \ 9455786Sbostic panic("lfs_truncate: negative bytes in segment %d\n", \ 9555786Sbostic lastseg); \ 9652681Sstaelin sup->su_nbytes -= num << fs->lfs_bshift; \ 97*55938Sbostic e1 = VOP_BWRITE(sup_bp); \ 9852225Sbostic blocksreleased += num; \ 9952225Sbostic } 10052222Sbostic 10152222Sbostic #define SEGDEC { \ 10252222Sbostic if (daddr != UNASSIGNED) { \ 10352222Sbostic if (lastseg != (seg = datosn(fs, daddr))) { \ 10452222Sbostic UPDATE_SEGUSE; \ 10552222Sbostic num = 1; \ 10652222Sbostic lastseg = seg; \ 10752222Sbostic } else \ 10852222Sbostic ++num; \ 10952222Sbostic } \ 11052222Sbostic } 11152222Sbostic 11224Sbill /* 11352222Sbostic * Truncate the inode ip to at most length size. Update segment usage 11452222Sbostic * table information. 11524Sbill */ 11651484Sbostic /* ARGSUSED */ 11751484Sbostic int 11854694Sbostic lfs_truncate(ap) 11954694Sbostic struct vop_truncate_args /* { 12054694Sbostic struct vnode *a_vp; 12154694Sbostic off_t a_length; 12254694Sbostic int a_flags; 12354694Sbostic struct ucred *a_cred; 12454694Sbostic struct proc *a_p; 12554694Sbostic } */ *ap; 12624Sbill { 12753505Sheideman register INDIR *inp; 12852222Sbostic register int i; 12952222Sbostic register daddr_t *daddrp; 13054694Sbostic register struct vnode *vp = ap->a_vp; 13154694Sbostic off_t length = ap->a_length; 13252225Sbostic struct buf *bp, *sup_bp; 13354765Smckusick struct timeval tv; 13452327Sbostic struct ifile *ifp; 13552222Sbostic struct inode *ip; 13652222Sbostic struct lfs *fs; 13752222Sbostic INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 13852222Sbostic SEGUSE *sup; 13952222Sbostic daddr_t daddr, lastblock, lbn, olastblock; 14053233Smckusick long off, blocksreleased; 14154694Sbostic int e1, e2, depth, lastseg, num, offset, seg, size; 1429165Ssam 14355456Sbostic ip = VTOI(vp); 14455456Sbostic tv = time; 14555456Sbostic if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) { 14655456Sbostic #ifdef DIAGNOSTIC 14755456Sbostic if (length != 0) 14855456Sbostic panic("lfs_truncate: partial truncate of symlink"); 14955456Sbostic #endif 15055456Sbostic bzero((char *)&ip->i_shortlink, (u_int)ip->i_size); 15155456Sbostic ip->i_size = 0; 15255456Sbostic ip->i_flag |= ICHG|IUPD; 15355549Sbostic return (VOP_UPDATE(vp, &tv, &tv, 0)); 15455456Sbostic } 15554694Sbostic vnode_pager_setsize(vp, (u_long)length); 15652327Sbostic 15752327Sbostic fs = ip->i_lfs; 15852327Sbostic 15954694Sbostic /* If length is larger than the file, just update the times. */ 16054694Sbostic if (ip->i_size <= length) { 16152222Sbostic ip->i_flag |= ICHG|IUPD; 16255549Sbostic return (VOP_UPDATE(vp, &tv, &tv, 0)); 16313000Ssam } 16452327Sbostic 16552222Sbostic /* 16652222Sbostic * Calculate index into inode's block list of last direct and indirect 16752222Sbostic * blocks (if any) which we want to keep. Lastblock is 0 when the 16852222Sbostic * file is truncated to 0. 16952222Sbostic */ 17054694Sbostic lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 17152222Sbostic olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 17251484Sbostic 1731203Sbill /* 17451484Sbostic * Update the size of the file. If the file is not being truncated to 17551484Sbostic * a block boundry, the contents of the partial block following the end 17651484Sbostic * of the file must be zero'ed in case it ever become accessable again 17751484Sbostic * because of subsequent file growth. 1781203Sbill */ 17954694Sbostic offset = blkoff(fs, length); 18051484Sbostic if (offset == 0) 18154694Sbostic ip->i_size = length; 18251484Sbostic else { 18354694Sbostic lbn = lblkno(fs, length); 18441313Smckusick #ifdef QUOTA 18554694Sbostic if (e1 = getinoquota(ip)) 18654694Sbostic return (e1); 18751183Sbostic #endif 18854694Sbostic if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 18954694Sbostic return (e1); 19054694Sbostic ip->i_size = length; 19151857Sbostic size = blksize(fs); 19254694Sbostic (void)vnode_pager_uncache(vp); 19326272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 19445112Smckusick allocbuf(bp, size); 195*55938Sbostic if (e1 = VOP_BWRITE(bp)) 196*55938Sbostic return (e1); 19717942Smckusick } 19852078Sbostic /* 19952222Sbostic * Modify sup->su_nbyte counters for each deleted block; keep track 20052222Sbostic * of number of blocks removed for ip->i_blocks. 20152222Sbostic */ 20252222Sbostic blocksreleased = 0; 20352222Sbostic num = 0; 20452222Sbostic lastseg = -1; 20552222Sbostic 20652222Sbostic for (lbn = olastblock; lbn >= lastblock;) { 20754694Sbostic lfs_bmaparray(vp, lbn, &daddr, a, &depth); 20852222Sbostic if (lbn == olastblock) 20952222Sbostic for (i = NIADDR + 2; i--;) 21052222Sbostic a_end[i] = a[i]; 21152222Sbostic switch (depth) { 21252222Sbostic case 0: /* Direct block. */ 21352222Sbostic daddr = ip->i_db[lbn]; 21452222Sbostic SEGDEC; 21552222Sbostic ip->i_db[lbn] = 0; 21652222Sbostic --lbn; 21752222Sbostic break; 21852222Sbostic #ifdef DIAGNOSTIC 21952222Sbostic case 1: /* An indirect block. */ 22052222Sbostic panic("lfs_truncate: lfs_bmaparray returned depth 1"); 22152222Sbostic /* NOTREACHED */ 22252222Sbostic #endif 22352222Sbostic default: /* Chain of indirect blocks. */ 22453505Sheideman inp = a + --depth; 22553505Sheideman if (inp->in_off > 0 && lbn != lastblock) { 22653505Sheideman lbn -= inp->in_off < lbn - lastblock ? 22753505Sheideman inp->in_off : lbn - lastblock; 22852222Sbostic break; 22952222Sbostic } 23053505Sheideman for (; depth && (inp->in_off == 0 || lbn == lastblock); 23153505Sheideman --inp, --depth) { 23252222Sbostic /* 23352222Sbostic * XXX 23452222Sbostic * The indirect block may not yet exist, so 23552222Sbostic * bread will create one just so we can free 23652222Sbostic * it. 23752222Sbostic */ 23854694Sbostic if (bread(vp, 23953505Sheideman inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 24052222Sbostic panic("lfs_truncate: bread bno %d", 24153505Sheideman inp->in_lbn); 24253505Sheideman daddrp = bp->b_un.b_daddr + inp->in_off; 24353505Sheideman for (i = inp->in_off; 24452222Sbostic i++ <= a_end[depth].in_off;) { 24552222Sbostic daddr = *daddrp++; 24652222Sbostic SEGDEC; 24752222Sbostic } 24853144Sstaelin a_end[depth].in_off = NINDIR(fs) - 1; 24953505Sheideman if (inp->in_off == 0) 25053144Sstaelin brelse (bp); 25153144Sstaelin else { 25253505Sheideman bzero(bp->b_un.b_daddr + inp->in_off, 25352222Sbostic fs->lfs_bsize - 25453505Sheideman inp->in_off * sizeof(daddr_t)); 255*55938Sbostic if (e1 = VOP_BWRITE(bp)) 256*55938Sbostic return (e1); 25753144Sstaelin } 25852222Sbostic } 25953144Sstaelin if (depth == 0 && a[1].in_off == 0) { 26052222Sbostic off = a[0].in_off; 26152222Sbostic daddr = ip->i_ib[off]; 26252222Sbostic SEGDEC; 26352222Sbostic ip->i_ib[off] = 0; 26452222Sbostic } 26552681Sstaelin if (lbn == lastblock || lbn <= NDADDR) 26652222Sbostic --lbn; 26752222Sbostic else { 26852222Sbostic lbn -= NINDIR(fs); 26952222Sbostic if (lbn < lastblock) 27052222Sbostic lbn = lastblock; 27152222Sbostic } 27252222Sbostic } 27352222Sbostic } 27452225Sbostic UPDATE_SEGUSE; 27555786Sbostic 27655786Sbostic /* If truncating the file to 0, update the version number. */ 27755786Sbostic if (length == 0) { 27855786Sbostic LFS_IENTRY(ifp, fs, ip->i_number, bp); 27955786Sbostic ++ifp->if_version; 280*55938Sbostic (void) VOP_BWRITE(bp); 28155786Sbostic } 28255786Sbostic 28355459Sbostic #ifdef DIAGNOSTIC 284*55938Sbostic if (ip->i_blocks < fsbtodb(fs, blocksreleased)) 28555459Sbostic panic("lfs_truncate: block count < 0"); 28655459Sbostic #endif 287*55938Sbostic ip->i_blocks -= fsbtodb(fs, blocksreleased); 288*55938Sbostic fs->lfs_bfree += fsbtodb(fs, blocksreleased); 28952222Sbostic ip->i_flag |= ICHG|IUPD; 290*55938Sbostic /* 291*55938Sbostic * Traverse dirty block list counting number of dirty buffers 292*55938Sbostic * that are being deleted out of the cache, so that the lfs_avail 293*55938Sbostic * field can be updated. 294*55938Sbostic */ 295*55938Sbostic for (bp = vp->v_dirtyblkhd; bp; bp = bp->b_blockf) 296*55938Sbostic if (bp->b_flags & B_LOCKED) 297*55938Sbostic fs->lfs_avail -= fsbtodb(fs, 1); 29854694Sbostic e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 29955456Sbostic e2 = VOP_UPDATE(vp, &tv, &tv, 0); 30054694Sbostic return (e1 ? e1 : e2 ? e2 : 0); 30124Sbill } 302