lfs_segment.c revision 1.4 1 /* $NetBSD: lfs_segment.c,v 1.4 1996/02/09 22:28:54 christos Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)lfs_segment.c 8.5 (Berkeley) 1/4/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/kernel.h>
42 #include <sys/resourcevar.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/conf.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51
52 #include <miscfs/specfs/specdev.h>
53 #include <miscfs/fifofs/fifo.h>
54
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ufs/inode.h>
57 #include <ufs/ufs/dir.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60
61 #include <ufs/lfs/lfs.h>
62 #include <ufs/lfs/lfs_extern.h>
63
64 extern int count_lock_queue __P((void));
65
66 #define MAX_ACTIVE 10
67 /*
68 * Determine if it's OK to start a partial in this segment, or if we need
69 * to go on to a new segment.
70 */
71 #define LFS_PARTIAL_FITS(fs) \
72 ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
73 1 << (fs)->lfs_fsbtodb)
74
75 void lfs_callback __P((struct buf *));
76 void lfs_gather __P((struct lfs *, struct segment *,
77 struct vnode *, int (*) __P((struct lfs *, struct buf *))));
78 int lfs_gatherblock __P((struct segment *, struct buf *, int *));
79 void lfs_iset __P((struct inode *, daddr_t, time_t));
80 int lfs_match_data __P((struct lfs *, struct buf *));
81 int lfs_match_dindir __P((struct lfs *, struct buf *));
82 int lfs_match_indir __P((struct lfs *, struct buf *));
83 int lfs_match_tindir __P((struct lfs *, struct buf *));
84 void lfs_newseg __P((struct lfs *));
85 void lfs_shellsort __P((struct buf **, daddr_t *, register int));
86 void lfs_supercallback __P((struct buf *));
87 void lfs_updatemeta __P((struct segment *));
88 int lfs_vref __P((struct vnode *));
89 void lfs_vunref __P((struct vnode *));
90 void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
91 int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
92 int lfs_writeseg __P((struct lfs *, struct segment *));
93 void lfs_writesuper __P((struct lfs *));
94 void lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
95 struct segment *sp, int dirops));
96
97 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
98
99 /* Statistics Counters */
100 #define DOSTATS
101 struct lfs_stats lfs_stats;
102
103 /* op values to lfs_writevnodes */
104 #define VN_REG 0
105 #define VN_DIROP 1
106 #define VN_EMPTY 2
107
108 /*
109 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
110 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
111 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
112 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
113 */
114
115 int
116 lfs_vflush(vp)
117 struct vnode *vp;
118 {
119 struct inode *ip;
120 struct lfs *fs;
121 struct segment *sp;
122
123 fs = VFSTOUFS(vp->v_mount)->um_lfs;
124 if (fs->lfs_nactive > MAX_ACTIVE)
125 return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP));
126 lfs_seglock(fs, SEGM_SYNC);
127 sp = fs->lfs_sp;
128
129
130 ip = VTOI(vp);
131 if (vp->v_dirtyblkhd.lh_first == NULL)
132 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
133
134 do {
135 do {
136 if (vp->v_dirtyblkhd.lh_first != NULL)
137 lfs_writefile(fs, sp, vp);
138 } while (lfs_writeinode(fs, sp, ip));
139
140 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
141
142 #ifdef DOSTATS
143 ++lfs_stats.nwrites;
144 if (sp->seg_flags & SEGM_SYNC)
145 ++lfs_stats.nsync_writes;
146 if (sp->seg_flags & SEGM_CKP)
147 ++lfs_stats.ncheckpoints;
148 #endif
149 lfs_segunlock(fs);
150 return (0);
151 }
152
153 void
154 lfs_writevnodes(fs, mp, sp, op)
155 struct lfs *fs;
156 struct mount *mp;
157 struct segment *sp;
158 int op;
159 {
160 struct inode *ip;
161 struct vnode *vp;
162
163 loop:
164 for (vp = mp->mnt_vnodelist.lh_first;
165 vp != NULL;
166 vp = vp->v_mntvnodes.le_next) {
167 /*
168 * If the vnode that we are about to sync is no longer
169 * associated with this mount point, start over.
170 */
171 if (vp->v_mount != mp)
172 goto loop;
173
174 /* XXX ignore dirops for now
175 if (op == VN_DIROP && !(vp->v_flag & VDIROP) ||
176 op != VN_DIROP && (vp->v_flag & VDIROP))
177 continue;
178 */
179
180 if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first)
181 continue;
182
183 if (vp->v_type == VNON)
184 continue;
185
186 if (lfs_vref(vp))
187 continue;
188
189 /*
190 * Write the inode/file if dirty and it's not the
191 * the IFILE.
192 */
193 ip = VTOI(vp);
194 if ((ip->i_flag &
195 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) ||
196 vp->v_dirtyblkhd.lh_first != NULL) &&
197 ip->i_number != LFS_IFILE_INUM) {
198 if (vp->v_dirtyblkhd.lh_first != NULL)
199 lfs_writefile(fs, sp, vp);
200 (void) lfs_writeinode(fs, sp, ip);
201 }
202 vp->v_flag &= ~VDIROP;
203 lfs_vunref(vp);
204 }
205 }
206
207 int
208 lfs_segwrite(mp, flags)
209 struct mount *mp;
210 int flags; /* Do a checkpoint. */
211 {
212 struct buf *bp;
213 struct inode *ip;
214 struct lfs *fs;
215 struct segment *sp;
216 struct vnode *vp;
217 SEGUSE *segusep;
218 daddr_t ibno;
219 CLEANERINFO *cip;
220 int clean, do_ckp, error, i;
221
222 fs = VFSTOUFS(mp)->um_lfs;
223
224 /*
225 * If we have fewer than 2 clean segments, wait until cleaner
226 * writes.
227 */
228 do {
229 LFS_CLEANERINFO(cip, fs, bp);
230 clean = cip->clean;
231 brelse(bp);
232 if (clean <= 2) {
233 printf ("segs clean: %d\n", clean);
234 wakeup(&lfs_allclean_wakeup);
235 error = tsleep(&fs->lfs_avail, PRIBIO + 1,
236 "lfs writer", 0);
237 if (error)
238 return (error);
239 }
240 } while (clean <= 2 );
241
242 /*
243 * Allocate a segment structure and enough space to hold pointers to
244 * the maximum possible number of buffers which can be described in a
245 * single summary block.
246 */
247 do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE;
248 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
249 sp = fs->lfs_sp;
250
251 lfs_writevnodes(fs, mp, sp, VN_REG);
252
253 /* XXX ignore ordering of dirops for now */
254 /* XXX
255 fs->lfs_writer = 1;
256 if (fs->lfs_dirops && (error =
257 tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
258 free(sp->bpp, M_SEGMENT);
259 free(sp, M_SEGMENT);
260 fs->lfs_writer = 0;
261 return (error);
262 }
263
264 lfs_writevnodes(fs, mp, sp, VN_DIROP);
265 */
266
267 /*
268 * If we are doing a checkpoint, mark everything since the
269 * last checkpoint as no longer ACTIVE.
270 */
271 if (do_ckp)
272 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
273 --ibno >= fs->lfs_cleansz; ) {
274 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
275 NOCRED, &bp))
276
277 panic("lfs: ifile read");
278 segusep = (SEGUSE *)bp->b_data;
279 for (i = fs->lfs_sepb; i--; segusep++)
280 segusep->su_flags &= ~SEGUSE_ACTIVE;
281
282 error = VOP_BWRITE(bp);
283 }
284
285 if (do_ckp || fs->lfs_doifile) {
286 redo:
287 vp = fs->lfs_ivnode;
288 while (vget(vp, 1));
289 ip = VTOI(vp);
290 if (vp->v_dirtyblkhd.lh_first != NULL)
291 lfs_writefile(fs, sp, vp);
292 (void)lfs_writeinode(fs, sp, ip);
293 vput(vp);
294 if (lfs_writeseg(fs, sp) && do_ckp)
295 goto redo;
296 } else
297 (void) lfs_writeseg(fs, sp);
298
299 /*
300 * If the I/O count is non-zero, sleep until it reaches zero. At the
301 * moment, the user's process hangs around so we can sleep.
302 */
303 /* XXX ignore dirops for now
304 fs->lfs_writer = 0;
305 fs->lfs_doifile = 0;
306 wakeup(&fs->lfs_dirops);
307 */
308
309 #ifdef DOSTATS
310 ++lfs_stats.nwrites;
311 if (sp->seg_flags & SEGM_SYNC)
312 ++lfs_stats.nsync_writes;
313 if (sp->seg_flags & SEGM_CKP)
314 ++lfs_stats.ncheckpoints;
315 #endif
316 lfs_segunlock(fs);
317 return (0);
318 }
319
320 /*
321 * Write the dirty blocks associated with a vnode.
322 */
323 void
324 lfs_writefile(fs, sp, vp)
325 struct lfs *fs;
326 struct segment *sp;
327 struct vnode *vp;
328 {
329 struct buf *bp;
330 struct finfo *fip;
331 IFILE *ifp;
332
333 if (sp->seg_bytes_left < fs->lfs_bsize ||
334 sp->sum_bytes_left < sizeof(struct finfo))
335 (void) lfs_writeseg(fs, sp);
336
337 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
338 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
339
340 fip = sp->fip;
341 fip->fi_nblocks = 0;
342 fip->fi_ino = VTOI(vp)->i_number;
343 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
344 fip->fi_version = ifp->if_version;
345 brelse(bp);
346
347 /*
348 * It may not be necessary to write the meta-data blocks at this point,
349 * as the roll-forward recovery code should be able to reconstruct the
350 * list.
351 */
352 lfs_gather(fs, sp, vp, lfs_match_data);
353 lfs_gather(fs, sp, vp, lfs_match_indir);
354 lfs_gather(fs, sp, vp, lfs_match_dindir);
355 #ifdef TRIPLE
356 lfs_gather(fs, sp, vp, lfs_match_tindir);
357 #endif
358
359 fip = sp->fip;
360 if (fip->fi_nblocks != 0) {
361 sp->fip =
362 (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
363 sizeof(daddr_t) * (fip->fi_nblocks - 1));
364 sp->start_lbp = &sp->fip->fi_blocks[0];
365 } else {
366 sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
367 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
368 }
369 }
370
371 int
372 lfs_writeinode(fs, sp, ip)
373 struct lfs *fs;
374 struct segment *sp;
375 struct inode *ip;
376 {
377 struct buf *bp, *ibp;
378 IFILE *ifp;
379 SEGUSE *sup;
380 daddr_t daddr;
381 ino_t ino;
382 int error, i, ndx;
383 int redo_ifile = 0;
384
385 if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)))
386 return(0);
387
388 /* Allocate a new inode block if necessary. */
389 if (sp->ibp == NULL) {
390 /* Allocate a new segment if necessary. */
391 if (sp->seg_bytes_left < fs->lfs_bsize ||
392 sp->sum_bytes_left < sizeof(daddr_t))
393 (void) lfs_writeseg(fs, sp);
394
395 /* Get next inode block. */
396 daddr = fs->lfs_offset;
397 fs->lfs_offset += fsbtodb(fs, 1);
398 sp->ibp = *sp->cbpp++ =
399 lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
400 fs->lfs_bsize);
401 /* Zero out inode numbers */
402 for (i = 0; i < INOPB(fs); ++i)
403 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
404 ++sp->start_bpp;
405 fs->lfs_avail -= fsbtodb(fs, 1);
406 /* Set remaining space counters. */
407 sp->seg_bytes_left -= fs->lfs_bsize;
408 sp->sum_bytes_left -= sizeof(daddr_t);
409 ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
410 sp->ninodes / INOPB(fs) - 1;
411 ((daddr_t *)(sp->segsum))[ndx] = daddr;
412 }
413
414 /* Update the inode times and copy the inode onto the inode page. */
415 if (ip->i_flag & IN_MODIFIED)
416 --fs->lfs_uinodes;
417 ITIMES(ip, &time, &time);
418 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
419 bp = sp->ibp;
420 ((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] = ip->i_din;
421 /* Increment inode count in segment summary block. */
422 ++((SEGSUM *)(sp->segsum))->ss_ninos;
423
424 /* If this page is full, set flag to allocate a new page. */
425 if (++sp->ninodes % INOPB(fs) == 0)
426 sp->ibp = NULL;
427
428 /*
429 * If updating the ifile, update the super-block. Update the disk
430 * address and access times for this inode in the ifile.
431 */
432 ino = ip->i_number;
433 if (ino == LFS_IFILE_INUM) {
434 daddr = fs->lfs_idaddr;
435 fs->lfs_idaddr = bp->b_blkno;
436 } else {
437 LFS_IENTRY(ifp, fs, ino, ibp);
438 daddr = ifp->if_daddr;
439 ifp->if_daddr = bp->b_blkno;
440 error = VOP_BWRITE(ibp);
441 }
442
443 /*
444 * No need to update segment usage if there was no former inode address
445 * or if the last inode address is in the current partial segment.
446 */
447 if (daddr != LFS_UNUSED_DADDR &&
448 !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
449 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
450 #ifdef DIAGNOSTIC
451 if (sup->su_nbytes < sizeof(struct dinode)) {
452 /* XXX -- Change to a panic. */
453 printf("lfs: negative bytes (segment %d)\n",
454 datosn(fs, daddr));
455 panic("negative bytes");
456 }
457 #endif
458 sup->su_nbytes -= sizeof(struct dinode);
459 redo_ifile =
460 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
461 error = VOP_BWRITE(bp);
462 }
463 return (redo_ifile);
464 }
465
466 int
467 lfs_gatherblock(sp, bp, sptr)
468 struct segment *sp;
469 struct buf *bp;
470 int *sptr;
471 {
472 struct lfs *fs;
473 int version;
474
475 /*
476 * If full, finish this segment. We may be doing I/O, so
477 * release and reacquire the splbio().
478 */
479 #ifdef DIAGNOSTIC
480 if (sp->vp == NULL)
481 panic ("lfs_gatherblock: Null vp in segment");
482 #endif
483 fs = sp->fs;
484 if (sp->sum_bytes_left < sizeof(daddr_t) ||
485 sp->seg_bytes_left < fs->lfs_bsize) {
486 if (sptr)
487 splx(*sptr);
488 lfs_updatemeta(sp);
489
490 version = sp->fip->fi_version;
491 (void) lfs_writeseg(fs, sp);
492
493 sp->fip->fi_version = version;
494 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
495 /* Add the current file to the segment summary. */
496 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
497 sp->sum_bytes_left -=
498 sizeof(struct finfo) - sizeof(daddr_t);
499
500 if (sptr)
501 *sptr = splbio();
502 return(1);
503 }
504
505 /* Insert into the buffer list, update the FINFO block. */
506 bp->b_flags |= B_GATHERED;
507 *sp->cbpp++ = bp;
508 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
509
510 sp->sum_bytes_left -= sizeof(daddr_t);
511 sp->seg_bytes_left -= fs->lfs_bsize;
512 return(0);
513 }
514
515 void
516 lfs_gather(fs, sp, vp, match)
517 struct lfs *fs;
518 struct segment *sp;
519 struct vnode *vp;
520 int (*match) __P((struct lfs *, struct buf *));
521 {
522 struct buf *bp;
523 int s;
524
525 sp->vp = vp;
526 s = splbio();
527 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
528 if (bp->b_flags & B_BUSY || !match(fs, bp) ||
529 bp->b_flags & B_GATHERED)
530 continue;
531 #ifdef DIAGNOSTIC
532 if (!(bp->b_flags & B_DELWRI))
533 panic("lfs_gather: bp not B_DELWRI");
534 if (!(bp->b_flags & B_LOCKED))
535 panic("lfs_gather: bp not B_LOCKED");
536 #endif
537 if (lfs_gatherblock(sp, bp, &s))
538 goto loop;
539 }
540 splx(s);
541 lfs_updatemeta(sp);
542 sp->vp = NULL;
543 }
544
545
546 /*
547 * Update the metadata that points to the blocks listed in the FINFO
548 * array.
549 */
550 void
551 lfs_updatemeta(sp)
552 struct segment *sp;
553 {
554 SEGUSE *sup;
555 struct buf *bp;
556 struct lfs *fs;
557 struct vnode *vp;
558 struct indir a[NIADDR + 2], *ap;
559 struct inode *ip;
560 daddr_t daddr, lbn, off;
561 int db_per_fsb, error, i, nblocks, num;
562
563 vp = sp->vp;
564 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
565 if (vp == NULL || nblocks == 0)
566 return;
567
568 /* Sort the blocks. */
569 if (!(sp->seg_flags & SEGM_CLEAN))
570 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
571
572 /*
573 * Assign disk addresses, and update references to the logical
574 * block and the segment usage information.
575 */
576 fs = sp->fs;
577 db_per_fsb = fsbtodb(fs, 1);
578 for (i = nblocks; i--; ++sp->start_bpp) {
579 lbn = *sp->start_lbp++;
580 (*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
581 fs->lfs_offset += db_per_fsb;
582
583 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
584 if (error)
585 panic("lfs_updatemeta: ufs_bmaparray %d", error);
586 ip = VTOI(vp);
587 switch (num) {
588 case 0:
589 ip->i_db[lbn] = off;
590 break;
591 case 1:
592 ip->i_ib[a[0].in_off] = off;
593 break;
594 default:
595 ap = &a[num - 1];
596 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
597 panic("lfs_updatemeta: bread bno %d",
598 ap->in_lbn);
599 /*
600 * Bread may create a new indirect block which needs
601 * to get counted for the inode.
602 */
603 if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
604 printf ("Updatemeta allocating indirect block: shouldn't happen\n");
605 ip->i_blocks += btodb(fs->lfs_bsize);
606 fs->lfs_bfree -= btodb(fs->lfs_bsize);
607 }
608 ((daddr_t *)bp->b_data)[ap->in_off] = off;
609 VOP_BWRITE(bp);
610 }
611
612 /* Update segment usage information. */
613 if (daddr != UNASSIGNED &&
614 !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
615 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
616 #ifdef DIAGNOSTIC
617 if (sup->su_nbytes < fs->lfs_bsize) {
618 /* XXX -- Change to a panic. */
619 printf("lfs: negative bytes (segment %d)\n",
620 datosn(fs, daddr));
621 panic ("Negative Bytes");
622 }
623 #endif
624 sup->su_nbytes -= fs->lfs_bsize;
625 error = VOP_BWRITE(bp);
626 }
627 }
628 }
629
630 /*
631 * Start a new segment.
632 */
633 int
634 lfs_initseg(fs)
635 struct lfs *fs;
636 {
637 struct segment *sp;
638 SEGUSE *sup;
639 SEGSUM *ssp;
640 struct buf *bp;
641 int repeat;
642
643 sp = fs->lfs_sp;
644
645 repeat = 0;
646 /* Advance to the next segment. */
647 if (!LFS_PARTIAL_FITS(fs)) {
648 /* Wake up any cleaning procs waiting on this file system. */
649 wakeup(&lfs_allclean_wakeup);
650
651 lfs_newseg(fs);
652 repeat = 1;
653 fs->lfs_offset = fs->lfs_curseg;
654 sp->seg_number = datosn(fs, fs->lfs_curseg);
655 sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
656
657 /*
658 * If the segment contains a superblock, update the offset
659 * and summary address to skip over it.
660 */
661 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
662 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
663 fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
664 sp->seg_bytes_left -= LFS_SBPAD;
665 }
666 brelse(bp);
667 } else {
668 sp->seg_number = datosn(fs, fs->lfs_curseg);
669 sp->seg_bytes_left = (fs->lfs_dbpseg -
670 (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
671 }
672 fs->lfs_lastpseg = fs->lfs_offset;
673
674 sp->fs = fs;
675 sp->ibp = NULL;
676 sp->ninodes = 0;
677
678 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
679 sp->cbpp = sp->bpp;
680 *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
681 LFS_SUMMARY_SIZE);
682 sp->segsum = (*sp->cbpp)->b_data;
683 bzero(sp->segsum, LFS_SUMMARY_SIZE);
684 sp->start_bpp = ++sp->cbpp;
685 fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
686
687 /* Set point to SEGSUM, initialize it. */
688 ssp = sp->segsum;
689 ssp->ss_next = fs->lfs_nextseg;
690 ssp->ss_nfinfo = ssp->ss_ninos = 0;
691
692 /* Set pointer to first FINFO, initialize it. */
693 sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
694 sp->fip->fi_nblocks = 0;
695 sp->start_lbp = &sp->fip->fi_blocks[0];
696
697 sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
698 sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
699
700 return(repeat);
701 }
702
703 /*
704 * Return the next segment to write.
705 */
706 void
707 lfs_newseg(fs)
708 struct lfs *fs;
709 {
710 CLEANERINFO *cip;
711 SEGUSE *sup;
712 struct buf *bp;
713 int curseg, isdirty, sn;
714
715 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
716 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
717 sup->su_nbytes = 0;
718 sup->su_nsums = 0;
719 sup->su_ninos = 0;
720 (void) VOP_BWRITE(bp);
721
722 LFS_CLEANERINFO(cip, fs, bp);
723 --cip->clean;
724 ++cip->dirty;
725 (void) VOP_BWRITE(bp);
726
727 fs->lfs_lastseg = fs->lfs_curseg;
728 fs->lfs_curseg = fs->lfs_nextseg;
729 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
730 sn = (sn + 1) % fs->lfs_nseg;
731 if (sn == curseg)
732 panic("lfs_nextseg: no clean segments");
733 LFS_SEGENTRY(sup, fs, sn, bp);
734 isdirty = sup->su_flags & SEGUSE_DIRTY;
735 brelse(bp);
736 if (!isdirty)
737 break;
738 }
739
740 ++fs->lfs_nactive;
741 fs->lfs_nextseg = sntoda(fs, sn);
742 #ifdef DOSTATS
743 ++lfs_stats.segsused;
744 #endif
745 }
746
747 int
748 lfs_writeseg(fs, sp)
749 struct lfs *fs;
750 struct segment *sp;
751 {
752 extern int locked_queue_count;
753 struct buf **bpp, *bp, *cbp;
754 SEGUSE *sup;
755 SEGSUM *ssp;
756 dev_t i_dev;
757 size_t size;
758 u_long *datap, *dp;
759 int ch_per_blk, do_again, i, nblocks, num, s;
760 int (*strategy)__P((void *));
761 struct vop_strategy_args vop_strategy_a;
762 u_short ninos;
763 char *p;
764
765 /*
766 * If there are no buffers other than the segment summary to write
767 * and it is not a checkpoint, don't do anything. On a checkpoint,
768 * even if there aren't any buffers, you need to write the superblock.
769 */
770 if ((nblocks = sp->cbpp - sp->bpp) == 1)
771 return (0);
772
773 ssp = (SEGSUM *)sp->segsum;
774
775 /* Update the segment usage information. */
776 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
777 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
778 sup->su_nbytes += (nblocks - 1 - ninos) << fs->lfs_bshift;
779 sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
780 sup->su_nbytes += LFS_SUMMARY_SIZE;
781 sup->su_lastmod = time.tv_sec;
782 sup->su_ninos += ninos;
783 ++sup->su_nsums;
784 do_again = !(bp->b_flags & B_GATHERED);
785 (void)VOP_BWRITE(bp);
786 /*
787 * Compute checksum across data and then across summary; the first
788 * block (the summary block) is skipped. Set the create time here
789 * so that it's guaranteed to be later than the inode mod times.
790 *
791 * XXX
792 * Fix this to do it inline, instead of malloc/copy.
793 */
794 datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
795 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
796 if ((*++bpp)->b_flags & B_INVAL) {
797 if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
798 panic("lfs_writeseg: copyin failed");
799 } else
800 *dp++ = ((u_long *)(*bpp)->b_data)[0];
801 }
802 ssp->ss_create = time.tv_sec;
803 ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
804 ssp->ss_sumsum =
805 cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
806 free(datap, M_SEGMENT);
807 #ifdef DIAGNOSTIC
808 if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
809 panic("lfs_writeseg: No diskspace for summary");
810 #endif
811 fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
812
813 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
814 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
815
816 /*
817 * When we simply write the blocks we lose a rotation for every block
818 * written. To avoid this problem, we allocate memory in chunks, copy
819 * the buffers into the chunk and write the chunk. MAXPHYS is the
820 * largest size I/O devices can handle.
821 * When the data is copied to the chunk, turn off the the B_LOCKED bit
822 * and brelse the buffer (which will move them to the LRU list). Add
823 * the B_CALL flag to the buffer header so we can count I/O's for the
824 * checkpoints and so we can release the allocated memory.
825 *
826 * XXX
827 * This should be removed if the new virtual memory system allows us to
828 * easily make the buffers contiguous in kernel memory and if that's
829 * fast enough.
830 */
831 ch_per_blk = MAXPHYS / fs->lfs_bsize;
832 for (bpp = sp->bpp, i = nblocks; i;) {
833 num = ch_per_blk;
834 if (num > i)
835 num = i;
836 i -= num;
837 size = num * fs->lfs_bsize;
838
839 cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
840 (*bpp)->b_blkno, size);
841 cbp->b_dev = i_dev;
842 cbp->b_flags |= B_ASYNC | B_BUSY;
843
844 s = splbio();
845 ++fs->lfs_iocount;
846 for (p = cbp->b_data; num--;) {
847 bp = *bpp++;
848 /*
849 * Fake buffers from the cleaner are marked as B_INVAL.
850 * We need to copy the data from user space rather than
851 * from the buffer indicated.
852 * XXX == what do I do on an error?
853 */
854 if (bp->b_flags & B_INVAL) {
855 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
856 panic("lfs_writeseg: copyin failed");
857 } else
858 bcopy(bp->b_data, p, bp->b_bcount);
859 p += bp->b_bcount;
860 if (bp->b_flags & B_LOCKED)
861 --locked_queue_count;
862 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
863 B_LOCKED | B_GATHERED);
864 if (bp->b_flags & B_CALL) {
865 /* if B_CALL, it was created with newbuf */
866 brelvp(bp);
867 if (!(bp->b_flags & B_INVAL))
868 free(bp->b_data, M_SEGMENT);
869 free(bp, M_SEGMENT);
870 } else {
871 bremfree(bp);
872 bp->b_flags |= B_DONE;
873 reassignbuf(bp, bp->b_vp);
874 brelse(bp);
875 }
876 }
877 ++cbp->b_vp->v_numoutput;
878 splx(s);
879 cbp->b_bcount = p - (char *)cbp->b_data;
880 /*
881 * XXXX This is a gross and disgusting hack. Since these
882 * buffers are physically addressed, they hang off the
883 * device vnode (devvp). As a result, they have no way
884 * of getting to the LFS superblock or lfs structure to
885 * keep track of the number of I/O's pending. So, I am
886 * going to stuff the fs into the saveaddr field of
887 * the buffer (yuk).
888 */
889 cbp->b_saveaddr = (caddr_t)fs;
890 vop_strategy_a.a_desc = VDESC(vop_strategy);
891 vop_strategy_a.a_bp = cbp;
892 (strategy)(&vop_strategy_a);
893 }
894 /*
895 * XXX
896 * Vinvalbuf can move locked buffers off the locked queue
897 * and we have no way of knowing about this. So, after
898 * doing a big write, we recalculate how many bufers are
899 * really still left on the locked queue.
900 */
901 locked_queue_count = count_lock_queue();
902 wakeup(&locked_queue_count);
903 #ifdef DOSTATS
904 ++lfs_stats.psegwrites;
905 lfs_stats.blocktot += nblocks - 1;
906 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
907 ++lfs_stats.psyncwrites;
908 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
909 ++lfs_stats.pcleanwrites;
910 lfs_stats.cleanblocks += nblocks - 1;
911 }
912 #endif
913 return (lfs_initseg(fs) || do_again);
914 }
915
916 void
917 lfs_writesuper(fs)
918 struct lfs *fs;
919 {
920 struct buf *bp;
921 dev_t i_dev;
922 int (*strategy) __P((void *));
923 int s;
924 struct vop_strategy_args vop_strategy_a;
925
926 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
927 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
928
929 /* Checksum the superblock and copy it into a buffer. */
930 fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
931 bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
932 LFS_SBPAD);
933 *(struct lfs *)bp->b_data = *fs;
934
935 /* XXX Toggle between first two superblocks; for now just write first */
936 bp->b_dev = i_dev;
937 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
938 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
939 bp->b_iodone = lfs_supercallback;
940 vop_strategy_a.a_desc = VDESC(vop_strategy);
941 vop_strategy_a.a_bp = bp;
942 s = splbio();
943 ++bp->b_vp->v_numoutput;
944 splx(s);
945 (strategy)(&vop_strategy_a);
946 }
947
948 /*
949 * Logical block number match routines used when traversing the dirty block
950 * chain.
951 */
952 int
953 lfs_match_data(fs, bp)
954 struct lfs *fs;
955 struct buf *bp;
956 {
957 return (bp->b_lblkno >= 0);
958 }
959
960 int
961 lfs_match_indir(fs, bp)
962 struct lfs *fs;
963 struct buf *bp;
964 {
965 int lbn;
966
967 lbn = bp->b_lblkno;
968 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
969 }
970
971 int
972 lfs_match_dindir(fs, bp)
973 struct lfs *fs;
974 struct buf *bp;
975 {
976 int lbn;
977
978 lbn = bp->b_lblkno;
979 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
980 }
981
982 int
983 lfs_match_tindir(fs, bp)
984 struct lfs *fs;
985 struct buf *bp;
986 {
987 int lbn;
988
989 lbn = bp->b_lblkno;
990 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
991 }
992
993 /*
994 * Allocate a new buffer header.
995 */
996 struct buf *
997 lfs_newbuf(vp, daddr, size)
998 struct vnode *vp;
999 daddr_t daddr;
1000 size_t size;
1001 {
1002 struct buf *bp;
1003 size_t nbytes;
1004
1005 nbytes = roundup(size, DEV_BSIZE);
1006 bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
1007 bzero(bp, sizeof(struct buf));
1008 if (nbytes)
1009 bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
1010 bgetvp(vp, bp);
1011 bp->b_bufsize = size;
1012 bp->b_bcount = size;
1013 bp->b_lblkno = daddr;
1014 bp->b_blkno = daddr;
1015 bp->b_error = 0;
1016 bp->b_resid = 0;
1017 bp->b_iodone = lfs_callback;
1018 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
1019 return (bp);
1020 }
1021
1022 void
1023 lfs_callback(bp)
1024 struct buf *bp;
1025 {
1026 struct lfs *fs;
1027
1028 fs = (struct lfs *)bp->b_saveaddr;
1029 #ifdef DIAGNOSTIC
1030 if (fs->lfs_iocount == 0)
1031 panic("lfs_callback: zero iocount\n");
1032 #endif
1033 if (--fs->lfs_iocount == 0)
1034 wakeup(&fs->lfs_iocount);
1035
1036 brelvp(bp);
1037 free(bp->b_data, M_SEGMENT);
1038 free(bp, M_SEGMENT);
1039 }
1040
1041 void
1042 lfs_supercallback(bp)
1043 struct buf *bp;
1044 {
1045 brelvp(bp);
1046 free(bp->b_data, M_SEGMENT);
1047 free(bp, M_SEGMENT);
1048 }
1049
1050 /*
1051 * Shellsort (diminishing increment sort) from Data Structures and
1052 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
1053 * see also Knuth Vol. 3, page 84. The increments are selected from
1054 * formula (8), page 95. Roughly O(N^3/2).
1055 */
1056 /*
1057 * This is our own private copy of shellsort because we want to sort
1058 * two parallel arrays (the array of buffer pointers and the array of
1059 * logical block numbers) simultaneously. Note that we cast the array
1060 * of logical block numbers to a unsigned in this routine so that the
1061 * negative block numbers (meta data blocks) sort AFTER the data blocks.
1062 */
1063 void
1064 lfs_shellsort(bp_array, lb_array, nmemb)
1065 struct buf **bp_array;
1066 daddr_t *lb_array;
1067 register int nmemb;
1068 {
1069 static int __rsshell_increments[] = { 4, 1, 0 };
1070 register int incr, *incrp, t1, t2;
1071 struct buf *bp_temp;
1072 u_long lb_temp;
1073
1074 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
1075 for (t1 = incr; t1 < nmemb; ++t1)
1076 for (t2 = t1 - incr; t2 >= 0;)
1077 if (lb_array[t2] > lb_array[t2 + incr]) {
1078 lb_temp = lb_array[t2];
1079 lb_array[t2] = lb_array[t2 + incr];
1080 lb_array[t2 + incr] = lb_temp;
1081 bp_temp = bp_array[t2];
1082 bp_array[t2] = bp_array[t2 + incr];
1083 bp_array[t2 + incr] = bp_temp;
1084 t2 -= incr;
1085 } else
1086 break;
1087 }
1088
1089 /*
1090 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
1091 */
1092 int
1093 lfs_vref(vp)
1094 register struct vnode *vp;
1095 {
1096
1097 if (vp->v_flag & VXLOCK)
1098 return(1);
1099 return (vget(vp, 0));
1100 }
1101
1102 void
1103 lfs_vunref(vp)
1104 register struct vnode *vp;
1105 {
1106 extern int lfs_no_inactive;
1107
1108 /*
1109 * This is vrele except that we do not want to VOP_INACTIVE
1110 * this vnode. Rather than inline vrele here, we use a global
1111 * flag to tell lfs_inactive not to run. Yes, its gross.
1112 */
1113 lfs_no_inactive = 1;
1114 vrele(vp);
1115 lfs_no_inactive = 0;
1116 }
1117