segwrite.c revision 1.12 1 /* $NetBSD: segwrite.c,v 1.12 2006/05/23 22:35:20 jnemeth 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 assert(!(bp->b_flags & B_INVAL));
321 brelse(bp);
322 }
323 /* Increment inode count in segment summary block. */
324 ++((SEGSUM *) (sp->segsum))->ss_ninos;
325
326 /* If this page is full, set flag to allocate a new page. */
327 if (++sp->ninodes % INOPB(fs) == 0)
328 sp->ibp = NULL;
329
330 /*
331 * If updating the ifile, update the super-block. Update the disk
332 * address and access times for this inode in the ifile.
333 */
334 ino = ip->i_number;
335 if (ino == LFS_IFILE_INUM) {
336 daddr = fs->lfs_idaddr;
337 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
338 } else {
339 LFS_IENTRY(ifp, fs, ino, ibp);
340 daddr = ifp->if_daddr;
341 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
342 error = LFS_BWRITE_LOG(ibp); /* Ifile */
343 }
344
345 /*
346 * Account the inode: it no longer belongs to its former segment,
347 * though it will not belong to the new segment until that segment
348 * is actually written.
349 */
350 if (daddr != LFS_UNUSED_DADDR) {
351 u_int32_t oldsn = dtosn(fs, daddr);
352 LFS_SEGENTRY(sup, fs, oldsn, bp);
353 sup->su_nbytes -= DINODE1_SIZE;
354 redo_ifile =
355 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
356 if (redo_ifile)
357 fs->lfs_flags |= LFS_IFDIRTY;
358 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
359 }
360 return redo_ifile;
361 }
362
363 int
364 lfs_gatherblock(struct segment * sp, struct ubuf * bp)
365 {
366 struct lfs *fs;
367 int version;
368 int j, blksinblk;
369
370 /*
371 * If full, finish this segment. We may be doing I/O, so
372 * release and reacquire the splbio().
373 */
374 fs = sp->fs;
375 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
376 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) * blksinblk ||
377 sp->seg_bytes_left < bp->b_bcount) {
378 lfs_updatemeta(sp);
379
380 version = sp->fip->fi_version;
381 (void) lfs_writeseg(fs, sp);
382
383 sp->fip->fi_version = version;
384 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
385 /* Add the current file to the segment summary. */
386 ++((SEGSUM *) (sp->segsum))->ss_nfinfo;
387 sp->sum_bytes_left -= FINFOSIZE;
388
389 return 1;
390 }
391 /* Insert into the buffer list, update the FINFO block. */
392 bp->b_flags |= B_GATHERED;
393 /* bp->b_flags &= ~B_DONE; */
394
395 *sp->cbpp++ = bp;
396 for (j = 0; j < blksinblk; j++)
397 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
398
399 sp->sum_bytes_left -= sizeof(ufs_daddr_t) * blksinblk;
400 sp->seg_bytes_left -= bp->b_bcount;
401 return 0;
402 }
403
404 int
405 lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *))
406 {
407 struct ubuf *bp, *nbp;
408 int count = 0;
409
410 sp->vp = vp;
411 loop:
412 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
413 nbp = LIST_NEXT(bp, b_vnbufs);
414
415 assert(bp->b_flags & B_DELWRI);
416 if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) {
417 continue;
418 }
419 if (lfs_gatherblock(sp, bp)) {
420 goto loop;
421 }
422 count++;
423 }
424
425 lfs_updatemeta(sp);
426 sp->vp = NULL;
427 return count;
428 }
429
430
431 /*
432 * Change the given block's address to ndaddr, finding its previous
433 * location using ufs_bmaparray().
434 *
435 * Account for this change in the segment table.
436 */
437 void
438 lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn,
439 ufs_daddr_t ndaddr, int size)
440 {
441 SEGUSE *sup;
442 struct ubuf *bp;
443 struct indir a[NIADDR + 2], *ap;
444 struct inode *ip;
445 struct uvnode *vp;
446 daddr_t daddr, ooff;
447 int num, error;
448 int bb, osize, obb;
449
450 vp = sp->vp;
451 ip = VTOI(vp);
452
453 error = ufs_bmaparray(fs, vp, lbn, &daddr, a, &num);
454 if (error)
455 errx(1, "lfs_updatemeta: ufs_bmaparray returned %d looking up lbn %" PRId64 "\n", error, lbn);
456 if (daddr > 0)
457 daddr = dbtofsb(fs, daddr);
458
459 bb = fragstofsb(fs, numfrags(fs, size));
460 switch (num) {
461 case 0:
462 ooff = ip->i_ffs1_db[lbn];
463 if (ooff == UNWRITTEN)
464 ip->i_ffs1_blocks += bb;
465 else {
466 /* possible fragment truncation or extension */
467 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
468 ip->i_ffs1_blocks += (bb - obb);
469 }
470 ip->i_ffs1_db[lbn] = ndaddr;
471 break;
472 case 1:
473 ooff = ip->i_ffs1_ib[a[0].in_off];
474 if (ooff == UNWRITTEN)
475 ip->i_ffs1_blocks += bb;
476 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
477 break;
478 default:
479 ap = &a[num - 1];
480 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NULL, &bp))
481 errx(1, "lfs_updatemeta: bread bno %" PRId64,
482 ap->in_lbn);
483
484 ooff = ((ufs_daddr_t *) bp->b_data)[ap->in_off];
485 if (ooff == UNWRITTEN)
486 ip->i_ffs1_blocks += bb;
487 ((ufs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
488 (void) VOP_BWRITE(bp);
489 }
490
491 /*
492 * Update segment usage information, based on old size
493 * and location.
494 */
495 if (daddr > 0) {
496 u_int32_t oldsn = dtosn(fs, daddr);
497 if (lbn >= 0 && lbn < NDADDR)
498 osize = ip->i_lfs_fragsize[lbn];
499 else
500 osize = fs->lfs_bsize;
501 LFS_SEGENTRY(sup, fs, oldsn, bp);
502 sup->su_nbytes -= osize;
503 if (!(bp->b_flags & B_GATHERED))
504 fs->lfs_flags |= LFS_IFDIRTY;
505 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
506 }
507 /*
508 * Now that this block has a new address, and its old
509 * segment no longer owns it, we can forget about its
510 * old size.
511 */
512 if (lbn >= 0 && lbn < NDADDR)
513 ip->i_lfs_fragsize[lbn] = size;
514 }
515
516 /*
517 * Update the metadata that points to the blocks listed in the FINFO
518 * array.
519 */
520 void
521 lfs_updatemeta(struct segment * sp)
522 {
523 struct ubuf *sbp;
524 struct lfs *fs;
525 struct uvnode *vp;
526 daddr_t lbn;
527 int i, nblocks, num;
528 int bb;
529 int bytesleft, size;
530
531 vp = sp->vp;
532 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
533
534 if (vp == NULL || nblocks == 0)
535 return;
536
537 /*
538 * This count may be high due to oversize blocks from lfs_gop_write.
539 * Correct for this. (XXX we should be able to keep track of these.)
540 */
541 fs = sp->fs;
542 for (i = 0; i < nblocks; i++) {
543 if (sp->start_bpp[i] == NULL) {
544 printf("nblocks = %d, not %d\n", i, nblocks);
545 nblocks = i;
546 break;
547 }
548 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
549 nblocks -= num - 1;
550 }
551
552 /*
553 * Sort the blocks.
554 */
555 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
556
557 /*
558 * Record the length of the last block in case it's a fragment.
559 * If there are indirect blocks present, they sort last. An
560 * indirect block will be lfs_bsize and its presence indicates
561 * that you cannot have fragments.
562 */
563 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
564 fs->lfs_bmask) + 1;
565
566 /*
567 * Assign disk addresses, and update references to the logical
568 * block and the segment usage information.
569 */
570 for (i = nblocks; i--; ++sp->start_bpp) {
571 sbp = *sp->start_bpp;
572 lbn = *sp->start_lbp;
573
574 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
575
576 /*
577 * If we write a frag in the wrong place, the cleaner won't
578 * be able to correctly identify its size later, and the
579 * segment will be uncleanable. (Even worse, it will assume
580 * that the indirect block that actually ends the list
581 * is of a smaller size!)
582 */
583 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
584 errx(1, "lfs_updatemeta: fragment is not last block");
585
586 /*
587 * For each subblock in this possibly oversized block,
588 * update its address on disk.
589 */
590 for (bytesleft = sbp->b_bcount; bytesleft > 0;
591 bytesleft -= fs->lfs_bsize) {
592 size = MIN(bytesleft, fs->lfs_bsize);
593 bb = fragstofsb(fs, numfrags(fs, size));
594 lbn = *sp->start_lbp++;
595 lfs_update_single(fs, sp, lbn, fs->lfs_offset, size);
596 fs->lfs_offset += bb;
597 }
598
599 }
600 }
601
602 /*
603 * Start a new segment.
604 */
605 int
606 lfs_initseg(struct lfs * fs)
607 {
608 struct segment *sp;
609 SEGUSE *sup;
610 SEGSUM *ssp;
611 struct ubuf *bp, *sbp;
612 int repeat;
613
614 sp = fs->lfs_sp;
615
616 repeat = 0;
617
618 /* Advance to the next segment. */
619 if (!LFS_PARTIAL_FITS(fs)) {
620 /* lfs_avail eats the remaining space */
621 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
622 fs->lfs_curseg);
623 lfs_newseg(fs);
624 repeat = 1;
625 fs->lfs_offset = fs->lfs_curseg;
626
627 sp->seg_number = dtosn(fs, fs->lfs_curseg);
628 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
629
630 /*
631 * If the segment contains a superblock, update the offset
632 * and summary address to skip over it.
633 */
634 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
635 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
636 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
637 sp->seg_bytes_left -= LFS_SBPAD;
638 }
639 brelse(bp);
640 /* Segment zero could also contain the labelpad */
641 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
642 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
643 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
644 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
645 }
646 } else {
647 sp->seg_number = dtosn(fs, fs->lfs_curseg);
648 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
649 (fs->lfs_offset - fs->lfs_curseg));
650 }
651 fs->lfs_lastpseg = fs->lfs_offset;
652
653 sp->fs = fs;
654 sp->ibp = NULL;
655 sp->idp = NULL;
656 sp->ninodes = 0;
657 sp->ndupino = 0;
658
659 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
660 sp->cbpp = sp->bpp;
661 sbp = *sp->cbpp = getblk(fs->lfs_devvp,
662 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
663 sp->segsum = sbp->b_data;
664 memset(sp->segsum, 0, fs->lfs_sumsize);
665 sp->start_bpp = ++sp->cbpp;
666 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
667
668 /* Set point to SEGSUM, initialize it. */
669 ssp = sp->segsum;
670 ssp->ss_next = fs->lfs_nextseg;
671 ssp->ss_nfinfo = ssp->ss_ninos = 0;
672 ssp->ss_magic = SS_MAGIC;
673
674 /* Set pointer to first FINFO, initialize it. */
675 sp->fip = (struct finfo *) ((caddr_t) sp->segsum + SEGSUM_SIZE(fs));
676 sp->fip->fi_nblocks = 0;
677 sp->start_lbp = &sp->fip->fi_blocks[0];
678 sp->fip->fi_lastlength = 0;
679
680 sp->seg_bytes_left -= fs->lfs_sumsize;
681 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
682
683 LFS_LOCK_BUF(sbp);
684 brelse(sbp);
685 return repeat;
686 }
687
688 /*
689 * Return the next segment to write.
690 */
691 void
692 lfs_newseg(struct lfs * fs)
693 {
694 CLEANERINFO *cip;
695 SEGUSE *sup;
696 struct ubuf *bp;
697 int curseg, isdirty, sn;
698
699 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
700 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
701 sup->su_nbytes = 0;
702 sup->su_nsums = 0;
703 sup->su_ninos = 0;
704 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
705
706 LFS_CLEANERINFO(cip, fs, bp);
707 --cip->clean;
708 ++cip->dirty;
709 fs->lfs_nclean = cip->clean;
710 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
711
712 fs->lfs_lastseg = fs->lfs_curseg;
713 fs->lfs_curseg = fs->lfs_nextseg;
714 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
715 sn = (sn + 1) % fs->lfs_nseg;
716 if (sn == curseg)
717 errx(1, "lfs_nextseg: no clean segments");
718 LFS_SEGENTRY(sup, fs, sn, bp);
719 isdirty = sup->su_flags & SEGUSE_DIRTY;
720 brelse(bp);
721
722 if (!isdirty)
723 break;
724 }
725
726 ++fs->lfs_nactive;
727 fs->lfs_nextseg = sntod(fs, sn);
728 }
729
730
731 int
732 lfs_writeseg(struct lfs * fs, struct segment * sp)
733 {
734 struct ubuf **bpp, *bp;
735 SEGUSE *sup;
736 SEGSUM *ssp;
737 char *datap, *dp;
738 int i;
739 int do_again, nblocks, byteoffset;
740 size_t el_size;
741 u_short ninos;
742 struct uvnode *devvp;
743
744 /*
745 * If there are no buffers other than the segment summary to write
746 * and it is not a checkpoint, don't do anything. On a checkpoint,
747 * even if there aren't any buffers, you need to write the superblock.
748 */
749 if ((nblocks = sp->cbpp - sp->bpp) == 1)
750 return 0;
751
752 devvp = fs->lfs_devvp;
753
754 /* Update the segment usage information. */
755 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
756
757 /* Loop through all blocks, except the segment summary. */
758 for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
759 if ((*bpp)->b_vp != devvp) {
760 sup->su_nbytes += (*bpp)->b_bcount;
761 }
762 }
763
764 ssp = (SEGSUM *) sp->segsum;
765
766 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
767 sup->su_nbytes += ssp->ss_ninos * DINODE1_SIZE;
768
769 if (fs->lfs_version == 1)
770 sup->su_olastmod = write_time;
771 else
772 sup->su_lastmod = write_time;
773 sup->su_ninos += ninos;
774 ++sup->su_nsums;
775 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
776 fs->lfs_ibsize));
777 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
778
779 do_again = !(bp->b_flags & B_GATHERED);
780 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
781
782 /*
783 * Compute checksum across data and then across summary; the first
784 * block (the summary block) is skipped. Set the create time here
785 * so that it's guaranteed to be later than the inode mod times.
786 */
787 if (fs->lfs_version == 1)
788 el_size = sizeof(u_long);
789 else
790 el_size = sizeof(u_int32_t);
791 datap = dp = malloc(nblocks * el_size);
792 if (dp == NULL)
793 err(1, NULL);
794 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
795 ++bpp;
796 /* Loop through gop_write cluster blocks */
797 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
798 byteoffset += fs->lfs_bsize) {
799 memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
800 dp += el_size;
801 }
802 bremfree(*bpp);
803 (*bpp)->b_flags |= B_BUSY;
804 }
805 if (fs->lfs_version == 1)
806 ssp->ss_ocreate = write_time;
807 else {
808 ssp->ss_create = write_time;
809 ssp->ss_serial = ++fs->lfs_serial;
810 ssp->ss_ident = fs->lfs_ident;
811 }
812 /* Set the summary block busy too */
813 bremfree(*(sp->bpp));
814 (*(sp->bpp))->b_flags |= B_BUSY;
815
816 ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
817 ssp->ss_sumsum =
818 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
819 free(datap);
820 datap = dp = NULL;
821 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
822 btofsb(fs, fs->lfs_sumsize));
823
824 if (devvp == NULL)
825 errx(1, "devvp is NULL");
826 for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
827 bp = *bpp;
828 #if 0
829 printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
830 nblocks - i, bp, bp->b_flags, bp->b_blkno);
831 printf(" vp = %p\n", bp->b_vp);
832 if (bp->b_vp != fs->lfs_devvp)
833 printf(" ino = %d lbn = %" PRId64 "\n",
834 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
835 #endif
836 if (bp->b_vp == fs->lfs_devvp)
837 written_dev += bp->b_bcount;
838 else {
839 if (bp->b_lblkno >= 0)
840 written_data += bp->b_bcount;
841 else
842 written_indir += bp->b_bcount;
843 }
844 bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
845 B_LOCKED);
846 bwrite(bp);
847 written_bytes += bp->b_bcount;
848 }
849 written_inodes += ninos;
850
851 return (lfs_initseg(fs) || do_again);
852 }
853
854 /*
855 * Our own copy of shellsort. XXX use qsort or heapsort.
856 */
857 void
858 lfs_shellsort(struct ubuf ** bp_array, ufs_daddr_t * lb_array, int nmemb, int size)
859 {
860 static int __rsshell_increments[] = {4, 1, 0};
861 int incr, *incrp, t1, t2;
862 struct ubuf *bp_temp;
863
864 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
865 for (t1 = incr; t1 < nmemb; ++t1)
866 for (t2 = t1 - incr; t2 >= 0;)
867 if ((u_int32_t) bp_array[t2]->b_lblkno >
868 (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
869 bp_temp = bp_array[t2];
870 bp_array[t2] = bp_array[t2 + incr];
871 bp_array[t2 + incr] = bp_temp;
872 t2 -= incr;
873 } else
874 break;
875
876 /* Reform the list of logical blocks */
877 incr = 0;
878 for (t1 = 0; t1 < nmemb; t1++) {
879 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
880 lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
881 }
882 }
883 }
884
885
886 /*
887 * lfs_seglock --
888 * Single thread the segment writer.
889 */
890 int
891 lfs_seglock(struct lfs * fs, unsigned long flags)
892 {
893 struct segment *sp;
894
895 if (fs->lfs_seglock) {
896 ++fs->lfs_seglock;
897 fs->lfs_sp->seg_flags |= flags;
898 return 0;
899 }
900 fs->lfs_seglock = 1;
901
902 sp = fs->lfs_sp = (struct segment *) malloc(sizeof(*sp));
903 if (sp == NULL)
904 err(1, NULL);
905 sp->bpp = (struct ubuf **) malloc(fs->lfs_ssize * sizeof(struct ubuf *));
906 if (!sp->bpp)
907 errx(!preen, "Could not allocate %zu bytes: %s",
908 (size_t)(fs->lfs_ssize * sizeof(struct ubuf *)),
909 strerror(errno));
910 sp->seg_flags = flags;
911 sp->vp = NULL;
912 sp->seg_iocount = 0;
913 (void) lfs_initseg(fs);
914
915 return 0;
916 }
917
918 /*
919 * lfs_segunlock --
920 * Single thread the segment writer.
921 */
922 void
923 lfs_segunlock(struct lfs * fs)
924 {
925 struct segment *sp;
926 struct ubuf *bp;
927
928 sp = fs->lfs_sp;
929
930 if (fs->lfs_seglock == 1) {
931 if (sp->bpp != sp->cbpp) {
932 /* Free allocated segment summary */
933 fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
934 bp = *sp->bpp;
935 bremfree(bp);
936 bp->b_flags |= B_DONE | B_INVAL;
937 bp->b_flags &= ~B_DELWRI;
938 reassignbuf(bp, bp->b_vp);
939 bp->b_flags |= B_BUSY; /* XXX */
940 brelse(bp);
941 } else
942 printf("unlock to 0 with no summary");
943
944 free(sp->bpp);
945 sp->bpp = NULL;
946 free(sp);
947 fs->lfs_sp = NULL;
948
949 fs->lfs_nactive = 0;
950
951 /* Since we *know* everything's on disk, write both sbs */
952 lfs_writesuper(fs, fs->lfs_sboffs[0]);
953 lfs_writesuper(fs, fs->lfs_sboffs[1]);
954
955 --fs->lfs_seglock;
956 fs->lfs_lockpid = 0;
957 } else if (fs->lfs_seglock == 0) {
958 errx(1, "Seglock not held");
959 } else {
960 --fs->lfs_seglock;
961 }
962 }
963
964 int
965 lfs_writevnodes(struct lfs *fs, struct segment *sp, int op)
966 {
967 struct inode *ip;
968 struct uvnode *vp;
969 int inodes_written = 0;
970
971 LIST_FOREACH(vp, &vnodelist, v_mntvnodes) {
972 if (vp->v_bmap_op != lfs_vop_bmap)
973 continue;
974
975 ip = VTOI(vp);
976
977 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
978 (op != VN_DIROP && (vp->v_flag & VDIROP))) {
979 continue;
980 }
981 /*
982 * Write the inode/file if dirty and it's not the IFILE.
983 */
984 if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) {
985 if (ip->i_number != LFS_IFILE_INUM)
986 lfs_writefile(fs, sp, vp);
987 (void) lfs_writeinode(fs, sp, ip);
988 inodes_written++;
989 }
990 }
991 return inodes_written;
992 }
993
994 void
995 lfs_writesuper(struct lfs *fs, ufs_daddr_t daddr)
996 {
997 struct ubuf *bp;
998
999 /* Set timestamp of this version of the superblock */
1000 if (fs->lfs_version == 1)
1001 fs->lfs_otstamp = write_time;
1002 fs->lfs_tstamp = write_time;
1003
1004 /* Checksum the superblock and copy it into a buffer. */
1005 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1006 assert(daddr > 0);
1007 bp = getblk(fs->lfs_devvp, fsbtodb(fs, daddr), LFS_SBPAD);
1008 memset(bp->b_data + sizeof(struct dlfs), 0,
1009 LFS_SBPAD - sizeof(struct dlfs));
1010 *(struct dlfs *) bp->b_data = fs->lfs_dlfs;
1011
1012 bwrite(bp);
1013 }
1014