lfs_segment.c revision 1.91 1 /* $NetBSD: lfs_segment.c,v 1.91 2003/01/24 21:55:27 fvdl 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 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_segment.c,v 1.91 2003/01/24 21:55:27 fvdl Exp $");
75
76 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str))
77
78 #if defined(_KERNEL_OPT)
79 #include "opt_ddb.h"
80 #endif
81
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/namei.h>
85 #include <sys/kernel.h>
86 #include <sys/resourcevar.h>
87 #include <sys/file.h>
88 #include <sys/stat.h>
89 #include <sys/buf.h>
90 #include <sys/proc.h>
91 #include <sys/vnode.h>
92 #include <sys/malloc.h>
93 #include <sys/mount.h>
94
95 #include <miscfs/specfs/specdev.h>
96 #include <miscfs/fifofs/fifo.h>
97
98 #include <ufs/ufs/inode.h>
99 #include <ufs/ufs/dir.h>
100 #include <ufs/ufs/ufsmount.h>
101 #include <ufs/ufs/ufs_extern.h>
102
103 #include <ufs/lfs/lfs.h>
104 #include <ufs/lfs/lfs_extern.h>
105
106 #include <uvm/uvm.h>
107 #include <uvm/uvm_extern.h>
108
109 extern int count_lock_queue(void);
110 extern struct simplelock vnode_free_list_slock; /* XXX */
111
112 static void lfs_generic_callback(struct buf *, void (*)(struct buf *));
113 static void lfs_super_aiodone(struct buf *);
114 static void lfs_cluster_aiodone(struct buf *);
115 static void lfs_cluster_callback(struct buf *);
116 static struct buf **lookahead_pagemove(struct buf **, int, size_t *);
117
118 /*
119 * Determine if it's OK to start a partial in this segment, or if we need
120 * to go on to a new segment.
121 */
122 #define LFS_PARTIAL_FITS(fs) \
123 ((fs)->lfs_fsbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
124 fragstofsb((fs), (fs)->lfs_frag))
125
126 void lfs_callback(struct buf *);
127 int lfs_gather(struct lfs *, struct segment *,
128 struct vnode *, int (*)(struct lfs *, struct buf *));
129 int lfs_gatherblock(struct segment *, struct buf *, int *);
130 void lfs_iset(struct inode *, daddr_t, time_t);
131 int lfs_match_fake(struct lfs *, struct buf *);
132 int lfs_match_data(struct lfs *, struct buf *);
133 int lfs_match_dindir(struct lfs *, struct buf *);
134 int lfs_match_indir(struct lfs *, struct buf *);
135 int lfs_match_tindir(struct lfs *, struct buf *);
136 void lfs_newseg(struct lfs *);
137 /* XXX ondisk32 */
138 void lfs_shellsort(struct buf **, int32_t *, int);
139 void lfs_supercallback(struct buf *);
140 void lfs_updatemeta(struct segment *);
141 int lfs_vref(struct vnode *);
142 void lfs_vunref(struct vnode *);
143 void lfs_writefile(struct lfs *, struct segment *, struct vnode *);
144 int lfs_writeinode(struct lfs *, struct segment *, struct inode *);
145 int lfs_writeseg(struct lfs *, struct segment *);
146 void lfs_writesuper(struct lfs *, daddr_t);
147 int lfs_writevnodes(struct lfs *fs, struct mount *mp,
148 struct segment *sp, int dirops);
149
150 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
151 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */
152 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */
153 int lfs_dirvcount = 0; /* # active dirops */
154
155 /* Statistics Counters */
156 int lfs_dostats = 1;
157 struct lfs_stats lfs_stats;
158
159 extern int locked_queue_count;
160 extern long locked_queue_bytes;
161
162 /* op values to lfs_writevnodes */
163 #define VN_REG 0
164 #define VN_DIROP 1
165 #define VN_EMPTY 2
166 #define VN_CLEAN 3
167
168 #define LFS_MAX_ACTIVE 10
169
170 /*
171 * XXX KS - Set modification time on the Ifile, so the cleaner can
172 * read the fs mod time off of it. We don't set IN_UPDATE here,
173 * since we don't really need this to be flushed to disk (and in any
174 * case that wouldn't happen to the Ifile until we checkpoint).
175 */
176 void
177 lfs_imtime(struct lfs *fs)
178 {
179 struct timespec ts;
180 struct inode *ip;
181
182 TIMEVAL_TO_TIMESPEC(&time, &ts);
183 ip = VTOI(fs->lfs_ivnode);
184 ip->i_ffs_mtime = ts.tv_sec;
185 ip->i_ffs_mtimensec = ts.tv_nsec;
186 }
187
188 /*
189 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
190 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
191 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
192 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
193 */
194
195 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
196 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp))
197 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
198
199 int
200 lfs_vflush(struct vnode *vp)
201 {
202 struct inode *ip;
203 struct lfs *fs;
204 struct segment *sp;
205 struct buf *bp, *nbp, *tbp, *tnbp;
206 int error, s;
207
208 ip = VTOI(vp);
209 fs = VFSTOUFS(vp->v_mount)->um_lfs;
210
211 if (ip->i_flag & IN_CLEANING) {
212 #ifdef DEBUG_LFS
213 ivndebug(vp,"vflush/in_cleaning");
214 #endif
215 LFS_CLR_UINO(ip, IN_CLEANING);
216 LFS_SET_UINO(ip, IN_MODIFIED);
217
218 /*
219 * Toss any cleaning buffers that have real counterparts
220 * to avoid losing new data
221 */
222 s = splbio();
223 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
224 nbp = LIST_NEXT(bp, b_vnbufs);
225 if (bp->b_flags & B_CALL) {
226 for (tbp = LIST_FIRST(&vp->v_dirtyblkhd); tbp;
227 tbp = tnbp)
228 {
229 tnbp = LIST_NEXT(tbp, b_vnbufs);
230 if (tbp->b_vp == bp->b_vp
231 && tbp->b_lblkno == bp->b_lblkno
232 && tbp != bp)
233 {
234 fs->lfs_avail += btofsb(fs, bp->b_bcount);
235 wakeup(&fs->lfs_avail);
236 lfs_freebuf(bp);
237 bp = NULL;
238 break;
239 }
240 }
241 }
242 }
243 splx(s);
244 }
245
246 /* If the node is being written, wait until that is done */
247 s = splbio();
248 if (WRITEINPROG(vp)) {
249 #ifdef DEBUG_LFS
250 ivndebug(vp,"vflush/writeinprog");
251 #endif
252 tsleep(vp, PRIBIO+1, "lfs_vw", 0);
253 }
254 splx(s);
255
256 /* Protect against VXLOCK deadlock in vinvalbuf() */
257 lfs_seglock(fs, SEGM_SYNC);
258
259 /* If we're supposed to flush a freed inode, just toss it */
260 /* XXX - seglock, so these buffers can't be gathered, right? */
261 if (ip->i_ffs_mode == 0) {
262 printf("lfs_vflush: ino %d is freed, not flushing\n",
263 ip->i_number);
264 s = splbio();
265 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
266 nbp = LIST_NEXT(bp, b_vnbufs);
267 if (bp->b_flags & B_DELWRI) { /* XXX always true? */
268 fs->lfs_avail += btofsb(fs, bp->b_bcount);
269 wakeup(&fs->lfs_avail);
270 }
271 /* Copied from lfs_writeseg */
272 if (bp->b_flags & B_CALL) {
273 /* if B_CALL, it was created with newbuf */
274 lfs_freebuf(bp);
275 bp = NULL;
276 } else {
277 bremfree(bp);
278 LFS_UNLOCK_BUF(bp);
279 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
280 B_GATHERED);
281 bp->b_flags |= B_DONE;
282 reassignbuf(bp, vp);
283 brelse(bp);
284 }
285 }
286 splx(s);
287 LFS_CLR_UINO(ip, IN_CLEANING);
288 LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
289 ip->i_flag &= ~IN_ALLMOD;
290 printf("lfs_vflush: done not flushing ino %d\n",
291 ip->i_number);
292 lfs_segunlock(fs);
293 return 0;
294 }
295
296 SET_FLUSHING(fs,vp);
297 if (fs->lfs_nactive > LFS_MAX_ACTIVE ||
298 (fs->lfs_sp->seg_flags & SEGM_CKP)) {
299 error = lfs_segwrite(vp->v_mount, SEGM_CKP | SEGM_SYNC);
300 CLR_FLUSHING(fs,vp);
301 lfs_segunlock(fs);
302 return error;
303 }
304 sp = fs->lfs_sp;
305
306 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
307 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
308 } else if ((ip->i_flag & IN_CLEANING) &&
309 (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
310 #ifdef DEBUG_LFS
311 ivndebug(vp,"vflush/clean");
312 #endif
313 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
314 } else if (lfs_dostats) {
315 if (LIST_FIRST(&vp->v_dirtyblkhd) || (VTOI(vp)->i_flag & IN_ALLMOD))
316 ++lfs_stats.vflush_invoked;
317 #ifdef DEBUG_LFS
318 ivndebug(vp,"vflush");
319 #endif
320 }
321
322 #ifdef DIAGNOSTIC
323 /* XXX KS This actually can happen right now, though it shouldn't(?) */
324 if (vp->v_flag & VDIROP) {
325 printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
326 /* panic("VDIROP being flushed...this can\'t happen"); */
327 }
328 if (vp->v_usecount < 0) {
329 printf("usecount=%ld\n", (long)vp->v_usecount);
330 panic("lfs_vflush: usecount<0");
331 }
332 #endif
333
334 do {
335 do {
336 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL)
337 lfs_writefile(fs, sp, vp);
338 } while (lfs_writeinode(fs, sp, ip));
339 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
340
341 if (lfs_dostats) {
342 ++lfs_stats.nwrites;
343 if (sp->seg_flags & SEGM_SYNC)
344 ++lfs_stats.nsync_writes;
345 if (sp->seg_flags & SEGM_CKP)
346 ++lfs_stats.ncheckpoints;
347 }
348 /*
349 * If we were called from somewhere that has already held the seglock
350 * (e.g., lfs_markv()), the lfs_segunlock will not wait for
351 * the write to complete because we are still locked.
352 * Since lfs_vflush() must return the vnode with no dirty buffers,
353 * we must explicitly wait, if that is the case.
354 *
355 * We compare the iocount against 1, not 0, because it is
356 * artificially incremented by lfs_seglock().
357 */
358 if (fs->lfs_seglock > 1) {
359 while (fs->lfs_iocount > 1)
360 (void)tsleep(&fs->lfs_iocount, PRIBIO + 1,
361 "lfs_vflush", 0);
362 }
363 lfs_segunlock(fs);
364
365 CLR_FLUSHING(fs,vp);
366 return (0);
367 }
368
369 #ifdef DEBUG_LFS_VERBOSE
370 # 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)
371 #else
372 # define vndebug(vp,str)
373 #endif
374
375 int
376 lfs_writevnodes(struct lfs *fs, struct mount *mp, struct segment *sp, int op)
377 {
378 struct inode *ip;
379 struct vnode *vp, *nvp;
380 int inodes_written = 0, only_cleaning;
381
382 #ifndef LFS_NO_BACKVP_HACK
383 /* BEGIN HACK */
384 #define VN_OFFSET (((caddr_t)&LIST_NEXT(vp, v_mntvnodes)) - (caddr_t)vp)
385 #define BACK_VP(VP) ((struct vnode *)(((caddr_t)(VP)->v_mntvnodes.le_prev) - VN_OFFSET))
386 #define BEG_OF_VLIST ((struct vnode *)(((caddr_t)&(LIST_FIRST(&mp->mnt_vnodelist))) - VN_OFFSET))
387
388 /* Find last vnode. */
389 loop: for (vp = LIST_FIRST(&mp->mnt_vnodelist);
390 vp && LIST_NEXT(vp, v_mntvnodes) != NULL;
391 vp = LIST_NEXT(vp, v_mntvnodes));
392 for (; vp && vp != BEG_OF_VLIST; vp = nvp) {
393 nvp = BACK_VP(vp);
394 #else
395 loop:
396 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
397 nvp = LIST_NEXT(vp, v_mntvnodes);
398 #endif
399 /*
400 * If the vnode that we are about to sync is no longer
401 * associated with this mount point, start over.
402 */
403 if (vp->v_mount != mp) {
404 printf("lfs_writevnodes: starting over\n");
405 goto loop;
406 }
407
408 if (vp->v_type == VNON) {
409 continue;
410 }
411
412 ip = VTOI(vp);
413 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
414 (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
415 vndebug(vp,"dirop");
416 continue;
417 }
418
419 if (op == VN_EMPTY && LIST_FIRST(&vp->v_dirtyblkhd)) {
420 vndebug(vp,"empty");
421 continue;
422 }
423
424 if (op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
425 && vp != fs->lfs_flushvp
426 && !(ip->i_flag & IN_CLEANING)) {
427 vndebug(vp,"cleaning");
428 continue;
429 }
430
431 if (lfs_vref(vp)) {
432 vndebug(vp,"vref");
433 continue;
434 }
435
436 only_cleaning = 0;
437 /*
438 * Write the inode/file if dirty and it's not the IFILE.
439 */
440 if ((ip->i_flag & IN_ALLMOD) ||
441 (LIST_FIRST(&vp->v_dirtyblkhd) != NULL))
442 {
443 only_cleaning = ((ip->i_flag & IN_ALLMOD) == IN_CLEANING);
444
445 if (ip->i_number != LFS_IFILE_INUM
446 && LIST_FIRST(&vp->v_dirtyblkhd) != NULL)
447 {
448 lfs_writefile(fs, sp, vp);
449 }
450 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) {
451 if (WRITEINPROG(vp)) {
452 #ifdef DEBUG_LFS
453 ivndebug(vp,"writevnodes/write2");
454 #endif
455 } else if (!(ip->i_flag & IN_ALLMOD)) {
456 #ifdef DEBUG_LFS
457 printf("<%d>",ip->i_number);
458 #endif
459 LFS_SET_UINO(ip, IN_MODIFIED);
460 }
461 }
462 (void) lfs_writeinode(fs, sp, ip);
463 inodes_written++;
464 }
465
466 if (lfs_clean_vnhead && only_cleaning)
467 lfs_vunref_head(vp);
468 else
469 lfs_vunref(vp);
470 }
471 return inodes_written;
472 }
473
474 /*
475 * Do a checkpoint.
476 */
477 int
478 lfs_segwrite(struct mount *mp, int flags)
479 {
480 struct buf *bp;
481 struct inode *ip;
482 struct lfs *fs;
483 struct segment *sp;
484 struct vnode *vp;
485 SEGUSE *segusep;
486 daddr_t ibno;
487 int do_ckp, did_ckp, error, i;
488 int writer_set = 0;
489 int dirty;
490 int redo;
491
492 fs = VFSTOUFS(mp)->um_lfs;
493
494 if (fs->lfs_ronly)
495 return EROFS;
496
497 lfs_imtime(fs);
498
499 /* printf("lfs_segwrite: ifile flags are 0x%lx\n",
500 (long)(VTOI(fs->lfs_ivnode)->i_flag)); */
501
502 #if 0
503 /*
504 * If we are not the cleaner, and there is no space available,
505 * wait until cleaner writes.
506 */
507 if (!(flags & SEGM_CLEAN) && !(fs->lfs_seglock && fs->lfs_sp &&
508 (fs->lfs_sp->seg_flags & SEGM_CLEAN)))
509 {
510 while (fs->lfs_avail <= 0) {
511 LFS_CLEANERINFO(cip, fs, bp);
512 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
513
514 wakeup(&lfs_allclean_wakeup);
515 wakeup(&fs->lfs_nextseg);
516 error = tsleep(&fs->lfs_avail, PRIBIO + 1, "lfs_av2",
517 0);
518 if (error) {
519 return (error);
520 }
521 }
522 }
523 #endif
524 /*
525 * Allocate a segment structure and enough space to hold pointers to
526 * the maximum possible number of buffers which can be described in a
527 * single summary block.
528 */
529 do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
530 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
531 sp = fs->lfs_sp;
532
533 /*
534 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
535 * in which case we have to flush *all* buffers off of this vnode.
536 * We don't care about other nodes, but write any non-dirop nodes
537 * anyway in anticipation of another getnewvnode().
538 *
539 * If we're cleaning we only write cleaning and ifile blocks, and
540 * no dirops, since otherwise we'd risk corruption in a crash.
541 */
542 if (sp->seg_flags & SEGM_CLEAN)
543 lfs_writevnodes(fs, mp, sp, VN_CLEAN);
544 else {
545 lfs_writevnodes(fs, mp, sp, VN_REG);
546 if (!fs->lfs_dirops || !fs->lfs_flushvp) {
547 while (fs->lfs_dirops)
548 if ((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
549 "lfs writer", 0)))
550 {
551 /* XXX why not segunlock? */
552 free(sp->bpp, M_SEGMENT);
553 sp->bpp = NULL;
554 free(sp, M_SEGMENT);
555 fs->lfs_sp = NULL;
556 return (error);
557 }
558 fs->lfs_writer++;
559 writer_set = 1;
560 lfs_writevnodes(fs, mp, sp, VN_DIROP);
561 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
562 }
563 }
564
565 /*
566 * If we are doing a checkpoint, mark everything since the
567 * last checkpoint as no longer ACTIVE.
568 */
569 if (do_ckp) {
570 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
571 --ibno >= fs->lfs_cleansz; ) {
572 dirty = 0;
573 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
574
575 panic("lfs_segwrite: ifile read");
576 segusep = (SEGUSE *)bp->b_data;
577 for (i = fs->lfs_sepb; i--;) {
578 if (segusep->su_flags & SEGUSE_ACTIVE) {
579 segusep->su_flags &= ~SEGUSE_ACTIVE;
580 ++dirty;
581 }
582 if (fs->lfs_version > 1)
583 ++segusep;
584 else
585 segusep = (SEGUSE *)
586 ((SEGUSE_V1 *)segusep + 1);
587 }
588
589 /* But the current segment is still ACTIVE */
590 segusep = (SEGUSE *)bp->b_data;
591 if (dtosn(fs, fs->lfs_curseg) / fs->lfs_sepb ==
592 (ibno-fs->lfs_cleansz)) {
593 if (fs->lfs_version > 1)
594 segusep[dtosn(fs, fs->lfs_curseg) %
595 fs->lfs_sepb].su_flags |=
596 SEGUSE_ACTIVE;
597 else
598 ((SEGUSE *)
599 ((SEGUSE_V1 *)(bp->b_data) +
600 (dtosn(fs, fs->lfs_curseg) %
601 fs->lfs_sepb)))->su_flags
602 |= SEGUSE_ACTIVE;
603 --dirty;
604 }
605 if (dirty)
606 error = LFS_BWRITE_LOG(bp); /* Ifile */
607 else
608 brelse(bp);
609 }
610 }
611
612 did_ckp = 0;
613 if (do_ckp || fs->lfs_doifile) {
614 do {
615 vp = fs->lfs_ivnode;
616
617 vget(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
618 #ifdef DEBUG
619 LFS_ENTER_LOG("pretend", __FILE__, __LINE__, 0, 0);
620 #endif
621 fs->lfs_flags &= ~LFS_IFDIRTY;
622
623 ip = VTOI(vp);
624 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL)
625 lfs_writefile(fs, sp, vp);
626 if (ip->i_flag & IN_ALLMOD)
627 ++did_ckp;
628 redo = lfs_writeinode(fs, sp, ip);
629
630 vput(vp);
631 /*
632 * if we know we'll redo, no need to writeseg here.
633 */
634 if (!(redo && do_ckp)) {
635 redo += lfs_writeseg(fs, sp);
636 }
637 redo += (fs->lfs_flags & LFS_IFDIRTY);
638 } while (redo && do_ckp);
639
640 /* The ifile should now be all clear */
641 if (do_ckp && LIST_FIRST(&vp->v_dirtyblkhd)) {
642 struct buf *bp;
643 int s, warned = 0, dopanic = 0;
644 s = splbio();
645 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = LIST_NEXT(bp, b_vnbufs)) {
646 if (!(bp->b_flags & B_GATHERED)) {
647 if (!warned)
648 printf("lfs_segwrite: ifile still has dirty blocks?!\n");
649 ++dopanic;
650 ++warned;
651 printf("bp=%p, lbn %lld, flags 0x%lx\n",
652 bp, (long long)bp->b_lblkno,
653 bp->b_flags);
654 }
655 }
656 if (dopanic)
657 panic("dirty blocks");
658 splx(s);
659 }
660 LFS_CLR_UINO(ip, IN_ALLMOD);
661 } else {
662 (void) lfs_writeseg(fs, sp);
663 }
664
665 /*
666 * If the I/O count is non-zero, sleep until it reaches zero.
667 * At the moment, the user's process hangs around so we can
668 * sleep.
669 */
670 fs->lfs_doifile = 0;
671 if (writer_set && --fs->lfs_writer == 0)
672 wakeup(&fs->lfs_dirops);
673
674 /*
675 * If we didn't write the Ifile, we didn't really do anything.
676 * That means that (1) there is a checkpoint on disk and (2)
677 * nothing has changed since it was written.
678 *
679 * Take the flags off of the segment so that lfs_segunlock
680 * doesn't have to write the superblock either.
681 */
682 if (do_ckp && !did_ckp) {
683 sp->seg_flags &= ~SEGM_CKP;
684 /* if (do_ckp) printf("lfs_segwrite: no checkpoint\n"); */
685 }
686
687 if (lfs_dostats) {
688 ++lfs_stats.nwrites;
689 if (sp->seg_flags & SEGM_SYNC)
690 ++lfs_stats.nsync_writes;
691 if (sp->seg_flags & SEGM_CKP)
692 ++lfs_stats.ncheckpoints;
693 }
694 lfs_segunlock(fs);
695 return (0);
696 }
697
698 /*
699 * Write the dirty blocks associated with a vnode.
700 */
701 void
702 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp)
703 {
704 struct buf *bp;
705 struct finfo *fip;
706 struct inode *ip;
707 IFILE *ifp;
708 int i, frag;
709
710 ip = VTOI(vp);
711
712 if (sp->seg_bytes_left < fs->lfs_bsize ||
713 sp->sum_bytes_left < sizeof(struct finfo))
714 (void) lfs_writeseg(fs, sp);
715
716 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
717 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
718
719 if (vp->v_flag & VDIROP)
720 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
721
722 fip = sp->fip;
723 fip->fi_nblocks = 0;
724 fip->fi_ino = ip->i_number;
725 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
726 fip->fi_version = ifp->if_version;
727 brelse(bp);
728
729 if (sp->seg_flags & SEGM_CLEAN) {
730 lfs_gather(fs, sp, vp, lfs_match_fake);
731 /*
732 * For a file being flushed, we need to write *all* blocks.
733 * This means writing the cleaning blocks first, and then
734 * immediately following with any non-cleaning blocks.
735 * The same is true of the Ifile since checkpoints assume
736 * that all valid Ifile blocks are written.
737 */
738 if (IS_FLUSHING(fs,vp) || vp == fs->lfs_ivnode)
739 lfs_gather(fs, sp, vp, lfs_match_data);
740 } else
741 lfs_gather(fs, sp, vp, lfs_match_data);
742
743 /*
744 * It may not be necessary to write the meta-data blocks at this point,
745 * as the roll-forward recovery code should be able to reconstruct the
746 * list.
747 *
748 * We have to write them anyway, though, under two conditions: (1) the
749 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
750 * checkpointing.
751 *
752 * BUT if we are cleaning, we might have indirect blocks that refer to
753 * new blocks not being written yet, in addition to fragments being
754 * moved out of a cleaned segment. If that is the case, don't
755 * write the indirect blocks, or the finfo will have a small block
756 * in the middle of it!
757 * XXX in this case isn't the inode size wrong too?
758 */
759 frag = 0;
760 if (sp->seg_flags & SEGM_CLEAN) {
761 for (i = 0; i < NDADDR; i++)
762 if (ip->i_lfs_fragsize[i] > 0 &&
763 ip->i_lfs_fragsize[i] < fs->lfs_bsize)
764 ++frag;
765 }
766 #ifdef DIAGNOSTIC
767 if (frag > 1)
768 panic("lfs_writefile: more than one fragment!");
769 #endif
770 if (IS_FLUSHING(fs, vp) ||
771 (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) {
772 lfs_gather(fs, sp, vp, lfs_match_indir);
773 lfs_gather(fs, sp, vp, lfs_match_dindir);
774 lfs_gather(fs, sp, vp, lfs_match_tindir);
775 }
776 fip = sp->fip;
777 if (fip->fi_nblocks != 0) {
778 sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
779 sizeof(daddr_t) * (fip->fi_nblocks-1));
780 sp->start_lbp = &sp->fip->fi_blocks[0];
781 } else {
782 sp->sum_bytes_left += sizeof(FINFO) - sizeof(daddr_t);
783 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
784 }
785 }
786
787 int
788 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
789 {
790 struct buf *bp, *ibp;
791 struct dinode *cdp;
792 IFILE *ifp;
793 SEGUSE *sup;
794 daddr_t daddr;
795 int32_t *daddrp; /* XXX ondisk32 */
796 ino_t ino;
797 int error, i, ndx, fsb = 0;
798 int redo_ifile = 0;
799 struct timespec ts;
800 int gotblk = 0;
801
802 if (!(ip->i_flag & IN_ALLMOD))
803 return (0);
804
805 /* Allocate a new inode block if necessary. */
806 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) && sp->ibp == NULL) {
807 /* Allocate a new segment if necessary. */
808 if (sp->seg_bytes_left < fs->lfs_ibsize ||
809 sp->sum_bytes_left < sizeof(daddr_t))
810 (void) lfs_writeseg(fs, sp);
811
812 /* Get next inode block. */
813 daddr = fs->lfs_offset;
814 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
815 sp->ibp = *sp->cbpp++ =
816 getblk(VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr),
817 fs->lfs_ibsize, 0, 0);
818 gotblk++;
819
820 /* Zero out inode numbers */
821 for (i = 0; i < INOPB(fs); ++i)
822 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
823
824 ++sp->start_bpp;
825 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
826 /* Set remaining space counters. */
827 sp->seg_bytes_left -= fs->lfs_ibsize;
828 sp->sum_bytes_left -= sizeof(daddr_t);
829 ndx = fs->lfs_sumsize / sizeof(daddr_t) -
830 sp->ninodes / INOPB(fs) - 1;
831 /* fvdl -- not on disk, should be ok */
832 ((daddr_t *)(sp->segsum))[ndx] = daddr;
833 }
834
835 /* Update the inode times and copy the inode onto the inode page. */
836 TIMEVAL_TO_TIMESPEC(&time, &ts);
837 /* XXX kludge --- don't redirty the ifile just to put times on it */
838 if (ip->i_number != LFS_IFILE_INUM)
839 LFS_ITIMES(ip, &ts, &ts, &ts);
840
841 /*
842 * If this is the Ifile, and we've already written the Ifile in this
843 * partial segment, just overwrite it (it's not on disk yet) and
844 * continue.
845 *
846 * XXX we know that the bp that we get the second time around has
847 * already been gathered.
848 */
849 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
850 *(sp->idp) = ip->i_din.ffs_din;
851 ip->i_lfs_osize = ip->i_ffs_size;
852 return 0;
853 }
854
855 bp = sp->ibp;
856 cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
857 *cdp = ip->i_din.ffs_din;
858 #ifdef LFS_IFILE_FRAG_ADDRESSING
859 if (fs->lfs_version > 1)
860 fsb = (sp->ninodes % INOPB(fs)) / INOPF(fs);
861 #endif
862
863 /*
864 * If we are cleaning, ensure that we don't write UNWRITTEN disk
865 * addresses to disk; possibly revert the inode size.
866 */
867 if (ip->i_lfs_effnblks != ip->i_ffs_blocks) {
868 cdp->di_size = ip->i_lfs_osize;
869 #ifdef DEBUG_LFS
870 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
871 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks);
872 #endif
873 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
874 daddrp++) {
875 if (*daddrp == UNWRITTEN) {
876 #ifdef DEBUG_LFS
877 printf("lfs_writeinode: wiping UNWRITTEN\n");
878 #endif
879 *daddrp = 0;
880 }
881 }
882 } else {
883 /* If all blocks are goig to disk, update the "size on disk" */
884 ip->i_lfs_osize = ip->i_ffs_size;
885 }
886
887 if (ip->i_flag & IN_CLEANING)
888 LFS_CLR_UINO(ip, IN_CLEANING);
889 else {
890 /* XXX IN_ALLMOD */
891 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
892 IN_UPDATE);
893 if (ip->i_lfs_effnblks == ip->i_ffs_blocks)
894 LFS_CLR_UINO(ip, IN_MODIFIED);
895 #ifdef DEBUG_LFS
896 else
897 printf("lfs_writeinode: ino %d: real blks=%d, "
898 "eff=%d\n", ip->i_number, ip->i_ffs_blocks,
899 ip->i_lfs_effnblks);
900 #endif
901 }
902
903 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
904 sp->idp = ((struct dinode *)bp->b_data) +
905 (sp->ninodes % INOPB(fs));
906 if (gotblk) {
907 LFS_LOCK_BUF(bp);
908 brelse(bp);
909 }
910
911 /* Increment inode count in segment summary block. */
912 ++((SEGSUM *)(sp->segsum))->ss_ninos;
913
914 /* If this page is full, set flag to allocate a new page. */
915 if (++sp->ninodes % INOPB(fs) == 0)
916 sp->ibp = NULL;
917
918 /*
919 * If updating the ifile, update the super-block. Update the disk
920 * address and access times for this inode in the ifile.
921 */
922 ino = ip->i_number;
923 if (ino == LFS_IFILE_INUM) {
924 daddr = fs->lfs_idaddr;
925 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
926 } else {
927 LFS_IENTRY(ifp, fs, ino, ibp);
928 daddr = ifp->if_daddr;
929 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
930 #ifdef LFS_DEBUG_NEXTFREE
931 if (ino > 3 && ifp->if_nextfree) {
932 vprint("lfs_writeinode",ITOV(ip));
933 printf("lfs_writeinode: updating free ino %d\n",
934 ip->i_number);
935 }
936 #endif
937 error = LFS_BWRITE_LOG(ibp); /* Ifile */
938 }
939
940 /*
941 * The inode's last address should not be in the current partial
942 * segment, except under exceptional circumstances (lfs_writevnodes
943 * had to start over, and in the meantime more blocks were written
944 * to a vnode). Both inodes will be accounted to this segment
945 * in lfs_writeseg so we need to subtract the earlier version
946 * here anyway. The segment count can temporarily dip below
947 * zero here; keep track of how many duplicates we have in
948 * "dupino" so we don't panic below.
949 */
950 if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno)) {
951 ++sp->ndupino;
952 printf("lfs_writeinode: last inode addr in current pseg "
953 "(ino %d daddr 0x%llx) ndupino=%d\n", ino,
954 (long long)daddr, sp->ndupino);
955 }
956 /*
957 * Account the inode: it no longer belongs to its former segment,
958 * though it will not belong to the new segment until that segment
959 * is actually written.
960 */
961 if (daddr != LFS_UNUSED_DADDR) {
962 u_int32_t oldsn = dtosn(fs, daddr);
963 #ifdef DIAGNOSTIC
964 int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0;
965 #endif
966 LFS_SEGENTRY(sup, fs, oldsn, bp);
967 #ifdef DIAGNOSTIC
968 if (sup->su_nbytes + DINODE_SIZE * ndupino < DINODE_SIZE) {
969 printf("lfs_writeinode: negative bytes "
970 "(segment %d short by %d, "
971 "oldsn=%u, cursn=%u, daddr=%d, su_nbytes=%u, "
972 "ndupino=%d)\n",
973 dtosn(fs, daddr),
974 (int)DINODE_SIZE * (1 - sp->ndupino)
975 - sup->su_nbytes,
976 (unsigned int)oldsn,
977 (unsigned int)sp->seg_number,
978 (int)daddr,
979 (unsigned int)sup->su_nbytes,
980 sp->ndupino);
981 panic("lfs_writeinode: negative bytes");
982 sup->su_nbytes = DINODE_SIZE;
983 }
984 #endif
985 #ifdef DEBUG_SU_NBYTES
986 printf("seg %d -= %d for ino %d inode\n",
987 dtosn(fs, daddr), DINODE_SIZE, ino);
988 #endif
989 sup->su_nbytes -= DINODE_SIZE;
990 redo_ifile =
991 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
992 if (redo_ifile)
993 fs->lfs_flags |= LFS_IFDIRTY;
994 error = LFS_BWRITE_LOG(bp); /* Ifile */
995 }
996 return (redo_ifile);
997 }
998
999 int
1000 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
1001 {
1002 struct lfs *fs;
1003 int version;
1004
1005 /*
1006 * If full, finish this segment. We may be doing I/O, so
1007 * release and reacquire the splbio().
1008 */
1009 #ifdef DIAGNOSTIC
1010 if (sp->vp == NULL)
1011 panic ("lfs_gatherblock: Null vp in segment");
1012 #endif
1013 fs = sp->fs;
1014 if (sp->sum_bytes_left < sizeof(daddr_t) ||
1015 sp->seg_bytes_left < bp->b_bcount) {
1016 if (sptr)
1017 splx(*sptr);
1018 lfs_updatemeta(sp);
1019
1020 version = sp->fip->fi_version;
1021 (void) lfs_writeseg(fs, sp);
1022
1023 sp->fip->fi_version = version;
1024 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
1025 /* Add the current file to the segment summary. */
1026 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1027 sp->sum_bytes_left -=
1028 sizeof(struct finfo) - sizeof(daddr_t);
1029
1030 if (sptr)
1031 *sptr = splbio();
1032 return (1);
1033 }
1034
1035 #ifdef DEBUG
1036 if (bp->b_flags & B_GATHERED) {
1037 printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
1038 sp->fip->fi_ino, bp->b_lblkno);
1039 return (0);
1040 }
1041 #endif
1042 /* Insert into the buffer list, update the FINFO block. */
1043 bp->b_flags |= B_GATHERED;
1044 bp->b_flags &= ~B_DONE;
1045
1046 *sp->cbpp++ = bp;
1047 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
1048
1049 sp->sum_bytes_left -= sizeof(daddr_t);
1050 sp->seg_bytes_left -= bp->b_bcount;
1051 return (0);
1052 }
1053
1054 int
1055 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp, int (*match)(struct lfs *, struct buf *))
1056 {
1057 struct buf *bp, *nbp;
1058 int s, count = 0;
1059
1060 sp->vp = vp;
1061 s = splbio();
1062
1063 #ifndef LFS_NO_BACKBUF_HACK
1064 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
1065 # define BUF_OFFSET (((caddr_t)&LIST_NEXT(bp, b_vnbufs)) - (caddr_t)bp)
1066 # define BACK_BUF(BP) ((struct buf *)(((caddr_t)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
1067 # define BEG_OF_LIST ((struct buf *)(((caddr_t)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
1068 /* Find last buffer. */
1069 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp && LIST_NEXT(bp, b_vnbufs) != NULL;
1070 bp = LIST_NEXT(bp, b_vnbufs));
1071 for (; bp && bp != BEG_OF_LIST; bp = nbp) {
1072 nbp = BACK_BUF(bp);
1073 #else /* LFS_NO_BACKBUF_HACK */
1074 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1075 nbp = LIST_NEXT(bp, b_vnbufs);
1076 #endif /* LFS_NO_BACKBUF_HACK */
1077 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) {
1078 #ifdef DEBUG_LFS
1079 if (vp == fs->lfs_ivnode && (bp->b_flags & (B_BUSY|B_GATHERED)) == B_BUSY)
1080 printf("(%d:%lx)", bp->b_lblkno, bp->b_flags);
1081 #endif
1082 continue;
1083 }
1084 if (vp->v_type == VBLK) {
1085 /* For block devices, just write the blocks. */
1086 /* XXX Do we really need to even do this? */
1087 #ifdef DEBUG_LFS
1088 if (count == 0)
1089 printf("BLK(");
1090 printf(".");
1091 #endif
1092 /* Get the block before bwrite, so we don't corrupt the free list */
1093 bp->b_flags |= B_BUSY;
1094 bremfree(bp);
1095 bwrite(bp);
1096 } else {
1097 #ifdef DIAGNOSTIC
1098 if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
1099 printf("lfs_gather: lbn %d is B_INVAL\n",
1100 bp->b_lblkno);
1101 VOP_PRINT(bp->b_vp);
1102 }
1103 if (!(bp->b_flags & B_DELWRI))
1104 panic("lfs_gather: bp not B_DELWRI");
1105 if (!(bp->b_flags & B_LOCKED)) {
1106 printf("lfs_gather: lbn %d blk %d"
1107 " not B_LOCKED\n", bp->b_lblkno,
1108 dbtofsb(fs, bp->b_blkno));
1109 VOP_PRINT(bp->b_vp);
1110 panic("lfs_gather: bp not B_LOCKED");
1111 }
1112 #endif
1113 if (lfs_gatherblock(sp, bp, &s)) {
1114 goto loop;
1115 }
1116 }
1117 count++;
1118 }
1119 splx(s);
1120 #ifdef DEBUG_LFS
1121 if (vp->v_type == VBLK && count)
1122 printf(")\n");
1123 #endif
1124 lfs_updatemeta(sp);
1125 sp->vp = NULL;
1126 return count;
1127 }
1128
1129 /*
1130 * Update the metadata that points to the blocks listed in the FINFO
1131 * array.
1132 */
1133 void
1134 lfs_updatemeta(struct segment *sp)
1135 {
1136 SEGUSE *sup;
1137 struct buf *bp, *sbp;
1138 struct lfs *fs;
1139 struct vnode *vp;
1140 struct indir a[NIADDR + 2], *ap;
1141 struct inode *ip;
1142 daddr_t daddr, lbn, off;
1143 daddr_t ooff;
1144 int error, i, nblocks, num;
1145 int bb, osize, obb;
1146
1147 vp = sp->vp;
1148 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1149 if (nblocks < 0)
1150 panic("This is a bad thing");
1151 if (vp == NULL || nblocks == 0)
1152 return;
1153
1154 /* Sort the blocks. */
1155 /*
1156 * XXX KS - We have to sort even if the blocks come from the
1157 * cleaner, because there might be other pending blocks on the
1158 * same inode...and if we don't sort, and there are fragments
1159 * present, blocks may be written in the wrong place.
1160 */
1161 /* if (!(sp->seg_flags & SEGM_CLEAN)) */
1162 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
1163
1164 /*
1165 * Record the length of the last block in case it's a fragment.
1166 * If there are indirect blocks present, they sort last. An
1167 * indirect block will be lfs_bsize and its presence indicates
1168 * that you cannot have fragments.
1169 *
1170 * XXX This last is a lie. A cleaned fragment can coexist with
1171 * XXX a later indirect block. This will continue to be
1172 * XXX true until lfs_markv is fixed to do everything with
1173 * XXX fake blocks (including fake inodes and fake indirect blocks).
1174 */
1175 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
1176
1177 /*
1178 * Assign disk addresses, and update references to the logical
1179 * block and the segment usage information.
1180 */
1181 fs = sp->fs;
1182 for (i = nblocks; i--; ++sp->start_bpp) {
1183 lbn = *sp->start_lbp++;
1184 sbp = *sp->start_bpp;
1185
1186 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1187 off = fs->lfs_offset;
1188 if (sbp->b_blkno == sbp->b_lblkno) {
1189 printf("lfs_updatemeta: ino %d blk %lld"
1190 " has same lbn and daddr\n",
1191 VTOI(vp)->i_number, (long long)off);
1192 }
1193
1194 /*
1195 * If we write a frag in the wrong place, the cleaner won't
1196 * be able to correctly identify its size later, and the
1197 * segment will be uncleanable. (Even worse, it will assume
1198 * that the indirect block that actually ends the list
1199 * is of a smaller size!)
1200 */
1201 if (sbp->b_bcount < fs->lfs_bsize && i != 0)
1202 panic("lfs_updatemeta: fragment is not last block");
1203
1204 bb = fragstofsb(fs, numfrags(fs, sbp->b_bcount));
1205 fs->lfs_offset += bb;
1206 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
1207 if (daddr > 0)
1208 daddr = dbtofsb(fs, daddr);
1209 if (error)
1210 panic("lfs_updatemeta: ufs_bmaparray %d", error);
1211 ip = VTOI(vp);
1212 switch (num) {
1213 case 0:
1214 ooff = ip->i_ffs_db[lbn];
1215 #ifdef DEBUG
1216 if (ooff == 0) {
1217 printf("lfs_updatemeta[1]: warning: writing "
1218 "ino %d lbn %d at 0x%x, was 0x0\n",
1219 ip->i_number, lbn, off);
1220 }
1221 #endif
1222 if (ooff == UNWRITTEN)
1223 ip->i_ffs_blocks += bb;
1224 else {
1225 /* possible fragment truncation or extension */
1226 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
1227 ip->i_ffs_blocks += (bb - obb);
1228 }
1229 ip->i_ffs_db[lbn] = off;
1230 break;
1231 case 1:
1232 ooff = ip->i_ffs_ib[a[0].in_off];
1233 #ifdef DEBUG
1234 if (ooff == 0) {
1235 printf("lfs_updatemeta[2]: warning: writing "
1236 "ino %d lbn %d at 0x%x, was 0x0\n",
1237 ip->i_number, lbn, off);
1238 }
1239 #endif
1240 if (ooff == UNWRITTEN)
1241 ip->i_ffs_blocks += bb;
1242 ip->i_ffs_ib[a[0].in_off] = off;
1243 break;
1244 default:
1245 ap = &a[num - 1];
1246 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1247 panic("lfs_updatemeta: bread bno %lld",
1248 (long long)ap->in_lbn);
1249
1250 /* XXX ondisk32 */
1251 ooff = ((int32_t *)bp->b_data)[ap->in_off];
1252 #if DEBUG
1253 if (ooff == 0) {
1254 printf("lfs_updatemeta[3]: warning: writing "
1255 "ino %d lbn %d at 0x%x, was 0x0\n",
1256 ip->i_number, lbn, off);
1257 }
1258 #endif
1259 if (ooff == UNWRITTEN)
1260 ip->i_ffs_blocks += bb;
1261 /* XXX ondisk32 */
1262 ((int32_t *)bp->b_data)[ap->in_off] = off;
1263 (void) VOP_BWRITE(bp);
1264 }
1265 #ifdef DEBUG
1266 if (daddr >= fs->lfs_lastpseg && daddr <= off) {
1267 printf("lfs_updatemeta: ino %d, lbn %d, addr = %x "
1268 "in same pseg\n", VTOI(sp->vp)->i_number,
1269 sbp->b_lblkno, daddr);
1270 }
1271 #endif
1272 /*
1273 * Update segment usage information, based on old size
1274 * and location.
1275 */
1276 if (daddr > 0) {
1277 u_int32_t oldsn = dtosn(fs, daddr);
1278 #ifdef DIAGNOSTIC
1279 int ndupino = (sp->seg_number == oldsn) ?
1280 sp->ndupino : 0;
1281 #endif
1282 if (lbn >= 0 && lbn < NDADDR)
1283 osize = ip->i_lfs_fragsize[lbn];
1284 else
1285 osize = fs->lfs_bsize;
1286 LFS_SEGENTRY(sup, fs, oldsn, bp);
1287 #ifdef DIAGNOSTIC
1288 if (sup->su_nbytes + DINODE_SIZE * ndupino < osize) {
1289 printf("lfs_updatemeta: negative bytes "
1290 "(segment %d short by %d)\n",
1291 dtosn(fs, daddr),
1292 osize - sup->su_nbytes);
1293 printf("lfs_updatemeta: ino %d, lbn %d, "
1294 "addr = 0x%x\n", VTOI(sp->vp)->i_number,
1295 lbn, daddr);
1296 printf("lfs_updatemeta: ndupino=%d\n", ndupino);
1297 panic("lfs_updatemeta: negative bytes");
1298 sup->su_nbytes = osize;
1299 }
1300 #endif
1301 #ifdef DEBUG_SU_NBYTES
1302 printf("seg %d -= %ld for ino %d lbn %d db 0x%x\n",
1303 dtosn(fs, daddr), osize, VTOI(sp->vp)->i_number,
1304 lbn, daddr);
1305 #endif
1306 sup->su_nbytes -= osize;
1307 if (!(bp->b_flags & B_GATHERED))
1308 fs->lfs_flags |= LFS_IFDIRTY;
1309 error = LFS_BWRITE_LOG(bp); /* Ifile */
1310 }
1311 /*
1312 * Now that this block has a new address, and its old
1313 * segment no longer owns it, we can forget about its
1314 * old size.
1315 */
1316 if (lbn >= 0 && lbn < NDADDR)
1317 ip->i_lfs_fragsize[lbn] = sbp->b_bcount;
1318 }
1319 }
1320
1321 /*
1322 * Start a new segment.
1323 */
1324 int
1325 lfs_initseg(struct lfs *fs)
1326 {
1327 struct segment *sp;
1328 SEGUSE *sup;
1329 SEGSUM *ssp;
1330 struct buf *bp, *sbp;
1331 int repeat;
1332
1333 sp = fs->lfs_sp;
1334
1335 repeat = 0;
1336 /* Advance to the next segment. */
1337 if (!LFS_PARTIAL_FITS(fs)) {
1338 /* lfs_avail eats the remaining space */
1339 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1340 fs->lfs_curseg);
1341 /* Wake up any cleaning procs waiting on this file system. */
1342 wakeup(&lfs_allclean_wakeup);
1343 wakeup(&fs->lfs_nextseg);
1344 lfs_newseg(fs);
1345 repeat = 1;
1346 fs->lfs_offset = fs->lfs_curseg;
1347 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1348 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1349 /*
1350 * If the segment contains a superblock, update the offset
1351 * and summary address to skip over it.
1352 */
1353 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1354 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1355 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1356 sp->seg_bytes_left -= LFS_SBPAD;
1357 }
1358 brelse(bp);
1359 /* Segment zero could also contain the labelpad */
1360 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1361 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1362 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1363 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1364 }
1365 } else {
1366 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1367 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1368 (fs->lfs_offset - fs->lfs_curseg));
1369 }
1370 fs->lfs_lastpseg = fs->lfs_offset;
1371
1372 sp->fs = fs;
1373 sp->ibp = NULL;
1374 sp->idp = NULL;
1375 sp->ninodes = 0;
1376 sp->ndupino = 0;
1377
1378 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
1379 sp->cbpp = sp->bpp;
1380 #ifdef LFS_MALLOC_SUMMARY
1381 sbp = *sp->cbpp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1382 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
1383 sp->segsum = (*sp->cbpp)->b_data;
1384 #else
1385 sbp = *sp->cbpp = getblk(VTOI(fs->lfs_ivnode)->i_devvp,
1386 fsbtodb(fs, fs->lfs_offset), NBPG, 0, 0);
1387 memset(sbp->b_data, 0x5a, NBPG);
1388 sp->segsum = (*sp->cbpp)->b_data + NBPG - fs->lfs_sumsize;
1389 #endif
1390 bzero(sp->segsum, fs->lfs_sumsize);
1391 sp->start_bpp = ++sp->cbpp;
1392 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1393
1394 /* Set point to SEGSUM, initialize it. */
1395 ssp = sp->segsum;
1396 ssp->ss_next = fs->lfs_nextseg;
1397 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1398 ssp->ss_magic = SS_MAGIC;
1399
1400 /* Set pointer to first FINFO, initialize it. */
1401 sp->fip = (struct finfo *)((caddr_t)sp->segsum + SEGSUM_SIZE(fs));
1402 sp->fip->fi_nblocks = 0;
1403 sp->start_lbp = &sp->fip->fi_blocks[0];
1404 sp->fip->fi_lastlength = 0;
1405
1406 sp->seg_bytes_left -= fs->lfs_sumsize;
1407 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1408
1409 #ifndef LFS_MALLOC_SUMMARY
1410 LFS_LOCK_BUF(sbp);
1411 brelse(sbp);
1412 #endif
1413 return (repeat);
1414 }
1415
1416 /*
1417 * Return the next segment to write.
1418 */
1419 void
1420 lfs_newseg(struct lfs *fs)
1421 {
1422 CLEANERINFO *cip;
1423 SEGUSE *sup;
1424 struct buf *bp;
1425 int curseg, isdirty, sn;
1426
1427 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1428 #ifdef DEBUG_SU_NBYTES
1429 printf("lfs_newseg: seg %d := 0 in newseg\n", /* XXXDEBUG */
1430 dtosn(fs, fs->lfs_nextseg)); /* XXXDEBUG */
1431 #endif
1432 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1433 sup->su_nbytes = 0;
1434 sup->su_nsums = 0;
1435 sup->su_ninos = 0;
1436 (void) LFS_BWRITE_LOG(bp); /* Ifile */
1437
1438 LFS_CLEANERINFO(cip, fs, bp);
1439 --cip->clean;
1440 ++cip->dirty;
1441 fs->lfs_nclean = cip->clean;
1442 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1443
1444 fs->lfs_lastseg = fs->lfs_curseg;
1445 fs->lfs_curseg = fs->lfs_nextseg;
1446 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1447 sn = (sn + 1) % fs->lfs_nseg;
1448 if (sn == curseg)
1449 panic("lfs_nextseg: no clean segments");
1450 LFS_SEGENTRY(sup, fs, sn, bp);
1451 isdirty = sup->su_flags & SEGUSE_DIRTY;
1452 brelse(bp);
1453 if (!isdirty)
1454 break;
1455 }
1456
1457 ++fs->lfs_nactive;
1458 fs->lfs_nextseg = sntod(fs, sn);
1459 if (lfs_dostats) {
1460 ++lfs_stats.segsused;
1461 }
1462 }
1463
1464 static struct buf **
1465 lookahead_pagemove(struct buf **bpp, int nblocks, size_t *size)
1466 {
1467 size_t maxsize;
1468 #ifndef LFS_NO_PAGEMOVE
1469 struct buf *bp;
1470 #endif
1471
1472 maxsize = *size;
1473 *size = 0;
1474 #ifdef LFS_NO_PAGEMOVE
1475 return bpp;
1476 #else
1477 while((bp = *bpp) != NULL && *size < maxsize && nblocks--) {
1478 if(bp->b_flags & B_CALL)
1479 return bpp;
1480 if(bp->b_bcount % NBPG)
1481 return bpp;
1482 *size += bp->b_bcount;
1483 ++bpp;
1484 }
1485 return NULL;
1486 #endif
1487 }
1488
1489 #define BQUEUES 4 /* XXX */
1490 #define BQ_EMPTY 3 /* XXX */
1491 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
1492
1493 #define BUFHASH(dvp, lbn) \
1494 (&bufhashtbl[((long)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash])
1495 extern LIST_HEAD(bufhashhdr, buf) invalhash;
1496 /*
1497 * Insq/Remq for the buffer hash lists.
1498 */
1499 #define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash)
1500 #define bremhash(bp) LIST_REMOVE(bp, b_hash)
1501
1502 static struct buf *
1503 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr, int n)
1504 {
1505 struct lfs_cluster *cl;
1506 struct buf **bpp, *bp;
1507 int s;
1508
1509 cl = (struct lfs_cluster *)malloc(sizeof(*cl), M_SEGMENT, M_WAITOK);
1510 bpp = (struct buf **)malloc(n*sizeof(*bpp), M_SEGMENT, M_WAITOK);
1511 memset(cl, 0, sizeof(*cl));
1512 cl->fs = fs;
1513 cl->bpp = bpp;
1514 cl->bufcount = 0;
1515 cl->bufsize = 0;
1516
1517 /* If this segment is being written synchronously, note that */
1518 if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
1519 cl->flags |= LFS_CL_SYNC;
1520 cl->seg = fs->lfs_sp;
1521 ++cl->seg->seg_iocount;
1522 /* printf("+ %x => %d\n", cl->seg, cl->seg->seg_iocount); */
1523 }
1524
1525 /* Get an empty buffer header, or maybe one with something on it */
1526 s = splbio();
1527 if((bp = bufqueues[BQ_EMPTY].tqh_first) != NULL) {
1528 bremfree(bp);
1529 /* clear out various other fields */
1530 bp->b_flags = B_BUSY;
1531 bp->b_dev = NODEV;
1532 bp->b_blkno = bp->b_lblkno = 0;
1533 bp->b_error = 0;
1534 bp->b_resid = 0;
1535 bp->b_bcount = 0;
1536
1537 /* nuke any credentials we were holding */
1538 /* XXXXXX */
1539
1540 bremhash(bp);
1541
1542 /* disassociate us from our vnode, if we had one... */
1543 if (bp->b_vp)
1544 brelvp(bp);
1545 }
1546 splx(s);
1547 while (!bp)
1548 bp = getnewbuf(0, 0);
1549 s = splbio();
1550 bgetvp(vp, bp);
1551 binshash(bp,&invalhash);
1552 splx(s);
1553 bp->b_bcount = 0;
1554 bp->b_blkno = bp->b_lblkno = addr;
1555
1556 bp->b_flags |= B_CALL;
1557 bp->b_iodone = lfs_cluster_callback;
1558 cl->saveaddr = bp->b_saveaddr; /* XXX is this ever used? */
1559 bp->b_saveaddr = (caddr_t)cl;
1560
1561 return bp;
1562 }
1563
1564 int
1565 lfs_writeseg(struct lfs *fs, struct segment *sp)
1566 {
1567 struct buf **bpp, *bp, *cbp, *newbp, **pmlastbpp;
1568 SEGUSE *sup;
1569 SEGSUM *ssp;
1570 dev_t i_dev;
1571 char *datap, *dp;
1572 int do_again, i, nblocks, s;
1573 size_t el_size;
1574 struct lfs_cluster *cl;
1575 int (*strategy)(void *);
1576 struct vop_strategy_args vop_strategy_a;
1577 u_short ninos;
1578 struct vnode *devvp;
1579 char *p;
1580 struct vnode *vp;
1581 struct inode *ip;
1582 size_t pmsize;
1583 int use_pagemove;
1584 daddr_t pseg_daddr;
1585 int32_t *daddrp; /* XXX ondisk32 */
1586 int changed;
1587 #if defined(DEBUG) && defined(LFS_PROPELLER)
1588 static int propeller;
1589 char propstring[4] = "-\\|/";
1590
1591 printf("%c\b",propstring[propeller++]);
1592 if (propeller == 4)
1593 propeller = 0;
1594 #endif
1595 pseg_daddr = (*(sp->bpp))->b_blkno;
1596
1597 /*
1598 * If there are no buffers other than the segment summary to write
1599 * and it is not a checkpoint, don't do anything. On a checkpoint,
1600 * even if there aren't any buffers, you need to write the superblock.
1601 */
1602 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1603 return (0);
1604
1605 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1606 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1607
1608 /* Update the segment usage information. */
1609 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1610
1611 /* Loop through all blocks, except the segment summary. */
1612 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1613 if ((*bpp)->b_vp != devvp) {
1614 sup->su_nbytes += (*bpp)->b_bcount;
1615 #ifdef DEBUG_SU_NBYTES
1616 printf("seg %d += %ld for ino %d lbn %d db 0x%x\n",
1617 sp->seg_number, (*bpp)->b_bcount,
1618 VTOI((*bpp)->b_vp)->i_number,
1619 (*bpp)->b_lblkno, (*bpp)->b_blkno);
1620 #endif
1621 }
1622 }
1623
1624 ssp = (SEGSUM *)sp->segsum;
1625
1626 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1627 #ifdef DEBUG_SU_NBYTES
1628 printf("seg %d += %d for %d inodes\n", /* XXXDEBUG */
1629 sp->seg_number, ssp->ss_ninos * DINODE_SIZE,
1630 ssp->ss_ninos);
1631 #endif
1632 sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
1633 /* sup->su_nbytes += fs->lfs_sumsize; */
1634 if (fs->lfs_version == 1)
1635 sup->su_olastmod = time.tv_sec;
1636 else
1637 sup->su_lastmod = time.tv_sec;
1638 sup->su_ninos += ninos;
1639 ++sup->su_nsums;
1640 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
1641 fs->lfs_ibsize));
1642 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
1643
1644 do_again = !(bp->b_flags & B_GATHERED);
1645 (void)LFS_BWRITE_LOG(bp); /* Ifile */
1646 /*
1647 * Mark blocks B_BUSY, to prevent then from being changed between
1648 * the checksum computation and the actual write.
1649 *
1650 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1651 * there are any, replace them with copies that have UNASSIGNED
1652 * instead.
1653 */
1654 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1655 ++bpp;
1656 if ((*bpp)->b_flags & B_CALL)
1657 continue;
1658 bp = *bpp;
1659 again:
1660 s = splbio();
1661 if (bp->b_flags & B_BUSY) {
1662 #ifdef DEBUG
1663 printf("lfs_writeseg: avoiding potential data "
1664 "summary corruption for ino %d, lbn %d\n",
1665 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
1666 #endif
1667 bp->b_flags |= B_WANTED;
1668 tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
1669 splx(s);
1670 goto again;
1671 }
1672 bp->b_flags |= B_BUSY;
1673 splx(s);
1674 /* Check and replace indirect block UNWRITTEN bogosity */
1675 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1676 VTOI(bp->b_vp)->i_ffs_blocks !=
1677 VTOI(bp->b_vp)->i_lfs_effnblks) {
1678 #ifdef DEBUG_LFS
1679 printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
1680 VTOI(bp->b_vp)->i_number,
1681 VTOI(bp->b_vp)->i_lfs_effnblks,
1682 VTOI(bp->b_vp)->i_ffs_blocks);
1683 #endif
1684 /* Make a copy we'll make changes to */
1685 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
1686 bp->b_bcount);
1687 newbp->b_blkno = bp->b_blkno;
1688 memcpy(newbp->b_data, bp->b_data,
1689 newbp->b_bcount);
1690 *bpp = newbp;
1691
1692 changed = 0;
1693 /* XXX ondisk32 */
1694 for (daddrp = (int32_t *)(newbp->b_data);
1695 daddrp < (int32_t *)(newbp->b_data +
1696 newbp->b_bcount); daddrp++) {
1697 if (*daddrp == UNWRITTEN) {
1698 ++changed;
1699 #ifdef DEBUG_LFS
1700 printf("lfs_writeseg: replacing UNWRITTEN\n");
1701 #endif
1702 *daddrp = 0;
1703 }
1704 }
1705 /*
1706 * Get rid of the old buffer. Don't mark it clean,
1707 * though, if it still has dirty data on it.
1708 */
1709 if (changed) {
1710 bp->b_flags &= ~(B_ERROR | B_GATHERED);
1711 if (bp->b_flags & B_CALL) {
1712 lfs_freebuf(bp);
1713 bp = NULL;
1714 } else {
1715 /* Still on free list, leave it there */
1716 s = splbio();
1717 bp->b_flags &= ~B_BUSY;
1718 if (bp->b_flags & B_WANTED)
1719 wakeup(bp);
1720 splx(s);
1721 /*
1722 * We have to re-decrement lfs_avail
1723 * since this block is going to come
1724 * back around to us in the next
1725 * segment.
1726 */
1727 fs->lfs_avail -= btofsb(fs, bp->b_bcount);
1728 }
1729 } else {
1730 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1731 B_GATHERED);
1732 if (bp->b_flags & B_CALL) {
1733 lfs_freebuf(bp);
1734 bp = NULL;
1735 } else {
1736 bremfree(bp);
1737 bp->b_flags |= B_DONE;
1738 s = splbio();
1739 reassignbuf(bp, bp->b_vp);
1740 splx(s);
1741 LFS_UNLOCK_BUF(bp);
1742 brelse(bp);
1743 }
1744 }
1745
1746 }
1747 }
1748 /*
1749 * Compute checksum across data and then across summary; the first
1750 * block (the summary block) is skipped. Set the create time here
1751 * so that it's guaranteed to be later than the inode mod times.
1752 *
1753 * XXX
1754 * Fix this to do it inline, instead of malloc/copy.
1755 */
1756 if (fs->lfs_version == 1)
1757 el_size = sizeof(u_long);
1758 else
1759 el_size = sizeof(u_int32_t);
1760 datap = dp = malloc(nblocks * el_size, M_SEGMENT, M_WAITOK);
1761 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1762 if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1763 if (copyin((*bpp)->b_saveaddr, dp, el_size))
1764 panic("lfs_writeseg: copyin failed [1]: "
1765 "ino %d blk %lld",
1766 VTOI((*bpp)->b_vp)->i_number,
1767 (long long)(*bpp)->b_lblkno);
1768 } else
1769 memcpy(dp, (*bpp)->b_data, el_size);
1770 dp += el_size;
1771 }
1772 if (fs->lfs_version == 1)
1773 ssp->ss_ocreate = time.tv_sec;
1774 else {
1775 ssp->ss_create = time.tv_sec;
1776 ssp->ss_serial = ++fs->lfs_serial;
1777 ssp->ss_ident = fs->lfs_ident;
1778 }
1779 #ifndef LFS_MALLOC_SUMMARY
1780 /* Set the summary block busy too */
1781 (*(sp->bpp))->b_flags |= B_BUSY;
1782 #endif
1783 ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
1784 ssp->ss_sumsum =
1785 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
1786 free(datap, M_SEGMENT);
1787 datap = dp = NULL;
1788 #ifdef DIAGNOSTIC
1789 if (fs->lfs_bfree < btofsb(fs, ninos * fs->lfs_ibsize) + btofsb(fs, fs->lfs_sumsize))
1790 panic("lfs_writeseg: No diskspace for summary");
1791 #endif
1792 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
1793 btofsb(fs, fs->lfs_sumsize));
1794
1795 strategy = devvp->v_op[VOFFSET(vop_strategy)];
1796
1797 /*
1798 * When we simply write the blocks we lose a rotation for every block
1799 * written. To avoid this problem, we use pagemove to cluster
1800 * the buffers into a chunk and write the chunk. CHUNKSIZE is the
1801 * largest size I/O devices can handle.
1802 *
1803 * XXX - right now MAXPHYS is only 64k; could it be larger?
1804 */
1805
1806 #define CHUNKSIZE MAXPHYS
1807
1808 if (devvp == NULL)
1809 panic("devvp is NULL");
1810 for (bpp = sp->bpp, i = nblocks; i;) {
1811 cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
1812 cl = (struct lfs_cluster *)cbp->b_saveaddr;
1813
1814 cbp->b_dev = i_dev;
1815 cbp->b_flags |= B_ASYNC | B_BUSY;
1816 cbp->b_bcount = 0;
1817
1818 /*
1819 * Find out if we can use pagemove to build the cluster,
1820 * or if we are stuck using malloc/copy. If this is the
1821 * first cluster, set the shift flag (see below).
1822 */
1823 pmsize = CHUNKSIZE;
1824 use_pagemove = 0;
1825 if(bpp == sp->bpp) {
1826 /* Summary blocks have to get special treatment */
1827 pmlastbpp = lookahead_pagemove(bpp + 1, i - 1, &pmsize);
1828 if(pmsize >= CHUNKSIZE - fs->lfs_sumsize ||
1829 pmlastbpp == NULL) {
1830 use_pagemove = 1;
1831 cl->flags |= LFS_CL_SHIFT;
1832 } else {
1833 /*
1834 * If we're not using pagemove, we have
1835 * to copy the summary down to the bottom
1836 * end of the block.
1837 */
1838 #ifndef LFS_MALLOC_SUMMARY
1839 memcpy((*bpp)->b_data, (*bpp)->b_data +
1840 NBPG - fs->lfs_sumsize,
1841 fs->lfs_sumsize);
1842 #endif /* LFS_MALLOC_SUMMARY */
1843 }
1844 } else {
1845 pmlastbpp = lookahead_pagemove(bpp, i, &pmsize);
1846 if(pmsize >= CHUNKSIZE || pmlastbpp == NULL) {
1847 use_pagemove = 1;
1848 }
1849 }
1850 if(use_pagemove == 0) {
1851 cl->flags |= LFS_CL_MALLOC;
1852 cl->olddata = cbp->b_data;
1853 cbp->b_data = malloc(CHUNKSIZE, M_SEGMENT, M_WAITOK);
1854 }
1855 #if defined(DEBUG) && defined(DIAGNOSTIC)
1856 if(dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno + btodb((*bpp)->b_bcount - 1))) !=
1857 dtosn(fs, dbtofsb(fs, cbp->b_blkno))) {
1858 printf("block at %x (%d), cbp at %x (%d)\n",
1859 (*bpp)->b_blkno, dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno)),
1860 cbp->b_blkno, dtosn(fs, dbtofsb(fs, cbp->b_blkno)));
1861 panic("lfs_writeseg: Segment overwrite");
1862 }
1863 #endif
1864
1865 /*
1866 * Construct the cluster.
1867 */
1868 while (fs->lfs_iocount >= LFS_THROTTLE) {
1869 #ifdef DEBUG_LFS
1870 printf("[%d]", fs->lfs_iocount);
1871 #endif
1872 tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs_throttle", 0);
1873 }
1874 ++fs->lfs_iocount;
1875
1876 for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
1877 bp = *bpp;
1878
1879 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1880 break;
1881
1882 /*
1883 * Fake buffers from the cleaner are marked as B_INVAL.
1884 * We need to copy the data from user space rather than
1885 * from the buffer indicated.
1886 * XXX == what do I do on an error?
1887 */
1888 if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1889 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1890 panic("lfs_writeseg: copyin failed [2]");
1891 } else if (use_pagemove) {
1892 pagemove(bp->b_data, p, bp->b_bcount);
1893 cbp->b_bufsize += bp->b_bcount;
1894 bp->b_bufsize -= bp->b_bcount;
1895 } else {
1896 bcopy(bp->b_data, p, bp->b_bcount);
1897 /* printf("copy in %p\n", bp->b_data); */
1898 }
1899
1900 /*
1901 * XXX If we are *not* shifting, the summary
1902 * block is only fs->lfs_sumsize. Otherwise,
1903 * it is NBPG but shifted.
1904 */
1905 if(bpp == sp->bpp && !(cl->flags & LFS_CL_SHIFT)) {
1906 p += fs->lfs_sumsize;
1907 cbp->b_bcount += fs->lfs_sumsize;
1908 cl->bufsize += fs->lfs_sumsize;
1909 } else {
1910 p += bp->b_bcount;
1911 cbp->b_bcount += bp->b_bcount;
1912 cl->bufsize += bp->b_bcount;
1913 }
1914 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | B_DONE);
1915 cl->bpp[cl->bufcount++] = bp;
1916 vp = bp->b_vp;
1917 s = splbio();
1918 ++vp->v_numoutput;
1919 splx(s);
1920
1921 /*
1922 * Although it cannot be freed for reuse before the
1923 * cluster is written to disk, this buffer does not
1924 * need to be held busy. Therefore we unbusy it,
1925 * while leaving it on the locked list. It will
1926 * be freed or requeued by the callback depending
1927 * on whether it has had B_DELWRI set again in the
1928 * meantime.
1929 *
1930 * If we are using pagemove, we have to hold the block
1931 * busy to prevent its contents from changing before
1932 * it hits the disk, and invalidating the checksum.
1933 */
1934 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
1935 #ifdef LFS_MNOBUSY
1936 if (cl->flags & LFS_CL_MALLOC) {
1937 if (!(bp->b_flags & B_CALL))
1938 brelse(bp); /* Still B_LOCKED */
1939 }
1940 #endif
1941 bpp++;
1942
1943 /*
1944 * If this is the last block for this vnode, but
1945 * there are other blocks on its dirty list,
1946 * set IN_MODIFIED/IN_CLEANING depending on what
1947 * sort of block. Only do this for our mount point,
1948 * not for, e.g., inode blocks that are attached to
1949 * the devvp.
1950 * XXX KS - Shouldn't we set *both* if both types
1951 * of blocks are present (traverse the dirty list?)
1952 */
1953 s = splbio();
1954 if ((i == 1 ||
1955 (i > 1 && vp && *bpp && (*bpp)->b_vp != vp)) &&
1956 (bp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL &&
1957 vp->v_mount == fs->lfs_ivnode->v_mount)
1958 {
1959 ip = VTOI(vp);
1960 #ifdef DEBUG_LFS
1961 printf("lfs_writeseg: marking ino %d\n",
1962 ip->i_number);
1963 #endif
1964 if (bp->b_flags & B_CALL)
1965 LFS_SET_UINO(ip, IN_CLEANING);
1966 else
1967 LFS_SET_UINO(ip, IN_MODIFIED);
1968 }
1969 splx(s);
1970 wakeup(vp);
1971 }
1972 s = splbio();
1973 ++cbp->b_vp->v_numoutput;
1974 splx(s);
1975 /*
1976 * In order to include the summary in a clustered block,
1977 * it may be necessary to shift the block forward (since
1978 * summary blocks are in generay smaller than can be
1979 * addressed by pagemove(). After the write, the block
1980 * will be corrected before disassembly.
1981 */
1982 if(cl->flags & LFS_CL_SHIFT) {
1983 cbp->b_data += (NBPG - fs->lfs_sumsize);
1984 cbp->b_bcount -= (NBPG - fs->lfs_sumsize);
1985 }
1986 vop_strategy_a.a_desc = VDESC(vop_strategy);
1987 vop_strategy_a.a_bp = cbp;
1988 (strategy)(&vop_strategy_a);
1989 }
1990
1991 if (lfs_dostats) {
1992 ++lfs_stats.psegwrites;
1993 lfs_stats.blocktot += nblocks - 1;
1994 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
1995 ++lfs_stats.psyncwrites;
1996 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
1997 ++lfs_stats.pcleanwrites;
1998 lfs_stats.cleanblocks += nblocks - 1;
1999 }
2000 }
2001 return (lfs_initseg(fs) || do_again);
2002 }
2003
2004 void
2005 lfs_writesuper(struct lfs *fs, daddr_t daddr)
2006 {
2007 struct buf *bp;
2008 dev_t i_dev;
2009 int (*strategy)(void *);
2010 int s;
2011 struct vop_strategy_args vop_strategy_a;
2012
2013 /*
2014 * If we can write one superblock while another is in
2015 * progress, we risk not having a complete checkpoint if we crash.
2016 * So, block here if a superblock write is in progress.
2017 */
2018 s = splbio();
2019 while (fs->lfs_sbactive) {
2020 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
2021 }
2022 fs->lfs_sbactive = daddr;
2023 splx(s);
2024 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
2025 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
2026
2027 /* Set timestamp of this version of the superblock */
2028 if (fs->lfs_version == 1)
2029 fs->lfs_otstamp = time.tv_sec;
2030 fs->lfs_tstamp = time.tv_sec;
2031
2032 /* Checksum the superblock and copy it into a buffer. */
2033 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
2034 bp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr), LFS_SBPAD);
2035 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
2036
2037 bp->b_dev = i_dev;
2038 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
2039 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
2040 bp->b_iodone = lfs_supercallback;
2041 /* XXX KS - same nasty hack as above */
2042 bp->b_saveaddr = (caddr_t)fs;
2043
2044 vop_strategy_a.a_desc = VDESC(vop_strategy);
2045 vop_strategy_a.a_bp = bp;
2046 s = splbio();
2047 ++bp->b_vp->v_numoutput;
2048 splx(s);
2049 ++fs->lfs_iocount;
2050 (strategy)(&vop_strategy_a);
2051 }
2052
2053 /*
2054 * Logical block number match routines used when traversing the dirty block
2055 * chain.
2056 */
2057 int
2058 lfs_match_fake(struct lfs *fs, struct buf *bp)
2059 {
2060 return (bp->b_flags & B_CALL);
2061 }
2062
2063 int
2064 lfs_match_data(struct lfs *fs, struct buf *bp)
2065 {
2066 return (bp->b_lblkno >= 0);
2067 }
2068
2069 int
2070 lfs_match_indir(struct lfs *fs, struct buf *bp)
2071 {
2072 daddr_t lbn;
2073
2074 lbn = bp->b_lblkno;
2075 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
2076 }
2077
2078 int
2079 lfs_match_dindir(struct lfs *fs, struct buf *bp)
2080 {
2081 daddr_t lbn;
2082
2083 lbn = bp->b_lblkno;
2084 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
2085 }
2086
2087 int
2088 lfs_match_tindir(struct lfs *fs, struct buf *bp)
2089 {
2090 daddr_t lbn;
2091
2092 lbn = bp->b_lblkno;
2093 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
2094 }
2095
2096 /*
2097 * XXX - The only buffers that are going to hit these functions are the
2098 * segment write blocks, or the segment summaries, or the superblocks.
2099 *
2100 * All of the above are created by lfs_newbuf, and so do not need to be
2101 * released via brelse.
2102 */
2103 void
2104 lfs_callback(struct buf *bp)
2105 {
2106 /* struct lfs *fs; */
2107 /* fs = (struct lfs *)bp->b_saveaddr; */
2108 lfs_freebuf(bp);
2109 }
2110
2111 static void
2112 lfs_super_aiodone(struct buf *bp)
2113 {
2114 struct lfs *fs;
2115
2116 fs = (struct lfs *)bp->b_saveaddr;
2117 fs->lfs_sbactive = 0;
2118 wakeup(&fs->lfs_sbactive);
2119 if (--fs->lfs_iocount < LFS_THROTTLE)
2120 wakeup(&fs->lfs_iocount);
2121 lfs_freebuf(bp);
2122 }
2123
2124 static void
2125 lfs_cluster_aiodone(struct buf *bp)
2126 {
2127 struct lfs_cluster *cl;
2128 struct lfs *fs;
2129 struct buf *tbp;
2130 struct vnode *vp;
2131 int s, error=0;
2132 char *cp;
2133 extern int locked_queue_count;
2134 extern long locked_queue_bytes;
2135
2136 if(bp->b_flags & B_ERROR)
2137 error = bp->b_error;
2138
2139 cl = (struct lfs_cluster *)bp->b_saveaddr;
2140 fs = cl->fs;
2141 bp->b_saveaddr = cl->saveaddr;
2142
2143 /* If shifted, shift back now */
2144 if(cl->flags & LFS_CL_SHIFT) {
2145 bp->b_data -= (NBPG - fs->lfs_sumsize);
2146 bp->b_bcount += (NBPG - fs->lfs_sumsize);
2147 }
2148
2149 cp = (char *)bp->b_data + cl->bufsize;
2150 /* Put the pages back, and release the buffer */
2151 while(cl->bufcount--) {
2152 tbp = cl->bpp[cl->bufcount];
2153 if(!(cl->flags & LFS_CL_MALLOC)) {
2154 cp -= tbp->b_bcount;
2155 printf("pm(%p,%p,%lx)",cp,tbp->b_data,tbp->b_bcount);
2156 pagemove(cp, tbp->b_data, tbp->b_bcount);
2157 bp->b_bufsize -= tbp->b_bcount;
2158 tbp->b_bufsize += tbp->b_bcount;
2159 }
2160 if(error) {
2161 tbp->b_flags |= B_ERROR;
2162 tbp->b_error = error;
2163 }
2164
2165 /*
2166 * We're done with tbp. If it has not been re-dirtied since
2167 * the cluster was written, free it. Otherwise, keep it on
2168 * the locked list to be written again.
2169 */
2170 if ((tbp->b_flags & (B_LOCKED | B_DELWRI)) == B_LOCKED)
2171 LFS_UNLOCK_BUF(tbp);
2172 tbp->b_flags &= ~B_GATHERED;
2173
2174 LFS_BCLEAN_LOG(fs, tbp);
2175
2176 vp = tbp->b_vp;
2177 /* Segment summary for a shifted cluster */
2178 if(!cl->bufcount && (cl->flags & LFS_CL_SHIFT))
2179 tbp->b_flags |= B_INVAL;
2180 if(!(tbp->b_flags & B_CALL)) {
2181 bremfree(tbp);
2182 s = splbio();
2183 if(vp)
2184 reassignbuf(tbp, vp);
2185 splx(s);
2186 tbp->b_flags |= B_ASYNC; /* for biodone */
2187 }
2188 #ifdef DIAGNOSTIC
2189 if (tbp->b_flags & B_DONE) {
2190 printf("blk %d biodone already (flags %lx)\n",
2191 cl->bufcount, (long)tbp->b_flags);
2192 }
2193 #endif
2194 if (tbp->b_flags & (B_BUSY | B_CALL)) {
2195 biodone(tbp);
2196 }
2197 }
2198
2199 /* Fix up the cluster buffer, and release it */
2200 if(!(cl->flags & LFS_CL_MALLOC) && bp->b_bufsize) {
2201 printf("PM(%p,%p,%lx)", (char *)bp->b_data + bp->b_bcount,
2202 (char *)bp->b_data, bp->b_bufsize);
2203 pagemove((char *)bp->b_data + bp->b_bcount,
2204 (char *)bp->b_data, bp->b_bufsize);
2205 }
2206 if(cl->flags & LFS_CL_MALLOC) {
2207 free(bp->b_data, M_SEGMENT);
2208 bp->b_data = cl->olddata;
2209 }
2210 bp->b_bcount = 0;
2211 bp->b_iodone = NULL;
2212 bp->b_flags &= ~B_DELWRI;
2213 bp->b_flags |= B_DONE;
2214 s = splbio();
2215 reassignbuf(bp, bp->b_vp);
2216 splx(s);
2217 brelse(bp);
2218
2219 /* Note i/o done */
2220 if (cl->flags & LFS_CL_SYNC) {
2221 if (--cl->seg->seg_iocount == 0)
2222 wakeup(&cl->seg->seg_iocount);
2223 /* printf("- %x => %d\n", cl->seg, cl->seg->seg_iocount); */
2224 }
2225 #ifdef DIAGNOSTIC
2226 if (fs->lfs_iocount == 0)
2227 panic("lfs_cluster_aiodone: zero iocount");
2228 #endif
2229 if (--fs->lfs_iocount < LFS_THROTTLE)
2230 wakeup(&fs->lfs_iocount);
2231 #if 0
2232 if (fs->lfs_iocount == 0) {
2233 /*
2234 * Vinvalbuf can move locked buffers off the locked queue
2235 * and we have no way of knowing about this. So, after
2236 * doing a big write, we recalculate how many buffers are
2237 * really still left on the locked queue.
2238 */
2239 lfs_countlocked(&locked_queue_count, &locked_queue_bytes, "lfs_cluster_callback");
2240 wakeup(&locked_queue_count);
2241 }
2242 #endif
2243
2244 free(cl->bpp, M_SEGMENT);
2245 free(cl, M_SEGMENT);
2246 }
2247
2248 static void
2249 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
2250 {
2251 /* reset b_iodone for when this is a single-buf i/o. */
2252 bp->b_iodone = aiodone;
2253
2254 simple_lock(&uvm.aiodoned_lock); /* locks uvm.aio_done */
2255 TAILQ_INSERT_TAIL(&uvm.aio_done, bp, b_freelist);
2256 wakeup(&uvm.aiodoned);
2257 simple_unlock(&uvm.aiodoned_lock);
2258 }
2259
2260 static void
2261 lfs_cluster_callback(struct buf *bp)
2262 {
2263 lfs_generic_callback(bp, lfs_cluster_aiodone);
2264 }
2265
2266 void
2267 lfs_supercallback(struct buf *bp)
2268 {
2269 lfs_generic_callback(bp, lfs_super_aiodone);
2270 }
2271
2272 /*
2273 * Shellsort (diminishing increment sort) from Data Structures and
2274 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
2275 * see also Knuth Vol. 3, page 84. The increments are selected from
2276 * formula (8), page 95. Roughly O(N^3/2).
2277 */
2278 /*
2279 * This is our own private copy of shellsort because we want to sort
2280 * two parallel arrays (the array of buffer pointers and the array of
2281 * logical block numbers) simultaneously. Note that we cast the array
2282 * of logical block numbers to a unsigned in this routine so that the
2283 * negative block numbers (meta data blocks) sort AFTER the data blocks.
2284 */
2285
2286 void
2287 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb)
2288 {
2289 static int __rsshell_increments[] = { 4, 1, 0 };
2290 int incr, *incrp, t1, t2;
2291 struct buf *bp_temp;
2292 u_long lb_temp;
2293
2294 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
2295 for (t1 = incr; t1 < nmemb; ++t1)
2296 for (t2 = t1 - incr; t2 >= 0;)
2297 if (lb_array[t2] > lb_array[t2 + incr]) {
2298 lb_temp = lb_array[t2];
2299 lb_array[t2] = lb_array[t2 + incr];
2300 lb_array[t2 + incr] = lb_temp;
2301 bp_temp = bp_array[t2];
2302 bp_array[t2] = bp_array[t2 + incr];
2303 bp_array[t2 + incr] = bp_temp;
2304 t2 -= incr;
2305 } else
2306 break;
2307 }
2308
2309 /*
2310 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
2311 */
2312 int
2313 lfs_vref(struct vnode *vp)
2314 {
2315 /*
2316 * If we return 1 here during a flush, we risk vinvalbuf() not
2317 * being able to flush all of the pages from this vnode, which
2318 * will cause it to panic. So, return 0 if a flush is in progress.
2319 */
2320 if (vp->v_flag & VXLOCK) {
2321 if (IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2322 return 0;
2323 }
2324 return (1);
2325 }
2326 return (vget(vp, 0));
2327 }
2328
2329 /*
2330 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
2331 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
2332 */
2333 void
2334 lfs_vunref(struct vnode *vp)
2335 {
2336 /*
2337 * Analogous to lfs_vref, if the node is flushing, fake it.
2338 */
2339 if ((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2340 return;
2341 }
2342
2343 simple_lock(&vp->v_interlock);
2344 #ifdef DIAGNOSTIC
2345 if (vp->v_usecount <= 0) {
2346 printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
2347 printf("lfs_vunref: flags are 0x%lx\n", (u_long)vp->v_flag);
2348 printf("lfs_vunref: usecount = %ld\n", (long)vp->v_usecount);
2349 panic("lfs_vunref: v_usecount<0");
2350 }
2351 #endif
2352 vp->v_usecount--;
2353 if (vp->v_usecount > 0) {
2354 simple_unlock(&vp->v_interlock);
2355 return;
2356 }
2357 /*
2358 * insert at tail of LRU list
2359 */
2360 simple_lock(&vnode_free_list_slock);
2361 if (vp->v_holdcnt > 0)
2362 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2363 else
2364 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2365 simple_unlock(&vnode_free_list_slock);
2366 simple_unlock(&vp->v_interlock);
2367 }
2368
2369 /*
2370 * We use this when we have vnodes that were loaded in solely for cleaning.
2371 * There is no reason to believe that these vnodes will be referenced again
2372 * soon, since the cleaning process is unrelated to normal filesystem
2373 * activity. Putting cleaned vnodes at the tail of the list has the effect
2374 * of flushing the vnode LRU. So, put vnodes that were loaded only for
2375 * cleaning at the head of the list, instead.
2376 */
2377 void
2378 lfs_vunref_head(struct vnode *vp)
2379 {
2380 simple_lock(&vp->v_interlock);
2381 #ifdef DIAGNOSTIC
2382 if (vp->v_usecount == 0) {
2383 panic("lfs_vunref: v_usecount<0");
2384 }
2385 #endif
2386 vp->v_usecount--;
2387 if (vp->v_usecount > 0) {
2388 simple_unlock(&vp->v_interlock);
2389 return;
2390 }
2391 /*
2392 * insert at head of LRU list
2393 */
2394 simple_lock(&vnode_free_list_slock);
2395 if (vp->v_holdcnt > 0)
2396 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2397 else
2398 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2399 simple_unlock(&vnode_free_list_slock);
2400 simple_unlock(&vp->v_interlock);
2401 }
2402
2403