160727d8bSWarner Losh /*- 2996c772fSJohn Dyson * Copyright (c) 1991, 1993, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34996c772fSJohn Dyson * @(#)ufs_inode.c 8.9 (Berkeley) 5/14/95 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37f4636c59SDavid E. O'Brien #include <sys/cdefs.h> 38f4636c59SDavid E. O'Brien __FBSDID("$FreeBSD$"); 39f4636c59SDavid E. O'Brien 4001733a9bSGarrett Wollman #include "opt_quota.h" 41516081f2SRobert Watson #include "opt_ufs.h" 4201733a9bSGarrett Wollman 43df8bae1dSRodney W. Grimes #include <sys/param.h> 445df14863SJeff Roberson #include <sys/systm.h> 45df8bae1dSRodney W. Grimes #include <sys/vnode.h> 467106ca0dSJohn Baldwin #include <sys/lock.h> 47df8bae1dSRodney W. Grimes #include <sys/mount.h> 482cfc47fbSPoul-Henning Kamp #include <sys/malloc.h> 491b367556SJason Evans #include <sys/mutex.h> 50df8bae1dSRodney W. Grimes 51a64ed089SRobert Watson #include <ufs/ufs/extattr.h> 52df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h> 53df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h> 542cfc47fbSPoul-Henning Kamp #include <ufs/ufs/ufsmount.h> 55df8bae1dSRodney W. Grimes #include <ufs/ufs/ufs_extern.h> 569b5ad47fSIan Dowse #ifdef UFS_DIRHASH 579b5ad47fSIan Dowse #include <ufs/ufs/dir.h> 589b5ad47fSIan Dowse #include <ufs/ufs/dirhash.h> 599b5ad47fSIan Dowse #endif 601a60c7fcSPawel Jakub Dawidek #ifdef UFS_GJOURNAL 611a60c7fcSPawel Jakub Dawidek #include <ufs/ufs/gjournal.h> 621a60c7fcSPawel Jakub Dawidek #endif 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes /* 65df8bae1dSRodney W. Grimes * Last reference to an inode. If necessary, write or delete it. 66df8bae1dSRodney W. Grimes */ 67df8bae1dSRodney W. Grimes int 68df8bae1dSRodney W. Grimes ufs_inactive(ap) 69df8bae1dSRodney W. Grimes struct vop_inactive_args /* { 70df8bae1dSRodney W. Grimes struct vnode *a_vp; 71b40ce416SJulian Elischer struct thread *a_td; 72df8bae1dSRodney W. Grimes } */ *ap; 73df8bae1dSRodney W. Grimes { 74996c772fSJohn Dyson struct vnode *vp = ap->a_vp; 75996c772fSJohn Dyson struct inode *ip = VTOI(vp); 76b40ce416SJulian Elischer struct thread *td = ap->a_td; 771c85e6a3SKirk McKusick mode_t mode; 781c85e6a3SKirk McKusick int error = 0; 79abf6c181SKonstantin Belousov off_t isize; 80791dd2faSTor Egge struct mount *mp; 81df8bae1dSRodney W. Grimes 82791dd2faSTor Egge mp = NULL; 83996c772fSJohn Dyson /* 84996c772fSJohn Dyson * Ignore inodes related to stale file handles. 85996c772fSJohn Dyson */ 86996c772fSJohn Dyson if (ip->i_mode == 0) 87996c772fSJohn Dyson goto out; 881a60c7fcSPawel Jakub Dawidek #ifdef UFS_GJOURNAL 891a60c7fcSPawel Jakub Dawidek ufs_gjournal_close(vp); 901a60c7fcSPawel Jakub Dawidek #endif 91dca5e0ecSKirk McKusick #ifdef QUOTA 92dca5e0ecSKirk McKusick /* 93dca5e0ecSKirk McKusick * Before moving off the active list, we must be sure that 94dca5e0ecSKirk McKusick * any modified quotas have been pushed since these will no 95dca5e0ecSKirk McKusick * longer be checked once the vnode is on the inactive list. 96dca5e0ecSKirk McKusick */ 97dca5e0ecSKirk McKusick qsyncvp(vp); 98dca5e0ecSKirk McKusick #endif 994613aa0eSTor Egge if ((ip->i_effnlink == 0 && DOINGSOFTDEP(vp)) || 10090446e36SKonstantin Belousov (ip->i_nlink <= 0 && !UFS_RDONLY(ip))) { 101ca2fa807STor Egge loop: 102ca2fa807STor Egge if (vn_start_secondary_write(vp, &mp, V_NOWAIT) != 0) { 103ca2fa807STor Egge /* Cannot delete file while file system is suspended */ 104ca2fa807STor Egge if ((vp->v_iflag & VI_DOOMED) != 0) { 105ca2fa807STor Egge /* Cannot return before file is deleted */ 106ca2fa807STor Egge (void) vn_start_secondary_write(vp, &mp, 107ca2fa807STor Egge V_WAIT); 108ca2fa807STor Egge } else { 109ca2fa807STor Egge MNT_ILOCK(mp); 110ca2fa807STor Egge if ((mp->mnt_kern_flag & 111ca2fa807STor Egge (MNTK_SUSPEND2 | MNTK_SUSPENDED)) == 0) { 112ca2fa807STor Egge MNT_IUNLOCK(mp); 113ca2fa807STor Egge goto loop; 114ca2fa807STor Egge } 115ca2fa807STor Egge /* 116ca2fa807STor Egge * Fail to inactivate vnode now and 117ca2fa807STor Egge * let ffs_snapshot() clean up after 118ca2fa807STor Egge * it has resumed the file system. 119ca2fa807STor Egge */ 120ca2fa807STor Egge VI_LOCK(vp); 121ca2fa807STor Egge vp->v_iflag |= VI_OWEINACT; 122ca2fa807STor Egge VI_UNLOCK(vp); 123ca2fa807STor Egge MNT_IUNLOCK(mp); 124ca2fa807STor Egge return (0); 125ca2fa807STor Egge } 126ca2fa807STor Egge } 1274613aa0eSTor Egge } 1289f9c8c59SJeff Roberson isize = ip->i_size; 1299f9c8c59SJeff Roberson if (ip->i_ump->um_fstype == UFS2) 1309f9c8c59SJeff Roberson isize += ip->i_din2->di_extsize; 131c0f4e7afSJeff Roberson if (ip->i_effnlink <= 0 && isize && !UFS_RDONLY(ip)) 132c52fd858SEdward Tomasz Napierala error = UFS_TRUNCATE(vp, (off_t)0, IO_EXT | IO_NORMAL, NOCRED); 133c0f4e7afSJeff Roberson if (ip->i_nlink <= 0 && ip->i_mode && !UFS_RDONLY(ip)) { 134df8bae1dSRodney W. Grimes #ifdef QUOTA 135df8bae1dSRodney W. Grimes if (!getinoquota(ip)) 136a7d50c22SKirk McKusick (void)chkiq(ip, -1, NOCRED, FORCE); 137df8bae1dSRodney W. Grimes #endif 138516081f2SRobert Watson #ifdef UFS_EXTATTR 1392fc6567eSRobert Watson ufs_extattr_vnode_inactive(vp, td); 140a64ed089SRobert Watson #endif 141812b1d41SKirk McKusick /* 142812b1d41SKirk McKusick * Setting the mode to zero needs to wait for the inode 143812b1d41SKirk McKusick * to be written just as does a change to the link count. 144812b1d41SKirk McKusick * So, rather than creating a new entry point to do the 145812b1d41SKirk McKusick * same thing, we just use softdep_change_linkcnt(). 146812b1d41SKirk McKusick */ 147b403319bSAlexander Kabaev DIP_SET(ip, i_rdev, 0); 148df8bae1dSRodney W. Grimes mode = ip->i_mode; 149df8bae1dSRodney W. Grimes ip->i_mode = 0; 150b403319bSAlexander Kabaev DIP_SET(ip, i_mode, 0); 151df8bae1dSRodney W. Grimes ip->i_flag |= IN_CHANGE | IN_UPDATE; 152812b1d41SKirk McKusick if (DOINGSOFTDEP(vp)) 153812b1d41SKirk McKusick softdep_change_linkcnt(ip); 154cec0f20cSPoul-Henning Kamp UFS_VFREE(vp, ip->i_number, mode); 155df8bae1dSRodney W. Grimes } 156f2a2857bSKirk McKusick if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) { 157f2a2857bSKirk McKusick if ((ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 && 158791dd2faSTor Egge mp == NULL && 159791dd2faSTor Egge vn_start_secondary_write(vp, &mp, V_NOWAIT)) { 160791dd2faSTor Egge mp = NULL; 161f2a2857bSKirk McKusick ip->i_flag &= ~IN_ACCESS; 162f2a2857bSKirk McKusick } else { 163791dd2faSTor Egge if (mp == NULL) 164791dd2faSTor Egge (void) vn_start_secondary_write(vp, &mp, 165791dd2faSTor Egge V_WAIT); 166de5d1ba5SBruce Evans UFS_UPDATE(vp, 0); 167f2a2857bSKirk McKusick } 168f2a2857bSKirk McKusick } 169996c772fSJohn Dyson out: 170df8bae1dSRodney W. Grimes /* 171df8bae1dSRodney W. Grimes * If we are done with the inode, reclaim it 172df8bae1dSRodney W. Grimes * so that it can be reused immediately. 173df8bae1dSRodney W. Grimes */ 174996c772fSJohn Dyson if (ip->i_mode == 0) 175d4eb29baSPoul-Henning Kamp vrecycle(vp, td); 176791dd2faSTor Egge if (mp != NULL) 177791dd2faSTor Egge vn_finished_secondary_write(mp); 178df8bae1dSRodney W. Grimes return (error); 179df8bae1dSRodney W. Grimes } 180df8bae1dSRodney W. Grimes 181d9ca1af7SKonstantin Belousov void 182d9ca1af7SKonstantin Belousov ufs_prepare_reclaim(struct vnode *vp) 183d9ca1af7SKonstantin Belousov { 184d9ca1af7SKonstantin Belousov struct inode *ip; 185d9ca1af7SKonstantin Belousov #ifdef QUOTA 186d9ca1af7SKonstantin Belousov int i; 187d9ca1af7SKonstantin Belousov #endif 188d9ca1af7SKonstantin Belousov 189d9ca1af7SKonstantin Belousov ip = VTOI(vp); 190d9ca1af7SKonstantin Belousov 191d9ca1af7SKonstantin Belousov vnode_destroy_vobject(vp); 192d9ca1af7SKonstantin Belousov #ifdef QUOTA 193d9ca1af7SKonstantin Belousov for (i = 0; i < MAXQUOTAS; i++) { 194d9ca1af7SKonstantin Belousov if (ip->i_dquot[i] != NODQUOT) { 195d9ca1af7SKonstantin Belousov dqrele(vp, ip->i_dquot[i]); 196d9ca1af7SKonstantin Belousov ip->i_dquot[i] = NODQUOT; 197d9ca1af7SKonstantin Belousov } 198d9ca1af7SKonstantin Belousov } 199d9ca1af7SKonstantin Belousov #endif 200d9ca1af7SKonstantin Belousov #ifdef UFS_DIRHASH 201d9ca1af7SKonstantin Belousov if (ip->i_dirhash != NULL) 202d9ca1af7SKonstantin Belousov ufsdirhash_free(ip); 203d9ca1af7SKonstantin Belousov #endif 204d9ca1af7SKonstantin Belousov } 205d9ca1af7SKonstantin Belousov 206df8bae1dSRodney W. Grimes /* 207df8bae1dSRodney W. Grimes * Reclaim an inode so that it can be used for other purposes. 208df8bae1dSRodney W. Grimes */ 209df8bae1dSRodney W. Grimes int 2102cfc47fbSPoul-Henning Kamp ufs_reclaim(ap) 2112cfc47fbSPoul-Henning Kamp struct vop_reclaim_args /* { 2122cfc47fbSPoul-Henning Kamp struct vnode *a_vp; 213b40ce416SJulian Elischer struct thread *a_td; 2142cfc47fbSPoul-Henning Kamp } */ *ap; 215df8bae1dSRodney W. Grimes { 21605f4ff5dSPoul-Henning Kamp struct vnode *vp = ap->a_vp; 2171c85e6a3SKirk McKusick struct inode *ip = VTOI(vp); 2181c85e6a3SKirk McKusick struct ufsmount *ump = ip->i_ump; 219df8bae1dSRodney W. Grimes 220d9ca1af7SKonstantin Belousov ufs_prepare_reclaim(vp); 221d9ca1af7SKonstantin Belousov 222d66ba370SKonstantin Belousov if (ip->i_flag & IN_LAZYMOD) 22330551872SBruce Evans ip->i_flag |= IN_MODIFIED; 224de5d1ba5SBruce Evans UFS_UPDATE(vp, 0); 225df8bae1dSRodney W. Grimes /* 22640715905SPoul-Henning Kamp * Remove the inode from its hash chain. 22740715905SPoul-Henning Kamp */ 22814bc0685SPoul-Henning Kamp vfs_hash_remove(vp); 229d9ca1af7SKonstantin Belousov 23023d15e85SJeff Roberson /* 23123d15e85SJeff Roberson * Lock the clearing of v_data so ffs_lock() can inspect it 23223d15e85SJeff Roberson * prior to obtaining the lock. 23323d15e85SJeff Roberson */ 23423d15e85SJeff Roberson VI_LOCK(vp); 2352cfc47fbSPoul-Henning Kamp vp->v_data = 0; 23623d15e85SJeff Roberson VI_UNLOCK(vp); 23723d15e85SJeff Roberson UFS_IFREE(ump, ip); 238df8bae1dSRodney W. Grimes return (0); 239df8bae1dSRodney W. Grimes } 240