xref: /freebsd/sys/ufs/ufs/ufs_inode.c (revision 60727d8b)
160727d8bSWarner Losh /*-
2996c772fSJohn Dyson  * Copyright (c) 1991, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34996c772fSJohn Dyson  *	@(#)ufs_inode.c	8.9 (Berkeley) 5/14/95
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37f4636c59SDavid E. O'Brien #include <sys/cdefs.h>
38f4636c59SDavid E. O'Brien __FBSDID("$FreeBSD$");
39f4636c59SDavid E. O'Brien 
4001733a9bSGarrett Wollman #include "opt_quota.h"
41516081f2SRobert Watson #include "opt_ufs.h"
4201733a9bSGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
445df14863SJeff Roberson #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/vnode.h>
467106ca0dSJohn Baldwin #include <sys/lock.h>
47df8bae1dSRodney W. Grimes #include <sys/mount.h>
482cfc47fbSPoul-Henning Kamp #include <sys/malloc.h>
491b367556SJason Evans #include <sys/mutex.h>
50df8bae1dSRodney W. Grimes 
51a64ed089SRobert Watson #include <ufs/ufs/extattr.h>
52df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h>
53df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h>
542cfc47fbSPoul-Henning Kamp #include <ufs/ufs/ufsmount.h>
55df8bae1dSRodney W. Grimes #include <ufs/ufs/ufs_extern.h>
569b5ad47fSIan Dowse #ifdef UFS_DIRHASH
579b5ad47fSIan Dowse #include <ufs/ufs/dir.h>
589b5ad47fSIan Dowse #include <ufs/ufs/dirhash.h>
599b5ad47fSIan Dowse #endif
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  * Last reference to an inode.  If necessary, write or delete it.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes int
65df8bae1dSRodney W. Grimes ufs_inactive(ap)
66df8bae1dSRodney W. Grimes 	struct vop_inactive_args /* {
67df8bae1dSRodney W. Grimes 		struct vnode *a_vp;
68b40ce416SJulian Elischer 		struct thread *a_td;
69df8bae1dSRodney W. Grimes 	} */ *ap;
70df8bae1dSRodney W. Grimes {
71996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
72996c772fSJohn Dyson 	struct inode *ip = VTOI(vp);
73b40ce416SJulian Elischer 	struct thread *td = ap->a_td;
741c85e6a3SKirk McKusick 	mode_t mode;
751c85e6a3SKirk McKusick 	int error = 0;
76df8bae1dSRodney W. Grimes 
77d4820f80SJeff Roberson 	VI_LOCK(vp);
78df8bae1dSRodney W. Grimes 	if (prtactive && vp->v_usecount != 0)
7961acca9fSJustin T. Gibbs 		vprint("ufs_inactive: pushing active", vp);
80d4820f80SJeff Roberson 	VI_UNLOCK(vp);
81df8bae1dSRodney W. Grimes 
82996c772fSJohn Dyson 	/*
83996c772fSJohn Dyson 	 * Ignore inodes related to stale file handles.
84996c772fSJohn Dyson 	 */
85996c772fSJohn Dyson 	if (ip->i_mode == 0)
86996c772fSJohn Dyson 		goto out;
879ccb939eSKirk McKusick 	if (ip->i_effnlink == 0 && DOINGSOFTDEP(vp))
889ccb939eSKirk McKusick 		softdep_releasefile(ip);
89cd600596SKirk McKusick 	if (ip->i_nlink <= 0) {
909b971133SKirk McKusick 		(void) vn_write_suspend_wait(vp, NULL, V_WAIT);
91df8bae1dSRodney W. Grimes #ifdef QUOTA
92df8bae1dSRodney W. Grimes 		if (!getinoquota(ip))
93a7d50c22SKirk McKusick 			(void)chkiq(ip, -1, NOCRED, FORCE);
94df8bae1dSRodney W. Grimes #endif
95516081f2SRobert Watson #ifdef UFS_EXTATTR
962fc6567eSRobert Watson 		ufs_extattr_vnode_inactive(vp, td);
97a64ed089SRobert Watson #endif
987aca6291SKirk McKusick 		error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL,
997aca6291SKirk McKusick 		    NOCRED, td);
100812b1d41SKirk McKusick 		/*
101812b1d41SKirk McKusick 		 * Setting the mode to zero needs to wait for the inode
102812b1d41SKirk McKusick 		 * to be written just as does a change to the link count.
103812b1d41SKirk McKusick 		 * So, rather than creating a new entry point to do the
104812b1d41SKirk McKusick 		 * same thing, we just use softdep_change_linkcnt().
105812b1d41SKirk McKusick 		 */
106b403319bSAlexander Kabaev 		DIP_SET(ip, i_rdev, 0);
107df8bae1dSRodney W. Grimes 		mode = ip->i_mode;
108df8bae1dSRodney W. Grimes 		ip->i_mode = 0;
109b403319bSAlexander Kabaev 		DIP_SET(ip, i_mode, 0);
110df8bae1dSRodney W. Grimes 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
111812b1d41SKirk McKusick 		if (DOINGSOFTDEP(vp))
112812b1d41SKirk McKusick 			softdep_change_linkcnt(ip);
113cec0f20cSPoul-Henning Kamp 		UFS_VFREE(vp, ip->i_number, mode);
114df8bae1dSRodney W. Grimes 	}
115f2a2857bSKirk McKusick 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
116f2a2857bSKirk McKusick 		if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
1179b971133SKirk McKusick 		    vn_write_suspend_wait(vp, NULL, V_NOWAIT)) {
118f2a2857bSKirk McKusick 			ip->i_flag &= ~IN_ACCESS;
119f2a2857bSKirk McKusick 		} else {
1209b971133SKirk McKusick 			(void) vn_write_suspend_wait(vp, NULL, V_WAIT);
121de5d1ba5SBruce Evans 			UFS_UPDATE(vp, 0);
122f2a2857bSKirk McKusick 		}
123f2a2857bSKirk McKusick 	}
124996c772fSJohn Dyson out:
125b40ce416SJulian Elischer 	VOP_UNLOCK(vp, 0, td);
126df8bae1dSRodney W. Grimes 	/*
127df8bae1dSRodney W. Grimes 	 * If we are done with the inode, reclaim it
128df8bae1dSRodney W. Grimes 	 * so that it can be reused immediately.
129df8bae1dSRodney W. Grimes 	 */
130996c772fSJohn Dyson 	if (ip->i_mode == 0)
131b40ce416SJulian Elischer 		vrecycle(vp, NULL, td);
132df8bae1dSRodney W. Grimes 	return (error);
133df8bae1dSRodney W. Grimes }
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes /*
136df8bae1dSRodney W. Grimes  * Reclaim an inode so that it can be used for other purposes.
137df8bae1dSRodney W. Grimes  */
138df8bae1dSRodney W. Grimes int
1392cfc47fbSPoul-Henning Kamp ufs_reclaim(ap)
1402cfc47fbSPoul-Henning Kamp 	struct vop_reclaim_args /* {
1412cfc47fbSPoul-Henning Kamp 		struct vnode *a_vp;
142b40ce416SJulian Elischer 		struct thread *a_td;
1432cfc47fbSPoul-Henning Kamp 	} */ *ap;
144df8bae1dSRodney W. Grimes {
14505f4ff5dSPoul-Henning Kamp 	struct vnode *vp = ap->a_vp;
1461c85e6a3SKirk McKusick 	struct inode *ip = VTOI(vp);
1471c85e6a3SKirk McKusick 	struct ufsmount *ump = ip->i_ump;
1482fe5b4cbSAndrey A. Chernov #ifdef QUOTA
1492fe5b4cbSAndrey A. Chernov 	int i;
1502fe5b4cbSAndrey A. Chernov #endif
151df8bae1dSRodney W. Grimes 
152d4820f80SJeff Roberson 	VI_LOCK(vp);
153df8bae1dSRodney W. Grimes 	if (prtactive && vp->v_usecount != 0)
154df8bae1dSRodney W. Grimes 		vprint("ufs_reclaim: pushing active", vp);
155d4820f80SJeff Roberson 	VI_UNLOCK(vp);
15630551872SBruce Evans 	if (ip->i_flag & IN_LAZYMOD) {
15730551872SBruce Evans 		ip->i_flag |= IN_MODIFIED;
158de5d1ba5SBruce Evans 		UFS_UPDATE(vp, 0);
15930551872SBruce Evans 	}
160df8bae1dSRodney W. Grimes 	/*
16140715905SPoul-Henning Kamp 	 * Remove the inode from its hash chain.
16240715905SPoul-Henning Kamp 	 */
16340715905SPoul-Henning Kamp 	ufs_ihashrem(ip);
16440715905SPoul-Henning Kamp 	/*
165df8bae1dSRodney W. Grimes 	 * Purge old data structures associated with the inode.
166df8bae1dSRodney W. Grimes 	 */
167df8bae1dSRodney W. Grimes 	vrele(ip->i_devvp);
168df8bae1dSRodney W. Grimes #ifdef QUOTA
169df8bae1dSRodney W. Grimes 	for (i = 0; i < MAXQUOTAS; i++) {
170df8bae1dSRodney W. Grimes 		if (ip->i_dquot[i] != NODQUOT) {
171df8bae1dSRodney W. Grimes 			dqrele(vp, ip->i_dquot[i]);
172df8bae1dSRodney W. Grimes 			ip->i_dquot[i] = NODQUOT;
173df8bae1dSRodney W. Grimes 		}
174df8bae1dSRodney W. Grimes 	}
175df8bae1dSRodney W. Grimes #endif
1769b5ad47fSIan Dowse #ifdef UFS_DIRHASH
1779b5ad47fSIan Dowse 	if (ip->i_dirhash != NULL)
1789b5ad47fSIan Dowse 		ufsdirhash_free(ip);
1799b5ad47fSIan Dowse #endif
180975512a9SPoul-Henning Kamp 	UFS_IFREE(ump, ip);
1812cfc47fbSPoul-Henning Kamp 	vp->v_data = 0;
182df8bae1dSRodney W. Grimes 	return (0);
183df8bae1dSRodney W. Grimes }
184