160727d8bSWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4996c772fSJohn Dyson * Copyright (c) 1991, 1993, 1995
5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved.
6df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc.
7df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed
8df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph
9df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc.
11df8bae1dSRodney W. Grimes *
12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes * are met:
15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes * without specific prior written permission.
23df8bae1dSRodney W. Grimes *
24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes */
36df8bae1dSRodney W. Grimes
37f4636c59SDavid E. O'Brien #include <sys/cdefs.h>
3801733a9bSGarrett Wollman #include "opt_quota.h"
39516081f2SRobert Watson #include "opt_ufs.h"
4001733a9bSGarrett Wollman
41df8bae1dSRodney W. Grimes #include <sys/param.h>
425df14863SJeff Roberson #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/vnode.h>
447106ca0dSJohn Baldwin #include <sys/lock.h>
45df8bae1dSRodney W. Grimes #include <sys/mount.h>
462cfc47fbSPoul-Henning Kamp #include <sys/malloc.h>
471b367556SJason Evans #include <sys/mutex.h>
48df8bae1dSRodney W. Grimes
49a64ed089SRobert Watson #include <ufs/ufs/extattr.h>
50df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h>
51df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h>
522cfc47fbSPoul-Henning Kamp #include <ufs/ufs/ufsmount.h>
53df8bae1dSRodney W. Grimes #include <ufs/ufs/ufs_extern.h>
549b5ad47fSIan Dowse #ifdef UFS_DIRHASH
559b5ad47fSIan Dowse #include <ufs/ufs/dir.h>
569b5ad47fSIan Dowse #include <ufs/ufs/dirhash.h>
579b5ad47fSIan Dowse #endif
581a60c7fcSPawel Jakub Dawidek #ifdef UFS_GJOURNAL
591a60c7fcSPawel Jakub Dawidek #include <ufs/ufs/gjournal.h>
601a60c7fcSPawel Jakub Dawidek #endif
61df8bae1dSRodney W. Grimes
62332f313cSKonstantin Belousov int
ufs_need_inactive(struct vop_need_inactive_args * ap)63064e6b43SKirk McKusick ufs_need_inactive(struct vop_need_inactive_args *ap)
64332f313cSKonstantin Belousov {
65332f313cSKonstantin Belousov struct vnode *vp;
66332f313cSKonstantin Belousov struct inode *ip;
67332f313cSKonstantin Belousov #ifdef QUOTA
68332f313cSKonstantin Belousov int i;
69332f313cSKonstantin Belousov #endif
70332f313cSKonstantin Belousov
71332f313cSKonstantin Belousov vp = ap->a_vp;
72332f313cSKonstantin Belousov ip = VTOI(vp);
73332f313cSKonstantin Belousov if (UFS_RDONLY(ip))
74332f313cSKonstantin Belousov return (0);
75901b05fbSMateusz Guzik if (vn_need_pageq_flush(vp))
76901b05fbSMateusz Guzik return (1);
77332f313cSKonstantin Belousov if (ip->i_mode == 0 || ip->i_nlink <= 0 ||
78332f313cSKonstantin Belousov (ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
79332f313cSKonstantin Belousov (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED |
80332f313cSKonstantin Belousov IN_UPDATE)) != 0 ||
81332f313cSKonstantin Belousov (ip->i_effnlink <= 0 && (ip->i_size != 0 || (I_IS_UFS2(ip) &&
82332f313cSKonstantin Belousov ip->i_din2->di_extsize != 0))))
83332f313cSKonstantin Belousov return (1);
84332f313cSKonstantin Belousov #ifdef QUOTA
85332f313cSKonstantin Belousov for (i = 0; i < MAXQUOTAS; i++) {
86332f313cSKonstantin Belousov if (ip->i_dquot[i] != NULL)
87332f313cSKonstantin Belousov return (1);
88332f313cSKonstantin Belousov }
89332f313cSKonstantin Belousov #endif
90332f313cSKonstantin Belousov /*
91332f313cSKonstantin Belousov * No need to check ufs_gjournal_close() condition since we
92332f313cSKonstantin Belousov * return 1 if only i_nlink <= 0.
93332f313cSKonstantin Belousov */
94332f313cSKonstantin Belousov return (0);
95332f313cSKonstantin Belousov }
96332f313cSKonstantin Belousov
97df8bae1dSRodney W. Grimes /*
98df8bae1dSRodney W. Grimes * Last reference to an inode. If necessary, write or delete it.
99df8bae1dSRodney W. Grimes */
100df8bae1dSRodney W. Grimes int
ufs_inactive(struct vop_inactive_args * ap)101064e6b43SKirk McKusick ufs_inactive(
102df8bae1dSRodney W. Grimes struct vop_inactive_args /* {
103df8bae1dSRodney W. Grimes struct vnode *a_vp;
104064e6b43SKirk McKusick } */ *ap)
105df8bae1dSRodney W. Grimes {
106996c772fSJohn Dyson struct vnode *vp = ap->a_vp;
107996c772fSJohn Dyson struct inode *ip = VTOI(vp);
1081c85e6a3SKirk McKusick mode_t mode;
1091c85e6a3SKirk McKusick int error = 0;
110abf6c181SKonstantin Belousov off_t isize;
111791dd2faSTor Egge struct mount *mp;
112df8bae1dSRodney W. Grimes
113791dd2faSTor Egge mp = NULL;
114996c772fSJohn Dyson /*
115996c772fSJohn Dyson * Ignore inodes related to stale file handles.
116996c772fSJohn Dyson */
117996c772fSJohn Dyson if (ip->i_mode == 0)
118996c772fSJohn Dyson goto out;
1191a60c7fcSPawel Jakub Dawidek #ifdef UFS_GJOURNAL
1201a60c7fcSPawel Jakub Dawidek ufs_gjournal_close(vp);
1211a60c7fcSPawel Jakub Dawidek #endif
122dca5e0ecSKirk McKusick #ifdef QUOTA
123dca5e0ecSKirk McKusick /*
124dca5e0ecSKirk McKusick * Before moving off the active list, we must be sure that
125dca5e0ecSKirk McKusick * any modified quotas have been pushed since these will no
126dca5e0ecSKirk McKusick * longer be checked once the vnode is on the inactive list.
127dca5e0ecSKirk McKusick */
128dca5e0ecSKirk McKusick qsyncvp(vp);
129dca5e0ecSKirk McKusick #endif
1304613aa0eSTor Egge if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
13190446e36SKonstantin Belousov (ip->i_nlink <= 0 && !UFS_RDONLY(ip))) {
132ca2fa807STor Egge loop:
133ca2fa807STor Egge if (vn_start_secondary_write(vp, &mp, V_NOWAIT) != 0) {
134ca2fa807STor Egge /* Cannot delete file while file system is suspended */
135abd80ddbSMateusz Guzik if (VN_IS_DOOMED(vp)) {
136ca2fa807STor Egge /* Cannot return before file is deleted */
137ca2fa807STor Egge (void) vn_start_secondary_write(vp, &mp,
138ca2fa807STor Egge V_WAIT);
139ca2fa807STor Egge } else {
140ca2fa807STor Egge MNT_ILOCK(mp);
141ca2fa807STor Egge if ((mp->mnt_kern_flag &
142ca2fa807STor Egge (MNTK_SUSPEND2 | MNTK_SUSPENDED)) == 0) {
143ca2fa807STor Egge MNT_IUNLOCK(mp);
144ca2fa807STor Egge goto loop;
145ca2fa807STor Egge }
146ca2fa807STor Egge /*
147ca2fa807STor Egge * Fail to inactivate vnode now and
148ca2fa807STor Egge * let ffs_snapshot() clean up after
149ca2fa807STor Egge * it has resumed the file system.
150ca2fa807STor Egge */
151ca2fa807STor Egge VI_LOCK(vp);
152ca2fa807STor Egge vp->v_iflag |= VI_OWEINACT;
153ca2fa807STor Egge VI_UNLOCK(vp);
154ca2fa807STor Egge MNT_IUNLOCK(mp);
155ca2fa807STor Egge return (0);
156ca2fa807STor Egge }
157ca2fa807STor Egge }
1584613aa0eSTor Egge }
1599f9c8c59SJeff Roberson isize = ip->i_size;
160e1db6897SKonstantin Belousov if (I_IS_UFS2(ip))
1619f9c8c59SJeff Roberson isize += ip->i_din2->di_extsize;
162c0f4e7afSJeff Roberson if (ip->i_effnlink <= 0 && isize && !UFS_RDONLY(ip))
163c52fd858SEdward Tomasz Napierala error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL, NOCRED);
1648a1509e4SKonstantin Belousov if (ip->i_nlink <= 0 && ip->i_mode != 0 && !UFS_RDONLY(ip) &&
1658a1509e4SKonstantin Belousov (vp->v_iflag & VI_OWEINACT) == 0) {
166df8bae1dSRodney W. Grimes #ifdef QUOTA
167df8bae1dSRodney W. Grimes if (!getinoquota(ip))
168a7d50c22SKirk McKusick (void)chkiq(ip, -1, NOCRED, FORCE);
169df8bae1dSRodney W. Grimes #endif
170516081f2SRobert Watson #ifdef UFS_EXTATTR
171e9fb2bd9SMateusz Guzik ufs_extattr_vnode_inactive(vp);
172a64ed089SRobert Watson #endif
173812b1d41SKirk McKusick /*
174812b1d41SKirk McKusick * Setting the mode to zero needs to wait for the inode
175812b1d41SKirk McKusick * to be written just as does a change to the link count.
176812b1d41SKirk McKusick * So, rather than creating a new entry point to do the
177812b1d41SKirk McKusick * same thing, we just use softdep_change_linkcnt().
178812b1d41SKirk McKusick */
179b403319bSAlexander Kabaev DIP_SET(ip, i_rdev, 0);
180df8bae1dSRodney W. Grimes mode = ip->i_mode;
181df8bae1dSRodney W. Grimes ip->i_mode = 0;
182b403319bSAlexander Kabaev DIP_SET(ip, i_mode, 0);
183ac4ec141SMateusz Guzik UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
184812b1d41SKirk McKusick if (DOINGSOFTDEP(vp))
185812b1d41SKirk McKusick softdep_change_linkcnt(ip);
186cec0f20cSPoul-Henning Kamp UFS_VFREE(vp, ip->i_number, mode);
187df8bae1dSRodney W. Grimes }
188f2a2857bSKirk McKusick if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
189f2a2857bSKirk McKusick if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
190791dd2faSTor Egge mp == NULL &&
191791dd2faSTor Egge vn_start_secondary_write(vp, &mp, V_NOWAIT)) {
192791dd2faSTor Egge mp = NULL;
193f2a2857bSKirk McKusick ip->i_flag &= ~IN_ACCESS;
194f2a2857bSKirk McKusick } else {
195791dd2faSTor Egge if (mp == NULL)
196791dd2faSTor Egge (void) vn_start_secondary_write(vp, &mp,
197791dd2faSTor Egge V_WAIT);
198de5d1ba5SBruce Evans UFS_UPDATE(vp, 0);
199f2a2857bSKirk McKusick }
200f2a2857bSKirk McKusick }
201996c772fSJohn Dyson out:
202df8bae1dSRodney W. Grimes /*
203df8bae1dSRodney W. Grimes * If we are done with the inode, reclaim it
204df8bae1dSRodney W. Grimes * so that it can be reused immediately.
205df8bae1dSRodney W. Grimes */
2068a1509e4SKonstantin Belousov if (ip->i_mode == 0 && (vp->v_iflag & VI_OWEINACT) == 0)
207af6e6b87SEdward Tomasz Napierala vrecycle(vp);
208791dd2faSTor Egge if (mp != NULL)
209791dd2faSTor Egge vn_finished_secondary_write(mp);
210df8bae1dSRodney W. Grimes return (error);
211df8bae1dSRodney W. Grimes }
212df8bae1dSRodney W. Grimes
213df8bae1dSRodney W. Grimes /*
214df8bae1dSRodney W. Grimes * Reclaim an inode so that it can be used for other purposes.
215df8bae1dSRodney W. Grimes */
216df8bae1dSRodney W. Grimes int
ufs_reclaim(struct vop_reclaim_args * ap)217064e6b43SKirk McKusick ufs_reclaim(
2182cfc47fbSPoul-Henning Kamp struct vop_reclaim_args /* {
2192cfc47fbSPoul-Henning Kamp struct vnode *a_vp;
220064e6b43SKirk McKusick } */ *ap)
221df8bae1dSRodney W. Grimes {
22205f4ff5dSPoul-Henning Kamp struct vnode *vp = ap->a_vp;
2231c85e6a3SKirk McKusick struct inode *ip = VTOI(vp);
2246470c8d3SKonstantin Belousov #ifdef QUOTA
2256470c8d3SKonstantin Belousov int i;
226df8bae1dSRodney W. Grimes
2276470c8d3SKonstantin Belousov for (i = 0; i < MAXQUOTAS; i++) {
2286470c8d3SKonstantin Belousov if (ip->i_dquot[i] != NODQUOT) {
2296470c8d3SKonstantin Belousov dqrele(vp, ip->i_dquot[i]);
2306470c8d3SKonstantin Belousov ip->i_dquot[i] = NODQUOT;
2316470c8d3SKonstantin Belousov }
2326470c8d3SKonstantin Belousov }
2336470c8d3SKonstantin Belousov #endif
2346470c8d3SKonstantin Belousov #ifdef UFS_DIRHASH
2356470c8d3SKonstantin Belousov if (ip->i_dirhash != NULL)
2366470c8d3SKonstantin Belousov ufsdirhash_free(ip);
2376470c8d3SKonstantin Belousov #endif
238d9ca1af7SKonstantin Belousov
239d66ba370SKonstantin Belousov if (ip->i_flag & IN_LAZYMOD)
240ac4ec141SMateusz Guzik UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
241de5d1ba5SBruce Evans UFS_UPDATE(vp, 0);
242df8bae1dSRodney W. Grimes /*
24340715905SPoul-Henning Kamp * Remove the inode from its hash chain.
24440715905SPoul-Henning Kamp */
24514bc0685SPoul-Henning Kamp vfs_hash_remove(vp);
246d9ca1af7SKonstantin Belousov
24723d15e85SJeff Roberson /*
24823d15e85SJeff Roberson * Lock the clearing of v_data so ffs_lock() can inspect it
24923d15e85SJeff Roberson * prior to obtaining the lock.
25023d15e85SJeff Roberson */
25123d15e85SJeff Roberson VI_LOCK(vp);
2522cfc47fbSPoul-Henning Kamp vp->v_data = 0;
25323d15e85SJeff Roberson VI_UNLOCK(vp);
254e1db6897SKonstantin Belousov UFS_IFREE(ITOUMP(ip), ip);
255df8bae1dSRodney W. Grimes return (0);
256df8bae1dSRodney W. Grimes }
257