update
[unix-history] / usr / src / sys / ufs / lfs / lfs_segment.c
CommitLineData
84c30241
KB
1/*
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 *
9a46ddb2 7 * @(#)lfs_segment.c 7.12 (Berkeley) %G%
84c30241
KB
8 */
9
34a084a9
KB
10#include <sys/param.h>
11#include <sys/systm.h>
12#include <sys/namei.h>
34a084a9 13#include <sys/kernel.h>
a1b8db53 14#include <sys/resourcevar.h>
34a084a9
KB
15#include <sys/file.h>
16#include <sys/stat.h>
17#include <sys/buf.h>
18#include <sys/proc.h>
19#include <sys/conf.h>
20#include <sys/vnode.h>
21#include <sys/specdev.h>
22#include <sys/fifo.h>
23#include <sys/malloc.h>
24#include <sys/mount.h>
25
0a011bb1
KB
26#include <ufs/ufs/quota.h>
27#include <ufs/ufs/inode.h>
28#include <ufs/ufs/dir.h>
29#include <ufs/ufs/ufsmount.h>
34a084a9 30
0a011bb1
KB
31#include <ufs/lfs/lfs.h>
32#include <ufs/lfs/lfs_extern.h>
84c30241 33
dc7e45d3 34/* In-memory description of a segment about to be written. */
dc7e45d3 35struct segment {
a1b8db53
KB
36 struct buf **bpp; /* pointer to buffer array */
37 struct buf **cbpp; /* pointer to next available bp */
38 struct buf *ibp; /* buffer pointer to inode page */
39 struct finfo *fip; /* current fileinfo pointer */
dc7e45d3
KB
40 void *segsum; /* segment summary info */
41 u_long ninodes; /* number of inodes in this segment */
42 u_long seg_bytes_left; /* bytes left in segment */
43 u_long sum_bytes_left; /* bytes left in summary block */
44 u_long seg_number; /* number of this segment */
45#define SEGM_CKP 0x01 /* doing a checkpoint */
46 u_long seg_flags; /* run-time flags for this segment */
dc7e45d3
KB
47};
48
84c30241 49/*
dc7e45d3
KB
50 * Determine if it's OK to start a partial in this segment, or if we need
51 * to go on to a new segment.
8954e52c 52 */
dc7e45d3
KB
53#define LFS_PARTIAL_FITS(fs) \
54 ((fs)->lfs_dbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
55 1 << (fs)->lfs_fsbtodb)
56
a1b8db53
KB
57int lfs_callback __P((struct buf *));
58void lfs_gather __P((struct lfs *, struct segment *,
59 struct vnode *, int (*) __P((struct lfs *, struct buf *))));
60void lfs_initseg __P((struct lfs *, struct segment *));
61void lfs_iset __P((struct inode *, daddr_t, time_t));
62int lfs_match_data __P((struct lfs *, struct buf *));
63int lfs_match_dindir __P((struct lfs *, struct buf *));
64int lfs_match_indir __P((struct lfs *, struct buf *));
65int lfs_match_tindir __P((struct lfs *, struct buf *));
66struct buf *
67 lfs_newbuf __P((struct lfs *, struct segment *, daddr_t, size_t));
87804018 68void lfs_newseg __P((struct lfs *));
a1b8db53 69void lfs_shellsort __P((struct buf **, daddr_t *, register int));
87804018 70void lfs_updatemeta __P((struct lfs *,
a1b8db53
KB
71 struct segment *, struct vnode *, daddr_t *, struct buf **, int));
72void lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
73void lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
74void lfs_writeseg __P((struct lfs *, struct segment *));
75void lfs_writesuper __P((struct lfs *, struct segment *));
84c30241 76
dc7e45d3
KB
77int lfs_allclean_wakeup; /* Cleaner wakeup address. */
78
44819c56
KB
79/*
80 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
81 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
82 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
83 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
84 */
85
86int
87lfs_vflush(vp)
88 struct vnode *vp;
89{
90 struct inode *ip;
91 struct lfs *fs;
92 struct mount *mp;
93 struct segment *sp;
94 int error, s;
95
96#ifdef VERBOSE
97 printf("lfs_vflush\n");
98#endif
99 mp = vp->v_mount;
100 fs = VFSTOUFS(mp)->um_lfs;
101
102 /*
103 * XXX
104 * check flags?
105 * mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY) ||
106 */
107 if (vfs_busy(mp))
108 return (0);
109
110 /*
111 * Allocate a segment structure and enough space to hold pointers to
112 * the maximum possible number of buffers which can be described in a
113 * single summary block.
114 */
115 sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
116 sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
117 sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
118 sp->seg_flags = SEGM_CKP;
119 lfs_initseg(fs, sp);
120
121 /*
122 * Keep a cumulative count of the outstanding I/O operations. If the
123 * disk drive catches up with us it could go to zero before we finish,
124 * so we artificially increment it by one until we've scheduled all of
125 * the writes we intend to do.
126 */
127 s = splbio();
128 fs->lfs_iocount = 1;
129 splx(s);
130
131 if (vp->v_dirtyblkhd != NULL)
132 lfs_writefile(fs, sp, vp);
133 ip = VTOI(vp);
134 lfs_writeinode(fs, sp, ip);
135 ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
136
137 lfs_writeseg(fs, sp);
138
139 /*
140 * If the I/O count is non-zero, sleep until it reaches zero. At the
141 * moment, the user's process hangs around so we can sleep.
142 */
143 s = splbio();
144 if (--fs->lfs_iocount && (error =
b68b931c 145 tsleep((caddr_t)&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0)))
44819c56
KB
146 return (error);
147 splx(s);
148 vfs_unbusy(mp);
149
150 free(sp->bpp, M_SEGMENT);
151 free(sp, M_SEGMENT);
152
153 return (0);
154}
155
84c30241 156int
275ca4f0 157lfs_segwrite(mp, do_ckp)
a1b8db53 158 struct mount *mp;
dc7e45d3 159 int do_ckp; /* Do a checkpoint. */
84c30241 160{
a1b8db53 161 struct inode *ip;
0a011bb1 162 struct lfs *fs;
a1b8db53
KB
163 struct segment *sp;
164 struct vnode *vp;
44819c56 165 int error, islocked, s;
84c30241 166
dc7e45d3
KB
167#ifdef VERBOSE
168 printf("lfs_segwrite\n");
aa4dc149 169#endif
44819c56
KB
170 fs = VFSTOUFS(mp)->um_lfs;
171
172 /*
173 * Allocate a segment structure and enough space to hold pointers to
174 * the maximum possible number of buffers which can be described in a
175 * single summary block.
176 */
177 sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
178 sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
179 sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
180 sp->seg_flags = do_ckp ? SEGM_CKP : 0;
181 lfs_initseg(fs, sp);
a1b8db53 182
8954e52c 183 /*
dc7e45d3
KB
184 * If doing a checkpoint, we keep a cumulative count of the outstanding
185 * I/O operations. If the disk drive catches up with us it could go to
186 * zero before we finish, so we artificially increment it by one until
187 * we've scheduled all of the writes we intend to do.
8954e52c 188 */
dc7e45d3
KB
189 if (do_ckp) {
190 s = splbio();
191 fs->lfs_iocount = 1;
192 splx(s);
193 }
aa4dc149 194
44819c56 195loop: for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
84c30241
KB
196 /*
197 * If the vnode that we are about to sync is no longer
198 * associated with this mount point, start over.
199 */
84c30241
KB
200 if (vp->v_mount != mp)
201 goto loop;
dc7e45d3 202
44819c56 203 islocked = VOP_ISLOCKED(vp);
dc7e45d3 204
87804018
KB
205 /*
206 * XXX
207 * This is wrong, I think -- we should just wait until we
208 * get the vnode and go on. Probably going to reschedule
209 * all of the writes we already scheduled...
210 */
44819c56
KB
211 if (islocked)
212 VREF(vp);
213 else if (vget(vp))
a1b8db53
KB
214{
215printf("lfs_segment: failed to get vnode (tell Keith)!\n");
84c30241 216 goto loop;
a1b8db53 217}
44819c56
KB
218 /*
219 * Write the inode/file if dirty and it's not the
220 * the IFILE.
221 */
222 ip = VTOI(vp);
223 if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) ||
224 vp->v_dirtyblkhd != NULL) &&
225 ip->i_number != LFS_IFILE_INUM) {
226 if (vp->v_dirtyblkhd != NULL)
227 lfs_writefile(fs, sp, vp);
228 lfs_writeinode(fs, sp, ip);
229 ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
230 }
231 if (islocked)
232 vrele(vp);
233 else
234 vput(vp);
84c30241 235 }
dc7e45d3 236 if (do_ckp) {
87804018
KB
237 vp = fs->lfs_ivnode;
238 while (vget(vp));
239 ip = VTOI(vp);
240 if (vp->v_dirtyblkhd != NULL)
241 lfs_writefile(fs, sp, vp);
242 lfs_writeinode(fs, sp, ip);
243 ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
244 vput(vp);
dc7e45d3 245 }
aa4dc149
KB
246 lfs_writeseg(fs, sp);
247
275ca4f0 248 /*
dc7e45d3
KB
249 * If the I/O count is non-zero, sleep until it reaches zero. At the
250 * moment, the user's process hangs around so we can sleep.
275ca4f0 251 */
dc7e45d3
KB
252 if (do_ckp) {
253 s = splbio();
b68b931c
KM
254 if (--fs->lfs_iocount &&
255 (error = tsleep((caddr_t)&fs->lfs_iocount, PRIBIO + 1,
256 "lfs sync", 0)))
12304d41 257 return (error);
dc7e45d3
KB
258 splx(s);
259 lfs_writesuper(fs, sp);
260 }
275ca4f0 261
c222b129
KB
262 free(sp->bpp, M_SEGMENT);
263 free(sp, M_SEGMENT);
275ca4f0 264
dc7e45d3 265 return (0);
84c30241
KB
266}
267
dc7e45d3
KB
268/*
269 * Write the dirty blocks associated with a vnode.
270 */
87804018 271void
dc7e45d3 272lfs_writefile(fs, sp, vp)
0a011bb1 273 struct lfs *fs;
a1b8db53
KB
274 struct segment *sp;
275 struct vnode *vp;
84c30241 276{
dc7e45d3 277 struct buf *bp;
a1b8db53 278 struct finfo *fip;
dc7e45d3 279 IFILE *ifp;
275ca4f0 280
dc7e45d3
KB
281#ifdef VERBOSE
282 printf("lfs_writefile\n");
283#endif
a1b8db53
KB
284 if (sp->seg_bytes_left < fs->lfs_bsize ||
285 sp->sum_bytes_left < sizeof(struct finfo)) {
286 lfs_writeseg(fs, sp);
287 lfs_initseg(fs, sp);
288 }
289 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
84c30241 290
a1b8db53
KB
291 fip = sp->fip;
292 fip->fi_nblocks = 0;
293 fip->fi_ino = VTOI(vp)->i_number;
294 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
295 fip->fi_version = ifp->if_version;
296 brelse(bp);
297
298 /*
299 * It may not be necessary to write the meta-data blocks at this point,
300 * as the roll-forward recovery code should be able to reconstruct the
301 * list.
302 */
303 lfs_gather(fs, sp, vp, lfs_match_data);
304 lfs_gather(fs, sp, vp, lfs_match_indir);
305 lfs_gather(fs, sp, vp, lfs_match_dindir);
dc7e45d3 306#ifdef TRIPLE
a1b8db53 307 lfs_gather(fs, sp, vp, lfs_match_tindir);
dc7e45d3 308#endif
aa4dc149 309
a1b8db53 310 fip = sp->fip;
dc7e45d3 311#ifdef META
a1b8db53 312 printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks);
dc7e45d3 313#endif
a1b8db53
KB
314 if (fip->fi_nblocks != 0) {
315 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
316 sp->fip =
317 (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
318 sizeof(daddr_t) * (fip->fi_nblocks - 1));
9a46ddb2
CS
319 } else
320 sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
12304d41
KB
321}
322
87804018 323void
12304d41
KB
324lfs_writeinode(fs, sp, ip)
325 struct lfs *fs;
a1b8db53
KB
326 struct segment *sp;
327 struct inode *ip;
12304d41 328{
a1b8db53 329 struct buf *bp, *ibp;
87804018 330 IFILE *ifp;
9a46ddb2
CS
331 SEGUSE *sup;
332 daddr_t daddr;
87804018 333 ino_t ino;
12304d41
KB
334 int ndx;
335
336#ifdef VERBOSE
337 printf("lfs_writeinode\n");
338#endif
339 /* Allocate a new inode block if necessary. */
340 if (sp->ibp == NULL) {
341 /* Allocate a new segment if necessary. */
342 if (sp->seg_bytes_left < fs->lfs_bsize ||
343 sp->sum_bytes_left < sizeof(daddr_t)) {
344 lfs_writeseg(fs, sp);
345 lfs_initseg(fs, sp);
346 }
347
348 /* Get next inode block. */
9a46ddb2 349 daddr = fs->lfs_offset;
12304d41
KB
350 fs->lfs_offset += fsbtodb(fs, 1);
351 sp->ibp = *sp->cbpp++ =
9a46ddb2 352 lfs_newbuf(fs, sp, daddr, fs->lfs_bsize);
12304d41
KB
353
354 /* Set remaining space counter. */
355 sp->seg_bytes_left -= fs->lfs_bsize;
356 sp->sum_bytes_left -= sizeof(daddr_t);
87804018 357 ndx = LFS_SUMMARY_SIZE / sizeof(daddr_t) -
12304d41 358 sp->ninodes / INOPB(fs) - 1;
9a46ddb2 359 ((daddr_t *)(sp->segsum))[ndx] = daddr;
12304d41
KB
360 }
361
a1b8db53 362 /* Update the inode times and copy the inode onto the inode page. */
87804018 363 ITIMES(ip, &time, &time);
12304d41 364 bp = sp->ibp;
a1b8db53 365 bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
12304d41
KB
366
367 /* Increment inode count in segment summary block. */
368 ++((SEGSUM *)(sp->segsum))->ss_ninos;
369
370 /* If this page is full, set flag to allocate a new page. */
371 if (++sp->ninodes % INOPB(fs) == 0)
372 sp->ibp = NULL;
373
374 /*
87804018
KB
375 * If updating the ifile, update the super-block. Update the disk
376 * address and access times for this inode in the ifile.
12304d41 377 */
87804018
KB
378 ino = ip->i_number;
379 if (ino == LFS_IFILE_INUM)
12304d41 380 fs->lfs_idaddr = bp->b_blkno;
87804018
KB
381
382 LFS_IENTRY(ifp, fs, ino, ibp);
9a46ddb2 383 daddr = ifp->if_daddr;
87804018 384 ifp->if_daddr = bp->b_blkno;
a1b8db53 385 LFS_UBWRITE(ibp);
9a46ddb2
CS
386
387 if (daddr != LFS_UNUSED_DADDR) {
388 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
389#ifdef DIAGNOSTIC
390 if (sup->su_nbytes < sizeof(struct dinode))
391 panic("lfs: negative bytes (segment %d)\n",
392 datosn(fs, daddr));
393#endif
394 sup->su_nbytes -= sizeof(struct dinode);
395 LFS_UBWRITE(bp);
396 }
275ca4f0
KB
397}
398
87804018 399void
275ca4f0 400lfs_gather(fs, sp, vp, match)
0a011bb1 401 struct lfs *fs;
a1b8db53
KB
402 struct segment *sp;
403 struct vnode *vp;
404 int (*match) __P((struct lfs *, struct buf *));
275ca4f0 405{
a1b8db53
KB
406 struct buf **bpp, *bp, *nbp;
407 struct finfo *fip;
408 struct inode *ip;
275ca4f0 409 daddr_t *lbp, *start_lbp;
aa4dc149
KB
410 u_long version;
411 int s;
275ca4f0 412
dc7e45d3
KB
413#ifdef VERBOSE
414 printf("lfs_gather\n");
415#endif
275ca4f0
KB
416 ip = VTOI(vp);
417 bpp = sp->cbpp;
418 fip = sp->fip;
275ca4f0 419 start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks];
275ca4f0
KB
420
421 s = splbio();
422 for (bp = vp->v_dirtyblkhd; bp; bp = nbp) {
423 nbp = bp->b_blockf;
12304d41
KB
424 /*
425 * XXX
9a46ddb2 426 * Should sleep on any BUSY buffer if doing an fsync?
12304d41 427 */
44819c56 428 if (bp->b_flags & B_BUSY || !match(fs, bp))
275ca4f0 429 continue;
aa4dc149 430#ifdef DIAGNOSTIC
dc7e45d3 431 if (!(bp->b_flags & B_DELWRI))
12304d41 432 panic("lfs_gather: bp not B_DELWRI");
dc7e45d3 433 if (!(bp->b_flags & B_LOCKED))
12304d41 434 panic("lfs_gather: bp not B_LOCKED");
aa4dc149 435#endif
aa4dc149 436 /*
dc7e45d3
KB
437 * If full, finish this segment. We may be doing I/O, so
438 * release and reacquire the splbio().
aa4dc149
KB
439 */
440 if (sp->sum_bytes_left < sizeof(daddr_t) ||
275ca4f0 441 sp->seg_bytes_left < fs->lfs_bsize) {
275ca4f0 442 splx(s);
aa4dc149 443 lfs_updatemeta(fs,
dc7e45d3 444 sp, vp, start_lbp, bpp, lbp - start_lbp);
275ca4f0 445
aa4dc149
KB
446 /* Add the current file to the segment summary. */
447 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
275ca4f0 448
aa4dc149 449 version = fip->fi_version;
dc7e45d3 450 lfs_writeseg(fs, sp);
12304d41 451 lfs_initseg(fs, sp);
aa4dc149 452
275ca4f0 453 fip = sp->fip;
275ca4f0 454 fip->fi_version = version;
aa4dc149 455 fip->fi_ino = ip->i_number;
275ca4f0 456 start_lbp = lbp = fip->fi_blocks;
aa4dc149 457
9a46ddb2
CS
458 sp->sum_bytes_left -=
459 sizeof(struct finfo) - sizeof(daddr_t);
460
aa4dc149 461 bpp = sp->cbpp;
275ca4f0
KB
462 s = splbio();
463 }
9a46ddb2
CS
464
465 /* Insert into the buffer list, update the FINFO block. */
466 *sp->cbpp++ = bp;
467 ++fip->fi_nblocks;
468 *lbp++ = bp->b_lblkno;
469
470 sp->sum_bytes_left -= sizeof(daddr_t);
471 sp->seg_bytes_left -= bp->b_bufsize;
84c30241 472 }
275ca4f0 473 splx(s);
dc7e45d3 474 lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp);
84c30241
KB
475}
476
84c30241 477/*
aa4dc149 478 * Update the metadata that points to the blocks listed in the FINFO
84c30241
KB
479 * array.
480 */
87804018 481void
dc7e45d3 482lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks)
0a011bb1 483 struct lfs *fs;
a1b8db53
KB
484 struct segment *sp;
485 struct vnode *vp;
275ca4f0 486 daddr_t *lbp;
a1b8db53 487 struct buf **bpp;
275ca4f0 488 int nblocks;
84c30241 489{
12304d41 490 SEGUSE *sup;
a1b8db53 491 struct buf *bp;
dc7e45d3 492 INDIR a[NIADDR], *ap;
a1b8db53 493 struct inode *ip;
12304d41 494 daddr_t daddr, lbn, off;
dc7e45d3 495 int db_per_fsb, error, i, num;
84c30241 496
dc7e45d3
KB
497#ifdef VERBOSE
498 printf("lfs_updatemeta\n");
499#endif
aa4dc149 500 if (nblocks == 0)
275ca4f0
KB
501 return;
502
12304d41 503 /* Sort the blocks. */
87804018 504 lfs_shellsort(bpp, lbp, nblocks);
275ca4f0 505
12304d41
KB
506 /*
507 * Assign disk addresses, and update references to the logical
508 * block and the segment usage information.
509 */
dc7e45d3 510 db_per_fsb = fsbtodb(fs, 1);
12304d41
KB
511 for (i = nblocks; i--; ++bpp) {
512 lbn = *lbp++;
513 (*bpp)->b_blkno = off = fs->lfs_offset;
dc7e45d3 514 fs->lfs_offset += db_per_fsb;
275ca4f0 515
dc7e45d3 516 if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num))
a1b8db53 517 panic("lfs_updatemeta: lfs_bmaparray %d", error);
dc7e45d3
KB
518 ip = VTOI(vp);
519 switch (num) {
520 case 0:
12304d41 521 ip->i_db[lbn] = off;
dc7e45d3
KB
522 break;
523 case 1:
12304d41 524 ip->i_ib[a[0].in_off] = off;
dc7e45d3
KB
525 break;
526 default:
527 ap = &a[num - 1];
dc7e45d3
KB
528 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
529 panic("lfs_updatemeta: bread bno %d",
530 ap->in_lbn);
12304d41
KB
531 bp->b_un.b_daddr[ap->in_off] = off;
532 lfs_bwrite(bp);
533 }
534
535 /* Update segment usage information. */
536 if (daddr != UNASSIGNED) {
537 LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
12304d41
KB
538#ifdef DIAGNOSTIC
539 if (sup->su_nbytes < fs->lfs_bsize)
540 panic("lfs: negative bytes (segment %d)\n",
541 datosn(fs, daddr));
542#endif
543 sup->su_nbytes -= fs->lfs_bsize;
a1b8db53 544 LFS_UBWRITE(bp);
84c30241 545 }
84c30241 546 }
84c30241
KB
547}
548
12304d41
KB
549/*
550 * Start a new segment.
551 */
87804018 552void
12304d41 553lfs_initseg(fs, sp)
0a011bb1 554 struct lfs *fs;
a1b8db53 555 struct segment *sp;
84c30241 556{
12304d41
KB
557 SEGUSE *sup;
558 SEGSUM *ssp;
559 struct buf *bp;
560 daddr_t lbn, *lbnp;
275ca4f0 561
dc7e45d3 562#ifdef VERBOSE
12304d41 563 printf("lfs_initseg\n");
dc7e45d3 564#endif
12304d41 565 /* Advance to the next segment. */
c222b129 566 if (!LFS_PARTIAL_FITS(fs)) {
9a46ddb2
CS
567 /* Wake up any cleaning procs waiting on this file system. */
568 wakeup((caddr_t)&fs->lfs_nextseg);
569 wakeup((caddr_t)&lfs_allclean_wakeup);
570
c222b129
KB
571 lfs_newseg(fs);
572 fs->lfs_offset = fs->lfs_curseg;
12304d41
KB
573 sp->seg_number = datosn(fs, fs->lfs_curseg);
574 sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
575
576 /*
c222b129
KB
577 * If the segment contains a superblock, update the offset
578 * and summary address to skip over it.
12304d41 579 */
87804018 580 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
c222b129 581 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
12304d41
KB
582 fs->lfs_offset += LFS_SBPAD / DEV_BSIZE;
583 sp->seg_bytes_left -= LFS_SBPAD;
275ca4f0 584 }
a1b8db53 585 brelse(bp);
12304d41
KB
586 } else {
587 sp->seg_number = datosn(fs, fs->lfs_curseg);
588 sp->seg_bytes_left = (fs->lfs_dbpseg -
589 (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
590 }
aa4dc149 591
12304d41
KB
592 sp->ibp = NULL;
593 sp->ninodes = 0;
aa4dc149 594
12304d41
KB
595 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
596 sp->cbpp = sp->bpp;
597 *sp->cbpp = lfs_newbuf(fs, sp, fs->lfs_offset, LFS_SUMMARY_SIZE);
598 sp->segsum = (*sp->cbpp)->b_un.b_addr;
599 ++sp->cbpp;
600 fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
aa4dc149 601
12304d41
KB
602 /* Set point to SEGSUM, initialize it. */
603 ssp = sp->segsum;
604 ssp->ss_next = fs->lfs_nextseg;
12304d41 605 ssp->ss_nfinfo = ssp->ss_ninos = 0;
aa4dc149 606
12304d41 607 /* Set pointer to first FINFO, initialize it. */
a1b8db53 608 sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
12304d41 609 sp->fip->fi_nblocks = 0;
aa4dc149 610
12304d41
KB
611 sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
612 sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
613}
aa4dc149 614
12304d41
KB
615/*
616 * Return the next segment to write.
617 */
87804018 618void
12304d41
KB
619lfs_newseg(fs)
620 struct lfs *fs;
621{
c222b129 622 CLEANERINFO *cip;
12304d41
KB
623 SEGUSE *sup;
624 struct buf *bp;
c222b129 625 int curseg, isdirty, sn;
12304d41
KB
626
627#ifdef VERBOSE
628 printf("lfs_newseg\n");
629#endif
c222b129
KB
630 /*
631 * Turn off the active bit for the current segment, turn on the
632 * active and dirty bits for the next segment, update the cleaner
633 * info. Set the current segment to the next segment, get a new
634 * next segment.
635 */
636 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp);
637 sup->su_flags &= ~SEGUSE_ACTIVE;
a1b8db53 638 LFS_UBWRITE(bp);
c222b129
KB
639
640 LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
641 sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY;
a1b8db53 642 LFS_UBWRITE(bp);
c222b129
KB
643
644 LFS_CLEANERINFO(cip, fs, bp);
645 --cip->clean;
646 ++cip->dirty;
a1b8db53 647 LFS_UBWRITE(bp);
c222b129
KB
648
649 fs->lfs_lastseg = fs->lfs_curseg;
650 fs->lfs_curseg = fs->lfs_nextseg;
651 for (sn = curseg = datosn(fs, fs->lfs_curseg);;) {
12304d41 652 sn = (sn + 1) % fs->lfs_nseg;
c222b129 653 if (sn == curseg)
12304d41
KB
654 panic("lfs_nextseg: no clean segments");
655 LFS_SEGENTRY(sup, fs, sn, bp);
656 isdirty = sup->su_flags & SEGUSE_DIRTY;
a1b8db53 657 brelse(bp);
12304d41
KB
658 if (!isdirty)
659 break;
660 }
c222b129 661 fs->lfs_nextseg = sntoda(fs, sn);
84c30241
KB
662}
663
87804018 664void
84c30241 665lfs_writeseg(fs, sp)
0a011bb1 666 struct lfs *fs;
a1b8db53 667 struct segment *sp;
84c30241 668{
a1b8db53 669 struct buf **bpp, *bp;
84c30241 670 SEGUSE *sup;
a1b8db53 671 SEGSUM *ssp;
dc7e45d3
KB
672 dev_t i_dev;
673 u_long *datap, *dp;
aa4dc149 674 void *pmeta;
a1b8db53 675 int flags, i, nblocks, s, (*strategy)__P((struct buf *));
84c30241 676
dc7e45d3
KB
677#ifdef VERBOSE
678 printf("lfs_writeseg\n");
679#endif
a1b8db53
KB
680 if ((nblocks = sp->cbpp - sp->bpp) == 0)
681 return;
682
84c30241 683 /*
a1b8db53
KB
684 * Compute checksum across data and then across summary; the first
685 * block (the summary block) is skipped. Set the create time here
686 * so that it's guaranteed to be later than the inode mod times.
dc7e45d3
KB
687 *
688 * XXX
689 * Fix this to do it inline, instead of malloc/copy.
84c30241 690 */
dc7e45d3 691 datap = dp = malloc(nblocks * sizeof(u_long), M_SEGMENT, M_WAITOK);
12304d41
KB
692 for (bpp = sp->bpp, i = nblocks - 1; i--;)
693 *dp++ = (*++bpp)->b_un.b_words[0];
a1b8db53 694 ssp = (SEGSUM *)sp->segsum;
89bed312 695 ssp->ss_create = time.tv_sec;
a1b8db53
KB
696 ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long));
697 ssp->ss_sumsum =
698 cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
c222b129 699 free(datap, M_SEGMENT);
8954e52c 700
dc7e45d3
KB
701 /*
702 * When we gathered the blocks for I/O we did not mark them busy or
703 * remove them from the freelist. As we do this, turn off the B_LOCKED
704 * bit so the future brelse will put them on the LRU list, and add the
705 * B_CALL flags if we're doing a checkpoint so we can count I/O's. LFS
706 * requires that the super blocks (on checkpoint) be written after all
707 * the segment data.
708 */
709 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
710 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy;
275ca4f0 711
8954e52c 712 s = splbio();
dc7e45d3
KB
713 if (sp->seg_flags & SEGM_CKP) {
714 fs->lfs_iocount += nblocks;
12304d41 715 flags = B_ASYNC | B_BUSY | B_CALL;
dc7e45d3 716 } else
12304d41 717 flags = B_ASYNC | B_BUSY;
dc7e45d3
KB
718 for (bpp = sp->bpp, i = nblocks; i--;) {
719 bp = *bpp++;
720 bp->b_flags |= flags;
12304d41
KB
721 bp->b_flags &=
722 ~(B_DONE | B_ERROR | B_READ | B_DELWRI | B_LOCKED);
dc7e45d3
KB
723 bp->b_dev = i_dev;
724 bp->b_iodone = lfs_callback;
dc7e45d3
KB
725 if (!(bp->b_flags & B_NOCACHE)) {
726 bremfree(bp);
727 reassignbuf(bp, bp->b_vp);
728 }
8954e52c 729 }
dc7e45d3 730 splx(s);
87804018 731
dc7e45d3
KB
732 for (bpp = sp->bpp, i = nblocks; i--;)
733 (strategy)(*bpp++);
9a46ddb2
CS
734
735 /* Update the segment usage information. */
736 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
737 sup->su_nbytes += nblocks - 1 -
738 (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift;
739 sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
740 sup->su_lastmod = time.tv_sec;
741 LFS_UBWRITE(bp);
275ca4f0
KB
742}
743
87804018 744void
dc7e45d3 745lfs_writesuper(fs, sp)
0a011bb1 746 struct lfs *fs;
a1b8db53 747 struct segment *sp;
275ca4f0 748{
a1b8db53 749 struct buf *bp;
dc7e45d3 750 dev_t i_dev;
a1b8db53 751 int (*strategy) __P((struct buf *));
275ca4f0 752
dc7e45d3
KB
753#ifdef VERBOSE
754 printf("lfs_writesuper\n");
755#endif
dc7e45d3
KB
756 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
757 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op->vop_strategy;
14712628 758
aa4dc149 759 /* Checksum the superblock and copy it into a buffer. */
0a011bb1 760 fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
dc7e45d3
KB
761 bp = lfs_newbuf(fs, sp, fs->lfs_sboffs[0], LFS_SBPAD);
762 *bp->b_un.b_lfs = *fs;
275ca4f0 763
14712628 764 /* Write the first superblock (wait). */
dc7e45d3 765 bp->b_dev = i_dev;
dc7e45d3 766 bp->b_flags |= B_BUSY;
12304d41 767 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
aa4dc149 768 (strategy)(bp);
275ca4f0 769 biowait(bp);
aa4dc149 770
14712628 771 /* Write the second superblock (don't wait). */
275ca4f0 772 bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1];
12304d41 773 bp->b_flags |= B_ASYNC | B_BUSY;
dc7e45d3 774 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
aa4dc149 775 (strategy)(bp);
275ca4f0
KB
776}
777
aa4dc149
KB
778/*
779 * Logical block number match routines used when traversing the dirty block
780 * chain.
781 */
87804018
KB
782int
783lfs_match_data(fs, bp)
dc7e45d3 784 struct lfs *fs;
a1b8db53 785 struct buf *bp;
275ca4f0 786{
aa4dc149 787 return (bp->b_lblkno >= 0);
275ca4f0
KB
788}
789
87804018
KB
790int
791lfs_match_indir(fs, bp)
dc7e45d3 792 struct lfs *fs;
a1b8db53 793 struct buf *bp;
275ca4f0 794{
dc7e45d3
KB
795 int lbn;
796
797 lbn = bp->b_lblkno;
798 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
275ca4f0
KB
799}
800
87804018
KB
801int
802lfs_match_dindir(fs, bp)
dc7e45d3 803 struct lfs *fs;
a1b8db53 804 struct buf *bp;
275ca4f0 805{
dc7e45d3
KB
806 int lbn;
807
808 lbn = bp->b_lblkno;
809 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
aa4dc149
KB
810}
811
87804018
KB
812int
813lfs_match_tindir(fs, bp)
0a011bb1 814 struct lfs *fs;
a1b8db53 815 struct buf *bp;
aa4dc149 816{
dc7e45d3 817 int lbn;
aa4dc149 818
dc7e45d3
KB
819 lbn = bp->b_lblkno;
820 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
821}
aa4dc149 822
dc7e45d3
KB
823/*
824 * Allocate a new buffer header.
825 */
a1b8db53 826struct buf *
dc7e45d3
KB
827lfs_newbuf(fs, sp, daddr, size)
828 struct lfs *fs;
a1b8db53 829 struct segment *sp;
dc7e45d3
KB
830 daddr_t daddr;
831 size_t size;
832{
a1b8db53 833 struct buf *bp;
aa4dc149 834
dc7e45d3
KB
835#ifdef VERBOSE
836 printf("lfs_newbuf\n");
837#endif
838 bp = getnewbuf();
839 bremhash(bp);
840 bgetvp(fs->lfs_ivnode, bp);
841 bp->b_bcount = 0;
842 bp->b_lblkno = daddr;
843 bp->b_blkno = daddr;
844 bp->b_error = 0;
845 bp->b_resid = 0;
846 allocbuf(bp, size);
847 bp->b_flags |= B_NOCACHE;
12304d41 848 binshash(bp, &bfreelist[BQ_AGE]);
dc7e45d3
KB
849 return (bp);
850}
aa4dc149 851
87804018 852int /* XXX should be void */
dc7e45d3 853lfs_callback(bp)
a1b8db53 854 struct buf *bp;
dc7e45d3
KB
855{
856 struct lfs *fs;
aa4dc149 857
dc7e45d3
KB
858 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
859#ifdef DIAGNOSTIC
860 if (fs->lfs_iocount == 0)
861 panic("lfs_callback: zero iocount\n");
862#endif
863 if (--fs->lfs_iocount == 0)
b68b931c 864 wakeup((caddr_t)&fs->lfs_iocount);
12304d41 865
dc7e45d3 866 brelse(bp);
84c30241
KB
867}
868
869/*
870 * Shellsort (diminishing increment sort) from Data Structures and
871 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
872 * see also Knuth Vol. 3, page 84. The increments are selected from
873 * formula (8), page 95. Roughly O(N^3/2).
874 */
875/*
876 * This is our own private copy of shellsort because we want to sort
877 * two parallel arrays (the array of buffer pointers and the array of
878 * logical block numbers) simultaneously. Note that we cast the array
879 * of logical block numbers to a unsigned in this routine so that the
880 * negative block numbers (meta data blocks) sort AFTER the data blocks.
881 */
87804018
KB
882void
883lfs_shellsort(bp_array, lb_array, nmemb)
a1b8db53 884 struct buf **bp_array;
275ca4f0 885 daddr_t *lb_array;
84c30241
KB
886 register int nmemb;
887{
888 static int __rsshell_increments[] = { 4, 1, 0 };
889 register int incr, *incrp, t1, t2;
a1b8db53 890 struct buf *bp_temp;
84c30241
KB
891 u_long lb_temp;
892
893 for (incrp = __rsshell_increments; incr = *incrp++;)
894 for (t1 = incr; t1 < nmemb; ++t1)
895 for (t2 = t1 - incr; t2 >= 0;)
896 if (lb_array[t2] > lb_array[t2 + incr]) {
897 lb_temp = lb_array[t2];
898 lb_array[t2] = lb_array[t2 + incr];
899 lb_array[t2 + incr] = lb_temp;
900 bp_temp = bp_array[t2];
901 bp_array[t2] = bp_array[t2 + incr];
902 bp_array[t2 + incr] = bp_temp;
903 t2 -= incr;
904 } else
905 break;
906}