xref: /dragonfly/sys/vfs/ufs/ufs_inode.c (revision c3762235)
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)ufs_inode.c	8.9 (Berkeley) 5/14/95
35  * $FreeBSD: src/sys/ufs/ufs/ufs_inode.c,v 1.25.2.3 2002/07/05 22:42:31 dillon Exp $
36  * $DragonFly: src/sys/vfs/ufs/ufs_inode.c,v 1.24 2008/01/02 22:42:37 dillon Exp $
37  */
38 
39 #include "opt_quota.h"
40 #include "opt_ufs.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/malloc.h>
47 
48 #include "quota.h"
49 #include "inode.h"
50 #include "ufsmount.h"
51 #include "ufs_extern.h"
52 #include "ffs_extern.h"
53 #ifdef UFS_DIRHASH
54 #include "dir.h"
55 #include "dirhash.h"
56 #endif
57 
58 int	prtactive = 0;		/* 1 => print out reclaim of active vnodes */
59 
60 /*
61  * Last reference to an inode.  If necessary, write or delete it.
62  *
63  * ufs_inactive(struct vnode *a_vp)
64  */
65 int
66 ufs_inactive(struct vop_inactive_args *ap)
67 {
68 	struct vnode *vp = ap->a_vp;
69 	struct inode *ip = VTOI(vp);
70 	int mode, error = 0;
71 
72 	if (prtactive && VREFCNT(vp) > 1)
73 		vprint("ufs_inactive: pushing active", vp);
74 
75 	/*
76 	 * Ignore inodes related to stale file handles.
77 	 */
78 	if (ip == NULL || ip->i_mode == 0)
79 		goto out;
80 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
81 #ifdef QUOTA
82 		if (!ufs_getinoquota(ip))
83 			(void)ufs_chkiq(ip, -1, NOCRED, FORCE);
84 #endif
85 		/* Must have a VM object to truncate */
86 		error = ffs_truncate(vp, (off_t)0, 0, NOCRED);
87 		ip->i_rdev = 0;
88 		mode = ip->i_mode;
89 		ip->i_mode = 0;
90 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
91 		ffs_vfree(vp, ip->i_number, mode);
92 	}
93 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))
94 		ffs_update(vp, 0);
95 out:
96 	/*
97 	 * If we are done with the inode, reclaim it
98 	 * so that it can be reused immediately.
99 	 */
100 	if (ip == NULL || ip->i_mode == 0)
101 		vrecycle(vp);
102 	return (error);
103 }
104 
105 /*
106  * Reclaim an inode so that it can be used for other purposes.
107  *
108  * ufs_reclaim(struct vnode *a_vp)
109  */
110 int
111 ufs_reclaim(struct vop_reclaim_args *ap)
112 {
113 	struct inode *ip;
114 	struct vnode *vp = ap->a_vp;
115 	struct ufsmount *ump;
116 #ifdef QUOTA
117 	int i;
118 #endif
119 
120 	ump = VFSTOUFS(vp->v_mount);
121 
122 	if (prtactive && VREFCNT(vp) > 1)
123 		vprint("ufs_reclaim: pushing active", vp);
124 	ip = VTOI(vp);
125 
126 	/*
127 	 * Lazy updates.
128 	 */
129 	if (ip) {
130 		if (ip->i_flag & IN_LAZYMOD) {
131 			ip->i_flag |= IN_MODIFIED;
132 			ffs_update(vp, 0);
133 		}
134 	}
135 #ifdef INVARIANTS
136 	if (ip && (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))) {
137 		kprintf("WARNING: INODE %ld flags %08x: modified inode being released!\n", (long)ip->i_number, (int)ip->i_flag);
138 		ip->i_flag |= IN_MODIFIED;
139 		ffs_update(vp, 0);
140 	}
141 #endif
142 	/*
143 	 * Remove the inode from its hash chain and purge namecache
144 	 * data associated with the vnode.
145 	 */
146 	vp->v_data = NULL;
147 	if (ip) {
148 		ufs_ihashrem(ump, ip);
149 		if (ip->i_devvp) {
150 			vrele(ip->i_devvp);
151 			ip->i_devvp = 0;
152 		}
153 #ifdef QUOTA
154 		for (i = 0; i < MAXQUOTAS; i++) {
155 			if (ip->i_dquot[i] != NODQUOT) {
156 				ufs_dqrele(vp, ip->i_dquot[i]);
157 				ip->i_dquot[i] = NODQUOT;
158 			}
159 		}
160 #endif
161 #ifdef UFS_DIRHASH
162 		if (ip->i_dirhash != NULL)
163 			ufsdirhash_free(ip);
164 #endif
165 		kfree(ip, VFSTOUFS(vp->v_mount)->um_malloctype);
166 	}
167 	return (0);
168 }
169