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*55459Sbostic * @(#)lfs_inode.c 7.73 (Berkeley) 07/20/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. */ 9153867Sheideman return (ap->a_waitfor ? 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); \ 9852681Sstaelin sup->su_nbytes -= num << fs->lfs_bshift; \ 9952225Sbostic LFS_UBWRITE(sup_bp); \ 10052225Sbostic blocksreleased += num; \ 10152225Sbostic } 10252222Sbostic 10352222Sbostic #define SEGDEC { \ 10452222Sbostic if (daddr != UNASSIGNED) { \ 10552222Sbostic if (lastseg != (seg = datosn(fs, daddr))) { \ 10652222Sbostic UPDATE_SEGUSE; \ 10752222Sbostic num = 1; \ 10852222Sbostic lastseg = seg; \ 10952222Sbostic } else \ 11052222Sbostic ++num; \ 11152222Sbostic } \ 11252222Sbostic } 11352222Sbostic 11424Sbill /* 11552222Sbostic * Truncate the inode ip to at most length size. Update segment usage 11652222Sbostic * table information. 11724Sbill */ 11851484Sbostic /* ARGSUSED */ 11951484Sbostic int 12054694Sbostic lfs_truncate(ap) 12154694Sbostic struct vop_truncate_args /* { 12254694Sbostic struct vnode *a_vp; 12354694Sbostic off_t a_length; 12454694Sbostic int a_flags; 12554694Sbostic struct ucred *a_cred; 12654694Sbostic struct proc *a_p; 12754694Sbostic } */ *ap; 12824Sbill { 12953505Sheideman register INDIR *inp; 13052222Sbostic register int i; 13152222Sbostic register daddr_t *daddrp; 13254694Sbostic register struct vnode *vp = ap->a_vp; 13354694Sbostic off_t length = ap->a_length; 13452225Sbostic struct buf *bp, *sup_bp; 13554765Smckusick struct timeval tv; 13652327Sbostic struct ifile *ifp; 13752222Sbostic struct inode *ip; 13852222Sbostic struct lfs *fs; 13952222Sbostic INDIR a[NIADDR + 2], a_end[NIADDR + 2]; 14052222Sbostic SEGUSE *sup; 14152222Sbostic daddr_t daddr, lastblock, lbn, olastblock; 14253233Smckusick long off, blocksreleased; 14354694Sbostic int e1, e2, depth, lastseg, num, offset, seg, size; 1449165Ssam 14551857Sbostic #ifdef VERBOSE 14651857Sbostic printf("lfs_truncate\n"); 14751857Sbostic #endif 14855456Sbostic ip = VTOI(vp); 14955456Sbostic tv = time; 15055456Sbostic if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) { 15155456Sbostic #ifdef DIAGNOSTIC 15255456Sbostic if (length != 0) 15355456Sbostic panic("lfs_truncate: partial truncate of symlink"); 15455456Sbostic #endif 15555456Sbostic bzero((char *)&ip->i_shortlink, (u_int)ip->i_size); 15655456Sbostic ip->i_size = 0; 15755456Sbostic ip->i_flag |= ICHG|IUPD; 15855456Sbostic return (VOP_UPDATE(vp, &tv, &tv, 1)); 15955456Sbostic } 16054694Sbostic vnode_pager_setsize(vp, (u_long)length); 16152327Sbostic 16252327Sbostic fs = ip->i_lfs; 16352327Sbostic 16452327Sbostic /* If truncating the file to 0, update the version number. */ 16554694Sbostic if (length == 0) { 16652327Sbostic LFS_IENTRY(ifp, fs, ip->i_number, bp); 16752327Sbostic ++ifp->if_version; 16852327Sbostic LFS_UBWRITE(bp); 16952327Sbostic } 17052327Sbostic 17154694Sbostic /* If length is larger than the file, just update the times. */ 17254694Sbostic if (ip->i_size <= length) { 17352222Sbostic ip->i_flag |= ICHG|IUPD; 17454765Smckusick return (VOP_UPDATE(vp, &tv, &tv, 1)); 17513000Ssam } 17652327Sbostic 17752222Sbostic /* 17852222Sbostic * Calculate index into inode's block list of last direct and indirect 17952222Sbostic * blocks (if any) which we want to keep. Lastblock is 0 when the 18052222Sbostic * file is truncated to 0. 18152222Sbostic */ 18254694Sbostic lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 18352222Sbostic olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 18451484Sbostic 1851203Sbill /* 18651484Sbostic * Update the size of the file. If the file is not being truncated to 18751484Sbostic * a block boundry, the contents of the partial block following the end 18851484Sbostic * of the file must be zero'ed in case it ever become accessable again 18951484Sbostic * because of subsequent file growth. 1901203Sbill */ 19154694Sbostic offset = blkoff(fs, length); 19251484Sbostic if (offset == 0) 19354694Sbostic ip->i_size = length; 19451484Sbostic else { 19554694Sbostic lbn = lblkno(fs, length); 19641313Smckusick #ifdef QUOTA 19754694Sbostic if (e1 = getinoquota(ip)) 19854694Sbostic return (e1); 19951183Sbostic #endif 20054694Sbostic if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 20154694Sbostic return (e1); 20254694Sbostic ip->i_size = length; 20351857Sbostic size = blksize(fs); 20454694Sbostic (void)vnode_pager_uncache(vp); 20526272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 20645112Smckusick allocbuf(bp, size); 20752078Sbostic LFS_UBWRITE(bp); 20817942Smckusick } 20952078Sbostic /* 21052222Sbostic * Modify sup->su_nbyte counters for each deleted block; keep track 21152222Sbostic * of number of blocks removed for ip->i_blocks. 21252222Sbostic */ 21352222Sbostic blocksreleased = 0; 21452222Sbostic num = 0; 21552222Sbostic lastseg = -1; 21652222Sbostic 21752222Sbostic for (lbn = olastblock; lbn >= lastblock;) { 21854694Sbostic lfs_bmaparray(vp, lbn, &daddr, a, &depth); 21952222Sbostic if (lbn == olastblock) 22052222Sbostic for (i = NIADDR + 2; i--;) 22152222Sbostic a_end[i] = a[i]; 22252222Sbostic switch (depth) { 22352222Sbostic case 0: /* Direct block. */ 22452222Sbostic daddr = ip->i_db[lbn]; 22552222Sbostic SEGDEC; 22652222Sbostic ip->i_db[lbn] = 0; 22752222Sbostic --lbn; 22852222Sbostic break; 22952222Sbostic #ifdef DIAGNOSTIC 23052222Sbostic case 1: /* An indirect block. */ 23152222Sbostic panic("lfs_truncate: lfs_bmaparray returned depth 1"); 23252222Sbostic /* NOTREACHED */ 23352222Sbostic #endif 23452222Sbostic default: /* Chain of indirect blocks. */ 23553505Sheideman inp = a + --depth; 23653505Sheideman if (inp->in_off > 0 && lbn != lastblock) { 23753505Sheideman lbn -= inp->in_off < lbn - lastblock ? 23853505Sheideman inp->in_off : lbn - lastblock; 23952222Sbostic break; 24052222Sbostic } 24153505Sheideman for (; depth && (inp->in_off == 0 || lbn == lastblock); 24253505Sheideman --inp, --depth) { 24352222Sbostic /* 24452222Sbostic * XXX 24552222Sbostic * The indirect block may not yet exist, so 24652222Sbostic * bread will create one just so we can free 24752222Sbostic * it. 24852222Sbostic */ 24954694Sbostic if (bread(vp, 25053505Sheideman inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 25152222Sbostic panic("lfs_truncate: bread bno %d", 25253505Sheideman inp->in_lbn); 25353505Sheideman daddrp = bp->b_un.b_daddr + inp->in_off; 25453505Sheideman for (i = inp->in_off; 25552222Sbostic i++ <= a_end[depth].in_off;) { 25652222Sbostic daddr = *daddrp++; 25752222Sbostic SEGDEC; 25852222Sbostic } 25953144Sstaelin a_end[depth].in_off = NINDIR(fs) - 1; 26053505Sheideman if (inp->in_off == 0) 26153144Sstaelin brelse (bp); 26253144Sstaelin else { 26353505Sheideman bzero(bp->b_un.b_daddr + inp->in_off, 26452222Sbostic fs->lfs_bsize - 26553505Sheideman inp->in_off * sizeof(daddr_t)); 26652222Sbostic LFS_UBWRITE(bp); 26753144Sstaelin } 26852222Sbostic } 26953144Sstaelin if (depth == 0 && a[1].in_off == 0) { 27052222Sbostic off = a[0].in_off; 27152222Sbostic daddr = ip->i_ib[off]; 27252222Sbostic SEGDEC; 27352222Sbostic ip->i_ib[off] = 0; 27452222Sbostic } 27552681Sstaelin if (lbn == lastblock || lbn <= NDADDR) 27652222Sbostic --lbn; 27752222Sbostic else { 27852222Sbostic lbn -= NINDIR(fs); 27952222Sbostic if (lbn < lastblock) 28052222Sbostic lbn = lastblock; 28152222Sbostic } 28252222Sbostic } 28352222Sbostic } 28452225Sbostic UPDATE_SEGUSE; 28555456Sbostic ip->i_blocks -= btodb(blocksreleased << fs->lfs_bshift); 286*55459Sbostic #ifdef DIAGNOSTIC 28752222Sbostic if (ip->i_blocks < 0) 288*55459Sbostic panic("lfs_truncate: block count < 0"); 289*55459Sbostic #endif 29052222Sbostic ip->i_flag |= ICHG|IUPD; 29154694Sbostic e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 29255456Sbostic e2 = VOP_UPDATE(vp, &tv, &tv, 0); 29354694Sbostic return (e1 ? e1 : e2 ? e2 : 0); 29424Sbill } 295