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.7 (Berkeley) 07/22/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 done; 35 36 if (done) 37 return (0); 38 done = 1; 39 ufs_ihashinit(); 40 #ifdef QUOTA 41 dqinit(); 42 #endif 43 return (0); 44 } 45 46 /* 47 * Last reference to an inode. If necessary, write or delete it. 48 */ 49 int 50 ufs_inactive(ap) 51 struct vop_inactive_args /* { 52 struct vnode *a_vp; 53 } */ *ap; 54 { 55 register struct vnode *vp = ap->a_vp; 56 register struct inode *ip = VTOI(vp); 57 struct timeval tv; 58 int mode, error; 59 extern int prtactive; 60 61 if (prtactive && vp->v_usecount != 0) 62 vprint("ffs_inactive: pushing active", vp); 63 64 /* Get rid of inodes related to stale file handles. */ 65 if (ip->i_mode == 0) { 66 if ((vp->v_flag & VXLOCK) == 0) 67 vgone(vp); 68 return (0); 69 } 70 71 error = 0; 72 #ifdef DIAGNOSTIC 73 if (VOP_ISLOCKED(vp)) 74 panic("ffs_inactive: locked inode"); 75 if (curproc) 76 ip->i_lockholder = curproc->p_pid; 77 else 78 ip->i_lockholder = -1; 79 #endif 80 ip->i_flag |= IN_LOCKED; 81 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 82 #ifdef QUOTA 83 if (!getinoquota(ip)) 84 (void)chkiq(ip, -1, NOCRED, 0); 85 #endif 86 error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL); 87 ip->i_rdev = 0; 88 mode = ip->i_mode; 89 ip->i_mode = 0; 90 ip->i_flag |= IN_CHANGE | IN_UPDATE; 91 VOP_VFREE(vp, ip->i_number, mode); 92 } 93 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 94 tv = time; 95 VOP_UPDATE(vp, &tv, &tv, 0); 96 } 97 VOP_UNLOCK(vp); 98 /* 99 * If we are done with the inode, reclaim it 100 * so that it can be reused immediately. 101 */ 102 if (vp->v_usecount == 0 && ip->i_mode == 0) 103 vgone(vp); 104 return (error); 105 } 106 107 /* 108 * Reclaim an inode so that it can be used for other purposes. 109 */ 110 int 111 ufs_reclaim(vp) 112 register struct vnode *vp; 113 { 114 register struct inode *ip; 115 int i; 116 extern int prtactive; 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 return (0); 142 } 143