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