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