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