lfs_segment.c revision 1.23.2.10 1 /* $NetBSD: lfs_segment.c,v 1.23.2.10 2000/01/20 21:10:03 he Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
71 */
72
73 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str))
74
75 #include "opt_ddb.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/namei.h>
79 #include <sys/kernel.h>
80 #include <sys/resourcevar.h>
81 #include <sys/file.h>
82 #include <sys/stat.h>
83 #include <sys/buf.h>
84 #include <sys/proc.h>
85 #include <sys/conf.h>
86 #include <sys/vnode.h>
87 #include <sys/malloc.h>
88 #include <sys/mount.h>
89
90 #include <miscfs/specfs/specdev.h>
91 #include <miscfs/fifofs/fifo.h>
92
93 #include <ufs/ufs/quota.h>
94 #include <ufs/ufs/inode.h>
95 #include <ufs/ufs/dir.h>
96 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ufs/ufs_extern.h>
98
99 #include <ufs/lfs/lfs.h>
100 #include <ufs/lfs/lfs_extern.h>
101
102 extern int count_lock_queue __P((void));
103 extern struct simplelock vnode_free_list_slock; /* XXX */
104 extern TAILQ_HEAD(freelst, vnode) vnode_free_list; /* XXX */
105
106 /*
107 * Determine if it's OK to start a partial in this segment, or if we need
108 * to go on to a new segment.
109 */
110 #define LFS_PARTIAL_FITS(fs) \
111 ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
112 1 << (fs)->lfs_fsbtodb)
113
114 void lfs_callback __P((struct buf *));
115 int lfs_gather __P((struct lfs *, struct segment *,
116 struct vnode *, int (*) __P((struct lfs *, struct buf *))));
117 int lfs_gatherblock __P((struct segment *, struct buf *, int *));
118 void lfs_iset __P((struct inode *, ufs_daddr_t, time_t));
119 int lfs_match_fake __P((struct lfs *, struct buf *));
120 int lfs_match_data __P((struct lfs *, struct buf *));
121 int lfs_match_dindir __P((struct lfs *, struct buf *));
122 int lfs_match_indir __P((struct lfs *, struct buf *));
123 int lfs_match_tindir __P((struct lfs *, struct buf *));
124 void lfs_newseg __P((struct lfs *));
125 void lfs_shellsort __P((struct buf **, ufs_daddr_t *, register int));
126 void lfs_supercallback __P((struct buf *));
127 void lfs_updatemeta __P((struct segment *));
128 int lfs_vref __P((struct vnode *));
129 void lfs_vunref __P((struct vnode *));
130 void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
131 int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
132 int lfs_writeseg __P((struct lfs *, struct segment *));
133 void lfs_writesuper __P((struct lfs *, daddr_t));
134 int lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
135 struct segment *sp, int dirops));
136
137 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
138 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */
139 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */
140 int lfs_dirvcount = 0; /* # active dirops */
141
142 /* Statistics Counters */
143 int lfs_dostats = 1;
144 struct lfs_stats lfs_stats;
145
146 /* op values to lfs_writevnodes */
147 #define VN_REG 0
148 #define VN_DIROP 1
149 #define VN_EMPTY 2
150 #define VN_CLEAN 3
151
152 #define LFS_MAX_ACTIVE 10
153
154 /*
155 * XXX KS - Set modification time on the Ifile, so the cleaner can
156 * read the fs mod time off of it. We don't set IN_UPDATE here,
157 * since we don't really need this to be flushed to disk (and in any
158 * case that wouldn't happen to the Ifile until we checkpoint).
159 */
160 void
161 lfs_imtime(fs)
162 struct lfs *fs;
163 {
164 struct timespec ts;
165 struct inode *ip;
166
167 TIMEVAL_TO_TIMESPEC(&time, &ts);
168 ip = VTOI(fs->lfs_ivnode);
169 ip->i_ffs_mtime = ts.tv_sec;
170 ip->i_ffs_mtimensec = ts.tv_nsec;
171 }
172
173 /*
174 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
175 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
176 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
177 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
178 */
179
180 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
181 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp))
182 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
183
184 int
185 lfs_vflush(vp)
186 struct vnode *vp;
187 {
188 struct inode *ip;
189 struct lfs *fs;
190 struct segment *sp;
191 struct buf *bp, *nbp, *tbp, *tnbp;
192 int error, s;
193
194 ip = VTOI(vp);
195 fs = VFSTOUFS(vp->v_mount)->um_lfs;
196
197 if(ip->i_flag & IN_CLEANING) {
198 #ifdef DEBUG_LFS
199 ivndebug(vp,"vflush/in_cleaning");
200 #endif
201 ip->i_flag &= ~IN_CLEANING;
202 if(ip->i_flag & IN_MODIFIED) {
203 fs->lfs_uinodes--;
204 } else
205 ip->i_flag |= IN_MODIFIED;
206 /*
207 * Toss any cleaning buffers that have real counterparts
208 * to avoid losing new data
209 */
210 s = splbio();
211 for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
212 nbp = bp->b_vnbufs.le_next;
213 if(bp->b_flags & B_CALL) {
214 for(tbp=vp->v_dirtyblkhd.lh_first; tbp;
215 tbp=tnbp)
216 {
217 tnbp = tbp->b_vnbufs.le_next;
218 if(tbp->b_vp == bp->b_vp
219 && tbp->b_lblkno == bp->b_lblkno
220 && tbp != bp)
221 {
222 lfs_freebuf(bp);
223 }
224 }
225 }
226 }
227 splx(s);
228 }
229
230 /* If the node is being written, wait until that is done */
231 if(WRITEINPROG(vp)) {
232 #ifdef DEBUG_LFS
233 ivndebug(vp,"vflush/writeinprog");
234 #endif
235 tsleep(vp, PRIBIO+1, "lfs_vw", 0);
236 }
237
238 /* Protect against VXLOCK deadlock in vinvalbuf() */
239 lfs_seglock(fs, SEGM_SYNC);
240
241 /* If we're supposed to flush a freed inode, just toss it */
242 /* XXX - seglock, so these buffers can't be gathered, right? */
243 if(ip->i_ffs_mode == 0) {
244 printf("lfs_vflush: ino %d is freed, not flushing\n",
245 ip->i_number);
246 s = splbio();
247 for(bp=vp->v_dirtyblkhd.lh_first; bp; bp=nbp) {
248 nbp = bp->b_vnbufs.le_next;
249 /* Copied from lfs_writeseg */
250 if (bp->b_flags & B_CALL) {
251 /* if B_CALL, it was created with newbuf */
252 lfs_freebuf(bp);
253 } else {
254 bremfree(bp);
255 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
256 B_LOCKED | B_GATHERED);
257 bp->b_flags |= B_DONE;
258 reassignbuf(bp, vp);
259 brelse(bp);
260 }
261 }
262 splx(s);
263 if(ip->i_flag & IN_CLEANING)
264 fs->lfs_uinodes--;
265 if(ip->i_flag & IN_MODIFIED)
266 fs->lfs_uinodes--;
267 ip->i_flag &= ~(IN_MODIFIED|IN_UPDATE|IN_ACCESS|IN_CHANGE|IN_CLEANING);
268 printf("lfs_vflush: done not flushing ino %d\n",
269 ip->i_number);
270 lfs_segunlock(fs);
271 return 0;
272 }
273
274 SET_FLUSHING(fs,vp);
275 if (fs->lfs_nactive > LFS_MAX_ACTIVE) {
276 error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP);
277 CLR_FLUSHING(fs,vp);
278 lfs_segunlock(fs);
279 return error;
280 }
281 sp = fs->lfs_sp;
282
283 if (vp->v_dirtyblkhd.lh_first == NULL) {
284 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
285 } else if((ip->i_flag & IN_CLEANING) && (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
286 #ifdef DEBUG_LFS
287 ivndebug(vp,"vflush/clean");
288 #endif
289 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
290 }
291 else if(lfs_dostats) {
292 if(vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & (IN_MODIFIED|IN_UPDATE|IN_ACCESS|IN_CHANGE|IN_CLEANING)))
293 ++lfs_stats.vflush_invoked;
294 #ifdef DEBUG_LFS
295 ivndebug(vp,"vflush");
296 #endif
297 }
298
299 #ifdef DIAGNOSTIC
300 /* XXX KS This actually can happen right now, though it shouldn't(?) */
301 if(vp->v_flag & VDIROP) {
302 printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
303 /* panic("VDIROP being flushed...this can\'t happen"); */
304 }
305 if(vp->v_usecount<0) {
306 printf("usecount=%ld\n",vp->v_usecount);
307 panic("lfs_vflush: usecount<0");
308 }
309 #endif
310
311 do {
312 do {
313 if (vp->v_dirtyblkhd.lh_first != NULL)
314 lfs_writefile(fs, sp, vp);
315 } while (lfs_writeinode(fs, sp, ip));
316 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
317
318 if(lfs_dostats) {
319 ++lfs_stats.nwrites;
320 if (sp->seg_flags & SEGM_SYNC)
321 ++lfs_stats.nsync_writes;
322 if (sp->seg_flags & SEGM_CKP)
323 ++lfs_stats.ncheckpoints;
324 }
325 lfs_segunlock(fs);
326
327 CLR_FLUSHING(fs,vp);
328 return (0);
329 }
330
331 #ifdef DEBUG_LFS_VERBOSE
332 # define vndebug(vp,str) if(VTOI(vp)->i_flag & IN_CLEANING) printf("not writing ino %d because %s (op %d)\n",VTOI(vp)->i_number,(str),op)
333 #else
334 # define vndebug(vp,str)
335 #endif
336
337 int
338 lfs_writevnodes(fs, mp, sp, op)
339 struct lfs *fs;
340 struct mount *mp;
341 struct segment *sp;
342 int op;
343 {
344 struct inode *ip;
345 struct vnode *vp;
346 int inodes_written=0, only_cleaning;
347
348 #ifndef LFS_NO_BACKVP_HACK
349 /* BEGIN HACK */
350 #define VN_OFFSET (((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
351 #define BACK_VP(VP) ((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
352 #define BEG_OF_VLIST ((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
353
354 /* Find last vnode. */
355 loop: for (vp = mp->mnt_vnodelist.lh_first;
356 vp && vp->v_mntvnodes.le_next != NULL;
357 vp = vp->v_mntvnodes.le_next);
358 for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
359 #else
360 loop:
361 for (vp = mp->mnt_vnodelist.lh_first;
362 vp != NULL;
363 vp = vp->v_mntvnodes.le_next) {
364 #endif
365 /*
366 * If the vnode that we are about to sync is no longer
367 * associated with this mount point, start over.
368 */
369 if (vp->v_mount != mp)
370 goto loop;
371
372 ip = VTOI(vp);
373 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
374 (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
375 vndebug(vp,"dirop");
376 continue;
377 }
378
379 if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) {
380 vndebug(vp,"empty");
381 continue;
382 }
383
384 if (vp->v_type == VNON) {
385 continue;
386 }
387
388 if(op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
389 && vp != fs->lfs_flushvp
390 && !(ip->i_flag & IN_CLEANING)) {
391 vndebug(vp,"cleaning");
392 continue;
393 }
394
395 if (lfs_vref(vp)) {
396 vndebug(vp,"vref");
397 continue;
398 }
399
400 #if 0 /* XXX KS - if we skip the ifile, things could go badly for us. */
401 if(WRITEINPROG(vp)) {
402 lfs_vunref(vp);
403 #ifdef DEBUG_LFS
404 ivndebug(vp,"writevnodes/writeinprog");
405 #endif
406 continue;
407 }
408 #endif
409 only_cleaning = 0;
410 /*
411 * Write the inode/file if dirty and it's not the
412 * the IFILE.
413 */
414 if ((ip->i_flag &
415 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE | IN_CLEANING) ||
416 vp->v_dirtyblkhd.lh_first != NULL))
417 {
418 only_cleaning = ((ip->i_flag & (IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE|IN_CLEANING))==IN_CLEANING);
419
420 if(ip->i_number != LFS_IFILE_INUM
421 && vp->v_dirtyblkhd.lh_first != NULL)
422 {
423 lfs_writefile(fs, sp, vp);
424 }
425 if(vp->v_dirtyblkhd.lh_first != NULL) {
426 if(WRITEINPROG(vp)) {
427 #ifdef DEBUG_LFS
428 ivndebug(vp,"writevnodes/write2");
429 #endif
430 } else if(!(ip->i_flag & (IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE|IN_CLEANING))) {
431 #ifdef DEBUG_LFS
432 printf("<%d>",ip->i_number);
433 #endif
434 ip->i_flag |= IN_MODIFIED;
435 ++fs->lfs_uinodes;
436 }
437 }
438 (void) lfs_writeinode(fs, sp, ip);
439 inodes_written++;
440 }
441
442 if(lfs_clean_vnhead && only_cleaning)
443 lfs_vunref_head(vp);
444 else
445 lfs_vunref(vp);
446 }
447 return inodes_written;
448 }
449
450 int
451 lfs_segwrite(mp, flags)
452 struct mount *mp;
453 int flags; /* Do a checkpoint. */
454 {
455 struct buf *bp;
456 struct inode *ip;
457 struct lfs *fs;
458 struct segment *sp;
459 struct vnode *vp;
460 SEGUSE *segusep;
461 ufs_daddr_t ibno;
462 int do_ckp, error, i;
463 int writer_set = 0;
464 int need_unlock = 0;
465
466 fs = VFSTOUFS(mp)->um_lfs;
467
468 lfs_imtime(fs);
469
470 /*
471 * If we are not the cleaner, and we have fewer than MIN_FREE_SEGS
472 * clean segments, wait until cleaner writes.
473 */
474 if(!(flags & SEGM_CLEAN)
475 && (!fs->lfs_seglock || !(fs->lfs_sp->seg_flags & SEGM_CLEAN)))
476 {
477 do {
478 if (fs->lfs_nclean <= MIN_FREE_SEGS
479 || fs->lfs_avail <= 0)
480 {
481 wakeup(&lfs_allclean_wakeup);
482 wakeup(&fs->lfs_nextseg);
483 error = tsleep(&fs->lfs_avail, PRIBIO + 1,
484 "lfs_avail", 0);
485 if (error) {
486 return (error);
487 }
488 }
489 } while (fs->lfs_nclean <= MIN_FREE_SEGS || fs->lfs_avail <= 0);
490 }
491
492 /*
493 * Allocate a segment structure and enough space to hold pointers to
494 * the maximum possible number of buffers which can be described in a
495 * single summary block.
496 */
497 do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
498 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
499 sp = fs->lfs_sp;
500
501 /*
502 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
503 * in which case we have to flush *all* buffers off of this vnode.
504 * We don't care about other nodes, but write any non-dirop nodes
505 * anyway in anticipation of another getnewvnode().
506 *
507 * If we're cleaning we only write cleaning and ifile blocks, and
508 * no dirops, since otherwise we'd risk corruption in a crash.
509 */
510 if(sp->seg_flags & SEGM_CLEAN)
511 lfs_writevnodes(fs, mp, sp, VN_CLEAN);
512 else {
513 lfs_writevnodes(fs, mp, sp, VN_REG);
514 if(!fs->lfs_dirops || !fs->lfs_flushvp) {
515 while(fs->lfs_dirops)
516 if((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
517 "lfs writer", 0)))
518 {
519 free(sp->bpp, M_SEGMENT);
520 free(sp, M_SEGMENT);
521 return (error);
522 }
523 fs->lfs_writer++;
524 writer_set=1;
525 lfs_writevnodes(fs, mp, sp, VN_DIROP);
526 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
527 }
528 }
529
530 /*
531 * If we are doing a checkpoint, mark everything since the
532 * last checkpoint as no longer ACTIVE.
533 */
534 if (do_ckp) {
535 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
536 --ibno >= fs->lfs_cleansz; ) {
537 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
538
539 panic("lfs_segwrite: ifile read");
540 segusep = (SEGUSE *)bp->b_data;
541 for (i = fs->lfs_sepb; i--; segusep++)
542 segusep->su_flags &= ~SEGUSE_ACTIVE;
543
544 /* But the current segment is still ACTIVE */
545 if (fs->lfs_curseg/fs->lfs_sepb==(ibno-fs->lfs_cleansz))
546 ((SEGUSE *)(bp->b_data))[fs->lfs_curseg%fs->lfs_sepb].su_flags |= SEGUSE_ACTIVE;
547 error = VOP_BWRITE(bp);
548 }
549 }
550
551 if (do_ckp || fs->lfs_doifile) {
552 redo:
553 vp = fs->lfs_ivnode;
554 /*
555 * Depending on the circumstances of our calling, the ifile
556 * inode might be locked. If it is, and if it is locked by
557 * us, we should VREF instead of vget here.
558 */
559 need_unlock = 0;
560 if(VOP_ISLOCKED(vp)
561 && VTOI(vp)->i_lock.lk_lockholder == curproc->p_pid) {
562 VREF(vp);
563 } else {
564 while (vget(vp, LK_EXCLUSIVE))
565 continue;
566 need_unlock = 1;
567 }
568 ip = VTOI(vp);
569 if (vp->v_dirtyblkhd.lh_first != NULL)
570 lfs_writefile(fs, sp, vp);
571 (void)lfs_writeinode(fs, sp, ip);
572
573 /* Only vput if we used vget() above. */
574 if(need_unlock)
575 vput(vp);
576 else
577 vrele(vp);
578
579 if (lfs_writeseg(fs, sp) && do_ckp)
580 goto redo;
581 } else {
582 (void) lfs_writeseg(fs, sp);
583 }
584
585 /*
586 * If the I/O count is non-zero, sleep until it reaches zero.
587 * At the moment, the user's process hangs around so we can
588 * sleep.
589 */
590 fs->lfs_doifile = 0;
591 if(writer_set && --fs->lfs_writer==0)
592 wakeup(&fs->lfs_dirops);
593
594 if(lfs_dostats) {
595 ++lfs_stats.nwrites;
596 if (sp->seg_flags & SEGM_SYNC)
597 ++lfs_stats.nsync_writes;
598 if (sp->seg_flags & SEGM_CKP)
599 ++lfs_stats.ncheckpoints;
600 }
601 lfs_segunlock(fs);
602 return (0);
603 }
604
605 /*
606 * Write the dirty blocks associated with a vnode.
607 */
608 void
609 lfs_writefile(fs, sp, vp)
610 struct lfs *fs;
611 struct segment *sp;
612 struct vnode *vp;
613 {
614 struct buf *bp;
615 struct finfo *fip;
616 IFILE *ifp;
617
618
619 if (sp->seg_bytes_left < fs->lfs_bsize ||
620 sp->sum_bytes_left < sizeof(struct finfo))
621 (void) lfs_writeseg(fs, sp);
622
623 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
624 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
625
626 if(vp->v_flag & VDIROP)
627 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
628
629 fip = sp->fip;
630 fip->fi_nblocks = 0;
631 fip->fi_ino = VTOI(vp)->i_number;
632 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
633 fip->fi_version = ifp->if_version;
634 brelse(bp);
635
636 if(sp->seg_flags & SEGM_CLEAN)
637 {
638 lfs_gather(fs, sp, vp, lfs_match_fake);
639 /*
640 * For a file being flushed, we need to write *all* blocks.
641 * This means writing the cleaning blocks first, and then
642 * immediately following with any non-cleaning blocks.
643 * The same is true of the Ifile since checkpoints assume
644 * that all valid Ifile blocks are written.
645 */
646 if(IS_FLUSHING(fs,vp) || VTOI(vp)->i_number == LFS_IFILE_INUM)
647 lfs_gather(fs, sp, vp, lfs_match_data);
648 } else
649 lfs_gather(fs, sp, vp, lfs_match_data);
650
651 /*
652 * It may not be necessary to write the meta-data blocks at this point,
653 * as the roll-forward recovery code should be able to reconstruct the
654 * list.
655 *
656 * We have to write them anyway, though, under two conditions: (1) the
657 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
658 * checkpointing.
659 */
660 if(lfs_writeindir
661 || IS_FLUSHING(fs,vp)
662 || (sp->seg_flags & SEGM_CKP))
663 {
664 lfs_gather(fs, sp, vp, lfs_match_indir);
665 lfs_gather(fs, sp, vp, lfs_match_dindir);
666 lfs_gather(fs, sp, vp, lfs_match_tindir);
667 }
668 fip = sp->fip;
669 if (fip->fi_nblocks != 0) {
670 sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
671 sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
672 sp->start_lbp = &sp->fip->fi_blocks[0];
673 } else {
674 sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
675 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
676 }
677 }
678
679 int
680 lfs_writeinode(fs, sp, ip)
681 struct lfs *fs;
682 struct segment *sp;
683 struct inode *ip;
684 {
685 struct buf *bp, *ibp;
686 IFILE *ifp;
687 SEGUSE *sup;
688 ufs_daddr_t daddr;
689 ino_t ino;
690 int error, i, ndx;
691 int redo_ifile = 0;
692 struct timespec ts;
693 int gotblk=0;
694
695 if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE | IN_CLEANING)))
696 return(0);
697
698 /* Allocate a new inode block if necessary. */
699 if ((ip->i_number != LFS_IFILE_INUM || sp->idp==NULL) && sp->ibp == NULL) {
700 /* Allocate a new segment if necessary. */
701 if (sp->seg_bytes_left < fs->lfs_bsize ||
702 sp->sum_bytes_left < sizeof(ufs_daddr_t))
703 (void) lfs_writeseg(fs, sp);
704
705 /* Get next inode block. */
706 daddr = fs->lfs_offset;
707 fs->lfs_offset += fsbtodb(fs, 1);
708 sp->ibp = *sp->cbpp++ =
709 getblk(VTOI(fs->lfs_ivnode)->i_devvp, daddr, fs->lfs_bsize, 0, 0);
710 gotblk++;
711
712 /* Zero out inode numbers */
713 for (i = 0; i < INOPB(fs); ++i)
714 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
715
716 ++sp->start_bpp;
717 fs->lfs_avail -= fsbtodb(fs, 1);
718 /* Set remaining space counters. */
719 sp->seg_bytes_left -= fs->lfs_bsize;
720 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
721 ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
722 sp->ninodes / INOPB(fs) - 1;
723 ((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
724 }
725
726 /* Update the inode times and copy the inode onto the inode page. */
727 if (ip->i_flag & (IN_CLEANING|IN_MODIFIED))
728 --fs->lfs_uinodes;
729 TIMEVAL_TO_TIMESPEC(&time, &ts);
730 LFS_ITIMES(ip, &ts, &ts, &ts);
731
732 if(ip->i_flag & IN_CLEANING)
733 ip->i_flag &= ~IN_CLEANING;
734 else
735 ip->i_flag &= ~(IN_ACCESS|IN_CHANGE|IN_MODIFIED|IN_UPDATE);
736
737 /*
738 * If this is the Ifile, and we've already written the Ifile in this
739 * partial segment, just overwrite it (it's not on disk yet) and
740 * continue.
741 *
742 * XXX we know that the bp that we get the second time around has
743 * already been gathered.
744 */
745 if(ip->i_number == LFS_IFILE_INUM && sp->idp) {
746 *(sp->idp) = ip->i_din.ffs_din;
747 return 0;
748 }
749
750 bp = sp->ibp;
751 ((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] =
752 ip->i_din.ffs_din;
753
754 if(ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
755 sp->idp = ((struct dinode *)bp->b_data)+(sp->ninodes % INOPB(fs));
756 if(gotblk) {
757 bp->b_flags |= B_LOCKED;
758 brelse(bp);
759 }
760
761 /* Increment inode count in segment summary block. */
762 ++((SEGSUM *)(sp->segsum))->ss_ninos;
763
764 /* If this page is full, set flag to allocate a new page. */
765 if (++sp->ninodes % INOPB(fs) == 0)
766 sp->ibp = NULL;
767
768 /*
769 * If updating the ifile, update the super-block. Update the disk
770 * address and access times for this inode in the ifile.
771 */
772 ino = ip->i_number;
773 if (ino == LFS_IFILE_INUM) {
774 daddr = fs->lfs_idaddr;
775 fs->lfs_idaddr = bp->b_blkno;
776 } else {
777 LFS_IENTRY(ifp, fs, ino, ibp);
778 daddr = ifp->if_daddr;
779 ifp->if_daddr = bp->b_blkno;
780 #ifdef LFS_DEBUG_NEXTFREE
781 if(ino > 3 && ifp->if_nextfree) {
782 vprint("lfs_writeinode",ITOV(ip));
783 printf("lfs_writeinode: updating free ino %d\n",
784 ip->i_number);
785 }
786 #endif
787 error = VOP_BWRITE(ibp);
788 }
789
790 /*
791 * No need to update segment usage if there was no former inode address
792 * or if the last inode address is in the current partial segment.
793 */
794 if (daddr != LFS_UNUSED_DADDR &&
795 !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
796 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
797 #ifdef DIAGNOSTIC
798 if (sup->su_nbytes < DINODE_SIZE) {
799 /* XXX -- Change to a panic. */
800 printf("lfs_writeinode: negative bytes (segment %d short by %d)\n",
801 datosn(fs, daddr), (int)DINODE_SIZE - sup->su_nbytes);
802 panic("lfs_writeinode: negative bytes");
803 sup->su_nbytes = DINODE_SIZE;
804 }
805 #endif
806 sup->su_nbytes -= DINODE_SIZE;
807 redo_ifile =
808 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
809 error = VOP_BWRITE(bp);
810 }
811 return (redo_ifile);
812 }
813
814 int
815 lfs_gatherblock(sp, bp, sptr)
816 struct segment *sp;
817 struct buf *bp;
818 int *sptr;
819 {
820 struct lfs *fs;
821 int version;
822
823 /*
824 * If full, finish this segment. We may be doing I/O, so
825 * release and reacquire the splbio().
826 */
827 #ifdef DIAGNOSTIC
828 if (sp->vp == NULL)
829 panic ("lfs_gatherblock: Null vp in segment");
830 #endif
831 fs = sp->fs;
832 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
833 sp->seg_bytes_left < bp->b_bcount) {
834 if (sptr)
835 splx(*sptr);
836 lfs_updatemeta(sp);
837
838 version = sp->fip->fi_version;
839 (void) lfs_writeseg(fs, sp);
840
841 sp->fip->fi_version = version;
842 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
843 /* Add the current file to the segment summary. */
844 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
845 sp->sum_bytes_left -=
846 sizeof(struct finfo) - sizeof(ufs_daddr_t);
847
848 if (sptr)
849 *sptr = splbio();
850 return(1);
851 }
852
853 #ifdef DEBUG
854 if(bp->b_flags & B_GATHERED) {
855 printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
856 sp->fip->fi_ino, bp->b_lblkno);
857 return(0);
858 }
859 #endif
860 /* Insert into the buffer list, update the FINFO block. */
861 bp->b_flags |= B_GATHERED;
862 *sp->cbpp++ = bp;
863 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
864
865 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
866 sp->seg_bytes_left -= bp->b_bcount;
867 return(0);
868 }
869
870 int
871 lfs_gather(fs, sp, vp, match)
872 struct lfs *fs;
873 struct segment *sp;
874 struct vnode *vp;
875 int (*match) __P((struct lfs *, struct buf *));
876 {
877 struct buf *bp;
878 int s, count=0;
879
880 sp->vp = vp;
881 s = splbio();
882
883 #ifndef LFS_NO_BACKBUF_HACK
884 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
885 #else /* LFS_NO_BACKBUF_HACK */
886 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
887 # define BUF_OFFSET (((void *)&bp->b_vnbufs.le_next) - (void *)bp)
888 # define BACK_BUF(BP) ((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
889 # define BEG_OF_LIST ((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
890 /* Find last buffer. */
891 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
892 bp = bp->b_vnbufs.le_next);
893 for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
894 #endif /* LFS_NO_BACKBUF_HACK */
895 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
896 continue;
897 if(vp->v_type == VBLK) {
898 /* For block devices, just write the blocks. */
899 /* XXX Do we really need to even do this? */
900 #ifdef DEBUG_LFS
901 if(count==0)
902 printf("BLK(");
903 printf(".");
904 #endif
905 /* Get the block before bwrite, so we don't corrupt the free list */
906 bp->b_flags |= B_BUSY;
907 bremfree(bp);
908 bwrite(bp);
909 } else {
910 #ifdef DIAGNOSTIC
911 if (!(bp->b_flags & B_DELWRI))
912 panic("lfs_gather: bp not B_DELWRI");
913 if (!(bp->b_flags & B_LOCKED)) {
914 printf("lfs_gather: lbn %d blk %d not B_LOCKED\n", bp->b_lblkno, bp->b_blkno);
915 VOP_PRINT(bp->b_vp);
916 panic("lfs_gather: bp not B_LOCKED");
917 }
918 #endif
919 if (lfs_gatherblock(sp, bp, &s)) {
920 goto loop;
921 }
922 }
923 count++;
924 }
925 splx(s);
926 #ifdef DEBUG_LFS
927 if(vp->v_type == VBLK && count)
928 printf(")\n");
929 #endif
930 lfs_updatemeta(sp);
931 sp->vp = NULL;
932 return count;
933 }
934
935 /*
936 * Update the metadata that points to the blocks listed in the FINFO
937 * array.
938 */
939 void
940 lfs_updatemeta(sp)
941 struct segment *sp;
942 {
943 SEGUSE *sup;
944 struct buf *bp;
945 struct lfs *fs;
946 struct vnode *vp;
947 struct indir a[NIADDR + 2], *ap;
948 struct inode *ip;
949 ufs_daddr_t daddr, lbn, off;
950 int error, i, nblocks, num;
951
952 vp = sp->vp;
953 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
954 if (nblocks < 0)
955 panic("This is a bad thing\n");
956 if (vp == NULL || nblocks == 0)
957 return;
958
959 /* Sort the blocks. */
960 /*
961 * XXX KS - We have to sort even if the blocks come from the
962 * cleaner, because there might be other pending blocks on the
963 * same inode...and if we don't sort, and there are fragments
964 * present, blocks may be written in the wrong place.
965 */
966 /* if (!(sp->seg_flags & SEGM_CLEAN)) */
967 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
968
969 /*
970 * Record the length of the last block in case it's a fragment.
971 * If there are indirect blocks present, they sort last. An
972 * indirect block will be lfs_bsize and its presence indicates
973 * that you cannot have fragments.
974 */
975 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
976
977 /*
978 * Assign disk addresses, and update references to the logical
979 * block and the segment usage information.
980 */
981 fs = sp->fs;
982 for (i = nblocks; i--; ++sp->start_bpp) {
983 lbn = *sp->start_lbp++;
984
985 (*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
986 if((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) {
987 printf("lfs_updatemeta: ino %d blk %d has same lbn and daddr\n", VTOI(vp)->i_number, off);
988 }
989 fs->lfs_offset +=
990 fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
991 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
992 if (error)
993 panic("lfs_updatemeta: ufs_bmaparray %d", error);
994 ip = VTOI(vp);
995 switch (num) {
996 case 0:
997 ip->i_ffs_db[lbn] = off;
998 break;
999 case 1:
1000 ip->i_ffs_ib[a[0].in_off] = off;
1001 break;
1002 default:
1003 ap = &a[num - 1];
1004 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1005 panic("lfs_updatemeta: bread bno %d",
1006 ap->in_lbn);
1007 /*
1008 * Bread may create a new (indirect) block which needs
1009 * to get counted for the inode.
1010 */
1011 if (/* bp->b_blkno == -1 && */
1012 !(bp->b_flags & (B_DELWRI|B_DONE))) {
1013 ip->i_ffs_blocks += fsbtodb(fs, 1);
1014 fs->lfs_bfree -= fragstodb(fs, fs->lfs_frag);
1015 }
1016 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
1017 VOP_BWRITE(bp);
1018 }
1019 /* Update segment usage information. */
1020 if (daddr != UNASSIGNED && !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
1021 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
1022 #ifdef DIAGNOSTIC
1023 if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
1024 /* XXX -- Change to a panic. */
1025 printf("lfs_updatemeta: negative bytes (segment %d short by %ld)\n",
1026 datosn(fs, daddr), (*sp->start_bpp)->b_bcount - sup->su_nbytes);
1027 printf("lfs_updatemeta: ino %d, lbn %d, addr = %x\n",
1028 VTOI(sp->vp)->i_number, (*sp->start_bpp)->b_lblkno, daddr);
1029 panic("lfs_updatemeta: negative bytes");
1030 sup->su_nbytes = (*sp->start_bpp)->b_bcount;
1031 }
1032 #endif
1033 sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
1034 error = VOP_BWRITE(bp);
1035 }
1036 }
1037 }
1038
1039 /*
1040 * Start a new segment.
1041 */
1042 int
1043 lfs_initseg(fs)
1044 struct lfs *fs;
1045 {
1046 struct segment *sp;
1047 SEGUSE *sup;
1048 SEGSUM *ssp;
1049 struct buf *bp;
1050 int repeat;
1051
1052 sp = fs->lfs_sp;
1053
1054 repeat = 0;
1055 /* Advance to the next segment. */
1056 if (!LFS_PARTIAL_FITS(fs)) {
1057 /* Wake up any cleaning procs waiting on this file system. */
1058 wakeup(&lfs_allclean_wakeup);
1059 wakeup(&fs->lfs_nextseg);
1060 lfs_newseg(fs);
1061 repeat = 1;
1062 fs->lfs_offset = fs->lfs_curseg;
1063 sp->seg_number = datosn(fs, fs->lfs_curseg);
1064 sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
1065 /*
1066 * If the segment contains a superblock, update the offset
1067 * and summary address to skip over it.
1068 */
1069 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1070 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1071 fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
1072 sp->seg_bytes_left -= LFS_SBPAD;
1073 }
1074 brelse(bp);
1075 } else {
1076 sp->seg_number = datosn(fs, fs->lfs_curseg);
1077 sp->seg_bytes_left = (fs->lfs_dbpseg -
1078 (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
1079 }
1080 fs->lfs_lastpseg = fs->lfs_offset;
1081
1082 sp->fs = fs;
1083 sp->ibp = NULL;
1084 sp->idp = NULL;
1085 sp->ninodes = 0;
1086
1087 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
1088 sp->cbpp = sp->bpp;
1089 *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
1090 fs->lfs_offset, LFS_SUMMARY_SIZE);
1091 sp->segsum = (*sp->cbpp)->b_data;
1092 bzero(sp->segsum, LFS_SUMMARY_SIZE);
1093 sp->start_bpp = ++sp->cbpp;
1094 fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
1095
1096 /* Set point to SEGSUM, initialize it. */
1097 ssp = sp->segsum;
1098 ssp->ss_next = fs->lfs_nextseg;
1099 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1100 ssp->ss_magic = SS_MAGIC;
1101
1102 /* Set pointer to first FINFO, initialize it. */
1103 sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
1104 sp->fip->fi_nblocks = 0;
1105 sp->start_lbp = &sp->fip->fi_blocks[0];
1106 sp->fip->fi_lastlength = 0;
1107
1108 sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
1109 sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
1110
1111 return(repeat);
1112 }
1113
1114 /*
1115 * Return the next segment to write.
1116 */
1117 void
1118 lfs_newseg(fs)
1119 struct lfs *fs;
1120 {
1121 CLEANERINFO *cip;
1122 SEGUSE *sup;
1123 struct buf *bp;
1124 int curseg, isdirty, sn;
1125
1126 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
1127 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1128 sup->su_nbytes = 0;
1129 sup->su_nsums = 0;
1130 sup->su_ninos = 0;
1131 (void) VOP_BWRITE(bp);
1132
1133 LFS_CLEANERINFO(cip, fs, bp);
1134 --cip->clean;
1135 ++cip->dirty;
1136 fs->lfs_nclean = cip->clean;
1137 (void) VOP_BWRITE(bp);
1138
1139 fs->lfs_lastseg = fs->lfs_curseg;
1140 fs->lfs_curseg = fs->lfs_nextseg;
1141 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
1142 sn = (sn + 1) % fs->lfs_nseg;
1143 if (sn == curseg)
1144 panic("lfs_nextseg: no clean segments");
1145 LFS_SEGENTRY(sup, fs, sn, bp);
1146 isdirty = sup->su_flags & SEGUSE_DIRTY;
1147 brelse(bp);
1148 if (!isdirty)
1149 break;
1150 }
1151
1152 ++fs->lfs_nactive;
1153 fs->lfs_nextseg = sntoda(fs, sn);
1154 if(lfs_dostats) {
1155 ++lfs_stats.segsused;
1156 }
1157 }
1158
1159 int
1160 lfs_writeseg(fs, sp)
1161 struct lfs *fs;
1162 struct segment *sp;
1163 {
1164 extern int locked_queue_count;
1165 extern long locked_queue_bytes;
1166 struct buf **bpp, *bp, *cbp;
1167 SEGUSE *sup;
1168 SEGSUM *ssp;
1169 dev_t i_dev;
1170 u_long *datap, *dp;
1171 int do_again, i, nblocks, s;
1172 #ifdef LFS_TRACK_IOS
1173 int j;
1174 #endif
1175 int (*strategy)__P((void *));
1176 struct vop_strategy_args vop_strategy_a;
1177 u_short ninos;
1178 struct vnode *devvp;
1179 char *p;
1180 struct vnode *vn;
1181 struct inode *ip;
1182 #if defined(DEBUG) && defined(LFS_PROPELLER)
1183 static int propeller;
1184 char propstring[4] = "-\\|/";
1185
1186 printf("%c\b",propstring[propeller++]);
1187 if(propeller==4)
1188 propeller = 0;
1189 #endif
1190
1191 /*
1192 * If there are no buffers other than the segment summary to write
1193 * and it is not a checkpoint, don't do anything. On a checkpoint,
1194 * even if there aren't any buffers, you need to write the superblock.
1195 */
1196 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1197 return (0);
1198
1199 #ifdef DEBUG_LFS
1200 lfs_check_bpp(fs,sp,__FILE__,__LINE__);
1201 #endif
1202 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1203 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1204
1205 /* Update the segment usage information. */
1206 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1207
1208 /* Loop through all blocks, except the segment summary. */
1209 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1210 if((*bpp)->b_vp != devvp)
1211 sup->su_nbytes += (*bpp)->b_bcount;
1212 }
1213
1214 ssp = (SEGSUM *)sp->segsum;
1215
1216 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1217 sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
1218 /* sup->su_nbytes += LFS_SUMMARY_SIZE; */
1219 sup->su_lastmod = time.tv_sec;
1220 sup->su_ninos += ninos;
1221 ++sup->su_nsums;
1222
1223 do_again = !(bp->b_flags & B_GATHERED);
1224 (void)VOP_BWRITE(bp);
1225 /*
1226 * Compute checksum across data and then across summary; the first
1227 * block (the summary block) is skipped. Set the create time here
1228 * so that it's guaranteed to be later than the inode mod times.
1229 *
1230 * XXX
1231 * Fix this to do it inline, instead of malloc/copy.
1232 */
1233 datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
1234 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1235 if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1236 if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
1237 panic("lfs_writeseg: copyin failed [1]: ino %d blk %d", VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno);
1238 } else {
1239 if( !((*bpp)->b_flags & B_CALL) ) {
1240 /*
1241 * Before we record data for a checksm,
1242 * make sure the data won't change in between
1243 * the checksum calculation and the write,
1244 * by marking the buffer B_BUSY. It will
1245 * be freed later by brelse().
1246 */
1247 again:
1248 s = splbio();
1249 if((*bpp)->b_flags & B_BUSY) {
1250 #ifdef DEBUG
1251 printf("lfs_writeseg: avoiding potential data summary corruption for ino %d, lbn %d\n",
1252 VTOI((*bpp)->b_vp)->i_number,
1253 bp->b_lblkno);
1254 #endif
1255 (*bpp)->b_flags |= B_WANTED;
1256 tsleep((*bpp), (PRIBIO + 1),
1257 "lfs_writeseg", 0);
1258 splx(s);
1259 goto again;
1260 }
1261 (*bpp)->b_flags |= B_BUSY;
1262 splx(s);
1263 }
1264 *dp++ = ((u_long *)(*bpp)->b_data)[0];
1265 }
1266 }
1267 ssp->ss_create = time.tv_sec;
1268 ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
1269 ssp->ss_sumsum =
1270 cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
1271 free(datap, M_SEGMENT);
1272 #ifdef DIAGNOSTIC
1273 if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
1274 panic("lfs_writeseg: No diskspace for summary");
1275 #endif
1276 fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
1277
1278 strategy = devvp->v_op[VOFFSET(vop_strategy)];
1279
1280 /*
1281 * When we simply write the blocks we lose a rotation for every block
1282 * written. To avoid this problem, we allocate memory in chunks, copy
1283 * the buffers into the chunk and write the chunk. CHUNKSIZE is the
1284 * largest size I/O devices can handle.
1285 * When the data is copied to the chunk, turn off the the B_LOCKED bit
1286 * and brelse the buffer (which will move them to the LRU list). Add
1287 * the B_CALL flag to the buffer header so we can count I/O's for the
1288 * checkpoints and so we can release the allocated memory.
1289 *
1290 * XXX
1291 * This should be removed if the new virtual memory system allows us to
1292 * easily make the buffers contiguous in kernel memory and if that's
1293 * fast enough.
1294 */
1295
1296 #define CHUNKSIZE MAXPHYS
1297
1298 if(devvp==NULL)
1299 panic("devvp is NULL");
1300 for (bpp = sp->bpp,i = nblocks; i;) {
1301 cbp = lfs_newbuf(devvp, (*bpp)->b_blkno, CHUNKSIZE);
1302 cbp->b_dev = i_dev;
1303 cbp->b_flags |= B_ASYNC | B_BUSY;
1304 cbp->b_bcount = 0;
1305
1306 #ifdef DIAGNOSTIC
1307 if(datosn(fs,(*bpp)->b_blkno + ((*bpp)->b_bcount - 1)/DEV_BSIZE) != datosn(fs,cbp->b_blkno)) {
1308 panic("lfs_writeseg: Segment overwrite");
1309 }
1310 #endif
1311
1312 s = splbio();
1313 if(fs->lfs_iocount >= LFS_THROTTLE) {
1314 tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
1315 }
1316 ++fs->lfs_iocount;
1317 #ifdef LFS_TRACK_IOS
1318 for(j=0;j<LFS_THROTTLE;j++) {
1319 if(fs->lfs_pending[j]==LFS_UNUSED_DADDR) {
1320 fs->lfs_pending[j] = cbp->b_blkno;
1321 break;
1322 }
1323 }
1324 #endif /* LFS_TRACK_IOS */
1325 for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
1326 bp = *bpp;
1327
1328 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1329 break;
1330
1331 /*
1332 * Fake buffers from the cleaner are marked as B_INVAL.
1333 * We need to copy the data from user space rather than
1334 * from the buffer indicated.
1335 * XXX == what do I do on an error?
1336 */
1337 if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1338 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1339 panic("lfs_writeseg: copyin failed [2]");
1340 } else
1341 bcopy(bp->b_data, p, bp->b_bcount);
1342 p += bp->b_bcount;
1343 cbp->b_bcount += bp->b_bcount;
1344 if (bp->b_flags & B_LOCKED) {
1345 --locked_queue_count;
1346 locked_queue_bytes -= bp->b_bufsize;
1347 }
1348 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1349 B_LOCKED | B_GATHERED);
1350 vn = bp->b_vp;
1351 if (bp->b_flags & B_CALL) {
1352 /* if B_CALL, it was created with newbuf */
1353 lfs_freebuf(bp);
1354 } else {
1355 bremfree(bp);
1356 bp->b_flags |= B_DONE;
1357 if(vn)
1358 reassignbuf(bp, vn);
1359 brelse(bp);
1360 }
1361 if(bp->b_flags & B_NEEDCOMMIT) { /* XXX */
1362 bp->b_flags &= ~B_NEEDCOMMIT;
1363 wakeup(bp);
1364 }
1365
1366 bpp++;
1367
1368 /*
1369 * If this is the last block for this vnode, but
1370 * there are other blocks on its dirty list,
1371 * set IN_MODIFIED/IN_CLEANING depending on what
1372 * sort of block. Only do this for our mount point,
1373 * not for, e.g., inode blocks that are attached to
1374 * the devvp.
1375 */
1376 if(i>1 && vn && *bpp && (*bpp)->b_vp != vn
1377 && (*bpp)->b_vp && (bp=vn->v_dirtyblkhd.lh_first)!=NULL &&
1378 vn->v_mount == fs->lfs_ivnode->v_mount)
1379 {
1380 ip = VTOI(vn);
1381 #ifdef DEBUG_LFS
1382 printf("lfs_writeseg: marking ino %d\n",ip->i_number);
1383 #endif
1384 if(!(ip->i_flag & (IN_CLEANING|IN_MODIFIED))) {
1385 fs->lfs_uinodes++;
1386 if(bp->b_flags & B_CALL)
1387 ip->i_flag |= IN_CLEANING;
1388 else
1389 ip->i_flag |= IN_MODIFIED;
1390 }
1391 }
1392 /* if(vn->v_dirtyblkhd.lh_first == NULL) */
1393 wakeup(vn);
1394 }
1395 ++cbp->b_vp->v_numoutput;
1396 splx(s);
1397 /*
1398 * XXXX This is a gross and disgusting hack. Since these
1399 * buffers are physically addressed, they hang off the
1400 * device vnode (devvp). As a result, they have no way
1401 * of getting to the LFS superblock or lfs structure to
1402 * keep track of the number of I/O's pending. So, I am
1403 * going to stuff the fs into the saveaddr field of
1404 * the buffer (yuk).
1405 */
1406 cbp->b_saveaddr = (caddr_t)fs;
1407 vop_strategy_a.a_desc = VDESC(vop_strategy);
1408 vop_strategy_a.a_bp = cbp;
1409 (strategy)(&vop_strategy_a);
1410 }
1411 /*
1412 * XXX
1413 * Vinvalbuf can move locked buffers off the locked queue
1414 * and we have no way of knowing about this. So, after
1415 * doing a big write, we recalculate how many buffers are
1416 * really still left on the locked queue.
1417 */
1418 lfs_countlocked(&locked_queue_count,&locked_queue_bytes);
1419 wakeup(&locked_queue_count);
1420 if(lfs_dostats) {
1421 ++lfs_stats.psegwrites;
1422 lfs_stats.blocktot += nblocks - 1;
1423 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
1424 ++lfs_stats.psyncwrites;
1425 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
1426 ++lfs_stats.pcleanwrites;
1427 lfs_stats.cleanblocks += nblocks - 1;
1428 }
1429 }
1430 return (lfs_initseg(fs) || do_again);
1431 }
1432
1433 void
1434 lfs_writesuper(fs, daddr)
1435 struct lfs *fs;
1436 daddr_t daddr;
1437 {
1438 struct buf *bp;
1439 dev_t i_dev;
1440 int (*strategy) __P((void *));
1441 int s;
1442 struct vop_strategy_args vop_strategy_a;
1443
1444 #ifdef LFS_CANNOT_ROLLFW
1445 /*
1446 * If we can write one superblock while another is in
1447 * progress, we risk not having a complete checkpoint if we crash.
1448 * So, block here if a superblock write is in progress.
1449 */
1450 s = splbio();
1451 while(fs->lfs_sbactive) {
1452 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
1453 }
1454 fs->lfs_sbactive = daddr;
1455 splx(s);
1456 #endif
1457 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1458 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
1459
1460 /* Set timestamp of this version of the superblock */
1461 fs->lfs_tstamp = time.tv_sec;
1462
1463 /* Checksum the superblock and copy it into a buffer. */
1464 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1465 bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr, LFS_SBPAD);
1466 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
1467
1468 bp->b_dev = i_dev;
1469 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
1470 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
1471 bp->b_iodone = lfs_supercallback;
1472 /* XXX KS - same nasty hack as above */
1473 bp->b_saveaddr = (caddr_t)fs;
1474
1475 vop_strategy_a.a_desc = VDESC(vop_strategy);
1476 vop_strategy_a.a_bp = bp;
1477 s = splbio();
1478 ++bp->b_vp->v_numoutput;
1479 splx(s);
1480 (strategy)(&vop_strategy_a);
1481 }
1482
1483 /*
1484 * Logical block number match routines used when traversing the dirty block
1485 * chain.
1486 */
1487 int
1488 lfs_match_fake(fs, bp)
1489 struct lfs *fs;
1490 struct buf *bp;
1491 {
1492 return (bp->b_flags & B_CALL);
1493 }
1494
1495 int
1496 lfs_match_data(fs, bp)
1497 struct lfs *fs;
1498 struct buf *bp;
1499 {
1500 return (bp->b_lblkno >= 0);
1501 }
1502
1503 int
1504 lfs_match_indir(fs, bp)
1505 struct lfs *fs;
1506 struct buf *bp;
1507 {
1508 int lbn;
1509
1510 lbn = bp->b_lblkno;
1511 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
1512 }
1513
1514 int
1515 lfs_match_dindir(fs, bp)
1516 struct lfs *fs;
1517 struct buf *bp;
1518 {
1519 int lbn;
1520
1521 lbn = bp->b_lblkno;
1522 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
1523 }
1524
1525 int
1526 lfs_match_tindir(fs, bp)
1527 struct lfs *fs;
1528 struct buf *bp;
1529 {
1530 int lbn;
1531
1532 lbn = bp->b_lblkno;
1533 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
1534 }
1535
1536 /*
1537 * XXX - The only buffers that are going to hit these functions are the
1538 * segment write blocks, or the segment summaries, or the superblocks.
1539 *
1540 * All of the above are created by lfs_newbuf, and so do not need to be
1541 * released via brelse.
1542 */
1543 void
1544 lfs_callback(bp)
1545 struct buf *bp;
1546 {
1547 struct lfs *fs;
1548 #ifdef LFS_TRACK_IOS
1549 int j;
1550 #endif
1551
1552 fs = (struct lfs *)bp->b_saveaddr;
1553 #ifdef DIAGNOSTIC
1554 if (fs->lfs_iocount == 0)
1555 panic("lfs_callback: zero iocount\n");
1556 #endif
1557 if (--fs->lfs_iocount < LFS_THROTTLE)
1558 wakeup(&fs->lfs_iocount);
1559 #ifdef LFS_TRACK_IOS
1560 for(j=0;j<LFS_THROTTLE;j++) {
1561 if(fs->lfs_pending[j]==bp->b_blkno) {
1562 fs->lfs_pending[j] = LFS_UNUSED_DADDR;
1563 wakeup(&(fs->lfs_pending[j]));
1564 break;
1565 }
1566 }
1567 #endif /* LFS_TRACK_IOS */
1568
1569 lfs_freebuf(bp);
1570 }
1571
1572 void
1573 lfs_supercallback(bp)
1574 struct buf *bp;
1575 {
1576 #ifdef LFS_CANNOT_ROLLFW
1577 struct lfs *fs;
1578
1579 fs = (struct lfs *)bp->b_saveaddr;
1580 fs->lfs_sbactive=NULL;
1581 wakeup(&fs->lfs_sbactive);
1582 #endif
1583 lfs_freebuf(bp);
1584 }
1585
1586 /*
1587 * Shellsort (diminishing increment sort) from Data Structures and
1588 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
1589 * see also Knuth Vol. 3, page 84. The increments are selected from
1590 * formula (8), page 95. Roughly O(N^3/2).
1591 */
1592 /*
1593 * This is our own private copy of shellsort because we want to sort
1594 * two parallel arrays (the array of buffer pointers and the array of
1595 * logical block numbers) simultaneously. Note that we cast the array
1596 * of logical block numbers to a unsigned in this routine so that the
1597 * negative block numbers (meta data blocks) sort AFTER the data blocks.
1598 */
1599
1600 void
1601 lfs_shellsort(bp_array, lb_array, nmemb)
1602 struct buf **bp_array;
1603 ufs_daddr_t *lb_array;
1604 register int nmemb;
1605 {
1606 static int __rsshell_increments[] = { 4, 1, 0 };
1607 register int incr, *incrp, t1, t2;
1608 struct buf *bp_temp;
1609 u_long lb_temp;
1610
1611 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
1612 for (t1 = incr; t1 < nmemb; ++t1)
1613 for (t2 = t1 - incr; t2 >= 0;)
1614 if (lb_array[t2] > lb_array[t2 + incr]) {
1615 lb_temp = lb_array[t2];
1616 lb_array[t2] = lb_array[t2 + incr];
1617 lb_array[t2 + incr] = lb_temp;
1618 bp_temp = bp_array[t2];
1619 bp_array[t2] = bp_array[t2 + incr];
1620 bp_array[t2 + incr] = bp_temp;
1621 t2 -= incr;
1622 } else
1623 break;
1624 }
1625
1626 /*
1627 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
1628 */
1629 int
1630 lfs_vref(vp)
1631 register struct vnode *vp;
1632 {
1633 /*
1634 * If we return 1 here during a flush, we risk vinvalbuf() not
1635 * being able to flush all of the pages from this vnode, which
1636 * will cause it to panic. So, return 0 if a flush is in progress.
1637 */
1638 if (vp->v_flag & VXLOCK) {
1639 if(IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1640 return 0;
1641 }
1642 return(1);
1643 }
1644 return (vget(vp, 0));
1645 }
1646
1647 /*
1648 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
1649 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
1650 */
1651 void
1652 lfs_vunref(vp)
1653 register struct vnode *vp;
1654 {
1655 /*
1656 * Analogous to lfs_vref, if the node is flushing, fake it.
1657 */
1658 if((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1659 return;
1660 }
1661
1662 simple_lock(&vp->v_interlock);
1663 #ifdef DIAGNOSTIC
1664 if(vp->v_usecount<=0) {
1665 printf("lfs_vunref: flags are 0x%lx\n", vp->v_flag);
1666 printf("lfs_vunref: usecount = %ld\n", vp->v_usecount);
1667 panic("lfs_vunref: v_usecount<0");
1668 }
1669 #endif
1670 vp->v_usecount--;
1671 if (vp->v_usecount > 0) {
1672 simple_unlock(&vp->v_interlock);
1673 return;
1674 }
1675 #ifdef DIAGNOSTIC
1676 if(VOP_ISLOCKED(vp))
1677 panic("lfs_vunref: vnode locked");
1678 #endif
1679 /*
1680 * insert at tail of LRU list
1681 */
1682 simple_lock(&vnode_free_list_slock);
1683 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1684 simple_unlock(&vnode_free_list_slock);
1685 simple_unlock(&vp->v_interlock);
1686 }
1687
1688 /*
1689 * We use this when we have vnodes that were loaded in solely for cleaning.
1690 * There is no reason to believe that these vnodes will be referenced again
1691 * soon, since the cleaning process is unrelated to normal filesystem
1692 * activity. Putting cleaned vnodes at the tail of the list has the effect
1693 * of flushing the vnode LRU. So, put vnodes that were loaded only for
1694 * cleaning at the head of the list, instead.
1695 */
1696 void
1697 lfs_vunref_head(vp)
1698 register struct vnode *vp;
1699 {
1700 simple_lock(&vp->v_interlock);
1701 #ifdef DIAGNOSTIC
1702 if(vp->v_usecount==0) {
1703 panic("lfs_vunref: v_usecount<0");
1704 }
1705 #endif
1706 vp->v_usecount--;
1707 if (vp->v_usecount > 0) {
1708 simple_unlock(&vp->v_interlock);
1709 return;
1710 }
1711 #ifdef DIAGNOSTIC
1712 if(VOP_ISLOCKED(vp))
1713 panic("lfs_vunref_head: vnode locked");
1714 #endif
1715 /*
1716 * insert at head of LRU list
1717 */
1718 simple_lock(&vnode_free_list_slock);
1719 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1720 simple_unlock(&vnode_free_list_slock);
1721 simple_unlock(&vp->v_interlock);
1722 }
1723
1724