segwrite.c revision 1.15 1 1.15 christos /* $NetBSD: segwrite.c,v 1.15 2006/11/09 19:36:36 christos Exp $ */
2 1.1 perseant /*-
3 1.1 perseant * Copyright (c) 2003 The NetBSD Foundation, Inc.
4 1.1 perseant * All rights reserved.
5 1.1 perseant *
6 1.1 perseant * This code is derived from software contributed to The NetBSD Foundation
7 1.1 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
8 1.1 perseant *
9 1.1 perseant * Redistribution and use in source and binary forms, with or without
10 1.1 perseant * modification, are permitted provided that the following conditions
11 1.1 perseant * are met:
12 1.1 perseant * 1. Redistributions of source code must retain the above copyright
13 1.1 perseant * notice, this list of conditions and the following disclaimer.
14 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 perseant * notice, this list of conditions and the following disclaimer in the
16 1.1 perseant * documentation and/or other materials provided with the distribution.
17 1.1 perseant * 3. All advertising materials mentioning features or use of this software
18 1.1 perseant * must display the following acknowledgement:
19 1.1 perseant * This product includes software developed by the NetBSD
20 1.1 perseant * Foundation, Inc. and its contributors.
21 1.1 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 perseant * contributors may be used to endorse or promote products derived
23 1.1 perseant * from this software without specific prior written permission.
24 1.1 perseant *
25 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 perseant * POSSIBILITY OF SUCH DAMAGE.
36 1.1 perseant */
37 1.1 perseant /*
38 1.1 perseant * Copyright (c) 1991, 1993
39 1.1 perseant * The Regents of the University of California. All rights reserved.
40 1.1 perseant *
41 1.1 perseant * Redistribution and use in source and binary forms, with or without
42 1.1 perseant * modification, are permitted provided that the following conditions
43 1.1 perseant * are met:
44 1.1 perseant * 1. Redistributions of source code must retain the above copyright
45 1.1 perseant * notice, this list of conditions and the following disclaimer.
46 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
47 1.1 perseant * notice, this list of conditions and the following disclaimer in the
48 1.1 perseant * documentation and/or other materials provided with the distribution.
49 1.5 agc * 3. Neither the name of the University nor the names of its contributors
50 1.1 perseant * may be used to endorse or promote products derived from this software
51 1.1 perseant * without specific prior written permission.
52 1.1 perseant *
53 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.1 perseant * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.1 perseant * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.1 perseant * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.1 perseant * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.1 perseant * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.1 perseant * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.1 perseant * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.1 perseant * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.1 perseant * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.1 perseant * SUCH DAMAGE.
64 1.1 perseant *
65 1.1 perseant * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
66 1.1 perseant */
67 1.1 perseant
68 1.1 perseant /*
69 1.1 perseant * Partial segment writer, taken from the kernel and adapted for userland.
70 1.1 perseant */
71 1.1 perseant #include <sys/types.h>
72 1.1 perseant #include <sys/param.h>
73 1.1 perseant #include <sys/time.h>
74 1.1 perseant #include <sys/buf.h>
75 1.1 perseant #include <sys/mount.h>
76 1.1 perseant
77 1.1 perseant #include <ufs/ufs/inode.h>
78 1.1 perseant #include <ufs/ufs/ufsmount.h>
79 1.1 perseant
80 1.1 perseant /* Override certain things to make <ufs/lfs/lfs.h> work */
81 1.1 perseant #define vnode uvnode
82 1.1 perseant #define buf ubuf
83 1.1 perseant #define panic call_panic
84 1.1 perseant
85 1.1 perseant #include <ufs/lfs/lfs.h>
86 1.1 perseant
87 1.1 perseant #include <assert.h>
88 1.1 perseant #include <stdio.h>
89 1.1 perseant #include <stdlib.h>
90 1.1 perseant #include <string.h>
91 1.1 perseant #include <err.h>
92 1.1 perseant #include <errno.h>
93 1.15 christos #include <util.h>
94 1.1 perseant
95 1.1 perseant #include "bufcache.h"
96 1.1 perseant #include "vnode.h"
97 1.10 christos #include "lfs_user.h"
98 1.1 perseant #include "segwrite.h"
99 1.1 perseant
100 1.1 perseant /* Compatibility definitions */
101 1.1 perseant extern off_t locked_queue_bytes;
102 1.1 perseant int locked_queue_count;
103 1.1 perseant off_t written_bytes = 0;
104 1.1 perseant off_t written_data = 0;
105 1.1 perseant off_t written_indir = 0;
106 1.1 perseant off_t written_dev = 0;
107 1.1 perseant int written_inodes = 0;
108 1.1 perseant
109 1.1 perseant /* Global variables */
110 1.1 perseant time_t write_time;
111 1.1 perseant
112 1.1 perseant extern u_int32_t cksum(void *, size_t);
113 1.1 perseant extern u_int32_t lfs_sb_cksum(struct dlfs *);
114 1.7 perseant extern int preen;
115 1.1 perseant
116 1.1 perseant /*
117 1.1 perseant * Logical block number match routines used when traversing the dirty block
118 1.1 perseant * chain.
119 1.1 perseant */
120 1.1 perseant int
121 1.1 perseant lfs_match_data(struct lfs * fs, struct ubuf * bp)
122 1.1 perseant {
123 1.1 perseant return (bp->b_lblkno >= 0);
124 1.1 perseant }
125 1.1 perseant
126 1.1 perseant int
127 1.1 perseant lfs_match_indir(struct lfs * fs, struct ubuf * bp)
128 1.1 perseant {
129 1.1 perseant daddr_t lbn;
130 1.1 perseant
131 1.1 perseant lbn = bp->b_lblkno;
132 1.1 perseant return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
133 1.1 perseant }
134 1.1 perseant
135 1.1 perseant int
136 1.1 perseant lfs_match_dindir(struct lfs * fs, struct ubuf * bp)
137 1.1 perseant {
138 1.1 perseant daddr_t lbn;
139 1.1 perseant
140 1.1 perseant lbn = bp->b_lblkno;
141 1.1 perseant return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
142 1.1 perseant }
143 1.1 perseant
144 1.1 perseant int
145 1.1 perseant lfs_match_tindir(struct lfs * fs, struct ubuf * bp)
146 1.1 perseant {
147 1.1 perseant daddr_t lbn;
148 1.1 perseant
149 1.1 perseant lbn = bp->b_lblkno;
150 1.1 perseant return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
151 1.1 perseant }
152 1.1 perseant
153 1.1 perseant /*
154 1.1 perseant * Do a checkpoint.
155 1.1 perseant */
156 1.1 perseant int
157 1.1 perseant lfs_segwrite(struct lfs * fs, int flags)
158 1.1 perseant {
159 1.1 perseant struct inode *ip;
160 1.1 perseant struct segment *sp;
161 1.1 perseant struct uvnode *vp;
162 1.1 perseant int redo;
163 1.1 perseant
164 1.1 perseant lfs_seglock(fs, flags | SEGM_CKP);
165 1.1 perseant sp = fs->lfs_sp;
166 1.1 perseant
167 1.1 perseant lfs_writevnodes(fs, sp, VN_REG);
168 1.1 perseant lfs_writevnodes(fs, sp, VN_DIROP);
169 1.1 perseant ((SEGSUM *) (sp->segsum))->ss_flags &= ~(SS_CONT);
170 1.1 perseant
171 1.1 perseant do {
172 1.1 perseant vp = fs->lfs_ivnode;
173 1.1 perseant fs->lfs_flags &= ~LFS_IFDIRTY;
174 1.1 perseant ip = VTOI(vp);
175 1.7 perseant if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL || fs->lfs_idaddr <= 0)
176 1.1 perseant lfs_writefile(fs, sp, vp);
177 1.1 perseant
178 1.1 perseant redo = lfs_writeinode(fs, sp, ip);
179 1.1 perseant redo += lfs_writeseg(fs, sp);
180 1.1 perseant redo += (fs->lfs_flags & LFS_IFDIRTY);
181 1.1 perseant } while (redo);
182 1.1 perseant
183 1.1 perseant lfs_segunlock(fs);
184 1.1 perseant #if 0
185 1.1 perseant printf("wrote %" PRId64 " bytes (%" PRId32 " fsb)\n",
186 1.1 perseant written_bytes, (ufs_daddr_t)btofsb(fs, written_bytes));
187 1.1 perseant printf("wrote %" PRId64 " bytes data (%" PRId32 " fsb)\n",
188 1.1 perseant written_data, (ufs_daddr_t)btofsb(fs, written_data));
189 1.1 perseant printf("wrote %" PRId64 " bytes indir (%" PRId32 " fsb)\n",
190 1.1 perseant written_indir, (ufs_daddr_t)btofsb(fs, written_indir));
191 1.1 perseant printf("wrote %" PRId64 " bytes dev (%" PRId32 " fsb)\n",
192 1.1 perseant written_dev, (ufs_daddr_t)btofsb(fs, written_dev));
193 1.1 perseant printf("wrote %d inodes (%" PRId32 " fsb)\n",
194 1.1 perseant written_inodes, btofsb(fs, written_inodes * fs->lfs_ibsize));
195 1.1 perseant #endif
196 1.1 perseant return 0;
197 1.1 perseant }
198 1.1 perseant
199 1.1 perseant /*
200 1.1 perseant * Write the dirty blocks associated with a vnode.
201 1.1 perseant */
202 1.1 perseant void
203 1.1 perseant lfs_writefile(struct lfs * fs, struct segment * sp, struct uvnode * vp)
204 1.1 perseant {
205 1.1 perseant struct ubuf *bp;
206 1.1 perseant struct finfo *fip;
207 1.1 perseant struct inode *ip;
208 1.1 perseant IFILE *ifp;
209 1.1 perseant
210 1.1 perseant ip = VTOI(vp);
211 1.1 perseant
212 1.1 perseant if (sp->seg_bytes_left < fs->lfs_bsize ||
213 1.1 perseant sp->sum_bytes_left < sizeof(struct finfo))
214 1.1 perseant (void) lfs_writeseg(fs, sp);
215 1.1 perseant
216 1.1 perseant sp->sum_bytes_left -= FINFOSIZE;
217 1.1 perseant ++((SEGSUM *) (sp->segsum))->ss_nfinfo;
218 1.1 perseant
219 1.1 perseant if (vp->v_flag & VDIROP)
220 1.1 perseant ((SEGSUM *) (sp->segsum))->ss_flags |= (SS_DIROP | SS_CONT);
221 1.1 perseant
222 1.1 perseant fip = sp->fip;
223 1.1 perseant fip->fi_nblocks = 0;
224 1.1 perseant fip->fi_ino = ip->i_number;
225 1.1 perseant LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
226 1.1 perseant fip->fi_version = ifp->if_version;
227 1.1 perseant brelse(bp);
228 1.1 perseant
229 1.1 perseant lfs_gather(fs, sp, vp, lfs_match_data);
230 1.1 perseant lfs_gather(fs, sp, vp, lfs_match_indir);
231 1.1 perseant lfs_gather(fs, sp, vp, lfs_match_dindir);
232 1.1 perseant lfs_gather(fs, sp, vp, lfs_match_tindir);
233 1.1 perseant
234 1.1 perseant fip = sp->fip;
235 1.1 perseant if (fip->fi_nblocks != 0) {
236 1.1 perseant sp->fip = (FINFO *) ((caddr_t) fip + FINFOSIZE +
237 1.1 perseant sizeof(ufs_daddr_t) * (fip->fi_nblocks));
238 1.1 perseant sp->start_lbp = &sp->fip->fi_blocks[0];
239 1.1 perseant } else {
240 1.1 perseant sp->sum_bytes_left += FINFOSIZE;
241 1.1 perseant --((SEGSUM *) (sp->segsum))->ss_nfinfo;
242 1.1 perseant }
243 1.1 perseant }
244 1.1 perseant
245 1.1 perseant int
246 1.1 perseant lfs_writeinode(struct lfs * fs, struct segment * sp, struct inode * ip)
247 1.1 perseant {
248 1.1 perseant struct ubuf *bp, *ibp;
249 1.3 fvdl struct ufs1_dinode *cdp;
250 1.1 perseant IFILE *ifp;
251 1.1 perseant SEGUSE *sup;
252 1.1 perseant daddr_t daddr;
253 1.1 perseant ino_t ino;
254 1.1 perseant int error, i, ndx, fsb = 0;
255 1.1 perseant int redo_ifile = 0;
256 1.1 perseant struct timespec ts;
257 1.1 perseant int gotblk = 0;
258 1.1 perseant
259 1.1 perseant /* Allocate a new inode block if necessary. */
260 1.1 perseant if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
261 1.1 perseant sp->ibp == NULL) {
262 1.1 perseant /* Allocate a new segment if necessary. */
263 1.1 perseant if (sp->seg_bytes_left < fs->lfs_ibsize ||
264 1.1 perseant sp->sum_bytes_left < sizeof(ufs_daddr_t))
265 1.1 perseant (void) lfs_writeseg(fs, sp);
266 1.1 perseant
267 1.1 perseant /* Get next inode block. */
268 1.1 perseant daddr = fs->lfs_offset;
269 1.1 perseant fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
270 1.1 perseant sp->ibp = *sp->cbpp++ =
271 1.8 perseant getblk(fs->lfs_devvp, fsbtodb(fs, daddr),
272 1.1 perseant fs->lfs_ibsize);
273 1.1 perseant sp->ibp->b_flags |= B_GATHERED;
274 1.1 perseant gotblk++;
275 1.1 perseant
276 1.1 perseant /* Zero out inode numbers */
277 1.1 perseant for (i = 0; i < INOPB(fs); ++i)
278 1.3 fvdl ((struct ufs1_dinode *) sp->ibp->b_data)[i].di_inumber = 0;
279 1.1 perseant
280 1.1 perseant ++sp->start_bpp;
281 1.1 perseant fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
282 1.1 perseant /* Set remaining space counters. */
283 1.1 perseant sp->seg_bytes_left -= fs->lfs_ibsize;
284 1.1 perseant sp->sum_bytes_left -= sizeof(ufs_daddr_t);
285 1.1 perseant ndx = fs->lfs_sumsize / sizeof(ufs_daddr_t) -
286 1.1 perseant sp->ninodes / INOPB(fs) - 1;
287 1.1 perseant ((ufs_daddr_t *) (sp->segsum))[ndx] = daddr;
288 1.1 perseant }
289 1.1 perseant /* Update the inode times and copy the inode onto the inode page. */
290 1.1 perseant ts.tv_nsec = 0;
291 1.1 perseant ts.tv_sec = write_time;
292 1.1 perseant /* XXX kludge --- don't redirty the ifile just to put times on it */
293 1.1 perseant if (ip->i_number != LFS_IFILE_INUM)
294 1.1 perseant LFS_ITIMES(ip, &ts, &ts, &ts);
295 1.1 perseant
296 1.1 perseant /*
297 1.1 perseant * If this is the Ifile, and we've already written the Ifile in this
298 1.1 perseant * partial segment, just overwrite it (it's not on disk yet) and
299 1.1 perseant * continue.
300 1.1 perseant *
301 1.1 perseant * XXX we know that the bp that we get the second time around has
302 1.1 perseant * already been gathered.
303 1.1 perseant */
304 1.1 perseant if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
305 1.3 fvdl *(sp->idp) = *ip->i_din.ffs1_din;
306 1.3 fvdl ip->i_lfs_osize = ip->i_ffs1_size;
307 1.1 perseant return 0;
308 1.1 perseant }
309 1.1 perseant bp = sp->ibp;
310 1.3 fvdl cdp = ((struct ufs1_dinode *) bp->b_data) + (sp->ninodes % INOPB(fs));
311 1.3 fvdl *cdp = *ip->i_din.ffs1_din;
312 1.1 perseant
313 1.1 perseant /* If all blocks are goig to disk, update the "size on disk" */
314 1.3 fvdl ip->i_lfs_osize = ip->i_ffs1_size;
315 1.1 perseant
316 1.1 perseant if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
317 1.3 fvdl sp->idp = ((struct ufs1_dinode *) bp->b_data) +
318 1.1 perseant (sp->ninodes % INOPB(fs));
319 1.1 perseant if (gotblk) {
320 1.1 perseant LFS_LOCK_BUF(bp);
321 1.12 jnemeth assert(!(bp->b_flags & B_INVAL));
322 1.1 perseant brelse(bp);
323 1.1 perseant }
324 1.1 perseant /* Increment inode count in segment summary block. */
325 1.1 perseant ++((SEGSUM *) (sp->segsum))->ss_ninos;
326 1.1 perseant
327 1.1 perseant /* If this page is full, set flag to allocate a new page. */
328 1.1 perseant if (++sp->ninodes % INOPB(fs) == 0)
329 1.1 perseant sp->ibp = NULL;
330 1.1 perseant
331 1.1 perseant /*
332 1.1 perseant * If updating the ifile, update the super-block. Update the disk
333 1.1 perseant * address and access times for this inode in the ifile.
334 1.1 perseant */
335 1.1 perseant ino = ip->i_number;
336 1.1 perseant if (ino == LFS_IFILE_INUM) {
337 1.1 perseant daddr = fs->lfs_idaddr;
338 1.1 perseant fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
339 1.13 perseant sbdirty();
340 1.1 perseant } else {
341 1.1 perseant LFS_IENTRY(ifp, fs, ino, ibp);
342 1.1 perseant daddr = ifp->if_daddr;
343 1.1 perseant ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
344 1.1 perseant error = LFS_BWRITE_LOG(ibp); /* Ifile */
345 1.1 perseant }
346 1.1 perseant
347 1.1 perseant /*
348 1.1 perseant * Account the inode: it no longer belongs to its former segment,
349 1.1 perseant * though it will not belong to the new segment until that segment
350 1.1 perseant * is actually written.
351 1.1 perseant */
352 1.1 perseant if (daddr != LFS_UNUSED_DADDR) {
353 1.1 perseant u_int32_t oldsn = dtosn(fs, daddr);
354 1.1 perseant LFS_SEGENTRY(sup, fs, oldsn, bp);
355 1.3 fvdl sup->su_nbytes -= DINODE1_SIZE;
356 1.1 perseant redo_ifile =
357 1.1 perseant (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
358 1.1 perseant if (redo_ifile)
359 1.1 perseant fs->lfs_flags |= LFS_IFDIRTY;
360 1.1 perseant LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
361 1.1 perseant }
362 1.1 perseant return redo_ifile;
363 1.1 perseant }
364 1.1 perseant
365 1.1 perseant int
366 1.1 perseant lfs_gatherblock(struct segment * sp, struct ubuf * bp)
367 1.1 perseant {
368 1.1 perseant struct lfs *fs;
369 1.1 perseant int version;
370 1.1 perseant int j, blksinblk;
371 1.1 perseant
372 1.1 perseant /*
373 1.1 perseant * If full, finish this segment. We may be doing I/O, so
374 1.1 perseant * release and reacquire the splbio().
375 1.1 perseant */
376 1.1 perseant fs = sp->fs;
377 1.1 perseant blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
378 1.1 perseant if (sp->sum_bytes_left < sizeof(ufs_daddr_t) * blksinblk ||
379 1.1 perseant sp->seg_bytes_left < bp->b_bcount) {
380 1.1 perseant lfs_updatemeta(sp);
381 1.1 perseant
382 1.1 perseant version = sp->fip->fi_version;
383 1.1 perseant (void) lfs_writeseg(fs, sp);
384 1.1 perseant
385 1.1 perseant sp->fip->fi_version = version;
386 1.1 perseant sp->fip->fi_ino = VTOI(sp->vp)->i_number;
387 1.1 perseant /* Add the current file to the segment summary. */
388 1.1 perseant ++((SEGSUM *) (sp->segsum))->ss_nfinfo;
389 1.1 perseant sp->sum_bytes_left -= FINFOSIZE;
390 1.1 perseant
391 1.1 perseant return 1;
392 1.1 perseant }
393 1.1 perseant /* Insert into the buffer list, update the FINFO block. */
394 1.1 perseant bp->b_flags |= B_GATHERED;
395 1.1 perseant /* bp->b_flags &= ~B_DONE; */
396 1.1 perseant
397 1.1 perseant *sp->cbpp++ = bp;
398 1.1 perseant for (j = 0; j < blksinblk; j++)
399 1.1 perseant sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
400 1.1 perseant
401 1.1 perseant sp->sum_bytes_left -= sizeof(ufs_daddr_t) * blksinblk;
402 1.1 perseant sp->seg_bytes_left -= bp->b_bcount;
403 1.1 perseant return 0;
404 1.1 perseant }
405 1.1 perseant
406 1.1 perseant int
407 1.1 perseant lfs_gather(struct lfs * fs, struct segment * sp, struct uvnode * vp, int (*match) (struct lfs *, struct ubuf *))
408 1.1 perseant {
409 1.1 perseant struct ubuf *bp, *nbp;
410 1.1 perseant int count = 0;
411 1.1 perseant
412 1.1 perseant sp->vp = vp;
413 1.1 perseant loop:
414 1.1 perseant for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
415 1.1 perseant nbp = LIST_NEXT(bp, b_vnbufs);
416 1.1 perseant
417 1.1 perseant assert(bp->b_flags & B_DELWRI);
418 1.1 perseant if ((bp->b_flags & (B_BUSY | B_GATHERED)) || !match(fs, bp)) {
419 1.1 perseant continue;
420 1.1 perseant }
421 1.1 perseant if (lfs_gatherblock(sp, bp)) {
422 1.1 perseant goto loop;
423 1.1 perseant }
424 1.1 perseant count++;
425 1.1 perseant }
426 1.1 perseant
427 1.1 perseant lfs_updatemeta(sp);
428 1.1 perseant sp->vp = NULL;
429 1.1 perseant return count;
430 1.1 perseant }
431 1.1 perseant
432 1.1 perseant
433 1.1 perseant /*
434 1.1 perseant * Change the given block's address to ndaddr, finding its previous
435 1.1 perseant * location using ufs_bmaparray().
436 1.1 perseant *
437 1.1 perseant * Account for this change in the segment table.
438 1.1 perseant */
439 1.1 perseant void
440 1.1 perseant lfs_update_single(struct lfs * fs, struct segment * sp, daddr_t lbn,
441 1.1 perseant ufs_daddr_t ndaddr, int size)
442 1.1 perseant {
443 1.1 perseant SEGUSE *sup;
444 1.1 perseant struct ubuf *bp;
445 1.1 perseant struct indir a[NIADDR + 2], *ap;
446 1.1 perseant struct inode *ip;
447 1.1 perseant struct uvnode *vp;
448 1.1 perseant daddr_t daddr, ooff;
449 1.1 perseant int num, error;
450 1.1 perseant int bb, osize, obb;
451 1.1 perseant
452 1.1 perseant vp = sp->vp;
453 1.1 perseant ip = VTOI(vp);
454 1.1 perseant
455 1.1 perseant error = ufs_bmaparray(fs, vp, lbn, &daddr, a, &num);
456 1.1 perseant if (error)
457 1.1 perseant errx(1, "lfs_updatemeta: ufs_bmaparray returned %d looking up lbn %" PRId64 "\n", error, lbn);
458 1.1 perseant if (daddr > 0)
459 1.1 perseant daddr = dbtofsb(fs, daddr);
460 1.1 perseant
461 1.1 perseant bb = fragstofsb(fs, numfrags(fs, size));
462 1.1 perseant switch (num) {
463 1.1 perseant case 0:
464 1.3 fvdl ooff = ip->i_ffs1_db[lbn];
465 1.1 perseant if (ooff == UNWRITTEN)
466 1.3 fvdl ip->i_ffs1_blocks += bb;
467 1.1 perseant else {
468 1.1 perseant /* possible fragment truncation or extension */
469 1.1 perseant obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
470 1.3 fvdl ip->i_ffs1_blocks += (bb - obb);
471 1.1 perseant }
472 1.3 fvdl ip->i_ffs1_db[lbn] = ndaddr;
473 1.1 perseant break;
474 1.1 perseant case 1:
475 1.3 fvdl ooff = ip->i_ffs1_ib[a[0].in_off];
476 1.1 perseant if (ooff == UNWRITTEN)
477 1.3 fvdl ip->i_ffs1_blocks += bb;
478 1.3 fvdl ip->i_ffs1_ib[a[0].in_off] = ndaddr;
479 1.1 perseant break;
480 1.1 perseant default:
481 1.1 perseant ap = &a[num - 1];
482 1.1 perseant if (bread(vp, ap->in_lbn, fs->lfs_bsize, NULL, &bp))
483 1.1 perseant errx(1, "lfs_updatemeta: bread bno %" PRId64,
484 1.1 perseant ap->in_lbn);
485 1.1 perseant
486 1.1 perseant ooff = ((ufs_daddr_t *) bp->b_data)[ap->in_off];
487 1.1 perseant if (ooff == UNWRITTEN)
488 1.3 fvdl ip->i_ffs1_blocks += bb;
489 1.1 perseant ((ufs_daddr_t *) bp->b_data)[ap->in_off] = ndaddr;
490 1.1 perseant (void) VOP_BWRITE(bp);
491 1.1 perseant }
492 1.1 perseant
493 1.1 perseant /*
494 1.1 perseant * Update segment usage information, based on old size
495 1.1 perseant * and location.
496 1.1 perseant */
497 1.1 perseant if (daddr > 0) {
498 1.1 perseant u_int32_t oldsn = dtosn(fs, daddr);
499 1.1 perseant if (lbn >= 0 && lbn < NDADDR)
500 1.1 perseant osize = ip->i_lfs_fragsize[lbn];
501 1.1 perseant else
502 1.1 perseant osize = fs->lfs_bsize;
503 1.1 perseant LFS_SEGENTRY(sup, fs, oldsn, bp);
504 1.1 perseant sup->su_nbytes -= osize;
505 1.1 perseant if (!(bp->b_flags & B_GATHERED))
506 1.1 perseant fs->lfs_flags |= LFS_IFDIRTY;
507 1.1 perseant LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
508 1.1 perseant }
509 1.1 perseant /*
510 1.1 perseant * Now that this block has a new address, and its old
511 1.1 perseant * segment no longer owns it, we can forget about its
512 1.1 perseant * old size.
513 1.1 perseant */
514 1.1 perseant if (lbn >= 0 && lbn < NDADDR)
515 1.1 perseant ip->i_lfs_fragsize[lbn] = size;
516 1.1 perseant }
517 1.1 perseant
518 1.1 perseant /*
519 1.1 perseant * Update the metadata that points to the blocks listed in the FINFO
520 1.1 perseant * array.
521 1.1 perseant */
522 1.1 perseant void
523 1.1 perseant lfs_updatemeta(struct segment * sp)
524 1.1 perseant {
525 1.1 perseant struct ubuf *sbp;
526 1.1 perseant struct lfs *fs;
527 1.1 perseant struct uvnode *vp;
528 1.1 perseant daddr_t lbn;
529 1.1 perseant int i, nblocks, num;
530 1.1 perseant int bb;
531 1.1 perseant int bytesleft, size;
532 1.1 perseant
533 1.1 perseant vp = sp->vp;
534 1.1 perseant nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
535 1.1 perseant
536 1.1 perseant if (vp == NULL || nblocks == 0)
537 1.1 perseant return;
538 1.1 perseant
539 1.1 perseant /*
540 1.1 perseant * This count may be high due to oversize blocks from lfs_gop_write.
541 1.1 perseant * Correct for this. (XXX we should be able to keep track of these.)
542 1.1 perseant */
543 1.1 perseant fs = sp->fs;
544 1.1 perseant for (i = 0; i < nblocks; i++) {
545 1.1 perseant if (sp->start_bpp[i] == NULL) {
546 1.1 perseant printf("nblocks = %d, not %d\n", i, nblocks);
547 1.1 perseant nblocks = i;
548 1.1 perseant break;
549 1.1 perseant }
550 1.1 perseant num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
551 1.1 perseant nblocks -= num - 1;
552 1.1 perseant }
553 1.1 perseant
554 1.1 perseant /*
555 1.1 perseant * Sort the blocks.
556 1.1 perseant */
557 1.1 perseant lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
558 1.1 perseant
559 1.1 perseant /*
560 1.1 perseant * Record the length of the last block in case it's a fragment.
561 1.1 perseant * If there are indirect blocks present, they sort last. An
562 1.1 perseant * indirect block will be lfs_bsize and its presence indicates
563 1.1 perseant * that you cannot have fragments.
564 1.1 perseant */
565 1.1 perseant sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
566 1.1 perseant fs->lfs_bmask) + 1;
567 1.1 perseant
568 1.1 perseant /*
569 1.1 perseant * Assign disk addresses, and update references to the logical
570 1.1 perseant * block and the segment usage information.
571 1.1 perseant */
572 1.1 perseant for (i = nblocks; i--; ++sp->start_bpp) {
573 1.1 perseant sbp = *sp->start_bpp;
574 1.1 perseant lbn = *sp->start_lbp;
575 1.1 perseant
576 1.1 perseant sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
577 1.1 perseant
578 1.1 perseant /*
579 1.1 perseant * If we write a frag in the wrong place, the cleaner won't
580 1.1 perseant * be able to correctly identify its size later, and the
581 1.1 perseant * segment will be uncleanable. (Even worse, it will assume
582 1.1 perseant * that the indirect block that actually ends the list
583 1.1 perseant * is of a smaller size!)
584 1.1 perseant */
585 1.1 perseant if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
586 1.1 perseant errx(1, "lfs_updatemeta: fragment is not last block");
587 1.1 perseant
588 1.1 perseant /*
589 1.1 perseant * For each subblock in this possibly oversized block,
590 1.1 perseant * update its address on disk.
591 1.1 perseant */
592 1.1 perseant for (bytesleft = sbp->b_bcount; bytesleft > 0;
593 1.1 perseant bytesleft -= fs->lfs_bsize) {
594 1.1 perseant size = MIN(bytesleft, fs->lfs_bsize);
595 1.1 perseant bb = fragstofsb(fs, numfrags(fs, size));
596 1.1 perseant lbn = *sp->start_lbp++;
597 1.1 perseant lfs_update_single(fs, sp, lbn, fs->lfs_offset, size);
598 1.1 perseant fs->lfs_offset += bb;
599 1.1 perseant }
600 1.1 perseant
601 1.1 perseant }
602 1.1 perseant }
603 1.1 perseant
604 1.1 perseant /*
605 1.1 perseant * Start a new segment.
606 1.1 perseant */
607 1.1 perseant int
608 1.1 perseant lfs_initseg(struct lfs * fs)
609 1.1 perseant {
610 1.1 perseant struct segment *sp;
611 1.1 perseant SEGUSE *sup;
612 1.1 perseant SEGSUM *ssp;
613 1.1 perseant struct ubuf *bp, *sbp;
614 1.1 perseant int repeat;
615 1.1 perseant
616 1.1 perseant sp = fs->lfs_sp;
617 1.1 perseant
618 1.1 perseant repeat = 0;
619 1.1 perseant
620 1.1 perseant /* Advance to the next segment. */
621 1.1 perseant if (!LFS_PARTIAL_FITS(fs)) {
622 1.1 perseant /* lfs_avail eats the remaining space */
623 1.1 perseant fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
624 1.1 perseant fs->lfs_curseg);
625 1.1 perseant lfs_newseg(fs);
626 1.1 perseant repeat = 1;
627 1.1 perseant fs->lfs_offset = fs->lfs_curseg;
628 1.1 perseant
629 1.1 perseant sp->seg_number = dtosn(fs, fs->lfs_curseg);
630 1.1 perseant sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
631 1.1 perseant
632 1.1 perseant /*
633 1.1 perseant * If the segment contains a superblock, update the offset
634 1.1 perseant * and summary address to skip over it.
635 1.1 perseant */
636 1.1 perseant LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
637 1.1 perseant if (sup->su_flags & SEGUSE_SUPERBLOCK) {
638 1.1 perseant fs->lfs_offset += btofsb(fs, LFS_SBPAD);
639 1.1 perseant sp->seg_bytes_left -= LFS_SBPAD;
640 1.1 perseant }
641 1.1 perseant brelse(bp);
642 1.1 perseant /* Segment zero could also contain the labelpad */
643 1.1 perseant if (fs->lfs_version > 1 && sp->seg_number == 0 &&
644 1.1 perseant fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
645 1.1 perseant fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
646 1.1 perseant sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
647 1.1 perseant }
648 1.1 perseant } else {
649 1.1 perseant sp->seg_number = dtosn(fs, fs->lfs_curseg);
650 1.1 perseant sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
651 1.1 perseant (fs->lfs_offset - fs->lfs_curseg));
652 1.1 perseant }
653 1.1 perseant fs->lfs_lastpseg = fs->lfs_offset;
654 1.1 perseant
655 1.1 perseant sp->fs = fs;
656 1.1 perseant sp->ibp = NULL;
657 1.1 perseant sp->idp = NULL;
658 1.1 perseant sp->ninodes = 0;
659 1.1 perseant sp->ndupino = 0;
660 1.1 perseant
661 1.1 perseant /* Get a new buffer for SEGSUM and enter it into the buffer list. */
662 1.1 perseant sp->cbpp = sp->bpp;
663 1.8 perseant sbp = *sp->cbpp = getblk(fs->lfs_devvp,
664 1.1 perseant fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
665 1.1 perseant sp->segsum = sbp->b_data;
666 1.1 perseant memset(sp->segsum, 0, fs->lfs_sumsize);
667 1.1 perseant sp->start_bpp = ++sp->cbpp;
668 1.1 perseant fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
669 1.1 perseant
670 1.1 perseant /* Set point to SEGSUM, initialize it. */
671 1.1 perseant ssp = sp->segsum;
672 1.1 perseant ssp->ss_next = fs->lfs_nextseg;
673 1.1 perseant ssp->ss_nfinfo = ssp->ss_ninos = 0;
674 1.1 perseant ssp->ss_magic = SS_MAGIC;
675 1.1 perseant
676 1.1 perseant /* Set pointer to first FINFO, initialize it. */
677 1.1 perseant sp->fip = (struct finfo *) ((caddr_t) sp->segsum + SEGSUM_SIZE(fs));
678 1.1 perseant sp->fip->fi_nblocks = 0;
679 1.1 perseant sp->start_lbp = &sp->fip->fi_blocks[0];
680 1.1 perseant sp->fip->fi_lastlength = 0;
681 1.1 perseant
682 1.1 perseant sp->seg_bytes_left -= fs->lfs_sumsize;
683 1.1 perseant sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
684 1.1 perseant
685 1.1 perseant LFS_LOCK_BUF(sbp);
686 1.1 perseant brelse(sbp);
687 1.1 perseant return repeat;
688 1.1 perseant }
689 1.1 perseant
690 1.1 perseant /*
691 1.1 perseant * Return the next segment to write.
692 1.1 perseant */
693 1.1 perseant void
694 1.1 perseant lfs_newseg(struct lfs * fs)
695 1.1 perseant {
696 1.1 perseant CLEANERINFO *cip;
697 1.1 perseant SEGUSE *sup;
698 1.1 perseant struct ubuf *bp;
699 1.1 perseant int curseg, isdirty, sn;
700 1.1 perseant
701 1.1 perseant LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
702 1.1 perseant sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
703 1.1 perseant sup->su_nbytes = 0;
704 1.1 perseant sup->su_nsums = 0;
705 1.1 perseant sup->su_ninos = 0;
706 1.1 perseant LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
707 1.1 perseant
708 1.1 perseant LFS_CLEANERINFO(cip, fs, bp);
709 1.1 perseant --cip->clean;
710 1.1 perseant ++cip->dirty;
711 1.1 perseant fs->lfs_nclean = cip->clean;
712 1.1 perseant LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
713 1.1 perseant
714 1.1 perseant fs->lfs_lastseg = fs->lfs_curseg;
715 1.1 perseant fs->lfs_curseg = fs->lfs_nextseg;
716 1.1 perseant for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
717 1.1 perseant sn = (sn + 1) % fs->lfs_nseg;
718 1.1 perseant if (sn == curseg)
719 1.1 perseant errx(1, "lfs_nextseg: no clean segments");
720 1.1 perseant LFS_SEGENTRY(sup, fs, sn, bp);
721 1.1 perseant isdirty = sup->su_flags & SEGUSE_DIRTY;
722 1.1 perseant brelse(bp);
723 1.1 perseant
724 1.1 perseant if (!isdirty)
725 1.1 perseant break;
726 1.1 perseant }
727 1.1 perseant
728 1.1 perseant ++fs->lfs_nactive;
729 1.1 perseant fs->lfs_nextseg = sntod(fs, sn);
730 1.1 perseant }
731 1.1 perseant
732 1.1 perseant
733 1.1 perseant int
734 1.1 perseant lfs_writeseg(struct lfs * fs, struct segment * sp)
735 1.1 perseant {
736 1.1 perseant struct ubuf **bpp, *bp;
737 1.1 perseant SEGUSE *sup;
738 1.1 perseant SEGSUM *ssp;
739 1.1 perseant char *datap, *dp;
740 1.1 perseant int i;
741 1.1 perseant int do_again, nblocks, byteoffset;
742 1.1 perseant size_t el_size;
743 1.1 perseant u_short ninos;
744 1.1 perseant struct uvnode *devvp;
745 1.1 perseant
746 1.1 perseant /*
747 1.1 perseant * If there are no buffers other than the segment summary to write
748 1.1 perseant * and it is not a checkpoint, don't do anything. On a checkpoint,
749 1.1 perseant * even if there aren't any buffers, you need to write the superblock.
750 1.1 perseant */
751 1.13 perseant nblocks = sp->cbpp - sp->bpp;
752 1.13 perseant #if 0
753 1.13 perseant printf("write %d blocks at 0x%x\n",
754 1.13 perseant nblocks, (int)dbtofsb(fs, (*sp->bpp)->b_blkno));
755 1.13 perseant #endif
756 1.13 perseant if (nblocks == 1)
757 1.1 perseant return 0;
758 1.1 perseant
759 1.8 perseant devvp = fs->lfs_devvp;
760 1.1 perseant
761 1.1 perseant /* Update the segment usage information. */
762 1.1 perseant LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
763 1.13 perseant sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
764 1.1 perseant
765 1.1 perseant /* Loop through all blocks, except the segment summary. */
766 1.1 perseant for (bpp = sp->bpp; ++bpp < sp->cbpp;) {
767 1.1 perseant if ((*bpp)->b_vp != devvp) {
768 1.1 perseant sup->su_nbytes += (*bpp)->b_bcount;
769 1.1 perseant }
770 1.13 perseant assert(dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno)) == sp->seg_number);
771 1.1 perseant }
772 1.1 perseant
773 1.1 perseant ssp = (SEGSUM *) sp->segsum;
774 1.14 perseant ssp->ss_flags |= SS_RFW;
775 1.1 perseant
776 1.1 perseant ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
777 1.3 fvdl sup->su_nbytes += ssp->ss_ninos * DINODE1_SIZE;
778 1.1 perseant
779 1.1 perseant if (fs->lfs_version == 1)
780 1.1 perseant sup->su_olastmod = write_time;
781 1.1 perseant else
782 1.1 perseant sup->su_lastmod = write_time;
783 1.1 perseant sup->su_ninos += ninos;
784 1.1 perseant ++sup->su_nsums;
785 1.1 perseant fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
786 1.1 perseant fs->lfs_ibsize));
787 1.1 perseant fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
788 1.1 perseant
789 1.1 perseant do_again = !(bp->b_flags & B_GATHERED);
790 1.1 perseant LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
791 1.1 perseant
792 1.1 perseant /*
793 1.1 perseant * Compute checksum across data and then across summary; the first
794 1.1 perseant * block (the summary block) is skipped. Set the create time here
795 1.1 perseant * so that it's guaranteed to be later than the inode mod times.
796 1.1 perseant */
797 1.1 perseant if (fs->lfs_version == 1)
798 1.1 perseant el_size = sizeof(u_long);
799 1.1 perseant else
800 1.1 perseant el_size = sizeof(u_int32_t);
801 1.15 christos datap = dp = emalloc(nblocks * el_size);
802 1.1 perseant for (bpp = sp->bpp, i = nblocks - 1; i--;) {
803 1.1 perseant ++bpp;
804 1.1 perseant /* Loop through gop_write cluster blocks */
805 1.1 perseant for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
806 1.1 perseant byteoffset += fs->lfs_bsize) {
807 1.1 perseant memcpy(dp, (*bpp)->b_data + byteoffset, el_size);
808 1.1 perseant dp += el_size;
809 1.1 perseant }
810 1.2 perseant bremfree(*bpp);
811 1.1 perseant (*bpp)->b_flags |= B_BUSY;
812 1.1 perseant }
813 1.1 perseant if (fs->lfs_version == 1)
814 1.1 perseant ssp->ss_ocreate = write_time;
815 1.1 perseant else {
816 1.1 perseant ssp->ss_create = write_time;
817 1.1 perseant ssp->ss_serial = ++fs->lfs_serial;
818 1.1 perseant ssp->ss_ident = fs->lfs_ident;
819 1.1 perseant }
820 1.1 perseant /* Set the summary block busy too */
821 1.1 perseant bremfree(*(sp->bpp));
822 1.1 perseant (*(sp->bpp))->b_flags |= B_BUSY;
823 1.1 perseant
824 1.1 perseant ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
825 1.1 perseant ssp->ss_sumsum =
826 1.1 perseant cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
827 1.1 perseant free(datap);
828 1.1 perseant datap = dp = NULL;
829 1.1 perseant fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
830 1.1 perseant btofsb(fs, fs->lfs_sumsize));
831 1.1 perseant
832 1.1 perseant if (devvp == NULL)
833 1.1 perseant errx(1, "devvp is NULL");
834 1.1 perseant for (bpp = sp->bpp, i = nblocks; i; bpp++, i--) {
835 1.1 perseant bp = *bpp;
836 1.1 perseant #if 0
837 1.2 perseant printf("i = %d, bp = %p, flags %lx, bn = %" PRIx64 "\n",
838 1.1 perseant nblocks - i, bp, bp->b_flags, bp->b_blkno);
839 1.1 perseant printf(" vp = %p\n", bp->b_vp);
840 1.8 perseant if (bp->b_vp != fs->lfs_devvp)
841 1.1 perseant printf(" ino = %d lbn = %" PRId64 "\n",
842 1.1 perseant VTOI(bp->b_vp)->i_number, bp->b_lblkno);
843 1.1 perseant #endif
844 1.8 perseant if (bp->b_vp == fs->lfs_devvp)
845 1.1 perseant written_dev += bp->b_bcount;
846 1.1 perseant else {
847 1.1 perseant if (bp->b_lblkno >= 0)
848 1.1 perseant written_data += bp->b_bcount;
849 1.1 perseant else
850 1.1 perseant written_indir += bp->b_bcount;
851 1.1 perseant }
852 1.2 perseant bp->b_flags &= ~(B_DELWRI | B_READ | B_GATHERED | B_ERROR |
853 1.2 perseant B_LOCKED);
854 1.1 perseant bwrite(bp);
855 1.1 perseant written_bytes += bp->b_bcount;
856 1.1 perseant }
857 1.1 perseant written_inodes += ninos;
858 1.1 perseant
859 1.1 perseant return (lfs_initseg(fs) || do_again);
860 1.1 perseant }
861 1.1 perseant
862 1.1 perseant /*
863 1.1 perseant * Our own copy of shellsort. XXX use qsort or heapsort.
864 1.1 perseant */
865 1.1 perseant void
866 1.1 perseant lfs_shellsort(struct ubuf ** bp_array, ufs_daddr_t * lb_array, int nmemb, int size)
867 1.1 perseant {
868 1.1 perseant static int __rsshell_increments[] = {4, 1, 0};
869 1.1 perseant int incr, *incrp, t1, t2;
870 1.1 perseant struct ubuf *bp_temp;
871 1.1 perseant
872 1.1 perseant for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
873 1.1 perseant for (t1 = incr; t1 < nmemb; ++t1)
874 1.1 perseant for (t2 = t1 - incr; t2 >= 0;)
875 1.1 perseant if ((u_int32_t) bp_array[t2]->b_lblkno >
876 1.1 perseant (u_int32_t) bp_array[t2 + incr]->b_lblkno) {
877 1.1 perseant bp_temp = bp_array[t2];
878 1.1 perseant bp_array[t2] = bp_array[t2 + incr];
879 1.1 perseant bp_array[t2 + incr] = bp_temp;
880 1.1 perseant t2 -= incr;
881 1.1 perseant } else
882 1.1 perseant break;
883 1.1 perseant
884 1.1 perseant /* Reform the list of logical blocks */
885 1.1 perseant incr = 0;
886 1.1 perseant for (t1 = 0; t1 < nmemb; t1++) {
887 1.1 perseant for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
888 1.1 perseant lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
889 1.1 perseant }
890 1.1 perseant }
891 1.1 perseant }
892 1.1 perseant
893 1.1 perseant
894 1.1 perseant /*
895 1.1 perseant * lfs_seglock --
896 1.1 perseant * Single thread the segment writer.
897 1.1 perseant */
898 1.1 perseant int
899 1.1 perseant lfs_seglock(struct lfs * fs, unsigned long flags)
900 1.1 perseant {
901 1.1 perseant struct segment *sp;
902 1.1 perseant
903 1.1 perseant if (fs->lfs_seglock) {
904 1.1 perseant ++fs->lfs_seglock;
905 1.1 perseant fs->lfs_sp->seg_flags |= flags;
906 1.1 perseant return 0;
907 1.1 perseant }
908 1.1 perseant fs->lfs_seglock = 1;
909 1.1 perseant
910 1.15 christos sp = fs->lfs_sp = emalloc(sizeof(*sp));
911 1.15 christos sp->bpp = emalloc(fs->lfs_ssize * sizeof(struct ubuf *));
912 1.6 heas if (!sp->bpp)
913 1.7 perseant errx(!preen, "Could not allocate %zu bytes: %s",
914 1.6 heas (size_t)(fs->lfs_ssize * sizeof(struct ubuf *)),
915 1.6 heas strerror(errno));
916 1.1 perseant sp->seg_flags = flags;
917 1.1 perseant sp->vp = NULL;
918 1.1 perseant sp->seg_iocount = 0;
919 1.1 perseant (void) lfs_initseg(fs);
920 1.1 perseant
921 1.1 perseant return 0;
922 1.1 perseant }
923 1.1 perseant
924 1.1 perseant /*
925 1.1 perseant * lfs_segunlock --
926 1.1 perseant * Single thread the segment writer.
927 1.1 perseant */
928 1.1 perseant void
929 1.1 perseant lfs_segunlock(struct lfs * fs)
930 1.1 perseant {
931 1.1 perseant struct segment *sp;
932 1.1 perseant struct ubuf *bp;
933 1.1 perseant
934 1.1 perseant sp = fs->lfs_sp;
935 1.1 perseant
936 1.1 perseant if (fs->lfs_seglock == 1) {
937 1.1 perseant if (sp->bpp != sp->cbpp) {
938 1.1 perseant /* Free allocated segment summary */
939 1.1 perseant fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
940 1.1 perseant bp = *sp->bpp;
941 1.1 perseant bremfree(bp);
942 1.1 perseant bp->b_flags |= B_DONE | B_INVAL;
943 1.1 perseant bp->b_flags &= ~B_DELWRI;
944 1.1 perseant reassignbuf(bp, bp->b_vp);
945 1.1 perseant bp->b_flags |= B_BUSY; /* XXX */
946 1.1 perseant brelse(bp);
947 1.1 perseant } else
948 1.1 perseant printf("unlock to 0 with no summary");
949 1.1 perseant
950 1.1 perseant free(sp->bpp);
951 1.1 perseant sp->bpp = NULL;
952 1.1 perseant free(sp);
953 1.1 perseant fs->lfs_sp = NULL;
954 1.1 perseant
955 1.1 perseant fs->lfs_nactive = 0;
956 1.1 perseant
957 1.1 perseant /* Since we *know* everything's on disk, write both sbs */
958 1.4 yamt lfs_writesuper(fs, fs->lfs_sboffs[0]);
959 1.4 yamt lfs_writesuper(fs, fs->lfs_sboffs[1]);
960 1.1 perseant
961 1.1 perseant --fs->lfs_seglock;
962 1.1 perseant fs->lfs_lockpid = 0;
963 1.1 perseant } else if (fs->lfs_seglock == 0) {
964 1.1 perseant errx(1, "Seglock not held");
965 1.1 perseant } else {
966 1.1 perseant --fs->lfs_seglock;
967 1.1 perseant }
968 1.1 perseant }
969 1.1 perseant
970 1.1 perseant int
971 1.1 perseant lfs_writevnodes(struct lfs *fs, struct segment *sp, int op)
972 1.1 perseant {
973 1.1 perseant struct inode *ip;
974 1.1 perseant struct uvnode *vp;
975 1.1 perseant int inodes_written = 0;
976 1.1 perseant
977 1.1 perseant LIST_FOREACH(vp, &vnodelist, v_mntvnodes) {
978 1.1 perseant if (vp->v_bmap_op != lfs_vop_bmap)
979 1.1 perseant continue;
980 1.1 perseant
981 1.1 perseant ip = VTOI(vp);
982 1.1 perseant
983 1.1 perseant if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
984 1.1 perseant (op != VN_DIROP && (vp->v_flag & VDIROP))) {
985 1.1 perseant continue;
986 1.1 perseant }
987 1.1 perseant /*
988 1.1 perseant * Write the inode/file if dirty and it's not the IFILE.
989 1.1 perseant */
990 1.1 perseant if (ip->i_flag & IN_ALLMOD || !LIST_EMPTY(&vp->v_dirtyblkhd)) {
991 1.1 perseant if (ip->i_number != LFS_IFILE_INUM)
992 1.1 perseant lfs_writefile(fs, sp, vp);
993 1.1 perseant (void) lfs_writeinode(fs, sp, ip);
994 1.1 perseant inodes_written++;
995 1.1 perseant }
996 1.1 perseant }
997 1.1 perseant return inodes_written;
998 1.1 perseant }
999 1.1 perseant
1000 1.1 perseant void
1001 1.1 perseant lfs_writesuper(struct lfs *fs, ufs_daddr_t daddr)
1002 1.1 perseant {
1003 1.1 perseant struct ubuf *bp;
1004 1.1 perseant
1005 1.1 perseant /* Set timestamp of this version of the superblock */
1006 1.1 perseant if (fs->lfs_version == 1)
1007 1.1 perseant fs->lfs_otstamp = write_time;
1008 1.1 perseant fs->lfs_tstamp = write_time;
1009 1.1 perseant
1010 1.1 perseant /* Checksum the superblock and copy it into a buffer. */
1011 1.1 perseant fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1012 1.1 perseant assert(daddr > 0);
1013 1.8 perseant bp = getblk(fs->lfs_devvp, fsbtodb(fs, daddr), LFS_SBPAD);
1014 1.1 perseant memset(bp->b_data + sizeof(struct dlfs), 0,
1015 1.1 perseant LFS_SBPAD - sizeof(struct dlfs));
1016 1.1 perseant *(struct dlfs *) bp->b_data = fs->lfs_dlfs;
1017 1.1 perseant
1018 1.1 perseant bwrite(bp);
1019 1.1 perseant }
1020