| 1 | /* |
| 2 | * Copyright (c) 1991 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | * |
| 7 | * @(#)ufs_inode.c 7.42 (Berkeley) %G% |
| 8 | */ |
| 9 | |
| 10 | #include <sys/param.h> |
| 11 | #include <sys/systm.h> |
| 12 | #include <sys/proc.h> |
| 13 | #include <sys/vnode.h> |
| 14 | #include <sys/mount.h> |
| 15 | #include <sys/kernel.h> |
| 16 | |
| 17 | #include <ufs/ufs/quota.h> |
| 18 | #include <ufs/ufs/inode.h> |
| 19 | #include <ufs/ufs/ufsmount.h> |
| 20 | #include <ufs/ufs/ufs_extern.h> |
| 21 | |
| 22 | u_long nextgennumber; /* Next generation number to assign. */ |
| 23 | int prtactive = 0; /* 1 => print out reclaim of active vnodes */ |
| 24 | |
| 25 | int |
| 26 | ufs_init() |
| 27 | { |
| 28 | static int first = 1; |
| 29 | |
| 30 | if (!first) |
| 31 | return (0); |
| 32 | first = 0; |
| 33 | |
| 34 | #ifdef DIAGNOSTIC |
| 35 | if (VN_MAXPRIVATE < sizeof(struct inode)) |
| 36 | panic("ufs_init: inode too small"); |
| 37 | #endif |
| 38 | ufs_ihashinit(); |
| 39 | dqinit(); |
| 40 | return (0); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * Unlock and decrement the reference count of an inode structure. |
| 45 | */ |
| 46 | void |
| 47 | ufs_iput(ip) |
| 48 | register struct inode *ip; |
| 49 | { |
| 50 | |
| 51 | if ((ip->i_flag & ILOCKED) == 0) |
| 52 | panic("iput"); |
| 53 | IUNLOCK(ip); |
| 54 | vrele(ITOV(ip)); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Reclaim an inode so that it can be used for other purposes. |
| 59 | */ |
| 60 | int |
| 61 | ufs_reclaim(vp) |
| 62 | register struct vnode *vp; |
| 63 | { |
| 64 | register struct inode *ip; |
| 65 | int i; |
| 66 | |
| 67 | if (prtactive && vp->v_usecount != 0) |
| 68 | vprint("ufs_reclaim: pushing active", vp); |
| 69 | /* |
| 70 | * Remove the inode from its hash chain. |
| 71 | */ |
| 72 | ip = VTOI(vp); |
| 73 | remque(ip); |
| 74 | ip->i_forw = ip; |
| 75 | ip->i_back = ip; |
| 76 | /* |
| 77 | * Purge old data structures associated with the inode. |
| 78 | */ |
| 79 | cache_purge(vp); |
| 80 | if (ip->i_devvp) { |
| 81 | vrele(ip->i_devvp); |
| 82 | ip->i_devvp = 0; |
| 83 | } |
| 84 | #ifdef QUOTA |
| 85 | for (i = 0; i < MAXQUOTAS; i++) { |
| 86 | if (ip->i_dquot[i] != NODQUOT) { |
| 87 | dqrele(vp, ip->i_dquot[i]); |
| 88 | ip->i_dquot[i] = NODQUOT; |
| 89 | } |
| 90 | } |
| 91 | #endif |
| 92 | ip->i_flag = 0; |
| 93 | return (0); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Lock an inode. If its already locked, set the WANT bit and sleep. |
| 98 | */ |
| 99 | void |
| 100 | ufs_ilock(ip) |
| 101 | register struct inode *ip; |
| 102 | { |
| 103 | |
| 104 | while (ip->i_flag & ILOCKED) { |
| 105 | ip->i_flag |= IWANT; |
| 106 | if (ip->i_spare0 == curproc->p_pid) |
| 107 | panic("locking against myself"); |
| 108 | ip->i_spare1 = curproc->p_pid; |
| 109 | (void) sleep((caddr_t)ip, PINOD); |
| 110 | } |
| 111 | ip->i_spare1 = 0; |
| 112 | ip->i_spare0 = curproc->p_pid; |
| 113 | ip->i_flag |= ILOCKED; |
| 114 | curproc->p_spare[2]++; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Unlock an inode. If WANT bit is on, wakeup. |
| 119 | */ |
| 120 | void |
| 121 | ufs_iunlock(ip) |
| 122 | register struct inode *ip; |
| 123 | { |
| 124 | |
| 125 | if ((ip->i_flag & ILOCKED) == 0) |
| 126 | vprint("ufs_iunlock: unlocked inode", ITOV(ip)); |
| 127 | ip->i_spare0 = 0; |
| 128 | ip->i_flag &= ~ILOCKED; |
| 129 | curproc->p_spare[2]--; |
| 130 | if (ip->i_flag&IWANT) { |
| 131 | ip->i_flag &= ~IWANT; |
| 132 | wakeup((caddr_t)ip); |
| 133 | } |
| 134 | } |