lfs_segment.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/namei.h>
39 #include <sys/kernel.h>
40 #include <sys/resourcevar.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/buf.h>
44 #include <sys/proc.h>
45 #include <sys/conf.h>
46 #include <sys/vnode.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49
50 #include <miscfs/specfs/specdev.h>
51 #include <miscfs/fifofs/fifo.h>
52
53 #include <ufs/ufs/quota.h>
54 #include <ufs/ufs/inode.h>
55 #include <ufs/ufs/dir.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/ufs_extern.h>
58
59 #include <ufs/lfs/lfs.h>
60 #include <ufs/lfs/lfs_extern.h>
61
62 extern int count_lock_queue __P((void));
63
64 #define MAX_ACTIVE 10
65 /*
66 * Determine if it's OK to start a partial in this segment, or if we need
67 * to go on to a new segment.
68 */
69 #define LFS_PARTIAL_FITS(fs) \
70 ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
71 1 << (fs)->lfs_fsbtodb)
72
73 void lfs_callback __P((struct buf *));
74 void lfs_gather __P((struct lfs *, struct segment *,
75 struct vnode *, int (*) __P((struct lfs *, struct buf *))));
76 int lfs_gatherblock __P((struct segment *, struct buf *, int *));
77 void lfs_iset __P((struct inode *, ufs_daddr_t, time_t));
78 int lfs_match_data __P((struct lfs *, struct buf *));
79 int lfs_match_dindir __P((struct lfs *, struct buf *));
80 int lfs_match_indir __P((struct lfs *, struct buf *));
81 int lfs_match_tindir __P((struct lfs *, struct buf *));
82 void lfs_newseg __P((struct lfs *));
83 void lfs_shellsort __P((struct buf **, ufs_daddr_t *, register int));
84 void lfs_supercallback __P((struct buf *));
85 void lfs_updatemeta __P((struct segment *));
86 int lfs_vref __P((struct vnode *));
87 void lfs_vunref __P((struct vnode *));
88 void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
89 int lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
90 int lfs_writeseg __P((struct lfs *, struct segment *));
91 void lfs_writesuper __P((struct lfs *));
92 void lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
93 struct segment *sp, int dirops));
94
95 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
96
97 /* Statistics Counters */
98 #define DOSTATS
99 struct lfs_stats lfs_stats;
100
101 /* op values to lfs_writevnodes */
102 #define VN_REG 0
103 #define VN_DIROP 1
104 #define VN_EMPTY 2
105
106 /*
107 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
108 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
109 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
110 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
111 */
112
113 int
114 lfs_vflush(vp)
115 struct vnode *vp;
116 {
117 struct inode *ip;
118 struct lfs *fs;
119 struct segment *sp;
120
121 fs = VFSTOUFS(vp->v_mount)->um_lfs;
122 if (fs->lfs_nactive > MAX_ACTIVE)
123 return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP));
124 lfs_seglock(fs, SEGM_SYNC);
125 sp = fs->lfs_sp;
126
127
128 ip = VTOI(vp);
129 if (vp->v_dirtyblkhd.lh_first == NULL)
130 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
131
132 do {
133 do {
134 if (vp->v_dirtyblkhd.lh_first != NULL)
135 lfs_writefile(fs, sp, vp);
136 } while (lfs_writeinode(fs, sp, ip));
137
138 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
139
140 #ifdef DOSTATS
141 ++lfs_stats.nwrites;
142 if (sp->seg_flags & SEGM_SYNC)
143 ++lfs_stats.nsync_writes;
144 if (sp->seg_flags & SEGM_CKP)
145 ++lfs_stats.ncheckpoints;
146 #endif
147 lfs_segunlock(fs);
148 return (0);
149 }
150
151 void
152 lfs_writevnodes(fs, mp, sp, op)
153 struct lfs *fs;
154 struct mount *mp;
155 struct segment *sp;
156 int op;
157 {
158 struct inode *ip;
159 struct vnode *vp;
160
161 /* BEGIN HACK */
162 #define VN_OFFSET (((void *)&vp->v_mntvnodes.le_next) - (void *)vp)
163 #define BACK_VP(VP) ((struct vnode *)(((void *)VP->v_mntvnodes.le_prev) - VN_OFFSET))
164 #define BEG_OF_VLIST ((struct vnode *)(((void *)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
165
166 /* Find last vnode. */
167 loop: for (vp = mp->mnt_vnodelist.lh_first;
168 vp && vp->v_mntvnodes.le_next != NULL;
169 vp = vp->v_mntvnodes.le_next);
170 for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
171 /* END HACK */
172 /*
173 loop:
174 for (vp = mp->mnt_vnodelist.lh_first;
175 vp != NULL;
176 vp = vp->v_mntvnodes.le_next) {
177 */
178 /*
179 * If the vnode that we are about to sync is no longer
180 * associated with this mount point, start over.
181 */
182 if (vp->v_mount != mp)
183 goto loop;
184
185 /* XXX ignore dirops for now
186 if (op == VN_DIROP && !(vp->v_flag & VDIROP) ||
187 op != VN_DIROP && (vp->v_flag & VDIROP))
188 continue;
189 */
190
191 if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first)
192 continue;
193
194 if (vp->v_type == VNON)
195 continue;
196
197 if (lfs_vref(vp))
198 continue;
199
200 /*
201 * Write the inode/file if dirty and it's not the
202 * the IFILE.
203 */
204 ip = VTOI(vp);
205 if ((ip->i_flag &
206 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) ||
207 vp->v_dirtyblkhd.lh_first != NULL) &&
208 ip->i_number != LFS_IFILE_INUM) {
209 if (vp->v_dirtyblkhd.lh_first != NULL)
210 lfs_writefile(fs, sp, vp);
211 (void) lfs_writeinode(fs, sp, ip);
212 }
213 vp->v_flag &= ~VDIROP;
214 lfs_vunref(vp);
215 }
216 }
217
218 int
219 lfs_segwrite(mp, flags)
220 struct mount *mp;
221 int flags; /* Do a checkpoint. */
222 {
223 struct proc *p = curproc; /* XXX */
224 struct buf *bp;
225 struct inode *ip;
226 struct lfs *fs;
227 struct segment *sp;
228 struct vnode *vp;
229 SEGUSE *segusep;
230 ufs_daddr_t ibno;
231 CLEANERINFO *cip;
232 int clean, do_ckp, error, i;
233
234 fs = VFSTOUFS(mp)->um_lfs;
235
236 /*
237 * If we have fewer than 2 clean segments, wait until cleaner
238 * writes.
239 */
240 do {
241 LFS_CLEANERINFO(cip, fs, bp);
242 clean = cip->clean;
243 brelse(bp);
244 if (clean <= 2 || fs->lfs_avail <= 0) {
245 /* printf ("segs clean: %d\n", clean); */
246 wakeup(&lfs_allclean_wakeup);
247 wakeup(&fs->lfs_nextseg);
248 if (error = tsleep(&fs->lfs_avail, PRIBIO + 1,
249 "lfs writer", 0))
250 return (error);
251 }
252 } while (clean <= 2 || fs->lfs_avail <= 0);
253
254 /*
255 * Allocate a segment structure and enough space to hold pointers to
256 * the maximum possible number of buffers which can be described in a
257 * single summary block.
258 */
259 do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE;
260 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
261 sp = fs->lfs_sp;
262
263 lfs_writevnodes(fs, mp, sp, VN_REG);
264
265 /* XXX ignore ordering of dirops for now */
266 /* XXX
267 fs->lfs_writer = 1;
268 if (fs->lfs_dirops && (error =
269 tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
270 free(sp->bpp, M_SEGMENT);
271 free(sp, M_SEGMENT);
272 fs->lfs_writer = 0;
273 return (error);
274 }
275
276 lfs_writevnodes(fs, mp, sp, VN_DIROP);
277 */
278
279 /*
280 * If we are doing a checkpoint, mark everything since the
281 * last checkpoint as no longer ACTIVE.
282 */
283 if (do_ckp)
284 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
285 --ibno >= fs->lfs_cleansz; ) {
286 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
287 NOCRED, &bp))
288
289 panic("lfs: ifile read");
290 segusep = (SEGUSE *)bp->b_data;
291 for (i = fs->lfs_sepb; i--; segusep++)
292 segusep->su_flags &= ~SEGUSE_ACTIVE;
293
294 error = VOP_BWRITE(bp);
295 }
296
297 if (do_ckp || fs->lfs_doifile) {
298 redo:
299 vp = fs->lfs_ivnode;
300 while (vget(vp, LK_EXCLUSIVE, p))
301 continue;
302 ip = VTOI(vp);
303 if (vp->v_dirtyblkhd.lh_first != NULL)
304 lfs_writefile(fs, sp, vp);
305 (void)lfs_writeinode(fs, sp, ip);
306 vput(vp);
307 if (lfs_writeseg(fs, sp) && do_ckp)
308 goto redo;
309 } else
310 (void) lfs_writeseg(fs, sp);
311
312 /*
313 * If the I/O count is non-zero, sleep until it reaches zero. At the
314 * moment, the user's process hangs around so we can sleep.
315 */
316 /* XXX ignore dirops for now
317 fs->lfs_writer = 0;
318 fs->lfs_doifile = 0;
319 wakeup(&fs->lfs_dirops);
320 */
321
322 #ifdef DOSTATS
323 ++lfs_stats.nwrites;
324 if (sp->seg_flags & SEGM_SYNC)
325 ++lfs_stats.nsync_writes;
326 if (sp->seg_flags & SEGM_CKP)
327 ++lfs_stats.ncheckpoints;
328 #endif
329 lfs_segunlock(fs);
330 return (0);
331 }
332
333 /*
334 * Write the dirty blocks associated with a vnode.
335 */
336 void
337 lfs_writefile(fs, sp, vp)
338 struct lfs *fs;
339 struct segment *sp;
340 struct vnode *vp;
341 {
342 struct buf *bp;
343 struct finfo *fip;
344 IFILE *ifp;
345
346 if (sp->seg_bytes_left < fs->lfs_bsize ||
347 sp->sum_bytes_left < sizeof(struct finfo))
348 (void) lfs_writeseg(fs, sp);
349
350 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
351 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
352
353 fip = sp->fip;
354 fip->fi_nblocks = 0;
355 fip->fi_ino = VTOI(vp)->i_number;
356 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
357 fip->fi_version = ifp->if_version;
358 brelse(bp);
359
360 /*
361 * It may not be necessary to write the meta-data blocks at this point,
362 * as the roll-forward recovery code should be able to reconstruct the
363 * list.
364 */
365 lfs_gather(fs, sp, vp, lfs_match_data);
366 lfs_gather(fs, sp, vp, lfs_match_indir);
367 lfs_gather(fs, sp, vp, lfs_match_dindir);
368 #ifdef TRIPLE
369 lfs_gather(fs, sp, vp, lfs_match_tindir);
370 #endif
371
372 fip = sp->fip;
373 if (fip->fi_nblocks != 0) {
374 sp->fip =
375 (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
376 sizeof(ufs_daddr_t) * (fip->fi_nblocks - 1));
377 sp->start_lbp = &sp->fip->fi_blocks[0];
378 } else {
379 sp->sum_bytes_left += sizeof(struct finfo) - sizeof(ufs_daddr_t);
380 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
381 }
382 }
383
384 int
385 lfs_writeinode(fs, sp, ip)
386 struct lfs *fs;
387 struct segment *sp;
388 struct inode *ip;
389 {
390 struct buf *bp, *ibp;
391 IFILE *ifp;
392 SEGUSE *sup;
393 ufs_daddr_t daddr;
394 ino_t ino;
395 int error, i, ndx;
396 int redo_ifile = 0;
397
398 if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)))
399 return(0);
400
401 /* Allocate a new inode block if necessary. */
402 if (sp->ibp == NULL) {
403 /* Allocate a new segment if necessary. */
404 if (sp->seg_bytes_left < fs->lfs_bsize ||
405 sp->sum_bytes_left < sizeof(ufs_daddr_t))
406 (void) lfs_writeseg(fs, sp);
407
408 /* Get next inode block. */
409 daddr = fs->lfs_offset;
410 fs->lfs_offset += fsbtodb(fs, 1);
411 sp->ibp = *sp->cbpp++ =
412 lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
413 fs->lfs_bsize);
414 /* Zero out inode numbers */
415 for (i = 0; i < INOPB(fs); ++i)
416 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
417 ++sp->start_bpp;
418 fs->lfs_avail -= fsbtodb(fs, 1);
419 /* Set remaining space counters. */
420 sp->seg_bytes_left -= fs->lfs_bsize;
421 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
422 ndx = LFS_SUMMARY_SIZE / sizeof(ufs_daddr_t) -
423 sp->ninodes / INOPB(fs) - 1;
424 ((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
425 }
426
427 /* Update the inode times and copy the inode onto the inode page. */
428 if (ip->i_flag & IN_MODIFIED)
429 --fs->lfs_uinodes;
430 ITIMES(ip, &time, &time);
431 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
432 bp = sp->ibp;
433 ((struct dinode *)bp->b_data)[sp->ninodes % INOPB(fs)] = ip->i_din;
434 /* Increment inode count in segment summary block. */
435 ++((SEGSUM *)(sp->segsum))->ss_ninos;
436
437 /* If this page is full, set flag to allocate a new page. */
438 if (++sp->ninodes % INOPB(fs) == 0)
439 sp->ibp = NULL;
440
441 /*
442 * If updating the ifile, update the super-block. Update the disk
443 * address and access times for this inode in the ifile.
444 */
445 ino = ip->i_number;
446 if (ino == LFS_IFILE_INUM) {
447 daddr = fs->lfs_idaddr;
448 fs->lfs_idaddr = bp->b_blkno;
449 } else {
450 LFS_IENTRY(ifp, fs, ino, ibp);
451 daddr = ifp->if_daddr;
452 ifp->if_daddr = bp->b_blkno;
453 error = VOP_BWRITE(ibp);
454 }
455
456 /*
457 * No need to update segment usage if there was no former inode address
458 * or if the last inode address is in the current partial segment.
459 */
460 if (daddr != LFS_UNUSED_DADDR &&
461 !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
462 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
463 #ifdef DIAGNOSTIC
464 if (sup->su_nbytes < sizeof(struct dinode)) {
465 /* XXX -- Change to a panic. */
466 printf("lfs: negative bytes (segment %d)\n",
467 datosn(fs, daddr));
468 panic("negative bytes");
469 }
470 #endif
471 sup->su_nbytes -= sizeof(struct dinode);
472 redo_ifile =
473 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
474 error = VOP_BWRITE(bp);
475 }
476 return (redo_ifile);
477 }
478
479 int
480 lfs_gatherblock(sp, bp, sptr)
481 struct segment *sp;
482 struct buf *bp;
483 int *sptr;
484 {
485 struct lfs *fs;
486 int version;
487
488 /*
489 * If full, finish this segment. We may be doing I/O, so
490 * release and reacquire the splbio().
491 */
492 #ifdef DIAGNOSTIC
493 if (sp->vp == NULL)
494 panic ("lfs_gatherblock: Null vp in segment");
495 #endif
496 fs = sp->fs;
497 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
498 sp->seg_bytes_left < bp->b_bcount) {
499 if (sptr)
500 splx(*sptr);
501 lfs_updatemeta(sp);
502
503 version = sp->fip->fi_version;
504 (void) lfs_writeseg(fs, sp);
505
506 sp->fip->fi_version = version;
507 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
508 /* Add the current file to the segment summary. */
509 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
510 sp->sum_bytes_left -=
511 sizeof(struct finfo) - sizeof(ufs_daddr_t);
512
513 if (sptr)
514 *sptr = splbio();
515 return(1);
516 }
517
518 /* Insert into the buffer list, update the FINFO block. */
519 bp->b_flags |= B_GATHERED;
520 *sp->cbpp++ = bp;
521 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
522
523 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
524 sp->seg_bytes_left -= bp->b_bcount;
525 return(0);
526 }
527
528 void
529 lfs_gather(fs, sp, vp, match)
530 struct lfs *fs;
531 struct segment *sp;
532 struct vnode *vp;
533 int (*match) __P((struct lfs *, struct buf *));
534 {
535 struct buf *bp;
536 int s;
537
538 sp->vp = vp;
539 s = splbio();
540 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
541 /* BEGIN HACK */
542 #define BUF_OFFSET (((void *)&bp->b_vnbufs.le_next) - (void *)bp)
543 #define BACK_BUF(BP) ((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
544 #define BEG_OF_LIST ((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
545
546
547 /*loop: for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {*/
548 /* Find last buffer. */
549 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
550 bp = bp->b_vnbufs.le_next);
551 for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
552 /* END HACK */
553 if (bp->b_flags & B_BUSY || !match(fs, bp) ||
554 bp->b_flags & B_GATHERED)
555 continue;
556 #ifdef DIAGNOSTIC
557 if (!(bp->b_flags & B_DELWRI))
558 panic("lfs_gather: bp not B_DELWRI");
559 if (!(bp->b_flags & B_LOCKED))
560 panic("lfs_gather: bp not B_LOCKED");
561 #endif
562 if (lfs_gatherblock(sp, bp, &s))
563 goto loop;
564 }
565 splx(s);
566 lfs_updatemeta(sp);
567 sp->vp = NULL;
568 }
569
570
571 /*
572 * Update the metadata that points to the blocks listed in the FINFO
573 * array.
574 */
575 void
576 lfs_updatemeta(sp)
577 struct segment *sp;
578 {
579 SEGUSE *sup;
580 struct buf *bp;
581 struct lfs *fs;
582 struct vnode *vp;
583 struct indir a[NIADDR + 2], *ap;
584 struct inode *ip;
585 ufs_daddr_t daddr, lbn, off;
586 int error, i, nblocks, num;
587
588 vp = sp->vp;
589 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
590 if (nblocks < 0)
591 panic("This is a bad thing\n");
592 if (vp == NULL || nblocks == 0)
593 return;
594
595 /* Sort the blocks. */
596 if (!(sp->seg_flags & SEGM_CLEAN))
597 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
598
599 /*
600 * Record the length of the last block in case it's a fragment.
601 * If there are indirect blocks present, they sort last. An
602 * indirect block will be lfs_bsize and its presence indicates
603 * that you cannot have fragments.
604 */
605 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
606
607 /*
608 * Assign disk addresses, and update references to the logical
609 * block and the segment usage information.
610 */
611 fs = sp->fs;
612 for (i = nblocks; i--; ++sp->start_bpp) {
613 lbn = *sp->start_lbp++;
614 (*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
615 fs->lfs_offset +=
616 fragstodb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
617
618 if (error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL))
619 panic("lfs_updatemeta: ufs_bmaparray %d", error);
620 ip = VTOI(vp);
621 switch (num) {
622 case 0:
623 ip->i_db[lbn] = off;
624 break;
625 case 1:
626 ip->i_ib[a[0].in_off] = off;
627 break;
628 default:
629 ap = &a[num - 1];
630 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
631 panic("lfs_updatemeta: bread bno %d",
632 ap->in_lbn);
633 /*
634 * Bread may create a new indirect block which needs
635 * to get counted for the inode.
636 */
637 if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
638 ip->i_blocks += fsbtodb(fs, 1);
639 fs->lfs_bfree -= fragstodb(fs, fs->lfs_frag);
640 }
641 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
642 VOP_BWRITE(bp);
643 }
644
645 /* Update segment usage information. */
646 if (daddr != UNASSIGNED &&
647 !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
648 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
649 #ifdef DIAGNOSTIC
650 if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
651 /* XXX -- Change to a panic. */
652 printf("lfs: negative bytes (segment %d)\n",
653 datosn(fs, daddr));
654 printf("lfs: bp = 0x%x, addr = 0x%x\n",
655 bp, bp->b_un.b_addr);
656 panic ("Negative Bytes");
657 }
658 #endif
659 sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
660 error = VOP_BWRITE(bp);
661 }
662 }
663 }
664
665 /*
666 * Start a new segment.
667 */
668 int
669 lfs_initseg(fs)
670 struct lfs *fs;
671 {
672 struct segment *sp;
673 SEGUSE *sup;
674 SEGSUM *ssp;
675 struct buf *bp;
676 int repeat;
677
678 sp = fs->lfs_sp;
679
680 repeat = 0;
681 /* Advance to the next segment. */
682 if (!LFS_PARTIAL_FITS(fs)) {
683 /* Wake up any cleaning procs waiting on this file system. */
684 wakeup(&lfs_allclean_wakeup);
685 wakeup(&fs->lfs_nextseg);
686
687 lfs_newseg(fs);
688 repeat = 1;
689 fs->lfs_offset = fs->lfs_curseg;
690 sp->seg_number = datosn(fs, fs->lfs_curseg);
691 sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
692
693 /*
694 * If the segment contains a superblock, update the offset
695 * and summary address to skip over it.
696 */
697 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
698 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
699 fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
700 sp->seg_bytes_left -= LFS_SBPAD;
701 }
702 brelse(bp);
703 } else {
704 sp->seg_number = datosn(fs, fs->lfs_curseg);
705 sp->seg_bytes_left = (fs->lfs_dbpseg -
706 (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
707 }
708 fs->lfs_lastpseg = fs->lfs_offset;
709
710 sp->fs = fs;
711 sp->ibp = NULL;
712 sp->ninodes = 0;
713
714 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
715 sp->cbpp = sp->bpp;
716 *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
717 LFS_SUMMARY_SIZE);
718 sp->segsum = (*sp->cbpp)->b_data;
719 bzero(sp->segsum, LFS_SUMMARY_SIZE);
720 sp->start_bpp = ++sp->cbpp;
721 fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
722
723 /* Set point to SEGSUM, initialize it. */
724 ssp = sp->segsum;
725 ssp->ss_next = fs->lfs_nextseg;
726 ssp->ss_nfinfo = ssp->ss_ninos = 0;
727 ssp->ss_magic = SS_MAGIC;
728
729 /* Set pointer to first FINFO, initialize it. */
730 sp->fip = (struct finfo *)((caddr_t)sp->segsum + sizeof(SEGSUM));
731 sp->fip->fi_nblocks = 0;
732 sp->start_lbp = &sp->fip->fi_blocks[0];
733 sp->fip->fi_lastlength = 0;
734
735 sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
736 sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
737
738 return(repeat);
739 }
740
741 /*
742 * Return the next segment to write.
743 */
744 void
745 lfs_newseg(fs)
746 struct lfs *fs;
747 {
748 CLEANERINFO *cip;
749 SEGUSE *sup;
750 struct buf *bp;
751 int curseg, isdirty, sn;
752
753 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
754 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
755 sup->su_nbytes = 0;
756 sup->su_nsums = 0;
757 sup->su_ninos = 0;
758 (void) VOP_BWRITE(bp);
759
760 LFS_CLEANERINFO(cip, fs, bp);
761 --cip->clean;
762 ++cip->dirty;
763 (void) VOP_BWRITE(bp);
764
765 fs->lfs_lastseg = fs->lfs_curseg;
766 fs->lfs_curseg = fs->lfs_nextseg;
767 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
768 sn = (sn + 1) % fs->lfs_nseg;
769 if (sn == curseg)
770 panic("lfs_nextseg: no clean segments");
771 LFS_SEGENTRY(sup, fs, sn, bp);
772 isdirty = sup->su_flags & SEGUSE_DIRTY;
773 brelse(bp);
774 if (!isdirty)
775 break;
776 }
777
778 ++fs->lfs_nactive;
779 fs->lfs_nextseg = sntoda(fs, sn);
780 #ifdef DOSTATS
781 ++lfs_stats.segsused;
782 #endif
783 }
784
785 int
786 lfs_writeseg(fs, sp)
787 struct lfs *fs;
788 struct segment *sp;
789 {
790 extern int locked_queue_count;
791 struct buf **bpp, *bp, *cbp;
792 SEGUSE *sup;
793 SEGSUM *ssp;
794 dev_t i_dev;
795 u_long *datap, *dp;
796 int do_again, i, nblocks, s;
797 int (*strategy)__P((struct vop_strategy_args *));
798 struct vop_strategy_args vop_strategy_a;
799 u_short ninos;
800 char *p;
801
802 /*
803 * If there are no buffers other than the segment summary to write
804 * and it is not a checkpoint, don't do anything. On a checkpoint,
805 * even if there aren't any buffers, you need to write the superblock.
806 */
807 if ((nblocks = sp->cbpp - sp->bpp) == 1)
808 return (0);
809
810 /* Update the segment usage information. */
811 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
812
813 /* Loop through all blocks, except the segment summary. */
814 for (bpp = sp->bpp; ++bpp < sp->cbpp; )
815 sup->su_nbytes += (*bpp)->b_bcount;
816
817 ssp = (SEGSUM *)sp->segsum;
818
819 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
820 sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
821 sup->su_nbytes += LFS_SUMMARY_SIZE;
822 sup->su_lastmod = time.tv_sec;
823 sup->su_ninos += ninos;
824 ++sup->su_nsums;
825 do_again = !(bp->b_flags & B_GATHERED);
826 (void)VOP_BWRITE(bp);
827 /*
828 * Compute checksum across data and then across summary; the first
829 * block (the summary block) is skipped. Set the create time here
830 * so that it's guaranteed to be later than the inode mod times.
831 *
832 * XXX
833 * Fix this to do it inline, instead of malloc/copy.
834 */
835 datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
836 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
837 if ((*++bpp)->b_flags & B_INVAL) {
838 if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
839 panic("lfs_writeseg: copyin failed");
840 } else
841 *dp++ = ((u_long *)(*bpp)->b_data)[0];
842 }
843 ssp->ss_create = time.tv_sec;
844 ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
845 ssp->ss_sumsum =
846 cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
847 free(datap, M_SEGMENT);
848 #ifdef DIAGNOSTIC
849 if (fs->lfs_bfree < fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE)
850 panic("lfs_writeseg: No diskspace for summary");
851 #endif
852 fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
853
854 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
855 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
856
857 /*
858 * When we simply write the blocks we lose a rotation for every block
859 * written. To avoid this problem, we allocate memory in chunks, copy
860 * the buffers into the chunk and write the chunk. MAXPHYS is the
861 * largest size I/O devices can handle.
862 * When the data is copied to the chunk, turn off the the B_LOCKED bit
863 * and brelse the buffer (which will move them to the LRU list). Add
864 * the B_CALL flag to the buffer header so we can count I/O's for the
865 * checkpoints and so we can release the allocated memory.
866 *
867 * XXX
868 * This should be removed if the new virtual memory system allows us to
869 * easily make the buffers contiguous in kernel memory and if that's
870 * fast enough.
871 */
872 for (bpp = sp->bpp, i = nblocks; i;) {
873 cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
874 (*bpp)->b_blkno, MAXPHYS);
875 cbp->b_dev = i_dev;
876 cbp->b_flags |= B_ASYNC | B_BUSY;
877 cbp->b_bcount = 0;
878
879 s = splbio();
880 ++fs->lfs_iocount;
881 for (p = cbp->b_data; i && cbp->b_bcount < MAXPHYS; i--) {
882 bp = *bpp;
883 if (bp->b_bcount > (MAXPHYS - cbp->b_bcount))
884 break;
885 bpp++;
886
887 /*
888 * Fake buffers from the cleaner are marked as B_INVAL.
889 * We need to copy the data from user space rather than
890 * from the buffer indicated.
891 * XXX == what do I do on an error?
892 */
893 if (bp->b_flags & B_INVAL) {
894 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
895 panic("lfs_writeseg: copyin failed");
896 } else
897 bcopy(bp->b_data, p, bp->b_bcount);
898 p += bp->b_bcount;
899 cbp->b_bcount += bp->b_bcount;
900 if (bp->b_flags & B_LOCKED)
901 --locked_queue_count;
902 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
903 B_LOCKED | B_GATHERED);
904 if (bp->b_flags & B_CALL) {
905 /* if B_CALL, it was created with newbuf */
906 brelvp(bp);
907 if (!(bp->b_flags & B_INVAL))
908 free(bp->b_data, M_SEGMENT);
909 free(bp, M_SEGMENT);
910 } else {
911 bremfree(bp);
912 bp->b_flags |= B_DONE;
913 reassignbuf(bp, bp->b_vp);
914 brelse(bp);
915 }
916 }
917 ++cbp->b_vp->v_numoutput;
918 splx(s);
919 /*
920 * XXXX This is a gross and disgusting hack. Since these
921 * buffers are physically addressed, they hang off the
922 * device vnode (devvp). As a result, they have no way
923 * of getting to the LFS superblock or lfs structure to
924 * keep track of the number of I/O's pending. So, I am
925 * going to stuff the fs into the saveaddr field of
926 * the buffer (yuk).
927 */
928 cbp->b_saveaddr = (caddr_t)fs;
929 vop_strategy_a.a_desc = VDESC(vop_strategy);
930 vop_strategy_a.a_bp = cbp;
931 (strategy)(&vop_strategy_a);
932 }
933 /*
934 * XXX
935 * Vinvalbuf can move locked buffers off the locked queue
936 * and we have no way of knowing about this. So, after
937 * doing a big write, we recalculate how many bufers are
938 * really still left on the locked queue.
939 */
940 locked_queue_count = count_lock_queue();
941 wakeup(&locked_queue_count);
942 #ifdef DOSTATS
943 ++lfs_stats.psegwrites;
944 lfs_stats.blocktot += nblocks - 1;
945 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
946 ++lfs_stats.psyncwrites;
947 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
948 ++lfs_stats.pcleanwrites;
949 lfs_stats.cleanblocks += nblocks - 1;
950 }
951 #endif
952 return (lfs_initseg(fs) || do_again);
953 }
954
955 void
956 lfs_writesuper(fs)
957 struct lfs *fs;
958 {
959 struct buf *bp;
960 dev_t i_dev;
961 int (*strategy) __P((struct vop_strategy_args *));
962 int s;
963 struct vop_strategy_args vop_strategy_a;
964
965 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
966 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
967
968 /* Checksum the superblock and copy it into a buffer. */
969 fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
970 bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
971 LFS_SBPAD);
972 *(struct lfs *)bp->b_data = *fs;
973
974 /* XXX Toggle between first two superblocks; for now just write first */
975 bp->b_dev = i_dev;
976 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
977 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
978 bp->b_iodone = lfs_supercallback;
979 vop_strategy_a.a_desc = VDESC(vop_strategy);
980 vop_strategy_a.a_bp = bp;
981 s = splbio();
982 ++bp->b_vp->v_numoutput;
983 splx(s);
984 (strategy)(&vop_strategy_a);
985 }
986
987 /*
988 * Logical block number match routines used when traversing the dirty block
989 * chain.
990 */
991 int
992 lfs_match_data(fs, bp)
993 struct lfs *fs;
994 struct buf *bp;
995 {
996 return (bp->b_lblkno >= 0);
997 }
998
999 int
1000 lfs_match_indir(fs, bp)
1001 struct lfs *fs;
1002 struct buf *bp;
1003 {
1004 int lbn;
1005
1006 lbn = bp->b_lblkno;
1007 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
1008 }
1009
1010 int
1011 lfs_match_dindir(fs, bp)
1012 struct lfs *fs;
1013 struct buf *bp;
1014 {
1015 int lbn;
1016
1017 lbn = bp->b_lblkno;
1018 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
1019 }
1020
1021 int
1022 lfs_match_tindir(fs, bp)
1023 struct lfs *fs;
1024 struct buf *bp;
1025 {
1026 int lbn;
1027
1028 lbn = bp->b_lblkno;
1029 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
1030 }
1031
1032 /*
1033 * Allocate a new buffer header.
1034 */
1035 struct buf *
1036 lfs_newbuf(vp, daddr, size)
1037 struct vnode *vp;
1038 ufs_daddr_t daddr;
1039 size_t size;
1040 {
1041 struct buf *bp;
1042 size_t nbytes;
1043
1044 nbytes = roundup(size, DEV_BSIZE);
1045 bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
1046 bzero(bp, sizeof(struct buf));
1047 if (nbytes)
1048 bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
1049 bgetvp(vp, bp);
1050 bp->b_bufsize = size;
1051 bp->b_bcount = size;
1052 bp->b_lblkno = daddr;
1053 bp->b_blkno = daddr;
1054 bp->b_error = 0;
1055 bp->b_resid = 0;
1056 bp->b_iodone = lfs_callback;
1057 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
1058 return (bp);
1059 }
1060
1061 void
1062 lfs_callback(bp)
1063 struct buf *bp;
1064 {
1065 struct lfs *fs;
1066
1067 fs = (struct lfs *)bp->b_saveaddr;
1068 #ifdef DIAGNOSTIC
1069 if (fs->lfs_iocount == 0)
1070 panic("lfs_callback: zero iocount\n");
1071 #endif
1072 if (--fs->lfs_iocount == 0)
1073 wakeup(&fs->lfs_iocount);
1074
1075 brelvp(bp);
1076 free(bp->b_data, M_SEGMENT);
1077 free(bp, M_SEGMENT);
1078 }
1079
1080 void
1081 lfs_supercallback(bp)
1082 struct buf *bp;
1083 {
1084 brelvp(bp);
1085 free(bp->b_data, M_SEGMENT);
1086 free(bp, M_SEGMENT);
1087 }
1088
1089 /*
1090 * Shellsort (diminishing increment sort) from Data Structures and
1091 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
1092 * see also Knuth Vol. 3, page 84. The increments are selected from
1093 * formula (8), page 95. Roughly O(N^3/2).
1094 */
1095 /*
1096 * This is our own private copy of shellsort because we want to sort
1097 * two parallel arrays (the array of buffer pointers and the array of
1098 * logical block numbers) simultaneously. Note that we cast the array
1099 * of logical block numbers to a unsigned in this routine so that the
1100 * negative block numbers (meta data blocks) sort AFTER the data blocks.
1101 */
1102 void
1103 lfs_shellsort(bp_array, lb_array, nmemb)
1104 struct buf **bp_array;
1105 ufs_daddr_t *lb_array;
1106 register int nmemb;
1107 {
1108 static int __rsshell_increments[] = { 4, 1, 0 };
1109 register int incr, *incrp, t1, t2;
1110 struct buf *bp_temp;
1111 u_long lb_temp;
1112
1113 for (incrp = __rsshell_increments; incr = *incrp++;)
1114 for (t1 = incr; t1 < nmemb; ++t1)
1115 for (t2 = t1 - incr; t2 >= 0;)
1116 if (lb_array[t2] > lb_array[t2 + incr]) {
1117 lb_temp = lb_array[t2];
1118 lb_array[t2] = lb_array[t2 + incr];
1119 lb_array[t2 + incr] = lb_temp;
1120 bp_temp = bp_array[t2];
1121 bp_array[t2] = bp_array[t2 + incr];
1122 bp_array[t2 + incr] = bp_temp;
1123 t2 -= incr;
1124 } else
1125 break;
1126 }
1127
1128 /*
1129 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
1130 */
1131 lfs_vref(vp)
1132 register struct vnode *vp;
1133 {
1134 struct proc *p = curproc; /* XXX */
1135
1136 if (vp->v_flag & VXLOCK) /* XXX */
1137 return(1);
1138 return (vget(vp, 0, p));
1139 }
1140
1141 /*
1142 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
1143 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
1144 */
1145 void
1146 lfs_vunref(vp)
1147 register struct vnode *vp;
1148 {
1149 struct proc *p = curproc; /* XXX */
1150 extern struct simplelock vnode_free_list_slock; /* XXX */
1151 extern TAILQ_HEAD(freelst, vnode) vnode_free_list; /* XXX */
1152
1153 simple_lock(&vp->v_interlock);
1154 vp->v_usecount--;
1155 if (vp->v_usecount > 0) {
1156 simple_unlock(&vp->v_interlock);
1157 return;
1158 }
1159 /*
1160 * insert at tail of LRU list
1161 */
1162 simple_lock(&vnode_free_list_slock);
1163 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1164 simple_unlock(&vnode_free_list_slock);
1165 simple_unlock(&vp->v_interlock);
1166 }
1167