segwrite.c revision 1.11 1 /* $NetBSD: segwrite.c,v 1.11 2006/03/17 15:53:46 rumble 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 #define vnode uvnode
82 #define buf ubuf
83 #define panic call_panic
84
85 #include <ufs/lfs/lfs.h>
86
87 #include <assert.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <err.h>
92 #include <errno.h>
93
94 #include "bufcache.h"
95 #include "vnode.h"
96 #include "lfs_user.h"
97 #include "segwrite.h"
98
99 /* Compatibility definitions */
100 extern off_t locked_queue_bytes;
101 int locked_queue_count;
102 off_t written_bytes = 0;
103 off_t written_data = 0;
104 off_t written_indir = 0;
105 off_t written_dev = 0;
106 int written_inodes = 0;
107
108 /* Global variables */
109 time_t write_time;
110
111 extern u_int32_t cksum(void *, size_t);
112 extern u_int32_t lfs_sb_cksum(struct dlfs *);
113 extern int preen;
114
115 /*
116 * Logical block number match routines used when traversing the dirty block
117 * chain.
118 */
119 int
120 lfs_match_data(struct lfs * fs, struct ubuf * bp)
121 {
122 return (bp->b_lblkno >= 0);
123 }
124
125 int
126 lfs_match_indir(struct lfs * fs, struct ubuf * bp)
127 {
128 daddr_t lbn;
129
130 lbn = bp->b_lblkno;
131 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
132 }
133
134 int
135 lfs_match_dindir(struct lfs * fs, struct ubuf * bp)
136 {
137 daddr_t lbn;
138
139 lbn = bp->b_lblkno;
140 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
141 }
142
143 int
144 lfs_match_tindir(struct lfs * fs, struct ubuf * bp)
145 {
146 daddr_t lbn;
147
148 lbn = bp->b_lblkno;
149 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
150 }
151
152 /*
153 * Do a checkpoint.
154 */
155 int
156 lfs_segwrite(struct lfs * fs, int flags)
157 {
158 struct inode *ip;
159 struct segment *sp;
160 struct uvnode *vp;
161 int redo;
162
163 lfs_seglock(fs, flags | SEGM_CKP);
164 sp = fs->lfs_sp;
165
166 lfs_writevnodes(fs, sp, VN_REG);
167 lfs_writevnodes(fs, sp, VN_DIROP);
168 ((SEGSUM *) (sp->segsum))->ss_flags &= ~(SS_CONT);
169
170 do {
171 vp = fs->lfs_ivnode;
172 fs->lfs_flags &= ~LFS_IFDIRTY;
173 ip = VTOI(vp);
174 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL || fs->lfs_idaddr <= 0)
175 lfs_writefile(fs, sp, vp);
176
177 redo = lfs_writeinode(fs, sp, ip);
178 redo += lfs_writeseg(fs, sp);
179 redo += (fs->lfs_flags & LFS_IFDIRTY);
180 } while (redo);
181
182 lfs_segunlock(fs);
183 #if 0
184 printf("wrote %" PRId64 " bytes (%" PRId32 " fsb)\n",
185 written_bytes, (ufs_daddr_t)btofsb(fs, written_bytes));
186 printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n",
187 written_data, (ufs_daddr_t)btofsb(fs, written_data));
188 printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n",
189 written_indir, (ufs_daddr_t)btofsb(fs, written_indir));
190 printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n",
191 written_dev, (ufs_daddr_t)btofsb(fs, written_dev));
192 printf("wrote %d inodes (%" PRId32 " fsb)\n",
193 written_inodes, btofsb(fs, written_inodes * fs->lfs_ibsize));
194 #endif
195 return 0;
196 }
197
198 /*
199 * Write the dirty blocks associated with a vnode.
200 */
201 void
202 lfs_writefile(struct lfs * fs, struct segment * sp, struct uvnode * vp)
203 {
204 struct ubuf *bp;
205 struct finfo *fip;
206 struct inode *ip;
207 IFILE *ifp;
208
209 ip = VTOI(vp);
210
211 if (sp->seg_bytes_left < fs->lfs_bsize ||
212 sp->sum_bytes_left < sizeof(struct finfo))
213 (void) lfs_writeseg(fs, sp);
214
215 sp->sum_bytes_left -= FINFOSIZE;
216 ++((SEGSUM *) (sp->segsum))->ss_nfinfo;
217
218 if (vp->v_flag & VDIROP)
219 ((SEGSUM *) (sp->segsum))->ss_flags |= (SS_DIROP | SS_CONT);
220
221 fip = sp->fip;
222 fip->fi_nblocks = 0;
223 fip->fi_ino = ip->i_number;
224 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
225 fip->fi_version = ifp->if_version;
226 brelse(bp);
227
228 lfs_gather(fs, sp, vp, lfs_match_data);
229 lfs_gather(fs, sp, vp, lfs_match_indir);
230 lfs_gather(fs, sp, vp, lfs_match_dindir);
231 lfs_gather(fs, sp, vp, lfs_match_tindir);
232
233 fip = sp->fip;
234 if (fip->fi_nblocks != 0) {
235 sp->fip = (FINFO *) ((caddr_t) fip + FINFOSIZE +
236 sizeof(ufs_daddr_t) * (fip->fi_nblocks));
237 sp->start_lbp = &sp->fip->fi_blocks[0];
238 } else {
239 sp->sum_bytes_left += FINFOSIZE;
240 --((SEGSUM *) (sp->segsum))->ss_nfinfo;
241 }
242 }
243
244 int
245 lfs_writeinode(struct lfs * fs, struct segment * sp, struct inode * ip)
246 {
247 struct ubuf *bp, *ibp;
248 struct ufs1_dinode *cdp;
249 IFILE *ifp;
250 SEGUSE *sup;
251 daddr_t daddr;
252 ino_t ino;
253 int error, i, ndx, fsb = 0;
254 int redo_ifile = 0;
255 struct timespec ts;
256 int gotblk = 0;
257
258 /* Allocate a new inode block if necessary. */
259 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
260 sp->ibp == NULL) {
261 /* Allocate a new segment if necessary. */
262 if (sp->seg_bytes_left < fs->lfs_ibsize ||
263 sp->sum_bytes_left < sizeof(ufs_daddr_t))
264 (void) lfs_writeseg(fs, sp);
265
266 /* Get next inode block. */
267 daddr = fs->lfs_offset;
268 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
269 sp->ibp = *sp->cbpp++ =
270 getblk(fs->lfs_devvp, fsbtodb(fs, daddr),
271 fs->lfs_ibsize);
272 sp->ibp->b_flags |= B_GATHERED;
273 gotblk++;
274
275 /* Zero out inode numbers */
276 for (i = 0; i < INOPB(fs); ++i)
277 ((struct ufs1_dinode *) sp->ibp->b_data)[i].di_inumber = 0;
278
279 ++sp->start_bpp;
280 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
281 /* Set remaining space counters. */
282 sp->seg_bytes_left -= fs->lfs_ibsize;
283 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
284 ndx = fs->lfs_sumsize / sizeof(ufs_daddr_t) -
285 sp->ninodes / INOPB(fs) - 1;
286 ((ufs_daddr_t *) (sp->segsum))[ndx] = daddr;
287 }
288 /* Update the inode times and copy the inode onto the inode page. */
289 ts.tv_nsec = 0;
290 ts.tv_sec = write_time;
291 /* XXX kludge --- don't redirty the ifile just to put times on it */
292 if (ip->i_number != LFS_IFILE_INUM)
293 LFS_ITIMES(ip, &ts, &ts, &ts);
294
295 /*
296 * If this is the Ifile, and we've already written the Ifile in this
297 * partial segment, just overwrite it (it's not on disk yet) and
298 * continue.
299 *
300 * XXX we know that the bp that we get the second time around has
301 * already been gathered.
302 */
303 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
304 *(sp->idp) = *ip->i_din.ffs1_din;
305 ip->i_lfs_osize = ip->i_ffs1_size;
306 return 0;
307 }
308 bp = sp->ibp;
309 cdp = ((struct ufs1_dinode *) bp->b_data) + (sp->ninodes % INOPB(fs));
310 *cdp = *ip->i_din.ffs1_din;
311
312 /* If all blocks are goig to disk, update the "size on disk" */
313 ip->i_lfs_osize = ip->i_ffs1_size;
314
315 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
316 sp->idp = ((struct ufs1_dinode *) bp->b_data) +
317 (sp->ninodes % INOPB(fs));
318 if (gotblk) {
319 LFS_LOCK_BUF(bp);
320 brelse(bp);
321 }
322 /* Increment inode count in segment summary block. */
323 ++((SEGSUM *) (sp->segsum))->ss_ninos;
324
325 /* If this page is full, set flag to allocate a new page. */
326 if (++sp->ninodes % INOPB(fs) == 0)
327 sp->ibp = NULL;
328
329 /*
330 * If updating the ifile, update the super-block. Update the disk
331 * address and access times for this inode in the ifile.
332 */
333 ino = ip->i_number;
334 if (ino == LFS_IFILE_INUM) {
335 daddr = fs->lfs_idaddr;
336 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
337 } else {
338 LFS_IENTRY(ifp, fs, ino, ibp);
339 daddr = ifp->if_daddr;
340 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
341 error = LFS_BWRITE_LOG(ibp); /* Ifile */
342 }
343
344 /*
345 * Account the inode: it no longer belongs to its former segment,
346 * though it will not belong to the new segment until that segment
347 * is actually written.
348 */
349 if (daddr != LFS_UNUSED_DADDR) {
350 u_int32_t oldsn = dtosn(fs, daddr);
351 LFS_SEGENTRY(sup, fs, oldsn, bp);
352 sup->su_nbytes -= DINODE1_SIZE;
353 redo_ifile =
354 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
355 if (redo_ifile)
356 fs->lfs_flags |= LFS_IFDIRTY;
357 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
358 }
359 return redo_ifile;
360 }
361
362 int
363 lfs_gatherblock(struct segment * sp, struct ubuf * bp)
364 {
365 struct lfs *fs;
366 int version;
367 int j, blksinblk;
368
369 /*
370 * If full, finish this segment. We may be doing I/O, so
371 * release and reacquire the splbio().
372 */
373 fs = sp->fs;
374 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
375 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) * blksinblk ||
376 sp->seg_bytes_left < bp->b_bcount) {
377 lfs_updatemeta(sp);
378
379 version = sp->fip->fi_version;
380 (void) lfs_writeseg(fs, sp);
381
382 sp->fip->fi_version = version;
383 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
384 /* Add the current file to the segment summary. */
385 ++((SEGSUM *) (sp->segsum))->ss_nfinfo;
386 sp->sum_bytes_left -= FINFOSIZE;
387
388 return 1;
389 }
390 /* Insert into the buffer list, update the FINFO block. */
391 bp->b_flags |= B_GATHERED;
392 /* bp->b_flags &= ~B_DONE; */
393
394 *sp->cbpp++ = bp;
395 for (j = 0; j < blksinblk; j++)
396 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
397
398 sp->sum_bytes_left -= sizeof(ufs_daddr_t) * blksinblk;
399 sp->seg_bytes_left -= bp->b_bcount;
400 return 0;
401 }
402
403 int
404 lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *))
405 {
406 struct ubuf *bp, *nbp;
407 int count = 0;
408
409 sp->vp = vp;
410 loop:
411 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
412 nbp = LIST_NEXT(bp, b_vnbufs);
413
414 assert(bp->b_flags & B_DELWRI);
415 if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) {
416 continue;
417 }
418 if (lfs_gatherblock(sp, bp)) {
419 goto loop;
420 }
421 count++;
422 }
423
424 lfs_updatemeta(sp);
425 sp->vp = NULL;
426 return count;
427 }
428
429
430 /*
431 * Change the given block's address to ndaddr, finding its previous
432 * location using ufs_bmaparray().
433 *
434 * Account for this change in the segment table.
435 */
436 void
437 lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn,
438 ufs_daddr_t ndaddr, int size)
439 {
440 SEGUSE *sup;
441 struct ubuf *bp;
442 struct indir a[NIADDR + 2], *ap;
443 struct inode *ip;
444 struct uvnode *vp;
445 daddr_t daddr, ooff;
446 int num, error;
447 int bb, osize, obb;
448
449 vp = sp->vp;
450 ip = VTOI(vp);
451
452 error = ufs_bmaparray(fs, vp, lbn, &daddr, a, &num);
453 if (error)
454 errx(1, "lfs_updatemeta: ufs_bmaparray returned %d looking up lbn %" PRId64 "\n", error, lbn);
455 if (daddr > 0)
456 daddr = dbtofsb(fs, daddr);
457
458 bb = fragstofsb(fs, numfrags(fs, size));
459 switch (num) {
460 case 0:
461 ooff = ip->i_ffs1_db[lbn];
462 if (ooff == UNWRITTEN)
463 ip->i_ffs1_blocks += bb;
464 else {
465 /* possible fragment truncation or extension */
466 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
467 ip->i_ffs1_blocks += (bb - obb);
468 }
469 ip->i_ffs1_db[lbn] = ndaddr;
470 break;
471 case 1:
472 ooff = ip->i_ffs1_ib[a[0].in_off];
473 if (ooff == UNWRITTEN)
474 ip->i_ffs1_blocks += bb;
475 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
476 break;
477 default:
478 ap = &a[num - 1];
479 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NULL, &bp))
480 errx(1, "lfs_updatemeta: bread bno %" PRId64,
481 ap->in_lbn);
482
483 ooff = ((ufs_daddr_t *) bp->b_data)[ap->in_off];
484 if (ooff == UNWRITTEN)
485 ip->i_ffs1_blocks += bb;
486 ((ufs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
487 (void) VOP_BWRITE(bp);
488 }
489
490 /*
491 * Update segment usage information, based on old size
492 * and location.
493 */
494 if (daddr > 0) {
495 u_int32_t oldsn = dtosn(fs, daddr);
496 if (lbn >= 0 && lbn < NDADDR)
497 osize = ip->i_lfs_fragsize[lbn];
498 else
499 osize = fs->lfs_bsize;
500 LFS_SEGENTRY(sup, fs, oldsn, bp);
501 sup->su_nbytes -= osize;
502 if (!(bp->b_flags & B_GATHERED))
503 fs->lfs_flags |= LFS_IFDIRTY;
504 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
505 }
506 /*
507 * Now that this block has a new address, and its old
508 * segment no longer owns it, we can forget about its
509 * old size.
510 */
511 if (lbn >= 0 && lbn < NDADDR)
512 ip->i_lfs_fragsize[lbn] = size;
513 }
514
515 /*
516 * Update the metadata that points to the blocks listed in the FINFO
517 * array.
518 */
519 void
520 lfs_updatemeta(struct segment * sp)
521 {
522 struct ubuf *sbp;
523 struct lfs *fs;
524 struct uvnode *vp;
525 daddr_t lbn;
526 int i, nblocks, num;
527 int bb;
528 int bytesleft, size;
529
530 vp = sp->vp;
531 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
532
533 if (vp == NULL || nblocks == 0)
534 return;
535
536 /*
537 * This count may be high due to oversize blocks from lfs_gop_write.
538 * Correct for this. (XXX we should be able to keep track of these.)
539 */
540 fs = sp->fs;
541 for (i = 0; i < nblocks; i++) {
542 if (sp->start_bpp[i] == NULL) {
543 printf("nblocks = %d, not %d\n", i, nblocks);
544 nblocks = i;
545 break;
546 }
547 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
548 nblocks -= num - 1;
549 }
550
551 /*
552 * Sort the blocks.
553 */
554 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
555
556 /*
557 * Record the length of the last block in case it's a fragment.
558 * If there are indirect blocks present, they sort last. An
559 * indirect block will be lfs_bsize and its presence indicates
560 * that you cannot have fragments.
561 */
562 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
563 fs->lfs_bmask) + 1;
564
565 /*
566 * Assign disk addresses, and update references to the logical
567 * block and the segment usage information.
568 */
569 for (i = nblocks; i--; ++sp->start_bpp) {
570 sbp = *sp->start_bpp;
571 lbn = *sp->start_lbp;
572
573 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
574
575 /*
576 * If we write a frag in the wrong place, the cleaner won't
577 * be able to correctly identify its size later, and the
578 * segment will be uncleanable. (Even worse, it will assume
579 * that the indirect block that actually ends the list
580 * is of a smaller size!)
581 */
582 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
583 errx(1, "lfs_updatemeta: fragment is not last block");
584
585 /*
586 * For each subblock in this possibly oversized block,
587 * update its address on disk.
588 */
589 for (bytesleft = sbp->b_bcount; bytesleft > 0;
590 bytesleft -= fs->lfs_bsize) {
591 size = MIN(bytesleft, fs->lfs_bsize);
592 bb = fragstofsb(fs, numfrags(fs, size));
593 lbn = *sp->start_lbp++;
594 lfs_update_single(fs, sp, lbn, fs->lfs_offset, size);
595 fs->lfs_offset += bb;
596 }
597
598 }
599 }
600
601 /*
602 * Start a new segment.
603 */
604 int
605 lfs_initseg(struct lfs * fs)
606 {
607 struct segment *sp;
608 SEGUSE *sup;
609 SEGSUM *ssp;
610 struct ubuf *bp, *sbp;
611 int repeat;
612
613 sp = fs->lfs_sp;
614
615 repeat = 0;
616
617 /* Advance to the next segment. */
618 if (!LFS_PARTIAL_FITS(fs)) {
619 /* lfs_avail eats the remaining space */
620 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
621 fs->lfs_curseg);
622 lfs_newseg(fs);
623 repeat = 1;
624 fs->lfs_offset = fs->lfs_curseg;
625
626 sp->seg_number = dtosn(fs, fs->lfs_curseg);
627 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
628
629 /*
630 * If the segment contains a superblock, update the offset
631 * and summary address to skip over it.
632 */
633 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
634 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
635 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
636 sp->seg_bytes_left -= LFS_SBPAD;
637 }
638 brelse(bp);
639 /* Segment zero could also contain the labelpad */
640 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
641 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
642 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
643 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
644 }
645 } else {
646 sp->seg_number = dtosn(fs, fs->lfs_curseg);
647 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
648 (fs->lfs_offset - fs->lfs_curseg));
649 }
650 fs->lfs_lastpseg = fs->lfs_offset;
651
652 sp->fs = fs;
653 sp->ibp = NULL;
654 sp->idp = NULL;
655 sp->ninodes = 0;
656 sp->ndupino = 0;
657
658 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
659 sp->cbpp = sp->bpp;
660 sbp = *sp->cbpp = getblk(fs->lfs_devvp,
661 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
662 sp->segsum = sbp->b_data;
663 memset(sp->segsum, 0, fs->lfs_sumsize);
664 sp->start_bpp = ++sp->cbpp;
665 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
666
667 /* Set point to SEGSUM, initialize it. */
668 ssp = sp->segsum;
669 ssp->ss_next = fs->lfs_nextseg;
670 ssp->ss_nfinfo = ssp->ss_ninos = 0;
671 ssp->ss_magic = SS_MAGIC;
672
673 /* Set pointer to first FINFO, initialize it. */
674 sp->fip = (struct finfo *) ((caddr_t) sp->segsum + SEGSUM_SIZE(fs));
675 sp->fip->fi_nblocks = 0;
676 sp->start_lbp = &sp->fip->fi_blocks[0];
677 sp->fip->fi_lastlength = 0;
678
679 sp->seg_bytes_left -= fs->lfs_sumsize;
680 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
681
682 LFS_LOCK_BUF(sbp);
683 brelse(sbp);
684 return repeat;
685 }
686
687 /*
688 * Return the next segment to write.
689 */
690 void
691 lfs_newseg(struct lfs * fs)
692 {
693 CLEANERINFO *cip;
694 SEGUSE *sup;
695 struct ubuf *bp;
696 int curseg, isdirty, sn;
697
698 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
699 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
700 sup->su_nbytes = 0;
701 sup->su_nsums = 0;
702 sup->su_ninos = 0;
703 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
704
705 LFS_CLEANERINFO(cip, fs, bp);
706 --cip->clean;
707 ++cip->dirty;
708 fs->lfs_nclean = cip->clean;
709 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
710
711 fs->lfs_lastseg = fs->lfs_curseg;
712 fs->lfs_curseg = fs->lfs_nextseg;
713 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
714 sn = (sn + 1) % fs->lfs_nseg;
715 if (sn == curseg)
716 errx(1, "lfs_nextseg: no clean segments");
717 LFS_SEGENTRY(sup, fs, sn, bp);
718 isdirty = sup->su_flags & SEGUSE_DIRTY;
719 brelse(bp);
720
721 if (!isdirty)
722 break;
723 }
724
725 ++fs->lfs_nactive;
726 fs->lfs_nextseg = sntod(fs, sn);
727 }
728
729
730 int
731 lfs_writeseg(struct lfs * fs, struct segment * sp)
732 {
733 struct ubuf **bpp, *bp;
734 SEGUSE *sup;
735 SEGSUM *ssp;
736 char *datap, *dp;
737 int i;
738 int do_again, nblocks, byteoffset;
739 size_t el_size;
740 u_short ninos;
741 struct uvnode *devvp;
742
743 /*
744 * If there are no buffers other than the segment summary to write
745 * and it is not a checkpoint, don't do anything. On a checkpoint,
746 * even if there aren't any buffers, you need to write the superblock.
747 */
748 if ((nblocks = sp->cbpp - sp->bpp) == 1)
749 return 0;
750
751 devvp = fs->lfs_devvp;
752
753 /* Update the segment usage information. */
754 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
755
756 /* Loop through all blocks, except the segment summary. */
757 for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
758 if ((*bpp)->b_vp != devvp) {
759 sup->su_nbytes += (*bpp)->b_bcount;
760 }
761 }
762
763 ssp = (SEGSUM *) sp->segsum;
764
765 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
766 sup->su_nbytes += ssp->ss_ninos * DINODE1_SIZE;
767
768 if (fs->lfs_version == 1)
769 sup->su_olastmod = write_time;
770 else
771 sup->su_lastmod = write_time;
772 sup->su_ninos += ninos;
773 ++sup->su_nsums;
774 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
775 fs->lfs_ibsize));
776 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
777
778 do_again = !(bp->b_flags & B_GATHERED);
779 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
780
781 /*
782 * Compute checksum across data and then across summary; the first
783 * block (the summary block) is skipped. Set the create time here
784 * so that it's guaranteed to be later than the inode mod times.
785 */
786 if (fs->lfs_version == 1)
787 el_size = sizeof(u_long);
788 else
789 el_size = sizeof(u_int32_t);
790 datap = dp = malloc(nblocks * el_size);
791 if (dp == NULL)
792 err(1, NULL);
793 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
794 ++bpp;
795 /* Loop through gop_write cluster blocks */
796 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
797 byteoffset += fs->lfs_bsize) {
798 memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
799 dp += el_size;
800 }
801 bremfree(*bpp);
802 (*bpp)->b_flags |= B_BUSY;
803 }
804 if (fs->lfs_version == 1)
805 ssp->ss_ocreate = write_time;
806 else {
807 ssp->ss_create = write_time;
808 ssp->ss_serial = ++fs->lfs_serial;
809 ssp->ss_ident = fs->lfs_ident;
810 }
811 /* Set the summary block busy too */
812 bremfree(*(sp->bpp));
813 (*(sp->bpp))->b_flags |= B_BUSY;
814
815 ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
816 ssp->ss_sumsum =
817 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
818 free(datap);
819 datap = dp = NULL;
820 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
821 btofsb(fs, fs->lfs_sumsize));
822
823 if (devvp == NULL)
824 errx(1, "devvp is NULL");
825 for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
826 bp = *bpp;
827 #if 0
828 printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
829 nblocks - i, bp, bp->b_flags, bp->b_blkno);
830 printf(" vp = %p\n", bp->b_vp);
831 if (bp->b_vp != fs->lfs_devvp)
832 printf(" ino = %d lbn = %" PRId64 "\n",
833 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
834 #endif
835 if (bp->b_vp == fs->lfs_devvp)
836 written_dev += bp->b_bcount;
837 else {
838 if (bp->b_lblkno >= 0)
839 written_data += bp->b_bcount;
840 else
841 written_indir += bp->b_bcount;
842 }
843 bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
844 B_LOCKED);
845 bwrite(bp);
846 written_bytes += bp->b_bcount;
847 }
848 written_inodes += ninos;
849
850 return (lfs_initseg(fs) || do_again);
851 }
852
853 /*
854 * Our own copy of shellsort. XXX use qsort or heapsort.
855 */
856 void
857 lfs_shellsort(struct ubuf ** bp_array, ufs_daddr_t * lb_array, int nmemb, int size)
858 {
859 static int __rsshell_increments[] = {4, 1, 0};
860 int incr, *incrp, t1, t2;
861 struct ubuf *bp_temp;
862
863 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
864 for (t1 = incr; t1 < nmemb; ++t1)
865 for (t2 = t1 - incr; t2 >= 0;)
866 if ((u_int32_t) bp_array[t2]->b_lblkno >
867 (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
868 bp_temp = bp_array[t2];
869 bp_array[t2] = bp_array[t2 + incr];
870 bp_array[t2 + incr] = bp_temp;
871 t2 -= incr;
872 } else
873 break;
874
875 /* Reform the list of logical blocks */
876 incr = 0;
877 for (t1 = 0; t1 < nmemb; t1++) {
878 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
879 lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
880 }
881 }
882 }
883
884
885 /*
886 * lfs_seglock --
887 * Single thread the segment writer.
888 */
889 int
890 lfs_seglock(struct lfs * fs, unsigned long flags)
891 {
892 struct segment *sp;
893
894 if (fs->lfs_seglock) {
895 ++fs->lfs_seglock;
896 fs->lfs_sp->seg_flags |= flags;
897 return 0;
898 }
899 fs->lfs_seglock = 1;
900
901 sp = fs->lfs_sp = (struct segment *) malloc(sizeof(*sp));
902 if (sp == NULL)
903 err(1, NULL);
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