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