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