lfs.c revision 1.75.8.1 1 /* $NetBSD: lfs.c,v 1.75.8.1 2024/06/29 19:43:25 perseant 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 #define vnode uvnode
74 #include <ufs/lfs/lfs.h>
75 #include <ufs/lfs/lfs_inode.h>
76 #include <ufs/lfs/lfs_accessors.h>
77 #undef vnode
78
79 #include <assert.h>
80 #include <err.h>
81 #include <errno.h>
82 #include <stdarg.h>
83 #include <stdbool.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
88 #include <util.h>
89
90 #include "bufcache.h"
91 #include "extern.h"
92 #include "lfs_user.h"
93 #include "segwrite.h"
94 #include "kernelops.h"
95
96 #define panic call_panic
97
98 long dev_bsize = DEV_BSIZE;
99
100 static int
101 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **);
102
103 int fsdirty = 0;
104 void (*panic_func)(int, const char *, va_list) = my_vpanic;
105
106 /*
107 * LFS buffer and uvnode operations
108 */
109
110 int
111 lfs_vop_strategy(struct ubuf * bp)
112 {
113 int count;
114
115 if (bp->b_flags & B_READ) {
116 count = kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
117 bp->b_blkno * dev_bsize);
118 if (count == bp->b_bcount)
119 bp->b_flags |= B_DONE;
120 } else {
121 count = kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
122 bp->b_blkno * dev_bsize);
123 if (count == 0) {
124 perror("pwrite");
125 return -1;
126 }
127 bp->b_flags &= ~B_DELWRI;
128 reassignbuf(bp, bp->b_vp);
129 }
130 return 0;
131 }
132
133 int
134 lfs_vop_bwrite(struct ubuf * bp)
135 {
136 struct lfs *fs;
137
138 fs = bp->b_vp->v_fs;
139 if (!(bp->b_flags & B_DELWRI)) {
140 lfs_sb_subavail(fs, lfs_btofsb(fs, bp->b_bcount));
141 }
142 bp->b_flags |= B_DELWRI | B_LOCKED;
143 reassignbuf(bp, bp->b_vp);
144 brelse(bp, 0);
145 return 0;
146 }
147
148 /*
149 * ulfs_bmaparray does the bmap conversion, and if requested returns the
150 * array of logical blocks which must be traversed to get to a block.
151 * Each entry contains the offset into that block that gets you to the
152 * next block and the disk address of the block (if it is assigned).
153 */
154 int
155 ulfs_bmaparray(struct lfs * fs, struct uvnode * vp, daddr_t bn, daddr_t * bnp, struct indir * ap, int *nump)
156 {
157 struct inode *ip;
158 struct ubuf *bp;
159 struct indir a[ULFS_NIADDR + 1], *xap;
160 daddr_t daddr;
161 daddr_t metalbn;
162 int error, num;
163
164 ip = VTOI(vp);
165
166 if (bn >= 0 && bn < ULFS_NDADDR) {
167 if (nump != NULL)
168 *nump = 0;
169 *bnp = LFS_FSBTODB(fs, lfs_dino_getdb(fs, ip->i_din, bn));
170 if (*bnp == 0)
171 *bnp = -1;
172 return (0);
173 }
174 xap = ap == NULL ? a : ap;
175 if (!nump)
176 nump = #
177 if ((error = ulfs_getlbns(fs, vp, bn, xap, nump)) != 0)
178 return (error);
179
180 num = *nump;
181
182 /* Get disk address out of indirect block array */
183 daddr = lfs_dino_getib(fs, ip->i_din, xap->in_off);
184
185 for (bp = NULL, ++xap; --num; ++xap) {
186 /* Exit the loop if there is no disk address assigned yet and
187 * the indirect block isn't in the cache, or if we were
188 * looking for an indirect block and we've found it. */
189
190 metalbn = xap->in_lbn;
191 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
192 break;
193 /*
194 * If we get here, we've either got the block in the cache
195 * or we have a disk address for it, go fetch it.
196 */
197 if (bp)
198 brelse(bp, 0);
199
200 xap->in_exists = 1;
201 bp = getblk(vp, metalbn, lfs_sb_getbsize(fs));
202
203 if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
204 bp->b_blkno = LFS_FSBTODB(fs, daddr);
205 bp->b_flags |= B_READ;
206 VOP_STRATEGY(bp);
207 }
208 daddr = lfs_iblock_get(fs, bp->b_data, xap->in_off);
209 }
210 if (bp)
211 brelse(bp, 0);
212
213 daddr = LFS_FSBTODB(fs, daddr);
214 *bnp = daddr == 0 ? -1 : daddr;
215 return (0);
216 }
217
218 /*
219 * Create an array of logical block number/offset pairs which represent the
220 * path of indirect blocks required to access a data block. The first "pair"
221 * contains the logical block number of the appropriate single, double or
222 * triple indirect block and the offset into the inode indirect block array.
223 * Note, the logical block number of the inode single/double/triple indirect
224 * block appears twice in the array, once with the offset into di_ib and
225 * once with the offset into the page itself.
226 */
227 int
228 ulfs_getlbns(struct lfs * fs, struct uvnode * vp, daddr_t bn, struct indir * ap, int *nump)
229 {
230 daddr_t metalbn, realbn;
231 int64_t blockcnt;
232 int lbc;
233 int i, numlevels, off;
234 int lognindir, indir;
235
236 metalbn = 0; /* XXXGCC -Wuninitialized [sh3] */
237
238 if (nump)
239 *nump = 0;
240 numlevels = 0;
241 realbn = bn;
242 if (bn < 0)
243 bn = -bn;
244
245 lognindir = -1;
246 for (indir = lfs_sb_getnindir(fs); indir; indir >>= 1)
247 ++lognindir;
248
249 /* Determine the number of levels of indirection. After this loop is
250 * done, blockcnt indicates the number of data blocks possible at the
251 * given level of indirection, and ULFS_NIADDR - i is the number of levels
252 * of indirection needed to locate the requested block. */
253
254 bn -= ULFS_NDADDR;
255 for (lbc = 0, i = ULFS_NIADDR;; i--, bn -= blockcnt) {
256 if (i == 0)
257 return (EFBIG);
258
259 lbc += lognindir;
260 blockcnt = (int64_t) 1 << lbc;
261
262 if (bn < blockcnt)
263 break;
264 }
265
266 /* Calculate the address of the first meta-block. */
267 metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + ULFS_NIADDR - i);
268
269 /* At each iteration, off is the offset into the bap array which is an
270 * array of disk addresses at the current level of indirection. The
271 * logical block number and the offset in that block are stored into
272 * the argument array. */
273 ap->in_lbn = metalbn;
274 ap->in_off = off = ULFS_NIADDR - i;
275 ap->in_exists = 0;
276 ap++;
277 for (++numlevels; i <= ULFS_NIADDR; i++) {
278 /* If searching for a meta-data block, quit when found. */
279 if (metalbn == realbn)
280 break;
281
282 lbc -= lognindir;
283 /*blockcnt = (int64_t) 1 << lbc;*/
284 off = (bn >> lbc) & (lfs_sb_getnindir(fs) - 1);
285
286 ++numlevels;
287 ap->in_lbn = metalbn;
288 ap->in_off = off;
289 ap->in_exists = 0;
290 ++ap;
291
292 metalbn -= -1 + (off << lbc);
293 }
294 if (nump)
295 *nump = numlevels;
296 return (0);
297 }
298
299 int
300 lfs_vop_bmap(struct uvnode * vp, daddr_t lbn, daddr_t * daddrp)
301 {
302 return ulfs_bmaparray(vp->v_fs, vp, lbn, daddrp, NULL, NULL);
303 }
304
305 /* Search a block for a specific dinode. */
306 union lfs_dinode *
307 lfs_ifind(struct lfs *fs, ino_t ino, struct ubuf *bp)
308 {
309 union lfs_dinode *ldip;
310 unsigned i, num;
311
312 num = LFS_INOPB(fs);
313
314 /*
315 * Read the inode block backwards, since later versions of the
316 * inode will supercede earlier ones. Though it is unlikely, it is
317 * possible that the same inode will appear in the same inode block.
318 */
319 for (i = num; i-- > 0; ) {
320 ldip = DINO_IN_BLOCK(fs, bp->b_data, i);
321 if (lfs_dino_getinumber(fs, ldip) == ino)
322 return (ldip);
323 }
324 return NULL;
325 }
326
327 /*
328 * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
329 * XXX it currently loses atime information.
330 */
331 struct uvnode *
332 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, daddr_t daddr)
333 {
334 struct uvnode *vp;
335 struct inode *ip;
336 union lfs_dinode *dip;
337 struct ubuf *bp;
338 int i;
339
340 vp = ecalloc(1, sizeof(*vp));
341 vp->v_fd = fd;
342 vp->v_fs = fs;
343 vp->v_usecount = 0;
344 vp->v_strategy_op = lfs_vop_strategy;
345 vp->v_bwrite_op = lfs_vop_bwrite;
346 vp->v_bmap_op = lfs_vop_bmap;
347 LIST_INIT(&vp->v_cleanblkhd);
348 LIST_INIT(&vp->v_dirtyblkhd);
349
350 ip = ecalloc(1, sizeof(*ip));
351
352 ip->i_din = dip = ecalloc(1, sizeof(*dip));
353
354 /* Initialize the inode -- from lfs_vcreate. */
355 ip->inode_ext.lfs = ecalloc(1, sizeof(*ip->inode_ext.lfs));
356 vp->v_data = ip;
357 /* ip->i_vnode = vp; */
358 ip->i_lockf = 0;
359 ip->i_state = 0;
360
361 /* Load inode block and find inode */
362 if (daddr > 0) {
363 bread(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs),
364 0, &bp);
365 bp->b_flags |= B_AGE;
366 dip = lfs_ifind(fs, ino, bp);
367 if (dip == NULL) {
368 brelse(bp, 0);
369 free(ip->i_din);
370 free(ip->inode_ext.lfs);
371 free(ip);
372 free(vp);
373 return NULL;
374 }
375 lfs_copy_dinode(fs, ip->i_din, dip);
376 brelse(bp, 0);
377 }
378 ip->i_number = ino;
379 /* ip->i_devvp = fs->lfs_devvp; */
380 ip->i_lfs = fs;
381
382 ip->i_lfs_effnblks = lfs_dino_getblocks(fs, ip->i_din);
383 ip->i_lfs_osize = lfs_dino_getsize(fs, ip->i_din);
384 #if 0
385 if (lfs_sb_getversion(fs) > 1) {
386 lfs_dino_setatime(fs, ip->i_din, ts.tv_sec);
387 lfs_dino_setatimensec(fs, ip->i_din, ts.tv_nsec);
388 }
389 #endif
390
391 memset(ip->i_lfs_fragsize, 0, ULFS_NDADDR * sizeof(*ip->i_lfs_fragsize));
392 for (i = 0; i < ULFS_NDADDR; i++)
393 if (lfs_dino_getdb(fs, ip->i_din, i) != 0)
394 ip->i_lfs_fragsize[i] = lfs_blksize(fs, ip, i);
395
396 return vp;
397 }
398
399 static struct uvnode *
400 lfs_vget(void *vfs, ino_t ino, void *arg)
401 {
402 struct lfs *fs = (struct lfs *)vfs;
403 daddr_t daddr;
404 struct ubuf *bp;
405 IFILE *ifp;
406
407 LFS_IENTRY(ifp, fs, ino, bp);
408 daddr = lfs_if_getdaddr(fs, ifp);
409 brelse(bp, 0);
410 if (daddr <= 0 || lfs_dtosn(fs, daddr) >= lfs_sb_getnseg(fs))
411 return NULL;
412 return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
413 }
414
415
416 static int
417 lfs_vnode_destroy(struct uvnode *tossvp)
418 {
419 free(VTOI(tossvp)->inode_ext.lfs);
420 free(VTOI(tossvp)->i_din);
421 memset(VTOI(tossvp), 0, sizeof(struct inode));
422 return 0;
423 }
424
425 /*
426 * Check superblock magic number and checksum.
427 * Sets lfs_is64 and lfs_dobyteswap.
428 */
429 static int
430 check_sb(struct lfs *fs)
431 {
432 u_int32_t checksum;
433 u_int32_t magic;
434
435 /* we can read the magic out of either the 32-bit or 64-bit dlfs */
436 magic = fs->lfs_dlfs_u.u_32.dlfs_magic;
437
438 switch (magic) {
439 case LFS_MAGIC:
440 fs->lfs_is64 = false;
441 fs->lfs_dobyteswap = false;
442 break;
443 case LFS_MAGIC_SWAPPED:
444 fs->lfs_is64 = false;
445 fs->lfs_dobyteswap = true;
446 break;
447 case LFS64_MAGIC:
448 fs->lfs_is64 = true;
449 fs->lfs_dobyteswap = false;
450 break;
451 case LFS64_MAGIC_SWAPPED:
452 fs->lfs_is64 = true;
453 fs->lfs_dobyteswap = true;
454 break;
455 default:
456 printf("Superblock magic number (0x%lx) does not match "
457 "expected 0x%lx\n", (unsigned long) magic,
458 (unsigned long) LFS_MAGIC);
459 return 1;
460 }
461
462 /* checksum */
463 checksum = lfs_sb_cksum(fs);
464 if (lfs_sb_getcksum(fs) != checksum) {
465 printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
466 (unsigned long) lfs_sb_getcksum(fs), (unsigned long) checksum);
467 return 1;
468 }
469 return 0;
470 }
471
472 /* Initialize LFS library; load superblocks and choose which to use. */
473 struct lfs *
474 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
475 {
476 struct uvnode *devvp;
477 struct ubuf *bp;
478 int tryalt;
479 struct lfs *fs, *altfs;
480
481 vfs_init();
482
483 devvp = ecalloc(1, sizeof(*devvp));
484 devvp->v_fs = NULL;
485 devvp->v_fd = devfd;
486 devvp->v_strategy_op = raw_vop_strategy;
487 devvp->v_bwrite_op = raw_vop_bwrite;
488 devvp->v_bmap_op = raw_vop_bmap;
489 LIST_INIT(&devvp->v_cleanblkhd);
490 LIST_INIT(&devvp->v_dirtyblkhd);
491
492 tryalt = 0;
493 if (dummy_read) {
494 if (sblkno == 0)
495 sblkno = LFS_LABELPAD / dev_bsize;
496 fs = ecalloc(1, sizeof(*fs));
497 fs->lfs_devvp = devvp;
498 } else {
499 if (sblkno == 0) {
500 sblkno = LFS_LABELPAD / dev_bsize;
501 tryalt = 1;
502 } else if (debug) {
503 printf("No -b flag given, not attempting to verify checkpoint\n");
504 }
505
506 dev_bsize = DEV_BSIZE;
507
508 (void)bread(devvp, sblkno, LFS_SBPAD, 0, &bp);
509 fs = ecalloc(1, sizeof(*fs));
510 __CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
511 memcpy(&fs->lfs_dlfs_u, bp->b_data, sizeof(struct dlfs));
512 fs->lfs_devvp = devvp;
513 bp->b_flags |= B_INVAL;
514 brelse(bp, 0);
515
516 dev_bsize = lfs_sb_getfsize(fs) >> lfs_sb_getfsbtodb(fs);
517
518 if (tryalt) {
519 (void)bread(devvp, LFS_FSBTODB(fs, lfs_sb_getsboff(fs, 1)),
520 LFS_SBPAD, 0, &bp);
521 altfs = ecalloc(1, sizeof(*altfs));
522 memcpy(&altfs->lfs_dlfs_u, bp->b_data,
523 sizeof(struct dlfs));
524 altfs->lfs_devvp = devvp;
525 bp->b_flags |= B_INVAL;
526 brelse(bp, 0);
527
528 if (check_sb(fs) || lfs_sb_getidaddr(fs) <= 0) {
529 if (debug)
530 printf("Primary superblock is no good, using first alternate\n");
531 free(fs);
532 fs = altfs;
533 } else {
534 /* If both superblocks check out, try verification */
535 if (check_sb(altfs)) {
536 if (debug)
537 printf("First alternate superblock is no good, using primary\n");
538 free(altfs);
539 } else {
540 if (lfs_verify(fs, altfs, devvp, debug) == fs) {
541 free(altfs);
542 } else {
543 free(fs);
544 fs = altfs;
545 }
546 }
547 }
548 }
549 if (check_sb(fs)) {
550 free(fs);
551 return NULL;
552 }
553 }
554
555 /* Compatibility */
556 if (lfs_sb_getversion(fs) < 2) {
557 lfs_sb_setsumsize(fs, LFS_V1_SUMMARY_SIZE);
558 lfs_sb_setibsize(fs, lfs_sb_getbsize(fs));
559 lfs_sb_sets0addr(fs, lfs_sb_getsboff(fs, 0));
560 lfs_sb_settstamp(fs, lfs_sb_getotstamp(fs));
561 lfs_sb_setfsbtodb(fs, 0);
562 }
563
564 if (!dummy_read) {
565 fs->lfs_suflags = emalloc(2 * sizeof(u_int32_t *));
566 fs->lfs_suflags[0] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
567 fs->lfs_suflags[1] = emalloc(lfs_sb_getnseg(fs) * sizeof(u_int32_t));
568 }
569
570 if (idaddr == 0)
571 idaddr = lfs_sb_getidaddr(fs);
572 else
573 lfs_sb_setidaddr(fs, idaddr);
574 /* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
575 fs->lfs_ivnode = lfs_raw_vget(fs, LFS_IFILE_INUM,
576 devvp->v_fd, idaddr);
577 if (fs->lfs_ivnode == NULL)
578 return NULL;
579
580 register_vget((void *)fs, lfs_vget, lfs_vnode_destroy);
581
582 return fs;
583 }
584
585 /*
586 * Check partial segment validity between fs->lfs_offset and the given goal.
587 *
588 * If goal == 0, just keep on going until the segments stop making sense,
589 * and return the address of the last valid partial segment.
590 *
591 * If goal != 0, return the address of the first partial segment that failed,
592 * or "goal" if we reached it without failure (the partial segment *at* goal
593 * need not be valid).
594 */
595 daddr_t
596 try_verify(struct lfs *osb, struct uvnode *devvp, daddr_t goal, int debug)
597 {
598 daddr_t daddr, odaddr;
599 SEGSUM *sp;
600 int i, bc, hitclean;
601 struct ubuf *bp;
602 daddr_t nodirop_daddr;
603 u_int64_t serial;
604
605 bc = 0;
606 hitclean = 0;
607 odaddr = -1;
608 daddr = lfs_sb_getoffset(osb);
609 nodirop_daddr = daddr;
610 serial = lfs_sb_getserial(osb);
611 while (daddr != goal) {
612 /*
613 * Don't mistakenly read a superblock, if there is one here.
614 */
615 if (lfs_sntod(osb, lfs_dtosn(osb, daddr)) == daddr) {
616 if (daddr == lfs_sb_gets0addr(osb))
617 daddr += lfs_btofsb(osb, LFS_LABELPAD);
618 for (i = 0; i < LFS_MAXNUMSB; i++) {
619 /* XXX dholland 20150828 I think this is wrong */
620 if (lfs_sb_getsboff(osb, i) < daddr)
621 break;
622 if (lfs_sb_getsboff(osb, i) == daddr)
623 daddr += lfs_btofsb(osb, LFS_SBPAD);
624 }
625 }
626
627 /* Read in summary block */
628 bread(devvp, LFS_FSBTODB(osb, daddr), lfs_sb_getsumsize(osb),
629 0, &bp);
630 sp = (SEGSUM *)bp->b_data;
631
632 /*
633 * Check for a valid segment summary belonging to our fs.
634 */
635 if (lfs_ss_getmagic(osb, sp) != SS_MAGIC ||
636 lfs_ss_getident(osb, sp) != lfs_sb_getident(osb) ||
637 lfs_ss_getserial(osb, sp) < serial || /* XXX strengthen this */
638 lfs_ss_getsumsum(osb, sp) !=
639 cksum((char *)sp + lfs_ss_getsumstart(osb),
640 lfs_sb_getsumsize(osb) - lfs_ss_getsumstart(osb))) {
641 brelse(bp, 0);
642 if (debug) {
643 if (lfs_ss_getmagic(osb, sp) != SS_MAGIC)
644 pwarn("pseg at 0x%jx: "
645 "wrong magic number\n",
646 (uintmax_t)daddr);
647 else if (lfs_ss_getident(osb, sp) != lfs_sb_getident(osb))
648 pwarn("pseg at 0x%jx: "
649 "expected ident %jx, got %jx\n",
650 (uintmax_t)daddr,
651 (uintmax_t)lfs_ss_getident(osb, sp),
652 (uintmax_t)lfs_sb_getident(osb));
653 else if (lfs_ss_getserial(osb, sp) >= serial)
654 pwarn("pseg at 0x%jx: "
655 "serial %d < %d\n",
656 (uintmax_t)daddr,
657 (int)lfs_ss_getserial(osb, sp), (int)serial);
658 else
659 pwarn("pseg at 0x%jx: "
660 "summary checksum wrong\n",
661 (uintmax_t)daddr);
662 }
663 break;
664 }
665 if (debug && lfs_ss_getserial(osb, sp) != serial)
666 pwarn("warning, serial=%d ss_serial=%d\n",
667 (int)serial, (int)lfs_ss_getserial(osb, sp));
668 ++serial;
669 bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
670 if (bc == 0) {
671 brelse(bp, 0);
672 break;
673 }
674 if (debug)
675 pwarn("summary good: 0x%x/%d\n", (uintmax_t)daddr,
676 (int)lfs_ss_getserial(osb, sp));
677 assert (bc > 0);
678 odaddr = daddr;
679 daddr += lfs_btofsb(osb, lfs_sb_getsumsize(osb) + bc);
680 if (lfs_dtosn(osb, odaddr) != lfs_dtosn(osb, daddr) ||
681 lfs_dtosn(osb, daddr) != lfs_dtosn(osb, daddr +
682 lfs_btofsb(osb, lfs_sb_getsumsize(osb) + lfs_sb_getbsize(osb)) - 1)) {
683 daddr = lfs_ss_getnext(osb, sp);
684 }
685
686 /*
687 * Check for the beginning and ending of a sequence of
688 * dirops. Writes from the cleaner never involve new
689 * information, and are always checkpoints; so don't try
690 * to roll forward through them. Likewise, psegs written
691 * by a previous roll-forward attempt are not interesting.
692 */
693 if (lfs_ss_getflags(osb, sp) & (SS_CLEAN | SS_RFW))
694 hitclean = 1;
695 if (hitclean == 0 && (lfs_ss_getflags(osb, sp) & SS_CONT) == 0)
696 nodirop_daddr = daddr;
697
698 brelse(bp, 0);
699 }
700
701 if (goal == 0)
702 return nodirop_daddr;
703 else
704 return daddr;
705 }
706
707 /* Use try_verify to check whether the newer superblock is valid. */
708 struct lfs *
709 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
710 {
711 daddr_t daddr;
712 struct lfs *osb, *nsb;
713
714 /*
715 * Verify the checkpoint of the newer superblock,
716 * if the timestamp/serial number of the two superblocks is
717 * different.
718 */
719
720 osb = NULL;
721 if (debug)
722 pwarn("sb0 %ju, sb1 %ju",
723 (uintmax_t) lfs_sb_getserial(sb0),
724 (uintmax_t) lfs_sb_getserial(sb1));
725
726 if ((lfs_sb_getversion(sb0) == 1 &&
727 lfs_sb_getotstamp(sb0) != lfs_sb_getotstamp(sb1)) ||
728 (lfs_sb_getversion(sb0) > 1 &&
729 lfs_sb_getserial(sb0) != lfs_sb_getserial(sb1))) {
730 if (lfs_sb_getversion(sb0) == 1) {
731 if (lfs_sb_getotstamp(sb0) > lfs_sb_getotstamp(sb1)) {
732 osb = sb1;
733 nsb = sb0;
734 } else {
735 osb = sb0;
736 nsb = sb1;
737 }
738 } else {
739 if (lfs_sb_getserial(sb0) > lfs_sb_getserial(sb1)) {
740 osb = sb1;
741 nsb = sb0;
742 } else {
743 osb = sb0;
744 nsb = sb1;
745 }
746 }
747 if (debug) {
748 printf("Attempting to verify newer checkpoint...");
749 fflush(stdout);
750 }
751 daddr = try_verify(osb, devvp, lfs_sb_getoffset(nsb), debug);
752
753 if (debug)
754 printf("done.\n");
755 if (daddr == lfs_sb_getoffset(nsb)) {
756 pwarn("** Newer checkpoint verified; recovered %jd seconds of data\n",
757 (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
758 sbdirty();
759 } else {
760 pwarn("** Newer checkpoint invalid; lost %jd seconds of data\n", (intmax_t)(lfs_sb_gettstamp(nsb) - lfs_sb_gettstamp(osb)));
761 }
762 return (daddr == lfs_sb_getoffset(nsb) ? nsb : osb);
763 }
764 /* Nothing to check */
765 return osb;
766 }
767
768 /* Verify a partial-segment summary; return the number of bytes on disk. */
769 int
770 check_summary(struct lfs *fs, SEGSUM *sp, daddr_t pseg_addr, int debug,
771 struct uvnode *devvp, void (func(daddr_t, FINFO *)))
772 {
773 FINFO *fp;
774 int bc; /* Bytes in partial segment */
775 int nblocks;
776 daddr_t daddr;
777 IINFO *iibase, *iip;
778 struct ubuf *bp;
779 int i, j, k, datac, len;
780 lfs_checkword *datap;
781 u_int32_t ccksum;
782
783 /* We've already checked the sumsum, just do the data bounds and sum */
784
785 /* Count the blocks. */
786 nblocks = howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs));
787 bc = nblocks << (lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) : lfs_sb_getbshift(fs));
788 assert(bc >= 0);
789
790 fp = SEGSUM_FINFOBASE(fs, sp);
791 for (i = 0; i < lfs_ss_getnfinfo(fs, sp); i++) {
792 nblocks += lfs_fi_getnblocks(fs, fp);
793 bc += lfs_fi_getlastlength(fs, fp) + ((lfs_fi_getnblocks(fs, fp) - 1)
794 << lfs_sb_getbshift(fs));
795 assert(bc >= 0);
796 fp = NEXT_FINFO(fs, fp);
797 if (((char *)fp) - (char *)sp > lfs_sb_getsumsize(fs))
798 return 0;
799 }
800 datap = emalloc(nblocks * sizeof(*datap));
801 datac = 0;
802
803 iibase = SEGSUM_IINFOSTART(fs, sp);
804
805 iip = iibase;
806 daddr = pseg_addr + lfs_btofsb(fs, lfs_sb_getsumsize(fs));
807 fp = SEGSUM_FINFOBASE(fs, sp);
808 for (i = 0, j = 0;
809 i < lfs_ss_getnfinfo(fs, sp) || j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)); i++) {
810 if (i >= lfs_ss_getnfinfo(fs, sp) && lfs_ii_getblock(fs, iip) != daddr) {
811 pwarn("Not enough inode blocks in pseg at 0x%jx: "
812 "found %d, wanted %d\n",
813 pseg_addr, j, howmany(lfs_ss_getninos(fs, sp),
814 LFS_INOPB(fs)));
815 if (debug)
816 pwarn("iip=0x%jx, daddr=0x%jx\n",
817 (uintmax_t)lfs_ii_getblock(fs, iip),
818 (intmax_t)daddr);
819 break;
820 }
821 while (j < howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)) && lfs_ii_getblock(fs, iip) == daddr) {
822 bread(devvp, LFS_FSBTODB(fs, daddr), lfs_sb_getibsize(fs),
823 0, &bp);
824 datap[datac++] = ((lfs_checkword *)bp->b_data)[0];
825 brelse(bp, 0);
826
827 ++j;
828 daddr += lfs_btofsb(fs, lfs_sb_getibsize(fs));
829 iip = NEXTLOWER_IINFO(fs, iip);
830 }
831 if (i < lfs_ss_getnfinfo(fs, sp)) {
832 if (func)
833 func(daddr, fp);
834 for (k = 0; k < lfs_fi_getnblocks(fs, fp); k++) {
835 len = (k == lfs_fi_getnblocks(fs, fp) - 1 ?
836 lfs_fi_getlastlength(fs, fp)
837 : lfs_sb_getbsize(fs));
838 bread(devvp, LFS_FSBTODB(fs, daddr), len,
839 0, &bp);
840 datap[datac++] = ((lfs_checkword *)bp->b_data)[0];
841 brelse(bp, 0);
842 daddr += lfs_btofsb(fs, len);
843 }
844 fp = NEXT_FINFO(fs, fp);
845 }
846 }
847
848 if (datac != nblocks) {
849 pwarn("Partial segment at 0x%jx expected %d blocks counted %d\n",
850 (intmax_t)pseg_addr, nblocks, datac);
851 }
852 ccksum = cksum(datap, nblocks * sizeof(datap[0]));
853 /* Check the data checksum */
854 if (ccksum != lfs_ss_getdatasum(fs, sp)) {
855 pwarn("Partial segment at 0x%jx data checksum"
856 " mismatch: given 0x%x, computed 0x%x\n",
857 (uintmax_t)pseg_addr, lfs_ss_getdatasum(fs, sp), ccksum);
858 free(datap);
859 return 0;
860 }
861 free(datap);
862 assert(bc >= 0);
863 return bc;
864 }
865
866 /* print message and exit */
867 void
868 my_vpanic(int fatal, const char *fmt, va_list ap)
869 {
870 (void) vprintf(fmt, ap);
871 exit(8);
872 }
873
874 void
875 call_panic(const char *fmt, ...)
876 {
877 va_list ap;
878
879 va_start(ap, fmt);
880 panic_func(1, fmt, ap);
881 va_end(ap);
882 }
883
884 /* Allocate a new inode. */
885 struct uvnode *
886 lfs_valloc(struct lfs *fs, ino_t ino)
887 {
888 struct ubuf *bp, *cbp;
889 IFILE *ifp;
890 ino_t new_ino;
891 int error;
892 CLEANERINFO *cip;
893
894 /* Get the head of the freelist. */
895 LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
896
897 /*
898 * Remove the inode from the free list and write the new start
899 * of the free list into the superblock.
900 */
901 LFS_IENTRY(ifp, fs, new_ino, bp);
902 if (lfs_if_getdaddr(fs, ifp) != LFS_UNUSED_DADDR)
903 panic("lfs_valloc: inuse inode %d on the free list", new_ino);
904 LFS_PUT_HEADFREE(fs, cip, cbp, lfs_if_getnextfree(fs, ifp));
905
906 brelse(bp, 0);
907
908 /* Extend IFILE so that the next lfs_valloc will succeed. */
909 if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
910 if ((error = extend_ifile(fs)) != 0) {
911 LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
912 return NULL;
913 }
914 }
915
916 /* Set superblock modified bit and increment file count. */
917 sbdirty();
918 lfs_sb_addnfiles(fs, 1);
919
920 return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
921 }
922
923 #ifdef IN_FSCK_LFS
924 void reset_maxino(ino_t);
925 #endif
926
927 /*
928 * Add a new block to the Ifile, to accommodate future file creations.
929 */
930 int
931 extend_ifile(struct lfs *fs)
932 {
933 struct uvnode *vp;
934 struct inode *ip;
935 IFILE64 *ifp64;
936 IFILE32 *ifp32;
937 IFILE_V1 *ifp_v1;
938 struct ubuf *bp, *cbp;
939 daddr_t i, blkno, max;
940 ino_t oldlast;
941 CLEANERINFO *cip;
942
943 vp = fs->lfs_ivnode;
944 ip = VTOI(vp);
945 blkno = lfs_lblkno(fs, lfs_dino_getsize(fs, ip->i_din));
946
947 lfs_balloc(vp, lfs_dino_getsize(fs, ip->i_din), lfs_sb_getbsize(fs), &bp);
948 lfs_dino_setsize(fs, ip->i_din,
949 lfs_dino_getsize(fs, ip->i_din) + lfs_sb_getbsize(fs));
950 ip->i_state |= IN_MODIFIED;
951
952 i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
953 lfs_sb_getifpb(fs);
954 LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
955 LFS_PUT_HEADFREE(fs, cip, cbp, i);
956 max = i + lfs_sb_getifpb(fs);
957 lfs_sb_subbfree(fs, lfs_btofsb(fs, lfs_sb_getbsize(fs)));
958
959 if (fs->lfs_is64) {
960 for (ifp64 = (IFILE64 *)bp->b_data; i < max; ++ifp64) {
961 ifp64->if_version = 1;
962 ifp64->if_daddr = LFS_UNUSED_DADDR;
963 ifp64->if_nextfree = ++i;
964 }
965 ifp64--;
966 ifp64->if_nextfree = oldlast;
967 } else if (lfs_sb_getversion(fs) > 1) {
968 for (ifp32 = (IFILE32 *)bp->b_data; i < max; ++ifp32) {
969 ifp32->if_version = 1;
970 ifp32->if_daddr = LFS_UNUSED_DADDR;
971 ifp32->if_nextfree = ++i;
972 }
973 ifp32--;
974 ifp32->if_nextfree = oldlast;
975 } else {
976 for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
977 ifp_v1->if_version = 1;
978 ifp_v1->if_daddr = LFS_UNUSED_DADDR;
979 ifp_v1->if_nextfree = ++i;
980 }
981 ifp_v1--;
982 ifp_v1->if_nextfree = oldlast;
983 }
984 LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
985
986 LFS_BWRITE_LOG(bp);
987
988 #ifdef IN_FSCK_LFS
989 reset_maxino(((lfs_dino_getsize(fs, ip->i_din) >> lfs_sb_getbshift(fs))
990 - lfs_sb_getsegtabsz(fs)
991 - lfs_sb_getcleansz(fs)) * lfs_sb_getifpb(fs));
992 #endif
993 return 0;
994 }
995
996 /*
997 * Allocate a block, and to inode and filesystem block accounting for it
998 * and for any indirect blocks the may need to be created in order for
999 * this block to be created.
1000 *
1001 * Blocks which have never been accounted for (i.e., which "do not exist")
1002 * have disk address 0, which is translated by ulfs_bmap to the special value
1003 * UNASSIGNED == -1, as in the historical ULFS.
1004 *
1005 * Blocks which have been accounted for but which have not yet been written
1006 * to disk are given the new special disk address UNWRITTEN == -2, so that
1007 * they can be differentiated from completely new blocks.
1008 */
1009 int
1010 lfs_balloc(struct uvnode *vp, off_t startoffset, int iosize, struct ubuf **bpp)
1011 {
1012 int offset;
1013 daddr_t daddr, idaddr;
1014 struct ubuf *ibp, *bp;
1015 struct inode *ip;
1016 struct lfs *fs;
1017 struct indir indirs[ULFS_NIADDR+2], *idp;
1018 daddr_t lbn, lastblock;
1019 int bcount;
1020 int error, frags, i, nsize, osize, num;
1021
1022 ip = VTOI(vp);
1023 fs = ip->i_lfs;
1024 offset = lfs_blkoff(fs, startoffset);
1025 lbn = lfs_lblkno(fs, startoffset);
1026
1027 /*
1028 * Three cases: it's a block beyond the end of file, it's a block in
1029 * the file that may or may not have been assigned a disk address or
1030 * we're writing an entire block.
1031 *
1032 * Note, if the daddr is UNWRITTEN, the block already exists in
1033 * the cache (it was read or written earlier). If so, make sure
1034 * we don't count it as a new block or zero out its contents. If
1035 * it did not, make sure we allocate any necessary indirect
1036 * blocks.
1037 *
1038 * If we are writing a block beyond the end of the file, we need to
1039 * check if the old last block was a fragment. If it was, we need
1040 * to rewrite it.
1041 */
1042
1043 if (bpp)
1044 *bpp = NULL;
1045
1046 /* Check for block beyond end of file and fragment extension needed. */
1047 lastblock = lfs_lblkno(fs, lfs_dino_getsize(fs, ip->i_din));
1048 if (lastblock < ULFS_NDADDR && lastblock < lbn) {
1049 osize = lfs_blksize(fs, ip, lastblock);
1050 if (osize < lfs_sb_getbsize(fs) && osize > 0) {
1051 if ((error = lfs_fragextend(vp, osize, lfs_sb_getbsize(fs),
1052 lastblock,
1053 (bpp ? &bp : NULL))))
1054 return (error);
1055 lfs_dino_setsize(fs, ip->i_din, (lastblock + 1) * lfs_sb_getbsize(fs));
1056 ip->i_state |= IN_CHANGE | IN_UPDATE;
1057 if (bpp)
1058 (void) VOP_BWRITE(bp);
1059 }
1060 }
1061
1062 /*
1063 * If the block we are writing is a direct block, it's the last
1064 * block in the file, and offset + iosize is less than a full
1065 * block, we can write one or more fragments. There are two cases:
1066 * the block is brand new and we should allocate it the correct
1067 * size or it already exists and contains some fragments and
1068 * may need to extend it.
1069 */
1070 if (lbn < ULFS_NDADDR && lfs_lblkno(fs, lfs_dino_getsize(fs, ip->i_din)) <= lbn) {
1071 osize = lfs_blksize(fs, ip, lbn);
1072 nsize = lfs_fragroundup(fs, offset + iosize);
1073 if (lfs_lblktosize(fs, lbn) >= lfs_dino_getsize(fs, ip->i_din)) {
1074 /* Brand new block or fragment */
1075 frags = lfs_numfrags(fs, nsize);
1076 if (bpp) {
1077 *bpp = bp = getblk(vp, lbn, nsize);
1078 bp->b_blkno = UNWRITTEN;
1079 }
1080 ip->i_lfs_effnblks += frags;
1081 lfs_sb_subbfree(fs, frags);
1082 lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN);
1083 } else {
1084 if (nsize <= osize) {
1085 /* No need to extend */
1086 if (bpp && (error = bread(vp, lbn, osize,
1087 0, &bp)))
1088 return error;
1089 } else {
1090 /* Extend existing block */
1091 if ((error =
1092 lfs_fragextend(vp, osize, nsize, lbn,
1093 (bpp ? &bp : NULL))))
1094 return error;
1095 }
1096 if (bpp)
1097 *bpp = bp;
1098 }
1099 return 0;
1100 }
1101
1102 error = ulfs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
1103 if (error)
1104 return (error);
1105
1106 /*
1107 * Do byte accounting all at once, so we can gracefully fail *before*
1108 * we start assigning blocks.
1109 */
1110 frags = LFS_FSBTODB(fs, 1); /* frags = VFSTOULFS(vp->v_mount)->um_seqinc; */
1111 bcount = 0;
1112 if (daddr == UNASSIGNED) {
1113 bcount = frags;
1114 }
1115 for (i = 1; i < num; ++i) {
1116 if (!indirs[i].in_exists) {
1117 bcount += frags;
1118 }
1119 }
1120 lfs_sb_subbfree(fs, bcount);
1121 ip->i_lfs_effnblks += bcount;
1122
1123 if (daddr == UNASSIGNED) {
1124 if (num > 0 && lfs_dino_getib(fs, ip->i_din, indirs[0].in_off) == 0) {
1125 lfs_dino_setib(fs, ip->i_din, indirs[0].in_off,
1126 UNWRITTEN);
1127 }
1128
1129 /*
1130 * Create new indirect blocks if necessary
1131 */
1132 if (num > 1) {
1133 idaddr = lfs_dino_getib(fs, ip->i_din, indirs[0].in_off);
1134 for (i = 1; i < num; ++i) {
1135 ibp = getblk(vp, indirs[i].in_lbn,
1136 lfs_sb_getbsize(fs));
1137 if (!indirs[i].in_exists) {
1138 memset(ibp->b_data, 0, ibp->b_bufsize);
1139 ibp->b_blkno = UNWRITTEN;
1140 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
1141 ibp->b_blkno = LFS_FSBTODB(fs, idaddr);
1142 ibp->b_flags |= B_READ;
1143 VOP_STRATEGY(ibp);
1144 }
1145 /*
1146 * This block exists, but the next one may not.
1147 * If that is the case mark it UNWRITTEN to
1148 * keep the accounting straight.
1149 */
1150 if (lfs_iblock_get(fs, ibp->b_data,
1151 indirs[i].in_off) == 0)
1152 lfs_iblock_set(fs, ibp->b_data,
1153 indirs[i].in_off, UNWRITTEN);
1154 idaddr = lfs_iblock_get(fs, ibp->b_data,
1155 indirs[i].in_off);
1156 if ((error = VOP_BWRITE(ibp)))
1157 return error;
1158 }
1159 }
1160 }
1161
1162
1163 /*
1164 * Get the existing block from the cache, if requested.
1165 */
1166 if (bpp)
1167 *bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn));
1168
1169 /*
1170 * The block we are writing may be a brand new block
1171 * in which case we need to do accounting.
1172 *
1173 * We can tell a truly new block because ulfs_bmaparray will say
1174 * it is UNASSIGNED. Once we allocate it we will assign it the
1175 * disk address UNWRITTEN.
1176 */
1177 if (daddr == UNASSIGNED) {
1178 if (bpp) {
1179 /* Note the new address */
1180 bp->b_blkno = UNWRITTEN;
1181 }
1182
1183 switch (num) {
1184 case 0:
1185 lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN);
1186 break;
1187 case 1:
1188 lfs_dino_setib(fs, ip->i_din, indirs[0].in_off,
1189 UNWRITTEN);
1190 break;
1191 default:
1192 idp = &indirs[num - 1];
1193 if (bread(vp, idp->in_lbn, lfs_sb_getbsize(fs), 0, &ibp))
1194 panic("lfs_balloc: bread bno %lld",
1195 (long long)idp->in_lbn);
1196 lfs_iblock_set(fs, ibp->b_data, idp->in_off,
1197 UNWRITTEN);
1198 VOP_BWRITE(ibp);
1199 }
1200 } else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
1201 /*
1202 * Not a brand new block, also not in the cache;
1203 * read it in from disk.
1204 */
1205 if (iosize == lfs_sb_getbsize(fs))
1206 /* Optimization: I/O is unnecessary. */
1207 bp->b_blkno = daddr;
1208 else {
1209 /*
1210 * We need to read the block to preserve the
1211 * existing bytes.
1212 */
1213 bp->b_blkno = daddr;
1214 bp->b_flags |= B_READ;
1215 VOP_STRATEGY(bp);
1216 return 0;
1217 }
1218 }
1219
1220 return (0);
1221 }
1222
1223 int
1224 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
1225 struct ubuf **bpp)
1226 {
1227 struct inode *ip;
1228 struct lfs *fs;
1229 int frags;
1230 int error;
1231
1232 ip = VTOI(vp);
1233 fs = ip->i_lfs;
1234 frags = (long)lfs_numfrags(fs, nsize - osize);
1235 error = 0;
1236
1237 /*
1238 * If we are not asked to actually return the block, all we need
1239 * to do is allocate space for it. UBC will handle dirtying the
1240 * appropriate things and making sure it all goes to disk.
1241 * Don't bother to read in that case.
1242 */
1243 if (bpp && (error = bread(vp, lbn, osize, 0, bpp))) {
1244 brelse(*bpp, 0);
1245 goto out;
1246 }
1247
1248 lfs_sb_subbfree(fs, frags);
1249 ip->i_lfs_effnblks += frags;
1250 ip->i_state |= IN_CHANGE | IN_UPDATE;
1251
1252 if (bpp) {
1253 (*bpp)->b_data = erealloc((*bpp)->b_data, nsize);
1254 (void)memset((*bpp)->b_data + osize, 0, nsize - osize);
1255 }
1256
1257 out:
1258 return (error);
1259 }
1260