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