lfs.c revision 1.2 1 1.2 fvdl /* $NetBSD: lfs.c,v 1.2 2003/04/02 10:39:27 fvdl Exp $ */
2 1.1 perseant /*-
3 1.1 perseant * Copyright (c) 2003 The NetBSD Foundation, Inc.
4 1.1 perseant * All rights reserved.
5 1.1 perseant *
6 1.1 perseant * This code is derived from software contributed to The NetBSD Foundation
7 1.1 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
8 1.1 perseant *
9 1.1 perseant * Redistribution and use in source and binary forms, with or without
10 1.1 perseant * modification, are permitted provided that the following conditions
11 1.1 perseant * are met:
12 1.1 perseant * 1. Redistributions of source code must retain the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer.
14 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 perseant * notice, this list of conditions and the following disclaimer in the
16 1.1 perseant * documentation and/or other materials provided with the distribution.
17 1.1 perseant * 3. All advertising materials mentioning features or use of this software
18 1.1 perseant * must display the following acknowledgement:
19 1.1 perseant * This product includes software developed by the NetBSD
20 1.1 perseant * Foundation, Inc. and its contributors.
21 1.1 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 perseant * contributors may be used to endorse or promote products derived
23 1.1 perseant * from this software without specific prior written permission.
24 1.1 perseant *
25 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 perseant * POSSIBILITY OF SUCH DAMAGE.
36 1.1 perseant */
37 1.1 perseant /*
38 1.1 perseant * Copyright (c) 1989, 1991, 1993
39 1.1 perseant * The Regents of the University of California. All rights reserved.
40 1.1 perseant * (c) UNIX System Laboratories, Inc.
41 1.1 perseant * All or some portions of this file are derived from material licensed
42 1.1 perseant * to the University of California by American Telephone and Telegraph
43 1.1 perseant * Co. or Unix System Laboratories, Inc. and are reproduced herein with
44 1.1 perseant * the permission of UNIX System Laboratories, Inc.
45 1.1 perseant *
46 1.1 perseant * Redistribution and use in source and binary forms, with or without
47 1.1 perseant * modification, are permitted provided that the following conditions
48 1.1 perseant * are met:
49 1.1 perseant * 1. Redistributions of source code must retain the above copyright
50 1.1 perseant * notice, this list of conditions and the following disclaimer.
51 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 perseant * notice, this list of conditions and the following disclaimer in the
53 1.1 perseant * documentation and/or other materials provided with the distribution.
54 1.1 perseant * 3. All advertising materials mentioning features or use of this software
55 1.1 perseant * must display the following acknowledgement:
56 1.1 perseant * This product includes software developed by the University of
57 1.1 perseant * California, Berkeley and its contributors.
58 1.1 perseant * 4. Neither the name of the University nor the names of its contributors
59 1.1 perseant * may be used to endorse or promote products derived from this software
60 1.1 perseant * without specific prior written permission.
61 1.1 perseant *
62 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.1 perseant * SUCH DAMAGE.
73 1.1 perseant *
74 1.1 perseant * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95
75 1.1 perseant */
76 1.1 perseant
77 1.1 perseant
78 1.1 perseant #include <sys/types.h>
79 1.1 perseant #include <sys/param.h>
80 1.1 perseant #include <sys/time.h>
81 1.1 perseant #include <sys/buf.h>
82 1.1 perseant #include <sys/mount.h>
83 1.1 perseant
84 1.1 perseant #include <ufs/ufs/inode.h>
85 1.1 perseant #include <ufs/ufs/ufsmount.h>
86 1.1 perseant #define vnode uvnode
87 1.1 perseant #include <ufs/lfs/lfs.h>
88 1.1 perseant #undef vnode
89 1.1 perseant
90 1.1 perseant #include <assert.h>
91 1.1 perseant #include <err.h>
92 1.1 perseant #include <errno.h>
93 1.1 perseant #include <stdarg.h>
94 1.1 perseant #include <stdio.h>
95 1.1 perseant #include <stdlib.h>
96 1.1 perseant #include <string.h>
97 1.1 perseant #include <unistd.h>
98 1.1 perseant
99 1.1 perseant #include "bufcache.h"
100 1.1 perseant #include "vnode.h"
101 1.1 perseant #include "lfs.h"
102 1.1 perseant #include "segwrite.h"
103 1.1 perseant
104 1.1 perseant #define panic call_panic
105 1.1 perseant
106 1.1 perseant extern u_int32_t cksum(void *, size_t);
107 1.1 perseant extern u_int32_t lfs_sb_cksum(struct dlfs *);
108 1.1 perseant
109 1.1 perseant extern struct uvnodelst vnodelist;
110 1.1 perseant extern struct uvnodelst getvnodelist;
111 1.1 perseant extern int nvnodes;
112 1.1 perseant
113 1.1 perseant int fsdirty = 0;
114 1.1 perseant void (*panic_func)(int, const char *, va_list) = my_vpanic;
115 1.1 perseant
116 1.1 perseant /*
117 1.1 perseant * LFS buffer and uvnode operations
118 1.1 perseant */
119 1.1 perseant
120 1.1 perseant int
121 1.1 perseant lfs_vop_strategy(struct ubuf * bp)
122 1.1 perseant {
123 1.1 perseant int count;
124 1.1 perseant
125 1.1 perseant if (bp->b_flags & B_READ) {
126 1.1 perseant count = pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
127 1.1 perseant dbtob(bp->b_blkno));
128 1.1 perseant if (count == bp->b_bcount)
129 1.1 perseant bp->b_flags |= B_DONE;
130 1.1 perseant } else {
131 1.1 perseant count = pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
132 1.1 perseant dbtob(bp->b_blkno));
133 1.1 perseant if (count == 0) {
134 1.1 perseant perror("pwrite");
135 1.1 perseant return -1;
136 1.1 perseant }
137 1.1 perseant bp->b_flags &= ~B_DELWRI;
138 1.1 perseant reassignbuf(bp, bp->b_vp);
139 1.1 perseant }
140 1.1 perseant return 0;
141 1.1 perseant }
142 1.1 perseant
143 1.1 perseant int
144 1.1 perseant lfs_vop_bwrite(struct ubuf * bp)
145 1.1 perseant {
146 1.1 perseant struct lfs *fs;
147 1.1 perseant
148 1.1 perseant fs = bp->b_vp->v_fs;
149 1.1 perseant if (!(bp->b_flags & B_DELWRI)) {
150 1.1 perseant fs->lfs_avail -= btofsb(fs, bp->b_bcount);
151 1.1 perseant }
152 1.1 perseant bp->b_flags |= B_DELWRI | B_LOCKED;
153 1.1 perseant reassignbuf(bp, bp->b_vp);
154 1.1 perseant brelse(bp);
155 1.1 perseant return 0;
156 1.1 perseant }
157 1.1 perseant
158 1.1 perseant /*
159 1.1 perseant * ufs_bmaparray does the bmap conversion, and if requested returns the
160 1.1 perseant * array of logical blocks which must be traversed to get to a block.
161 1.1 perseant * Each entry contains the offset into that block that gets you to the
162 1.1 perseant * next block and the disk address of the block (if it is assigned).
163 1.1 perseant */
164 1.1 perseant int
165 1.1 perseant ufs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump)
166 1.1 perseant {
167 1.1 perseant struct inode *ip;
168 1.1 perseant struct ubuf *bp;
169 1.1 perseant struct indir a[NIADDR + 1], *xap;
170 1.1 perseant daddr_t daddr;
171 1.1 perseant daddr_t metalbn;
172 1.1 perseant int error, num;
173 1.1 perseant
174 1.1 perseant ip = VTOI(vp);
175 1.1 perseant
176 1.1 perseant if (bn >= 0 && bn < NDADDR) {
177 1.1 perseant if (nump != NULL)
178 1.1 perseant *nump = 0;
179 1.2 fvdl *bnp = fsbtodb(fs, ip->i_ffs1_db[bn]);
180 1.1 perseant if (*bnp == 0)
181 1.1 perseant *bnp = -1;
182 1.1 perseant return (0);
183 1.1 perseant }
184 1.1 perseant xap = ap == NULL ? a : ap;
185 1.1 perseant if (!nump)
186 1.1 perseant nump = #
187 1.1 perseant if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0)
188 1.1 perseant return (error);
189 1.1 perseant
190 1.1 perseant num = *nump;
191 1.1 perseant
192 1.1 perseant /* Get disk address out of indirect block array */
193 1.2 fvdl daddr = ip->i_ffs1_ib[xap->in_off];
194 1.1 perseant
195 1.1 perseant for (bp = NULL, ++xap; --num; ++xap) {
196 1.1 perseant /* Exit the loop if there is no disk address assigned yet and
197 1.1 perseant * the indirect block isn't in the cache, or if we were
198 1.1 perseant * looking for an indirect block and we've found it. */
199 1.1 perseant
200 1.1 perseant metalbn = xap->in_lbn;
201 1.1 perseant if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
202 1.1 perseant break;
203 1.1 perseant /*
204 1.1 perseant * If we get here, we've either got the block in the cache
205 1.1 perseant * or we have a disk address for it, go fetch it.
206 1.1 perseant */
207 1.1 perseant if (bp)
208 1.1 perseant brelse(bp);
209 1.1 perseant
210 1.1 perseant xap->in_exists = 1;
211 1.1 perseant bp = getblk(vp, metalbn, fs->lfs_bsize);
212 1.1 perseant
213 1.1 perseant if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
214 1.1 perseant bp->b_blkno = fsbtodb(fs, daddr);
215 1.1 perseant bp->b_flags |= B_READ;
216 1.1 perseant VOP_STRATEGY(bp);
217 1.1 perseant }
218 1.1 perseant daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off];
219 1.1 perseant }
220 1.1 perseant if (bp)
221 1.1 perseant brelse(bp);
222 1.1 perseant
223 1.1 perseant daddr = fsbtodb(fs, (ufs_daddr_t) daddr);
224 1.1 perseant *bnp = daddr == 0 ? -1 : daddr;
225 1.1 perseant return (0);
226 1.1 perseant }
227 1.1 perseant
228 1.1 perseant /*
229 1.1 perseant * Create an array of logical block number/offset pairs which represent the
230 1.1 perseant * path of indirect blocks required to access a data block. The first "pair"
231 1.1 perseant * contains the logical block number of the appropriate single, double or
232 1.1 perseant * triple indirect block and the offset into the inode indirect block array.
233 1.1 perseant * Note, the logical block number of the inode single/double/triple indirect
234 1.2 fvdl * block appears twice in the array, once with the offset into the i_ffs1_ib and
235 1.1 perseant * once with the offset into the page itself.
236 1.1 perseant */
237 1.1 perseant int
238 1.1 perseant ufs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump)
239 1.1 perseant {
240 1.1 perseant daddr_t metalbn, realbn;
241 1.1 perseant int64_t blockcnt;
242 1.1 perseant int lbc;
243 1.1 perseant int i, numlevels, off;
244 1.1 perseant int lognindir, indir;
245 1.1 perseant
246 1.1 perseant if (nump)
247 1.1 perseant *nump = 0;
248 1.1 perseant numlevels = 0;
249 1.1 perseant realbn = bn;
250 1.1 perseant if (bn < 0)
251 1.1 perseant bn = -bn;
252 1.1 perseant
253 1.1 perseant lognindir = -1;
254 1.1 perseant for (indir = fs->lfs_nindir; indir; indir >>= 1)
255 1.1 perseant ++lognindir;
256 1.1 perseant
257 1.1 perseant /* Determine the number of levels of indirection. After this loop is
258 1.1 perseant * done, blockcnt indicates the number of data blocks possible at the
259 1.1 perseant * given level of indirection, and NIADDR - i is the number of levels
260 1.1 perseant * of indirection needed to locate the requested block. */
261 1.1 perseant
262 1.1 perseant bn -= NDADDR;
263 1.1 perseant for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
264 1.1 perseant if (i == 0)
265 1.1 perseant return (EFBIG);
266 1.1 perseant
267 1.1 perseant lbc += lognindir;
268 1.1 perseant blockcnt = (int64_t) 1 << lbc;
269 1.1 perseant
270 1.1 perseant if (bn < blockcnt)
271 1.1 perseant break;
272 1.1 perseant }
273 1.1 perseant
274 1.1 perseant /* Calculate the address of the first meta-block. */
275 1.1 perseant if (realbn >= 0)
276 1.1 perseant metalbn = -(realbn - bn + NIADDR - i);
277 1.1 perseant else
278 1.1 perseant metalbn = -(-realbn - bn + NIADDR - i);
279 1.1 perseant
280 1.1 perseant /* At each iteration, off is the offset into the bap array which is an
281 1.1 perseant * array of disk addresses at the current level of indirection. The
282 1.1 perseant * logical block number and the offset in that block are stored into
283 1.1 perseant * the argument array. */
284 1.1 perseant ap->in_lbn = metalbn;
285 1.1 perseant ap->in_off = off = NIADDR - i;
286 1.1 perseant ap->in_exists = 0;
287 1.1 perseant ap++;
288 1.1 perseant for (++numlevels; i <= NIADDR; i++) {
289 1.1 perseant /* If searching for a meta-data block, quit when found. */
290 1.1 perseant if (metalbn == realbn)
291 1.1 perseant break;
292 1.1 perseant
293 1.1 perseant lbc -= lognindir;
294 1.1 perseant blockcnt = (int64_t) 1 << lbc;
295 1.1 perseant off = (bn >> lbc) & (fs->lfs_nindir - 1);
296 1.1 perseant
297 1.1 perseant ++numlevels;
298 1.1 perseant ap->in_lbn = metalbn;
299 1.1 perseant ap->in_off = off;
300 1.1 perseant ap->in_exists = 0;
301 1.1 perseant ++ap;
302 1.1 perseant
303 1.1 perseant metalbn -= -1 + (off << lbc);
304 1.1 perseant }
305 1.1 perseant if (nump)
306 1.1 perseant *nump = numlevels;
307 1.1 perseant return (0);
308 1.1 perseant }
309 1.1 perseant
310 1.1 perseant int
311 1.1 perseant lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
312 1.1 perseant {
313 1.1 perseant return ufs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
314 1.1 perseant }
315 1.1 perseant
316 1.1 perseant /* Search a block for a specific dinode. */
317 1.2 fvdl struct ufs1_dinode *
318 1.1 perseant lfs_ifind(struct lfs * fs, ino_t ino, struct ubuf * bp)
319 1.1 perseant {
320 1.2 fvdl struct ufs1_dinode *dip = (struct ufs1_dinode *) bp->b_data;
321 1.2 fvdl struct ufs1_dinode *ldip, *fin;
322 1.1 perseant
323 1.1 perseant fin = dip + INOPB(fs);
324 1.1 perseant
325 1.1 perseant /*
326 1.1 perseant * Read the inode block backwards, since later versions of the
327 1.1 perseant * inode will supercede earlier ones. Though it is unlikely, it is
328 1.1 perseant * possible that the same inode will appear in the same inode block.
329 1.1 perseant */
330 1.1 perseant for (ldip = fin - 1; ldip >= dip; --ldip)
331 1.1 perseant if (ldip->di_inumber == ino)
332 1.1 perseant return (ldip);
333 1.1 perseant return NULL;
334 1.1 perseant }
335 1.1 perseant
336 1.1 perseant /*
337 1.1 perseant * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
338 1.1 perseant * XXX it currently loses atime information.
339 1.1 perseant */
340 1.1 perseant struct uvnode *
341 1.1 perseant lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr)
342 1.1 perseant {
343 1.1 perseant struct uvnode *vp;
344 1.1 perseant struct inode *ip;
345 1.2 fvdl struct ufs1_dinode *dip;
346 1.1 perseant struct ubuf *bp;
347 1.1 perseant int i;
348 1.1 perseant
349 1.1 perseant vp = (struct uvnode *) malloc(sizeof(*vp));
350 1.1 perseant memset(vp, 0, sizeof(*vp));
351 1.1 perseant vp->v_fd = fd;
352 1.1 perseant vp->v_fs = fs;
353 1.1 perseant vp->v_usecount = 0;
354 1.1 perseant vp->v_strategy_op = lfs_vop_strategy;
355 1.1 perseant vp->v_bwrite_op = lfs_vop_bwrite;
356 1.1 perseant vp->v_bmap_op = lfs_vop_bmap;
357 1.1 perseant
358 1.1 perseant ++nvnodes;
359 1.1 perseant LIST_INSERT_HEAD(&getvnodelist, vp, v_getvnodes);
360 1.1 perseant LIST_INSERT_HEAD(&vnodelist, vp, v_mntvnodes);
361 1.1 perseant
362 1.1 perseant vp->v_data = ip = (struct inode *) malloc(sizeof(*ip));
363 1.1 perseant memset(ip, 0, sizeof(*ip));
364 1.1 perseant
365 1.2 fvdl ip->i_din.ffs1_din = (struct ufs1_dinode *)
366 1.2 fvdl malloc(sizeof(struct ufs1_dinode));
367 1.2 fvdl memset(ip->i_din.ffs1_din, 0, sizeof (struct ufs1_dinode));
368 1.2 fvdl
369 1.1 perseant /* Initialize the inode -- from lfs_vcreate. */
370 1.1 perseant ip->inode_ext.lfs = malloc(sizeof(struct lfs_inode_ext));
371 1.1 perseant memset(ip->inode_ext.lfs, 0, sizeof(struct lfs_inode_ext));
372 1.1 perseant vp->v_data = ip;
373 1.1 perseant /* ip->i_vnode = vp; */
374 1.1 perseant ip->i_number = ino;
375 1.1 perseant ip->i_lockf = 0;
376 1.1 perseant ip->i_diroff = 0;
377 1.1 perseant ip->i_lfs_effnblks = 0;
378 1.1 perseant ip->i_flag = 0;
379 1.1 perseant
380 1.1 perseant /* Load inode block and find inode */
381 1.1 perseant bread(fs->lfs_unlockvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NULL, &bp);
382 1.1 perseant bp->b_flags |= B_AGE;
383 1.1 perseant dip = lfs_ifind(fs, ino, bp);
384 1.1 perseant if (dip == NULL) {
385 1.1 perseant brelse(bp);
386 1.1 perseant free(vp);
387 1.1 perseant return NULL;
388 1.1 perseant }
389 1.2 fvdl memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
390 1.1 perseant brelse(bp);
391 1.1 perseant ip->i_number = ino;
392 1.1 perseant /* ip->i_devvp = fs->lfs_unlockvp; */
393 1.1 perseant ip->i_lfs = fs;
394 1.1 perseant
395 1.2 fvdl ip->i_ffs_effnlink = ip->i_ffs1_nlink;
396 1.2 fvdl ip->i_lfs_effnblks = ip->i_ffs1_blocks;
397 1.2 fvdl ip->i_lfs_osize = ip->i_ffs1_size;
398 1.1 perseant #if 0
399 1.1 perseant if (fs->lfs_version > 1) {
400 1.2 fvdl ip->i_ffs1_atime = ts.tv_sec;
401 1.2 fvdl ip->i_ffs1_atimensec = ts.tv_nsec;
402 1.1 perseant }
403 1.1 perseant #endif
404 1.1 perseant
405 1.1 perseant memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
406 1.1 perseant for (i = 0; i < NDADDR; i++)
407 1.2 fvdl if (ip->i_ffs1_db[i] != 0)
408 1.1 perseant ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
409 1.1 perseant
410 1.1 perseant return vp;
411 1.1 perseant }
412 1.1 perseant
413 1.1 perseant static struct uvnode *
414 1.1 perseant lfs_vget(void *vfs, ino_t ino)
415 1.1 perseant {
416 1.1 perseant struct lfs *fs = (struct lfs *)vfs;
417 1.1 perseant ufs_daddr_t daddr;
418 1.1 perseant struct ubuf *bp;
419 1.1 perseant IFILE *ifp;
420 1.1 perseant
421 1.1 perseant LFS_IENTRY(ifp, fs, ino, bp);
422 1.1 perseant daddr = ifp->if_daddr;
423 1.1 perseant brelse(bp);
424 1.1 perseant if (daddr == 0)
425 1.1 perseant return NULL;
426 1.1 perseant return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
427 1.1 perseant }
428 1.1 perseant
429 1.1 perseant /* Check superblock magic number and checksum */
430 1.1 perseant static int
431 1.1 perseant check_sb(struct lfs *fs)
432 1.1 perseant {
433 1.1 perseant u_int32_t checksum;
434 1.1 perseant
435 1.1 perseant if (fs->lfs_magic != LFS_MAGIC) {
436 1.1 perseant printf("Superblock magic number (0x%lx) does not match "
437 1.1 perseant "expected 0x%lx\n", (unsigned long) fs->lfs_magic,
438 1.1 perseant (unsigned long) LFS_MAGIC);
439 1.1 perseant return 1;
440 1.1 perseant }
441 1.1 perseant /* checksum */
442 1.1 perseant checksum = lfs_sb_cksum(&(fs->lfs_dlfs));
443 1.1 perseant if (fs->lfs_cksum != checksum) {
444 1.1 perseant printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
445 1.1 perseant (unsigned long) fs->lfs_cksum, (unsigned long) checksum);
446 1.1 perseant return 1;
447 1.1 perseant }
448 1.1 perseant return 0;
449 1.1 perseant }
450 1.1 perseant
451 1.1 perseant /* Initialize LFS library; load superblocks and choose which to use. */
452 1.1 perseant struct lfs *
453 1.1 perseant lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int debug)
454 1.1 perseant {
455 1.1 perseant struct uvnode *devvp;
456 1.1 perseant struct ubuf *bp;
457 1.1 perseant int tryalt;
458 1.1 perseant struct lfs *fs, *altfs;
459 1.1 perseant int error;
460 1.1 perseant
461 1.1 perseant vfs_init();
462 1.1 perseant
463 1.1 perseant devvp = (struct uvnode *) malloc(sizeof(*devvp));
464 1.1 perseant devvp->v_fs = NULL;
465 1.1 perseant devvp->v_fd = devfd;
466 1.1 perseant devvp->v_strategy_op = raw_vop_strategy;
467 1.1 perseant devvp->v_bwrite_op = raw_vop_bwrite;
468 1.1 perseant devvp->v_bmap_op = raw_vop_bmap;
469 1.1 perseant
470 1.1 perseant tryalt = 0;
471 1.1 perseant if (sblkno == 0) {
472 1.1 perseant sblkno = btodb(LFS_LABELPAD);
473 1.1 perseant tryalt = 1;
474 1.1 perseant } else if (debug) {
475 1.1 perseant printf("No -b flag given, not attempting to verify checkpoint\n");
476 1.1 perseant }
477 1.1 perseant error = bread(devvp, sblkno, LFS_SBPAD, NOCRED, &bp);
478 1.1 perseant fs = (struct lfs *) malloc(sizeof(*fs));
479 1.1 perseant *fs = *((struct lfs *) bp->b_data);
480 1.1 perseant fs->lfs_unlockvp = devvp;
481 1.1 perseant bp->b_flags |= B_INVAL;
482 1.1 perseant brelse(bp);
483 1.1 perseant
484 1.1 perseant if (tryalt) {
485 1.1 perseant error = bread(devvp, fsbtodb(fs, fs->lfs_sboffs[1]),
486 1.1 perseant LFS_SBPAD, NOCRED, &bp);
487 1.1 perseant altfs = (struct lfs *) malloc(sizeof(*fs));
488 1.1 perseant *altfs = *((struct lfs *) bp->b_data);
489 1.1 perseant altfs->lfs_unlockvp = devvp;
490 1.1 perseant bp->b_flags |= B_INVAL;
491 1.1 perseant brelse(bp);
492 1.1 perseant
493 1.1 perseant if (check_sb(fs)) {
494 1.1 perseant if (debug)
495 1.1 perseant printf("Primary superblock is no good, using first alternate\n");
496 1.1 perseant free(fs);
497 1.1 perseant fs = altfs;
498 1.1 perseant } else {
499 1.1 perseant /* If both superblocks check out, try verification */
500 1.1 perseant if (check_sb(altfs)) {
501 1.1 perseant if (debug)
502 1.1 perseant printf("First alternate superblock is no good, using primary\n");
503 1.1 perseant free(altfs);
504 1.1 perseant } else {
505 1.1 perseant if (lfs_verify(fs, altfs, devvp, debug) == fs) {
506 1.1 perseant free(altfs);
507 1.1 perseant } else {
508 1.1 perseant free(fs);
509 1.1 perseant fs = altfs;
510 1.1 perseant }
511 1.1 perseant }
512 1.1 perseant }
513 1.1 perseant }
514 1.1 perseant if (check_sb(fs)) {
515 1.1 perseant free(fs);
516 1.1 perseant return NULL;
517 1.1 perseant }
518 1.1 perseant /* Compatibility */
519 1.1 perseant if (fs->lfs_version < 2) {
520 1.1 perseant fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
521 1.1 perseant fs->lfs_ibsize = fs->lfs_bsize;
522 1.1 perseant fs->lfs_start = fs->lfs_sboffs[0];
523 1.1 perseant fs->lfs_tstamp = fs->lfs_otstamp;
524 1.1 perseant fs->lfs_fsbtodb = 0;
525 1.1 perseant }
526 1.1 perseant fs->lfs_suflags = (u_int32_t **) malloc(2 * sizeof(u_int32_t *));
527 1.1 perseant fs->lfs_suflags[0] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
528 1.1 perseant fs->lfs_suflags[1] = (u_int32_t *) malloc(fs->lfs_nseg * sizeof(u_int32_t));
529 1.1 perseant
530 1.1 perseant if (idaddr == 0)
531 1.1 perseant idaddr = fs->lfs_idaddr;
532 1.1 perseant fs->lfs_ivnode = lfs_raw_vget(fs, fs->lfs_ifile, devvp->v_fd, idaddr);
533 1.1 perseant
534 1.1 perseant register_vget((void *)fs, lfs_vget);
535 1.1 perseant
536 1.1 perseant return fs;
537 1.1 perseant }
538 1.1 perseant
539 1.1 perseant /*
540 1.1 perseant * Check partial segment validity between fs->lfs_offset and the given goal.
541 1.1 perseant * If goal == 0, just keep on going until the segments stop making sense.
542 1.1 perseant * Return the address of the first partial segment that failed.
543 1.1 perseant */
544 1.1 perseant ufs_daddr_t
545 1.1 perseant try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
546 1.1 perseant {
547 1.1 perseant ufs_daddr_t daddr, odaddr;
548 1.1 perseant SEGSUM *sp;
549 1.1 perseant int bc, flag;
550 1.1 perseant struct ubuf *bp;
551 1.1 perseant ufs_daddr_t nodirop_daddr;
552 1.1 perseant u_int64_t serial;
553 1.1 perseant
554 1.1 perseant daddr = osb->lfs_offset;
555 1.1 perseant nodirop_daddr = daddr;
556 1.1 perseant serial = osb->lfs_serial;
557 1.1 perseant while (daddr != goal) {
558 1.1 perseant flag = 0;
559 1.1 perseant oncemore:
560 1.1 perseant /* Read in summary block */
561 1.1 perseant bread(devvp, fsbtodb(osb, daddr), osb->lfs_sumsize, NULL, &bp);
562 1.1 perseant sp = (SEGSUM *)bp->b_data;
563 1.1 perseant
564 1.1 perseant /*
565 1.1 perseant * Could be a superblock instead of a segment summary.
566 1.1 perseant * XXX should use gseguse, but right now we need to do more
567 1.1 perseant * setup before we can...fix this
568 1.1 perseant */
569 1.1 perseant if (sp->ss_magic != SS_MAGIC ||
570 1.1 perseant sp->ss_ident != osb->lfs_ident ||
571 1.1 perseant sp->ss_serial < serial ||
572 1.1 perseant sp->ss_sumsum != cksum(&sp->ss_datasum, osb->lfs_sumsize -
573 1.1 perseant sizeof(sp->ss_sumsum))) {
574 1.1 perseant brelse(bp);
575 1.1 perseant if (flag == 0) {
576 1.1 perseant flag = 1;
577 1.1 perseant daddr += btofsb(osb, LFS_SBPAD);
578 1.1 perseant goto oncemore;
579 1.1 perseant }
580 1.1 perseant break;
581 1.1 perseant }
582 1.1 perseant ++serial;
583 1.1 perseant bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
584 1.1 perseant if (bc == 0) {
585 1.1 perseant brelse(bp);
586 1.1 perseant break;
587 1.1 perseant }
588 1.1 perseant assert (bc > 0);
589 1.1 perseant odaddr = daddr;
590 1.1 perseant daddr += btofsb(osb, osb->lfs_sumsize + bc);
591 1.1 perseant if (dtosn(osb, odaddr) != dtosn(osb, daddr) ||
592 1.1 perseant dtosn(osb, daddr) != dtosn(osb, daddr +
593 1.1 perseant btofsb(osb, osb->lfs_sumsize + osb->lfs_bsize))) {
594 1.1 perseant daddr = sp->ss_next;
595 1.1 perseant }
596 1.1 perseant if (!(sp->ss_flags & SS_CONT))
597 1.1 perseant nodirop_daddr = daddr;
598 1.1 perseant brelse(bp);
599 1.1 perseant }
600 1.1 perseant
601 1.1 perseant if (goal == 0)
602 1.1 perseant return nodirop_daddr;
603 1.1 perseant else
604 1.1 perseant return daddr;
605 1.1 perseant }
606 1.1 perseant
607 1.1 perseant /* Use try_verify to check whether the newer superblock is valid. */
608 1.1 perseant struct lfs *
609 1.1 perseant lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
610 1.1 perseant {
611 1.1 perseant ufs_daddr_t daddr;
612 1.1 perseant struct lfs *osb, *nsb;
613 1.1 perseant
614 1.1 perseant /*
615 1.1 perseant * Verify the checkpoint of the newer superblock,
616 1.1 perseant * if the timestamp/serial number of the two superblocks is
617 1.1 perseant * different.
618 1.1 perseant */
619 1.1 perseant
620 1.1 perseant if (debug)
621 1.1 perseant printf("sb0 %lld, sb1 %lld\n", (long long) sb0->lfs_serial,
622 1.1 perseant (long long) sb1->lfs_serial);
623 1.1 perseant
624 1.1 perseant if ((sb0->lfs_version == 1 &&
625 1.1 perseant sb0->lfs_otstamp != sb1->lfs_otstamp) ||
626 1.1 perseant (sb0->lfs_version > 1 &&
627 1.1 perseant sb0->lfs_serial != sb1->lfs_serial)) {
628 1.1 perseant if (sb0->lfs_version == 1) {
629 1.1 perseant if (sb0->lfs_otstamp > sb1->lfs_otstamp) {
630 1.1 perseant osb = sb1;
631 1.1 perseant nsb = sb0;
632 1.1 perseant } else {
633 1.1 perseant osb = sb0;
634 1.1 perseant nsb = sb1;
635 1.1 perseant }
636 1.1 perseant } else {
637 1.1 perseant if (sb0->lfs_serial > sb1->lfs_serial) {
638 1.1 perseant osb = sb1;
639 1.1 perseant nsb = sb0;
640 1.1 perseant } else {
641 1.1 perseant osb = sb0;
642 1.1 perseant nsb = sb1;
643 1.1 perseant }
644 1.1 perseant }
645 1.1 perseant if (debug) {
646 1.1 perseant printf("Attempting to verify newer checkpoint...");
647 1.1 perseant fflush(stdout);
648 1.1 perseant }
649 1.1 perseant daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
650 1.1 perseant
651 1.1 perseant if (debug)
652 1.1 perseant printf("done.\n");
653 1.1 perseant if (daddr == nsb->lfs_offset) {
654 1.1 perseant warnx("** Newer checkpoint verified, recovered %lld seconds of data\n",
655 1.1 perseant (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
656 1.1 perseant sbdirty();
657 1.1 perseant } else {
658 1.1 perseant warnx("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb->lfs_tstamp - (long long) osb->lfs_tstamp);
659 1.1 perseant }
660 1.1 perseant return (daddr == nsb->lfs_offset ? nsb : osb);
661 1.1 perseant }
662 1.1 perseant /* Nothing to check */
663 1.1 perseant return osb;
664 1.1 perseant }
665 1.1 perseant
666 1.1 perseant /* Verify a partial-segment summary; return the number of bytes on disk. */
667 1.1 perseant int
668 1.1 perseant check_summary(struct lfs *fs, SEGSUM *sp, ufs_daddr_t pseg_addr, int debug,
669 1.1 perseant struct uvnode *devvp, void (func(ufs_daddr_t, FINFO *)))
670 1.1 perseant {
671 1.1 perseant FINFO *fp;
672 1.1 perseant int bc; /* Bytes in partial segment */
673 1.1 perseant int nblocks;
674 1.1 perseant ufs_daddr_t seg_addr, daddr;
675 1.1 perseant ufs_daddr_t *dp, *idp;
676 1.1 perseant struct ubuf *bp;
677 1.1 perseant int i, j, k, datac, len;
678 1.1 perseant long sn;
679 1.1 perseant u_int32_t *datap;
680 1.1 perseant u_int32_t ccksum;
681 1.1 perseant
682 1.1 perseant sn = dtosn(fs, pseg_addr);
683 1.1 perseant seg_addr = sntod(fs, sn);
684 1.1 perseant
685 1.1 perseant /* We've already checked the sumsum, just do the data bounds and sum */
686 1.1 perseant
687 1.1 perseant /* Count the blocks. */
688 1.1 perseant nblocks = howmany(sp->ss_ninos, INOPB(fs));
689 1.1 perseant bc = nblocks << (fs->lfs_version > 1 ? fs->lfs_ffshift : fs->lfs_bshift);
690 1.1 perseant assert(bc >= 0);
691 1.1 perseant
692 1.1 perseant fp = (FINFO *) (sp + 1);
693 1.1 perseant for (i = 0; i < sp->ss_nfinfo; i++) {
694 1.1 perseant nblocks += fp->fi_nblocks;
695 1.1 perseant bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
696 1.1 perseant << fs->lfs_bshift);
697 1.1 perseant assert(bc >= 0);
698 1.1 perseant fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
699 1.1 perseant }
700 1.1 perseant datap = (u_int32_t *) malloc(nblocks * sizeof(*datap));
701 1.1 perseant datac = 0;
702 1.1 perseant
703 1.1 perseant dp = (ufs_daddr_t *) sp;
704 1.1 perseant dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
705 1.1 perseant dp--;
706 1.1 perseant
707 1.1 perseant idp = dp;
708 1.1 perseant daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
709 1.1 perseant fp = (FINFO *) (sp + 1);
710 1.1 perseant for (i = 0, j = 0;
711 1.1 perseant i < sp->ss_nfinfo || j < howmany(sp->ss_ninos, INOPB(fs)); i++) {
712 1.1 perseant if (i >= sp->ss_nfinfo && *idp != daddr) {
713 1.1 perseant warnx("Not enough inode blocks in pseg at 0x" PRIx32
714 1.1 perseant ": found %d, wanted %d\n",
715 1.1 perseant pseg_addr, j, howmany(sp->ss_ninos, INOPB(fs)));
716 1.1 perseant if (debug)
717 1.1 perseant warnx("*idp=%x, daddr=%" PRIx32 "\n", *idp,
718 1.1 perseant daddr);
719 1.1 perseant break;
720 1.1 perseant }
721 1.1 perseant while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
722 1.1 perseant bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize, NOCRED, &bp);
723 1.1 perseant datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
724 1.1 perseant brelse(bp);
725 1.1 perseant
726 1.1 perseant ++j;
727 1.1 perseant daddr += btofsb(fs, fs->lfs_ibsize);
728 1.1 perseant --idp;
729 1.1 perseant }
730 1.1 perseant if (i < sp->ss_nfinfo) {
731 1.1 perseant if (func)
732 1.1 perseant func(daddr, fp);
733 1.1 perseant for (k = 0; k < fp->fi_nblocks; k++) {
734 1.1 perseant len = (k == fp->fi_nblocks - 1 ?
735 1.1 perseant fp->fi_lastlength
736 1.1 perseant : fs->lfs_bsize);
737 1.1 perseant bread(devvp, fsbtodb(fs, daddr), len, NOCRED, &bp);
738 1.1 perseant datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
739 1.1 perseant brelse(bp);
740 1.1 perseant daddr += btofsb(fs, len);
741 1.1 perseant }
742 1.1 perseant fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
743 1.1 perseant }
744 1.1 perseant }
745 1.1 perseant
746 1.1 perseant if (datac != nblocks) {
747 1.1 perseant warnx("Partial segment at 0x%llx expected %d blocks counted %d\n",
748 1.1 perseant (long long) pseg_addr, nblocks, datac);
749 1.1 perseant }
750 1.1 perseant ccksum = cksum(datap, nblocks * sizeof(u_int32_t));
751 1.1 perseant /* Check the data checksum */
752 1.1 perseant if (ccksum != sp->ss_datasum) {
753 1.1 perseant warnx("Partial segment at 0x%" PRIx32 " data checksum"
754 1.1 perseant " mismatch: given 0x%x, computed 0x%x\n",
755 1.1 perseant pseg_addr, sp->ss_datasum, ccksum);
756 1.1 perseant free(datap);
757 1.1 perseant return 0;
758 1.1 perseant }
759 1.1 perseant free(datap);
760 1.1 perseant assert(bc >= 0);
761 1.1 perseant return bc;
762 1.1 perseant }
763 1.1 perseant
764 1.1 perseant /* print message and exit */
765 1.1 perseant void
766 1.1 perseant my_vpanic(int fatal, const char *fmt, va_list ap)
767 1.1 perseant {
768 1.1 perseant (void) vprintf(fmt, ap);
769 1.1 perseant exit(8);
770 1.1 perseant }
771 1.1 perseant
772 1.1 perseant void
773 1.1 perseant call_panic(const char *fmt, ...)
774 1.1 perseant {
775 1.1 perseant va_list ap;
776 1.1 perseant
777 1.1 perseant va_start(ap, fmt);
778 1.1 perseant panic_func(1, fmt, ap);
779 1.1 perseant va_end(ap);
780 1.1 perseant }
781