]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ufs/ufs/ufs_inode.c
Bring the call to softdep_releasefile() within the region protected by
[FreeBSD/FreeBSD.git] / sys / ufs / 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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)ufs_inode.c 8.9 (Berkeley) 5/14/95
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include "opt_quota.h"
41 #include "opt_ufs.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/lock.h>
47 #include <sys/mount.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50
51 #include <ufs/ufs/extattr.h>
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/ufs_extern.h>
56 #ifdef UFS_DIRHASH
57 #include <ufs/ufs/dir.h>
58 #include <ufs/ufs/dirhash.h>
59 #endif
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 thread *a_td;
69         } */ *ap;
70 {
71         struct vnode *vp = ap->a_vp;
72         struct inode *ip = VTOI(vp);
73         struct thread *td = ap->a_td;
74         mode_t mode;
75         int error = 0;
76         struct mount *mp;
77
78         mp = NULL;
79         if (prtactive && vp->v_usecount != 0)
80                 vprint("ufs_inactive: pushing active", vp);
81         /*
82          * Ignore inodes related to stale file handles.
83          */
84         if (ip->i_mode == 0)
85                 goto out;
86         if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
87             (ip->i_nlink <= 0 &&
88              (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)) {
89         loop:
90                 if (vn_start_secondary_write(vp, &mp, V_NOWAIT) != 0) {
91                         /* Cannot delete file while file system is suspended */
92                         if ((vp->v_iflag & VI_DOOMED) != 0) {
93                                 /* Cannot return before file is deleted */
94                                 (void) vn_start_secondary_write(vp, &mp,
95                                                                 V_WAIT);
96                         } else {
97                                 MNT_ILOCK(mp);
98                                 if ((mp->mnt_kern_flag &
99                                      (MNTK_SUSPEND2 | MNTK_SUSPENDED)) == 0) {
100                                         MNT_IUNLOCK(mp);
101                                         goto loop;
102                                 }
103                                 /*
104                                  * Fail to inactivate vnode now and
105                                  * let ffs_snapshot() clean up after
106                                  * it has resumed the file system.
107                                  */
108                                 VI_LOCK(vp);
109                                 vp->v_iflag |= VI_OWEINACT;
110                                 VI_UNLOCK(vp);
111                                 MNT_IUNLOCK(mp);
112                                 return (0);
113                         }
114                 }
115         }
116         if (ip->i_effnlink == 0 && DOINGSOFTDEP(vp))
117                 softdep_releasefile(ip);
118         if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
119 #ifdef QUOTA
120                 if (!getinoquota(ip))
121                         (void)chkiq(ip, -1, NOCRED, FORCE);
122 #endif
123 #ifdef UFS_EXTATTR
124                 ufs_extattr_vnode_inactive(vp, td);
125 #endif
126                 error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL,
127                     NOCRED, td);
128                 /*
129                  * Setting the mode to zero needs to wait for the inode
130                  * to be written just as does a change to the link count.
131                  * So, rather than creating a new entry point to do the
132                  * same thing, we just use softdep_change_linkcnt().
133                  */
134                 DIP_SET(ip, i_rdev, 0);
135                 mode = ip->i_mode;
136                 ip->i_mode = 0;
137                 DIP_SET(ip, i_mode, 0);
138                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
139                 if (DOINGSOFTDEP(vp))
140                         softdep_change_linkcnt(ip);
141                 UFS_VFREE(vp, ip->i_number, mode);
142         }
143         if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
144                 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
145                     mp == NULL &&
146                     vn_start_secondary_write(vp, &mp, V_NOWAIT)) {
147                         mp = NULL;
148                         ip->i_flag &= ~IN_ACCESS;
149                 } else {
150                         if (mp == NULL)
151                                 (void) vn_start_secondary_write(vp, &mp,
152                                                                 V_WAIT);
153                         UFS_UPDATE(vp, 0);
154                 }
155         }
156 out:
157         /*
158          * If we are done with the inode, reclaim it
159          * so that it can be reused immediately.
160          */
161         if (ip->i_mode == 0)
162                 vrecycle(vp, td);
163         if (mp != NULL)
164                 vn_finished_secondary_write(mp);
165         return (error);
166 }
167
168 /*
169  * Reclaim an inode so that it can be used for other purposes.
170  */
171 int
172 ufs_reclaim(ap)
173         struct vop_reclaim_args /* {
174                 struct vnode *a_vp;
175                 struct thread *a_td;
176         } */ *ap;
177 {
178         struct vnode *vp = ap->a_vp;
179         struct inode *ip = VTOI(vp);
180         struct ufsmount *ump = ip->i_ump;
181 #ifdef QUOTA
182         int i;
183 #endif
184
185         if (prtactive && vp->v_usecount != 0)
186                 vprint("ufs_reclaim: pushing active", vp);
187         /*
188          * Destroy the vm object and flush associated pages.
189          */
190         vnode_destroy_vobject(vp);
191         if (ip->i_flag & IN_LAZYMOD) {
192                 ip->i_flag |= IN_MODIFIED;
193                 UFS_UPDATE(vp, 0);
194         }
195         /*
196          * Remove the inode from its hash chain.
197          */
198         vfs_hash_remove(vp);
199         /*
200          * Purge old data structures associated with the inode.
201          */
202 #ifdef QUOTA
203         for (i = 0; i < MAXQUOTAS; i++) {
204                 if (ip->i_dquot[i] != NODQUOT) {
205                         dqrele(vp, ip->i_dquot[i]);
206                         ip->i_dquot[i] = NODQUOT;
207                 }
208         }
209 #endif
210 #ifdef UFS_DIRHASH
211         if (ip->i_dirhash != NULL)
212                 ufsdirhash_free(ip);
213 #endif
214         /*
215          * Lock the clearing of v_data so ffs_lock() can inspect it
216          * prior to obtaining the lock.
217          */
218         VI_LOCK(vp);
219         vp->v_data = 0;
220         VI_UNLOCK(vp);
221         UFS_IFREE(ump, ip);
222         return (0);
223 }