lfs_segment.c revision 1.61 1 /* $NetBSD: lfs_segment.c,v 1.61 2000/11/12 07:58:36 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
71 */
72
73 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str))
74
75 #include "opt_ddb.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/namei.h>
79 #include <sys/kernel.h>
80 #include <sys/resourcevar.h>
81 #include <sys/file.h>
82 #include <sys/stat.h>
83 #include <sys/buf.h>
84 #include <sys/proc.h>
85 #include <sys/conf.h>
86 #include <sys/vnode.h>
87 #include <sys/malloc.h>
88 #include <sys/mount.h>
89
90 #include <miscfs/specfs/specdev.h>
91 #include <miscfs/fifofs/fifo.h>
92
93 #include <ufs/ufs/quota.h>
94 #include <ufs/ufs/inode.h>
95 #include <ufs/ufs/dir.h>
96 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ufs/ufs_extern.h>
98
99 #include <ufs/lfs/lfs.h>
100 #include <ufs/lfs/lfs_extern.h>
101
102 extern int count_lock_queue __P((void));
103 extern struct simplelock vnode_free_list_slock; /* XXX */
104
105 /*
106 * Determine if it's OK to start a partial in this segment, or if we need
107 * to go on to a new segment.
108 */
109 #define LFS_PARTIAL_FITS(fs) \
110 ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
111 1 << (fs)->lfs_fsbtodb)
112
113 void lfs_callback __P((struct buf *));
114 int lfs_gather __P((struct lfs *, struct segment *,
115 struct vnode *, int (*) __P((struct lfs *, struct buf *))));
116 int lfs_gatherblock __P((struct segment *, struct buf *, int *));
117 void lfs_iset __P((struct inode *, ufs_daddr_t, time_t));
118 int lfs_match_fake __P((struct lfs *, struct buf *));
119 int lfs_match_data __P((struct lfs *, struct buf *));
120 int lfs_match_dindir __P((struct lfs *, struct buf *));
121 int lfs_match_indir __P((struct lfs *, struct buf *));
122 int lfs_match_tindir __P((struct lfs *, struct buf *));
123 void lfs_newseg __P((struct lfs *));
124 void lfs_shellsort __P((struct buf **, ufs_daddr_t *, int));
125 void lfs_supercallback __P((struct buf *));
126 void lfs_updatemeta __P((struct segment *));
127 int lfs_vref __P((struct vnode *));
128 void lfs_vunref __P((struct vnode *));
129 void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
130 int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
131 int lfs_writeseg __P((struct lfs *, struct segment *));
132 void lfs_writesuper __P((struct lfs *, daddr_t));
133 int lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
134 struct segment *sp, int dirops));
135
136 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
137 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */
138 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */
139 int lfs_dirvcount = 0; /* # active dirops */
140
141 /* Statistics Counters */
142 int lfs_dostats = 1;
143 struct lfs_stats lfs_stats;
144
145 /* op values to lfs_writevnodes */
146 #define VN_REG 0
147 #define VN_DIROP 1
148 #define VN_EMPTY 2
149 #define VN_CLEAN 3
150
151 #define LFS_MAX_ACTIVE 10
152
153 /*
154 * XXX KS - Set modification time on the Ifile, so the cleaner can
155 * read the fs mod time off of it. We don't set IN_UPDATE here,
156 * since we don't really need this to be flushed to disk (and in any
157 * case that wouldn't happen to the Ifile until we checkpoint).
158 */
159 void
160 lfs_imtime(fs)
161 struct lfs *fs;
162 {
163 struct timespec ts;
164 struct inode *ip;
165
166 TIMEVAL_TO_TIMESPEC(&time, &ts);
167 ip = VTOI(fs->lfs_ivnode);
168 ip->i_ffs_mtime = ts.tv_sec;
169 ip->i_ffs_mtimensec = ts.tv_nsec;
170 }
171
172 /*
173 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
174 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
175 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
176 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
177 */
178
179 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
180 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp))
181 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
182
183 int
184 lfs_vflush(vp)
185 struct vnode *vp;
186 {
187 struct inode *ip;
188 struct lfs *fs;
189 struct segment *sp;
190 struct buf *bp, *nbp, *tbp, *tnbp;
191 int error, s;
192
193 ip = VTOI(vp);
194 fs = VFSTOUFS(vp->v_mount)->um_lfs;
195
196 if(ip->i_flag & IN_CLEANING) {
197 #ifdef DEBUG_LFS
198 ivndebug(vp,"vflush/in_cleaning");
199 #endif
200 LFS_CLR_UINO(ip, IN_CLEANING);
201 LFS_SET_UINO(ip, IN_MODIFIED);
202
203 /*
204 * Toss any cleaning buffers that have real counterparts
205 * to avoid losing new data
206 */
207 s = splbio();
208 for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
209 nbp = bp->b_vnbufs.le_next;
210 if(bp->b_flags & B_CALL) {
211 for(tbp=vp->v_dirtyblkhd.lh_first; tbp;
212 tbp=tnbp)
213 {
214 tnbp = tbp->b_vnbufs.le_next;
215 if(tbp->b_vp == bp->b_vp
216 && tbp->b_lblkno == bp->b_lblkno
217 && tbp != bp)
218 {
219 lfs_freebuf(bp);
220 }
221 }
222 }
223 }
224 splx(s);
225 }
226
227 /* If the node is being written, wait until that is done */
228 if(WRITEINPROG(vp)) {
229 #ifdef DEBUG_LFS
230 ivndebug(vp,"vflush/writeinprog");
231 #endif
232 tsleep(vp, PRIBIO+1, "lfs_vw", 0);
233 }
234
235 /* Protect against VXLOCK deadlock in vinvalbuf() */
236 lfs_seglock(fs, SEGM_SYNC);
237
238 /* If we're supposed to flush a freed inode, just toss it */
239 /* XXX - seglock, so these buffers can't be gathered, right? */
240 if(ip->i_ffs_mode == 0) {
241 printf("lfs_vflush: ino %d is freed, not flushing\n",
242 ip->i_number);
243 s = splbio();
244 for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
245 nbp = bp->b_vnbufs.le_next;
246 /* Copied from lfs_writeseg */
247 if (bp->b_flags & B_CALL) {
248 /* if B_CALL, it was created with newbuf */
249 lfs_freebuf(bp);
250 } else {
251 bremfree(bp);
252 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
253 B_LOCKED | B_GATHERED);
254 bp->b_flags |= B_DONE;
255 reassignbuf(bp, vp);
256 brelse(bp);
257 }
258 }
259 splx(s);
260 LFS_CLR_UINO(ip, IN_CLEANING);
261 LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
262 ip->i_flag &= ~IN_ALLMOD;
263 printf("lfs_vflush: done not flushing ino %d\n",
264 ip->i_number);
265 lfs_segunlock(fs);
266 return 0;
267 }
268
269 SET_FLUSHING(fs,vp);
270 if (fs->lfs_nactive > LFS_MAX_ACTIVE) {
271 error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP);
272 CLR_FLUSHING(fs,vp);
273 lfs_segunlock(fs);
274 return error;
275 }
276 sp = fs->lfs_sp;
277
278 if (vp->v_dirtyblkhd.lh_first == NULL) {
279 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
280 } else if((ip->i_flag & IN_CLEANING) &&
281 (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
282 #ifdef DEBUG_LFS
283 ivndebug(vp,"vflush/clean");
284 #endif
285 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
286 }
287 else if(lfs_dostats) {
288 if(vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & IN_ALLMOD))
289 ++lfs_stats.vflush_invoked;
290 #ifdef DEBUG_LFS
291 ivndebug(vp,"vflush");
292 #endif
293 }
294
295 #ifdef DIAGNOSTIC
296 /* XXX KS This actually can happen right now, though it shouldn't(?) */
297 if(vp->v_flag & VDIROP) {
298 printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
299 /* panic("VDIROP being flushed...this can\'t happen"); */
300 }
301 if(vp->v_usecount<0) {
302 printf("usecount=%ld\n",vp->v_usecount);
303 panic("lfs_vflush: usecount<0");
304 }
305 #endif
306
307 do {
308 do {
309 if (vp->v_dirtyblkhd.lh_first != NULL)
310 lfs_writefile(fs, sp, vp);
311 } while (lfs_writeinode(fs, sp, ip));
312 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
313
314 if(lfs_dostats) {
315 ++lfs_stats.nwrites;
316 if (sp->seg_flags & SEGM_SYNC)
317 ++lfs_stats.nsync_writes;
318 if (sp->seg_flags & SEGM_CKP)
319 ++lfs_stats.ncheckpoints;
320 }
321 lfs_segunlock(fs);
322
323 CLR_FLUSHING(fs,vp);
324 return (0);
325 }
326
327 #ifdef DEBUG_LFS_VERBOSE
328 # define vndebug(vp,str) if(VTOI(vp)->i_flag & IN_CLEANING) printf("not writing ino %d because %s (op %d)\n",VTOI(vp)->i_number,(str),op)
329 #else
330 # define vndebug(vp,str)
331 #endif
332
333 int
334 lfs_writevnodes(fs, mp, sp, op)
335 struct lfs *fs;
336 struct mount *mp;
337 struct segment *sp;
338 int op;
339 {
340 struct inode *ip;
341 struct vnode *vp;
342 int inodes_written=0, only_cleaning;
343 int needs_unlock;
344
345 #ifndef LFS_NO_BACKVP_HACK
346 /* BEGIN HACK */
347 #define VN_OFFSET (((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
348 #define BACK_VP(VP) ((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
349 #define BEG_OF_VLIST ((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
350
351 /* Find last vnode. */
352 loop: for (vp = mp->mnt_vnodelist.lh_first;
353 vp && vp->v_mntvnodes.le_next != NULL;
354 vp = vp->v_mntvnodes.le_next);
355 for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
356 #else
357 loop:
358 for (vp = mp->mnt_vnodelist.lh_first;
359 vp != NULL;
360 vp = vp->v_mntvnodes.le_next) {
361 #endif
362 /*
363 * If the vnode that we are about to sync is no longer
364 * associated with this mount point, start over.
365 */
366 if (vp->v_mount != mp) {
367 printf("lfs_writevnodes: starting over\n");
368 goto loop;
369 }
370
371 ip = VTOI(vp);
372 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
373 (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
374 vndebug(vp,"dirop");
375 continue;
376 }
377
378 if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) {
379 vndebug(vp,"empty");
380 continue;
381 }
382
383 if (vp->v_type == VNON) {
384 continue;
385 }
386
387 if(op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
388 && vp != fs->lfs_flushvp
389 && !(ip->i_flag & IN_CLEANING)) {
390 vndebug(vp,"cleaning");
391 continue;
392 }
393
394 if (lfs_vref(vp)) {
395 vndebug(vp,"vref");
396 continue;
397 }
398
399 needs_unlock = 0;
400 if (VOP_ISLOCKED(vp)) {
401 if (vp != fs->lfs_ivnode &&
402 vp->v_lock.lk_lockholder != curproc->p_pid) {
403 #ifdef DEBUG_LFS
404 printf("lfs_writevnodes: not writing ino %d,"
405 " locked by pid %d\n",
406 VTOI(vp)->i_number,
407 vp->v_lock.lk_lockholder);
408 #endif
409 lfs_vunref(vp);
410 continue;
411 }
412 } else if (vp != fs->lfs_ivnode) {
413 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
414 needs_unlock = 1;
415 }
416
417 only_cleaning = 0;
418 /*
419 * Write the inode/file if dirty and it's not the IFILE.
420 */
421 if ((ip->i_flag & IN_ALLMOD) ||
422 (vp->v_dirtyblkhd.lh_first != NULL))
423 {
424 only_cleaning = ((ip->i_flag & IN_ALLMOD)==IN_CLEANING);
425
426 if(ip->i_number != LFS_IFILE_INUM
427 && vp->v_dirtyblkhd.lh_first != NULL)
428 {
429 lfs_writefile(fs, sp, vp);
430 }
431 if(vp->v_dirtyblkhd.lh_first != NULL) {
432 if(WRITEINPROG(vp)) {
433 #ifdef DEBUG_LFS
434 ivndebug(vp,"writevnodes/write2");
435 #endif
436 } else if(!(ip->i_flag & IN_ALLMOD)) {
437 #ifdef DEBUG_LFS
438 printf("<%d>",ip->i_number);
439 #endif
440 LFS_SET_UINO(ip, IN_MODIFIED);
441 }
442 }
443 (void) lfs_writeinode(fs, sp, ip);
444 inodes_written++;
445 }
446
447 if (needs_unlock)
448 VOP_UNLOCK(vp, 0);
449
450 if (lfs_clean_vnhead && only_cleaning)
451 lfs_vunref_head(vp);
452 else
453 lfs_vunref(vp);
454 }
455 return inodes_written;
456 }
457
458 int
459 lfs_segwrite(mp, flags)
460 struct mount *mp;
461 int flags; /* Do a checkpoint. */
462 {
463 struct buf *bp;
464 struct inode *ip;
465 struct lfs *fs;
466 struct segment *sp;
467 struct vnode *vp;
468 SEGUSE *segusep;
469 ufs_daddr_t ibno;
470 int do_ckp, did_ckp, error, i;
471 int writer_set = 0;
472 int dirty;
473
474 fs = VFSTOUFS(mp)->um_lfs;
475
476 if (fs->lfs_ronly)
477 return EROFS;
478
479 lfs_imtime(fs);
480
481 /* printf("lfs_segwrite: ifile flags are 0x%lx\n",
482 (long)(VTOI(fs->lfs_ivnode)->i_flag)); */
483
484 #if 0
485 /*
486 * If we are not the cleaner, and there is no space available,
487 * wait until cleaner writes.
488 */
489 if(!(flags & SEGM_CLEAN) && !(fs->lfs_seglock && fs->lfs_sp &&
490 (fs->lfs_sp->seg_flags & SEGM_CLEAN)))
491 {
492 while (fs->lfs_avail <= 0) {
493 LFS_CLEANERINFO(cip, fs, bp);
494 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
495
496 wakeup(&lfs_allclean_wakeup);
497 wakeup(&fs->lfs_nextseg);
498 error = tsleep(&fs->lfs_avail, PRIBIO + 1, "lfs_av2",
499 0);
500 if (error) {
501 return (error);
502 }
503 }
504 }
505 #endif
506 /*
507 * Allocate a segment structure and enough space to hold pointers to
508 * the maximum possible number of buffers which can be described in a
509 * single summary block.
510 */
511 do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
512 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
513 sp = fs->lfs_sp;
514
515 /*
516 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
517 * in which case we have to flush *all* buffers off of this vnode.
518 * We don't care about other nodes, but write any non-dirop nodes
519 * anyway in anticipation of another getnewvnode().
520 *
521 * If we're cleaning we only write cleaning and ifile blocks, and
522 * no dirops, since otherwise we'd risk corruption in a crash.
523 */
524 if(sp->seg_flags & SEGM_CLEAN)
525 lfs_writevnodes(fs, mp, sp, VN_CLEAN);
526 else {
527 lfs_writevnodes(fs, mp, sp, VN_REG);
528 if(!fs->lfs_dirops || !fs->lfs_flushvp) {
529 while(fs->lfs_dirops)
530 if((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
531 "lfs writer", 0)))
532 {
533 free(sp->bpp, M_SEGMENT);
534 free(sp, M_SEGMENT);
535 return (error);
536 }
537 fs->lfs_writer++;
538 writer_set=1;
539 lfs_writevnodes(fs, mp, sp, VN_DIROP);
540 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
541 }
542 }
543
544 /*
545 * If we are doing a checkpoint, mark everything since the
546 * last checkpoint as no longer ACTIVE.
547 */
548 if (do_ckp) {
549 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
550 --ibno >= fs->lfs_cleansz; ) {
551 dirty = 0;
552 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
553
554 panic("lfs_segwrite: ifile read");
555 segusep = (SEGUSE *)bp->b_data;
556 for (i = fs->lfs_sepb; i--; segusep++) {
557 if (segusep->su_flags & SEGUSE_ACTIVE) {
558 segusep->su_flags &= ~SEGUSE_ACTIVE;
559 ++dirty;
560 }
561 }
562
563 /* But the current segment is still ACTIVE */
564 segusep = (SEGUSE *)bp->b_data;
565 if (datosn(fs, fs->lfs_curseg) / fs->lfs_sepb ==
566 (ibno-fs->lfs_cleansz)) {
567 segusep[datosn(fs, fs->lfs_curseg) %
568 fs->lfs_sepb].su_flags |= SEGUSE_ACTIVE;
569 --dirty;
570 }
571 if (dirty)
572 error = VOP_BWRITE(bp); /* Ifile */
573 else
574 brelse(bp);
575 }
576 }
577
578 did_ckp = 0;
579 if (do_ckp || fs->lfs_doifile) {
580 redo:
581 vp = fs->lfs_ivnode;
582
583 vget(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
584
585 ip = VTOI(vp);
586 if (vp->v_dirtyblkhd.lh_first != NULL)
587 lfs_writefile(fs, sp, vp);
588 if (ip->i_flag & IN_ALLMOD)
589 ++did_ckp;
590 (void) lfs_writeinode(fs, sp, ip);
591
592 vput(vp);
593
594 if (lfs_writeseg(fs, sp) && do_ckp)
595 goto redo;
596 /* The ifile should now be all clear */
597 LFS_CLR_UINO(ip, IN_ALLMOD);
598 } else {
599 (void) lfs_writeseg(fs, sp);
600 }
601
602 /*
603 * If the I/O count is non-zero, sleep until it reaches zero.
604 * At the moment, the user's process hangs around so we can
605 * sleep.
606 */
607 fs->lfs_doifile = 0;
608 if(writer_set && --fs->lfs_writer==0)
609 wakeup(&fs->lfs_dirops);
610
611 /*
612 * If we didn't write the Ifile, we didn't really do anything.
613 * That means that (1) there is a checkpoint on disk and (2)
614 * nothing has changed since it was written.
615 *
616 * Take the flags off of the segment so that lfs_segunlock
617 * doesn't have to write the superblock either.
618 */
619 if (did_ckp == 0) {
620 sp->seg_flags &= ~(SEGM_SYNC|SEGM_CKP);
621 /* if(do_ckp) printf("lfs_segwrite: no checkpoint\n"); */
622 }
623
624 if(lfs_dostats) {
625 ++lfs_stats.nwrites;
626 if (sp->seg_flags & SEGM_SYNC)
627 ++lfs_stats.nsync_writes;
628 if (sp->seg_flags & SEGM_CKP)
629 ++lfs_stats.ncheckpoints;
630 }
631 lfs_segunlock(fs);
632 return (0);
633 }
634
635 /*
636 * Write the dirty blocks associated with a vnode.
637 */
638 void
639 lfs_writefile(fs, sp, vp)
640 struct lfs *fs;
641 struct segment *sp;
642 struct vnode *vp;
643 {
644 struct buf *bp;
645 struct finfo *fip;
646 IFILE *ifp;
647
648
649 if (sp->seg_bytes_left < fs->lfs_bsize ||
650 sp->sum_bytes_left < sizeof(struct finfo))
651 (void) lfs_writeseg(fs, sp);
652
653 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
654 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
655
656 if(vp->v_flag & VDIROP)
657 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
658
659 fip = sp->fip;
660 fip->fi_nblocks = 0;
661 fip->fi_ino = VTOI(vp)->i_number;
662 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
663 fip->fi_version = ifp->if_version;
664 brelse(bp);
665
666 if(sp->seg_flags & SEGM_CLEAN)
667 {
668 lfs_gather(fs, sp, vp, lfs_match_fake);
669 /*
670 * For a file being flushed, we need to write *all* blocks.
671 * This means writing the cleaning blocks first, and then
672 * immediately following with any non-cleaning blocks.
673 * The same is true of the Ifile since checkpoints assume
674 * that all valid Ifile blocks are written.
675 */
676 if(IS_FLUSHING(fs,vp) || VTOI(vp)->i_number == LFS_IFILE_INUM)
677 lfs_gather(fs, sp, vp, lfs_match_data);
678 } else
679 lfs_gather(fs, sp, vp, lfs_match_data);
680
681 /*
682 * It may not be necessary to write the meta-data blocks at this point,
683 * as the roll-forward recovery code should be able to reconstruct the
684 * list.
685 *
686 * We have to write them anyway, though, under two conditions: (1) the
687 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
688 * checkpointing.
689 */
690 if(lfs_writeindir
691 || IS_FLUSHING(fs,vp)
692 || (sp->seg_flags & SEGM_CKP))
693 {
694 lfs_gather(fs, sp, vp, lfs_match_indir);
695 lfs_gather(fs, sp, vp, lfs_match_dindir);
696 lfs_gather(fs, sp, vp, lfs_match_tindir);
697 }
698 fip = sp->fip;
699 if (fip->fi_nblocks != 0) {
700 sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
701 sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
702 sp->start_lbp = &sp->fip->fi_blocks[0];
703 } else {
704 sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
705 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
706 }
707 }
708
709 int
710 lfs_writeinode(fs, sp, ip)
711 struct lfs *fs;
712 struct segment *sp;
713 struct inode *ip;
714 {
715 struct buf *bp, *ibp;
716 struct dinode *cdp;
717 IFILE *ifp;
718 SEGUSE *sup;
719 ufs_daddr_t daddr;
720 daddr_t *daddrp;
721 ino_t ino;
722 int error, i, ndx;
723 int redo_ifile = 0;
724 struct timespec ts;
725 int gotblk=0;
726
727 if (!(ip->i_flag & IN_ALLMOD))
728 return(0);
729
730 /* Allocate a new inode block if necessary. */
731 if ((ip->i_number != LFS_IFILE_INUM || sp->idp==NULL) && sp->ibp == NULL) {
732 /* Allocate a new segment if necessary. */
733 if (sp->seg_bytes_left < fs->lfs_bsize ||
734 sp->sum_bytes_left < sizeof(ufs_daddr_t))
735 (void) lfs_writeseg(fs, sp);
736
737 /* Get next inode block. */
738 daddr = fs->lfs_offset;
739 fs->lfs_offset += fsbtodb(fs, 1);
740 sp->ibp = *sp->cbpp++ =
741 getblk(VTOI(fs->lfs_ivnode)->i_devvp, daddr, fs->lfs_bsize, 0, 0);
742 gotblk++;
743
744 /* Zero out inode numbers */
745 for (i = 0; i < INOPB(fs); ++i)
746 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
747
748 ++sp->start_bpp;
749 fs->lfs_avail -= fsbtodb(fs, 1);
750 /* Set remaining space counters. */
751 sp->seg_bytes_left -= fs->lfs_bsize;
752 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
753 ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
754 sp->ninodes / INOPB(fs) - 1;
755 ((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
756 }
757
758 /* Update the inode times and copy the inode onto the inode page. */
759 TIMEVAL_TO_TIMESPEC(&time, &ts);
760 LFS_ITIMES(ip, &ts, &ts, &ts);
761
762 /*
763 * If this is the Ifile, and we've already written the Ifile in this
764 * partial segment, just overwrite it (it's not on disk yet) and
765 * continue.
766 *
767 * XXX we know that the bp that we get the second time around has
768 * already been gathered.
769 */
770 if(ip->i_number == LFS_IFILE_INUM && sp->idp) {
771 *(sp->idp) = ip->i_din.ffs_din;
772 return 0;
773 }
774
775 bp = sp->ibp;
776 cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
777 *cdp = ip->i_din.ffs_din;
778
779 /*
780 * If we are cleaning, ensure that we don't write UNWRITTEN disk
781 * addresses to disk.
782 */
783 if (ip->i_lfs_effnblks != ip->i_ffs_blocks) {
784 #ifdef DEBUG_LFS
785 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
786 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks);
787 #endif
788 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
789 daddrp++) {
790 if (*daddrp == UNWRITTEN) {
791 #ifdef DEBUG_LFS
792 printf("lfs_writeinode: wiping UNWRITTEN\n");
793 #endif
794 *daddrp = 0;
795 }
796 }
797 }
798
799 if(ip->i_flag & IN_CLEANING)
800 LFS_CLR_UINO(ip, IN_CLEANING);
801 else {
802 /* XXX IN_ALLMOD */
803 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
804 IN_UPDATE);
805 if (ip->i_lfs_effnblks == ip->i_ffs_blocks)
806 LFS_CLR_UINO(ip, IN_MODIFIED);
807 }
808
809 if(ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
810 sp->idp = ((struct dinode *)bp->b_data) +
811 (sp->ninodes % INOPB(fs));
812 if(gotblk) {
813 bp->b_flags |= B_LOCKED;
814 brelse(bp);
815 }
816
817 /* Increment inode count in segment summary block. */
818 ++((SEGSUM *)(sp->segsum))->ss_ninos;
819
820 /* If this page is full, set flag to allocate a new page. */
821 if (++sp->ninodes % INOPB(fs) == 0)
822 sp->ibp = NULL;
823
824 /*
825 * If updating the ifile, update the super-block. Update the disk
826 * address and access times for this inode in the ifile.
827 */
828 ino = ip->i_number;
829 if (ino == LFS_IFILE_INUM) {
830 daddr = fs->lfs_idaddr;
831 fs->lfs_idaddr = bp->b_blkno;
832 } else {
833 LFS_IENTRY(ifp, fs, ino, ibp);
834 daddr = ifp->if_daddr;
835 ifp->if_daddr = bp->b_blkno;
836 #ifdef LFS_DEBUG_NEXTFREE
837 if(ino > 3 && ifp->if_nextfree) {
838 vprint("lfs_writeinode",ITOV(ip));
839 printf("lfs_writeinode: updating free ino %d\n",
840 ip->i_number);
841 }
842 #endif
843 error = VOP_BWRITE(ibp); /* Ifile */
844 }
845
846 /*
847 * Account the inode: it no longer belongs to its former segment,
848 * though it will not belong to the new segment until that segment
849 * is actually written.
850 */
851 #ifdef DEBUG
852 /*
853 * The inode's last address should not be in the current partial
854 * segment, except under exceptional circumstances (lfs_writevnodes
855 * had to start over, and in the meantime more blocks were written
856 * to a vnode). Although the previous inode won't be accounted in
857 * su_nbytes until lfs_writeseg, this shouldn't be a problem as we
858 * have more data blocks in the current partial segment.
859 */
860 if (daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)
861 printf("lfs_writeinode: last inode addr in current pseg "
862 "(ino %d daddr 0x%x)\n", ino, daddr);
863 #endif
864 if (daddr != LFS_UNUSED_DADDR) {
865 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
866 #ifdef DIAGNOSTIC
867 if (sup->su_nbytes < DINODE_SIZE) {
868 printf("lfs_writeinode: negative bytes "
869 "(segment %d short by %d)\n",
870 datosn(fs, daddr),
871 (int)DINODE_SIZE - sup->su_nbytes);
872 panic("lfs_writeinode: negative bytes");
873 sup->su_nbytes = DINODE_SIZE;
874 }
875 #endif
876 sup->su_nbytes -= DINODE_SIZE;
877 redo_ifile =
878 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
879 error = VOP_BWRITE(bp); /* Ifile */
880 }
881 return (redo_ifile);
882 }
883
884 int
885 lfs_gatherblock(sp, bp, sptr)
886 struct segment *sp;
887 struct buf *bp;
888 int *sptr;
889 {
890 struct lfs *fs;
891 int version;
892
893 /*
894 * If full, finish this segment. We may be doing I/O, so
895 * release and reacquire the splbio().
896 */
897 #ifdef DIAGNOSTIC
898 if (sp->vp == NULL)
899 panic ("lfs_gatherblock: Null vp in segment");
900 #endif
901 fs = sp->fs;
902 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
903 sp->seg_bytes_left < bp->b_bcount) {
904 if (sptr)
905 splx(*sptr);
906 lfs_updatemeta(sp);
907
908 version = sp->fip->fi_version;
909 (void) lfs_writeseg(fs, sp);
910
911 sp->fip->fi_version = version;
912 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
913 /* Add the current file to the segment summary. */
914 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
915 sp->sum_bytes_left -=
916 sizeof(struct finfo) - sizeof(ufs_daddr_t);
917
918 if (sptr)
919 *sptr = splbio();
920 return(1);
921 }
922
923 #ifdef DEBUG
924 if(bp->b_flags & B_GATHERED) {
925 printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
926 sp->fip->fi_ino, bp->b_lblkno);
927 return(0);
928 }
929 #endif
930 /* Insert into the buffer list, update the FINFO block. */
931 bp->b_flags |= B_GATHERED;
932 *sp->cbpp++ = bp;
933 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
934
935 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
936 sp->seg_bytes_left -= bp->b_bcount;
937 return(0);
938 }
939
940 int
941 lfs_gather(fs, sp, vp, match)
942 struct lfs *fs;
943 struct segment *sp;
944 struct vnode *vp;
945 int (*match) __P((struct lfs *, struct buf *));
946 {
947 struct buf *bp;
948 int s, count=0;
949
950 sp->vp = vp;
951 s = splbio();
952
953 #ifndef LFS_NO_BACKBUF_HACK
954 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
955 #else /* LFS_NO_BACKBUF_HACK */
956 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
957 # define BUF_OFFSET (((void *)&bp->b_vnbufs.le_next) - (void *)bp)
958 # define BACK_BUF(BP) ((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
959 # define BEG_OF_LIST ((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
960 /* Find last buffer. */
961 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
962 bp = bp->b_vnbufs.le_next);
963 for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
964 #endif /* LFS_NO_BACKBUF_HACK */
965 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
966 continue;
967 if(vp->v_type == VBLK) {
968 /* For block devices, just write the blocks. */
969 /* XXX Do we really need to even do this? */
970 #ifdef DEBUG_LFS
971 if(count==0)
972 printf("BLK(");
973 printf(".");
974 #endif
975 /* Get the block before bwrite, so we don't corrupt the free list */
976 bp->b_flags |= B_BUSY;
977 bremfree(bp);
978 bwrite(bp);
979 } else {
980 #ifdef DIAGNOSTIC
981 if ((bp->b_flags & (B_CALL|B_INVAL))==B_INVAL) {
982 printf("lfs_gather: lbn %d is B_INVAL\n",
983 bp->b_lblkno);
984 VOP_PRINT(bp->b_vp);
985 }
986 if (!(bp->b_flags & B_DELWRI))
987 panic("lfs_gather: bp not B_DELWRI");
988 if (!(bp->b_flags & B_LOCKED)) {
989 printf("lfs_gather: lbn %d blk %d"
990 " not B_LOCKED\n", bp->b_lblkno,
991 bp->b_blkno);
992 VOP_PRINT(bp->b_vp);
993 panic("lfs_gather: bp not B_LOCKED");
994 }
995 #endif
996 if (lfs_gatherblock(sp, bp, &s)) {
997 goto loop;
998 }
999 }
1000 count++;
1001 }
1002 splx(s);
1003 #ifdef DEBUG_LFS
1004 if(vp->v_type == VBLK && count)
1005 printf(")\n");
1006 #endif
1007 lfs_updatemeta(sp);
1008 sp->vp = NULL;
1009 return count;
1010 }
1011
1012 /*
1013 * Update the metadata that points to the blocks listed in the FINFO
1014 * array.
1015 */
1016 void
1017 lfs_updatemeta(sp)
1018 struct segment *sp;
1019 {
1020 SEGUSE *sup;
1021 struct buf *bp;
1022 struct lfs *fs;
1023 struct vnode *vp;
1024 struct indir a[NIADDR + 2], *ap;
1025 struct inode *ip;
1026 ufs_daddr_t daddr, lbn, off;
1027 daddr_t ooff;
1028 int error, i, nblocks, num;
1029 int bb;
1030
1031 vp = sp->vp;
1032 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1033 if (nblocks < 0)
1034 panic("This is a bad thing\n");
1035 if (vp == NULL || nblocks == 0)
1036 return;
1037
1038 /* Sort the blocks. */
1039 /*
1040 * XXX KS - We have to sort even if the blocks come from the
1041 * cleaner, because there might be other pending blocks on the
1042 * same inode...and if we don't sort, and there are fragments
1043 * present, blocks may be written in the wrong place.
1044 */
1045 /* if (!(sp->seg_flags & SEGM_CLEAN)) */
1046 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
1047
1048 /*
1049 * Record the length of the last block in case it's a fragment.
1050 * If there are indirect blocks present, they sort last. An
1051 * indirect block will be lfs_bsize and its presence indicates
1052 * that you cannot have fragments.
1053 */
1054 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
1055
1056 /*
1057 * Assign disk addresses, and update references to the logical
1058 * block and the segment usage information.
1059 */
1060 fs = sp->fs;
1061 for (i = nblocks; i--; ++sp->start_bpp) {
1062 lbn = *sp->start_lbp++;
1063
1064 (*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
1065 if((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) {
1066 printf("lfs_updatemeta: ino %d blk %d"
1067 " has same lbn and daddr\n",
1068 VTOI(vp)->i_number, off);
1069 }
1070 bb = fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
1071 fs->lfs_offset += bb;
1072 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
1073 if (error)
1074 panic("lfs_updatemeta: ufs_bmaparray %d", error);
1075 ip = VTOI(vp);
1076 switch (num) {
1077 case 0:
1078 ooff = ip->i_ffs_db[lbn];
1079 #ifdef DEBUG
1080 if (ooff == 0) {
1081 printf("lfs_updatemeta[1]: warning: writing "
1082 "ino %d lbn %d at 0x%x, was 0x0\n",
1083 ip->i_number, lbn, off);
1084 }
1085 #endif
1086 if (ooff == UNWRITTEN)
1087 ip->i_ffs_blocks += bb;
1088 ip->i_ffs_db[lbn] = off;
1089 break;
1090 case 1:
1091 ooff = ip->i_ffs_ib[a[0].in_off];
1092 #ifdef DEBUG
1093 if (ooff == 0) {
1094 printf("lfs_updatemeta[2]: warning: writing "
1095 "ino %d lbn %d at 0x%x, was 0x0\n",
1096 ip->i_number, lbn, off);
1097 }
1098 #endif
1099 if (ooff == UNWRITTEN)
1100 ip->i_ffs_blocks += bb;
1101 ip->i_ffs_ib[a[0].in_off] = off;
1102 break;
1103 default:
1104 ap = &a[num - 1];
1105 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1106 panic("lfs_updatemeta: bread bno %d",
1107 ap->in_lbn);
1108
1109 ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off];
1110 #if DEBUG
1111 if (ooff == 0) {
1112 printf("lfs_updatemeta[3]: warning: writing "
1113 "ino %d lbn %d at 0x%x, was 0x0\n",
1114 ip->i_number, lbn, off);
1115 }
1116 #endif
1117 if (ooff == UNWRITTEN)
1118 ip->i_ffs_blocks += bb;
1119 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
1120 (void) VOP_BWRITE(bp);
1121 }
1122 #ifdef DEBUG
1123 if (daddr >= fs->lfs_lastpseg && daddr <= off) {
1124 printf("lfs_updatemeta: ino %d, lbn %d, addr = %x "
1125 "in same pseg\n", VTOI(sp->vp)->i_number,
1126 (*sp->start_bpp)->b_lblkno, daddr);
1127 }
1128 #endif
1129 /* Update segment usage information. */
1130 if (daddr > 0) {
1131 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
1132 #ifdef DIAGNOSTIC
1133 if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
1134 /* XXX -- Change to a panic. */
1135 printf("lfs_updatemeta: negative bytes "
1136 "(segment %d short by %ld)\n",
1137 datosn(fs, daddr),
1138 (*sp->start_bpp)->b_bcount -
1139 sup->su_nbytes);
1140 printf("lfs_updatemeta: ino %d, lbn %d, "
1141 "addr = %x\n", VTOI(sp->vp)->i_number,
1142 (*sp->start_bpp)->b_lblkno, daddr);
1143 panic("lfs_updatemeta: negative bytes");
1144 sup->su_nbytes = (*sp->start_bpp)->b_bcount;
1145 }
1146 #endif
1147 sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
1148 error = VOP_BWRITE(bp); /* Ifile */
1149 }
1150 }
1151 }
1152
1153 /*
1154 * Start a new segment.
1155 */
1156 int
1157 lfs_initseg(fs)
1158 struct lfs *fs;
1159 {
1160 struct segment *sp;
1161 SEGUSE *sup;
1162 SEGSUM *ssp;
1163 struct buf *bp;
1164 int repeat;
1165
1166 sp = fs->lfs_sp;
1167
1168 repeat = 0;
1169 /* Advance to the next segment. */
1170 if (!LFS_PARTIAL_FITS(fs)) {
1171 /* lfs_avail eats the remaining space */
1172 fs->lfs_avail -= fs->lfs_dbpseg - (fs->lfs_offset -
1173 fs->lfs_curseg);
1174 /* Wake up any cleaning procs waiting on this file system. */
1175 wakeup(&lfs_allclean_wakeup);
1176 wakeup(&fs->lfs_nextseg);
1177 lfs_newseg(fs);
1178 repeat = 1;
1179 fs->lfs_offset = fs->lfs_curseg;
1180 sp->seg_number = datosn(fs, fs->lfs_curseg);
1181 sp->seg_bytes_left = dbtob(fs->lfs_dbpseg);
1182 /*
1183 * If the segment contains a superblock, update the offset
1184 * and summary address to skip over it.
1185 */
1186 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1187 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1188 fs->lfs_offset += btodb(LFS_SBPAD);
1189 sp->seg_bytes_left -= LFS_SBPAD;
1190 }
1191 brelse(bp);
1192 } else {
1193 sp->seg_number = datosn(fs, fs->lfs_curseg);
1194 sp->seg_bytes_left = dbtob(fs->lfs_dbpseg -
1195 (fs->lfs_offset - fs->lfs_curseg));
1196 }
1197 fs->lfs_lastpseg = fs->lfs_offset;
1198
1199 sp->fs = fs;
1200 sp->ibp = NULL;
1201 sp->idp = NULL;
1202 sp->ninodes = 0;
1203
1204 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
1205 sp->cbpp = sp->bpp;
1206 *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
1207 fs->lfs_offset, LFS_SUMMARY_SIZE);
1208 sp->segsum = (*sp->cbpp)->b_data;
1209 bzero(sp->segsum, LFS_SUMMARY_SIZE);
1210 sp->start_bpp = ++sp->cbpp;
1211 fs->lfs_offset += btodb(LFS_SUMMARY_SIZE);
1212
1213 /* Set point to SEGSUM, initialize it. */
1214 ssp = sp->segsum;
1215 ssp->ss_next = fs->lfs_nextseg;
1216 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1217 ssp->ss_magic = SS_MAGIC;
1218
1219 /* Set pointer to first FINFO, initialize it. */
1220 sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
1221 sp->fip->fi_nblocks = 0;
1222 sp->start_lbp = &sp->fip->fi_blocks[0];
1223 sp->fip->fi_lastlength = 0;
1224
1225 sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
1226 sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
1227
1228 return(repeat);
1229 }
1230
1231 /*
1232 * Return the next segment to write.
1233 */
1234 void
1235 lfs_newseg(fs)
1236 struct lfs *fs;
1237 {
1238 CLEANERINFO *cip;
1239 SEGUSE *sup;
1240 struct buf *bp;
1241 int curseg, isdirty, sn;
1242
1243 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
1244 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1245 sup->su_nbytes = 0;
1246 sup->su_nsums = 0;
1247 sup->su_ninos = 0;
1248 (void) VOP_BWRITE(bp); /* Ifile */
1249
1250 LFS_CLEANERINFO(cip, fs, bp);
1251 --cip->clean;
1252 ++cip->dirty;
1253 fs->lfs_nclean = cip->clean;
1254 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1255
1256 fs->lfs_lastseg = fs->lfs_curseg;
1257 fs->lfs_curseg = fs->lfs_nextseg;
1258 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
1259 sn = (sn + 1) % fs->lfs_nseg;
1260 if (sn == curseg)
1261 panic("lfs_nextseg: no clean segments");
1262 LFS_SEGENTRY(sup, fs, sn, bp);
1263 isdirty = sup->su_flags & SEGUSE_DIRTY;
1264 brelse(bp);
1265 if (!isdirty)
1266 break;
1267 }
1268
1269 ++fs->lfs_nactive;
1270 fs->lfs_nextseg = sntoda(fs, sn);
1271 if(lfs_dostats) {
1272 ++lfs_stats.segsused;
1273 }
1274 }
1275
1276 int
1277 lfs_writeseg(fs, sp)
1278 struct lfs *fs;
1279 struct segment *sp;
1280 {
1281 extern int locked_queue_count;
1282 extern long locked_queue_bytes;
1283 struct buf **bpp, *bp, *cbp, *newbp;
1284 SEGUSE *sup;
1285 SEGSUM *ssp;
1286 dev_t i_dev;
1287 u_long *datap, *dp;
1288 int do_again, i, nblocks, s;
1289 #ifdef LFS_TRACK_IOS
1290 int j;
1291 #endif
1292 int (*strategy)__P((void *));
1293 struct vop_strategy_args vop_strategy_a;
1294 u_short ninos;
1295 struct vnode *devvp;
1296 char *p;
1297 struct vnode *vn;
1298 struct inode *ip;
1299 daddr_t *daddrp;
1300 int changed;
1301 #if defined(DEBUG) && defined(LFS_PROPELLER)
1302 static int propeller;
1303 char propstring[4] = "-\\|/";
1304
1305 printf("%c\b",propstring[propeller++]);
1306 if(propeller==4)
1307 propeller = 0;
1308 #endif
1309
1310 /*
1311 * If there are no buffers other than the segment summary to write
1312 * and it is not a checkpoint, don't do anything. On a checkpoint,
1313 * even if there aren't any buffers, you need to write the superblock.
1314 */
1315 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1316 return (0);
1317
1318 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1319 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1320
1321 /* Update the segment usage information. */
1322 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1323
1324 /* Loop through all blocks, except the segment summary. */
1325 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1326 if((*bpp)->b_vp != devvp)
1327 sup->su_nbytes += (*bpp)->b_bcount;
1328 }
1329
1330 ssp = (SEGSUM *)sp->segsum;
1331
1332 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1333 sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
1334 /* sup->su_nbytes += LFS_SUMMARY_SIZE; */
1335 sup->su_lastmod = time.tv_sec;
1336 sup->su_ninos += ninos;
1337 ++sup->su_nsums;
1338 fs->lfs_dmeta += (btodb(LFS_SUMMARY_SIZE) + fsbtodb(fs, ninos));
1339 fs->lfs_avail -= btodb(LFS_SUMMARY_SIZE);
1340
1341 do_again = !(bp->b_flags & B_GATHERED);
1342 (void)VOP_BWRITE(bp); /* Ifile */
1343 /*
1344 * Mark blocks B_BUSY, to prevent then from being changed between
1345 * the checksum computation and the actual write.
1346 *
1347 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1348 * there are any, replace them with copies that have UNASSIGNED
1349 * instead.
1350 */
1351 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1352 ++bpp;
1353 if((*bpp)->b_flags & B_CALL)
1354 continue;
1355 bp = *bpp;
1356 again:
1357 s = splbio();
1358 if(bp->b_flags & B_BUSY) {
1359 #ifdef DEBUG
1360 printf("lfs_writeseg: avoiding potential data "
1361 "summary corruption for ino %d, lbn %d\n",
1362 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
1363 #endif
1364 bp->b_flags |= B_WANTED;
1365 tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
1366 splx(s);
1367 goto again;
1368 }
1369 bp->b_flags |= B_BUSY;
1370 splx(s);
1371 /* Check and replace indirect block UNWRITTEN bogosity */
1372 if(bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1373 VTOI(bp->b_vp)->i_ffs_blocks !=
1374 VTOI(bp->b_vp)->i_lfs_effnblks) {
1375 #ifdef DEBUG_LFS
1376 printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
1377 VTOI(bp->b_vp)->i_number,
1378 VTOI(bp->b_vp)->i_lfs_effnblks,
1379 VTOI(bp->b_vp)->i_ffs_blocks);
1380 #endif
1381 /* Make a copy we'll make changes to */
1382 newbp = lfs_newbuf(bp->b_vp, bp->b_lblkno,
1383 bp->b_bcount);
1384 newbp->b_blkno = bp->b_blkno;
1385 memcpy(newbp->b_data, bp->b_data,
1386 newbp->b_bcount);
1387 *bpp = newbp;
1388
1389 changed = 0;
1390 for (daddrp = (daddr_t *)(newbp->b_data);
1391 daddrp < (daddr_t *)(newbp->b_data +
1392 newbp->b_bcount); daddrp++) {
1393 if (*daddrp == UNWRITTEN) {
1394 ++changed;
1395 #ifdef DEBUG_LFS
1396 printf("lfs_writeseg: replacing UNWRITTEN\n");
1397 #endif
1398 *daddrp = 0;
1399 }
1400 }
1401 /*
1402 * Get rid of the old buffer. Don't mark it clean,
1403 * though, if it still has dirty data on it.
1404 */
1405 if (changed) {
1406 bp->b_flags &= ~(B_ERROR | B_GATHERED);
1407 if (bp->b_flags & B_CALL)
1408 lfs_freebuf(bp);
1409 else {
1410 /* Still on free list, leave it there */
1411 s = splbio();
1412 bp->b_flags &= ~B_BUSY;
1413 if (bp->b_flags & B_WANTED)
1414 wakeup(bp);
1415 splx(s);
1416 }
1417 } else {
1418 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1419 B_LOCKED | B_GATHERED);
1420 if (bp->b_flags & B_CALL)
1421 lfs_freebuf(bp);
1422 else {
1423 bremfree(bp);
1424 bp->b_flags |= B_DONE;
1425 reassignbuf(bp, bp->b_vp);
1426 brelse(bp);
1427 }
1428 }
1429
1430 }
1431 }
1432 /*
1433 * Compute checksum across data and then across summary; the first
1434 * block (the summary block) is skipped. Set the create time here
1435 * so that it's guaranteed to be later than the inode mod times.
1436 *
1437 * XXX
1438 * Fix this to do it inline, instead of malloc/copy.
1439 */
1440 datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
1441 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1442 if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1443 if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
1444 panic("lfs_writeseg: copyin failed [1]: "
1445 "ino %d blk %d",
1446 VTOI((*bpp)->b_vp)->i_number,
1447 (*bpp)->b_lblkno);
1448 } else
1449 *dp++ = ((u_long *)(*bpp)->b_data)[0];
1450 }
1451 ssp->ss_create = time.tv_sec;
1452 ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
1453 ssp->ss_sumsum =
1454 cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
1455 free(datap, M_SEGMENT);
1456
1457 fs->lfs_bfree -= (fsbtodb(fs, ninos) + btodb(LFS_SUMMARY_SIZE));
1458
1459 strategy = devvp->v_op[VOFFSET(vop_strategy)];
1460
1461 /*
1462 * When we simply write the blocks we lose a rotation for every block
1463 * written. To avoid this problem, we allocate memory in chunks, copy
1464 * the buffers into the chunk and write the chunk. CHUNKSIZE is the
1465 * largest size I/O devices can handle.
1466 * When the data is copied to the chunk, turn off the B_LOCKED bit
1467 * and brelse the buffer (which will move them to the LRU list). Add
1468 * the B_CALL flag to the buffer header so we can count I/O's for the
1469 * checkpoints and so we can release the allocated memory.
1470 *
1471 * XXX
1472 * This should be removed if the new virtual memory system allows us to
1473 * easily make the buffers contiguous in kernel memory and if that's
1474 * fast enough.
1475 */
1476
1477 #define CHUNKSIZE MAXPHYS
1478
1479 if(devvp==NULL)
1480 panic("devvp is NULL");
1481 for (bpp = sp->bpp,i = nblocks; i;) {
1482 cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE);
1483 cbp->b_dev = i_dev;
1484 cbp->b_flags |= B_ASYNC | B_BUSY;
1485 cbp->b_bcount = 0;
1486
1487 #ifdef DIAGNOSTIC
1488 if(datosn(fs, (*bpp)->b_blkno + btodb((*bpp)->b_bcount) - 1) !=
1489 datosn(fs, cbp->b_blkno)) {
1490 panic("lfs_writeseg: Segment overwrite");
1491 }
1492 #endif
1493
1494 s = splbio();
1495 if(fs->lfs_iocount >= LFS_THROTTLE) {
1496 tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
1497 }
1498 ++fs->lfs_iocount;
1499 #ifdef LFS_TRACK_IOS
1500 for(j=0;j<LFS_THROTTLE;j++) {
1501 if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) {
1502 fs->lfs_pending[j] = cbp->b_blkno;
1503 break;
1504 }
1505 }
1506 #endif /* LFS_TRACK_IOS */
1507 for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
1508 bp = *bpp;
1509
1510 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1511 break;
1512
1513 /*
1514 * Fake buffers from the cleaner are marked as B_INVAL.
1515 * We need to copy the data from user space rather than
1516 * from the buffer indicated.
1517 * XXX == what do I do on an error?
1518 */
1519 if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1520 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1521 panic("lfs_writeseg: copyin failed [2]");
1522 } else
1523 bcopy(bp->b_data, p, bp->b_bcount);
1524 p += bp->b_bcount;
1525 cbp->b_bcount += bp->b_bcount;
1526 if (bp->b_flags & B_LOCKED) {
1527 --locked_queue_count;
1528 locked_queue_bytes -= bp->b_bufsize;
1529 }
1530 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1531 B_LOCKED | B_GATHERED);
1532 vn = bp->b_vp;
1533 if (bp->b_flags & B_CALL) {
1534 /* if B_CALL, it was created with newbuf */
1535 lfs_freebuf(bp);
1536 } else {
1537 bremfree(bp);
1538 bp->b_flags |= B_DONE;
1539 if(vn)
1540 reassignbuf(bp, vn);
1541 brelse(bp);
1542 }
1543 if(bp->b_flags & B_NEEDCOMMIT) { /* XXX */
1544 bp->b_flags &= ~B_NEEDCOMMIT;
1545 wakeup(bp);
1546 }
1547
1548 bpp++;
1549
1550 /*
1551 * If this is the last block for this vnode, but
1552 * there are other blocks on its dirty list,
1553 * set IN_MODIFIED/IN_CLEANING depending on what
1554 * sort of block. Only do this for our mount point,
1555 * not for, e.g., inode blocks that are attached to
1556 * the devvp.
1557 */
1558 if(i>1 && vn && *bpp && (*bpp)->b_vp != vn
1559 && (*bpp)->b_vp && (bp=vn->v_dirtyblkhd.lh_first)!=NULL &&
1560 vn->v_mount == fs->lfs_ivnode->v_mount)
1561 {
1562 ip = VTOI(vn);
1563 #ifdef DEBUG_LFS
1564 printf("lfs_writeseg: marking ino %d\n",ip->i_number);
1565 #endif
1566 if(bp->b_flags & B_CALL)
1567 LFS_SET_UINO(ip, IN_CLEANING);
1568 else
1569 LFS_SET_UINO(ip, IN_MODIFIED);
1570 }
1571 /* if(vn->v_dirtyblkhd.lh_first == NULL) */
1572 wakeup(vn);
1573 }
1574 ++cbp->b_vp->v_numoutput;
1575 splx(s);
1576 /*
1577 * XXXX This is a gross and disgusting hack. Since these
1578 * buffers are physically addressed, they hang off the
1579 * device vnode (devvp). As a result, they have no way
1580 * of getting to the LFS superblock or lfs structure to
1581 * keep track of the number of I/O's pending. So, I am
1582 * going to stuff the fs into the saveaddr field of
1583 * the buffer (yuk).
1584 */
1585 cbp->b_saveaddr = (caddr_t)fs;
1586 vop_strategy_a.a_desc = VDESC(vop_strategy);
1587 vop_strategy_a.a_bp = cbp;
1588 (strategy)(&vop_strategy_a);
1589 }
1590 /*
1591 * XXX
1592 * Vinvalbuf can move locked buffers off the locked queue
1593 * and we have no way of knowing about this. So, after
1594 * doing a big write, we recalculate how many buffers are
1595 * really still left on the locked queue.
1596 */
1597 s = splbio();
1598 lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
1599 splx(s);
1600 wakeup(&locked_queue_count);
1601 if(lfs_dostats) {
1602 ++lfs_stats.psegwrites;
1603 lfs_stats.blocktot += nblocks - 1;
1604 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
1605 ++lfs_stats.psyncwrites;
1606 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
1607 ++lfs_stats.pcleanwrites;
1608 lfs_stats.cleanblocks += nblocks - 1;
1609 }
1610 }
1611 return (lfs_initseg(fs) || do_again);
1612 }
1613
1614 void
1615 lfs_writesuper(fs, daddr)
1616 struct lfs *fs;
1617 daddr_t daddr;
1618 {
1619 struct buf *bp;
1620 dev_t i_dev;
1621 int (*strategy) __P((void *));
1622 int s;
1623 struct vop_strategy_args vop_strategy_a;
1624
1625 #ifdef LFS_CANNOT_ROLLFW
1626 /*
1627 * If we can write one superblock while another is in
1628 * progress, we risk not having a complete checkpoint if we crash.
1629 * So, block here if a superblock write is in progress.
1630 */
1631 s = splbio();
1632 while(fs->lfs_sbactive) {
1633 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
1634 }
1635 fs->lfs_sbactive = daddr;
1636 splx(s);
1637 #endif
1638 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1639 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
1640
1641 /* Set timestamp of this version of the superblock */
1642 fs->lfs_tstamp = time.tv_sec;
1643
1644 /* Checksum the superblock and copy it into a buffer. */
1645 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1646 bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD);
1647 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
1648
1649 bp->b_dev = i_dev;
1650 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
1651 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
1652 bp->b_iodone = lfs_supercallback;
1653 /* XXX KS - same nasty hack as above */
1654 bp->b_saveaddr = (caddr_t)fs;
1655
1656 vop_strategy_a.a_desc = VDESC(vop_strategy);
1657 vop_strategy_a.a_bp = bp;
1658 s = splbio();
1659 ++bp->b_vp->v_numoutput;
1660 ++fs->lfs_iocount;
1661 splx(s);
1662 (strategy)(&vop_strategy_a);
1663 }
1664
1665 /*
1666 * Logical block number match routines used when traversing the dirty block
1667 * chain.
1668 */
1669 int
1670 lfs_match_fake(fs, bp)
1671 struct lfs *fs;
1672 struct buf *bp;
1673 {
1674 return (bp->b_flags & B_CALL);
1675 }
1676
1677 int
1678 lfs_match_data(fs, bp)
1679 struct lfs *fs;
1680 struct buf *bp;
1681 {
1682 return (bp->b_lblkno >= 0);
1683 }
1684
1685 int
1686 lfs_match_indir(fs, bp)
1687 struct lfs *fs;
1688 struct buf *bp;
1689 {
1690 int lbn;
1691
1692 lbn = bp->b_lblkno;
1693 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
1694 }
1695
1696 int
1697 lfs_match_dindir(fs, bp)
1698 struct lfs *fs;
1699 struct buf *bp;
1700 {
1701 int lbn;
1702
1703 lbn = bp->b_lblkno;
1704 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
1705 }
1706
1707 int
1708 lfs_match_tindir(fs, bp)
1709 struct lfs *fs;
1710 struct buf *bp;
1711 {
1712 int lbn;
1713
1714 lbn = bp->b_lblkno;
1715 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
1716 }
1717
1718 /*
1719 * XXX - The only buffers that are going to hit these functions are the
1720 * segment write blocks, or the segment summaries, or the superblocks.
1721 *
1722 * All of the above are created by lfs_newbuf, and so do not need to be
1723 * released via brelse.
1724 */
1725 void
1726 lfs_callback(bp)
1727 struct buf *bp;
1728 {
1729 struct lfs *fs;
1730 #ifdef LFS_TRACK_IOS
1731 int j;
1732 #endif
1733
1734 fs = (struct lfs *)bp->b_saveaddr;
1735 #ifdef DIAGNOSTIC
1736 if (fs->lfs_iocount == 0)
1737 panic("lfs_callback: zero iocount\n");
1738 #endif
1739 if (--fs->lfs_iocount < LFS_THROTTLE)
1740 wakeup(&fs->lfs_iocount);
1741 #ifdef LFS_TRACK_IOS
1742 for(j=0;j<LFS_THROTTLE;j++) {
1743 if(fs->lfs_pending[j]==bp->b_blkno) {
1744 fs->lfs_pending[j] = LFS_UNUSED_DADDR;
1745 wakeup(&(fs->lfs_pending[j]));
1746 break;
1747 }
1748 }
1749 #endif /* LFS_TRACK_IOS */
1750
1751 lfs_freebuf(bp);
1752 }
1753
1754 void
1755 lfs_supercallback(bp)
1756 struct buf *bp;
1757 {
1758 struct lfs *fs;
1759
1760 fs = (struct lfs *)bp->b_saveaddr;
1761 #ifdef LFS_CANNOT_ROLLFW
1762 fs->lfs_sbactive = 0;
1763 wakeup(&fs->lfs_sbactive);
1764 #endif
1765 if (--fs->lfs_iocount < LFS_THROTTLE)
1766 wakeup(&fs->lfs_iocount);
1767 lfs_freebuf(bp);
1768 }
1769
1770 /*
1771 * Shellsort (diminishing increment sort) from Data Structures and
1772 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
1773 * see also Knuth Vol. 3, page 84. The increments are selected from
1774 * formula (8), page 95. Roughly O(N^3/2).
1775 */
1776 /*
1777 * This is our own private copy of shellsort because we want to sort
1778 * two parallel arrays (the array of buffer pointers and the array of
1779 * logical block numbers) simultaneously. Note that we cast the array
1780 * of logical block numbers to a unsigned in this routine so that the
1781 * negative block numbers (meta data blocks) sort AFTER the data blocks.
1782 */
1783
1784 void
1785 lfs_shellsort(bp_array, lb_array, nmemb)
1786 struct buf **bp_array;
1787 ufs_daddr_t *lb_array;
1788 int nmemb;
1789 {
1790 static int __rsshell_increments[] = { 4, 1, 0 };
1791 int incr, *incrp, t1, t2;
1792 struct buf *bp_temp;
1793 u_long lb_temp;
1794
1795 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
1796 for (t1 = incr; t1 < nmemb; ++t1)
1797 for (t2 = t1 - incr; t2 >= 0;)
1798 if (lb_array[t2] > lb_array[t2 + incr]) {
1799 lb_temp = lb_array[t2];
1800 lb_array[t2] = lb_array[t2 + incr];
1801 lb_array[t2 + incr] = lb_temp;
1802 bp_temp = bp_array[t2];
1803 bp_array[t2] = bp_array[t2 + incr];
1804 bp_array[t2 + incr] = bp_temp;
1805 t2 -= incr;
1806 } else
1807 break;
1808 }
1809
1810 /*
1811 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
1812 */
1813 int
1814 lfs_vref(vp)
1815 struct vnode *vp;
1816 {
1817 /*
1818 * If we return 1 here during a flush, we risk vinvalbuf() not
1819 * being able to flush all of the pages from this vnode, which
1820 * will cause it to panic. So, return 0 if a flush is in progress.
1821 */
1822 if (vp->v_flag & VXLOCK) {
1823 if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1824 return 0;
1825 }
1826 return(1);
1827 }
1828 return (vget(vp, 0));
1829 }
1830
1831 /*
1832 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
1833 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
1834 */
1835 void
1836 lfs_vunref(vp)
1837 struct vnode *vp;
1838 {
1839 /*
1840 * Analogous to lfs_vref, if the node is flushing, fake it.
1841 */
1842 if((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1843 return;
1844 }
1845
1846 simple_lock(&vp->v_interlock);
1847 #ifdef DIAGNOSTIC
1848 if(vp->v_usecount<=0) {
1849 printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
1850 printf("lfs_vunref: flags are 0x%lx\n", vp->v_flag);
1851 printf("lfs_vunref: usecount = %ld\n", vp->v_usecount);
1852 panic("lfs_vunref: v_usecount<0");
1853 }
1854 #endif
1855 vp->v_usecount--;
1856 if (vp->v_usecount > 0) {
1857 simple_unlock(&vp->v_interlock);
1858 return;
1859 }
1860 /*
1861 * insert at tail of LRU list
1862 */
1863 simple_lock(&vnode_free_list_slock);
1864 if (vp->v_holdcnt > 0)
1865 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
1866 else
1867 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1868 simple_unlock(&vnode_free_list_slock);
1869 simple_unlock(&vp->v_interlock);
1870 }
1871
1872 /*
1873 * We use this when we have vnodes that were loaded in solely for cleaning.
1874 * There is no reason to believe that these vnodes will be referenced again
1875 * soon, since the cleaning process is unrelated to normal filesystem
1876 * activity. Putting cleaned vnodes at the tail of the list has the effect
1877 * of flushing the vnode LRU. So, put vnodes that were loaded only for
1878 * cleaning at the head of the list, instead.
1879 */
1880 void
1881 lfs_vunref_head(vp)
1882 struct vnode *vp;
1883 {
1884 simple_lock(&vp->v_interlock);
1885 #ifdef DIAGNOSTIC
1886 if(vp->v_usecount==0) {
1887 panic("lfs_vunref: v_usecount<0");
1888 }
1889 #endif
1890 vp->v_usecount--;
1891 if (vp->v_usecount > 0) {
1892 simple_unlock(&vp->v_interlock);
1893 return;
1894 }
1895 /*
1896 * insert at head of LRU list
1897 */
1898 simple_lock(&vnode_free_list_slock);
1899 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1900 simple_unlock(&vnode_free_list_slock);
1901 simple_unlock(&vp->v_interlock);
1902 }
1903
1904