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