segwrite.c revision 1.8 1 /* $NetBSD: segwrite.c,v 1.8 2005/03/25 20:16:37 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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37 /*
38 * Copyright (c) 1991, 1993
39 * The Regents of the University of California. All rights reserved.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
66 */
67
68 /*
69 * Partial segment writer, taken from the kernel and adapted for userland.
70 */
71 #include <sys/types.h>
72 #include <sys/param.h>
73 #include <sys/time.h>
74 #include <sys/buf.h>
75 #include <sys/mount.h>
76
77 #include <ufs/ufs/inode.h>
78 #include <ufs/ufs/ufsmount.h>
79
80 /* Override certain things to make <ufs/lfs/lfs.h> work */
81 #undef simple_lock
82 #define simple_lock(x)
83 #undef simple_unlock
84 #define simple_unlock(x)
85 #define vnode uvnode
86 #define buf ubuf
87 #define panic call_panic
88
89 #include <ufs/lfs/lfs.h>
90
91 #include <assert.h>
92 #include <stdio.h>
93 #include <stdlib.h>
94 #include <string.h>
95 #include <err.h>
96 #include <errno.h>
97
98 #include "bufcache.h"
99 #include "vnode.h"
100 #include "lfs.h"
101 #include "segwrite.h"
102
103 /* Compatibility definitions */
104 extern off_t locked_queue_bytes;
105 int locked_queue_count;
106 off_t written_bytes = 0;
107 off_t written_data = 0;
108 off_t written_indir = 0;
109 off_t written_dev = 0;
110 int written_inodes = 0;
111
112 /* Global variables */
113 time_t write_time;
114
115 extern u_int32_t cksum(void *, size_t);
116 extern u_int32_t lfs_sb_cksum(struct dlfs *);
117 extern int preen;
118
119 /*
120 * Logical block number match routines used when traversing the dirty block
121 * chain.
122 */
123 int
124 lfs_match_data(struct lfs * fs, struct ubuf * bp)
125 {
126 return (bp->b_lblkno >= 0);
127 }
128
129 int
130 lfs_match_indir(struct lfs * fs, struct ubuf * bp)
131 {
132 daddr_t lbn;
133
134 lbn = bp->b_lblkno;
135 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
136 }
137
138 int
139 lfs_match_dindir(struct lfs * fs, struct ubuf * bp)
140 {
141 daddr_t lbn;
142
143 lbn = bp->b_lblkno;
144 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
145 }
146
147 int
148 lfs_match_tindir(struct lfs * fs, struct ubuf * bp)
149 {
150 daddr_t lbn;
151
152 lbn = bp->b_lblkno;
153 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
154 }
155
156 /*
157 * Do a checkpoint.
158 */
159 int
160 lfs_segwrite(struct lfs * fs, int flags)
161 {
162 struct inode *ip;
163 struct segment *sp;
164 struct uvnode *vp;
165 int redo;
166
167 lfs_seglock(fs, flags | SEGM_CKP);
168 sp = fs->lfs_sp;
169
170 lfs_writevnodes(fs, sp, VN_REG);
171 lfs_writevnodes(fs, sp, VN_DIROP);
172 ((SEGSUM *) (sp->segsum))->ss_flags &= ~(SS_CONT);
173
174 do {
175 vp = fs->lfs_ivnode;
176 fs->lfs_flags &= ~LFS_IFDIRTY;
177 ip = VTOI(vp);
178 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL || fs->lfs_idaddr <= 0)
179 lfs_writefile(fs, sp, vp);
180
181 redo = lfs_writeinode(fs, sp, ip);
182 redo += lfs_writeseg(fs, sp);
183 redo += (fs->lfs_flags & LFS_IFDIRTY);
184 } while (redo);
185
186 lfs_segunlock(fs);
187 #if 0
188 printf("wrote %" PRId64 " bytes (%" PRId32 " fsb)\n",
189 written_bytes, (ufs_daddr_t)btofsb(fs, written_bytes));
190 printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n",
191 written_data, (ufs_daddr_t)btofsb(fs, written_data));
192 printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n",
193 written_indir, (ufs_daddr_t)btofsb(fs, written_indir));
194 printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n",
195 written_dev, (ufs_daddr_t)btofsb(fs, written_dev));
196 printf("wrote %d inodes (%" PRId32 " fsb)\n",
197 written_inodes, btofsb(fs, written_inodes * fs->lfs_ibsize));
198 #endif
199 return 0;
200 }
201
202 /*
203 * Write the dirty blocks associated with a vnode.
204 */
205 void
206 lfs_writefile(struct lfs * fs, struct segment * sp, struct uvnode * vp)
207 {
208 struct ubuf *bp;
209 struct finfo *fip;
210 struct inode *ip;
211 IFILE *ifp;
212
213 ip = VTOI(vp);
214
215 if (sp->seg_bytes_left < fs->lfs_bsize ||
216 sp->sum_bytes_left < sizeof(struct finfo))
217 (void) lfs_writeseg(fs, sp);
218
219 sp->sum_bytes_left -= FINFOSIZE;
220 ++((SEGSUM *) (sp->segsum))->ss_nfinfo;
221
222 if (vp->v_flag & VDIROP)
223 ((SEGSUM *) (sp->segsum))->ss_flags |= (SS_DIROP | SS_CONT);
224
225 fip = sp->fip;
226 fip->fi_nblocks = 0;
227 fip->fi_ino = ip->i_number;
228 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
229 fip->fi_version = ifp->if_version;
230 brelse(bp);
231
232 lfs_gather(fs, sp, vp, lfs_match_data);
233 lfs_gather(fs, sp, vp, lfs_match_indir);
234 lfs_gather(fs, sp, vp, lfs_match_dindir);
235 lfs_gather(fs, sp, vp, lfs_match_tindir);
236
237 fip = sp->fip;
238 if (fip->fi_nblocks != 0) {
239 sp->fip = (FINFO *) ((caddr_t) fip + FINFOSIZE +
240 sizeof(ufs_daddr_t) * (fip->fi_nblocks));
241 sp->start_lbp = &sp->fip->fi_blocks[0];
242 } else {
243 sp->sum_bytes_left += FINFOSIZE;
244 --((SEGSUM *) (sp->segsum))->ss_nfinfo;
245 }
246 }
247
248 int
249 lfs_writeinode(struct lfs * fs, struct segment * sp, struct inode * ip)
250 {
251 struct ubuf *bp, *ibp;
252 struct ufs1_dinode *cdp;
253 IFILE *ifp;
254 SEGUSE *sup;
255 daddr_t daddr;
256 ino_t ino;
257 int error, i, ndx, fsb = 0;
258 int redo_ifile = 0;
259 struct timespec ts;
260 int gotblk = 0;
261
262 /* Allocate a new inode block if necessary. */
263 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
264 sp->ibp == NULL) {
265 /* Allocate a new segment if necessary. */
266 if (sp->seg_bytes_left < fs->lfs_ibsize ||
267 sp->sum_bytes_left < sizeof(ufs_daddr_t))
268 (void) lfs_writeseg(fs, sp);
269
270 /* Get next inode block. */
271 daddr = fs->lfs_offset;
272 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
273 sp->ibp = *sp->cbpp++ =
274 getblk(fs->lfs_devvp, fsbtodb(fs, daddr),
275 fs->lfs_ibsize);
276 sp->ibp->b_flags |= B_GATHERED;
277 gotblk++;
278
279 /* Zero out inode numbers */
280 for (i = 0; i < INOPB(fs); ++i)
281 ((struct ufs1_dinode *) sp->ibp->b_data)[i].di_inumber = 0;
282
283 ++sp->start_bpp;
284 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
285 /* Set remaining space counters. */
286 sp->seg_bytes_left -= fs->lfs_ibsize;
287 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
288 ndx = fs->lfs_sumsize / sizeof(ufs_daddr_t) -
289 sp->ninodes / INOPB(fs) - 1;
290 ((ufs_daddr_t *) (sp->segsum))[ndx] = daddr;
291 }
292 /* Update the inode times and copy the inode onto the inode page. */
293 ts.tv_nsec = 0;
294 ts.tv_sec = write_time;
295 /* XXX kludge --- don't redirty the ifile just to put times on it */
296 if (ip->i_number != LFS_IFILE_INUM)
297 LFS_ITIMES(ip, &ts, &ts, &ts);
298
299 /*
300 * If this is the Ifile, and we've already written the Ifile in this
301 * partial segment, just overwrite it (it's not on disk yet) and
302 * continue.
303 *
304 * XXX we know that the bp that we get the second time around has
305 * already been gathered.
306 */
307 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
308 *(sp->idp) = *ip->i_din.ffs1_din;
309 ip->i_lfs_osize = ip->i_ffs1_size;
310 return 0;
311 }
312 bp = sp->ibp;
313 cdp = ((struct ufs1_dinode *) bp->b_data) + (sp->ninodes % INOPB(fs));
314 *cdp = *ip->i_din.ffs1_din;
315
316 /* If all blocks are goig to disk, update the "size on disk" */
317 ip->i_lfs_osize = ip->i_ffs1_size;
318
319 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
320 sp->idp = ((struct ufs1_dinode *) bp->b_data) +
321 (sp->ninodes % INOPB(fs));
322 if (gotblk) {
323 LFS_LOCK_BUF(bp);
324 brelse(bp);
325 }
326 /* Increment inode count in segment summary block. */
327 ++((SEGSUM *) (sp->segsum))->ss_ninos;
328
329 /* If this page is full, set flag to allocate a new page. */
330 if (++sp->ninodes % INOPB(fs) == 0)
331 sp->ibp = NULL;
332
333 /*
334 * If updating the ifile, update the super-block. Update the disk
335 * address and access times for this inode in the ifile.
336 */
337 ino = ip->i_number;
338 if (ino == LFS_IFILE_INUM) {
339 daddr = fs->lfs_idaddr;
340 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
341 } else {
342 LFS_IENTRY(ifp, fs, ino, ibp);
343 daddr = ifp->if_daddr;
344 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
345 error = LFS_BWRITE_LOG(ibp); /* Ifile */
346 }
347
348 /*
349 * Account the inode: it no longer belongs to its former segment,
350 * though it will not belong to the new segment until that segment
351 * is actually written.
352 */
353 if (daddr != LFS_UNUSED_DADDR) {
354 u_int32_t oldsn = dtosn(fs, daddr);
355 LFS_SEGENTRY(sup, fs, oldsn, bp);
356 sup->su_nbytes -= DINODE1_SIZE;
357 redo_ifile =
358 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
359 if (redo_ifile)
360 fs->lfs_flags |= LFS_IFDIRTY;
361 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
362 }
363 return redo_ifile;
364 }
365
366 int
367 lfs_gatherblock(struct segment * sp, struct ubuf * bp)
368 {
369 struct lfs *fs;
370 int version;
371 int j, blksinblk;
372
373 /*
374 * If full, finish this segment. We may be doing I/O, so
375 * release and reacquire the splbio().
376 */
377 fs = sp->fs;
378 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
379 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) * blksinblk ||
380 sp->seg_bytes_left < bp->b_bcount) {
381 lfs_updatemeta(sp);
382
383 version = sp->fip->fi_version;
384 (void) lfs_writeseg(fs, sp);
385
386 sp->fip->fi_version = version;
387 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
388 /* Add the current file to the segment summary. */
389 ++((SEGSUM *) (sp->segsum))->ss_nfinfo;
390 sp->sum_bytes_left -= FINFOSIZE;
391
392 return 1;
393 }
394 /* Insert into the buffer list, update the FINFO block. */
395 bp->b_flags |= B_GATHERED;
396 /* bp->b_flags &= ~B_DONE; */
397
398 *sp->cbpp++ = bp;
399 for (j = 0; j < blksinblk; j++)
400 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
401
402 sp->sum_bytes_left -= sizeof(ufs_daddr_t) * blksinblk;
403 sp->seg_bytes_left -= bp->b_bcount;
404 return 0;
405 }
406
407 int
408 lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *))
409 {
410 struct ubuf *bp, *nbp;
411 int count = 0;
412
413 sp->vp = vp;
414 loop:
415 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
416 nbp = LIST_NEXT(bp, b_vnbufs);
417
418 assert(bp->b_flags & B_DELWRI);
419 if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) {
420 continue;
421 }
422 if (lfs_gatherblock(sp, bp)) {
423 goto loop;
424 }
425 count++;
426 }
427
428 lfs_updatemeta(sp);
429 sp->vp = NULL;
430 return count;
431 }
432
433
434 /*
435 * Change the given block's address to ndaddr, finding its previous
436 * location using ufs_bmaparray().
437 *
438 * Account for this change in the segment table.
439 */
440 void
441 lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn,
442 ufs_daddr_t ndaddr, int size)
443 {
444 SEGUSE *sup;
445 struct ubuf *bp;
446 struct indir a[NIADDR + 2], *ap;
447 struct inode *ip;
448 struct uvnode *vp;
449 daddr_t daddr, ooff;
450 int num, error;
451 int bb, osize, obb;
452
453 vp = sp->vp;
454 ip = VTOI(vp);
455
456 error = ufs_bmaparray(fs, vp, lbn, &daddr, a, &num);
457 if (error)
458 errx(1, "lfs_updatemeta: ufs_bmaparray returned %d looking up lbn %" PRId64 "\n", error, lbn);
459 if (daddr > 0)
460 daddr = dbtofsb(fs, daddr);
461
462 bb = fragstofsb(fs, numfrags(fs, size));
463 switch (num) {
464 case 0:
465 ooff = ip->i_ffs1_db[lbn];
466 if (ooff == UNWRITTEN)
467 ip->i_ffs1_blocks += bb;
468 else {
469 /* possible fragment truncation or extension */
470 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
471 ip->i_ffs1_blocks += (bb - obb);
472 }
473 ip->i_ffs1_db[lbn] = ndaddr;
474 break;
475 case 1:
476 ooff = ip->i_ffs1_ib[a[0].in_off];
477 if (ooff == UNWRITTEN)
478 ip->i_ffs1_blocks += bb;
479 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
480 break;
481 default:
482 ap = &a[num - 1];
483 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NULL, &bp))
484 errx(1, "lfs_updatemeta: bread bno %" PRId64,
485 ap->in_lbn);
486
487 ooff = ((ufs_daddr_t *) bp->b_data)[ap->in_off];
488 if (ooff == UNWRITTEN)
489 ip->i_ffs1_blocks += bb;
490 ((ufs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
491 (void) VOP_BWRITE(bp);
492 }
493
494 /*
495 * Update segment usage information, based on old size
496 * and location.
497 */
498 if (daddr > 0) {
499 u_int32_t oldsn = dtosn(fs, daddr);
500 if (lbn >= 0 && lbn < NDADDR)
501 osize = ip->i_lfs_fragsize[lbn];
502 else
503 osize = fs->lfs_bsize;
504 LFS_SEGENTRY(sup, fs, oldsn, bp);
505 sup->su_nbytes -= osize;
506 if (!(bp->b_flags & B_GATHERED))
507 fs->lfs_flags |= LFS_IFDIRTY;
508 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
509 }
510 /*
511 * Now that this block has a new address, and its old
512 * segment no longer owns it, we can forget about its
513 * old size.
514 */
515 if (lbn >= 0 && lbn < NDADDR)
516 ip->i_lfs_fragsize[lbn] = size;
517 }
518
519 /*
520 * Update the metadata that points to the blocks listed in the FINFO
521 * array.
522 */
523 void
524 lfs_updatemeta(struct segment * sp)
525 {
526 struct ubuf *sbp;
527 struct lfs *fs;
528 struct uvnode *vp;
529 daddr_t lbn;
530 int i, nblocks, num;
531 int bb;
532 int bytesleft, size;
533
534 vp = sp->vp;
535 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
536
537 if (vp == NULL || nblocks == 0)
538 return;
539
540 /*
541 * This count may be high due to oversize blocks from lfs_gop_write.
542 * Correct for this. (XXX we should be able to keep track of these.)
543 */
544 fs = sp->fs;
545 for (i = 0; i < nblocks; i++) {
546 if (sp->start_bpp[i] == NULL) {
547 printf("nblocks = %d, not %d\n", i, nblocks);
548 nblocks = i;
549 break;
550 }
551 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
552 nblocks -= num - 1;
553 }
554
555 /*
556 * Sort the blocks.
557 */
558 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
559
560 /*
561 * Record the length of the last block in case it's a fragment.
562 * If there are indirect blocks present, they sort last. An
563 * indirect block will be lfs_bsize and its presence indicates
564 * that you cannot have fragments.
565 */
566 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
567 fs->lfs_bmask) + 1;
568
569 /*
570 * Assign disk addresses, and update references to the logical
571 * block and the segment usage information.
572 */
573 for (i = nblocks; i--; ++sp->start_bpp) {
574 sbp = *sp->start_bpp;
575 lbn = *sp->start_lbp;
576
577 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
578
579 /*
580 * If we write a frag in the wrong place, the cleaner won't
581 * be able to correctly identify its size later, and the
582 * segment will be uncleanable. (Even worse, it will assume
583 * that the indirect block that actually ends the list
584 * is of a smaller size!)
585 */
586 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
587 errx(1, "lfs_updatemeta: fragment is not last block");
588
589 /*
590 * For each subblock in this possibly oversized block,
591 * update its address on disk.
592 */
593 for (bytesleft = sbp->b_bcount; bytesleft > 0;
594 bytesleft -= fs->lfs_bsize) {
595 size = MIN(bytesleft, fs->lfs_bsize);
596 bb = fragstofsb(fs, numfrags(fs, size));
597 lbn = *sp->start_lbp++;
598 lfs_update_single(fs, sp, lbn, fs->lfs_offset, size);
599 fs->lfs_offset += bb;
600 }
601
602 }
603 }
604
605 /*
606 * Start a new segment.
607 */
608 int
609 lfs_initseg(struct lfs * fs)
610 {
611 struct segment *sp;
612 SEGUSE *sup;
613 SEGSUM *ssp;
614 struct ubuf *bp, *sbp;
615 int repeat;
616
617 sp = fs->lfs_sp;
618
619 repeat = 0;
620
621 /* Advance to the next segment. */
622 if (!LFS_PARTIAL_FITS(fs)) {
623 /* lfs_avail eats the remaining space */
624 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
625 fs->lfs_curseg);
626 lfs_newseg(fs);
627 repeat = 1;
628 fs->lfs_offset = fs->lfs_curseg;
629
630 sp->seg_number = dtosn(fs, fs->lfs_curseg);
631 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
632
633 /*
634 * If the segment contains a superblock, update the offset
635 * and summary address to skip over it.
636 */
637 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
638 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
639 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
640 sp->seg_bytes_left -= LFS_SBPAD;
641 }
642 brelse(bp);
643 /* Segment zero could also contain the labelpad */
644 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
645 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
646 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
647 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
648 }
649 } else {
650 sp->seg_number = dtosn(fs, fs->lfs_curseg);
651 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
652 (fs->lfs_offset - fs->lfs_curseg));
653 }
654 fs->lfs_lastpseg = fs->lfs_offset;
655
656 sp->fs = fs;
657 sp->ibp = NULL;
658 sp->idp = NULL;
659 sp->ninodes = 0;
660 sp->ndupino = 0;
661
662 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
663 sp->cbpp = sp->bpp;
664 sbp = *sp->cbpp = getblk(fs->lfs_devvp,
665 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
666 sp->segsum = sbp->b_data;
667 memset(sp->segsum, 0, fs->lfs_sumsize);
668 sp->start_bpp = ++sp->cbpp;
669 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
670
671 /* Set point to SEGSUM, initialize it. */
672 ssp = sp->segsum;
673 ssp->ss_next = fs->lfs_nextseg;
674 ssp->ss_nfinfo = ssp->ss_ninos = 0;
675 ssp->ss_magic = SS_MAGIC;
676
677 /* Set pointer to first FINFO, initialize it. */
678 sp->fip = (struct finfo *) ((caddr_t) sp->segsum + SEGSUM_SIZE(fs));
679 sp->fip->fi_nblocks = 0;
680 sp->start_lbp = &sp->fip->fi_blocks[0];
681 sp->fip->fi_lastlength = 0;
682
683 sp->seg_bytes_left -= fs->lfs_sumsize;
684 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
685
686 LFS_LOCK_BUF(sbp);
687 brelse(sbp);
688 return repeat;
689 }
690
691 /*
692 * Return the next segment to write.
693 */
694 void
695 lfs_newseg(struct lfs * fs)
696 {
697 CLEANERINFO *cip;
698 SEGUSE *sup;
699 struct ubuf *bp;
700 int curseg, isdirty, sn;
701
702 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
703 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
704 sup->su_nbytes = 0;
705 sup->su_nsums = 0;
706 sup->su_ninos = 0;
707 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
708
709 LFS_CLEANERINFO(cip, fs, bp);
710 --cip->clean;
711 ++cip->dirty;
712 fs->lfs_nclean = cip->clean;
713 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
714
715 fs->lfs_lastseg = fs->lfs_curseg;
716 fs->lfs_curseg = fs->lfs_nextseg;
717 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
718 sn = (sn + 1) % fs->lfs_nseg;
719 if (sn == curseg)
720 errx(1, "lfs_nextseg: no clean segments");
721 LFS_SEGENTRY(sup, fs, sn, bp);
722 isdirty = sup->su_flags & SEGUSE_DIRTY;
723 brelse(bp);
724
725 if (!isdirty)
726 break;
727 }
728
729 ++fs->lfs_nactive;
730 fs->lfs_nextseg = sntod(fs, sn);
731 }
732
733
734 int
735 lfs_writeseg(struct lfs * fs, struct segment * sp)
736 {
737 struct ubuf **bpp, *bp;
738 SEGUSE *sup;
739 SEGSUM *ssp;
740 char *datap, *dp;
741 int i;
742 int do_again, nblocks, byteoffset;
743 size_t el_size;
744 u_short ninos;
745 struct uvnode *devvp;
746
747 /*
748 * If there are no buffers other than the segment summary to write
749 * and it is not a checkpoint, don't do anything. On a checkpoint,
750 * even if there aren't any buffers, you need to write the superblock.
751 */
752 if ((nblocks = sp->cbpp - sp->bpp) == 1)
753 return 0;
754
755 devvp = fs->lfs_devvp;
756
757 /* Update the segment usage information. */
758 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
759
760 /* Loop through all blocks, except the segment summary. */
761 for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
762 if ((*bpp)->b_vp != devvp) {
763 sup->su_nbytes += (*bpp)->b_bcount;
764 }
765 }
766
767 ssp = (SEGSUM *) sp->segsum;
768
769 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
770 sup->su_nbytes += ssp->ss_ninos * DINODE1_SIZE;
771
772 if (fs->lfs_version == 1)
773 sup->su_olastmod = write_time;
774 else
775 sup->su_lastmod = write_time;
776 sup->su_ninos += ninos;
777 ++sup->su_nsums;
778 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
779 fs->lfs_ibsize));
780 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
781
782 do_again = !(bp->b_flags & B_GATHERED);
783 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
784
785 /*
786 * Compute checksum across data and then across summary; the first
787 * block (the summary block) is skipped. Set the create time here
788 * so that it's guaranteed to be later than the inode mod times.
789 */
790 if (fs->lfs_version == 1)
791 el_size = sizeof(u_long);
792 else
793 el_size = sizeof(u_int32_t);
794 datap = dp = malloc(nblocks * el_size);
795 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
796 ++bpp;
797 /* Loop through gop_write cluster blocks */
798 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
799 byteoffset += fs->lfs_bsize) {
800 memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
801 dp += el_size;
802 }
803 bremfree(*bpp);
804 (*bpp)->b_flags |= B_BUSY;
805 }
806 if (fs->lfs_version == 1)
807 ssp->ss_ocreate = write_time;
808 else {
809 ssp->ss_create = write_time;
810 ssp->ss_serial = ++fs->lfs_serial;
811 ssp->ss_ident = fs->lfs_ident;
812 }
813 /* Set the summary block busy too */
814 bremfree(*(sp->bpp));
815 (*(sp->bpp))->b_flags |= B_BUSY;
816
817 ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
818 ssp->ss_sumsum =
819 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
820 free(datap);
821 datap = dp = NULL;
822 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
823 btofsb(fs, fs->lfs_sumsize));
824
825 if (devvp == NULL)
826 errx(1, "devvp is NULL");
827 for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
828 bp = *bpp;
829 #if 0
830 printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
831 nblocks - i, bp, bp->b_flags, bp->b_blkno);
832 printf(" vp = %p\n", bp->b_vp);
833 if (bp->b_vp != fs->lfs_devvp)
834 printf(" ino = %d lbn = %" PRId64 "\n",
835 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
836 #endif
837 if (bp->b_vp == fs->lfs_devvp)
838 written_dev += bp->b_bcount;
839 else {
840 if (bp->b_lblkno >= 0)
841 written_data += bp->b_bcount;
842 else
843 written_indir += bp->b_bcount;
844 }
845 bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
846 B_LOCKED);
847 bwrite(bp);
848 written_bytes += bp->b_bcount;
849 }
850 written_inodes += ninos;
851
852 return (lfs_initseg(fs) || do_again);
853 }
854
855 /*
856 * Our own copy of shellsort. XXX use qsort or heapsort.
857 */
858 void
859 lfs_shellsort(struct ubuf ** bp_array, ufs_daddr_t * lb_array, int nmemb, int size)
860 {
861 static int __rsshell_increments[] = {4, 1, 0};
862 int incr, *incrp, t1, t2;
863 struct ubuf *bp_temp;
864
865 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
866 for (t1 = incr; t1 < nmemb; ++t1)
867 for (t2 = t1 - incr; t2 >= 0;)
868 if ((u_int32_t) bp_array[t2]->b_lblkno >
869 (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
870 bp_temp = bp_array[t2];
871 bp_array[t2] = bp_array[t2 + incr];
872 bp_array[t2 + incr] = bp_temp;
873 t2 -= incr;
874 } else
875 break;
876
877 /* Reform the list of logical blocks */
878 incr = 0;
879 for (t1 = 0; t1 < nmemb; t1++) {
880 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
881 lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
882 }
883 }
884 }
885
886
887 /*
888 * lfs_seglock --
889 * Single thread the segment writer.
890 */
891 int
892 lfs_seglock(struct lfs * fs, unsigned long flags)
893 {
894 struct segment *sp;
895
896 if (fs->lfs_seglock) {
897 ++fs->lfs_seglock;
898 fs->lfs_sp->seg_flags |= flags;
899 return 0;
900 }
901 fs->lfs_seglock = 1;
902
903 sp = fs->lfs_sp = (struct segment *) malloc(sizeof(*sp));
904 sp->bpp = (struct ubuf **) malloc(fs->lfs_ssize * sizeof(struct ubuf *));
905 if (!sp->bpp)
906 errx(!preen, "Could not allocate %zu bytes: %s",
907 (size_t)(fs->lfs_ssize * sizeof(struct ubuf *)),
908 strerror(errno));
909 sp->seg_flags = flags;
910 sp->vp = NULL;
911 sp->seg_iocount = 0;
912 (void) lfs_initseg(fs);
913
914 return 0;
915 }
916
917 /*
918 * lfs_segunlock --
919 * Single thread the segment writer.
920 */
921 void
922 lfs_segunlock(struct lfs * fs)
923 {
924 struct segment *sp;
925 struct ubuf *bp;
926
927 sp = fs->lfs_sp;
928
929 if (fs->lfs_seglock == 1) {
930 if (sp->bpp != sp->cbpp) {
931 /* Free allocated segment summary */
932 fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
933 bp = *sp->bpp;
934 bremfree(bp);
935 bp->b_flags |= B_DONE | B_INVAL;
936 bp->b_flags &= ~B_DELWRI;
937 reassignbuf(bp, bp->b_vp);
938 bp->b_flags |= B_BUSY; /* XXX */
939 brelse(bp);
940 } else
941 printf("unlock to 0 with no summary");
942
943 free(sp->bpp);
944 sp->bpp = NULL;
945 free(sp);
946 fs->lfs_sp = NULL;
947
948 fs->lfs_nactive = 0;
949
950 /* Since we *know* everything's on disk, write both sbs */
951 lfs_writesuper(fs, fs->lfs_sboffs[0]);
952 lfs_writesuper(fs, fs->lfs_sboffs[1]);
953
954 --fs->lfs_seglock;
955 fs->lfs_lockpid = 0;
956 } else if (fs->lfs_seglock == 0) {
957 errx(1, "Seglock not held");
958 } else {
959 --fs->lfs_seglock;
960 }
961 }
962
963 int
964 lfs_writevnodes(struct lfs *fs, struct segment *sp, int op)
965 {
966 struct inode *ip;
967 struct uvnode *vp;
968 int inodes_written = 0;
969
970 LIST_FOREACH(vp, &vnodelist, v_mntvnodes) {
971 if (vp->v_bmap_op != lfs_vop_bmap)
972 continue;
973
974 ip = VTOI(vp);
975
976 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
977 (op != VN_DIROP && (vp->v_flag & VDIROP))) {
978 continue;
979 }
980 /*
981 * Write the inode/file if dirty and it's not the IFILE.
982 */
983 if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) {
984 if (ip->i_number != LFS_IFILE_INUM)
985 lfs_writefile(fs, sp, vp);
986 (void) lfs_writeinode(fs, sp, ip);
987 inodes_written++;
988 }
989 }
990 return inodes_written;
991 }
992
993 void
994 lfs_writesuper(struct lfs *fs, ufs_daddr_t daddr)
995 {
996 struct ubuf *bp;
997
998 /* Set timestamp of this version of the superblock */
999 if (fs->lfs_version == 1)
1000 fs->lfs_otstamp = write_time;
1001 fs->lfs_tstamp = write_time;
1002
1003 /* Checksum the superblock and copy it into a buffer. */
1004 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1005 assert(daddr > 0);
1006 bp = getblk(fs->lfs_devvp, fsbtodb(fs, daddr), LFS_SBPAD);
1007 memset(bp->b_data + sizeof(struct dlfs), 0,
1008 LFS_SBPAD - sizeof(struct dlfs));
1009 *(struct dlfs *) bp->b_data = fs->lfs_dlfs;
1010
1011 bwrite(bp);
1012 }
1013