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