lfs_segment.c revision 1.93 1 /* $NetBSD: lfs_segment.c,v 1.93 2003/01/25 18:12:33 tron 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.93 2003/01/25 18:12:33 tron 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 %" PRId64 ", "
652 "flags 0x%lx\n",
653 bp, (long long)bp->b_lblkno,
654 bp->b_flags);
655 }
656 }
657 if (dopanic)
658 panic("dirty blocks");
659 splx(s);
660 }
661 LFS_CLR_UINO(ip, IN_ALLMOD);
662 } else {
663 (void) lfs_writeseg(fs, sp);
664 }
665
666 /*
667 * If the I/O count is non-zero, sleep until it reaches zero.
668 * At the moment, the user's process hangs around so we can
669 * sleep.
670 */
671 fs->lfs_doifile = 0;
672 if (writer_set && --fs->lfs_writer == 0)
673 wakeup(&fs->lfs_dirops);
674
675 /*
676 * If we didn't write the Ifile, we didn't really do anything.
677 * That means that (1) there is a checkpoint on disk and (2)
678 * nothing has changed since it was written.
679 *
680 * Take the flags off of the segment so that lfs_segunlock
681 * doesn't have to write the superblock either.
682 */
683 if (do_ckp && !did_ckp) {
684 sp->seg_flags &= ~SEGM_CKP;
685 /* if (do_ckp) printf("lfs_segwrite: no checkpoint\n"); */
686 }
687
688 if (lfs_dostats) {
689 ++lfs_stats.nwrites;
690 if (sp->seg_flags & SEGM_SYNC)
691 ++lfs_stats.nsync_writes;
692 if (sp->seg_flags & SEGM_CKP)
693 ++lfs_stats.ncheckpoints;
694 }
695 lfs_segunlock(fs);
696 return (0);
697 }
698
699 /*
700 * Write the dirty blocks associated with a vnode.
701 */
702 void
703 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp)
704 {
705 struct buf *bp;
706 struct finfo *fip;
707 struct inode *ip;
708 IFILE *ifp;
709 int i, frag;
710
711 ip = VTOI(vp);
712
713 if (sp->seg_bytes_left < fs->lfs_bsize ||
714 sp->sum_bytes_left < sizeof(struct finfo))
715 (void) lfs_writeseg(fs, sp);
716
717 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
718 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
719
720 if (vp->v_flag & VDIROP)
721 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
722
723 fip = sp->fip;
724 fip->fi_nblocks = 0;
725 fip->fi_ino = ip->i_number;
726 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
727 fip->fi_version = ifp->if_version;
728 brelse(bp);
729
730 if (sp->seg_flags & SEGM_CLEAN) {
731 lfs_gather(fs, sp, vp, lfs_match_fake);
732 /*
733 * For a file being flushed, we need to write *all* blocks.
734 * This means writing the cleaning blocks first, and then
735 * immediately following with any non-cleaning blocks.
736 * The same is true of the Ifile since checkpoints assume
737 * that all valid Ifile blocks are written.
738 */
739 if (IS_FLUSHING(fs,vp) || vp == fs->lfs_ivnode)
740 lfs_gather(fs, sp, vp, lfs_match_data);
741 } else
742 lfs_gather(fs, sp, vp, lfs_match_data);
743
744 /*
745 * It may not be necessary to write the meta-data blocks at this point,
746 * as the roll-forward recovery code should be able to reconstruct the
747 * list.
748 *
749 * We have to write them anyway, though, under two conditions: (1) the
750 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
751 * checkpointing.
752 *
753 * BUT if we are cleaning, we might have indirect blocks that refer to
754 * new blocks not being written yet, in addition to fragments being
755 * moved out of a cleaned segment. If that is the case, don't
756 * write the indirect blocks, or the finfo will have a small block
757 * in the middle of it!
758 * XXX in this case isn't the inode size wrong too?
759 */
760 frag = 0;
761 if (sp->seg_flags & SEGM_CLEAN) {
762 for (i = 0; i < NDADDR; i++)
763 if (ip->i_lfs_fragsize[i] > 0 &&
764 ip->i_lfs_fragsize[i] < fs->lfs_bsize)
765 ++frag;
766 }
767 #ifdef DIAGNOSTIC
768 if (frag > 1)
769 panic("lfs_writefile: more than one fragment!");
770 #endif
771 if (IS_FLUSHING(fs, vp) ||
772 (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) {
773 lfs_gather(fs, sp, vp, lfs_match_indir);
774 lfs_gather(fs, sp, vp, lfs_match_dindir);
775 lfs_gather(fs, sp, vp, lfs_match_tindir);
776 }
777 fip = sp->fip;
778 if (fip->fi_nblocks != 0) {
779 sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
780 sizeof(daddr_t) * (fip->fi_nblocks-1));
781 sp->start_lbp = &sp->fip->fi_blocks[0];
782 } else {
783 sp->sum_bytes_left += sizeof(FINFO) - sizeof(daddr_t);
784 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
785 }
786 }
787
788 int
789 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
790 {
791 struct buf *bp, *ibp;
792 struct dinode *cdp;
793 IFILE *ifp;
794 SEGUSE *sup;
795 daddr_t daddr;
796 int32_t *daddrp; /* XXX ondisk32 */
797 ino_t ino;
798 int error, i, ndx, fsb = 0;
799 int redo_ifile = 0;
800 struct timespec ts;
801 int gotblk = 0;
802
803 if (!(ip->i_flag & IN_ALLMOD))
804 return (0);
805
806 /* Allocate a new inode block if necessary. */
807 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) && sp->ibp == NULL) {
808 /* Allocate a new segment if necessary. */
809 if (sp->seg_bytes_left < fs->lfs_ibsize ||
810 sp->sum_bytes_left < sizeof(daddr_t))
811 (void) lfs_writeseg(fs, sp);
812
813 /* Get next inode block. */
814 daddr = fs->lfs_offset;
815 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
816 sp->ibp = *sp->cbpp++ =
817 getblk(VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr),
818 fs->lfs_ibsize, 0, 0);
819 gotblk++;
820
821 /* Zero out inode numbers */
822 for (i = 0; i < INOPB(fs); ++i)
823 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
824
825 ++sp->start_bpp;
826 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
827 /* Set remaining space counters. */
828 sp->seg_bytes_left -= fs->lfs_ibsize;
829 sp->sum_bytes_left -= sizeof(daddr_t);
830 ndx = fs->lfs_sumsize / sizeof(daddr_t) -
831 sp->ninodes / INOPB(fs) - 1;
832 /* fvdl -- not on disk, should be ok */
833 ((daddr_t *)(sp->segsum))[ndx] = daddr;
834 }
835
836 /* Update the inode times and copy the inode onto the inode page. */
837 TIMEVAL_TO_TIMESPEC(&time, &ts);
838 /* XXX kludge --- don't redirty the ifile just to put times on it */
839 if (ip->i_number != LFS_IFILE_INUM)
840 LFS_ITIMES(ip, &ts, &ts, &ts);
841
842 /*
843 * If this is the Ifile, and we've already written the Ifile in this
844 * partial segment, just overwrite it (it's not on disk yet) and
845 * continue.
846 *
847 * XXX we know that the bp that we get the second time around has
848 * already been gathered.
849 */
850 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
851 *(sp->idp) = ip->i_din.ffs_din;
852 ip->i_lfs_osize = ip->i_ffs_size;
853 return 0;
854 }
855
856 bp = sp->ibp;
857 cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
858 *cdp = ip->i_din.ffs_din;
859 #ifdef LFS_IFILE_FRAG_ADDRESSING
860 if (fs->lfs_version > 1)
861 fsb = (sp->ninodes % INOPB(fs)) / INOPF(fs);
862 #endif
863
864 /*
865 * If we are cleaning, ensure that we don't write UNWRITTEN disk
866 * addresses to disk; possibly revert the inode size.
867 */
868 if (ip->i_lfs_effnblks != ip->i_ffs_blocks) {
869 cdp->di_size = ip->i_lfs_osize;
870 #ifdef DEBUG_LFS
871 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
872 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks);
873 #endif
874 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
875 daddrp++) {
876 if (*daddrp == UNWRITTEN) {
877 #ifdef DEBUG_LFS
878 printf("lfs_writeinode: wiping UNWRITTEN\n");
879 #endif
880 *daddrp = 0;
881 }
882 }
883 } else {
884 /* If all blocks are goig to disk, update the "size on disk" */
885 ip->i_lfs_osize = ip->i_ffs_size;
886 }
887
888 if (ip->i_flag & IN_CLEANING)
889 LFS_CLR_UINO(ip, IN_CLEANING);
890 else {
891 /* XXX IN_ALLMOD */
892 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
893 IN_UPDATE);
894 if (ip->i_lfs_effnblks == ip->i_ffs_blocks)
895 LFS_CLR_UINO(ip, IN_MODIFIED);
896 #ifdef DEBUG_LFS
897 else
898 printf("lfs_writeinode: ino %d: real blks=%d, "
899 "eff=%d\n", ip->i_number, ip->i_ffs_blocks,
900 ip->i_lfs_effnblks);
901 #endif
902 }
903
904 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
905 sp->idp = ((struct dinode *)bp->b_data) +
906 (sp->ninodes % INOPB(fs));
907 if (gotblk) {
908 LFS_LOCK_BUF(bp);
909 brelse(bp);
910 }
911
912 /* Increment inode count in segment summary block. */
913 ++((SEGSUM *)(sp->segsum))->ss_ninos;
914
915 /* If this page is full, set flag to allocate a new page. */
916 if (++sp->ninodes % INOPB(fs) == 0)
917 sp->ibp = NULL;
918
919 /*
920 * If updating the ifile, update the super-block. Update the disk
921 * address and access times for this inode in the ifile.
922 */
923 ino = ip->i_number;
924 if (ino == LFS_IFILE_INUM) {
925 daddr = fs->lfs_idaddr;
926 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
927 } else {
928 LFS_IENTRY(ifp, fs, ino, ibp);
929 daddr = ifp->if_daddr;
930 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
931 #ifdef LFS_DEBUG_NEXTFREE
932 if (ino > 3 && ifp->if_nextfree) {
933 vprint("lfs_writeinode",ITOV(ip));
934 printf("lfs_writeinode: updating free ino %d\n",
935 ip->i_number);
936 }
937 #endif
938 error = LFS_BWRITE_LOG(ibp); /* Ifile */
939 }
940
941 /*
942 * The inode's last address should not be in the current partial
943 * segment, except under exceptional circumstances (lfs_writevnodes
944 * had to start over, and in the meantime more blocks were written
945 * to a vnode). Both inodes will be accounted to this segment
946 * in lfs_writeseg so we need to subtract the earlier version
947 * here anyway. The segment count can temporarily dip below
948 * zero here; keep track of how many duplicates we have in
949 * "dupino" so we don't panic below.
950 */
951 if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno)) {
952 ++sp->ndupino;
953 printf("lfs_writeinode: last inode addr in current pseg "
954 "(ino %d daddr 0x%llx) ndupino=%d\n", ino,
955 (long long)daddr, sp->ndupino);
956 }
957 /*
958 * Account the inode: it no longer belongs to its former segment,
959 * though it will not belong to the new segment until that segment
960 * is actually written.
961 */
962 if (daddr != LFS_UNUSED_DADDR) {
963 u_int32_t oldsn = dtosn(fs, daddr);
964 #ifdef DIAGNOSTIC
965 int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0;
966 #endif
967 LFS_SEGENTRY(sup, fs, oldsn, bp);
968 #ifdef DIAGNOSTIC
969 if (sup->su_nbytes + DINODE_SIZE * ndupino < DINODE_SIZE) {
970 printf("lfs_writeinode: negative bytes "
971 "(segment %" PRId64 " short by %d, "
972 "oldsn=%u, cursn=%u, daddr=%d, su_nbytes=%u, "
973 "ndupino=%d)\n",
974 dtosn(fs, daddr),
975 (int)DINODE_SIZE * (1 - sp->ndupino)
976 - sup->su_nbytes,
977 (unsigned int)oldsn,
978 (unsigned int)sp->seg_number,
979 (int)daddr,
980 (unsigned int)sup->su_nbytes,
981 sp->ndupino);
982 panic("lfs_writeinode: negative bytes");
983 sup->su_nbytes = DINODE_SIZE;
984 }
985 #endif
986 #ifdef DEBUG_SU_NBYTES
987 printf("seg %d -= %d for ino %d inode\n",
988 dtosn(fs, daddr), DINODE_SIZE, ino);
989 #endif
990 sup->su_nbytes -= DINODE_SIZE;
991 redo_ifile =
992 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
993 if (redo_ifile)
994 fs->lfs_flags |= LFS_IFDIRTY;
995 error = LFS_BWRITE_LOG(bp); /* Ifile */
996 }
997 return (redo_ifile);
998 }
999
1000 int
1001 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
1002 {
1003 struct lfs *fs;
1004 int version;
1005
1006 /*
1007 * If full, finish this segment. We may be doing I/O, so
1008 * release and reacquire the splbio().
1009 */
1010 #ifdef DIAGNOSTIC
1011 if (sp->vp == NULL)
1012 panic ("lfs_gatherblock: Null vp in segment");
1013 #endif
1014 fs = sp->fs;
1015 if (sp->sum_bytes_left < sizeof(daddr_t) ||
1016 sp->seg_bytes_left < bp->b_bcount) {
1017 if (sptr)
1018 splx(*sptr);
1019 lfs_updatemeta(sp);
1020
1021 version = sp->fip->fi_version;
1022 (void) lfs_writeseg(fs, sp);
1023
1024 sp->fip->fi_version = version;
1025 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
1026 /* Add the current file to the segment summary. */
1027 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1028 sp->sum_bytes_left -=
1029 sizeof(struct finfo) - sizeof(daddr_t);
1030
1031 if (sptr)
1032 *sptr = splbio();
1033 return (1);
1034 }
1035
1036 #ifdef DEBUG
1037 if (bp->b_flags & B_GATHERED) {
1038 printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
1039 sp->fip->fi_ino, bp->b_lblkno);
1040 return (0);
1041 }
1042 #endif
1043 /* Insert into the buffer list, update the FINFO block. */
1044 bp->b_flags |= B_GATHERED;
1045 bp->b_flags &= ~B_DONE;
1046
1047 *sp->cbpp++ = bp;
1048 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
1049
1050 sp->sum_bytes_left -= sizeof(daddr_t);
1051 sp->seg_bytes_left -= bp->b_bcount;
1052 return (0);
1053 }
1054
1055 int
1056 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp, int (*match)(struct lfs *, struct buf *))
1057 {
1058 struct buf *bp, *nbp;
1059 int s, count = 0;
1060
1061 sp->vp = vp;
1062 s = splbio();
1063
1064 #ifndef LFS_NO_BACKBUF_HACK
1065 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
1066 # define BUF_OFFSET (((caddr_t)&LIST_NEXT(bp, b_vnbufs)) - (caddr_t)bp)
1067 # define BACK_BUF(BP) ((struct buf *)(((caddr_t)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
1068 # define BEG_OF_LIST ((struct buf *)(((caddr_t)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
1069 /* Find last buffer. */
1070 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp && LIST_NEXT(bp, b_vnbufs) != NULL;
1071 bp = LIST_NEXT(bp, b_vnbufs));
1072 for (; bp && bp != BEG_OF_LIST; bp = nbp) {
1073 nbp = BACK_BUF(bp);
1074 #else /* LFS_NO_BACKBUF_HACK */
1075 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1076 nbp = LIST_NEXT(bp, b_vnbufs);
1077 #endif /* LFS_NO_BACKBUF_HACK */
1078 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) {
1079 #ifdef DEBUG_LFS
1080 if (vp == fs->lfs_ivnode && (bp->b_flags & (B_BUSY|B_GATHERED)) == B_BUSY)
1081 printf("(%d:%lx)", bp->b_lblkno, bp->b_flags);
1082 #endif
1083 continue;
1084 }
1085 if (vp->v_type == VBLK) {
1086 /* For block devices, just write the blocks. */
1087 /* XXX Do we really need to even do this? */
1088 #ifdef DEBUG_LFS
1089 if (count == 0)
1090 printf("BLK(");
1091 printf(".");
1092 #endif
1093 /* Get the block before bwrite, so we don't corrupt the free list */
1094 bp->b_flags |= B_BUSY;
1095 bremfree(bp);
1096 bwrite(bp);
1097 } else {
1098 #ifdef DIAGNOSTIC
1099 if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
1100 printf("lfs_gather: lbn %" PRId64 " is "
1101 "B_INVAL\n", bp->b_lblkno);
1102 VOP_PRINT(bp->b_vp);
1103 }
1104 if (!(bp->b_flags & B_DELWRI))
1105 panic("lfs_gather: bp not B_DELWRI");
1106 if (!(bp->b_flags & B_LOCKED)) {
1107 printf("lfs_gather: lbn %" PRId64 " blk "
1108 "%" PRId64 " not B_LOCKED\n",
1109 bp->b_lblkno,
1110 dbtofsb(fs, bp->b_blkno));
1111 VOP_PRINT(bp->b_vp);
1112 panic("lfs_gather: bp not B_LOCKED");
1113 }
1114 #endif
1115 if (lfs_gatherblock(sp, bp, &s)) {
1116 goto loop;
1117 }
1118 }
1119 count++;
1120 }
1121 splx(s);
1122 #ifdef DEBUG_LFS
1123 if (vp->v_type == VBLK && count)
1124 printf(")\n");
1125 #endif
1126 lfs_updatemeta(sp);
1127 sp->vp = NULL;
1128 return count;
1129 }
1130
1131 /*
1132 * Update the metadata that points to the blocks listed in the FINFO
1133 * array.
1134 */
1135 void
1136 lfs_updatemeta(struct segment *sp)
1137 {
1138 SEGUSE *sup;
1139 struct buf *bp, *sbp;
1140 struct lfs *fs;
1141 struct vnode *vp;
1142 struct indir a[NIADDR + 2], *ap;
1143 struct inode *ip;
1144 daddr_t daddr, lbn, off;
1145 daddr_t ooff;
1146 int error, i, nblocks, num;
1147 int bb, osize, obb;
1148
1149 vp = sp->vp;
1150 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1151 if (nblocks < 0)
1152 panic("This is a bad thing");
1153 if (vp == NULL || nblocks == 0)
1154 return;
1155
1156 /* Sort the blocks. */
1157 /*
1158 * XXX KS - We have to sort even if the blocks come from the
1159 * cleaner, because there might be other pending blocks on the
1160 * same inode...and if we don't sort, and there are fragments
1161 * present, blocks may be written in the wrong place.
1162 */
1163 /* if (!(sp->seg_flags & SEGM_CLEAN)) */
1164 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
1165
1166 /*
1167 * Record the length of the last block in case it's a fragment.
1168 * If there are indirect blocks present, they sort last. An
1169 * indirect block will be lfs_bsize and its presence indicates
1170 * that you cannot have fragments.
1171 *
1172 * XXX This last is a lie. A cleaned fragment can coexist with
1173 * XXX a later indirect block. This will continue to be
1174 * XXX true until lfs_markv is fixed to do everything with
1175 * XXX fake blocks (including fake inodes and fake indirect blocks).
1176 */
1177 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
1178
1179 /*
1180 * Assign disk addresses, and update references to the logical
1181 * block and the segment usage information.
1182 */
1183 fs = sp->fs;
1184 for (i = nblocks; i--; ++sp->start_bpp) {
1185 lbn = *sp->start_lbp++;
1186 sbp = *sp->start_bpp;
1187
1188 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1189 off = fs->lfs_offset;
1190 if (sbp->b_blkno == sbp->b_lblkno) {
1191 printf("lfs_updatemeta: ino %d blk %" PRId64
1192 " has same lbn and daddr\n",
1193 VTOI(vp)->i_number, (long long)off);
1194 }
1195
1196 /*
1197 * If we write a frag in the wrong place, the cleaner won't
1198 * be able to correctly identify its size later, and the
1199 * segment will be uncleanable. (Even worse, it will assume
1200 * that the indirect block that actually ends the list
1201 * is of a smaller size!)
1202 */
1203 if (sbp->b_bcount < fs->lfs_bsize && i != 0)
1204 panic("lfs_updatemeta: fragment is not last block");
1205
1206 bb = fragstofsb(fs, numfrags(fs, sbp->b_bcount));
1207 fs->lfs_offset += bb;
1208 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
1209 if (daddr > 0)
1210 daddr = dbtofsb(fs, daddr);
1211 if (error)
1212 panic("lfs_updatemeta: ufs_bmaparray %d", error);
1213 ip = VTOI(vp);
1214 switch (num) {
1215 case 0:
1216 ooff = ip->i_ffs_db[lbn];
1217 #ifdef DEBUG
1218 if (ooff == 0) {
1219 printf("lfs_updatemeta[1]: warning: writing "
1220 "ino %d lbn %" PRId64 " at 0x%x, :"
1221 "was 0x0\n", ip->i_number, lbn, off);
1222 }
1223 #endif
1224 if (ooff == UNWRITTEN)
1225 ip->i_ffs_blocks += bb;
1226 else {
1227 /* possible fragment truncation or extension */
1228 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
1229 ip->i_ffs_blocks += (bb - obb);
1230 }
1231 ip->i_ffs_db[lbn] = off;
1232 break;
1233 case 1:
1234 ooff = ip->i_ffs_ib[a[0].in_off];
1235 #ifdef DEBUG
1236 if (ooff == 0) {
1237 printf("lfs_updatemeta[2]: warning: writing "
1238 "ino %d lbn %" PRId64 " at 0x%x, was "
1239 "0x0\n", ip->i_number, lbn, off);
1240 }
1241 #endif
1242 if (ooff == UNWRITTEN)
1243 ip->i_ffs_blocks += bb;
1244 ip->i_ffs_ib[a[0].in_off] = off;
1245 break;
1246 default:
1247 ap = &a[num - 1];
1248 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1249 panic("lfs_updatemeta: bread bno %" PRId64,
1250 (long long)ap->in_lbn);
1251
1252 /* XXX ondisk32 */
1253 ooff = ((int32_t *)bp->b_data)[ap->in_off];
1254 #if DEBUG
1255 if (ooff == 0) {
1256 printf("lfs_updatemeta[3]: warning: writing "
1257 "ino %d lbn %" PRId64 " at 0x%x, was "
1258 "0x0\n", ip->i_number, lbn, off);
1259 }
1260 #endif
1261 if (ooff == UNWRITTEN)
1262 ip->i_ffs_blocks += bb;
1263 /* XXX ondisk32 */
1264 ((int32_t *)bp->b_data)[ap->in_off] = off;
1265 (void) VOP_BWRITE(bp);
1266 }
1267 #ifdef DEBUG
1268 if (daddr >= fs->lfs_lastpseg && daddr <= off) {
1269 printf("lfs_updatemeta: ino %d, lbn %" PRId64 ", "
1270 "addr = %x in same pseg\n",
1271 VTOI(sp->vp)->i_number, sbp->b_lblkno, daddr);
1272 }
1273 #endif
1274 /*
1275 * Update segment usage information, based on old size
1276 * and location.
1277 */
1278 if (daddr > 0) {
1279 u_int32_t oldsn = dtosn(fs, daddr);
1280 #ifdef DIAGNOSTIC
1281 int ndupino = (sp->seg_number == oldsn) ?
1282 sp->ndupino : 0;
1283 #endif
1284 if (lbn >= 0 && lbn < NDADDR)
1285 osize = ip->i_lfs_fragsize[lbn];
1286 else
1287 osize = fs->lfs_bsize;
1288 LFS_SEGENTRY(sup, fs, oldsn, bp);
1289 #ifdef DIAGNOSTIC
1290 if (sup->su_nbytes + DINODE_SIZE * ndupino < osize) {
1291 printf("lfs_updatemeta: negative bytes "
1292 "(segment %" PRId64 " short by %d)\n",
1293 dtosn(fs, daddr),
1294 osize - sup->su_nbytes);
1295 printf("lfs_updatemeta: ino %d, lbn %" PRId64
1296 ", addr = 0x%llx\n",
1297 VTOI(sp->vp)->i_number, lbn, daddr);
1298 printf("lfs_updatemeta: ndupino=%d\n", ndupino);
1299 panic("lfs_updatemeta: negative bytes");
1300 sup->su_nbytes = osize;
1301 }
1302 #endif
1303 #ifdef DEBUG_SU_NBYTES
1304 printf("seg %d -= %ld for ino %d lbn %" PRId64
1305 " db 0x%x\n",
1306 dtosn(fs, daddr), osize,
1307 VTOI(sp->vp)->i_number, lbn, daddr);
1308 #endif
1309 sup->su_nbytes -= osize;
1310 if (!(bp->b_flags & B_GATHERED))
1311 fs->lfs_flags |= LFS_IFDIRTY;
1312 error = LFS_BWRITE_LOG(bp); /* Ifile */
1313 }
1314 /*
1315 * Now that this block has a new address, and its old
1316 * segment no longer owns it, we can forget about its
1317 * old size.
1318 */
1319 if (lbn >= 0 && lbn < NDADDR)
1320 ip->i_lfs_fragsize[lbn] = sbp->b_bcount;
1321 }
1322 }
1323
1324 /*
1325 * Start a new segment.
1326 */
1327 int
1328 lfs_initseg(struct lfs *fs)
1329 {
1330 struct segment *sp;
1331 SEGUSE *sup;
1332 SEGSUM *ssp;
1333 struct buf *bp, *sbp;
1334 int repeat;
1335
1336 sp = fs->lfs_sp;
1337
1338 repeat = 0;
1339 /* Advance to the next segment. */
1340 if (!LFS_PARTIAL_FITS(fs)) {
1341 /* lfs_avail eats the remaining space */
1342 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1343 fs->lfs_curseg);
1344 /* Wake up any cleaning procs waiting on this file system. */
1345 wakeup(&lfs_allclean_wakeup);
1346 wakeup(&fs->lfs_nextseg);
1347 lfs_newseg(fs);
1348 repeat = 1;
1349 fs->lfs_offset = fs->lfs_curseg;
1350 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1351 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1352 /*
1353 * If the segment contains a superblock, update the offset
1354 * and summary address to skip over it.
1355 */
1356 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1357 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1358 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1359 sp->seg_bytes_left -= LFS_SBPAD;
1360 }
1361 brelse(bp);
1362 /* Segment zero could also contain the labelpad */
1363 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1364 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1365 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1366 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1367 }
1368 } else {
1369 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1370 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1371 (fs->lfs_offset - fs->lfs_curseg));
1372 }
1373 fs->lfs_lastpseg = fs->lfs_offset;
1374
1375 sp->fs = fs;
1376 sp->ibp = NULL;
1377 sp->idp = NULL;
1378 sp->ninodes = 0;
1379 sp->ndupino = 0;
1380
1381 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
1382 sp->cbpp = sp->bpp;
1383 #ifdef LFS_MALLOC_SUMMARY
1384 sbp = *sp->cbpp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1385 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
1386 sp->segsum = (*sp->cbpp)->b_data;
1387 #else
1388 sbp = *sp->cbpp = getblk(VTOI(fs->lfs_ivnode)->i_devvp,
1389 fsbtodb(fs, fs->lfs_offset), NBPG, 0, 0);
1390 memset(sbp->b_data, 0x5a, NBPG);
1391 sp->segsum = (*sp->cbpp)->b_data + NBPG - fs->lfs_sumsize;
1392 #endif
1393 bzero(sp->segsum, fs->lfs_sumsize);
1394 sp->start_bpp = ++sp->cbpp;
1395 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1396
1397 /* Set point to SEGSUM, initialize it. */
1398 ssp = sp->segsum;
1399 ssp->ss_next = fs->lfs_nextseg;
1400 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1401 ssp->ss_magic = SS_MAGIC;
1402
1403 /* Set pointer to first FINFO, initialize it. */
1404 sp->fip = (struct finfo *)((caddr_t)sp->segsum + SEGSUM_SIZE(fs));
1405 sp->fip->fi_nblocks = 0;
1406 sp->start_lbp = &sp->fip->fi_blocks[0];
1407 sp->fip->fi_lastlength = 0;
1408
1409 sp->seg_bytes_left -= fs->lfs_sumsize;
1410 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1411
1412 #ifndef LFS_MALLOC_SUMMARY
1413 LFS_LOCK_BUF(sbp);
1414 brelse(sbp);
1415 #endif
1416 return (repeat);
1417 }
1418
1419 /*
1420 * Return the next segment to write.
1421 */
1422 void
1423 lfs_newseg(struct lfs *fs)
1424 {
1425 CLEANERINFO *cip;
1426 SEGUSE *sup;
1427 struct buf *bp;
1428 int curseg, isdirty, sn;
1429
1430 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1431 #ifdef DEBUG_SU_NBYTES
1432 printf("lfs_newseg: seg %d := 0 in newseg\n", /* XXXDEBUG */
1433 dtosn(fs, fs->lfs_nextseg)); /* XXXDEBUG */
1434 #endif
1435 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1436 sup->su_nbytes = 0;
1437 sup->su_nsums = 0;
1438 sup->su_ninos = 0;
1439 (void) LFS_BWRITE_LOG(bp); /* Ifile */
1440
1441 LFS_CLEANERINFO(cip, fs, bp);
1442 --cip->clean;
1443 ++cip->dirty;
1444 fs->lfs_nclean = cip->clean;
1445 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1446
1447 fs->lfs_lastseg = fs->lfs_curseg;
1448 fs->lfs_curseg = fs->lfs_nextseg;
1449 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1450 sn = (sn + 1) % fs->lfs_nseg;
1451 if (sn == curseg)
1452 panic("lfs_nextseg: no clean segments");
1453 LFS_SEGENTRY(sup, fs, sn, bp);
1454 isdirty = sup->su_flags & SEGUSE_DIRTY;
1455 brelse(bp);
1456 if (!isdirty)
1457 break;
1458 }
1459
1460 ++fs->lfs_nactive;
1461 fs->lfs_nextseg = sntod(fs, sn);
1462 if (lfs_dostats) {
1463 ++lfs_stats.segsused;
1464 }
1465 }
1466
1467 static struct buf **
1468 lookahead_pagemove(struct buf **bpp, int nblocks, size_t *size)
1469 {
1470 size_t maxsize;
1471 #ifndef LFS_NO_PAGEMOVE
1472 struct buf *bp;
1473 #endif
1474
1475 maxsize = *size;
1476 *size = 0;
1477 #ifdef LFS_NO_PAGEMOVE
1478 return bpp;
1479 #else
1480 while((bp = *bpp) != NULL && *size < maxsize && nblocks--) {
1481 if(bp->b_flags & B_CALL)
1482 return bpp;
1483 if(bp->b_bcount % NBPG)
1484 return bpp;
1485 *size += bp->b_bcount;
1486 ++bpp;
1487 }
1488 return NULL;
1489 #endif
1490 }
1491
1492 #define BQUEUES 4 /* XXX */
1493 #define BQ_EMPTY 3 /* XXX */
1494 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
1495
1496 #define BUFHASH(dvp, lbn) \
1497 (&bufhashtbl[((long)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash])
1498 extern LIST_HEAD(bufhashhdr, buf) invalhash;
1499 /*
1500 * Insq/Remq for the buffer hash lists.
1501 */
1502 #define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash)
1503 #define bremhash(bp) LIST_REMOVE(bp, b_hash)
1504
1505 static struct buf *
1506 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr, int n)
1507 {
1508 struct lfs_cluster *cl;
1509 struct buf **bpp, *bp;
1510 int s;
1511
1512 cl = (struct lfs_cluster *)malloc(sizeof(*cl), M_SEGMENT, M_WAITOK);
1513 bpp = (struct buf **)malloc(n*sizeof(*bpp), M_SEGMENT, M_WAITOK);
1514 memset(cl, 0, sizeof(*cl));
1515 cl->fs = fs;
1516 cl->bpp = bpp;
1517 cl->bufcount = 0;
1518 cl->bufsize = 0;
1519
1520 /* If this segment is being written synchronously, note that */
1521 if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
1522 cl->flags |= LFS_CL_SYNC;
1523 cl->seg = fs->lfs_sp;
1524 ++cl->seg->seg_iocount;
1525 /* printf("+ %x => %d\n", cl->seg, cl->seg->seg_iocount); */
1526 }
1527
1528 /* Get an empty buffer header, or maybe one with something on it */
1529 s = splbio();
1530 if((bp = bufqueues[BQ_EMPTY].tqh_first) != NULL) {
1531 bremfree(bp);
1532 /* clear out various other fields */
1533 bp->b_flags = B_BUSY;
1534 bp->b_dev = NODEV;
1535 bp->b_blkno = bp->b_lblkno = 0;
1536 bp->b_error = 0;
1537 bp->b_resid = 0;
1538 bp->b_bcount = 0;
1539
1540 /* nuke any credentials we were holding */
1541 /* XXXXXX */
1542
1543 bremhash(bp);
1544
1545 /* disassociate us from our vnode, if we had one... */
1546 if (bp->b_vp)
1547 brelvp(bp);
1548 }
1549 splx(s);
1550 while (!bp)
1551 bp = getnewbuf(0, 0);
1552 s = splbio();
1553 bgetvp(vp, bp);
1554 binshash(bp,&invalhash);
1555 splx(s);
1556 bp->b_bcount = 0;
1557 bp->b_blkno = bp->b_lblkno = addr;
1558
1559 bp->b_flags |= B_CALL;
1560 bp->b_iodone = lfs_cluster_callback;
1561 cl->saveaddr = bp->b_saveaddr; /* XXX is this ever used? */
1562 bp->b_saveaddr = (caddr_t)cl;
1563
1564 return bp;
1565 }
1566
1567 int
1568 lfs_writeseg(struct lfs *fs, struct segment *sp)
1569 {
1570 struct buf **bpp, *bp, *cbp, *newbp, **pmlastbpp;
1571 SEGUSE *sup;
1572 SEGSUM *ssp;
1573 dev_t i_dev;
1574 char *datap, *dp;
1575 int do_again, i, nblocks, s;
1576 size_t el_size;
1577 struct lfs_cluster *cl;
1578 int (*strategy)(void *);
1579 struct vop_strategy_args vop_strategy_a;
1580 u_short ninos;
1581 struct vnode *devvp;
1582 char *p;
1583 struct vnode *vp;
1584 struct inode *ip;
1585 size_t pmsize;
1586 int use_pagemove;
1587 daddr_t pseg_daddr;
1588 int32_t *daddrp; /* XXX ondisk32 */
1589 int changed;
1590 #if defined(DEBUG) && defined(LFS_PROPELLER)
1591 static int propeller;
1592 char propstring[4] = "-\\|/";
1593
1594 printf("%c\b",propstring[propeller++]);
1595 if (propeller == 4)
1596 propeller = 0;
1597 #endif
1598 pseg_daddr = (*(sp->bpp))->b_blkno;
1599
1600 /*
1601 * If there are no buffers other than the segment summary to write
1602 * and it is not a checkpoint, don't do anything. On a checkpoint,
1603 * even if there aren't any buffers, you need to write the superblock.
1604 */
1605 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1606 return (0);
1607
1608 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1609 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1610
1611 /* Update the segment usage information. */
1612 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1613
1614 /* Loop through all blocks, except the segment summary. */
1615 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1616 if ((*bpp)->b_vp != devvp) {
1617 sup->su_nbytes += (*bpp)->b_bcount;
1618 #ifdef DEBUG_SU_NBYTES
1619 printf("seg %d += %ld for ino %d lbn %" PRId64 " db 0x%x\n",
1620 sp->seg_number, (*bpp)->b_bcount,
1621 VTOI((*bpp)->b_vp)->i_number,
1622 (*bpp)->b_lblkno, (*bpp)->b_blkno);
1623 #endif
1624 }
1625 }
1626
1627 ssp = (SEGSUM *)sp->segsum;
1628
1629 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1630 #ifdef DEBUG_SU_NBYTES
1631 printf("seg %d += %d for %d inodes\n", /* XXXDEBUG */
1632 sp->seg_number, ssp->ss_ninos * DINODE_SIZE,
1633 ssp->ss_ninos);
1634 #endif
1635 sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
1636 /* sup->su_nbytes += fs->lfs_sumsize; */
1637 if (fs->lfs_version == 1)
1638 sup->su_olastmod = time.tv_sec;
1639 else
1640 sup->su_lastmod = time.tv_sec;
1641 sup->su_ninos += ninos;
1642 ++sup->su_nsums;
1643 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
1644 fs->lfs_ibsize));
1645 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
1646
1647 do_again = !(bp->b_flags & B_GATHERED);
1648 (void)LFS_BWRITE_LOG(bp); /* Ifile */
1649 /*
1650 * Mark blocks B_BUSY, to prevent then from being changed between
1651 * the checksum computation and the actual write.
1652 *
1653 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1654 * there are any, replace them with copies that have UNASSIGNED
1655 * instead.
1656 */
1657 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1658 ++bpp;
1659 if ((*bpp)->b_flags & B_CALL)
1660 continue;
1661 bp = *bpp;
1662 again:
1663 s = splbio();
1664 if (bp->b_flags & B_BUSY) {
1665 #ifdef DEBUG
1666 printf("lfs_writeseg: avoiding potential data summary "
1667 "corruption for ino %d, lbn %" PRId64 "\n",
1668 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
1669 #endif
1670 bp->b_flags |= B_WANTED;
1671 tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
1672 splx(s);
1673 goto again;
1674 }
1675 bp->b_flags |= B_BUSY;
1676 splx(s);
1677 /* Check and replace indirect block UNWRITTEN bogosity */
1678 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1679 VTOI(bp->b_vp)->i_ffs_blocks !=
1680 VTOI(bp->b_vp)->i_lfs_effnblks) {
1681 #ifdef DEBUG_LFS
1682 printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
1683 VTOI(bp->b_vp)->i_number,
1684 VTOI(bp->b_vp)->i_lfs_effnblks,
1685 VTOI(bp->b_vp)->i_ffs_blocks);
1686 #endif
1687 /* Make a copy we'll make changes to */
1688 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
1689 bp->b_bcount);
1690 newbp->b_blkno = bp->b_blkno;
1691 memcpy(newbp->b_data, bp->b_data,
1692 newbp->b_bcount);
1693 *bpp = newbp;
1694
1695 changed = 0;
1696 /* XXX ondisk32 */
1697 for (daddrp = (int32_t *)(newbp->b_data);
1698 daddrp < (int32_t *)(newbp->b_data +
1699 newbp->b_bcount); daddrp++) {
1700 if (*daddrp == UNWRITTEN) {
1701 ++changed;
1702 #ifdef DEBUG_LFS
1703 printf("lfs_writeseg: replacing UNWRITTEN\n");
1704 #endif
1705 *daddrp = 0;
1706 }
1707 }
1708 /*
1709 * Get rid of the old buffer. Don't mark it clean,
1710 * though, if it still has dirty data on it.
1711 */
1712 if (changed) {
1713 bp->b_flags &= ~(B_ERROR | B_GATHERED);
1714 if (bp->b_flags & B_CALL) {
1715 lfs_freebuf(bp);
1716 bp = NULL;
1717 } else {
1718 /* Still on free list, leave it there */
1719 s = splbio();
1720 bp->b_flags &= ~B_BUSY;
1721 if (bp->b_flags & B_WANTED)
1722 wakeup(bp);
1723 splx(s);
1724 /*
1725 * We have to re-decrement lfs_avail
1726 * since this block is going to come
1727 * back around to us in the next
1728 * segment.
1729 */
1730 fs->lfs_avail -= btofsb(fs, bp->b_bcount);
1731 }
1732 } else {
1733 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1734 B_GATHERED);
1735 if (bp->b_flags & B_CALL) {
1736 lfs_freebuf(bp);
1737 bp = NULL;
1738 } else {
1739 bremfree(bp);
1740 bp->b_flags |= B_DONE;
1741 s = splbio();
1742 reassignbuf(bp, bp->b_vp);
1743 splx(s);
1744 LFS_UNLOCK_BUF(bp);
1745 brelse(bp);
1746 }
1747 }
1748
1749 }
1750 }
1751 /*
1752 * Compute checksum across data and then across summary; the first
1753 * block (the summary block) is skipped. Set the create time here
1754 * so that it's guaranteed to be later than the inode mod times.
1755 *
1756 * XXX
1757 * Fix this to do it inline, instead of malloc/copy.
1758 */
1759 if (fs->lfs_version == 1)
1760 el_size = sizeof(u_long);
1761 else
1762 el_size = sizeof(u_int32_t);
1763 datap = dp = malloc(nblocks * el_size, M_SEGMENT, M_WAITOK);
1764 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1765 if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1766 if (copyin((*bpp)->b_saveaddr, dp, el_size))
1767 panic("lfs_writeseg: copyin failed [1]: "
1768 "ino %d blk %" PRId64,
1769 VTOI((*bpp)->b_vp)->i_number,
1770 (long long)(*bpp)->b_lblkno);
1771 } else
1772 memcpy(dp, (*bpp)->b_data, el_size);
1773 dp += el_size;
1774 }
1775 if (fs->lfs_version == 1)
1776 ssp->ss_ocreate = time.tv_sec;
1777 else {
1778 ssp->ss_create = time.tv_sec;
1779 ssp->ss_serial = ++fs->lfs_serial;
1780 ssp->ss_ident = fs->lfs_ident;
1781 }
1782 #ifndef LFS_MALLOC_SUMMARY
1783 /* Set the summary block busy too */
1784 (*(sp->bpp))->b_flags |= B_BUSY;
1785 #endif
1786 ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
1787 ssp->ss_sumsum =
1788 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
1789 free(datap, M_SEGMENT);
1790 datap = dp = NULL;
1791 #ifdef DIAGNOSTIC
1792 if (fs->lfs_bfree < btofsb(fs, ninos * fs->lfs_ibsize) + btofsb(fs, fs->lfs_sumsize))
1793 panic("lfs_writeseg: No diskspace for summary");
1794 #endif
1795 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
1796 btofsb(fs, fs->lfs_sumsize));
1797
1798 strategy = devvp->v_op[VOFFSET(vop_strategy)];
1799
1800 /*
1801 * When we simply write the blocks we lose a rotation for every block
1802 * written. To avoid this problem, we use pagemove to cluster
1803 * the buffers into a chunk and write the chunk. CHUNKSIZE is the
1804 * largest size I/O devices can handle.
1805 *
1806 * XXX - right now MAXPHYS is only 64k; could it be larger?
1807 */
1808
1809 #define CHUNKSIZE MAXPHYS
1810
1811 if (devvp == NULL)
1812 panic("devvp is NULL");
1813 for (bpp = sp->bpp, i = nblocks; i;) {
1814 cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
1815 cl = (struct lfs_cluster *)cbp->b_saveaddr;
1816
1817 cbp->b_dev = i_dev;
1818 cbp->b_flags |= B_ASYNC | B_BUSY;
1819 cbp->b_bcount = 0;
1820
1821 /*
1822 * Find out if we can use pagemove to build the cluster,
1823 * or if we are stuck using malloc/copy. If this is the
1824 * first cluster, set the shift flag (see below).
1825 */
1826 pmsize = CHUNKSIZE;
1827 use_pagemove = 0;
1828 if(bpp == sp->bpp) {
1829 /* Summary blocks have to get special treatment */
1830 pmlastbpp = lookahead_pagemove(bpp + 1, i - 1, &pmsize);
1831 if(pmsize >= CHUNKSIZE - fs->lfs_sumsize ||
1832 pmlastbpp == NULL) {
1833 use_pagemove = 1;
1834 cl->flags |= LFS_CL_SHIFT;
1835 } else {
1836 /*
1837 * If we're not using pagemove, we have
1838 * to copy the summary down to the bottom
1839 * end of the block.
1840 */
1841 #ifndef LFS_MALLOC_SUMMARY
1842 memcpy((*bpp)->b_data, (*bpp)->b_data +
1843 NBPG - fs->lfs_sumsize,
1844 fs->lfs_sumsize);
1845 #endif /* LFS_MALLOC_SUMMARY */
1846 }
1847 } else {
1848 pmlastbpp = lookahead_pagemove(bpp, i, &pmsize);
1849 if(pmsize >= CHUNKSIZE || pmlastbpp == NULL) {
1850 use_pagemove = 1;
1851 }
1852 }
1853 if(use_pagemove == 0) {
1854 cl->flags |= LFS_CL_MALLOC;
1855 cl->olddata = cbp->b_data;
1856 cbp->b_data = malloc(CHUNKSIZE, M_SEGMENT, M_WAITOK);
1857 }
1858 #if defined(DEBUG) && defined(DIAGNOSTIC)
1859 if(dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno + btodb((*bpp)->b_bcount - 1))) !=
1860 dtosn(fs, dbtofsb(fs, cbp->b_blkno))) {
1861 printf("block at %x (%d), cbp at %x (%d)\n",
1862 (*bpp)->b_blkno, dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno)),
1863 cbp->b_blkno, dtosn(fs, dbtofsb(fs, cbp->b_blkno)));
1864 panic("lfs_writeseg: Segment overwrite");
1865 }
1866 #endif
1867
1868 /*
1869 * Construct the cluster.
1870 */
1871 while (fs->lfs_iocount >= LFS_THROTTLE) {
1872 #ifdef DEBUG_LFS
1873 printf("[%d]", fs->lfs_iocount);
1874 #endif
1875 tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs_throttle", 0);
1876 }
1877 ++fs->lfs_iocount;
1878
1879 for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
1880 bp = *bpp;
1881
1882 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1883 break;
1884
1885 /*
1886 * Fake buffers from the cleaner are marked as B_INVAL.
1887 * We need to copy the data from user space rather than
1888 * from the buffer indicated.
1889 * XXX == what do I do on an error?
1890 */
1891 if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1892 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1893 panic("lfs_writeseg: copyin failed [2]");
1894 } else if (use_pagemove) {
1895 pagemove(bp->b_data, p, bp->b_bcount);
1896 cbp->b_bufsize += bp->b_bcount;
1897 bp->b_bufsize -= bp->b_bcount;
1898 } else {
1899 bcopy(bp->b_data, p, bp->b_bcount);
1900 /* printf("copy in %p\n", bp->b_data); */
1901 }
1902
1903 /*
1904 * XXX If we are *not* shifting, the summary
1905 * block is only fs->lfs_sumsize. Otherwise,
1906 * it is NBPG but shifted.
1907 */
1908 if(bpp == sp->bpp && !(cl->flags & LFS_CL_SHIFT)) {
1909 p += fs->lfs_sumsize;
1910 cbp->b_bcount += fs->lfs_sumsize;
1911 cl->bufsize += fs->lfs_sumsize;
1912 } else {
1913 p += bp->b_bcount;
1914 cbp->b_bcount += bp->b_bcount;
1915 cl->bufsize += bp->b_bcount;
1916 }
1917 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | B_DONE);
1918 cl->bpp[cl->bufcount++] = bp;
1919 vp = bp->b_vp;
1920 s = splbio();
1921 ++vp->v_numoutput;
1922 splx(s);
1923
1924 /*
1925 * Although it cannot be freed for reuse before the
1926 * cluster is written to disk, this buffer does not
1927 * need to be held busy. Therefore we unbusy it,
1928 * while leaving it on the locked list. It will
1929 * be freed or requeued by the callback depending
1930 * on whether it has had B_DELWRI set again in the
1931 * meantime.
1932 *
1933 * If we are using pagemove, we have to hold the block
1934 * busy to prevent its contents from changing before
1935 * it hits the disk, and invalidating the checksum.
1936 */
1937 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
1938 #ifdef LFS_MNOBUSY
1939 if (cl->flags & LFS_CL_MALLOC) {
1940 if (!(bp->b_flags & B_CALL))
1941 brelse(bp); /* Still B_LOCKED */
1942 }
1943 #endif
1944 bpp++;
1945
1946 /*
1947 * If this is the last block for this vnode, but
1948 * there are other blocks on its dirty list,
1949 * set IN_MODIFIED/IN_CLEANING depending on what
1950 * sort of block. Only do this for our mount point,
1951 * not for, e.g., inode blocks that are attached to
1952 * the devvp.
1953 * XXX KS - Shouldn't we set *both* if both types
1954 * of blocks are present (traverse the dirty list?)
1955 */
1956 s = splbio();
1957 if ((i == 1 ||
1958 (i > 1 && vp && *bpp && (*bpp)->b_vp != vp)) &&
1959 (bp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL &&
1960 vp->v_mount == fs->lfs_ivnode->v_mount)
1961 {
1962 ip = VTOI(vp);
1963 #ifdef DEBUG_LFS
1964 printf("lfs_writeseg: marking ino %d\n",
1965 ip->i_number);
1966 #endif
1967 if (bp->b_flags & B_CALL)
1968 LFS_SET_UINO(ip, IN_CLEANING);
1969 else
1970 LFS_SET_UINO(ip, IN_MODIFIED);
1971 }
1972 splx(s);
1973 wakeup(vp);
1974 }
1975 s = splbio();
1976 ++cbp->b_vp->v_numoutput;
1977 splx(s);
1978 /*
1979 * In order to include the summary in a clustered block,
1980 * it may be necessary to shift the block forward (since
1981 * summary blocks are in generay smaller than can be
1982 * addressed by pagemove(). After the write, the block
1983 * will be corrected before disassembly.
1984 */
1985 if(cl->flags & LFS_CL_SHIFT) {
1986 cbp->b_data += (NBPG - fs->lfs_sumsize);
1987 cbp->b_bcount -= (NBPG - fs->lfs_sumsize);
1988 }
1989 vop_strategy_a.a_desc = VDESC(vop_strategy);
1990 vop_strategy_a.a_bp = cbp;
1991 (strategy)(&vop_strategy_a);
1992 }
1993
1994 if (lfs_dostats) {
1995 ++lfs_stats.psegwrites;
1996 lfs_stats.blocktot += nblocks - 1;
1997 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
1998 ++lfs_stats.psyncwrites;
1999 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
2000 ++lfs_stats.pcleanwrites;
2001 lfs_stats.cleanblocks += nblocks - 1;
2002 }
2003 }
2004 return (lfs_initseg(fs) || do_again);
2005 }
2006
2007 void
2008 lfs_writesuper(struct lfs *fs, daddr_t daddr)
2009 {
2010 struct buf *bp;
2011 dev_t i_dev;
2012 int (*strategy)(void *);
2013 int s;
2014 struct vop_strategy_args vop_strategy_a;
2015
2016 /*
2017 * If we can write one superblock while another is in
2018 * progress, we risk not having a complete checkpoint if we crash.
2019 * So, block here if a superblock write is in progress.
2020 */
2021 s = splbio();
2022 while (fs->lfs_sbactive) {
2023 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
2024 }
2025 fs->lfs_sbactive = daddr;
2026 splx(s);
2027 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
2028 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
2029
2030 /* Set timestamp of this version of the superblock */
2031 if (fs->lfs_version == 1)
2032 fs->lfs_otstamp = time.tv_sec;
2033 fs->lfs_tstamp = time.tv_sec;
2034
2035 /* Checksum the superblock and copy it into a buffer. */
2036 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
2037 bp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr), LFS_SBPAD);
2038 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
2039
2040 bp->b_dev = i_dev;
2041 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
2042 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
2043 bp->b_iodone = lfs_supercallback;
2044 /* XXX KS - same nasty hack as above */
2045 bp->b_saveaddr = (caddr_t)fs;
2046
2047 vop_strategy_a.a_desc = VDESC(vop_strategy);
2048 vop_strategy_a.a_bp = bp;
2049 s = splbio();
2050 ++bp->b_vp->v_numoutput;
2051 splx(s);
2052 ++fs->lfs_iocount;
2053 (strategy)(&vop_strategy_a);
2054 }
2055
2056 /*
2057 * Logical block number match routines used when traversing the dirty block
2058 * chain.
2059 */
2060 int
2061 lfs_match_fake(struct lfs *fs, struct buf *bp)
2062 {
2063 return (bp->b_flags & B_CALL);
2064 }
2065
2066 int
2067 lfs_match_data(struct lfs *fs, struct buf *bp)
2068 {
2069 return (bp->b_lblkno >= 0);
2070 }
2071
2072 int
2073 lfs_match_indir(struct lfs *fs, struct buf *bp)
2074 {
2075 daddr_t lbn;
2076
2077 lbn = bp->b_lblkno;
2078 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
2079 }
2080
2081 int
2082 lfs_match_dindir(struct lfs *fs, struct buf *bp)
2083 {
2084 daddr_t lbn;
2085
2086 lbn = bp->b_lblkno;
2087 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
2088 }
2089
2090 int
2091 lfs_match_tindir(struct lfs *fs, struct buf *bp)
2092 {
2093 daddr_t lbn;
2094
2095 lbn = bp->b_lblkno;
2096 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
2097 }
2098
2099 /*
2100 * XXX - The only buffers that are going to hit these functions are the
2101 * segment write blocks, or the segment summaries, or the superblocks.
2102 *
2103 * All of the above are created by lfs_newbuf, and so do not need to be
2104 * released via brelse.
2105 */
2106 void
2107 lfs_callback(struct buf *bp)
2108 {
2109 /* struct lfs *fs; */
2110 /* fs = (struct lfs *)bp->b_saveaddr; */
2111 lfs_freebuf(bp);
2112 }
2113
2114 static void
2115 lfs_super_aiodone(struct buf *bp)
2116 {
2117 struct lfs *fs;
2118
2119 fs = (struct lfs *)bp->b_saveaddr;
2120 fs->lfs_sbactive = 0;
2121 wakeup(&fs->lfs_sbactive);
2122 if (--fs->lfs_iocount < LFS_THROTTLE)
2123 wakeup(&fs->lfs_iocount);
2124 lfs_freebuf(bp);
2125 }
2126
2127 static void
2128 lfs_cluster_aiodone(struct buf *bp)
2129 {
2130 struct lfs_cluster *cl;
2131 struct lfs *fs;
2132 struct buf *tbp;
2133 struct vnode *vp;
2134 int s, error=0;
2135 char *cp;
2136 extern int locked_queue_count;
2137 extern long locked_queue_bytes;
2138
2139 if(bp->b_flags & B_ERROR)
2140 error = bp->b_error;
2141
2142 cl = (struct lfs_cluster *)bp->b_saveaddr;
2143 fs = cl->fs;
2144 bp->b_saveaddr = cl->saveaddr;
2145
2146 /* If shifted, shift back now */
2147 if(cl->flags & LFS_CL_SHIFT) {
2148 bp->b_data -= (NBPG - fs->lfs_sumsize);
2149 bp->b_bcount += (NBPG - fs->lfs_sumsize);
2150 }
2151
2152 cp = (char *)bp->b_data + cl->bufsize;
2153 /* Put the pages back, and release the buffer */
2154 while(cl->bufcount--) {
2155 tbp = cl->bpp[cl->bufcount];
2156 if(!(cl->flags & LFS_CL_MALLOC)) {
2157 cp -= tbp->b_bcount;
2158 printf("pm(%p,%p,%lx)",cp,tbp->b_data,tbp->b_bcount);
2159 pagemove(cp, tbp->b_data, tbp->b_bcount);
2160 bp->b_bufsize -= tbp->b_bcount;
2161 tbp->b_bufsize += tbp->b_bcount;
2162 }
2163 if(error) {
2164 tbp->b_flags |= B_ERROR;
2165 tbp->b_error = error;
2166 }
2167
2168 /*
2169 * We're done with tbp. If it has not been re-dirtied since
2170 * the cluster was written, free it. Otherwise, keep it on
2171 * the locked list to be written again.
2172 */
2173 if ((tbp->b_flags & (B_LOCKED | B_DELWRI)) == B_LOCKED)
2174 LFS_UNLOCK_BUF(tbp);
2175 tbp->b_flags &= ~B_GATHERED;
2176
2177 LFS_BCLEAN_LOG(fs, tbp);
2178
2179 vp = tbp->b_vp;
2180 /* Segment summary for a shifted cluster */
2181 if(!cl->bufcount && (cl->flags & LFS_CL_SHIFT))
2182 tbp->b_flags |= B_INVAL;
2183 if(!(tbp->b_flags & B_CALL)) {
2184 bremfree(tbp);
2185 s = splbio();
2186 if(vp)
2187 reassignbuf(tbp, vp);
2188 splx(s);
2189 tbp->b_flags |= B_ASYNC; /* for biodone */
2190 }
2191 #ifdef DIAGNOSTIC
2192 if (tbp->b_flags & B_DONE) {
2193 printf("blk %d biodone already (flags %lx)\n",
2194 cl->bufcount, (long)tbp->b_flags);
2195 }
2196 #endif
2197 if (tbp->b_flags & (B_BUSY | B_CALL)) {
2198 biodone(tbp);
2199 }
2200 }
2201
2202 /* Fix up the cluster buffer, and release it */
2203 if(!(cl->flags & LFS_CL_MALLOC) && bp->b_bufsize) {
2204 printf("PM(%p,%p,%lx)", (char *)bp->b_data + bp->b_bcount,
2205 (char *)bp->b_data, bp->b_bufsize);
2206 pagemove((char *)bp->b_data + bp->b_bcount,
2207 (char *)bp->b_data, bp->b_bufsize);
2208 }
2209 if(cl->flags & LFS_CL_MALLOC) {
2210 free(bp->b_data, M_SEGMENT);
2211 bp->b_data = cl->olddata;
2212 }
2213 bp->b_bcount = 0;
2214 bp->b_iodone = NULL;
2215 bp->b_flags &= ~B_DELWRI;
2216 bp->b_flags |= B_DONE;
2217 s = splbio();
2218 reassignbuf(bp, bp->b_vp);
2219 splx(s);
2220 brelse(bp);
2221
2222 /* Note i/o done */
2223 if (cl->flags & LFS_CL_SYNC) {
2224 if (--cl->seg->seg_iocount == 0)
2225 wakeup(&cl->seg->seg_iocount);
2226 /* printf("- %x => %d\n", cl->seg, cl->seg->seg_iocount); */
2227 }
2228 #ifdef DIAGNOSTIC
2229 if (fs->lfs_iocount == 0)
2230 panic("lfs_cluster_aiodone: zero iocount");
2231 #endif
2232 if (--fs->lfs_iocount < LFS_THROTTLE)
2233 wakeup(&fs->lfs_iocount);
2234 #if 0
2235 if (fs->lfs_iocount == 0) {
2236 /*
2237 * Vinvalbuf can move locked buffers off the locked queue
2238 * and we have no way of knowing about this. So, after
2239 * doing a big write, we recalculate how many buffers are
2240 * really still left on the locked queue.
2241 */
2242 lfs_countlocked(&locked_queue_count, &locked_queue_bytes, "lfs_cluster_callback");
2243 wakeup(&locked_queue_count);
2244 }
2245 #endif
2246
2247 free(cl->bpp, M_SEGMENT);
2248 free(cl, M_SEGMENT);
2249 }
2250
2251 static void
2252 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
2253 {
2254 /* reset b_iodone for when this is a single-buf i/o. */
2255 bp->b_iodone = aiodone;
2256
2257 simple_lock(&uvm.aiodoned_lock); /* locks uvm.aio_done */
2258 TAILQ_INSERT_TAIL(&uvm.aio_done, bp, b_freelist);
2259 wakeup(&uvm.aiodoned);
2260 simple_unlock(&uvm.aiodoned_lock);
2261 }
2262
2263 static void
2264 lfs_cluster_callback(struct buf *bp)
2265 {
2266 lfs_generic_callback(bp, lfs_cluster_aiodone);
2267 }
2268
2269 void
2270 lfs_supercallback(struct buf *bp)
2271 {
2272 lfs_generic_callback(bp, lfs_super_aiodone);
2273 }
2274
2275 /*
2276 * Shellsort (diminishing increment sort) from Data Structures and
2277 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
2278 * see also Knuth Vol. 3, page 84. The increments are selected from
2279 * formula (8), page 95. Roughly O(N^3/2).
2280 */
2281 /*
2282 * This is our own private copy of shellsort because we want to sort
2283 * two parallel arrays (the array of buffer pointers and the array of
2284 * logical block numbers) simultaneously. Note that we cast the array
2285 * of logical block numbers to a unsigned in this routine so that the
2286 * negative block numbers (meta data blocks) sort AFTER the data blocks.
2287 */
2288
2289 void
2290 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb)
2291 {
2292 static int __rsshell_increments[] = { 4, 1, 0 };
2293 int incr, *incrp, t1, t2;
2294 struct buf *bp_temp;
2295 u_long lb_temp;
2296
2297 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
2298 for (t1 = incr; t1 < nmemb; ++t1)
2299 for (t2 = t1 - incr; t2 >= 0;)
2300 if (lb_array[t2] > lb_array[t2 + incr]) {
2301 lb_temp = lb_array[t2];
2302 lb_array[t2] = lb_array[t2 + incr];
2303 lb_array[t2 + incr] = lb_temp;
2304 bp_temp = bp_array[t2];
2305 bp_array[t2] = bp_array[t2 + incr];
2306 bp_array[t2 + incr] = bp_temp;
2307 t2 -= incr;
2308 } else
2309 break;
2310 }
2311
2312 /*
2313 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
2314 */
2315 int
2316 lfs_vref(struct vnode *vp)
2317 {
2318 /*
2319 * If we return 1 here during a flush, we risk vinvalbuf() not
2320 * being able to flush all of the pages from this vnode, which
2321 * will cause it to panic. So, return 0 if a flush is in progress.
2322 */
2323 if (vp->v_flag & VXLOCK) {
2324 if (IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2325 return 0;
2326 }
2327 return (1);
2328 }
2329 return (vget(vp, 0));
2330 }
2331
2332 /*
2333 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
2334 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
2335 */
2336 void
2337 lfs_vunref(struct vnode *vp)
2338 {
2339 /*
2340 * Analogous to lfs_vref, if the node is flushing, fake it.
2341 */
2342 if ((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2343 return;
2344 }
2345
2346 simple_lock(&vp->v_interlock);
2347 #ifdef DIAGNOSTIC
2348 if (vp->v_usecount <= 0) {
2349 printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
2350 printf("lfs_vunref: flags are 0x%lx\n", (u_long)vp->v_flag);
2351 printf("lfs_vunref: usecount = %ld\n", (long)vp->v_usecount);
2352 panic("lfs_vunref: v_usecount<0");
2353 }
2354 #endif
2355 vp->v_usecount--;
2356 if (vp->v_usecount > 0) {
2357 simple_unlock(&vp->v_interlock);
2358 return;
2359 }
2360 /*
2361 * insert at tail of LRU list
2362 */
2363 simple_lock(&vnode_free_list_slock);
2364 if (vp->v_holdcnt > 0)
2365 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2366 else
2367 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2368 simple_unlock(&vnode_free_list_slock);
2369 simple_unlock(&vp->v_interlock);
2370 }
2371
2372 /*
2373 * We use this when we have vnodes that were loaded in solely for cleaning.
2374 * There is no reason to believe that these vnodes will be referenced again
2375 * soon, since the cleaning process is unrelated to normal filesystem
2376 * activity. Putting cleaned vnodes at the tail of the list has the effect
2377 * of flushing the vnode LRU. So, put vnodes that were loaded only for
2378 * cleaning at the head of the list, instead.
2379 */
2380 void
2381 lfs_vunref_head(struct vnode *vp)
2382 {
2383 simple_lock(&vp->v_interlock);
2384 #ifdef DIAGNOSTIC
2385 if (vp->v_usecount == 0) {
2386 panic("lfs_vunref: v_usecount<0");
2387 }
2388 #endif
2389 vp->v_usecount--;
2390 if (vp->v_usecount > 0) {
2391 simple_unlock(&vp->v_interlock);
2392 return;
2393 }
2394 /*
2395 * insert at head of LRU list
2396 */
2397 simple_lock(&vnode_free_list_slock);
2398 if (vp->v_holdcnt > 0)
2399 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2400 else
2401 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2402 simple_unlock(&vnode_free_list_slock);
2403 simple_unlock(&vp->v_interlock);
2404 }
2405
2406