1 /*	$OpenBSD: ufs_inode.c,v 1.29 2005/06/10 17:37:41 pedro Exp $	*/
2 /*	$NetBSD: ufs_inode.c,v 1.7 1996/05/11 18:27:52 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)ufs_inode.c	8.7 (Berkeley) 7/22/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/namei.h>
48 
49 #include <ufs/ufs/extattr.h>
50 #include <ufs/ufs/quota.h>
51 #include <ufs/ufs/inode.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ufs/ufs_extern.h>
54 #ifdef UFS_DIRHASH
55 #include <ufs/ufs/dir.h>
56 #include <ufs/ufs/dirhash.h>
57 #endif
58 
59 u_long	nextgennumber;		/* Next generation number to assign. */
60 
61 /*
62  * Last reference to an inode.  If necessary, write or delete it.
63  */
64 int
ufs_inactive(v)65 ufs_inactive(v)
66 	void *v;
67 {
68 	struct vop_inactive_args /* {
69 		struct vnode *a_vp;
70 		struct proc *a_p;
71 	} */ *ap = v;
72 	struct vnode *vp = ap->a_vp;
73 	struct inode *ip = VTOI(vp);
74 	struct proc *p = ap->a_p;
75 	mode_t mode;
76 	int error = 0;
77 	extern int prtactive;
78 
79 	if (prtactive && vp->v_usecount != 0)
80 		vprint("ffs_inactive: pushing active", vp);
81 
82 	/*
83 	 * Ignore inodes related to stale file handles.
84 	 */
85 	if (ip->i_ffs_mode == 0)
86 		goto out;
87 
88 	if (ip->i_ffs_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
89 		if (getinoquota(ip) == 0)
90 			(void)ufs_quota_free_inode(ip, NOCRED);
91 
92 		error = UFS_TRUNCATE(ip, (off_t)0, 0, NOCRED);
93 
94 		ip->i_ffs_rdev = 0;
95 		mode = ip->i_ffs_mode;
96 		ip->i_ffs_mode = 0;
97 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
98 
99 		/*
100 		 * Setting the mode to zero needs to wait for the inode to be
101 		 * written just as does a change to the link count. So, rather
102 		 * than creating a new entry point to do the same thing, we
103 		 * just use softdep_change_linkcnt(). Also, we can't let
104 		 * softdep co-opt us to help on its worklist, as we may end up
105 		 * trying to recycle vnodes and getting to this same point a
106 		 * couple of times, blowing the kernel stack. However, this
107 		 * could be optimized by checking if we are coming from
108 		 * vrele(), vput() or vclean() (by checking for VXLOCK) and
109 		 * just avoiding the co-opt to happen in the last case.
110 		 */
111 		if (DOINGSOFTDEP(vp))
112 			softdep_change_linkcnt(ip, 1);
113 
114 		UFS_INODE_FREE(ip, ip->i_number, mode);
115 	}
116 
117 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
118 		UFS_UPDATE(ip, 0);
119 	}
120 out:
121 	VOP_UNLOCK(vp, 0, p);
122 
123 	/*
124 	 * If we are done with the inode, reclaim it
125 	 * so that it can be reused immediately.
126 	 */
127 	if (ip->i_ffs_mode == 0)
128 		vrecycle(vp, NULL, p);
129 
130 	return (error);
131 }
132 
133 /*
134  * Reclaim an inode so that it can be used for other purposes.
135  */
136 int
ufs_reclaim(vp,p)137 ufs_reclaim(vp, p)
138 	register struct vnode *vp;
139 	struct proc *p;
140 {
141 	register struct inode *ip;
142 	extern int prtactive;
143 
144 	if (prtactive && vp->v_usecount != 0)
145 		vprint("ufs_reclaim: pushing active", vp);
146 	/*
147 	 * Remove the inode from its hash chain.
148 	 */
149 	ip = VTOI(vp);
150 	ufs_ihashrem(ip);
151 	/*
152 	 * Purge old data structures associated with the inode.
153 	 */
154 	cache_purge(vp);
155 
156 	if (ip->i_devvp) {
157 		vrele(ip->i_devvp);
158 	}
159 #ifdef UFS_DIRHASH
160 	if (ip->i_dirhash != NULL)
161 		ufsdirhash_free(ip);
162 #endif
163 	ufs_quota_delete(ip);
164 	return (0);
165 }
166