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