123399Smckusick /* 237736Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337736Smckusick * All rights reserved. 423399Smckusick * 544537Sbostic * %sccs.include.redist.c% 637736Smckusick * 7*46205Smckusick * @(#)lfs_inode.c 7.37 (Berkeley) 02/01/91 823399Smckusick */ 924Sbill 1017099Sbloom #include "param.h" 1117099Sbloom #include "systm.h" 1217099Sbloom #include "mount.h" 1317099Sbloom #include "user.h" 1439795Smckusick #include "proc.h" 1537736Smckusick #include "file.h" 1617099Sbloom #include "buf.h" 1724525Sbloom #include "cmap.h" 1837736Smckusick #include "vnode.h" 1941313Smckusick #include "../ufs/quota.h" 2037736Smckusick #include "../ufs/inode.h" 2137736Smckusick #include "../ufs/fs.h" 2237736Smckusick #include "../ufs/ufsmount.h" 2317099Sbloom #include "kernel.h" 2431661Smckusick #include "malloc.h" 2524Sbill 2616840Smckusick #define INOHSZ 512 277334Skre #if ((INOHSZ&(INOHSZ-1)) == 0) 287334Skre #define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1)) 297334Skre #else 3010852Ssam #define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ) 317334Skre #endif 3224Sbill 3339392Smckusick union ihead { 347334Skre union ihead *ih_head[2]; 357334Skre struct inode *ih_chain[2]; 367334Skre } ihead[INOHSZ]; 377334Skre 3839574Smckusick int prtactive; /* 1 => print out reclaim of active vnodes */ 3939574Smckusick 4024Sbill /* 4139392Smckusick * Initialize hash links for inodes. 4224Sbill */ 4339440Smckusick ufs_init() 4424Sbill { 4524Sbill register int i; 4639392Smckusick register union ihead *ih = ihead; 4724Sbill 4839492Smckusick #ifndef lint 4939392Smckusick if (VN_MAXPRIVATE < sizeof(struct inode)) 5039392Smckusick panic("ihinit: too small"); 5139492Smckusick #endif /* not lint */ 527334Skre for (i = INOHSZ; --i >= 0; ih++) { 537334Skre ih->ih_head[0] = ih; 547334Skre ih->ih_head[1] = ih; 557334Skre } 5641313Smckusick #ifdef QUOTA 5741313Smckusick dqinit(); 5841313Smckusick #endif /* QUOTA */ 5924Sbill } 6024Sbill 6124Sbill /* 6237736Smckusick * Look up an vnode/inode by device,inumber. 6324Sbill * If it is in core (in the inode structure), 6424Sbill * honor the locking protocol. 6524Sbill * If it is not in core, read it in from the 6624Sbill * specified device. 6737736Smckusick * Callers must check for mount points!! 6824Sbill * In all cases, a pointer to a locked 6924Sbill * inode structure is returned. 7024Sbill */ 7137736Smckusick iget(xp, ino, ipp) 7237736Smckusick struct inode *xp; 734818Swnj ino_t ino; 7437736Smckusick struct inode **ipp; 7524Sbill { 7637736Smckusick dev_t dev = xp->i_dev; 7737736Smckusick struct mount *mntp = ITOV(xp)->v_mount; 7837736Smckusick register struct fs *fs = VFSTOUFS(mntp)->um_fs; 7939440Smckusick extern struct vnodeops ufs_vnodeops, spec_inodeops; 8037736Smckusick register struct inode *ip, *iq; 8137736Smckusick register struct vnode *vp; 8239440Smckusick struct vnode *nvp; 8337736Smckusick struct buf *bp; 8439440Smckusick struct dinode *dp; 8541313Smckusick union ihead *ih; 8641313Smckusick int i, error; 8724Sbill 8839440Smckusick ih = &ihead[INOHASH(dev, ino)]; 8924Sbill loop: 9039392Smckusick for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) { 9139392Smckusick if (ino != ip->i_number || dev != ip->i_dev) 9239392Smckusick continue; 9339392Smckusick if ((ip->i_flag&ILOCKED) != 0) { 9439392Smckusick ip->i_flag |= IWANT; 9539392Smckusick sleep((caddr_t)ip, PINOD); 9639392Smckusick goto loop; 9739392Smckusick } 9839440Smckusick if (vget(ITOV(ip))) 9939440Smckusick goto loop; 10039392Smckusick *ipp = ip; 10139392Smckusick return(0); 10239392Smckusick } 10339440Smckusick /* 10439440Smckusick * Allocate a new inode. 10539440Smckusick */ 10639440Smckusick if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) { 10737736Smckusick *ipp = 0; 10837736Smckusick return (error); 10937736Smckusick } 11039440Smckusick ip = VTOI(nvp); 11139440Smckusick ip->i_vnode = nvp; 11239440Smckusick ip->i_flag = 0; 11339440Smckusick ip->i_devvp = 0; 11439440Smckusick ip->i_mode = 0; 11539879Smckusick ip->i_diroff = 0; 116*46205Smckusick ip->i_lockf = 0; 11739440Smckusick #ifdef QUOTA 11841313Smckusick for (i = 0; i < MAXQUOTAS; i++) 11941313Smckusick ip->i_dquot[i] = NODQUOT; 12039440Smckusick #endif 12137736Smckusick /* 12239440Smckusick * Put it onto its hash chain and lock it so that other requests for 12339440Smckusick * this inode will block if they arrive while we are sleeping waiting 12439440Smckusick * for old data structures to be purged or for the contents of the 12539440Smckusick * disk portion of this inode to be read. 12639440Smckusick */ 12739440Smckusick ip->i_dev = dev; 12839440Smckusick ip->i_number = ino; 12939440Smckusick insque(ip, ih); 13039440Smckusick ILOCK(ip); 13139440Smckusick /* 13237736Smckusick * Read in the disk contents for the inode. 13337736Smckusick */ 13437736Smckusick if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)), 13538776Smckusick (int)fs->fs_bsize, NOCRED, &bp)) { 13637736Smckusick /* 13741334Smckusick * The inode does not contain anything useful, so it would 13841334Smckusick * be misleading to leave it on its hash chain. 13941334Smckusick * Iput() will take care of putting it back on the free list. 14041334Smckusick */ 14141334Smckusick remque(ip); 14241334Smckusick ip->i_forw = ip; 14341334Smckusick ip->i_back = ip; 14441334Smckusick /* 14539392Smckusick * Unlock and discard unneeded inode. 14637736Smckusick */ 14739440Smckusick iput(ip); 14837736Smckusick brelse(bp); 14937736Smckusick *ipp = 0; 15039440Smckusick return (error); 15137736Smckusick } 15239440Smckusick dp = bp->b_un.b_dino; 15339440Smckusick dp += itoo(fs, ino); 15439440Smckusick ip->i_din = *dp; 15539440Smckusick brelse(bp); 15637736Smckusick /* 15739440Smckusick * Initialize the associated vnode 15837736Smckusick */ 15939440Smckusick vp = ITOV(ip); 16039440Smckusick vp->v_type = IFTOVT(ip->i_mode); 16140289Smckusick if (vp->v_type == VFIFO) { 16240289Smckusick #ifdef FIFO 16340289Smckusick extern struct vnodeops fifo_inodeops; 16440289Smckusick vp->v_op = &fifo_inodeops; 16540289Smckusick #else 16640289Smckusick iput(ip); 16740289Smckusick *ipp = 0; 16840289Smckusick return (EOPNOTSUPP); 16940289Smckusick #endif /* FIFO */ 17040289Smckusick } 17139440Smckusick if (vp->v_type == VCHR || vp->v_type == VBLK) { 17239440Smckusick vp->v_op = &spec_inodeops; 17339617Smckusick if (nvp = checkalias(vp, ip->i_rdev, mntp)) { 17437736Smckusick /* 17539440Smckusick * Reinitialize aliased inode. 17637736Smckusick */ 17739440Smckusick vp = nvp; 17839440Smckusick iq = VTOI(vp); 17939440Smckusick iq->i_vnode = vp; 18039517Smckusick iq->i_flag = 0; 18139440Smckusick ILOCK(iq); 18239440Smckusick iq->i_din = ip->i_din; 18339440Smckusick iq->i_dev = dev; 18439440Smckusick iq->i_number = ino; 18539440Smckusick insque(iq, ih); 18637736Smckusick /* 18739440Smckusick * Discard unneeded vnode 18837736Smckusick */ 18939440Smckusick ip->i_mode = 0; 19039440Smckusick iput(ip); 19137736Smckusick ip = iq; 19237736Smckusick } 19337736Smckusick } 19439440Smckusick if (ino == ROOTINO) 19539440Smckusick vp->v_flag |= VROOT; 19637736Smckusick /* 19737736Smckusick * Finish inode initialization. 19837736Smckusick */ 19937736Smckusick ip->i_fs = fs; 20037736Smckusick ip->i_devvp = VFSTOUFS(mntp)->um_devvp; 20138345Smckusick VREF(ip->i_devvp); 20238256Smckusick /* 20338256Smckusick * Set up a generation number for this inode if it does not 20438256Smckusick * already have one. This should only happen on old filesystems. 20538256Smckusick */ 20638256Smckusick if (ip->i_gen == 0) { 20738256Smckusick if (++nextgennumber < (u_long)time.tv_sec) 20838256Smckusick nextgennumber = time.tv_sec; 20938256Smckusick ip->i_gen = nextgennumber; 21041397Smckusick if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) 21138256Smckusick ip->i_flag |= IMOD; 21238256Smckusick } 21337736Smckusick *ipp = ip; 21437736Smckusick return (0); 21537736Smckusick } 2167334Skre 21737736Smckusick /* 21839392Smckusick * Unlock and decrement the reference count of an inode structure. 21924Sbill */ 22024Sbill iput(ip) 2214818Swnj register struct inode *ip; 22224Sbill { 2237118Smckusick 2248452Sroot if ((ip->i_flag & ILOCKED) == 0) 2257118Smckusick panic("iput"); 22616665Smckusick IUNLOCK(ip); 22737736Smckusick vrele(ITOV(ip)); 2287118Smckusick } 2297118Smckusick 23039392Smckusick /* 23139392Smckusick * Last reference to an inode, write the inode out and if necessary, 23239392Smckusick * truncate and deallocate the file. 23339392Smckusick */ 23437736Smckusick ufs_inactive(vp) 23537736Smckusick struct vnode *vp; 2367118Smckusick { 23737736Smckusick register struct inode *ip = VTOI(vp); 23839392Smckusick int mode, error = 0; 23924Sbill 24039816Smckusick if (prtactive && vp->v_usecount != 0) 24139676Smckusick vprint("ufs_inactive: pushing active", vp); 24238452Smckusick /* 24338452Smckusick * Get rid of inodes related to stale file handles. 24438452Smckusick */ 24539440Smckusick if (ip->i_mode == 0) { 24639676Smckusick if ((vp->v_flag & VXLOCK) == 0) 24739676Smckusick vgone(vp); 24839440Smckusick return (0); 24939440Smckusick } 25038226Smckusick ILOCK(ip); 25141397Smckusick if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 25241313Smckusick #ifdef QUOTA 25341313Smckusick if (!getinoquota(ip)) 25441313Smckusick (void) chkiq(ip, -1, NOCRED, 0); 25541313Smckusick #endif 25639676Smckusick error = itrunc(ip, (u_long)0, 0); 25737736Smckusick mode = ip->i_mode; 25837736Smckusick ip->i_mode = 0; 25944893Strent ip->i_rdev = 0; 26037736Smckusick ip->i_flag |= IUPD|ICHG; 26137736Smckusick ifree(ip, ip->i_number, mode); 26237736Smckusick } 26337736Smckusick IUPDAT(ip, &time, &time, 0); 26440033Smckusick IUNLOCK(ip); 26540033Smckusick ip->i_flag = 0; 26637736Smckusick /* 26739392Smckusick * If we are done with the inode, reclaim it 26839392Smckusick * so that it can be reused immediately. 26937736Smckusick */ 27041313Smckusick if (vp->v_usecount == 0 && ip->i_mode == 0) 27140033Smckusick vgone(vp); 27237736Smckusick return (error); 27324Sbill } 27424Sbill 27524Sbill /* 27639392Smckusick * Reclaim an inode so that it can be used for other purposes. 27724Sbill */ 27839392Smckusick ufs_reclaim(vp) 27939392Smckusick register struct vnode *vp; 28039392Smckusick { 28139492Smckusick register struct inode *ip = VTOI(vp); 28241313Smckusick int i; 28339392Smckusick 28439816Smckusick if (prtactive && vp->v_usecount != 0) 28539676Smckusick vprint("ufs_reclaim: pushing active", vp); 28639392Smckusick /* 28739392Smckusick * Remove the inode from its hash chain. 28839392Smckusick */ 28939392Smckusick remque(ip); 29039392Smckusick ip->i_forw = ip; 29139392Smckusick ip->i_back = ip; 29239392Smckusick /* 29339392Smckusick * Purge old data structures associated with the inode. 29439392Smckusick */ 29539392Smckusick cache_purge(vp); 29639392Smckusick if (ip->i_devvp) { 29739392Smckusick vrele(ip->i_devvp); 29839392Smckusick ip->i_devvp = 0; 29939392Smckusick } 30039392Smckusick #ifdef QUOTA 30141313Smckusick for (i = 0; i < MAXQUOTAS; i++) { 30241313Smckusick if (ip->i_dquot[i] != NODQUOT) { 30341313Smckusick dqrele(vp, ip->i_dquot[i]); 30441313Smckusick ip->i_dquot[i] = NODQUOT; 30541313Smckusick } 30641313Smckusick } 30739392Smckusick #endif 30839392Smckusick ip->i_flag = 0; 30939392Smckusick return (0); 31039392Smckusick } 31139392Smckusick 31239392Smckusick /* 31339392Smckusick * Check accessed and update flags on an inode structure. 31439392Smckusick * If any is on, update the inode with the current time. 31539392Smckusick * If waitfor is given, then must ensure I/O order, 31639392Smckusick * so wait for write to complete. 31739392Smckusick */ 3181203Sbill iupdat(ip, ta, tm, waitfor) 3194818Swnj register struct inode *ip; 3208630Sroot struct timeval *ta, *tm; 3214818Swnj int waitfor; 32224Sbill { 32337736Smckusick struct buf *bp; 32437736Smckusick struct vnode *vp = ITOV(ip); 32524Sbill struct dinode *dp; 32630749Skarels register struct fs *fs; 32737736Smckusick int error; 32824Sbill 32930749Skarels fs = ip->i_fs; 33037736Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 33137736Smckusick return (0); 33241397Smckusick if (vp->v_mount->mnt_flag & MNT_RDONLY) 33337736Smckusick return (0); 33437736Smckusick error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), 33538776Smckusick (int)fs->fs_bsize, NOCRED, &bp); 33637736Smckusick if (error) { 33737736Smckusick brelse(bp); 33837736Smckusick return (error); 33924Sbill } 34037736Smckusick if (ip->i_flag&IACC) 34137736Smckusick ip->i_atime = ta->tv_sec; 34237736Smckusick if (ip->i_flag&IUPD) 34337736Smckusick ip->i_mtime = tm->tv_sec; 34437736Smckusick if (ip->i_flag&ICHG) 34537736Smckusick ip->i_ctime = time.tv_sec; 34637736Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 34737736Smckusick dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 34839392Smckusick *dp = ip->i_din; 34937736Smckusick if (waitfor) { 35037736Smckusick return (bwrite(bp)); 35137736Smckusick } else { 35237736Smckusick bdwrite(bp); 35337736Smckusick return (0); 35437736Smckusick } 35524Sbill } 35624Sbill 35710736Ssam #define SINGLE 0 /* index of single indirect block */ 35810736Ssam #define DOUBLE 1 /* index of double indirect block */ 35910736Ssam #define TRIPLE 2 /* index of triple indirect block */ 36024Sbill /* 36139392Smckusick * Truncate the inode ip to at most length size. Free affected disk 36239392Smckusick * blocks -- the blocks of the file are removed in reverse order. 36310736Ssam * 36410736Ssam * NB: triple indirect blocks are untested. 36524Sbill */ 36639676Smckusick itrunc(oip, length, flags) 36717942Smckusick register struct inode *oip; 3689165Ssam u_long length; 36939676Smckusick int flags; 37024Sbill { 3719165Ssam register daddr_t lastblock; 37226272Skarels daddr_t bn, lbn, lastiblock[NIADDR]; 3736569Smckusic register struct fs *fs; 37410736Ssam register struct inode *ip; 37517942Smckusick struct buf *bp; 37637736Smckusick int offset, osize, size, level; 37737736Smckusick long count, nblocks, blocksreleased = 0; 37817942Smckusick register int i; 37939676Smckusick int aflags, error, allerror; 38010736Ssam struct inode tip; 3819165Ssam 38245721Smckusick vnode_pager_setsize(ITOV(oip), length); 38313000Ssam if (oip->i_size <= length) { 38413000Ssam oip->i_flag |= ICHG|IUPD; 38537736Smckusick error = iupdat(oip, &time, &time, 1); 38637736Smckusick return (error); 38713000Ssam } 3881203Sbill /* 38910736Ssam * Calculate index into inode's block list of 39010736Ssam * last direct and indirect blocks (if any) 39110736Ssam * which we want to keep. Lastblock is -1 when 39210736Ssam * the file is truncated to 0. 3931203Sbill */ 39410736Ssam fs = oip->i_fs; 3959165Ssam lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 39610736Ssam lastiblock[SINGLE] = lastblock - NDADDR; 39710736Ssam lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 39810736Ssam lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 39912645Ssam nblocks = btodb(fs->fs_bsize); 4006569Smckusic /* 40117942Smckusick * Update the size of the file. If the file is not being 40217942Smckusick * truncated to a block boundry, the contents of the 40317942Smckusick * partial block following the end of the file must be 40417942Smckusick * zero'ed in case it ever become accessable again because 40517942Smckusick * of subsequent file growth. 40617942Smckusick */ 40717942Smckusick osize = oip->i_size; 40817942Smckusick offset = blkoff(fs, length); 40917942Smckusick if (offset == 0) { 41017942Smckusick oip->i_size = length; 41117942Smckusick } else { 41217942Smckusick lbn = lblkno(fs, length); 41339676Smckusick aflags = B_CLRBUF; 41439676Smckusick if (flags & IO_SYNC) 41539676Smckusick aflags |= B_SYNC; 41641313Smckusick #ifdef QUOTA 41741313Smckusick if (error = getinoquota(oip)) 41841313Smckusick return (error); 41941313Smckusick #endif 42039676Smckusick if (error = balloc(oip, lbn, offset, &bp, aflags)) 42137736Smckusick return (error); 42217942Smckusick oip->i_size = length; 42317942Smckusick size = blksize(fs, oip, lbn); 42445721Smckusick (void) vnode_pager_uncache(ITOV(oip)); 42526272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 42645112Smckusick allocbuf(bp, size); 42739676Smckusick if (flags & IO_SYNC) 42839676Smckusick bwrite(bp); 42939676Smckusick else 43039676Smckusick bdwrite(bp); 43117942Smckusick } 43217942Smckusick /* 43317942Smckusick * Update file and block pointers 43410736Ssam * on disk before we start freeing blocks. 43510736Ssam * If we crash before free'ing blocks below, 43610736Ssam * the blocks will be returned to the free list. 43710736Ssam * lastiblock values are also normalized to -1 43810736Ssam * for calls to indirtrunc below. 4396569Smckusic */ 44010736Ssam tip = *oip; 44117942Smckusick tip.i_size = osize; 44210736Ssam for (level = TRIPLE; level >= SINGLE; level--) 44310736Ssam if (lastiblock[level] < 0) { 44410736Ssam oip->i_ib[level] = 0; 44510736Ssam lastiblock[level] = -1; 4469165Ssam } 44710736Ssam for (i = NDADDR - 1; i > lastblock; i--) 44810736Ssam oip->i_db[i] = 0; 44910736Ssam oip->i_flag |= ICHG|IUPD; 45039676Smckusick vinvalbuf(ITOV(oip), (length > 0)); 45139740Smckusick allerror = iupdat(oip, &time, &time, MNT_WAIT); 45210736Ssam 4536569Smckusic /* 45410736Ssam * Indirect blocks first. 4556569Smckusic */ 45617942Smckusick ip = &tip; 45710736Ssam for (level = TRIPLE; level >= SINGLE; level--) { 45810736Ssam bn = ip->i_ib[level]; 4599165Ssam if (bn != 0) { 46037736Smckusick error = indirtrunc(ip, bn, lastiblock[level], level, 46137736Smckusick &count); 46237736Smckusick if (error) 46337736Smckusick allerror = error; 46437736Smckusick blocksreleased += count; 46510736Ssam if (lastiblock[level] < 0) { 46610736Ssam ip->i_ib[level] = 0; 46731402Smckusick blkfree(ip, bn, (off_t)fs->fs_bsize); 46810736Ssam blocksreleased += nblocks; 46910736Ssam } 47010736Ssam } 47110736Ssam if (lastiblock[level] >= 0) 47210736Ssam goto done; 4739165Ssam } 47410736Ssam 4756569Smckusic /* 47610736Ssam * All whole direct blocks or frags. 4776569Smckusic */ 4789165Ssam for (i = NDADDR - 1; i > lastblock; i--) { 47926359Skarels register off_t bsize; 4809165Ssam 4816569Smckusic bn = ip->i_db[i]; 4829165Ssam if (bn == 0) 48324Sbill continue; 4849165Ssam ip->i_db[i] = 0; 48524525Sbloom bsize = (off_t)blksize(fs, ip, i); 48631402Smckusick blkfree(ip, bn, bsize); 48724525Sbloom blocksreleased += btodb(bsize); 48824Sbill } 48910736Ssam if (lastblock < 0) 49010736Ssam goto done; 49110736Ssam 4921203Sbill /* 4939165Ssam * Finally, look for a change in size of the 4949165Ssam * last direct block; release any frags. 4951203Sbill */ 49610736Ssam bn = ip->i_db[lastblock]; 49710736Ssam if (bn != 0) { 49826359Skarels off_t oldspace, newspace; 49910736Ssam 5009165Ssam /* 5019165Ssam * Calculate amount of space we're giving 5029165Ssam * back as old block size minus new block size. 5039165Ssam */ 50410736Ssam oldspace = blksize(fs, ip, lastblock); 5059165Ssam ip->i_size = length; 50610736Ssam newspace = blksize(fs, ip, lastblock); 50710736Ssam if (newspace == 0) 50810736Ssam panic("itrunc: newspace"); 50910736Ssam if (oldspace - newspace > 0) { 5109165Ssam /* 5119165Ssam * Block number of space to be free'd is 5129165Ssam * the old block # plus the number of frags 5139165Ssam * required for the storage we're keeping. 5149165Ssam */ 51510736Ssam bn += numfrags(fs, newspace); 51631402Smckusick blkfree(ip, bn, oldspace - newspace); 51712645Ssam blocksreleased += btodb(oldspace - newspace); 5189165Ssam } 5199165Ssam } 5209165Ssam done: 52110736Ssam /* BEGIN PARANOIA */ 52210736Ssam for (level = SINGLE; level <= TRIPLE; level++) 52310736Ssam if (ip->i_ib[level] != oip->i_ib[level]) 52410736Ssam panic("itrunc1"); 52510736Ssam for (i = 0; i < NDADDR; i++) 52610736Ssam if (ip->i_db[i] != oip->i_db[i]) 52710736Ssam panic("itrunc2"); 52810736Ssam /* END PARANOIA */ 52912645Ssam oip->i_blocks -= blocksreleased; 53012645Ssam if (oip->i_blocks < 0) /* sanity */ 53112645Ssam oip->i_blocks = 0; 53212645Ssam oip->i_flag |= ICHG; 5339165Ssam #ifdef QUOTA 53441313Smckusick if (!getinoquota(oip)) 53541313Smckusick (void) chkdq(oip, -blocksreleased, NOCRED, 0); 5369165Ssam #endif 53737736Smckusick return (allerror); 53824Sbill } 53924Sbill 5409165Ssam /* 5419165Ssam * Release blocks associated with the inode ip and 5429165Ssam * stored in the indirect block bn. Blocks are free'd 5439165Ssam * in LIFO order up to (but not including) lastbn. If 54410736Ssam * level is greater than SINGLE, the block is an indirect 54510736Ssam * block and recursive calls to indirtrunc must be used to 54610736Ssam * cleanse other indirect blocks. 54710736Ssam * 54810736Ssam * NB: triple indirect blocks are untested. 5499165Ssam */ 55037736Smckusick indirtrunc(ip, bn, lastbn, level, countp) 5516569Smckusic register struct inode *ip; 5529165Ssam daddr_t bn, lastbn; 55310736Ssam int level; 55437736Smckusick long *countp; 55524Sbill { 5569165Ssam register int i; 55731661Smckusick struct buf *bp; 55831661Smckusick register struct fs *fs = ip->i_fs; 55924Sbill register daddr_t *bap; 56031661Smckusick daddr_t *copy, nb, last; 56137736Smckusick long blkcount, factor; 56237736Smckusick int nblocks, blocksreleased = 0; 56337736Smckusick int error, allerror = 0; 56424Sbill 56510736Ssam /* 56610736Ssam * Calculate index in current block of last 56710736Ssam * block to be kept. -1 indicates the entire 56810736Ssam * block so we need not calculate the index. 56910736Ssam */ 57010736Ssam factor = 1; 57110736Ssam for (i = SINGLE; i < level; i++) 57210736Ssam factor *= NINDIR(fs); 5739165Ssam last = lastbn; 57410736Ssam if (lastbn > 0) 57510736Ssam last /= factor; 57612645Ssam nblocks = btodb(fs->fs_bsize); 57710736Ssam /* 57810736Ssam * Get buffer of block pointers, zero those 57910736Ssam * entries corresponding to blocks to be free'd, 58010736Ssam * and update on disk copy first. 58110736Ssam */ 58238776Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, 58338776Smckusick NOCRED, &bp); 58437736Smckusick if (error) { 58510736Ssam brelse(bp); 58637736Smckusick *countp = 0; 58737736Smckusick return (error); 58810736Ssam } 58910736Ssam bap = bp->b_un.b_daddr; 59031661Smckusick MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 59131661Smckusick bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 59210736Ssam bzero((caddr_t)&bap[last + 1], 59310736Ssam (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 59439676Smckusick if (last == -1) 59539676Smckusick bp->b_flags |= B_INVAL; 59637736Smckusick error = bwrite(bp); 59737736Smckusick if (error) 59837736Smckusick allerror = error; 59931661Smckusick bap = copy; 60010736Ssam 60110736Ssam /* 60210736Ssam * Recursively free totally unused blocks. 60310736Ssam */ 6049165Ssam for (i = NINDIR(fs) - 1; i > last; i--) { 60524Sbill nb = bap[i]; 6069165Ssam if (nb == 0) 60724Sbill continue; 60837736Smckusick if (level > SINGLE) { 60937736Smckusick error = indirtrunc(ip, nb, (daddr_t)-1, level - 1, 61037736Smckusick &blkcount); 61137736Smckusick if (error) 61237736Smckusick allerror = error; 61337736Smckusick blocksreleased += blkcount; 61437736Smckusick } 61531402Smckusick blkfree(ip, nb, (off_t)fs->fs_bsize); 6169165Ssam blocksreleased += nblocks; 61724Sbill } 61810736Ssam 61910736Ssam /* 62010736Ssam * Recursively free last partial block. 62110736Ssam */ 62210736Ssam if (level > SINGLE && lastbn >= 0) { 62310736Ssam last = lastbn % factor; 6249165Ssam nb = bap[i]; 62537736Smckusick if (nb != 0) { 62637736Smckusick error = indirtrunc(ip, nb, last, level - 1, &blkcount); 62737736Smckusick if (error) 62837736Smckusick allerror = error; 62937736Smckusick blocksreleased += blkcount; 63037736Smckusick } 6319165Ssam } 63231661Smckusick FREE(copy, M_TEMP); 63337736Smckusick *countp = blocksreleased; 63437736Smckusick return (allerror); 63524Sbill } 63624Sbill 63724Sbill /* 6384818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 6393617Sroot */ 6404818Swnj ilock(ip) 6414818Swnj register struct inode *ip; 6423617Sroot { 6433617Sroot 64437736Smckusick while (ip->i_flag & ILOCKED) { 64537736Smckusick ip->i_flag |= IWANT; 64639795Smckusick if (ip->i_spare0 == u.u_procp->p_pid) 64739795Smckusick panic("locking against myself"); 64839795Smckusick ip->i_spare1 = u.u_procp->p_pid; 64937736Smckusick (void) sleep((caddr_t)ip, PINOD); 65037736Smckusick } 65139795Smckusick ip->i_spare1 = 0; 65239795Smckusick ip->i_spare0 = u.u_procp->p_pid; 65339795Smckusick u.u_spare[0]++; 65437736Smckusick ip->i_flag |= ILOCKED; 6553617Sroot } 6563617Sroot 6573617Sroot /* 6584818Swnj * Unlock an inode. If WANT bit is on, wakeup. 6593617Sroot */ 6607118Smckusick iunlock(ip) 6614818Swnj register struct inode *ip; 6623617Sroot { 6633617Sroot 66437736Smckusick if ((ip->i_flag & ILOCKED) == 0) 66539676Smckusick vprint("iunlock: unlocked inode", ITOV(ip)); 66639795Smckusick ip->i_spare0 = 0; 66739795Smckusick u.u_spare[0]--; 66837736Smckusick ip->i_flag &= ~ILOCKED; 66937736Smckusick if (ip->i_flag&IWANT) { 67037736Smckusick ip->i_flag &= ~IWANT; 67137736Smckusick wakeup((caddr_t)ip); 67237736Smckusick } 6733617Sroot } 674