ulfs_inode.c revision 1.1 1 1.1 dholland /* $NetBSD: ulfs_inode.c,v 1.1 2013/06/06 00:40:55 dholland Exp $ */
2 1.1 dholland /* from NetBSD: ufs_inode.c,v 1.89 2013/01/22 09:39:18 dholland Exp */
3 1.1 dholland
4 1.1 dholland /*
5 1.1 dholland * Copyright (c) 1991, 1993
6 1.1 dholland * The Regents of the University of California. All rights reserved.
7 1.1 dholland * (c) UNIX System Laboratories, Inc.
8 1.1 dholland * All or some portions of this file are derived from material licensed
9 1.1 dholland * to the University of California by American Telephone and Telegraph
10 1.1 dholland * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 1.1 dholland * the permission of UNIX System Laboratories, Inc.
12 1.1 dholland *
13 1.1 dholland * Redistribution and use in source and binary forms, with or without
14 1.1 dholland * modification, are permitted provided that the following conditions
15 1.1 dholland * are met:
16 1.1 dholland * 1. Redistributions of source code must retain the above copyright
17 1.1 dholland * notice, this list of conditions and the following disclaimer.
18 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 dholland * notice, this list of conditions and the following disclaimer in the
20 1.1 dholland * documentation and/or other materials provided with the distribution.
21 1.1 dholland * 3. Neither the name of the University nor the names of its contributors
22 1.1 dholland * may be used to endorse or promote products derived from this software
23 1.1 dholland * without specific prior written permission.
24 1.1 dholland *
25 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 dholland * SUCH DAMAGE.
36 1.1 dholland *
37 1.1 dholland * @(#)ufs_inode.c 8.9 (Berkeley) 5/14/95
38 1.1 dholland */
39 1.1 dholland
40 1.1 dholland #include <sys/cdefs.h>
41 1.1 dholland __KERNEL_RCSID(0, "$NetBSD: ulfs_inode.c,v 1.1 2013/06/06 00:40:55 dholland Exp $");
42 1.1 dholland
43 1.1 dholland #if defined(_KERNEL_OPT)
44 1.1 dholland #include "opt_ffs.h"
45 1.1 dholland #include "opt_quota.h"
46 1.1 dholland #include "opt_wapbl.h"
47 1.1 dholland #endif
48 1.1 dholland
49 1.1 dholland #include <sys/param.h>
50 1.1 dholland #include <sys/systm.h>
51 1.1 dholland #include <sys/proc.h>
52 1.1 dholland #include <sys/vnode.h>
53 1.1 dholland #include <sys/mount.h>
54 1.1 dholland #include <sys/kernel.h>
55 1.1 dholland #include <sys/namei.h>
56 1.1 dholland #include <sys/kauth.h>
57 1.1 dholland #include <sys/wapbl.h>
58 1.1 dholland #include <sys/fstrans.h>
59 1.1 dholland #include <sys/kmem.h>
60 1.1 dholland
61 1.1 dholland #include <ufs/ufs/inode.h>
62 1.1 dholland #include <ufs/ufs/ufsmount.h>
63 1.1 dholland #include <ufs/ufs/ufs_extern.h>
64 1.1 dholland #include <ufs/ufs/ufs_wapbl.h>
65 1.1 dholland #ifdef UFS_DIRHASH
66 1.1 dholland #include <ufs/ufs/dirhash.h>
67 1.1 dholland #endif
68 1.1 dholland #ifdef UFS_EXTATTR
69 1.1 dholland #include <ufs/ufs/extattr.h>
70 1.1 dholland #endif
71 1.1 dholland
72 1.1 dholland #include <uvm/uvm.h>
73 1.1 dholland
74 1.1 dholland extern int prtactive;
75 1.1 dholland
76 1.1 dholland /*
77 1.1 dholland * Last reference to an inode. If necessary, write or delete it.
78 1.1 dholland */
79 1.1 dholland int
80 1.1 dholland ufs_inactive(void *v)
81 1.1 dholland {
82 1.1 dholland struct vop_inactive_args /* {
83 1.1 dholland struct vnode *a_vp;
84 1.1 dholland struct bool *a_recycle;
85 1.1 dholland } */ *ap = v;
86 1.1 dholland struct vnode *vp = ap->a_vp;
87 1.1 dholland struct inode *ip = VTOI(vp);
88 1.1 dholland struct mount *transmp;
89 1.1 dholland mode_t mode;
90 1.1 dholland int error = 0;
91 1.1 dholland int logged = 0;
92 1.1 dholland
93 1.1 dholland UFS_WAPBL_JUNLOCK_ASSERT(vp->v_mount);
94 1.1 dholland
95 1.1 dholland transmp = vp->v_mount;
96 1.1 dholland fstrans_start(transmp, FSTRANS_LAZY);
97 1.1 dholland /*
98 1.1 dholland * Ignore inodes related to stale file handles.
99 1.1 dholland */
100 1.1 dholland if (ip->i_mode == 0)
101 1.1 dholland goto out;
102 1.1 dholland if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
103 1.1 dholland #ifdef UFS_EXTATTR
104 1.1 dholland ufs_extattr_vnode_inactive(vp, curlwp);
105 1.1 dholland #endif
106 1.1 dholland error = UFS_WAPBL_BEGIN(vp->v_mount);
107 1.1 dholland if (error)
108 1.1 dholland goto out;
109 1.1 dholland logged = 1;
110 1.1 dholland if (ip->i_size != 0) {
111 1.1 dholland /*
112 1.1 dholland * When journaling, only truncate one indirect block
113 1.1 dholland * at a time
114 1.1 dholland */
115 1.1 dholland if (vp->v_mount->mnt_wapbl) {
116 1.1 dholland uint64_t incr = MNINDIR(ip->i_ump) <<
117 1.1 dholland vp->v_mount->mnt_fs_bshift; /* Power of 2 */
118 1.1 dholland uint64_t base = UFS_NDADDR <<
119 1.1 dholland vp->v_mount->mnt_fs_bshift;
120 1.1 dholland while (!error && ip->i_size > base + incr) {
121 1.1 dholland /*
122 1.1 dholland * round down to next full indirect
123 1.1 dholland * block boundary.
124 1.1 dholland */
125 1.1 dholland uint64_t nsize = base +
126 1.1 dholland ((ip->i_size - base - 1) &
127 1.1 dholland ~(incr - 1));
128 1.1 dholland error = UFS_TRUNCATE(vp, nsize, 0,
129 1.1 dholland NOCRED);
130 1.1 dholland if (error)
131 1.1 dholland break;
132 1.1 dholland UFS_WAPBL_END(vp->v_mount);
133 1.1 dholland error = UFS_WAPBL_BEGIN(vp->v_mount);
134 1.1 dholland if (error)
135 1.1 dholland goto out;
136 1.1 dholland }
137 1.1 dholland }
138 1.1 dholland if (!error)
139 1.1 dholland error = UFS_TRUNCATE(vp, (off_t)0, 0, NOCRED);
140 1.1 dholland }
141 1.1 dholland #if defined(QUOTA) || defined(QUOTA2)
142 1.1 dholland (void)chkiq(ip, -1, NOCRED, 0);
143 1.1 dholland #endif
144 1.1 dholland DIP_ASSIGN(ip, rdev, 0);
145 1.1 dholland mode = ip->i_mode;
146 1.1 dholland ip->i_mode = 0;
147 1.1 dholland ip->i_omode = mode;
148 1.1 dholland DIP_ASSIGN(ip, mode, 0);
149 1.1 dholland ip->i_flag |= IN_CHANGE | IN_UPDATE;
150 1.1 dholland /*
151 1.1 dholland * Defer final inode free and update to ufs_reclaim().
152 1.1 dholland */
153 1.1 dholland }
154 1.1 dholland
155 1.1 dholland if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
156 1.1 dholland if (!logged++) {
157 1.1 dholland int err;
158 1.1 dholland err = UFS_WAPBL_BEGIN(vp->v_mount);
159 1.1 dholland if (err)
160 1.1 dholland goto out;
161 1.1 dholland }
162 1.1 dholland UFS_UPDATE(vp, NULL, NULL, 0);
163 1.1 dholland }
164 1.1 dholland if (logged)
165 1.1 dholland UFS_WAPBL_END(vp->v_mount);
166 1.1 dholland out:
167 1.1 dholland /*
168 1.1 dholland * If we are done with the inode, reclaim it
169 1.1 dholland * so that it can be reused immediately.
170 1.1 dholland */
171 1.1 dholland *ap->a_recycle = (ip->i_mode == 0);
172 1.1 dholland VOP_UNLOCK(vp);
173 1.1 dholland fstrans_done(transmp);
174 1.1 dholland return (error);
175 1.1 dholland }
176 1.1 dholland
177 1.1 dholland /*
178 1.1 dholland * Reclaim an inode so that it can be used for other purposes.
179 1.1 dholland */
180 1.1 dholland int
181 1.1 dholland ufs_reclaim(struct vnode *vp)
182 1.1 dholland {
183 1.1 dholland struct inode *ip = VTOI(vp);
184 1.1 dholland
185 1.1 dholland if (prtactive && vp->v_usecount > 1)
186 1.1 dholland vprint("ufs_reclaim: pushing active", vp);
187 1.1 dholland
188 1.1 dholland if (!UFS_WAPBL_BEGIN(vp->v_mount)) {
189 1.1 dholland UFS_UPDATE(vp, NULL, NULL, UPDATE_CLOSE);
190 1.1 dholland UFS_WAPBL_END(vp->v_mount);
191 1.1 dholland }
192 1.1 dholland UFS_UPDATE(vp, NULL, NULL, UPDATE_CLOSE);
193 1.1 dholland
194 1.1 dholland /*
195 1.1 dholland * Remove the inode from its hash chain.
196 1.1 dholland */
197 1.1 dholland ufs_ihashrem(ip);
198 1.1 dholland
199 1.1 dholland if (ip->i_devvp) {
200 1.1 dholland vrele(ip->i_devvp);
201 1.1 dholland ip->i_devvp = 0;
202 1.1 dholland }
203 1.1 dholland #if defined(QUOTA) || defined(QUOTA2)
204 1.1 dholland ufsquota_free(ip);
205 1.1 dholland #endif
206 1.1 dholland #ifdef UFS_DIRHASH
207 1.1 dholland if (ip->i_dirhash != NULL)
208 1.1 dholland ufsdirhash_free(ip);
209 1.1 dholland #endif
210 1.1 dholland return (0);
211 1.1 dholland }
212 1.1 dholland
213 1.1 dholland /*
214 1.1 dholland * allocate a range of blocks in a file.
215 1.1 dholland * after this function returns, any page entirely contained within the range
216 1.1 dholland * will map to invalid data and thus must be overwritten before it is made
217 1.1 dholland * accessible to others.
218 1.1 dholland */
219 1.1 dholland
220 1.1 dholland int
221 1.1 dholland ufs_balloc_range(struct vnode *vp, off_t off, off_t len, kauth_cred_t cred,
222 1.1 dholland int flags)
223 1.1 dholland {
224 1.1 dholland off_t neweof; /* file size after the operation */
225 1.1 dholland off_t neweob; /* offset next to the last block after the operation */
226 1.1 dholland off_t pagestart; /* starting offset of range covered by pgs */
227 1.1 dholland off_t eob; /* offset next to allocated blocks */
228 1.1 dholland struct uvm_object *uobj;
229 1.1 dholland int i, delta, error, npages;
230 1.1 dholland int bshift = vp->v_mount->mnt_fs_bshift;
231 1.1 dholland int bsize = 1 << bshift;
232 1.1 dholland int ppb = MAX(bsize >> PAGE_SHIFT, 1);
233 1.1 dholland struct vm_page **pgs;
234 1.1 dholland size_t pgssize;
235 1.1 dholland UVMHIST_FUNC("ufs_balloc_range"); UVMHIST_CALLED(ubchist);
236 1.1 dholland UVMHIST_LOG(ubchist, "vp %p off 0x%x len 0x%x u_size 0x%x",
237 1.1 dholland vp, off, len, vp->v_size);
238 1.1 dholland
239 1.1 dholland neweof = MAX(vp->v_size, off + len);
240 1.1 dholland GOP_SIZE(vp, neweof, &neweob, 0);
241 1.1 dholland
242 1.1 dholland error = 0;
243 1.1 dholland uobj = &vp->v_uobj;
244 1.1 dholland
245 1.1 dholland /*
246 1.1 dholland * read or create pages covering the range of the allocation and
247 1.1 dholland * keep them locked until the new block is allocated, so there
248 1.1 dholland * will be no window where the old contents of the new block are
249 1.1 dholland * visible to racing threads.
250 1.1 dholland */
251 1.1 dholland
252 1.1 dholland pagestart = trunc_page(off) & ~(bsize - 1);
253 1.1 dholland npages = MIN(ppb, (round_page(neweob) - pagestart) >> PAGE_SHIFT);
254 1.1 dholland pgssize = npages * sizeof(struct vm_page *);
255 1.1 dholland pgs = kmem_zalloc(pgssize, KM_SLEEP);
256 1.1 dholland
257 1.1 dholland /*
258 1.1 dholland * adjust off to be block-aligned.
259 1.1 dholland */
260 1.1 dholland
261 1.1 dholland delta = off & (bsize - 1);
262 1.1 dholland off -= delta;
263 1.1 dholland len += delta;
264 1.1 dholland
265 1.1 dholland genfs_node_wrlock(vp);
266 1.1 dholland mutex_enter(uobj->vmobjlock);
267 1.1 dholland error = VOP_GETPAGES(vp, pagestart, pgs, &npages, 0,
268 1.1 dholland VM_PROT_WRITE, 0, PGO_SYNCIO | PGO_PASTEOF | PGO_NOBLOCKALLOC |
269 1.1 dholland PGO_NOTIMESTAMP | PGO_GLOCKHELD);
270 1.1 dholland if (error) {
271 1.1 dholland goto out;
272 1.1 dholland }
273 1.1 dholland
274 1.1 dholland /*
275 1.1 dholland * now allocate the range.
276 1.1 dholland */
277 1.1 dholland
278 1.1 dholland error = GOP_ALLOC(vp, off, len, flags, cred);
279 1.1 dholland genfs_node_unlock(vp);
280 1.1 dholland
281 1.1 dholland /*
282 1.1 dholland * if the allocation succeeded, clear PG_CLEAN on all the pages
283 1.1 dholland * and clear PG_RDONLY on any pages that are now fully backed
284 1.1 dholland * by disk blocks. if the allocation failed, we do not invalidate
285 1.1 dholland * the pages since they might have already existed and been dirty,
286 1.1 dholland * in which case we need to keep them around. if we created the pages,
287 1.1 dholland * they will be clean and read-only, and leaving such pages
288 1.1 dholland * in the cache won't cause any problems.
289 1.1 dholland */
290 1.1 dholland
291 1.1 dholland GOP_SIZE(vp, off + len, &eob, 0);
292 1.1 dholland mutex_enter(uobj->vmobjlock);
293 1.1 dholland mutex_enter(&uvm_pageqlock);
294 1.1 dholland for (i = 0; i < npages; i++) {
295 1.1 dholland KASSERT((pgs[i]->flags & PG_RELEASED) == 0);
296 1.1 dholland if (!error) {
297 1.1 dholland if (off <= pagestart + (i << PAGE_SHIFT) &&
298 1.1 dholland pagestart + ((i + 1) << PAGE_SHIFT) <= eob) {
299 1.1 dholland pgs[i]->flags &= ~PG_RDONLY;
300 1.1 dholland }
301 1.1 dholland pgs[i]->flags &= ~PG_CLEAN;
302 1.1 dholland }
303 1.1 dholland uvm_pageactivate(pgs[i]);
304 1.1 dholland }
305 1.1 dholland mutex_exit(&uvm_pageqlock);
306 1.1 dholland uvm_page_unbusy(pgs, npages);
307 1.1 dholland mutex_exit(uobj->vmobjlock);
308 1.1 dholland
309 1.1 dholland out:
310 1.1 dholland kmem_free(pgs, pgssize);
311 1.1 dholland return error;
312 1.1 dholland }
313