xref: /netbsd-src/sys/ufs/lfs/ulfs_inode.c (revision 9fc453562f6ebe8eabdfd51e21ae0a0058906d4f)
1 /*	$NetBSD: ulfs_inode.c,v 1.26 2020/09/05 16:30:13 riastradh Exp $	*/
2 /*  from NetBSD: ufs_inode.c,v 1.95 2015/06/13 14:56:45 hannken 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.26 2020/09/05 16:30:13 riastradh Exp $");
42 
43 #if defined(_KERNEL_OPT)
44 #include "opt_lfs.h"
45 #include "opt_quota.h"
46 #include "opt_uvmhist.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/kmem.h>
58 
59 #include <ufs/lfs/lfs.h>
60 #include <ufs/lfs/lfs_accessors.h>
61 #include <ufs/lfs/lfs_extern.h>
62 
63 #include <ufs/lfs/ulfs_inode.h>
64 #include <ufs/lfs/ulfsmount.h>
65 #include <ufs/lfs/ulfs_extern.h>
66 #ifdef LFS_DIRHASH
67 #include <ufs/lfs/ulfs_dirhash.h>
68 #endif
69 #ifdef LFS_EXTATTR
70 #include <ufs/lfs/ulfs_extattr.h>
71 #endif
72 
73 #ifdef UVMHIST
74 #include <uvm/uvm.h>
75 #endif
76 #include <uvm/uvm_page.h>
77 #include <uvm/uvm_stat.h>
78 
79 /*
80  * Last reference to an inode.  If necessary, write or delete it.
81  */
82 int
ulfs_inactive(void * v)83 ulfs_inactive(void *v)
84 {
85 	struct vop_inactive_v2_args /* {
86 		struct vnode *a_vp;
87 		struct bool *a_recycle;
88 	} */ *ap = v;
89 	struct vnode *vp = ap->a_vp;
90 	struct inode *ip = VTOI(vp);
91 	mode_t mode;
92 	int error = 0;
93 
94 	/*
95 	 * Ignore inodes related to stale file handles.
96 	 */
97 	if (ip->i_mode == 0)
98 		goto out;
99 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
100 #ifdef LFS_EXTATTR
101 		ulfs_extattr_vnode_inactive(vp, curlwp);
102 #endif
103 		if (ip->i_size != 0) {
104 			error = lfs_truncate(vp, (off_t)0, 0, NOCRED);
105 		}
106 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
107 		(void)lfs_chkiq(ip, -1, NOCRED, 0);
108 #endif
109 		DIP_ASSIGN(ip, rdev, 0);
110 		mode = ip->i_mode;
111 		ip->i_mode = 0;
112 		ip->i_omode = mode;
113 		DIP_ASSIGN(ip, mode, 0);
114 		ip->i_state |= IN_CHANGE | IN_UPDATE;
115 		/*
116 		 * Defer final inode free and update to ulfs_reclaim().
117 		 */
118 	}
119 
120 	if (ip->i_state & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
121 		lfs_update(vp, NULL, NULL, 0);
122 	}
123 
124 out:
125 	/*
126 	 * If we are done with the inode, reclaim it
127 	 * so that it can be reused immediately.
128 	 */
129 	*ap->a_recycle = (ip->i_mode == 0);
130 
131 	return (error);
132 }
133 
134 /*
135  * Reclaim an inode so that it can be used for other purposes.
136  */
137 int
ulfs_reclaim(struct vnode * vp)138 ulfs_reclaim(struct vnode *vp)
139 {
140 	struct inode *ip = VTOI(vp);
141 
142 	/* XXX: do we really need two of these? */
143 	/* note: originally the first was inside a wapbl txn */
144 	lfs_update(vp, NULL, NULL, UPDATE_CLOSE);
145 	lfs_update(vp, NULL, NULL, UPDATE_CLOSE);
146 
147 	if (ip->i_devvp) {
148 		vrele(ip->i_devvp);
149 		ip->i_devvp = 0;
150 	}
151 #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
152 	ulfsquota_free(ip);
153 #endif
154 #ifdef LFS_DIRHASH
155 	if (ip->i_dirhash != NULL)
156 		ulfsdirhash_free(ip);
157 #endif
158 	return (0);
159 }
160 
161 /*
162  * allocate a range of blocks in a file.
163  * after this function returns, any page entirely contained within the range
164  * will map to invalid data and thus must be overwritten before it is made
165  * accessible to others.
166  */
167 
168 int
ulfs_balloc_range(struct vnode * vp,off_t off,off_t len,kauth_cred_t cred,int flags)169 ulfs_balloc_range(struct vnode *vp, off_t off, off_t len, kauth_cred_t cred,
170     int flags)
171 {
172 	off_t neweof;	/* file size after the operation */
173 	off_t neweob;	/* offset next to the last block after the operation */
174 	off_t pagestart; /* starting offset of range covered by pgs */
175 	off_t eob;	/* offset next to allocated blocks */
176 	struct uvm_object *uobj;
177 	int i, delta, error, npages;
178 	int bshift = vp->v_mount->mnt_fs_bshift;
179 	int bsize = 1 << bshift;
180 	int ppb = MAX(bsize >> PAGE_SHIFT, 1);
181 	struct vm_page **pgs;
182 	size_t pgssize;
183 	UVMHIST_FUNC("ulfs_balloc_range"); UVMHIST_CALLED(ubchist);
184 	UVMHIST_LOG(ubchist, "vp %#jx off 0x%jx len 0x%jx u_size 0x%jx",
185 		    (uintptr_t)vp, off, len, vp->v_size);
186 
187 	neweof = MAX(vp->v_size, off + len);
188 	GOP_SIZE(vp, neweof, &neweob, 0);
189 
190 	error = 0;
191 	uobj = &vp->v_uobj;
192 
193 	/*
194 	 * read or create pages covering the range of the allocation and
195 	 * keep them locked until the new block is allocated, so there
196 	 * will be no window where the old contents of the new block are
197 	 * visible to racing threads.
198 	 */
199 
200 	pagestart = trunc_page(off) & ~(bsize - 1);
201 	npages = MIN(ppb, (round_page(neweob) - pagestart) >> PAGE_SHIFT);
202 	pgssize = npages * sizeof(struct vm_page *);
203 	pgs = kmem_zalloc(pgssize, KM_SLEEP);
204 
205 	/*
206 	 * adjust off to be block-aligned.
207 	 */
208 
209 	delta = off & (bsize - 1);
210 	off -= delta;
211 	len += delta;
212 
213 	genfs_node_wrlock(vp);
214 	rw_enter(uobj->vmobjlock, RW_WRITER);
215 	error = VOP_GETPAGES(vp, pagestart, pgs, &npages, 0,
216 	    VM_PROT_WRITE, 0, PGO_SYNCIO | PGO_PASTEOF | PGO_NOBLOCKALLOC |
217 	    PGO_NOTIMESTAMP | PGO_GLOCKHELD);
218 	if (error) {
219 		genfs_node_unlock(vp);
220 		goto out;
221 	}
222 
223 	/*
224 	 * now allocate the range.
225 	 */
226 
227 	error = GOP_ALLOC(vp, off, len, flags, cred);
228 	genfs_node_unlock(vp);
229 
230 	/*
231 	 * if the allocation succeeded, mark all pages dirty and clear
232 	 * PG_RDONLY on any pages that are now fully backed by disk blocks.
233 	 * if the allocation failed, we do not invalidate the pages since
234 	 * they might have already existed and been dirty, in which case we
235 	 * need to keep them around.  if we created the pages, they will be
236 	 * clean and read-only, and leaving such pages in the cache won't
237 	 * cause any problems.
238 	 */
239 
240 	GOP_SIZE(vp, off + len, &eob, 0);
241 	rw_enter(uobj->vmobjlock, RW_WRITER);
242 	for (i = 0; i < npages; i++) {
243 		KASSERT((pgs[i]->flags & PG_RELEASED) == 0);
244 		if (!error) {
245 			if (off <= pagestart + (i << PAGE_SHIFT) &&
246 			    pagestart + ((i + 1) << PAGE_SHIFT) <= eob) {
247 				pgs[i]->flags &= ~PG_RDONLY;
248 			}
249 			uvm_pagemarkdirty(pgs[i], UVM_PAGE_STATUS_DIRTY);
250 		}
251 		uvm_pagelock(pgs[i]);
252 		uvm_pageactivate(pgs[i]);
253 		uvm_pageunlock(pgs[i]);
254 	}
255 	uvm_page_unbusy(pgs, npages);
256 	rw_exit(uobj->vmobjlock);
257 
258  out:
259  	kmem_free(pgs, pgssize);
260 	return error;
261 }
262