2 * SPDX-License-Identifier: BSD-3-Clause
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)ufs_inode.c 8.9 (Berkeley) 5/14/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_quota.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/malloc.h>
51 #include <sys/mutex.h>
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>
59 #include <ufs/ufs/dir.h>
60 #include <ufs/ufs/dirhash.h>
63 #include <ufs/ufs/gjournal.h>
67 * Last reference to an inode. If necessary, write or delete it.
71 struct vop_inactive_args /* {
76 struct vnode *vp = ap->a_vp;
77 struct inode *ip = VTOI(vp);
85 * Ignore inodes related to stale file handles.
90 ufs_gjournal_close(vp);
94 * Before moving off the active list, we must be sure that
95 * any modified quotas have been pushed since these will no
96 * longer be checked once the vnode is on the inactive list.
100 if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
101 (ip->i_nlink <= 0 && !UFS_RDONLY(ip))) {
103 if (vn_start_secondary_write(vp, &mp, V_NOWAIT) != 0) {
104 /* Cannot delete file while file system is suspended */
105 if ((vp->v_iflag & VI_DOOMED) != 0) {
106 /* Cannot return before file is deleted */
107 (void) vn_start_secondary_write(vp, &mp,
111 if ((mp->mnt_kern_flag &
112 (MNTK_SUSPEND2 | MNTK_SUSPENDED)) == 0) {
117 * Fail to inactivate vnode now and
118 * let ffs_snapshot() clean up after
119 * it has resumed the file system.
122 vp->v_iflag |= VI_OWEINACT;
131 isize += ip->i_din2->di_extsize;
132 if (ip->i_effnlink <= 0 && isize && !UFS_RDONLY(ip))
133 error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL, NOCRED);
134 if (ip->i_nlink <= 0 && ip->i_mode && !UFS_RDONLY(ip)) {
136 if (!getinoquota(ip))
137 (void)chkiq(ip, -1, NOCRED, FORCE);
140 ufs_extattr_vnode_inactive(vp, ap->a_td);
143 * Setting the mode to zero needs to wait for the inode
144 * to be written just as does a change to the link count.
145 * So, rather than creating a new entry point to do the
146 * same thing, we just use softdep_change_linkcnt().
148 DIP_SET(ip, i_rdev, 0);
151 DIP_SET(ip, i_mode, 0);
152 ip->i_flag |= IN_CHANGE | IN_UPDATE;
153 if (DOINGSOFTDEP(vp))
154 softdep_change_linkcnt(ip);
155 UFS_VFREE(vp, ip->i_number, mode);
157 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
158 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
160 vn_start_secondary_write(vp, &mp, V_NOWAIT)) {
162 ip->i_flag &= ~IN_ACCESS;
165 (void) vn_start_secondary_write(vp, &mp,
172 * If we are done with the inode, reclaim it
173 * so that it can be reused immediately.
178 vn_finished_secondary_write(mp);
183 ufs_prepare_reclaim(struct vnode *vp)
192 vnode_destroy_vobject(vp);
194 for (i = 0; i < MAXQUOTAS; i++) {
195 if (ip->i_dquot[i] != NODQUOT) {
196 dqrele(vp, ip->i_dquot[i]);
197 ip->i_dquot[i] = NODQUOT;
202 if (ip->i_dirhash != NULL)
208 * Reclaim an inode so that it can be used for other purposes.
212 struct vop_reclaim_args /* {
217 struct vnode *vp = ap->a_vp;
218 struct inode *ip = VTOI(vp);
220 ufs_prepare_reclaim(vp);
222 if (ip->i_flag & IN_LAZYMOD)
223 ip->i_flag |= IN_MODIFIED;
226 * Remove the inode from its hash chain.
231 * Lock the clearing of v_data so ffs_lock() can inspect it
232 * prior to obtaining the lock.
237 UFS_IFREE(ITOUMP(ip), ip);