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