xref: /freebsd/sys/ufs/ufs/ufs_inode.c (revision c1d255d3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1991, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)ufs_inode.c	8.9 (Berkeley) 5/14/95
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_quota.h"
43 #include "opt_ufs.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/vnode.h>
48 #include <sys/lock.h>
49 #include <sys/mount.h>
50 #include <sys/malloc.h>
51 #include <sys/mutex.h>
52 
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/ufs_extern.h>
58 #ifdef UFS_DIRHASH
59 #include <ufs/ufs/dir.h>
60 #include <ufs/ufs/dirhash.h>
61 #endif
62 #ifdef UFS_GJOURNAL
63 #include <ufs/ufs/gjournal.h>
64 #endif
65 
66 int
67 ufs_need_inactive(ap)
68 	struct vop_need_inactive_args *ap;
69 {
70 	struct vnode *vp;
71 	struct inode *ip;
72 #ifdef QUOTA
73 	int i;
74 #endif
75 
76 	vp = ap->a_vp;
77 	ip = VTOI(vp);
78 	if (UFS_RDONLY(ip))
79 		return (0);
80 	if (vn_need_pageq_flush(vp))
81 		return (1);
82 	if (ip->i_mode == 0 ||  ip->i_nlink <= 0 ||
83 	    (ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
84 	    (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
85 	    IN_UPDATE)) != 0 ||
86 	    (ip->i_effnlink <= 0 && (ip->i_size != 0 || (I_IS_UFS2(ip) &&
87 	    ip->i_din2->di_extsize != 0))))
88 		return (1);
89 #ifdef QUOTA
90 	for (i = 0; i < MAXQUOTAS; i++) {
91 		if (ip->i_dquot[i] != NULL)
92 			return (1);
93 	}
94 #endif
95 	/*
96 	 * No need to check ufs_gjournal_close() condition since we
97 	 * return 1 if only i_nlink <= 0.
98 	 */
99 	return (0);
100 }
101 
102 /*
103  * Last reference to an inode.  If necessary, write or delete it.
104  */
105 int
106 ufs_inactive(ap)
107 	struct vop_inactive_args /* {
108 		struct vnode *a_vp;
109 	} */ *ap;
110 {
111 	struct vnode *vp = ap->a_vp;
112 	struct inode *ip = VTOI(vp);
113 	mode_t mode;
114 	int error = 0;
115 	off_t isize;
116 	struct mount *mp;
117 
118 	mp = NULL;
119 	/*
120 	 * Ignore inodes related to stale file handles.
121 	 */
122 	if (ip->i_mode == 0)
123 		goto out;
124 #ifdef UFS_GJOURNAL
125 	ufs_gjournal_close(vp);
126 #endif
127 #ifdef QUOTA
128 	/*
129 	 * Before moving off the active list, we must be sure that
130 	 * any modified quotas have been pushed since these will no
131 	 * longer be checked once the vnode is on the inactive list.
132 	 */
133 	qsyncvp(vp);
134 #endif
135 	if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
136 	    (ip->i_nlink <= 0 && !UFS_RDONLY(ip))) {
137 	loop:
138 		if (vn_start_secondary_write(vp, &mp, V_NOWAIT) != 0) {
139 			/* Cannot delete file while file system is suspended */
140 			if (VN_IS_DOOMED(vp)) {
141 				/* Cannot return before file is deleted */
142 				(void) vn_start_secondary_write(vp, &mp,
143 								V_WAIT);
144 			} else {
145 				MNT_ILOCK(mp);
146 				if ((mp->mnt_kern_flag &
147 				     (MNTK_SUSPEND2 | MNTK_SUSPENDED)) == 0) {
148 					MNT_IUNLOCK(mp);
149 					goto loop;
150 				}
151 				/*
152 				 * Fail to inactivate vnode now and
153 				 * let ffs_snapshot() clean up after
154 				 * it has resumed the file system.
155 				 */
156 				VI_LOCK(vp);
157 				vp->v_iflag |= VI_OWEINACT;
158 				VI_UNLOCK(vp);
159 				MNT_IUNLOCK(mp);
160 				return (0);
161 			}
162 		}
163 	}
164 	isize = ip->i_size;
165 	if (I_IS_UFS2(ip))
166 		isize += ip->i_din2->di_extsize;
167 	if (ip->i_effnlink <= 0 && isize && !UFS_RDONLY(ip))
168 		error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL, NOCRED);
169 	if (ip->i_nlink <= 0 && ip->i_mode != 0 && !UFS_RDONLY(ip) &&
170 	    (vp->v_iflag & VI_OWEINACT) == 0) {
171 #ifdef QUOTA
172 		if (!getinoquota(ip))
173 			(void)chkiq(ip, -1, NOCRED, FORCE);
174 #endif
175 #ifdef UFS_EXTATTR
176 		ufs_extattr_vnode_inactive(vp);
177 #endif
178 		/*
179 		 * Setting the mode to zero needs to wait for the inode
180 		 * to be written just as does a change to the link count.
181 		 * So, rather than creating a new entry point to do the
182 		 * same thing, we just use softdep_change_linkcnt().
183 		 */
184 		DIP_SET(ip, i_rdev, 0);
185 		mode = ip->i_mode;
186 		ip->i_mode = 0;
187 		DIP_SET(ip, i_mode, 0);
188 		UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
189 		if (DOINGSOFTDEP(vp))
190 			softdep_change_linkcnt(ip);
191 		UFS_VFREE(vp, ip->i_number, mode);
192 	}
193 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
194 		if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
195 		    mp == NULL &&
196 		    vn_start_secondary_write(vp, &mp, V_NOWAIT)) {
197 			mp = NULL;
198 			ip->i_flag &= ~IN_ACCESS;
199 		} else {
200 			if (mp == NULL)
201 				(void) vn_start_secondary_write(vp, &mp,
202 								V_WAIT);
203 			UFS_UPDATE(vp, 0);
204 		}
205 	}
206 out:
207 	/*
208 	 * If we are done with the inode, reclaim it
209 	 * so that it can be reused immediately.
210 	 */
211 	if (ip->i_mode == 0 && (vp->v_iflag & VI_OWEINACT) == 0)
212 		vrecycle(vp);
213 	if (mp != NULL)
214 		vn_finished_secondary_write(mp);
215 	return (error);
216 }
217 
218 /*
219  * Reclaim an inode so that it can be used for other purposes.
220  */
221 int
222 ufs_reclaim(ap)
223 	struct vop_reclaim_args /* {
224 		struct vnode *a_vp;
225 	} */ *ap;
226 {
227 	struct vnode *vp = ap->a_vp;
228 	struct inode *ip = VTOI(vp);
229 #ifdef QUOTA
230 	int i;
231 
232 	for (i = 0; i < MAXQUOTAS; i++) {
233 		if (ip->i_dquot[i] != NODQUOT) {
234 			dqrele(vp, ip->i_dquot[i]);
235 			ip->i_dquot[i] = NODQUOT;
236 		}
237 	}
238 #endif
239 #ifdef UFS_DIRHASH
240 	if (ip->i_dirhash != NULL)
241 		ufsdirhash_free(ip);
242 #endif
243 
244 	if (ip->i_flag & IN_LAZYMOD)
245 		UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
246 	UFS_UPDATE(vp, 0);
247 	/*
248 	 * Remove the inode from its hash chain.
249 	 */
250 	vfs_hash_remove(vp);
251 
252 	/*
253 	 * Lock the clearing of v_data so ffs_lock() can inspect it
254 	 * prior to obtaining the lock.
255 	 */
256 	VI_LOCK(vp);
257 	vp->v_data = 0;
258 	VI_UNLOCK(vp);
259 	UFS_IFREE(ITOUMP(ip), ip);
260 	return (0);
261 }
262