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