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