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