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*55786Sbostic * @(#)lfs_inode.c 7.76 (Berkeley) 07/29/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 { 3351857Sbostic #ifdef VERBOSE 3451857Sbostic printf("lfs_init\n"); 3551857Sbostic #endif 3651484Sbostic return (ufs_init()); 3724Sbill } 3824Sbill 3952834Sbostic /* Search a block for a specific dinode. */ 4054694Sbostic struct dinode * 4152834Sbostic lfs_ifind(fs, ino, dip) 4252834Sbostic struct lfs *fs; 4352834Sbostic ino_t ino; 4452834Sbostic register struct dinode *dip; 4552834Sbostic { 4652834Sbostic register int cnt; 4754264Sbostic register struct dinode *ldip; 4852834Sbostic 4952834Sbostic #ifdef VERBOSE 5052834Sbostic printf("lfs_ifind: inode %d\n", ino); 5152834Sbostic #endif 5254264Sbostic for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip) 5354264Sbostic if (ldip->di_inum == ino) 5454264Sbostic return (ldip); 5552834Sbostic 5652834Sbostic panic("lfs_ifind: dinode %u not found", ino); 5752834Sbostic /* NOTREACHED */ 5852834Sbostic } 5952834Sbostic 6051346Sbostic int 6154694Sbostic lfs_update(ap) 6254694Sbostic struct vop_update_args /* { 6354694Sbostic struct vnode *a_vp; 6454694Sbostic struct timeval *a_ta; 6554694Sbostic struct timeval *a_tm; 6654694Sbostic int a_waitfor; 6754694Sbostic } */ *ap; 687118Smckusick { 6953867Sheideman struct vnode *vp = ap->a_vp; 7051562Smckusick struct inode *ip; 7151562Smckusick 7251857Sbostic #ifdef VERBOSE 7351857Sbostic printf("lfs_update\n"); 7451857Sbostic #endif 7553867Sheideman if (vp->v_mount->mnt_flag & MNT_RDONLY) 7651562Smckusick return (0); 7753867Sheideman ip = VTOI(vp); 7851562Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 7951562Smckusick return (0); 8051562Smckusick if (ip->i_flag&IACC) 8154103Smckusick ip->i_atime.ts_sec = ap->a_ta->tv_sec; 8252018Smckusick if (ip->i_flag&IUPD) { 8354103Smckusick ip->i_mtime.ts_sec = ap->a_tm->tv_sec; 8454128Smckusick (ip)->i_modrev++; 8552018Smckusick } 8651562Smckusick if (ip->i_flag&ICHG) 8754103Smckusick ip->i_ctime.ts_sec = time.tv_sec; 8851562Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 8951562Smckusick 9052327Sbostic /* Push back the vnode and any dirty blocks it may have. */ 9155549Sbostic return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0); 9224Sbill } 9324Sbill 9452222Sbostic /* Update segment usage information when removing a block. */ 9552225Sbostic #define UPDATE_SEGUSE \ 9652225Sbostic if (lastseg != -1) { \ 9752225Sbostic LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \ 98*55786Sbostic if ((num << fs->lfs_bshift) > sup->su_nbytes) \ 99*55786Sbostic panic("lfs_truncate: negative bytes in segment %d\n", \ 100*55786Sbostic lastseg); \ 10152681Sstaelin sup->su_nbytes -= num << fs->lfs_bshift; \ 10252225Sbostic LFS_UBWRITE(sup_bp); \ 10352225Sbostic blocksreleased += num; \ 10452225Sbostic } 10552222Sbostic 10652222Sbostic #define SEGDEC { \ 10752222Sbostic if (daddr != UNASSIGNED) { \ 10852222Sbostic if (lastseg != (seg = datosn(fs, daddr))) { \ 10952222Sbostic UPDATE_SEGUSE; \ 11052222Sbostic num = 1; \ 11152222Sbostic lastseg = seg; \ 11252222Sbostic } else \ 11352222Sbostic ++num; \ 11452222Sbostic } \ 11552222Sbostic } 11652222Sbostic 11724Sbill /* 11852222Sbostic * Truncate the inode ip to at most length size. Update segment usage 11952222Sbostic * table information. 12024Sbill */ 12151484Sbostic /* ARGSUSED */ 12251484Sbostic int 12354694Sbostic lfs_truncate(ap) 12454694Sbostic struct vop_truncate_args /* { 12554694Sbostic struct vnode *a_vp; 12654694Sbostic off_t a_length; 12754694Sbostic int a_flags; 12854694Sbostic struct ucred *a_cred; 12954694Sbostic struct proc *a_p; 13054694Sbostic } */ *ap; 13124Sbill { 13253505Sheideman register INDIR *inp; 13352222Sbostic register int i; 13452222Sbostic register daddr_t *daddrp; 13554694Sbostic register struct vnode *vp = ap->a_vp; 13654694Sbostic off_t length = ap->a_length; 13752225Sbostic struct buf *bp, *sup_bp; 13854765Smckusick struct timeval tv; 13952327Sbostic struct ifile *ifp; 14052222Sbostic struct inode *ip; 14152222Sbostic struct lfs *fs; 14252222Sbostic INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 14352222Sbostic SEGUSE *sup; 14452222Sbostic daddr_t daddr, lastblock, lbn, olastblock; 14553233Smckusick long off, blocksreleased; 14654694Sbostic int e1, e2, depth, lastseg, num, offset, seg, size; 1479165Ssam 14851857Sbostic #ifdef VERBOSE 14951857Sbostic printf("lfs_truncate\n"); 15051857Sbostic #endif 15155456Sbostic ip = VTOI(vp); 15255456Sbostic tv = time; 15355456Sbostic if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) { 15455456Sbostic #ifdef DIAGNOSTIC 15555456Sbostic if (length != 0) 15655456Sbostic panic("lfs_truncate: partial truncate of symlink"); 15755456Sbostic #endif 15855456Sbostic bzero((char *)&ip->i_shortlink, (u_int)ip->i_size); 15955456Sbostic ip->i_size = 0; 16055456Sbostic ip->i_flag |= ICHG|IUPD; 16155549Sbostic return (VOP_UPDATE(vp, &tv, &tv, 0)); 16255456Sbostic } 16354694Sbostic vnode_pager_setsize(vp, (u_long)length); 16452327Sbostic 16552327Sbostic fs = ip->i_lfs; 16652327Sbostic 16754694Sbostic /* If length is larger than the file, just update the times. */ 16854694Sbostic if (ip->i_size <= length) { 16952222Sbostic ip->i_flag |= ICHG|IUPD; 17055549Sbostic return (VOP_UPDATE(vp, &tv, &tv, 0)); 17113000Ssam } 17252327Sbostic 17352222Sbostic /* 17452222Sbostic * Calculate index into inode's block list of last direct and indirect 17552222Sbostic * blocks (if any) which we want to keep. Lastblock is 0 when the 17652222Sbostic * file is truncated to 0. 17752222Sbostic */ 17854694Sbostic lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 17952222Sbostic olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 18051484Sbostic 1811203Sbill /* 18251484Sbostic * Update the size of the file. If the file is not being truncated to 18351484Sbostic * a block boundry, the contents of the partial block following the end 18451484Sbostic * of the file must be zero'ed in case it ever become accessable again 18551484Sbostic * because of subsequent file growth. 1861203Sbill */ 18754694Sbostic offset = blkoff(fs, length); 18851484Sbostic if (offset == 0) 18954694Sbostic ip->i_size = length; 19051484Sbostic else { 19154694Sbostic lbn = lblkno(fs, length); 19241313Smckusick #ifdef QUOTA 19354694Sbostic if (e1 = getinoquota(ip)) 19454694Sbostic return (e1); 19551183Sbostic #endif 19654694Sbostic if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 19754694Sbostic return (e1); 19854694Sbostic ip->i_size = length; 19951857Sbostic size = blksize(fs); 20054694Sbostic (void)vnode_pager_uncache(vp); 20126272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 20245112Smckusick allocbuf(bp, size); 20352078Sbostic LFS_UBWRITE(bp); 20417942Smckusick } 20552078Sbostic /* 20652222Sbostic * Modify sup->su_nbyte counters for each deleted block; keep track 20752222Sbostic * of number of blocks removed for ip->i_blocks. 20852222Sbostic */ 20952222Sbostic blocksreleased = 0; 21052222Sbostic num = 0; 21152222Sbostic lastseg = -1; 21252222Sbostic 21352222Sbostic for (lbn = olastblock; lbn >= lastblock;) { 21454694Sbostic lfs_bmaparray(vp, lbn, &daddr, a, &depth); 21552222Sbostic if (lbn == olastblock) 21652222Sbostic for (i = NIADDR + 2; i--;) 21752222Sbostic a_end[i] = a[i]; 21852222Sbostic switch (depth) { 21952222Sbostic case 0: /* Direct block. */ 22052222Sbostic daddr = ip->i_db[lbn]; 22152222Sbostic SEGDEC; 22252222Sbostic ip->i_db[lbn] = 0; 22352222Sbostic --lbn; 22452222Sbostic break; 22552222Sbostic #ifdef DIAGNOSTIC 22652222Sbostic case 1: /* An indirect block. */ 22752222Sbostic panic("lfs_truncate: lfs_bmaparray returned depth 1"); 22852222Sbostic /* NOTREACHED */ 22952222Sbostic #endif 23052222Sbostic default: /* Chain of indirect blocks. */ 23153505Sheideman inp = a + --depth; 23253505Sheideman if (inp->in_off > 0 && lbn != lastblock) { 23353505Sheideman lbn -= inp->in_off < lbn - lastblock ? 23453505Sheideman inp->in_off : lbn - lastblock; 23552222Sbostic break; 23652222Sbostic } 23753505Sheideman for (; depth && (inp->in_off == 0 || lbn == lastblock); 23853505Sheideman --inp, --depth) { 23952222Sbostic /* 24052222Sbostic * XXX 24152222Sbostic * The indirect block may not yet exist, so 24252222Sbostic * bread will create one just so we can free 24352222Sbostic * it. 24452222Sbostic */ 24554694Sbostic if (bread(vp, 24653505Sheideman inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 24752222Sbostic panic("lfs_truncate: bread bno %d", 24853505Sheideman inp->in_lbn); 24953505Sheideman daddrp = bp->b_un.b_daddr + inp->in_off; 25053505Sheideman for (i = inp->in_off; 25152222Sbostic i++ <= a_end[depth].in_off;) { 25252222Sbostic daddr = *daddrp++; 25352222Sbostic SEGDEC; 25452222Sbostic } 25553144Sstaelin a_end[depth].in_off = NINDIR(fs) - 1; 25653505Sheideman if (inp->in_off == 0) 25753144Sstaelin brelse (bp); 25853144Sstaelin else { 25953505Sheideman bzero(bp->b_un.b_daddr + inp->in_off, 26052222Sbostic fs->lfs_bsize - 26153505Sheideman inp->in_off * sizeof(daddr_t)); 26252222Sbostic LFS_UBWRITE(bp); 26353144Sstaelin } 26452222Sbostic } 26553144Sstaelin if (depth == 0 && a[1].in_off == 0) { 26652222Sbostic off = a[0].in_off; 26752222Sbostic daddr = ip->i_ib[off]; 26852222Sbostic SEGDEC; 26952222Sbostic ip->i_ib[off] = 0; 27052222Sbostic } 27152681Sstaelin if (lbn == lastblock || lbn <= NDADDR) 27252222Sbostic --lbn; 27352222Sbostic else { 27452222Sbostic lbn -= NINDIR(fs); 27552222Sbostic if (lbn < lastblock) 27652222Sbostic lbn = lastblock; 27752222Sbostic } 27852222Sbostic } 27952222Sbostic } 28052225Sbostic UPDATE_SEGUSE; 281*55786Sbostic 282*55786Sbostic /* If truncating the file to 0, update the version number. */ 283*55786Sbostic if (length == 0) { 284*55786Sbostic LFS_IENTRY(ifp, fs, ip->i_number, bp); 285*55786Sbostic ++ifp->if_version; 286*55786Sbostic LFS_UBWRITE(bp); 287*55786Sbostic } 288*55786Sbostic 28955456Sbostic ip->i_blocks -= btodb(blocksreleased << fs->lfs_bshift); 29055589Sbostic fs->lfs_bfree += btodb(blocksreleased << fs->lfs_bshift); 29155459Sbostic #ifdef DIAGNOSTIC 29252222Sbostic if (ip->i_blocks < 0) 29355459Sbostic panic("lfs_truncate: block count < 0"); 29455459Sbostic #endif 29552222Sbostic ip->i_flag |= ICHG|IUPD; 29654694Sbostic e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 29755456Sbostic e2 = VOP_UPDATE(vp, &tv, &tv, 0); 29854694Sbostic return (e1 ? e1 : e2 ? e2 : 0); 29924Sbill } 300