lfs_rfw.c revision 1.25 1 1.25 dholland /* $NetBSD: lfs_rfw.c,v 1.25 2015/08/02 18:14:16 dholland Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.1 perseant * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 1.1 perseant * All rights reserved.
6 1.1 perseant *
7 1.1 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.1 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.1 perseant *
10 1.1 perseant * Redistribution and use in source and binary forms, with or without
11 1.1 perseant * modification, are permitted provided that the following conditions
12 1.1 perseant * are met:
13 1.1 perseant * 1. Redistributions of source code must retain the above copyright
14 1.1 perseant * notice, this list of conditions and the following disclaimer.
15 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 perseant * notice, this list of conditions and the following disclaimer in the
17 1.1 perseant * documentation and/or other materials provided with the distribution.
18 1.1 perseant *
19 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 perseant * POSSIBILITY OF SUCH DAMAGE.
30 1.1 perseant */
31 1.1 perseant
32 1.2 perseant #include <sys/cdefs.h>
33 1.25 dholland __KERNEL_RCSID(0, "$NetBSD: lfs_rfw.c,v 1.25 2015/08/02 18:14:16 dholland Exp $");
34 1.2 perseant
35 1.2 perseant #if defined(_KERNEL_OPT)
36 1.2 perseant #include "opt_quota.h"
37 1.2 perseant #endif
38 1.2 perseant
39 1.2 perseant #include <sys/param.h>
40 1.2 perseant #include <sys/systm.h>
41 1.2 perseant #include <sys/namei.h>
42 1.2 perseant #include <sys/proc.h>
43 1.2 perseant #include <sys/kernel.h>
44 1.2 perseant #include <sys/vnode.h>
45 1.2 perseant #include <sys/mount.h>
46 1.2 perseant #include <sys/kthread.h>
47 1.2 perseant #include <sys/buf.h>
48 1.2 perseant #include <sys/device.h>
49 1.2 perseant #include <sys/mbuf.h>
50 1.2 perseant #include <sys/file.h>
51 1.2 perseant #include <sys/disklabel.h>
52 1.2 perseant #include <sys/ioctl.h>
53 1.2 perseant #include <sys/errno.h>
54 1.2 perseant #include <sys/malloc.h>
55 1.2 perseant #include <sys/pool.h>
56 1.2 perseant #include <sys/socket.h>
57 1.2 perseant #include <sys/syslog.h>
58 1.2 perseant #include <uvm/uvm_extern.h>
59 1.2 perseant #include <sys/sysctl.h>
60 1.2 perseant #include <sys/conf.h>
61 1.2 perseant #include <sys/kauth.h>
62 1.2 perseant
63 1.2 perseant #include <miscfs/specfs/specdev.h>
64 1.2 perseant
65 1.14 dholland #include <ufs/lfs/ulfs_quotacommon.h>
66 1.14 dholland #include <ufs/lfs/ulfs_inode.h>
67 1.14 dholland #include <ufs/lfs/ulfsmount.h>
68 1.14 dholland #include <ufs/lfs/ulfs_extern.h>
69 1.2 perseant
70 1.2 perseant #include <uvm/uvm.h>
71 1.2 perseant #include <uvm/uvm_stat.h>
72 1.2 perseant #include <uvm/uvm_pager.h>
73 1.2 perseant #include <uvm/uvm_pdaemon.h>
74 1.2 perseant
75 1.2 perseant #include <ufs/lfs/lfs.h>
76 1.24 dholland #include <ufs/lfs/lfs_accessors.h>
77 1.18 dholland #include <ufs/lfs/lfs_kernel.h>
78 1.2 perseant #include <ufs/lfs/lfs_extern.h>
79 1.2 perseant
80 1.2 perseant #include <miscfs/genfs/genfs.h>
81 1.2 perseant #include <miscfs/genfs/genfs_node.h>
82 1.2 perseant
83 1.1 perseant /*
84 1.1 perseant * Roll-forward code.
85 1.1 perseant */
86 1.1 perseant static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
87 1.1 perseant kauth_cred_t, int, int *, struct lwp *);
88 1.1 perseant
89 1.3 perseant extern int lfs_do_rfw;
90 1.3 perseant
91 1.1 perseant /*
92 1.1 perseant * Allocate a particular inode with a particular version number, freeing
93 1.1 perseant * any previous versions of this inode that may have gone before.
94 1.1 perseant * Used by the roll-forward code.
95 1.1 perseant *
96 1.1 perseant * XXX this function does not have appropriate locking to be used on a live fs;
97 1.1 perseant * XXX but something similar could probably be used for an "undelete" call.
98 1.1 perseant *
99 1.1 perseant * Called with the Ifile inode locked.
100 1.1 perseant */
101 1.1 perseant int
102 1.1 perseant lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
103 1.1 perseant struct vnode **vpp)
104 1.1 perseant {
105 1.20 hannken struct vattr va;
106 1.1 perseant struct vnode *vp;
107 1.1 perseant struct inode *ip;
108 1.1 perseant int error;
109 1.1 perseant
110 1.1 perseant ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
111 1.1 perseant
112 1.1 perseant /*
113 1.1 perseant * First, just try a vget. If the version number is the one we want,
114 1.1 perseant * we don't have to do anything else. If the version number is wrong,
115 1.1 perseant * take appropriate action.
116 1.1 perseant */
117 1.1 perseant error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
118 1.1 perseant if (error == 0) {
119 1.1 perseant DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
120 1.1 perseant
121 1.1 perseant *vpp = vp;
122 1.1 perseant ip = VTOI(vp);
123 1.1 perseant if (ip->i_gen == vers)
124 1.1 perseant return 0;
125 1.1 perseant else if (ip->i_gen < vers) {
126 1.8 he lfs_truncate(vp, (off_t)0, 0, NOCRED);
127 1.1 perseant ip->i_gen = ip->i_ffs1_gen = vers;
128 1.1 perseant LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
129 1.1 perseant return 0;
130 1.1 perseant } else {
131 1.1 perseant DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
132 1.1 perseant ino, vers, ip->i_ffs1_gen));
133 1.1 perseant vput(vp);
134 1.1 perseant *vpp = NULLVP;
135 1.1 perseant return EEXIST;
136 1.1 perseant }
137 1.1 perseant }
138 1.1 perseant
139 1.20 hannken /* Not found, create as regular file. */
140 1.20 hannken vattr_null(&va);
141 1.20 hannken va.va_type = VREG;
142 1.20 hannken va.va_mode = 0;
143 1.20 hannken va.va_fileid = ino;
144 1.20 hannken va.va_gen = vers;
145 1.20 hannken error = vcache_new(fs->lfs_ivnode->v_mount, NULL, &va, NOCRED, &vp);
146 1.20 hannken if (error)
147 1.20 hannken return error;
148 1.20 hannken error = vn_lock(vp, LK_EXCLUSIVE);
149 1.20 hannken if (error) {
150 1.20 hannken vrele(vp);
151 1.20 hannken *vpp = NULLVP;
152 1.20 hannken return error;
153 1.1 perseant }
154 1.20 hannken ip = VTOI(vp);
155 1.20 hannken ip->i_nlink = ip->i_ffs1_nlink = 1;
156 1.1 perseant *vpp = vp;
157 1.20 hannken return 0;
158 1.1 perseant }
159 1.1 perseant
160 1.1 perseant /*
161 1.1 perseant * Load the appropriate indirect block, and change the appropriate pointer.
162 1.1 perseant * Mark the block dirty. Do segment and avail accounting.
163 1.1 perseant */
164 1.1 perseant static int
165 1.1 perseant update_meta(struct lfs *fs, ino_t ino, int vers, daddr_t lbn,
166 1.1 perseant daddr_t ndaddr, size_t size, struct lwp *l)
167 1.1 perseant {
168 1.1 perseant int error;
169 1.1 perseant struct vnode *vp;
170 1.1 perseant struct inode *ip;
171 1.1 perseant #ifdef DEBUG
172 1.1 perseant daddr_t odaddr;
173 1.15 dholland struct indir a[ULFS_NIADDR];
174 1.1 perseant int num;
175 1.1 perseant int i;
176 1.1 perseant #endif /* DEBUG */
177 1.1 perseant struct buf *bp;
178 1.1 perseant SEGUSE *sup;
179 1.1 perseant
180 1.1 perseant KASSERT(lbn >= 0); /* no indirect blocks */
181 1.1 perseant
182 1.1 perseant if ((error = lfs_rf_valloc(fs, ino, vers, l, &vp)) != 0) {
183 1.1 perseant DLOG((DLOG_RF, "update_meta: ino %d: lfs_rf_valloc"
184 1.1 perseant " returned %d\n", ino, error));
185 1.1 perseant return error;
186 1.1 perseant }
187 1.1 perseant
188 1.23 dholland if ((error = lfs_balloc(vp, (lbn << lfs_sb_getbshift(fs)), size,
189 1.1 perseant NOCRED, 0, &bp)) != 0) {
190 1.1 perseant vput(vp);
191 1.1 perseant return (error);
192 1.1 perseant }
193 1.1 perseant /* No need to write, the block is already on disk */
194 1.9 ad if (bp->b_oflags & BO_DELWRI) {
195 1.1 perseant LFS_UNLOCK_BUF(bp);
196 1.22 dholland lfs_sb_addavail(fs, lfs_btofsb(fs, bp->b_bcount));
197 1.22 dholland /* XXX should this wake up fs->lfs_availsleep? */
198 1.1 perseant }
199 1.4 ad brelse(bp, BC_INVAL);
200 1.1 perseant
201 1.1 perseant /*
202 1.1 perseant * Extend the file, if it is not large enough already.
203 1.1 perseant * XXX this is not exactly right, we don't know how much of the
204 1.1 perseant * XXX last block is actually used. We hope that an inode will
205 1.1 perseant * XXX appear later to give the correct size.
206 1.1 perseant */
207 1.1 perseant ip = VTOI(vp);
208 1.23 dholland if (ip->i_size <= (lbn << lfs_sb_getbshift(fs))) {
209 1.1 perseant u_int64_t newsize;
210 1.1 perseant
211 1.15 dholland if (lbn < ULFS_NDADDR)
212 1.23 dholland newsize = ip->i_ffs1_size = (lbn << lfs_sb_getbshift(fs)) +
213 1.22 dholland (size - lfs_sb_getfsize(fs)) + 1;
214 1.1 perseant else
215 1.23 dholland newsize = ip->i_ffs1_size = (lbn << lfs_sb_getbshift(fs)) + 1;
216 1.1 perseant
217 1.1 perseant if (ip->i_size < newsize) {
218 1.1 perseant ip->i_size = newsize;
219 1.1 perseant /*
220 1.1 perseant * tell vm our new size for the case the inode won't
221 1.1 perseant * appear later.
222 1.1 perseant */
223 1.1 perseant uvm_vnp_setsize(vp, newsize);
224 1.1 perseant }
225 1.1 perseant }
226 1.1 perseant
227 1.1 perseant lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
228 1.1 perseant
229 1.17 christos LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
230 1.1 perseant sup->su_nbytes += size;
231 1.17 christos LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ndaddr), bp);
232 1.1 perseant
233 1.1 perseant /* differences here should be due to UNWRITTEN indirect blocks. */
234 1.17 christos KASSERT((lfs_lblkno(fs, ip->i_size) > ULFS_NDADDR &&
235 1.1 perseant ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
236 1.1 perseant ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
237 1.1 perseant
238 1.1 perseant #ifdef DEBUG
239 1.1 perseant /* Now look again to make sure it worked */
240 1.15 dholland ulfs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
241 1.1 perseant for (i = num; i > 0; i--) {
242 1.1 perseant if (!a[i].in_exists)
243 1.1 perseant panic("update_meta: absent %d lv indirect block", i);
244 1.1 perseant }
245 1.17 christos if (LFS_DBTOFSB(fs, odaddr) != ndaddr)
246 1.1 perseant DLOG((DLOG_RF, "update_meta: failed setting ino %d lbn %"
247 1.1 perseant PRId64 " to %" PRId64 "\n", ino, lbn, ndaddr));
248 1.1 perseant #endif /* DEBUG */
249 1.1 perseant vput(vp);
250 1.1 perseant return 0;
251 1.1 perseant }
252 1.1 perseant
253 1.1 perseant static int
254 1.1 perseant update_inoblk(struct lfs *fs, daddr_t offset, kauth_cred_t cred,
255 1.1 perseant struct lwp *l)
256 1.1 perseant {
257 1.1 perseant struct vnode *devvp, *vp;
258 1.1 perseant struct inode *ip;
259 1.15 dholland struct ulfs1_dinode *dip;
260 1.1 perseant struct buf *dbp, *ibp;
261 1.1 perseant int error;
262 1.1 perseant daddr_t daddr;
263 1.1 perseant IFILE *ifp;
264 1.1 perseant SEGUSE *sup;
265 1.1 perseant
266 1.1 perseant devvp = VTOI(fs->lfs_ivnode)->i_devvp;
267 1.1 perseant
268 1.1 perseant /*
269 1.1 perseant * Get the inode, update times and perms.
270 1.1 perseant * DO NOT update disk blocks, we do that separately.
271 1.1 perseant */
272 1.23 dholland error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getibsize(fs),
273 1.19 maxv 0, &dbp);
274 1.1 perseant if (error) {
275 1.1 perseant DLOG((DLOG_RF, "update_inoblk: bread returned %d\n", error));
276 1.1 perseant return error;
277 1.1 perseant }
278 1.17 christos dip = ((struct ulfs1_dinode *)(dbp->b_data)) + LFS_INOPB(fs);
279 1.15 dholland while (--dip >= (struct ulfs1_dinode *)dbp->b_data) {
280 1.1 perseant if (dip->di_inumber > LFS_IFILE_INUM) {
281 1.1 perseant error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
282 1.1 perseant l, &vp);
283 1.1 perseant if (error) {
284 1.1 perseant DLOG((DLOG_RF, "update_inoblk: lfs_rf_valloc"
285 1.1 perseant " returned %d\n", error));
286 1.1 perseant continue;
287 1.1 perseant }
288 1.1 perseant ip = VTOI(vp);
289 1.1 perseant if (dip->di_size != ip->i_size)
290 1.8 he lfs_truncate(vp, dip->di_size, 0, NOCRED);
291 1.1 perseant /* Get mode, link count, size, and times */
292 1.1 perseant memcpy(ip->i_din.ffs1_din, dip,
293 1.15 dholland offsetof(struct ulfs1_dinode, di_db[0]));
294 1.1 perseant
295 1.1 perseant /* Then the rest, except di_blocks */
296 1.1 perseant ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
297 1.1 perseant ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
298 1.1 perseant ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
299 1.1 perseant ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
300 1.1 perseant
301 1.1 perseant ip->i_mode = ip->i_ffs1_mode;
302 1.12 ad ip->i_nlink = ip->i_ffs1_nlink;
303 1.1 perseant ip->i_size = ip->i_ffs1_size;
304 1.1 perseant
305 1.1 perseant LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
306 1.1 perseant
307 1.1 perseant /* Re-initialize to get type right */
308 1.15 dholland ulfs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
309 1.1 perseant &vp);
310 1.1 perseant vput(vp);
311 1.1 perseant
312 1.1 perseant /* Record change in location */
313 1.1 perseant LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
314 1.1 perseant daddr = ifp->if_daddr;
315 1.17 christos ifp->if_daddr = LFS_DBTOFSB(fs, dbp->b_blkno);
316 1.1 perseant error = LFS_BWRITE_LOG(ibp); /* Ifile */
317 1.1 perseant /* And do segment accounting */
318 1.17 christos if (lfs_dtosn(fs, daddr) != lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno))) {
319 1.1 perseant if (daddr > 0) {
320 1.17 christos LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, daddr),
321 1.1 perseant ibp);
322 1.15 dholland sup->su_nbytes -= sizeof (struct ulfs1_dinode);
323 1.1 perseant LFS_WRITESEGENTRY(sup, fs,
324 1.17 christos lfs_dtosn(fs, daddr),
325 1.1 perseant ibp);
326 1.1 perseant }
327 1.17 christos LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
328 1.1 perseant ibp);
329 1.15 dholland sup->su_nbytes += sizeof (struct ulfs1_dinode);
330 1.1 perseant LFS_WRITESEGENTRY(sup, fs,
331 1.17 christos lfs_dtosn(fs, LFS_DBTOFSB(fs, dbp->b_blkno)),
332 1.1 perseant ibp);
333 1.1 perseant }
334 1.1 perseant }
335 1.1 perseant }
336 1.4 ad brelse(dbp, BC_AGE);
337 1.1 perseant
338 1.1 perseant return 0;
339 1.1 perseant }
340 1.1 perseant
341 1.1 perseant #define CHECK_CKSUM 0x0001 /* Check the checksum to make sure it's valid */
342 1.1 perseant #define CHECK_UPDATE 0x0002 /* Update Ifile for new data blocks / inodes */
343 1.1 perseant
344 1.1 perseant static daddr_t
345 1.1 perseant check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
346 1.1 perseant kauth_cred_t cred, int flags, int *pseg_flags, struct lwp *l)
347 1.1 perseant {
348 1.1 perseant struct vnode *devvp;
349 1.1 perseant struct buf *bp, *dbp;
350 1.1 perseant int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
351 1.1 perseant SEGSUM *ssp;
352 1.1 perseant u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
353 1.1 perseant daddr_t oldoffset;
354 1.1 perseant int32_t *iaddr; /* XXX ondisk32 */
355 1.1 perseant FINFO *fip;
356 1.1 perseant SEGUSE *sup;
357 1.1 perseant size_t size;
358 1.1 perseant
359 1.1 perseant devvp = VTOI(fs->lfs_ivnode)->i_devvp;
360 1.1 perseant /*
361 1.1 perseant * If the segment has a superblock and we're at the top
362 1.1 perseant * of the segment, skip the superblock.
363 1.1 perseant */
364 1.17 christos if (lfs_sntod(fs, lfs_dtosn(fs, offset)) == offset) {
365 1.17 christos LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
366 1.1 perseant if (sup->su_flags & SEGUSE_SUPERBLOCK)
367 1.17 christos offset += lfs_btofsb(fs, LFS_SBPAD);
368 1.4 ad brelse(bp, 0);
369 1.1 perseant }
370 1.1 perseant
371 1.1 perseant /* Read in the segment summary */
372 1.23 dholland error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getsumsize(fs),
373 1.19 maxv 0, &bp);
374 1.1 perseant if (error)
375 1.1 perseant return -1;
376 1.1 perseant
377 1.1 perseant /* Check summary checksum */
378 1.1 perseant ssp = (SEGSUM *)bp->b_data;
379 1.1 perseant if (flags & CHECK_CKSUM) {
380 1.1 perseant if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
381 1.23 dholland lfs_sb_getsumsize(fs) -
382 1.1 perseant sizeof(ssp->ss_sumsum))) {
383 1.1 perseant DLOG((DLOG_RF, "Sumsum error at 0x%" PRIx64 "\n", offset));
384 1.1 perseant offset = -1;
385 1.1 perseant goto err1;
386 1.1 perseant }
387 1.1 perseant if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
388 1.1 perseant DLOG((DLOG_RF, "Empty pseg at 0x%" PRIx64 "\n", offset));
389 1.1 perseant offset = -1;
390 1.1 perseant goto err1;
391 1.1 perseant }
392 1.22 dholland if (ssp->ss_create < lfs_sb_gettstamp(fs)) {
393 1.1 perseant DLOG((DLOG_RF, "Old data at 0x%" PRIx64 "\n", offset));
394 1.1 perseant offset = -1;
395 1.1 perseant goto err1;
396 1.1 perseant }
397 1.1 perseant }
398 1.25 dholland if (lfs_sb_getversion(fs) > 1) {
399 1.1 perseant if (ssp->ss_serial != nextserial) {
400 1.1 perseant DLOG((DLOG_RF, "Unexpected serial number at 0x%" PRIx64
401 1.1 perseant "\n", offset));
402 1.1 perseant offset = -1;
403 1.1 perseant goto err1;
404 1.1 perseant }
405 1.23 dholland if (ssp->ss_ident != lfs_sb_getident(fs)) {
406 1.1 perseant DLOG((DLOG_RF, "Incorrect fsid (0x%x vs 0x%x) at 0x%"
407 1.23 dholland PRIx64 "\n", ssp->ss_ident, lfs_sb_getident(fs), offset));
408 1.1 perseant offset = -1;
409 1.1 perseant goto err1;
410 1.1 perseant }
411 1.1 perseant }
412 1.1 perseant if (pseg_flags)
413 1.1 perseant *pseg_flags = ssp->ss_flags;
414 1.1 perseant oldoffset = offset;
415 1.23 dholland offset += lfs_btofsb(fs, lfs_sb_getsumsize(fs));
416 1.1 perseant
417 1.17 christos ninos = howmany(ssp->ss_ninos, LFS_INOPB(fs));
418 1.1 perseant /* XXX ondisk32 */
419 1.23 dholland iaddr = (int32_t *)((char*)bp->b_data + lfs_sb_getsumsize(fs) - sizeof(int32_t));
420 1.1 perseant if (flags & CHECK_CKSUM) {
421 1.1 perseant /* Count blocks */
422 1.1 perseant nblocks = 0;
423 1.8 he fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
424 1.1 perseant for (i = 0; i < ssp->ss_nfinfo; ++i) {
425 1.1 perseant nblocks += fip->fi_nblocks;
426 1.1 perseant if (fip->fi_nblocks <= 0)
427 1.1 perseant break;
428 1.1 perseant /* XXX ondisk32 */
429 1.1 perseant fip = (FINFO *)(((char *)fip) + FINFOSIZE +
430 1.1 perseant (fip->fi_nblocks * sizeof(int32_t)));
431 1.1 perseant }
432 1.1 perseant nblocks += ninos;
433 1.1 perseant /* Create the sum array */
434 1.21 dholland datap = dp = malloc(nblocks * sizeof(u_long),
435 1.21 dholland M_SEGMENT, M_WAITOK);
436 1.1 perseant }
437 1.1 perseant
438 1.1 perseant /* Handle individual blocks */
439 1.8 he fip = (FINFO *)((char*)bp->b_data + SEGSUM_SIZE(fs));
440 1.1 perseant for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
441 1.1 perseant /* Inode block? */
442 1.1 perseant if (ninos && *iaddr == offset) {
443 1.1 perseant if (flags & CHECK_CKSUM) {
444 1.1 perseant /* Read in the head and add to the buffer */
445 1.22 dholland error = bread(devvp, LFS_FSBTODB(fs, offset), lfs_sb_getbsize(fs),
446 1.19 maxv 0, &dbp);
447 1.1 perseant if (error) {
448 1.1 perseant offset = -1;
449 1.1 perseant goto err2;
450 1.1 perseant }
451 1.1 perseant (*dp++) = ((u_long *)(dbp->b_data))[0];
452 1.4 ad brelse(dbp, BC_AGE);
453 1.1 perseant }
454 1.1 perseant if (flags & CHECK_UPDATE) {
455 1.1 perseant if ((error = update_inoblk(fs, offset, cred, l))
456 1.1 perseant != 0) {
457 1.1 perseant offset = -1;
458 1.1 perseant goto err2;
459 1.1 perseant }
460 1.1 perseant }
461 1.23 dholland offset += lfs_btofsb(fs, lfs_sb_getibsize(fs));
462 1.1 perseant --iaddr;
463 1.1 perseant --ninos;
464 1.1 perseant --i; /* compensate */
465 1.1 perseant continue;
466 1.1 perseant }
467 1.22 dholland size = lfs_sb_getbsize(fs);
468 1.1 perseant for (j = 0; j < fip->fi_nblocks; ++j) {
469 1.1 perseant if (j == fip->fi_nblocks - 1)
470 1.1 perseant size = fip->fi_lastlength;
471 1.1 perseant if (flags & CHECK_CKSUM) {
472 1.17 christos error = bread(devvp, LFS_FSBTODB(fs, offset), size,
473 1.19 maxv 0, &dbp);
474 1.1 perseant if (error) {
475 1.1 perseant offset = -1;
476 1.1 perseant goto err2;
477 1.1 perseant }
478 1.1 perseant (*dp++) = ((u_long *)(dbp->b_data))[0];
479 1.4 ad brelse(dbp, BC_AGE);
480 1.1 perseant }
481 1.1 perseant /* Account for and update any direct blocks */
482 1.1 perseant if ((flags & CHECK_UPDATE) &&
483 1.1 perseant fip->fi_ino > LFS_IFILE_INUM &&
484 1.1 perseant fip->fi_blocks[j] >= 0) {
485 1.1 perseant update_meta(fs, fip->fi_ino, fip->fi_version,
486 1.1 perseant fip->fi_blocks[j], offset, size, l);
487 1.1 perseant }
488 1.17 christos offset += lfs_btofsb(fs, size);
489 1.1 perseant }
490 1.1 perseant /* XXX ondisk32 */
491 1.1 perseant fip = (FINFO *)(((char *)fip) + FINFOSIZE
492 1.1 perseant + fip->fi_nblocks * sizeof(int32_t));
493 1.1 perseant }
494 1.1 perseant /* Checksum the array, compare */
495 1.1 perseant if ((flags & CHECK_CKSUM) &&
496 1.1 perseant ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
497 1.1 perseant {
498 1.1 perseant DLOG((DLOG_RF, "Datasum error at 0x%" PRIx64
499 1.1 perseant " (wanted %x got %x)\n",
500 1.1 perseant offset, ssp->ss_datasum, cksum(datap, nblocks *
501 1.1 perseant sizeof(u_long))));
502 1.1 perseant offset = -1;
503 1.1 perseant goto err2;
504 1.1 perseant }
505 1.1 perseant
506 1.1 perseant /* If we're at the end of the segment, move to the next */
507 1.23 dholland if (lfs_dtosn(fs, offset + lfs_btofsb(fs, lfs_sb_getsumsize(fs) + lfs_sb_getbsize(fs))) !=
508 1.17 christos lfs_dtosn(fs, offset)) {
509 1.17 christos if (lfs_dtosn(fs, offset) == lfs_dtosn(fs, ssp->ss_next)) {
510 1.1 perseant offset = -1;
511 1.1 perseant goto err2;
512 1.1 perseant }
513 1.1 perseant offset = ssp->ss_next;
514 1.1 perseant DLOG((DLOG_RF, "LFS roll forward: moving to offset 0x%" PRIx64
515 1.17 christos " -> segment %d\n", offset, lfs_dtosn(fs,offset)));
516 1.1 perseant }
517 1.1 perseant
518 1.1 perseant if (flags & CHECK_UPDATE) {
519 1.22 dholland lfs_sb_subavail(fs, offset - oldoffset);
520 1.1 perseant /* Don't clog the buffer queue */
521 1.9 ad mutex_enter(&lfs_lock);
522 1.1 perseant if (locked_queue_count > LFS_MAX_BUFS ||
523 1.1 perseant locked_queue_bytes > LFS_MAX_BYTES) {
524 1.1 perseant lfs_flush(fs, SEGM_CKP, 0);
525 1.1 perseant }
526 1.9 ad mutex_exit(&lfs_lock);
527 1.1 perseant }
528 1.1 perseant
529 1.1 perseant err2:
530 1.1 perseant if (flags & CHECK_CKSUM)
531 1.1 perseant free(datap, M_SEGMENT);
532 1.1 perseant err1:
533 1.7 ad brelse(bp, BC_AGE);
534 1.1 perseant
535 1.1 perseant /* XXX should we update the serial number even for bad psegs? */
536 1.25 dholland if ((flags & CHECK_UPDATE) && offset > 0 && lfs_sb_getversion(fs) > 1)
537 1.22 dholland lfs_sb_setserial(fs, nextserial);
538 1.1 perseant return offset;
539 1.1 perseant }
540 1.1 perseant
541 1.1 perseant void
542 1.2 perseant lfs_roll_forward(struct lfs *fs, struct mount *mp, struct lwp *l)
543 1.1 perseant {
544 1.3 perseant int flags, dirty;
545 1.3 perseant daddr_t offset, oldoffset, lastgoodpseg;
546 1.3 perseant int sn, curseg, do_rollforward;
547 1.3 perseant struct proc *p;
548 1.3 perseant kauth_cred_t cred;
549 1.3 perseant SEGUSE *sup;
550 1.3 perseant struct buf *bp;
551 1.3 perseant
552 1.3 perseant p = l ? l->l_proc : NULL;
553 1.3 perseant cred = p ? p->p_cred : NOCRED;
554 1.1 perseant
555 1.1 perseant /*
556 1.1 perseant * Roll forward.
557 1.1 perseant *
558 1.1 perseant * We don't roll forward for v1 filesystems, because
559 1.1 perseant * of the danger that the clock was turned back between the last
560 1.1 perseant * checkpoint and crash. This would roll forward garbage.
561 1.1 perseant *
562 1.1 perseant * v2 filesystems don't have this problem because they use a
563 1.1 perseant * monotonically increasing serial number instead of a timestamp.
564 1.1 perseant */
565 1.23 dholland do_rollforward = (!(lfs_sb_getpflags(fs) & LFS_PF_CLEAN) &&
566 1.25 dholland lfs_do_rfw && lfs_sb_getversion(fs) > 1 && p != NULL);
567 1.1 perseant if (do_rollforward) {
568 1.1 perseant u_int64_t nextserial;
569 1.1 perseant /*
570 1.1 perseant * Phase I: Find the address of the last good partial
571 1.1 perseant * segment that was written after the checkpoint. Mark
572 1.1 perseant * the segments in question dirty, so they won't be
573 1.1 perseant * reallocated.
574 1.1 perseant */
575 1.22 dholland lastgoodpseg = oldoffset = offset = lfs_sb_getoffset(fs);
576 1.1 perseant flags = 0x0;
577 1.1 perseant DLOG((DLOG_RF, "LFS roll forward phase 1: start at offset 0x%"
578 1.1 perseant PRIx64 "\n", offset));
579 1.17 christos LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
580 1.1 perseant if (!(sup->su_flags & SEGUSE_DIRTY))
581 1.23 dholland lfs_sb_subnclean(fs, 1);
582 1.1 perseant sup->su_flags |= SEGUSE_DIRTY;
583 1.17 christos LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, offset), bp);
584 1.22 dholland nextserial = lfs_sb_getserial(fs) + 1;
585 1.1 perseant while ((offset = check_segsum(fs, offset, nextserial,
586 1.1 perseant cred, CHECK_CKSUM, &flags, l)) > 0) {
587 1.1 perseant nextserial++;
588 1.17 christos if (lfs_sntod(fs, oldoffset) != lfs_sntod(fs, offset)) {
589 1.17 christos LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
590 1.1 perseant bp);
591 1.1 perseant if (!(sup->su_flags & SEGUSE_DIRTY))
592 1.23 dholland lfs_sb_subnclean(fs, 1);
593 1.1 perseant sup->su_flags |= SEGUSE_DIRTY;
594 1.17 christos LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, oldoffset),
595 1.1 perseant bp);
596 1.1 perseant }
597 1.1 perseant
598 1.1 perseant DLOG((DLOG_RF, "LFS roll forward phase 1: offset=0x%"
599 1.1 perseant PRIx64 "\n", offset));
600 1.1 perseant if (flags & SS_DIROP) {
601 1.1 perseant DLOG((DLOG_RF, "lfs_mountfs: dirops at 0x%"
602 1.1 perseant PRIx64 "\n", oldoffset));
603 1.8 he if (!(flags & SS_CONT)) {
604 1.1 perseant DLOG((DLOG_RF, "lfs_mountfs: dirops end "
605 1.1 perseant "at 0x%" PRIx64 "\n", oldoffset));
606 1.8 he }
607 1.1 perseant }
608 1.1 perseant if (!(flags & SS_CONT))
609 1.1 perseant lastgoodpseg = offset;
610 1.1 perseant oldoffset = offset;
611 1.1 perseant }
612 1.1 perseant if (flags & SS_CONT) {
613 1.1 perseant DLOG((DLOG_RF, "LFS roll forward: warning: incomplete "
614 1.1 perseant "dirops discarded\n"));
615 1.1 perseant }
616 1.1 perseant DLOG((DLOG_RF, "LFS roll forward phase 1: completed: "
617 1.1 perseant "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg));
618 1.22 dholland oldoffset = lfs_sb_getoffset(fs);
619 1.22 dholland if (lfs_sb_getoffset(fs) != lastgoodpseg) {
620 1.1 perseant /* Don't overwrite what we're trying to preserve */
621 1.22 dholland offset = lfs_sb_getoffset(fs);
622 1.22 dholland lfs_sb_setoffset(fs, lastgoodpseg);
623 1.22 dholland lfs_sb_setcurseg(fs, lfs_sntod(fs, lfs_dtosn(fs, lfs_sb_getoffset(fs))));
624 1.22 dholland for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs));;) {
625 1.23 dholland sn = (sn + 1) % lfs_sb_getnseg(fs);
626 1.1 perseant if (sn == curseg)
627 1.1 perseant panic("lfs_mountfs: no clean segments");
628 1.1 perseant LFS_SEGENTRY(sup, fs, sn, bp);
629 1.1 perseant dirty = (sup->su_flags & SEGUSE_DIRTY);
630 1.4 ad brelse(bp, 0);
631 1.1 perseant if (!dirty)
632 1.1 perseant break;
633 1.1 perseant }
634 1.22 dholland lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
635 1.1 perseant
636 1.1 perseant /*
637 1.1 perseant * Phase II: Roll forward from the first superblock.
638 1.1 perseant */
639 1.1 perseant while (offset != lastgoodpseg) {
640 1.1 perseant DLOG((DLOG_RF, "LFS roll forward phase 2: 0x%"
641 1.1 perseant PRIx64 "\n", offset));
642 1.1 perseant offset = check_segsum(fs, offset,
643 1.22 dholland lfs_sb_getserial(fs) + 1, cred, CHECK_UPDATE,
644 1.1 perseant NULL, l);
645 1.1 perseant }
646 1.1 perseant
647 1.1 perseant /*
648 1.1 perseant * Finish: flush our changes to disk.
649 1.1 perseant */
650 1.1 perseant lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
651 1.1 perseant DLOG((DLOG_RF, "lfs_mountfs: roll forward ",
652 1.22 dholland "recovered %jd blocks\n",
653 1.22 dholland (intmax_t)(lastgoodpseg - oldoffset)));
654 1.1 perseant }
655 1.1 perseant DLOG((DLOG_RF, "LFS roll forward complete\n"));
656 1.1 perseant }
657 1.1 perseant }
658