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*38452Smckusick * @(#)lfs_inode.c 7.10 (Berkeley) 07/16/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); 14838345Smckusick 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; 24938345Smckusick 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 } 38738345Smckusick 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"); 463*38452Smckusick /* 464*38452Smckusick * Get rid of inodes related to stale file handles. 465*38452Smckusick */ 466*38452Smckusick if (ip->i_mode == 0) 467*38452Smckusick goto freeit; 46838226Smckusick ILOCK(ip); 46937736Smckusick if (ip->i_nlink <= 0 && (ITOV(ip)->v_mount->m_flag&M_RDONLY) == 0) { 47037736Smckusick error = itrunc(ip, (u_long)0); 47137736Smckusick mode = ip->i_mode; 47237736Smckusick ip->i_mode = 0; 47337736Smckusick ip->i_rdev = 0; 47437736Smckusick ip->i_flag |= IUPD|ICHG; 47537736Smckusick ifree(ip, ip->i_number, mode); 4767651Ssam #ifdef QUOTA 47737736Smckusick (void) chkiq(ip->i_dev, ip, ip->i_uid, 0); 47837736Smckusick dqrele(ip->i_dquot); 47937736Smckusick ip->i_dquot = NODQUOT; 4807492Skre #endif 48137736Smckusick } 48237736Smckusick IUPDAT(ip, &time, &time, 0); 48337736Smckusick IUNLOCK(ip); 484*38452Smckusick freeit: 48537736Smckusick ip->i_flag = 0; 48637736Smckusick /* 48737736Smckusick * Put the inode on the end of the free list. 48837736Smckusick * Possibly in some cases it would be better to 48937736Smckusick * put the inode at the head of the free list, 49037736Smckusick * (eg: where i_mode == 0 || i_number == 0). 49137736Smckusick */ 49237736Smckusick INSFREE(ip); 49337736Smckusick return (error); 49424Sbill } 49524Sbill 49624Sbill /* 49724Sbill * Check accessed and update flags on 49824Sbill * an inode structure. 49924Sbill * If any is on, update the inode 50024Sbill * with the current time. 5011203Sbill * If waitfor is given, then must insure 5021203Sbill * i/o order so wait for write to complete. 50324Sbill */ 5041203Sbill iupdat(ip, ta, tm, waitfor) 5054818Swnj register struct inode *ip; 5068630Sroot struct timeval *ta, *tm; 5074818Swnj int waitfor; 50824Sbill { 50937736Smckusick struct buf *bp; 51037736Smckusick struct vnode *vp = ITOV(ip); 51124Sbill struct dinode *dp; 51230749Skarels register struct fs *fs; 51337736Smckusick int error; 51424Sbill 51530749Skarels fs = ip->i_fs; 51637736Smckusick if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0) 51737736Smckusick return (0); 51837736Smckusick if (vp->v_mount->m_flag & M_RDONLY) 51937736Smckusick return (0); 52037736Smckusick error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)), 52137736Smckusick (int)fs->fs_bsize, &bp); 52237736Smckusick if (error) { 52337736Smckusick brelse(bp); 52437736Smckusick return (error); 52524Sbill } 52637736Smckusick if (ip->i_flag&IACC) 52737736Smckusick ip->i_atime = ta->tv_sec; 52837736Smckusick if (ip->i_flag&IUPD) 52937736Smckusick ip->i_mtime = tm->tv_sec; 53037736Smckusick if (ip->i_flag&ICHG) 53137736Smckusick ip->i_ctime = time.tv_sec; 53237736Smckusick ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD); 53337736Smckusick dp = bp->b_un.b_dino + itoo(fs, ip->i_number); 53437736Smckusick dp->di_ic = ip->i_ic; 53537736Smckusick if (waitfor) { 53637736Smckusick return (bwrite(bp)); 53737736Smckusick } else { 53837736Smckusick bdwrite(bp); 53937736Smckusick return (0); 54037736Smckusick } 54124Sbill } 54224Sbill 54310736Ssam #define SINGLE 0 /* index of single indirect block */ 54410736Ssam #define DOUBLE 1 /* index of double indirect block */ 54510736Ssam #define TRIPLE 2 /* index of triple indirect block */ 54624Sbill /* 5477702Ssam * Truncate the inode ip to at most 5487702Ssam * length size. Free affected disk 5497702Ssam * blocks -- the blocks of the file 5507702Ssam * are removed in reverse order. 55110736Ssam * 55210736Ssam * NB: triple indirect blocks are untested. 55324Sbill */ 55410736Ssam itrunc(oip, length) 55517942Smckusick register struct inode *oip; 5569165Ssam u_long length; 55724Sbill { 5589165Ssam register daddr_t lastblock; 55926272Skarels daddr_t bn, lbn, lastiblock[NIADDR]; 5606569Smckusic register struct fs *fs; 56110736Ssam register struct inode *ip; 56217942Smckusick struct buf *bp; 56337736Smckusick int offset, osize, size, level; 56437736Smckusick long count, nblocks, blocksreleased = 0; 56517942Smckusick register int i; 56637736Smckusick int error, allerror = 0; 56710736Ssam struct inode tip; 5689165Ssam 56913000Ssam if (oip->i_size <= length) { 57013000Ssam oip->i_flag |= ICHG|IUPD; 57137736Smckusick error = iupdat(oip, &time, &time, 1); 57237736Smckusick return (error); 57313000Ssam } 5741203Sbill /* 57510736Ssam * Calculate index into inode's block list of 57610736Ssam * last direct and indirect blocks (if any) 57710736Ssam * which we want to keep. Lastblock is -1 when 57810736Ssam * the file is truncated to 0. 5791203Sbill */ 58010736Ssam fs = oip->i_fs; 5819165Ssam lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1; 58210736Ssam lastiblock[SINGLE] = lastblock - NDADDR; 58310736Ssam lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs); 58410736Ssam lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs); 58512645Ssam nblocks = btodb(fs->fs_bsize); 5866569Smckusic /* 58717942Smckusick * Update the size of the file. If the file is not being 58817942Smckusick * truncated to a block boundry, the contents of the 58917942Smckusick * partial block following the end of the file must be 59017942Smckusick * zero'ed in case it ever become accessable again because 59117942Smckusick * of subsequent file growth. 59217942Smckusick */ 59317942Smckusick osize = oip->i_size; 59417942Smckusick offset = blkoff(fs, length); 59517942Smckusick if (offset == 0) { 59617942Smckusick oip->i_size = length; 59717942Smckusick } else { 59817942Smckusick lbn = lblkno(fs, length); 59937736Smckusick error = balloc(oip, lbn, offset, &bn, B_CLRBUF); 60037736Smckusick if (error) 60137736Smckusick return (error); 60237736Smckusick if ((long)bn < 0) 60337736Smckusick panic("itrunc: hole"); 60417942Smckusick oip->i_size = length; 60517942Smckusick size = blksize(fs, oip, lbn); 60630749Skarels count = howmany(size, CLBYTES); 60730749Skarels for (i = 0; i < count; i++) 60837736Smckusick munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 60937736Smckusick error = bread(oip->i_devvp, bn, size, &bp); 61037736Smckusick if (error) { 61117942Smckusick oip->i_size = osize; 61217942Smckusick brelse(bp); 61337736Smckusick return (error); 61417942Smckusick } 61526272Skarels bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset)); 61617942Smckusick bdwrite(bp); 61717942Smckusick } 61817942Smckusick /* 61917942Smckusick * Update file and block pointers 62010736Ssam * on disk before we start freeing blocks. 62110736Ssam * If we crash before free'ing blocks below, 62210736Ssam * the blocks will be returned to the free list. 62310736Ssam * lastiblock values are also normalized to -1 62410736Ssam * for calls to indirtrunc below. 6256569Smckusic */ 62610736Ssam tip = *oip; 62717942Smckusick tip.i_size = osize; 62810736Ssam for (level = TRIPLE; level >= SINGLE; level--) 62910736Ssam if (lastiblock[level] < 0) { 63010736Ssam oip->i_ib[level] = 0; 63110736Ssam lastiblock[level] = -1; 6329165Ssam } 63310736Ssam for (i = NDADDR - 1; i > lastblock; i--) 63410736Ssam oip->i_db[i] = 0; 63510736Ssam oip->i_flag |= ICHG|IUPD; 63637736Smckusick allerror = syncip(oip); 63710736Ssam 6386569Smckusic /* 63910736Ssam * Indirect blocks first. 6406569Smckusic */ 64117942Smckusick ip = &tip; 64210736Ssam for (level = TRIPLE; level >= SINGLE; level--) { 64310736Ssam bn = ip->i_ib[level]; 6449165Ssam if (bn != 0) { 64537736Smckusick error = indirtrunc(ip, bn, lastiblock[level], level, 64637736Smckusick &count); 64737736Smckusick if (error) 64837736Smckusick allerror = error; 64937736Smckusick blocksreleased += count; 65010736Ssam if (lastiblock[level] < 0) { 65110736Ssam ip->i_ib[level] = 0; 65231402Smckusick blkfree(ip, bn, (off_t)fs->fs_bsize); 65310736Ssam blocksreleased += nblocks; 65410736Ssam } 65510736Ssam } 65610736Ssam if (lastiblock[level] >= 0) 65710736Ssam goto done; 6589165Ssam } 65910736Ssam 6606569Smckusic /* 66110736Ssam * All whole direct blocks or frags. 6626569Smckusic */ 6639165Ssam for (i = NDADDR - 1; i > lastblock; i--) { 66426359Skarels register off_t bsize; 6659165Ssam 6666569Smckusic bn = ip->i_db[i]; 6679165Ssam if (bn == 0) 66824Sbill continue; 6699165Ssam ip->i_db[i] = 0; 67024525Sbloom bsize = (off_t)blksize(fs, ip, i); 67131402Smckusick blkfree(ip, bn, bsize); 67224525Sbloom blocksreleased += btodb(bsize); 67324Sbill } 67410736Ssam if (lastblock < 0) 67510736Ssam goto done; 67610736Ssam 6771203Sbill /* 6789165Ssam * Finally, look for a change in size of the 6799165Ssam * last direct block; release any frags. 6801203Sbill */ 68110736Ssam bn = ip->i_db[lastblock]; 68210736Ssam if (bn != 0) { 68326359Skarels off_t oldspace, newspace; 68410736Ssam 6859165Ssam /* 6869165Ssam * Calculate amount of space we're giving 6879165Ssam * back as old block size minus new block size. 6889165Ssam */ 68910736Ssam oldspace = blksize(fs, ip, lastblock); 6909165Ssam ip->i_size = length; 69110736Ssam newspace = blksize(fs, ip, lastblock); 69210736Ssam if (newspace == 0) 69310736Ssam panic("itrunc: newspace"); 69410736Ssam if (oldspace - newspace > 0) { 6959165Ssam /* 6969165Ssam * Block number of space to be free'd is 6979165Ssam * the old block # plus the number of frags 6989165Ssam * required for the storage we're keeping. 6999165Ssam */ 70010736Ssam bn += numfrags(fs, newspace); 70131402Smckusick blkfree(ip, bn, oldspace - newspace); 70212645Ssam blocksreleased += btodb(oldspace - newspace); 7039165Ssam } 7049165Ssam } 7059165Ssam done: 70610736Ssam /* BEGIN PARANOIA */ 70710736Ssam for (level = SINGLE; level <= TRIPLE; level++) 70810736Ssam if (ip->i_ib[level] != oip->i_ib[level]) 70910736Ssam panic("itrunc1"); 71010736Ssam for (i = 0; i < NDADDR; i++) 71110736Ssam if (ip->i_db[i] != oip->i_db[i]) 71210736Ssam panic("itrunc2"); 71310736Ssam /* END PARANOIA */ 71412645Ssam oip->i_blocks -= blocksreleased; 71512645Ssam if (oip->i_blocks < 0) /* sanity */ 71612645Ssam oip->i_blocks = 0; 71712645Ssam oip->i_flag |= ICHG; 7189165Ssam #ifdef QUOTA 71912645Ssam (void) chkdq(oip, -blocksreleased, 0); 7209165Ssam #endif 72137736Smckusick return (allerror); 72224Sbill } 72324Sbill 7249165Ssam /* 7259165Ssam * Release blocks associated with the inode ip and 7269165Ssam * stored in the indirect block bn. Blocks are free'd 7279165Ssam * in LIFO order up to (but not including) lastbn. If 72810736Ssam * level is greater than SINGLE, the block is an indirect 72910736Ssam * block and recursive calls to indirtrunc must be used to 73010736Ssam * cleanse other indirect blocks. 73110736Ssam * 73210736Ssam * NB: triple indirect blocks are untested. 7339165Ssam */ 73437736Smckusick indirtrunc(ip, bn, lastbn, level, countp) 7356569Smckusic register struct inode *ip; 7369165Ssam daddr_t bn, lastbn; 73710736Ssam int level; 73837736Smckusick long *countp; 73924Sbill { 7409165Ssam register int i; 74131661Smckusick struct buf *bp; 74231661Smckusick register struct fs *fs = ip->i_fs; 74324Sbill register daddr_t *bap; 74431661Smckusick daddr_t *copy, nb, last; 74537736Smckusick long blkcount, factor; 74637736Smckusick int nblocks, blocksreleased = 0; 74737736Smckusick int error, allerror = 0; 74824Sbill 74910736Ssam /* 75010736Ssam * Calculate index in current block of last 75110736Ssam * block to be kept. -1 indicates the entire 75210736Ssam * block so we need not calculate the index. 75310736Ssam */ 75410736Ssam factor = 1; 75510736Ssam for (i = SINGLE; i < level; i++) 75610736Ssam factor *= NINDIR(fs); 7579165Ssam last = lastbn; 75810736Ssam if (lastbn > 0) 75910736Ssam last /= factor; 76012645Ssam nblocks = btodb(fs->fs_bsize); 76110736Ssam /* 76210736Ssam * Get buffer of block pointers, zero those 76310736Ssam * entries corresponding to blocks to be free'd, 76410736Ssam * and update on disk copy first. 76510736Ssam */ 76637736Smckusick error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, &bp); 76737736Smckusick if (error) { 76810736Ssam brelse(bp); 76937736Smckusick *countp = 0; 77037736Smckusick return (error); 77110736Ssam } 77210736Ssam bap = bp->b_un.b_daddr; 77331661Smckusick MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK); 77431661Smckusick bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize); 77510736Ssam bzero((caddr_t)&bap[last + 1], 77610736Ssam (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t)); 77737736Smckusick error = bwrite(bp); 77837736Smckusick if (error) 77937736Smckusick allerror = error; 78031661Smckusick bap = copy; 78110736Ssam 78210736Ssam /* 78310736Ssam * Recursively free totally unused blocks. 78410736Ssam */ 7859165Ssam for (i = NINDIR(fs) - 1; i > last; i--) { 78624Sbill nb = bap[i]; 7879165Ssam if (nb == 0) 78824Sbill continue; 78937736Smckusick if (level > SINGLE) { 79037736Smckusick error = indirtrunc(ip, nb, (daddr_t)-1, level - 1, 79137736Smckusick &blkcount); 79237736Smckusick if (error) 79337736Smckusick allerror = error; 79437736Smckusick blocksreleased += blkcount; 79537736Smckusick } 79631402Smckusick blkfree(ip, nb, (off_t)fs->fs_bsize); 7979165Ssam blocksreleased += nblocks; 79824Sbill } 79910736Ssam 80010736Ssam /* 80110736Ssam * Recursively free last partial block. 80210736Ssam */ 80310736Ssam if (level > SINGLE && lastbn >= 0) { 80410736Ssam last = lastbn % factor; 8059165Ssam nb = bap[i]; 80637736Smckusick if (nb != 0) { 80737736Smckusick error = indirtrunc(ip, nb, last, level - 1, &blkcount); 80837736Smckusick if (error) 80937736Smckusick allerror = error; 81037736Smckusick blocksreleased += blkcount; 81137736Smckusick } 8129165Ssam } 81331661Smckusick FREE(copy, M_TEMP); 81437736Smckusick *countp = blocksreleased; 81537736Smckusick return (allerror); 81624Sbill } 81724Sbill 81824Sbill /* 81930749Skarels * Remove any inodes in the inode cache belonging to dev. 8207334Skre * 8217334Skre * There should not be any active ones, return error if any are found 82230749Skarels * (nb: this is a user error, not a system err). 8237334Skre */ 8247651Ssam #ifdef QUOTA 8257504Sroot iflush(dev, iq) 8267492Skre dev_t dev; 8277504Sroot struct inode *iq; 8287492Skre #else 8297334Skre iflush(dev) 8307334Skre dev_t dev; 8317492Skre #endif 8327334Skre { 8337335Skre register struct inode *ip; 8347334Skre 8357334Skre for (ip = inode; ip < inodeNINODE; ip++) { 8367651Ssam #ifdef QUOTA 8377492Skre if (ip != iq && ip->i_dev == dev) 8387492Skre #else 8397334Skre if (ip->i_dev == dev) 8407492Skre #endif 84137736Smckusick if (ITOV(ip)->v_count) 84230749Skarels return (EBUSY); 8437334Skre else { 8447335Skre remque(ip); 8457334Skre ip->i_forw = ip; 8467334Skre ip->i_back = ip; 8477334Skre /* 84837736Smckusick * as v_count == 0, the inode was on the free 8497334Skre * list already, just leave it there, it will 8507334Skre * fall off the bottom eventually. We could 8517334Skre * perhaps move it to the head of the free 8527334Skre * list, but as umounts are done so 8537334Skre * infrequently, we would gain very little, 8547334Skre * while making the code bigger. 8557334Skre */ 8567651Ssam #ifdef QUOTA 8577492Skre dqrele(ip->i_dquot); 8587492Skre ip->i_dquot = NODQUOT; 8597492Skre #endif 86037736Smckusick if (ip->i_devvp) { 86137736Smckusick vrele(ip->i_devvp); 86237736Smckusick ip->i_devvp = 0; 86337736Smckusick } 8647334Skre } 8657334Skre } 86630749Skarels return (0); 8677334Skre } 8687334Skre 8693617Sroot /* 8704818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 8713617Sroot */ 8724818Swnj ilock(ip) 8734818Swnj register struct inode *ip; 8743617Sroot { 8753617Sroot 87637736Smckusick while (ip->i_flag & ILOCKED) { 87737736Smckusick ip->i_flag |= IWANT; 87837736Smckusick (void) sleep((caddr_t)ip, PINOD); 87937736Smckusick } 88037736Smckusick ip->i_flag |= ILOCKED; 8813617Sroot } 8823617Sroot 8833617Sroot /* 8844818Swnj * Unlock an inode. If WANT bit is on, wakeup. 8853617Sroot */ 8867118Smckusick iunlock(ip) 8874818Swnj register struct inode *ip; 8883617Sroot { 8893617Sroot 89037736Smckusick if ((ip->i_flag & ILOCKED) == 0) 89137736Smckusick printf("unlocking unlocked inode %d on dev 0x%x\n", 89237736Smckusick ip->i_number, ip->i_dev); 89337736Smckusick ip->i_flag &= ~ILOCKED; 89437736Smckusick if (ip->i_flag&IWANT) { 89537736Smckusick ip->i_flag &= ~IWANT; 89637736Smckusick wakeup((caddr_t)ip); 89737736Smckusick } 8983617Sroot } 89937736Smckusick 90037736Smckusick /* 90137736Smckusick * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. 90237736Smckusick * The mode is shifted to select the owner/group/other fields. The 90337736Smckusick * super user is granted all permissions. 90437736Smckusick * 90537736Smckusick * NB: Called from vnode op table. It seems this could all be done 90637736Smckusick * using vattr's but... 90737736Smckusick */ 90837736Smckusick iaccess(ip, mode, cred) 90937736Smckusick register struct inode *ip; 91037736Smckusick register int mode; 91137736Smckusick struct ucred *cred; 91237736Smckusick { 91337736Smckusick register gid_t *gp; 91437736Smckusick register struct vnode *vp = ITOV(ip); 91537736Smckusick int i; 91637736Smckusick 91737736Smckusick /* 91837736Smckusick * If you're the super-user, 91937736Smckusick * you always get access. 92037736Smckusick */ 92137736Smckusick if (cred->cr_uid == 0) 92237736Smckusick return (0); 92337736Smckusick /* 92437736Smckusick * Access check is based on only one of owner, group, public. 92537736Smckusick * If not owner, then check group. If not a member of the 92637736Smckusick * group, then check public access. 92737736Smckusick */ 92837736Smckusick if (cred->cr_uid != ip->i_uid) { 92937736Smckusick mode >>= 3; 93037736Smckusick gp = cred->cr_groups; 93137736Smckusick for (i = 0; i < cred->cr_ngroups; i++, gp++) 93237736Smckusick if (ip->i_gid == *gp) 93337736Smckusick goto found; 93437736Smckusick mode >>= 3; 93537736Smckusick found: 93637736Smckusick ; 93737736Smckusick } 93837736Smckusick if ((ip->i_mode & mode) != 0) 93937736Smckusick return (0); 94037736Smckusick return (EACCES); 94137736Smckusick } 942