xref: /minix3/sys/ufs/lfs/ulfs_inode.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ulfs_inode.c,v 1.11 2015/09/01 06:16:59 dholland Exp $	*/
284d9c625SLionel Sambuc /*  from NetBSD: ufs_inode.c,v 1.89 2013/01/22 09:39:18 dholland Exp  */
384d9c625SLionel Sambuc 
484d9c625SLionel Sambuc /*
584d9c625SLionel Sambuc  * Copyright (c) 1991, 1993
684d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
784d9c625SLionel Sambuc  * (c) UNIX System Laboratories, Inc.
884d9c625SLionel Sambuc  * All or some portions of this file are derived from material licensed
984d9c625SLionel Sambuc  * to the University of California by American Telephone and Telegraph
1084d9c625SLionel Sambuc  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1184d9c625SLionel Sambuc  * the permission of UNIX System Laboratories, Inc.
1284d9c625SLionel Sambuc  *
1384d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1484d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1584d9c625SLionel Sambuc  * are met:
1684d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1784d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1884d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1984d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
2084d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
2184d9c625SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
2284d9c625SLionel Sambuc  *    may be used to endorse or promote products derived from this software
2384d9c625SLionel Sambuc  *    without specific prior written permission.
2484d9c625SLionel Sambuc  *
2584d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2684d9c625SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2784d9c625SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2884d9c625SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2984d9c625SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3084d9c625SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3184d9c625SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3284d9c625SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3384d9c625SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3484d9c625SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3584d9c625SLionel Sambuc  * SUCH DAMAGE.
3684d9c625SLionel Sambuc  *
3784d9c625SLionel Sambuc  *	@(#)ufs_inode.c	8.9 (Berkeley) 5/14/95
3884d9c625SLionel Sambuc  */
3984d9c625SLionel Sambuc 
4084d9c625SLionel Sambuc #include <sys/cdefs.h>
41*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ulfs_inode.c,v 1.11 2015/09/01 06:16:59 dholland Exp $");
4284d9c625SLionel Sambuc 
4384d9c625SLionel Sambuc #if defined(_KERNEL_OPT)
4484d9c625SLionel Sambuc #include "opt_lfs.h"
4584d9c625SLionel Sambuc #include "opt_quota.h"
4684d9c625SLionel Sambuc #include "opt_wapbl.h"
4784d9c625SLionel Sambuc #endif
4884d9c625SLionel Sambuc 
4984d9c625SLionel Sambuc #include <sys/param.h>
5084d9c625SLionel Sambuc #include <sys/systm.h>
5184d9c625SLionel Sambuc #include <sys/proc.h>
5284d9c625SLionel Sambuc #include <sys/vnode.h>
5384d9c625SLionel Sambuc #include <sys/mount.h>
5484d9c625SLionel Sambuc #include <sys/kernel.h>
5584d9c625SLionel Sambuc #include <sys/namei.h>
5684d9c625SLionel Sambuc #include <sys/kauth.h>
5784d9c625SLionel Sambuc #include <sys/wapbl.h>
5884d9c625SLionel Sambuc #include <sys/fstrans.h>
5984d9c625SLionel Sambuc #include <sys/kmem.h>
6084d9c625SLionel Sambuc 
61*0a6a1f1dSLionel Sambuc #include <ufs/lfs/lfs.h>
62*0a6a1f1dSLionel Sambuc #include <ufs/lfs/lfs_accessors.h>
6384d9c625SLionel Sambuc #include <ufs/lfs/lfs_extern.h>
6484d9c625SLionel Sambuc 
6584d9c625SLionel Sambuc #include <ufs/lfs/ulfs_inode.h>
6684d9c625SLionel Sambuc #include <ufs/lfs/ulfsmount.h>
6784d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extern.h>
6884d9c625SLionel Sambuc #ifdef LFS_DIRHASH
6984d9c625SLionel Sambuc #include <ufs/lfs/ulfs_dirhash.h>
7084d9c625SLionel Sambuc #endif
7184d9c625SLionel Sambuc #ifdef LFS_EXTATTR
7284d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extattr.h>
7384d9c625SLionel Sambuc #endif
7484d9c625SLionel Sambuc 
7584d9c625SLionel Sambuc #include <uvm/uvm.h>
7684d9c625SLionel Sambuc 
7784d9c625SLionel Sambuc extern int prtactive;
7884d9c625SLionel Sambuc 
7984d9c625SLionel Sambuc /*
8084d9c625SLionel Sambuc  * Last reference to an inode.  If necessary, write or delete it.
8184d9c625SLionel Sambuc  */
8284d9c625SLionel Sambuc int
ulfs_inactive(void * v)8384d9c625SLionel Sambuc ulfs_inactive(void *v)
8484d9c625SLionel Sambuc {
8584d9c625SLionel Sambuc 	struct vop_inactive_args /* {
8684d9c625SLionel Sambuc 		struct vnode *a_vp;
8784d9c625SLionel Sambuc 		struct bool *a_recycle;
8884d9c625SLionel Sambuc 	} */ *ap = v;
8984d9c625SLionel Sambuc 	struct vnode *vp = ap->a_vp;
9084d9c625SLionel Sambuc 	struct inode *ip = VTOI(vp);
9184d9c625SLionel Sambuc 	struct mount *transmp;
9284d9c625SLionel Sambuc 	mode_t mode;
9384d9c625SLionel Sambuc 	int error = 0;
9484d9c625SLionel Sambuc 
9584d9c625SLionel Sambuc 	transmp = vp->v_mount;
9684d9c625SLionel Sambuc 	fstrans_start(transmp, FSTRANS_LAZY);
9784d9c625SLionel Sambuc 	/*
9884d9c625SLionel Sambuc 	 * Ignore inodes related to stale file handles.
9984d9c625SLionel Sambuc 	 */
10084d9c625SLionel Sambuc 	if (ip->i_mode == 0)
10184d9c625SLionel Sambuc 		goto out;
10284d9c625SLionel Sambuc 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
10384d9c625SLionel Sambuc #ifdef LFS_EXTATTR
10484d9c625SLionel Sambuc 		ulfs_extattr_vnode_inactive(vp, curlwp);
10584d9c625SLionel Sambuc #endif
10684d9c625SLionel Sambuc 		if (ip->i_size != 0) {
10784d9c625SLionel Sambuc 			error = lfs_truncate(vp, (off_t)0, 0, NOCRED);
10884d9c625SLionel Sambuc 		}
10984d9c625SLionel Sambuc #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
11084d9c625SLionel Sambuc 		(void)lfs_chkiq(ip, -1, NOCRED, 0);
11184d9c625SLionel Sambuc #endif
11284d9c625SLionel Sambuc 		DIP_ASSIGN(ip, rdev, 0);
11384d9c625SLionel Sambuc 		mode = ip->i_mode;
11484d9c625SLionel Sambuc 		ip->i_mode = 0;
11584d9c625SLionel Sambuc 		ip->i_omode = mode;
11684d9c625SLionel Sambuc 		DIP_ASSIGN(ip, mode, 0);
11784d9c625SLionel Sambuc 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
11884d9c625SLionel Sambuc 		/*
11984d9c625SLionel Sambuc 		 * Defer final inode free and update to ulfs_reclaim().
12084d9c625SLionel Sambuc 		 */
12184d9c625SLionel Sambuc 	}
12284d9c625SLionel Sambuc 
12384d9c625SLionel Sambuc 	if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
12484d9c625SLionel Sambuc 		lfs_update(vp, NULL, NULL, 0);
12584d9c625SLionel Sambuc 	}
12684d9c625SLionel Sambuc 
12784d9c625SLionel Sambuc out:
12884d9c625SLionel Sambuc 	/*
12984d9c625SLionel Sambuc 	 * If we are done with the inode, reclaim it
13084d9c625SLionel Sambuc 	 * so that it can be reused immediately.
13184d9c625SLionel Sambuc 	 */
13284d9c625SLionel Sambuc 	*ap->a_recycle = (ip->i_mode == 0);
13384d9c625SLionel Sambuc 	VOP_UNLOCK(vp);
13484d9c625SLionel Sambuc 	fstrans_done(transmp);
13584d9c625SLionel Sambuc 	return (error);
13684d9c625SLionel Sambuc }
13784d9c625SLionel Sambuc 
13884d9c625SLionel Sambuc /*
13984d9c625SLionel Sambuc  * Reclaim an inode so that it can be used for other purposes.
14084d9c625SLionel Sambuc  */
14184d9c625SLionel Sambuc int
ulfs_reclaim(struct vnode * vp)14284d9c625SLionel Sambuc ulfs_reclaim(struct vnode *vp)
14384d9c625SLionel Sambuc {
14484d9c625SLionel Sambuc 	struct inode *ip = VTOI(vp);
14584d9c625SLionel Sambuc 
14684d9c625SLionel Sambuc 	if (prtactive && vp->v_usecount > 1)
14784d9c625SLionel Sambuc 		vprint("ulfs_reclaim: pushing active", vp);
14884d9c625SLionel Sambuc 
14984d9c625SLionel Sambuc 	/* XXX: do we really need two of these? */
15084d9c625SLionel Sambuc 	/* note: originally the first was inside a wapbl txn */
15184d9c625SLionel Sambuc 	lfs_update(vp, NULL, NULL, UPDATE_CLOSE);
15284d9c625SLionel Sambuc 	lfs_update(vp, NULL, NULL, UPDATE_CLOSE);
15384d9c625SLionel Sambuc 
15484d9c625SLionel Sambuc 	/*
155*0a6a1f1dSLionel Sambuc 	 * Remove the inode from the vnode cache.
15684d9c625SLionel Sambuc 	 */
157*0a6a1f1dSLionel Sambuc 	vcache_remove(vp->v_mount, &ip->i_number, sizeof(ip->i_number));
15884d9c625SLionel Sambuc 
15984d9c625SLionel Sambuc 	if (ip->i_devvp) {
16084d9c625SLionel Sambuc 		vrele(ip->i_devvp);
16184d9c625SLionel Sambuc 		ip->i_devvp = 0;
16284d9c625SLionel Sambuc 	}
16384d9c625SLionel Sambuc #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
16484d9c625SLionel Sambuc 	ulfsquota_free(ip);
16584d9c625SLionel Sambuc #endif
16684d9c625SLionel Sambuc #ifdef LFS_DIRHASH
16784d9c625SLionel Sambuc 	if (ip->i_dirhash != NULL)
16884d9c625SLionel Sambuc 		ulfsdirhash_free(ip);
16984d9c625SLionel Sambuc #endif
17084d9c625SLionel Sambuc 	return (0);
17184d9c625SLionel Sambuc }
17284d9c625SLionel Sambuc 
17384d9c625SLionel Sambuc /*
17484d9c625SLionel Sambuc  * allocate a range of blocks in a file.
17584d9c625SLionel Sambuc  * after this function returns, any page entirely contained within the range
17684d9c625SLionel Sambuc  * will map to invalid data and thus must be overwritten before it is made
17784d9c625SLionel Sambuc  * accessible to others.
17884d9c625SLionel Sambuc  */
17984d9c625SLionel Sambuc 
18084d9c625SLionel Sambuc int
ulfs_balloc_range(struct vnode * vp,off_t off,off_t len,kauth_cred_t cred,int flags)18184d9c625SLionel Sambuc ulfs_balloc_range(struct vnode *vp, off_t off, off_t len, kauth_cred_t cred,
18284d9c625SLionel Sambuc     int flags)
18384d9c625SLionel Sambuc {
18484d9c625SLionel Sambuc 	off_t neweof;	/* file size after the operation */
18584d9c625SLionel Sambuc 	off_t neweob;	/* offset next to the last block after the operation */
18684d9c625SLionel Sambuc 	off_t pagestart; /* starting offset of range covered by pgs */
18784d9c625SLionel Sambuc 	off_t eob;	/* offset next to allocated blocks */
18884d9c625SLionel Sambuc 	struct uvm_object *uobj;
18984d9c625SLionel Sambuc 	int i, delta, error, npages;
19084d9c625SLionel Sambuc 	int bshift = vp->v_mount->mnt_fs_bshift;
19184d9c625SLionel Sambuc 	int bsize = 1 << bshift;
19284d9c625SLionel Sambuc 	int ppb = MAX(bsize >> PAGE_SHIFT, 1);
19384d9c625SLionel Sambuc 	struct vm_page **pgs;
19484d9c625SLionel Sambuc 	size_t pgssize;
19584d9c625SLionel Sambuc 	UVMHIST_FUNC("ulfs_balloc_range"); UVMHIST_CALLED(ubchist);
19684d9c625SLionel Sambuc 	UVMHIST_LOG(ubchist, "vp %p off 0x%x len 0x%x u_size 0x%x",
19784d9c625SLionel Sambuc 		    vp, off, len, vp->v_size);
19884d9c625SLionel Sambuc 
19984d9c625SLionel Sambuc 	neweof = MAX(vp->v_size, off + len);
20084d9c625SLionel Sambuc 	GOP_SIZE(vp, neweof, &neweob, 0);
20184d9c625SLionel Sambuc 
20284d9c625SLionel Sambuc 	error = 0;
20384d9c625SLionel Sambuc 	uobj = &vp->v_uobj;
20484d9c625SLionel Sambuc 
20584d9c625SLionel Sambuc 	/*
20684d9c625SLionel Sambuc 	 * read or create pages covering the range of the allocation and
20784d9c625SLionel Sambuc 	 * keep them locked until the new block is allocated, so there
20884d9c625SLionel Sambuc 	 * will be no window where the old contents of the new block are
20984d9c625SLionel Sambuc 	 * visible to racing threads.
21084d9c625SLionel Sambuc 	 */
21184d9c625SLionel Sambuc 
21284d9c625SLionel Sambuc 	pagestart = trunc_page(off) & ~(bsize - 1);
21384d9c625SLionel Sambuc 	npages = MIN(ppb, (round_page(neweob) - pagestart) >> PAGE_SHIFT);
21484d9c625SLionel Sambuc 	pgssize = npages * sizeof(struct vm_page *);
21584d9c625SLionel Sambuc 	pgs = kmem_zalloc(pgssize, KM_SLEEP);
21684d9c625SLionel Sambuc 
21784d9c625SLionel Sambuc 	/*
21884d9c625SLionel Sambuc 	 * adjust off to be block-aligned.
21984d9c625SLionel Sambuc 	 */
22084d9c625SLionel Sambuc 
22184d9c625SLionel Sambuc 	delta = off & (bsize - 1);
22284d9c625SLionel Sambuc 	off -= delta;
22384d9c625SLionel Sambuc 	len += delta;
22484d9c625SLionel Sambuc 
22584d9c625SLionel Sambuc 	genfs_node_wrlock(vp);
22684d9c625SLionel Sambuc 	mutex_enter(uobj->vmobjlock);
22784d9c625SLionel Sambuc 	error = VOP_GETPAGES(vp, pagestart, pgs, &npages, 0,
22884d9c625SLionel Sambuc 	    VM_PROT_WRITE, 0, PGO_SYNCIO | PGO_PASTEOF | PGO_NOBLOCKALLOC |
22984d9c625SLionel Sambuc 	    PGO_NOTIMESTAMP | PGO_GLOCKHELD);
23084d9c625SLionel Sambuc 	if (error) {
23184d9c625SLionel Sambuc 		goto out;
23284d9c625SLionel Sambuc 	}
23384d9c625SLionel Sambuc 
23484d9c625SLionel Sambuc 	/*
23584d9c625SLionel Sambuc 	 * now allocate the range.
23684d9c625SLionel Sambuc 	 */
23784d9c625SLionel Sambuc 
23884d9c625SLionel Sambuc 	error = GOP_ALLOC(vp, off, len, flags, cred);
23984d9c625SLionel Sambuc 	genfs_node_unlock(vp);
24084d9c625SLionel Sambuc 
24184d9c625SLionel Sambuc 	/*
24284d9c625SLionel Sambuc 	 * if the allocation succeeded, clear PG_CLEAN on all the pages
24384d9c625SLionel Sambuc 	 * and clear PG_RDONLY on any pages that are now fully backed
24484d9c625SLionel Sambuc 	 * by disk blocks.  if the allocation failed, we do not invalidate
24584d9c625SLionel Sambuc 	 * the pages since they might have already existed and been dirty,
24684d9c625SLionel Sambuc 	 * in which case we need to keep them around.  if we created the pages,
24784d9c625SLionel Sambuc 	 * they will be clean and read-only, and leaving such pages
24884d9c625SLionel Sambuc 	 * in the cache won't cause any problems.
24984d9c625SLionel Sambuc 	 */
25084d9c625SLionel Sambuc 
25184d9c625SLionel Sambuc 	GOP_SIZE(vp, off + len, &eob, 0);
25284d9c625SLionel Sambuc 	mutex_enter(uobj->vmobjlock);
25384d9c625SLionel Sambuc 	mutex_enter(&uvm_pageqlock);
25484d9c625SLionel Sambuc 	for (i = 0; i < npages; i++) {
25584d9c625SLionel Sambuc 		KASSERT((pgs[i]->flags & PG_RELEASED) == 0);
25684d9c625SLionel Sambuc 		if (!error) {
25784d9c625SLionel Sambuc 			if (off <= pagestart + (i << PAGE_SHIFT) &&
25884d9c625SLionel Sambuc 			    pagestart + ((i + 1) << PAGE_SHIFT) <= eob) {
25984d9c625SLionel Sambuc 				pgs[i]->flags &= ~PG_RDONLY;
26084d9c625SLionel Sambuc 			}
26184d9c625SLionel Sambuc 			pgs[i]->flags &= ~PG_CLEAN;
26284d9c625SLionel Sambuc 		}
26384d9c625SLionel Sambuc 		uvm_pageactivate(pgs[i]);
26484d9c625SLionel Sambuc 	}
26584d9c625SLionel Sambuc 	mutex_exit(&uvm_pageqlock);
26684d9c625SLionel Sambuc 	uvm_page_unbusy(pgs, npages);
26784d9c625SLionel Sambuc 	mutex_exit(uobj->vmobjlock);
26884d9c625SLionel Sambuc 
26984d9c625SLionel Sambuc  out:
27084d9c625SLionel Sambuc  	kmem_free(pgs, pgssize);
27184d9c625SLionel Sambuc 	return error;
27284d9c625SLionel Sambuc }
273