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