ffs_wapbl.c revision 1.49 1 1.49 msaitoh /* $NetBSD: ffs_wapbl.c,v 1.49 2024/05/13 00:24:19 msaitoh Exp $ */
2 1.2 simonb
3 1.2 simonb /*-
4 1.2 simonb * Copyright (c) 2003,2006,2008 The NetBSD Foundation, Inc.
5 1.2 simonb * All rights reserved.
6 1.2 simonb *
7 1.2 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.2 simonb * by Wasabi Systems, Inc.
9 1.2 simonb *
10 1.2 simonb * Redistribution and use in source and binary forms, with or without
11 1.2 simonb * modification, are permitted provided that the following conditions
12 1.2 simonb * are met:
13 1.2 simonb * 1. Redistributions of source code must retain the above copyright
14 1.2 simonb * notice, this list of conditions and the following disclaimer.
15 1.2 simonb * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 simonb * notice, this list of conditions and the following disclaimer in the
17 1.2 simonb * documentation and/or other materials provided with the distribution.
18 1.2 simonb *
19 1.2 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 simonb * POSSIBILITY OF SUCH DAMAGE.
30 1.2 simonb */
31 1.2 simonb
32 1.2 simonb #include <sys/cdefs.h>
33 1.49 msaitoh __KERNEL_RCSID(0, "$NetBSD: ffs_wapbl.c,v 1.49 2024/05/13 00:24:19 msaitoh Exp $");
34 1.7 joerg
35 1.7 joerg #define WAPBL_INTERNAL
36 1.2 simonb
37 1.2 simonb #if defined(_KERNEL_OPT)
38 1.2 simonb #include "opt_ffs.h"
39 1.2 simonb #endif
40 1.2 simonb
41 1.2 simonb #include <sys/param.h>
42 1.2 simonb #include <sys/systm.h>
43 1.2 simonb #include <sys/kernel.h>
44 1.2 simonb #include <sys/vnode.h>
45 1.2 simonb #include <sys/mount.h>
46 1.2 simonb #include <sys/file.h>
47 1.2 simonb #include <sys/disk.h>
48 1.2 simonb #include <sys/ioctl.h>
49 1.2 simonb #include <sys/errno.h>
50 1.2 simonb #include <sys/kauth.h>
51 1.2 simonb #include <sys/wapbl.h>
52 1.2 simonb
53 1.2 simonb #include <ufs/ufs/inode.h>
54 1.2 simonb #include <ufs/ufs/quota.h>
55 1.2 simonb #include <ufs/ufs/ufsmount.h>
56 1.2 simonb #include <ufs/ufs/ufs_bswap.h>
57 1.2 simonb #include <ufs/ufs/ufs_extern.h>
58 1.2 simonb #include <ufs/ufs/ufs_wapbl.h>
59 1.2 simonb
60 1.2 simonb #include <ufs/ffs/fs.h>
61 1.2 simonb #include <ufs/ffs/ffs_extern.h>
62 1.2 simonb
63 1.2 simonb #undef WAPBL_DEBUG
64 1.2 simonb #ifdef WAPBL_DEBUG
65 1.2 simonb int ffs_wapbl_debug = 1;
66 1.2 simonb #define DPRINTF(fmt, args...) \
67 1.2 simonb do { \
68 1.2 simonb if (ffs_wapbl_debug) \
69 1.2 simonb printf("%s:%d "fmt, __func__ , __LINE__, ##args); \
70 1.2 simonb } while (/* CONSTCOND */0)
71 1.2 simonb #else
72 1.2 simonb #define DPRINTF(fmt, args...) \
73 1.2 simonb do { \
74 1.2 simonb /* nothing */ \
75 1.2 simonb } while (/* CONSTCOND */0)
76 1.2 simonb #endif
77 1.2 simonb
78 1.4 simonb static int ffs_superblock_layout(struct fs *);
79 1.2 simonb static int wapbl_log_position(struct mount *, struct fs *, struct vnode *,
80 1.2 simonb daddr_t *, size_t *, size_t *, uint64_t *);
81 1.2 simonb static int wapbl_create_infs_log(struct mount *, struct fs *, struct vnode *,
82 1.14 mlelstv daddr_t *, size_t *, uint64_t *);
83 1.2 simonb static void wapbl_find_log_start(struct mount *, struct vnode *, off_t,
84 1.2 simonb daddr_t *, daddr_t *, size_t *);
85 1.2 simonb static int wapbl_remove_log(struct mount *);
86 1.14 mlelstv static int wapbl_allocate_log_file(struct mount *, struct vnode *,
87 1.14 mlelstv daddr_t *, size_t *, uint64_t *);
88 1.2 simonb
89 1.2 simonb /*
90 1.4 simonb * Return the super block layout format - UFS1 or UFS2.
91 1.4 simonb * WAPBL only works with UFS2 layout (which is still available
92 1.4 simonb * with FFSv1).
93 1.4 simonb *
94 1.4 simonb * XXX Should this be in ufs/ffs/fs.h? Same style of check is
95 1.4 simonb * also used in ffs_alloc.c in a few places.
96 1.4 simonb */
97 1.4 simonb static int
98 1.4 simonb ffs_superblock_layout(struct fs *fs)
99 1.4 simonb {
100 1.4 simonb if ((fs->fs_magic == FS_UFS1_MAGIC) &&
101 1.4 simonb ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0))
102 1.4 simonb return 1;
103 1.4 simonb else
104 1.4 simonb return 2;
105 1.4 simonb }
106 1.4 simonb
107 1.4 simonb /*
108 1.2 simonb * This function is invoked after a log is replayed to
109 1.2 simonb * disk to perform logical cleanup actions as described by
110 1.2 simonb * the log
111 1.2 simonb */
112 1.2 simonb void
113 1.2 simonb ffs_wapbl_replay_finish(struct mount *mp)
114 1.2 simonb {
115 1.2 simonb struct wapbl_replay *wr = mp->mnt_wapbl_replay;
116 1.2 simonb int i;
117 1.2 simonb int error;
118 1.2 simonb
119 1.2 simonb if (!wr)
120 1.2 simonb return;
121 1.2 simonb
122 1.2 simonb KDASSERT((mp->mnt_flag & MNT_RDONLY) == 0);
123 1.2 simonb
124 1.2 simonb for (i = 0; i < wr->wr_inodescnt; i++) {
125 1.2 simonb struct vnode *vp;
126 1.2 simonb struct inode *ip;
127 1.45 ad error = VFS_VGET(mp, wr->wr_inodes[i].wr_inumber,
128 1.45 ad LK_EXCLUSIVE, &vp);
129 1.2 simonb if (error) {
130 1.35 christos printf("%s: %s: unable to cleanup inode %" PRIu32 "\n",
131 1.35 christos __func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
132 1.2 simonb wr->wr_inodes[i].wr_inumber);
133 1.2 simonb continue;
134 1.2 simonb }
135 1.2 simonb ip = VTOI(vp);
136 1.2 simonb KDASSERT(wr->wr_inodes[i].wr_inumber == ip->i_number);
137 1.6 joerg #ifdef WAPBL_DEBUG
138 1.35 christos printf("%s%s: %s: cleaning inode %" PRIu64 " size=%" PRIu64
139 1.35 christos " mode=%o nlink=%d\n",
140 1.35 christos __func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
141 1.2 simonb ip->i_number, ip->i_size, ip->i_mode, ip->i_nlink);
142 1.6 joerg #endif
143 1.2 simonb KASSERT(ip->i_nlink == 0);
144 1.2 simonb
145 1.2 simonb /*
146 1.2 simonb * The journal may have left partially allocated inodes in mode
147 1.48 andvar * zero. This may occur if a crash occurs between the node
148 1.2 simonb * allocation in ffs_nodeallocg and when the node is properly
149 1.47 reinoud * initialized in ufs_makeinode. If so, just deallocate them.
150 1.2 simonb */
151 1.2 simonb if (ip->i_mode == 0) {
152 1.26 dholland error = UFS_WAPBL_BEGIN(mp);
153 1.26 dholland if (error) {
154 1.35 christos printf("%s: %s: "
155 1.26 dholland "unable to cleanup inode %" PRIu32 "\n",
156 1.35 christos __func__, VFSTOUFS(mp)->um_fs->fs_fsmnt,
157 1.26 dholland wr->wr_inodes[i].wr_inumber);
158 1.26 dholland } else {
159 1.26 dholland ffs_vfree(vp, ip->i_number,
160 1.26 dholland wr->wr_inodes[i].wr_imode);
161 1.26 dholland UFS_WAPBL_END(mp);
162 1.26 dholland }
163 1.2 simonb }
164 1.2 simonb vput(vp);
165 1.2 simonb }
166 1.8 joerg wapbl_replay_stop(wr);
167 1.2 simonb wapbl_replay_free(wr);
168 1.8 joerg mp->mnt_wapbl_replay = NULL;
169 1.2 simonb }
170 1.2 simonb
171 1.2 simonb /* Callback for wapbl */
172 1.2 simonb void
173 1.34 jdolecek ffs_wapbl_sync_metadata(struct mount *mp, struct wapbl_dealloc *fdealloc)
174 1.2 simonb {
175 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
176 1.2 simonb struct fs *fs = ump->um_fs;
177 1.34 jdolecek int error __diagused;
178 1.34 jdolecek struct wapbl_dealloc *wd;
179 1.2 simonb
180 1.43 jdolecek UFS_WAPBL_JLOCK_ASSERT(ump->um_mountp);
181 1.31 jdolecek
182 1.36 jdolecek for (wd = fdealloc; wd != NULL; wd = TAILQ_NEXT(wd, wd_entries)) {
183 1.2 simonb /*
184 1.2 simonb * blkfree errors are unreported, might silently fail
185 1.2 simonb * if it cannot read the cylinder group block
186 1.2 simonb */
187 1.2 simonb ffs_blkfree(fs, ump->um_devvp,
188 1.34 jdolecek FFS_DBTOFSB(fs, wd->wd_blkno), wd->wd_len, -1);
189 1.2 simonb }
190 1.2 simonb
191 1.31 jdolecek if (fs->fs_fmod != 0) {
192 1.31 jdolecek fs->fs_fmod = 0;
193 1.31 jdolecek fs->fs_time = time_second;
194 1.31 jdolecek error = ffs_cgupdate(ump, 0);
195 1.32 jdolecek KASSERT(error == 0);
196 1.31 jdolecek }
197 1.2 simonb }
198 1.2 simonb
199 1.2 simonb void
200 1.34 jdolecek ffs_wapbl_abort_sync_metadata(struct mount *mp, struct wapbl_dealloc *fdealloc)
201 1.2 simonb {
202 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
203 1.2 simonb struct fs *fs = ump->um_fs;
204 1.34 jdolecek struct wapbl_dealloc *wd;
205 1.2 simonb
206 1.36 jdolecek for (wd = fdealloc; wd != NULL; wd = TAILQ_NEXT(wd, wd_entries)) {
207 1.2 simonb /*
208 1.2 simonb * Since the above blkfree may have failed, this blkalloc might
209 1.2 simonb * fail as well, so don't check its error. Note that if the
210 1.2 simonb * blkfree succeeded above, then this shouldn't fail because
211 1.2 simonb * the buffer will be locked in the current transaction.
212 1.2 simonb */
213 1.34 jdolecek ffs_blkalloc_ump(ump, FFS_DBTOFSB(fs, wd->wd_blkno),
214 1.34 jdolecek wd->wd_len);
215 1.2 simonb }
216 1.2 simonb }
217 1.2 simonb
218 1.2 simonb static int
219 1.2 simonb wapbl_remove_log(struct mount *mp)
220 1.2 simonb {
221 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
222 1.2 simonb struct fs *fs = ump->um_fs;
223 1.2 simonb struct vnode *vp;
224 1.2 simonb struct inode *ip;
225 1.2 simonb ino_t log_ino;
226 1.2 simonb int error;
227 1.2 simonb
228 1.4 simonb /* If super block layout is too old to support WAPBL, return */
229 1.4 simonb if (ffs_superblock_layout(fs) < 2)
230 1.4 simonb return 0;
231 1.4 simonb
232 1.2 simonb /* If all the log locators are 0, just clean up */
233 1.2 simonb if (fs->fs_journallocs[0] == 0 &&
234 1.2 simonb fs->fs_journallocs[1] == 0 &&
235 1.2 simonb fs->fs_journallocs[2] == 0 &&
236 1.2 simonb fs->fs_journallocs[3] == 0) {
237 1.2 simonb DPRINTF("empty locators, just clear\n");
238 1.2 simonb goto done;
239 1.2 simonb }
240 1.2 simonb
241 1.2 simonb switch (fs->fs_journal_location) {
242 1.2 simonb case UFS_WAPBL_JOURNALLOC_NONE:
243 1.2 simonb /* nothing! */
244 1.2 simonb DPRINTF("no log\n");
245 1.2 simonb break;
246 1.2 simonb
247 1.2 simonb case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
248 1.2 simonb log_ino = fs->fs_journallocs[UFS_WAPBL_INFS_INO];
249 1.2 simonb DPRINTF("in-fs log, ino = %" PRId64 "\n",log_ino);
250 1.2 simonb
251 1.2 simonb /* if no existing log inode, just clear all fields and bail */
252 1.2 simonb if (log_ino == 0)
253 1.2 simonb goto done;
254 1.45 ad error = VFS_VGET(mp, log_ino, LK_EXCLUSIVE, &vp);
255 1.2 simonb if (error != 0) {
256 1.35 christos printf("%s: %s: vget failed %d\n", __func__,
257 1.35 christos fs->fs_fsmnt, error);
258 1.2 simonb /* clear out log info on error */
259 1.2 simonb goto done;
260 1.2 simonb }
261 1.2 simonb ip = VTOI(vp);
262 1.2 simonb KASSERT(log_ino == ip->i_number);
263 1.2 simonb if ((ip->i_flags & SF_LOG) == 0) {
264 1.35 christos printf("%s: %s: try to clear non-log inode "
265 1.35 christos "%" PRId64 "\n", __func__, fs->fs_fsmnt, log_ino);
266 1.2 simonb vput(vp);
267 1.2 simonb /* clear out log info on error */
268 1.2 simonb goto done;
269 1.2 simonb }
270 1.2 simonb
271 1.2 simonb /*
272 1.2 simonb * remove the log inode by setting its link count back
273 1.2 simonb * to zero and bail.
274 1.2 simonb */
275 1.2 simonb ip->i_nlink = 0;
276 1.2 simonb DIP_ASSIGN(ip, nlink, 0);
277 1.2 simonb vput(vp);
278 1.33 jdolecek break;
279 1.2 simonb
280 1.2 simonb case UFS_WAPBL_JOURNALLOC_END_PARTITION:
281 1.2 simonb DPRINTF("end-of-partition log\n");
282 1.2 simonb /* no extra work required */
283 1.2 simonb break;
284 1.2 simonb
285 1.2 simonb default:
286 1.35 christos printf("%s: %s: unknown journal type %d\n", __func__,
287 1.35 christos fs->fs_fsmnt, fs->fs_journal_location);
288 1.13 bouyer break;
289 1.2 simonb }
290 1.2 simonb
291 1.2 simonb
292 1.2 simonb done:
293 1.2 simonb /* Clear out all previous knowledge of journal */
294 1.2 simonb fs->fs_journal_version = 0;
295 1.2 simonb fs->fs_journal_location = 0;
296 1.2 simonb fs->fs_journal_flags = 0;
297 1.2 simonb fs->fs_journallocs[0] = 0;
298 1.2 simonb fs->fs_journallocs[1] = 0;
299 1.2 simonb fs->fs_journallocs[2] = 0;
300 1.2 simonb fs->fs_journallocs[3] = 0;
301 1.2 simonb (void) ffs_sbupdate(ump, MNT_WAIT);
302 1.2 simonb
303 1.2 simonb return 0;
304 1.2 simonb }
305 1.2 simonb
306 1.2 simonb int
307 1.2 simonb ffs_wapbl_start(struct mount *mp)
308 1.2 simonb {
309 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
310 1.2 simonb struct fs *fs = ump->um_fs;
311 1.2 simonb struct vnode *devvp = ump->um_devvp;
312 1.2 simonb daddr_t off;
313 1.2 simonb size_t count;
314 1.2 simonb size_t blksize;
315 1.2 simonb uint64_t extradata;
316 1.2 simonb int error;
317 1.2 simonb
318 1.11 yamt if (mp->mnt_wapbl == NULL) {
319 1.2 simonb if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG) {
320 1.2 simonb /* Clear out any existing journal file */
321 1.2 simonb error = wapbl_remove_log(mp);
322 1.2 simonb if (error != 0)
323 1.2 simonb return error;
324 1.2 simonb }
325 1.2 simonb
326 1.2 simonb if (mp->mnt_flag & MNT_LOG) {
327 1.2 simonb KDASSERT(fs->fs_ronly == 0);
328 1.2 simonb
329 1.4 simonb /* WAPBL needs UFS2 format super block */
330 1.4 simonb if (ffs_superblock_layout(fs) < 2) {
331 1.35 christos printf("%s: %s: fs superblock in old format, "
332 1.35 christos "not journaling\n", __func__,
333 1.4 simonb VFSTOUFS(mp)->um_fs->fs_fsmnt);
334 1.4 simonb mp->mnt_flag &= ~MNT_LOG;
335 1.4 simonb return EINVAL;
336 1.4 simonb }
337 1.4 simonb
338 1.40 jdolecek error = wapbl_log_position(mp, fs, devvp, &off,
339 1.40 jdolecek &count, &blksize, &extradata);
340 1.40 jdolecek if (error)
341 1.40 jdolecek return error;
342 1.40 jdolecek
343 1.38 jdolecek /*
344 1.38 jdolecek * Make sure we don't carry over any delayed write
345 1.39 jdolecek * buffers when updating to log. Need to turn off
346 1.39 jdolecek * async termporarily, to prevent ffs_sync() writes
347 1.39 jdolecek * themselves being turned into delayed writes.
348 1.38 jdolecek */
349 1.39 jdolecek if (mp->mnt_flag & MNT_UPDATE) {
350 1.39 jdolecek int saveflag = mp->mnt_flag & MNT_ASYNC;
351 1.39 jdolecek mp->mnt_flag &= ~MNT_ASYNC;
352 1.38 jdolecek ffs_sync(mp, MNT_WAIT, FSCRED);
353 1.39 jdolecek mp->mnt_flag |= saveflag;
354 1.39 jdolecek }
355 1.38 jdolecek
356 1.2 simonb error = wapbl_start(&mp->mnt_wapbl, mp, devvp, off,
357 1.2 simonb count, blksize, mp->mnt_wapbl_replay,
358 1.2 simonb ffs_wapbl_sync_metadata,
359 1.2 simonb ffs_wapbl_abort_sync_metadata);
360 1.2 simonb if (error)
361 1.2 simonb return error;
362 1.2 simonb
363 1.2 simonb mp->mnt_wapbl_op = &wapbl_ops;
364 1.2 simonb
365 1.2 simonb #ifdef WAPBL_DEBUG
366 1.35 christos printf("%s: %s: enabling logging\n", __func__,
367 1.35 christos fs->fs_fsmnt);
368 1.2 simonb #endif
369 1.2 simonb
370 1.2 simonb if ((fs->fs_flags & FS_DOWAPBL) == 0) {
371 1.28 christos fs->fs_flags |= FS_DOWAPBL;
372 1.27 christos if ((error = UFS_WAPBL_BEGIN(mp)) != 0)
373 1.27 christos goto out;
374 1.2 simonb error = ffs_sbupdate(ump, MNT_WAIT);
375 1.2 simonb if (error) {
376 1.2 simonb UFS_WAPBL_END(mp);
377 1.27 christos goto out;
378 1.2 simonb }
379 1.2 simonb UFS_WAPBL_END(mp);
380 1.2 simonb error = wapbl_flush(mp->mnt_wapbl, 1);
381 1.27 christos if (error)
382 1.27 christos goto out;
383 1.2 simonb }
384 1.37 jdolecek
385 1.37 jdolecek /*
386 1.37 jdolecek * XXX discard interferes with block deallocation
387 1.37 jdolecek * registration and hence log consistency
388 1.37 jdolecek */
389 1.37 jdolecek if (mp->mnt_flag & MNT_DISCARD) {
390 1.37 jdolecek CLR(mp->mnt_flag, MNT_DISCARD);
391 1.37 jdolecek printf("%s: %s: disabling discard to preserve log consistency\n", __func__,
392 1.37 jdolecek fs->fs_fsmnt);
393 1.37 jdolecek
394 1.37 jdolecek if (ump->um_discarddata != NULL) {
395 1.37 jdolecek ffs_discard_finish(ump->um_discarddata,
396 1.37 jdolecek 0);
397 1.37 jdolecek ump->um_discarddata = NULL;
398 1.37 jdolecek }
399 1.37 jdolecek }
400 1.37 jdolecek
401 1.2 simonb } else if (fs->fs_flags & FS_DOWAPBL) {
402 1.2 simonb fs->fs_fmod = 1;
403 1.2 simonb fs->fs_flags &= ~FS_DOWAPBL;
404 1.2 simonb }
405 1.2 simonb }
406 1.2 simonb
407 1.2 simonb /*
408 1.2 simonb * It is recommended that you finish replay with logging enabled.
409 1.2 simonb * However, even if logging is not enabled, the remaining log
410 1.2 simonb * replay should be safely recoverable with an fsck, so perform
411 1.2 simonb * it anyway.
412 1.2 simonb */
413 1.2 simonb if ((fs->fs_ronly == 0) && mp->mnt_wapbl_replay) {
414 1.2 simonb int saveflag = mp->mnt_flag & MNT_RDONLY;
415 1.2 simonb /*
416 1.2 simonb * Make sure MNT_RDONLY is not set so that the inode
417 1.2 simonb * cleanup in ufs_inactive will actually do its work.
418 1.2 simonb */
419 1.2 simonb mp->mnt_flag &= ~MNT_RDONLY;
420 1.2 simonb ffs_wapbl_replay_finish(mp);
421 1.2 simonb mp->mnt_flag |= saveflag;
422 1.2 simonb KASSERT(fs->fs_ronly == 0);
423 1.2 simonb }
424 1.2 simonb
425 1.2 simonb return 0;
426 1.27 christos out:
427 1.27 christos ffs_wapbl_stop(mp, MNT_FORCE);
428 1.27 christos return error;
429 1.2 simonb }
430 1.2 simonb
431 1.2 simonb int
432 1.2 simonb ffs_wapbl_stop(struct mount *mp, int force)
433 1.2 simonb {
434 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
435 1.2 simonb struct fs *fs = ump->um_fs;
436 1.2 simonb int error;
437 1.2 simonb
438 1.2 simonb if (mp->mnt_wapbl) {
439 1.2 simonb KDASSERT(fs->fs_ronly == 0);
440 1.2 simonb
441 1.2 simonb /*
442 1.2 simonb * Make sure turning off FS_DOWAPBL is only removed
443 1.2 simonb * as the only change in the final flush since otherwise
444 1.2 simonb * a transaction may reorder writes.
445 1.2 simonb */
446 1.2 simonb error = wapbl_flush(mp->mnt_wapbl, 1);
447 1.2 simonb if (error && !force)
448 1.2 simonb return error;
449 1.2 simonb if (error && force)
450 1.2 simonb goto forceout;
451 1.2 simonb error = UFS_WAPBL_BEGIN(mp);
452 1.2 simonb if (error && !force)
453 1.2 simonb return error;
454 1.2 simonb if (error && force)
455 1.2 simonb goto forceout;
456 1.2 simonb KASSERT(fs->fs_flags & FS_DOWAPBL);
457 1.2 simonb
458 1.2 simonb fs->fs_flags &= ~FS_DOWAPBL;
459 1.2 simonb error = ffs_sbupdate(ump, MNT_WAIT);
460 1.2 simonb KASSERT(error == 0); /* XXX a bit drastic! */
461 1.2 simonb UFS_WAPBL_END(mp);
462 1.2 simonb forceout:
463 1.2 simonb error = wapbl_stop(mp->mnt_wapbl, force);
464 1.2 simonb if (error) {
465 1.2 simonb KASSERT(!force);
466 1.2 simonb fs->fs_flags |= FS_DOWAPBL;
467 1.2 simonb return error;
468 1.2 simonb }
469 1.2 simonb fs->fs_flags &= ~FS_DOWAPBL; /* Repeat in case of forced error */
470 1.11 yamt mp->mnt_wapbl = NULL;
471 1.2 simonb
472 1.2 simonb #ifdef WAPBL_DEBUG
473 1.35 christos printf("%s: %s: disabled logging\n", __func__, fs->fs_fsmnt);
474 1.2 simonb #endif
475 1.2 simonb }
476 1.2 simonb
477 1.2 simonb return 0;
478 1.2 simonb }
479 1.2 simonb
480 1.2 simonb int
481 1.2 simonb ffs_wapbl_replay_start(struct mount *mp, struct fs *fs, struct vnode *devvp)
482 1.2 simonb {
483 1.2 simonb int error;
484 1.2 simonb daddr_t off;
485 1.2 simonb size_t count;
486 1.2 simonb size_t blksize;
487 1.2 simonb uint64_t extradata;
488 1.2 simonb
489 1.4 simonb /*
490 1.4 simonb * WAPBL needs UFS2 format super block, if we got here with a
491 1.4 simonb * UFS1 format super block something is amiss...
492 1.4 simonb */
493 1.4 simonb if (ffs_superblock_layout(fs) < 2)
494 1.4 simonb return EINVAL;
495 1.4 simonb
496 1.2 simonb error = wapbl_log_position(mp, fs, devvp, &off, &count, &blksize,
497 1.2 simonb &extradata);
498 1.2 simonb
499 1.2 simonb if (error)
500 1.2 simonb return error;
501 1.2 simonb
502 1.2 simonb error = wapbl_replay_start(&mp->mnt_wapbl_replay, devvp, off,
503 1.2 simonb count, blksize);
504 1.2 simonb if (error)
505 1.2 simonb return error;
506 1.2 simonb
507 1.2 simonb mp->mnt_wapbl_op = &wapbl_ops;
508 1.2 simonb
509 1.2 simonb return 0;
510 1.2 simonb }
511 1.2 simonb
512 1.2 simonb /*
513 1.2 simonb * If the superblock doesn't already have a recorded journal location
514 1.2 simonb * then we allocate the journal in one of two positions:
515 1.2 simonb *
516 1.2 simonb * - At the end of the partition after the filesystem if there's
517 1.2 simonb * enough space. "Enough space" is defined as >= 1MB of journal
518 1.2 simonb * per 1GB of filesystem or 64MB, whichever is smaller.
519 1.2 simonb *
520 1.2 simonb * - Inside the filesystem. We try to allocate a contiguous journal
521 1.2 simonb * based on the total filesystem size - the target is 1MB of journal
522 1.2 simonb * per 1GB of filesystem, up to a maximum journal size of 64MB. As
523 1.2 simonb * a worst case allowing for fragmentation, we'll allocate a journal
524 1.2 simonb * 1/4 of the desired size but never smaller than 1MB.
525 1.2 simonb *
526 1.2 simonb * XXX In the future if we allow for non-contiguous journal files we
527 1.2 simonb * can tighten the above restrictions.
528 1.2 simonb *
529 1.2 simonb * XXX
530 1.2 simonb * These seems like a lot of duplication both here and in some of
531 1.2 simonb * the userland tools (fsck_ffs, dumpfs, tunefs) with similar
532 1.2 simonb * "switch (fs_journal_location)" constructs. Can we centralise
533 1.2 simonb * this sort of code somehow/somewhere?
534 1.2 simonb */
535 1.2 simonb static int
536 1.2 simonb wapbl_log_position(struct mount *mp, struct fs *fs, struct vnode *devvp,
537 1.2 simonb daddr_t *startp, size_t *countp, size_t *blksizep, uint64_t *extradatap)
538 1.2 simonb {
539 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
540 1.2 simonb daddr_t logstart, logend, desired_logsize;
541 1.14 mlelstv uint64_t numsecs;
542 1.14 mlelstv unsigned secsize;
543 1.14 mlelstv int error, location;
544 1.2 simonb
545 1.2 simonb if (fs->fs_journal_version == UFS_WAPBL_VERSION) {
546 1.2 simonb switch (fs->fs_journal_location) {
547 1.2 simonb case UFS_WAPBL_JOURNALLOC_END_PARTITION:
548 1.2 simonb DPRINTF("found existing end-of-partition log\n");
549 1.2 simonb *startp = fs->fs_journallocs[UFS_WAPBL_EPART_ADDR];
550 1.2 simonb *countp = fs->fs_journallocs[UFS_WAPBL_EPART_COUNT];
551 1.2 simonb *blksizep = fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
552 1.5 pooka DPRINTF(" start = %" PRId64 ", size = %zu, "
553 1.5 pooka "blksize = %zu\n", *startp, *countp, *blksizep);
554 1.2 simonb return 0;
555 1.2 simonb
556 1.2 simonb case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
557 1.2 simonb DPRINTF("found existing in-filesystem log\n");
558 1.2 simonb *startp = fs->fs_journallocs[UFS_WAPBL_INFS_ADDR];
559 1.2 simonb *countp = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT];
560 1.2 simonb *blksizep = fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
561 1.5 pooka DPRINTF(" start = %" PRId64 ", size = %zu, "
562 1.5 pooka "blksize = %zu\n", *startp, *countp, *blksizep);
563 1.2 simonb return 0;
564 1.2 simonb
565 1.2 simonb default:
566 1.35 christos printf("%s: %s: unknown journal type %d\n", __func__,
567 1.35 christos fs->fs_fsmnt, fs->fs_journal_location);
568 1.2 simonb return EINVAL;
569 1.2 simonb }
570 1.2 simonb }
571 1.2 simonb
572 1.2 simonb desired_logsize =
573 1.21 dholland ffs_lfragtosize(fs, fs->fs_size) / UFS_WAPBL_JOURNAL_SCALE;
574 1.2 simonb DPRINTF("desired log size = %" PRId64 " kB\n", desired_logsize / 1024);
575 1.42 riastrad desired_logsize = uimax(desired_logsize, UFS_WAPBL_MIN_JOURNAL_SIZE);
576 1.42 riastrad desired_logsize = uimin(desired_logsize, UFS_WAPBL_MAX_JOURNAL_SIZE);
577 1.2 simonb DPRINTF("adjusted desired log size = %" PRId64 " kB\n",
578 1.2 simonb desired_logsize / 1024);
579 1.2 simonb
580 1.2 simonb /* Is there space after after filesystem on partition for log? */
581 1.20 dholland logstart = FFS_FSBTODB(fs, fs->fs_size);
582 1.14 mlelstv error = getdisksize(devvp, &numsecs, &secsize);
583 1.14 mlelstv if (error)
584 1.14 mlelstv return error;
585 1.14 mlelstv KDASSERT(secsize != 0);
586 1.14 mlelstv logend = btodb(numsecs * secsize);
587 1.2 simonb
588 1.14 mlelstv if (dbtob(logend - logstart) >= desired_logsize) {
589 1.14 mlelstv DPRINTF("enough space, use end-of-partition log\n");
590 1.2 simonb
591 1.14 mlelstv location = UFS_WAPBL_JOURNALLOC_END_PARTITION;
592 1.14 mlelstv *blksizep = secsize;
593 1.2 simonb
594 1.2 simonb *startp = logstart;
595 1.2 simonb *countp = (logend - logstart);
596 1.2 simonb *extradatap = 0;
597 1.2 simonb
598 1.15 mlelstv /* convert to physical block numbers */
599 1.15 mlelstv *startp = dbtob(*startp) / secsize;
600 1.15 mlelstv *countp = dbtob(*countp) / secsize;
601 1.15 mlelstv
602 1.2 simonb fs->fs_journallocs[UFS_WAPBL_EPART_ADDR] = *startp;
603 1.2 simonb fs->fs_journallocs[UFS_WAPBL_EPART_COUNT] = *countp;
604 1.2 simonb fs->fs_journallocs[UFS_WAPBL_EPART_BLKSZ] = *blksizep;
605 1.2 simonb fs->fs_journallocs[UFS_WAPBL_EPART_UNUSED] = *extradatap;
606 1.14 mlelstv } else {
607 1.14 mlelstv DPRINTF("end-of-partition has only %" PRId64 " free\n",
608 1.14 mlelstv logend - logstart);
609 1.14 mlelstv
610 1.14 mlelstv location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
611 1.14 mlelstv *blksizep = secsize;
612 1.2 simonb
613 1.14 mlelstv error = wapbl_create_infs_log(mp, fs, devvp,
614 1.14 mlelstv startp, countp, extradatap);
615 1.14 mlelstv ffs_sync(mp, MNT_WAIT, FSCRED);
616 1.14 mlelstv
617 1.15 mlelstv /* convert to physical block numbers */
618 1.15 mlelstv *startp = dbtob(*startp) / secsize;
619 1.15 mlelstv *countp = dbtob(*countp) / secsize;
620 1.15 mlelstv
621 1.14 mlelstv fs->fs_journallocs[UFS_WAPBL_INFS_ADDR] = *startp;
622 1.14 mlelstv fs->fs_journallocs[UFS_WAPBL_INFS_COUNT] = *countp;
623 1.14 mlelstv fs->fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = *blksizep;
624 1.14 mlelstv fs->fs_journallocs[UFS_WAPBL_INFS_INO] = *extradatap;
625 1.2 simonb }
626 1.2 simonb
627 1.14 mlelstv if (error == 0) {
628 1.14 mlelstv /* update superblock with log location */
629 1.14 mlelstv fs->fs_journal_version = UFS_WAPBL_VERSION;
630 1.14 mlelstv fs->fs_journal_location = location;
631 1.14 mlelstv fs->fs_journal_flags = 0;
632 1.2 simonb
633 1.14 mlelstv error = ffs_sbupdate(ump, MNT_WAIT);
634 1.14 mlelstv }
635 1.2 simonb
636 1.2 simonb return error;
637 1.2 simonb }
638 1.2 simonb
639 1.2 simonb /*
640 1.2 simonb * Try to create a journal log inside the filesystem.
641 1.2 simonb */
642 1.2 simonb static int
643 1.2 simonb wapbl_create_infs_log(struct mount *mp, struct fs *fs, struct vnode *devvp,
644 1.14 mlelstv daddr_t *startp, size_t *countp, uint64_t *extradatap)
645 1.2 simonb {
646 1.2 simonb struct vnode *vp, *rvp;
647 1.29 hannken struct vattr va;
648 1.2 simonb struct inode *ip;
649 1.2 simonb int error;
650 1.2 simonb
651 1.45 ad if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)) != 0)
652 1.2 simonb return error;
653 1.2 simonb
654 1.29 hannken vattr_null(&va);
655 1.29 hannken va.va_type = VREG;
656 1.29 hannken va.va_mode = 0;
657 1.29 hannken
658 1.44 hannken error = vcache_new(mp, rvp, &va, NOCRED, NULL, &vp);
659 1.29 hannken vput(rvp);
660 1.29 hannken if (error)
661 1.29 hannken return error;
662 1.29 hannken
663 1.29 hannken error = vn_lock(vp, LK_EXCLUSIVE);
664 1.29 hannken if (error) {
665 1.29 hannken vrele(vp);
666 1.29 hannken return error;
667 1.17 mlelstv }
668 1.2 simonb
669 1.2 simonb ip = VTOI(vp);
670 1.2 simonb ip->i_flags = SF_LOG;
671 1.2 simonb DIP_ASSIGN(ip, flags, ip->i_flags);
672 1.2 simonb ip->i_nlink = 1;
673 1.2 simonb DIP_ASSIGN(ip, nlink, 1);
674 1.29 hannken ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
675 1.2 simonb ffs_update(vp, NULL, NULL, UPDATE_WAIT);
676 1.2 simonb
677 1.14 mlelstv if ((error = wapbl_allocate_log_file(mp, vp,
678 1.14 mlelstv startp, countp, extradatap)) != 0) {
679 1.2 simonb /*
680 1.2 simonb * If we couldn't allocate the space for the log file,
681 1.2 simonb * remove the inode by setting its link count back to
682 1.2 simonb * zero and bail.
683 1.2 simonb */
684 1.2 simonb ip->i_nlink = 0;
685 1.2 simonb DIP_ASSIGN(ip, nlink, 0);
686 1.41 hannken vput(vp);
687 1.2 simonb
688 1.2 simonb return error;
689 1.2 simonb }
690 1.2 simonb
691 1.2 simonb /*
692 1.2 simonb * Now that we have the place-holder inode for the journal,
693 1.2 simonb * we don't need the vnode ever again.
694 1.2 simonb */
695 1.41 hannken vput(vp);
696 1.2 simonb
697 1.2 simonb return 0;
698 1.2 simonb }
699 1.2 simonb
700 1.2 simonb int
701 1.14 mlelstv wapbl_allocate_log_file(struct mount *mp, struct vnode *vp,
702 1.14 mlelstv daddr_t *startp, size_t *countp, uint64_t *extradatap)
703 1.2 simonb {
704 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
705 1.2 simonb struct fs *fs = ump->um_fs;
706 1.2 simonb daddr_t addr, indir_addr;
707 1.2 simonb off_t logsize;
708 1.2 simonb size_t size;
709 1.2 simonb int error;
710 1.2 simonb
711 1.2 simonb logsize = 0;
712 1.2 simonb /* check if there's a suggested log size */
713 1.2 simonb if (fs->fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG &&
714 1.2 simonb fs->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM)
715 1.2 simonb logsize = fs->fs_journallocs[UFS_WAPBL_INFS_COUNT];
716 1.2 simonb
717 1.2 simonb if (vp->v_size > 0) {
718 1.35 christos printf("%s: %s: file size (%" PRId64 ") non zero\n", __func__,
719 1.35 christos fs->fs_fsmnt, vp->v_size);
720 1.2 simonb return EEXIST;
721 1.2 simonb }
722 1.2 simonb wapbl_find_log_start(mp, vp, logsize, &addr, &indir_addr, &size);
723 1.2 simonb if (addr == 0) {
724 1.35 christos printf("%s: %s: log not allocated, largest extent is "
725 1.35 christos "%" PRId64 "MB\n", __func__, fs->fs_fsmnt,
726 1.21 dholland ffs_lblktosize(fs, size) / (1024 * 1024));
727 1.2 simonb return ENOSPC;
728 1.2 simonb }
729 1.2 simonb
730 1.21 dholland logsize = ffs_lblktosize(fs, size); /* final log size */
731 1.2 simonb
732 1.2 simonb VTOI(vp)->i_ffs_first_data_blk = addr;
733 1.2 simonb VTOI(vp)->i_ffs_first_indir_blk = indir_addr;
734 1.2 simonb
735 1.2 simonb error = GOP_ALLOC(vp, 0, logsize, B_CONTIG, FSCRED);
736 1.2 simonb if (error) {
737 1.35 christos printf("%s: %s: GOP_ALLOC error %d\n", __func__, fs->fs_fsmnt,
738 1.35 christos error);
739 1.2 simonb return error;
740 1.2 simonb }
741 1.2 simonb
742 1.20 dholland *startp = FFS_FSBTODB(fs, addr);
743 1.14 mlelstv *countp = btodb(logsize);
744 1.14 mlelstv *extradatap = VTOI(vp)->i_number;
745 1.2 simonb
746 1.14 mlelstv return 0;
747 1.2 simonb }
748 1.2 simonb
749 1.2 simonb /*
750 1.2 simonb * Find a suitable location for the journal in the filesystem.
751 1.2 simonb *
752 1.2 simonb * Our strategy here is to look for a contiguous block of free space
753 1.2 simonb * at least "logfile" MB in size (plus room for any indirect blocks).
754 1.2 simonb * We start at the middle of the filesystem and check each cylinder
755 1.2 simonb * group working outwards. If "logfile" MB is not available as a
756 1.49 msaitoh * single contiguous chunk, then return the address and size of the
757 1.2 simonb * largest chunk found.
758 1.2 simonb *
759 1.2 simonb * XXX
760 1.2 simonb * At what stage does the search fail? Is if the largest space we could
761 1.2 simonb * find is less than a quarter the requested space reasonable? If the
762 1.2 simonb * search fails entirely, return a block address if "0" it indicate this.
763 1.2 simonb */
764 1.2 simonb static void
765 1.2 simonb wapbl_find_log_start(struct mount *mp, struct vnode *vp, off_t logsize,
766 1.2 simonb daddr_t *addr, daddr_t *indir_addr, size_t *size)
767 1.2 simonb {
768 1.2 simonb struct ufsmount *ump = VFSTOUFS(mp);
769 1.2 simonb struct fs *fs = ump->um_fs;
770 1.2 simonb struct vnode *devvp = ump->um_devvp;
771 1.2 simonb struct cg *cgp;
772 1.2 simonb struct buf *bp;
773 1.2 simonb uint8_t *blksfree;
774 1.2 simonb daddr_t blkno, best_addr, start_addr;
775 1.2 simonb daddr_t desired_blks, min_desired_blks;
776 1.2 simonb daddr_t freeblks, best_blks;
777 1.2 simonb int bpcg, cg, error, fixedsize, indir_blks, n, s;
778 1.2 simonb const int needswap = UFS_FSNEEDSWAP(fs);
779 1.2 simonb
780 1.2 simonb if (logsize == 0) {
781 1.2 simonb fixedsize = 0; /* We can adjust the size if tight */
782 1.21 dholland logsize = ffs_lfragtosize(fs, fs->fs_dsize) /
783 1.2 simonb UFS_WAPBL_JOURNAL_SCALE;
784 1.2 simonb DPRINTF("suggested log size = %" PRId64 "\n", logsize);
785 1.42 riastrad logsize = uimax(logsize, UFS_WAPBL_MIN_JOURNAL_SIZE);
786 1.42 riastrad logsize = uimin(logsize, UFS_WAPBL_MAX_JOURNAL_SIZE);
787 1.2 simonb DPRINTF("adjusted log size = %" PRId64 "\n", logsize);
788 1.2 simonb } else {
789 1.2 simonb fixedsize = 1;
790 1.2 simonb DPRINTF("fixed log size = %" PRId64 "\n", logsize);
791 1.2 simonb }
792 1.2 simonb
793 1.2 simonb desired_blks = logsize / fs->fs_bsize;
794 1.2 simonb DPRINTF("desired blocks = %" PRId64 "\n", desired_blks);
795 1.2 simonb
796 1.2 simonb /* add in number of indirect blocks needed */
797 1.2 simonb indir_blks = 0;
798 1.19 dholland if (desired_blks >= UFS_NDADDR) {
799 1.19 dholland struct indir indirs[UFS_NIADDR + 2];
800 1.2 simonb int num;
801 1.2 simonb
802 1.2 simonb error = ufs_getlbns(vp, desired_blks, indirs, &num);
803 1.2 simonb if (error) {
804 1.35 christos printf("%s: %s: ufs_getlbns failed, error %d!\n",
805 1.35 christos __func__, fs->fs_fsmnt, error);
806 1.2 simonb goto bad;
807 1.2 simonb }
808 1.2 simonb
809 1.2 simonb switch (num) {
810 1.2 simonb case 2:
811 1.2 simonb indir_blks = 1; /* 1st level indirect */
812 1.2 simonb break;
813 1.2 simonb case 3:
814 1.2 simonb indir_blks = 1 + /* 1st level indirect */
815 1.2 simonb 1 + /* 2nd level indirect */
816 1.2 simonb indirs[1].in_off + 1; /* extra 1st level indirect */
817 1.2 simonb break;
818 1.2 simonb default:
819 1.35 christos printf("%s: %s: unexpected numlevels %d from "
820 1.35 christos "ufs_getlbns\n", __func__, fs->fs_fsmnt, num);
821 1.2 simonb *size = 0;
822 1.2 simonb goto bad;
823 1.2 simonb }
824 1.2 simonb desired_blks += indir_blks;
825 1.2 simonb }
826 1.2 simonb DPRINTF("desired blocks = %" PRId64 " (including indirect)\n",
827 1.2 simonb desired_blks);
828 1.2 simonb
829 1.2 simonb /*
830 1.2 simonb * If a specific size wasn't requested, allow for a smaller log
831 1.2 simonb * if we're really tight for space...
832 1.2 simonb */
833 1.2 simonb min_desired_blks = desired_blks;
834 1.2 simonb if (!fixedsize)
835 1.2 simonb min_desired_blks = desired_blks / 4;
836 1.2 simonb
837 1.2 simonb /* Look at number of blocks per CG. If it's too small, bail early. */
838 1.22 dholland bpcg = ffs_fragstoblks(fs, fs->fs_fpg);
839 1.2 simonb if (min_desired_blks > bpcg) {
840 1.35 christos printf("%s: %s: cylinder group size of %" PRId64 " MB "
841 1.35 christos " is not big enough for journal\n", __func__, fs->fs_fsmnt,
842 1.21 dholland ffs_lblktosize(fs, bpcg) / (1024 * 1024));
843 1.2 simonb goto bad;
844 1.2 simonb }
845 1.2 simonb
846 1.2 simonb /*
847 1.2 simonb * Start with the middle cylinder group, and search outwards in
848 1.2 simonb * both directions until we either find the requested log size
849 1.2 simonb * or reach the start/end of the file system. If we reach the
850 1.2 simonb * start/end without finding enough space for the full requested
851 1.2 simonb * log size, use the largest extent found if it is large enough
852 1.2 simonb * to satisfy the our minimum size.
853 1.2 simonb *
854 1.2 simonb * XXX
855 1.2 simonb * Can we just use the cluster contigsum stuff (esp on UFS2)
856 1.2 simonb * here to simplify this search code?
857 1.2 simonb */
858 1.2 simonb best_addr = 0;
859 1.2 simonb best_blks = 0;
860 1.2 simonb for (cg = fs->fs_ncg / 2, s = 0, n = 1;
861 1.2 simonb best_blks < desired_blks && cg >= 0 && cg < fs->fs_ncg;
862 1.2 simonb s++, n = -n, cg += n * s) {
863 1.2 simonb DPRINTF("check cg %d of %d\n", cg, fs->fs_ncg);
864 1.20 dholland error = bread(devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
865 1.30 maxv fs->fs_cgsize, 0, &bp);
866 1.18 hannken if (error) {
867 1.18 hannken continue;
868 1.18 hannken }
869 1.2 simonb cgp = (struct cg *)bp->b_data;
870 1.18 hannken if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
871 1.2 simonb brelse(bp, 0);
872 1.2 simonb continue;
873 1.2 simonb }
874 1.2 simonb
875 1.2 simonb blksfree = cg_blksfree(cgp, needswap);
876 1.2 simonb
877 1.2 simonb for (blkno = 0; blkno < bpcg;) {
878 1.2 simonb /* look for next free block */
879 1.2 simonb /* XXX use scanc() and fragtbl[] here? */
880 1.2 simonb for (; blkno < bpcg - min_desired_blks; blkno++)
881 1.2 simonb if (ffs_isblock(fs, blksfree, blkno))
882 1.2 simonb break;
883 1.2 simonb
884 1.2 simonb /* past end of search space in this CG? */
885 1.2 simonb if (blkno >= bpcg - min_desired_blks)
886 1.2 simonb break;
887 1.2 simonb
888 1.2 simonb /* count how many free blocks in this extent */
889 1.2 simonb start_addr = blkno;
890 1.2 simonb for (freeblks = 0; blkno < bpcg; blkno++, freeblks++)
891 1.2 simonb if (!ffs_isblock(fs, blksfree, blkno))
892 1.2 simonb break;
893 1.2 simonb
894 1.2 simonb if (freeblks > best_blks) {
895 1.2 simonb best_blks = freeblks;
896 1.22 dholland best_addr = ffs_blkstofrags(fs, start_addr) +
897 1.2 simonb cgbase(fs, cg);
898 1.2 simonb
899 1.2 simonb if (freeblks >= desired_blks) {
900 1.2 simonb DPRINTF("found len %" PRId64
901 1.2 simonb " at offset %" PRId64 " in gc\n",
902 1.2 simonb freeblks, start_addr);
903 1.2 simonb break;
904 1.2 simonb }
905 1.2 simonb }
906 1.2 simonb }
907 1.2 simonb brelse(bp, 0);
908 1.2 simonb }
909 1.2 simonb DPRINTF("best found len = %" PRId64 ", wanted %" PRId64
910 1.2 simonb " at addr %" PRId64 "\n", best_blks, desired_blks, best_addr);
911 1.2 simonb
912 1.2 simonb if (best_blks < min_desired_blks) {
913 1.2 simonb *addr = 0;
914 1.2 simonb *indir_addr = 0;
915 1.2 simonb } else {
916 1.2 simonb /* put indirect blocks at start, and data blocks after */
917 1.22 dholland *addr = best_addr + ffs_blkstofrags(fs, indir_blks);
918 1.2 simonb *indir_addr = best_addr;
919 1.2 simonb }
920 1.42 riastrad *size = uimin(desired_blks, best_blks) - indir_blks;
921 1.2 simonb return;
922 1.2 simonb
923 1.2 simonb bad:
924 1.2 simonb *addr = 0;
925 1.2 simonb *indir_addr = 0;
926 1.2 simonb *size = 0;
927 1.2 simonb return;
928 1.2 simonb }
929