lfs_segment.c revision 1.218 1 /* $NetBSD: lfs_segment.c,v 1.218 2011/03/23 00:58:39 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: lfs_segment.c,v 1.218 2011/03/23 00:58:39 rmind Exp $");
64
65 #ifdef DEBUG
66 # define vndebug(vp, str) do { \
67 if (VTOI(vp)->i_flag & IN_CLEANING) \
68 DLOG((DLOG_WVNODE, "not writing ino %d because %s (op %d)\n", \
69 VTOI(vp)->i_number, (str), op)); \
70 } while(0)
71 #else
72 # define vndebug(vp, str)
73 #endif
74 #define ivndebug(vp, str) \
75 DLOG((DLOG_WVNODE, "ino %d: %s\n", VTOI(vp)->i_number, (str)))
76
77 #if defined(_KERNEL_OPT)
78 #include "opt_ddb.h"
79 #endif
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/namei.h>
84 #include <sys/kernel.h>
85 #include <sys/resourcevar.h>
86 #include <sys/file.h>
87 #include <sys/stat.h>
88 #include <sys/buf.h>
89 #include <sys/proc.h>
90 #include <sys/vnode.h>
91 #include <sys/mount.h>
92 #include <sys/kauth.h>
93 #include <sys/syslog.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_JUSTDEFINE(M_SEGMENT, "LFS segment", "Segment for LFS");
110
111 extern kmutex_t vnode_free_list_lock; /* XXX */
112
113 static void lfs_generic_callback(struct buf *, void (*)(struct buf *));
114 static void lfs_free_aiodone(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
119 /*
120 * Determine if it's OK to start a partial in this segment, or if we need
121 * to go on to a new segment.
122 */
123 #define LFS_PARTIAL_FITS(fs) \
124 ((fs)->lfs_fsbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
125 (fs)->lfs_frag)
126
127 /*
128 * Figure out whether we should do a checkpoint write or go ahead with
129 * an ordinary write.
130 */
131 #define LFS_SHOULD_CHECKPOINT(fs, flags) \
132 ((flags & SEGM_CLEAN) == 0 && \
133 ((fs->lfs_nactive > LFS_MAX_ACTIVE || \
134 (flags & SEGM_CKP) || \
135 fs->lfs_nclean < LFS_MAX_ACTIVE)))
136
137 int lfs_match_fake(struct lfs *, struct buf *);
138 void lfs_newseg(struct lfs *);
139 /* XXX ondisk32 */
140 void lfs_shellsort(struct buf **, int32_t *, int, int);
141 void lfs_supercallback(struct buf *);
142 void lfs_updatemeta(struct segment *);
143 void lfs_writesuper(struct lfs *, daddr_t);
144 int lfs_writevnodes(struct lfs *fs, struct mount *mp,
145 struct segment *sp, int dirops);
146
147 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
148 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */
149 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */
150 int lfs_dirvcount = 0; /* # active dirops */
151
152 /* Statistics Counters */
153 int lfs_dostats = 1;
154 struct lfs_stats lfs_stats;
155
156 /* op values to lfs_writevnodes */
157 #define VN_REG 0
158 #define VN_DIROP 1
159 #define VN_EMPTY 2
160 #define VN_CLEAN 3
161
162 /*
163 * XXX KS - Set modification time on the Ifile, so the cleaner can
164 * read the fs mod time off of it. We don't set IN_UPDATE here,
165 * since we don't really need this to be flushed to disk (and in any
166 * case that wouldn't happen to the Ifile until we checkpoint).
167 */
168 void
169 lfs_imtime(struct lfs *fs)
170 {
171 struct timespec ts;
172 struct inode *ip;
173
174 ASSERT_MAYBE_SEGLOCK(fs);
175 vfs_timestamp(&ts);
176 ip = VTOI(fs->lfs_ivnode);
177 ip->i_ffs1_mtime = ts.tv_sec;
178 ip->i_ffs1_mtimensec = ts.tv_nsec;
179 }
180
181 /*
182 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
183 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
184 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
185 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
186 */
187
188 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp))
189
190 int
191 lfs_vflush(struct vnode *vp)
192 {
193 struct inode *ip;
194 struct lfs *fs;
195 struct segment *sp;
196 struct buf *bp, *nbp, *tbp, *tnbp;
197 int error;
198 int flushed;
199 int relock;
200 int loopcount;
201
202 ip = VTOI(vp);
203 fs = VFSTOUFS(vp->v_mount)->um_lfs;
204 relock = 0;
205
206 top:
207 ASSERT_NO_SEGLOCK(fs);
208 if (ip->i_flag & IN_CLEANING) {
209 ivndebug(vp,"vflush/in_cleaning");
210 mutex_enter(&lfs_lock);
211 LFS_CLR_UINO(ip, IN_CLEANING);
212 LFS_SET_UINO(ip, IN_MODIFIED);
213 mutex_exit(&lfs_lock);
214
215 /*
216 * Toss any cleaning buffers that have real counterparts
217 * to avoid losing new data.
218 */
219 mutex_enter(&vp->v_interlock);
220 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
221 nbp = LIST_NEXT(bp, b_vnbufs);
222 if (!LFS_IS_MALLOC_BUF(bp))
223 continue;
224 /*
225 * Look for pages matching the range covered
226 * by cleaning blocks. It's okay if more dirty
227 * pages appear, so long as none disappear out
228 * from under us.
229 */
230 if (bp->b_lblkno > 0 && vp->v_type == VREG &&
231 vp != fs->lfs_ivnode) {
232 struct vm_page *pg;
233 voff_t off;
234
235 for (off = lblktosize(fs, bp->b_lblkno);
236 off < lblktosize(fs, bp->b_lblkno + 1);
237 off += PAGE_SIZE) {
238 pg = uvm_pagelookup(&vp->v_uobj, off);
239 if (pg == NULL)
240 continue;
241 if ((pg->flags & PG_CLEAN) == 0 ||
242 pmap_is_modified(pg)) {
243 fs->lfs_avail += btofsb(fs,
244 bp->b_bcount);
245 wakeup(&fs->lfs_avail);
246 mutex_exit(&vp->v_interlock);
247 lfs_freebuf(fs, bp);
248 mutex_enter(&vp->v_interlock);
249 bp = NULL;
250 break;
251 }
252 }
253 }
254 for (tbp = LIST_FIRST(&vp->v_dirtyblkhd); tbp;
255 tbp = tnbp)
256 {
257 tnbp = LIST_NEXT(tbp, b_vnbufs);
258 if (tbp->b_vp == bp->b_vp
259 && tbp->b_lblkno == bp->b_lblkno
260 && tbp != bp)
261 {
262 fs->lfs_avail += btofsb(fs,
263 bp->b_bcount);
264 wakeup(&fs->lfs_avail);
265 mutex_exit(&vp->v_interlock);
266 lfs_freebuf(fs, bp);
267 mutex_enter(&vp->v_interlock);
268 bp = NULL;
269 break;
270 }
271 }
272 }
273 } else {
274 mutex_enter(&vp->v_interlock);
275 }
276
277 /* If the node is being written, wait until that is done */
278 while (WRITEINPROG(vp)) {
279 ivndebug(vp,"vflush/writeinprog");
280 cv_wait(&vp->v_cv, &vp->v_interlock);
281 }
282 mutex_exit(&vp->v_interlock);
283
284 /* Protect against VI_XLOCK deadlock in vinvalbuf() */
285 lfs_seglock(fs, SEGM_SYNC);
286
287 /* If we're supposed to flush a freed inode, just toss it */
288 if (ip->i_lfs_iflags & LFSI_DELETED) {
289 DLOG((DLOG_VNODE, "lfs_vflush: ino %d freed, not flushing\n",
290 ip->i_number));
291 /* Drain v_numoutput */
292 mutex_enter(&vp->v_interlock);
293 while (vp->v_numoutput > 0) {
294 cv_wait(&vp->v_cv, &vp->v_interlock);
295 }
296 KASSERT(vp->v_numoutput == 0);
297 mutex_exit(&vp->v_interlock);
298
299 mutex_enter(&bufcache_lock);
300 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
301 nbp = LIST_NEXT(bp, b_vnbufs);
302
303 KASSERT((bp->b_flags & B_GATHERED) == 0);
304 if (bp->b_oflags & BO_DELWRI) { /* XXX always true? */
305 fs->lfs_avail += btofsb(fs, bp->b_bcount);
306 wakeup(&fs->lfs_avail);
307 }
308 /* Copied from lfs_writeseg */
309 if (bp->b_iodone != NULL) {
310 mutex_exit(&bufcache_lock);
311 biodone(bp);
312 mutex_enter(&bufcache_lock);
313 } else {
314 bremfree(bp);
315 LFS_UNLOCK_BUF(bp);
316 mutex_enter(&vp->v_interlock);
317 bp->b_flags &= ~(B_READ | B_GATHERED);
318 bp->b_oflags = (bp->b_oflags & ~BO_DELWRI) | BO_DONE;
319 bp->b_error = 0;
320 reassignbuf(bp, vp);
321 mutex_exit(&vp->v_interlock);
322 brelse(bp, 0);
323 }
324 }
325 mutex_exit(&bufcache_lock);
326 LFS_CLR_UINO(ip, IN_CLEANING);
327 LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
328 ip->i_flag &= ~IN_ALLMOD;
329 DLOG((DLOG_VNODE, "lfs_vflush: done not flushing ino %d\n",
330 ip->i_number));
331 lfs_segunlock(fs);
332
333 KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL);
334
335 return 0;
336 }
337
338 fs->lfs_flushvp = vp;
339 if (LFS_SHOULD_CHECKPOINT(fs, fs->lfs_sp->seg_flags)) {
340 error = lfs_segwrite(vp->v_mount, SEGM_CKP | SEGM_SYNC);
341 fs->lfs_flushvp = NULL;
342 KASSERT(fs->lfs_flushvp_fakevref == 0);
343 lfs_segunlock(fs);
344
345 /* Make sure that any pending buffers get written */
346 mutex_enter(&vp->v_interlock);
347 while (vp->v_numoutput > 0) {
348 cv_wait(&vp->v_cv, &vp->v_interlock);
349 }
350 KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL);
351 KASSERT(vp->v_numoutput == 0);
352 mutex_exit(&vp->v_interlock);
353
354 return error;
355 }
356 sp = fs->lfs_sp;
357
358 flushed = 0;
359 if (VPISEMPTY(vp)) {
360 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
361 ++flushed;
362 } else if ((ip->i_flag & IN_CLEANING) &&
363 (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
364 ivndebug(vp,"vflush/clean");
365 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
366 ++flushed;
367 } else if (lfs_dostats) {
368 if (!VPISEMPTY(vp) || (VTOI(vp)->i_flag & IN_ALLMOD))
369 ++lfs_stats.vflush_invoked;
370 ivndebug(vp,"vflush");
371 }
372
373 #ifdef DIAGNOSTIC
374 if (vp->v_uflag & VU_DIROP) {
375 DLOG((DLOG_VNODE, "lfs_vflush: flushing VU_DIROP\n"));
376 /* panic("lfs_vflush: VU_DIROP being flushed...this can\'t happen"); */
377 }
378 #endif
379
380 do {
381 loopcount = 0;
382 do {
383 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) {
384 relock = lfs_writefile(fs, sp, vp);
385 if (relock) {
386 /*
387 * Might have to wait for the
388 * cleaner to run; but we're
389 * still not done with this vnode.
390 */
391 KDASSERT(ip->i_number != LFS_IFILE_INUM);
392 lfs_writeinode(fs, sp, ip);
393 mutex_enter(&lfs_lock);
394 LFS_SET_UINO(ip, IN_MODIFIED);
395 mutex_exit(&lfs_lock);
396 lfs_writeseg(fs, sp);
397 lfs_segunlock(fs);
398 lfs_segunlock_relock(fs);
399 goto top;
400 }
401 }
402 /*
403 * If we begin a new segment in the middle of writing
404 * the Ifile, it creates an inconsistent checkpoint,
405 * since the Ifile information for the new segment
406 * is not up-to-date. Take care of this here by
407 * sending the Ifile through again in case there
408 * are newly dirtied blocks. But wait, there's more!
409 * This second Ifile write could *also* cross a segment
410 * boundary, if the first one was large. The second
411 * one is guaranteed to be no more than 8 blocks,
412 * though (two segment blocks and supporting indirects)
413 * so the third write *will not* cross the boundary.
414 */
415 if (vp == fs->lfs_ivnode) {
416 lfs_writefile(fs, sp, vp);
417 lfs_writefile(fs, sp, vp);
418 }
419 #ifdef DEBUG
420 if (++loopcount > 2)
421 log(LOG_NOTICE, "lfs_vflush: looping count=%d\n", loopcount);
422 #endif
423 } while (lfs_writeinode(fs, sp, ip));
424 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
425
426 if (lfs_dostats) {
427 ++lfs_stats.nwrites;
428 if (sp->seg_flags & SEGM_SYNC)
429 ++lfs_stats.nsync_writes;
430 if (sp->seg_flags & SEGM_CKP)
431 ++lfs_stats.ncheckpoints;
432 }
433 /*
434 * If we were called from somewhere that has already held the seglock
435 * (e.g., lfs_markv()), the lfs_segunlock will not wait for
436 * the write to complete because we are still locked.
437 * Since lfs_vflush() must return the vnode with no dirty buffers,
438 * we must explicitly wait, if that is the case.
439 *
440 * We compare the iocount against 1, not 0, because it is
441 * artificially incremented by lfs_seglock().
442 */
443 mutex_enter(&lfs_lock);
444 if (fs->lfs_seglock > 1) {
445 while (fs->lfs_iocount > 1)
446 (void)mtsleep(&fs->lfs_iocount, PRIBIO + 1,
447 "lfs_vflush", 0, &lfs_lock);
448 }
449 mutex_exit(&lfs_lock);
450
451 lfs_segunlock(fs);
452
453 /* Wait for these buffers to be recovered by aiodoned */
454 mutex_enter(&vp->v_interlock);
455 while (vp->v_numoutput > 0) {
456 cv_wait(&vp->v_cv, &vp->v_interlock);
457 }
458 KASSERT(LIST_FIRST(&vp->v_dirtyblkhd) == NULL);
459 KASSERT(vp->v_numoutput == 0);
460 mutex_exit(&vp->v_interlock);
461
462 fs->lfs_flushvp = NULL;
463 KASSERT(fs->lfs_flushvp_fakevref == 0);
464
465 return (0);
466 }
467
468 int
469 lfs_writevnodes(struct lfs *fs, struct mount *mp, struct segment *sp, int op)
470 {
471 struct inode *ip;
472 struct vnode *vp;
473 int inodes_written = 0, only_cleaning;
474 int error = 0;
475
476 ASSERT_SEGLOCK(fs);
477 loop:
478 /* start at last (newest) vnode. */
479 mutex_enter(&mntvnode_lock);
480 TAILQ_FOREACH_REVERSE(vp, &mp->mnt_vnodelist, vnodelst, v_mntvnodes) {
481 /*
482 * If the vnode that we are about to sync is no longer
483 * associated with this mount point, start over.
484 */
485 if (vp->v_mount != mp) {
486 DLOG((DLOG_VNODE, "lfs_writevnodes: starting over\n"));
487 /*
488 * After this, pages might be busy
489 * due to our own previous putpages.
490 * Start actual segment write here to avoid deadlock.
491 */
492 mutex_exit(&mntvnode_lock);
493 (void)lfs_writeseg(fs, sp);
494 goto loop;
495 }
496
497 mutex_enter(&vp->v_interlock);
498 if (vp->v_type == VNON || vismarker(vp) ||
499 (vp->v_iflag & VI_CLEAN) != 0) {
500 mutex_exit(&vp->v_interlock);
501 continue;
502 }
503
504 ip = VTOI(vp);
505 if ((op == VN_DIROP && !(vp->v_uflag & VU_DIROP)) ||
506 (op != VN_DIROP && op != VN_CLEAN &&
507 (vp->v_uflag & VU_DIROP))) {
508 mutex_exit(&vp->v_interlock);
509 vndebug(vp,"dirop");
510 continue;
511 }
512
513 if (op == VN_EMPTY && !VPISEMPTY(vp)) {
514 mutex_exit(&vp->v_interlock);
515 vndebug(vp,"empty");
516 continue;
517 }
518
519 if (op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
520 && vp != fs->lfs_flushvp
521 && !(ip->i_flag & IN_CLEANING)) {
522 mutex_exit(&vp->v_interlock);
523 vndebug(vp,"cleaning");
524 continue;
525 }
526
527 mutex_exit(&mntvnode_lock);
528 if (lfs_vref(vp)) {
529 vndebug(vp,"vref");
530 mutex_enter(&mntvnode_lock);
531 continue;
532 }
533
534 only_cleaning = 0;
535 /*
536 * Write the inode/file if dirty and it's not the IFILE.
537 */
538 if ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp)) {
539 only_cleaning =
540 ((ip->i_flag & IN_ALLMOD) == IN_CLEANING);
541
542 if (ip->i_number != LFS_IFILE_INUM) {
543 error = lfs_writefile(fs, sp, vp);
544 if (error) {
545 lfs_vunref(vp);
546 if (error == EAGAIN) {
547 /*
548 * This error from lfs_putpages
549 * indicates we need to drop
550 * the segment lock and start
551 * over after the cleaner has
552 * had a chance to run.
553 */
554 lfs_writeinode(fs, sp, ip);
555 lfs_writeseg(fs, sp);
556 if (!VPISEMPTY(vp) &&
557 !WRITEINPROG(vp) &&
558 !(ip->i_flag & IN_ALLMOD)) {
559 mutex_enter(&lfs_lock);
560 LFS_SET_UINO(ip, IN_MODIFIED);
561 mutex_exit(&lfs_lock);
562 }
563 mutex_enter(&mntvnode_lock);
564 break;
565 }
566 error = 0; /* XXX not quite right */
567 mutex_enter(&mntvnode_lock);
568 continue;
569 }
570
571 if (!VPISEMPTY(vp)) {
572 if (WRITEINPROG(vp)) {
573 ivndebug(vp,"writevnodes/write2");
574 } else if (!(ip->i_flag & IN_ALLMOD)) {
575 mutex_enter(&lfs_lock);
576 LFS_SET_UINO(ip, IN_MODIFIED);
577 mutex_exit(&lfs_lock);
578 }
579 }
580 (void) lfs_writeinode(fs, sp, ip);
581 inodes_written++;
582 }
583 }
584
585 if (lfs_clean_vnhead && only_cleaning)
586 lfs_vunref_head(vp);
587 else
588 lfs_vunref(vp);
589
590 mutex_enter(&mntvnode_lock);
591 }
592 mutex_exit(&mntvnode_lock);
593 return error;
594 }
595
596 /*
597 * Do a checkpoint.
598 */
599 int
600 lfs_segwrite(struct mount *mp, int flags)
601 {
602 struct buf *bp;
603 struct inode *ip;
604 struct lfs *fs;
605 struct segment *sp;
606 struct vnode *vp;
607 SEGUSE *segusep;
608 int do_ckp, did_ckp, error;
609 unsigned n, segleft, maxseg, sn, i, curseg;
610 int writer_set = 0;
611 int dirty;
612 int redo;
613 int um_error;
614 int loopcount;
615
616 fs = VFSTOUFS(mp)->um_lfs;
617 ASSERT_MAYBE_SEGLOCK(fs);
618
619 if (fs->lfs_ronly)
620 return EROFS;
621
622 lfs_imtime(fs);
623
624 /*
625 * Allocate a segment structure and enough space to hold pointers to
626 * the maximum possible number of buffers which can be described in a
627 * single summary block.
628 */
629 do_ckp = LFS_SHOULD_CHECKPOINT(fs, flags);
630
631 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
632 sp = fs->lfs_sp;
633 if (sp->seg_flags & (SEGM_CLEAN | SEGM_CKP))
634 do_ckp = 1;
635
636 /*
637 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
638 * in which case we have to flush *all* buffers off of this vnode.
639 * We don't care about other nodes, but write any non-dirop nodes
640 * anyway in anticipation of another getnewvnode().
641 *
642 * If we're cleaning we only write cleaning and ifile blocks, and
643 * no dirops, since otherwise we'd risk corruption in a crash.
644 */
645 if (sp->seg_flags & SEGM_CLEAN)
646 lfs_writevnodes(fs, mp, sp, VN_CLEAN);
647 else if (!(sp->seg_flags & SEGM_FORCE_CKP)) {
648 do {
649 um_error = lfs_writevnodes(fs, mp, sp, VN_REG);
650
651 if (do_ckp || fs->lfs_dirops == 0) {
652 if (!writer_set) {
653 lfs_writer_enter(fs, "lfs writer");
654 writer_set = 1;
655 }
656 error = lfs_writevnodes(fs, mp, sp, VN_DIROP);
657 if (um_error == 0)
658 um_error = error;
659 /* In case writevnodes errored out */
660 lfs_flush_dirops(fs);
661 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
662 lfs_finalize_fs_seguse(fs);
663 }
664 if (do_ckp && um_error) {
665 lfs_segunlock_relock(fs);
666 sp = fs->lfs_sp;
667 }
668 } while (do_ckp && um_error != 0);
669 }
670
671 /*
672 * If we are doing a checkpoint, mark everything since the
673 * last checkpoint as no longer ACTIVE.
674 */
675 if (do_ckp || fs->lfs_doifile) {
676 segleft = fs->lfs_nseg;
677 curseg = 0;
678 for (n = 0; n < fs->lfs_segtabsz; n++) {
679 dirty = 0;
680 if (bread(fs->lfs_ivnode, fs->lfs_cleansz + n,
681 fs->lfs_bsize, NOCRED, B_MODIFY, &bp))
682 panic("lfs_segwrite: ifile read");
683 segusep = (SEGUSE *)bp->b_data;
684 maxseg = min(segleft, fs->lfs_sepb);
685 for (i = 0; i < maxseg; i++) {
686 sn = curseg + i;
687 if (sn != dtosn(fs, fs->lfs_curseg) &&
688 segusep->su_flags & SEGUSE_ACTIVE) {
689 segusep->su_flags &= ~SEGUSE_ACTIVE;
690 --fs->lfs_nactive;
691 ++dirty;
692 }
693 fs->lfs_suflags[fs->lfs_activesb][sn] =
694 segusep->su_flags;
695 if (fs->lfs_version > 1)
696 ++segusep;
697 else
698 segusep = (SEGUSE *)
699 ((SEGUSE_V1 *)segusep + 1);
700 }
701
702 if (dirty)
703 error = LFS_BWRITE_LOG(bp); /* Ifile */
704 else
705 brelse(bp, 0);
706 segleft -= fs->lfs_sepb;
707 curseg += fs->lfs_sepb;
708 }
709 }
710
711 KASSERT(LFS_SEGLOCK_HELD(fs));
712
713 did_ckp = 0;
714 if (do_ckp || fs->lfs_doifile) {
715 vp = fs->lfs_ivnode;
716 vn_lock(vp, LK_EXCLUSIVE);
717 loopcount = 0;
718 do {
719 #ifdef DEBUG
720 LFS_ENTER_LOG("pretend", __FILE__, __LINE__, 0, 0, curproc->p_pid);
721 #endif
722 mutex_enter(&lfs_lock);
723 fs->lfs_flags &= ~LFS_IFDIRTY;
724 mutex_exit(&lfs_lock);
725
726 ip = VTOI(vp);
727
728 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL) {
729 /*
730 * Ifile has no pages, so we don't need
731 * to check error return here.
732 */
733 lfs_writefile(fs, sp, vp);
734 /*
735 * Ensure the Ifile takes the current segment
736 * into account. See comment in lfs_vflush.
737 */
738 lfs_writefile(fs, sp, vp);
739 lfs_writefile(fs, sp, vp);
740 }
741
742 if (ip->i_flag & IN_ALLMOD)
743 ++did_ckp;
744 #if 0
745 redo = (do_ckp ? lfs_writeinode(fs, sp, ip) : 0);
746 #else
747 redo = lfs_writeinode(fs, sp, ip);
748 #endif
749 redo += lfs_writeseg(fs, sp);
750 mutex_enter(&lfs_lock);
751 redo += (fs->lfs_flags & LFS_IFDIRTY);
752 mutex_exit(&lfs_lock);
753 #ifdef DEBUG
754 if (++loopcount > 2)
755 log(LOG_NOTICE, "lfs_segwrite: looping count=%d\n",
756 loopcount);
757 #endif
758 } while (redo && do_ckp);
759
760 /*
761 * Unless we are unmounting, the Ifile may continue to have
762 * dirty blocks even after a checkpoint, due to changes to
763 * inodes' atime. If we're checkpointing, it's "impossible"
764 * for other parts of the Ifile to be dirty after the loop
765 * above, since we hold the segment lock.
766 */
767 mutex_enter(&vp->v_interlock);
768 if (LIST_EMPTY(&vp->v_dirtyblkhd)) {
769 LFS_CLR_UINO(ip, IN_ALLMOD);
770 }
771 #ifdef DIAGNOSTIC
772 else if (do_ckp) {
773 int do_panic = 0;
774 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
775 if (bp->b_lblkno < fs->lfs_cleansz +
776 fs->lfs_segtabsz &&
777 !(bp->b_flags & B_GATHERED)) {
778 printf("ifile lbn %ld still dirty (flags %lx)\n",
779 (long)bp->b_lblkno,
780 (long)bp->b_flags);
781 ++do_panic;
782 }
783 }
784 if (do_panic)
785 panic("dirty blocks");
786 }
787 #endif
788 mutex_exit(&vp->v_interlock);
789 VOP_UNLOCK(vp);
790 } else {
791 (void) lfs_writeseg(fs, sp);
792 }
793
794 /* Note Ifile no longer needs to be written */
795 fs->lfs_doifile = 0;
796 if (writer_set)
797 lfs_writer_leave(fs);
798
799 /*
800 * If we didn't write the Ifile, we didn't really do anything.
801 * That means that (1) there is a checkpoint on disk and (2)
802 * nothing has changed since it was written.
803 *
804 * Take the flags off of the segment so that lfs_segunlock
805 * doesn't have to write the superblock either.
806 */
807 if (do_ckp && !did_ckp) {
808 sp->seg_flags &= ~SEGM_CKP;
809 }
810
811 if (lfs_dostats) {
812 ++lfs_stats.nwrites;
813 if (sp->seg_flags & SEGM_SYNC)
814 ++lfs_stats.nsync_writes;
815 if (sp->seg_flags & SEGM_CKP)
816 ++lfs_stats.ncheckpoints;
817 }
818 lfs_segunlock(fs);
819 return (0);
820 }
821
822 /*
823 * Write the dirty blocks associated with a vnode.
824 */
825 int
826 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp)
827 {
828 struct finfo *fip;
829 struct inode *ip;
830 int i, frag;
831 int error;
832
833 ASSERT_SEGLOCK(fs);
834 error = 0;
835 ip = VTOI(vp);
836
837 fip = sp->fip;
838 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
839
840 if (vp->v_uflag & VU_DIROP)
841 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
842
843 if (sp->seg_flags & SEGM_CLEAN) {
844 lfs_gather(fs, sp, vp, lfs_match_fake);
845 /*
846 * For a file being flushed, we need to write *all* blocks.
847 * This means writing the cleaning blocks first, and then
848 * immediately following with any non-cleaning blocks.
849 * The same is true of the Ifile since checkpoints assume
850 * that all valid Ifile blocks are written.
851 */
852 if (IS_FLUSHING(fs, vp) || vp == fs->lfs_ivnode) {
853 lfs_gather(fs, sp, vp, lfs_match_data);
854 /*
855 * Don't call VOP_PUTPAGES: if we're flushing,
856 * we've already done it, and the Ifile doesn't
857 * use the page cache.
858 */
859 }
860 } else {
861 lfs_gather(fs, sp, vp, lfs_match_data);
862 /*
863 * If we're flushing, we've already called VOP_PUTPAGES
864 * so don't do it again. Otherwise, we want to write
865 * everything we've got.
866 */
867 if (!IS_FLUSHING(fs, vp)) {
868 mutex_enter(&vp->v_interlock);
869 error = VOP_PUTPAGES(vp, 0, 0,
870 PGO_CLEANIT | PGO_ALLPAGES | PGO_LOCKED);
871 }
872 }
873
874 /*
875 * It may not be necessary to write the meta-data blocks at this point,
876 * as the roll-forward recovery code should be able to reconstruct the
877 * list.
878 *
879 * We have to write them anyway, though, under two conditions: (1) the
880 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
881 * checkpointing.
882 *
883 * BUT if we are cleaning, we might have indirect blocks that refer to
884 * new blocks not being written yet, in addition to fragments being
885 * moved out of a cleaned segment. If that is the case, don't
886 * write the indirect blocks, or the finfo will have a small block
887 * in the middle of it!
888 * XXX in this case isn't the inode size wrong too?
889 */
890 frag = 0;
891 if (sp->seg_flags & SEGM_CLEAN) {
892 for (i = 0; i < NDADDR; i++)
893 if (ip->i_lfs_fragsize[i] > 0 &&
894 ip->i_lfs_fragsize[i] < fs->lfs_bsize)
895 ++frag;
896 }
897 #ifdef DIAGNOSTIC
898 if (frag > 1)
899 panic("lfs_writefile: more than one fragment!");
900 #endif
901 if (IS_FLUSHING(fs, vp) ||
902 (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) {
903 lfs_gather(fs, sp, vp, lfs_match_indir);
904 lfs_gather(fs, sp, vp, lfs_match_dindir);
905 lfs_gather(fs, sp, vp, lfs_match_tindir);
906 }
907 fip = sp->fip;
908 lfs_release_finfo(fs);
909
910 return error;
911 }
912
913 /*
914 * Update segment accounting to reflect this inode's change of address.
915 */
916 static int
917 lfs_update_iaddr(struct lfs *fs, struct segment *sp, struct inode *ip, daddr_t ndaddr)
918 {
919 struct buf *bp;
920 daddr_t daddr;
921 IFILE *ifp;
922 SEGUSE *sup;
923 ino_t ino;
924 int redo_ifile, error;
925 u_int32_t sn;
926
927 redo_ifile = 0;
928
929 /*
930 * If updating the ifile, update the super-block. Update the disk
931 * address and access times for this inode in the ifile.
932 */
933 ino = ip->i_number;
934 if (ino == LFS_IFILE_INUM) {
935 daddr = fs->lfs_idaddr;
936 fs->lfs_idaddr = dbtofsb(fs, ndaddr);
937 } else {
938 LFS_IENTRY(ifp, fs, ino, bp);
939 daddr = ifp->if_daddr;
940 ifp->if_daddr = dbtofsb(fs, ndaddr);
941 error = LFS_BWRITE_LOG(bp); /* Ifile */
942 }
943
944 /*
945 * If this is the Ifile and lfs_offset is set to the first block
946 * in the segment, dirty the new segment's accounting block
947 * (XXX should already be dirty?) and tell the caller to do it again.
948 */
949 if (ip->i_number == LFS_IFILE_INUM) {
950 sn = dtosn(fs, fs->lfs_offset);
951 if (sntod(fs, sn) + btofsb(fs, fs->lfs_sumsize) ==
952 fs->lfs_offset) {
953 LFS_SEGENTRY(sup, fs, sn, bp);
954 KASSERT(bp->b_oflags & BO_DELWRI);
955 LFS_WRITESEGENTRY(sup, fs, sn, bp);
956 /* fs->lfs_flags |= LFS_IFDIRTY; */
957 redo_ifile |= 1;
958 }
959 }
960
961 /*
962 * The inode's last address should not be in the current partial
963 * segment, except under exceptional circumstances (lfs_writevnodes
964 * had to start over, and in the meantime more blocks were written
965 * to a vnode). Both inodes will be accounted to this segment
966 * in lfs_writeseg so we need to subtract the earlier version
967 * here anyway. The segment count can temporarily dip below
968 * zero here; keep track of how many duplicates we have in
969 * "dupino" so we don't panic below.
970 */
971 if (daddr >= fs->lfs_lastpseg && daddr <= fs->lfs_offset) {
972 ++sp->ndupino;
973 DLOG((DLOG_SEG, "lfs_writeinode: last inode addr in current pseg "
974 "(ino %d daddr 0x%llx) ndupino=%d\n", ino,
975 (long long)daddr, sp->ndupino));
976 }
977 /*
978 * Account the inode: it no longer belongs to its former segment,
979 * though it will not belong to the new segment until that segment
980 * is actually written.
981 */
982 if (daddr != LFS_UNUSED_DADDR) {
983 u_int32_t oldsn = dtosn(fs, daddr);
984 #ifdef DIAGNOSTIC
985 int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0;
986 #endif
987 LFS_SEGENTRY(sup, fs, oldsn, bp);
988 #ifdef DIAGNOSTIC
989 if (sup->su_nbytes +
990 sizeof (struct ufs1_dinode) * ndupino
991 < sizeof (struct ufs1_dinode)) {
992 printf("lfs_writeinode: negative bytes "
993 "(segment %" PRIu32 " short by %d, "
994 "oldsn=%" PRIu32 ", cursn=%" PRIu32
995 ", daddr=%" PRId64 ", su_nbytes=%u, "
996 "ndupino=%d)\n",
997 dtosn(fs, daddr),
998 (int)sizeof (struct ufs1_dinode) *
999 (1 - sp->ndupino) - sup->su_nbytes,
1000 oldsn, sp->seg_number, daddr,
1001 (unsigned int)sup->su_nbytes,
1002 sp->ndupino);
1003 panic("lfs_writeinode: negative bytes");
1004 sup->su_nbytes = sizeof (struct ufs1_dinode);
1005 }
1006 #endif
1007 DLOG((DLOG_SU, "seg %d -= %d for ino %d inode\n",
1008 dtosn(fs, daddr), sizeof (struct ufs1_dinode), ino));
1009 sup->su_nbytes -= sizeof (struct ufs1_dinode);
1010 redo_ifile |=
1011 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
1012 if (redo_ifile) {
1013 mutex_enter(&lfs_lock);
1014 fs->lfs_flags |= LFS_IFDIRTY;
1015 mutex_exit(&lfs_lock);
1016 /* Don't double-account */
1017 fs->lfs_idaddr = 0x0;
1018 }
1019 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
1020 }
1021
1022 return redo_ifile;
1023 }
1024
1025 int
1026 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
1027 {
1028 struct buf *bp;
1029 struct ufs1_dinode *cdp;
1030 daddr_t daddr;
1031 int32_t *daddrp; /* XXX ondisk32 */
1032 int i, ndx;
1033 int redo_ifile = 0;
1034 int gotblk = 0;
1035 int count;
1036
1037 ASSERT_SEGLOCK(fs);
1038 if (!(ip->i_flag & IN_ALLMOD))
1039 return (0);
1040
1041 /* Can't write ifile when writer is not set */
1042 KASSERT(ip->i_number != LFS_IFILE_INUM || fs->lfs_writer > 0 ||
1043 (sp->seg_flags & SEGM_CLEAN));
1044
1045 /*
1046 * If this is the Ifile, see if writing it here will generate a
1047 * temporary misaccounting. If it will, do the accounting and write
1048 * the blocks, postponing the inode write until the accounting is
1049 * solid.
1050 */
1051 count = 0;
1052 while (ip->i_number == LFS_IFILE_INUM) {
1053 int redo = 0;
1054
1055 if (sp->idp == NULL && sp->ibp == NULL &&
1056 (sp->seg_bytes_left < fs->lfs_ibsize ||
1057 sp->sum_bytes_left < sizeof(int32_t))) {
1058 (void) lfs_writeseg(fs, sp);
1059 continue;
1060 }
1061
1062 /* Look for dirty Ifile blocks */
1063 LIST_FOREACH(bp, &fs->lfs_ivnode->v_dirtyblkhd, b_vnbufs) {
1064 if (!(bp->b_flags & B_GATHERED)) {
1065 redo = 1;
1066 break;
1067 }
1068 }
1069
1070 if (redo == 0)
1071 redo = lfs_update_iaddr(fs, sp, ip, 0x0);
1072 if (redo == 0)
1073 break;
1074
1075 if (sp->idp) {
1076 sp->idp->di_inumber = 0;
1077 sp->idp = NULL;
1078 }
1079 ++count;
1080 if (count > 2)
1081 log(LOG_NOTICE, "lfs_writeinode: looping count=%d\n", count);
1082 lfs_writefile(fs, sp, fs->lfs_ivnode);
1083 }
1084
1085 /* Allocate a new inode block if necessary. */
1086 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
1087 sp->ibp == NULL) {
1088 /* Allocate a new segment if necessary. */
1089 if (sp->seg_bytes_left < fs->lfs_ibsize ||
1090 sp->sum_bytes_left < sizeof(int32_t))
1091 (void) lfs_writeseg(fs, sp);
1092
1093 /* Get next inode block. */
1094 daddr = fs->lfs_offset;
1095 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
1096 sp->ibp = *sp->cbpp++ =
1097 getblk(VTOI(fs->lfs_ivnode)->i_devvp,
1098 fsbtodb(fs, daddr), fs->lfs_ibsize, 0, 0);
1099 gotblk++;
1100
1101 /* Zero out inode numbers */
1102 for (i = 0; i < INOPB(fs); ++i)
1103 ((struct ufs1_dinode *)sp->ibp->b_data)[i].di_inumber =
1104 0;
1105
1106 ++sp->start_bpp;
1107 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
1108 /* Set remaining space counters. */
1109 sp->seg_bytes_left -= fs->lfs_ibsize;
1110 sp->sum_bytes_left -= sizeof(int32_t);
1111 ndx = fs->lfs_sumsize / sizeof(int32_t) -
1112 sp->ninodes / INOPB(fs) - 1;
1113 ((int32_t *)(sp->segsum))[ndx] = daddr;
1114 }
1115
1116 /* Check VU_DIROP in case there is a new file with no data blocks */
1117 if (ITOV(ip)->v_uflag & VU_DIROP)
1118 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
1119
1120 /* Update the inode times and copy the inode onto the inode page. */
1121 /* XXX kludge --- don't redirty the ifile just to put times on it */
1122 if (ip->i_number != LFS_IFILE_INUM)
1123 LFS_ITIMES(ip, NULL, NULL, NULL);
1124
1125 /*
1126 * If this is the Ifile, and we've already written the Ifile in this
1127 * partial segment, just overwrite it (it's not on disk yet) and
1128 * continue.
1129 *
1130 * XXX we know that the bp that we get the second time around has
1131 * already been gathered.
1132 */
1133 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
1134 *(sp->idp) = *ip->i_din.ffs1_din;
1135 ip->i_lfs_osize = ip->i_size;
1136 return 0;
1137 }
1138
1139 bp = sp->ibp;
1140 cdp = ((struct ufs1_dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
1141 *cdp = *ip->i_din.ffs1_din;
1142
1143 /*
1144 * If cleaning, link counts and directory file sizes cannot change,
1145 * since those would be directory operations---even if the file
1146 * we are writing is marked VU_DIROP we should write the old values.
1147 * If we're not cleaning, of course, update the values so we get
1148 * current values the next time we clean.
1149 */
1150 if (sp->seg_flags & SEGM_CLEAN) {
1151 if (ITOV(ip)->v_uflag & VU_DIROP) {
1152 cdp->di_nlink = ip->i_lfs_odnlink;
1153 /* if (ITOV(ip)->v_type == VDIR) */
1154 cdp->di_size = ip->i_lfs_osize;
1155 }
1156 } else {
1157 ip->i_lfs_odnlink = cdp->di_nlink;
1158 ip->i_lfs_osize = ip->i_size;
1159 }
1160
1161
1162 /* We can finish the segment accounting for truncations now */
1163 lfs_finalize_ino_seguse(fs, ip);
1164
1165 /*
1166 * If we are cleaning, ensure that we don't write UNWRITTEN disk
1167 * addresses to disk; possibly change the on-disk record of
1168 * the inode size, either by reverting to the previous size
1169 * (in the case of cleaning) or by verifying the inode's block
1170 * holdings (in the case of files being allocated as they are being
1171 * written).
1172 * XXX By not writing UNWRITTEN blocks, we are making the lfs_avail
1173 * XXX count on disk wrong by the same amount. We should be
1174 * XXX able to "borrow" from lfs_avail and return it after the
1175 * XXX Ifile is written. See also in lfs_writeseg.
1176 */
1177
1178 /* Check file size based on highest allocated block */
1179 if (((ip->i_ffs1_mode & IFMT) == IFREG ||
1180 (ip->i_ffs1_mode & IFMT) == IFDIR) &&
1181 ip->i_size > ((ip->i_lfs_hiblk + 1) << fs->lfs_bshift)) {
1182 cdp->di_size = (ip->i_lfs_hiblk + 1) << fs->lfs_bshift;
1183 DLOG((DLOG_SEG, "lfs_writeinode: ino %d size %" PRId64 " -> %"
1184 PRId64 "\n", (int)ip->i_number, ip->i_size, cdp->di_size));
1185 }
1186 if (ip->i_lfs_effnblks != ip->i_ffs1_blocks) {
1187 DLOG((DLOG_SEG, "lfs_writeinode: cleansing ino %d eff %d != nblk %d)"
1188 " at %x\n", ip->i_number, ip->i_lfs_effnblks,
1189 ip->i_ffs1_blocks, fs->lfs_offset));
1190 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
1191 daddrp++) {
1192 if (*daddrp == UNWRITTEN) {
1193 DLOG((DLOG_SEG, "lfs_writeinode: wiping UNWRITTEN\n"));
1194 *daddrp = 0;
1195 }
1196 }
1197 }
1198
1199 #ifdef DIAGNOSTIC
1200 /*
1201 * Check dinode held blocks against dinode size.
1202 * This should be identical to the check in lfs_vget().
1203 */
1204 for (i = (cdp->di_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
1205 i < NDADDR; i++) {
1206 KASSERT(i >= 0);
1207 if ((cdp->di_mode & IFMT) == IFLNK)
1208 continue;
1209 if (((cdp->di_mode & IFMT) == IFBLK ||
1210 (cdp->di_mode & IFMT) == IFCHR) && i == 0)
1211 continue;
1212 if (cdp->di_db[i] != 0) {
1213 # ifdef DEBUG
1214 lfs_dump_dinode(cdp);
1215 # endif
1216 panic("writing inconsistent inode");
1217 }
1218 }
1219 #endif /* DIAGNOSTIC */
1220
1221 if (ip->i_flag & IN_CLEANING)
1222 LFS_CLR_UINO(ip, IN_CLEANING);
1223 else {
1224 /* XXX IN_ALLMOD */
1225 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
1226 IN_UPDATE | IN_MODIFY);
1227 if (ip->i_lfs_effnblks == ip->i_ffs1_blocks)
1228 LFS_CLR_UINO(ip, IN_MODIFIED);
1229 else {
1230 DLOG((DLOG_VNODE, "lfs_writeinode: ino %d: real "
1231 "blks=%d, eff=%d\n", ip->i_number,
1232 ip->i_ffs1_blocks, ip->i_lfs_effnblks));
1233 }
1234 }
1235
1236 if (ip->i_number == LFS_IFILE_INUM) {
1237 /* We know sp->idp == NULL */
1238 sp->idp = ((struct ufs1_dinode *)bp->b_data) +
1239 (sp->ninodes % INOPB(fs));
1240
1241 /* Not dirty any more */
1242 mutex_enter(&lfs_lock);
1243 fs->lfs_flags &= ~LFS_IFDIRTY;
1244 mutex_exit(&lfs_lock);
1245 }
1246
1247 if (gotblk) {
1248 mutex_enter(&bufcache_lock);
1249 LFS_LOCK_BUF(bp);
1250 brelsel(bp, 0);
1251 mutex_exit(&bufcache_lock);
1252 }
1253
1254 /* Increment inode count in segment summary block. */
1255 ++((SEGSUM *)(sp->segsum))->ss_ninos;
1256
1257 /* If this page is full, set flag to allocate a new page. */
1258 if (++sp->ninodes % INOPB(fs) == 0)
1259 sp->ibp = NULL;
1260
1261 redo_ifile = lfs_update_iaddr(fs, sp, ip, bp->b_blkno);
1262
1263 KASSERT(redo_ifile == 0);
1264 return (redo_ifile);
1265 }
1266
1267 int
1268 lfs_gatherblock(struct segment *sp, struct buf *bp, kmutex_t *mptr)
1269 {
1270 struct lfs *fs;
1271 int vers;
1272 int j, blksinblk;
1273
1274 ASSERT_SEGLOCK(sp->fs);
1275 /*
1276 * If full, finish this segment. We may be doing I/O, so
1277 * release and reacquire the splbio().
1278 */
1279 #ifdef DIAGNOSTIC
1280 if (sp->vp == NULL)
1281 panic ("lfs_gatherblock: Null vp in segment");
1282 #endif
1283 fs = sp->fs;
1284 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
1285 if (sp->sum_bytes_left < sizeof(int32_t) * blksinblk ||
1286 sp->seg_bytes_left < bp->b_bcount) {
1287 if (mptr)
1288 mutex_exit(mptr);
1289 lfs_updatemeta(sp);
1290
1291 vers = sp->fip->fi_version;
1292 (void) lfs_writeseg(fs, sp);
1293
1294 /* Add the current file to the segment summary. */
1295 lfs_acquire_finfo(fs, VTOI(sp->vp)->i_number, vers);
1296
1297 if (mptr)
1298 mutex_enter(mptr);
1299 return (1);
1300 }
1301
1302 if (bp->b_flags & B_GATHERED) {
1303 DLOG((DLOG_SEG, "lfs_gatherblock: already gathered! Ino %d,"
1304 " lbn %" PRId64 "\n",
1305 sp->fip->fi_ino, bp->b_lblkno));
1306 return (0);
1307 }
1308
1309 /* Insert into the buffer list, update the FINFO block. */
1310 bp->b_flags |= B_GATHERED;
1311
1312 *sp->cbpp++ = bp;
1313 for (j = 0; j < blksinblk; j++) {
1314 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
1315 /* This block's accounting moves from lfs_favail to lfs_avail */
1316 lfs_deregister_block(sp->vp, bp->b_lblkno + j);
1317 }
1318
1319 sp->sum_bytes_left -= sizeof(int32_t) * blksinblk;
1320 sp->seg_bytes_left -= bp->b_bcount;
1321 return (0);
1322 }
1323
1324 int
1325 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp,
1326 int (*match)(struct lfs *, struct buf *))
1327 {
1328 struct buf *bp, *nbp;
1329 int count = 0;
1330
1331 ASSERT_SEGLOCK(fs);
1332 if (vp->v_type == VBLK)
1333 return 0;
1334 KASSERT(sp->vp == NULL);
1335 sp->vp = vp;
1336 mutex_enter(&bufcache_lock);
1337
1338 #ifndef LFS_NO_BACKBUF_HACK
1339 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
1340 # define BUF_OFFSET \
1341 (((char *)&LIST_NEXT(bp, b_vnbufs)) - (char *)bp)
1342 # define BACK_BUF(BP) \
1343 ((struct buf *)(((char *)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
1344 # define BEG_OF_LIST \
1345 ((struct buf *)(((char *)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
1346
1347 loop:
1348 /* Find last buffer. */
1349 for (bp = LIST_FIRST(&vp->v_dirtyblkhd);
1350 bp && LIST_NEXT(bp, b_vnbufs) != NULL;
1351 bp = LIST_NEXT(bp, b_vnbufs))
1352 /* nothing */;
1353 for (; bp && bp != BEG_OF_LIST; bp = nbp) {
1354 nbp = BACK_BUF(bp);
1355 #else /* LFS_NO_BACKBUF_HACK */
1356 loop:
1357 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1358 nbp = LIST_NEXT(bp, b_vnbufs);
1359 #endif /* LFS_NO_BACKBUF_HACK */
1360 if ((bp->b_cflags & BC_BUSY) != 0 ||
1361 (bp->b_flags & B_GATHERED) != 0 || !match(fs, bp)) {
1362 #ifdef DEBUG
1363 if (vp == fs->lfs_ivnode &&
1364 (bp->b_cflags & BC_BUSY) != 0 &&
1365 (bp->b_flags & B_GATHERED) == 0)
1366 log(LOG_NOTICE, "lfs_gather: ifile lbn %"
1367 PRId64 " busy (%x) at 0x%x",
1368 bp->b_lblkno, bp->b_flags,
1369 (unsigned)fs->lfs_offset);
1370 #endif
1371 continue;
1372 }
1373 #ifdef DIAGNOSTIC
1374 # ifdef LFS_USE_B_INVAL
1375 if ((bp->b_flags & BC_INVAL) != 0 && bp->b_iodone == NULL) {
1376 DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64
1377 " is BC_INVAL\n", bp->b_lblkno));
1378 VOP_PRINT(bp->b_vp);
1379 }
1380 # endif /* LFS_USE_B_INVAL */
1381 if (!(bp->b_oflags & BO_DELWRI))
1382 panic("lfs_gather: bp not BO_DELWRI");
1383 if (!(bp->b_flags & B_LOCKED)) {
1384 DLOG((DLOG_SEG, "lfs_gather: lbn %" PRId64
1385 " blk %" PRId64 " not B_LOCKED\n",
1386 bp->b_lblkno,
1387 dbtofsb(fs, bp->b_blkno)));
1388 VOP_PRINT(bp->b_vp);
1389 panic("lfs_gather: bp not B_LOCKED");
1390 }
1391 #endif
1392 if (lfs_gatherblock(sp, bp, &bufcache_lock)) {
1393 goto loop;
1394 }
1395 count++;
1396 }
1397 mutex_exit(&bufcache_lock);
1398 lfs_updatemeta(sp);
1399 KASSERT(sp->vp == vp);
1400 sp->vp = NULL;
1401 return count;
1402 }
1403
1404 #if DEBUG
1405 # define DEBUG_OOFF(n) do { \
1406 if (ooff == 0) { \
1407 DLOG((DLOG_SEG, "lfs_updatemeta[%d]: warning: writing " \
1408 "ino %d lbn %" PRId64 " at 0x%" PRIx32 \
1409 ", was 0x0 (or %" PRId64 ")\n", \
1410 (n), ip->i_number, lbn, ndaddr, daddr)); \
1411 } \
1412 } while (0)
1413 #else
1414 # define DEBUG_OOFF(n)
1415 #endif
1416
1417 /*
1418 * Change the given block's address to ndaddr, finding its previous
1419 * location using ufs_bmaparray().
1420 *
1421 * Account for this change in the segment table.
1422 *
1423 * called with sp == NULL by roll-forwarding code.
1424 */
1425 void
1426 lfs_update_single(struct lfs *fs, struct segment *sp,
1427 struct vnode *vp, daddr_t lbn, int32_t ndaddr, int size)
1428 {
1429 SEGUSE *sup;
1430 struct buf *bp;
1431 struct indir a[NIADDR + 2], *ap;
1432 struct inode *ip;
1433 daddr_t daddr, ooff;
1434 int num, error;
1435 int bb, osize, obb;
1436
1437 ASSERT_SEGLOCK(fs);
1438 KASSERT(sp == NULL || sp->vp == vp);
1439 ip = VTOI(vp);
1440
1441 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL, NULL);
1442 if (error)
1443 panic("lfs_updatemeta: ufs_bmaparray returned %d", error);
1444
1445 daddr = (daddr_t)((int32_t)daddr); /* XXX ondisk32 */
1446 KASSERT(daddr <= LFS_MAX_DADDR);
1447 if (daddr > 0)
1448 daddr = dbtofsb(fs, daddr);
1449
1450 bb = numfrags(fs, size);
1451 switch (num) {
1452 case 0:
1453 ooff = ip->i_ffs1_db[lbn];
1454 DEBUG_OOFF(0);
1455 if (ooff == UNWRITTEN)
1456 ip->i_ffs1_blocks += bb;
1457 else {
1458 /* possible fragment truncation or extension */
1459 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
1460 ip->i_ffs1_blocks += (bb - obb);
1461 }
1462 ip->i_ffs1_db[lbn] = ndaddr;
1463 break;
1464 case 1:
1465 ooff = ip->i_ffs1_ib[a[0].in_off];
1466 DEBUG_OOFF(1);
1467 if (ooff == UNWRITTEN)
1468 ip->i_ffs1_blocks += bb;
1469 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
1470 break;
1471 default:
1472 ap = &a[num - 1];
1473 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED,
1474 B_MODIFY, &bp))
1475 panic("lfs_updatemeta: bread bno %" PRId64,
1476 ap->in_lbn);
1477
1478 /* XXX ondisk32 */
1479 ooff = ((int32_t *)bp->b_data)[ap->in_off];
1480 DEBUG_OOFF(num);
1481 if (ooff == UNWRITTEN)
1482 ip->i_ffs1_blocks += bb;
1483 /* XXX ondisk32 */
1484 ((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
1485 (void) VOP_BWRITE(bp);
1486 }
1487
1488 KASSERT(ooff == 0 || ooff == UNWRITTEN || ooff == daddr);
1489
1490 /* Update hiblk when extending the file */
1491 if (lbn > ip->i_lfs_hiblk)
1492 ip->i_lfs_hiblk = lbn;
1493
1494 /*
1495 * Though we'd rather it couldn't, this *can* happen right now
1496 * if cleaning blocks and regular blocks coexist.
1497 */
1498 /* KASSERT(daddr < fs->lfs_lastpseg || daddr > ndaddr); */
1499
1500 /*
1501 * Update segment usage information, based on old size
1502 * and location.
1503 */
1504 if (daddr > 0) {
1505 u_int32_t oldsn = dtosn(fs, daddr);
1506 #ifdef DIAGNOSTIC
1507 int ndupino;
1508
1509 if (sp && sp->seg_number == oldsn) {
1510 ndupino = sp->ndupino;
1511 } else {
1512 ndupino = 0;
1513 }
1514 #endif
1515 KASSERT(oldsn < fs->lfs_nseg);
1516 if (lbn >= 0 && lbn < NDADDR)
1517 osize = ip->i_lfs_fragsize[lbn];
1518 else
1519 osize = fs->lfs_bsize;
1520 LFS_SEGENTRY(sup, fs, oldsn, bp);
1521 #ifdef DIAGNOSTIC
1522 if (sup->su_nbytes + sizeof (struct ufs1_dinode) * ndupino
1523 < osize) {
1524 printf("lfs_updatemeta: negative bytes "
1525 "(segment %" PRIu32 " short by %" PRId64
1526 ")\n", dtosn(fs, daddr),
1527 (int64_t)osize -
1528 (sizeof (struct ufs1_dinode) * ndupino +
1529 sup->su_nbytes));
1530 printf("lfs_updatemeta: ino %llu, lbn %" PRId64
1531 ", addr = 0x%" PRIx64 "\n",
1532 (unsigned long long)ip->i_number, lbn, daddr);
1533 printf("lfs_updatemeta: ndupino=%d\n", ndupino);
1534 panic("lfs_updatemeta: negative bytes");
1535 sup->su_nbytes = osize -
1536 sizeof (struct ufs1_dinode) * ndupino;
1537 }
1538 #endif
1539 DLOG((DLOG_SU, "seg %" PRIu32 " -= %d for ino %d lbn %" PRId64
1540 " db 0x%" PRIx64 "\n",
1541 dtosn(fs, daddr), osize,
1542 ip->i_number, lbn, daddr));
1543 sup->su_nbytes -= osize;
1544 if (!(bp->b_flags & B_GATHERED)) {
1545 mutex_enter(&lfs_lock);
1546 fs->lfs_flags |= LFS_IFDIRTY;
1547 mutex_exit(&lfs_lock);
1548 }
1549 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
1550 }
1551 /*
1552 * Now that this block has a new address, and its old
1553 * segment no longer owns it, we can forget about its
1554 * old size.
1555 */
1556 if (lbn >= 0 && lbn < NDADDR)
1557 ip->i_lfs_fragsize[lbn] = size;
1558 }
1559
1560 /*
1561 * Update the metadata that points to the blocks listed in the FINFO
1562 * array.
1563 */
1564 void
1565 lfs_updatemeta(struct segment *sp)
1566 {
1567 struct buf *sbp;
1568 struct lfs *fs;
1569 struct vnode *vp;
1570 daddr_t lbn;
1571 int i, nblocks, num;
1572 int bb;
1573 int bytesleft, size;
1574
1575 ASSERT_SEGLOCK(sp->fs);
1576 vp = sp->vp;
1577 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1578 KASSERT(nblocks >= 0);
1579 KASSERT(vp != NULL);
1580 if (nblocks == 0)
1581 return;
1582
1583 /*
1584 * This count may be high due to oversize blocks from lfs_gop_write.
1585 * Correct for this. (XXX we should be able to keep track of these.)
1586 */
1587 fs = sp->fs;
1588 for (i = 0; i < nblocks; i++) {
1589 if (sp->start_bpp[i] == NULL) {
1590 DLOG((DLOG_SEG, "lfs_updatemeta: nblocks = %d, not %d\n", i, nblocks));
1591 nblocks = i;
1592 break;
1593 }
1594 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
1595 KASSERT(sp->start_bpp[i]->b_lblkno >= 0 || num == 1);
1596 nblocks -= num - 1;
1597 }
1598
1599 KASSERT(vp->v_type == VREG ||
1600 nblocks == &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp);
1601 KASSERT(nblocks == sp->cbpp - sp->start_bpp);
1602
1603 /*
1604 * Sort the blocks.
1605 *
1606 * We have to sort even if the blocks come from the
1607 * cleaner, because there might be other pending blocks on the
1608 * same inode...and if we don't sort, and there are fragments
1609 * present, blocks may be written in the wrong place.
1610 */
1611 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
1612
1613 /*
1614 * Record the length of the last block in case it's a fragment.
1615 * If there are indirect blocks present, they sort last. An
1616 * indirect block will be lfs_bsize and its presence indicates
1617 * that you cannot have fragments.
1618 *
1619 * XXX This last is a lie. A cleaned fragment can coexist with
1620 * XXX a later indirect block. This will continue to be
1621 * XXX true until lfs_markv is fixed to do everything with
1622 * XXX fake blocks (including fake inodes and fake indirect blocks).
1623 */
1624 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
1625 fs->lfs_bmask) + 1;
1626
1627 /*
1628 * Assign disk addresses, and update references to the logical
1629 * block and the segment usage information.
1630 */
1631 for (i = nblocks; i--; ++sp->start_bpp) {
1632 sbp = *sp->start_bpp;
1633 lbn = *sp->start_lbp;
1634 KASSERT(sbp->b_lblkno == lbn);
1635
1636 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1637
1638 /*
1639 * If we write a frag in the wrong place, the cleaner won't
1640 * be able to correctly identify its size later, and the
1641 * segment will be uncleanable. (Even worse, it will assume
1642 * that the indirect block that actually ends the list
1643 * is of a smaller size!)
1644 */
1645 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
1646 panic("lfs_updatemeta: fragment is not last block");
1647
1648 /*
1649 * For each subblock in this possibly oversized block,
1650 * update its address on disk.
1651 */
1652 KASSERT(lbn >= 0 || sbp->b_bcount == fs->lfs_bsize);
1653 KASSERT(vp == sbp->b_vp);
1654 for (bytesleft = sbp->b_bcount; bytesleft > 0;
1655 bytesleft -= fs->lfs_bsize) {
1656 size = MIN(bytesleft, fs->lfs_bsize);
1657 bb = numfrags(fs, size);
1658 lbn = *sp->start_lbp++;
1659 lfs_update_single(fs, sp, sp->vp, lbn, fs->lfs_offset,
1660 size);
1661 fs->lfs_offset += bb;
1662 }
1663
1664 }
1665
1666 /* This inode has been modified */
1667 LFS_SET_UINO(VTOI(vp), IN_MODIFIED);
1668 }
1669
1670 /*
1671 * Move lfs_offset to a segment earlier than sn.
1672 */
1673 int
1674 lfs_rewind(struct lfs *fs, int newsn)
1675 {
1676 int sn, osn, isdirty;
1677 struct buf *bp;
1678 SEGUSE *sup;
1679
1680 ASSERT_SEGLOCK(fs);
1681
1682 osn = dtosn(fs, fs->lfs_offset);
1683 if (osn < newsn)
1684 return 0;
1685
1686 /* lfs_avail eats the remaining space in this segment */
1687 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset - fs->lfs_curseg);
1688
1689 /* Find a low-numbered segment */
1690 for (sn = 0; sn < fs->lfs_nseg; ++sn) {
1691 LFS_SEGENTRY(sup, fs, sn, bp);
1692 isdirty = sup->su_flags & SEGUSE_DIRTY;
1693 brelse(bp, 0);
1694
1695 if (!isdirty)
1696 break;
1697 }
1698 if (sn == fs->lfs_nseg)
1699 panic("lfs_rewind: no clean segments");
1700 if (newsn >= 0 && sn >= newsn)
1701 return ENOENT;
1702 fs->lfs_nextseg = sn;
1703 lfs_newseg(fs);
1704 fs->lfs_offset = fs->lfs_curseg;
1705
1706 return 0;
1707 }
1708
1709 /*
1710 * Start a new partial segment.
1711 *
1712 * Return 1 when we entered to a new segment.
1713 * Otherwise, return 0.
1714 */
1715 int
1716 lfs_initseg(struct lfs *fs)
1717 {
1718 struct segment *sp = fs->lfs_sp;
1719 SEGSUM *ssp;
1720 struct buf *sbp; /* buffer for SEGSUM */
1721 int repeat = 0; /* return value */
1722
1723 ASSERT_SEGLOCK(fs);
1724 /* Advance to the next segment. */
1725 if (!LFS_PARTIAL_FITS(fs)) {
1726 SEGUSE *sup;
1727 struct buf *bp;
1728
1729 /* lfs_avail eats the remaining space */
1730 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1731 fs->lfs_curseg);
1732 /* Wake up any cleaning procs waiting on this file system. */
1733 lfs_wakeup_cleaner(fs);
1734 lfs_newseg(fs);
1735 repeat = 1;
1736 fs->lfs_offset = fs->lfs_curseg;
1737
1738 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1739 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1740
1741 /*
1742 * If the segment contains a superblock, update the offset
1743 * and summary address to skip over it.
1744 */
1745 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1746 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1747 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1748 sp->seg_bytes_left -= LFS_SBPAD;
1749 }
1750 brelse(bp, 0);
1751 /* Segment zero could also contain the labelpad */
1752 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1753 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1754 fs->lfs_offset +=
1755 btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1756 sp->seg_bytes_left -=
1757 LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1758 }
1759 } else {
1760 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1761 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1762 (fs->lfs_offset - fs->lfs_curseg));
1763 }
1764 fs->lfs_lastpseg = fs->lfs_offset;
1765
1766 /* Record first address of this partial segment */
1767 if (sp->seg_flags & SEGM_CLEAN) {
1768 fs->lfs_cleanint[fs->lfs_cleanind] = fs->lfs_offset;
1769 if (++fs->lfs_cleanind >= LFS_MAX_CLEANIND) {
1770 /* "1" is the artificial inc in lfs_seglock */
1771 mutex_enter(&lfs_lock);
1772 while (fs->lfs_iocount > 1) {
1773 mtsleep(&fs->lfs_iocount, PRIBIO + 1,
1774 "lfs_initseg", 0, &lfs_lock);
1775 }
1776 mutex_exit(&lfs_lock);
1777 fs->lfs_cleanind = 0;
1778 }
1779 }
1780
1781 sp->fs = fs;
1782 sp->ibp = NULL;
1783 sp->idp = NULL;
1784 sp->ninodes = 0;
1785 sp->ndupino = 0;
1786
1787 sp->cbpp = sp->bpp;
1788
1789 /* Get a new buffer for SEGSUM */
1790 sbp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1791 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize, LFS_NB_SUMMARY);
1792
1793 /* ... and enter it into the buffer list. */
1794 *sp->cbpp = sbp;
1795 sp->cbpp++;
1796 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1797
1798 sp->start_bpp = sp->cbpp;
1799
1800 /* Set point to SEGSUM, initialize it. */
1801 ssp = sp->segsum = sbp->b_data;
1802 memset(ssp, 0, fs->lfs_sumsize);
1803 ssp->ss_next = fs->lfs_nextseg;
1804 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1805 ssp->ss_magic = SS_MAGIC;
1806
1807 /* Set pointer to first FINFO, initialize it. */
1808 sp->fip = (struct finfo *)((char *)sp->segsum + SEGSUM_SIZE(fs));
1809 sp->fip->fi_nblocks = 0;
1810 sp->start_lbp = &sp->fip->fi_blocks[0];
1811 sp->fip->fi_lastlength = 0;
1812
1813 sp->seg_bytes_left -= fs->lfs_sumsize;
1814 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1815
1816 return (repeat);
1817 }
1818
1819 /*
1820 * Remove SEGUSE_INVAL from all segments.
1821 */
1822 void
1823 lfs_unset_inval_all(struct lfs *fs)
1824 {
1825 SEGUSE *sup;
1826 struct buf *bp;
1827 int i;
1828
1829 for (i = 0; i < fs->lfs_nseg; i++) {
1830 LFS_SEGENTRY(sup, fs, i, bp);
1831 if (sup->su_flags & SEGUSE_INVAL) {
1832 sup->su_flags &= ~SEGUSE_INVAL;
1833 LFS_WRITESEGENTRY(sup, fs, i, bp);
1834 } else
1835 brelse(bp, 0);
1836 }
1837 }
1838
1839 /*
1840 * Return the next segment to write.
1841 */
1842 void
1843 lfs_newseg(struct lfs *fs)
1844 {
1845 CLEANERINFO *cip;
1846 SEGUSE *sup;
1847 struct buf *bp;
1848 int curseg, isdirty, sn, skip_inval;
1849
1850 ASSERT_SEGLOCK(fs);
1851
1852 /* Honor LFCNWRAPSTOP */
1853 mutex_enter(&lfs_lock);
1854 while (fs->lfs_nextseg < fs->lfs_curseg && fs->lfs_nowrap) {
1855 if (fs->lfs_wrappass) {
1856 log(LOG_NOTICE, "%s: wrappass=%d\n",
1857 fs->lfs_fsmnt, fs->lfs_wrappass);
1858 fs->lfs_wrappass = 0;
1859 break;
1860 }
1861 fs->lfs_wrapstatus = LFS_WRAP_WAITING;
1862 wakeup(&fs->lfs_nowrap);
1863 log(LOG_NOTICE, "%s: waiting at log wrap\n", fs->lfs_fsmnt);
1864 mtsleep(&fs->lfs_wrappass, PVFS, "newseg", 10 * hz,
1865 &lfs_lock);
1866 }
1867 fs->lfs_wrapstatus = LFS_WRAP_GOING;
1868 mutex_exit(&lfs_lock);
1869
1870 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1871 DLOG((DLOG_SU, "lfs_newseg: seg %d := 0 in newseg\n",
1872 dtosn(fs, fs->lfs_nextseg)));
1873 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1874 sup->su_nbytes = 0;
1875 sup->su_nsums = 0;
1876 sup->su_ninos = 0;
1877 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1878
1879 LFS_CLEANERINFO(cip, fs, bp);
1880 --cip->clean;
1881 ++cip->dirty;
1882 fs->lfs_nclean = cip->clean;
1883 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1884
1885 fs->lfs_lastseg = fs->lfs_curseg;
1886 fs->lfs_curseg = fs->lfs_nextseg;
1887 skip_inval = 1;
1888 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1889 sn = (sn + 1) % fs->lfs_nseg;
1890
1891 if (sn == curseg) {
1892 if (skip_inval)
1893 skip_inval = 0;
1894 else
1895 panic("lfs_nextseg: no clean segments");
1896 }
1897 LFS_SEGENTRY(sup, fs, sn, bp);
1898 isdirty = sup->su_flags & (SEGUSE_DIRTY | (skip_inval ? SEGUSE_INVAL : 0));
1899 /* Check SEGUSE_EMPTY as we go along */
1900 if (isdirty && sup->su_nbytes == 0 &&
1901 !(sup->su_flags & SEGUSE_EMPTY))
1902 LFS_WRITESEGENTRY(sup, fs, sn, bp);
1903 else
1904 brelse(bp, 0);
1905
1906 if (!isdirty)
1907 break;
1908 }
1909 if (skip_inval == 0)
1910 lfs_unset_inval_all(fs);
1911
1912 ++fs->lfs_nactive;
1913 fs->lfs_nextseg = sntod(fs, sn);
1914 if (lfs_dostats) {
1915 ++lfs_stats.segsused;
1916 }
1917 }
1918
1919 static struct buf *
1920 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr,
1921 int n)
1922 {
1923 struct lfs_cluster *cl;
1924 struct buf **bpp, *bp;
1925
1926 ASSERT_SEGLOCK(fs);
1927 cl = (struct lfs_cluster *)pool_get(&fs->lfs_clpool, PR_WAITOK);
1928 bpp = (struct buf **)pool_get(&fs->lfs_bpppool, PR_WAITOK);
1929 memset(cl, 0, sizeof(*cl));
1930 cl->fs = fs;
1931 cl->bpp = bpp;
1932 cl->bufcount = 0;
1933 cl->bufsize = 0;
1934
1935 /* If this segment is being written synchronously, note that */
1936 if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
1937 cl->flags |= LFS_CL_SYNC;
1938 cl->seg = fs->lfs_sp;
1939 ++cl->seg->seg_iocount;
1940 }
1941
1942 /* Get an empty buffer header, or maybe one with something on it */
1943 bp = getiobuf(vp, true);
1944 bp->b_dev = NODEV;
1945 bp->b_blkno = bp->b_lblkno = addr;
1946 bp->b_iodone = lfs_cluster_callback;
1947 bp->b_private = cl;
1948
1949 return bp;
1950 }
1951
1952 int
1953 lfs_writeseg(struct lfs *fs, struct segment *sp)
1954 {
1955 struct buf **bpp, *bp, *cbp, *newbp, *unbusybp;
1956 SEGUSE *sup;
1957 SEGSUM *ssp;
1958 int i;
1959 int do_again, nblocks, byteoffset;
1960 size_t el_size;
1961 struct lfs_cluster *cl;
1962 u_short ninos;
1963 struct vnode *devvp;
1964 char *p = NULL;
1965 struct vnode *vp;
1966 int32_t *daddrp; /* XXX ondisk32 */
1967 int changed;
1968 u_int32_t sum;
1969 #ifdef DEBUG
1970 FINFO *fip;
1971 int findex;
1972 #endif
1973
1974 ASSERT_SEGLOCK(fs);
1975
1976 ssp = (SEGSUM *)sp->segsum;
1977
1978 /*
1979 * If there are no buffers other than the segment summary to write,
1980 * don't do anything. If we are the end of a dirop sequence, however,
1981 * write the empty segment summary anyway, to help out the
1982 * roll-forward agent.
1983 */
1984 if ((nblocks = sp->cbpp - sp->bpp) == 1) {
1985 if ((ssp->ss_flags & (SS_DIROP | SS_CONT)) != SS_DIROP)
1986 return 0;
1987 }
1988
1989 /* Note if partial segment is being written by the cleaner */
1990 if (sp->seg_flags & SEGM_CLEAN)
1991 ssp->ss_flags |= SS_CLEAN;
1992
1993 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1994
1995 /* Update the segment usage information. */
1996 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1997
1998 /* Loop through all blocks, except the segment summary. */
1999 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
2000 if ((*bpp)->b_vp != devvp) {
2001 sup->su_nbytes += (*bpp)->b_bcount;
2002 DLOG((DLOG_SU, "seg %" PRIu32 " += %ld for ino %d"
2003 " lbn %" PRId64 " db 0x%" PRIx64 "\n",
2004 sp->seg_number, (*bpp)->b_bcount,
2005 VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno,
2006 (*bpp)->b_blkno));
2007 }
2008 }
2009
2010 #ifdef DEBUG
2011 /* Check for zero-length and zero-version FINFO entries. */
2012 fip = (struct finfo *)((char *)ssp + SEGSUM_SIZE(fs));
2013 for (findex = 0; findex < ssp->ss_nfinfo; findex++) {
2014 KDASSERT(fip->fi_nblocks > 0);
2015 KDASSERT(fip->fi_version > 0);
2016 fip = (FINFO *)((char *)fip + FINFOSIZE +
2017 sizeof(int32_t) * fip->fi_nblocks);
2018 }
2019 #endif /* DEBUG */
2020
2021 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
2022 DLOG((DLOG_SU, "seg %d += %d for %d inodes\n",
2023 sp->seg_number, ssp->ss_ninos * sizeof (struct ufs1_dinode),
2024 ssp->ss_ninos));
2025 sup->su_nbytes += ssp->ss_ninos * sizeof (struct ufs1_dinode);
2026 /* sup->su_nbytes += fs->lfs_sumsize; */
2027 if (fs->lfs_version == 1)
2028 sup->su_olastmod = time_second;
2029 else
2030 sup->su_lastmod = time_second;
2031 sup->su_ninos += ninos;
2032 ++sup->su_nsums;
2033 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
2034
2035 do_again = !(bp->b_flags & B_GATHERED);
2036 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
2037
2038 /*
2039 * Mark blocks B_BUSY, to prevent then from being changed between
2040 * the checksum computation and the actual write.
2041 *
2042 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
2043 * there are any, replace them with copies that have UNASSIGNED
2044 * instead.
2045 */
2046 mutex_enter(&bufcache_lock);
2047 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
2048 ++bpp;
2049 bp = *bpp;
2050 if (bp->b_iodone != NULL) { /* UBC or malloced buffer */
2051 bp->b_cflags |= BC_BUSY;
2052 continue;
2053 }
2054
2055 while (bp->b_cflags & BC_BUSY) {
2056 DLOG((DLOG_SEG, "lfs_writeseg: avoiding potential"
2057 " data summary corruption for ino %d, lbn %"
2058 PRId64 "\n",
2059 VTOI(bp->b_vp)->i_number, bp->b_lblkno));
2060 bp->b_cflags |= BC_WANTED;
2061 cv_wait(&bp->b_busy, &bufcache_lock);
2062 }
2063 bp->b_cflags |= BC_BUSY;
2064 mutex_exit(&bufcache_lock);
2065 unbusybp = NULL;
2066
2067 /*
2068 * Check and replace indirect block UNWRITTEN bogosity.
2069 * XXX See comment in lfs_writefile.
2070 */
2071 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
2072 VTOI(bp->b_vp)->i_ffs1_blocks !=
2073 VTOI(bp->b_vp)->i_lfs_effnblks) {
2074 DLOG((DLOG_VNODE, "lfs_writeseg: cleansing ino %d (%d != %d)\n",
2075 VTOI(bp->b_vp)->i_number,
2076 VTOI(bp->b_vp)->i_lfs_effnblks,
2077 VTOI(bp->b_vp)->i_ffs1_blocks));
2078 /* Make a copy we'll make changes to */
2079 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
2080 bp->b_bcount, LFS_NB_IBLOCK);
2081 newbp->b_blkno = bp->b_blkno;
2082 memcpy(newbp->b_data, bp->b_data,
2083 newbp->b_bcount);
2084
2085 changed = 0;
2086 /* XXX ondisk32 */
2087 for (daddrp = (int32_t *)(newbp->b_data);
2088 daddrp < (int32_t *)((char *)newbp->b_data +
2089 newbp->b_bcount); daddrp++) {
2090 if (*daddrp == UNWRITTEN) {
2091 ++changed;
2092 *daddrp = 0;
2093 }
2094 }
2095 /*
2096 * Get rid of the old buffer. Don't mark it clean,
2097 * though, if it still has dirty data on it.
2098 */
2099 if (changed) {
2100 DLOG((DLOG_SEG, "lfs_writeseg: replacing UNWRITTEN(%d):"
2101 " bp = %p newbp = %p\n", changed, bp,
2102 newbp));
2103 *bpp = newbp;
2104 bp->b_flags &= ~B_GATHERED;
2105 bp->b_error = 0;
2106 if (bp->b_iodone != NULL) {
2107 DLOG((DLOG_SEG, "lfs_writeseg: "
2108 "indir bp should not be B_CALL\n"));
2109 biodone(bp);
2110 bp = NULL;
2111 } else {
2112 /* Still on free list, leave it there */
2113 unbusybp = bp;
2114 /*
2115 * We have to re-decrement lfs_avail
2116 * since this block is going to come
2117 * back around to us in the next
2118 * segment.
2119 */
2120 fs->lfs_avail -=
2121 btofsb(fs, bp->b_bcount);
2122 }
2123 } else {
2124 lfs_freebuf(fs, newbp);
2125 }
2126 }
2127 mutex_enter(&bufcache_lock);
2128 if (unbusybp != NULL) {
2129 unbusybp->b_cflags &= ~BC_BUSY;
2130 if (unbusybp->b_cflags & BC_WANTED)
2131 cv_broadcast(&bp->b_busy);
2132 }
2133 }
2134 mutex_exit(&bufcache_lock);
2135
2136 /*
2137 * Compute checksum across data and then across summary; the first
2138 * block (the summary block) is skipped. Set the create time here
2139 * so that it's guaranteed to be later than the inode mod times.
2140 */
2141 sum = 0;
2142 if (fs->lfs_version == 1)
2143 el_size = sizeof(u_long);
2144 else
2145 el_size = sizeof(u_int32_t);
2146 for (bpp = sp->bpp, i = nblocks - 1; i--; ) {
2147 ++bpp;
2148 /* Loop through gop_write cluster blocks */
2149 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
2150 byteoffset += fs->lfs_bsize) {
2151 #ifdef LFS_USE_B_INVAL
2152 if (((*bpp)->b_cflags & BC_INVAL) != 0 &&
2153 (*bpp)->b_iodone != NULL) {
2154 if (copyin((void *)(*bpp)->b_saveaddr +
2155 byteoffset, dp, el_size)) {
2156 panic("lfs_writeseg: copyin failed [1]:"
2157 " ino %d blk %" PRId64,
2158 VTOI((*bpp)->b_vp)->i_number,
2159 (*bpp)->b_lblkno);
2160 }
2161 } else
2162 #endif /* LFS_USE_B_INVAL */
2163 {
2164 sum = lfs_cksum_part((char *)
2165 (*bpp)->b_data + byteoffset, el_size, sum);
2166 }
2167 }
2168 }
2169 if (fs->lfs_version == 1)
2170 ssp->ss_ocreate = time_second;
2171 else {
2172 ssp->ss_create = time_second;
2173 ssp->ss_serial = ++fs->lfs_serial;
2174 ssp->ss_ident = fs->lfs_ident;
2175 }
2176 ssp->ss_datasum = lfs_cksum_fold(sum);
2177 ssp->ss_sumsum = cksum(&ssp->ss_datasum,
2178 fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
2179
2180 mutex_enter(&lfs_lock);
2181 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
2182 btofsb(fs, fs->lfs_sumsize));
2183 fs->lfs_dmeta += (btofsb(fs, ninos * fs->lfs_ibsize) +
2184 btofsb(fs, fs->lfs_sumsize));
2185 mutex_exit(&lfs_lock);
2186
2187 /*
2188 * When we simply write the blocks we lose a rotation for every block
2189 * written. To avoid this problem, we cluster the buffers into a
2190 * chunk and write the chunk. MAXPHYS is the largest size I/O
2191 * devices can handle, use that for the size of the chunks.
2192 *
2193 * Blocks that are already clusters (from GOP_WRITE), however, we
2194 * don't bother to copy into other clusters.
2195 */
2196
2197 #define CHUNKSIZE MAXPHYS
2198
2199 if (devvp == NULL)
2200 panic("devvp is NULL");
2201 for (bpp = sp->bpp, i = nblocks; i;) {
2202 cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
2203 cl = cbp->b_private;
2204
2205 cbp->b_flags |= B_ASYNC;
2206 cbp->b_cflags |= BC_BUSY;
2207 cbp->b_bcount = 0;
2208
2209 #if defined(DEBUG) && defined(DIAGNOSTIC)
2210 if (bpp - sp->bpp > (fs->lfs_sumsize - SEGSUM_SIZE(fs))
2211 / sizeof(int32_t)) {
2212 panic("lfs_writeseg: real bpp overwrite");
2213 }
2214 if (bpp - sp->bpp > segsize(fs) / fs->lfs_fsize) {
2215 panic("lfs_writeseg: theoretical bpp overwrite");
2216 }
2217 #endif
2218
2219 /*
2220 * Construct the cluster.
2221 */
2222 mutex_enter(&lfs_lock);
2223 ++fs->lfs_iocount;
2224 mutex_exit(&lfs_lock);
2225 while (i && cbp->b_bcount < CHUNKSIZE) {
2226 bp = *bpp;
2227
2228 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
2229 break;
2230 if (cbp->b_bcount > 0 && !(cl->flags & LFS_CL_MALLOC))
2231 break;
2232
2233 /* Clusters from GOP_WRITE are expedited */
2234 if (bp->b_bcount > fs->lfs_bsize) {
2235 if (cbp->b_bcount > 0)
2236 /* Put in its own buffer */
2237 break;
2238 else {
2239 cbp->b_data = bp->b_data;
2240 }
2241 } else if (cbp->b_bcount == 0) {
2242 p = cbp->b_data = lfs_malloc(fs, CHUNKSIZE,
2243 LFS_NB_CLUSTER);
2244 cl->flags |= LFS_CL_MALLOC;
2245 }
2246 #ifdef DIAGNOSTIC
2247 if (dtosn(fs, dbtofsb(fs, bp->b_blkno +
2248 btodb(bp->b_bcount - 1))) !=
2249 sp->seg_number) {
2250 printf("blk size %d daddr %" PRIx64
2251 " not in seg %d\n",
2252 bp->b_bcount, bp->b_blkno,
2253 sp->seg_number);
2254 panic("segment overwrite");
2255 }
2256 #endif
2257
2258 #ifdef LFS_USE_B_INVAL
2259 /*
2260 * Fake buffers from the cleaner are marked as B_INVAL.
2261 * We need to copy the data from user space rather than
2262 * from the buffer indicated.
2263 * XXX == what do I do on an error?
2264 */
2265 if ((bp->b_cflags & BC_INVAL) != 0 &&
2266 bp->b_iodone != NULL) {
2267 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
2268 panic("lfs_writeseg: "
2269 "copyin failed [2]");
2270 } else
2271 #endif /* LFS_USE_B_INVAL */
2272 if (cl->flags & LFS_CL_MALLOC) {
2273 /* copy data into our cluster. */
2274 memcpy(p, bp->b_data, bp->b_bcount);
2275 p += bp->b_bcount;
2276 }
2277
2278 cbp->b_bcount += bp->b_bcount;
2279 cl->bufsize += bp->b_bcount;
2280
2281 bp->b_flags &= ~B_READ;
2282 bp->b_error = 0;
2283 cl->bpp[cl->bufcount++] = bp;
2284
2285 vp = bp->b_vp;
2286 mutex_enter(&bufcache_lock);
2287 mutex_enter(&vp->v_interlock);
2288 bp->b_oflags &= ~(BO_DELWRI | BO_DONE);
2289 reassignbuf(bp, vp);
2290 vp->v_numoutput++;
2291 mutex_exit(&vp->v_interlock);
2292 mutex_exit(&bufcache_lock);
2293
2294 bpp++;
2295 i--;
2296 }
2297 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
2298 BIO_SETPRIO(cbp, BPRIO_TIMECRITICAL);
2299 else
2300 BIO_SETPRIO(cbp, BPRIO_TIMELIMITED);
2301 mutex_enter(&devvp->v_interlock);
2302 devvp->v_numoutput++;
2303 mutex_exit(&devvp->v_interlock);
2304 VOP_STRATEGY(devvp, cbp);
2305 curlwp->l_ru.ru_oublock++;
2306 }
2307
2308 if (lfs_dostats) {
2309 ++lfs_stats.psegwrites;
2310 lfs_stats.blocktot += nblocks - 1;
2311 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
2312 ++lfs_stats.psyncwrites;
2313 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
2314 ++lfs_stats.pcleanwrites;
2315 lfs_stats.cleanblocks += nblocks - 1;
2316 }
2317 }
2318
2319 return (lfs_initseg(fs) || do_again);
2320 }
2321
2322 void
2323 lfs_writesuper(struct lfs *fs, daddr_t daddr)
2324 {
2325 struct buf *bp;
2326 struct vnode *devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2327 int s;
2328
2329 ASSERT_MAYBE_SEGLOCK(fs);
2330 #ifdef DIAGNOSTIC
2331 KASSERT(fs->lfs_magic == LFS_MAGIC);
2332 #endif
2333 /*
2334 * If we can write one superblock while another is in
2335 * progress, we risk not having a complete checkpoint if we crash.
2336 * So, block here if a superblock write is in progress.
2337 */
2338 mutex_enter(&lfs_lock);
2339 s = splbio();
2340 while (fs->lfs_sbactive) {
2341 mtsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0,
2342 &lfs_lock);
2343 }
2344 fs->lfs_sbactive = daddr;
2345 splx(s);
2346 mutex_exit(&lfs_lock);
2347
2348 /* Set timestamp of this version of the superblock */
2349 if (fs->lfs_version == 1)
2350 fs->lfs_otstamp = time_second;
2351 fs->lfs_tstamp = time_second;
2352
2353 /* Checksum the superblock and copy it into a buffer. */
2354 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
2355 bp = lfs_newbuf(fs, devvp,
2356 fsbtodb(fs, daddr), LFS_SBPAD, LFS_NB_SBLOCK);
2357 memset((char *)bp->b_data + sizeof(struct dlfs), 0,
2358 LFS_SBPAD - sizeof(struct dlfs));
2359 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
2360
2361 bp->b_cflags |= BC_BUSY;
2362 bp->b_flags = (bp->b_flags & ~B_READ) | B_ASYNC;
2363 bp->b_oflags &= ~(BO_DONE | BO_DELWRI);
2364 bp->b_error = 0;
2365 bp->b_iodone = lfs_supercallback;
2366
2367 if (fs->lfs_sp != NULL && fs->lfs_sp->seg_flags & SEGM_SYNC)
2368 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
2369 else
2370 BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
2371 curlwp->l_ru.ru_oublock++;
2372
2373 mutex_enter(&devvp->v_interlock);
2374 devvp->v_numoutput++;
2375 mutex_exit(&devvp->v_interlock);
2376
2377 mutex_enter(&lfs_lock);
2378 ++fs->lfs_iocount;
2379 mutex_exit(&lfs_lock);
2380 VOP_STRATEGY(devvp, bp);
2381 }
2382
2383 /*
2384 * Logical block number match routines used when traversing the dirty block
2385 * chain.
2386 */
2387 int
2388 lfs_match_fake(struct lfs *fs, struct buf *bp)
2389 {
2390
2391 ASSERT_SEGLOCK(fs);
2392 return LFS_IS_MALLOC_BUF(bp);
2393 }
2394
2395 #if 0
2396 int
2397 lfs_match_real(struct lfs *fs, struct buf *bp)
2398 {
2399
2400 ASSERT_SEGLOCK(fs);
2401 return (lfs_match_data(fs, bp) && !lfs_match_fake(fs, bp));
2402 }
2403 #endif
2404
2405 int
2406 lfs_match_data(struct lfs *fs, struct buf *bp)
2407 {
2408
2409 ASSERT_SEGLOCK(fs);
2410 return (bp->b_lblkno >= 0);
2411 }
2412
2413 int
2414 lfs_match_indir(struct lfs *fs, struct buf *bp)
2415 {
2416 daddr_t lbn;
2417
2418 ASSERT_SEGLOCK(fs);
2419 lbn = bp->b_lblkno;
2420 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
2421 }
2422
2423 int
2424 lfs_match_dindir(struct lfs *fs, struct buf *bp)
2425 {
2426 daddr_t lbn;
2427
2428 ASSERT_SEGLOCK(fs);
2429 lbn = bp->b_lblkno;
2430 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
2431 }
2432
2433 int
2434 lfs_match_tindir(struct lfs *fs, struct buf *bp)
2435 {
2436 daddr_t lbn;
2437
2438 ASSERT_SEGLOCK(fs);
2439 lbn = bp->b_lblkno;
2440 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
2441 }
2442
2443 static void
2444 lfs_free_aiodone(struct buf *bp)
2445 {
2446 struct lfs *fs;
2447
2448 KERNEL_LOCK(1, curlwp);
2449 fs = bp->b_private;
2450 ASSERT_NO_SEGLOCK(fs);
2451 lfs_freebuf(fs, bp);
2452 KERNEL_UNLOCK_LAST(curlwp);
2453 }
2454
2455 static void
2456 lfs_super_aiodone(struct buf *bp)
2457 {
2458 struct lfs *fs;
2459
2460 KERNEL_LOCK(1, curlwp);
2461 fs = bp->b_private;
2462 ASSERT_NO_SEGLOCK(fs);
2463 mutex_enter(&lfs_lock);
2464 fs->lfs_sbactive = 0;
2465 if (--fs->lfs_iocount <= 1)
2466 wakeup(&fs->lfs_iocount);
2467 wakeup(&fs->lfs_sbactive);
2468 mutex_exit(&lfs_lock);
2469 lfs_freebuf(fs, bp);
2470 KERNEL_UNLOCK_LAST(curlwp);
2471 }
2472
2473 static void
2474 lfs_cluster_aiodone(struct buf *bp)
2475 {
2476 struct lfs_cluster *cl;
2477 struct lfs *fs;
2478 struct buf *tbp, *fbp;
2479 struct vnode *vp, *devvp, *ovp;
2480 struct inode *ip;
2481 int error;
2482
2483 KERNEL_LOCK(1, curlwp);
2484
2485 error = bp->b_error;
2486 cl = bp->b_private;
2487 fs = cl->fs;
2488 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2489 ASSERT_NO_SEGLOCK(fs);
2490
2491 /* Put the pages back, and release the buffer */
2492 while (cl->bufcount--) {
2493 tbp = cl->bpp[cl->bufcount];
2494 KASSERT(tbp->b_cflags & BC_BUSY);
2495 if (error) {
2496 tbp->b_error = error;
2497 }
2498
2499 /*
2500 * We're done with tbp. If it has not been re-dirtied since
2501 * the cluster was written, free it. Otherwise, keep it on
2502 * the locked list to be written again.
2503 */
2504 vp = tbp->b_vp;
2505
2506 tbp->b_flags &= ~B_GATHERED;
2507
2508 LFS_BCLEAN_LOG(fs, tbp);
2509
2510 mutex_enter(&bufcache_lock);
2511 if (tbp->b_iodone == NULL) {
2512 KASSERT(tbp->b_flags & B_LOCKED);
2513 bremfree(tbp);
2514 if (vp) {
2515 mutex_enter(&vp->v_interlock);
2516 reassignbuf(tbp, vp);
2517 mutex_exit(&vp->v_interlock);
2518 }
2519 tbp->b_flags |= B_ASYNC; /* for biodone */
2520 }
2521
2522 if (((tbp->b_flags | tbp->b_oflags) &
2523 (B_LOCKED | BO_DELWRI)) == B_LOCKED)
2524 LFS_UNLOCK_BUF(tbp);
2525
2526 if (tbp->b_oflags & BO_DONE) {
2527 DLOG((DLOG_SEG, "blk %d biodone already (flags %lx)\n",
2528 cl->bufcount, (long)tbp->b_flags));
2529 }
2530
2531 if (tbp->b_iodone != NULL && !LFS_IS_MALLOC_BUF(tbp)) {
2532 /*
2533 * A buffer from the page daemon.
2534 * We use the same iodone as it does,
2535 * so we must manually disassociate its
2536 * buffers from the vp.
2537 */
2538 if ((ovp = tbp->b_vp) != NULL) {
2539 /* This is just silly */
2540 mutex_enter(&ovp->v_interlock);
2541 brelvp(tbp);
2542 mutex_exit(&ovp->v_interlock);
2543 tbp->b_vp = vp;
2544 tbp->b_objlock = &vp->v_interlock;
2545 }
2546 /* Put it back the way it was */
2547 tbp->b_flags |= B_ASYNC;
2548 /* Master buffers have BC_AGE */
2549 if (tbp->b_private == tbp)
2550 tbp->b_cflags |= BC_AGE;
2551 }
2552 mutex_exit(&bufcache_lock);
2553
2554 biodone(tbp);
2555
2556 /*
2557 * If this is the last block for this vnode, but
2558 * there are other blocks on its dirty list,
2559 * set IN_MODIFIED/IN_CLEANING depending on what
2560 * sort of block. Only do this for our mount point,
2561 * not for, e.g., inode blocks that are attached to
2562 * the devvp.
2563 * XXX KS - Shouldn't we set *both* if both types
2564 * of blocks are present (traverse the dirty list?)
2565 */
2566 mutex_enter(&lfs_lock);
2567 mutex_enter(&vp->v_interlock);
2568 if (vp != devvp && vp->v_numoutput == 0 &&
2569 (fbp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL) {
2570 ip = VTOI(vp);
2571 DLOG((DLOG_SEG, "lfs_cluster_aiodone: mark ino %d\n",
2572 ip->i_number));
2573 if (LFS_IS_MALLOC_BUF(fbp))
2574 LFS_SET_UINO(ip, IN_CLEANING);
2575 else
2576 LFS_SET_UINO(ip, IN_MODIFIED);
2577 }
2578 cv_broadcast(&vp->v_cv);
2579 mutex_exit(&vp->v_interlock);
2580 mutex_exit(&lfs_lock);
2581 }
2582
2583 /* Fix up the cluster buffer, and release it */
2584 if (cl->flags & LFS_CL_MALLOC)
2585 lfs_free(fs, bp->b_data, LFS_NB_CLUSTER);
2586 putiobuf(bp);
2587
2588 /* Note i/o done */
2589 if (cl->flags & LFS_CL_SYNC) {
2590 if (--cl->seg->seg_iocount == 0)
2591 wakeup(&cl->seg->seg_iocount);
2592 }
2593 mutex_enter(&lfs_lock);
2594 #ifdef DIAGNOSTIC
2595 if (fs->lfs_iocount == 0)
2596 panic("lfs_cluster_aiodone: zero iocount");
2597 #endif
2598 if (--fs->lfs_iocount <= 1)
2599 wakeup(&fs->lfs_iocount);
2600 mutex_exit(&lfs_lock);
2601
2602 KERNEL_UNLOCK_LAST(curlwp);
2603
2604 pool_put(&fs->lfs_bpppool, cl->bpp);
2605 cl->bpp = NULL;
2606 pool_put(&fs->lfs_clpool, cl);
2607 }
2608
2609 static void
2610 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
2611 {
2612 /* reset b_iodone for when this is a single-buf i/o. */
2613 bp->b_iodone = aiodone;
2614
2615 workqueue_enqueue(uvm.aiodone_queue, &bp->b_work, NULL);
2616 }
2617
2618 static void
2619 lfs_cluster_callback(struct buf *bp)
2620 {
2621
2622 lfs_generic_callback(bp, lfs_cluster_aiodone);
2623 }
2624
2625 void
2626 lfs_supercallback(struct buf *bp)
2627 {
2628
2629 lfs_generic_callback(bp, lfs_super_aiodone);
2630 }
2631
2632 /*
2633 * The only buffers that are going to hit these functions are the
2634 * segment write blocks, or the segment summaries, or the superblocks.
2635 *
2636 * All of the above are created by lfs_newbuf, and so do not need to be
2637 * released via brelse.
2638 */
2639 void
2640 lfs_callback(struct buf *bp)
2641 {
2642
2643 lfs_generic_callback(bp, lfs_free_aiodone);
2644 }
2645
2646 /*
2647 * Shellsort (diminishing increment sort) from Data Structures and
2648 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
2649 * see also Knuth Vol. 3, page 84. The increments are selected from
2650 * formula (8), page 95. Roughly O(N^3/2).
2651 */
2652 /*
2653 * This is our own private copy of shellsort because we want to sort
2654 * two parallel arrays (the array of buffer pointers and the array of
2655 * logical block numbers) simultaneously. Note that we cast the array
2656 * of logical block numbers to a unsigned in this routine so that the
2657 * negative block numbers (meta data blocks) sort AFTER the data blocks.
2658 */
2659
2660 void
2661 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb, int size)
2662 {
2663 static int __rsshell_increments[] = { 4, 1, 0 };
2664 int incr, *incrp, t1, t2;
2665 struct buf *bp_temp;
2666
2667 #ifdef DEBUG
2668 incr = 0;
2669 for (t1 = 0; t1 < nmemb; t1++) {
2670 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2671 if (lb_array[incr++] != bp_array[t1]->b_lblkno + t2) {
2672 /* dump before panic */
2673 printf("lfs_shellsort: nmemb=%d, size=%d\n",
2674 nmemb, size);
2675 incr = 0;
2676 for (t1 = 0; t1 < nmemb; t1++) {
2677 const struct buf *bp = bp_array[t1];
2678
2679 printf("bp[%d]: lbn=%" PRIu64 ", size=%"
2680 PRIu64 "\n", t1,
2681 (uint64_t)bp->b_bcount,
2682 (uint64_t)bp->b_lblkno);
2683 printf("lbns:");
2684 for (t2 = 0; t2 * size < bp->b_bcount;
2685 t2++) {
2686 printf(" %" PRId32,
2687 lb_array[incr++]);
2688 }
2689 printf("\n");
2690 }
2691 panic("lfs_shellsort: inconsistent input");
2692 }
2693 }
2694 }
2695 #endif
2696
2697 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
2698 for (t1 = incr; t1 < nmemb; ++t1)
2699 for (t2 = t1 - incr; t2 >= 0;)
2700 if ((u_int32_t)bp_array[t2]->b_lblkno >
2701 (u_int32_t)bp_array[t2 + incr]->b_lblkno) {
2702 bp_temp = bp_array[t2];
2703 bp_array[t2] = bp_array[t2 + incr];
2704 bp_array[t2 + incr] = bp_temp;
2705 t2 -= incr;
2706 } else
2707 break;
2708
2709 /* Reform the list of logical blocks */
2710 incr = 0;
2711 for (t1 = 0; t1 < nmemb; t1++) {
2712 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2713 lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
2714 }
2715 }
2716 }
2717
2718 /*
2719 * Call vget with LK_NOWAIT. If we are the one who holds VI_XLOCK,
2720 * however, we must press on. Just fake success in that case.
2721 */
2722 int
2723 lfs_vref(struct vnode *vp)
2724 {
2725 int error;
2726 struct lfs *fs;
2727
2728 KASSERT(mutex_owned(&vp->v_interlock));
2729
2730 fs = VTOI(vp)->i_lfs;
2731
2732 ASSERT_MAYBE_SEGLOCK(fs);
2733
2734 /*
2735 * If we return 1 here during a flush, we risk vinvalbuf() not
2736 * being able to flush all of the pages from this vnode, which
2737 * will cause it to panic. So, return 0 if a flush is in progress.
2738 */
2739 error = vget(vp, LK_NOWAIT);
2740 if (error == EBUSY && IS_FLUSHING(VTOI(vp)->i_lfs, vp)) {
2741 ++fs->lfs_flushvp_fakevref;
2742 return 0;
2743 }
2744 return error;
2745 }
2746
2747 /*
2748 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
2749 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
2750 */
2751 void
2752 lfs_vunref(struct vnode *vp)
2753 {
2754 struct lfs *fs;
2755
2756 fs = VTOI(vp)->i_lfs;
2757 ASSERT_MAYBE_SEGLOCK(fs);
2758
2759 /*
2760 * Analogous to lfs_vref, if the node is flushing, fake it.
2761 */
2762 if (IS_FLUSHING(fs, vp) && fs->lfs_flushvp_fakevref) {
2763 --fs->lfs_flushvp_fakevref;
2764 return;
2765 }
2766
2767 /* does not call inactive */
2768 mutex_enter(&vp->v_interlock);
2769 vrelel(vp, VRELEL_NOINACTIVE);
2770 }
2771
2772 /*
2773 * We use this when we have vnodes that were loaded in solely for cleaning.
2774 * There is no reason to believe that these vnodes will be referenced again
2775 * soon, since the cleaning process is unrelated to normal filesystem
2776 * activity. Putting cleaned vnodes at the tail of the list has the effect
2777 * of flushing the vnode LRU. So, put vnodes that were loaded only for
2778 * cleaning at the head of the list, instead.
2779 */
2780 void
2781 lfs_vunref_head(struct vnode *vp)
2782 {
2783
2784 ASSERT_SEGLOCK(VTOI(vp)->i_lfs);
2785
2786 /* does not call inactive, inserts non-held vnode at head of freelist */
2787 mutex_enter(&vp->v_interlock);
2788 vrelel(vp, VRELEL_NOINACTIVE | VRELEL_ONHEAD);
2789 }
2790
2791
2792 /*
2793 * Set up an FINFO entry for a new file. The fip pointer is assumed to
2794 * point at uninitialized space.
2795 */
2796 void
2797 lfs_acquire_finfo(struct lfs *fs, ino_t ino, int vers)
2798 {
2799 struct segment *sp = fs->lfs_sp;
2800
2801 KASSERT(vers > 0);
2802
2803 if (sp->seg_bytes_left < fs->lfs_bsize ||
2804 sp->sum_bytes_left < sizeof(struct finfo))
2805 (void) lfs_writeseg(fs, fs->lfs_sp);
2806
2807 sp->sum_bytes_left -= FINFOSIZE;
2808 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
2809 sp->fip->fi_nblocks = 0;
2810 sp->fip->fi_ino = ino;
2811 sp->fip->fi_version = vers;
2812 }
2813
2814 /*
2815 * Release the FINFO entry, either clearing out an unused entry or
2816 * advancing us to the next available entry.
2817 */
2818 void
2819 lfs_release_finfo(struct lfs *fs)
2820 {
2821 struct segment *sp = fs->lfs_sp;
2822
2823 if (sp->fip->fi_nblocks != 0) {
2824 sp->fip = (FINFO*)((char *)sp->fip + FINFOSIZE +
2825 sizeof(int32_t) * sp->fip->fi_nblocks);
2826 sp->start_lbp = &sp->fip->fi_blocks[0];
2827 } else {
2828 sp->sum_bytes_left += FINFOSIZE;
2829 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
2830 }
2831 }
2832