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