segwrite.c revision 1.41 1 /* $NetBSD: segwrite.c,v 1.41 2015/08/12 18:28:00 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 VU_DIROP 0x01000000 /* XXX XXX from sys/vnode.h */
72 #define vnode uvnode
73 #define buf ubuf
74 #define panic call_panic
75
76 #include <ufs/lfs/lfs.h>
77 #include <ufs/lfs/lfs_accessors.h>
78 #include <ufs/lfs/lfs_inode.h>
79
80 #include <assert.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <err.h>
85 #include <errno.h>
86 #include <util.h>
87
88 #include "bufcache.h"
89 #include "vnode.h"
90 #include "lfs_user.h"
91 #include "segwrite.h"
92
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;
101
102 /* Global variables */
103 time_t write_time;
104
105 extern u_int32_t cksum(void *, size_t);
106 extern u_int32_t lfs_sb_cksum(struct lfs *);
107 extern int preen;
108
109 static void lfs_shellsort(struct lfs *,
110 struct ubuf **, union lfs_blocks *, int, int);
111
112 /*
113 * Logical block number match routines used when traversing the dirty block
114 * chain.
115 */
116 int
117 lfs_match_data(struct lfs * fs, struct ubuf * bp)
118 {
119 return (bp->b_lblkno >= 0);
120 }
121
122 int
123 lfs_match_indir(struct lfs * fs, struct ubuf * bp)
124 {
125 daddr_t lbn;
126
127 lbn = bp->b_lblkno;
128 return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 0);
129 }
130
131 int
132 lfs_match_dindir(struct lfs * fs, struct ubuf * bp)
133 {
134 daddr_t lbn;
135
136 lbn = bp->b_lblkno;
137 return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 1);
138 }
139
140 int
141 lfs_match_tindir(struct lfs * fs, struct ubuf * bp)
142 {
143 daddr_t lbn;
144
145 lbn = bp->b_lblkno;
146 return (lbn < 0 && (-lbn - ULFS_NDADDR) % LFS_NINDIR(fs) == 2);
147 }
148
149 /*
150 * Do a checkpoint.
151 */
152 int
153 lfs_segwrite(struct lfs * fs, int flags)
154 {
155 struct inode *ip;
156 struct segment *sp;
157 struct uvnode *vp;
158 SEGSUM *ssp;
159 int redo;
160
161 lfs_seglock(fs, flags | SEGM_CKP);
162 sp = fs->lfs_sp;
163
164 lfs_writevnodes(fs, sp, VN_REG);
165 lfs_writevnodes(fs, sp, VN_DIROP);
166 ssp = (SEGSUM *)sp->segsum;
167 lfs_ss_setflags(fs, ssp, lfs_ss_getflags(fs, ssp) & ~(SS_CONT));
168
169 do {
170 vp = fs->lfs_ivnode;
171 fs->lfs_flags &= ~LFS_IFDIRTY;
172 ip = VTOI(vp);
173 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL || lfs_sb_getidaddr(fs) <= 0)
174 lfs_writefile(fs, sp, vp);
175
176 redo = lfs_writeinode(fs, sp, ip);
177 redo += lfs_writeseg(fs, sp);
178 redo += (fs->lfs_flags & LFS_IFDIRTY);
179 } while (redo);
180
181 lfs_segunlock(fs);
182 #if 0
183 printf("wrote %" PRId64 " bytes (%" PRId32 " fsb)\n",
184 written_bytes, (ulfs_daddr_t)lfs_btofsb(fs, written_bytes));
185 printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n",
186 written_data, (ulfs_daddr_t)lfs_btofsb(fs, written_data));
187 printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n",
188 written_indir, (ulfs_daddr_t)lfs_btofsb(fs, written_indir));
189 printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n",
190 written_dev, (ulfs_daddr_t)lfs_btofsb(fs, written_dev));
191 printf("wrote %d inodes (%" PRId32 " fsb)\n",
192 written_inodes, lfs_btofsb(fs, written_inodes * fs->lfs_ibsize));
193 #endif
194 return 0;
195 }
196
197 /*
198 * Write the dirty blocks associated with a vnode.
199 */
200 void
201 lfs_writefile(struct lfs * fs, struct segment * sp, struct uvnode * vp)
202 {
203 struct ubuf *bp;
204 FINFO *fip;
205 struct inode *ip;
206 IFILE *ifp;
207 SEGSUM *ssp;
208
209 ip = VTOI(vp);
210
211 if (sp->seg_bytes_left < lfs_sb_getbsize(fs) ||
212 sp->sum_bytes_left < FINFOSIZE(fs) + LFS_BLKPTRSIZE(fs))
213 (void) lfs_writeseg(fs, sp);
214
215 sp->sum_bytes_left -= FINFOSIZE(fs);
216 ssp = (SEGSUM *)sp->segsum;
217 lfs_ss_setnfinfo(fs, ssp, lfs_ss_getnfinfo(fs, ssp) + 1);
218
219 if (vp->v_uflag & VU_DIROP) {
220 lfs_ss_setflags(fs, ssp,
221 lfs_ss_getflags(fs, ssp) | (SS_DIROP | SS_CONT));
222 }
223
224 fip = sp->fip;
225 lfs_fi_setnblocks(fs, fip, 0);
226 lfs_fi_setino(fs, fip, ip->i_number);
227 LFS_IENTRY(ifp, fs, lfs_fi_getino(fs, fip), bp);
228 lfs_fi_setversion(fs, fip, lfs_if_getversion(fs, ifp));
229 brelse(bp, 0);
230
231 lfs_gather(fs, sp, vp, lfs_match_data);
232 lfs_gather(fs, sp, vp, lfs_match_indir);
233 lfs_gather(fs, sp, vp, lfs_match_dindir);
234 lfs_gather(fs, sp, vp, lfs_match_tindir);
235
236 fip = sp->fip;
237 if (lfs_fi_getnblocks(fs, fip) != 0) {
238 sp->fip = NEXT_FINFO(fs, fip);
239 lfs_blocks_fromfinfo(fs, &sp->start_lbp, sp->fip);
240 } else {
241 /* XXX shouldn't this update sp->fip? */
242 sp->sum_bytes_left += FINFOSIZE(fs);
243 lfs_ss_setnfinfo(fs, ssp, lfs_ss_getnfinfo(fs, ssp) - 1);
244 }
245 }
246
247 int
248 lfs_writeinode(struct lfs * fs, struct segment * sp, struct inode * ip)
249 {
250 struct ubuf *bp, *ibp;
251 union lfs_dinode *cdp;
252 IFILE *ifp;
253 SEGUSE *sup;
254 SEGSUM *ssp;
255 daddr_t daddr;
256 ino_t ino;
257 int i, ndx, fsb = 0;
258 int redo_ifile = 0;
259 struct timespec ts;
260 int gotblk = 0;
261
262 /* Allocate a new inode block if necessary. */
263 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
264 sp->ibp == NULL) {
265 /* Allocate a new segment if necessary. */
266 if (sp->seg_bytes_left < lfs_sb_getibsize(fs) ||
267 sp->sum_bytes_left < sizeof(ulfs_daddr_t))
268 (void) lfs_writeseg(fs, sp);
269
270 /* Get next inode block. */
271 daddr = lfs_sb_getoffset(fs);
272 lfs_sb_addoffset(fs, lfs_btofsb(fs, lfs_sb_getibsize(fs)));
273 sp->ibp = *sp->cbpp++ =
274 getblk(fs->lfs_devvp, LFS_FSBTODB(fs, daddr),
275 lfs_sb_getibsize(fs));
276 sp->ibp->b_flags |= B_GATHERED;
277 gotblk++;
278
279 /* Zero out inode numbers */
280 for (i = 0; i < LFS_INOPB(fs); ++i) {
281 union lfs_dinode *tmpdip;
282
283 tmpdip = DINO_IN_BLOCK(fs, sp->ibp->b_data, i);
284 lfs_dino_setinumber(fs, tmpdip, 0);
285 }
286
287 ++sp->start_bpp;
288 lfs_sb_subavail(fs, lfs_btofsb(fs, lfs_sb_getibsize(fs)));
289 /* Set remaining space counters. */
290 sp->seg_bytes_left -= lfs_sb_getibsize(fs);
291 sp->sum_bytes_left -= sizeof(ulfs_daddr_t);
292 ndx = lfs_sb_getsumsize(fs) / sizeof(ulfs_daddr_t) -
293 sp->ninodes / LFS_INOPB(fs) - 1;
294 ((ulfs_daddr_t *) (sp->segsum))[ndx] = daddr;
295 }
296 /* Update the inode times and copy the inode onto the inode page. */
297 ts.tv_nsec = 0;
298 ts.tv_sec = write_time;
299 /* XXX kludge --- don't redirty the ifile just to put times on it */
300 if (ip->i_number != LFS_IFILE_INUM)
301 LFS_ITIMES(ip, &ts, &ts, &ts);
302
303 /*
304 * If this is the Ifile, and we've already written the Ifile in this
305 * partial segment, just overwrite it (it's not on disk yet) and
306 * continue.
307 *
308 * XXX we know that the bp that we get the second time around has
309 * already been gathered.
310 */
311 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
312 // XXX weak
313 if (fs->lfs_is64) {
314 sp->idp->u_64 = *ip->i_din.ffs2_din;
315 } else {
316 sp->idp->u_32 = *ip->i_din.ffs1_din;
317 }
318 ip->i_lfs_osize = ip->i_ffs1_size;
319 return 0;
320 }
321 bp = sp->ibp;
322 cdp = DINO_IN_BLOCK(fs, bp->b_data, sp->ninodes % LFS_INOPB(fs));
323 // XXX weak
324 if (fs->lfs_is64) {
325 cdp->u_64 = *ip->i_din.ffs2_din;
326 } else {
327 cdp->u_32 = *ip->i_din.ffs1_din;
328 }
329
330 /* If all blocks are goig to disk, update the "size on disk" */
331 ip->i_lfs_osize = ip->i_ffs1_size;
332
333 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
334 sp->idp = DINO_IN_BLOCK(fs, bp->b_data, sp->ninodes % LFS_INOPB(fs));
335 if (gotblk) {
336 LFS_LOCK_BUF(bp);
337 assert(!(bp->b_flags & B_INVAL));
338 brelse(bp, 0);
339 }
340 /* Increment inode count in segment summary block. */
341 ssp = (SEGSUM *)sp->segsum;
342 lfs_ss_setninos(fs, ssp, lfs_ss_getninos(fs, ssp) + 1);
343
344 /* If this page is full, set flag to allocate a new page. */
345 if (++sp->ninodes % LFS_INOPB(fs) == 0)
346 sp->ibp = NULL;
347
348 /*
349 * If updating the ifile, update the super-block. Update the disk
350 * address for this inode in the ifile.
351 */
352 ino = ip->i_number;
353 if (ino == LFS_IFILE_INUM) {
354 daddr = lfs_sb_getidaddr(fs);
355 lfs_sb_setidaddr(fs, LFS_DBTOFSB(fs, bp->b_blkno));
356 sbdirty();
357 } else {
358 LFS_IENTRY(ifp, fs, ino, ibp);
359 daddr = lfs_if_getdaddr(fs, ifp);
360 lfs_if_setdaddr(fs, ifp, LFS_DBTOFSB(fs, bp->b_blkno) + fsb);
361 (void)LFS_BWRITE_LOG(ibp); /* Ifile */
362 }
363
364 /*
365 * Account the inode: it no longer belongs to its former segment,
366 * though it will not belong to the new segment until that segment
367 * is actually written.
368 */
369 if (daddr != LFS_UNUSED_DADDR) {
370 u_int32_t oldsn = lfs_dtosn(fs, daddr);
371 LFS_SEGENTRY(sup, fs, oldsn, bp);
372 sup->su_nbytes -= DINOSIZE(fs);
373 redo_ifile =
374 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
375 if (redo_ifile)
376 fs->lfs_flags |= LFS_IFDIRTY;
377 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
378 }
379 return redo_ifile;
380 }
381
382 int
383 lfs_gatherblock(struct segment * sp, struct ubuf * bp)
384 {
385 struct lfs *fs;
386 SEGSUM *ssp;
387 int version;
388 int j, blksinblk;
389
390 /*
391 * If full, finish this segment. We may be doing I/O, so
392 * release and reacquire the splbio().
393 */
394 fs = sp->fs;
395 blksinblk = howmany(bp->b_bcount, lfs_sb_getbsize(fs));
396 if (sp->sum_bytes_left < sizeof(ulfs_daddr_t) * blksinblk ||
397 sp->seg_bytes_left < bp->b_bcount) {
398 lfs_updatemeta(sp);
399
400 version = lfs_fi_getversion(fs, sp->fip);
401 (void) lfs_writeseg(fs, sp);
402
403 lfs_fi_setversion(fs, sp->fip, version);
404 lfs_fi_setino(fs, sp->fip, VTOI(sp->vp)->i_number);
405 /* Add the current file to the segment summary. */
406 ssp = (SEGSUM *)sp->segsum;
407 lfs_ss_setnfinfo(fs, ssp, lfs_ss_getnfinfo(fs, ssp) + 1);
408 sp->sum_bytes_left -= FINFOSIZE(fs);
409
410 return 1;
411 }
412 /* Insert into the buffer list, update the FINFO block. */
413 bp->b_flags |= B_GATHERED;
414 /* bp->b_flags &= ~B_DONE; */
415
416 *sp->cbpp++ = bp;
417 for (j = 0; j < blksinblk; j++) {
418 unsigned bn;
419
420 bn = lfs_fi_getnblocks(fs, sp->fip);
421 lfs_fi_setnblocks(fs, sp->fip, bn + 1);
422 lfs_fi_setblock(fs, sp->fip, bn, bp->b_lblkno + j);;
423 }
424
425 sp->sum_bytes_left -= sizeof(ulfs_daddr_t) * blksinblk;
426 sp->seg_bytes_left -= bp->b_bcount;
427 return 0;
428 }
429
430 int
431 lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *))
432 {
433 struct ubuf *bp, *nbp;
434 int count = 0;
435
436 sp->vp = vp;
437 loop:
438 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
439 nbp = LIST_NEXT(bp, b_vnbufs);
440
441 assert(bp->b_flags & B_DELWRI);
442 if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) {
443 continue;
444 }
445 if (lfs_gatherblock(sp, bp)) {
446 goto loop;
447 }
448 count++;
449 }
450
451 lfs_updatemeta(sp);
452 sp->vp = NULL;
453 return count;
454 }
455
456
457 /*
458 * Change the given block's address to ndaddr, finding its previous
459 * location using ulfs_bmaparray().
460 *
461 * Account for this change in the segment table.
462 */
463 void
464 lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn,
465 ulfs_daddr_t ndaddr, int size)
466 {
467 SEGUSE *sup;
468 struct ubuf *bp;
469 struct indir a[ULFS_NIADDR + 2], *ap;
470 struct inode *ip;
471 struct uvnode *vp;
472 daddr_t daddr, ooff;
473 int num, error;
474 int osize;
475 int frags, ofrags;
476
477 vp = sp->vp;
478 ip = VTOI(vp);
479
480 error = ulfs_bmaparray(fs, vp, lbn, &daddr, a, &num);
481 if (error)
482 errx(EXIT_FAILURE, "%s: ulfs_bmaparray returned %d looking up lbn %"
483 PRId64 "", __func__, error, lbn);
484 if (daddr > 0)
485 daddr = LFS_DBTOFSB(fs, daddr);
486
487 frags = lfs_numfrags(fs, size);
488 switch (num) {
489 case 0:
490 ooff = ip->i_ffs1_db[lbn];
491 if (ooff == UNWRITTEN)
492 ip->i_ffs1_blocks += frags;
493 else {
494 /* possible fragment truncation or extension */
495 ofrags = lfs_btofsb(fs, ip->i_lfs_fragsize[lbn]);
496 ip->i_ffs1_blocks += (frags - ofrags);
497 }
498 ip->i_ffs1_db[lbn] = ndaddr;
499 break;
500 case 1:
501 ooff = ip->i_ffs1_ib[a[0].in_off];
502 if (ooff == UNWRITTEN)
503 ip->i_ffs1_blocks += frags;
504 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
505 break;
506 default:
507 ap = &a[num - 1];
508 if (bread(vp, ap->in_lbn, lfs_sb_getbsize(fs), 0, &bp))
509 errx(EXIT_FAILURE, "%s: bread bno %" PRId64, __func__,
510 ap->in_lbn);
511
512 ooff = ((ulfs_daddr_t *) bp->b_data)[ap->in_off];
513 if (ooff == UNWRITTEN)
514 ip->i_ffs1_blocks += frags;
515 ((ulfs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
516 (void) VOP_BWRITE(bp);
517 }
518
519 /*
520 * Update segment usage information, based on old size
521 * and location.
522 */
523 if (daddr > 0) {
524 u_int32_t oldsn = lfs_dtosn(fs, daddr);
525 if (lbn >= 0 && lbn < ULFS_NDADDR)
526 osize = ip->i_lfs_fragsize[lbn];
527 else
528 osize = lfs_sb_getbsize(fs);
529 LFS_SEGENTRY(sup, fs, oldsn, bp);
530 sup->su_nbytes -= osize;
531 if (!(bp->b_flags & B_GATHERED))
532 fs->lfs_flags |= LFS_IFDIRTY;
533 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
534 }
535 /*
536 * Now that this block has a new address, and its old
537 * segment no longer owns it, we can forget about its
538 * old size.
539 */
540 if (lbn >= 0 && lbn < ULFS_NDADDR)
541 ip->i_lfs_fragsize[lbn] = size;
542 }
543
544 /*
545 * Update the metadata that points to the blocks listed in the FINFO
546 * array.
547 */
548 void
549 lfs_updatemeta(struct segment * sp)
550 {
551 struct ubuf *sbp;
552 struct lfs *fs;
553 struct uvnode *vp;
554 daddr_t lbn;
555 int i, nblocks, num;
556 int frags;
557 int bytesleft, size;
558 union lfs_blocks tmpptr;
559
560 fs = sp->fs;
561 vp = sp->vp;
562
563 /*
564 * This code was cutpasted from the kernel. See the
565 * corresponding comment in lfs_segment.c.
566 */
567 #if 0
568 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
569 #else
570 lfs_blocks_fromvoid(fs, &tmpptr, (void *)NEXT_FINFO(fs, sp->fip));
571 nblocks = lfs_blocks_sub(fs, &tmpptr, &sp->start_lbp);
572 //nblocks_orig = nblocks;
573 #endif
574
575 if (vp == NULL || nblocks == 0)
576 return;
577
578 /*
579 * This count may be high due to oversize blocks from lfs_gop_write.
580 * Correct for this. (XXX we should be able to keep track of these.)
581 */
582 for (i = 0; i < nblocks; i++) {
583 if (sp->start_bpp[i] == NULL) {
584 printf("nblocks = %d, not %d\n", i, nblocks);
585 nblocks = i;
586 break;
587 }
588 num = howmany(sp->start_bpp[i]->b_bcount, lfs_sb_getbsize(fs));
589 nblocks -= num - 1;
590 }
591
592 /*
593 * Sort the blocks.
594 */
595 lfs_shellsort(fs, sp->start_bpp, &sp->start_lbp, nblocks, lfs_sb_getbsize(fs));
596
597 /*
598 * Record the length of the last block in case it's a fragment.
599 * If there are indirect blocks present, they sort last. An
600 * indirect block will be lfs_bsize and its presence indicates
601 * that you cannot have fragments.
602 */
603 lfs_fi_setlastlength(fs, sp->fip, ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
604 lfs_sb_getbmask(fs)) + 1);
605
606 /*
607 * Assign disk addresses, and update references to the logical
608 * block and the segment usage information.
609 */
610 for (i = nblocks; i--; ++sp->start_bpp) {
611 sbp = *sp->start_bpp;
612 lbn = lfs_blocks_get(fs, &sp->start_lbp, 0);
613
614 sbp->b_blkno = LFS_FSBTODB(fs, lfs_sb_getoffset(fs));
615
616 /*
617 * If we write a frag in the wrong place, the cleaner won't
618 * be able to correctly identify its size later, and the
619 * segment will be uncleanable. (Even worse, it will assume
620 * that the indirect block that actually ends the list
621 * is of a smaller size!)
622 */
623 if ((sbp->b_bcount & lfs_sb_getbmask(fs)) && i != 0)
624 errx(EXIT_FAILURE, "%s: fragment is not last block", __func__);
625
626 /*
627 * For each subblock in this possibly oversized block,
628 * update its address on disk.
629 */
630 for (bytesleft = sbp->b_bcount; bytesleft > 0;
631 bytesleft -= lfs_sb_getbsize(fs)) {
632 size = MIN(bytesleft, lfs_sb_getbsize(fs));
633 frags = lfs_numfrags(fs, size);
634 lbn = lfs_blocks_get(fs, &sp->start_lbp, 0);
635 lfs_blocks_inc(fs, &sp->start_lbp);
636 lfs_update_single(fs, sp, lbn, lfs_sb_getoffset(fs), size);
637 lfs_sb_addoffset(fs, frags);
638 }
639
640 }
641 }
642
643 /*
644 * Start a new segment.
645 */
646 int
647 lfs_initseg(struct lfs * fs)
648 {
649 struct segment *sp;
650 SEGUSE *sup;
651 SEGSUM *ssp;
652 struct ubuf *bp, *sbp;
653 int repeat;
654
655 sp = fs->lfs_sp;
656
657 repeat = 0;
658
659 /* Advance to the next segment. */
660 if (!LFS_PARTIAL_FITS(fs)) {
661 /* lfs_avail eats the remaining space */
662 lfs_sb_subavail(fs, lfs_sb_getfsbpseg(fs) - (lfs_sb_getoffset(fs) -
663 lfs_sb_getcurseg(fs)));
664 lfs_newseg(fs);
665 repeat = 1;
666 lfs_sb_setoffset(fs, lfs_sb_getcurseg(fs));
667
668 sp->seg_number = lfs_dtosn(fs, lfs_sb_getcurseg(fs));
669 sp->seg_bytes_left = lfs_fsbtob(fs, lfs_sb_getfsbpseg(fs));
670
671 /*
672 * If the segment contains a superblock, update the offset
673 * and summary address to skip over it.
674 */
675 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
676 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
677 lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
678 sp->seg_bytes_left -= LFS_SBPAD;
679 }
680 brelse(bp, 0);
681 /* Segment zero could also contain the labelpad */
682 if (lfs_sb_getversion(fs) > 1 && sp->seg_number == 0 &&
683 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD)) {
684 lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_LABELPAD) - lfs_sb_gets0addr(fs));
685 sp->seg_bytes_left -= LFS_LABELPAD - lfs_fsbtob(fs, lfs_sb_gets0addr(fs));
686 }
687 } else {
688 sp->seg_number = lfs_dtosn(fs, lfs_sb_getcurseg(fs));
689 sp->seg_bytes_left = lfs_fsbtob(fs, lfs_sb_getfsbpseg(fs) -
690 (lfs_sb_getoffset(fs) - lfs_sb_getcurseg(fs)));
691 }
692 lfs_sb_setlastpseg(fs, lfs_sb_getoffset(fs));
693
694 sp->fs = fs;
695 sp->ibp = NULL;
696 sp->idp = NULL;
697 sp->ninodes = 0;
698 sp->ndupino = 0;
699
700 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
701 sp->cbpp = sp->bpp;
702 sbp = *sp->cbpp = getblk(fs->lfs_devvp,
703 LFS_FSBTODB(fs, lfs_sb_getoffset(fs)), lfs_sb_getsumsize(fs));
704 sp->segsum = sbp->b_data;
705 memset(sp->segsum, 0, lfs_sb_getsumsize(fs));
706 sp->start_bpp = ++sp->cbpp;
707 lfs_sb_addoffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
708
709 /* Set point to SEGSUM, initialize it. */
710 ssp = sp->segsum;
711 lfs_ss_setnext(fs, ssp, lfs_sb_getnextseg(fs));
712 lfs_ss_setnfinfo(fs, ssp, 0);
713 lfs_ss_setninos(fs, ssp, 0);
714 lfs_ss_setmagic(fs, ssp, SS_MAGIC);
715
716 /* Set pointer to first FINFO, initialize it. */
717 sp->fip = SEGSUM_FINFOBASE(fs, ssp);
718 lfs_fi_setnblocks(fs, sp->fip, 0);
719 lfs_blocks_fromfinfo(fs, &sp->start_lbp, sp->fip);
720 lfs_fi_setlastlength(fs, sp->fip, 0);
721
722 sp->seg_bytes_left -= lfs_sb_getsumsize(fs);
723 sp->sum_bytes_left = lfs_sb_getsumsize(fs) - SEGSUM_SIZE(fs);
724
725 LFS_LOCK_BUF(sbp);
726 brelse(sbp, 0);
727 return repeat;
728 }
729
730 /*
731 * Return the next segment to write.
732 */
733 void
734 lfs_newseg(struct lfs * fs)
735 {
736 CLEANERINFO *cip;
737 SEGUSE *sup;
738 struct ubuf *bp;
739 int curseg, isdirty, sn;
740
741 LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getnextseg(fs)), bp);
742 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
743 sup->su_nbytes = 0;
744 sup->su_nsums = 0;
745 sup->su_ninos = 0;
746 LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, lfs_sb_getnextseg(fs)), bp);
747
748 LFS_CLEANERINFO(cip, fs, bp);
749 lfs_ci_shiftcleantodirty(fs, cip, 1);
750 lfs_sb_setnclean(fs, lfs_ci_getclean(fs, cip));
751 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
752
753 lfs_sb_setlastseg(fs, lfs_sb_getcurseg(fs));
754 lfs_sb_setcurseg(fs, lfs_sb_getnextseg(fs));
755 for (sn = curseg = lfs_dtosn(fs, lfs_sb_getcurseg(fs)) + lfs_sb_getinterleave(fs);;) {
756 sn = (sn + 1) % lfs_sb_getnseg(fs);
757 if (sn == curseg)
758 errx(EXIT_FAILURE, "%s: no clean segments", __func__);
759 LFS_SEGENTRY(sup, fs, sn, bp);
760 isdirty = sup->su_flags & SEGUSE_DIRTY;
761 brelse(bp, 0);
762
763 if (!isdirty)
764 break;
765 }
766
767 ++fs->lfs_nactive;
768 lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
769 }
770
771
772 int
773 lfs_writeseg(struct lfs * fs, struct segment * sp)
774 {
775 struct ubuf **bpp, *bp;
776 SEGUSE *sup;
777 SEGSUM *ssp;
778 char *datap, *dp;
779 int i;
780 int do_again, nblocks, byteoffset;
781 size_t el_size;
782 u_short ninos;
783 size_t sumstart;
784 struct uvnode *devvp;
785
786 /*
787 * If there are no buffers other than the segment summary to write
788 * and it is not a checkpoint, don't do anything. On a checkpoint,
789 * even if there aren't any buffers, you need to write the superblock.
790 */
791 nblocks = sp->cbpp - sp->bpp;
792 #if 0
793 printf("write %d blocks at 0x%x\n",
794 nblocks, (int)LFS_DBTOFSB(fs, (*sp->bpp)->b_blkno));
795 #endif
796 if (nblocks == 1)
797 return 0;
798
799 devvp = fs->lfs_devvp;
800
801 /* Update the segment usage information. */
802 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
803 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
804
805 /* Loop through all blocks, except the segment summary. */
806 for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
807 if ((*bpp)->b_vp != devvp) {
808 sup->su_nbytes += (*bpp)->b_bcount;
809 }
810 assert(lfs_dtosn(fs, LFS_DBTOFSB(fs, (*bpp)->b_blkno)) == sp->seg_number);
811 }
812
813 ssp = (SEGSUM *) sp->segsum;
814 lfs_ss_setflags(fs, ssp, lfs_ss_getflags(fs, ssp) | SS_RFW);
815
816 ninos = (lfs_ss_getninos(fs, ssp) + LFS_INOPB(fs) - 1) / LFS_INOPB(fs);
817 sup->su_nbytes += lfs_ss_getninos(fs, ssp) * DINOSIZE(fs);
818
819 if (lfs_sb_getversion(fs) == 1)
820 sup->su_olastmod = write_time;
821 else
822 sup->su_lastmod = write_time;
823 sup->su_ninos += ninos;
824 ++sup->su_nsums;
825 lfs_sb_adddmeta(fs, (lfs_btofsb(fs, lfs_sb_getsumsize(fs)) + lfs_btofsb(fs, ninos *
826 lfs_sb_getibsize(fs))));
827 lfs_sb_subavail(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
828
829 do_again = !(bp->b_flags & B_GATHERED);
830 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
831
832 /*
833 * Compute checksum across data and then across summary; the first
834 * block (the summary block) is skipped. Set the create time here
835 * so that it's guaranteed to be later than the inode mod times.
836 */
837 if (lfs_sb_getversion(fs) == 1)
838 el_size = sizeof(u_long);
839 else
840 el_size = sizeof(u_int32_t);
841 datap = dp = emalloc(nblocks * el_size);
842 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
843 ++bpp;
844 /* Loop through gop_write cluster blocks */
845 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
846 byteoffset += lfs_sb_getbsize(fs)) {
847 memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
848 dp += el_size;
849 }
850 bremfree(*bpp);
851 (*bpp)->b_flags |= B_BUSY;
852 }
853 if (lfs_sb_getversion(fs) == 1)
854 lfs_ss_setocreate(fs, ssp, write_time);
855 else {
856 lfs_ss_setcreate(fs, ssp, write_time);
857 lfs_sb_addserial(fs, 1);
858 lfs_ss_setserial(fs, ssp, lfs_sb_getserial(fs));
859 lfs_ss_setident(fs, ssp, lfs_sb_getident(fs));
860 }
861 /* Set the summary block busy too */
862 bremfree(*(sp->bpp));
863 (*(sp->bpp))->b_flags |= B_BUSY;
864
865 lfs_ss_setdatasum(fs, ssp, cksum(datap, (nblocks - 1) * el_size));
866 sumstart = lfs_ss_getsumstart(fs);
867 lfs_ss_setsumsum(fs, ssp,
868 cksum((char *)ssp + sumstart, lfs_sb_getsumsize(fs) - sumstart));
869 free(datap);
870 datap = dp = NULL;
871 lfs_sb_subbfree(fs, (lfs_btofsb(fs, ninos * lfs_sb_getibsize(fs)) +
872 lfs_btofsb(fs, lfs_sb_getsumsize(fs))));
873
874 if (devvp == NULL)
875 errx(EXIT_FAILURE, "devvp is NULL");
876 for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
877 bp = *bpp;
878 #if 0
879 printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
880 nblocks - i, bp, bp->b_flags, bp->b_blkno);
881 printf(" vp = %p\n", bp->b_vp);
882 if (bp->b_vp != fs->lfs_devvp)
883 printf(" ino = %d lbn = %" PRId64 "\n",
884 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
885 #endif
886 if (bp->b_vp == fs->lfs_devvp)
887 written_dev += bp->b_bcount;
888 else {
889 if (bp->b_lblkno >= 0)
890 written_data += bp->b_bcount;
891 else
892 written_indir += bp->b_bcount;
893 }
894 bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
895 B_LOCKED);
896 bwrite(bp);
897 written_bytes += bp->b_bcount;
898 }
899 written_inodes += ninos;
900
901 return (lfs_initseg(fs) || do_again);
902 }
903
904 /*
905 * Our own copy of shellsort. XXX use qsort or heapsort.
906 */
907 static void
908 lfs_shellsort(struct lfs *fs,
909 struct ubuf ** bp_array, union lfs_blocks *lb_array, int nmemb, int size)
910 {
911 static int __rsshell_increments[] = {4, 1, 0};
912 int incr, *incrp, t1, t2;
913 struct ubuf *bp_temp;
914
915 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
916 for (t1 = incr; t1 < nmemb; ++t1)
917 for (t2 = t1 - incr; t2 >= 0;)
918 if ((u_int32_t) bp_array[t2]->b_lblkno >
919 (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
920 bp_temp = bp_array[t2];
921 bp_array[t2] = bp_array[t2 + incr];
922 bp_array[t2 + incr] = bp_temp;
923 t2 -= incr;
924 } else
925 break;
926
927 /* Reform the list of logical blocks */
928 incr = 0;
929 for (t1 = 0; t1 < nmemb; t1++) {
930 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
931 lfs_blocks_set(fs, lb_array, incr++,
932 bp_array[t1]->b_lblkno + t2);
933 }
934 }
935 }
936
937
938 /*
939 * lfs_seglock --
940 * Single thread the segment writer.
941 */
942 int
943 lfs_seglock(struct lfs * fs, unsigned long flags)
944 {
945 struct segment *sp;
946 size_t allocsize;
947
948 if (fs->lfs_seglock) {
949 ++fs->lfs_seglock;
950 fs->lfs_sp->seg_flags |= flags;
951 return 0;
952 }
953 fs->lfs_seglock = 1;
954
955 sp = fs->lfs_sp = emalloc(sizeof(*sp));
956 allocsize = lfs_sb_getssize(fs) * sizeof(struct ubuf *);
957 sp->bpp = emalloc(allocsize);
958 if (!sp->bpp)
959 err(!preen, "Could not allocate %zu bytes", allocsize);
960 sp->seg_flags = flags;
961 sp->vp = NULL;
962 sp->seg_iocount = 0;
963 (void) lfs_initseg(fs);
964
965 return 0;
966 }
967
968 /*
969 * lfs_segunlock --
970 * Single thread the segment writer.
971 */
972 void
973 lfs_segunlock(struct lfs * fs)
974 {
975 struct segment *sp;
976 struct ubuf *bp;
977
978 sp = fs->lfs_sp;
979
980 if (fs->lfs_seglock == 1) {
981 if (sp->bpp != sp->cbpp) {
982 /* Free allocated segment summary */
983 lfs_sb_suboffset(fs, lfs_btofsb(fs, lfs_sb_getsumsize(fs)));
984 bp = *sp->bpp;
985 bremfree(bp);
986 bp->b_flags |= B_DONE | B_INVAL;
987 bp->b_flags &= ~B_DELWRI;
988 reassignbuf(bp, bp->b_vp);
989 bp->b_flags |= B_BUSY; /* XXX */
990 brelse(bp, 0);
991 } else
992 printf("unlock to 0 with no summary");
993
994 free(sp->bpp);
995 sp->bpp = NULL;
996 free(sp);
997 fs->lfs_sp = NULL;
998
999 fs->lfs_nactive = 0;
1000
1001 /* Since we *know* everything's on disk, write both sbs */
1002 lfs_writesuper(fs, lfs_sb_getsboff(fs, 0));
1003 lfs_writesuper(fs, lfs_sb_getsboff(fs, 1));
1004
1005 --fs->lfs_seglock;
1006 fs->lfs_lockpid = 0;
1007 } else if (fs->lfs_seglock == 0) {
1008 errx(EXIT_FAILURE, "Seglock not held");
1009 } else {
1010 --fs->lfs_seglock;
1011 }
1012 }
1013
1014 int
1015 lfs_writevnodes(struct lfs *fs, struct segment *sp, int op)
1016 {
1017 struct inode *ip;
1018 struct uvnode *vp;
1019 int inodes_written = 0;
1020
1021 LIST_FOREACH(vp, &vnodelist, v_mntvnodes) {
1022 if (vp->v_bmap_op != lfs_vop_bmap)
1023 continue;
1024
1025 ip = VTOI(vp);
1026
1027 if ((op == VN_DIROP && !(vp->v_uflag & VU_DIROP)) ||
1028 (op != VN_DIROP && (vp->v_uflag & VU_DIROP))) {
1029 continue;
1030 }
1031 /*
1032 * Write the inode/file if dirty and it's not the IFILE.
1033 */
1034 if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) {
1035 if (ip->i_number != LFS_IFILE_INUM)
1036 lfs_writefile(fs, sp, vp);
1037 (void) lfs_writeinode(fs, sp, ip);
1038 inodes_written++;
1039 }
1040 }
1041 return inodes_written;
1042 }
1043
1044 void
1045 lfs_writesuper(struct lfs *fs, ulfs_daddr_t daddr)
1046 {
1047 struct ubuf *bp;
1048
1049 /* Set timestamp of this version of the superblock */
1050 if (lfs_sb_getversion(fs) == 1)
1051 lfs_sb_setotstamp(fs, write_time);
1052 lfs_sb_settstamp(fs, write_time);
1053
1054 __CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
1055
1056 /* Checksum the superblock and copy it into a buffer. */
1057 lfs_sb_setcksum(fs, lfs_sb_cksum(fs));
1058 assert(daddr > 0);
1059 bp = getblk(fs->lfs_devvp, LFS_FSBTODB(fs, daddr), LFS_SBPAD);
1060 memcpy(bp->b_data, &fs->lfs_dlfs_u, sizeof(struct dlfs));
1061 memset(bp->b_data + sizeof(struct dlfs), 0,
1062 LFS_SBPAD - sizeof(struct dlfs));
1063
1064 bwrite(bp);
1065 }
1066