123399Smckusick /* 251509Sbostic * Copyright (c) 1991 Regents of the University of California. 337736Smckusick * All rights reserved. 423399Smckusick * 544537Sbostic * %sccs.include.redist.c% 637736Smckusick * 7*53524Sheideman * @(#)ufs_inode.c 7.45 (Berkeley) 05/14/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 62*53524Sheideman ufs_reclaim (ap) 63*53524Sheideman struct vop_reclaim_args *ap; 64*53524Sheideman #define vp (ap->a_vp) 6539392Smckusick { 6651509Sbostic register struct inode *ip; 6751987Smckusick int i, type; 6839392Smckusick 6939816Smckusick if (prtactive && vp->v_usecount != 0) 7039676Smckusick vprint("ufs_reclaim: pushing active", vp); 7139392Smckusick /* 7239392Smckusick * Remove the inode from its hash chain. 7339392Smckusick */ 7451509Sbostic ip = VTOI(vp); 7539392Smckusick remque(ip); 7639392Smckusick /* 7739392Smckusick * Purge old data structures associated with the inode. 7839392Smckusick */ 7939392Smckusick 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) { 8741313Smckusick dqrele(vp, ip->i_dquot[i]); 8841313Smckusick ip->i_dquot[i] = NODQUOT; 8941313Smckusick } 9041313Smckusick } 9139392Smckusick #endif 9251987Smckusick 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 } 10551987Smckusick FREE(vp->v_data, type); 10651987Smckusick vp->v_data = NULL; 10739392Smckusick return (0); 10839392Smckusick } 109*53524Sheideman #undef vp 11039392Smckusick 11139392Smckusick /* 1124818Swnj * Lock an inode. If its already locked, set the WANT bit and sleep. 1133617Sroot */ 11451509Sbostic void 11551509Sbostic ufs_ilock(ip) 1164818Swnj register struct inode *ip; 1173617Sroot { 11852037Smckusick struct proc *p = curproc; /* XXX */ 1193617Sroot 12037736Smckusick while (ip->i_flag & ILOCKED) { 12137736Smckusick ip->i_flag |= IWANT; 12252037Smckusick #ifdef DIAGNOSTIC 12352037Smckusick if (p) { 12452037Smckusick if (p->p_pid == ip->i_lockholder) 12552037Smckusick panic("locking against myself"); 12652037Smckusick ip->i_lockwaiter = p->p_pid; 12752037Smckusick } 12852037Smckusick #endif 12937736Smckusick (void) sleep((caddr_t)ip, PINOD); 13037736Smckusick } 13152037Smckusick #ifdef DIAGNOSTIC 13251987Smckusick ip->i_lockwaiter = 0; 13352037Smckusick if (p) 13452037Smckusick ip->i_lockholder = p->p_pid; 13552037Smckusick #endif 13637736Smckusick ip->i_flag |= ILOCKED; 1373617Sroot } 1383617Sroot 1393617Sroot /* 1404818Swnj * Unlock an inode. If WANT bit is on, wakeup. 1413617Sroot */ 14251509Sbostic void 14351509Sbostic ufs_iunlock(ip) 1444818Swnj register struct inode *ip; 1453617Sroot { 1463617Sroot 14737736Smckusick if ((ip->i_flag & ILOCKED) == 0) 14851509Sbostic vprint("ufs_iunlock: unlocked inode", ITOV(ip)); 14952037Smckusick #ifdef DIAGNOSTIC 15051987Smckusick ip->i_lockholder = 0; 15152037Smckusick #endif 15237736Smckusick ip->i_flag &= ~ILOCKED; 15337736Smckusick if (ip->i_flag&IWANT) { 15437736Smckusick ip->i_flag &= ~IWANT; 15537736Smckusick wakeup((caddr_t)ip); 15637736Smckusick } 1573617Sroot } 158