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*54765Smckusick * @(#)lfs_inode.c 7.71 (Berkeley) 07/07/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; 135*54765Smckusick 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 14854694Sbostic vnode_pager_setsize(vp, (u_long)length); 14952327Sbostic 15054694Sbostic ip = VTOI(vp); 15152327Sbostic fs = ip->i_lfs; 15252327Sbostic 15352327Sbostic /* If truncating the file to 0, update the version number. */ 15454694Sbostic if (length == 0) { 15552327Sbostic LFS_IENTRY(ifp, fs, ip->i_number, bp); 15652327Sbostic ++ifp->if_version; 15752327Sbostic LFS_UBWRITE(bp); 15852327Sbostic } 15952327Sbostic 16054694Sbostic /* If length is larger than the file, just update the times. */ 161*54765Smckusick tv = time; 16254694Sbostic if (ip->i_size <= length) { 16352222Sbostic ip->i_flag |= ICHG|IUPD; 164*54765Smckusick return (VOP_UPDATE(vp, &tv, &tv, 1)); 16513000Ssam } 16652327Sbostic 16752222Sbostic /* 16852222Sbostic * Calculate index into inode's block list of last direct and indirect 16952222Sbostic * blocks (if any) which we want to keep. Lastblock is 0 when the 17052222Sbostic * file is truncated to 0. 17152222Sbostic */ 17254694Sbostic lastblock = lblkno(fs, length + fs->lfs_bsize - 1); 17352222Sbostic olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1; 17451484Sbostic 1751203Sbill /* 17651484Sbostic * Update the size of the file. If the file is not being truncated to 17751484Sbostic * a block boundry, the contents of the partial block following the end 17851484Sbostic * of the file must be zero'ed in case it ever become accessable again 17951484Sbostic * because of subsequent file growth. 1801203Sbill */ 18154694Sbostic offset = blkoff(fs, length); 18251484Sbostic if (offset == 0) 18354694Sbostic ip->i_size = length; 18451484Sbostic else { 18554694Sbostic lbn = lblkno(fs, length); 18641313Smckusick #ifdef QUOTA 18754694Sbostic if (e1 = getinoquota(ip)) 18854694Sbostic return (e1); 18951183Sbostic #endif 19054694Sbostic if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp)) 19154694Sbostic return (e1); 19254694Sbostic ip->i_size = length; 19351857Sbostic size = blksize(fs); 19454694Sbostic (void)vnode_pager_uncache(vp); 19526272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 19645112Smckusick allocbuf(bp, size); 19752078Sbostic LFS_UBWRITE(bp); 19817942Smckusick } 19952078Sbostic /* 20052222Sbostic * Modify sup->su_nbyte counters for each deleted block; keep track 20152222Sbostic * of number of blocks removed for ip->i_blocks. 20252222Sbostic */ 20352222Sbostic blocksreleased = 0; 20452222Sbostic num = 0; 20552222Sbostic lastseg = -1; 20652222Sbostic 20752222Sbostic for (lbn = olastblock; lbn >= lastblock;) { 20854694Sbostic lfs_bmaparray(vp, lbn, &daddr, a, &depth); 20952222Sbostic if (lbn == olastblock) 21052222Sbostic for (i = NIADDR + 2; i--;) 21152222Sbostic a_end[i] = a[i]; 21252222Sbostic switch (depth) { 21352222Sbostic case 0: /* Direct block. */ 21452222Sbostic daddr = ip->i_db[lbn]; 21552222Sbostic SEGDEC; 21652222Sbostic ip->i_db[lbn] = 0; 21752222Sbostic --lbn; 21852222Sbostic break; 21952222Sbostic #ifdef DIAGNOSTIC 22052222Sbostic case 1: /* An indirect block. */ 22152222Sbostic panic("lfs_truncate: lfs_bmaparray returned depth 1"); 22252222Sbostic /* NOTREACHED */ 22352222Sbostic #endif 22452222Sbostic default: /* Chain of indirect blocks. */ 22553505Sheideman inp = a + --depth; 22653505Sheideman if (inp->in_off > 0 && lbn != lastblock) { 22753505Sheideman lbn -= inp->in_off < lbn - lastblock ? 22853505Sheideman inp->in_off : lbn - lastblock; 22952222Sbostic break; 23052222Sbostic } 23153505Sheideman for (; depth && (inp->in_off == 0 || lbn == lastblock); 23253505Sheideman --inp, --depth) { 23352222Sbostic /* 23452222Sbostic * XXX 23552222Sbostic * The indirect block may not yet exist, so 23652222Sbostic * bread will create one just so we can free 23752222Sbostic * it. 23852222Sbostic */ 23954694Sbostic if (bread(vp, 24053505Sheideman inp->in_lbn, fs->lfs_bsize, NOCRED, &bp)) 24152222Sbostic panic("lfs_truncate: bread bno %d", 24253505Sheideman inp->in_lbn); 24353505Sheideman daddrp = bp->b_un.b_daddr + inp->in_off; 24453505Sheideman for (i = inp->in_off; 24552222Sbostic i++ <= a_end[depth].in_off;) { 24652222Sbostic daddr = *daddrp++; 24752222Sbostic SEGDEC; 24852222Sbostic } 24953144Sstaelin a_end[depth].in_off = NINDIR(fs) - 1; 25053505Sheideman if (inp->in_off == 0) 25153144Sstaelin brelse (bp); 25253144Sstaelin else { 25353505Sheideman bzero(bp->b_un.b_daddr + inp->in_off, 25452222Sbostic fs->lfs_bsize - 25553505Sheideman inp->in_off * sizeof(daddr_t)); 25652222Sbostic LFS_UBWRITE(bp); 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; 27552222Sbostic ip->i_blocks -= blocksreleased; 27652222Sbostic /* 27752078Sbostic * XXX 27852222Sbostic * Currently, we don't know when we allocate an indirect block, so 27952222Sbostic * ip->i_blocks isn't getting incremented appropriately. As a result, 28052222Sbostic * when we delete any indirect blocks, we get a bad number here. 28152078Sbostic */ 28252222Sbostic if (ip->i_blocks < 0) 28352222Sbostic ip->i_blocks = 0; 28452222Sbostic ip->i_flag |= ICHG|IUPD; 28554694Sbostic e1 = vinvalbuf(vp, length > 0, ap->a_cred, ap->a_p); 286*54765Smckusick e2 = VOP_UPDATE(vp, &tv, &tv, MNT_WAIT); 28754694Sbostic return (e1 ? e1 : e2 ? e2 : 0); 28824Sbill } 289