lfs.c revision 1.27.8.1 1 /* $NetBSD: lfs.c,v 1.27.8.1 2008/06/02 13:21:21 mjf Exp $ */
2 /*-
3 * Copyright (c) 2003 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Konrad E. Schroder <perseant (at) hhhh.org>.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30 /*
31 * Copyright (c) 1989, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
33 * (c) UNIX System Laboratories, Inc.
34 * All or some portions of this file are derived from material licensed
35 * to the University of California by American Telephone and Telegraph
36 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
37 * the permission of UNIX System Laboratories, Inc.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95
64 */
65
66
67 #include <sys/types.h>
68 #include <sys/param.h>
69 #include <sys/time.h>
70 #include <sys/buf.h>
71 #include <sys/mount.h>
72
73 #include <ufs/ufs/inode.h>
74 #include <ufs/ufs/ufsmount.h>
75 #define vnode uvnode
76 #include <ufs/lfs/lfs.h>
77 #undef vnode
78
79 #include <assert.h>
80 #include <err.h>
81 #include <errno.h>
82 #include <stdarg.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #include <util.h>
88
89 #include "bufcache.h"
90 #include "vnode.h"
91 #include "lfs_user.h"
92 #include "segwrite.h"
93
94 #define panic call_panic
95
96 extern u_int32_t cksum(void *, size_t);
97 extern u_int32_t lfs_sb_cksum(struct dlfs *);
98 extern void pwarn(const char *, ...);
99
100 extern struct uvnodelst vnodelist;
101 extern struct uvnodelst getvnodelist[VNODE_HASH_MAX];
102 extern int nvnodes;
103
104 static int
105 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **);
106
107 int fsdirty = 0;
108 void (*panic_func)(int, const char *, va_list) = my_vpanic;
109
110 /*
111 * LFS buffer and uvnode operations
112 */
113
114 int
115 lfs_vop_strategy(struct ubuf * bp)
116 {
117 int count;
118
119 if (bp->b_flags & B_READ) {
120 count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
121 dbtob(bp->b_blkno));
122 if (count == bp->b_bcount)
123 bp->b_flags |= B_DONE;
124 } else {
125 count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
126 dbtob(bp->b_blkno));
127 if (count == 0) {
128 perror("pwrite");
129 return -1;
130 }
131 bp->b_flags &= ~B_DELWRI;
132 reassignbuf(bp, bp->b_vp);
133 }
134 return 0;
135 }
136
137 int
138 lfs_vop_bwrite(struct ubuf * bp)
139 {
140 struct lfs *fs;
141
142 fs = bp->b_vp->v_fs;
143 if (!(bp->b_flags & B_DELWRI)) {
144 fs->lfs_avail -= btofsb(fs, bp->b_bcount);
145 }
146 bp->b_flags |= B_DELWRI | B_LOCKED;
147 reassignbuf(bp, bp->b_vp);
148 brelse(bp, 0);
149 return 0;
150 }
151
152 /*
153 * ufs_bmaparray does the bmap conversion, and if requested returns the
154 * array of logical blocks which must be traversed to get to a block.
155 * Each entry contains the offset into that block that gets you to the
156 * next block and the disk address of the block (if it is assigned).
157 */
158 int
159 ufs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump)
160 {
161 struct inode *ip;
162 struct ubuf *bp;
163 struct indir a[NIADDR + 1], *xap;
164 daddr_t daddr;
165 daddr_t metalbn;
166 int error, num;
167
168 ip = VTOI(vp);
169
170 if (bn >= 0 && bn < NDADDR) {
171 if (nump != NULL)
172 *nump = 0;
173 *bnp = fsbtodb(fs, ip->i_ffs1_db[bn]);
174 if (*bnp == 0)
175 *bnp = -1;
176 return (0);
177 }
178 xap = ap == NULL ? a : ap;
179 if (!nump)
180 nump = #
181 if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0)
182 return (error);
183
184 num = *nump;
185
186 /* Get disk address out of indirect block array */
187 daddr = ip->i_ffs1_ib[xap->in_off];
188
189 for (bp = NULL, ++xap; --num; ++xap) {
190 /* Exit the loop if there is no disk address assigned yet and
191 * the indirect block isn't in the cache, or if we were
192 * looking for an indirect block and we've found it. */
193
194 metalbn = xap->in_lbn;
195 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
196 break;
197 /*
198 * If we get here, we've either got the block in the cache
199 * or we have a disk address for it, go fetch it.
200 */
201 if (bp)
202 brelse(bp, 0);
203
204 xap->in_exists = 1;
205 bp = getblk(vp, metalbn, fs->lfs_bsize);
206
207 if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
208 bp->b_blkno = fsbtodb(fs, daddr);
209 bp->b_flags |= B_READ;
210 VOP_STRATEGY(bp);
211 }
212 daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off];
213 }
214 if (bp)
215 brelse(bp, 0);
216
217 daddr = fsbtodb(fs, (ufs_daddr_t) daddr);
218 *bnp = daddr == 0 ? -1 : daddr;
219 return (0);
220 }
221
222 /*
223 * Create an array of logical block number/offset pairs which represent the
224 * path of indirect blocks required to access a data block. The first "pair"
225 * contains the logical block number of the appropriate single, double or
226 * triple indirect block and the offset into the inode indirect block array.
227 * Note, the logical block number of the inode single/double/triple indirect
228 * block appears twice in the array, once with the offset into the i_ffs1_ib and
229 * once with the offset into the page itself.
230 */
231 int
232 ufs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump)
233 {
234 daddr_t metalbn, realbn;
235 int64_t blockcnt;
236 int lbc;
237 int i, numlevels, off;
238 int lognindir, indir;
239
240 metalbn = 0; /* XXXGCC -Wuninitialized [sh3] */
241
242 if (nump)
243 *nump = 0;
244 numlevels = 0;
245 realbn = bn;
246 if (bn < 0)
247 bn = -bn;
248
249 lognindir = -1;
250 for (indir = fs->lfs_nindir; indir; indir >>= 1)
251 ++lognindir;
252
253 /* Determine the number of levels of indirection. After this loop is
254 * done, blockcnt indicates the number of data blocks possible at the
255 * given level of indirection, and NIADDR - i is the number of levels
256 * of indirection needed to locate the requested block. */
257
258 bn -= NDADDR;
259 for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
260 if (i == 0)
261 return (EFBIG);
262
263 lbc += lognindir;
264 blockcnt = (int64_t) 1 << lbc;
265
266 if (bn < blockcnt)
267 break;
268 }
269
270 /* Calculate the address of the first meta-block. */
271 metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + NIADDR - i);
272
273 /* At each iteration, off is the offset into the bap array which is an
274 * array of disk addresses at the current level of indirection. The
275 * logical block number and the offset in that block are stored into
276 * the argument array. */
277 ap->in_lbn = metalbn;
278 ap->in_off = off = NIADDR - i;
279 ap->in_exists = 0;
280 ap++;
281 for (++numlevels; i <= NIADDR; i++) {
282 /* If searching for a meta-data block, quit when found. */
283 if (metalbn == realbn)
284 break;
285
286 lbc -= lognindir;
287 blockcnt = (int64_t) 1 << lbc;
288 off = (bn >> lbc) & (fs->lfs_nindir - 1);
289
290 ++numlevels;
291 ap->in_lbn = metalbn;
292 ap->in_off = off;
293 ap->in_exists = 0;
294 ++ap;
295
296 metalbn -= -1 + (off << lbc);
297 }
298 if (nump)
299 *nump = numlevels;
300 return (0);
301 }
302
303 int
304 lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
305 {
306 return ufs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
307 }
308
309 /* Search a block for a specific dinode. */
310 struct ufs1_dinode *
311 lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
312 {
313 struct ufs1_dinode *dip = (struct ufs1_dinode *) bp->b_data;
314 struct ufs1_dinode *ldip, *fin;
315
316 fin = dip + INOPB(fs);
317
318 /*
319 * Read the inode block backwards, since later versions of the
320 * inode will supercede earlier ones. Though it is unlikely, it is
321 * possible that the same inode will appear in the same inode block.
322 */
323 for (ldip = fin - 1; ldip >= dip; --ldip)
324 if (ldip->di_inumber == ino)
325 return (ldip);
326 return NULL;
327 }
328
329 /*
330 * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
331 * XXX it currently loses atime information.
332 */
333 struct uvnode *
334 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr)
335 {
336 struct uvnode *vp;
337 struct inode *ip;
338 struct ufs1_dinode *dip;
339 struct ubuf *bp;
340 int i, hash;
341
342 vp = ecalloc(1, sizeof(*vp));
343 vp->v_fd = fd;
344 vp->v_fs = fs;
345 vp->v_usecount = 0;
346 vp->v_strategy_op = lfs_vop_strategy;
347 vp->v_bwrite_op = lfs_vop_bwrite;
348 vp->v_bmap_op = lfs_vop_bmap;
349 LIST_INIT(&vp->v_cleanblkhd);
350 LIST_INIT(&vp->v_dirtyblkhd);
351
352 ip = ecalloc(1, sizeof(*ip));
353
354 ip->i_din.ffs1_din = ecalloc(1, sizeof(*ip->i_din.ffs1_din));
355
356 /* Initialize the inode -- from lfs_vcreate. */
357 ip->inode_ext.lfs = ecalloc(1, sizeof(*ip->inode_ext.lfs));
358 vp->v_data = ip;
359 /* ip->i_vnode = vp; */
360 ip->i_number = ino;
361 ip->i_lockf = 0;
362 ip->i_diroff = 0;
363 ip->i_lfs_effnblks = 0;
364 ip->i_flag = 0;
365
366 /* Load inode block and find inode */
367 if (daddr > 0) {
368 bread(fs->lfs_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
369 NULL, 0, &bp);
370 bp->b_flags |= B_AGE;
371 dip = lfs_ifind(fs, ino, bp);
372 if (dip == NULL) {
373 brelse(bp, 0);
374 free(ip);
375 free(vp);
376 return NULL;
377 }
378 memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
379 brelse(bp, 0);
380 }
381 ip->i_number = ino;
382 /* ip->i_devvp = fs->lfs_devvp; */
383 ip->i_lfs = fs;
384
385 ip->i_ffs_effnlink = ip->i_ffs1_nlink;
386 ip->i_lfs_effnblks = ip->i_ffs1_blocks;
387 ip->i_lfs_osize = ip->i_ffs1_size;
388 #if 0
389 if (fs->lfs_version > 1) {
390 ip->i_ffs1_atime = ts.tv_sec;
391 ip->i_ffs1_atimensec = ts.tv_nsec;
392 }
393 #endif
394
395 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
396 for (i = 0; i < NDADDR; i++)
397 if (ip->i_ffs1_db[i] != 0)
398 ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
399
400 ++nvnodes;
401 hash = ((int)(intptr_t)fs + ino) & (VNODE_HASH_MAX - 1);
402 LIST_INSERT_HEAD(&getvnodelist[hash], vp, v_getvnodes);
403 LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
404
405 return vp;
406 }
407
408 static struct uvnode *
409 lfs_vget(void *vfs, ino_t ino)
410 {
411 struct lfs *fs = (struct lfs *)vfs;
412 ufs_daddr_t daddr;
413 struct ubuf *bp;
414 IFILE *ifp;
415
416 LFS_IENTRY(ifp, fs, ino, bp);
417 daddr = ifp->if_daddr;
418 brelse(bp, 0);
419 if (daddr <= 0 || dtosn(fs, daddr) >= fs->lfs_nseg)
420 return NULL;
421 return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
422 }
423
424 /* Check superblock magic number and checksum */
425 static int
426 check_sb(struct lfs *fs)
427 {
428 u_int32_t checksum;
429
430 if (fs->lfs_magic != LFS_MAGIC) {
431 printf("Superblock magic number (0x%lx) does not match "
432 "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
433 (unsigned long) LFS_MAGIC);
434 return 1;
435 }
436 /* checksum */
437 checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
438 if (fs->lfs_cksum != checksum) {
439 printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
440 (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
441 return 1;
442 }
443 return 0;
444 }
445
446 /* Initialize LFS library; load superblocks and choose which to use. */
447 struct lfs *
448 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
449 {
450 struct uvnode *devvp;
451 struct ubuf *bp;
452 int tryalt;
453 struct lfs *fs, *altfs;
454 int error;
455
456 vfs_init();
457
458 devvp = ecalloc(1, sizeof(*devvp));
459 devvp->v_fs = NULL;
460 devvp->v_fd = devfd;
461 devvp->v_strategy_op = raw_vop_strategy;
462 devvp->v_bwrite_op = raw_vop_bwrite;
463 devvp->v_bmap_op = raw_vop_bmap;
464 LIST_INIT(&devvp->v_cleanblkhd);
465 LIST_INIT(&devvp->v_dirtyblkhd);
466
467 tryalt = 0;
468 if (dummy_read) {
469 if (sblkno == 0)
470 sblkno = btodb(LFS_LABELPAD);
471 fs = ecalloc(1, sizeof(*fs));
472 fs->lfs_devvp = devvp;
473 } else {
474 if (sblkno == 0) {
475 sblkno = btodb(LFS_LABELPAD);
476 tryalt = 1;
477 } else if (debug) {
478 printf("No -b flag given, not attempting to verify checkpoint\n");
479 }
480 error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, 0, &bp);
481 fs = ecalloc(1, sizeof(*fs));
482 fs->lfs_dlfs = *((struct dlfs *) bp->b_data);
483 fs->lfs_devvp = devvp;
484 bp->b_flags |= B_INVAL;
485 brelse(bp, 0);
486
487 if (tryalt) {
488 error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
489 LFS_SBPAD, NOCRED, 0, &bp);
490 altfs = ecalloc(1, sizeof(*altfs));
491 altfs->lfs_dlfs = *((struct dlfs *) bp->b_data);
492 altfs->lfs_devvp = devvp;
493 bp->b_flags |= B_INVAL;
494 brelse(bp, 0);
495
496 if (check_sb(fs) || fs->lfs_idaddr <= 0) {
497 if (debug)
498 printf("Primary superblock is no good, using first alternate\n");
499 free(fs);
500 fs = altfs;
501 } else {
502 /* If both superblocks check out, try verification */
503 if (check_sb(altfs)) {
504 if (debug)
505 printf("First alternate superblock is no good, using primary\n");
506 free(altfs);
507 } else {
508 if (lfs_verify(fs, altfs, devvp, debug) == fs) {
509 free(altfs);
510 } else {
511 free(fs);
512 fs = altfs;
513 }
514 }
515 }
516 }
517 if (check_sb(fs)) {
518 free(fs);
519 return NULL;
520 }
521 }
522
523 /* Compatibility */
524 if (fs->lfs_version < 2) {
525 fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
526 fs->lfs_ibsize = fs->lfs_bsize;
527 fs->lfs_start = fs->lfs_sboffs[0];
528 fs->lfs_tstamp = fs->lfs_otstamp;
529 fs->lfs_fsbtodb = 0;
530 }
531
532 if (!dummy_read) {
533 fs->lfs_suflags = emalloc(2 * sizeof(u_int32_t *));
534 fs->lfs_suflags[0] = emalloc(fs->lfs_nseg * sizeof(u_int32_t));
535 fs->lfs_suflags[1] = emalloc(fs->lfs_nseg * sizeof(u_int32_t));
536 }
537
538 if (idaddr == 0)
539 idaddr = fs->lfs_idaddr;
540 else
541 fs->lfs_idaddr = idaddr;
542 /* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
543 fs->lfs_ivnode = lfs_raw_vget(fs,
544 (dummy_read ? LFS_IFILE_INUM : fs->lfs_ifile), devvp->v_fd,
545 idaddr);
546 if (fs->lfs_ivnode == NULL)
547 return NULL;
548
549 register_vget((void *)fs, lfs_vget);
550
551 return fs;
552 }
553
554 /*
555 * Check partial segment validity between fs->lfs_offset and the given goal.
556 *
557 * If goal == 0, just keep on going until the segments stop making sense,
558 * and return the address of the last valid partial segment.
559 *
560 * If goal != 0, return the address of the first partial segment that failed,
561 * or "goal" if we reached it without failure (the partial segment *at* goal
562 * need not be valid).
563 */
564 ufs_daddr_t
565 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
566 {
567 ufs_daddr_t daddr, odaddr;
568 SEGSUM *sp;
569 int i, bc, hitclean;
570 struct ubuf *bp;
571 ufs_daddr_t nodirop_daddr;
572 u_int64_t serial;
573
574 bc = 0;
575 hitclean = 0;
576 odaddr = -1;
577 daddr = osb->lfs_offset;
578 nodirop_daddr = daddr;
579 serial = osb->lfs_serial;
580 while (daddr != goal) {
581 /*
582 * Don't mistakenly read a superblock, if there is one here.
583 */
584 if (sntod(osb, dtosn(osb, daddr)) == daddr) {
585 if (daddr == osb->lfs_start)
586 daddr += btofsb(osb, LFS_LABELPAD);
587 for (i = 0; i < LFS_MAXNUMSB; i++) {
588 if (osb->lfs_sboffs[i] < daddr)
589 break;
590 if (osb->lfs_sboffs[i] == daddr)
591 daddr += btofsb(osb, LFS_SBPAD);
592 }
593 }
594
595 /* Read in summary block */
596 bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize,
597 NULL, 0, &bp);
598 sp = (SEGSUM *)bp->b_data;
599
600 /*
601 * Check for a valid segment summary belonging to our fs.
602 */
603 if (sp->ss_magic != SS_MAGIC ||
604 sp->ss_ident != osb->lfs_ident ||
605 sp->ss_serial < serial || /* XXX strengthen this */
606 sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
607 sizeof(sp->ss_sumsum))) {
608 brelse(bp, 0);
609 if (debug) {
610 if (sp->ss_magic != SS_MAGIC)
611 pwarn("pseg at 0x%x: "
612 "wrong magic number\n",
613 (int)daddr);
614 else if (sp->ss_ident != osb->lfs_ident)
615 pwarn("pseg at 0x%x: "
616 "expected ident %llx, got %llx\n",
617 (int)daddr,
618 (long long)sp->ss_ident,
619 (long long)osb->lfs_ident);
620 else if (sp->ss_serial >= serial)
621 pwarn("pseg at 0x%x: "
622 "serial %d < %d\n", (int)daddr,
623 (int)sp->ss_serial, (int)serial);
624 else
625 pwarn("pseg at 0x%x: "
626 "summary checksum wrong\n",
627 (int)daddr);
628 }
629 break;
630 }
631 if (debug && sp->ss_serial != serial)
632 pwarn("warning, serial=%d ss_serial=%d\n",
633 (int)serial, (int)sp->ss_serial);
634 ++serial;
635 bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
636 if (bc == 0) {
637 brelse(bp, 0);
638 break;
639 }
640 if (debug)
641 pwarn("summary good: 0x%x/%d\n", (int)daddr,
642 (int)sp->ss_serial);
643 assert (bc > 0);
644 odaddr = daddr;
645 daddr += btofsb(osb, osb->lfs_sumsize + bc);
646 if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
647 dtosn(osb, daddr) != dtosn(osb, daddr +
648 btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize) - 1)) {
649 daddr = sp->ss_next;
650 }
651
652 /*
653 * Check for the beginning and ending of a sequence of
654 * dirops. Writes from the cleaner never involve new
655 * information, and are always checkpoints; so don't try
656 * to roll forward through them. Likewise, psegs written
657 * by a previous roll-forward attempt are not interesting.
658 */
659 if (sp->ss_flags & (SS_CLEAN | SS_RFW))
660 hitclean = 1;
661 if (hitclean == 0 && (sp->ss_flags & SS_CONT) == 0)
662 nodirop_daddr = daddr;
663
664 brelse(bp, 0);
665 }
666
667 if (goal == 0)
668 return nodirop_daddr;
669 else
670 return daddr;
671 }
672
673 /* Use try_verify to check whether the newer superblock is valid. */
674 struct lfs *
675 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
676 {
677 ufs_daddr_t daddr;
678 struct lfs *osb, *nsb;
679
680 /*
681 * Verify the checkpoint of the newer superblock,
682 * if the timestamp/serial number of the two superblocks is
683 * different.
684 */
685
686 osb = NULL;
687 if (debug)
688 pwarn("sb0 %lld, sb1 %lld",
689 (long long) sb0->lfs_serial,
690 (long long) sb1->lfs_serial);
691
692 if ((sb0->lfs_version == 1 &&
693 sb0->lfs_otstamp != sb1->lfs_otstamp) ||
694 (sb0->lfs_version > 1 &&
695 sb0->lfs_serial != sb1->lfs_serial)) {
696 if (sb0->lfs_version == 1) {
697 if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
698 osb = sb1;
699 nsb = sb0;
700 } else {
701 osb = sb0;
702 nsb = sb1;
703 }
704 } else {
705 if (sb0->lfs_serial > sb1->lfs_serial) {
706 osb = sb1;
707 nsb = sb0;
708 } else {
709 osb = sb0;
710 nsb = sb1;
711 }
712 }
713 if (debug) {
714 printf("Attempting to verify newer checkpoint...");
715 fflush(stdout);
716 }
717 daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
718
719 if (debug)
720 printf("done.\n");
721 if (daddr == nsb->lfs_offset) {
722 pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
723 (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
724 sbdirty();
725 } else {
726 pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
727 }
728 return (daddr == nsb->lfs_offset ? nsb : osb);
729 }
730 /* Nothing to check */
731 return osb;
732 }
733
734 /* Verify a partial-segment summary; return the number of bytes on disk. */
735 int
736 check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
737 struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
738 {
739 FINFO *fp;
740 int bc; /* Bytes in partial segment */
741 int nblocks;
742 ufs_daddr_t seg_addr, daddr;
743 ufs_daddr_t *dp, *idp;
744 struct ubuf *bp;
745 int i, j, k, datac, len;
746 long sn;
747 u_int32_t *datap;
748 u_int32_t ccksum;
749
750 sn = dtosn(fs, pseg_addr);
751 seg_addr = sntod(fs, sn);
752
753 /* We've already checked the sumsum, just do the data bounds and sum */
754
755 /* Count the blocks. */
756 nblocks = howmany(sp->ss_ninos, INOPB(fs));
757 bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
758 assert(bc >= 0);
759
760 fp = (FINFO *) (sp + 1);
761 for (i = 0; i < sp->ss_nfinfo; i++) {
762 nblocks += fp->fi_nblocks;
763 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
764 << fs->lfs_bshift);
765 assert(bc >= 0);
766 fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
767 if (((char *)fp) - (char *)sp > fs->lfs_sumsize)
768 return 0;
769 }
770 datap = emalloc(nblocks * sizeof(*datap));
771 datac = 0;
772
773 dp = (ufs_daddr_t *) sp;
774 dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
775 dp--;
776
777 idp = dp;
778 daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
779 fp = (FINFO *) (sp + 1);
780 for (i = 0, j = 0;
781 i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
782 if (i >= sp->ss_nfinfo && *idp != daddr) {
783 pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
784 ": found %d, wanted %d\n",
785 pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
786 if (debug)
787 pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
788 daddr);
789 break;
790 }
791 while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
792 bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
793 NOCRED, 0, &bp);
794 datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
795 brelse(bp, 0);
796
797 ++j;
798 daddr += btofsb(fs, fs->lfs_ibsize);
799 --idp;
800 }
801 if (i < sp->ss_nfinfo) {
802 if (func)
803 func(daddr, fp);
804 for (k = 0; k < fp->fi_nblocks; k++) {
805 len = (k == fp->fi_nblocks - 1 ?
806 fp->fi_lastlength
807 : fs->lfs_bsize);
808 bread(devvp, fsbtodb(fs, daddr), len,
809 NOCRED, 0, &bp);
810 datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
811 brelse(bp, 0);
812 daddr += btofsb(fs, len);
813 }
814 fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
815 }
816 }
817
818 if (datac != nblocks) {
819 pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
820 (long long) pseg_addr, nblocks, datac);
821 }
822 ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
823 /* Check the data checksum */
824 if (ccksum != sp->ss_datasum) {
825 pwarn("Partial segment at 0x%" PRIx32 " data checksum"
826 " mismatch: given 0x%x, computed 0x%x\n",
827 pseg_addr, sp->ss_datasum, ccksum);
828 free(datap);
829 return 0;
830 }
831 free(datap);
832 assert(bc >= 0);
833 return bc;
834 }
835
836 /* print message and exit */
837 void
838 my_vpanic(int fatal, const char *fmt, va_list ap)
839 {
840 (void) vprintf(fmt, ap);
841 exit(8);
842 }
843
844 void
845 call_panic(const char *fmt, ...)
846 {
847 va_list ap;
848
849 va_start(ap, fmt);
850 panic_func(1, fmt, ap);
851 va_end(ap);
852 }
853
854 /* Allocate a new inode. */
855 struct uvnode *
856 lfs_valloc(struct lfs *fs, ino_t ino)
857 {
858 struct ubuf *bp, *cbp;
859 struct ifile *ifp;
860 ino_t new_ino;
861 int error;
862 int new_gen;
863 CLEANERINFO *cip;
864
865 /* Get the head of the freelist. */
866 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
867
868 /*
869 * Remove the inode from the free list and write the new start
870 * of the free list into the superblock.
871 */
872 LFS_IENTRY(ifp, fs, new_ino, bp);
873 if (ifp->if_daddr != LFS_UNUSED_DADDR)
874 panic("lfs_valloc: inuse inode %d on the free list", new_ino);
875 LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
876
877 new_gen = ifp->if_version; /* version was updated by vfree */
878 brelse(bp, 0);
879
880 /* Extend IFILE so that the next lfs_valloc will succeed. */
881 if (fs->lfs_freehd == LFS_UNUSED_INUM) {
882 if ((error = extend_ifile(fs)) != 0) {
883 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
884 return NULL;
885 }
886 }
887
888 /* Set superblock modified bit and increment file count. */
889 sbdirty();
890 ++fs->lfs_nfiles;
891
892 return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
893 }
894
895 #ifdef IN_FSCK_LFS
896 void reset_maxino(ino_t);
897 #endif
898
899 /*
900 * Add a new block to the Ifile, to accommodate future file creations.
901 */
902 int
903 extend_ifile(struct lfs *fs)
904 {
905 struct uvnode *vp;
906 struct inode *ip;
907 IFILE *ifp;
908 IFILE_V1 *ifp_v1;
909 struct ubuf *bp, *cbp;
910 daddr_t i, blkno, max;
911 ino_t oldlast;
912 CLEANERINFO *cip;
913
914 vp = fs->lfs_ivnode;
915 ip = VTOI(vp);
916 blkno = lblkno(fs, ip->i_ffs1_size);
917
918 lfs_balloc(vp, ip->i_ffs1_size, fs->lfs_bsize, &bp);
919 ip->i_ffs1_size += fs->lfs_bsize;
920 ip->i_flag |= IN_MODIFIED;
921
922 i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
923 fs->lfs_ifpb;
924 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
925 LFS_PUT_HEADFREE(fs, cip, cbp, i);
926 max = i + fs->lfs_ifpb;
927 fs->lfs_bfree -= btofsb(fs, fs->lfs_bsize);
928
929 if (fs->lfs_version == 1) {
930 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
931 ifp_v1->if_version = 1;
932 ifp_v1->if_daddr = LFS_UNUSED_DADDR;
933 ifp_v1->if_nextfree = ++i;
934 }
935 ifp_v1--;
936 ifp_v1->if_nextfree = oldlast;
937 } else {
938 for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
939 ifp->if_version = 1;
940 ifp->if_daddr = LFS_UNUSED_DADDR;
941 ifp->if_nextfree = ++i;
942 }
943 ifp--;
944 ifp->if_nextfree = oldlast;
945 }
946 LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
947
948 LFS_BWRITE_LOG(bp);
949
950 #ifdef IN_FSCK_LFS
951 reset_maxino(((ip->i_ffs1_size >> fs->lfs_bshift) - fs->lfs_segtabsz -
952 fs->lfs_cleansz) * fs->lfs_ifpb);
953 #endif
954 return 0;
955 }
956
957 /*
958 * Allocate a block, and to inode and filesystem block accounting for it
959 * and for any indirect blocks the may need to be created in order for
960 * this block to be created.
961 *
962 * Blocks which have never been accounted for (i.e., which "do not exist")
963 * have disk address 0, which is translated by ufs_bmap to the special value
964 * UNASSIGNED == -1, as in the historical UFS.
965 *
966 * Blocks which have been accounted for but which have not yet been written
967 * to disk are given the new special disk address UNWRITTEN == -2, so that
968 * they can be differentiated from completely new blocks.
969 */
970 int
971 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
972 {
973 int offset;
974 daddr_t daddr, idaddr;
975 struct ubuf *ibp, *bp;
976 struct inode *ip;
977 struct lfs *fs;
978 struct indir indirs[NIADDR+2], *idp;
979 daddr_t lbn, lastblock;
980 int bb, bcount;
981 int error, frags, i, nsize, osize, num;
982
983 ip = VTOI(vp);
984 fs = ip->i_lfs;
985 offset = blkoff(fs, startoffset);
986 lbn = lblkno(fs, startoffset);
987
988 /*
989 * Three cases: it's a block beyond the end of file, it's a block in
990 * the file that may or may not have been assigned a disk address or
991 * we're writing an entire block.
992 *
993 * Note, if the daddr is UNWRITTEN, the block already exists in
994 * the cache (it was read or written earlier). If so, make sure
995 * we don't count it as a new block or zero out its contents. If
996 * it did not, make sure we allocate any necessary indirect
997 * blocks.
998 *
999 * If we are writing a block beyond the end of the file, we need to
1000 * check if the old last block was a fragment. If it was, we need
1001 * to rewrite it.
1002 */
1003
1004 if (bpp)
1005 *bpp = NULL;
1006
1007 /* Check for block beyond end of file and fragment extension needed. */
1008 lastblock = lblkno(fs, ip->i_ffs1_size);
1009 if (lastblock < NDADDR && lastblock < lbn) {
1010 osize = blksize(fs, ip, lastblock);
1011 if (osize < fs->lfs_bsize && osize > 0) {
1012 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
1013 lastblock,
1014 (bpp ? &bp : NULL))))
1015 return (error);
1016 ip->i_ffs1_size = ip->i_ffs1_size =
1017 (lastblock + 1) * fs->lfs_bsize;
1018 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1019 if (bpp)
1020 (void) VOP_BWRITE(bp);
1021 }
1022 }
1023
1024 /*
1025 * If the block we are writing is a direct block, it's the last
1026 * block in the file, and offset + iosize is less than a full
1027 * block, we can write one or more fragments. There are two cases:
1028 * the block is brand new and we should allocate it the correct
1029 * size or it already exists and contains some fragments and
1030 * may need to extend it.
1031 */
1032 if (lbn < NDADDR && lblkno(fs, ip->i_ffs1_size) <= lbn) {
1033 osize = blksize(fs, ip, lbn);
1034 nsize = fragroundup(fs, offset + iosize);
1035 if (lblktosize(fs, lbn) >= ip->i_ffs1_size) {
1036 /* Brand new block or fragment */
1037 frags = numfrags(fs, nsize);
1038 bb = fragstofsb(fs, frags);
1039 if (bpp) {
1040 *bpp = bp = getblk(vp, lbn, nsize);
1041 bp->b_blkno = UNWRITTEN;
1042 }
1043 ip->i_lfs_effnblks += bb;
1044 fs->lfs_bfree -= bb;
1045 ip->i_ffs1_db[lbn] = UNWRITTEN;
1046 } else {
1047 if (nsize <= osize) {
1048 /* No need to extend */
1049 if (bpp && (error = bread(vp, lbn, osize,
1050 NOCRED, 0, &bp)))
1051 return error;
1052 } else {
1053 /* Extend existing block */
1054 if ((error =
1055 lfs_fragextend(vp, osize, nsize, lbn,
1056 (bpp ? &bp : NULL))))
1057 return error;
1058 }
1059 if (bpp)
1060 *bpp = bp;
1061 }
1062 return 0;
1063 }
1064
1065 error = ufs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
1066 if (error)
1067 return (error);
1068
1069 daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
1070
1071 /*
1072 * Do byte accounting all at once, so we can gracefully fail *before*
1073 * we start assigning blocks.
1074 */
1075 bb = fsbtodb(fs, 1); /* bb = VFSTOUFS(vp->v_mount)->um_seqinc; */
1076 bcount = 0;
1077 if (daddr == UNASSIGNED) {
1078 bcount = bb;
1079 }
1080 for (i = 1; i < num; ++i) {
1081 if (!indirs[i].in_exists) {
1082 bcount += bb;
1083 }
1084 }
1085 fs->lfs_bfree -= bcount;
1086 ip->i_lfs_effnblks += bcount;
1087
1088 if (daddr == UNASSIGNED) {
1089 if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
1090 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
1091 }
1092
1093 /*
1094 * Create new indirect blocks if necessary
1095 */
1096 if (num > 1) {
1097 idaddr = ip->i_ffs1_ib[indirs[0].in_off];
1098 for (i = 1; i < num; ++i) {
1099 ibp = getblk(vp, indirs[i].in_lbn,
1100 fs->lfs_bsize);
1101 if (!indirs[i].in_exists) {
1102 memset(ibp->b_data, 0, ibp->b_bufsize);
1103 ibp->b_blkno = UNWRITTEN;
1104 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
1105 ibp->b_blkno = fsbtodb(fs, idaddr);
1106 ibp->b_flags |= B_READ;
1107 VOP_STRATEGY(ibp);
1108 }
1109 /*
1110 * This block exists, but the next one may not.
1111 * If that is the case mark it UNWRITTEN to
1112 * keep the accounting straight.
1113 */
1114 /* XXX ondisk32 */
1115 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
1116 ((int32_t *)ibp->b_data)[indirs[i].in_off] =
1117 UNWRITTEN;
1118 /* XXX ondisk32 */
1119 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
1120 if ((error = VOP_BWRITE(ibp)))
1121 return error;
1122 }
1123 }
1124 }
1125
1126
1127 /*
1128 * Get the existing block from the cache, if requested.
1129 */
1130 frags = fsbtofrags(fs, bb);
1131 if (bpp)
1132 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn));
1133
1134 /*
1135 * The block we are writing may be a brand new block
1136 * in which case we need to do accounting.
1137 *
1138 * We can tell a truly new block because ufs_bmaparray will say
1139 * it is UNASSIGNED. Once we allocate it we will assign it the
1140 * disk address UNWRITTEN.
1141 */
1142 if (daddr == UNASSIGNED) {
1143 if (bpp) {
1144 /* Note the new address */
1145 bp->b_blkno = UNWRITTEN;
1146 }
1147
1148 switch (num) {
1149 case 0:
1150 ip->i_ffs1_db[lbn] = UNWRITTEN;
1151 break;
1152 case 1:
1153 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
1154 break;
1155 default:
1156 idp = &indirs[num - 1];
1157 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
1158 0, &ibp))
1159 panic("lfs_balloc: bread bno %lld",
1160 (long long)idp->in_lbn);
1161 /* XXX ondisk32 */
1162 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
1163 VOP_BWRITE(ibp);
1164 }
1165 } else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
1166 /*
1167 * Not a brand new block, also not in the cache;
1168 * read it in from disk.
1169 */
1170 if (iosize == fs->lfs_bsize)
1171 /* Optimization: I/O is unnecessary. */
1172 bp->b_blkno = daddr;
1173 else {
1174 /*
1175 * We need to read the block to preserve the
1176 * existing bytes.
1177 */
1178 bp->b_blkno = daddr;
1179 bp->b_flags |= B_READ;
1180 VOP_STRATEGY(bp);
1181 return 0;
1182 }
1183 }
1184
1185 return (0);
1186 }
1187
1188 int
1189 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
1190 struct ubuf **bpp)
1191 {
1192 struct inode *ip;
1193 struct lfs *fs;
1194 long bb;
1195 int error;
1196 size_t obufsize;
1197
1198 ip = VTOI(vp);
1199 fs = ip->i_lfs;
1200 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
1201 error = 0;
1202
1203 /*
1204 * If we are not asked to actually return the block, all we need
1205 * to do is allocate space for it. UBC will handle dirtying the
1206 * appropriate things and making sure it all goes to disk.
1207 * Don't bother to read in that case.
1208 */
1209 if (bpp && (error = bread(vp, lbn, osize, NOCRED, 0, bpp))) {
1210 brelse(*bpp, 0);
1211 goto out;
1212 }
1213
1214 fs->lfs_bfree -= bb;
1215 ip->i_lfs_effnblks += bb;
1216 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1217
1218 if (bpp) {
1219 obufsize = (*bpp)->b_bufsize;
1220 (*bpp)->b_data = erealloc((*bpp)->b_data, nsize);
1221 (void)memset((*bpp)->b_data + osize, 0, nsize - osize);
1222 }
1223
1224 out:
1225 return (error);
1226 }
1227