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