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