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