123399Smckusick /* 229117Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323399Smckusick * All rights reserved. The Berkeley software License Agreement 423399Smckusick * specifies the terms and conditions for redistribution. 523399Smckusick * 6*31402Smckusick * @(#)lfs_inode.c 7.3 (Berkeley) 06/05/87 723399Smckusick */ 824Sbill 917099Sbloom #include "param.h" 1017099Sbloom #include "systm.h" 1117099Sbloom #include "mount.h" 1217099Sbloom #include "dir.h" 1317099Sbloom #include "user.h" 1417099Sbloom #include "inode.h" 1517099Sbloom #include "fs.h" 1617099Sbloom #include "buf.h" 1724525Sbloom #include "cmap.h" 187651Ssam #ifdef QUOTA 1917099Sbloom #include "quota.h" 207504Sroot #endif 2117099Sbloom #include "kernel.h" 2224Sbill 2316840Smckusick #define INOHSZ 512 247334Skre #if ((INOHSZ&(INOHSZ-1)) == 0) 257334Skre #define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1)) 267334Skre #else 2710852Ssam #define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ) 287334Skre #endif 2924Sbill 307334Skre union ihead { /* inode LRU cache, Chris Maltby */ 317334Skre union ihead *ih_head[2]; 327334Skre struct inode *ih_chain[2]; 337334Skre } ihead[INOHSZ]; 347334Skre 357334Skre struct inode *ifreeh, **ifreet; 367334Skre 3724Sbill /* 3824Sbill * Initialize hash links for inodes 3924Sbill * and build inode free list. 4024Sbill */ 4124Sbill ihinit() 4224Sbill { 4324Sbill register int i; 442737Swnj register struct inode *ip = inode; 457334Skre register union ihead *ih = ihead; 4624Sbill 477334Skre for (i = INOHSZ; --i >= 0; ih++) { 487334Skre ih->ih_head[0] = ih; 497334Skre ih->ih_head[1] = ih; 507334Skre } 517334Skre ifreeh = ip; 527334Skre ifreet = &ip->i_freef; 537334Skre ip->i_freeb = &ifreeh; 547334Skre ip->i_forw = ip; 557334Skre ip->i_back = ip; 567334Skre for (i = ninode; --i > 0; ) { 577334Skre ++ip; 587334Skre ip->i_forw = ip; 597334Skre ip->i_back = ip; 607334Skre *ifreet = ip; 617334Skre ip->i_freeb = ifreet; 627334Skre ifreet = &ip->i_freef; 637334Skre } 647334Skre ip->i_freef = NULL; 6524Sbill } 6624Sbill 677334Skre #ifdef notdef 6824Sbill /* 697334Skre * Find an inode if it is incore. 707334Skre * This is the equivalent, for inodes, 717334Skre * of ``incore'' in bio.c or ``pfind'' in subr.c. 727334Skre */ 737334Skre struct inode * 747334Skre ifind(dev, ino) 757334Skre dev_t dev; 767334Skre ino_t ino; 777334Skre { 787334Skre register struct inode *ip; 797334Skre register union ihead *ih; 807334Skre 817334Skre ih = &ihead[INOHASH(dev, ino)]; 827334Skre for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) 837334Skre if (ino==ip->i_number && dev==ip->i_dev) 847334Skre return (ip); 857334Skre return ((struct inode *)0); 867334Skre } 877334Skre #endif notdef 887334Skre 897334Skre /* 9024Sbill * Look up an inode by device,inumber. 9124Sbill * If it is in core (in the inode structure), 9224Sbill * honor the locking protocol. 9324Sbill * If it is not in core, read it in from the 9424Sbill * specified device. 9524Sbill * If the inode is mounted on, perform 9624Sbill * the indicated indirection. 9724Sbill * In all cases, a pointer to a locked 9824Sbill * inode structure is returned. 9924Sbill * 10024Sbill * panic: no imt -- if the mounted file 10124Sbill * system is not in the mount table. 10224Sbill * "cannot happen" 10324Sbill */ 10424Sbill struct inode * 1056569Smckusic iget(dev, fs, ino) 1064818Swnj dev_t dev; 1076569Smckusic register struct fs *fs; 1084818Swnj ino_t ino; 10924Sbill { 1107335Skre register struct inode *ip; 1117335Skre register union ihead *ih; 11224Sbill register struct mount *mp; 11324Sbill register struct buf *bp; 11424Sbill register struct dinode *dp; 1157334Skre register struct inode *iq; 11624Sbill 11724Sbill loop: 1187334Skre ih = &ihead[INOHASH(dev, ino)]; 1197334Skre for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) 1204818Swnj if (ino == ip->i_number && dev == ip->i_dev) { 12116642Ssam /* 12216642Ssam * Following is essentially an inline expanded 12316642Ssam * copy of igrab(), expanded inline for speed, 12416642Ssam * and so that the test for a mounted on inode 12516642Ssam * can be deferred until after we are sure that 12616642Ssam * the inode isn't busy. 12716642Ssam */ 1288452Sroot if ((ip->i_flag&ILOCKED) != 0) { 12924Sbill ip->i_flag |= IWANT; 13024Sbill sleep((caddr_t)ip, PINOD); 13124Sbill goto loop; 13224Sbill } 1334818Swnj if ((ip->i_flag&IMOUNT) != 0) { 1346569Smckusic for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) 1357334Skre if(mp->m_inodp == ip) { 1367334Skre dev = mp->m_dev; 1377334Skre fs = mp->m_bufp->b_un.b_fs; 1387334Skre ino = ROOTINO; 1397334Skre goto loop; 1407334Skre } 14124Sbill panic("no imt"); 14224Sbill } 1437334Skre if (ip->i_count == 0) { /* ino on free list */ 1447334Skre if (iq = ip->i_freef) 1457334Skre iq->i_freeb = ip->i_freeb; 1467334Skre else 1477334Skre ifreet = ip->i_freeb; 1487334Skre *ip->i_freeb = iq; 1497334Skre ip->i_freef = NULL; 1507334Skre ip->i_freeb = NULL; 1517334Skre } 15224Sbill ip->i_count++; 1538452Sroot ip->i_flag |= ILOCKED; 15424Sbill return(ip); 15524Sbill } 1567334Skre 1577334Skre if ((ip = ifreeh) == NULL) { 1582933Swnj tablefull("inode"); 15924Sbill u.u_error = ENFILE; 16024Sbill return(NULL); 16124Sbill } 16216720Skarels if (ip->i_count) 16316720Skarels panic("free inode isn't"); 1647334Skre if (iq = ip->i_freef) 1657334Skre iq->i_freeb = &ifreeh; 1667334Skre ifreeh = iq; 1677334Skre ip->i_freef = NULL; 1687334Skre ip->i_freeb = NULL; 1697334Skre /* 1707334Skre * Now to take inode off the hash chain it was on 1717334Skre * (initially, or after an iflush, it is on a "hash chain" 1727334Skre * consisting entirely of itself, and pointed to by no-one, 1737334Skre * but that doesn't matter), and put it on the chain for 1747334Skre * its new (ino, dev) pair 1757334Skre */ 1767335Skre remque(ip); 1777335Skre insque(ip, ih); 17824Sbill ip->i_dev = dev; 1796569Smckusic ip->i_fs = fs; 18024Sbill ip->i_number = ino; 18116738Smckusick cacheinval(ip); 1828452Sroot ip->i_flag = ILOCKED; 18324Sbill ip->i_count++; 1846569Smckusic ip->i_lastr = 0; 18516720Skarels #ifdef QUOTA 18616720Skarels dqrele(ip->i_dquot); 18716720Skarels #endif 1888618Sroot bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize); 18924Sbill /* 19024Sbill * Check I/O errors 19124Sbill */ 1924818Swnj if ((bp->b_flags&B_ERROR) != 0) { 19324Sbill brelse(bp); 1947334Skre /* 1957334Skre * the inode doesn't contain anything useful, so it would 1967334Skre * be misleading to leave it on its hash chain. 1977334Skre * 'iput' will take care of putting it back on the free list. 1987334Skre */ 1997335Skre remque(ip); 2007334Skre ip->i_forw = ip; 2017334Skre ip->i_back = ip; 2027334Skre /* 2037334Skre * we also loose its inumber, just in case (as iput 2047334Skre * doesn't do that any more) - but as it isn't on its 2057334Skre * hash chain, I doubt if this is really necessary .. kre 2067334Skre * (probably the two methods are interchangable) 2077334Skre */ 2087334Skre ip->i_number = 0; 2097651Ssam #ifdef QUOTA 2107492Skre ip->i_dquot = NODQUOT; 2117492Skre #endif 21224Sbill iput(ip); 21324Sbill return(NULL); 21424Sbill } 21524Sbill dp = bp->b_un.b_dino; 2166569Smckusic dp += itoo(fs, ino); 2176569Smckusic ip->i_ic = dp->di_ic; 21824Sbill brelse(bp); 2197651Ssam #ifdef QUOTA 2207492Skre if (ip->i_mode == 0) 2217492Skre ip->i_dquot = NODQUOT; 2227492Skre else 2237492Skre ip->i_dquot = inoquota(ip); 2247492Skre #endif 2256569Smckusic return (ip); 22624Sbill } 22724Sbill 22824Sbill /* 22916642Ssam * Convert a pointer to an inode into a reference to an inode. 23016642Ssam * 23116642Ssam * This is basically the internal piece of iget (after the 23216642Ssam * inode pointer is located) but without the test for mounted 23316642Ssam * filesystems. It is caller's responsibility to check that 23416642Ssam * the inode pointer is valid. 23516642Ssam */ 23616642Ssam igrab(ip) 23716642Ssam register struct inode *ip; 23816642Ssam { 23916642Ssam while ((ip->i_flag&ILOCKED) != 0) { 24016642Ssam ip->i_flag |= IWANT; 24116642Ssam sleep((caddr_t)ip, PINOD); 24216642Ssam } 24316642Ssam if (ip->i_count == 0) { /* ino on free list */ 24416642Ssam register struct inode *iq; 24516642Ssam 24616642Ssam if (iq = ip->i_freef) 24716642Ssam iq->i_freeb = ip->i_freeb; 24816642Ssam else 24916642Ssam ifreet = ip->i_freeb; 25016642Ssam *ip->i_freeb = iq; 25116642Ssam ip->i_freef = NULL; 25216642Ssam ip->i_freeb = NULL; 25316642Ssam } 25416642Ssam ip->i_count++; 25516642Ssam ip->i_flag |= ILOCKED; 25616642Ssam } 25716642Ssam 25816642Ssam /* 25924Sbill * Decrement reference count of 26024Sbill * an inode structure. 26124Sbill * On the last reference, 26224Sbill * write the inode out and if necessary, 26324Sbill * truncate and deallocate the file. 26424Sbill */ 26524Sbill iput(ip) 2664818Swnj register struct inode *ip; 26724Sbill { 2687118Smckusick 2698452Sroot if ((ip->i_flag & ILOCKED) == 0) 2707118Smckusick panic("iput"); 27116665Smckusick IUNLOCK(ip); 2727118Smckusick irele(ip); 2737118Smckusick } 2747118Smckusick 2757118Smckusick irele(ip) 2767118Smckusick register struct inode *ip; 2777118Smckusick { 2786569Smckusic int mode; 27924Sbill 28018445Smckusick if (ip->i_count == 1) { 2818452Sroot ip->i_flag |= ILOCKED; 28218445Smckusick if (ip->i_nlink <= 0 && ip->i_fs->fs_ronly == 0) { 2839165Ssam itrunc(ip, (u_long)0); 2846569Smckusic mode = ip->i_mode; 28524Sbill ip->i_mode = 0; 2867351Skre ip->i_rdev = 0; 28724Sbill ip->i_flag |= IUPD|ICHG; 2886569Smckusic ifree(ip, ip->i_number, mode); 2897651Ssam #ifdef QUOTA 29012645Ssam (void) chkiq(ip->i_dev, ip, ip->i_uid, 0); 2917492Skre dqrele(ip->i_dquot); 2927492Skre ip->i_dquot = NODQUOT; 2937492Skre #endif 29424Sbill } 2958671Sroot IUPDAT(ip, &time, &time, 0); 29616665Smckusick IUNLOCK(ip); 2977334Skre ip->i_flag = 0; 2987334Skre /* 2997334Skre * Put the inode on the end of the free list. 3007334Skre * Possibly in some cases it would be better to 3017334Skre * put the inode at the head of the free list, 3027334Skre * (eg: where i_mode == 0 || i_number == 0) 3037334Skre * but I will think about that later .. kre 3047334Skre * (i_number is rarely 0 - only after an i/o error in iget, 3057334Skre * where i_mode == 0, the inode will probably be wanted 3067334Skre * again soon for an ialloc, so possibly we should keep it) 3077334Skre */ 3087334Skre if (ifreeh) { 3097334Skre *ifreet = ip; 3107334Skre ip->i_freeb = ifreet; 31124Sbill } else { 3127334Skre ifreeh = ip; 3137334Skre ip->i_freeb = &ifreeh; 31424Sbill } 3157334Skre ip->i_freef = NULL; 3167334Skre ifreet = &ip->i_freef; 31716058Skarels } else if (!(ip->i_flag & ILOCKED)) 31816058Skarels ITIMES(ip, &time, &time); 31924Sbill ip->i_count--; 32024Sbill } 32124Sbill 32224Sbill /* 32324Sbill * Check accessed and update flags on 32424Sbill * an inode structure. 32524Sbill * If any is on, update the inode 32624Sbill * with the current time. 3271203Sbill * If waitfor is given, then must insure 3281203Sbill * i/o order so wait for write to complete. 32924Sbill */ 3301203Sbill iupdat(ip, ta, tm, waitfor) 3314818Swnj register struct inode *ip; 3328630Sroot struct timeval *ta, *tm; 3334818Swnj int waitfor; 33424Sbill { 33524Sbill register struct buf *bp; 33624Sbill struct dinode *dp; 33730749Skarels register struct fs *fs; 33824Sbill 33930749Skarels fs = ip->i_fs; 34016058Skarels if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) != 0) { 34130749Skarels if (fs->fs_ronly) 34224Sbill return; 34330749Skarels bp = bread(ip->i_dev, fsbtodb(fs, itod(fs, ip->i_number)), 34430749Skarels (int)fs->fs_bsize); 34524Sbill if (bp->b_flags & B_ERROR) { 34624Sbill brelse(bp); 34724Sbill return; 34824Sbill } 3494818Swnj if (ip->i_flag&IACC) 3508630Sroot ip->i_atime = ta->tv_sec; 3514818Swnj if (ip->i_flag&IUPD) 3528630Sroot ip->i_mtime = tm->tv_sec; 3534818Swnj if (ip->i_flag&ICHG) 3548106Sroot ip->i_ctime = time.tv_sec; 35516058Skarels ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 35630749Skarels dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 3577343Skre dp->di_ic = ip->i_ic; 3581203Sbill if (waitfor) 3591203Sbill bwrite(bp); 3601203Sbill else 3611203Sbill bdwrite(bp); 36224Sbill } 36324Sbill } 36424Sbill 36510736Ssam #define SINGLE 0 /* index of single indirect block */ 36610736Ssam #define DOUBLE 1 /* index of double indirect block */ 36710736Ssam #define TRIPLE 2 /* index of triple indirect block */ 36824Sbill /* 3697702Ssam * Truncate the inode ip to at most 3707702Ssam * length size. Free affected disk 3717702Ssam * blocks -- the blocks of the file 3727702Ssam * are removed in reverse order. 37310736Ssam * 37410736Ssam * NB: triple indirect blocks are untested. 37524Sbill */ 37610736Ssam itrunc(oip, length) 37717942Smckusick register struct inode *oip; 3789165Ssam u_long length; 37924Sbill { 3809165Ssam register daddr_t lastblock; 38126272Skarels daddr_t bn, lbn, lastiblock[NIADDR]; 3826569Smckusic register struct fs *fs; 38310736Ssam register struct inode *ip; 38417942Smckusick struct buf *bp; 38530749Skarels int offset, osize, size, count, level; 38617942Smckusick long nblocks, blocksreleased = 0; 38717942Smckusick register int i; 38817942Smckusick dev_t dev; 38910736Ssam struct inode tip; 39017942Smckusick extern long indirtrunc(); 3919165Ssam 39213000Ssam if (oip->i_size <= length) { 39313000Ssam oip->i_flag |= ICHG|IUPD; 39413000Ssam iupdat(oip, &time, &time, 1); 3959165Ssam return; 39613000Ssam } 3971203Sbill /* 39810736Ssam * Calculate index into inode's block list of 39910736Ssam * last direct and indirect blocks (if any) 40010736Ssam * which we want to keep. Lastblock is -1 when 40110736Ssam * the file is truncated to 0. 4021203Sbill */ 40310736Ssam fs = oip->i_fs; 4049165Ssam lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 40510736Ssam lastiblock[SINGLE] = lastblock - NDADDR; 40610736Ssam lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 40710736Ssam lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 40812645Ssam nblocks = btodb(fs->fs_bsize); 4096569Smckusic /* 41017942Smckusick * Update the size of the file. If the file is not being 41117942Smckusick * truncated to a block boundry, the contents of the 41217942Smckusick * partial block following the end of the file must be 41317942Smckusick * zero'ed in case it ever become accessable again because 41417942Smckusick * of subsequent file growth. 41517942Smckusick */ 41617942Smckusick osize = oip->i_size; 41717942Smckusick offset = blkoff(fs, length); 41817942Smckusick if (offset == 0) { 41917942Smckusick oip->i_size = length; 42017942Smckusick } else { 42117942Smckusick lbn = lblkno(fs, length); 42217942Smckusick bn = fsbtodb(fs, bmap(oip, lbn, B_WRITE, offset)); 42317942Smckusick if (u.u_error || (long)bn < 0) 42417942Smckusick return; 42517942Smckusick oip->i_size = length; 42617942Smckusick size = blksize(fs, oip, lbn); 42730749Skarels count = howmany(size, CLBYTES); 42817942Smckusick dev = oip->i_dev; 42930749Skarels for (i = 0; i < count; i++) 43030749Skarels munhash(dev, bn + i * CLBYTES / DEV_BSIZE); 43117942Smckusick bp = bread(dev, bn, size); 43217942Smckusick if (bp->b_flags & B_ERROR) { 43317942Smckusick u.u_error = EIO; 43417942Smckusick oip->i_size = osize; 43517942Smckusick brelse(bp); 43617942Smckusick return; 43717942Smckusick } 43826272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 43917942Smckusick bdwrite(bp); 44017942Smckusick } 44117942Smckusick /* 44217942Smckusick * Update file and block pointers 44310736Ssam * on disk before we start freeing blocks. 44410736Ssam * If we crash before free'ing blocks below, 44510736Ssam * the blocks will be returned to the free list. 44610736Ssam * lastiblock values are also normalized to -1 44710736Ssam * for calls to indirtrunc below. 4486569Smckusic */ 44910736Ssam tip = *oip; 45017942Smckusick tip.i_size = osize; 45110736Ssam for (level = TRIPLE; level >= SINGLE; level--) 45210736Ssam if (lastiblock[level] < 0) { 45310736Ssam oip->i_ib[level] = 0; 45410736Ssam lastiblock[level] = -1; 4559165Ssam } 45610736Ssam for (i = NDADDR - 1; i > lastblock; i--) 45710736Ssam oip->i_db[i] = 0; 45810736Ssam oip->i_flag |= ICHG|IUPD; 45917942Smckusick syncip(oip); 46010736Ssam 4616569Smckusic /* 46210736Ssam * Indirect blocks first. 4636569Smckusic */ 46417942Smckusick ip = &tip; 46510736Ssam for (level = TRIPLE; level >= SINGLE; level--) { 46610736Ssam bn = ip->i_ib[level]; 4679165Ssam if (bn != 0) { 46810736Ssam blocksreleased += 46912645Ssam indirtrunc(ip, bn, lastiblock[level], level); 47010736Ssam if (lastiblock[level] < 0) { 47110736Ssam ip->i_ib[level] = 0; 472*31402Smckusick blkfree(ip, bn, (off_t)fs->fs_bsize); 47310736Ssam blocksreleased += nblocks; 47410736Ssam } 47510736Ssam } 47610736Ssam if (lastiblock[level] >= 0) 47710736Ssam goto done; 4789165Ssam } 47910736Ssam 4806569Smckusic /* 48110736Ssam * All whole direct blocks or frags. 4826569Smckusic */ 4839165Ssam for (i = NDADDR - 1; i > lastblock; i--) { 48426359Skarels register off_t bsize; 4859165Ssam 4866569Smckusic bn = ip->i_db[i]; 4879165Ssam if (bn == 0) 48824Sbill continue; 4899165Ssam ip->i_db[i] = 0; 49024525Sbloom bsize = (off_t)blksize(fs, ip, i); 491*31402Smckusick blkfree(ip, bn, bsize); 49224525Sbloom blocksreleased += btodb(bsize); 49324Sbill } 49410736Ssam if (lastblock < 0) 49510736Ssam goto done; 49610736Ssam 4971203Sbill /* 4989165Ssam * Finally, look for a change in size of the 4999165Ssam * last direct block; release any frags. 5001203Sbill */ 50110736Ssam bn = ip->i_db[lastblock]; 50210736Ssam if (bn != 0) { 50326359Skarels off_t oldspace, newspace; 50410736Ssam 5059165Ssam /* 5069165Ssam * Calculate amount of space we're giving 5079165Ssam * back as old block size minus new block size. 5089165Ssam */ 50910736Ssam oldspace = blksize(fs, ip, lastblock); 5109165Ssam ip->i_size = length; 51110736Ssam newspace = blksize(fs, ip, lastblock); 51210736Ssam if (newspace == 0) 51310736Ssam panic("itrunc: newspace"); 51410736Ssam if (oldspace - newspace > 0) { 5159165Ssam /* 5169165Ssam * Block number of space to be free'd is 5179165Ssam * the old block # plus the number of frags 5189165Ssam * required for the storage we're keeping. 5199165Ssam */ 52010736Ssam bn += numfrags(fs, newspace); 521*31402Smckusick blkfree(ip, bn, oldspace - newspace); 52212645Ssam blocksreleased += btodb(oldspace - newspace); 5239165Ssam } 5249165Ssam } 5259165Ssam done: 52610736Ssam /* BEGIN PARANOIA */ 52710736Ssam for (level = SINGLE; level <= TRIPLE; level++) 52810736Ssam if (ip->i_ib[level] != oip->i_ib[level]) 52910736Ssam panic("itrunc1"); 53010736Ssam for (i = 0; i < NDADDR; i++) 53110736Ssam if (ip->i_db[i] != oip->i_db[i]) 53210736Ssam panic("itrunc2"); 53310736Ssam /* END PARANOIA */ 53412645Ssam oip->i_blocks -= blocksreleased; 53512645Ssam if (oip->i_blocks < 0) /* sanity */ 53612645Ssam oip->i_blocks = 0; 53712645Ssam oip->i_flag |= ICHG; 5389165Ssam #ifdef QUOTA 53912645Ssam (void) chkdq(oip, -blocksreleased, 0); 5409165Ssam #endif 54124Sbill } 54224Sbill 5439165Ssam /* 5449165Ssam * Release blocks associated with the inode ip and 5459165Ssam * stored in the indirect block bn. Blocks are free'd 5469165Ssam * in LIFO order up to (but not including) lastbn. If 54710736Ssam * level is greater than SINGLE, the block is an indirect 54810736Ssam * block and recursive calls to indirtrunc must be used to 54910736Ssam * cleanse other indirect blocks. 55010736Ssam * 55110736Ssam * NB: triple indirect blocks are untested. 5529165Ssam */ 5537492Skre long 55410736Ssam indirtrunc(ip, bn, lastbn, level) 5556569Smckusic register struct inode *ip; 5569165Ssam daddr_t bn, lastbn; 55710736Ssam int level; 55824Sbill { 5599165Ssam register int i; 56010736Ssam struct buf *bp, *copy; 56124Sbill register daddr_t *bap; 56210736Ssam register struct fs *fs = ip->i_fs; 5639165Ssam daddr_t nb, last; 56410736Ssam long factor; 5659165Ssam int blocksreleased = 0, nblocks; 56624Sbill 56710736Ssam /* 56810736Ssam * Calculate index in current block of last 56910736Ssam * block to be kept. -1 indicates the entire 57010736Ssam * block so we need not calculate the index. 57110736Ssam */ 57210736Ssam factor = 1; 57310736Ssam for (i = SINGLE; i < level; i++) 57410736Ssam factor *= NINDIR(fs); 5759165Ssam last = lastbn; 57610736Ssam if (lastbn > 0) 57710736Ssam last /= factor; 57812645Ssam nblocks = btodb(fs->fs_bsize); 57910736Ssam /* 58010736Ssam * Get buffer of block pointers, zero those 58110736Ssam * entries corresponding to blocks to be free'd, 58210736Ssam * and update on disk copy first. 58310736Ssam */ 58410736Ssam copy = geteblk((int)fs->fs_bsize); 58510736Ssam bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize); 58610736Ssam if (bp->b_flags&B_ERROR) { 58710736Ssam brelse(copy); 58810736Ssam brelse(bp); 58910736Ssam return (0); 59010736Ssam } 59110736Ssam bap = bp->b_un.b_daddr; 59210736Ssam bcopy((caddr_t)bap, (caddr_t)copy->b_un.b_daddr, (u_int)fs->fs_bsize); 59310736Ssam bzero((caddr_t)&bap[last + 1], 59410736Ssam (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 59510736Ssam bwrite(bp); 59610736Ssam bp = copy, bap = bp->b_un.b_daddr; 59710736Ssam 59810736Ssam /* 59910736Ssam * Recursively free totally unused blocks. 60010736Ssam */ 6019165Ssam for (i = NINDIR(fs) - 1; i > last; i--) { 60224Sbill nb = bap[i]; 6039165Ssam if (nb == 0) 60424Sbill continue; 60510736Ssam if (level > SINGLE) 6069165Ssam blocksreleased += 60712645Ssam indirtrunc(ip, nb, (daddr_t)-1, level - 1); 608*31402Smckusick 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]; 6189165Ssam if (nb != 0) 61912645Ssam blocksreleased += indirtrunc(ip, nb, last, level - 1); 6209165Ssam } 62110736Ssam brelse(bp); 6229165Ssam return (blocksreleased); 62324Sbill } 62424Sbill 62524Sbill /* 62630749Skarels * Remove any inodes in the inode cache belonging to dev. 6277334Skre * 6287334Skre * There should not be any active ones, return error if any are found 62930749Skarels * (nb: this is a user error, not a system err). 6307334Skre */ 6317651Ssam #ifdef QUOTA 6327504Sroot iflush(dev, iq) 6337492Skre dev_t dev; 6347504Sroot struct inode *iq; 6357492Skre #else 6367334Skre iflush(dev) 6377334Skre dev_t dev; 6387492Skre #endif 6397334Skre { 6407335Skre register struct inode *ip; 6417334Skre 6427334Skre for (ip = inode; ip < inodeNINODE; ip++) { 6437651Ssam #ifdef QUOTA 6447492Skre if (ip != iq && ip->i_dev == dev) 6457492Skre #else 6467334Skre if (ip->i_dev == dev) 6477492Skre #endif 6487334Skre if (ip->i_count) 64930749Skarels return (EBUSY); 6507334Skre else { 6517335Skre remque(ip); 6527334Skre ip->i_forw = ip; 6537334Skre ip->i_back = ip; 6547334Skre /* 6557334Skre * as i_count == 0, the inode was on the free 6567334Skre * list already, just leave it there, it will 6577334Skre * fall off the bottom eventually. We could 6587334Skre * perhaps move it to the head of the free 6597334Skre * list, but as umounts are done so 6607334Skre * infrequently, we would gain very little, 6617334Skre * while making the code bigger. 6627334Skre */ 6637651Ssam #ifdef QUOTA 6647492Skre dqrele(ip->i_dquot); 6657492Skre ip->i_dquot = NODQUOT; 6667492Skre #endif 6677334Skre } 6687334Skre } 66930749Skarels return (0); 6707334Skre } 6717334Skre 6723617Sroot /* 6734818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 6743617Sroot */ 6754818Swnj ilock(ip) 6764818Swnj register struct inode *ip; 6773617Sroot { 6783617Sroot 6798452Sroot ILOCK(ip); 6803617Sroot } 6813617Sroot 6823617Sroot /* 6834818Swnj * Unlock an inode. If WANT bit is on, wakeup. 6843617Sroot */ 6857118Smckusick iunlock(ip) 6864818Swnj register struct inode *ip; 6873617Sroot { 6883617Sroot 6898452Sroot IUNLOCK(ip); 6903617Sroot } 691