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