xref: /netbsd-src/sys/ufs/lfs/lfs_alloc.c (revision 9bf46809165dbc5e69e70487ac78e5e8db0403e2)
1*9bf46809Sriastradh /*	$NetBSD: lfs_alloc.c,v 1.141 2020/02/23 08:49:46 riastradh Exp $	*/
2fccfa11aScgd 
31b8f5ea3Sperseant /*-
49abeea58Sad  * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007 The NetBSD Foundation, Inc.
51b8f5ea3Sperseant  * All rights reserved.
61b8f5ea3Sperseant  *
71b8f5ea3Sperseant  * This code is derived from software contributed to The NetBSD Foundation
81b8f5ea3Sperseant  * by Konrad E. Schroder <perseant@hhhh.org>.
91b8f5ea3Sperseant  *
101b8f5ea3Sperseant  * Redistribution and use in source and binary forms, with or without
111b8f5ea3Sperseant  * modification, are permitted provided that the following conditions
121b8f5ea3Sperseant  * are met:
131b8f5ea3Sperseant  * 1. Redistributions of source code must retain the above copyright
141b8f5ea3Sperseant  *    notice, this list of conditions and the following disclaimer.
151b8f5ea3Sperseant  * 2. Redistributions in binary form must reproduce the above copyright
161b8f5ea3Sperseant  *    notice, this list of conditions and the following disclaimer in the
171b8f5ea3Sperseant  *    documentation and/or other materials provided with the distribution.
181b8f5ea3Sperseant  *
191b8f5ea3Sperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201b8f5ea3Sperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211b8f5ea3Sperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221b8f5ea3Sperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231b8f5ea3Sperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241b8f5ea3Sperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251b8f5ea3Sperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261b8f5ea3Sperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271b8f5ea3Sperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281b8f5ea3Sperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291b8f5ea3Sperseant  * POSSIBILITY OF SUCH DAMAGE.
301b8f5ea3Sperseant  */
31264b874cSmycroft /*
32264b874cSmycroft  * Copyright (c) 1991, 1993
33264b874cSmycroft  *	The Regents of the University of California.  All rights reserved.
34264b874cSmycroft  *
35264b874cSmycroft  * Redistribution and use in source and binary forms, with or without
36264b874cSmycroft  * modification, are permitted provided that the following conditions
37264b874cSmycroft  * are met:
38264b874cSmycroft  * 1. Redistributions of source code must retain the above copyright
39264b874cSmycroft  *    notice, this list of conditions and the following disclaimer.
40264b874cSmycroft  * 2. Redistributions in binary form must reproduce the above copyright
41264b874cSmycroft  *    notice, this list of conditions and the following disclaimer in the
42264b874cSmycroft  *    documentation and/or other materials provided with the distribution.
43aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
44264b874cSmycroft  *    may be used to endorse or promote products derived from this software
45264b874cSmycroft  *    without specific prior written permission.
46264b874cSmycroft  *
47264b874cSmycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48264b874cSmycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49264b874cSmycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50264b874cSmycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51264b874cSmycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52264b874cSmycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53264b874cSmycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54264b874cSmycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55264b874cSmycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56264b874cSmycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57264b874cSmycroft  * SUCH DAMAGE.
58264b874cSmycroft  *
59fccfa11aScgd  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
60264b874cSmycroft  */
61264b874cSmycroft 
62ec624546Slukem #include <sys/cdefs.h>
63*9bf46809Sriastradh __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.141 2020/02/23 08:49:46 riastradh Exp $");
64ec624546Slukem 
6567afbd62Smrg #if defined(_KERNEL_OPT)
66d48f258fSscottr #include "opt_quota.h"
677171cca4Sscottr #endif
68d48f258fSscottr 
69264b874cSmycroft #include <sys/param.h>
707bd9e243Schristos #include <sys/systm.h>
71264b874cSmycroft #include <sys/kernel.h>
72264b874cSmycroft #include <sys/buf.h>
738886b0f4Sperseant #include <sys/lock.h>
74264b874cSmycroft #include <sys/vnode.h>
75264b874cSmycroft #include <sys/syslog.h>
76264b874cSmycroft #include <sys/mount.h>
77ff84dd34Sperseant #include <sys/malloc.h>
7838cf17f4Sthorpej #include <sys/pool.h>
7980373b7eSchs #include <sys/proc.h>
80fc9422c9Selad #include <sys/kauth.h>
81264b874cSmycroft 
8215158895Sdholland #include <ufs/lfs/ulfs_quotacommon.h>
8315158895Sdholland #include <ufs/lfs/ulfs_inode.h>
8415158895Sdholland #include <ufs/lfs/ulfsmount.h>
8515158895Sdholland #include <ufs/lfs/ulfs_extern.h>
86264b874cSmycroft 
87264b874cSmycroft #include <ufs/lfs/lfs.h>
8834f0d74cSdholland #include <ufs/lfs/lfs_accessors.h>
89264b874cSmycroft #include <ufs/lfs/lfs_extern.h>
905bc8cc2bSdholland #include <ufs/lfs/lfs_kernel.h>
91264b874cSmycroft 
92ff84dd34Sperseant /* Constants for inode free bitmap */
9307ebfab8Sperseant #define BMSHIFT 5	/* 2 ** 5 = 32 */
9407ebfab8Sperseant #define BMMASK  ((1 << BMSHIFT) - 1)
9507ebfab8Sperseant #define SET_BITMAP_FREE(F, I) do { \
9607ebfab8Sperseant 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), 	\
9707ebfab8Sperseant 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
985c041fb2Skamil 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1U << ((I) & BMMASK));	\
9907ebfab8Sperseant } while (0)
10007ebfab8Sperseant #define CLR_BITMAP_FREE(F, I) do { \
10107ebfab8Sperseant 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), 	\
10207ebfab8Sperseant 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
1035c041fb2Skamil 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1U << ((I) & BMMASK));	\
10407ebfab8Sperseant } while(0)
10507ebfab8Sperseant 
106ff84dd34Sperseant #define ISSET_BITMAP_FREE(F, I) \
1075c041fb2Skamil 	((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1U << ((I) & BMMASK)))
108ff84dd34Sperseant 
1090055236dSperseant /*
110ea03a1acSperseant  * Add a new block to the Ifile, to accommodate future file creations.
111ea03a1acSperseant  * Called with the segment lock held.
1128886b0f4Sperseant  */
11383771be8Sperseant int
lfs_extend_ifile(struct lfs * fs,kauth_cred_t cred)11483771be8Sperseant lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred)
1150055236dSperseant {
1160055236dSperseant 	struct vnode *vp;
1170055236dSperseant 	struct inode *ip;
11832577c4fSdholland 	IFILE64 *ifp64;
11932577c4fSdholland 	IFILE32 *ifp32;
1204e3fced9Sperseant 	IFILE_V1 *ifp_v1;
1214e3fced9Sperseant 	struct buf *bp, *cbp;
1220055236dSperseant 	int error;
123273df636Schristos 	daddr_t i, blkno, xmax;
124ff84dd34Sperseant 	ino_t oldlast, maxino;
1254e3fced9Sperseant 	CLEANERINFO *cip;
1260055236dSperseant 
1271ebfc508Sperseant 	ASSERT_SEGLOCK(fs);
1281ebfc508Sperseant 
1295251c783Sdholland 	/* XXX should check or assert that we aren't readonly. */
1305251c783Sdholland 
1315251c783Sdholland 	/*
1325251c783Sdholland 	 * Get a block and extend the ifile inode. Leave the buffer for
1335251c783Sdholland 	 * the block in bp.
1345251c783Sdholland 	 */
1355251c783Sdholland 
1360055236dSperseant 	vp = fs->lfs_ivnode;
1370055236dSperseant 	ip = VTOI(vp);
138855bb66eSchristos 	blkno = lfs_lblkno(fs, ip->i_size);
139f59b8f4bSdholland 	if ((error = lfs_balloc(vp, ip->i_size, lfs_sb_getbsize(fs), cred, 0,
1400055236dSperseant 				&bp)) != 0) {
1410055236dSperseant 		return (error);
1420055236dSperseant 	}
143f59b8f4bSdholland 	ip->i_size += lfs_sb_getbsize(fs);
144da32f22cSdholland 	lfs_dino_setsize(fs, ip->i_din, ip->i_size);
14542614ed3Sfvdl 	uvm_vnp_setsize(vp, ip->i_size);
1460055236dSperseant 
1475251c783Sdholland 	/*
1485251c783Sdholland 	 * Compute the new number of inodes, and reallocate the in-memory
1495251c783Sdholland 	 * inode freemap.
1505251c783Sdholland 	 */
1515251c783Sdholland 
152adca8af5Sdholland 	maxino = ((ip->i_size >> lfs_sb_getbshift(fs)) - lfs_sb_getcleansz(fs) -
153f59b8f4bSdholland 		  lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
15407ebfab8Sperseant 	fs->lfs_ino_bitmap = (lfs_bm_t *)
15507ebfab8Sperseant 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
15607ebfab8Sperseant 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
15707ebfab8Sperseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
158ff84dd34Sperseant 
1595251c783Sdholland 	/* first new inode number */
160f59b8f4bSdholland 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
161f59b8f4bSdholland 		lfs_sb_getifpb(fs);
162ff84dd34Sperseant 
163ff84dd34Sperseant 	/*
164ff84dd34Sperseant 	 * We insert the new inodes at the head of the free list.
165ff84dd34Sperseant 	 * Under normal circumstances, the free list is empty here,
166ff84dd34Sperseant 	 * so we are also incidentally placing them at the end (which
167ff84dd34Sperseant 	 * we must do if we are to keep them in order).
168ff84dd34Sperseant 	 */
1694e3fced9Sperseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
1704e3fced9Sperseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
1713a7fbf40Sriastradh 	KASSERTMSG((lfs_sb_getfreehd(fs) != LFS_UNUSED_INUM),
1723a7fbf40Sriastradh 	    "inode 0 allocated [2]");
1735251c783Sdholland 
1745251c783Sdholland 	/* inode number to stop at (XXX: why *x*max?) */
175f59b8f4bSdholland 	xmax = i + lfs_sb_getifpb(fs);
1764e3fced9Sperseant 
1775251c783Sdholland 	/*
1785251c783Sdholland 	 * Initialize the ifile block.
1795251c783Sdholland 	 *
1805251c783Sdholland 	 * XXX: these loops should be restructured to use the accessor
1815251c783Sdholland 	 * functions instead of using cutpaste polymorphism.
1825251c783Sdholland 	 */
1835251c783Sdholland 
18432577c4fSdholland 	if (fs->lfs_is64) {
18532577c4fSdholland 		for (ifp64 = (IFILE64 *)bp->b_data; i < xmax; ++ifp64) {
18632577c4fSdholland 			SET_BITMAP_FREE(fs, i);
18732577c4fSdholland 			ifp64->if_version = 1;
18832577c4fSdholland 			ifp64->if_daddr = LFS_UNUSED_DADDR;
18932577c4fSdholland 			ifp64->if_nextfree = ++i;
19032577c4fSdholland 		}
19132577c4fSdholland 		ifp64--;
19232577c4fSdholland 		ifp64->if_nextfree = oldlast;
19332577c4fSdholland 	} else if (lfs_sb_getversion(fs) > 1) {
19432577c4fSdholland 		for (ifp32 = (IFILE32 *)bp->b_data; i < xmax; ++ifp32) {
19532577c4fSdholland 			SET_BITMAP_FREE(fs, i);
19632577c4fSdholland 			ifp32->if_version = 1;
19732577c4fSdholland 			ifp32->if_daddr = LFS_UNUSED_DADDR;
19832577c4fSdholland 			ifp32->if_nextfree = ++i;
19932577c4fSdholland 		}
20032577c4fSdholland 		ifp32--;
20132577c4fSdholland 		ifp32->if_nextfree = oldlast;
20232577c4fSdholland 	} else {
203273df636Schristos 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
2047119533fSperseant 			SET_BITMAP_FREE(fs, i);
2054e3fced9Sperseant 			ifp_v1->if_version = 1;
2064e3fced9Sperseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
2074e3fced9Sperseant 			ifp_v1->if_nextfree = ++i;
2084e3fced9Sperseant 		}
2094e3fced9Sperseant 		ifp_v1--;
2104e3fced9Sperseant 		ifp_v1->if_nextfree = oldlast;
2114e3fced9Sperseant 	}
212273df636Schristos 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
2134e3fced9Sperseant 
2145251c783Sdholland 	/*
2155251c783Sdholland 	 * Write out the new block.
2165251c783Sdholland 	 */
2175251c783Sdholland 
2188886b0f4Sperseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
2190055236dSperseant 
2200055236dSperseant 	return 0;
2210055236dSperseant }
2220055236dSperseant 
2235251c783Sdholland /*
2245251c783Sdholland  * Allocate an inode for a new file.
2255251c783Sdholland  *
2265251c783Sdholland  * Takes the segment lock. Also (while holding it) takes lfs_lock
2275251c783Sdholland  * to frob fs->lfs_fmod.
2285251c783Sdholland  *
2295251c783Sdholland  * XXX: the mode argument is unused; should just get rid of it.
2305251c783Sdholland  */
231264b874cSmycroft /* ARGSUSED */
2329c7f8050Sperseant /* VOP_BWRITE 2i times */
233264b874cSmycroft int
lfs_valloc(struct vnode * pvp,int mode,kauth_cred_t cred,ino_t * ino,int * gen)234168cd830Schristos lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
235d8868b1eShannken     ino_t *ino, int *gen)
2367bd9e243Schristos {
237264b874cSmycroft 	struct lfs *fs;
2384e3fced9Sperseant 	struct buf *bp, *cbp;
23932577c4fSdholland 	IFILE *ifp;
240264b874cSmycroft 	int error;
2414e3fced9Sperseant 	CLEANERINFO *cip;
242264b874cSmycroft 
243a748ea88Syamt 	fs = VTOI(pvp)->i_lfs;
24439b86955Sperseant 	if (fs->lfs_ronly)
24539b86955Sperseant 		return EROFS;
2461b8f5ea3Sperseant 
2471ebfc508Sperseant 	ASSERT_NO_SEGLOCK(fs);
2481ebfc508Sperseant 
2498886b0f4Sperseant 	lfs_seglock(fs, SEGM_PROT);
2501b8f5ea3Sperseant 
2511b8f5ea3Sperseant 	/* Get the head of the freelist. */
252d8868b1eShannken 	LFS_GET_HEADFREE(fs, cip, cbp, ino);
2534e3fced9Sperseant 
2545251c783Sdholland 	/* paranoia */
2555251c783Sdholland 	KASSERT(*ino != LFS_UNUSED_INUM && *ino != LFS_IFILE_INUM);
256d8868b1eShannken 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %" PRId64 "\n",
257d8868b1eShannken 	     *ino));
258264b874cSmycroft 
2595251c783Sdholland 	/* Update the in-memory inode freemap */
260d8868b1eShannken 	CLR_BITMAP_FREE(fs, *ino);
2615251c783Sdholland 
2625251c783Sdholland 	/*
2635251c783Sdholland 	 * Fetch the ifile entry and make sure the inode is really
2645251c783Sdholland 	 * free.
2655251c783Sdholland 	 */
266d8868b1eShannken 	LFS_IENTRY(ifp, fs, *ino, bp);
26732577c4fSdholland 	if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR)
268d8868b1eShannken 		panic("lfs_valloc: inuse inode %" PRId64 " on the free list",
269d8868b1eShannken 		    *ino);
2705251c783Sdholland 
2715251c783Sdholland 	/* Update the inode freelist head in the superblock. */
27232577c4fSdholland 	LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp));
27332577c4fSdholland 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %" PRId64 " -> %ju\n",
27432577c4fSdholland 	     *ino, (uintmax_t)lfs_if_getnextfree(fs, ifp)));
2754e3fced9Sperseant 
2765251c783Sdholland 	/*
2775251c783Sdholland 	 * Retrieve the version number from the ifile entry. It was
2785251c783Sdholland 	 * bumped by vfree, so don't bump it again.
2795251c783Sdholland 	 */
28032577c4fSdholland 	*gen = lfs_if_getversion(fs, ifp);
2815251c783Sdholland 
2825251c783Sdholland 	/* Done with ifile entry */
2835c3b2b3fSad 	brelse(bp, 0);
284264b874cSmycroft 
285f59b8f4bSdholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
2865251c783Sdholland 		/*
2875251c783Sdholland 		 * No more inodes; extend the ifile so that the next
2885251c783Sdholland 		 * lfs_valloc will succeed.
2895251c783Sdholland 		 */
29083771be8Sperseant 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
2915251c783Sdholland 			/* restore the freelist */
292d8868b1eShannken 			LFS_PUT_HEADFREE(fs, cip, cbp, *ino);
2935251c783Sdholland 
2945251c783Sdholland 			/* unlock and return */
2958886b0f4Sperseant 			lfs_segunlock(fs);
2960055236dSperseant 			return error;
297dac789beSperseant 		}
2981b8f5ea3Sperseant 	}
2993a7fbf40Sriastradh 	KASSERTMSG((lfs_sb_getfreehd(fs) != LFS_UNUSED_INUM),
3003a7fbf40Sriastradh 	    "inode 0 allocated [3]");
301264b874cSmycroft 
3025251c783Sdholland 	/* Set superblock modified bit */
3034a780c9aSad 	mutex_enter(&lfs_lock);
3041ebfc508Sperseant 	fs->lfs_fmod = 1;
3054a780c9aSad 	mutex_exit(&lfs_lock);
3065251c783Sdholland 
3075251c783Sdholland 	/* increment file count */
308f59b8f4bSdholland 	lfs_sb_addnfiles(fs, 1);
3091ebfc508Sperseant 
3105251c783Sdholland 	/* done */
3118886b0f4Sperseant 	lfs_segunlock(fs);
312d8868b1eShannken 	return 0;
3130055236dSperseant }
3140055236dSperseant 
315ea03a1acSperseant /*
3165251c783Sdholland  * Allocate an inode for a new file, with given inode number and
3175251c783Sdholland  * version.
3185251c783Sdholland  *
3195251c783Sdholland  * Called in the same context as lfs_valloc and therefore shares the
3205251c783Sdholland  * same locking assumptions.
321ea03a1acSperseant  */
32283771be8Sperseant int
lfs_valloc_fixed(struct lfs * fs,ino_t ino,int vers)323d8868b1eShannken lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
3240055236dSperseant {
325d8868b1eShannken 	IFILE *ifp;
326d8868b1eShannken 	struct buf *bp, *cbp;
3278f73830cSdholland 	ino_t headino, thisino, oldnext;
328d8868b1eShannken 	CLEANERINFO *cip;
3290055236dSperseant 
330b3a28a4cSmaya 	if (fs->lfs_ronly)
331b3a28a4cSmaya 		return EROFS;
332b3a28a4cSmaya 
333b3a28a4cSmaya 	ASSERT_NO_SEGLOCK(fs);
334b3a28a4cSmaya 
335b3a28a4cSmaya 	lfs_seglock(fs, SEGM_PROT);
3365251c783Sdholland 
3375251c783Sdholland 	/*
3385251c783Sdholland 	 * If the ifile is too short to contain this inum, extend it.
3395251c783Sdholland 	 *
3405251c783Sdholland 	 * XXX: lfs_extend_ifile should take a size instead of always
3415251c783Sdholland 	 * doing just one block at time.
3425251c783Sdholland 	 */
343d8868b1eShannken 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
344f59b8f4bSdholland 		lfs_sb_getifpb(fs) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
345adca8af5Sdholland 		<< lfs_sb_getbshift(fs)) {
346d8868b1eShannken 		lfs_extend_ifile(fs, NOCRED);
34742614ed3Sfvdl 	}
348264b874cSmycroft 
3495251c783Sdholland 	/*
3505251c783Sdholland 	 * fetch the ifile entry; get the inode freelist next pointer,
3515251c783Sdholland 	 * and set the version as directed.
3525251c783Sdholland 	 */
353d8868b1eShannken 	LFS_IENTRY(ifp, fs, ino, bp);
35432577c4fSdholland 	oldnext = lfs_if_getnextfree(fs, ifp);
35532577c4fSdholland 	lfs_if_setversion(fs, ifp, vers);
356d8868b1eShannken 	brelse(bp, 0);
357264b874cSmycroft 
3585251c783Sdholland 	/* Get head of inode freelist */
3598f73830cSdholland 	LFS_GET_HEADFREE(fs, cip, cbp, &headino);
3608f73830cSdholland 	if (headino == ino) {
3615251c783Sdholland 		/* Easy case: the inode we wanted was at the head */
362d8868b1eShannken 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
363d8868b1eShannken 	} else {
36432577c4fSdholland 		ino_t nextfree;
36532577c4fSdholland 
3665251c783Sdholland 		/* Have to find the desired inode in the freelist... */
3675251c783Sdholland 
3688f73830cSdholland 		thisino = headino;
369d8868b1eShannken 		while (1) {
3705251c783Sdholland 			/* read this ifile entry */
3718f73830cSdholland 			LFS_IENTRY(ifp, fs, thisino, bp);
37232577c4fSdholland 			nextfree = lfs_if_getnextfree(fs, ifp);
3735251c783Sdholland 			/* stop if we find it or we hit the end */
37432577c4fSdholland 			if (nextfree == ino ||
37532577c4fSdholland 			    nextfree == LFS_UNUSED_INUM)
376d8868b1eShannken 				break;
3775251c783Sdholland 			/* nope, keep going... */
3788f73830cSdholland 			thisino = nextfree;
379d8868b1eShannken 			brelse(bp, 0);
380d8868b1eShannken 		}
38132577c4fSdholland 		if (nextfree == LFS_UNUSED_INUM) {
3825251c783Sdholland 			/* hit the end -- this inode is not available */
383d8868b1eShannken 			brelse(bp, 0);
384b3a28a4cSmaya 			lfs_segunlock(fs);
385d8868b1eShannken 			return ENOENT;
386d8868b1eShannken 		}
3875251c783Sdholland 		/* found it; update the next pointer */
38832577c4fSdholland 		lfs_if_setnextfree(fs, ifp, oldnext);
3895251c783Sdholland 		/* write the ifile block */
390d8868b1eShannken 		LFS_BWRITE_LOG(bp);
391264b874cSmycroft 	}
392264b874cSmycroft 
3935251c783Sdholland 	/* done */
394b3a28a4cSmaya 	lfs_segunlock(fs);
395d8868b1eShannken 	return 0;
396264b874cSmycroft }
397264b874cSmycroft 
398ff84dd34Sperseant #if 0
399ff84dd34Sperseant /*
400ff84dd34Sperseant  * Find the highest-numbered allocated inode.
401ff84dd34Sperseant  * This will be used to shrink the Ifile.
402ff84dd34Sperseant  */
403ff84dd34Sperseant static inline ino_t
404ff84dd34Sperseant lfs_last_alloc_ino(struct lfs *fs)
405ff84dd34Sperseant {
406ff84dd34Sperseant 	ino_t ino, maxino;
407ff84dd34Sperseant 
408adca8af5Sdholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
409ae888872Sdholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) *
410ae888872Sdholland 		lfs_sb_getifpb(fs);
411ff84dd34Sperseant 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
412ff84dd34Sperseant 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
413ff84dd34Sperseant 			break;
414ff84dd34Sperseant 	}
415ff84dd34Sperseant 	return ino;
416ff84dd34Sperseant }
417ff84dd34Sperseant #endif
418ff84dd34Sperseant 
419ff84dd34Sperseant /*
420ff84dd34Sperseant  * Find the previous (next lowest numbered) free inode, if any.
421ff84dd34Sperseant  * If there is none, return LFS_UNUSED_INUM.
4225251c783Sdholland  *
4235251c783Sdholland  * XXX: locking?
424ff84dd34Sperseant  */
425ff84dd34Sperseant static inline ino_t
lfs_freelist_prev(struct lfs * fs,ino_t ino)426ff84dd34Sperseant lfs_freelist_prev(struct lfs *fs, ino_t ino)
427ff84dd34Sperseant {
42807ebfab8Sperseant 	ino_t tino, bound, bb, freehdbb;
429ff84dd34Sperseant 
4305251c783Sdholland 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
4315251c783Sdholland 		/* No free inodes at all */
43207ebfab8Sperseant 		return LFS_UNUSED_INUM;
4335251c783Sdholland 	}
43407ebfab8Sperseant 
43507ebfab8Sperseant 	/* Search our own word first */
43607ebfab8Sperseant 	bound = ino & ~BMMASK;
4377119533fSperseant 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
43807ebfab8Sperseant 		if (ISSET_BITMAP_FREE(fs, tino))
43907ebfab8Sperseant 			return tino;
44007ebfab8Sperseant 	/* If there are no lower words to search, just return */
44107ebfab8Sperseant 	if (ino >> BMSHIFT == 0)
44207ebfab8Sperseant 		return LFS_UNUSED_INUM;
44307ebfab8Sperseant 
44407ebfab8Sperseant 	/*
44507ebfab8Sperseant 	 * Find a word with a free inode in it.  We have to be a bit
44607ebfab8Sperseant 	 * careful here since ino_t is unsigned.
44707ebfab8Sperseant 	 */
448f59b8f4bSdholland 	freehdbb = (lfs_sb_getfreehd(fs) >> BMSHIFT);
44907ebfab8Sperseant 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
45007ebfab8Sperseant 		if (fs->lfs_ino_bitmap[bb])
45107ebfab8Sperseant 			break;
45207ebfab8Sperseant 	if (fs->lfs_ino_bitmap[bb] == 0)
45307ebfab8Sperseant 		return LFS_UNUSED_INUM;
45407ebfab8Sperseant 
45507ebfab8Sperseant 	/* Search the word we found */
4567119533fSperseant 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
4577119533fSperseant 	     tino > LFS_UNUSED_INUM; tino--)
458ff84dd34Sperseant 		if (ISSET_BITMAP_FREE(fs, tino))
459ff84dd34Sperseant 			break;
46007ebfab8Sperseant 
4615251c783Sdholland 	/* Avoid returning reserved inode numbers */
462ff84dd34Sperseant 	if (tino <= LFS_IFILE_INUM)
463ff84dd34Sperseant 		tino = LFS_UNUSED_INUM;
464ff84dd34Sperseant 
465ff84dd34Sperseant 	return tino;
466ff84dd34Sperseant }
467ff84dd34Sperseant 
4685251c783Sdholland /*
4695251c783Sdholland  * Free an inode.
4705251c783Sdholland  *
4715251c783Sdholland  * Takes lfs_seglock. Also (independently) takes vp->v_interlock.
4725251c783Sdholland  */
473264b874cSmycroft /* ARGUSED */
4749c7f8050Sperseant /* VOP_BWRITE 2i times */
475264b874cSmycroft int
lfs_vfree(struct vnode * vp,ino_t ino,int mode)476168cd830Schristos lfs_vfree(struct vnode *vp, ino_t ino, int mode)
4777bd9e243Schristos {
478264b874cSmycroft 	SEGUSE *sup;
4794e3fced9Sperseant 	CLEANERINFO *cip;
4804e3fced9Sperseant 	struct buf *cbp, *bp;
48132577c4fSdholland 	IFILE *ifp;
482264b874cSmycroft 	struct inode *ip;
483264b874cSmycroft 	struct lfs *fs;
484a3ff3a30Sfvdl 	daddr_t old_iaddr;
485a748ea88Syamt 	ino_t otail;
486264b874cSmycroft 
487264b874cSmycroft 	/* Get the inode number and file system. */
48832bd59cdSperseant 	ip = VTOI(vp);
489264b874cSmycroft 	fs = ip->i_lfs;
490264b874cSmycroft 	ino = ip->i_number;
4911b8f5ea3Sperseant 
4925251c783Sdholland 	/* XXX: assert not readonly */
4935251c783Sdholland 
4941ebfc508Sperseant 	ASSERT_NO_SEGLOCK(fs);
49507ebfab8Sperseant 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
4961ebfc508Sperseant 
4974e3fced9Sperseant 	/* Drain of pending writes */
498e225b7bdSrmind 	mutex_enter(vp->v_interlock);
499992b9a23Sdholland 	while (lfs_sb_getversion(fs) > 1 && WRITEINPROG(vp)) {
500e225b7bdSrmind 		cv_wait(&vp->v_cv, vp->v_interlock);
5014a780c9aSad 	}
502e225b7bdSrmind 	mutex_exit(vp->v_interlock);
5034e3fced9Sperseant 
50491390453Sperseant 	lfs_seglock(fs, SEGM_PROT);
5051b8f5ea3Sperseant 
5065251c783Sdholland 	/*
5075251c783Sdholland 	 * If the inode was in a dirop, it isn't now.
5085251c783Sdholland 	 *
5098f063ba0Smaya 	 * XXX: why are (v_uflag & VU_DIROP) and (ip->i_state & IN_ADIROP)
5105251c783Sdholland 	 * not updated together in one function? (and why do both exist,
5115251c783Sdholland 	 * anyway?)
5125251c783Sdholland 	 */
513c3a3e06eSmaya 	UNMARK_VNODE(vp);
5145251c783Sdholland 
5154a780c9aSad 	mutex_enter(&lfs_lock);
5167dad9f73Sad 	if (vp->v_uflag & VU_DIROP) {
5177dad9f73Sad 		vp->v_uflag &= ~VU_DIROP;
5181ebfc508Sperseant 		--lfs_dirvcount;
519ce053245Sperseant 		--fs->lfs_dirvcount;
520b397c875Sperseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
521ce053245Sperseant 		wakeup(&fs->lfs_dirvcount);
52232bd59cdSperseant 		wakeup(&lfs_dirvcount);
5234a780c9aSad 		mutex_exit(&lfs_lock);
524d8868b1eShannken 		vrele(vp);
525481da54fSperseant 
526481da54fSperseant 		/*
527481da54fSperseant 		 * If this inode is not going to be written any more, any
528481da54fSperseant 		 * segment accounting left over from its truncation needs
529481da54fSperseant 		 * to occur at the end of the next dirops flush.  Attach
530481da54fSperseant 		 * them to the fs-wide list for that purpose.
531481da54fSperseant 		 */
532481da54fSperseant 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
533481da54fSperseant 			struct segdelta *sd;
534481da54fSperseant 
535481da54fSperseant 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
536481da54fSperseant 				LIST_REMOVE(sd, list);
537481da54fSperseant 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
538481da54fSperseant 			}
539481da54fSperseant 		}
540481da54fSperseant 	} else {
541481da54fSperseant 		/*
542481da54fSperseant 		 * If it's not a dirop, we can finalize right away.
543481da54fSperseant 		 */
5444a780c9aSad 		mutex_exit(&lfs_lock);
545481da54fSperseant 		lfs_finalize_ino_seguse(fs, ip);
54632bd59cdSperseant 	}
54732bd59cdSperseant 
5485251c783Sdholland 	/* it is no longer an unwritten inode, so update the counts */
5494a780c9aSad 	mutex_enter(&lfs_lock);
55090b9d9b5Sperseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
5514a780c9aSad 	mutex_exit(&lfs_lock);
5525251c783Sdholland 
5535251c783Sdholland 	/* Turn off all inode modification flags */
5548f063ba0Smaya 	ip->i_state &= ~IN_ALLMOD;
5555251c783Sdholland 
5565251c783Sdholland 	/* Mark it deleted */
557285f68c1Sperseant 	ip->i_lfs_iflags |= LFSI_DELETED;
55890b9d9b5Sperseant 
5595251c783Sdholland 	/* Mark it free in the in-memory inode freemap */
5605251c783Sdholland 	SET_BITMAP_FREE(fs, ino);
5615251c783Sdholland 
562264b874cSmycroft 	/*
563264b874cSmycroft 	 * Set the ifile's inode entry to unused, increment its version number
5644e3fced9Sperseant 	 * and link it onto the free chain.
565264b874cSmycroft 	 */
5665251c783Sdholland 
5675251c783Sdholland 	/* fetch the ifile entry */
568264b874cSmycroft 	LFS_IENTRY(ifp, fs, ino, bp);
5695251c783Sdholland 
5705251c783Sdholland 	/* update the on-disk address (to "nowhere") */
57132577c4fSdholland 	old_iaddr = lfs_if_getdaddr(fs, ifp);
57232577c4fSdholland 	lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
5735251c783Sdholland 
5745251c783Sdholland 	/* bump the version */
57532577c4fSdholland 	lfs_if_setversion(fs, ifp, lfs_if_getversion(fs, ifp) + 1);
5765251c783Sdholland 
577992b9a23Sdholland 	if (lfs_sb_getversion(fs) == 1) {
57832577c4fSdholland 		ino_t nextfree;
57932577c4fSdholland 
5805251c783Sdholland 		/* insert on freelist */
58132577c4fSdholland 		LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
58232577c4fSdholland 		lfs_if_setnextfree(fs, ifp, nextfree);
5834e3fced9Sperseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
5845251c783Sdholland 
5855251c783Sdholland 		/* write the ifile block */
5868886b0f4Sperseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
5874e3fced9Sperseant 	} else {
588ff84dd34Sperseant 		ino_t tino, onf;
589ff84dd34Sperseant 
5905251c783Sdholland 		/*
5915251c783Sdholland 		 * Clear the freelist next pointer and write the ifile
5925251c783Sdholland 		 * block. XXX: why? I'm sure there must be a reason but
5935251c783Sdholland 		 * it seems both silly and dangerous.
5945251c783Sdholland 		 */
59532577c4fSdholland 		lfs_if_setnextfree(fs, ifp, LFS_UNUSED_INUM);
5968886b0f4Sperseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
597ff84dd34Sperseant 
5985251c783Sdholland 		/*
5995251c783Sdholland 		 * Insert on freelist in order.
6005251c783Sdholland 		 */
6015251c783Sdholland 
6025251c783Sdholland 		/* Find the next lower (by number) free inode */
603ff84dd34Sperseant 		tino = lfs_freelist_prev(fs, ino);
6045251c783Sdholland 
605ff84dd34Sperseant 		if (tino == LFS_UNUSED_INUM) {
60632577c4fSdholland 			ino_t nextfree;
60732577c4fSdholland 
6085251c783Sdholland 			/*
6095251c783Sdholland 			 * There isn't one; put us on the freelist head.
6105251c783Sdholland 			 */
6115251c783Sdholland 
6125251c783Sdholland 			/* reload the ifile block */
613ff84dd34Sperseant 			LFS_IENTRY(ifp, fs, ino, bp);
6145251c783Sdholland 			/* update the list */
61532577c4fSdholland 			LFS_GET_HEADFREE(fs, cip, cbp, &nextfree);
61632577c4fSdholland 			lfs_if_setnextfree(fs, ifp, nextfree);
617ff84dd34Sperseant 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
61807ebfab8Sperseant 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
61932577c4fSdholland 			     (long long)nextfree, (long long)ino));
6205251c783Sdholland 			/* write the ifile block */
621ff84dd34Sperseant 			LFS_BWRITE_LOG(bp); /* Ifile */
622ff84dd34Sperseant 
623ff84dd34Sperseant 			/* If the list was empty, set tail too */
6244e3fced9Sperseant 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
625ff84dd34Sperseant 			if (otail == LFS_UNUSED_INUM) {
6264e3fced9Sperseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
627ff84dd34Sperseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
628ff84dd34Sperseant 				      "-> %lld\n", (long long)otail,
629ff84dd34Sperseant 				      (long long)ino));
630ff84dd34Sperseant 			}
631ff84dd34Sperseant 		} else {
632ff84dd34Sperseant 			/*
633ff84dd34Sperseant 			 * Insert this inode into the list after tino.
634ff84dd34Sperseant 			 * We hold the segment lock so we don't have to
635ff84dd34Sperseant 			 * worry about blocks being written out of order.
636ff84dd34Sperseant 			 */
6375251c783Sdholland 
638ff84dd34Sperseant 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
639ff84dd34Sperseant 			      " after %lld\n", ino, tino));
640ff84dd34Sperseant 
6415251c783Sdholland 			/* load the previous inode's ifile block */
642ff84dd34Sperseant 			LFS_IENTRY(ifp, fs, tino, bp);
6435251c783Sdholland 			/* update the list pointer */
64432577c4fSdholland 			onf = lfs_if_getnextfree(fs, ifp);
64532577c4fSdholland 			lfs_if_setnextfree(fs, ifp, ino);
6465251c783Sdholland 			/* write the block */
647ff84dd34Sperseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
648ff84dd34Sperseant 
6495251c783Sdholland 			/* load this inode's ifile block */
650ff84dd34Sperseant 			LFS_IENTRY(ifp, fs, ino, bp);
6515251c783Sdholland 			/* update the list pointer */
65232577c4fSdholland 			lfs_if_setnextfree(fs, ifp, onf);
6535251c783Sdholland 			/* write the block */
654ff84dd34Sperseant 			LFS_BWRITE_LOG(bp);	/* Ifile */
655ff84dd34Sperseant 
656ff84dd34Sperseant 			/* If we're last, put us on the tail */
657ff84dd34Sperseant 			if (onf == LFS_UNUSED_INUM) {
658ff84dd34Sperseant 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
659ff84dd34Sperseant 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
660ff84dd34Sperseant 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
661ff84dd34Sperseant 				      "-> %lld\n", (long long)otail,
662ff84dd34Sperseant 				      (long long)ino));
663ff84dd34Sperseant 			}
664ff84dd34Sperseant 		}
6654e3fced9Sperseant 	}
6665251c783Sdholland 	/* XXX: shouldn't this check be further up *before* we trash the fs? */
6673a7fbf40Sriastradh 	KASSERTMSG((ino != LFS_UNUSED_INUM), "inode 0 freed");
6685251c783Sdholland 
6695251c783Sdholland 	/*
6705251c783Sdholland 	 * Update the segment summary for the segment where the on-disk
6715251c783Sdholland 	 * copy used to be.
6725251c783Sdholland 	 */
673264b874cSmycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
6745251c783Sdholland 		/* load it */
675855bb66eSchristos 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
6765251c783Sdholland 		/* the number of bytes in the segment should not become < 0 */
6773a7fbf40Sriastradh 		KASSERTMSG((sup->su_nbytes >= DINOSIZE(fs)),
6783a7fbf40Sriastradh 		    "lfs_vfree: negative byte count"
679e41d3a6fSyamt 		    " (segment %" PRIu32 " short by %d)\n",
680855bb66eSchristos 		    lfs_dtosn(fs, old_iaddr),
6813a7fbf40Sriastradh 		    (int)DINOSIZE(fs) - sup->su_nbytes);
6825251c783Sdholland 		/* update the number of bytes in the segment */
683b1828e0bSdholland 		sup->su_nbytes -= DINOSIZE(fs);
6845251c783Sdholland 		/* write the segment entry */
685855bb66eSchristos 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
686264b874cSmycroft 	}
687264b874cSmycroft 
6885251c783Sdholland 	/* Set superblock modified bit. */
6894a780c9aSad 	mutex_enter(&lfs_lock);
690264b874cSmycroft 	fs->lfs_fmod = 1;
6914a780c9aSad 	mutex_exit(&lfs_lock);
6925251c783Sdholland 
6935251c783Sdholland 	/* Decrement file count. */
694f59b8f4bSdholland 	lfs_sb_subnfiles(fs, 1);
6951b8f5ea3Sperseant 
6968886b0f4Sperseant 	lfs_segunlock(fs);
697b397c875Sperseant 
698264b874cSmycroft 	return (0);
699264b874cSmycroft }
700ff84dd34Sperseant 
701ff84dd34Sperseant /*
702ff84dd34Sperseant  * Sort the freelist and set up the free-inode bitmap.
703ff84dd34Sperseant  * To be called by lfs_mountfs().
7045251c783Sdholland  *
7055251c783Sdholland  * Takes the segmenet lock.
706ff84dd34Sperseant  */
707ff84dd34Sperseant void
lfs_order_freelist(struct lfs * fs,ino_t ** orphanp,size_t * norphanp)708*9bf46809Sriastradh lfs_order_freelist(struct lfs *fs, ino_t **orphanp, size_t *norphanp)
709ff84dd34Sperseant {
710ff84dd34Sperseant 	CLEANERINFO *cip;
711ff84dd34Sperseant 	IFILE *ifp = NULL;
712ff84dd34Sperseant 	struct buf *bp;
713ff84dd34Sperseant 	ino_t ino, firstino, lastino, maxino;
714*9bf46809Sriastradh 	ino_t *orphan = NULL;
715*9bf46809Sriastradh 	size_t norphan = 0;
716*9bf46809Sriastradh 	size_t norphan_alloc = 0;
717ff84dd34Sperseant 
718a2aa7212Sperseant 	ASSERT_NO_SEGLOCK(fs);
719a2aa7212Sperseant 	lfs_seglock(fs, SEGM_PROT);
720a2aa7212Sperseant 
7215251c783Sdholland 	/* largest inode on fs */
722adca8af5Sdholland 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
723f59b8f4bSdholland 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
7245251c783Sdholland 
7255251c783Sdholland 	/* allocate the in-memory inode freemap */
7265251c783Sdholland 	/* XXX: assert that fs->lfs_ino_bitmap is null here */
72761051b20Sdholland 	fs->lfs_ino_bitmap =
72807ebfab8Sperseant 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
729ff84dd34Sperseant 		       M_SEGMENT, M_WAITOK | M_ZERO);
73007ebfab8Sperseant 	KASSERT(fs->lfs_ino_bitmap != NULL);
731ff84dd34Sperseant 
7325251c783Sdholland 	/*
7335251c783Sdholland 	 * Scan the ifile.
7345251c783Sdholland 	 */
7355251c783Sdholland 
736ff84dd34Sperseant 	firstino = lastino = LFS_UNUSED_INUM;
737ff84dd34Sperseant 	for (ino = 0; ino < maxino; ino++) {
7385251c783Sdholland 		/* Load this inode's ifile entry. */
739f59b8f4bSdholland 		if (ino % lfs_sb_getifpb(fs) == 0)
740ff84dd34Sperseant 			LFS_IENTRY(ifp, fs, ino, bp);
741ff84dd34Sperseant 		else
742a78588c6Smlelstv 			LFS_IENTRY_NEXT(ifp, fs);
743ff84dd34Sperseant 
744ff84dd34Sperseant 		/* Don't put zero or ifile on the free list */
745ff84dd34Sperseant 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
746ff84dd34Sperseant 			continue;
747ff84dd34Sperseant 
7485251c783Sdholland 		/*
7495251c783Sdholland 		 * Address orphaned files.
7505251c783Sdholland 		 *
7515251c783Sdholland 		 * The idea of this is to free inodes belonging to
7525251c783Sdholland 		 * files that were unlinked but not reclaimed, I guess
7535251c783Sdholland 		 * because if we're going to scan the whole ifile
7545251c783Sdholland 		 * anyway it costs very little to do this. I don't
7555251c783Sdholland 		 * immediately see any reason this should be disabled,
7565251c783Sdholland 		 * but presumably it doesn't work... not sure what
7575251c783Sdholland 		 * happens to such files currently. -- dholland 20160806
7585251c783Sdholland 		 */
759*9bf46809Sriastradh 		if (lfs_if_getnextfree(fs, ifp) == LFS_ORPHAN_NEXTFREE(fs)) {
760*9bf46809Sriastradh 			if (orphan == NULL) {
761*9bf46809Sriastradh 				norphan_alloc = 32; /* XXX pulled from arse */
762*9bf46809Sriastradh 				orphan = kmem_zalloc(sizeof(orphan[0]) *
763*9bf46809Sriastradh 				    norphan_alloc, KM_SLEEP);
764*9bf46809Sriastradh 			} else if (norphan == norphan_alloc) {
765*9bf46809Sriastradh 				ino_t *orphan_new;
766*9bf46809Sriastradh 				if (norphan_alloc >= 4096)
767*9bf46809Sriastradh 					norphan_alloc += 4096;
768*9bf46809Sriastradh 				else
769*9bf46809Sriastradh 					norphan_alloc *= 2;
770*9bf46809Sriastradh 				orphan_new = kmem_zalloc(sizeof(orphan[0]) *
771*9bf46809Sriastradh 				    norphan_alloc, KM_SLEEP);
772*9bf46809Sriastradh 				memcpy(orphan_new, orphan, sizeof(orphan[0]) *
773*9bf46809Sriastradh 				    norphan);
774*9bf46809Sriastradh 				kmem_free(orphan, sizeof(orphan[0]) * norphan);
775*9bf46809Sriastradh 				orphan = orphan_new;
776437e8552Sperseant 			}
777*9bf46809Sriastradh 			orphan[norphan++] = ino;
778*9bf46809Sriastradh 		}
779437e8552Sperseant 
78032577c4fSdholland 		if (lfs_if_getdaddr(fs, ifp) == LFS_UNUSED_DADDR) {
7815251c783Sdholland 
7825251c783Sdholland 			/*
7835251c783Sdholland 			 * This inode is free. Put it on the free list.
7845251c783Sdholland 			 */
7855251c783Sdholland 
7865251c783Sdholland 			if (firstino == LFS_UNUSED_INUM) {
7875251c783Sdholland 				/* XXX: assert lastino == LFS_UNUSED_INUM? */
7885251c783Sdholland 				/* remember the first free inode */
789ff84dd34Sperseant 				firstino = ino;
7905251c783Sdholland 			} else {
7915251c783Sdholland 				/* release this inode's ifile entry */
7925c3b2b3fSad 				brelse(bp, 0);
793ff84dd34Sperseant 
7945251c783Sdholland 				/* XXX: assert lastino != LFS_UNUSED_INUM? */
7955251c783Sdholland 
7965251c783Sdholland 				/* load lastino's ifile entry */
797ff84dd34Sperseant 				LFS_IENTRY(ifp, fs, lastino, bp);
7985251c783Sdholland 				/* set the list pointer */
79932577c4fSdholland 				lfs_if_setnextfree(fs, ifp, ino);
8005251c783Sdholland 				/* write the block */
801ff84dd34Sperseant 				LFS_BWRITE_LOG(bp);
802ff84dd34Sperseant 
8035251c783Sdholland 				/* reload this inode's ifile entry */
804ff84dd34Sperseant 				LFS_IENTRY(ifp, fs, ino, bp);
805ff84dd34Sperseant 			}
8065251c783Sdholland 			/* remember the last free inode seen so far */
807ff84dd34Sperseant 			lastino = ino;
808ff84dd34Sperseant 
8095251c783Sdholland 			/* Mark this inode free in the in-memory freemap */
810ff84dd34Sperseant 			SET_BITMAP_FREE(fs, ino);
811ff84dd34Sperseant 		}
812ff84dd34Sperseant 
8135251c783Sdholland 		/* If moving to the next ifile block, release the buffer. */
814f59b8f4bSdholland 		if ((ino + 1) % lfs_sb_getifpb(fs) == 0)
8155c3b2b3fSad 			brelse(bp, 0);
816ff84dd34Sperseant 	}
817ff84dd34Sperseant 
8185251c783Sdholland 	/* Write the freelist head and tail pointers */
8195251c783Sdholland 	/* XXX: do we need to mark the superblock dirty? */
820ff84dd34Sperseant 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
821ff84dd34Sperseant 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
822a2aa7212Sperseant 
8235251c783Sdholland 	/* done */
824a2aa7212Sperseant 	lfs_segunlock(fs);
825*9bf46809Sriastradh 
826*9bf46809Sriastradh 	/*
827*9bf46809Sriastradh 	 * Shrink the array of orphans so we don't have to carry around
828*9bf46809Sriastradh 	 * the allocation size.
829*9bf46809Sriastradh 	 */
830*9bf46809Sriastradh 	if (norphan < norphan_alloc) {
831*9bf46809Sriastradh 		ino_t *orphan_new = kmem_alloc(sizeof(orphan[0]) * norphan,
832*9bf46809Sriastradh 		    KM_SLEEP);
833*9bf46809Sriastradh 		memcpy(orphan_new, orphan, sizeof(orphan[0]) * norphan);
834*9bf46809Sriastradh 		kmem_free(orphan, sizeof(orphan[0]) * norphan_alloc);
835*9bf46809Sriastradh 		orphan = orphan_new;
836*9bf46809Sriastradh 		norphan_alloc = norphan;
837*9bf46809Sriastradh 	}
838*9bf46809Sriastradh 
839*9bf46809Sriastradh 	*orphanp = orphan;
840*9bf46809Sriastradh 	*norphanp = norphan;
841ff84dd34Sperseant }
842437e8552Sperseant 
8435251c783Sdholland /*
8445251c783Sdholland  * Mark a file orphaned (unlinked but not yet reclaimed) by inode
8455251c783Sdholland  * number. Do this with a magic freelist next pointer.
8465251c783Sdholland  *
8475251c783Sdholland  * XXX: howzabout some locking?
8485251c783Sdholland  */
849437e8552Sperseant void
lfs_orphan(struct lfs * fs,ino_t ino)850437e8552Sperseant lfs_orphan(struct lfs *fs, ino_t ino)
851437e8552Sperseant {
852437e8552Sperseant 	IFILE *ifp;
853437e8552Sperseant 	struct buf *bp;
854437e8552Sperseant 
855437e8552Sperseant 	LFS_IENTRY(ifp, fs, ino, bp);
856e70bb021Sriastradh 	lfs_if_setnextfree(fs, ifp, LFS_ORPHAN_NEXTFREE(fs));
857437e8552Sperseant 	LFS_BWRITE_LOG(bp);
858437e8552Sperseant }
859*9bf46809Sriastradh 
860*9bf46809Sriastradh /*
861*9bf46809Sriastradh  * Free orphans discovered during mount.  This is a separate stage
862*9bf46809Sriastradh  * because it requires fs->lfs_suflags to be set up, which is not done
863*9bf46809Sriastradh  * by the time we run lfs_order_freelist.  It's possible that we could
864*9bf46809Sriastradh  * run lfs_order_freelist later (i.e., set up fs->lfs_suflags sooner)
865*9bf46809Sriastradh  * but that requires more thought than I can put into this at the
866*9bf46809Sriastradh  * moment.
867*9bf46809Sriastradh  */
868*9bf46809Sriastradh void
lfs_free_orphans(struct lfs * fs,ino_t * orphan,size_t norphan)869*9bf46809Sriastradh lfs_free_orphans(struct lfs *fs, ino_t *orphan, size_t norphan)
870*9bf46809Sriastradh {
871*9bf46809Sriastradh 	size_t i;
872*9bf46809Sriastradh 
873*9bf46809Sriastradh 	for (i = 0; i < norphan; i++) {
874*9bf46809Sriastradh 		ino_t ino = orphan[i];
875*9bf46809Sriastradh 		unsigned segno;
876*9bf46809Sriastradh 		struct vnode *vp;
877*9bf46809Sriastradh 		struct inode *ip;
878*9bf46809Sriastradh 		struct buf *bp;
879*9bf46809Sriastradh 		IFILE *ifp;
880*9bf46809Sriastradh 		SEGUSE *sup;
881*9bf46809Sriastradh 		int error;
882*9bf46809Sriastradh 
883*9bf46809Sriastradh 		/* Get the segment the inode is in on disk.  */
884*9bf46809Sriastradh 		LFS_IENTRY(ifp, fs, ino, bp);
885*9bf46809Sriastradh 		segno = lfs_dtosn(fs, lfs_if_getdaddr(fs, ifp));
886*9bf46809Sriastradh 		brelse(bp, 0);
887*9bf46809Sriastradh 
888*9bf46809Sriastradh 		/*
889*9bf46809Sriastradh 		 * Try to get the vnode.  If we can't, tough -- hope
890*9bf46809Sriastradh 		 * you have backups!
891*9bf46809Sriastradh 		 */
892*9bf46809Sriastradh 		error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, LK_EXCLUSIVE,
893*9bf46809Sriastradh 		    &vp);
894*9bf46809Sriastradh 		if (error) {
895*9bf46809Sriastradh 			printf("orphan %jd vget error %d\n", (intmax_t)ino,
896*9bf46809Sriastradh 			    error);
897*9bf46809Sriastradh 			continue;
898*9bf46809Sriastradh 		}
899*9bf46809Sriastradh 
900*9bf46809Sriastradh 		/*
901*9bf46809Sriastradh 		 * Sanity-check the inode.
902*9bf46809Sriastradh 		 *
903*9bf46809Sriastradh 		 * XXX What to do if it is still referenced?
904*9bf46809Sriastradh 		 */
905*9bf46809Sriastradh 		ip = VTOI(vp);
906*9bf46809Sriastradh 		if (ip->i_nlink != 0)
907*9bf46809Sriastradh 			printf("orphan %jd nlink %d\n", (intmax_t)ino,
908*9bf46809Sriastradh 			    ip->i_nlink);
909*9bf46809Sriastradh 
910*9bf46809Sriastradh 		/*
911*9bf46809Sriastradh 		 * Truncate the inode, to free any blocks allocated for
912*9bf46809Sriastradh 		 * it, and release it, to free the inode number.
913*9bf46809Sriastradh 		 *
914*9bf46809Sriastradh 		 * XXX Isn't it redundant to truncate?  Won't vput do
915*9bf46809Sriastradh 		 * that for us?
916*9bf46809Sriastradh 		 */
917*9bf46809Sriastradh 		error = lfs_truncate(vp, 0, 0, NOCRED);
918*9bf46809Sriastradh 		if (error)
919*9bf46809Sriastradh 			printf("orphan %jd truncate error %d", (intmax_t)ino,
920*9bf46809Sriastradh 			    error);
921*9bf46809Sriastradh 		vput(vp);
922*9bf46809Sriastradh 
923*9bf46809Sriastradh 		/* Update the number of bytes in the segment summary.  */
924*9bf46809Sriastradh 		LFS_SEGENTRY(sup, fs, segno, bp);
925*9bf46809Sriastradh 		KASSERT(sup->su_nbytes >= DINOSIZE(fs));
926*9bf46809Sriastradh 		sup->su_nbytes -= DINOSIZE(fs);
927*9bf46809Sriastradh 		LFS_WRITESEGENTRY(sup, fs, segno, bp);
928*9bf46809Sriastradh 
929*9bf46809Sriastradh 		/* Drop the on-disk address.  */
930*9bf46809Sriastradh 		LFS_IENTRY(ifp, fs, ino, bp);
931*9bf46809Sriastradh 		lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
932*9bf46809Sriastradh 		LFS_BWRITE_LOG(bp);
933*9bf46809Sriastradh 	}
934*9bf46809Sriastradh 
935*9bf46809Sriastradh 	if (orphan)
936*9bf46809Sriastradh 		kmem_free(orphan, sizeof(orphan[0]) * norphan);
937*9bf46809Sriastradh }
938