123399Smckusick /* 251509Sbostic * Copyright (c) 1991 Regents of the University of California. 337736Smckusick * All rights reserved. 423399Smckusick * 544537Sbostic * %sccs.include.redist.c% 637736Smckusick * 7*53864Sheideman * @(#)ufs_inode.c 7.47 (Berkeley) 06/04/92 823399Smckusick */ 924Sbill 1051509Sbostic #include <sys/param.h> 1151509Sbostic #include <sys/systm.h> 1251509Sbostic #include <sys/proc.h> 1351509Sbostic #include <sys/vnode.h> 1451509Sbostic #include <sys/mount.h> 1551509Sbostic #include <sys/kernel.h> 1651987Smckusick #include <sys/malloc.h> 1724Sbill 1851509Sbostic #include <ufs/ufs/quota.h> 1951509Sbostic #include <ufs/ufs/inode.h> 2051509Sbostic #include <ufs/ufs/ufsmount.h> 2151509Sbostic #include <ufs/ufs/ufs_extern.h> 2247571Skarels 2351509Sbostic u_long nextgennumber; /* Next generation number to assign. */ 2451547Smckusick int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 2524Sbill 2651509Sbostic int 2739440Smckusick ufs_init() 2824Sbill { 2951509Sbostic static int first = 1; 3024Sbill 3151509Sbostic if (!first) 3251509Sbostic return (0); 3351509Sbostic first = 0; 3451509Sbostic 3551509Sbostic #ifdef DIAGNOSTIC 3651987Smckusick if ((sizeof(struct inode) - 1) & sizeof(struct inode)) 3751987Smckusick printf("ufs_init: bad size %d\n", sizeof(struct inode)); 3851509Sbostic #endif 3951509Sbostic ufs_ihashinit(); 4041313Smckusick dqinit(); 4137736Smckusick return (0); 4237736Smckusick } 437334Skre 4437736Smckusick /* 4539392Smckusick * Unlock and decrement the reference count of an inode structure. 4624Sbill */ 4751509Sbostic void 4851509Sbostic ufs_iput(ip) 494818Swnj register struct inode *ip; 5024Sbill { 517118Smckusick 528452Sroot if ((ip->i_flag & ILOCKED) == 0) 537118Smckusick panic("iput"); 5416665Smckusick IUNLOCK(ip); 5537736Smckusick vrele(ITOV(ip)); 567118Smckusick } 577118Smckusick 5839392Smckusick /* 5939392Smckusick * Reclaim an inode so that it can be used for other purposes. 6024Sbill */ 6151509Sbostic int 6253524Sheideman ufs_reclaim (ap) 6353524Sheideman struct vop_reclaim_args *ap; 6439392Smckusick { 65*53864Sheideman register struct vnode *vp = ap->a_vp; 6651509Sbostic register struct inode *ip; 6751987Smckusick int i, type; 6839392Smckusick 69*53864Sheideman if (prtactive && vp->v_usecount != 0) 70*53864Sheideman vprint("ufs_reclaim: pushing active", vp); 7139392Smckusick /* 7239392Smckusick * Remove the inode from its hash chain. 7339392Smckusick */ 74*53864Sheideman ip = VTOI(vp); 7539392Smckusick remque(ip); 7639392Smckusick /* 7739392Smckusick * Purge old data structures associated with the inode. 7839392Smckusick */ 79*53864Sheideman cache_purge(vp); 8039392Smckusick if (ip->i_devvp) { 8139392Smckusick vrele(ip->i_devvp); 8239392Smckusick ip->i_devvp = 0; 8339392Smckusick } 8439392Smckusick #ifdef QUOTA 8541313Smckusick for (i = 0; i < MAXQUOTAS; i++) { 8641313Smckusick if (ip->i_dquot[i] != NODQUOT) { 87*53864Sheideman dqrele(vp, ip->i_dquot[i]); 8841313Smckusick ip->i_dquot[i] = NODQUOT; 8941313Smckusick } 9041313Smckusick } 9139392Smckusick #endif 92*53864Sheideman switch (vp->v_mount->mnt_stat.f_type) { 9351987Smckusick case MOUNT_UFS: 9451987Smckusick type = M_FFSNODE; 9551987Smckusick break; 9651987Smckusick case MOUNT_MFS: 9751987Smckusick type = M_MFSNODE; 9851987Smckusick break; 9951987Smckusick case MOUNT_LFS: 10051987Smckusick type = M_LFSNODE; 10151987Smckusick break; 10251987Smckusick default: 10351987Smckusick panic("ufs_reclaim: not ufs file"); 10451987Smckusick } 105*53864Sheideman FREE(vp->v_data, type); 106*53864Sheideman vp->v_data = NULL; 10739392Smckusick return (0); 10839392Smckusick } 10939392Smckusick 11039392Smckusick /* 1114818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 1123617Sroot */ 11351509Sbostic void 11451509Sbostic ufs_ilock(ip) 1154818Swnj register struct inode *ip; 1163617Sroot { 11752037Smckusick struct proc *p = curproc; /* XXX */ 1183617Sroot 11937736Smckusick while (ip->i_flag & ILOCKED) { 12037736Smckusick ip->i_flag |= IWANT; 12152037Smckusick #ifdef DIAGNOSTIC 12252037Smckusick if (p) { 12352037Smckusick if (p->p_pid == ip->i_lockholder) 12452037Smckusick panic("locking against myself"); 12552037Smckusick ip->i_lockwaiter = p->p_pid; 12652037Smckusick } 12752037Smckusick #endif 12837736Smckusick (void) sleep((caddr_t)ip, PINOD); 12937736Smckusick } 13052037Smckusick #ifdef DIAGNOSTIC 13151987Smckusick ip->i_lockwaiter = 0; 13252037Smckusick if (p) 13352037Smckusick ip->i_lockholder = p->p_pid; 13452037Smckusick #endif 13537736Smckusick ip->i_flag |= ILOCKED; 1363617Sroot } 1373617Sroot 1383617Sroot /* 1394818Swnj * Unlock an inode. If WANT bit is on, wakeup. 1403617Sroot */ 14151509Sbostic void 14251509Sbostic ufs_iunlock(ip) 1434818Swnj register struct inode *ip; 1443617Sroot { 1453617Sroot 14637736Smckusick if ((ip->i_flag & ILOCKED) == 0) 14751509Sbostic vprint("ufs_iunlock: unlocked inode", ITOV(ip)); 14852037Smckusick #ifdef DIAGNOSTIC 14951987Smckusick ip->i_lockholder = 0; 15052037Smckusick #endif 15137736Smckusick ip->i_flag &= ~ILOCKED; 15237736Smckusick if (ip->i_flag&IWANT) { 15337736Smckusick ip->i_flag &= ~IWANT; 15437736Smckusick wakeup((caddr_t)ip); 15537736Smckusick } 1563617Sroot } 157