]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/ufs/ufs/ufs_inode.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.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 #ifdef UFS_GJOURNAL
61 #include <ufs/ufs/gjournal.h>
62 #endif
63
64 /*
65  * Last reference to an inode.  If necessary, write or delete it.
66  */
67 int
68 ufs_inactive(ap)
69         struct vop_inactive_args /* {
70                 struct vnode *a_vp;
71                 struct thread *a_td;
72         } */ *ap;
73 {
74         struct vnode *vp = ap->a_vp;
75         struct inode *ip = VTOI(vp);
76         struct thread *td = ap->a_td;
77         mode_t mode;
78         int error = 0;
79         off_t isize;
80         struct mount *mp;
81
82         mp = NULL;
83         /*
84          * Ignore inodes related to stale file handles.
85          */
86         if (ip->i_mode == 0)
87                 goto out;
88 #ifdef UFS_GJOURNAL
89         ufs_gjournal_close(vp);
90 #endif
91         if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) ||
92             (ip->i_nlink <= 0 && !UFS_RDONLY(ip))) {
93         loop:
94                 if (vn_start_secondary_write(vp, &mp, V_NOWAIT) != 0) {
95                         /* Cannot delete file while file system is suspended */
96                         if ((vp->v_iflag & VI_DOOMED) != 0) {
97                                 /* Cannot return before file is deleted */
98                                 (void) vn_start_secondary_write(vp, &mp,
99                                                                 V_WAIT);
100                         } else {
101                                 MNT_ILOCK(mp);
102                                 if ((mp->mnt_kern_flag &
103                                      (MNTK_SUSPEND2 | MNTK_SUSPENDED)) == 0) {
104                                         MNT_IUNLOCK(mp);
105                                         goto loop;
106                                 }
107                                 /*
108                                  * Fail to inactivate vnode now and
109                                  * let ffs_snapshot() clean up after
110                                  * it has resumed the file system.
111                                  */
112                                 VI_LOCK(vp);
113                                 vp->v_iflag |= VI_OWEINACT;
114                                 VI_UNLOCK(vp);
115                                 MNT_IUNLOCK(mp);
116                                 return (0);
117                         }
118                 }
119         }
120         isize = ip->i_size;
121         if (ip->i_ump->um_fstype == UFS2)
122                 isize += ip->i_din2->di_extsize;
123         if (ip->i_effnlink <= 0 && isize && !UFS_RDONLY(ip))
124                 error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL,
125                     NOCRED, td);
126         if (ip->i_nlink <= 0 && ip->i_mode && !UFS_RDONLY(ip)) {
127 #ifdef QUOTA
128                 if (!getinoquota(ip))
129                         (void)chkiq(ip, -1, NOCRED, FORCE);
130 #endif
131 #ifdef UFS_EXTATTR
132                 ufs_extattr_vnode_inactive(vp, td);
133 #endif
134                 /*
135                  * Setting the mode to zero needs to wait for the inode
136                  * to be written just as does a change to the link count.
137                  * So, rather than creating a new entry point to do the
138                  * same thing, we just use softdep_change_linkcnt().
139                  */
140                 DIP_SET(ip, i_rdev, 0);
141                 mode = ip->i_mode;
142                 ip->i_mode = 0;
143                 DIP_SET(ip, i_mode, 0);
144                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
145                 if (DOINGSOFTDEP(vp))
146                         softdep_change_linkcnt(ip);
147                 UFS_VFREE(vp, ip->i_number, mode);
148         }
149         if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
150                 if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
151                     mp == NULL &&
152                     vn_start_secondary_write(vp, &mp, V_NOWAIT)) {
153                         mp = NULL;
154                         ip->i_flag &= ~IN_ACCESS;
155                 } else {
156                         if (mp == NULL)
157                                 (void) vn_start_secondary_write(vp, &mp,
158                                                                 V_WAIT);
159                         UFS_UPDATE(vp, 0);
160                 }
161         }
162 out:
163         /*
164          * If we are done with the inode, reclaim it
165          * so that it can be reused immediately.
166          */
167         if (ip->i_mode == 0)
168                 vrecycle(vp, td);
169         if (mp != NULL)
170                 vn_finished_secondary_write(mp);
171         return (error);
172 }
173
174 void
175 ufs_prepare_reclaim(struct vnode *vp)
176 {
177         struct inode *ip;
178 #ifdef QUOTA
179         int i;
180 #endif
181
182         ip = VTOI(vp);
183
184         vnode_destroy_vobject(vp);
185 #ifdef QUOTA
186         for (i = 0; i < MAXQUOTAS; i++) {
187                 if (ip->i_dquot[i] != NODQUOT) {
188                         dqrele(vp, ip->i_dquot[i]);
189                         ip->i_dquot[i] = NODQUOT;
190                 }
191         }
192 #endif
193 #ifdef UFS_DIRHASH
194         if (ip->i_dirhash != NULL)
195                 ufsdirhash_free(ip);
196 #endif
197 }
198
199 /*
200  * Reclaim an inode so that it can be used for other purposes.
201  */
202 int
203 ufs_reclaim(ap)
204         struct vop_reclaim_args /* {
205                 struct vnode *a_vp;
206                 struct thread *a_td;
207         } */ *ap;
208 {
209         struct vnode *vp = ap->a_vp;
210         struct inode *ip = VTOI(vp);
211         struct ufsmount *ump = ip->i_ump;
212
213         ufs_prepare_reclaim(vp);
214
215         if (ip->i_flag & IN_LAZYMOD)
216                 ip->i_flag |= IN_MODIFIED;
217         UFS_UPDATE(vp, 0);
218         /*
219          * Remove the inode from its hash chain.
220          */
221         vfs_hash_remove(vp);
222
223         /*
224          * Lock the clearing of v_data so ffs_lock() can inspect it
225          * prior to obtaining the lock.
226          */
227         VI_LOCK(vp);
228         vp->v_data = 0;
229         VI_UNLOCK(vp);
230         UFS_IFREE(ump, ip);
231         return (0);
232 }