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