123399Smckusick /* 223399Smckusick * Copyright (c) 1982 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*24525Sbloom * @(#)lfs_inode.c 6.17 (Berkeley) 09/04/85 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" 17*24525Sbloom #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 11716656Smckusick 11824Sbill loop: 1197334Skre ih = &ihead[INOHASH(dev, ino)]; 1207334Skre for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) 1214818Swnj if (ino == ip->i_number && dev == ip->i_dev) { 12216642Ssam /* 12316642Ssam * Following is essentially an inline expanded 12416642Ssam * copy of igrab(), expanded inline for speed, 12516642Ssam * and so that the test for a mounted on inode 12616642Ssam * can be deferred until after we are sure that 12716642Ssam * the inode isn't busy. 12816642Ssam */ 1298452Sroot if ((ip->i_flag&ILOCKED) != 0) { 13024Sbill ip->i_flag |= IWANT; 13124Sbill sleep((caddr_t)ip, PINOD); 13224Sbill goto loop; 13324Sbill } 1344818Swnj if ((ip->i_flag&IMOUNT) != 0) { 1356569Smckusic for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++) 1367334Skre if(mp->m_inodp == ip) { 1377334Skre dev = mp->m_dev; 1387334Skre fs = mp->m_bufp->b_un.b_fs; 1397334Skre ino = ROOTINO; 1407334Skre goto loop; 1417334Skre } 14224Sbill panic("no imt"); 14324Sbill } 1447334Skre if (ip->i_count == 0) { /* ino on free list */ 1457334Skre if (iq = ip->i_freef) 1467334Skre iq->i_freeb = ip->i_freeb; 1477334Skre else 1487334Skre ifreet = ip->i_freeb; 1497334Skre *ip->i_freeb = iq; 1507334Skre ip->i_freef = NULL; 1517334Skre ip->i_freeb = NULL; 1527334Skre } 15324Sbill ip->i_count++; 1548452Sroot ip->i_flag |= ILOCKED; 15524Sbill return(ip); 15624Sbill } 1577334Skre 1587334Skre if ((ip = ifreeh) == NULL) { 1592933Swnj tablefull("inode"); 16024Sbill u.u_error = ENFILE; 16124Sbill return(NULL); 16224Sbill } 16316720Skarels if (ip->i_count) 16416720Skarels panic("free inode isn't"); 1657334Skre if (iq = ip->i_freef) 1667334Skre iq->i_freeb = &ifreeh; 1677334Skre ifreeh = iq; 1687334Skre ip->i_freef = NULL; 1697334Skre ip->i_freeb = NULL; 1707334Skre /* 1717334Skre * Now to take inode off the hash chain it was on 1727334Skre * (initially, or after an iflush, it is on a "hash chain" 1737334Skre * consisting entirely of itself, and pointed to by no-one, 1747334Skre * but that doesn't matter), and put it on the chain for 1757334Skre * its new (ino, dev) pair 1767334Skre */ 1777335Skre remque(ip); 1787335Skre insque(ip, ih); 17924Sbill ip->i_dev = dev; 1806569Smckusic ip->i_fs = fs; 18124Sbill ip->i_number = ino; 18216738Smckusick cacheinval(ip); 1838452Sroot ip->i_flag = ILOCKED; 18424Sbill ip->i_count++; 1856569Smckusic ip->i_lastr = 0; 18616720Skarels #ifdef QUOTA 18716720Skarels dqrele(ip->i_dquot); 18816720Skarels #endif 1898618Sroot bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize); 19024Sbill /* 19124Sbill * Check I/O errors 19224Sbill */ 1934818Swnj if ((bp->b_flags&B_ERROR) != 0) { 19424Sbill brelse(bp); 1957334Skre /* 1967334Skre * the inode doesn't contain anything useful, so it would 1977334Skre * be misleading to leave it on its hash chain. 1987334Skre * 'iput' will take care of putting it back on the free list. 1997334Skre */ 2007335Skre remque(ip); 2017334Skre ip->i_forw = ip; 2027334Skre ip->i_back = ip; 2037334Skre /* 2047334Skre * we also loose its inumber, just in case (as iput 2057334Skre * doesn't do that any more) - but as it isn't on its 2067334Skre * hash chain, I doubt if this is really necessary .. kre 2077334Skre * (probably the two methods are interchangable) 2087334Skre */ 2097334Skre ip->i_number = 0; 2107651Ssam #ifdef QUOTA 2117492Skre ip->i_dquot = NODQUOT; 2127492Skre #endif 21324Sbill iput(ip); 21424Sbill return(NULL); 21524Sbill } 21624Sbill dp = bp->b_un.b_dino; 2176569Smckusic dp += itoo(fs, ino); 2186569Smckusic ip->i_ic = dp->di_ic; 21924Sbill brelse(bp); 2207651Ssam #ifdef QUOTA 2217492Skre if (ip->i_mode == 0) 2227492Skre ip->i_dquot = NODQUOT; 2237492Skre else 2247492Skre ip->i_dquot = inoquota(ip); 2257492Skre #endif 2266569Smckusic return (ip); 22724Sbill } 22824Sbill 22924Sbill /* 23016642Ssam * Convert a pointer to an inode into a reference to an inode. 23116642Ssam * 23216642Ssam * This is basically the internal piece of iget (after the 23316642Ssam * inode pointer is located) but without the test for mounted 23416642Ssam * filesystems. It is caller's responsibility to check that 23516642Ssam * the inode pointer is valid. 23616642Ssam */ 23716642Ssam igrab(ip) 23816642Ssam register struct inode *ip; 23916642Ssam { 24016642Ssam while ((ip->i_flag&ILOCKED) != 0) { 24116642Ssam ip->i_flag |= IWANT; 24216642Ssam sleep((caddr_t)ip, PINOD); 24316642Ssam } 24416642Ssam if (ip->i_count == 0) { /* ino on free list */ 24516642Ssam register struct inode *iq; 24616642Ssam 24716642Ssam if (iq = ip->i_freef) 24816642Ssam iq->i_freeb = ip->i_freeb; 24916642Ssam else 25016642Ssam ifreet = ip->i_freeb; 25116642Ssam *ip->i_freeb = iq; 25216642Ssam ip->i_freef = NULL; 25316642Ssam ip->i_freeb = NULL; 25416642Ssam } 25516642Ssam ip->i_count++; 25616642Ssam ip->i_flag |= ILOCKED; 25716642Ssam } 25816642Ssam 25916642Ssam /* 26024Sbill * Decrement reference count of 26124Sbill * an inode structure. 26224Sbill * On the last reference, 26324Sbill * write the inode out and if necessary, 26424Sbill * truncate and deallocate the file. 26524Sbill */ 26624Sbill iput(ip) 2674818Swnj register struct inode *ip; 26824Sbill { 2697118Smckusick 2708452Sroot if ((ip->i_flag & ILOCKED) == 0) 2717118Smckusick panic("iput"); 27216665Smckusick IUNLOCK(ip); 2737118Smckusick irele(ip); 2747118Smckusick } 2757118Smckusick 2767118Smckusick irele(ip) 2777118Smckusick register struct inode *ip; 2787118Smckusick { 2796569Smckusic int mode; 28024Sbill 28118445Smckusick if (ip->i_count == 1) { 2828452Sroot ip->i_flag |= ILOCKED; 28318445Smckusick if (ip->i_nlink <= 0 && ip->i_fs->fs_ronly == 0) { 2849165Ssam itrunc(ip, (u_long)0); 2856569Smckusic mode = ip->i_mode; 28624Sbill ip->i_mode = 0; 2877351Skre ip->i_rdev = 0; 28824Sbill ip->i_flag |= IUPD|ICHG; 2896569Smckusic ifree(ip, ip->i_number, mode); 2907651Ssam #ifdef QUOTA 29112645Ssam (void) chkiq(ip->i_dev, ip, ip->i_uid, 0); 2927492Skre dqrele(ip->i_dquot); 2937492Skre ip->i_dquot = NODQUOT; 2947492Skre #endif 29524Sbill } 2968671Sroot IUPDAT(ip, &time, &time, 0); 29716665Smckusick IUNLOCK(ip); 2987334Skre ip->i_flag = 0; 2997334Skre /* 3007334Skre * Put the inode on the end of the free list. 3017334Skre * Possibly in some cases it would be better to 3027334Skre * put the inode at the head of the free list, 3037334Skre * (eg: where i_mode == 0 || i_number == 0) 3047334Skre * but I will think about that later .. kre 3057334Skre * (i_number is rarely 0 - only after an i/o error in iget, 3067334Skre * where i_mode == 0, the inode will probably be wanted 3077334Skre * again soon for an ialloc, so possibly we should keep it) 3087334Skre */ 3097334Skre if (ifreeh) { 3107334Skre *ifreet = ip; 3117334Skre ip->i_freeb = ifreet; 31224Sbill } else { 3137334Skre ifreeh = ip; 3147334Skre ip->i_freeb = &ifreeh; 31524Sbill } 3167334Skre ip->i_freef = NULL; 3177334Skre ifreet = &ip->i_freef; 31816058Skarels } else if (!(ip->i_flag & ILOCKED)) 31916058Skarels ITIMES(ip, &time, &time); 32024Sbill ip->i_count--; 32124Sbill } 32224Sbill 32324Sbill /* 32424Sbill * Check accessed and update flags on 32524Sbill * an inode structure. 32624Sbill * If any is on, update the inode 32724Sbill * with the current time. 3281203Sbill * If waitfor is given, then must insure 3291203Sbill * i/o order so wait for write to complete. 33024Sbill */ 3311203Sbill iupdat(ip, ta, tm, waitfor) 3324818Swnj register struct inode *ip; 3338630Sroot struct timeval *ta, *tm; 3344818Swnj int waitfor; 33524Sbill { 33624Sbill register struct buf *bp; 33724Sbill struct dinode *dp; 3386569Smckusic register struct fs *fp; 33924Sbill 3406569Smckusic fp = ip->i_fs; 34116058Skarels if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) != 0) { 3426569Smckusic if (fp->fs_ronly) 34324Sbill return; 3446569Smckusic bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)), 3458618Sroot (int)fp->fs_bsize); 34624Sbill if (bp->b_flags & B_ERROR) { 34724Sbill brelse(bp); 34824Sbill return; 34924Sbill } 3504818Swnj if (ip->i_flag&IACC) 3518630Sroot ip->i_atime = ta->tv_sec; 3524818Swnj if (ip->i_flag&IUPD) 3538630Sroot ip->i_mtime = tm->tv_sec; 3544818Swnj if (ip->i_flag&ICHG) 3558106Sroot ip->i_ctime = time.tv_sec; 35616058Skarels ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 3577343Skre dp = bp->b_un.b_dino + itoo(fp, ip->i_number); 3587343Skre dp->di_ic = ip->i_ic; 3591203Sbill if (waitfor) 3601203Sbill bwrite(bp); 3611203Sbill else 3621203Sbill bdwrite(bp); 36324Sbill } 36424Sbill } 36524Sbill 36610736Ssam #define SINGLE 0 /* index of single indirect block */ 36710736Ssam #define DOUBLE 1 /* index of double indirect block */ 36810736Ssam #define TRIPLE 2 /* index of triple indirect block */ 36924Sbill /* 3707702Ssam * Truncate the inode ip to at most 3717702Ssam * length size. Free affected disk 3727702Ssam * blocks -- the blocks of the file 3737702Ssam * are removed in reverse order. 37410736Ssam * 37510736Ssam * NB: triple indirect blocks are untested. 37624Sbill */ 37710736Ssam itrunc(oip, length) 37817942Smckusick register struct inode *oip; 3799165Ssam u_long length; 38024Sbill { 3819165Ssam register daddr_t lastblock; 38210736Ssam daddr_t bn, lastiblock[NIADDR]; 3836569Smckusic register struct fs *fs; 38410736Ssam register struct inode *ip; 38517942Smckusick struct buf *bp; 386*24525Sbloom int offset, lbn, osize, size, count, level, s; 38717942Smckusick long nblocks, blocksreleased = 0; 38817942Smckusick register int i; 38917942Smckusick dev_t dev; 39010736Ssam struct inode tip; 39117942Smckusick extern long indirtrunc(); 39217942Smckusick extern struct cmap *mfind(); 3939165Ssam 39413000Ssam if (oip->i_size <= length) { 39513000Ssam oip->i_flag |= ICHG|IUPD; 39613000Ssam iupdat(oip, &time, &time, 1); 3979165Ssam return; 39813000Ssam } 3991203Sbill /* 40010736Ssam * Calculate index into inode's block list of 40110736Ssam * last direct and indirect blocks (if any) 40210736Ssam * which we want to keep. Lastblock is -1 when 40310736Ssam * the file is truncated to 0. 4041203Sbill */ 40510736Ssam fs = oip->i_fs; 4069165Ssam lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 40710736Ssam lastiblock[SINGLE] = lastblock - NDADDR; 40810736Ssam lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 40910736Ssam lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 41012645Ssam nblocks = btodb(fs->fs_bsize); 4116569Smckusic /* 41217942Smckusick * Update the size of the file. If the file is not being 41317942Smckusick * truncated to a block boundry, the contents of the 41417942Smckusick * partial block following the end of the file must be 41517942Smckusick * zero'ed in case it ever become accessable again because 41617942Smckusick * of subsequent file growth. 41717942Smckusick */ 41817942Smckusick osize = oip->i_size; 41917942Smckusick offset = blkoff(fs, length); 42017942Smckusick if (offset == 0) { 42117942Smckusick oip->i_size = length; 42217942Smckusick } else { 42317942Smckusick lbn = lblkno(fs, length); 42417942Smckusick bn = fsbtodb(fs, bmap(oip, lbn, B_WRITE, offset)); 42517942Smckusick if (u.u_error || (long)bn < 0) 42617942Smckusick return; 42717942Smckusick oip->i_size = length; 42817942Smckusick size = blksize(fs, oip, lbn); 42917942Smckusick count = howmany(size, DEV_BSIZE); 43017942Smckusick dev = oip->i_dev; 43117942Smckusick s = splimp(); 43217942Smckusick for (i = 0; i < count; i += CLSIZE) 43317942Smckusick if (mfind(dev, bn + i)) 43417942Smckusick munhash(dev, bn + i); 43517942Smckusick splx(s); 43617942Smckusick bp = bread(dev, bn, size); 43717942Smckusick if (bp->b_flags & B_ERROR) { 43817942Smckusick u.u_error = EIO; 43917942Smckusick oip->i_size = osize; 44017942Smckusick brelse(bp); 44117942Smckusick return; 44217942Smckusick } 44317942Smckusick bzero(bp->b_un.b_addr + offset, size - offset); 44417942Smckusick bdwrite(bp); 44517942Smckusick } 44617942Smckusick /* 44717942Smckusick * Update file and block pointers 44810736Ssam * on disk before we start freeing blocks. 44910736Ssam * If we crash before free'ing blocks below, 45010736Ssam * the blocks will be returned to the free list. 45110736Ssam * lastiblock values are also normalized to -1 45210736Ssam * for calls to indirtrunc below. 4536569Smckusic */ 45410736Ssam tip = *oip; 45517942Smckusick tip.i_size = osize; 45610736Ssam for (level = TRIPLE; level >= SINGLE; level--) 45710736Ssam if (lastiblock[level] < 0) { 45810736Ssam oip->i_ib[level] = 0; 45910736Ssam lastiblock[level] = -1; 4609165Ssam } 46110736Ssam for (i = NDADDR - 1; i > lastblock; i--) 46210736Ssam oip->i_db[i] = 0; 46310736Ssam oip->i_flag |= ICHG|IUPD; 46417942Smckusick syncip(oip); 46510736Ssam 4666569Smckusic /* 46710736Ssam * Indirect blocks first. 4686569Smckusic */ 46917942Smckusick ip = &tip; 47010736Ssam for (level = TRIPLE; level >= SINGLE; level--) { 47110736Ssam bn = ip->i_ib[level]; 4729165Ssam if (bn != 0) { 47310736Ssam blocksreleased += 47412645Ssam indirtrunc(ip, bn, lastiblock[level], level); 47510736Ssam if (lastiblock[level] < 0) { 47610736Ssam ip->i_ib[level] = 0; 47710736Ssam free(ip, bn, (off_t)fs->fs_bsize); 47810736Ssam blocksreleased += nblocks; 47910736Ssam } 48010736Ssam } 48110736Ssam if (lastiblock[level] >= 0) 48210736Ssam goto done; 4839165Ssam } 48410736Ssam 4856569Smckusic /* 48610736Ssam * All whole direct blocks or frags. 4876569Smckusic */ 4889165Ssam for (i = NDADDR - 1; i > lastblock; i--) { 489*24525Sbloom register int bsize; 4909165Ssam 4916569Smckusic bn = ip->i_db[i]; 4929165Ssam if (bn == 0) 49324Sbill continue; 4949165Ssam ip->i_db[i] = 0; 495*24525Sbloom bsize = (off_t)blksize(fs, ip, i); 496*24525Sbloom free(ip, bn, bsize); 497*24525Sbloom blocksreleased += btodb(bsize); 49824Sbill } 49910736Ssam if (lastblock < 0) 50010736Ssam goto done; 50110736Ssam 5021203Sbill /* 5039165Ssam * Finally, look for a change in size of the 5049165Ssam * last direct block; release any frags. 5051203Sbill */ 50610736Ssam bn = ip->i_db[lastblock]; 50710736Ssam if (bn != 0) { 50810736Ssam int oldspace, newspace; 50910736Ssam 5109165Ssam /* 5119165Ssam * Calculate amount of space we're giving 5129165Ssam * back as old block size minus new block size. 5139165Ssam */ 51410736Ssam oldspace = blksize(fs, ip, lastblock); 5159165Ssam ip->i_size = length; 51610736Ssam newspace = blksize(fs, ip, lastblock); 51710736Ssam if (newspace == 0) 51810736Ssam panic("itrunc: newspace"); 51910736Ssam if (oldspace - newspace > 0) { 5209165Ssam /* 5219165Ssam * Block number of space to be free'd is 5229165Ssam * the old block # plus the number of frags 5239165Ssam * required for the storage we're keeping. 5249165Ssam */ 52510736Ssam bn += numfrags(fs, newspace); 52610736Ssam free(ip, bn, oldspace - newspace); 52712645Ssam blocksreleased += btodb(oldspace - newspace); 5289165Ssam } 5299165Ssam } 5309165Ssam done: 53110736Ssam /* BEGIN PARANOIA */ 53210736Ssam for (level = SINGLE; level <= TRIPLE; level++) 53310736Ssam if (ip->i_ib[level] != oip->i_ib[level]) 53410736Ssam panic("itrunc1"); 53510736Ssam for (i = 0; i < NDADDR; i++) 53610736Ssam if (ip->i_db[i] != oip->i_db[i]) 53710736Ssam panic("itrunc2"); 53810736Ssam /* END PARANOIA */ 53912645Ssam oip->i_blocks -= blocksreleased; 54012645Ssam if (oip->i_blocks < 0) /* sanity */ 54112645Ssam oip->i_blocks = 0; 54212645Ssam oip->i_flag |= ICHG; 5439165Ssam #ifdef QUOTA 54412645Ssam (void) chkdq(oip, -blocksreleased, 0); 5459165Ssam #endif 54624Sbill } 54724Sbill 5489165Ssam /* 5499165Ssam * Release blocks associated with the inode ip and 5509165Ssam * stored in the indirect block bn. Blocks are free'd 5519165Ssam * in LIFO order up to (but not including) lastbn. If 55210736Ssam * level is greater than SINGLE, the block is an indirect 55310736Ssam * block and recursive calls to indirtrunc must be used to 55410736Ssam * cleanse other indirect blocks. 55510736Ssam * 55610736Ssam * NB: triple indirect blocks are untested. 5579165Ssam */ 5587492Skre long 55910736Ssam indirtrunc(ip, bn, lastbn, level) 5606569Smckusic register struct inode *ip; 5619165Ssam daddr_t bn, lastbn; 56210736Ssam int level; 56324Sbill { 5649165Ssam register int i; 56510736Ssam struct buf *bp, *copy; 56624Sbill register daddr_t *bap; 56710736Ssam register struct fs *fs = ip->i_fs; 5689165Ssam daddr_t nb, last; 56910736Ssam long factor; 5709165Ssam int blocksreleased = 0, nblocks; 57124Sbill 57210736Ssam /* 57310736Ssam * Calculate index in current block of last 57410736Ssam * block to be kept. -1 indicates the entire 57510736Ssam * block so we need not calculate the index. 57610736Ssam */ 57710736Ssam factor = 1; 57810736Ssam for (i = SINGLE; i < level; i++) 57910736Ssam factor *= NINDIR(fs); 5809165Ssam last = lastbn; 58110736Ssam if (lastbn > 0) 58210736Ssam last /= factor; 58312645Ssam nblocks = btodb(fs->fs_bsize); 58410736Ssam /* 58510736Ssam * Get buffer of block pointers, zero those 58610736Ssam * entries corresponding to blocks to be free'd, 58710736Ssam * and update on disk copy first. 58810736Ssam */ 58910736Ssam copy = geteblk((int)fs->fs_bsize); 59010736Ssam bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize); 59110736Ssam if (bp->b_flags&B_ERROR) { 59210736Ssam brelse(copy); 59310736Ssam brelse(bp); 59410736Ssam return (0); 59510736Ssam } 59610736Ssam bap = bp->b_un.b_daddr; 59710736Ssam bcopy((caddr_t)bap, (caddr_t)copy->b_un.b_daddr, (u_int)fs->fs_bsize); 59810736Ssam bzero((caddr_t)&bap[last + 1], 59910736Ssam (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 60010736Ssam bwrite(bp); 60110736Ssam bp = copy, bap = bp->b_un.b_daddr; 60210736Ssam 60310736Ssam /* 60410736Ssam * Recursively free totally unused blocks. 60510736Ssam */ 6069165Ssam for (i = NINDIR(fs) - 1; i > last; i--) { 60724Sbill nb = bap[i]; 6089165Ssam if (nb == 0) 60924Sbill continue; 61010736Ssam if (level > SINGLE) 6119165Ssam blocksreleased += 61212645Ssam indirtrunc(ip, nb, (daddr_t)-1, level - 1); 6139165Ssam free(ip, nb, (int)fs->fs_bsize); 6149165Ssam blocksreleased += nblocks; 61524Sbill } 61610736Ssam 61710736Ssam /* 61810736Ssam * Recursively free last partial block. 61910736Ssam */ 62010736Ssam if (level > SINGLE && lastbn >= 0) { 62110736Ssam last = lastbn % factor; 6229165Ssam nb = bap[i]; 6239165Ssam if (nb != 0) 62412645Ssam blocksreleased += indirtrunc(ip, nb, last, level - 1); 6259165Ssam } 62610736Ssam brelse(bp); 6279165Ssam return (blocksreleased); 62824Sbill } 62924Sbill 63024Sbill /* 6317334Skre * remove any inodes in the inode cache belonging to dev 6327334Skre * 6337334Skre * There should not be any active ones, return error if any are found 6347334Skre * (nb: this is a user error, not a system err) 6357334Skre * 6367334Skre * Also, count the references to dev by block devices - this really 6377334Skre * has nothing to do with the object of the procedure, but as we have 6387334Skre * to scan the inode table here anyway, we might as well get the 6397334Skre * extra benefit. 6407334Skre * 6417334Skre * this is called from sumount()/sys3.c when dev is being unmounted 6427334Skre */ 6437651Ssam #ifdef QUOTA 6447504Sroot iflush(dev, iq) 6457492Skre dev_t dev; 6467504Sroot struct inode *iq; 6477492Skre #else 6487334Skre iflush(dev) 6497334Skre dev_t dev; 6507492Skre #endif 6517334Skre { 6527335Skre register struct inode *ip; 6537334Skre register open = 0; 6547334Skre 6557334Skre for (ip = inode; ip < inodeNINODE; ip++) { 6567651Ssam #ifdef QUOTA 6577492Skre if (ip != iq && ip->i_dev == dev) 6587492Skre #else 6597334Skre if (ip->i_dev == dev) 6607492Skre #endif 6617334Skre if (ip->i_count) 6627334Skre return(-1); 6637334Skre else { 6647335Skre remque(ip); 6657334Skre ip->i_forw = ip; 6667334Skre ip->i_back = ip; 6677334Skre /* 6687334Skre * as i_count == 0, the inode was on the free 6697334Skre * list already, just leave it there, it will 6707334Skre * fall off the bottom eventually. We could 6717334Skre * perhaps move it to the head of the free 6727334Skre * list, but as umounts are done so 6737334Skre * infrequently, we would gain very little, 6747334Skre * while making the code bigger. 6757334Skre */ 6767651Ssam #ifdef QUOTA 6777492Skre dqrele(ip->i_dquot); 6787492Skre ip->i_dquot = NODQUOT; 6797492Skre #endif 6807334Skre } 6817334Skre else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK && 6827334Skre ip->i_rdev == dev) 6837334Skre open++; 6847334Skre } 6857334Skre return (open); 6867334Skre } 6877334Skre 6883617Sroot /* 6894818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 6903617Sroot */ 6914818Swnj ilock(ip) 6924818Swnj register struct inode *ip; 6933617Sroot { 6943617Sroot 6958452Sroot ILOCK(ip); 6963617Sroot } 6973617Sroot 6983617Sroot /* 6994818Swnj * Unlock an inode. If WANT bit is on, wakeup. 7003617Sroot */ 7017118Smckusick iunlock(ip) 7024818Swnj register struct inode *ip; 7033617Sroot { 7043617Sroot 7058452Sroot IUNLOCK(ip); 7063617Sroot } 707