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