1 /* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ufs_inode.c 8.3 (Berkeley) 09/23/93 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 #include <sys/malloc.h> 17 18 #include <ufs/ufs/quota.h> 19 #include <ufs/ufs/inode.h> 20 #include <ufs/ufs/ufsmount.h> 21 #include <ufs/ufs/ufs_extern.h> 22 23 u_long nextgennumber; /* Next generation number to assign. */ 24 int prtactive = 0; /* 1 => print out reclaim of active vnodes */ 25 26 int 27 ufs_init() 28 { 29 static int first = 1; 30 31 if (!first) 32 return (0); 33 first = 0; 34 35 #ifdef DIAGNOSTIC 36 if ((sizeof(struct inode) - 1) & sizeof(struct inode)) 37 printf("ufs_init: bad size %d\n", sizeof(struct inode)); 38 #endif 39 ufs_ihashinit(); 40 dqinit(); 41 return (0); 42 } 43 44 /* 45 * Last reference to an inode. If necessary, write or delete it. 46 */ 47 int 48 ufs_inactive(ap) 49 struct vop_inactive_args /* { 50 struct vnode *a_vp; 51 } */ *ap; 52 { 53 register struct vnode *vp = ap->a_vp; 54 register struct inode *ip = VTOI(vp); 55 struct timeval tv; 56 int mode, error; 57 extern int prtactive; 58 59 if (prtactive && vp->v_usecount != 0) 60 vprint("ffs_inactive: pushing active", vp); 61 62 /* Get rid of inodes related to stale file handles. */ 63 if (ip->i_mode == 0) { 64 if ((vp->v_flag & VXLOCK) == 0) 65 vgone(vp); 66 return (0); 67 } 68 69 error = 0; 70 #ifdef DIAGNOSTIC 71 if (VOP_ISLOCKED(vp)) 72 panic("ffs_inactive: locked inode"); 73 if (curproc) 74 ip->i_lockholder = curproc->p_pid; 75 else 76 ip->i_lockholder = -1; 77 #endif 78 ip->i_flag |= IN_LOCKED; 79 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 80 #ifdef QUOTA 81 if (!getinoquota(ip)) 82 (void)chkiq(ip, -1, NOCRED, 0); 83 #endif 84 error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL); 85 ip->i_rdev = 0; 86 mode = ip->i_mode; 87 ip->i_mode = 0; 88 ip->i_flag |= IN_CHANGE | IN_UPDATE; 89 VOP_VFREE(vp, ip->i_number, mode); 90 } 91 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 92 tv = time; 93 VOP_UPDATE(vp, &tv, &tv, 0); 94 } 95 VOP_UNLOCK(vp); 96 /* 97 * If we are done with the inode, reclaim it 98 * so that it can be reused immediately. 99 */ 100 if (vp->v_usecount == 0 && ip->i_mode == 0) 101 vgone(vp); 102 return (error); 103 } 104 105 /* 106 * Reclaim an inode so that it can be used for other purposes. 107 */ 108 int 109 ufs_reclaim(ap) 110 struct vop_reclaim_args /* { 111 struct vnode *a_vp; 112 } */ *ap; 113 { 114 register struct vnode *vp = ap->a_vp; 115 register struct inode *ip; 116 int i, type; 117 118 if (prtactive && vp->v_usecount != 0) 119 vprint("ufs_reclaim: pushing active", vp); 120 /* 121 * Remove the inode from its hash chain. 122 */ 123 ip = VTOI(vp); 124 ufs_ihashrem(ip); 125 /* 126 * Purge old data structures associated with the inode. 127 */ 128 cache_purge(vp); 129 if (ip->i_devvp) { 130 vrele(ip->i_devvp); 131 ip->i_devvp = 0; 132 } 133 #ifdef QUOTA 134 for (i = 0; i < MAXQUOTAS; i++) { 135 if (ip->i_dquot[i] != NODQUOT) { 136 dqrele(vp, ip->i_dquot[i]); 137 ip->i_dquot[i] = NODQUOT; 138 } 139 } 140 #endif 141 switch (vp->v_mount->mnt_stat.f_type) { 142 case MOUNT_UFS: 143 type = M_FFSNODE; 144 break; 145 case MOUNT_MFS: 146 type = M_MFSNODE; 147 break; 148 case MOUNT_LFS: 149 type = M_LFSNODE; 150 break; 151 default: 152 panic("ufs_reclaim: not ufs file"); 153 } 154 FREE(vp->v_data, type); 155 vp->v_data = NULL; 156 return (0); 157 } 158