123399Smckusick /* 237736Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337736Smckusick * All rights reserved. 423399Smckusick * 537736Smckusick * Redistribution and use in source and binary forms are permitted 637736Smckusick * provided that the above copyright notice and this paragraph are 737736Smckusick * duplicated in all such forms and that any documentation, 837736Smckusick * advertising materials, and other materials related to such 937736Smckusick * distribution and use acknowledge that the software was developed 1037736Smckusick * by the University of California, Berkeley. The name of the 1137736Smckusick * University may not be used to endorse or promote products derived 1237736Smckusick * from this software without specific prior written permission. 1337736Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437736Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537736Smckusick * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637736Smckusick * 17*39816Smckusick * @(#)lfs_inode.c 7.26 (Berkeley) 12/30/89 1823399Smckusick */ 1924Sbill 2017099Sbloom #include "param.h" 2117099Sbloom #include "systm.h" 2217099Sbloom #include "mount.h" 2317099Sbloom #include "user.h" 2439795Smckusick #include "proc.h" 2537736Smckusick #include "file.h" 2617099Sbloom #include "buf.h" 2724525Sbloom #include "cmap.h" 2837736Smckusick #include "vnode.h" 2937736Smckusick #include "../ufs/inode.h" 3037736Smckusick #include "../ufs/fs.h" 3137736Smckusick #include "../ufs/ufsmount.h" 327651Ssam #ifdef QUOTA 3337736Smckusick #include "../ufs/quota.h" 347504Sroot #endif 3517099Sbloom #include "kernel.h" 3631661Smckusick #include "malloc.h" 3724Sbill 3816840Smckusick #define INOHSZ 512 397334Skre #if ((INOHSZ&(INOHSZ-1)) == 0) 407334Skre #define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1)) 417334Skre #else 4210852Ssam #define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ) 437334Skre #endif 4424Sbill 4539392Smckusick union ihead { 467334Skre union ihead *ih_head[2]; 477334Skre struct inode *ih_chain[2]; 487334Skre } ihead[INOHSZ]; 497334Skre 5039574Smckusick int prtactive; /* 1 => print out reclaim of active vnodes */ 5139574Smckusick 5224Sbill /* 5339392Smckusick * Initialize hash links for inodes. 5424Sbill */ 5539440Smckusick ufs_init() 5624Sbill { 5724Sbill register int i; 5839392Smckusick register union ihead *ih = ihead; 5924Sbill 6039492Smckusick #ifndef lint 6139392Smckusick if (VN_MAXPRIVATE < sizeof(struct inode)) 6239392Smckusick panic("ihinit: too small"); 6339492Smckusick #endif /* not lint */ 647334Skre for (i = INOHSZ; --i >= 0; ih++) { 657334Skre ih->ih_head[0] = ih; 667334Skre ih->ih_head[1] = ih; 677334Skre } 6824Sbill } 6924Sbill 7024Sbill /* 7137736Smckusick * Look up an vnode/inode by device,inumber. 7224Sbill * If it is in core (in the inode structure), 7324Sbill * honor the locking protocol. 7424Sbill * If it is not in core, read it in from the 7524Sbill * specified device. 7637736Smckusick * Callers must check for mount points!! 7724Sbill * In all cases, a pointer to a locked 7824Sbill * inode structure is returned. 7924Sbill */ 8037736Smckusick iget(xp, ino, ipp) 8137736Smckusick struct inode *xp; 824818Swnj ino_t ino; 8337736Smckusick struct inode **ipp; 8424Sbill { 8537736Smckusick dev_t dev = xp->i_dev; 8637736Smckusick struct mount *mntp = ITOV(xp)->v_mount; 8737736Smckusick register struct fs *fs = VFSTOUFS(mntp)->um_fs; 8839440Smckusick extern struct vnodeops ufs_vnodeops, spec_inodeops; 8937736Smckusick register struct inode *ip, *iq; 9037736Smckusick register struct vnode *vp; 9139440Smckusick struct vnode *nvp; 9237736Smckusick struct buf *bp; 9339440Smckusick struct dinode *dp; 9437736Smckusick union ihead *ih; 9537736Smckusick int error; 9624Sbill 9739440Smckusick ih = &ihead[INOHASH(dev, ino)]; 9824Sbill loop: 9939392Smckusick for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) { 10039392Smckusick if (ino != ip->i_number || dev != ip->i_dev) 10139392Smckusick continue; 10239392Smckusick if ((ip->i_flag&ILOCKED) != 0) { 10339392Smckusick ip->i_flag |= IWANT; 10439392Smckusick sleep((caddr_t)ip, PINOD); 10539392Smckusick goto loop; 10639392Smckusick } 10739440Smckusick if (vget(ITOV(ip))) 10839440Smckusick goto loop; 10939392Smckusick *ipp = ip; 11039392Smckusick return(0); 11139392Smckusick } 11239440Smckusick /* 11339440Smckusick * Allocate a new inode. 11439440Smckusick */ 11539440Smckusick if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) { 11637736Smckusick *ipp = 0; 11737736Smckusick return (error); 11837736Smckusick } 11939440Smckusick ip = VTOI(nvp); 12039440Smckusick ip->i_vnode = nvp; 12139440Smckusick ip->i_flag = 0; 12239440Smckusick ip->i_devvp = 0; 12339440Smckusick ip->i_mode = 0; 12439440Smckusick #ifdef QUOTA 12539440Smckusick ip->i_dquot = NODQUOT; 12639440Smckusick #endif 12737736Smckusick /* 12839440Smckusick * Put it onto its hash chain and lock it so that other requests for 12939440Smckusick * this inode will block if they arrive while we are sleeping waiting 13039440Smckusick * for old data structures to be purged or for the contents of the 13139440Smckusick * disk portion of this inode to be read. 13239440Smckusick */ 13339440Smckusick ip->i_dev = dev; 13439440Smckusick ip->i_number = ino; 13539440Smckusick insque(ip, ih); 13639440Smckusick ILOCK(ip); 13739440Smckusick /* 13837736Smckusick * Read in the disk contents for the inode. 13937736Smckusick */ 14037736Smckusick if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)), 14138776Smckusick (int)fs->fs_bsize, NOCRED, &bp)) { 14237736Smckusick /* 14339392Smckusick * Unlock and discard unneeded inode. 14437736Smckusick */ 14539440Smckusick iput(ip); 14637736Smckusick brelse(bp); 14737736Smckusick *ipp = 0; 14839440Smckusick return (error); 14937736Smckusick } 15039440Smckusick dp = bp->b_un.b_dino; 15139440Smckusick dp += itoo(fs, ino); 15239440Smckusick ip->i_din = *dp; 15339440Smckusick brelse(bp); 15437736Smckusick /* 15539440Smckusick * Initialize the associated vnode 15637736Smckusick */ 15739440Smckusick vp = ITOV(ip); 15839440Smckusick vp->v_type = IFTOVT(ip->i_mode); 15939440Smckusick if (vp->v_type == VCHR || vp->v_type == VBLK) { 16039440Smckusick vp->v_op = &spec_inodeops; 16139617Smckusick if (nvp = checkalias(vp, ip->i_rdev, mntp)) { 16237736Smckusick /* 16339440Smckusick * Reinitialize aliased inode. 16437736Smckusick */ 16539440Smckusick vp = nvp; 16639440Smckusick iq = VTOI(vp); 16739440Smckusick iq->i_vnode = vp; 16839517Smckusick iq->i_flag = 0; 16939440Smckusick ILOCK(iq); 17039440Smckusick iq->i_din = ip->i_din; 17139440Smckusick iq->i_dev = dev; 17239440Smckusick iq->i_number = ino; 17339440Smckusick insque(iq, ih); 17437736Smckusick /* 17539440Smckusick * Discard unneeded vnode 17637736Smckusick */ 17739440Smckusick ip->i_mode = 0; 17839440Smckusick iput(ip); 17937736Smckusick ip = iq; 18037736Smckusick } 18137736Smckusick } 18239440Smckusick if (ino == ROOTINO) 18339440Smckusick vp->v_flag |= VROOT; 18437736Smckusick /* 18537736Smckusick * Finish inode initialization. 18637736Smckusick */ 18737736Smckusick ip->i_fs = fs; 18837736Smckusick ip->i_devvp = VFSTOUFS(mntp)->um_devvp; 18938345Smckusick VREF(ip->i_devvp); 19037736Smckusick #ifdef QUOTA 19137736Smckusick if (ip->i_mode != 0) 19237736Smckusick ip->i_dquot = inoquota(ip); 19337736Smckusick #endif 19438256Smckusick /* 19538256Smckusick * Set up a generation number for this inode if it does not 19638256Smckusick * already have one. This should only happen on old filesystems. 19738256Smckusick */ 19838256Smckusick if (ip->i_gen == 0) { 19938256Smckusick if (++nextgennumber < (u_long)time.tv_sec) 20038256Smckusick nextgennumber = time.tv_sec; 20138256Smckusick ip->i_gen = nextgennumber; 20238256Smckusick if ((vp->v_mount->m_flag & M_RDONLY) == 0) 20338256Smckusick ip->i_flag |= IMOD; 20438256Smckusick } 20537736Smckusick *ipp = ip; 20637736Smckusick return (0); 20737736Smckusick } 2087334Skre 20937736Smckusick /* 21039392Smckusick * Unlock and decrement the reference count of an inode structure. 21124Sbill */ 21224Sbill iput(ip) 2134818Swnj register struct inode *ip; 21424Sbill { 2157118Smckusick 2168452Sroot if ((ip->i_flag & ILOCKED) == 0) 2177118Smckusick panic("iput"); 21816665Smckusick IUNLOCK(ip); 21937736Smckusick vrele(ITOV(ip)); 2207118Smckusick } 2217118Smckusick 22239392Smckusick /* 22339392Smckusick * Last reference to an inode, write the inode out and if necessary, 22439392Smckusick * truncate and deallocate the file. 22539392Smckusick */ 22637736Smckusick ufs_inactive(vp) 22737736Smckusick struct vnode *vp; 2287118Smckusick { 22937736Smckusick register struct inode *ip = VTOI(vp); 23039392Smckusick int mode, error = 0; 23124Sbill 232*39816Smckusick if (prtactive && vp->v_usecount != 0) 23339676Smckusick vprint("ufs_inactive: pushing active", vp); 23438452Smckusick /* 23538452Smckusick * Get rid of inodes related to stale file handles. 23638452Smckusick */ 23739440Smckusick if (ip->i_mode == 0) { 23839676Smckusick if ((vp->v_flag & VXLOCK) == 0) 23939676Smckusick vgone(vp); 24039440Smckusick return (0); 24139440Smckusick } 24238226Smckusick ILOCK(ip); 24339364Smckusick if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) { 24439676Smckusick error = itrunc(ip, (u_long)0, 0); 24537736Smckusick mode = ip->i_mode; 24637736Smckusick ip->i_mode = 0; 24737736Smckusick ip->i_rdev = 0; 24837736Smckusick ip->i_flag |= IUPD|ICHG; 24937736Smckusick ifree(ip, ip->i_number, mode); 2507651Ssam #ifdef QUOTA 25137736Smckusick (void) chkiq(ip->i_dev, ip, ip->i_uid, 0); 25237736Smckusick dqrele(ip->i_dquot); 25337736Smckusick ip->i_dquot = NODQUOT; 2547492Skre #endif 25537736Smckusick } 25637736Smckusick IUPDAT(ip, &time, &time, 0); 25737736Smckusick /* 25839392Smckusick * If we are done with the inode, reclaim it 25939392Smckusick * so that it can be reused immediately. 26037736Smckusick */ 261*39816Smckusick if (vp->v_usecount == 0 && ip->i_mode == 0) { 26239676Smckusick vinvalbuf(vp, 0); 26339676Smckusick IUNLOCK(ip); 26439676Smckusick ip->i_flag = 0; 26539676Smckusick if ((vp->v_flag & VXLOCK) == 0) 26639676Smckusick vgone(vp); 26739676Smckusick return (error); 26839676Smckusick } 26939676Smckusick IUNLOCK(ip); 27039676Smckusick ip->i_flag = 0; 27137736Smckusick return (error); 27224Sbill } 27324Sbill 27424Sbill /* 27539392Smckusick * Reclaim an inode so that it can be used for other purposes. 27624Sbill */ 27739392Smckusick ufs_reclaim(vp) 27839392Smckusick register struct vnode *vp; 27939392Smckusick { 28039492Smckusick register struct inode *ip = VTOI(vp); 28139392Smckusick 282*39816Smckusick if (prtactive && vp->v_usecount != 0) 28339676Smckusick vprint("ufs_reclaim: pushing active", vp); 28439392Smckusick /* 28539392Smckusick * Remove the inode from its hash chain. 28639392Smckusick */ 28739392Smckusick remque(ip); 28839392Smckusick ip->i_forw = ip; 28939392Smckusick ip->i_back = ip; 29039392Smckusick /* 29139392Smckusick * Purge old data structures associated with the inode. 29239392Smckusick */ 29339392Smckusick cache_purge(vp); 29439392Smckusick if (ip->i_devvp) { 29539392Smckusick vrele(ip->i_devvp); 29639392Smckusick ip->i_devvp = 0; 29739392Smckusick } 29839392Smckusick #ifdef QUOTA 29939392Smckusick dqrele(ip->i_dquot); 30039392Smckusick ip->i_dquot = NODQUOT; 30139392Smckusick #endif 30239392Smckusick ip->i_flag = 0; 30339392Smckusick return (0); 30439392Smckusick } 30539392Smckusick 30639392Smckusick /* 30739392Smckusick * Check accessed and update flags on an inode structure. 30839392Smckusick * If any is on, update the inode with the current time. 30939392Smckusick * If waitfor is given, then must ensure I/O order, 31039392Smckusick * so wait for write to complete. 31139392Smckusick */ 3121203Sbill iupdat(ip, ta, tm, waitfor) 3134818Swnj register struct inode *ip; 3148630Sroot struct timeval *ta, *tm; 3154818Swnj int waitfor; 31624Sbill { 31737736Smckusick struct buf *bp; 31837736Smckusick struct vnode *vp = ITOV(ip); 31924Sbill struct dinode *dp; 32030749Skarels register struct fs *fs; 32137736Smckusick int error; 32224Sbill 32330749Skarels fs = ip->i_fs; 32437736Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 32537736Smckusick return (0); 32637736Smckusick if (vp->v_mount->m_flag & M_RDONLY) 32737736Smckusick return (0); 32837736Smckusick error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), 32938776Smckusick (int)fs->fs_bsize, NOCRED, &bp); 33037736Smckusick if (error) { 33137736Smckusick brelse(bp); 33237736Smckusick return (error); 33324Sbill } 33437736Smckusick if (ip->i_flag&IACC) 33537736Smckusick ip->i_atime = ta->tv_sec; 33637736Smckusick if (ip->i_flag&IUPD) 33737736Smckusick ip->i_mtime = tm->tv_sec; 33837736Smckusick if (ip->i_flag&ICHG) 33937736Smckusick ip->i_ctime = time.tv_sec; 34037736Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 34137736Smckusick dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 34239392Smckusick *dp = ip->i_din; 34337736Smckusick if (waitfor) { 34437736Smckusick return (bwrite(bp)); 34537736Smckusick } else { 34637736Smckusick bdwrite(bp); 34737736Smckusick return (0); 34837736Smckusick } 34924Sbill } 35024Sbill 35110736Ssam #define SINGLE 0 /* index of single indirect block */ 35210736Ssam #define DOUBLE 1 /* index of double indirect block */ 35310736Ssam #define TRIPLE 2 /* index of triple indirect block */ 35424Sbill /* 35539392Smckusick * Truncate the inode ip to at most length size. Free affected disk 35639392Smckusick * blocks -- the blocks of the file are removed in reverse order. 35710736Ssam * 35810736Ssam * NB: triple indirect blocks are untested. 35924Sbill */ 36039676Smckusick itrunc(oip, length, flags) 36117942Smckusick register struct inode *oip; 3629165Ssam u_long length; 36339676Smckusick int flags; 36424Sbill { 3659165Ssam register daddr_t lastblock; 36626272Skarels daddr_t bn, lbn, lastiblock[NIADDR]; 3676569Smckusic register struct fs *fs; 36810736Ssam register struct inode *ip; 36917942Smckusick struct buf *bp; 37037736Smckusick int offset, osize, size, level; 37137736Smckusick long count, nblocks, blocksreleased = 0; 37217942Smckusick register int i; 37339676Smckusick int aflags, error, allerror; 37410736Ssam struct inode tip; 3759165Ssam 37613000Ssam if (oip->i_size <= length) { 37713000Ssam oip->i_flag |= ICHG|IUPD; 37837736Smckusick error = iupdat(oip, &time, &time, 1); 37937736Smckusick return (error); 38013000Ssam } 3811203Sbill /* 38210736Ssam * Calculate index into inode's block list of 38310736Ssam * last direct and indirect blocks (if any) 38410736Ssam * which we want to keep. Lastblock is -1 when 38510736Ssam * the file is truncated to 0. 3861203Sbill */ 38710736Ssam fs = oip->i_fs; 3889165Ssam lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 38910736Ssam lastiblock[SINGLE] = lastblock - NDADDR; 39010736Ssam lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 39110736Ssam lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 39212645Ssam nblocks = btodb(fs->fs_bsize); 3936569Smckusic /* 39417942Smckusick * Update the size of the file. If the file is not being 39517942Smckusick * truncated to a block boundry, the contents of the 39617942Smckusick * partial block following the end of the file must be 39717942Smckusick * zero'ed in case it ever become accessable again because 39817942Smckusick * of subsequent file growth. 39917942Smckusick */ 40017942Smckusick osize = oip->i_size; 40117942Smckusick offset = blkoff(fs, length); 40217942Smckusick if (offset == 0) { 40317942Smckusick oip->i_size = length; 40417942Smckusick } else { 40517942Smckusick lbn = lblkno(fs, length); 40639676Smckusick aflags = B_CLRBUF; 40739676Smckusick if (flags & IO_SYNC) 40839676Smckusick aflags |= B_SYNC; 40939676Smckusick if (error = balloc(oip, lbn, offset, &bp, aflags)) 41037736Smckusick return (error); 41117942Smckusick oip->i_size = length; 41217942Smckusick size = blksize(fs, oip, lbn); 41339676Smckusick bn = bp->b_blkno; 41430749Skarels count = howmany(size, CLBYTES); 41530749Skarels for (i = 0; i < count; i++) 41637736Smckusick munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 41726272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 41839676Smckusick brealloc(bp, size); 41939676Smckusick if (flags & IO_SYNC) 42039676Smckusick bwrite(bp); 42139676Smckusick else 42239676Smckusick bdwrite(bp); 42317942Smckusick } 42417942Smckusick /* 42517942Smckusick * Update file and block pointers 42610736Ssam * on disk before we start freeing blocks. 42710736Ssam * If we crash before free'ing blocks below, 42810736Ssam * the blocks will be returned to the free list. 42910736Ssam * lastiblock values are also normalized to -1 43010736Ssam * for calls to indirtrunc below. 4316569Smckusic */ 43210736Ssam tip = *oip; 43317942Smckusick tip.i_size = osize; 43410736Ssam for (level = TRIPLE; level >= SINGLE; level--) 43510736Ssam if (lastiblock[level] < 0) { 43610736Ssam oip->i_ib[level] = 0; 43710736Ssam lastiblock[level] = -1; 4389165Ssam } 43910736Ssam for (i = NDADDR - 1; i > lastblock; i--) 44010736Ssam oip->i_db[i] = 0; 44110736Ssam oip->i_flag |= ICHG|IUPD; 44239676Smckusick vinvalbuf(ITOV(oip), (length > 0)); 44339740Smckusick allerror = iupdat(oip, &time, &time, MNT_WAIT); 44410736Ssam 4456569Smckusic /* 44610736Ssam * Indirect blocks first. 4476569Smckusic */ 44817942Smckusick ip = &tip; 44910736Ssam for (level = TRIPLE; level >= SINGLE; level--) { 45010736Ssam bn = ip->i_ib[level]; 4519165Ssam if (bn != 0) { 45237736Smckusick error = indirtrunc(ip, bn, lastiblock[level], level, 45337736Smckusick &count); 45437736Smckusick if (error) 45537736Smckusick allerror = error; 45637736Smckusick blocksreleased += count; 45710736Ssam if (lastiblock[level] < 0) { 45810736Ssam ip->i_ib[level] = 0; 45931402Smckusick blkfree(ip, bn, (off_t)fs->fs_bsize); 46010736Ssam blocksreleased += nblocks; 46110736Ssam } 46210736Ssam } 46310736Ssam if (lastiblock[level] >= 0) 46410736Ssam goto done; 4659165Ssam } 46610736Ssam 4676569Smckusic /* 46810736Ssam * All whole direct blocks or frags. 4696569Smckusic */ 4709165Ssam for (i = NDADDR - 1; i > lastblock; i--) { 47126359Skarels register off_t bsize; 4729165Ssam 4736569Smckusic bn = ip->i_db[i]; 4749165Ssam if (bn == 0) 47524Sbill continue; 4769165Ssam ip->i_db[i] = 0; 47724525Sbloom bsize = (off_t)blksize(fs, ip, i); 47831402Smckusick blkfree(ip, bn, bsize); 47924525Sbloom blocksreleased += btodb(bsize); 48024Sbill } 48110736Ssam if (lastblock < 0) 48210736Ssam goto done; 48310736Ssam 4841203Sbill /* 4859165Ssam * Finally, look for a change in size of the 4869165Ssam * last direct block; release any frags. 4871203Sbill */ 48810736Ssam bn = ip->i_db[lastblock]; 48910736Ssam if (bn != 0) { 49026359Skarels off_t oldspace, newspace; 49110736Ssam 4929165Ssam /* 4939165Ssam * Calculate amount of space we're giving 4949165Ssam * back as old block size minus new block size. 4959165Ssam */ 49610736Ssam oldspace = blksize(fs, ip, lastblock); 4979165Ssam ip->i_size = length; 49810736Ssam newspace = blksize(fs, ip, lastblock); 49910736Ssam if (newspace == 0) 50010736Ssam panic("itrunc: newspace"); 50110736Ssam if (oldspace - newspace > 0) { 5029165Ssam /* 5039165Ssam * Block number of space to be free'd is 5049165Ssam * the old block # plus the number of frags 5059165Ssam * required for the storage we're keeping. 5069165Ssam */ 50710736Ssam bn += numfrags(fs, newspace); 50831402Smckusick blkfree(ip, bn, oldspace - newspace); 50912645Ssam blocksreleased += btodb(oldspace - newspace); 5109165Ssam } 5119165Ssam } 5129165Ssam done: 51310736Ssam /* BEGIN PARANOIA */ 51410736Ssam for (level = SINGLE; level <= TRIPLE; level++) 51510736Ssam if (ip->i_ib[level] != oip->i_ib[level]) 51610736Ssam panic("itrunc1"); 51710736Ssam for (i = 0; i < NDADDR; i++) 51810736Ssam if (ip->i_db[i] != oip->i_db[i]) 51910736Ssam panic("itrunc2"); 52010736Ssam /* END PARANOIA */ 52112645Ssam oip->i_blocks -= blocksreleased; 52212645Ssam if (oip->i_blocks < 0) /* sanity */ 52312645Ssam oip->i_blocks = 0; 52412645Ssam oip->i_flag |= ICHG; 5259165Ssam #ifdef QUOTA 52612645Ssam (void) chkdq(oip, -blocksreleased, 0); 5279165Ssam #endif 52837736Smckusick return (allerror); 52924Sbill } 53024Sbill 5319165Ssam /* 5329165Ssam * Release blocks associated with the inode ip and 5339165Ssam * stored in the indirect block bn. Blocks are free'd 5349165Ssam * in LIFO order up to (but not including) lastbn. If 53510736Ssam * level is greater than SINGLE, the block is an indirect 53610736Ssam * block and recursive calls to indirtrunc must be used to 53710736Ssam * cleanse other indirect blocks. 53810736Ssam * 53910736Ssam * NB: triple indirect blocks are untested. 5409165Ssam */ 54137736Smckusick indirtrunc(ip, bn, lastbn, level, countp) 5426569Smckusic register struct inode *ip; 5439165Ssam daddr_t bn, lastbn; 54410736Ssam int level; 54537736Smckusick long *countp; 54624Sbill { 5479165Ssam register int i; 54831661Smckusick struct buf *bp; 54931661Smckusick register struct fs *fs = ip->i_fs; 55024Sbill register daddr_t *bap; 55131661Smckusick daddr_t *copy, nb, last; 55237736Smckusick long blkcount, factor; 55337736Smckusick int nblocks, blocksreleased = 0; 55437736Smckusick int error, allerror = 0; 55524Sbill 55610736Ssam /* 55710736Ssam * Calculate index in current block of last 55810736Ssam * block to be kept. -1 indicates the entire 55910736Ssam * block so we need not calculate the index. 56010736Ssam */ 56110736Ssam factor = 1; 56210736Ssam for (i = SINGLE; i < level; i++) 56310736Ssam factor *= NINDIR(fs); 5649165Ssam last = lastbn; 56510736Ssam if (lastbn > 0) 56610736Ssam last /= factor; 56712645Ssam nblocks = btodb(fs->fs_bsize); 56810736Ssam /* 56910736Ssam * Get buffer of block pointers, zero those 57010736Ssam * entries corresponding to blocks to be free'd, 57110736Ssam * and update on disk copy first. 57210736Ssam */ 57338776Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, 57438776Smckusick NOCRED, &bp); 57537736Smckusick if (error) { 57610736Ssam brelse(bp); 57737736Smckusick *countp = 0; 57837736Smckusick return (error); 57910736Ssam } 58039676Smckusick if ((bp->b_flags & B_CACHE) == 0) 58139676Smckusick reassignbuf(bp, ITOV(ip)); 58210736Ssam bap = bp->b_un.b_daddr; 58331661Smckusick MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 58431661Smckusick bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 58510736Ssam bzero((caddr_t)&bap[last + 1], 58610736Ssam (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 58739676Smckusick if (last == -1) 58839676Smckusick bp->b_flags |= B_INVAL; 58937736Smckusick error = bwrite(bp); 59037736Smckusick if (error) 59137736Smckusick allerror = error; 59231661Smckusick bap = copy; 59310736Ssam 59410736Ssam /* 59510736Ssam * Recursively free totally unused blocks. 59610736Ssam */ 5979165Ssam for (i = NINDIR(fs) - 1; i > last; i--) { 59824Sbill nb = bap[i]; 5999165Ssam if (nb == 0) 60024Sbill continue; 60137736Smckusick if (level > SINGLE) { 60237736Smckusick error = indirtrunc(ip, nb, (daddr_t)-1, level - 1, 60337736Smckusick &blkcount); 60437736Smckusick if (error) 60537736Smckusick allerror = error; 60637736Smckusick blocksreleased += blkcount; 60737736Smckusick } 60831402Smckusick blkfree(ip, nb, (off_t)fs->fs_bsize); 6099165Ssam blocksreleased += nblocks; 61024Sbill } 61110736Ssam 61210736Ssam /* 61310736Ssam * Recursively free last partial block. 61410736Ssam */ 61510736Ssam if (level > SINGLE && lastbn >= 0) { 61610736Ssam last = lastbn % factor; 6179165Ssam nb = bap[i]; 61837736Smckusick if (nb != 0) { 61937736Smckusick error = indirtrunc(ip, nb, last, level - 1, &blkcount); 62037736Smckusick if (error) 62137736Smckusick allerror = error; 62237736Smckusick blocksreleased += blkcount; 62337736Smckusick } 6249165Ssam } 62531661Smckusick FREE(copy, M_TEMP); 62637736Smckusick *countp = blocksreleased; 62737736Smckusick return (allerror); 62824Sbill } 62924Sbill 63024Sbill /* 6314818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 6323617Sroot */ 6334818Swnj ilock(ip) 6344818Swnj register struct inode *ip; 6353617Sroot { 6363617Sroot 63737736Smckusick while (ip->i_flag & ILOCKED) { 63837736Smckusick ip->i_flag |= IWANT; 63939795Smckusick if (ip->i_spare0 == u.u_procp->p_pid) 64039795Smckusick panic("locking against myself"); 64139795Smckusick ip->i_spare1 = u.u_procp->p_pid; 64237736Smckusick (void) sleep((caddr_t)ip, PINOD); 64337736Smckusick } 64439795Smckusick ip->i_spare1 = 0; 64539795Smckusick ip->i_spare0 = u.u_procp->p_pid; 64639795Smckusick u.u_spare[0]++; 64737736Smckusick ip->i_flag |= ILOCKED; 6483617Sroot } 6493617Sroot 6503617Sroot /* 6514818Swnj * Unlock an inode. If WANT bit is on, wakeup. 6523617Sroot */ 6537118Smckusick iunlock(ip) 6544818Swnj register struct inode *ip; 6553617Sroot { 6563617Sroot 65737736Smckusick if ((ip->i_flag & ILOCKED) == 0) 65839676Smckusick vprint("iunlock: unlocked inode", ITOV(ip)); 65939795Smckusick ip->i_spare0 = 0; 66039795Smckusick u.u_spare[0]--; 66137736Smckusick ip->i_flag &= ~ILOCKED; 66237736Smckusick if (ip->i_flag&IWANT) { 66337736Smckusick ip->i_flag &= ~IWANT; 66437736Smckusick wakeup((caddr_t)ip); 66537736Smckusick } 6663617Sroot } 66737736Smckusick 66837736Smckusick /* 66937736Smckusick * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. 67037736Smckusick * The mode is shifted to select the owner/group/other fields. The 67137736Smckusick * super user is granted all permissions. 67237736Smckusick * 67337736Smckusick * NB: Called from vnode op table. It seems this could all be done 67437736Smckusick * using vattr's but... 67537736Smckusick */ 67637736Smckusick iaccess(ip, mode, cred) 67737736Smckusick register struct inode *ip; 67837736Smckusick register int mode; 67937736Smckusick struct ucred *cred; 68037736Smckusick { 68137736Smckusick register gid_t *gp; 68237736Smckusick int i; 68337736Smckusick 68437736Smckusick /* 68539392Smckusick * If you're the super-user, you always get access. 68637736Smckusick */ 68737736Smckusick if (cred->cr_uid == 0) 68837736Smckusick return (0); 68937736Smckusick /* 69037736Smckusick * Access check is based on only one of owner, group, public. 69137736Smckusick * If not owner, then check group. If not a member of the 69237736Smckusick * group, then check public access. 69337736Smckusick */ 69437736Smckusick if (cred->cr_uid != ip->i_uid) { 69537736Smckusick mode >>= 3; 69637736Smckusick gp = cred->cr_groups; 69737736Smckusick for (i = 0; i < cred->cr_ngroups; i++, gp++) 69837736Smckusick if (ip->i_gid == *gp) 69937736Smckusick goto found; 70037736Smckusick mode >>= 3; 70137736Smckusick found: 70237736Smckusick ; 70337736Smckusick } 70437736Smckusick if ((ip->i_mode & mode) != 0) 70537736Smckusick return (0); 70637736Smckusick return (EACCES); 70737736Smckusick } 708