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