1 /* $NetBSD: segwrite.c,v 1.18 2008/04/28 20:23:08 martin Exp $ */
3 * Copyright (c) 2003 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Konrad E. Schroder <perseant@hhhh.org>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
31 * Copyright (c) 1991, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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.
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
58 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
62 * Partial segment writer, taken from the kernel and adapted for userland.
64 #include <sys/types.h>
65 #include <sys/param.h>
68 #include <sys/mount.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/ufsmount.h>
73 /* Override certain things to make <ufs/lfs/lfs.h> work */
76 #define panic call_panic
78 #include <ufs/lfs/lfs.h>
93 /* Compatibility definitions */
94 extern off_t locked_queue_bytes
;
95 int locked_queue_count
;
96 off_t written_bytes
= 0;
97 off_t written_data
= 0;
98 off_t written_indir
= 0;
99 off_t written_dev
= 0;
100 int written_inodes
= 0;
102 /* Global variables */
105 extern u_int32_t
cksum(void *, size_t);
106 extern u_int32_t
lfs_sb_cksum(struct dlfs
*);
110 * Logical block number match routines used when traversing the dirty block
114 lfs_match_data(struct lfs
* fs
, struct ubuf
* bp
)
116 return (bp
->b_lblkno
>= 0);
120 lfs_match_indir(struct lfs
* fs
, struct ubuf
* bp
)
125 return (lbn
< 0 && (-lbn
- NDADDR
) % NINDIR(fs
) == 0);
129 lfs_match_dindir(struct lfs
* fs
, struct ubuf
* bp
)
134 return (lbn
< 0 && (-lbn
- NDADDR
) % NINDIR(fs
) == 1);
138 lfs_match_tindir(struct lfs
* fs
, struct ubuf
* bp
)
143 return (lbn
< 0 && (-lbn
- NDADDR
) % NINDIR(fs
) == 2);
150 lfs_segwrite(struct lfs
* fs
, int flags
)
157 lfs_seglock(fs
, flags
| SEGM_CKP
);
160 lfs_writevnodes(fs
, sp
, VN_REG
);
161 lfs_writevnodes(fs
, sp
, VN_DIROP
);
162 ((SEGSUM
*) (sp
->segsum
))->ss_flags
&= ~(SS_CONT
);
166 fs
->lfs_flags
&= ~LFS_IFDIRTY
;
168 if (LIST_FIRST(&vp
->v_dirtyblkhd
) != NULL
|| fs
->lfs_idaddr
<= 0)
169 lfs_writefile(fs
, sp
, vp
);
171 redo
= lfs_writeinode(fs
, sp
, ip
);
172 redo
+= lfs_writeseg(fs
, sp
);
173 redo
+= (fs
->lfs_flags
& LFS_IFDIRTY
);
178 printf("wrote %" PRId64
" bytes (%" PRId32
" fsb)\n",
179 written_bytes
, (ufs_daddr_t
)btofsb(fs
, written_bytes
));
180 printf("wrote %" PRId64
" bytes data (%" PRId32
" fsb)\n",
181 written_data
, (ufs_daddr_t
)btofsb(fs
, written_data
));
182 printf("wrote %" PRId64
" bytes indir (%" PRId32
" fsb)\n",
183 written_indir
, (ufs_daddr_t
)btofsb(fs
, written_indir
));
184 printf("wrote %" PRId64
" bytes dev (%" PRId32
" fsb)\n",
185 written_dev
, (ufs_daddr_t
)btofsb(fs
, written_dev
));
186 printf("wrote %d inodes (%" PRId32
" fsb)\n",
187 written_inodes
, btofsb(fs
, written_inodes
* fs
->lfs_ibsize
));
193 * Write the dirty blocks associated with a vnode.
196 lfs_writefile(struct lfs
* fs
, struct segment
* sp
, struct uvnode
* vp
)
205 if (sp
->seg_bytes_left
< fs
->lfs_bsize
||
206 sp
->sum_bytes_left
< sizeof(struct finfo
))
207 (void) lfs_writeseg(fs
, sp
);
209 sp
->sum_bytes_left
-= FINFOSIZE
;
210 ++((SEGSUM
*) (sp
->segsum
))->ss_nfinfo
;
212 if (vp
->v_uflag
& VU_DIROP
)
213 ((SEGSUM
*) (sp
->segsum
))->ss_flags
|= (SS_DIROP
| SS_CONT
);
217 fip
->fi_ino
= ip
->i_number
;
218 LFS_IENTRY(ifp
, fs
, fip
->fi_ino
, bp
);
219 fip
->fi_version
= ifp
->if_version
;
222 lfs_gather(fs
, sp
, vp
, lfs_match_data
);
223 lfs_gather(fs
, sp
, vp
, lfs_match_indir
);
224 lfs_gather(fs
, sp
, vp
, lfs_match_dindir
);
225 lfs_gather(fs
, sp
, vp
, lfs_match_tindir
);
228 if (fip
->fi_nblocks
!= 0) {
229 sp
->fip
= (FINFO
*) ((caddr_t
) fip
+ FINFOSIZE
+
230 sizeof(ufs_daddr_t
) * (fip
->fi_nblocks
));
231 sp
->start_lbp
= &sp
->fip
->fi_blocks
[0];
233 sp
->sum_bytes_left
+= FINFOSIZE
;
234 --((SEGSUM
*) (sp
->segsum
))->ss_nfinfo
;
239 lfs_writeinode(struct lfs
* fs
, struct segment
* sp
, struct inode
* ip
)
241 struct ubuf
*bp
, *ibp
;
242 struct ufs1_dinode
*cdp
;
247 int error
, i
, ndx
, fsb
= 0;
252 /* Allocate a new inode block if necessary. */
253 if ((ip
->i_number
!= LFS_IFILE_INUM
|| sp
->idp
== NULL
) &&
255 /* Allocate a new segment if necessary. */
256 if (sp
->seg_bytes_left
< fs
->lfs_ibsize
||
257 sp
->sum_bytes_left
< sizeof(ufs_daddr_t
))
258 (void) lfs_writeseg(fs
, sp
);
260 /* Get next inode block. */
261 daddr
= fs
->lfs_offset
;
262 fs
->lfs_offset
+= btofsb(fs
, fs
->lfs_ibsize
);
263 sp
->ibp
= *sp
->cbpp
++ =
264 getblk(fs
->lfs_devvp
, fsbtodb(fs
, daddr
),
266 sp
->ibp
->b_flags
|= B_GATHERED
;
269 /* Zero out inode numbers */
270 for (i
= 0; i
< INOPB(fs
); ++i
)
271 ((struct ufs1_dinode
*) sp
->ibp
->b_data
)[i
].di_inumber
= 0;
274 fs
->lfs_avail
-= btofsb(fs
, fs
->lfs_ibsize
);
275 /* Set remaining space counters. */
276 sp
->seg_bytes_left
-= fs
->lfs_ibsize
;
277 sp
->sum_bytes_left
-= sizeof(ufs_daddr_t
);
278 ndx
= fs
->lfs_sumsize
/ sizeof(ufs_daddr_t
) -
279 sp
->ninodes
/ INOPB(fs
) - 1;
280 ((ufs_daddr_t
*) (sp
->segsum
))[ndx
] = daddr
;
282 /* Update the inode times and copy the inode onto the inode page. */
284 ts
.tv_sec
= write_time
;
285 /* XXX kludge --- don't redirty the ifile just to put times on it */
286 if (ip
->i_number
!= LFS_IFILE_INUM
)
287 LFS_ITIMES(ip
, &ts
, &ts
, &ts
);
290 * If this is the Ifile, and we've already written the Ifile in this
291 * partial segment, just overwrite it (it's not on disk yet) and
294 * XXX we know that the bp that we get the second time around has
295 * already been gathered.
297 if (ip
->i_number
== LFS_IFILE_INUM
&& sp
->idp
) {
298 *(sp
->idp
) = *ip
->i_din
.ffs1_din
;
299 ip
->i_lfs_osize
= ip
->i_ffs1_size
;
303 cdp
= ((struct ufs1_dinode
*) bp
->b_data
) + (sp
->ninodes
% INOPB(fs
));
304 *cdp
= *ip
->i_din
.ffs1_din
;
306 /* If all blocks are goig to disk, update the "size on disk" */
307 ip
->i_lfs_osize
= ip
->i_ffs1_size
;
309 if (ip
->i_number
== LFS_IFILE_INUM
) /* We know sp->idp == NULL */
310 sp
->idp
= ((struct ufs1_dinode
*) bp
->b_data
) +
311 (sp
->ninodes
% INOPB(fs
));
314 assert(!(bp
->b_flags
& B_INVAL
));
317 /* Increment inode count in segment summary block. */
318 ++((SEGSUM
*) (sp
->segsum
))->ss_ninos
;
320 /* If this page is full, set flag to allocate a new page. */
321 if (++sp
->ninodes
% INOPB(fs
) == 0)
325 * If updating the ifile, update the super-block. Update the disk
326 * address and access times for this inode in the ifile.
329 if (ino
== LFS_IFILE_INUM
) {
330 daddr
= fs
->lfs_idaddr
;
331 fs
->lfs_idaddr
= dbtofsb(fs
, bp
->b_blkno
);
334 LFS_IENTRY(ifp
, fs
, ino
, ibp
);
335 daddr
= ifp
->if_daddr
;
336 ifp
->if_daddr
= dbtofsb(fs
, bp
->b_blkno
) + fsb
;
337 error
= LFS_BWRITE_LOG(ibp
); /* Ifile */
341 * Account the inode: it no longer belongs to its former segment,
342 * though it will not belong to the new segment until that segment
343 * is actually written.
345 if (daddr
!= LFS_UNUSED_DADDR
) {
346 u_int32_t oldsn
= dtosn(fs
, daddr
);
347 LFS_SEGENTRY(sup
, fs
, oldsn
, bp
);
348 sup
->su_nbytes
-= DINODE1_SIZE
;
350 (ino
== LFS_IFILE_INUM
&& !(bp
->b_flags
& B_GATHERED
));
352 fs
->lfs_flags
|= LFS_IFDIRTY
;
353 LFS_WRITESEGENTRY(sup
, fs
, oldsn
, bp
); /* Ifile */
359 lfs_gatherblock(struct segment
* sp
, struct ubuf
* bp
)
366 * If full, finish this segment. We may be doing I/O, so
367 * release and reacquire the splbio().
370 blksinblk
= howmany(bp
->b_bcount
, fs
->lfs_bsize
);
371 if (sp
->sum_bytes_left
< sizeof(ufs_daddr_t
) * blksinblk
||
372 sp
->seg_bytes_left
< bp
->b_bcount
) {
375 version
= sp
->fip
->fi_version
;
376 (void) lfs_writeseg(fs
, sp
);
378 sp
->fip
->fi_version
= version
;
379 sp
->fip
->fi_ino
= VTOI(sp
->vp
)->i_number
;
380 /* Add the current file to the segment summary. */
381 ++((SEGSUM
*) (sp
->segsum
))->ss_nfinfo
;
382 sp
->sum_bytes_left
-= FINFOSIZE
;
386 /* Insert into the buffer list, update the FINFO block. */
387 bp
->b_flags
|= B_GATHERED
;
388 /* bp->b_flags &= ~B_DONE; */
391 for (j
= 0; j
< blksinblk
; j
++)
392 sp
->fip
->fi_blocks
[sp
->fip
->fi_nblocks
++] = bp
->b_lblkno
+ j
;
394 sp
->sum_bytes_left
-= sizeof(ufs_daddr_t
) * blksinblk
;
395 sp
->seg_bytes_left
-= bp
->b_bcount
;
400 lfs_gather(struct lfs
* fs
, struct segment
* sp
, struct uvnode
* vp
, int (*match
) (struct lfs
*, struct ubuf
*))
402 struct ubuf
*bp
, *nbp
;
407 for (bp
= LIST_FIRST(&vp
->v_dirtyblkhd
); bp
; bp
= nbp
) {
408 nbp
= LIST_NEXT(bp
, b_vnbufs
);
410 assert(bp
->b_flags
& B_DELWRI
);
411 if ((bp
->b_flags
& (B_BUSY
| B_GATHERED
)) || !match(fs
, bp
)) {
414 if (lfs_gatherblock(sp
, bp
)) {
427 * Change the given block's address to ndaddr, finding its previous
428 * location using ufs_bmaparray().
430 * Account for this change in the segment table.
433 lfs_update_single(struct lfs
* fs
, struct segment
* sp
, daddr_t lbn
,
434 ufs_daddr_t ndaddr
, int size
)
438 struct indir a
[NIADDR
+ 2], *ap
;
448 error
= ufs_bmaparray(fs
, vp
, lbn
, &daddr
, a
, &num
);
450 errx(1, "lfs_updatemeta: ufs_bmaparray returned %d looking up lbn %" PRId64
"\n", error
, lbn
);
452 daddr
= dbtofsb(fs
, daddr
);
454 bb
= fragstofsb(fs
, numfrags(fs
, size
));
457 ooff
= ip
->i_ffs1_db
[lbn
];
458 if (ooff
== UNWRITTEN
)
459 ip
->i_ffs1_blocks
+= bb
;
461 /* possible fragment truncation or extension */
462 obb
= btofsb(fs
, ip
->i_lfs_fragsize
[lbn
]);
463 ip
->i_ffs1_blocks
+= (bb
- obb
);
465 ip
->i_ffs1_db
[lbn
] = ndaddr
;
468 ooff
= ip
->i_ffs1_ib
[a
[0].in_off
];
469 if (ooff
== UNWRITTEN
)
470 ip
->i_ffs1_blocks
+= bb
;
471 ip
->i_ffs1_ib
[a
[0].in_off
] = ndaddr
;
475 if (bread(vp
, ap
->in_lbn
, fs
->lfs_bsize
, NULL
, 0, &bp
))
476 errx(1, "lfs_updatemeta: bread bno %" PRId64
,
479 ooff
= ((ufs_daddr_t
*) bp
->b_data
)[ap
->in_off
];
480 if (ooff
== UNWRITTEN
)
481 ip
->i_ffs1_blocks
+= bb
;
482 ((ufs_daddr_t
*) bp
->b_data
)[ap
->in_off
] = ndaddr
;
483 (void) VOP_BWRITE(bp
);
487 * Update segment usage information, based on old size
491 u_int32_t oldsn
= dtosn(fs
, daddr
);
492 if (lbn
>= 0 && lbn
< NDADDR
)
493 osize
= ip
->i_lfs_fragsize
[lbn
];
495 osize
= fs
->lfs_bsize
;
496 LFS_SEGENTRY(sup
, fs
, oldsn
, bp
);
497 sup
->su_nbytes
-= osize
;
498 if (!(bp
->b_flags
& B_GATHERED
))
499 fs
->lfs_flags
|= LFS_IFDIRTY
;
500 LFS_WRITESEGENTRY(sup
, fs
, oldsn
, bp
);
503 * Now that this block has a new address, and its old
504 * segment no longer owns it, we can forget about its
507 if (lbn
>= 0 && lbn
< NDADDR
)
508 ip
->i_lfs_fragsize
[lbn
] = size
;
512 * Update the metadata that points to the blocks listed in the FINFO
516 lfs_updatemeta(struct segment
* sp
)
527 nblocks
= &sp
->fip
->fi_blocks
[sp
->fip
->fi_nblocks
] - sp
->start_lbp
;
529 if (vp
== NULL
|| nblocks
== 0)
533 * This count may be high due to oversize blocks from lfs_gop_write.
534 * Correct for this. (XXX we should be able to keep track of these.)
537 for (i
= 0; i
< nblocks
; i
++) {
538 if (sp
->start_bpp
[i
] == NULL
) {
539 printf("nblocks = %d, not %d\n", i
, nblocks
);
543 num
= howmany(sp
->start_bpp
[i
]->b_bcount
, fs
->lfs_bsize
);
550 lfs_shellsort(sp
->start_bpp
, sp
->start_lbp
, nblocks
, fs
->lfs_bsize
);
553 * Record the length of the last block in case it's a fragment.
554 * If there are indirect blocks present, they sort last. An
555 * indirect block will be lfs_bsize and its presence indicates
556 * that you cannot have fragments.
558 sp
->fip
->fi_lastlength
= ((sp
->start_bpp
[nblocks
- 1]->b_bcount
- 1) &
562 * Assign disk addresses, and update references to the logical
563 * block and the segment usage information.
565 for (i
= nblocks
; i
--; ++sp
->start_bpp
) {
566 sbp
= *sp
->start_bpp
;
567 lbn
= *sp
->start_lbp
;
569 sbp
->b_blkno
= fsbtodb(fs
, fs
->lfs_offset
);
572 * If we write a frag in the wrong place, the cleaner won't
573 * be able to correctly identify its size later, and the
574 * segment will be uncleanable. (Even worse, it will assume
575 * that the indirect block that actually ends the list
576 * is of a smaller size!)
578 if ((sbp
->b_bcount
& fs
->lfs_bmask
) && i
!= 0)
579 errx(1, "lfs_updatemeta: fragment is not last block");
582 * For each subblock in this possibly oversized block,
583 * update its address on disk.
585 for (bytesleft
= sbp
->b_bcount
; bytesleft
> 0;
586 bytesleft
-= fs
->lfs_bsize
) {
587 size
= MIN(bytesleft
, fs
->lfs_bsize
);
588 bb
= fragstofsb(fs
, numfrags(fs
, size
));
589 lbn
= *sp
->start_lbp
++;
590 lfs_update_single(fs
, sp
, lbn
, fs
->lfs_offset
, size
);
591 fs
->lfs_offset
+= bb
;
598 * Start a new segment.
601 lfs_initseg(struct lfs
* fs
)
606 struct ubuf
*bp
, *sbp
;
613 /* Advance to the next segment. */
614 if (!LFS_PARTIAL_FITS(fs
)) {
615 /* lfs_avail eats the remaining space */
616 fs
->lfs_avail
-= fs
->lfs_fsbpseg
- (fs
->lfs_offset
-
620 fs
->lfs_offset
= fs
->lfs_curseg
;
622 sp
->seg_number
= dtosn(fs
, fs
->lfs_curseg
);
623 sp
->seg_bytes_left
= fsbtob(fs
, fs
->lfs_fsbpseg
);
626 * If the segment contains a superblock, update the offset
627 * and summary address to skip over it.
629 LFS_SEGENTRY(sup
, fs
, sp
->seg_number
, bp
);
630 if (sup
->su_flags
& SEGUSE_SUPERBLOCK
) {
631 fs
->lfs_offset
+= btofsb(fs
, LFS_SBPAD
);
632 sp
->seg_bytes_left
-= LFS_SBPAD
;
635 /* Segment zero could also contain the labelpad */
636 if (fs
->lfs_version
> 1 && sp
->seg_number
== 0 &&
637 fs
->lfs_start
< btofsb(fs
, LFS_LABELPAD
)) {
638 fs
->lfs_offset
+= btofsb(fs
, LFS_LABELPAD
) - fs
->lfs_start
;
639 sp
->seg_bytes_left
-= LFS_LABELPAD
- fsbtob(fs
, fs
->lfs_start
);
642 sp
->seg_number
= dtosn(fs
, fs
->lfs_curseg
);
643 sp
->seg_bytes_left
= fsbtob(fs
, fs
->lfs_fsbpseg
-
644 (fs
->lfs_offset
- fs
->lfs_curseg
));
646 fs
->lfs_lastpseg
= fs
->lfs_offset
;
654 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
656 sbp
= *sp
->cbpp
= getblk(fs
->lfs_devvp
,
657 fsbtodb(fs
, fs
->lfs_offset
), fs
->lfs_sumsize
);
658 sp
->segsum
= sbp
->b_data
;
659 memset(sp
->segsum
, 0, fs
->lfs_sumsize
);
660 sp
->start_bpp
= ++sp
->cbpp
;
661 fs
->lfs_offset
+= btofsb(fs
, fs
->lfs_sumsize
);
663 /* Set point to SEGSUM, initialize it. */
665 ssp
->ss_next
= fs
->lfs_nextseg
;
666 ssp
->ss_nfinfo
= ssp
->ss_ninos
= 0;
667 ssp
->ss_magic
= SS_MAGIC
;
669 /* Set pointer to first FINFO, initialize it. */
670 sp
->fip
= (struct finfo
*) ((caddr_t
) sp
->segsum
+ SEGSUM_SIZE(fs
));
671 sp
->fip
->fi_nblocks
= 0;
672 sp
->start_lbp
= &sp
->fip
->fi_blocks
[0];
673 sp
->fip
->fi_lastlength
= 0;
675 sp
->seg_bytes_left
-= fs
->lfs_sumsize
;
676 sp
->sum_bytes_left
= fs
->lfs_sumsize
- SEGSUM_SIZE(fs
);
684 * Return the next segment to write.
687 lfs_newseg(struct lfs
* fs
)
692 int curseg
, isdirty
, sn
;
694 LFS_SEGENTRY(sup
, fs
, dtosn(fs
, fs
->lfs_nextseg
), bp
);
695 sup
->su_flags
|= SEGUSE_DIRTY
| SEGUSE_ACTIVE
;
699 LFS_WRITESEGENTRY(sup
, fs
, dtosn(fs
, fs
->lfs_nextseg
), bp
);
701 LFS_CLEANERINFO(cip
, fs
, bp
);
704 fs
->lfs_nclean
= cip
->clean
;
705 LFS_SYNC_CLEANERINFO(cip
, fs
, bp
, 1);
707 fs
->lfs_lastseg
= fs
->lfs_curseg
;
708 fs
->lfs_curseg
= fs
->lfs_nextseg
;
709 for (sn
= curseg
= dtosn(fs
, fs
->lfs_curseg
) + fs
->lfs_interleave
;;) {
710 sn
= (sn
+ 1) % fs
->lfs_nseg
;
712 errx(1, "lfs_nextseg: no clean segments");
713 LFS_SEGENTRY(sup
, fs
, sn
, bp
);
714 isdirty
= sup
->su_flags
& SEGUSE_DIRTY
;
722 fs
->lfs_nextseg
= sntod(fs
, sn
);
727 lfs_writeseg(struct lfs
* fs
, struct segment
* sp
)
729 struct ubuf
**bpp
, *bp
;
734 int do_again
, nblocks
, byteoffset
;
737 struct uvnode
*devvp
;
740 * If there are no buffers other than the segment summary to write
741 * and it is not a checkpoint, don't do anything. On a checkpoint,
742 * even if there aren't any buffers, you need to write the superblock.
744 nblocks
= sp
->cbpp
- sp
->bpp
;
746 printf("write %d blocks at 0x%x\n",
747 nblocks
, (int)dbtofsb(fs
, (*sp
->bpp
)->b_blkno
));
752 devvp
= fs
->lfs_devvp
;
754 /* Update the segment usage information. */
755 LFS_SEGENTRY(sup
, fs
, sp
->seg_number
, bp
);
756 sup
->su_flags
|= SEGUSE_DIRTY
| SEGUSE_ACTIVE
;
758 /* Loop through all blocks, except the segment summary. */
759 for (bpp
= sp
->bpp
; ++bpp
< sp
->cbpp
;) {
760 if ((*bpp
)->b_vp
!= devvp
) {
761 sup
->su_nbytes
+= (*bpp
)->b_bcount
;
763 assert(dtosn(fs
, dbtofsb(fs
, (*bpp
)->b_blkno
)) == sp
->seg_number
);
766 ssp
= (SEGSUM
*) sp
->segsum
;
767 ssp
->ss_flags
|= SS_RFW
;
769 ninos
= (ssp
->ss_ninos
+ INOPB(fs
) - 1) / INOPB(fs
);
770 sup
->su_nbytes
+= ssp
->ss_ninos
* DINODE1_SIZE
;
772 if (fs
->lfs_version
== 1)
773 sup
->su_olastmod
= write_time
;
775 sup
->su_lastmod
= write_time
;
776 sup
->su_ninos
+= ninos
;
778 fs
->lfs_dmeta
+= (btofsb(fs
, fs
->lfs_sumsize
) + btofsb(fs
, ninos
*
780 fs
->lfs_avail
-= btofsb(fs
, fs
->lfs_sumsize
);
782 do_again
= !(bp
->b_flags
& B_GATHERED
);
783 LFS_WRITESEGENTRY(sup
, fs
, sp
->seg_number
, bp
); /* Ifile */
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.
790 if (fs
->lfs_version
== 1)
791 el_size
= sizeof(u_long
);
793 el_size
= sizeof(u_int32_t
);
794 datap
= dp
= emalloc(nblocks
* el_size
);
795 for (bpp
= sp
->bpp
, i
= nblocks
- 1; i
--;) {
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
);
804 (*bpp
)->b_flags
|= B_BUSY
;
806 if (fs
->lfs_version
== 1)
807 ssp
->ss_ocreate
= write_time
;
809 ssp
->ss_create
= write_time
;
810 ssp
->ss_serial
= ++fs
->lfs_serial
;
811 ssp
->ss_ident
= fs
->lfs_ident
;
813 /* Set the summary block busy too */
814 bremfree(*(sp
->bpp
));
815 (*(sp
->bpp
))->b_flags
|= B_BUSY
;
817 ssp
->ss_datasum
= cksum(datap
, (nblocks
- 1) * el_size
);
819 cksum(&ssp
->ss_datasum
, fs
->lfs_sumsize
- sizeof(ssp
->ss_sumsum
));
822 fs
->lfs_bfree
-= (btofsb(fs
, ninos
* fs
->lfs_ibsize
) +
823 btofsb(fs
, fs
->lfs_sumsize
));
826 errx(1, "devvp is NULL");
827 for (bpp
= sp
->bpp
, i
= nblocks
; i
; bpp
++, i
--) {
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
);
837 if (bp
->b_vp
== fs
->lfs_devvp
)
838 written_dev
+= bp
->b_bcount
;
840 if (bp
->b_lblkno
>= 0)
841 written_data
+= bp
->b_bcount
;
843 written_indir
+= bp
->b_bcount
;
845 bp
->b_flags
&= ~(B_DELWRI
| B_READ
| B_GATHERED
| B_ERROR
|
848 written_bytes
+= bp
->b_bcount
;
850 written_inodes
+= ninos
;
852 return (lfs_initseg(fs
) || do_again
);
856 * Our own copy of shellsort. XXX use qsort or heapsort.
859 lfs_shellsort(struct ubuf
** bp_array
, ufs_daddr_t
* lb_array
, int nmemb
, int size
)
861 static int __rsshell_increments
[] = {4, 1, 0};
862 int incr
, *incrp
, t1
, t2
;
863 struct ubuf
*bp_temp
;
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
;
877 /* Reform the list of logical blocks */
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
;
889 * Single thread the segment writer.
892 lfs_seglock(struct lfs
* fs
, unsigned long flags
)
896 if (fs
->lfs_seglock
) {
898 fs
->lfs_sp
->seg_flags
|= flags
;
903 sp
= fs
->lfs_sp
= emalloc(sizeof(*sp
));
904 sp
->bpp
= emalloc(fs
->lfs_ssize
* sizeof(struct ubuf
*));
906 errx(!preen
, "Could not allocate %zu bytes: %s",
907 (size_t)(fs
->lfs_ssize
* sizeof(struct ubuf
*)),
909 sp
->seg_flags
= flags
;
912 (void) lfs_initseg(fs
);
919 * Single thread the segment writer.
922 lfs_segunlock(struct lfs
* fs
)
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
);
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 */
941 printf("unlock to 0 with no summary");
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]);
956 } else if (fs
->lfs_seglock
== 0) {
957 errx(1, "Seglock not held");
964 lfs_writevnodes(struct lfs
*fs
, struct segment
*sp
, int op
)
968 int inodes_written
= 0;
970 LIST_FOREACH(vp
, &vnodelist
, v_mntvnodes
) {
971 if (vp
->v_bmap_op
!= lfs_vop_bmap
)
976 if ((op
== VN_DIROP
&& !(vp
->v_uflag
& VU_DIROP
)) ||
977 (op
!= VN_DIROP
&& (vp
->v_uflag
& VU_DIROP
))) {
981 * Write the inode/file if dirty and it's not the IFILE.
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
);
990 return inodes_written
;
994 lfs_writesuper(struct lfs
*fs
, ufs_daddr_t daddr
)
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
;
1003 /* Checksum the superblock and copy it into a buffer. */
1004 fs
->lfs_cksum
= lfs_sb_cksum(&(fs
->lfs_dlfs
));
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
;