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*38345Smckusick * @(#)lfs_inode.c 7.9 (Berkeley) 06/27/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 4437736Smckusick #define INSFREE(ip) {\ 4537736Smckusick if (ifreeh) { \ 4637736Smckusick *ifreet = (ip); \ 4737736Smckusick (ip)->i_freeb = ifreet; \ 4837736Smckusick } else { \ 4937736Smckusick ifreeh = (ip); \ 5037736Smckusick (ip)->i_freeb = &ifreeh; \ 5137736Smckusick } \ 5237736Smckusick (ip)->i_freef = NULL; \ 5337736Smckusick ifreet = &(ip)->i_freef; \ 5437736Smckusick } 5537736Smckusick 567334Skre union ihead { /* inode LRU cache, Chris Maltby */ 577334Skre union ihead *ih_head[2]; 587334Skre struct inode *ih_chain[2]; 597334Skre } ihead[INOHSZ]; 607334Skre 6137736Smckusick struct inode *ifreeh, **ifreet, *bdevlisth; 627334Skre 6324Sbill /* 6424Sbill * Initialize hash links for inodes 6524Sbill * and build inode free list. 6624Sbill */ 6724Sbill ihinit() 6824Sbill { 6924Sbill register int i; 702737Swnj register struct inode *ip = inode; 717334Skre register union ihead *ih = ihead; 7224Sbill 737334Skre for (i = INOHSZ; --i >= 0; ih++) { 747334Skre ih->ih_head[0] = ih; 757334Skre ih->ih_head[1] = ih; 767334Skre } 777334Skre ifreeh = ip; 787334Skre ifreet = &ip->i_freef; 797334Skre ip->i_freeb = &ifreeh; 807334Skre ip->i_forw = ip; 817334Skre ip->i_back = ip; 8237736Smckusick ITOV(ip)->v_data = (qaddr_t)ip; 837334Skre for (i = ninode; --i > 0; ) { 847334Skre ++ip; 857334Skre ip->i_forw = ip; 867334Skre ip->i_back = ip; 8737736Smckusick ITOV(ip)->v_data = (qaddr_t)ip; 887334Skre *ifreet = ip; 897334Skre ip->i_freeb = ifreet; 907334Skre ifreet = &ip->i_freef; 917334Skre } 927334Skre ip->i_freef = NULL; 9324Sbill } 9424Sbill 9524Sbill /* 9637736Smckusick * Look up an vnode/inode by device,inumber. 9724Sbill * If it is in core (in the inode structure), 9824Sbill * honor the locking protocol. 9924Sbill * If it is not in core, read it in from the 10024Sbill * specified device. 10137736Smckusick * Callers must check for mount points!! 10224Sbill * In all cases, a pointer to a locked 10324Sbill * inode structure is returned. 10424Sbill */ 10537736Smckusick iget(xp, ino, ipp) 10637736Smckusick struct inode *xp; 1074818Swnj ino_t ino; 10837736Smckusick struct inode **ipp; 10924Sbill { 11037736Smckusick dev_t dev = xp->i_dev; 11137736Smckusick struct mount *mntp = ITOV(xp)->v_mount; 11237736Smckusick register struct fs *fs = VFSTOUFS(mntp)->um_fs; 11337736Smckusick register struct inode *ip, *iq; 11437736Smckusick register struct vnode *vp; 11537736Smckusick struct inode *nip; 11637736Smckusick struct buf *bp; 11737736Smckusick struct dinode tdip, *dp; 11837736Smckusick union ihead *ih; 11937736Smckusick int error; 12024Sbill 12124Sbill loop: 1227334Skre ih = &ihead[INOHASH(dev, ino)]; 1237334Skre for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) 1244818Swnj if (ino == ip->i_number && dev == ip->i_dev) { 12516642Ssam /* 12616642Ssam * Following is essentially an inline expanded 12716642Ssam * copy of igrab(), expanded inline for speed, 12816642Ssam * and so that the test for a mounted on inode 12916642Ssam * can be deferred until after we are sure that 13016642Ssam * the inode isn't busy. 13116642Ssam */ 1328452Sroot if ((ip->i_flag&ILOCKED) != 0) { 13324Sbill ip->i_flag |= IWANT; 13424Sbill sleep((caddr_t)ip, PINOD); 13524Sbill goto loop; 13624Sbill } 13737736Smckusick vp = ITOV(ip); 13837736Smckusick if (vp->v_count == 0) { /* ino on free list */ 1397334Skre if (iq = ip->i_freef) 1407334Skre iq->i_freeb = ip->i_freeb; 1417334Skre else 1427334Skre ifreet = ip->i_freeb; 1437334Skre *ip->i_freeb = iq; 1447334Skre ip->i_freef = NULL; 1457334Skre ip->i_freeb = NULL; 1467334Skre } 14738226Smckusick ILOCK(ip); 148*38345Smckusick VREF(vp); 14937736Smckusick *ipp = ip; 15037736Smckusick return(0); 15124Sbill } 15237736Smckusick if (error = getnewino(dev, ino, &nip)) { 15337736Smckusick *ipp = 0; 15437736Smckusick return (error); 15537736Smckusick } 15637736Smckusick ip = nip; 15737736Smckusick /* 15837736Smckusick * Read in the disk contents for the inode. 15937736Smckusick */ 16037736Smckusick if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)), 16137736Smckusick (int)fs->fs_bsize, &bp)) { 16237736Smckusick /* 16337736Smckusick * The inode doesn't contain anything useful, so it would 16437736Smckusick * be misleading to leave it on its hash chain. Iput() will 16537736Smckusick * take care of putting it back on the free list. We also 16637736Smckusick * lose its inumber, just in case. 16737736Smckusick */ 16837736Smckusick remque(ip); 16937736Smckusick ip->i_forw = ip; 17037736Smckusick ip->i_back = ip; 17137736Smckusick ip->i_number = 0; 17237736Smckusick INSFREE(ip); 17338226Smckusick iunlock(ip); 17437736Smckusick ip->i_flag = 0; 17537736Smckusick brelse(bp); 17637736Smckusick *ipp = 0; 17737736Smckusick return(error); 17837736Smckusick } 17937736Smckusick /* 18037736Smckusick * Check to see if the new inode represents a block device 18137736Smckusick * for which we already have an inode (either because of 18237736Smckusick * bdevvp() or because of a different inode representing 18337736Smckusick * the same block device). If such an alias exists, put the 18437736Smckusick * just allocated inode back on the free list, and replace 18537736Smckusick * the contents of the existing inode with the contents of 18637736Smckusick * the new inode. 18737736Smckusick */ 18837736Smckusick dp = bp->b_un.b_dino; 18937736Smckusick dp += itoo(fs, ino); 19037736Smckusick if ((dp->di_mode & IFMT) != IFBLK) { 19137736Smckusick ip->i_ic = dp->di_ic; 19237736Smckusick brelse(bp); 19337736Smckusick } else { 19437736Smckusick again: 19537736Smckusick for (iq = bdevlisth; iq; iq = iq->i_devlst) { 19637736Smckusick if (dp->di_rdev != ITOV(iq)->v_rdev) 19737736Smckusick continue; 19837736Smckusick igrab(iq); 19937736Smckusick if (dp->di_rdev != ITOV(iq)->v_rdev) { 20037736Smckusick iput(iq); 20137736Smckusick goto again; 20237736Smckusick } 20337736Smckusick /* 20437736Smckusick * Discard unneeded inode. 20537736Smckusick */ 20637736Smckusick remque(ip); 20737736Smckusick ip->i_forw = ip; 20837736Smckusick ip->i_back = ip; 20937736Smckusick ip->i_number = 0; 21037736Smckusick INSFREE(ip); 21138226Smckusick iunlock(ip); 21237736Smckusick ip->i_flag = 0; 21337736Smckusick /* 21437736Smckusick * Reinitialize aliased inode. 21537736Smckusick * We must release the buffer that we just read 21637736Smckusick * before doing the iupdat() to avoid a possible 21737736Smckusick * deadlock with updating an inode in the same 21837736Smckusick * disk block. 21937736Smckusick */ 22037736Smckusick ip = iq; 22137736Smckusick vp = ITOV(iq); 22237736Smckusick tdip.di_ic = dp->di_ic; 22337736Smckusick brelse(bp); 22437736Smckusick error = iupdat(ip, &time, &time, 1); 22537736Smckusick ip->i_ic = tdip.di_ic; 22637736Smckusick remque(ip); 22737736Smckusick insque(ip, ih); 22837736Smckusick ip->i_dev = dev; 22937736Smckusick ip->i_number = ino; 23037736Smckusick if (ip->i_devvp) { 23137736Smckusick vrele(ip->i_devvp); 23237736Smckusick ip->i_devvp = 0; 23337736Smckusick } 23437736Smckusick cache_purge(vp); 23537736Smckusick break; 23637736Smckusick } 23737736Smckusick if (iq == 0) { 23837736Smckusick ip->i_ic = dp->di_ic; 23937736Smckusick brelse(bp); 24037736Smckusick ip->i_devlst = bdevlisth; 24137736Smckusick bdevlisth = ip; 24237736Smckusick } 24337736Smckusick } 24437736Smckusick /* 24537736Smckusick * Finish inode initialization. 24637736Smckusick */ 24737736Smckusick ip->i_fs = fs; 24837736Smckusick ip->i_devvp = VFSTOUFS(mntp)->um_devvp; 249*38345Smckusick VREF(ip->i_devvp); 25037736Smckusick /* 25137736Smckusick * Initialize the associated vnode 25237736Smckusick */ 25337736Smckusick vp = ITOV(ip); 25437736Smckusick vinit(vp, mntp, IFTOVT(ip->i_mode), &ufs_vnodeops); 25537736Smckusick if (vp->v_type == VCHR || vp->v_type == VBLK) { 25637736Smckusick vp->v_rdev = ip->i_rdev; 25737736Smckusick vp->v_op = &blk_vnodeops; 25837736Smckusick } 25937736Smckusick if (ino == ROOTINO) 26037736Smckusick vp->v_flag |= VROOT; 26137736Smckusick #ifdef QUOTA 26237736Smckusick if (ip->i_mode != 0) 26337736Smckusick ip->i_dquot = inoquota(ip); 26437736Smckusick #endif 26538256Smckusick /* 26638256Smckusick * Set up a generation number for this inode if it does not 26738256Smckusick * already have one. This should only happen on old filesystems. 26838256Smckusick */ 26938256Smckusick if (ip->i_gen == 0) { 27038256Smckusick if (++nextgennumber < (u_long)time.tv_sec) 27138256Smckusick nextgennumber = time.tv_sec; 27238256Smckusick ip->i_gen = nextgennumber; 27338256Smckusick if ((vp->v_mount->m_flag & M_RDONLY) == 0) 27438256Smckusick ip->i_flag |= IMOD; 27538256Smckusick } 27637736Smckusick *ipp = ip; 27737736Smckusick return (0); 27837736Smckusick } 2797334Skre 28037736Smckusick /* 28137736Smckusick * Allocate a new inode. 28237736Smckusick * 28337736Smckusick * Put it onto its hash chain and lock it so that other requests for 28437736Smckusick * this inode will block if they arrive while we are sleeping waiting 28537736Smckusick * for old data structures to be purged or for the contents of the disk 28637736Smckusick * portion of this inode to be read. 28737736Smckusick */ 28837736Smckusick getnewino(dev, ino, ipp) 28937736Smckusick dev_t dev; 29037736Smckusick ino_t ino; 29137736Smckusick struct inode **ipp; 29237736Smckusick { 29337736Smckusick union ihead *ih; 29437736Smckusick register struct inode *ip, *iq; 29537736Smckusick register struct vnode *vp; 29637736Smckusick 29737736Smckusick /* 29837736Smckusick * Remove the next inode from the free list. 29937736Smckusick */ 3007334Skre if ((ip = ifreeh) == NULL) { 3012933Swnj tablefull("inode"); 30237736Smckusick *ipp = 0; 30337736Smckusick return(ENFILE); 30424Sbill } 30537736Smckusick vp = ITOV(ip); 30637736Smckusick if (vp->v_count) 30716720Skarels panic("free inode isn't"); 3087334Skre if (iq = ip->i_freef) 3097334Skre iq->i_freeb = &ifreeh; 3107334Skre ifreeh = iq; 3117334Skre ip->i_freef = NULL; 3127334Skre ip->i_freeb = NULL; 3137334Skre /* 3147334Skre * Now to take inode off the hash chain it was on 3157334Skre * (initially, or after an iflush, it is on a "hash chain" 31637736Smckusick * consisting entirely of itself, and pointed to by no-one) 31737736Smckusick * and put it on the chain for its new (ino, dev) pair. 3187334Skre */ 3197335Skre remque(ip); 32024Sbill ip->i_dev = dev; 32124Sbill ip->i_number = ino; 32237736Smckusick if (dev != NODEV) { 32337736Smckusick ih = &ihead[INOHASH(dev, ino)]; 32437736Smckusick insque(ip, ih); 32537736Smckusick } 32638226Smckusick ip->i_flag = 0; 32738226Smckusick ILOCK(ip); 3286569Smckusic ip->i_lastr = 0; 32924Sbill /* 33037736Smckusick * Purge old data structures associated with the inode. 33124Sbill */ 33237736Smckusick cache_purge(vp); 33337736Smckusick if (ip->i_devvp) { 33437736Smckusick vrele(ip->i_devvp); 33537736Smckusick ip->i_devvp = 0; 33637736Smckusick } 3377651Ssam #ifdef QUOTA 33837736Smckusick dqrele(ip->i_dquot); 33937736Smckusick ip->i_dquot = NODQUOT; 3407492Skre #endif 34137736Smckusick if (vp->v_type == VBLK) { 34237736Smckusick if (bdevlisth == ip) { 34337736Smckusick bdevlisth = ip->i_devlst; 34437736Smckusick } else { 34537736Smckusick for (iq = bdevlisth; iq; iq = iq->i_devlst) { 34637736Smckusick if (iq->i_devlst != ip) 34737736Smckusick continue; 34837736Smckusick iq->i_devlst = ip->i_devlst; 34937736Smckusick break; 35037736Smckusick } 35137736Smckusick if (iq == NULL) 35237736Smckusick panic("missing bdev"); 35337736Smckusick } 35424Sbill } 35537736Smckusick *ipp = ip; 35637736Smckusick return (0); 35724Sbill } 35824Sbill 35924Sbill /* 36016642Ssam * Convert a pointer to an inode into a reference to an inode. 36116642Ssam * 36216642Ssam * This is basically the internal piece of iget (after the 36316642Ssam * inode pointer is located) but without the test for mounted 36416642Ssam * filesystems. It is caller's responsibility to check that 36516642Ssam * the inode pointer is valid. 36616642Ssam */ 36716642Ssam igrab(ip) 36816642Ssam register struct inode *ip; 36916642Ssam { 37037736Smckusick register struct vnode *vp = ITOV(ip); 37137736Smckusick 37216642Ssam while ((ip->i_flag&ILOCKED) != 0) { 37316642Ssam ip->i_flag |= IWANT; 37416642Ssam sleep((caddr_t)ip, PINOD); 37516642Ssam } 37637736Smckusick if (vp->v_count == 0) { /* ino on free list */ 37716642Ssam register struct inode *iq; 37816642Ssam 37916642Ssam if (iq = ip->i_freef) 38016642Ssam iq->i_freeb = ip->i_freeb; 38116642Ssam else 38216642Ssam ifreet = ip->i_freeb; 38316642Ssam *ip->i_freeb = iq; 38416642Ssam ip->i_freef = NULL; 38516642Ssam ip->i_freeb = NULL; 38616642Ssam } 387*38345Smckusick VREF(vp); 38838226Smckusick ILOCK(ip); 38916642Ssam } 39016642Ssam 39116642Ssam /* 39237736Smckusick * Create a vnode for a block device. 39337736Smckusick * Used for root filesystem, argdev, and swap areas. 39437736Smckusick */ 39537736Smckusick bdevvp(dev, vpp) 39637736Smckusick dev_t dev; 39737736Smckusick struct vnode **vpp; 39837736Smckusick { 39937736Smckusick register struct inode *ip; 40037736Smckusick register struct vnode *vp; 40137736Smckusick struct inode *nip; 40237736Smckusick int error; 40337736Smckusick 40437736Smckusick /* 40537736Smckusick * Check for the existence of an existing vnode. 40637736Smckusick */ 40737736Smckusick again: 40837736Smckusick for (ip = bdevlisth; ip; ip = ip->i_devlst) { 40937736Smckusick vp = ITOV(ip); 41037736Smckusick if (dev != vp->v_rdev) 41137736Smckusick continue; 41237736Smckusick igrab(ip); 41337736Smckusick if (dev != vp->v_rdev) { 41437736Smckusick iput(ip); 41537736Smckusick goto again; 41637736Smckusick } 41737736Smckusick IUNLOCK(ip); 41837736Smckusick *vpp = vp; 41937736Smckusick return (0); 42037736Smckusick } 42137736Smckusick if (error = getnewino(NODEV, (ino_t)0, &nip)) { 42237736Smckusick *vpp = 0; 42337736Smckusick return (error); 42437736Smckusick } 42537736Smckusick ip = nip; 42637736Smckusick ip->i_fs = 0; 42737736Smckusick ip->i_devlst = bdevlisth; 42837736Smckusick bdevlisth = ip; 42937736Smckusick vp = ITOV(ip); 43037736Smckusick vinit(vp, 0, VBLK, &blk_vnodeops); 43137736Smckusick vp->v_rdev = dev; 43237736Smckusick IUNLOCK(ip); 43337736Smckusick *vpp = vp; 43437736Smckusick return (0); 43537736Smckusick } 43637736Smckusick 43737736Smckusick /* 43824Sbill * Decrement reference count of 43924Sbill * an inode structure. 44024Sbill * On the last reference, 44124Sbill * write the inode out and if necessary, 44224Sbill * truncate and deallocate the file. 44324Sbill */ 44424Sbill iput(ip) 4454818Swnj register struct inode *ip; 44624Sbill { 4477118Smckusick 4488452Sroot if ((ip->i_flag & ILOCKED) == 0) 4497118Smckusick panic("iput"); 45016665Smckusick IUNLOCK(ip); 45137736Smckusick vrele(ITOV(ip)); 4527118Smckusick } 4537118Smckusick 45437736Smckusick 45537736Smckusick ufs_inactive(vp) 45637736Smckusick struct vnode *vp; 4577118Smckusick { 45837736Smckusick register struct inode *ip = VTOI(vp); 45937736Smckusick int mode, error; 46024Sbill 46137736Smckusick if (ITOV(ip)->v_count != 0) 46237736Smckusick panic("ufs_inactive: not inactive"); 46338226Smckusick ILOCK(ip); 46437736Smckusick if (ip->i_nlink <= 0 && (ITOV(ip)->v_mount->m_flag&M_RDONLY) == 0) { 46537736Smckusick error = itrunc(ip, (u_long)0); 46637736Smckusick mode = ip->i_mode; 46737736Smckusick ip->i_mode = 0; 46837736Smckusick ip->i_rdev = 0; 46937736Smckusick ip->i_flag |= IUPD|ICHG; 47037736Smckusick ifree(ip, ip->i_number, mode); 4717651Ssam #ifdef QUOTA 47237736Smckusick (void) chkiq(ip->i_dev, ip, ip->i_uid, 0); 47337736Smckusick dqrele(ip->i_dquot); 47437736Smckusick ip->i_dquot = NODQUOT; 4757492Skre #endif 47637736Smckusick } 47737736Smckusick IUPDAT(ip, &time, &time, 0); 47837736Smckusick IUNLOCK(ip); 47937736Smckusick ip->i_flag = 0; 48037736Smckusick /* 48137736Smckusick * Put the inode on the end of the free list. 48237736Smckusick * Possibly in some cases it would be better to 48337736Smckusick * put the inode at the head of the free list, 48437736Smckusick * (eg: where i_mode == 0 || i_number == 0). 48537736Smckusick */ 48637736Smckusick INSFREE(ip); 48737736Smckusick return (error); 48824Sbill } 48924Sbill 49024Sbill /* 49124Sbill * Check accessed and update flags on 49224Sbill * an inode structure. 49324Sbill * If any is on, update the inode 49424Sbill * with the current time. 4951203Sbill * If waitfor is given, then must insure 4961203Sbill * i/o order so wait for write to complete. 49724Sbill */ 4981203Sbill iupdat(ip, ta, tm, waitfor) 4994818Swnj register struct inode *ip; 5008630Sroot struct timeval *ta, *tm; 5014818Swnj int waitfor; 50224Sbill { 50337736Smckusick struct buf *bp; 50437736Smckusick struct vnode *vp = ITOV(ip); 50524Sbill struct dinode *dp; 50630749Skarels register struct fs *fs; 50737736Smckusick int error; 50824Sbill 50930749Skarels fs = ip->i_fs; 51037736Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 51137736Smckusick return (0); 51237736Smckusick if (vp->v_mount->m_flag & M_RDONLY) 51337736Smckusick return (0); 51437736Smckusick error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), 51537736Smckusick (int)fs->fs_bsize, &bp); 51637736Smckusick if (error) { 51737736Smckusick brelse(bp); 51837736Smckusick return (error); 51924Sbill } 52037736Smckusick if (ip->i_flag&IACC) 52137736Smckusick ip->i_atime = ta->tv_sec; 52237736Smckusick if (ip->i_flag&IUPD) 52337736Smckusick ip->i_mtime = tm->tv_sec; 52437736Smckusick if (ip->i_flag&ICHG) 52537736Smckusick ip->i_ctime = time.tv_sec; 52637736Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 52737736Smckusick dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 52837736Smckusick dp->di_ic = ip->i_ic; 52937736Smckusick if (waitfor) { 53037736Smckusick return (bwrite(bp)); 53137736Smckusick } else { 53237736Smckusick bdwrite(bp); 53337736Smckusick return (0); 53437736Smckusick } 53524Sbill } 53624Sbill 53710736Ssam #define SINGLE 0 /* index of single indirect block */ 53810736Ssam #define DOUBLE 1 /* index of double indirect block */ 53910736Ssam #define TRIPLE 2 /* index of triple indirect block */ 54024Sbill /* 5417702Ssam * Truncate the inode ip to at most 5427702Ssam * length size. Free affected disk 5437702Ssam * blocks -- the blocks of the file 5447702Ssam * are removed in reverse order. 54510736Ssam * 54610736Ssam * NB: triple indirect blocks are untested. 54724Sbill */ 54810736Ssam itrunc(oip, length) 54917942Smckusick register struct inode *oip; 5509165Ssam u_long length; 55124Sbill { 5529165Ssam register daddr_t lastblock; 55326272Skarels daddr_t bn, lbn, lastiblock[NIADDR]; 5546569Smckusic register struct fs *fs; 55510736Ssam register struct inode *ip; 55617942Smckusick struct buf *bp; 55737736Smckusick int offset, osize, size, level; 55837736Smckusick long count, nblocks, blocksreleased = 0; 55917942Smckusick register int i; 56037736Smckusick int error, allerror = 0; 56110736Ssam struct inode tip; 5629165Ssam 56313000Ssam if (oip->i_size <= length) { 56413000Ssam oip->i_flag |= ICHG|IUPD; 56537736Smckusick error = iupdat(oip, &time, &time, 1); 56637736Smckusick return (error); 56713000Ssam } 5681203Sbill /* 56910736Ssam * Calculate index into inode's block list of 57010736Ssam * last direct and indirect blocks (if any) 57110736Ssam * which we want to keep. Lastblock is -1 when 57210736Ssam * the file is truncated to 0. 5731203Sbill */ 57410736Ssam fs = oip->i_fs; 5759165Ssam lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 57610736Ssam lastiblock[SINGLE] = lastblock - NDADDR; 57710736Ssam lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 57810736Ssam lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 57912645Ssam nblocks = btodb(fs->fs_bsize); 5806569Smckusic /* 58117942Smckusick * Update the size of the file. If the file is not being 58217942Smckusick * truncated to a block boundry, the contents of the 58317942Smckusick * partial block following the end of the file must be 58417942Smckusick * zero'ed in case it ever become accessable again because 58517942Smckusick * of subsequent file growth. 58617942Smckusick */ 58717942Smckusick osize = oip->i_size; 58817942Smckusick offset = blkoff(fs, length); 58917942Smckusick if (offset == 0) { 59017942Smckusick oip->i_size = length; 59117942Smckusick } else { 59217942Smckusick lbn = lblkno(fs, length); 59337736Smckusick error = balloc(oip, lbn, offset, &bn, B_CLRBUF); 59437736Smckusick if (error) 59537736Smckusick return (error); 59637736Smckusick if ((long)bn < 0) 59737736Smckusick panic("itrunc: hole"); 59817942Smckusick oip->i_size = length; 59917942Smckusick size = blksize(fs, oip, lbn); 60030749Skarels count = howmany(size, CLBYTES); 60130749Skarels for (i = 0; i < count; i++) 60237736Smckusick munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 60337736Smckusick error = bread(oip->i_devvp, bn, size, &bp); 60437736Smckusick if (error) { 60517942Smckusick oip->i_size = osize; 60617942Smckusick brelse(bp); 60737736Smckusick return (error); 60817942Smckusick } 60926272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 61017942Smckusick bdwrite(bp); 61117942Smckusick } 61217942Smckusick /* 61317942Smckusick * Update file and block pointers 61410736Ssam * on disk before we start freeing blocks. 61510736Ssam * If we crash before free'ing blocks below, 61610736Ssam * the blocks will be returned to the free list. 61710736Ssam * lastiblock values are also normalized to -1 61810736Ssam * for calls to indirtrunc below. 6196569Smckusic */ 62010736Ssam tip = *oip; 62117942Smckusick tip.i_size = osize; 62210736Ssam for (level = TRIPLE; level >= SINGLE; level--) 62310736Ssam if (lastiblock[level] < 0) { 62410736Ssam oip->i_ib[level] = 0; 62510736Ssam lastiblock[level] = -1; 6269165Ssam } 62710736Ssam for (i = NDADDR - 1; i > lastblock; i--) 62810736Ssam oip->i_db[i] = 0; 62910736Ssam oip->i_flag |= ICHG|IUPD; 63037736Smckusick allerror = syncip(oip); 63110736Ssam 6326569Smckusic /* 63310736Ssam * Indirect blocks first. 6346569Smckusic */ 63517942Smckusick ip = &tip; 63610736Ssam for (level = TRIPLE; level >= SINGLE; level--) { 63710736Ssam bn = ip->i_ib[level]; 6389165Ssam if (bn != 0) { 63937736Smckusick error = indirtrunc(ip, bn, lastiblock[level], level, 64037736Smckusick &count); 64137736Smckusick if (error) 64237736Smckusick allerror = error; 64337736Smckusick blocksreleased += count; 64410736Ssam if (lastiblock[level] < 0) { 64510736Ssam ip->i_ib[level] = 0; 64631402Smckusick blkfree(ip, bn, (off_t)fs->fs_bsize); 64710736Ssam blocksreleased += nblocks; 64810736Ssam } 64910736Ssam } 65010736Ssam if (lastiblock[level] >= 0) 65110736Ssam goto done; 6529165Ssam } 65310736Ssam 6546569Smckusic /* 65510736Ssam * All whole direct blocks or frags. 6566569Smckusic */ 6579165Ssam for (i = NDADDR - 1; i > lastblock; i--) { 65826359Skarels register off_t bsize; 6599165Ssam 6606569Smckusic bn = ip->i_db[i]; 6619165Ssam if (bn == 0) 66224Sbill continue; 6639165Ssam ip->i_db[i] = 0; 66424525Sbloom bsize = (off_t)blksize(fs, ip, i); 66531402Smckusick blkfree(ip, bn, bsize); 66624525Sbloom blocksreleased += btodb(bsize); 66724Sbill } 66810736Ssam if (lastblock < 0) 66910736Ssam goto done; 67010736Ssam 6711203Sbill /* 6729165Ssam * Finally, look for a change in size of the 6739165Ssam * last direct block; release any frags. 6741203Sbill */ 67510736Ssam bn = ip->i_db[lastblock]; 67610736Ssam if (bn != 0) { 67726359Skarels off_t oldspace, newspace; 67810736Ssam 6799165Ssam /* 6809165Ssam * Calculate amount of space we're giving 6819165Ssam * back as old block size minus new block size. 6829165Ssam */ 68310736Ssam oldspace = blksize(fs, ip, lastblock); 6849165Ssam ip->i_size = length; 68510736Ssam newspace = blksize(fs, ip, lastblock); 68610736Ssam if (newspace == 0) 68710736Ssam panic("itrunc: newspace"); 68810736Ssam if (oldspace - newspace > 0) { 6899165Ssam /* 6909165Ssam * Block number of space to be free'd is 6919165Ssam * the old block # plus the number of frags 6929165Ssam * required for the storage we're keeping. 6939165Ssam */ 69410736Ssam bn += numfrags(fs, newspace); 69531402Smckusick blkfree(ip, bn, oldspace - newspace); 69612645Ssam blocksreleased += btodb(oldspace - newspace); 6979165Ssam } 6989165Ssam } 6999165Ssam done: 70010736Ssam /* BEGIN PARANOIA */ 70110736Ssam for (level = SINGLE; level <= TRIPLE; level++) 70210736Ssam if (ip->i_ib[level] != oip->i_ib[level]) 70310736Ssam panic("itrunc1"); 70410736Ssam for (i = 0; i < NDADDR; i++) 70510736Ssam if (ip->i_db[i] != oip->i_db[i]) 70610736Ssam panic("itrunc2"); 70710736Ssam /* END PARANOIA */ 70812645Ssam oip->i_blocks -= blocksreleased; 70912645Ssam if (oip->i_blocks < 0) /* sanity */ 71012645Ssam oip->i_blocks = 0; 71112645Ssam oip->i_flag |= ICHG; 7129165Ssam #ifdef QUOTA 71312645Ssam (void) chkdq(oip, -blocksreleased, 0); 7149165Ssam #endif 71537736Smckusick return (allerror); 71624Sbill } 71724Sbill 7189165Ssam /* 7199165Ssam * Release blocks associated with the inode ip and 7209165Ssam * stored in the indirect block bn. Blocks are free'd 7219165Ssam * in LIFO order up to (but not including) lastbn. If 72210736Ssam * level is greater than SINGLE, the block is an indirect 72310736Ssam * block and recursive calls to indirtrunc must be used to 72410736Ssam * cleanse other indirect blocks. 72510736Ssam * 72610736Ssam * NB: triple indirect blocks are untested. 7279165Ssam */ 72837736Smckusick indirtrunc(ip, bn, lastbn, level, countp) 7296569Smckusic register struct inode *ip; 7309165Ssam daddr_t bn, lastbn; 73110736Ssam int level; 73237736Smckusick long *countp; 73324Sbill { 7349165Ssam register int i; 73531661Smckusick struct buf *bp; 73631661Smckusick register struct fs *fs = ip->i_fs; 73724Sbill register daddr_t *bap; 73831661Smckusick daddr_t *copy, nb, last; 73937736Smckusick long blkcount, factor; 74037736Smckusick int nblocks, blocksreleased = 0; 74137736Smckusick int error, allerror = 0; 74224Sbill 74310736Ssam /* 74410736Ssam * Calculate index in current block of last 74510736Ssam * block to be kept. -1 indicates the entire 74610736Ssam * block so we need not calculate the index. 74710736Ssam */ 74810736Ssam factor = 1; 74910736Ssam for (i = SINGLE; i < level; i++) 75010736Ssam factor *= NINDIR(fs); 7519165Ssam last = lastbn; 75210736Ssam if (lastbn > 0) 75310736Ssam last /= factor; 75412645Ssam nblocks = btodb(fs->fs_bsize); 75510736Ssam /* 75610736Ssam * Get buffer of block pointers, zero those 75710736Ssam * entries corresponding to blocks to be free'd, 75810736Ssam * and update on disk copy first. 75910736Ssam */ 76037736Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, &bp); 76137736Smckusick if (error) { 76210736Ssam brelse(bp); 76337736Smckusick *countp = 0; 76437736Smckusick return (error); 76510736Ssam } 76610736Ssam bap = bp->b_un.b_daddr; 76731661Smckusick MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 76831661Smckusick bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 76910736Ssam bzero((caddr_t)&bap[last + 1], 77010736Ssam (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 77137736Smckusick error = bwrite(bp); 77237736Smckusick if (error) 77337736Smckusick allerror = error; 77431661Smckusick bap = copy; 77510736Ssam 77610736Ssam /* 77710736Ssam * Recursively free totally unused blocks. 77810736Ssam */ 7799165Ssam for (i = NINDIR(fs) - 1; i > last; i--) { 78024Sbill nb = bap[i]; 7819165Ssam if (nb == 0) 78224Sbill continue; 78337736Smckusick if (level > SINGLE) { 78437736Smckusick error = indirtrunc(ip, nb, (daddr_t)-1, level - 1, 78537736Smckusick &blkcount); 78637736Smckusick if (error) 78737736Smckusick allerror = error; 78837736Smckusick blocksreleased += blkcount; 78937736Smckusick } 79031402Smckusick blkfree(ip, nb, (off_t)fs->fs_bsize); 7919165Ssam blocksreleased += nblocks; 79224Sbill } 79310736Ssam 79410736Ssam /* 79510736Ssam * Recursively free last partial block. 79610736Ssam */ 79710736Ssam if (level > SINGLE && lastbn >= 0) { 79810736Ssam last = lastbn % factor; 7999165Ssam nb = bap[i]; 80037736Smckusick if (nb != 0) { 80137736Smckusick error = indirtrunc(ip, nb, last, level - 1, &blkcount); 80237736Smckusick if (error) 80337736Smckusick allerror = error; 80437736Smckusick blocksreleased += blkcount; 80537736Smckusick } 8069165Ssam } 80731661Smckusick FREE(copy, M_TEMP); 80837736Smckusick *countp = blocksreleased; 80937736Smckusick return (allerror); 81024Sbill } 81124Sbill 81224Sbill /* 81330749Skarels * Remove any inodes in the inode cache belonging to dev. 8147334Skre * 8157334Skre * There should not be any active ones, return error if any are found 81630749Skarels * (nb: this is a user error, not a system err). 8177334Skre */ 8187651Ssam #ifdef QUOTA 8197504Sroot iflush(dev, iq) 8207492Skre dev_t dev; 8217504Sroot struct inode *iq; 8227492Skre #else 8237334Skre iflush(dev) 8247334Skre dev_t dev; 8257492Skre #endif 8267334Skre { 8277335Skre register struct inode *ip; 8287334Skre 8297334Skre for (ip = inode; ip < inodeNINODE; ip++) { 8307651Ssam #ifdef QUOTA 8317492Skre if (ip != iq && ip->i_dev == dev) 8327492Skre #else 8337334Skre if (ip->i_dev == dev) 8347492Skre #endif 83537736Smckusick if (ITOV(ip)->v_count) 83630749Skarels return (EBUSY); 8377334Skre else { 8387335Skre remque(ip); 8397334Skre ip->i_forw = ip; 8407334Skre ip->i_back = ip; 8417334Skre /* 84237736Smckusick * as v_count == 0, the inode was on the free 8437334Skre * list already, just leave it there, it will 8447334Skre * fall off the bottom eventually. We could 8457334Skre * perhaps move it to the head of the free 8467334Skre * list, but as umounts are done so 8477334Skre * infrequently, we would gain very little, 8487334Skre * while making the code bigger. 8497334Skre */ 8507651Ssam #ifdef QUOTA 8517492Skre dqrele(ip->i_dquot); 8527492Skre ip->i_dquot = NODQUOT; 8537492Skre #endif 85437736Smckusick if (ip->i_devvp) { 85537736Smckusick vrele(ip->i_devvp); 85637736Smckusick ip->i_devvp = 0; 85737736Smckusick } 8587334Skre } 8597334Skre } 86030749Skarels return (0); 8617334Skre } 8627334Skre 8633617Sroot /* 8644818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 8653617Sroot */ 8664818Swnj ilock(ip) 8674818Swnj register struct inode *ip; 8683617Sroot { 8693617Sroot 87037736Smckusick while (ip->i_flag & ILOCKED) { 87137736Smckusick ip->i_flag |= IWANT; 87237736Smckusick (void) sleep((caddr_t)ip, PINOD); 87337736Smckusick } 87437736Smckusick ip->i_flag |= ILOCKED; 8753617Sroot } 8763617Sroot 8773617Sroot /* 8784818Swnj * Unlock an inode. If WANT bit is on, wakeup. 8793617Sroot */ 8807118Smckusick iunlock(ip) 8814818Swnj register struct inode *ip; 8823617Sroot { 8833617Sroot 88437736Smckusick if ((ip->i_flag & ILOCKED) == 0) 88537736Smckusick printf("unlocking unlocked inode %d on dev 0x%x\n", 88637736Smckusick ip->i_number, ip->i_dev); 88737736Smckusick ip->i_flag &= ~ILOCKED; 88837736Smckusick if (ip->i_flag&IWANT) { 88937736Smckusick ip->i_flag &= ~IWANT; 89037736Smckusick wakeup((caddr_t)ip); 89137736Smckusick } 8923617Sroot } 89337736Smckusick 89437736Smckusick /* 89537736Smckusick * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. 89637736Smckusick * The mode is shifted to select the owner/group/other fields. The 89737736Smckusick * super user is granted all permissions. 89837736Smckusick * 89937736Smckusick * NB: Called from vnode op table. It seems this could all be done 90037736Smckusick * using vattr's but... 90137736Smckusick */ 90237736Smckusick iaccess(ip, mode, cred) 90337736Smckusick register struct inode *ip; 90437736Smckusick register int mode; 90537736Smckusick struct ucred *cred; 90637736Smckusick { 90737736Smckusick register gid_t *gp; 90837736Smckusick register struct vnode *vp = ITOV(ip); 90937736Smckusick int i; 91037736Smckusick 91137736Smckusick /* 91237736Smckusick * If you're the super-user, 91337736Smckusick * you always get access. 91437736Smckusick */ 91537736Smckusick if (cred->cr_uid == 0) 91637736Smckusick return (0); 91737736Smckusick /* 91837736Smckusick * Access check is based on only one of owner, group, public. 91937736Smckusick * If not owner, then check group. If not a member of the 92037736Smckusick * group, then check public access. 92137736Smckusick */ 92237736Smckusick if (cred->cr_uid != ip->i_uid) { 92337736Smckusick mode >>= 3; 92437736Smckusick gp = cred->cr_groups; 92537736Smckusick for (i = 0; i < cred->cr_ngroups; i++, gp++) 92637736Smckusick if (ip->i_gid == *gp) 92737736Smckusick goto found; 92837736Smckusick mode >>= 3; 92937736Smckusick found: 93037736Smckusick ; 93137736Smckusick } 93237736Smckusick if ((ip->i_mode & mode) != 0) 93337736Smckusick return (0); 93437736Smckusick return (EACCES); 93537736Smckusick } 936