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