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