xref: /freebsd/sys/ufs/ufs/ufs_inode.c (revision 29363fb4)
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 
37 #include <sys/cdefs.h>
38 #include "opt_quota.h"
39 #include "opt_ufs.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/vnode.h>
44 #include <sys/lock.h>
45 #include <sys/mount.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.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 #ifdef UFS_GJOURNAL
59 #include <ufs/ufs/gjournal.h>
60 #endif
61 
62 int
ufs_need_inactive(struct vop_need_inactive_args * ap)63 ufs_need_inactive(struct vop_need_inactive_args *ap)
64 {
65 	struct vnode *vp;
66 	struct inode *ip;
67 #ifdef QUOTA
68 	int i;
69 #endif
70 
71 	vp = ap->a_vp;
72 	ip = VTOI(vp);
73 	if (UFS_RDONLY(ip))
74 		return (0);
75 	if (vn_need_pageq_flush(vp))
76 		return (1);
77 	if (ip->i_mode == 0 ||  ip->i_nlink <= 0 ||
78 	    (ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
79 	    (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
80 	    IN_UPDATE)) != 0 ||
81 	    (ip->i_effnlink <= 0 && (ip->i_size != 0 || (I_IS_UFS2(ip) &&
82 	    ip->i_din2->di_extsize != 0))))
83 		return (1);
84 #ifdef QUOTA
85 	for (i = 0; i < MAXQUOTAS; i++) {
86 		if (ip->i_dquot[i] != NULL)
87 			return (1);
88 	}
89 #endif
90 	/*
91 	 * No need to check ufs_gjournal_close() condition since we
92 	 * return 1 if only i_nlink <= 0.
93 	 */
94 	return (0);
95 }
96 
97 /*
98  * Last reference to an inode.  If necessary, write or delete it.
99  */
100 int
ufs_inactive(struct vop_inactive_args * ap)101 ufs_inactive(
102 	struct vop_inactive_args /* {
103 		struct vnode *a_vp;
104 	} */ *ap)
105 {
106 	struct vnode *vp = ap->a_vp;
107 	struct inode *ip = VTOI(vp);
108 	mode_t mode;
109 	int error = 0;
110 	off_t isize;
111 	struct mount *mp;
112 
113 	mp = NULL;
114 	/*
115 	 * Ignore inodes related to stale file handles.
116 	 */
117 	if (ip->i_mode == 0)
118 		goto out;
119 #ifdef UFS_GJOURNAL
120 	ufs_gjournal_close(vp);
121 #endif
122 #ifdef QUOTA
123 	/*
124 	 * Before moving off the active list, we must be sure that
125 	 * any modified quotas have been pushed since these will no
126 	 * longer be checked once the vnode is on the inactive list.
127 	 */
128 	qsyncvp(vp);
129 #endif
130 	if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
131 	    (ip->i_nlink <= 0 && !UFS_RDONLY(ip))) {
132 	loop:
133 		if (vn_start_secondary_write(vp, &mp, V_NOWAIT) != 0) {
134 			/* Cannot delete file while file system is suspended */
135 			if (VN_IS_DOOMED(vp)) {
136 				/* Cannot return before file is deleted */
137 				(void) vn_start_secondary_write(vp, &mp,
138 								V_WAIT);
139 			} else {
140 				MNT_ILOCK(mp);
141 				if ((mp->mnt_kern_flag &
142 				     (MNTK_SUSPEND2 | MNTK_SUSPENDED)) == 0) {
143 					MNT_IUNLOCK(mp);
144 					goto loop;
145 				}
146 				/*
147 				 * Fail to inactivate vnode now and
148 				 * let ffs_snapshot() clean up after
149 				 * it has resumed the file system.
150 				 */
151 				VI_LOCK(vp);
152 				vp->v_iflag |= VI_OWEINACT;
153 				VI_UNLOCK(vp);
154 				MNT_IUNLOCK(mp);
155 				return (0);
156 			}
157 		}
158 	}
159 	isize = ip->i_size;
160 	if (I_IS_UFS2(ip))
161 		isize += ip->i_din2->di_extsize;
162 	if (ip->i_effnlink <= 0 && isize && !UFS_RDONLY(ip))
163 		error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL, NOCRED);
164 	if (ip->i_nlink <= 0 && ip->i_mode != 0 && !UFS_RDONLY(ip) &&
165 	    (vp->v_iflag & VI_OWEINACT) == 0) {
166 #ifdef QUOTA
167 		if (!getinoquota(ip))
168 			(void)chkiq(ip, -1, NOCRED, FORCE);
169 #endif
170 #ifdef UFS_EXTATTR
171 		ufs_extattr_vnode_inactive(vp);
172 #endif
173 		/*
174 		 * Setting the mode to zero needs to wait for the inode
175 		 * to be written just as does a change to the link count.
176 		 * So, rather than creating a new entry point to do the
177 		 * same thing, we just use softdep_change_linkcnt().
178 		 */
179 		DIP_SET(ip, i_rdev, 0);
180 		mode = ip->i_mode;
181 		ip->i_mode = 0;
182 		DIP_SET(ip, i_mode, 0);
183 		UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
184 		if (DOINGSOFTDEP(vp))
185 			softdep_change_linkcnt(ip);
186 		UFS_VFREE(vp, ip->i_number, mode);
187 	}
188 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
189 		if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
190 		    mp == NULL &&
191 		    vn_start_secondary_write(vp, &mp, V_NOWAIT)) {
192 			mp = NULL;
193 			ip->i_flag &= ~IN_ACCESS;
194 		} else {
195 			if (mp == NULL)
196 				(void) vn_start_secondary_write(vp, &mp,
197 								V_WAIT);
198 			UFS_UPDATE(vp, 0);
199 		}
200 	}
201 out:
202 	/*
203 	 * If we are done with the inode, reclaim it
204 	 * so that it can be reused immediately.
205 	 */
206 	if (ip->i_mode == 0 && (vp->v_iflag & VI_OWEINACT) == 0)
207 		vrecycle(vp);
208 	if (mp != NULL)
209 		vn_finished_secondary_write(mp);
210 	return (error);
211 }
212 
213 /*
214  * Reclaim an inode so that it can be used for other purposes.
215  */
216 int
ufs_reclaim(struct vop_reclaim_args * ap)217 ufs_reclaim(
218 	struct vop_reclaim_args /* {
219 		struct vnode *a_vp;
220 	} */ *ap)
221 {
222 	struct vnode *vp = ap->a_vp;
223 	struct inode *ip = VTOI(vp);
224 #ifdef QUOTA
225 	int i;
226 
227 	for (i = 0; i < MAXQUOTAS; i++) {
228 		if (ip->i_dquot[i] != NODQUOT) {
229 			dqrele(vp, ip->i_dquot[i]);
230 			ip->i_dquot[i] = NODQUOT;
231 		}
232 	}
233 #endif
234 #ifdef UFS_DIRHASH
235 	if (ip->i_dirhash != NULL)
236 		ufsdirhash_free(ip);
237 #endif
238 
239 	if (ip->i_flag & IN_LAZYMOD)
240 		UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
241 	UFS_UPDATE(vp, 0);
242 	/*
243 	 * Remove the inode from its hash chain.
244 	 */
245 	vfs_hash_remove(vp);
246 
247 	/*
248 	 * Lock the clearing of v_data so ffs_lock() can inspect it
249 	 * prior to obtaining the lock.
250 	 */
251 	VI_LOCK(vp);
252 	vp->v_data = 0;
253 	VI_UNLOCK(vp);
254 	UFS_IFREE(ITOUMP(ip), ip);
255 	return (0);
256 }
257