1/* $OpenBSD: ufs_inode.c,v 1.45 2024/02/03 18:51:58 beck Exp $ */ 2/* $NetBSD: ufs_inode.c,v 1.7 1996/05/11 18:27:52 mycroft 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.7 (Berkeley) 7/22/94 38 */ 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/vnode.h> 43#include <sys/mount.h> 44#include <sys/malloc.h> 45#include <sys/namei.h> 46 47#include <ufs/ufs/quota.h> 48#include <ufs/ufs/inode.h> 49#include <ufs/ufs/ufsmount.h> 50#include <ufs/ufs/ufs_extern.h> 51#ifdef UFS_DIRHASH 52#include <ufs/ufs/dir.h> 53#include <ufs/ufs/dirhash.h> 54#endif 55 56/* 57 * Last reference to an inode. If necessary, write or delete it. 58 */ 59int 60ufs_inactive(void *v) 61{ 62 struct vop_inactive_args *ap = v; 63 struct vnode *vp = ap->a_vp; 64 struct inode *ip = VTOI(vp); 65 mode_t mode; 66 int error = 0; 67#ifdef DIAGNOSTIC 68 extern int prtactive; 69 70 if (prtactive && vp->v_usecount != 0) 71 vprint("ufs_inactive: pushing active", vp); 72#endif 73 74 /* 75 * Ignore inodes related to stale file handles. 76 */ 77 if (ip->i_din1 == NULL || DIP(ip, mode) == 0) 78 goto out; 79 80 if (DIP(ip, nlink) <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 81 if (getinoquota(ip) == 0) 82 (void)ufs_quota_free_inode(ip, NOCRED); 83 84 error = UFS_TRUNCATE(ip, (off_t)0, 0, NOCRED); 85 86 DIP_ASSIGN(ip, rdev, 0); 87 mode = DIP(ip, mode); 88 DIP_ASSIGN(ip, mode, 0); 89 ip->i_flag |= IN_CHANGE | IN_UPDATE; 90 91 UFS_INODE_FREE(ip, ip->i_number, mode); 92 } 93 94 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 95 UFS_UPDATE(ip, 0); 96 } 97out: 98 VOP_UNLOCK(vp); 99 100 /* 101 * If we are done with the inode, reclaim it 102 * so that it can be reused immediately. 103 */ 104 if (ip->i_din1 == NULL || DIP(ip, mode) == 0) 105 vrecycle(vp, ap->a_p); 106 107 return (error); 108} 109 110/* 111 * Reclaim an inode so that it can be used for other purposes. 112 */ 113int 114ufs_reclaim(struct vnode *vp) 115{ 116 struct inode *ip; 117#ifdef DIAGNOSTIC 118 extern int prtactive; 119 120 if (prtactive && vp->v_usecount != 0) 121 vprint("ufs_reclaim: pushing active", vp); 122#endif 123 124 ip = VTOI(vp); 125 126 /* 127 * Stop deferring timestamp writes 128 */ 129 if (ip->i_flag & IN_LAZYMOD) { 130 ip->i_flag |= IN_MODIFIED; 131 UFS_UPDATE(ip, 0); 132 } 133 134 /* 135 * Remove the inode from its hash chain. 136 */ 137 ufs_ihashrem(ip); 138 /* 139 * Purge old data structures associated with the inode. 140 */ 141 cache_purge(vp); 142 143 if (ip->i_devvp) { 144 vrele(ip->i_devvp); 145 } 146#ifdef UFS_DIRHASH 147 if (ip->i_dirhash != NULL) 148 ufsdirhash_free(ip); 149#endif 150 ufs_quota_delete(ip); 151 return (0); 152} 153