xref: /dragonfly/sys/vfs/ufs/ufs_inode.c (revision 38a690d7)
1 /*
2  * Copyright (c) 1991, 1993, 1995
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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)ufs_inode.c	8.9 (Berkeley) 5/14/95
39  * $FreeBSD: src/sys/ufs/ufs/ufs_inode.c,v 1.25.2.3 2002/07/05 22:42:31 dillon Exp $
40  * $DragonFly: src/sys/vfs/ufs/ufs_inode.c,v 1.6 2003/08/07 21:17:44 dillon Exp $
41  */
42 
43 #include "opt_quota.h"
44 #include "opt_ufs.h"
45 
46 #include <sys/param.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/malloc.h>
50 
51 #include "quota.h"
52 #include "inode.h"
53 #include "ufsmount.h"
54 #include "ufs_extern.h"
55 #ifdef UFS_DIRHASH
56 #include "dir.h"
57 #include "dirhash.h"
58 #endif
59 
60 int	prtactive = 0;		/* 1 => print out reclaim of active vnodes */
61 
62 /*
63  * Last reference to an inode.  If necessary, write or delete it.
64  */
65 int
66 ufs_inactive(ap)
67 	struct vop_inactive_args /* {
68 		struct vnode *a_vp;
69 		struct thread *a_td;
70 	} */ *ap;
71 {
72 	struct vnode *vp = ap->a_vp;
73 	struct inode *ip = VTOI(vp);
74 	struct thread *td = ap->a_td;
75 	int mode, error = 0;
76 
77 	if (prtactive && vp->v_usecount != 0)
78 		vprint("ufs_inactive: pushing active", vp);
79 
80 	/*
81 	 * Ignore inodes related to stale file handles.
82 	 */
83 	if (ip->i_mode == 0)
84 		goto out;
85 	if (ip->i_nlink <= 0) {
86 #ifdef QUOTA
87 		if (!getinoquota(ip))
88 			(void)chkiq(ip, -1, NOCRED, FORCE);
89 #endif
90 		error = UFS_TRUNCATE(vp, (off_t)0, 0, NOCRED, td);
91 		ip->i_rdev = 0;
92 		mode = ip->i_mode;
93 		ip->i_mode = 0;
94 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
95 		UFS_VFREE(vp, ip->i_number, mode);
96 	}
97 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))
98 		UFS_UPDATE(vp, 0);
99 out:
100 	VOP_UNLOCK(vp, 0, td);
101 	/*
102 	 * If we are done with the inode, reclaim it
103 	 * so that it can be reused immediately.
104 	 */
105 	if (ip->i_mode == 0)
106 		vrecycle(vp, NULL, td);
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 		struct thread *a_td;
118 	} */ *ap;
119 {
120 	struct inode *ip;
121 	struct vnode *vp = ap->a_vp;
122 #ifdef QUOTA
123 	int i;
124 #endif
125 
126 	if (prtactive && vp->v_usecount != 0)
127 		vprint("ufs_reclaim: pushing active", vp);
128 	ip = VTOI(vp);
129 	if (ip->i_flag & IN_LAZYMOD) {
130 		ip->i_flag |= IN_MODIFIED;
131 		UFS_UPDATE(vp, 0);
132 	}
133 	/*
134 	 * Remove the inode from its hash chain.
135 	 */
136 	ufs_ihashrem(ip);
137 	/*
138 	 * Purge old data structures associated with the inode.
139 	 */
140 	cache_purge(vp);
141 	if (ip->i_devvp) {
142 		vrele(ip->i_devvp);
143 		ip->i_devvp = 0;
144 	}
145 #ifdef QUOTA
146 	for (i = 0; i < MAXQUOTAS; i++) {
147 		if (ip->i_dquot[i] != NODQUOT) {
148 			dqrele(vp, ip->i_dquot[i]);
149 			ip->i_dquot[i] = NODQUOT;
150 		}
151 	}
152 #endif
153 #ifdef UFS_DIRHASH
154 	if (ip->i_dirhash != NULL)
155 		ufsdirhash_free(ip);
156 #endif
157 	FREE(vp->v_data, VFSTOUFS(vp->v_mount)->um_malloctype);
158 	vp->v_data = 0;
159 	return (0);
160 }
161