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*39676Smckusick * @(#)lfs_inode.c 7.23 (Berkeley) 11/30/89 1823399Smckusick */ 1924Sbill 2017099Sbloom #include "param.h" 2117099Sbloom #include "systm.h" 2217099Sbloom #include "mount.h" 2317099Sbloom #include "user.h" 2437736Smckusick #include "file.h" 2517099Sbloom #include "buf.h" 2624525Sbloom #include "cmap.h" 2737736Smckusick #include "vnode.h" 2837736Smckusick #include "../ufs/inode.h" 2937736Smckusick #include "../ufs/fs.h" 3037736Smckusick #include "../ufs/ufsmount.h" 317651Ssam #ifdef QUOTA 3237736Smckusick #include "../ufs/quota.h" 337504Sroot #endif 3417099Sbloom #include "kernel.h" 3531661Smckusick #include "malloc.h" 3624Sbill 3716840Smckusick #define INOHSZ 512 387334Skre #if ((INOHSZ&(INOHSZ-1)) == 0) 397334Skre #define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1)) 407334Skre #else 4110852Ssam #define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ) 427334Skre #endif 4324Sbill 4439392Smckusick union ihead { 457334Skre union ihead *ih_head[2]; 467334Skre struct inode *ih_chain[2]; 477334Skre } ihead[INOHSZ]; 487334Skre 4939574Smckusick int prtactive; /* 1 => print out reclaim of active vnodes */ 5039574Smckusick 5124Sbill /* 5239392Smckusick * Initialize hash links for inodes. 5324Sbill */ 5439440Smckusick ufs_init() 5524Sbill { 5624Sbill register int i; 5739392Smckusick register union ihead *ih = ihead; 5824Sbill 5939492Smckusick #ifndef lint 6039392Smckusick if (VN_MAXPRIVATE < sizeof(struct inode)) 6139392Smckusick panic("ihinit: too small"); 6239492Smckusick #endif /* not lint */ 637334Skre for (i = INOHSZ; --i >= 0; ih++) { 647334Skre ih->ih_head[0] = ih; 657334Skre ih->ih_head[1] = ih; 667334Skre } 6724Sbill } 6824Sbill 6924Sbill /* 7037736Smckusick * Look up an vnode/inode by device,inumber. 7124Sbill * If it is in core (in the inode structure), 7224Sbill * honor the locking protocol. 7324Sbill * If it is not in core, read it in from the 7424Sbill * specified device. 7537736Smckusick * Callers must check for mount points!! 7624Sbill * In all cases, a pointer to a locked 7724Sbill * inode structure is returned. 7824Sbill */ 7937736Smckusick iget(xp, ino, ipp) 8037736Smckusick struct inode *xp; 814818Swnj ino_t ino; 8237736Smckusick struct inode **ipp; 8324Sbill { 8437736Smckusick dev_t dev = xp->i_dev; 8537736Smckusick struct mount *mntp = ITOV(xp)->v_mount; 8637736Smckusick register struct fs *fs = VFSTOUFS(mntp)->um_fs; 8739440Smckusick extern struct vnodeops ufs_vnodeops, spec_inodeops; 8837736Smckusick register struct inode *ip, *iq; 8937736Smckusick register struct vnode *vp; 9039440Smckusick struct vnode *nvp; 9137736Smckusick struct buf *bp; 9239440Smckusick struct dinode *dp; 9337736Smckusick union ihead *ih; 9437736Smckusick int error; 9524Sbill 9639440Smckusick ih = &ihead[INOHASH(dev, ino)]; 9724Sbill loop: 9839392Smckusick for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) { 9939392Smckusick if (ino != ip->i_number || dev != ip->i_dev) 10039392Smckusick continue; 10139392Smckusick if ((ip->i_flag&ILOCKED) != 0) { 10239392Smckusick ip->i_flag |= IWANT; 10339392Smckusick sleep((caddr_t)ip, PINOD); 10439392Smckusick goto loop; 10539392Smckusick } 10639440Smckusick if (vget(ITOV(ip))) 10739440Smckusick goto loop; 10839392Smckusick *ipp = ip; 10939392Smckusick return(0); 11039392Smckusick } 11139440Smckusick /* 11239440Smckusick * Allocate a new inode. 11339440Smckusick */ 11439440Smckusick if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) { 11537736Smckusick *ipp = 0; 11637736Smckusick return (error); 11737736Smckusick } 11839440Smckusick ip = VTOI(nvp); 11939440Smckusick ip->i_vnode = nvp; 12039440Smckusick ip->i_flag = 0; 12139440Smckusick ip->i_devvp = 0; 12239440Smckusick ip->i_lastr = 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; 16839440Smckusick iq->i_lastr = 0; 16939517Smckusick iq->i_flag = 0; 17039440Smckusick ILOCK(iq); 17139440Smckusick iq->i_din = ip->i_din; 17239440Smckusick iq->i_dev = dev; 17339440Smckusick iq->i_number = ino; 17439440Smckusick insque(iq, ih); 17537736Smckusick /* 17639440Smckusick * Discard unneeded vnode 17737736Smckusick */ 17839440Smckusick ip->i_mode = 0; 17939440Smckusick iput(ip); 18037736Smckusick ip = iq; 18137736Smckusick } 18237736Smckusick } 18339440Smckusick if (ino == ROOTINO) 18439440Smckusick vp->v_flag |= VROOT; 18537736Smckusick /* 18637736Smckusick * Finish inode initialization. 18737736Smckusick */ 18837736Smckusick ip->i_fs = fs; 18937736Smckusick ip->i_devvp = VFSTOUFS(mntp)->um_devvp; 19038345Smckusick VREF(ip->i_devvp); 19137736Smckusick #ifdef QUOTA 19237736Smckusick if (ip->i_mode != 0) 19337736Smckusick ip->i_dquot = inoquota(ip); 19437736Smckusick #endif 19538256Smckusick /* 19638256Smckusick * Set up a generation number for this inode if it does not 19738256Smckusick * already have one. This should only happen on old filesystems. 19838256Smckusick */ 19938256Smckusick if (ip->i_gen == 0) { 20038256Smckusick if (++nextgennumber < (u_long)time.tv_sec) 20138256Smckusick nextgennumber = time.tv_sec; 20238256Smckusick ip->i_gen = nextgennumber; 20338256Smckusick if ((vp->v_mount->m_flag & M_RDONLY) == 0) 20438256Smckusick ip->i_flag |= IMOD; 20538256Smckusick } 20637736Smckusick *ipp = ip; 20737736Smckusick return (0); 20837736Smckusick } 2097334Skre 21037736Smckusick /* 21139392Smckusick * Unlock and decrement the reference count of an inode structure. 21224Sbill */ 21324Sbill iput(ip) 2144818Swnj register struct inode *ip; 21524Sbill { 2167118Smckusick 2178452Sroot if ((ip->i_flag & ILOCKED) == 0) 2187118Smckusick panic("iput"); 21916665Smckusick IUNLOCK(ip); 22037736Smckusick vrele(ITOV(ip)); 2217118Smckusick } 2227118Smckusick 22339392Smckusick /* 22439392Smckusick * Last reference to an inode, write the inode out and if necessary, 22539392Smckusick * truncate and deallocate the file. 22639392Smckusick */ 22737736Smckusick ufs_inactive(vp) 22837736Smckusick struct vnode *vp; 2297118Smckusick { 23037736Smckusick register struct inode *ip = VTOI(vp); 23139392Smckusick int mode, error = 0; 23224Sbill 23339574Smckusick if (prtactive && vp->v_count != 0) 234*39676Smckusick vprint("ufs_inactive: pushing active", vp); 23538452Smckusick /* 23638452Smckusick * Get rid of inodes related to stale file handles. 23738452Smckusick */ 23839440Smckusick if (ip->i_mode == 0) { 239*39676Smckusick if ((vp->v_flag & VXLOCK) == 0) 240*39676Smckusick vgone(vp); 24139440Smckusick return (0); 24239440Smckusick } 24338226Smckusick ILOCK(ip); 24439364Smckusick if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) { 245*39676Smckusick error = itrunc(ip, (u_long)0, 0); 24637736Smckusick mode = ip->i_mode; 24737736Smckusick ip->i_mode = 0; 24837736Smckusick ip->i_rdev = 0; 24937736Smckusick ip->i_flag |= IUPD|ICHG; 25037736Smckusick ifree(ip, ip->i_number, mode); 2517651Ssam #ifdef QUOTA 25237736Smckusick (void) chkiq(ip->i_dev, ip, ip->i_uid, 0); 25337736Smckusick dqrele(ip->i_dquot); 25437736Smckusick ip->i_dquot = NODQUOT; 2557492Skre #endif 25637736Smckusick } 25737736Smckusick IUPDAT(ip, &time, &time, 0); 25837736Smckusick /* 25939392Smckusick * If we are done with the inode, reclaim it 26039392Smckusick * so that it can be reused immediately. 26137736Smckusick */ 262*39676Smckusick if (vp->v_count == 0 && ip->i_mode == 0) { 263*39676Smckusick vinvalbuf(vp, 0); 264*39676Smckusick IUNLOCK(ip); 265*39676Smckusick ip->i_flag = 0; 266*39676Smckusick if ((vp->v_flag & VXLOCK) == 0) 267*39676Smckusick vgone(vp); 268*39676Smckusick return (error); 269*39676Smckusick } 270*39676Smckusick IUNLOCK(ip); 271*39676Smckusick ip->i_flag = 0; 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); 28239392Smckusick 28339574Smckusick if (prtactive && vp->v_count != 0) 284*39676Smckusick vprint("ufs_reclaim: pushing active", vp); 28539392Smckusick /* 28639392Smckusick * Remove the inode from its hash chain. 28739392Smckusick */ 28839392Smckusick remque(ip); 28939392Smckusick ip->i_forw = ip; 29039392Smckusick ip->i_back = ip; 29139392Smckusick /* 29239392Smckusick * Purge old data structures associated with the inode. 29339392Smckusick */ 29439392Smckusick cache_purge(vp); 29539392Smckusick if (ip->i_devvp) { 29639392Smckusick vrele(ip->i_devvp); 29739392Smckusick ip->i_devvp = 0; 29839392Smckusick } 29939392Smckusick #ifdef QUOTA 30039392Smckusick dqrele(ip->i_dquot); 30139392Smckusick ip->i_dquot = NODQUOT; 30239392Smckusick #endif 30339392Smckusick ip->i_flag = 0; 30439392Smckusick return (0); 30539392Smckusick } 30639392Smckusick 30739392Smckusick /* 30839392Smckusick * Check accessed and update flags on an inode structure. 30939392Smckusick * If any is on, update the inode with the current time. 31039392Smckusick * If waitfor is given, then must ensure I/O order, 31139392Smckusick * so wait for write to complete. 31239392Smckusick */ 3131203Sbill iupdat(ip, ta, tm, waitfor) 3144818Swnj register struct inode *ip; 3158630Sroot struct timeval *ta, *tm; 3164818Swnj int waitfor; 31724Sbill { 31837736Smckusick struct buf *bp; 31937736Smckusick struct vnode *vp = ITOV(ip); 32024Sbill struct dinode *dp; 32130749Skarels register struct fs *fs; 32237736Smckusick int error; 32324Sbill 32430749Skarels fs = ip->i_fs; 32537736Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 32637736Smckusick return (0); 32737736Smckusick if (vp->v_mount->m_flag & M_RDONLY) 32837736Smckusick return (0); 32937736Smckusick error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), 33038776Smckusick (int)fs->fs_bsize, NOCRED, &bp); 33137736Smckusick if (error) { 33237736Smckusick brelse(bp); 33337736Smckusick return (error); 33424Sbill } 33537736Smckusick if (ip->i_flag&IACC) 33637736Smckusick ip->i_atime = ta->tv_sec; 33737736Smckusick if (ip->i_flag&IUPD) 33837736Smckusick ip->i_mtime = tm->tv_sec; 33937736Smckusick if (ip->i_flag&ICHG) 34037736Smckusick ip->i_ctime = time.tv_sec; 34137736Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 34237736Smckusick dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 34339392Smckusick *dp = ip->i_din; 34437736Smckusick if (waitfor) { 34537736Smckusick return (bwrite(bp)); 34637736Smckusick } else { 34737736Smckusick bdwrite(bp); 34837736Smckusick return (0); 34937736Smckusick } 35024Sbill } 35124Sbill 35210736Ssam #define SINGLE 0 /* index of single indirect block */ 35310736Ssam #define DOUBLE 1 /* index of double indirect block */ 35410736Ssam #define TRIPLE 2 /* index of triple indirect block */ 35524Sbill /* 35639392Smckusick * Truncate the inode ip to at most length size. Free affected disk 35739392Smckusick * blocks -- the blocks of the file are removed in reverse order. 35810736Ssam * 35910736Ssam * NB: triple indirect blocks are untested. 36024Sbill */ 361*39676Smckusick itrunc(oip, length, flags) 36217942Smckusick register struct inode *oip; 3639165Ssam u_long length; 364*39676Smckusick int flags; 36524Sbill { 3669165Ssam register daddr_t lastblock; 36726272Skarels daddr_t bn, lbn, lastiblock[NIADDR]; 3686569Smckusic register struct fs *fs; 36910736Ssam register struct inode *ip; 37017942Smckusick struct buf *bp; 37137736Smckusick int offset, osize, size, level; 37237736Smckusick long count, nblocks, blocksreleased = 0; 37317942Smckusick register int i; 374*39676Smckusick int aflags, error, allerror; 37510736Ssam struct inode tip; 3769165Ssam 37713000Ssam if (oip->i_size <= length) { 37813000Ssam oip->i_flag |= ICHG|IUPD; 37937736Smckusick error = iupdat(oip, &time, &time, 1); 38037736Smckusick return (error); 38113000Ssam } 3821203Sbill /* 38310736Ssam * Calculate index into inode's block list of 38410736Ssam * last direct and indirect blocks (if any) 38510736Ssam * which we want to keep. Lastblock is -1 when 38610736Ssam * the file is truncated to 0. 3871203Sbill */ 38810736Ssam fs = oip->i_fs; 3899165Ssam lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 39010736Ssam lastiblock[SINGLE] = lastblock - NDADDR; 39110736Ssam lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 39210736Ssam lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 39312645Ssam nblocks = btodb(fs->fs_bsize); 3946569Smckusic /* 39517942Smckusick * Update the size of the file. If the file is not being 39617942Smckusick * truncated to a block boundry, the contents of the 39717942Smckusick * partial block following the end of the file must be 39817942Smckusick * zero'ed in case it ever become accessable again because 39917942Smckusick * of subsequent file growth. 40017942Smckusick */ 40117942Smckusick osize = oip->i_size; 40217942Smckusick offset = blkoff(fs, length); 40317942Smckusick if (offset == 0) { 40417942Smckusick oip->i_size = length; 40517942Smckusick } else { 40617942Smckusick lbn = lblkno(fs, length); 407*39676Smckusick aflags = B_CLRBUF; 408*39676Smckusick if (flags & IO_SYNC) 409*39676Smckusick aflags |= B_SYNC; 410*39676Smckusick if (error = balloc(oip, lbn, offset, &bp, aflags)) 41137736Smckusick return (error); 41217942Smckusick oip->i_size = length; 41317942Smckusick size = blksize(fs, oip, lbn); 414*39676Smckusick bn = bp->b_blkno; 41530749Skarels count = howmany(size, CLBYTES); 41630749Skarels for (i = 0; i < count; i++) 41737736Smckusick munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 41826272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 419*39676Smckusick brealloc(bp, size); 420*39676Smckusick if (flags & IO_SYNC) 421*39676Smckusick bwrite(bp); 422*39676Smckusick else 423*39676Smckusick bdwrite(bp); 42417942Smckusick } 42517942Smckusick /* 42617942Smckusick * Update file and block pointers 42710736Ssam * on disk before we start freeing blocks. 42810736Ssam * If we crash before free'ing blocks below, 42910736Ssam * the blocks will be returned to the free list. 43010736Ssam * lastiblock values are also normalized to -1 43110736Ssam * for calls to indirtrunc below. 4326569Smckusic */ 43310736Ssam tip = *oip; 43417942Smckusick tip.i_size = osize; 43510736Ssam for (level = TRIPLE; level >= SINGLE; level--) 43610736Ssam if (lastiblock[level] < 0) { 43710736Ssam oip->i_ib[level] = 0; 43810736Ssam lastiblock[level] = -1; 4399165Ssam } 44010736Ssam for (i = NDADDR - 1; i > lastblock; i--) 44110736Ssam oip->i_db[i] = 0; 44210736Ssam oip->i_flag |= ICHG|IUPD; 443*39676Smckusick vinvalbuf(ITOV(oip), (length > 0)); 444*39676Smckusick allerror = iupdat(ip, &time, &time, MNT_WAIT); 44510736Ssam 4466569Smckusic /* 44710736Ssam * Indirect blocks first. 4486569Smckusic */ 44917942Smckusick ip = &tip; 45010736Ssam for (level = TRIPLE; level >= SINGLE; level--) { 45110736Ssam bn = ip->i_ib[level]; 4529165Ssam if (bn != 0) { 45337736Smckusick error = indirtrunc(ip, bn, lastiblock[level], level, 45437736Smckusick &count); 45537736Smckusick if (error) 45637736Smckusick allerror = error; 45737736Smckusick blocksreleased += count; 45810736Ssam if (lastiblock[level] < 0) { 45910736Ssam ip->i_ib[level] = 0; 46031402Smckusick blkfree(ip, bn, (off_t)fs->fs_bsize); 46110736Ssam blocksreleased += nblocks; 46210736Ssam } 46310736Ssam } 46410736Ssam if (lastiblock[level] >= 0) 46510736Ssam goto done; 4669165Ssam } 46710736Ssam 4686569Smckusic /* 46910736Ssam * All whole direct blocks or frags. 4706569Smckusic */ 4719165Ssam for (i = NDADDR - 1; i > lastblock; i--) { 47226359Skarels register off_t bsize; 4739165Ssam 4746569Smckusic bn = ip->i_db[i]; 4759165Ssam if (bn == 0) 47624Sbill continue; 4779165Ssam ip->i_db[i] = 0; 47824525Sbloom bsize = (off_t)blksize(fs, ip, i); 47931402Smckusick blkfree(ip, bn, bsize); 48024525Sbloom blocksreleased += btodb(bsize); 48124Sbill } 48210736Ssam if (lastblock < 0) 48310736Ssam goto done; 48410736Ssam 4851203Sbill /* 4869165Ssam * Finally, look for a change in size of the 4879165Ssam * last direct block; release any frags. 4881203Sbill */ 48910736Ssam bn = ip->i_db[lastblock]; 49010736Ssam if (bn != 0) { 49126359Skarels off_t oldspace, newspace; 49210736Ssam 4939165Ssam /* 4949165Ssam * Calculate amount of space we're giving 4959165Ssam * back as old block size minus new block size. 4969165Ssam */ 49710736Ssam oldspace = blksize(fs, ip, lastblock); 4989165Ssam ip->i_size = length; 49910736Ssam newspace = blksize(fs, ip, lastblock); 50010736Ssam if (newspace == 0) 50110736Ssam panic("itrunc: newspace"); 50210736Ssam if (oldspace - newspace > 0) { 5039165Ssam /* 5049165Ssam * Block number of space to be free'd is 5059165Ssam * the old block # plus the number of frags 5069165Ssam * required for the storage we're keeping. 5079165Ssam */ 50810736Ssam bn += numfrags(fs, newspace); 50931402Smckusick blkfree(ip, bn, oldspace - newspace); 51012645Ssam blocksreleased += btodb(oldspace - newspace); 5119165Ssam } 5129165Ssam } 5139165Ssam done: 51410736Ssam /* BEGIN PARANOIA */ 51510736Ssam for (level = SINGLE; level <= TRIPLE; level++) 51610736Ssam if (ip->i_ib[level] != oip->i_ib[level]) 51710736Ssam panic("itrunc1"); 51810736Ssam for (i = 0; i < NDADDR; i++) 51910736Ssam if (ip->i_db[i] != oip->i_db[i]) 52010736Ssam panic("itrunc2"); 52110736Ssam /* END PARANOIA */ 52212645Ssam oip->i_blocks -= blocksreleased; 52312645Ssam if (oip->i_blocks < 0) /* sanity */ 52412645Ssam oip->i_blocks = 0; 52512645Ssam oip->i_flag |= ICHG; 5269165Ssam #ifdef QUOTA 52712645Ssam (void) chkdq(oip, -blocksreleased, 0); 5289165Ssam #endif 52937736Smckusick return (allerror); 53024Sbill } 53124Sbill 5329165Ssam /* 5339165Ssam * Release blocks associated with the inode ip and 5349165Ssam * stored in the indirect block bn. Blocks are free'd 5359165Ssam * in LIFO order up to (but not including) lastbn. If 53610736Ssam * level is greater than SINGLE, the block is an indirect 53710736Ssam * block and recursive calls to indirtrunc must be used to 53810736Ssam * cleanse other indirect blocks. 53910736Ssam * 54010736Ssam * NB: triple indirect blocks are untested. 5419165Ssam */ 54237736Smckusick indirtrunc(ip, bn, lastbn, level, countp) 5436569Smckusic register struct inode *ip; 5449165Ssam daddr_t bn, lastbn; 54510736Ssam int level; 54637736Smckusick long *countp; 54724Sbill { 5489165Ssam register int i; 54931661Smckusick struct buf *bp; 55031661Smckusick register struct fs *fs = ip->i_fs; 55124Sbill register daddr_t *bap; 55231661Smckusick daddr_t *copy, nb, last; 55337736Smckusick long blkcount, factor; 55437736Smckusick int nblocks, blocksreleased = 0; 55537736Smckusick int error, allerror = 0; 55624Sbill 55710736Ssam /* 55810736Ssam * Calculate index in current block of last 55910736Ssam * block to be kept. -1 indicates the entire 56010736Ssam * block so we need not calculate the index. 56110736Ssam */ 56210736Ssam factor = 1; 56310736Ssam for (i = SINGLE; i < level; i++) 56410736Ssam factor *= NINDIR(fs); 5659165Ssam last = lastbn; 56610736Ssam if (lastbn > 0) 56710736Ssam last /= factor; 56812645Ssam nblocks = btodb(fs->fs_bsize); 56910736Ssam /* 57010736Ssam * Get buffer of block pointers, zero those 57110736Ssam * entries corresponding to blocks to be free'd, 57210736Ssam * and update on disk copy first. 57310736Ssam */ 57438776Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, 57538776Smckusick NOCRED, &bp); 57637736Smckusick if (error) { 57710736Ssam brelse(bp); 57837736Smckusick *countp = 0; 57937736Smckusick return (error); 58010736Ssam } 581*39676Smckusick if ((bp->b_flags & B_CACHE) == 0) 582*39676Smckusick reassignbuf(bp, ITOV(ip)); 58310736Ssam bap = bp->b_un.b_daddr; 58431661Smckusick MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 58531661Smckusick bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 58610736Ssam bzero((caddr_t)&bap[last + 1], 58710736Ssam (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 588*39676Smckusick if (last == -1) 589*39676Smckusick bp->b_flags |= B_INVAL; 59037736Smckusick error = bwrite(bp); 59137736Smckusick if (error) 59237736Smckusick allerror = error; 59331661Smckusick bap = copy; 59410736Ssam 59510736Ssam /* 59610736Ssam * Recursively free totally unused blocks. 59710736Ssam */ 5989165Ssam for (i = NINDIR(fs) - 1; i > last; i--) { 59924Sbill nb = bap[i]; 6009165Ssam if (nb == 0) 60124Sbill continue; 60237736Smckusick if (level > SINGLE) { 60337736Smckusick error = indirtrunc(ip, nb, (daddr_t)-1, level - 1, 60437736Smckusick &blkcount); 60537736Smckusick if (error) 60637736Smckusick allerror = error; 60737736Smckusick blocksreleased += blkcount; 60837736Smckusick } 60931402Smckusick blkfree(ip, nb, (off_t)fs->fs_bsize); 6109165Ssam blocksreleased += nblocks; 61124Sbill } 61210736Ssam 61310736Ssam /* 61410736Ssam * Recursively free last partial block. 61510736Ssam */ 61610736Ssam if (level > SINGLE && lastbn >= 0) { 61710736Ssam last = lastbn % factor; 6189165Ssam nb = bap[i]; 61937736Smckusick if (nb != 0) { 62037736Smckusick error = indirtrunc(ip, nb, last, level - 1, &blkcount); 62137736Smckusick if (error) 62237736Smckusick allerror = error; 62337736Smckusick blocksreleased += blkcount; 62437736Smckusick } 6259165Ssam } 62631661Smckusick FREE(copy, M_TEMP); 62737736Smckusick *countp = blocksreleased; 62837736Smckusick return (allerror); 62924Sbill } 63024Sbill 63124Sbill /* 6324818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 6333617Sroot */ 6344818Swnj ilock(ip) 6354818Swnj register struct inode *ip; 6363617Sroot { 6373617Sroot 63837736Smckusick while (ip->i_flag & ILOCKED) { 63937736Smckusick ip->i_flag |= IWANT; 64037736Smckusick (void) sleep((caddr_t)ip, PINOD); 64137736Smckusick } 64237736Smckusick ip->i_flag |= ILOCKED; 6433617Sroot } 6443617Sroot 6453617Sroot /* 6464818Swnj * Unlock an inode. If WANT bit is on, wakeup. 6473617Sroot */ 6487118Smckusick iunlock(ip) 6494818Swnj register struct inode *ip; 6503617Sroot { 6513617Sroot 65237736Smckusick if ((ip->i_flag & ILOCKED) == 0) 653*39676Smckusick vprint("iunlock: unlocked inode", ITOV(ip)); 65437736Smckusick ip->i_flag &= ~ILOCKED; 65537736Smckusick if (ip->i_flag&IWANT) { 65637736Smckusick ip->i_flag &= ~IWANT; 65737736Smckusick wakeup((caddr_t)ip); 65837736Smckusick } 6593617Sroot } 66037736Smckusick 66137736Smckusick /* 66237736Smckusick * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. 66337736Smckusick * The mode is shifted to select the owner/group/other fields. The 66437736Smckusick * super user is granted all permissions. 66537736Smckusick * 66637736Smckusick * NB: Called from vnode op table. It seems this could all be done 66737736Smckusick * using vattr's but... 66837736Smckusick */ 66937736Smckusick iaccess(ip, mode, cred) 67037736Smckusick register struct inode *ip; 67137736Smckusick register int mode; 67237736Smckusick struct ucred *cred; 67337736Smckusick { 67437736Smckusick register gid_t *gp; 67537736Smckusick int i; 67637736Smckusick 67737736Smckusick /* 67839392Smckusick * If you're the super-user, you always get access. 67937736Smckusick */ 68037736Smckusick if (cred->cr_uid == 0) 68137736Smckusick return (0); 68237736Smckusick /* 68337736Smckusick * Access check is based on only one of owner, group, public. 68437736Smckusick * If not owner, then check group. If not a member of the 68537736Smckusick * group, then check public access. 68637736Smckusick */ 68737736Smckusick if (cred->cr_uid != ip->i_uid) { 68837736Smckusick mode >>= 3; 68937736Smckusick gp = cred->cr_groups; 69037736Smckusick for (i = 0; i < cred->cr_ngroups; i++, gp++) 69137736Smckusick if (ip->i_gid == *gp) 69237736Smckusick goto found; 69337736Smckusick mode >>= 3; 69437736Smckusick found: 69537736Smckusick ; 69637736Smckusick } 69737736Smckusick if ((ip->i_mode & mode) != 0) 69837736Smckusick return (0); 69937736Smckusick return (EACCES); 70037736Smckusick } 701