1e09c00caSUlf Lilleengen /*-
2e09c00caSUlf Lilleengen * modified for Lites 1.1
3e09c00caSUlf Lilleengen *
4e09c00caSUlf Lilleengen * Aug 1995, Godmar Back (gback@cs.utah.edu)
5e09c00caSUlf Lilleengen * University of Utah, Department of Computer Science
6e09c00caSUlf Lilleengen */
7e09c00caSUlf Lilleengen /*-
851369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
951369649SPedro F. Giffuni *
10e09c00caSUlf Lilleengen * Copyright (c) 1982, 1986, 1989, 1993
11e09c00caSUlf Lilleengen * The Regents of the University of California. All rights reserved.
12e09c00caSUlf Lilleengen *
13e09c00caSUlf Lilleengen * Redistribution and use in source and binary forms, with or without
14e09c00caSUlf Lilleengen * modification, are permitted provided that the following conditions
15e09c00caSUlf Lilleengen * are met:
16e09c00caSUlf Lilleengen * 1. Redistributions of source code must retain the above copyright
17e09c00caSUlf Lilleengen * notice, this list of conditions and the following disclaimer.
18e09c00caSUlf Lilleengen * 2. Redistributions in binary form must reproduce the above copyright
19e09c00caSUlf Lilleengen * notice, this list of conditions and the following disclaimer in the
20e09c00caSUlf Lilleengen * documentation and/or other materials provided with the distribution.
21fca15474SPedro F. Giffuni * 3. Neither the name of the University nor the names of its contributors
22e09c00caSUlf Lilleengen * may be used to endorse or promote products derived from this software
23e09c00caSUlf Lilleengen * without specific prior written permission.
24e09c00caSUlf Lilleengen *
25e09c00caSUlf Lilleengen * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26e09c00caSUlf Lilleengen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27e09c00caSUlf Lilleengen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28e09c00caSUlf Lilleengen * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29e09c00caSUlf Lilleengen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30e09c00caSUlf Lilleengen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31e09c00caSUlf Lilleengen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32e09c00caSUlf Lilleengen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33e09c00caSUlf Lilleengen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34e09c00caSUlf Lilleengen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35e09c00caSUlf Lilleengen * SUCH DAMAGE.
36e09c00caSUlf Lilleengen */
37e09c00caSUlf Lilleengen
38e09c00caSUlf Lilleengen #include <sys/param.h>
39e09c00caSUlf Lilleengen #include <sys/systm.h>
40e09c00caSUlf Lilleengen #include <sys/conf.h>
41e09c00caSUlf Lilleengen #include <sys/vnode.h>
42ebc94b66SFedor Uporov #include <sys/sdt.h>
43e09c00caSUlf Lilleengen #include <sys/stat.h>
44e09c00caSUlf Lilleengen #include <sys/mount.h>
455b63c125SPedro F. Giffuni #include <sys/sysctl.h>
46e09c00caSUlf Lilleengen #include <sys/syslog.h>
47e09c00caSUlf Lilleengen #include <sys/buf.h>
48d23db91eSPedro F. Giffuni #include <sys/endian.h>
49e09c00caSUlf Lilleengen
50b6113fb3SPedro F. Giffuni #include <fs/ext2fs/fs.h>
51e09c00caSUlf Lilleengen #include <fs/ext2fs/inode.h>
52e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2_mount.h>
53e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2fs.h>
54e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2_extern.h>
55e09c00caSUlf Lilleengen
56ebc94b66SFedor Uporov SDT_PROVIDER_DEFINE(ext2fs);
57ebc94b66SFedor Uporov /*
58ebc94b66SFedor Uporov * ext2fs trace probe:
59ebc94b66SFedor Uporov * arg0: verbosity. Higher numbers give more verbose messages
60ebc94b66SFedor Uporov * arg1: Textual message
61ebc94b66SFedor Uporov */
62ebc94b66SFedor Uporov SDT_PROBE_DEFINE2(ext2fs, , alloc, trace, "int", "char*");
63ebc94b66SFedor Uporov SDT_PROBE_DEFINE3(ext2fs, , alloc, ext2_reallocblks_realloc,
64ebc94b66SFedor Uporov "ino_t", "e2fs_lbn_t", "e2fs_lbn_t");
65ebc94b66SFedor Uporov SDT_PROBE_DEFINE1(ext2fs, , alloc, ext2_reallocblks_bap, "uint32_t");
66ebc94b66SFedor Uporov SDT_PROBE_DEFINE1(ext2fs, , alloc, ext2_reallocblks_blkno, "e2fs_daddr_t");
67ebc94b66SFedor Uporov SDT_PROBE_DEFINE2(ext2fs, , alloc, ext2_b_bitmap_validate_error, "char*", "int");
68ebc94b66SFedor Uporov SDT_PROBE_DEFINE3(ext2fs, , alloc, ext2_nodealloccg_bmap_corrupted,
69ebc94b66SFedor Uporov "int", "daddr_t", "char*");
70ebc94b66SFedor Uporov SDT_PROBE_DEFINE2(ext2fs, , alloc, ext2_blkfree_bad_block, "ino_t", "e4fs_daddr_t");
71ebc94b66SFedor Uporov SDT_PROBE_DEFINE2(ext2fs, , alloc, ext2_vfree_doublefree, "char*", "ino_t");
72ebc94b66SFedor Uporov
73e09c00caSUlf Lilleengen static daddr_t ext2_alloccg(struct inode *, int, daddr_t, int);
745b63c125SPedro F. Giffuni static daddr_t ext2_clusteralloc(struct inode *, int, daddr_t, int);
75e09c00caSUlf Lilleengen static u_long ext2_dirpref(struct inode *);
76ffbde5eaSFedor Uporov static e4fs_daddr_t ext2_hashalloc(struct inode *, int, long, int,
77e09c00caSUlf Lilleengen daddr_t (*)(struct inode *, int, daddr_t,
78e09c00caSUlf Lilleengen int));
79e09c00caSUlf Lilleengen static daddr_t ext2_nodealloccg(struct inode *, int, daddr_t, int);
80e09c00caSUlf Lilleengen static daddr_t ext2_mapsearch(struct m_ext2fs *, char *, daddr_t);
81c767faa5SJohn Baldwin
82e09c00caSUlf Lilleengen /*
83e09c00caSUlf Lilleengen * Allocate a block in the filesystem.
84e09c00caSUlf Lilleengen *
85e09c00caSUlf Lilleengen * A preference may be optionally specified. If a preference is given
86e09c00caSUlf Lilleengen * the following hierarchy is used to allocate a block:
87e09c00caSUlf Lilleengen * 1) allocate the requested block.
88e09c00caSUlf Lilleengen * 2) allocate a rotationally optimal block in the same cylinder.
89e09c00caSUlf Lilleengen * 3) allocate a block in the same cylinder group.
908ea3cedaSGordon Bergling * 4) quadratically rehash into other cylinder groups, until an
91e09c00caSUlf Lilleengen * available block is located.
92e09c00caSUlf Lilleengen * If no block preference is given the following hierarchy is used
93e09c00caSUlf Lilleengen * to allocate a block:
94e09c00caSUlf Lilleengen * 1) allocate a block in the cylinder group that contains the
95e09c00caSUlf Lilleengen * inode for the file.
968ea3cedaSGordon Bergling * 2) quadratically rehash into other cylinder groups, until an
97e09c00caSUlf Lilleengen * available block is located.
98e09c00caSUlf Lilleengen */
99e09c00caSUlf Lilleengen int
ext2_alloc(struct inode * ip,daddr_t lbn,e4fs_daddr_t bpref,int size,struct ucred * cred,e4fs_daddr_t * bnp)10070097aacSPedro F. Giffuni ext2_alloc(struct inode *ip, daddr_t lbn, e4fs_daddr_t bpref, int size,
10170097aacSPedro F. Giffuni struct ucred *cred, e4fs_daddr_t *bnp)
102e09c00caSUlf Lilleengen {
103e09c00caSUlf Lilleengen struct m_ext2fs *fs;
104e09c00caSUlf Lilleengen struct ext2mount *ump;
105ffbde5eaSFedor Uporov e4fs_daddr_t bno;
106e09c00caSUlf Lilleengen int cg;
107bf9a211dSPedro F. Giffuni
108e09c00caSUlf Lilleengen *bnp = 0;
109e09c00caSUlf Lilleengen fs = ip->i_e2fs;
110e09c00caSUlf Lilleengen ump = ip->i_ump;
111e09c00caSUlf Lilleengen mtx_assert(EXT2_MTX(ump), MA_OWNED);
11277b193c2SPedro F. Giffuni #ifdef INVARIANTS
113e09c00caSUlf Lilleengen if ((u_int)size > fs->e2fs_bsize || blkoff(fs, size) != 0) {
114e09c00caSUlf Lilleengen vn_printf(ip->i_devvp, "bsize = %lu, size = %d, fs = %s\n",
115e09c00caSUlf Lilleengen (long unsigned int)fs->e2fs_bsize, size, fs->e2fs_fsmnt);
116e09c00caSUlf Lilleengen panic("ext2_alloc: bad size");
117e09c00caSUlf Lilleengen }
118e09c00caSUlf Lilleengen if (cred == NOCRED)
119e09c00caSUlf Lilleengen panic("ext2_alloc: missing credential");
12077b193c2SPedro F. Giffuni #endif /* INVARIANTS */
1213acd9182SFedor Uporov if (size == fs->e2fs_bsize && fs->e2fs_fbcount == 0)
122e09c00caSUlf Lilleengen goto nospace;
123e09c00caSUlf Lilleengen if (cred->cr_uid != 0 &&
1243acd9182SFedor Uporov fs->e2fs_fbcount < fs->e2fs_rbcount)
125e09c00caSUlf Lilleengen goto nospace;
1263acd9182SFedor Uporov if (bpref >= fs->e2fs_bcount)
127e09c00caSUlf Lilleengen bpref = 0;
128e09c00caSUlf Lilleengen if (bpref == 0)
129e09c00caSUlf Lilleengen cg = ino_to_cg(fs, ip->i_number);
130e09c00caSUlf Lilleengen else
131e09c00caSUlf Lilleengen cg = dtog(fs, bpref);
132e09c00caSUlf Lilleengen bno = (daddr_t)ext2_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
133e09c00caSUlf Lilleengen ext2_alloccg);
134e09c00caSUlf Lilleengen if (bno > 0) {
135c767faa5SJohn Baldwin /* set next_alloc fields as done in block_getblk */
136c767faa5SJohn Baldwin ip->i_next_alloc_block = lbn;
137c767faa5SJohn Baldwin ip->i_next_alloc_goal = bno;
138c767faa5SJohn Baldwin
139e09c00caSUlf Lilleengen ip->i_blocks += btodb(fs->e2fs_bsize);
140e09c00caSUlf Lilleengen ip->i_flag |= IN_CHANGE | IN_UPDATE;
141e09c00caSUlf Lilleengen *bnp = bno;
142e09c00caSUlf Lilleengen return (0);
143e09c00caSUlf Lilleengen }
144e09c00caSUlf Lilleengen nospace:
145e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
146ebc94b66SFedor Uporov SDT_PROBE2(ext2fs, , alloc, trace, 1, "cannot allocate data block");
147e09c00caSUlf Lilleengen return (ENOSPC);
148e09c00caSUlf Lilleengen }
149e09c00caSUlf Lilleengen
150e09c00caSUlf Lilleengen /*
15134f43888SPedro F. Giffuni * Allocate EA's block for inode.
15234f43888SPedro F. Giffuni */
153ffbde5eaSFedor Uporov e4fs_daddr_t
ext2_alloc_meta(struct inode * ip)154b394cd1eSFedor Uporov ext2_alloc_meta(struct inode *ip)
15534f43888SPedro F. Giffuni {
15634f43888SPedro F. Giffuni struct m_ext2fs *fs;
157b394cd1eSFedor Uporov daddr_t blk;
15834f43888SPedro F. Giffuni
15934f43888SPedro F. Giffuni fs = ip->i_e2fs;
16034f43888SPedro F. Giffuni
16134f43888SPedro F. Giffuni EXT2_LOCK(ip->i_ump);
162b394cd1eSFedor Uporov blk = ext2_hashalloc(ip, ino_to_cg(fs, ip->i_number), 0, fs->e2fs_bsize,
163b394cd1eSFedor Uporov ext2_alloccg);
164ebc94b66SFedor Uporov if (0 == blk) {
16534f43888SPedro F. Giffuni EXT2_UNLOCK(ip->i_ump);
166ebc94b66SFedor Uporov SDT_PROBE2(ext2fs, , alloc, trace, 1, "cannot allocate meta block");
167ebc94b66SFedor Uporov }
16834f43888SPedro F. Giffuni
169b394cd1eSFedor Uporov return (blk);
17034f43888SPedro F. Giffuni }
17134f43888SPedro F. Giffuni
17234f43888SPedro F. Giffuni /*
173e09c00caSUlf Lilleengen * Reallocate a sequence of blocks into a contiguous sequence of blocks.
174e09c00caSUlf Lilleengen *
175e09c00caSUlf Lilleengen * The vnode and an array of buffer pointers for a range of sequential
176e09c00caSUlf Lilleengen * logical blocks to be made contiguous is given. The allocator attempts
177e09c00caSUlf Lilleengen * to find a range of sequential blocks starting as close as possible to
178e09c00caSUlf Lilleengen * an fs_rotdelay offset from the end of the allocation for the logical
179e09c00caSUlf Lilleengen * block immediately preceding the current range. If successful, the
180e09c00caSUlf Lilleengen * physical block numbers in the buffer pointers and in the inode are
181e09c00caSUlf Lilleengen * changed to reflect the new allocation. If unsuccessful, the allocation
182e09c00caSUlf Lilleengen * is left unchanged. The success in doing the reallocation is returned.
183e09c00caSUlf Lilleengen * Note that the error return is not reflected back to the user. Rather
184e09c00caSUlf Lilleengen * the previous block allocation will be used.
185e09c00caSUlf Lilleengen */
186e09c00caSUlf Lilleengen
1877029da5cSPawel Biernacki static SYSCTL_NODE(_vfs, OID_AUTO, ext2fs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1887029da5cSPawel Biernacki "EXT2FS filesystem");
189e09c00caSUlf Lilleengen
19099984d22SPedro F. Giffuni static int doasyncfree = 1;
191bf9a211dSPedro F. Giffuni
192c767faa5SJohn Baldwin SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doasyncfree, CTLFLAG_RW, &doasyncfree, 0,
193c2f0581eSPedro F. Giffuni "Use asynchronous writes to update block pointers when freeing blocks");
194c767faa5SJohn Baldwin
195dc262e5bSFedor Uporov static int doreallocblks = 0;
196bf9a211dSPedro F. Giffuni
197c767faa5SJohn Baldwin SYSCTL_INT(_vfs_ext2fs, OID_AUTO, doreallocblks, CTLFLAG_RW, &doreallocblks, 0, "");
198e09c00caSUlf Lilleengen
199e09c00caSUlf Lilleengen int
ext2_reallocblks(struct vop_reallocblks_args * ap)200a9d1b299SPedro F. Giffuni ext2_reallocblks(struct vop_reallocblks_args *ap)
201e09c00caSUlf Lilleengen {
202e09c00caSUlf Lilleengen struct m_ext2fs *fs;
203e09c00caSUlf Lilleengen struct inode *ip;
204e09c00caSUlf Lilleengen struct vnode *vp;
205e09c00caSUlf Lilleengen struct buf *sbp, *ebp;
206e45e8680SPedro F. Giffuni uint32_t *bap, *sbap, *ebap;
207e09c00caSUlf Lilleengen struct ext2mount *ump;
208e09c00caSUlf Lilleengen struct cluster_save *buflist;
2091dc349abSEd Maste struct indir start_ap[EXT2_NIADDR + 1], end_ap[EXT2_NIADDR + 1], *idp;
210da057ed2SPedro F. Giffuni e2fs_lbn_t start_lbn, end_lbn;
21170097aacSPedro F. Giffuni int soff;
21270097aacSPedro F. Giffuni e2fs_daddr_t newblk, blkno;
213e09c00caSUlf Lilleengen int i, len, start_lvl, end_lvl, pref, ssize;
214e09c00caSUlf Lilleengen
2155b63c125SPedro F. Giffuni if (doreallocblks == 0)
2165b63c125SPedro F. Giffuni return (ENOSPC);
2175b63c125SPedro F. Giffuni
218e09c00caSUlf Lilleengen vp = ap->a_vp;
219e09c00caSUlf Lilleengen ip = VTOI(vp);
220e09c00caSUlf Lilleengen fs = ip->i_e2fs;
221e09c00caSUlf Lilleengen ump = ip->i_ump;
2225b63c125SPedro F. Giffuni
223b394cd1eSFedor Uporov if (fs->e2fs_contigsumsize <= 0 || ip->i_flag & IN_E4EXTENTS)
224e09c00caSUlf Lilleengen return (ENOSPC);
2255b63c125SPedro F. Giffuni
226e09c00caSUlf Lilleengen buflist = ap->a_buflist;
227e09c00caSUlf Lilleengen len = buflist->bs_nchildren;
228e09c00caSUlf Lilleengen start_lbn = buflist->bs_children[0]->b_lblkno;
229e09c00caSUlf Lilleengen end_lbn = start_lbn + len - 1;
23077b193c2SPedro F. Giffuni #ifdef INVARIANTS
231e09c00caSUlf Lilleengen for (i = 1; i < len; i++)
232e09c00caSUlf Lilleengen if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
233e09c00caSUlf Lilleengen panic("ext2_reallocblks: non-cluster");
234e09c00caSUlf Lilleengen #endif
235e09c00caSUlf Lilleengen /*
2367306dea4SPedro F. Giffuni * If the cluster crosses the boundary for the first indirect
2377306dea4SPedro F. Giffuni * block, leave space for the indirect block. Indirect blocks
2387306dea4SPedro F. Giffuni * are initially laid out in a position after the last direct
2397306dea4SPedro F. Giffuni * block. Block reallocation would usually destroy locality by
2407306dea4SPedro F. Giffuni * moving the indirect block out of the way to make room for
2417306dea4SPedro F. Giffuni * data blocks if we didn't compensate here. We should also do
2427306dea4SPedro F. Giffuni * this for other indirect block boundaries, but it is only
2437306dea4SPedro F. Giffuni * important for the first one.
2447306dea4SPedro F. Giffuni */
2451dc349abSEd Maste if (start_lbn < EXT2_NDADDR && end_lbn >= EXT2_NDADDR)
2467306dea4SPedro F. Giffuni return (ENOSPC);
2477306dea4SPedro F. Giffuni /*
248e09c00caSUlf Lilleengen * If the latest allocation is in a new cylinder group, assume that
249e09c00caSUlf Lilleengen * the filesystem has decided to move and do not force it back to
250e09c00caSUlf Lilleengen * the previous cylinder group.
251e09c00caSUlf Lilleengen */
252e09c00caSUlf Lilleengen if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
253e09c00caSUlf Lilleengen dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
254e09c00caSUlf Lilleengen return (ENOSPC);
255e09c00caSUlf Lilleengen if (ext2_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
256e09c00caSUlf Lilleengen ext2_getlbns(vp, end_lbn, end_ap, &end_lvl))
257e09c00caSUlf Lilleengen return (ENOSPC);
258e09c00caSUlf Lilleengen /*
259e09c00caSUlf Lilleengen * Get the starting offset and block map for the first block.
260e09c00caSUlf Lilleengen */
261e09c00caSUlf Lilleengen if (start_lvl == 0) {
262e09c00caSUlf Lilleengen sbap = &ip->i_db[0];
263e09c00caSUlf Lilleengen soff = start_lbn;
264e09c00caSUlf Lilleengen } else {
265e09c00caSUlf Lilleengen idp = &start_ap[start_lvl - 1];
266e09c00caSUlf Lilleengen if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &sbp)) {
267e09c00caSUlf Lilleengen brelse(sbp);
268e09c00caSUlf Lilleengen return (ENOSPC);
269e09c00caSUlf Lilleengen }
270f744956bSPedro F. Giffuni sbap = (u_int *)sbp->b_data;
271e09c00caSUlf Lilleengen soff = idp->in_off;
272e09c00caSUlf Lilleengen }
273e09c00caSUlf Lilleengen /*
274e09c00caSUlf Lilleengen * If the block range spans two block maps, get the second map.
275e09c00caSUlf Lilleengen */
276e45e8680SPedro F. Giffuni ebap = NULL;
277e09c00caSUlf Lilleengen if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
278e09c00caSUlf Lilleengen ssize = len;
279e09c00caSUlf Lilleengen } else {
28077b193c2SPedro F. Giffuni #ifdef INVARIANTS
281e09c00caSUlf Lilleengen if (start_ap[start_lvl - 1].in_lbn == idp->in_lbn)
282757224cbSPedro F. Giffuni panic("ext2_reallocblks: start == end");
283e09c00caSUlf Lilleengen #endif
284e09c00caSUlf Lilleengen ssize = len - (idp->in_off + 1);
2855b63c125SPedro F. Giffuni if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &ebp))
286e09c00caSUlf Lilleengen goto fail;
287f744956bSPedro F. Giffuni ebap = (u_int *)ebp->b_data;
288e09c00caSUlf Lilleengen }
289e09c00caSUlf Lilleengen /*
2905b63c125SPedro F. Giffuni * Find the preferred location for the cluster.
2915b63c125SPedro F. Giffuni */
2925b63c125SPedro F. Giffuni EXT2_LOCK(ump);
2935b63c125SPedro F. Giffuni pref = ext2_blkpref(ip, start_lbn, soff, sbap, 0);
2945b63c125SPedro F. Giffuni /*
295e09c00caSUlf Lilleengen * Search the block map looking for an allocation of the desired size.
296e09c00caSUlf Lilleengen */
29770097aacSPedro F. Giffuni if ((newblk = (e2fs_daddr_t)ext2_hashalloc(ip, dtog(fs, pref), pref,
298e09c00caSUlf Lilleengen len, ext2_clusteralloc)) == 0) {
299e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
300e09c00caSUlf Lilleengen goto fail;
301e09c00caSUlf Lilleengen }
302e09c00caSUlf Lilleengen /*
303e09c00caSUlf Lilleengen * We have found a new contiguous block.
304e09c00caSUlf Lilleengen *
305e09c00caSUlf Lilleengen * First we have to replace the old block pointers with the new
306e09c00caSUlf Lilleengen * block pointers in the inode and indirect blocks associated
307e09c00caSUlf Lilleengen * with the file.
308e09c00caSUlf Lilleengen */
309ebc94b66SFedor Uporov SDT_PROBE3(ext2fs, , alloc, ext2_reallocblks_realloc,
310ebc94b66SFedor Uporov ip->i_number, start_lbn, end_lbn);
311e09c00caSUlf Lilleengen blkno = newblk;
312e09c00caSUlf Lilleengen for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
3135b63c125SPedro F. Giffuni if (i == ssize) {
314e09c00caSUlf Lilleengen bap = ebap;
315e09c00caSUlf Lilleengen soff = -i;
3165b63c125SPedro F. Giffuni }
31777b193c2SPedro F. Giffuni #ifdef INVARIANTS
318e09c00caSUlf Lilleengen if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
319e09c00caSUlf Lilleengen panic("ext2_reallocblks: alloc mismatch");
320e09c00caSUlf Lilleengen #endif
321ebc94b66SFedor Uporov SDT_PROBE1(ext2fs, , alloc, ext2_reallocblks_bap, *bap);
322e09c00caSUlf Lilleengen *bap++ = blkno;
323e09c00caSUlf Lilleengen }
324e09c00caSUlf Lilleengen /*
325e09c00caSUlf Lilleengen * Next we must write out the modified inode and indirect blocks.
326e09c00caSUlf Lilleengen * For strict correctness, the writes should be synchronous since
327e09c00caSUlf Lilleengen * the old block values may have been written to disk. In practise
328e09c00caSUlf Lilleengen * they are almost never written, but if we are concerned about
329e09c00caSUlf Lilleengen * strict correctness, the `doasyncfree' flag should be set to zero.
330e09c00caSUlf Lilleengen *
331e09c00caSUlf Lilleengen * The test on `doasyncfree' should be changed to test a flag
332e09c00caSUlf Lilleengen * that shows whether the associated buffers and inodes have
333e09c00caSUlf Lilleengen * been written. The flag should be set when the cluster is
334e09c00caSUlf Lilleengen * started and cleared whenever the buffer or inode is flushed.
335e09c00caSUlf Lilleengen * We can then check below to see if it is set, and do the
336e09c00caSUlf Lilleengen * synchronous write only when it has been cleared.
337e09c00caSUlf Lilleengen */
338e09c00caSUlf Lilleengen if (sbap != &ip->i_db[0]) {
339e09c00caSUlf Lilleengen if (doasyncfree)
340e09c00caSUlf Lilleengen bdwrite(sbp);
341e09c00caSUlf Lilleengen else
342e09c00caSUlf Lilleengen bwrite(sbp);
343e09c00caSUlf Lilleengen } else {
344e09c00caSUlf Lilleengen ip->i_flag |= IN_CHANGE | IN_UPDATE;
345e09c00caSUlf Lilleengen if (!doasyncfree)
346e09c00caSUlf Lilleengen ext2_update(vp, 1);
347e09c00caSUlf Lilleengen }
348e09c00caSUlf Lilleengen if (ssize < len) {
349e09c00caSUlf Lilleengen if (doasyncfree)
350e09c00caSUlf Lilleengen bdwrite(ebp);
351e09c00caSUlf Lilleengen else
352e09c00caSUlf Lilleengen bwrite(ebp);
353e09c00caSUlf Lilleengen }
354e09c00caSUlf Lilleengen /*
355e09c00caSUlf Lilleengen * Last, free the old blocks and assign the new blocks to the buffers.
356e09c00caSUlf Lilleengen */
357e09c00caSUlf Lilleengen for (blkno = newblk, i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
358e09c00caSUlf Lilleengen ext2_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
359e09c00caSUlf Lilleengen fs->e2fs_bsize);
360e09c00caSUlf Lilleengen buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
361ebc94b66SFedor Uporov SDT_PROBE1(ext2fs, , alloc, ext2_reallocblks_blkno, blkno);
362e09c00caSUlf Lilleengen }
363ebc94b66SFedor Uporov
364e09c00caSUlf Lilleengen return (0);
365e09c00caSUlf Lilleengen
366e09c00caSUlf Lilleengen fail:
367e09c00caSUlf Lilleengen if (ssize < len)
368e09c00caSUlf Lilleengen brelse(ebp);
369e09c00caSUlf Lilleengen if (sbap != &ip->i_db[0])
370e09c00caSUlf Lilleengen brelse(sbp);
371e09c00caSUlf Lilleengen return (ENOSPC);
372e09c00caSUlf Lilleengen }
373e09c00caSUlf Lilleengen
374e09c00caSUlf Lilleengen /*
375e09c00caSUlf Lilleengen * Allocate an inode in the filesystem.
376e09c00caSUlf Lilleengen *
377e09c00caSUlf Lilleengen */
378e09c00caSUlf Lilleengen int
ext2_valloc(struct vnode * pvp,int mode,struct ucred * cred,struct vnode ** vpp)379a9d1b299SPedro F. Giffuni ext2_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp)
380e09c00caSUlf Lilleengen {
381035e4e04SPedro F. Giffuni struct timespec ts;
382e09c00caSUlf Lilleengen struct m_ext2fs *fs;
383e09c00caSUlf Lilleengen struct ext2mount *ump;
3843eed9f20SFedor Uporov struct inode *pip;
3853eed9f20SFedor Uporov struct inode *ip;
3863eed9f20SFedor Uporov struct vnode *vp;
3873eed9f20SFedor Uporov struct thread *td;
388e09c00caSUlf Lilleengen ino_t ino, ipref;
389b394cd1eSFedor Uporov int error, cg;
390e09c00caSUlf Lilleengen
391e09c00caSUlf Lilleengen *vpp = NULL;
392e09c00caSUlf Lilleengen pip = VTOI(pvp);
393e09c00caSUlf Lilleengen fs = pip->i_e2fs;
394e09c00caSUlf Lilleengen ump = pip->i_ump;
395e09c00caSUlf Lilleengen
396e09c00caSUlf Lilleengen EXT2_LOCK(ump);
397cd3acfe7SFedor Uporov if (fs->e2fs_ficount == 0)
398e09c00caSUlf Lilleengen goto noinodes;
399e09c00caSUlf Lilleengen /*
400e09c00caSUlf Lilleengen * If it is a directory then obtain a cylinder group based on
401e09c00caSUlf Lilleengen * ext2_dirpref else obtain it using ino_to_cg. The preferred inode is
402e09c00caSUlf Lilleengen * always the next inode.
403e09c00caSUlf Lilleengen */
404d8ba45e2SEd Maste if ((mode & IFMT) == IFDIR) {
405e09c00caSUlf Lilleengen cg = ext2_dirpref(pip);
406e09c00caSUlf Lilleengen if (fs->e2fs_contigdirs[cg] < 255)
407e09c00caSUlf Lilleengen fs->e2fs_contigdirs[cg]++;
408e09c00caSUlf Lilleengen } else {
409e09c00caSUlf Lilleengen cg = ino_to_cg(fs, pip->i_number);
410e09c00caSUlf Lilleengen if (fs->e2fs_contigdirs[cg] > 0)
411e09c00caSUlf Lilleengen fs->e2fs_contigdirs[cg]--;
412e09c00caSUlf Lilleengen }
413cd3acfe7SFedor Uporov ipref = cg * fs->e2fs_ipg + 1;
414e09c00caSUlf Lilleengen ino = (ino_t)ext2_hashalloc(pip, cg, (long)ipref, mode, ext2_nodealloccg);
415e09c00caSUlf Lilleengen if (ino == 0)
416e09c00caSUlf Lilleengen goto noinodes;
4173eed9f20SFedor Uporov
4183eed9f20SFedor Uporov td = curthread;
4193eed9f20SFedor Uporov error = vfs_hash_get(ump->um_mountp, ino, LK_EXCLUSIVE, td, vpp, NULL, NULL);
4203eed9f20SFedor Uporov if (error || *vpp != NULL) {
421e09c00caSUlf Lilleengen return (error);
422e09c00caSUlf Lilleengen }
423e09c00caSUlf Lilleengen
4243eed9f20SFedor Uporov ip = malloc(sizeof(struct inode), M_EXT2NODE, M_WAITOK | M_ZERO);
4253eed9f20SFedor Uporov
4263eed9f20SFedor Uporov /* Allocate a new vnode/inode. */
4273eed9f20SFedor Uporov if ((error = getnewvnode("ext2fs", ump->um_mountp, &ext2_vnodeops, &vp)) != 0) {
4283eed9f20SFedor Uporov free(ip, M_EXT2NODE);
4293eed9f20SFedor Uporov return (error);
4303eed9f20SFedor Uporov }
4313eed9f20SFedor Uporov
4320204d1c7SFedor Uporov lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
4333eed9f20SFedor Uporov vp->v_data = ip;
4343eed9f20SFedor Uporov ip->i_vnode = vp;
4353eed9f20SFedor Uporov ip->i_e2fs = fs = ump->um_e2fs;
4363eed9f20SFedor Uporov ip->i_ump = ump;
4373eed9f20SFedor Uporov ip->i_number = ino;
4383eed9f20SFedor Uporov ip->i_block_group = ino_to_cg(fs, ino);
4393eed9f20SFedor Uporov ip->i_next_alloc_block = 0;
4403eed9f20SFedor Uporov ip->i_next_alloc_goal = 0;
4413eed9f20SFedor Uporov
4423eed9f20SFedor Uporov error = insmntque(vp, ump->um_mountp);
4433eed9f20SFedor Uporov if (error) {
4443eed9f20SFedor Uporov free(ip, M_EXT2NODE);
4453eed9f20SFedor Uporov return (error);
4463eed9f20SFedor Uporov }
4473eed9f20SFedor Uporov
4483eed9f20SFedor Uporov error = vfs_hash_insert(vp, ino, LK_EXCLUSIVE, td, vpp, NULL, NULL);
4493eed9f20SFedor Uporov if (error || *vpp != NULL) {
4503eed9f20SFedor Uporov *vpp = NULL;
4513eed9f20SFedor Uporov free(ip, M_EXT2NODE);
4523eed9f20SFedor Uporov return (error);
4533eed9f20SFedor Uporov }
4543eed9f20SFedor Uporov
4553eed9f20SFedor Uporov if ((error = ext2_vinit(ump->um_mountp, &ext2_fifoops, &vp)) != 0) {
4563eed9f20SFedor Uporov vput(vp);
4573eed9f20SFedor Uporov *vpp = NULL;
4583eed9f20SFedor Uporov free(ip, M_EXT2NODE);
4593eed9f20SFedor Uporov return (error);
4603eed9f20SFedor Uporov }
4613eed9f20SFedor Uporov
462b394cd1eSFedor Uporov if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_EXTENTS)
463b394cd1eSFedor Uporov && (S_ISREG(mode) || S_ISDIR(mode)))
464b394cd1eSFedor Uporov ext4_ext_tree_init(ip);
465b394cd1eSFedor Uporov else
466b394cd1eSFedor Uporov memset(ip->i_data, 0, sizeof(ip->i_data));
467b394cd1eSFedor Uporov
468e09c00caSUlf Lilleengen /*
469e09c00caSUlf Lilleengen * Set up a new generation number for this inode.
47043ce40e8SPedro F. Giffuni * Avoid zero values.
471e09c00caSUlf Lilleengen */
47243ce40e8SPedro F. Giffuni do {
473df04a188SKevin Lo ip->i_gen = arc4random();
47443ce40e8SPedro F. Giffuni } while (ip->i_gen == 0);
475035e4e04SPedro F. Giffuni
476035e4e04SPedro F. Giffuni vfs_timestamp(&ts);
477035e4e04SPedro F. Giffuni ip->i_birthtime = ts.tv_sec;
478035e4e04SPedro F. Giffuni ip->i_birthnsec = ts.tv_nsec;
479035e4e04SPedro F. Giffuni
480829f0bcbSMateusz Guzik vn_set_state(vp, VSTATE_CONSTRUCTED);
4813eed9f20SFedor Uporov *vpp = vp;
4823eed9f20SFedor Uporov
483e09c00caSUlf Lilleengen return (0);
4843eed9f20SFedor Uporov
485e09c00caSUlf Lilleengen noinodes:
486e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
487ebc94b66SFedor Uporov SDT_PROBE2(ext2fs, , alloc, trace, 1, "out of inodes");
488e09c00caSUlf Lilleengen return (ENOSPC);
489e09c00caSUlf Lilleengen }
490e09c00caSUlf Lilleengen
491e09c00caSUlf Lilleengen /*
4923acd9182SFedor Uporov * 64-bit compatible getters and setters for struct ext2_gd from ext2fs.h
4933acd9182SFedor Uporov */
4946e38bf94SFedor Uporov uint64_t
e2fs_gd_get_b_bitmap(struct ext2_gd * gd)4953acd9182SFedor Uporov e2fs_gd_get_b_bitmap(struct ext2_gd *gd)
4963acd9182SFedor Uporov {
4973acd9182SFedor Uporov
498cd3acfe7SFedor Uporov return (((uint64_t)(le32toh(gd->ext4bgd_b_bitmap_hi)) << 32) |
499cd3acfe7SFedor Uporov le32toh(gd->ext2bgd_b_bitmap));
5003acd9182SFedor Uporov }
5013acd9182SFedor Uporov
5026e38bf94SFedor Uporov uint64_t
e2fs_gd_get_i_bitmap(struct ext2_gd * gd)5033acd9182SFedor Uporov e2fs_gd_get_i_bitmap(struct ext2_gd *gd)
5043acd9182SFedor Uporov {
5053acd9182SFedor Uporov
506cd3acfe7SFedor Uporov return (((uint64_t)(le32toh(gd->ext4bgd_i_bitmap_hi)) << 32) |
507cd3acfe7SFedor Uporov le32toh(gd->ext2bgd_i_bitmap));
5083acd9182SFedor Uporov }
5093acd9182SFedor Uporov
5103acd9182SFedor Uporov uint64_t
e2fs_gd_get_i_tables(struct ext2_gd * gd)5113acd9182SFedor Uporov e2fs_gd_get_i_tables(struct ext2_gd *gd)
5123acd9182SFedor Uporov {
5133acd9182SFedor Uporov
514cd3acfe7SFedor Uporov return (((uint64_t)(le32toh(gd->ext4bgd_i_tables_hi)) << 32) |
515cd3acfe7SFedor Uporov le32toh(gd->ext2bgd_i_tables));
5163acd9182SFedor Uporov }
5173acd9182SFedor Uporov
5183acd9182SFedor Uporov static uint32_t
e2fs_gd_get_nbfree(struct ext2_gd * gd)5193acd9182SFedor Uporov e2fs_gd_get_nbfree(struct ext2_gd *gd)
5203acd9182SFedor Uporov {
5213acd9182SFedor Uporov
522cd3acfe7SFedor Uporov return (((uint32_t)(le16toh(gd->ext4bgd_nbfree_hi)) << 16) |
523cd3acfe7SFedor Uporov le16toh(gd->ext2bgd_nbfree));
5243acd9182SFedor Uporov }
5253acd9182SFedor Uporov
5263acd9182SFedor Uporov static void
e2fs_gd_set_nbfree(struct ext2_gd * gd,uint32_t val)5273acd9182SFedor Uporov e2fs_gd_set_nbfree(struct ext2_gd *gd, uint32_t val)
5283acd9182SFedor Uporov {
5293acd9182SFedor Uporov
530cd3acfe7SFedor Uporov gd->ext2bgd_nbfree = htole16(val & 0xffff);
531cd3acfe7SFedor Uporov gd->ext4bgd_nbfree_hi = htole16(val >> 16);
5323acd9182SFedor Uporov }
5333acd9182SFedor Uporov
5343acd9182SFedor Uporov static uint32_t
e2fs_gd_get_nifree(struct ext2_gd * gd)5353acd9182SFedor Uporov e2fs_gd_get_nifree(struct ext2_gd *gd)
5363acd9182SFedor Uporov {
5373acd9182SFedor Uporov
538cd3acfe7SFedor Uporov return (((uint32_t)(le16toh(gd->ext4bgd_nifree_hi)) << 16) |
539cd3acfe7SFedor Uporov le16toh(gd->ext2bgd_nifree));
5403acd9182SFedor Uporov }
5413acd9182SFedor Uporov
5423acd9182SFedor Uporov static void
e2fs_gd_set_nifree(struct ext2_gd * gd,uint32_t val)5433acd9182SFedor Uporov e2fs_gd_set_nifree(struct ext2_gd *gd, uint32_t val)
5443acd9182SFedor Uporov {
5453acd9182SFedor Uporov
546cd3acfe7SFedor Uporov gd->ext2bgd_nifree = htole16(val & 0xffff);
547cd3acfe7SFedor Uporov gd->ext4bgd_nifree_hi = htole16(val >> 16);
5483acd9182SFedor Uporov }
5493acd9182SFedor Uporov
5503acd9182SFedor Uporov uint32_t
e2fs_gd_get_ndirs(struct ext2_gd * gd)5513acd9182SFedor Uporov e2fs_gd_get_ndirs(struct ext2_gd *gd)
5523acd9182SFedor Uporov {
5533acd9182SFedor Uporov
554cd3acfe7SFedor Uporov return (((uint32_t)(le16toh(gd->ext4bgd_ndirs_hi)) << 16) |
555cd3acfe7SFedor Uporov le16toh(gd->ext2bgd_ndirs));
5563acd9182SFedor Uporov }
5573acd9182SFedor Uporov
5583acd9182SFedor Uporov static void
e2fs_gd_set_ndirs(struct ext2_gd * gd,uint32_t val)5593acd9182SFedor Uporov e2fs_gd_set_ndirs(struct ext2_gd *gd, uint32_t val)
5603acd9182SFedor Uporov {
5613acd9182SFedor Uporov
562cd3acfe7SFedor Uporov gd->ext2bgd_ndirs = htole16(val & 0xffff);
563cd3acfe7SFedor Uporov gd->ext4bgd_ndirs_hi = htole16(val >> 16);
5643acd9182SFedor Uporov }
5653acd9182SFedor Uporov
5663acd9182SFedor Uporov static uint32_t
e2fs_gd_get_i_unused(struct ext2_gd * gd)5673acd9182SFedor Uporov e2fs_gd_get_i_unused(struct ext2_gd *gd)
5683acd9182SFedor Uporov {
569cd3acfe7SFedor Uporov return ((uint32_t)(le16toh(gd->ext4bgd_i_unused_hi) << 16) |
570cd3acfe7SFedor Uporov le16toh(gd->ext4bgd_i_unused));
5713acd9182SFedor Uporov }
5723acd9182SFedor Uporov
5733acd9182SFedor Uporov static void
e2fs_gd_set_i_unused(struct ext2_gd * gd,uint32_t val)5743acd9182SFedor Uporov e2fs_gd_set_i_unused(struct ext2_gd *gd, uint32_t val)
5753acd9182SFedor Uporov {
5763acd9182SFedor Uporov
577cd3acfe7SFedor Uporov gd->ext4bgd_i_unused = htole16(val & 0xffff);
578cd3acfe7SFedor Uporov gd->ext4bgd_i_unused_hi = htole16(val >> 16);
5793acd9182SFedor Uporov }
5803acd9182SFedor Uporov
5813acd9182SFedor Uporov /*
582e09c00caSUlf Lilleengen * Find a cylinder to place a directory.
583e09c00caSUlf Lilleengen *
584e09c00caSUlf Lilleengen * The policy implemented by this algorithm is to allocate a
585e09c00caSUlf Lilleengen * directory inode in the same cylinder group as its parent
586e09c00caSUlf Lilleengen * directory, but also to reserve space for its files inodes
587e09c00caSUlf Lilleengen * and data. Restrict the number of directories which may be
588e09c00caSUlf Lilleengen * allocated one after another in the same cylinder group
589e09c00caSUlf Lilleengen * without intervening allocation of files.
590e09c00caSUlf Lilleengen *
591e09c00caSUlf Lilleengen * If we allocate a first level directory then force allocation
592e09c00caSUlf Lilleengen * in another cylinder group.
593e09c00caSUlf Lilleengen *
594e09c00caSUlf Lilleengen */
595e09c00caSUlf Lilleengen static u_long
ext2_dirpref(struct inode * pip)596e09c00caSUlf Lilleengen ext2_dirpref(struct inode *pip)
597e09c00caSUlf Lilleengen {
598e09c00caSUlf Lilleengen struct m_ext2fs *fs;
599955ba37bSPedro F. Giffuni int cg, prefcg, cgsize;
6003acd9182SFedor Uporov uint64_t avgbfree, minbfree;
6013acd9182SFedor Uporov u_int avgifree, avgndir, curdirsize;
6023acd9182SFedor Uporov u_int minifree, maxndir;
603f744956bSPedro F. Giffuni u_int mincg, minndir;
604955ba37bSPedro F. Giffuni u_int dirsize, maxcontigdirs;
605e09c00caSUlf Lilleengen
606e09c00caSUlf Lilleengen mtx_assert(EXT2_MTX(pip->i_ump), MA_OWNED);
607e09c00caSUlf Lilleengen fs = pip->i_e2fs;
608e09c00caSUlf Lilleengen
609cd3acfe7SFedor Uporov avgifree = fs->e2fs_ficount / fs->e2fs_gcount;
6103acd9182SFedor Uporov avgbfree = fs->e2fs_fbcount / fs->e2fs_gcount;
611e09c00caSUlf Lilleengen avgndir = fs->e2fs_total_dir / fs->e2fs_gcount;
612e09c00caSUlf Lilleengen
613e09c00caSUlf Lilleengen /*
614e09c00caSUlf Lilleengen * Force allocation in another cg if creating a first level dir.
615e09c00caSUlf Lilleengen */
616e09c00caSUlf Lilleengen ASSERT_VOP_LOCKED(ITOV(pip), "ext2fs_dirpref");
617e09c00caSUlf Lilleengen if (ITOV(pip)->v_vflag & VV_ROOT) {
618e09c00caSUlf Lilleengen prefcg = arc4random() % fs->e2fs_gcount;
619e09c00caSUlf Lilleengen mincg = prefcg;
620e09c00caSUlf Lilleengen minndir = fs->e2fs_ipg;
621e09c00caSUlf Lilleengen for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
6223acd9182SFedor Uporov if (e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]) < minndir &&
6233acd9182SFedor Uporov e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) >= avgifree &&
6243acd9182SFedor Uporov e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) >= avgbfree) {
625e09c00caSUlf Lilleengen mincg = cg;
6263acd9182SFedor Uporov minndir = e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]);
627e09c00caSUlf Lilleengen }
628e09c00caSUlf Lilleengen for (cg = 0; cg < prefcg; cg++)
6293acd9182SFedor Uporov if (e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]) < minndir &&
6303acd9182SFedor Uporov e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) >= avgifree &&
6313acd9182SFedor Uporov e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) >= avgbfree) {
632e09c00caSUlf Lilleengen mincg = cg;
6333acd9182SFedor Uporov minndir = e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]);
634e09c00caSUlf Lilleengen }
635e09c00caSUlf Lilleengen return (mincg);
636e09c00caSUlf Lilleengen }
637e09c00caSUlf Lilleengen /*
638e09c00caSUlf Lilleengen * Count various limits which used for
639e09c00caSUlf Lilleengen * optimal allocation of a directory inode.
640e09c00caSUlf Lilleengen */
641e09c00caSUlf Lilleengen maxndir = min(avgndir + fs->e2fs_ipg / 16, fs->e2fs_ipg);
642e09c00caSUlf Lilleengen minifree = avgifree - avgifree / 4;
643e09c00caSUlf Lilleengen if (minifree < 1)
644e09c00caSUlf Lilleengen minifree = 1;
645e09c00caSUlf Lilleengen minbfree = avgbfree - avgbfree / 4;
646e09c00caSUlf Lilleengen if (minbfree < 1)
647e09c00caSUlf Lilleengen minbfree = 1;
648e09c00caSUlf Lilleengen cgsize = fs->e2fs_fsize * fs->e2fs_fpg;
649e09c00caSUlf Lilleengen dirsize = AVGDIRSIZE;
650cd3acfe7SFedor Uporov curdirsize = avgndir ?
651cd3acfe7SFedor Uporov (cgsize - avgbfree * fs->e2fs_bsize) / avgndir : 0;
652e09c00caSUlf Lilleengen if (dirsize < curdirsize)
653e09c00caSUlf Lilleengen dirsize = curdirsize;
654e09c00caSUlf Lilleengen maxcontigdirs = min((avgbfree * fs->e2fs_bsize) / dirsize, 255);
655e09c00caSUlf Lilleengen maxcontigdirs = min(maxcontigdirs, fs->e2fs_ipg / AFPDIR);
656e09c00caSUlf Lilleengen if (maxcontigdirs == 0)
657e09c00caSUlf Lilleengen maxcontigdirs = 1;
658e09c00caSUlf Lilleengen
659e09c00caSUlf Lilleengen /*
660e09c00caSUlf Lilleengen * Limit number of dirs in one cg and reserve space for
661e09c00caSUlf Lilleengen * regular files, but only if we have no deficit in
662e09c00caSUlf Lilleengen * inodes or space.
663e09c00caSUlf Lilleengen */
664e09c00caSUlf Lilleengen prefcg = ino_to_cg(fs, pip->i_number);
665e09c00caSUlf Lilleengen for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
6663acd9182SFedor Uporov if (e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]) < maxndir &&
6673acd9182SFedor Uporov e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) >= minifree &&
6683acd9182SFedor Uporov e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) >= minbfree) {
669e09c00caSUlf Lilleengen if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
670e09c00caSUlf Lilleengen return (cg);
671e09c00caSUlf Lilleengen }
672ca73017aSPedro F. Giffuni for (cg = 0; cg < prefcg; cg++)
6733acd9182SFedor Uporov if (e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]) < maxndir &&
6743acd9182SFedor Uporov e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) >= minifree &&
6753acd9182SFedor Uporov e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) >= minbfree) {
676e09c00caSUlf Lilleengen if (fs->e2fs_contigdirs[cg] < maxcontigdirs)
677e09c00caSUlf Lilleengen return (cg);
678e09c00caSUlf Lilleengen }
679e09c00caSUlf Lilleengen /*
680e09c00caSUlf Lilleengen * This is a backstop when we have deficit in space.
681e09c00caSUlf Lilleengen */
682e09c00caSUlf Lilleengen for (cg = prefcg; cg < fs->e2fs_gcount; cg++)
6833acd9182SFedor Uporov if (e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) >= avgifree)
684e09c00caSUlf Lilleengen return (cg);
685ca73017aSPedro F. Giffuni for (cg = 0; cg < prefcg; cg++)
6863acd9182SFedor Uporov if (e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) >= avgifree)
687e09c00caSUlf Lilleengen break;
688e09c00caSUlf Lilleengen return (cg);
689e09c00caSUlf Lilleengen }
690e09c00caSUlf Lilleengen
691e09c00caSUlf Lilleengen /*
692e09c00caSUlf Lilleengen * Select the desired position for the next block in a file.
693e09c00caSUlf Lilleengen *
694e09c00caSUlf Lilleengen * we try to mimic what Remy does in inode_getblk/block_getblk
695e09c00caSUlf Lilleengen *
696e09c00caSUlf Lilleengen * we note: blocknr == 0 means that we're about to allocate either
697e09c00caSUlf Lilleengen * a direct block or a pointer block at the first level of indirection
698e09c00caSUlf Lilleengen * (In other words, stuff that will go in i_db[] or i_ib[])
699e09c00caSUlf Lilleengen *
700e09c00caSUlf Lilleengen * blocknr != 0 means that we're allocating a block that is none
701e09c00caSUlf Lilleengen * of the above. Then, blocknr tells us the number of the block
702e09c00caSUlf Lilleengen * that will hold the pointer
703e09c00caSUlf Lilleengen */
70470097aacSPedro F. Giffuni e4fs_daddr_t
ext2_blkpref(struct inode * ip,e2fs_lbn_t lbn,int indx,e2fs_daddr_t * bap,e2fs_daddr_t blocknr)70570097aacSPedro F. Giffuni ext2_blkpref(struct inode *ip, e2fs_lbn_t lbn, int indx, e2fs_daddr_t *bap,
70670097aacSPedro F. Giffuni e2fs_daddr_t blocknr)
707e09c00caSUlf Lilleengen {
708b394cd1eSFedor Uporov struct m_ext2fs *fs;
709e09c00caSUlf Lilleengen int tmp;
710bf9a211dSPedro F. Giffuni
711b394cd1eSFedor Uporov fs = ip->i_e2fs;
712b394cd1eSFedor Uporov
713e09c00caSUlf Lilleengen mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
714e09c00caSUlf Lilleengen
715bf9a211dSPedro F. Giffuni /*
716bf9a211dSPedro F. Giffuni * If the next block is actually what we thought it is, then set the
717bf9a211dSPedro F. Giffuni * goal to what we thought it should be.
718e09c00caSUlf Lilleengen */
719e09c00caSUlf Lilleengen if (ip->i_next_alloc_block == lbn && ip->i_next_alloc_goal != 0)
720e09c00caSUlf Lilleengen return ip->i_next_alloc_goal;
721e09c00caSUlf Lilleengen
722bf9a211dSPedro F. Giffuni /*
723bf9a211dSPedro F. Giffuni * Now check whether we were provided with an array that basically
724bf9a211dSPedro F. Giffuni * tells us previous blocks to which we want to stay close.
725e09c00caSUlf Lilleengen */
726e09c00caSUlf Lilleengen if (bap)
727e09c00caSUlf Lilleengen for (tmp = indx - 1; tmp >= 0; tmp--)
728e09c00caSUlf Lilleengen if (bap[tmp])
729cd3acfe7SFedor Uporov return (le32toh(bap[tmp]));
730e09c00caSUlf Lilleengen
731bf9a211dSPedro F. Giffuni /*
732bf9a211dSPedro F. Giffuni * Else lets fall back to the blocknr or, if there is none, follow
733bf9a211dSPedro F. Giffuni * the rule that a block should be allocated near its inode.
734e09c00caSUlf Lilleengen */
735b394cd1eSFedor Uporov return (blocknr ? blocknr :
73670097aacSPedro F. Giffuni (e2fs_daddr_t)(ip->i_block_group *
737cd3acfe7SFedor Uporov EXT2_BLOCKS_PER_GROUP(fs)) + le32toh(fs->e2fs->e2fs_first_dblock));
738e09c00caSUlf Lilleengen }
739e09c00caSUlf Lilleengen
740e09c00caSUlf Lilleengen /*
741e09c00caSUlf Lilleengen * Implement the cylinder overflow algorithm.
742e09c00caSUlf Lilleengen *
743e09c00caSUlf Lilleengen * The policy implemented by this algorithm is:
744e09c00caSUlf Lilleengen * 1) allocate the block in its requested cylinder group.
7458ea3cedaSGordon Bergling * 2) quadratically rehash on the cylinder group number.
746e09c00caSUlf Lilleengen * 3) brute force search for a free block.
747e09c00caSUlf Lilleengen */
748ffbde5eaSFedor Uporov static e4fs_daddr_t
ext2_hashalloc(struct inode * ip,int cg,long pref,int size,daddr_t (* allocator)(struct inode *,int,daddr_t,int))749e09c00caSUlf Lilleengen ext2_hashalloc(struct inode *ip, int cg, long pref, int size,
750e09c00caSUlf Lilleengen daddr_t (*allocator) (struct inode *, int, daddr_t, int))
751e09c00caSUlf Lilleengen {
752e09c00caSUlf Lilleengen struct m_ext2fs *fs;
753ffbde5eaSFedor Uporov e4fs_daddr_t result;
754e09c00caSUlf Lilleengen int i, icg = cg;
755e09c00caSUlf Lilleengen
756e09c00caSUlf Lilleengen mtx_assert(EXT2_MTX(ip->i_ump), MA_OWNED);
757e09c00caSUlf Lilleengen fs = ip->i_e2fs;
758e09c00caSUlf Lilleengen /*
759e09c00caSUlf Lilleengen * 1: preferred cylinder group
760e09c00caSUlf Lilleengen */
761e09c00caSUlf Lilleengen result = (*allocator)(ip, cg, pref, size);
762e09c00caSUlf Lilleengen if (result)
763e09c00caSUlf Lilleengen return (result);
764e09c00caSUlf Lilleengen /*
765e09c00caSUlf Lilleengen * 2: quadratic rehash
766e09c00caSUlf Lilleengen */
767e09c00caSUlf Lilleengen for (i = 1; i < fs->e2fs_gcount; i *= 2) {
768e09c00caSUlf Lilleengen cg += i;
769e09c00caSUlf Lilleengen if (cg >= fs->e2fs_gcount)
770e09c00caSUlf Lilleengen cg -= fs->e2fs_gcount;
771e09c00caSUlf Lilleengen result = (*allocator)(ip, cg, 0, size);
772e09c00caSUlf Lilleengen if (result)
773e09c00caSUlf Lilleengen return (result);
774e09c00caSUlf Lilleengen }
775e09c00caSUlf Lilleengen /*
776e09c00caSUlf Lilleengen * 3: brute force search
777e09c00caSUlf Lilleengen * Note that we start at i == 2, since 0 was checked initially,
778e09c00caSUlf Lilleengen * and 1 is always checked in the quadratic rehash.
779e09c00caSUlf Lilleengen */
780e09c00caSUlf Lilleengen cg = (icg + 2) % fs->e2fs_gcount;
781e09c00caSUlf Lilleengen for (i = 2; i < fs->e2fs_gcount; i++) {
782e09c00caSUlf Lilleengen result = (*allocator)(ip, cg, 0, size);
783e09c00caSUlf Lilleengen if (result)
784e09c00caSUlf Lilleengen return (result);
785e09c00caSUlf Lilleengen cg++;
786e09c00caSUlf Lilleengen if (cg == fs->e2fs_gcount)
787e09c00caSUlf Lilleengen cg = 0;
788e09c00caSUlf Lilleengen }
789e09c00caSUlf Lilleengen return (0);
790e09c00caSUlf Lilleengen }
791e09c00caSUlf Lilleengen
7926e38bf94SFedor Uporov static uint64_t
ext2_cg_number_gdb_nometa(struct m_ext2fs * fs,int cg)793c0f16c65SFedor Uporov ext2_cg_number_gdb_nometa(struct m_ext2fs *fs, int cg)
794d23db91eSPedro F. Giffuni {
795d23db91eSPedro F. Giffuni
796d23db91eSPedro F. Giffuni if (!ext2_cg_has_sb(fs, cg))
797d23db91eSPedro F. Giffuni return (0);
798c0f16c65SFedor Uporov
799d23db91eSPedro F. Giffuni if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG))
800cd3acfe7SFedor Uporov return (le32toh(fs->e2fs->e3fs_first_meta_bg));
801c0f16c65SFedor Uporov
802c0f16c65SFedor Uporov return ((fs->e2fs_gcount + EXT2_DESCS_PER_BLOCK(fs) - 1) /
803c0f16c65SFedor Uporov EXT2_DESCS_PER_BLOCK(fs));
804d23db91eSPedro F. Giffuni }
805d23db91eSPedro F. Giffuni
8066e38bf94SFedor Uporov static uint64_t
ext2_cg_number_gdb_meta(struct m_ext2fs * fs,int cg)807c0f16c65SFedor Uporov ext2_cg_number_gdb_meta(struct m_ext2fs *fs, int cg)
808c0f16c65SFedor Uporov {
809c0f16c65SFedor Uporov unsigned long metagroup;
810c0f16c65SFedor Uporov int first, last;
811c0f16c65SFedor Uporov
812c0f16c65SFedor Uporov metagroup = cg / EXT2_DESCS_PER_BLOCK(fs);
813c0f16c65SFedor Uporov first = metagroup * EXT2_DESCS_PER_BLOCK(fs);
814c0f16c65SFedor Uporov last = first + EXT2_DESCS_PER_BLOCK(fs) - 1;
815c0f16c65SFedor Uporov
816d23db91eSPedro F. Giffuni if (cg == first || cg == first + 1 || cg == last)
817d23db91eSPedro F. Giffuni return (1);
818d23db91eSPedro F. Giffuni
819c0f16c65SFedor Uporov return (0);
820c0f16c65SFedor Uporov }
821c0f16c65SFedor Uporov
8226e38bf94SFedor Uporov uint64_t
ext2_cg_number_gdb(struct m_ext2fs * fs,int cg)823c0f16c65SFedor Uporov ext2_cg_number_gdb(struct m_ext2fs *fs, int cg)
824c0f16c65SFedor Uporov {
825c0f16c65SFedor Uporov unsigned long first_meta_bg, metagroup;
826c0f16c65SFedor Uporov
827cd3acfe7SFedor Uporov first_meta_bg = le32toh(fs->e2fs->e3fs_first_meta_bg);
828c0f16c65SFedor Uporov metagroup = cg / EXT2_DESCS_PER_BLOCK(fs);
829c0f16c65SFedor Uporov
830c0f16c65SFedor Uporov if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) ||
831c0f16c65SFedor Uporov metagroup < first_meta_bg)
832c0f16c65SFedor Uporov return (ext2_cg_number_gdb_nometa(fs, cg));
833c0f16c65SFedor Uporov
834c0f16c65SFedor Uporov return ext2_cg_number_gdb_meta(fs, cg);
835d23db91eSPedro F. Giffuni }
836d23db91eSPedro F. Giffuni
837d23db91eSPedro F. Giffuni static int
ext2_number_base_meta_blocks(struct m_ext2fs * fs,int cg)838c0f16c65SFedor Uporov ext2_number_base_meta_blocks(struct m_ext2fs *fs, int cg)
839d23db91eSPedro F. Giffuni {
840c0f16c65SFedor Uporov int number;
841d23db91eSPedro F. Giffuni
842c0f16c65SFedor Uporov number = ext2_cg_has_sb(fs, cg);
843d23db91eSPedro F. Giffuni
844d23db91eSPedro F. Giffuni if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_META_BG) ||
845cd3acfe7SFedor Uporov cg < le32toh(fs->e2fs->e3fs_first_meta_bg) *
846cd3acfe7SFedor Uporov EXT2_DESCS_PER_BLOCK(fs)) {
847c0f16c65SFedor Uporov if (number) {
848c0f16c65SFedor Uporov number += ext2_cg_number_gdb(fs, cg);
849cd3acfe7SFedor Uporov number += le16toh(fs->e2fs->e2fs_reserved_ngdb);
850d23db91eSPedro F. Giffuni }
851d23db91eSPedro F. Giffuni } else {
852c0f16c65SFedor Uporov number += ext2_cg_number_gdb(fs, cg);
853d23db91eSPedro F. Giffuni }
854d23db91eSPedro F. Giffuni
855c0f16c65SFedor Uporov return (number);
856d23db91eSPedro F. Giffuni }
857d23db91eSPedro F. Giffuni
858d23db91eSPedro F. Giffuni static void
ext2_mark_bitmap_end(int start_bit,int end_bit,char * bitmap)859d23db91eSPedro F. Giffuni ext2_mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
860d23db91eSPedro F. Giffuni {
861d23db91eSPedro F. Giffuni int i;
862d23db91eSPedro F. Giffuni
863d23db91eSPedro F. Giffuni if (start_bit >= end_bit)
864d23db91eSPedro F. Giffuni return;
865d23db91eSPedro F. Giffuni
866d23db91eSPedro F. Giffuni for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
867d23db91eSPedro F. Giffuni setbit(bitmap, i);
868d23db91eSPedro F. Giffuni if (i < end_bit)
869d23db91eSPedro F. Giffuni memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
870d23db91eSPedro F. Giffuni }
871d23db91eSPedro F. Giffuni
872d23db91eSPedro F. Giffuni static int
ext2_get_group_number(struct m_ext2fs * fs,e4fs_daddr_t block)873c0f16c65SFedor Uporov ext2_get_group_number(struct m_ext2fs *fs, e4fs_daddr_t block)
874c0f16c65SFedor Uporov {
875c0f16c65SFedor Uporov
876cd3acfe7SFedor Uporov return ((block - le32toh(fs->e2fs->e2fs_first_dblock)) /
877cd3acfe7SFedor Uporov fs->e2fs_bsize);
878c0f16c65SFedor Uporov }
879c0f16c65SFedor Uporov
880c0f16c65SFedor Uporov static int
ext2_block_in_group(struct m_ext2fs * fs,e4fs_daddr_t block,int cg)881c0f16c65SFedor Uporov ext2_block_in_group(struct m_ext2fs *fs, e4fs_daddr_t block, int cg)
882c0f16c65SFedor Uporov {
883c0f16c65SFedor Uporov
884c0f16c65SFedor Uporov return ((ext2_get_group_number(fs, block) == cg) ? 1 : 0);
885c0f16c65SFedor Uporov }
886c0f16c65SFedor Uporov
887c0f16c65SFedor Uporov static int
ext2_cg_block_bitmap_init(struct m_ext2fs * fs,int cg,struct buf * bp)888d23db91eSPedro F. Giffuni ext2_cg_block_bitmap_init(struct m_ext2fs *fs, int cg, struct buf *bp)
889d23db91eSPedro F. Giffuni {
890d23db91eSPedro F. Giffuni int bit, bit_max, inodes_per_block;
8913acd9182SFedor Uporov uint64_t start, tmp;
892d23db91eSPedro F. Giffuni
893cd3acfe7SFedor Uporov if (!(le16toh(fs->e2fs_gd[cg].ext4bgd_flags) & EXT2_BG_BLOCK_UNINIT))
894d23db91eSPedro F. Giffuni return (0);
895d23db91eSPedro F. Giffuni
896d23db91eSPedro F. Giffuni memset(bp->b_data, 0, fs->e2fs_bsize);
897d23db91eSPedro F. Giffuni
898c0f16c65SFedor Uporov bit_max = ext2_number_base_meta_blocks(fs, cg);
899d23db91eSPedro F. Giffuni if ((bit_max >> 3) >= fs->e2fs_bsize)
900d23db91eSPedro F. Giffuni return (EINVAL);
901d23db91eSPedro F. Giffuni
902d23db91eSPedro F. Giffuni for (bit = 0; bit < bit_max; bit++)
903d23db91eSPedro F. Giffuni setbit(bp->b_data, bit);
904d23db91eSPedro F. Giffuni
905cd3acfe7SFedor Uporov start = (uint64_t)cg * fs->e2fs_bpg +
906cd3acfe7SFedor Uporov le32toh(fs->e2fs->e2fs_first_dblock);
907d23db91eSPedro F. Giffuni
9083acd9182SFedor Uporov /* Set bits for block and inode bitmaps, and inode table. */
9093acd9182SFedor Uporov tmp = e2fs_gd_get_b_bitmap(&fs->e2fs_gd[cg]);
910d23db91eSPedro F. Giffuni if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) ||
911c0f16c65SFedor Uporov ext2_block_in_group(fs, tmp, cg))
912d23db91eSPedro F. Giffuni setbit(bp->b_data, tmp - start);
913d23db91eSPedro F. Giffuni
9143acd9182SFedor Uporov tmp = e2fs_gd_get_i_bitmap(&fs->e2fs_gd[cg]);
915d23db91eSPedro F. Giffuni if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) ||
916c0f16c65SFedor Uporov ext2_block_in_group(fs, tmp, cg))
917d23db91eSPedro F. Giffuni setbit(bp->b_data, tmp - start);
918d23db91eSPedro F. Giffuni
9193acd9182SFedor Uporov tmp = e2fs_gd_get_i_tables(&fs->e2fs_gd[cg]);
920d23db91eSPedro F. Giffuni inodes_per_block = fs->e2fs_bsize/EXT2_INODE_SIZE(fs);
9213acd9182SFedor Uporov while( tmp < e2fs_gd_get_i_tables(&fs->e2fs_gd[cg]) +
922cd3acfe7SFedor Uporov fs->e2fs_ipg / inodes_per_block ) {
923d23db91eSPedro F. Giffuni if (!EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG) ||
924c0f16c65SFedor Uporov ext2_block_in_group(fs, tmp, cg))
925d23db91eSPedro F. Giffuni setbit(bp->b_data, tmp - start);
926d23db91eSPedro F. Giffuni tmp++;
927d23db91eSPedro F. Giffuni }
928d23db91eSPedro F. Giffuni
929d23db91eSPedro F. Giffuni /*
930d23db91eSPedro F. Giffuni * Also if the number of blocks within the group is less than
931d23db91eSPedro F. Giffuni * the blocksize * 8 ( which is the size of bitmap ), set rest
932d23db91eSPedro F. Giffuni * of the block bitmap to 1
933d23db91eSPedro F. Giffuni */
934cd3acfe7SFedor Uporov ext2_mark_bitmap_end(fs->e2fs_bpg, fs->e2fs_bsize * 8,
935d23db91eSPedro F. Giffuni bp->b_data);
936d23db91eSPedro F. Giffuni
937d23db91eSPedro F. Giffuni /* Clean the flag */
938cd3acfe7SFedor Uporov fs->e2fs_gd[cg].ext4bgd_flags = htole16(le16toh(
939cd3acfe7SFedor Uporov fs->e2fs_gd[cg].ext4bgd_flags) & ~EXT2_BG_BLOCK_UNINIT);
940d23db91eSPedro F. Giffuni
941d23db91eSPedro F. Giffuni return (0);
942d23db91eSPedro F. Giffuni }
943d23db91eSPedro F. Giffuni
94480a4a971SFedor Uporov static int
ext2_b_bitmap_validate(struct m_ext2fs * fs,struct buf * bp,int cg)94580a4a971SFedor Uporov ext2_b_bitmap_validate(struct m_ext2fs *fs, struct buf *bp, int cg)
94680a4a971SFedor Uporov {
94780a4a971SFedor Uporov struct ext2_gd *gd;
94880a4a971SFedor Uporov uint64_t group_first_block;
94980a4a971SFedor Uporov unsigned int offset, max_bit;
95080a4a971SFedor Uporov
95180a4a971SFedor Uporov if (EXT2_HAS_INCOMPAT_FEATURE(fs, EXT2F_INCOMPAT_FLEX_BG)) {
95280a4a971SFedor Uporov /*
953cd3acfe7SFedor Uporov * It is not possible to check block bitmap in case of this
954cd3acfe7SFedor Uporov * feature, because the inode and block bitmaps and inode table
95580a4a971SFedor Uporov * blocks may not be in the group at all.
95680a4a971SFedor Uporov * So, skip check in this case.
95780a4a971SFedor Uporov */
95880a4a971SFedor Uporov return (0);
95980a4a971SFedor Uporov }
96080a4a971SFedor Uporov
96180a4a971SFedor Uporov gd = &fs->e2fs_gd[cg];
96280a4a971SFedor Uporov max_bit = fs->e2fs_fpg;
963cd3acfe7SFedor Uporov group_first_block = ((uint64_t)cg) * fs->e2fs_fpg +
964cd3acfe7SFedor Uporov le32toh(fs->e2fs->e2fs_first_dblock);
96580a4a971SFedor Uporov
96680a4a971SFedor Uporov /* Check block bitmap block number */
96780a4a971SFedor Uporov offset = e2fs_gd_get_b_bitmap(gd) - group_first_block;
96880a4a971SFedor Uporov if (offset >= max_bit || !isset(bp->b_data, offset)) {
969ebc94b66SFedor Uporov SDT_PROBE2(ext2fs, , alloc, ext2_b_bitmap_validate_error,
970ebc94b66SFedor Uporov "bad block bitmap, group", cg);
97180a4a971SFedor Uporov return (EINVAL);
97280a4a971SFedor Uporov }
97380a4a971SFedor Uporov
97480a4a971SFedor Uporov /* Check inode bitmap block number */
97580a4a971SFedor Uporov offset = e2fs_gd_get_i_bitmap(gd) - group_first_block;
97680a4a971SFedor Uporov if (offset >= max_bit || !isset(bp->b_data, offset)) {
977ebc94b66SFedor Uporov SDT_PROBE2(ext2fs, , alloc, ext2_b_bitmap_validate_error,
978ebc94b66SFedor Uporov "bad inode bitmap", cg);
97980a4a971SFedor Uporov return (EINVAL);
98080a4a971SFedor Uporov }
98180a4a971SFedor Uporov
98280a4a971SFedor Uporov /* Check inode table */
98380a4a971SFedor Uporov offset = e2fs_gd_get_i_tables(gd) - group_first_block;
98480a4a971SFedor Uporov if (offset >= max_bit || offset + fs->e2fs_itpg >= max_bit) {
985ebc94b66SFedor Uporov SDT_PROBE2(ext2fs, , alloc, ext2_b_bitmap_validate_error,
986ebc94b66SFedor Uporov "bad inode table, group", cg);
98780a4a971SFedor Uporov return (EINVAL);
98880a4a971SFedor Uporov }
98980a4a971SFedor Uporov
99080a4a971SFedor Uporov return (0);
99180a4a971SFedor Uporov }
99280a4a971SFedor Uporov
993e09c00caSUlf Lilleengen /*
994e09c00caSUlf Lilleengen * Determine whether a block can be allocated.
995e09c00caSUlf Lilleengen *
996e09c00caSUlf Lilleengen * Check to see if a block of the appropriate size is available,
997e09c00caSUlf Lilleengen * and if it is, allocate it.
998e09c00caSUlf Lilleengen */
999e09c00caSUlf Lilleengen static daddr_t
ext2_alloccg(struct inode * ip,int cg,daddr_t bpref,int size)1000e09c00caSUlf Lilleengen ext2_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
1001e09c00caSUlf Lilleengen {
1002e09c00caSUlf Lilleengen struct m_ext2fs *fs;
1003e09c00caSUlf Lilleengen struct buf *bp;
1004e09c00caSUlf Lilleengen struct ext2mount *ump;
1005c767faa5SJohn Baldwin daddr_t bno, runstart, runlen;
1006c767faa5SJohn Baldwin int bit, loc, end, error, start;
1007e09c00caSUlf Lilleengen char *bbp;
1008e09c00caSUlf Lilleengen /* XXX ondisk32 */
1009e09c00caSUlf Lilleengen fs = ip->i_e2fs;
1010e09c00caSUlf Lilleengen ump = ip->i_ump;
10113acd9182SFedor Uporov if (e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) == 0)
1012e09c00caSUlf Lilleengen return (0);
101380a4a971SFedor Uporov
1014e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
1015e09c00caSUlf Lilleengen error = bread(ip->i_devvp, fsbtodb(fs,
10163acd9182SFedor Uporov e2fs_gd_get_b_bitmap(&fs->e2fs_gd[cg])),
1017e09c00caSUlf Lilleengen (int)fs->e2fs_bsize, NOCRED, &bp);
101880a4a971SFedor Uporov if (error)
101980a4a971SFedor Uporov goto fail;
102080a4a971SFedor Uporov
1021512f29d1SFedor Uporov if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1022512f29d1SFedor Uporov EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
1023d23db91eSPedro F. Giffuni error = ext2_cg_block_bitmap_init(fs, cg, bp);
102480a4a971SFedor Uporov if (error)
102580a4a971SFedor Uporov goto fail;
102680a4a971SFedor Uporov
1027512f29d1SFedor Uporov ext2_gd_b_bitmap_csum_set(fs, cg, bp);
1028512f29d1SFedor Uporov }
1029512f29d1SFedor Uporov error = ext2_gd_b_bitmap_csum_verify(fs, cg, bp);
103080a4a971SFedor Uporov if (error)
103180a4a971SFedor Uporov goto fail;
103280a4a971SFedor Uporov
103380a4a971SFedor Uporov error = ext2_b_bitmap_validate(fs,bp, cg);
103480a4a971SFedor Uporov if (error)
103580a4a971SFedor Uporov goto fail;
103680a4a971SFedor Uporov
103773dd6d1fSJohn Baldwin /*
1038cd3acfe7SFedor Uporov * Check, that another thread did not not allocate the last block in
1039cd3acfe7SFedor Uporov * this group while we were waiting for the buffer.
104073dd6d1fSJohn Baldwin */
104180a4a971SFedor Uporov if (e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) == 0)
104280a4a971SFedor Uporov goto fail;
104380a4a971SFedor Uporov
1044e09c00caSUlf Lilleengen bbp = (char *)bp->b_data;
1045e09c00caSUlf Lilleengen
1046e09c00caSUlf Lilleengen if (dtog(fs, bpref) != cg)
1047e09c00caSUlf Lilleengen bpref = 0;
1048e09c00caSUlf Lilleengen if (bpref != 0) {
1049e09c00caSUlf Lilleengen bpref = dtogd(fs, bpref);
1050e09c00caSUlf Lilleengen /*
1051e09c00caSUlf Lilleengen * if the requested block is available, use it
1052e09c00caSUlf Lilleengen */
1053e09c00caSUlf Lilleengen if (isclr(bbp, bpref)) {
1054e09c00caSUlf Lilleengen bno = bpref;
1055e09c00caSUlf Lilleengen goto gotit;
1056e09c00caSUlf Lilleengen }
1057e09c00caSUlf Lilleengen }
1058e09c00caSUlf Lilleengen /*
1059e09c00caSUlf Lilleengen * no blocks in the requested cylinder, so take next
1060e09c00caSUlf Lilleengen * available one in this cylinder group.
1061e09c00caSUlf Lilleengen * first try to get 8 contigous blocks, then fall back to a single
1062e09c00caSUlf Lilleengen * block.
1063e09c00caSUlf Lilleengen */
1064e09c00caSUlf Lilleengen if (bpref)
1065e09c00caSUlf Lilleengen start = dtogd(fs, bpref) / NBBY;
1066e09c00caSUlf Lilleengen else
1067e09c00caSUlf Lilleengen start = 0;
1068*344243fcSFedor Uporov end = howmany(fs->e2fs_fpg, NBBY);
1069c767faa5SJohn Baldwin retry:
1070c767faa5SJohn Baldwin runlen = 0;
1071c767faa5SJohn Baldwin runstart = 0;
1072e09c00caSUlf Lilleengen for (loc = start; loc < end; loc++) {
1073c767faa5SJohn Baldwin if (bbp[loc] == (char)0xff) {
1074c767faa5SJohn Baldwin runlen = 0;
1075c767faa5SJohn Baldwin continue;
1076c767faa5SJohn Baldwin }
1077c767faa5SJohn Baldwin
1078c767faa5SJohn Baldwin /* Start of a run, find the number of high clear bits. */
1079c767faa5SJohn Baldwin if (runlen == 0) {
1080c767faa5SJohn Baldwin bit = fls(bbp[loc]);
1081c767faa5SJohn Baldwin runlen = NBBY - bit;
1082c767faa5SJohn Baldwin runstart = loc * NBBY + bit;
1083c767faa5SJohn Baldwin } else if (bbp[loc] == 0) {
1084c767faa5SJohn Baldwin /* Continue a run. */
1085c767faa5SJohn Baldwin runlen += NBBY;
1086c767faa5SJohn Baldwin } else {
1087c767faa5SJohn Baldwin /*
1088c767faa5SJohn Baldwin * Finish the current run. If it isn't long
1089c767faa5SJohn Baldwin * enough, start a new one.
1090c767faa5SJohn Baldwin */
1091c767faa5SJohn Baldwin bit = ffs(bbp[loc]) - 1;
1092c767faa5SJohn Baldwin runlen += bit;
1093c767faa5SJohn Baldwin if (runlen >= 8) {
1094c767faa5SJohn Baldwin bno = runstart;
1095c767faa5SJohn Baldwin goto gotit;
1096c767faa5SJohn Baldwin }
1097c767faa5SJohn Baldwin
1098c767faa5SJohn Baldwin /* Run was too short, start a new one. */
1099c767faa5SJohn Baldwin bit = fls(bbp[loc]);
1100c767faa5SJohn Baldwin runlen = NBBY - bit;
1101c767faa5SJohn Baldwin runstart = loc * NBBY + bit;
1102c767faa5SJohn Baldwin }
1103c767faa5SJohn Baldwin
1104c767faa5SJohn Baldwin /* If the current run is long enough, use it. */
1105c767faa5SJohn Baldwin if (runlen >= 8) {
1106c767faa5SJohn Baldwin bno = runstart;
1107e09c00caSUlf Lilleengen goto gotit;
1108e09c00caSUlf Lilleengen }
1109e09c00caSUlf Lilleengen }
1110c767faa5SJohn Baldwin if (start != 0) {
1111c767faa5SJohn Baldwin end = start;
1112c767faa5SJohn Baldwin start = 0;
1113c767faa5SJohn Baldwin goto retry;
1114e09c00caSUlf Lilleengen }
1115e09c00caSUlf Lilleengen bno = ext2_mapsearch(fs, bbp, bpref);
111680a4a971SFedor Uporov if (bno < 0)
111780a4a971SFedor Uporov goto fail;
111880a4a971SFedor Uporov
1119e09c00caSUlf Lilleengen gotit:
112077b193c2SPedro F. Giffuni #ifdef INVARIANTS
11218e42a406SJohn Baldwin if (isset(bbp, bno)) {
11228e42a406SJohn Baldwin printf("ext2fs_alloccgblk: cg=%d bno=%jd fs=%s\n",
11238e42a406SJohn Baldwin cg, (intmax_t)bno, fs->e2fs_fsmnt);
1124e09c00caSUlf Lilleengen panic("ext2fs_alloccg: dup alloc");
1125e09c00caSUlf Lilleengen }
1126e09c00caSUlf Lilleengen #endif
11278e42a406SJohn Baldwin setbit(bbp, bno);
1128e09c00caSUlf Lilleengen EXT2_LOCK(ump);
11295b63c125SPedro F. Giffuni ext2_clusteracct(fs, bbp, cg, bno, -1);
11303acd9182SFedor Uporov fs->e2fs_fbcount--;
11313acd9182SFedor Uporov e2fs_gd_set_nbfree(&fs->e2fs_gd[cg],
11323acd9182SFedor Uporov e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) - 1);
1133e09c00caSUlf Lilleengen fs->e2fs_fmod = 1;
1134e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
1135512f29d1SFedor Uporov ext2_gd_b_bitmap_csum_set(fs, cg, bp);
1136e09c00caSUlf Lilleengen bdwrite(bp);
1137cd3acfe7SFedor Uporov return (((uint64_t)cg) * fs->e2fs_fpg +
1138cd3acfe7SFedor Uporov le32toh(fs->e2fs->e2fs_first_dblock) + bno);
113980a4a971SFedor Uporov
114080a4a971SFedor Uporov fail:
114180a4a971SFedor Uporov brelse(bp);
114280a4a971SFedor Uporov EXT2_LOCK(ump);
114380a4a971SFedor Uporov return (0);
1144e09c00caSUlf Lilleengen }
1145e09c00caSUlf Lilleengen
1146e09c00caSUlf Lilleengen /*
11475b63c125SPedro F. Giffuni * Determine whether a cluster can be allocated.
11485b63c125SPedro F. Giffuni */
11495b63c125SPedro F. Giffuni static daddr_t
ext2_clusteralloc(struct inode * ip,int cg,daddr_t bpref,int len)11505b63c125SPedro F. Giffuni ext2_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
11515b63c125SPedro F. Giffuni {
11525b63c125SPedro F. Giffuni struct m_ext2fs *fs;
11535b63c125SPedro F. Giffuni struct ext2mount *ump;
11545b63c125SPedro F. Giffuni struct buf *bp;
11555b63c125SPedro F. Giffuni char *bbp;
11565b63c125SPedro F. Giffuni int bit, error, got, i, loc, run;
11575b63c125SPedro F. Giffuni int32_t *lp;
11585b63c125SPedro F. Giffuni daddr_t bno;
11595b63c125SPedro F. Giffuni
11605b63c125SPedro F. Giffuni fs = ip->i_e2fs;
11615b63c125SPedro F. Giffuni ump = ip->i_ump;
11625b63c125SPedro F. Giffuni
11635b63c125SPedro F. Giffuni if (fs->e2fs_maxcluster[cg] < len)
11645b63c125SPedro F. Giffuni return (0);
11655b63c125SPedro F. Giffuni
11665b63c125SPedro F. Giffuni EXT2_UNLOCK(ump);
11675b63c125SPedro F. Giffuni error = bread(ip->i_devvp,
11683acd9182SFedor Uporov fsbtodb(fs, e2fs_gd_get_b_bitmap(&fs->e2fs_gd[cg])),
11695b63c125SPedro F. Giffuni (int)fs->e2fs_bsize, NOCRED, &bp);
11705b63c125SPedro F. Giffuni if (error)
11715b63c125SPedro F. Giffuni goto fail_lock;
11725b63c125SPedro F. Giffuni
11735b63c125SPedro F. Giffuni bbp = (char *)bp->b_data;
11745b63c125SPedro F. Giffuni EXT2_LOCK(ump);
11755b63c125SPedro F. Giffuni /*
11765b63c125SPedro F. Giffuni * Check to see if a cluster of the needed size (or bigger) is
11775b63c125SPedro F. Giffuni * available in this cylinder group.
11785b63c125SPedro F. Giffuni */
11795b63c125SPedro F. Giffuni lp = &fs->e2fs_clustersum[cg].cs_sum[len];
11805b63c125SPedro F. Giffuni for (i = len; i <= fs->e2fs_contigsumsize; i++)
11815b63c125SPedro F. Giffuni if (*lp++ > 0)
11825b63c125SPedro F. Giffuni break;
11835b63c125SPedro F. Giffuni if (i > fs->e2fs_contigsumsize) {
11845b63c125SPedro F. Giffuni /*
11855b63c125SPedro F. Giffuni * Update the cluster summary information to reflect
11865b63c125SPedro F. Giffuni * the true maximum-sized cluster so that future cluster
11875b63c125SPedro F. Giffuni * allocation requests can avoid reading the bitmap only
11885b63c125SPedro F. Giffuni * to find no cluster.
11895b63c125SPedro F. Giffuni */
11905b63c125SPedro F. Giffuni lp = &fs->e2fs_clustersum[cg].cs_sum[len - 1];
11915b63c125SPedro F. Giffuni for (i = len - 1; i > 0; i--)
11925b63c125SPedro F. Giffuni if (*lp-- > 0)
11935b63c125SPedro F. Giffuni break;
11945b63c125SPedro F. Giffuni fs->e2fs_maxcluster[cg] = i;
11955b63c125SPedro F. Giffuni goto fail;
11965b63c125SPedro F. Giffuni }
11975b63c125SPedro F. Giffuni EXT2_UNLOCK(ump);
11985b63c125SPedro F. Giffuni
11995b63c125SPedro F. Giffuni /* Search the bitmap to find a big enough cluster like in FFS. */
12005b63c125SPedro F. Giffuni if (dtog(fs, bpref) != cg)
12015b63c125SPedro F. Giffuni bpref = 0;
12025b63c125SPedro F. Giffuni if (bpref != 0)
12035b63c125SPedro F. Giffuni bpref = dtogd(fs, bpref);
12045b63c125SPedro F. Giffuni loc = bpref / NBBY;
12055b63c125SPedro F. Giffuni bit = 1 << (bpref % NBBY);
1206cd3acfe7SFedor Uporov for (run = 0, got = bpref; got < fs->e2fs_fpg; got++) {
12075b63c125SPedro F. Giffuni if ((bbp[loc] & bit) != 0)
12085b63c125SPedro F. Giffuni run = 0;
12095b63c125SPedro F. Giffuni else {
12105b63c125SPedro F. Giffuni run++;
12115b63c125SPedro F. Giffuni if (run == len)
12125b63c125SPedro F. Giffuni break;
12135b63c125SPedro F. Giffuni }
12145b63c125SPedro F. Giffuni if ((got & (NBBY - 1)) != (NBBY - 1))
12155b63c125SPedro F. Giffuni bit <<= 1;
12165b63c125SPedro F. Giffuni else {
12175b63c125SPedro F. Giffuni loc++;
12185b63c125SPedro F. Giffuni bit = 1;
12195b63c125SPedro F. Giffuni }
12205b63c125SPedro F. Giffuni }
12215b63c125SPedro F. Giffuni
1222cd3acfe7SFedor Uporov if (got >= fs->e2fs_fpg)
12235b63c125SPedro F. Giffuni goto fail_lock;
12245b63c125SPedro F. Giffuni
12255b63c125SPedro F. Giffuni /* Allocate the cluster that we found. */
12265b63c125SPedro F. Giffuni for (i = 1; i < len; i++)
12275b63c125SPedro F. Giffuni if (!isclr(bbp, got - run + i))
12285b63c125SPedro F. Giffuni panic("ext2_clusteralloc: map mismatch");
12295b63c125SPedro F. Giffuni
12305b63c125SPedro F. Giffuni bno = got - run + 1;
1231cd3acfe7SFedor Uporov if (bno >= fs->e2fs_fpg)
12325b63c125SPedro F. Giffuni panic("ext2_clusteralloc: allocated out of group");
12335b63c125SPedro F. Giffuni
12345b63c125SPedro F. Giffuni EXT2_LOCK(ump);
12355b63c125SPedro F. Giffuni for (i = 0; i < len; i += fs->e2fs_fpb) {
12365b63c125SPedro F. Giffuni setbit(bbp, bno + i);
12375b63c125SPedro F. Giffuni ext2_clusteracct(fs, bbp, cg, bno + i, -1);
12383acd9182SFedor Uporov fs->e2fs_fbcount--;
12393acd9182SFedor Uporov e2fs_gd_set_nbfree(&fs->e2fs_gd[cg],
12403acd9182SFedor Uporov e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) - 1);
12415b63c125SPedro F. Giffuni }
12425b63c125SPedro F. Giffuni fs->e2fs_fmod = 1;
12435b63c125SPedro F. Giffuni EXT2_UNLOCK(ump);
12445b63c125SPedro F. Giffuni
12455b63c125SPedro F. Giffuni bdwrite(bp);
1246cd3acfe7SFedor Uporov return (cg * fs->e2fs_fpg + le32toh(fs->e2fs->e2fs_first_dblock)
1247cd3acfe7SFedor Uporov + bno);
12485b63c125SPedro F. Giffuni
12495b63c125SPedro F. Giffuni fail_lock:
12505b63c125SPedro F. Giffuni EXT2_LOCK(ump);
12515b63c125SPedro F. Giffuni fail:
12525b63c125SPedro F. Giffuni brelse(bp);
12535b63c125SPedro F. Giffuni return (0);
12545b63c125SPedro F. Giffuni }
12555b63c125SPedro F. Giffuni
1256d23db91eSPedro F. Giffuni static int
ext2_zero_inode_table(struct inode * ip,int cg)1257d23db91eSPedro F. Giffuni ext2_zero_inode_table(struct inode *ip, int cg)
1258d23db91eSPedro F. Giffuni {
1259d23db91eSPedro F. Giffuni struct m_ext2fs *fs;
1260d23db91eSPedro F. Giffuni struct buf *bp;
1261d23db91eSPedro F. Giffuni int i, all_blks, used_blks;
1262d23db91eSPedro F. Giffuni
1263d23db91eSPedro F. Giffuni fs = ip->i_e2fs;
1264d23db91eSPedro F. Giffuni
1265cd3acfe7SFedor Uporov if (le16toh(fs->e2fs_gd[cg].ext4bgd_flags) & EXT2_BG_INODE_ZEROED)
1266d23db91eSPedro F. Giffuni return (0);
1267d23db91eSPedro F. Giffuni
1268cd3acfe7SFedor Uporov all_blks = le16toh(fs->e2fs->e2fs_inode_size) * fs->e2fs_ipg /
1269d23db91eSPedro F. Giffuni fs->e2fs_bsize;
1270d23db91eSPedro F. Giffuni
1271cd3acfe7SFedor Uporov used_blks = howmany(fs->e2fs_ipg -
12723acd9182SFedor Uporov e2fs_gd_get_i_unused(&fs->e2fs_gd[cg]),
1273d23db91eSPedro F. Giffuni fs->e2fs_bsize / EXT2_INODE_SIZE(fs));
1274d23db91eSPedro F. Giffuni
1275d23db91eSPedro F. Giffuni for (i = 0; i < all_blks - used_blks; i++) {
1276d23db91eSPedro F. Giffuni bp = getblk(ip->i_devvp, fsbtodb(fs,
12773acd9182SFedor Uporov e2fs_gd_get_i_tables(&fs->e2fs_gd[cg]) + used_blks + i),
1278d23db91eSPedro F. Giffuni fs->e2fs_bsize, 0, 0, 0);
1279d23db91eSPedro F. Giffuni if (!bp)
1280d23db91eSPedro F. Giffuni return (EIO);
1281d23db91eSPedro F. Giffuni
1282d23db91eSPedro F. Giffuni vfs_bio_bzero_buf(bp, 0, fs->e2fs_bsize);
1283d23db91eSPedro F. Giffuni bawrite(bp);
1284d23db91eSPedro F. Giffuni }
1285d23db91eSPedro F. Giffuni
1286cd3acfe7SFedor Uporov fs->e2fs_gd[cg].ext4bgd_flags = htole16(le16toh(
1287cd3acfe7SFedor Uporov fs->e2fs_gd[cg].ext4bgd_flags) | EXT2_BG_INODE_ZEROED);
1288d23db91eSPedro F. Giffuni
1289d23db91eSPedro F. Giffuni return (0);
1290d23db91eSPedro F. Giffuni }
1291d23db91eSPedro F. Giffuni
1292ec81c9ccSFedor Uporov static void
ext2_fix_bitmap_tail(unsigned char * bitmap,int first,int last)1293ec81c9ccSFedor Uporov ext2_fix_bitmap_tail(unsigned char *bitmap, int first, int last)
1294ec81c9ccSFedor Uporov {
1295ec81c9ccSFedor Uporov int i;
1296ec81c9ccSFedor Uporov
1297ec81c9ccSFedor Uporov for (i = first; i <= last; i++)
1298ec81c9ccSFedor Uporov bitmap[i] = 0xff;
1299ec81c9ccSFedor Uporov }
1300ec81c9ccSFedor Uporov
13015b63c125SPedro F. Giffuni /*
1302e09c00caSUlf Lilleengen * Determine whether an inode can be allocated.
1303e09c00caSUlf Lilleengen *
1304e09c00caSUlf Lilleengen * Check to see if an inode is available, and if it is,
1305e09c00caSUlf Lilleengen * allocate it using tode in the specified cylinder group.
1306e09c00caSUlf Lilleengen */
1307e09c00caSUlf Lilleengen static daddr_t
ext2_nodealloccg(struct inode * ip,int cg,daddr_t ipref,int mode)1308e09c00caSUlf Lilleengen ext2_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
1309e09c00caSUlf Lilleengen {
1310e09c00caSUlf Lilleengen struct m_ext2fs *fs;
1311e09c00caSUlf Lilleengen struct buf *bp;
1312e09c00caSUlf Lilleengen struct ext2mount *ump;
1313ec81c9ccSFedor Uporov int error, start, len, ifree, ibytes;
13148f8d3027SEd Schouten char *ibp, *loc;
1315bf9a211dSPedro F. Giffuni
1316e09c00caSUlf Lilleengen ipref--; /* to avoid a lot of (ipref -1) */
1317e09c00caSUlf Lilleengen if (ipref == -1)
1318e09c00caSUlf Lilleengen ipref = 0;
1319e09c00caSUlf Lilleengen fs = ip->i_e2fs;
1320e09c00caSUlf Lilleengen ump = ip->i_ump;
13213acd9182SFedor Uporov if (e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) == 0)
1322e09c00caSUlf Lilleengen return (0);
1323e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
1324e09c00caSUlf Lilleengen error = bread(ip->i_devvp, fsbtodb(fs,
13253acd9182SFedor Uporov e2fs_gd_get_i_bitmap(&fs->e2fs_gd[cg])),
1326e09c00caSUlf Lilleengen (int)fs->e2fs_bsize, NOCRED, &bp);
1327e09c00caSUlf Lilleengen if (error) {
1328e09c00caSUlf Lilleengen EXT2_LOCK(ump);
1329e09c00caSUlf Lilleengen return (0);
1330e09c00caSUlf Lilleengen }
1331512f29d1SFedor Uporov if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
1332512f29d1SFedor Uporov EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
1333cd3acfe7SFedor Uporov if (le16toh(fs->e2fs_gd[cg].ext4bgd_flags) &
1334cd3acfe7SFedor Uporov EXT2_BG_INODE_UNINIT) {
1335ec81c9ccSFedor Uporov ibytes = fs->e2fs_ipg / 8;
1336ec81c9ccSFedor Uporov memset(bp->b_data, 0, ibytes - 1);
1337ec81c9ccSFedor Uporov ext2_fix_bitmap_tail(bp->b_data, ibytes,
1338ec81c9ccSFedor Uporov fs->e2fs_bsize - 1);
1339cd3acfe7SFedor Uporov fs->e2fs_gd[cg].ext4bgd_flags = htole16(le16toh(
1340cd3acfe7SFedor Uporov fs->e2fs_gd[cg].ext4bgd_flags) &
1341cd3acfe7SFedor Uporov ~EXT2_BG_INODE_UNINIT);
1342d23db91eSPedro F. Giffuni }
1343512f29d1SFedor Uporov ext2_gd_i_bitmap_csum_set(fs, cg, bp);
1344d23db91eSPedro F. Giffuni error = ext2_zero_inode_table(ip, cg);
1345d23db91eSPedro F. Giffuni if (error) {
1346d23db91eSPedro F. Giffuni brelse(bp);
1347d23db91eSPedro F. Giffuni EXT2_LOCK(ump);
1348d23db91eSPedro F. Giffuni return (0);
1349d23db91eSPedro F. Giffuni }
1350d23db91eSPedro F. Giffuni }
1351512f29d1SFedor Uporov error = ext2_gd_i_bitmap_csum_verify(fs, cg, bp);
1352512f29d1SFedor Uporov if (error) {
1353512f29d1SFedor Uporov brelse(bp);
1354512f29d1SFedor Uporov EXT2_LOCK(ump);
1355512f29d1SFedor Uporov return (0);
1356512f29d1SFedor Uporov }
13573acd9182SFedor Uporov if (e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) == 0) {
135873dd6d1fSJohn Baldwin /*
135973dd6d1fSJohn Baldwin * Another thread allocated the last i-node in this
136073dd6d1fSJohn Baldwin * group while we were waiting for the buffer.
136173dd6d1fSJohn Baldwin */
136273dd6d1fSJohn Baldwin brelse(bp);
136373dd6d1fSJohn Baldwin EXT2_LOCK(ump);
136473dd6d1fSJohn Baldwin return (0);
136573dd6d1fSJohn Baldwin }
1366e09c00caSUlf Lilleengen ibp = (char *)bp->b_data;
1367e09c00caSUlf Lilleengen if (ipref) {
1368cd3acfe7SFedor Uporov ipref %= fs->e2fs_ipg;
1369e09c00caSUlf Lilleengen if (isclr(ibp, ipref))
1370e09c00caSUlf Lilleengen goto gotit;
1371e09c00caSUlf Lilleengen }
1372e09c00caSUlf Lilleengen start = ipref / NBBY;
1373cd3acfe7SFedor Uporov len = howmany(fs->e2fs_ipg - ipref, NBBY);
13748f8d3027SEd Schouten loc = memcchr(&ibp[start], 0xff, len);
13758f8d3027SEd Schouten if (loc == NULL) {
1376e09c00caSUlf Lilleengen len = start + 1;
1377e09c00caSUlf Lilleengen start = 0;
13788f8d3027SEd Schouten loc = memcchr(&ibp[start], 0xff, len);
13798f8d3027SEd Schouten if (loc == NULL) {
1380cd3acfe7SFedor Uporov SDT_PROBE3(ext2fs, , alloc,
1381cd3acfe7SFedor Uporov ext2_nodealloccg_bmap_corrupted, cg, ipref,
1382cd3acfe7SFedor Uporov fs->e2fs_fsmnt);
13834ff6603aSFedor Uporov brelse(bp);
13844ff6603aSFedor Uporov EXT2_LOCK(ump);
13854ff6603aSFedor Uporov return (0);
1386e09c00caSUlf Lilleengen }
1387e09c00caSUlf Lilleengen }
13888f8d3027SEd Schouten ipref = (loc - ibp) * NBBY + ffs(~*loc) - 1;
1389e09c00caSUlf Lilleengen gotit:
1390e09c00caSUlf Lilleengen setbit(ibp, ipref);
1391e09c00caSUlf Lilleengen EXT2_LOCK(ump);
13923acd9182SFedor Uporov e2fs_gd_set_nifree(&fs->e2fs_gd[cg],
13933acd9182SFedor Uporov e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) - 1);
1394512f29d1SFedor Uporov if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
13954c1e1d2bSFedor Uporov EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
1396cd3acfe7SFedor Uporov ifree = fs->e2fs_ipg - e2fs_gd_get_i_unused(&fs->e2fs_gd[cg]);
13974c1e1d2bSFedor Uporov if (ipref + 1 > ifree)
13983acd9182SFedor Uporov e2fs_gd_set_i_unused(&fs->e2fs_gd[cg],
1399cd3acfe7SFedor Uporov fs->e2fs_ipg - (ipref + 1));
14004c1e1d2bSFedor Uporov }
1401cd3acfe7SFedor Uporov fs->e2fs_ficount--;
1402e09c00caSUlf Lilleengen fs->e2fs_fmod = 1;
1403d8ba45e2SEd Maste if ((mode & IFMT) == IFDIR) {
14043acd9182SFedor Uporov e2fs_gd_set_ndirs(&fs->e2fs_gd[cg],
14053acd9182SFedor Uporov e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]) + 1);
1406e09c00caSUlf Lilleengen fs->e2fs_total_dir++;
1407e09c00caSUlf Lilleengen }
1408e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
1409512f29d1SFedor Uporov ext2_gd_i_bitmap_csum_set(fs, cg, bp);
1410e09c00caSUlf Lilleengen bdwrite(bp);
14113acd9182SFedor Uporov return ((uint64_t)cg * fs->e2fs_ipg + ipref + 1);
1412e09c00caSUlf Lilleengen }
1413e09c00caSUlf Lilleengen
1414e09c00caSUlf Lilleengen /*
1415e09c00caSUlf Lilleengen * Free a block or fragment.
1416e09c00caSUlf Lilleengen *
1417e09c00caSUlf Lilleengen */
1418e09c00caSUlf Lilleengen void
ext2_blkfree(struct inode * ip,e4fs_daddr_t bno,long size)141970097aacSPedro F. Giffuni ext2_blkfree(struct inode *ip, e4fs_daddr_t bno, long size)
1420e09c00caSUlf Lilleengen {
1421e09c00caSUlf Lilleengen struct m_ext2fs *fs;
1422e09c00caSUlf Lilleengen struct buf *bp;
1423e09c00caSUlf Lilleengen struct ext2mount *ump;
1424e09c00caSUlf Lilleengen int cg, error;
1425e09c00caSUlf Lilleengen char *bbp;
1426e09c00caSUlf Lilleengen
1427e09c00caSUlf Lilleengen fs = ip->i_e2fs;
1428e09c00caSUlf Lilleengen ump = ip->i_ump;
1429e09c00caSUlf Lilleengen cg = dtog(fs, bno);
14303acd9182SFedor Uporov if (bno >= fs->e2fs_bcount) {
1431cd3acfe7SFedor Uporov SDT_PROBE2(ext2fs, , alloc, ext2_blkfree_bad_block,
1432cd3acfe7SFedor Uporov ip->i_number, bno);
1433e09c00caSUlf Lilleengen return;
1434e09c00caSUlf Lilleengen }
1435e09c00caSUlf Lilleengen error = bread(ip->i_devvp,
14363acd9182SFedor Uporov fsbtodb(fs, e2fs_gd_get_b_bitmap(&fs->e2fs_gd[cg])),
1437e09c00caSUlf Lilleengen (int)fs->e2fs_bsize, NOCRED, &bp);
1438e09c00caSUlf Lilleengen if (error) {
1439e09c00caSUlf Lilleengen return;
1440e09c00caSUlf Lilleengen }
1441e09c00caSUlf Lilleengen bbp = (char *)bp->b_data;
1442e09c00caSUlf Lilleengen bno = dtogd(fs, bno);
1443e09c00caSUlf Lilleengen if (isclr(bbp, bno)) {
1444ebc94b66SFedor Uporov panic("ext2_blkfree: freeing free block %lld, fs=%s",
1445e09c00caSUlf Lilleengen (long long)bno, fs->e2fs_fsmnt);
1446e09c00caSUlf Lilleengen }
1447e09c00caSUlf Lilleengen clrbit(bbp, bno);
1448e09c00caSUlf Lilleengen EXT2_LOCK(ump);
14495b63c125SPedro F. Giffuni ext2_clusteracct(fs, bbp, cg, bno, 1);
14503acd9182SFedor Uporov fs->e2fs_fbcount++;
14513acd9182SFedor Uporov e2fs_gd_set_nbfree(&fs->e2fs_gd[cg],
14523acd9182SFedor Uporov e2fs_gd_get_nbfree(&fs->e2fs_gd[cg]) + 1);
1453e09c00caSUlf Lilleengen fs->e2fs_fmod = 1;
1454e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
1455512f29d1SFedor Uporov ext2_gd_b_bitmap_csum_set(fs, cg, bp);
1456e09c00caSUlf Lilleengen bdwrite(bp);
1457e09c00caSUlf Lilleengen }
1458e09c00caSUlf Lilleengen
1459e09c00caSUlf Lilleengen /*
1460e09c00caSUlf Lilleengen * Free an inode.
1461e09c00caSUlf Lilleengen *
1462e09c00caSUlf Lilleengen */
1463e09c00caSUlf Lilleengen int
ext2_vfree(struct vnode * pvp,ino_t ino,int mode)1464a9d1b299SPedro F. Giffuni ext2_vfree(struct vnode *pvp, ino_t ino, int mode)
1465e09c00caSUlf Lilleengen {
1466e09c00caSUlf Lilleengen struct m_ext2fs *fs;
1467e09c00caSUlf Lilleengen struct inode *pip;
1468e09c00caSUlf Lilleengen struct buf *bp;
1469e09c00caSUlf Lilleengen struct ext2mount *ump;
1470e09c00caSUlf Lilleengen int error, cg;
1471e09c00caSUlf Lilleengen char *ibp;
1472e09c00caSUlf Lilleengen
1473e09c00caSUlf Lilleengen pip = VTOI(pvp);
1474e09c00caSUlf Lilleengen fs = pip->i_e2fs;
1475e09c00caSUlf Lilleengen ump = pip->i_ump;
1476e09c00caSUlf Lilleengen if ((u_int)ino > fs->e2fs_ipg * fs->e2fs_gcount)
1477fc8fdae0SMatthew D Fleming panic("ext2_vfree: range: devvp = %p, ino = %ju, fs = %s",
1478fc8fdae0SMatthew D Fleming pip->i_devvp, (uintmax_t)ino, fs->e2fs_fsmnt);
1479e09c00caSUlf Lilleengen
1480e09c00caSUlf Lilleengen cg = ino_to_cg(fs, ino);
1481e09c00caSUlf Lilleengen error = bread(pip->i_devvp,
14823acd9182SFedor Uporov fsbtodb(fs, e2fs_gd_get_i_bitmap(&fs->e2fs_gd[cg])),
1483e09c00caSUlf Lilleengen (int)fs->e2fs_bsize, NOCRED, &bp);
1484e09c00caSUlf Lilleengen if (error) {
1485e09c00caSUlf Lilleengen return (0);
1486e09c00caSUlf Lilleengen }
1487e09c00caSUlf Lilleengen ibp = (char *)bp->b_data;
1488cd3acfe7SFedor Uporov ino = (ino - 1) % fs->e2fs_ipg;
1489e09c00caSUlf Lilleengen if (isclr(ibp, ino)) {
1490ebc94b66SFedor Uporov SDT_PROBE2(ext2fs, , alloc, ext2_vfree_doublefree,
1491ebc94b66SFedor Uporov fs->e2fs_fsmnt, ino);
1492e09c00caSUlf Lilleengen if (fs->e2fs_ronly == 0)
1493757224cbSPedro F. Giffuni panic("ext2_vfree: freeing free inode");
1494e09c00caSUlf Lilleengen }
1495e09c00caSUlf Lilleengen clrbit(ibp, ino);
1496e09c00caSUlf Lilleengen EXT2_LOCK(ump);
1497cd3acfe7SFedor Uporov fs->e2fs_ficount++;
14983acd9182SFedor Uporov e2fs_gd_set_nifree(&fs->e2fs_gd[cg],
14993acd9182SFedor Uporov e2fs_gd_get_nifree(&fs->e2fs_gd[cg]) + 1);
1500d8ba45e2SEd Maste if ((mode & IFMT) == IFDIR) {
15013acd9182SFedor Uporov e2fs_gd_set_ndirs(&fs->e2fs_gd[cg],
15023acd9182SFedor Uporov e2fs_gd_get_ndirs(&fs->e2fs_gd[cg]) - 1);
1503e09c00caSUlf Lilleengen fs->e2fs_total_dir--;
1504e09c00caSUlf Lilleengen }
1505e09c00caSUlf Lilleengen fs->e2fs_fmod = 1;
1506e09c00caSUlf Lilleengen EXT2_UNLOCK(ump);
1507512f29d1SFedor Uporov ext2_gd_i_bitmap_csum_set(fs, cg, bp);
1508e09c00caSUlf Lilleengen bdwrite(bp);
1509e09c00caSUlf Lilleengen return (0);
1510e09c00caSUlf Lilleengen }
1511e09c00caSUlf Lilleengen
1512e09c00caSUlf Lilleengen /*
1513e09c00caSUlf Lilleengen * Find a block in the specified cylinder group.
1514e09c00caSUlf Lilleengen *
1515e09c00caSUlf Lilleengen * It is a panic if a request is made to find a block if none are
1516e09c00caSUlf Lilleengen * available.
1517e09c00caSUlf Lilleengen */
1518e09c00caSUlf Lilleengen static daddr_t
ext2_mapsearch(struct m_ext2fs * fs,char * bbp,daddr_t bpref)1519e09c00caSUlf Lilleengen ext2_mapsearch(struct m_ext2fs *fs, char *bbp, daddr_t bpref)
1520e09c00caSUlf Lilleengen {
15218f8d3027SEd Schouten char *loc;
15228f8d3027SEd Schouten int start, len;
1523e09c00caSUlf Lilleengen
1524e09c00caSUlf Lilleengen /*
1525e09c00caSUlf Lilleengen * find the fragment by searching through the free block
1526e09c00caSUlf Lilleengen * map for an appropriate bit pattern
1527e09c00caSUlf Lilleengen */
1528e09c00caSUlf Lilleengen if (bpref)
1529e09c00caSUlf Lilleengen start = dtogd(fs, bpref) / NBBY;
1530e09c00caSUlf Lilleengen else
1531e09c00caSUlf Lilleengen start = 0;
1532cd3acfe7SFedor Uporov len = howmany(fs->e2fs_fpg, NBBY) - start;
15338f8d3027SEd Schouten loc = memcchr(&bbp[start], 0xff, len);
15348f8d3027SEd Schouten if (loc == NULL) {
1535e09c00caSUlf Lilleengen len = start + 1;
1536e09c00caSUlf Lilleengen start = 0;
15378f8d3027SEd Schouten loc = memcchr(&bbp[start], 0xff, len);
15388f8d3027SEd Schouten if (loc == NULL) {
1539cd3acfe7SFedor Uporov panic("ext2_mapsearch: map corrupted: start=%d, len=%d,"
1540cd3acfe7SFedor Uporov "fs=%s", start, len, fs->e2fs_fsmnt);
1541e09c00caSUlf Lilleengen /* NOTREACHED */
1542e09c00caSUlf Lilleengen }
1543e09c00caSUlf Lilleengen }
15448f8d3027SEd Schouten return ((loc - bbp) * NBBY + ffs(~*loc) - 1);
1545e09c00caSUlf Lilleengen }
1546e09c00caSUlf Lilleengen
1547e09c00caSUlf Lilleengen int
ext2_cg_has_sb(struct m_ext2fs * fs,int cg)1548d23db91eSPedro F. Giffuni ext2_cg_has_sb(struct m_ext2fs *fs, int cg)
1549e09c00caSUlf Lilleengen {
1550e09c00caSUlf Lilleengen int a3, a5, a7;
1551e09c00caSUlf Lilleengen
1552d23db91eSPedro F. Giffuni if (cg == 0)
1553d23db91eSPedro F. Giffuni return (1);
1554d23db91eSPedro F. Giffuni
1555d23db91eSPedro F. Giffuni if (EXT2_HAS_COMPAT_FEATURE(fs, EXT2F_COMPAT_SPARSESUPER2)) {
1556cd3acfe7SFedor Uporov if (cg == le32toh(fs->e2fs->e4fs_backup_bgs[0]) ||
1557cd3acfe7SFedor Uporov cg == le32toh(fs->e2fs->e4fs_backup_bgs[1]))
1558d23db91eSPedro F. Giffuni return (1);
1559d23db91eSPedro F. Giffuni return (0);
1560d23db91eSPedro F. Giffuni }
1561d23db91eSPedro F. Giffuni
1562d23db91eSPedro F. Giffuni if ((cg <= 1) ||
1563d23db91eSPedro F. Giffuni !EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_SPARSESUPER))
1564d23db91eSPedro F. Giffuni return (1);
1565d23db91eSPedro F. Giffuni
1566d23db91eSPedro F. Giffuni if (!(cg & 1))
1567d23db91eSPedro F. Giffuni return (0);
1568d23db91eSPedro F. Giffuni
1569e09c00caSUlf Lilleengen for (a3 = 3, a5 = 5, a7 = 7;
1570d23db91eSPedro F. Giffuni a3 <= cg || a5 <= cg || a7 <= cg;
1571e09c00caSUlf Lilleengen a3 *= 3, a5 *= 5, a7 *= 7)
1572d23db91eSPedro F. Giffuni if (cg == a3 || cg == a5 || cg == a7)
1573d23db91eSPedro F. Giffuni return (1);
1574d23db91eSPedro F. Giffuni return (0);
1575e09c00caSUlf Lilleengen }
1576