xref: /minix3/sys/ufs/ext2fs/ext2fs_alloc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ext2fs_alloc.c,v 1.46 2015/03/28 19:24:04 maxv Exp $	*/
2d65f6f70SBen Gras 
3d65f6f70SBen Gras /*
4d65f6f70SBen Gras  * Copyright (c) 1982, 1986, 1989, 1993
5d65f6f70SBen Gras  *	The Regents of the University of California.  All rights reserved.
6d65f6f70SBen Gras  *
7d65f6f70SBen Gras  * Redistribution and use in source and binary forms, with or without
8d65f6f70SBen Gras  * modification, are permitted provided that the following conditions
9d65f6f70SBen Gras  * are met:
10d65f6f70SBen Gras  * 1. Redistributions of source code must retain the above copyright
11d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer.
12d65f6f70SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer in the
14d65f6f70SBen Gras  *    documentation and/or other materials provided with the distribution.
15d65f6f70SBen Gras  * 3. Neither the name of the University nor the names of its contributors
16d65f6f70SBen Gras  *    may be used to endorse or promote products derived from this software
17d65f6f70SBen Gras  *    without specific prior written permission.
18d65f6f70SBen Gras  *
19d65f6f70SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20d65f6f70SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d65f6f70SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d65f6f70SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23d65f6f70SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d65f6f70SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d65f6f70SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d65f6f70SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d65f6f70SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d65f6f70SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d65f6f70SBen Gras  * SUCH DAMAGE.
30d65f6f70SBen Gras  *
31d65f6f70SBen Gras  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
32d65f6f70SBen Gras  *  Modified for ext2fs by Manuel Bouyer.
33d65f6f70SBen Gras  */
34d65f6f70SBen Gras 
35d65f6f70SBen Gras /*
36d65f6f70SBen Gras  * Copyright (c) 1997 Manuel Bouyer.
37d65f6f70SBen Gras  *
38d65f6f70SBen Gras  * Redistribution and use in source and binary forms, with or without
39d65f6f70SBen Gras  * modification, are permitted provided that the following conditions
40d65f6f70SBen Gras  * are met:
41d65f6f70SBen Gras  * 1. Redistributions of source code must retain the above copyright
42d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer.
43d65f6f70SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
44d65f6f70SBen Gras  *    notice, this list of conditions and the following disclaimer in the
45d65f6f70SBen Gras  *    documentation and/or other materials provided with the distribution.
46d65f6f70SBen Gras  *
47d65f6f70SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48d65f6f70SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49d65f6f70SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50d65f6f70SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51d65f6f70SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52d65f6f70SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53d65f6f70SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54d65f6f70SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55d65f6f70SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56d65f6f70SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57d65f6f70SBen Gras  *
58d65f6f70SBen Gras  *	@(#)ffs_alloc.c	8.11 (Berkeley) 10/27/94
59d65f6f70SBen Gras  *  Modified for ext2fs by Manuel Bouyer.
60d65f6f70SBen Gras  */
61d65f6f70SBen Gras 
62d65f6f70SBen Gras #include <sys/cdefs.h>
63*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ext2fs_alloc.c,v 1.46 2015/03/28 19:24:04 maxv Exp $");
64d65f6f70SBen Gras 
65d65f6f70SBen Gras #include <sys/param.h>
66d65f6f70SBen Gras #include <sys/systm.h>
67d65f6f70SBen Gras #include <sys/buf.h>
68d65f6f70SBen Gras #include <sys/proc.h>
69d65f6f70SBen Gras #include <sys/vnode.h>
70d65f6f70SBen Gras #include <sys/mount.h>
71d65f6f70SBen Gras #include <sys/kernel.h>
72d65f6f70SBen Gras #include <sys/syslog.h>
73d65f6f70SBen Gras #include <sys/kauth.h>
74d65f6f70SBen Gras 
75d65f6f70SBen Gras #include <ufs/ufs/inode.h>
76d65f6f70SBen Gras #include <ufs/ufs/ufs_extern.h>
77d65f6f70SBen Gras #include <ufs/ufs/ufsmount.h>
78d65f6f70SBen Gras 
79d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs.h>
80d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs_extern.h>
81d65f6f70SBen Gras 
82d65f6f70SBen Gras u_long ext2gennumber;
83d65f6f70SBen Gras 
84d65f6f70SBen Gras static daddr_t	ext2fs_alloccg(struct inode *, int, daddr_t, int);
85d65f6f70SBen Gras static u_long	ext2fs_dirpref(struct m_ext2fs *);
86d65f6f70SBen Gras static void	ext2fs_fserr(struct m_ext2fs *, u_int, const char *);
87d65f6f70SBen Gras static u_long	ext2fs_hashalloc(struct inode *, int, long, int,
88d65f6f70SBen Gras 		    daddr_t (*)(struct inode *, int, daddr_t, int));
89d65f6f70SBen Gras static daddr_t	ext2fs_nodealloccg(struct inode *, int, daddr_t, int);
90d65f6f70SBen Gras static daddr_t	ext2fs_mapsearch(struct m_ext2fs *, char *, daddr_t);
91d65f6f70SBen Gras 
92d65f6f70SBen Gras /*
93d65f6f70SBen Gras  * Allocate a block in the file system.
94d65f6f70SBen Gras  *
95d65f6f70SBen Gras  * A preference may be optionally specified. If a preference is given
96d65f6f70SBen Gras  * the following hierarchy is used to allocate a block:
97d65f6f70SBen Gras  *   1) allocate the requested block.
98d65f6f70SBen Gras  *   2) allocate a rotationally optimal block in the same cylinder.
99d65f6f70SBen Gras  *   3) allocate a block in the same cylinder group.
100d65f6f70SBen Gras  *   4) quadradically rehash into other cylinder groups, until an
101d65f6f70SBen Gras  *	  available block is located.
102d65f6f70SBen Gras  * If no block preference is given the following hierarchy is used
103d65f6f70SBen Gras  * to allocate a block:
104d65f6f70SBen Gras  *   1) allocate a block in the cylinder group that contains the
105d65f6f70SBen Gras  *	  inode for the file.
106d65f6f70SBen Gras  *   2) quadradically rehash into other cylinder groups, until an
107d65f6f70SBen Gras  *	  available block is located.
108d65f6f70SBen Gras  */
109d65f6f70SBen Gras int
ext2fs_alloc(struct inode * ip,daddr_t lbn,daddr_t bpref,kauth_cred_t cred,daddr_t * bnp)110d65f6f70SBen Gras ext2fs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref,
111d65f6f70SBen Gras     kauth_cred_t cred, daddr_t *bnp)
112d65f6f70SBen Gras {
113d65f6f70SBen Gras 	struct m_ext2fs *fs;
114d65f6f70SBen Gras 	daddr_t bno;
115d65f6f70SBen Gras 	int cg;
116d65f6f70SBen Gras 
117d65f6f70SBen Gras 	*bnp = 0;
118d65f6f70SBen Gras 	fs = ip->i_e2fs;
119d65f6f70SBen Gras #ifdef DIAGNOSTIC
120d65f6f70SBen Gras 	if (cred == NOCRED)
121d65f6f70SBen Gras 		panic("ext2fs_alloc: missing credential");
122d65f6f70SBen Gras #endif /* DIAGNOSTIC */
123d65f6f70SBen Gras 	if (fs->e2fs.e2fs_fbcount == 0)
124d65f6f70SBen Gras 		goto nospace;
125d65f6f70SBen Gras 	if (kauth_authorize_system(cred, KAUTH_SYSTEM_FS_RESERVEDSPACE, 0, NULL,
126d65f6f70SBen Gras 	    NULL, NULL) != 0 &&
127d65f6f70SBen Gras 	    freespace(fs) <= 0)
128d65f6f70SBen Gras 		goto nospace;
129d65f6f70SBen Gras 	if (bpref >= fs->e2fs.e2fs_bcount)
130d65f6f70SBen Gras 		bpref = 0;
131d65f6f70SBen Gras 	if (bpref == 0)
132d65f6f70SBen Gras 		cg = ino_to_cg(fs, ip->i_number);
133d65f6f70SBen Gras 	else
134d65f6f70SBen Gras 		cg = dtog(fs, bpref);
135d65f6f70SBen Gras 	bno = (daddr_t)ext2fs_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
136d65f6f70SBen Gras 	    ext2fs_alloccg);
137d65f6f70SBen Gras 	if (bno > 0) {
13884d9c625SLionel Sambuc 		ext2fs_setnblock(ip, ext2fs_nblock(ip) + btodb(fs->e2fs_bsize));
139d65f6f70SBen Gras 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
140d65f6f70SBen Gras 		*bnp = bno;
141d65f6f70SBen Gras 		return (0);
142d65f6f70SBen Gras 	}
143d65f6f70SBen Gras nospace:
144d65f6f70SBen Gras 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
145d65f6f70SBen Gras 	uprintf("\n%s: write failed, file system is full\n", fs->e2fs_fsmnt);
146d65f6f70SBen Gras 	return (ENOSPC);
147d65f6f70SBen Gras }
148d65f6f70SBen Gras 
149d65f6f70SBen Gras /*
150d65f6f70SBen Gras  * Allocate an inode in the file system.
151d65f6f70SBen Gras  *
152d65f6f70SBen Gras  * If allocating a directory, use ext2fs_dirpref to select the inode.
153d65f6f70SBen Gras  * If allocating in a directory, the following hierarchy is followed:
154d65f6f70SBen Gras  *   1) allocate the preferred inode.
155d65f6f70SBen Gras  *   2) allocate an inode in the same cylinder group.
156d65f6f70SBen Gras  *   3) quadradically rehash into other cylinder groups, until an
157d65f6f70SBen Gras  *	  available inode is located.
158d65f6f70SBen Gras  * If no inode preference is given the following hierarchy is used
159d65f6f70SBen Gras  * to allocate an inode:
160d65f6f70SBen Gras  *   1) allocate an inode in cylinder group 0.
161d65f6f70SBen Gras  *   2) quadradically rehash into other cylinder groups, until an
162d65f6f70SBen Gras  *	  available inode is located.
163d65f6f70SBen Gras  */
164d65f6f70SBen Gras int
ext2fs_valloc(struct vnode * pvp,int mode,kauth_cred_t cred,struct vnode ** vpp)165d65f6f70SBen Gras ext2fs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
166d65f6f70SBen Gras     struct vnode **vpp)
167d65f6f70SBen Gras {
168d65f6f70SBen Gras 	struct inode *pip;
169d65f6f70SBen Gras 	struct m_ext2fs *fs;
170d65f6f70SBen Gras 	struct inode *ip;
171d65f6f70SBen Gras 	ino_t ino, ipref;
172d65f6f70SBen Gras 	int cg, error;
173d65f6f70SBen Gras 
174d65f6f70SBen Gras 	*vpp = NULL;
175d65f6f70SBen Gras 	pip = VTOI(pvp);
176d65f6f70SBen Gras 	fs = pip->i_e2fs;
177d65f6f70SBen Gras 	if (fs->e2fs.e2fs_ficount == 0)
178d65f6f70SBen Gras 		goto noinodes;
179d65f6f70SBen Gras 
180d65f6f70SBen Gras 	if ((mode & IFMT) == IFDIR)
181d65f6f70SBen Gras 		cg = ext2fs_dirpref(fs);
182d65f6f70SBen Gras 	else
183d65f6f70SBen Gras 		cg = ino_to_cg(fs, pip->i_number);
184d65f6f70SBen Gras 	ipref = cg * fs->e2fs.e2fs_ipg + 1;
185d65f6f70SBen Gras 	ino = (ino_t)ext2fs_hashalloc(pip, cg, (long)ipref, mode, ext2fs_nodealloccg);
186d65f6f70SBen Gras 	if (ino == 0)
187d65f6f70SBen Gras 		goto noinodes;
188d65f6f70SBen Gras 	error = VFS_VGET(pvp->v_mount, ino, vpp);
189d65f6f70SBen Gras 	if (error) {
190d65f6f70SBen Gras 		ext2fs_vfree(pvp, ino, mode);
191d65f6f70SBen Gras 		return (error);
192d65f6f70SBen Gras 	}
193d65f6f70SBen Gras 	ip = VTOI(*vpp);
194d65f6f70SBen Gras 	if (ip->i_e2fs_mode && ip->i_e2fs_nlink != 0) {
195d65f6f70SBen Gras 		printf("mode = 0%o, nlinks %d, inum = %llu, fs = %s\n",
196d65f6f70SBen Gras 		    ip->i_e2fs_mode, ip->i_e2fs_nlink,
197d65f6f70SBen Gras 		    (unsigned long long)ip->i_number, fs->e2fs_fsmnt);
198d65f6f70SBen Gras 		panic("ext2fs_valloc: dup alloc");
199d65f6f70SBen Gras 	}
200d65f6f70SBen Gras 
201d65f6f70SBen Gras 	memset(ip->i_din.e2fs_din, 0, sizeof(struct ext2fs_dinode));
202d65f6f70SBen Gras 
203d65f6f70SBen Gras 	/*
204d65f6f70SBen Gras 	 * Set up a new generation number for this inode.
205d65f6f70SBen Gras 	 */
206d65f6f70SBen Gras 	if (++ext2gennumber < time_second)
207d65f6f70SBen Gras 		ext2gennumber = time_second;
208d65f6f70SBen Gras 	ip->i_e2fs_gen = ext2gennumber;
209d65f6f70SBen Gras 	return (0);
210d65f6f70SBen Gras noinodes:
211d65f6f70SBen Gras 	ext2fs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes");
212d65f6f70SBen Gras 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
213d65f6f70SBen Gras 	return (ENOSPC);
214d65f6f70SBen Gras }
215d65f6f70SBen Gras 
216d65f6f70SBen Gras /*
217d65f6f70SBen Gras  * Find a cylinder to place a directory.
218d65f6f70SBen Gras  *
219d65f6f70SBen Gras  * The policy implemented by this algorithm is to select from
220d65f6f70SBen Gras  * among those cylinder groups with above the average number of
221d65f6f70SBen Gras  * free inodes, the one with the smallest number of directories.
222d65f6f70SBen Gras  */
223d65f6f70SBen Gras static u_long
ext2fs_dirpref(struct m_ext2fs * fs)224d65f6f70SBen Gras ext2fs_dirpref(struct m_ext2fs *fs)
225d65f6f70SBen Gras {
226d65f6f70SBen Gras 	int cg, maxspace, mincg, avgifree;
227d65f6f70SBen Gras 
228d65f6f70SBen Gras 	avgifree = fs->e2fs.e2fs_ficount / fs->e2fs_ncg;
229d65f6f70SBen Gras 	maxspace = 0;
230d65f6f70SBen Gras 	mincg = -1;
231d65f6f70SBen Gras 	for (cg = 0; cg < fs->e2fs_ncg; cg++)
232d65f6f70SBen Gras 		if ( fs->e2fs_gd[cg].ext2bgd_nifree >= avgifree) {
233d65f6f70SBen Gras 			if (mincg == -1 || fs->e2fs_gd[cg].ext2bgd_nbfree > maxspace) {
234d65f6f70SBen Gras 				mincg = cg;
235d65f6f70SBen Gras 				maxspace = fs->e2fs_gd[cg].ext2bgd_nbfree;
236d65f6f70SBen Gras 			}
237d65f6f70SBen Gras 		}
238d65f6f70SBen Gras 	return mincg;
239d65f6f70SBen Gras }
240d65f6f70SBen Gras 
241d65f6f70SBen Gras /*
242d65f6f70SBen Gras  * Select the desired position for the next block in a file.  The file is
243d65f6f70SBen Gras  * logically divided into sections. The first section is composed of the
244d65f6f70SBen Gras  * direct blocks. Each additional section contains fs_maxbpg blocks.
245d65f6f70SBen Gras  *
246d65f6f70SBen Gras  * If no blocks have been allocated in the first section, the policy is to
247d65f6f70SBen Gras  * request a block in the same cylinder group as the inode that describes
248d65f6f70SBen Gras  * the file. Otherwise, the policy is to try to allocate the blocks
249d65f6f70SBen Gras  * contigously. The two fields of the ext2 inode extension (see
250d65f6f70SBen Gras  * ufs/ufs/inode.h) help this.
251d65f6f70SBen Gras  */
252d65f6f70SBen Gras daddr_t
ext2fs_blkpref(struct inode * ip,daddr_t lbn,int indx,int32_t * bap)253d65f6f70SBen Gras ext2fs_blkpref(struct inode *ip, daddr_t lbn, int indx,
254d65f6f70SBen Gras 		int32_t *bap /* XXX ondisk32 */)
255d65f6f70SBen Gras {
256d65f6f70SBen Gras 	struct m_ext2fs *fs;
257d65f6f70SBen Gras 	int cg, i;
258d65f6f70SBen Gras 
259d65f6f70SBen Gras 	fs = ip->i_e2fs;
260d65f6f70SBen Gras 	/*
261d65f6f70SBen Gras 	 * if we are doing contigous lbn allocation, try to alloc blocks
262d65f6f70SBen Gras 	 * contigously on disk
263d65f6f70SBen Gras 	 */
264d65f6f70SBen Gras 
265d65f6f70SBen Gras 	if ( ip->i_e2fs_last_blk && lbn == ip->i_e2fs_last_lblk + 1) {
266d65f6f70SBen Gras 		return ip->i_e2fs_last_blk + 1;
267d65f6f70SBen Gras 	}
268d65f6f70SBen Gras 
269d65f6f70SBen Gras 	/*
270d65f6f70SBen Gras 	 * bap, if provided, gives us a list of blocks to which we want to
271d65f6f70SBen Gras 	 * stay close
272d65f6f70SBen Gras 	 */
273d65f6f70SBen Gras 
274d65f6f70SBen Gras 	if (bap) {
275d65f6f70SBen Gras 		for (i = indx; i >= 0 ; i--) {
276d65f6f70SBen Gras 			if (bap[i]) {
277d65f6f70SBen Gras 				return fs2h32(bap[i]) + 1;
278d65f6f70SBen Gras 			}
279d65f6f70SBen Gras 		}
280d65f6f70SBen Gras 	}
281d65f6f70SBen Gras 
282d65f6f70SBen Gras 	/* fall back to the first block of the cylinder containing the inode */
283d65f6f70SBen Gras 
284d65f6f70SBen Gras 	cg = ino_to_cg(fs, ip->i_number);
285d65f6f70SBen Gras 	return fs->e2fs.e2fs_bpg * cg + fs->e2fs.e2fs_first_dblock + 1;
286d65f6f70SBen Gras }
287d65f6f70SBen Gras 
288d65f6f70SBen Gras /*
289d65f6f70SBen Gras  * Implement the cylinder overflow algorithm.
290d65f6f70SBen Gras  *
291d65f6f70SBen Gras  * The policy implemented by this algorithm is:
292d65f6f70SBen Gras  *   1) allocate the block in its requested cylinder group.
293d65f6f70SBen Gras  *   2) quadradically rehash on the cylinder group number.
294d65f6f70SBen Gras  *   3) brute force search for a free block.
295d65f6f70SBen Gras  */
296d65f6f70SBen Gras static u_long
ext2fs_hashalloc(struct inode * ip,int cg,long pref,int size,daddr_t (* allocator)(struct inode *,int,daddr_t,int))297d65f6f70SBen Gras ext2fs_hashalloc(struct inode *ip, int cg, long pref, int size,
298d65f6f70SBen Gras 		daddr_t (*allocator)(struct inode *, int, daddr_t, int))
299d65f6f70SBen Gras {
300d65f6f70SBen Gras 	struct m_ext2fs *fs;
301d65f6f70SBen Gras 	long result;
302d65f6f70SBen Gras 	int i, icg = cg;
303d65f6f70SBen Gras 
304d65f6f70SBen Gras 	fs = ip->i_e2fs;
305d65f6f70SBen Gras 	/*
306d65f6f70SBen Gras 	 * 1: preferred cylinder group
307d65f6f70SBen Gras 	 */
308d65f6f70SBen Gras 	result = (*allocator)(ip, cg, pref, size);
309d65f6f70SBen Gras 	if (result)
310d65f6f70SBen Gras 		return (result);
311d65f6f70SBen Gras 	/*
312d65f6f70SBen Gras 	 * 2: quadratic rehash
313d65f6f70SBen Gras 	 */
314d65f6f70SBen Gras 	for (i = 1; i < fs->e2fs_ncg; i *= 2) {
315d65f6f70SBen Gras 		cg += i;
316d65f6f70SBen Gras 		if (cg >= fs->e2fs_ncg)
317d65f6f70SBen Gras 			cg -= fs->e2fs_ncg;
318d65f6f70SBen Gras 		result = (*allocator)(ip, cg, 0, size);
319d65f6f70SBen Gras 		if (result)
320d65f6f70SBen Gras 			return (result);
321d65f6f70SBen Gras 	}
322d65f6f70SBen Gras 	/*
323d65f6f70SBen Gras 	 * 3: brute force search
324d65f6f70SBen Gras 	 * Note that we start at i == 2, since 0 was checked initially,
325d65f6f70SBen Gras 	 * and 1 is always checked in the quadratic rehash.
326d65f6f70SBen Gras 	 */
327d65f6f70SBen Gras 	cg = (icg + 2) % fs->e2fs_ncg;
328d65f6f70SBen Gras 	for (i = 2; i < fs->e2fs_ncg; i++) {
329d65f6f70SBen Gras 		result = (*allocator)(ip, cg, 0, size);
330d65f6f70SBen Gras 		if (result)
331d65f6f70SBen Gras 			return (result);
332d65f6f70SBen Gras 		cg++;
333d65f6f70SBen Gras 		if (cg == fs->e2fs_ncg)
334d65f6f70SBen Gras 			cg = 0;
335d65f6f70SBen Gras 	}
336d65f6f70SBen Gras 	return (0);
337d65f6f70SBen Gras }
338d65f6f70SBen Gras 
339d65f6f70SBen Gras /*
340d65f6f70SBen Gras  * Determine whether a block can be allocated.
341d65f6f70SBen Gras  *
342d65f6f70SBen Gras  * Check to see if a block of the appropriate size is available,
343d65f6f70SBen Gras  * and if it is, allocate it.
344d65f6f70SBen Gras  */
345d65f6f70SBen Gras 
346d65f6f70SBen Gras static daddr_t
ext2fs_alloccg(struct inode * ip,int cg,daddr_t bpref,int size)347d65f6f70SBen Gras ext2fs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
348d65f6f70SBen Gras {
349d65f6f70SBen Gras 	struct m_ext2fs *fs;
350d65f6f70SBen Gras 	char *bbp;
351d65f6f70SBen Gras 	struct buf *bp;
352d65f6f70SBen Gras 	/* XXX ondisk32 */
353d65f6f70SBen Gras 	int error, bno, start, end, loc;
354d65f6f70SBen Gras 
355d65f6f70SBen Gras 	fs = ip->i_e2fs;
356d65f6f70SBen Gras 	if (fs->e2fs_gd[cg].ext2bgd_nbfree == 0)
357d65f6f70SBen Gras 		return (0);
35884d9c625SLionel Sambuc 	error = bread(ip->i_devvp, EXT2_FSBTODB(fs,
359d65f6f70SBen Gras 		fs->e2fs_gd[cg].ext2bgd_b_bitmap),
360*0a6a1f1dSLionel Sambuc 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
361d65f6f70SBen Gras 	if (error) {
362d65f6f70SBen Gras 		return (0);
363d65f6f70SBen Gras 	}
364d65f6f70SBen Gras 	bbp = (char *)bp->b_data;
365d65f6f70SBen Gras 
366d65f6f70SBen Gras 	if (dtog(fs, bpref) != cg)
367d65f6f70SBen Gras 		bpref = 0;
368d65f6f70SBen Gras 	if (bpref != 0) {
369d65f6f70SBen Gras 		bpref = dtogd(fs, bpref);
370d65f6f70SBen Gras 		/*
371d65f6f70SBen Gras 		 * if the requested block is available, use it
372d65f6f70SBen Gras 		 */
373d65f6f70SBen Gras 		if (isclr(bbp, bpref)) {
374d65f6f70SBen Gras 			bno = bpref;
375d65f6f70SBen Gras 			goto gotit;
376d65f6f70SBen Gras 		}
377d65f6f70SBen Gras 	}
378d65f6f70SBen Gras 	/*
379d65f6f70SBen Gras 	 * no blocks in the requested cylinder, so take next
380d65f6f70SBen Gras 	 * available one in this cylinder group.
381d65f6f70SBen Gras 	 * first try to get 8 contigous blocks, then fall back to a single
382d65f6f70SBen Gras 	 * block.
383d65f6f70SBen Gras 	 */
384d65f6f70SBen Gras 	if (bpref)
385d65f6f70SBen Gras 		start = dtogd(fs, bpref) / NBBY;
386d65f6f70SBen Gras 	else
387d65f6f70SBen Gras 		start = 0;
388d65f6f70SBen Gras 	end = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
389d65f6f70SBen Gras 	for (loc = start; loc < end; loc++) {
390d65f6f70SBen Gras 		if (bbp[loc] == 0) {
391d65f6f70SBen Gras 			bno = loc * NBBY;
392d65f6f70SBen Gras 			goto gotit;
393d65f6f70SBen Gras 		}
394d65f6f70SBen Gras 	}
395d65f6f70SBen Gras 	for (loc = 0; loc < start; loc++) {
396d65f6f70SBen Gras 		if (bbp[loc] == 0) {
397d65f6f70SBen Gras 			bno = loc * NBBY;
398d65f6f70SBen Gras 			goto gotit;
399d65f6f70SBen Gras 		}
400d65f6f70SBen Gras 	}
401d65f6f70SBen Gras 
402d65f6f70SBen Gras 	bno = ext2fs_mapsearch(fs, bbp, bpref);
403d65f6f70SBen Gras 	if (bno < 0)
404d65f6f70SBen Gras 		return (0);
405d65f6f70SBen Gras gotit:
406d65f6f70SBen Gras #ifdef DIAGNOSTIC
407d65f6f70SBen Gras 	if (isset(bbp, (daddr_t)bno)) {
408d65f6f70SBen Gras 		printf("ext2fs_alloccgblk: cg=%d bno=%d fs=%s\n",
409d65f6f70SBen Gras 			cg, bno, fs->e2fs_fsmnt);
410d65f6f70SBen Gras 		panic("ext2fs_alloccg: dup alloc");
411d65f6f70SBen Gras 	}
412d65f6f70SBen Gras #endif
413d65f6f70SBen Gras 	setbit(bbp, (daddr_t)bno);
414d65f6f70SBen Gras 	fs->e2fs.e2fs_fbcount--;
415d65f6f70SBen Gras 	fs->e2fs_gd[cg].ext2bgd_nbfree--;
416d65f6f70SBen Gras 	fs->e2fs_fmod = 1;
417d65f6f70SBen Gras 	bdwrite(bp);
418d65f6f70SBen Gras 	return (cg * fs->e2fs.e2fs_fpg + fs->e2fs.e2fs_first_dblock + bno);
419d65f6f70SBen Gras }
420d65f6f70SBen Gras 
421d65f6f70SBen Gras /*
422d65f6f70SBen Gras  * Determine whether an inode can be allocated.
423d65f6f70SBen Gras  *
424d65f6f70SBen Gras  * Check to see if an inode is available, and if it is,
425d65f6f70SBen Gras  * allocate it using the following policy:
426d65f6f70SBen Gras  *   1) allocate the requested inode.
427d65f6f70SBen Gras  *   2) allocate the next available inode after the requested
428d65f6f70SBen Gras  *	  inode in the specified cylinder group.
429d65f6f70SBen Gras  */
430d65f6f70SBen Gras static daddr_t
ext2fs_nodealloccg(struct inode * ip,int cg,daddr_t ipref,int mode)431d65f6f70SBen Gras ext2fs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
432d65f6f70SBen Gras {
433d65f6f70SBen Gras 	struct m_ext2fs *fs;
434d65f6f70SBen Gras 	char *ibp;
435d65f6f70SBen Gras 	struct buf *bp;
436d65f6f70SBen Gras 	int error, start, len, loc, map, i;
437d65f6f70SBen Gras 
438d65f6f70SBen Gras 	ipref--; /* to avoid a lot of (ipref -1) */
439d65f6f70SBen Gras 	if (ipref == -1)
440d65f6f70SBen Gras 		ipref = 0;
441d65f6f70SBen Gras 	fs = ip->i_e2fs;
442d65f6f70SBen Gras 	if (fs->e2fs_gd[cg].ext2bgd_nifree == 0)
443d65f6f70SBen Gras 		return (0);
44484d9c625SLionel Sambuc 	error = bread(ip->i_devvp, EXT2_FSBTODB(fs,
445d65f6f70SBen Gras 		fs->e2fs_gd[cg].ext2bgd_i_bitmap),
446*0a6a1f1dSLionel Sambuc 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
447d65f6f70SBen Gras 	if (error) {
448d65f6f70SBen Gras 		return (0);
449d65f6f70SBen Gras 	}
450d65f6f70SBen Gras 	ibp = (char *)bp->b_data;
451d65f6f70SBen Gras 	if (ipref) {
452d65f6f70SBen Gras 		ipref %= fs->e2fs.e2fs_ipg;
453d65f6f70SBen Gras 		if (isclr(ibp, ipref))
454d65f6f70SBen Gras 			goto gotit;
455d65f6f70SBen Gras 	}
456d65f6f70SBen Gras 	start = ipref / NBBY;
457d65f6f70SBen Gras 	len = howmany(fs->e2fs.e2fs_ipg - ipref, NBBY);
458d65f6f70SBen Gras 	loc = skpc(0xff, len, &ibp[start]);
459d65f6f70SBen Gras 	if (loc == 0) {
460d65f6f70SBen Gras 		len = start + 1;
461d65f6f70SBen Gras 		start = 0;
462d65f6f70SBen Gras 		loc = skpc(0xff, len, &ibp[0]);
463d65f6f70SBen Gras 		if (loc == 0) {
464d65f6f70SBen Gras 			printf("cg = %d, ipref = %lld, fs = %s\n",
465d65f6f70SBen Gras 				cg, (long long)ipref, fs->e2fs_fsmnt);
466d65f6f70SBen Gras 			panic("ext2fs_nodealloccg: map corrupted");
467d65f6f70SBen Gras 			/* NOTREACHED */
468d65f6f70SBen Gras 		}
469d65f6f70SBen Gras 	}
470d65f6f70SBen Gras 	i = start + len - loc;
471d65f6f70SBen Gras 	map = ibp[i] ^ 0xff;
472d65f6f70SBen Gras 	if (map == 0) {
473d65f6f70SBen Gras 		printf("fs = %s\n", fs->e2fs_fsmnt);
474d65f6f70SBen Gras 		panic("ext2fs_nodealloccg: block not in map");
475d65f6f70SBen Gras 	}
476d65f6f70SBen Gras 	ipref = i * NBBY + ffs(map) - 1;
477d65f6f70SBen Gras gotit:
478d65f6f70SBen Gras 	setbit(ibp, ipref);
479d65f6f70SBen Gras 	fs->e2fs.e2fs_ficount--;
480d65f6f70SBen Gras 	fs->e2fs_gd[cg].ext2bgd_nifree--;
481d65f6f70SBen Gras 	fs->e2fs_fmod = 1;
482d65f6f70SBen Gras 	if ((mode & IFMT) == IFDIR) {
483d65f6f70SBen Gras 		fs->e2fs_gd[cg].ext2bgd_ndirs++;
484d65f6f70SBen Gras 	}
485d65f6f70SBen Gras 	bdwrite(bp);
486d65f6f70SBen Gras 	return (cg * fs->e2fs.e2fs_ipg + ipref +1);
487d65f6f70SBen Gras }
488d65f6f70SBen Gras 
489d65f6f70SBen Gras /*
490d65f6f70SBen Gras  * Free a block.
491d65f6f70SBen Gras  *
492d65f6f70SBen Gras  * The specified block is placed back in the
493d65f6f70SBen Gras  * free map.
494d65f6f70SBen Gras  */
495d65f6f70SBen Gras void
ext2fs_blkfree(struct inode * ip,daddr_t bno)496d65f6f70SBen Gras ext2fs_blkfree(struct inode *ip, daddr_t bno)
497d65f6f70SBen Gras {
498d65f6f70SBen Gras 	struct m_ext2fs *fs;
499d65f6f70SBen Gras 	char *bbp;
500d65f6f70SBen Gras 	struct buf *bp;
501d65f6f70SBen Gras 	int error, cg;
502d65f6f70SBen Gras 
503d65f6f70SBen Gras 	fs = ip->i_e2fs;
504d65f6f70SBen Gras 	cg = dtog(fs, bno);
505d65f6f70SBen Gras 	if ((u_int)bno >= fs->e2fs.e2fs_bcount) {
506d65f6f70SBen Gras 		printf("bad block %lld, ino %llu\n", (long long)bno,
507d65f6f70SBen Gras 		    (unsigned long long)ip->i_number);
508d65f6f70SBen Gras 		ext2fs_fserr(fs, ip->i_uid, "bad block");
509d65f6f70SBen Gras 		return;
510d65f6f70SBen Gras 	}
511d65f6f70SBen Gras 	error = bread(ip->i_devvp,
51284d9c625SLionel Sambuc 		EXT2_FSBTODB(fs, fs->e2fs_gd[cg].ext2bgd_b_bitmap),
513*0a6a1f1dSLionel Sambuc 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
514d65f6f70SBen Gras 	if (error) {
515d65f6f70SBen Gras 		return;
516d65f6f70SBen Gras 	}
517d65f6f70SBen Gras 	bbp = (char *)bp->b_data;
518d65f6f70SBen Gras 	bno = dtogd(fs, bno);
519d65f6f70SBen Gras 	if (isclr(bbp, bno)) {
520d65f6f70SBen Gras 		printf("dev = 0x%llx, block = %lld, fs = %s\n",
521d65f6f70SBen Gras 		    (unsigned long long)ip->i_dev, (long long)bno,
522d65f6f70SBen Gras 		    fs->e2fs_fsmnt);
523d65f6f70SBen Gras 		panic("blkfree: freeing free block");
524d65f6f70SBen Gras 	}
525d65f6f70SBen Gras 	clrbit(bbp, bno);
526d65f6f70SBen Gras 	fs->e2fs.e2fs_fbcount++;
527d65f6f70SBen Gras 	fs->e2fs_gd[cg].ext2bgd_nbfree++;
528d65f6f70SBen Gras 
529d65f6f70SBen Gras 	fs->e2fs_fmod = 1;
530d65f6f70SBen Gras 	bdwrite(bp);
531d65f6f70SBen Gras }
532d65f6f70SBen Gras 
533d65f6f70SBen Gras /*
534d65f6f70SBen Gras  * Free an inode.
535d65f6f70SBen Gras  *
536d65f6f70SBen Gras  * The specified inode is placed back in the free map.
537d65f6f70SBen Gras  */
538d65f6f70SBen Gras int
ext2fs_vfree(struct vnode * pvp,ino_t ino,int mode)539d65f6f70SBen Gras ext2fs_vfree(struct vnode *pvp, ino_t ino, int mode)
540d65f6f70SBen Gras {
541d65f6f70SBen Gras 	struct m_ext2fs *fs;
542d65f6f70SBen Gras 	char *ibp;
543d65f6f70SBen Gras 	struct inode *pip;
544d65f6f70SBen Gras 	struct buf *bp;
545d65f6f70SBen Gras 	int error, cg;
546d65f6f70SBen Gras 
547d65f6f70SBen Gras 	pip = VTOI(pvp);
548d65f6f70SBen Gras 	fs = pip->i_e2fs;
549d65f6f70SBen Gras 	if ((u_int)ino > fs->e2fs.e2fs_icount || (u_int)ino < EXT2_FIRSTINO)
550d65f6f70SBen Gras 		panic("ifree: range: dev = 0x%llx, ino = %llu, fs = %s",
551d65f6f70SBen Gras 		    (unsigned long long)pip->i_dev, (unsigned long long)ino,
552d65f6f70SBen Gras 		    fs->e2fs_fsmnt);
553d65f6f70SBen Gras 	cg = ino_to_cg(fs, ino);
554d65f6f70SBen Gras 	error = bread(pip->i_devvp,
55584d9c625SLionel Sambuc 		EXT2_FSBTODB(fs, fs->e2fs_gd[cg].ext2bgd_i_bitmap),
556*0a6a1f1dSLionel Sambuc 		(int)fs->e2fs_bsize, B_MODIFY, &bp);
557d65f6f70SBen Gras 	if (error) {
558d65f6f70SBen Gras 		return (0);
559d65f6f70SBen Gras 	}
560d65f6f70SBen Gras 	ibp = (char *)bp->b_data;
561d65f6f70SBen Gras 	ino = (ino - 1) % fs->e2fs.e2fs_ipg;
562d65f6f70SBen Gras 	if (isclr(ibp, ino)) {
563d65f6f70SBen Gras 		printf("dev = 0x%llx, ino = %llu, fs = %s\n",
564d65f6f70SBen Gras 		    (unsigned long long)pip->i_dev,
565d65f6f70SBen Gras 		    (unsigned long long)ino, fs->e2fs_fsmnt);
566d65f6f70SBen Gras 		if (fs->e2fs_ronly == 0)
567d65f6f70SBen Gras 			panic("ifree: freeing free inode");
568d65f6f70SBen Gras 	}
569d65f6f70SBen Gras 	clrbit(ibp, ino);
570d65f6f70SBen Gras 	fs->e2fs.e2fs_ficount++;
571d65f6f70SBen Gras 	fs->e2fs_gd[cg].ext2bgd_nifree++;
572d65f6f70SBen Gras 	if ((mode & IFMT) == IFDIR) {
573d65f6f70SBen Gras 		fs->e2fs_gd[cg].ext2bgd_ndirs--;
574d65f6f70SBen Gras 	}
575d65f6f70SBen Gras 	fs->e2fs_fmod = 1;
576d65f6f70SBen Gras 	bdwrite(bp);
577d65f6f70SBen Gras 	return (0);
578d65f6f70SBen Gras }
579d65f6f70SBen Gras 
580d65f6f70SBen Gras /*
581d65f6f70SBen Gras  * Find a block in the specified cylinder group.
582d65f6f70SBen Gras  *
583d65f6f70SBen Gras  * It is a panic if a request is made to find a block if none are
584d65f6f70SBen Gras  * available.
585d65f6f70SBen Gras  */
586d65f6f70SBen Gras 
587d65f6f70SBen Gras static daddr_t
ext2fs_mapsearch(struct m_ext2fs * fs,char * bbp,daddr_t bpref)588d65f6f70SBen Gras ext2fs_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
589d65f6f70SBen Gras {
590d65f6f70SBen Gras 	int start, len, loc, i, map;
591d65f6f70SBen Gras 
592d65f6f70SBen Gras 	/*
593d65f6f70SBen Gras 	 * find the fragment by searching through the free block
594d65f6f70SBen Gras 	 * map for an appropriate bit pattern
595d65f6f70SBen Gras 	 */
596d65f6f70SBen Gras 	if (bpref)
597d65f6f70SBen Gras 		start = dtogd(fs, bpref) / NBBY;
598d65f6f70SBen Gras 	else
599d65f6f70SBen Gras 		start = 0;
600d65f6f70SBen Gras 	len = howmany(fs->e2fs.e2fs_fpg, NBBY) - start;
601d65f6f70SBen Gras 	loc = skpc(0xff, len, &bbp[start]);
602d65f6f70SBen Gras 	if (loc == 0) {
603d65f6f70SBen Gras 		len = start + 1;
604d65f6f70SBen Gras 		start = 0;
605d65f6f70SBen Gras 		loc = skpc(0xff, len, &bbp[start]);
606d65f6f70SBen Gras 		if (loc == 0) {
607d65f6f70SBen Gras 			printf("start = %d, len = %d, fs = %s\n",
608d65f6f70SBen Gras 				start, len, fs->e2fs_fsmnt);
609d65f6f70SBen Gras 			panic("ext2fs_alloccg: map corrupted");
610d65f6f70SBen Gras 			/* NOTREACHED */
611d65f6f70SBen Gras 		}
612d65f6f70SBen Gras 	}
613d65f6f70SBen Gras 	i = start + len - loc;
614d65f6f70SBen Gras 	map = bbp[i] ^ 0xff;
615d65f6f70SBen Gras 	if (map == 0) {
616d65f6f70SBen Gras 		printf("fs = %s\n", fs->e2fs_fsmnt);
617d65f6f70SBen Gras 		panic("ext2fs_mapsearch: block not in map");
618d65f6f70SBen Gras 	}
619d65f6f70SBen Gras 	return i * NBBY + ffs(map) - 1;
620d65f6f70SBen Gras }
621d65f6f70SBen Gras 
622d65f6f70SBen Gras /*
623d65f6f70SBen Gras  * Fserr prints the name of a file system with an error diagnostic.
624d65f6f70SBen Gras  *
625d65f6f70SBen Gras  * The form of the error message is:
626d65f6f70SBen Gras  *	fs: error message
627d65f6f70SBen Gras  */
628d65f6f70SBen Gras static void
ext2fs_fserr(struct m_ext2fs * fs,u_int uid,const char * cp)629d65f6f70SBen Gras ext2fs_fserr(struct m_ext2fs *fs, u_int uid, const char *cp)
630d65f6f70SBen Gras {
631d65f6f70SBen Gras 
632d65f6f70SBen Gras 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->e2fs_fsmnt, cp);
633d65f6f70SBen Gras }
634