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