Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / vfs / ufs / ufs_inode.c
1 /*
2  * Copyright (c) 1991, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)ufs_inode.c 8.9 (Berkeley) 5/14/95
39  * $FreeBSD: src/sys/ufs/ufs/ufs_inode.c,v 1.25.2.3 2002/07/05 22:42:31 dillon Exp $
40  */
41
42 #include "opt_quota.h"
43 #include "opt_ufs.h"
44
45 #include <sys/param.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/malloc.h>
49
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
59 int     prtactive = 0;          /* 1 => print out reclaim of active vnodes */
60
61 /*
62  * Last reference to an inode.  If necessary, write or delete it.
63  */
64 int
65 ufs_inactive(ap)
66         struct vop_inactive_args /* {
67                 struct vnode *a_vp;
68                 struct proc *a_p;
69         } */ *ap;
70 {
71         struct vnode *vp = ap->a_vp;
72         struct inode *ip = VTOI(vp);
73         struct proc *p = ap->a_p;
74         int mode, error = 0;
75
76         if (prtactive && vp->v_usecount != 0)
77                 vprint("ufs_inactive: pushing active", vp);
78
79         /*
80          * Ignore inodes related to stale file handles.
81          */
82         if (ip->i_mode == 0)
83                 goto out;
84         if (ip->i_nlink <= 0) {
85 #ifdef QUOTA
86                 if (!getinoquota(ip))
87                         (void)chkiq(ip, -1, NOCRED, FORCE);
88 #endif
89                 error = UFS_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
90                 ip->i_rdev = 0;
91                 mode = ip->i_mode;
92                 ip->i_mode = 0;
93                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
94                 UFS_VFREE(vp, ip->i_number, mode);
95         }
96         if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))
97                 UFS_UPDATE(vp, 0);
98 out:
99         VOP_UNLOCK(vp, 0, p);
100         /*
101          * If we are done with the inode, reclaim it
102          * so that it can be reused immediately.
103          */
104         if (ip->i_mode == 0)
105                 vrecycle(vp, (struct simplelock *)0, p);
106         return (error);
107 }
108
109 /*
110  * Reclaim an inode so that it can be used for other purposes.
111  */
112 int
113 ufs_reclaim(ap)
114         struct vop_reclaim_args /* {
115                 struct vnode *a_vp;
116                 struct proc *a_p;
117         } */ *ap;
118 {
119         register struct inode *ip;
120         register struct vnode *vp = ap->a_vp;
121 #ifdef QUOTA
122         int i;
123 #endif
124
125         if (prtactive && vp->v_usecount != 0)
126                 vprint("ufs_reclaim: pushing active", vp);
127         ip = VTOI(vp);
128         if (ip->i_flag & IN_LAZYMOD) {
129                 ip->i_flag |= IN_MODIFIED;
130                 UFS_UPDATE(vp, 0);
131         }
132         /*
133          * Remove the inode from its hash chain.
134          */
135         ufs_ihashrem(ip);
136         /*
137          * Purge old data structures associated with the inode.
138          */
139         cache_purge(vp);
140         if (ip->i_devvp) {
141                 vrele(ip->i_devvp);
142                 ip->i_devvp = 0;
143         }
144 #ifdef QUOTA
145         for (i = 0; i < MAXQUOTAS; i++) {
146                 if (ip->i_dquot[i] != NODQUOT) {
147                         dqrele(vp, ip->i_dquot[i]);
148                         ip->i_dquot[i] = NODQUOT;
149                 }
150         }
151 #endif
152 #ifdef UFS_DIRHASH
153         if (ip->i_dirhash != NULL)
154                 ufsdirhash_free(ip);
155 #endif
156         FREE(vp->v_data, VFSTOUFS(vp->v_mount)->um_malloctype);
157         vp->v_data = 0;
158         return (0);
159 }