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