lfs_syscalls.c revision 1.73 1 /* $NetBSD: lfs_syscalls.c,v 1.73 2002/11/24 16:39:13 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*-
39 * Copyright (c) 1991, 1993, 1994
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_syscalls.c 8.10 (Berkeley) 5/14/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_syscalls.c,v 1.73 2002/11/24 16:39:13 yamt Exp $");
75
76 #define LFS /* for prototypes in syscallargs.h */
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/proc.h>
81 #include <sys/buf.h>
82 #include <sys/mount.h>
83 #include <sys/vnode.h>
84 #include <sys/malloc.h>
85 #include <sys/kernel.h>
86
87 #include <sys/syscallargs.h>
88
89 #include <ufs/ufs/inode.h>
90 #include <ufs/ufs/ufsmount.h>
91 #include <ufs/ufs/ufs_extern.h>
92
93 #include <ufs/lfs/lfs.h>
94 #include <ufs/lfs/lfs_extern.h>
95
96 /* Flags for return from lfs_fastvget */
97 #define FVG_UNLOCK 0x01 /* Needs to be unlocked */
98 #define FVG_PUT 0x02 /* Needs to be vput() */
99
100 /* Max block count for lfs_markv() */
101 #define MARKV_MAXBLKCNT 65536
102
103 struct buf *lfs_fakebuf(struct lfs *, struct vnode *, int, size_t, caddr_t);
104 int lfs_fasthashget(dev_t, ino_t, int *, struct vnode **);
105
106 int debug_cleaner = 0;
107 int clean_vnlocked = 0;
108 int clean_inlocked = 0;
109 int verbose_debug = 0;
110
111 pid_t lfs_cleaner_pid = 0;
112
113 /*
114 * Definitions for the buffer free lists.
115 */
116 #define BQUEUES 4 /* number of free buffer queues */
117
118 #define BQ_LOCKED 0 /* super-blocks &c */
119 #define BQ_LRU 1 /* lru, useful buffers */
120 #define BQ_AGE 2 /* rubbish */
121 #define BQ_EMPTY 3 /* buffer headers with no memory */
122
123 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
124
125 #define LFS_FORCE_WRITE UNASSIGNED
126
127 #define LFS_VREF_THRESHOLD 128
128
129 static int lfs_bmapv(struct proc *, fsid_t *, BLOCK_INFO *, int);
130 static int lfs_markv(struct proc *, fsid_t *, BLOCK_INFO *, int);
131 static void lfs_fakebuf_iodone(struct buf *);
132
133 /*
134 * sys_lfs_markv:
135 *
136 * This will mark inodes and blocks dirty, so they are written into the log.
137 * It will block until all the blocks have been written. The segment create
138 * time passed in the block_info and inode_info structures is used to decide
139 * if the data is valid for each block (in case some process dirtied a block
140 * or inode that is being cleaned between the determination that a block is
141 * live and the lfs_markv call).
142 *
143 * 0 on success
144 * -1/errno is return on error.
145 */
146 #ifdef USE_64BIT_SYSCALLS
147 int
148 sys_lfs_markv(struct proc *p, void *v, register_t *retval)
149 {
150 struct sys_lfs_markv_args /* {
151 syscallarg(fsid_t *) fsidp;
152 syscallarg(struct block_info *) blkiov;
153 syscallarg(int) blkcnt;
154 } */ *uap = v;
155 BLOCK_INFO *blkiov;
156 int blkcnt, error;
157 fsid_t fsid;
158
159 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
160 return (error);
161
162 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
163 return (error);
164
165 blkcnt = SCARG(uap, blkcnt);
166 if ((u_int) blkcnt > MARKV_MAXBLKCNT)
167 return (EINVAL);
168
169 blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
170 if ((error = copyin(SCARG(uap, blkiov), blkiov,
171 blkcnt * sizeof(BLOCK_INFO))) != 0)
172 goto out;
173
174 if ((error = lfs_markv(p, &fsid, blkiov, blkcnt)) == 0)
175 copyout(blkiov, SCARG(uap, blkiov),
176 blkcnt * sizeof(BLOCK_INFO));
177 out:
178 free(blkiov, M_SEGMENT);
179 return error;
180 }
181 #else
182 int
183 sys_lfs_markv(struct proc *p, void *v, register_t *retval)
184 {
185 struct sys_lfs_markv_args /* {
186 syscallarg(fsid_t *) fsidp;
187 syscallarg(struct block_info *) blkiov;
188 syscallarg(int) blkcnt;
189 } */ *uap = v;
190 BLOCK_INFO *blkiov;
191 BLOCK_INFO_15 *blkiov15;
192 int i, blkcnt, error;
193 fsid_t fsid;
194
195 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
196 return (error);
197
198 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
199 return (error);
200
201 blkcnt = SCARG(uap, blkcnt);
202 if ((u_int) blkcnt > MARKV_MAXBLKCNT)
203 return (EINVAL);
204
205 blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
206 blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
207 if ((error = copyin(SCARG(uap, blkiov), blkiov15,
208 blkcnt * sizeof(BLOCK_INFO_15))) != 0)
209 goto out;
210
211 for (i = 0; i < blkcnt; i++) {
212 blkiov[i].bi_inode = blkiov15[i].bi_inode;
213 blkiov[i].bi_lbn = blkiov15[i].bi_lbn;
214 blkiov[i].bi_daddr = blkiov15[i].bi_daddr;
215 blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
216 blkiov[i].bi_version = blkiov15[i].bi_version;
217 blkiov[i].bi_bp = blkiov15[i].bi_bp;
218 blkiov[i].bi_size = blkiov15[i].bi_size;
219 }
220
221 if ((error = lfs_markv(p, &fsid, blkiov, blkcnt)) == 0) {
222 for (i = 0; i < blkcnt; i++) {
223 blkiov15[i].bi_inode = blkiov[i].bi_inode;
224 blkiov15[i].bi_lbn = blkiov[i].bi_lbn;
225 blkiov15[i].bi_daddr = blkiov[i].bi_daddr;
226 blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
227 blkiov15[i].bi_version = blkiov[i].bi_version;
228 blkiov15[i].bi_bp = blkiov[i].bi_bp;
229 blkiov15[i].bi_size = blkiov[i].bi_size;
230 }
231 copyout(blkiov15, SCARG(uap, blkiov),
232 blkcnt * sizeof(BLOCK_INFO_15));
233 }
234 out:
235 free(blkiov, M_SEGMENT);
236 free(blkiov15, M_SEGMENT);
237 return error;
238 }
239 #endif
240
241 static int
242 lfs_markv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
243 {
244 BLOCK_INFO *blkp;
245 IFILE *ifp;
246 struct buf *bp, *nbp;
247 struct inode *ip = NULL;
248 struct lfs *fs;
249 struct mount *mntp;
250 struct vnode *vp;
251 #ifdef DEBUG_LFS
252 int vputc = 0, iwritten = 0;
253 #endif
254 ino_t lastino;
255 ufs_daddr_t b_daddr, v_daddr;
256 int cnt, error, lfs_fastvget_unlock;
257 int do_again = 0;
258 int s;
259 #ifdef CHECK_COPYIN
260 int i;
261 #endif /* CHECK_COPYIN */
262 int numlocked = 0, numrefed = 0;
263 ino_t maxino;
264 size_t obsize;
265
266 if ((mntp = vfs_getvfs(fsidp)) == NULL)
267 return (ENOENT);
268
269 fs = VFSTOUFS(mntp)->um_lfs;
270 maxino = (fragstoblks(fs, fsbtofrags(fs, VTOI(fs->lfs_ivnode)->i_ffs_blocks)) -
271 fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
272
273 cnt = blkcnt;
274
275 if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
276 return (error);
277
278 /*
279 * This seglock is just to prevent the fact that we might have to sleep
280 * from allowing the possibility that our blocks might become
281 * invalid.
282 *
283 * It is also important to note here that unless we specify SEGM_CKP,
284 * any Ifile blocks that we might be asked to clean will never get
285 * to the disk.
286 */
287 lfs_seglock(fs, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
288
289 /* Mark blocks/inodes dirty. */
290 error = 0;
291
292 #ifdef DEBUG_LFS
293 /* Run through and count the inodes */
294 lastino = LFS_UNUSED_INUM;
295 for (blkp = blkiov; cnt--; ++blkp) {
296 if (lastino != blkp->bi_inode) {
297 lastino = blkp->bi_inode;
298 vputc++;
299 }
300 }
301 cnt = blkcnt;
302 printf("[%d/",vputc);
303 iwritten = 0;
304 #endif /* DEBUG_LFS */
305 /* these were inside the initialization for the for loop */
306 v_daddr = LFS_UNUSED_DADDR;
307 lastino = LFS_UNUSED_INUM;
308 for (blkp = blkiov; cnt--; ++blkp)
309 {
310 if (blkp->bi_daddr == LFS_FORCE_WRITE)
311 printf("lfs_markv: warning: force-writing ino %d lbn %d\n",
312 blkp->bi_inode, blkp->bi_lbn);
313 /* Bounds-check incoming data, avoid panic for failed VGET */
314 if (blkp->bi_inode <= 0 || blkp->bi_inode >= maxino) {
315 error = EINVAL;
316 goto again;
317 }
318 /*
319 * Get the IFILE entry (only once) and see if the file still
320 * exists.
321 */
322 if (lastino != blkp->bi_inode) {
323 /*
324 * Finish the old file, if there was one. The presence
325 * of a usable vnode in vp is signaled by a valid v_daddr.
326 */
327 if (v_daddr != LFS_UNUSED_DADDR) {
328 #ifdef DEBUG_LFS
329 if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
330 iwritten++;
331 #endif
332 if (lfs_fastvget_unlock) {
333 VOP_UNLOCK(vp, 0);
334 numlocked--;
335 }
336 lfs_vunref(vp);
337 numrefed--;
338 }
339
340 /*
341 * Start a new file
342 */
343 lastino = blkp->bi_inode;
344 if (blkp->bi_inode == LFS_IFILE_INUM)
345 v_daddr = fs->lfs_idaddr;
346 else {
347 LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
348 /* XXX fix for force write */
349 v_daddr = ifp->if_daddr;
350 brelse(bp);
351 }
352 /* Don't force-write the ifile */
353 if (blkp->bi_inode == LFS_IFILE_INUM
354 && blkp->bi_daddr == LFS_FORCE_WRITE)
355 {
356 continue;
357 }
358 if (v_daddr == LFS_UNUSED_DADDR
359 && blkp->bi_daddr != LFS_FORCE_WRITE)
360 {
361 continue;
362 }
363
364 /* Get the vnode/inode. */
365 error = lfs_fastvget(mntp, blkp->bi_inode, v_daddr,
366 &vp,
367 (blkp->bi_lbn == LFS_UNUSED_LBN
368 ? blkp->bi_bp
369 : NULL),
370 &lfs_fastvget_unlock);
371 if (lfs_fastvget_unlock)
372 numlocked++;
373
374 if (!error) {
375 numrefed++;
376 }
377 if (error) {
378 #ifdef DEBUG_LFS
379 printf("lfs_markv: lfs_fastvget failed with %d (ino %d, segment %d)\n",
380 error, blkp->bi_inode,
381 dtosn(fs, blkp->bi_daddr));
382 #endif /* DEBUG_LFS */
383 /*
384 * If we got EAGAIN, that means that the
385 * Inode was locked. This is
386 * recoverable: just clean the rest of
387 * this segment, and let the cleaner try
388 * again with another. (When the
389 * cleaner runs again, this segment will
390 * sort high on the list, since it is
391 * now almost entirely empty.) But, we
392 * still set v_daddr = LFS_UNUSED_ADDR
393 * so as not to test this over and over
394 * again.
395 */
396 if (error == EAGAIN) {
397 error = 0;
398 do_again++;
399 }
400 #ifdef DIAGNOSTIC
401 else if (error != ENOENT)
402 panic("lfs_markv VFS_VGET FAILED");
403 #endif
404 /* lastino = LFS_UNUSED_INUM; */
405 v_daddr = LFS_UNUSED_DADDR;
406 vp = NULL;
407 ip = NULL;
408 continue;
409 }
410 ip = VTOI(vp);
411 } else if (v_daddr == LFS_UNUSED_DADDR) {
412 /*
413 * This can only happen if the vnode is dead (or
414 * in any case we can't get it...e.g., it is
415 * inlocked). Keep going.
416 */
417 continue;
418 }
419
420 /* Past this point we are guaranteed that vp, ip are valid. */
421
422 /* If this BLOCK_INFO didn't contain a block, keep going. */
423 if (blkp->bi_lbn == LFS_UNUSED_LBN) {
424 /* XXX need to make sure that the inode gets written in this case */
425 /* XXX but only write the inode if it's the right one */
426 if (blkp->bi_inode != LFS_IFILE_INUM) {
427 LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
428 if (ifp->if_daddr == blkp->bi_daddr
429 || blkp->bi_daddr == LFS_FORCE_WRITE)
430 {
431 LFS_SET_UINO(ip, IN_CLEANING);
432 }
433 brelse(bp);
434 }
435 continue;
436 }
437
438 b_daddr = 0;
439 if (blkp->bi_daddr != LFS_FORCE_WRITE) {
440 if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
441 dbtofsb(fs, b_daddr) != blkp->bi_daddr)
442 {
443 if (dtosn(fs,dbtofsb(fs, b_daddr))
444 == dtosn(fs,blkp->bi_daddr))
445 {
446 printf("lfs_markv: wrong da same seg: %x vs %x\n",
447 blkp->bi_daddr, dbtofsb(fs, b_daddr));
448 }
449 do_again++;
450 continue;
451 }
452 }
453
454 /*
455 * Check block sizes. The blocks being cleaned come from
456 * disk, so they should have the same size as their on-disk
457 * counterparts.
458 */
459 if (blkp->bi_lbn >= 0)
460 obsize = blksize(fs, ip, blkp->bi_lbn);
461 else
462 obsize = fs->lfs_bsize;
463 /* Check for fragment size change */
464 if (blkp->bi_lbn >= 0 && blkp->bi_lbn < NDADDR) {
465 obsize = ip->i_lfs_fragsize[blkp->bi_lbn];
466 }
467 if (obsize != blkp->bi_size) {
468 printf("lfs_markv: ino %d lbn %d wrong size (%ld != %d), try again\n",
469 blkp->bi_inode, blkp->bi_lbn,
470 (long) obsize, blkp->bi_size);
471 do_again++;
472 continue;
473 }
474
475 /*
476 * If we get to here, then we are keeping the block. If
477 * it is an indirect block, we want to actually put it
478 * in the buffer cache so that it can be updated in the
479 * finish_meta section. If it's not, we need to
480 * allocate a fake buffer so that writeseg can perform
481 * the copyin and write the buffer.
482 */
483 if (ip->i_number != LFS_IFILE_INUM && blkp->bi_lbn >= 0) {
484 /* Data Block */
485 bp = lfs_fakebuf(fs, vp, blkp->bi_lbn,
486 blkp->bi_size, blkp->bi_bp);
487 /* Pretend we used bread() to get it */
488 bp->b_blkno = fsbtodb(fs, blkp->bi_daddr);
489 } else {
490 /* Indirect block */
491 if (blkp->bi_size != fs->lfs_bsize)
492 panic("lfs_markv: partial indirect block?"
493 " size=%d\n", blkp->bi_size);
494 bp = getblk(vp, blkp->bi_lbn, blkp->bi_size, 0, 0);
495 if (!(bp->b_flags & (B_DONE|B_DELWRI))) { /* B_CACHE */
496 /*
497 * The block in question was not found
498 * in the cache; i.e., the block that
499 * getblk() returned is empty. So, we
500 * can (and should) copy in the
501 * contents, because we've already
502 * determined that this was the right
503 * version of this block on disk.
504 *
505 * And, it can't have changed underneath
506 * us, because we have the segment lock.
507 */
508 error = copyin(blkp->bi_bp, bp->b_data, blkp->bi_size);
509 if (error)
510 goto err2;
511 }
512 }
513 if ((error = lfs_bwrite_ext(bp,BW_CLEAN)) != 0)
514 goto err2;
515 }
516
517 /*
518 * Finish the old file, if there was one
519 */
520 if (v_daddr != LFS_UNUSED_DADDR) {
521 #ifdef DEBUG_LFS
522 if (ip->i_flag & (IN_MODIFIED|IN_CLEANING))
523 iwritten++;
524 #endif
525 if (lfs_fastvget_unlock) {
526 VOP_UNLOCK(vp, 0);
527 numlocked--;
528 }
529 lfs_vunref(vp);
530 numrefed--;
531 }
532
533 /*
534 * The last write has to be SEGM_SYNC, because of calling semantics.
535 * It also has to be SEGM_CKP, because otherwise we could write
536 * over the newly cleaned data contained in a checkpoint, and then
537 * we'd be unhappy at recovery time.
538 */
539 lfs_segwrite(mntp, SEGM_CLEAN | SEGM_CKP | SEGM_SYNC);
540
541 lfs_segunlock(fs);
542
543 #ifdef DEBUG_LFS
544 printf("%d]",iwritten);
545 if (numlocked != 0 || numrefed != 0) {
546 panic("lfs_markv: numlocked=%d numrefed=%d", numlocked, numrefed);
547 }
548 #endif
549
550 vfs_unbusy(mntp);
551 if (error)
552 return (error);
553 else if (do_again)
554 return EAGAIN;
555
556 return 0;
557
558 err2:
559 printf("lfs_markv err2\n");
560 if (lfs_fastvget_unlock) {
561 VOP_UNLOCK(vp, 0);
562 --numlocked;
563 }
564 lfs_vunref(vp);
565 --numrefed;
566
567 /* Free up fakebuffers -- have to take these from the LOCKED list */
568 again:
569 s = splbio();
570 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp; bp = nbp) {
571 nbp = bp->b_freelist.tqe_next;
572 if (bp->b_flags & B_CALL) {
573 if (bp->b_flags & B_BUSY) { /* not bloody likely */
574 bp->b_flags |= B_WANTED;
575 tsleep(bp, PRIBIO+1, "markv", 0);
576 splx(s);
577 goto again;
578 }
579 if (bp->b_flags & B_DELWRI)
580 fs->lfs_avail += btofsb(fs, bp->b_bcount);
581 bremfree(bp);
582 splx(s);
583 brelse(bp);
584 s = splbio();
585 }
586 }
587 splx(s);
588 lfs_segunlock(fs);
589 vfs_unbusy(mntp);
590 #ifdef DEBUG_LFS
591 if (numlocked != 0 || numrefed != 0) {
592 panic("lfs_markv: numlocked=%d numrefed=%d", numlocked, numrefed);
593 }
594 #endif
595
596 return (error);
597 }
598
599 /*
600 * sys_lfs_bmapv:
601 *
602 * This will fill in the current disk address for arrays of blocks.
603 *
604 * 0 on success
605 * -1/errno is return on error.
606 */
607 #ifdef USE_64BIT_SYSCALLS
608 int
609 sys_lfs_bmapv(struct proc *p, void *v, register_t *retval)
610 {
611 struct sys_lfs_bmapv_args /* {
612 syscallarg(fsid_t *) fsidp;
613 syscallarg(struct block_info *) blkiov;
614 syscallarg(int) blkcnt;
615 } */ *uap = v;
616 BLOCK_INFO *blkiov;
617 int blkcnt, error;
618 fsid_t fsid;
619
620 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
621 return (error);
622
623 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
624 return (error);
625
626 blkcnt = SCARG(uap, blkcnt);
627 if ((u_int) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
628 return (EINVAL);
629 blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
630 if ((error = copyin(SCARG(uap, blkiov), blkiov,
631 blkcnt * sizeof(BLOCK_INFO))) != 0)
632 goto out;
633
634 if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0)
635 copyout(blkiov, SCARG(uap, blkiov),
636 blkcnt * sizeof(BLOCK_INFO));
637 out:
638 free(blkiov, M_SEGMENT);
639 return error;
640 }
641 #else
642 int
643 sys_lfs_bmapv(struct proc *p, void *v, register_t *retval)
644 {
645 struct sys_lfs_bmapv_args /* {
646 syscallarg(fsid_t *) fsidp;
647 syscallarg(struct block_info *) blkiov;
648 syscallarg(int) blkcnt;
649 } */ *uap = v;
650 BLOCK_INFO *blkiov;
651 BLOCK_INFO_15 *blkiov15;
652 int i, blkcnt, error;
653 fsid_t fsid;
654
655 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
656 return (error);
657
658 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
659 return (error);
660
661 blkcnt = SCARG(uap, blkcnt);
662 if ((u_int) blkcnt > SIZE_T_MAX / sizeof(BLOCK_INFO))
663 return (EINVAL);
664 blkiov = malloc(blkcnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
665 blkiov15 = malloc(blkcnt * sizeof(BLOCK_INFO_15), M_SEGMENT, M_WAITOK);
666 if ((error = copyin(SCARG(uap, blkiov), blkiov15,
667 blkcnt * sizeof(BLOCK_INFO_15))) != 0)
668 goto out;
669
670 for (i = 0; i < blkcnt; i++) {
671 blkiov[i].bi_inode = blkiov15[i].bi_inode;
672 blkiov[i].bi_lbn = blkiov15[i].bi_lbn;
673 blkiov[i].bi_daddr = blkiov15[i].bi_daddr;
674 blkiov[i].bi_segcreate = blkiov15[i].bi_segcreate;
675 blkiov[i].bi_version = blkiov15[i].bi_version;
676 blkiov[i].bi_bp = blkiov15[i].bi_bp;
677 blkiov[i].bi_size = blkiov15[i].bi_size;
678 }
679
680 if ((error = lfs_bmapv(p, &fsid, blkiov, blkcnt)) == 0) {
681 for (i = 0; i < blkcnt; i++) {
682 blkiov15[i].bi_inode = blkiov[i].bi_inode;
683 blkiov15[i].bi_lbn = blkiov[i].bi_lbn;
684 blkiov15[i].bi_daddr = blkiov[i].bi_daddr;
685 blkiov15[i].bi_segcreate = blkiov[i].bi_segcreate;
686 blkiov15[i].bi_version = blkiov[i].bi_version;
687 blkiov15[i].bi_bp = blkiov[i].bi_bp;
688 blkiov15[i].bi_size = blkiov[i].bi_size;
689 }
690 copyout(blkiov15, SCARG(uap, blkiov),
691 blkcnt * sizeof(BLOCK_INFO_15));
692 }
693 out:
694 free(blkiov, M_SEGMENT);
695 free(blkiov15, M_SEGMENT);
696 return error;
697 }
698 #endif
699
700 static int
701 lfs_bmapv(struct proc *p, fsid_t *fsidp, BLOCK_INFO *blkiov, int blkcnt)
702 {
703 BLOCK_INFO *blkp;
704 IFILE *ifp;
705 struct buf *bp;
706 struct inode *ip = NULL;
707 struct lfs *fs;
708 struct mount *mntp;
709 struct ufsmount *ump;
710 struct vnode *vp;
711 ino_t lastino;
712 ufs_daddr_t v_daddr;
713 int cnt, error, need_unlock = 0;
714 int numlocked = 0, numrefed = 0;
715
716 lfs_cleaner_pid = p->p_pid;
717
718 if ((mntp = vfs_getvfs(fsidp)) == NULL)
719 return (ENOENT);
720
721 ump = VFSTOUFS(mntp);
722 if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
723 return (error);
724
725 cnt = blkcnt;
726
727 fs = VFSTOUFS(mntp)->um_lfs;
728
729 error = 0;
730
731 /* these were inside the initialization for the for loop */
732 v_daddr = LFS_UNUSED_DADDR;
733 lastino = LFS_UNUSED_INUM;
734 for (blkp = blkiov; cnt--; ++blkp)
735 {
736 /*
737 * Get the IFILE entry (only once) and see if the file still
738 * exists.
739 */
740 if (lastino != blkp->bi_inode) {
741 /*
742 * Finish the old file, if there was one. The presence
743 * of a usable vnode in vp is signaled by a valid
744 * v_daddr.
745 */
746 if (v_daddr != LFS_UNUSED_DADDR) {
747 if (need_unlock) {
748 VOP_UNLOCK(vp, 0);
749 numlocked--;
750 }
751 lfs_vunref(vp);
752 numrefed--;
753 }
754
755 /*
756 * Start a new file
757 */
758 lastino = blkp->bi_inode;
759 if (blkp->bi_inode == LFS_IFILE_INUM)
760 v_daddr = fs->lfs_idaddr;
761 else {
762 LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
763 v_daddr = ifp->if_daddr;
764 brelse(bp);
765 }
766 if (v_daddr == LFS_UNUSED_DADDR) {
767 blkp->bi_daddr = LFS_UNUSED_DADDR;
768 continue;
769 }
770 /*
771 * A regular call to VFS_VGET could deadlock
772 * here. Instead, we try an unlocked access.
773 */
774 vp = ufs_ihashlookup(ump->um_dev, blkp->bi_inode);
775 if (vp != NULL && !(vp->v_flag & VXLOCK)) {
776 ip = VTOI(vp);
777 if (lfs_vref(vp)) {
778 v_daddr = LFS_UNUSED_DADDR;
779 need_unlock = 0;
780 continue;
781 }
782 numrefed++;
783 if (VOP_ISLOCKED(vp)) {
784 #ifdef DEBUG_LFS
785 printf("lfs_bmapv: inode %d inlocked\n",ip->i_number);
786 #endif
787 v_daddr = LFS_UNUSED_DADDR;
788 need_unlock = 0;
789 lfs_vunref(vp);
790 --numrefed;
791 continue;
792 } else {
793 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
794 need_unlock = FVG_UNLOCK;
795 numlocked++;
796 }
797 } else {
798 error = VFS_VGET(mntp, blkp->bi_inode, &vp);
799 if (error) {
800 #ifdef DEBUG_LFS
801 printf("lfs_bmapv: vget of ino %d failed with %d",blkp->bi_inode,error);
802 #endif
803 v_daddr = LFS_UNUSED_DADDR;
804 need_unlock = 0;
805 continue;
806 } else {
807 need_unlock = FVG_PUT;
808 numlocked++;
809 numrefed++;
810 }
811 }
812 ip = VTOI(vp);
813 } else if (v_daddr == LFS_UNUSED_DADDR) {
814 /*
815 * This can only happen if the vnode is dead.
816 * Keep going. Note that we DO NOT set the
817 * bi_addr to anything -- if we failed to get
818 * the vnode, for example, we want to assume
819 * conservatively that all of its blocks *are*
820 * located in the segment in question.
821 * lfs_markv will throw them out if we are
822 * wrong.
823 */
824 /* blkp->bi_daddr = LFS_UNUSED_DADDR; */
825 continue;
826 }
827
828 /* Past this point we are guaranteed that vp, ip are valid. */
829
830 if (blkp->bi_lbn == LFS_UNUSED_LBN) {
831 /*
832 * We just want the inode address, which is
833 * conveniently in v_daddr.
834 */
835 blkp->bi_daddr = v_daddr;
836 } else {
837 error = VOP_BMAP(vp, blkp->bi_lbn, NULL,
838 &(blkp->bi_daddr), NULL);
839 if (error)
840 {
841 blkp->bi_daddr = LFS_UNUSED_DADDR;
842 continue;
843 }
844 blkp->bi_daddr = dbtofsb(fs, blkp->bi_daddr);
845 /* Fill in the block size, too */
846 if (blkp->bi_lbn >= 0)
847 blkp->bi_size = blksize(fs, ip, blkp->bi_lbn);
848 else
849 blkp->bi_size = fs->lfs_bsize;
850 }
851 }
852
853 /*
854 * Finish the old file, if there was one. The presence
855 * of a usable vnode in vp is signaled by a valid v_daddr.
856 */
857 if (v_daddr != LFS_UNUSED_DADDR) {
858 if (need_unlock) {
859 VOP_UNLOCK(vp, 0);
860 numlocked--;
861 }
862 lfs_vunref(vp);
863 numrefed--;
864 }
865
866 if (numlocked != 0 || numrefed != 0) {
867 panic("lfs_bmapv: numlocked=%d numrefed=%d", numlocked,
868 numrefed);
869 }
870
871 vfs_unbusy(mntp);
872
873 return 0;
874 }
875
876 /*
877 * sys_lfs_segclean:
878 *
879 * Mark the segment clean.
880 *
881 * 0 on success
882 * -1/errno is return on error.
883 */
884 int
885 sys_lfs_segclean(struct proc *p, void *v, register_t *retval)
886 {
887 struct sys_lfs_segclean_args /* {
888 syscallarg(fsid_t *) fsidp;
889 syscallarg(u_long) segment;
890 } */ *uap = v;
891 CLEANERINFO *cip;
892 SEGUSE *sup;
893 struct buf *bp;
894 struct mount *mntp;
895 struct lfs *fs;
896 fsid_t fsid;
897 int error;
898 unsigned long segnum;
899
900 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
901 return (error);
902
903 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
904 return (error);
905 if ((mntp = vfs_getvfs(&fsid)) == NULL)
906 return (ENOENT);
907
908 fs = VFSTOUFS(mntp)->um_lfs;
909 segnum = SCARG(uap, segment);
910
911 if (dtosn(fs, fs->lfs_curseg) == segnum)
912 return (EBUSY);
913
914 if ((error = vfs_busy(mntp, LK_NOWAIT, NULL)) != 0)
915 return (error);
916 #ifdef LFS_AGGRESSIVE_SEGLOCK
917 lfs_seglock(fs, SEGM_PROT);
918 #endif
919 LFS_SEGENTRY(sup, fs, segnum, bp);
920 if (sup->su_nbytes) {
921 printf("lfs_segclean: not cleaning segment %lu: %d live bytes\n",
922 segnum, sup->su_nbytes);
923 brelse(bp);
924 #ifdef LFS_AGGRESSIVE_SEGLOCK
925 lfs_segunlock(fs);
926 #endif
927 vfs_unbusy(mntp);
928 return (EBUSY);
929 }
930 if (sup->su_flags & SEGUSE_ACTIVE) {
931 brelse(bp);
932 #ifdef LFS_AGGRESSIVE_SEGLOCK
933 lfs_segunlock(fs);
934 #endif
935 vfs_unbusy(mntp);
936 return (EBUSY);
937 }
938 if (!(sup->su_flags & SEGUSE_DIRTY)) {
939 brelse(bp);
940 #ifdef LFS_AGGRESSIVE_SEGLOCK
941 lfs_segunlock(fs);
942 #endif
943 vfs_unbusy(mntp);
944 return (EALREADY);
945 }
946
947 fs->lfs_avail += segtod(fs, 1);
948 if (sup->su_flags & SEGUSE_SUPERBLOCK)
949 fs->lfs_avail -= btofsb(fs, LFS_SBPAD);
950 if (fs->lfs_version > 1 && segnum == 0 &&
951 fs->lfs_start < btofsb(fs, LFS_LABELPAD))
952 fs->lfs_avail -= btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
953 fs->lfs_bfree += sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
954 btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
955 fs->lfs_dmeta -= sup->su_nsums * btofsb(fs, fs->lfs_sumsize) +
956 btofsb(fs, sup->su_ninos * fs->lfs_ibsize);
957 if (fs->lfs_dmeta < 0)
958 fs->lfs_dmeta = 0;
959 sup->su_flags &= ~SEGUSE_DIRTY;
960 (void) LFS_BWRITE_LOG(bp);
961
962 LFS_CLEANERINFO(cip, fs, bp);
963 ++cip->clean;
964 --cip->dirty;
965 fs->lfs_nclean = cip->clean;
966 cip->bfree = fs->lfs_bfree;
967 cip->avail = fs->lfs_avail - fs->lfs_ravail;
968 (void) LFS_BWRITE_LOG(bp);
969 wakeup(&fs->lfs_avail);
970 #ifdef LFS_AGGRESSIVE_SEGLOCK
971 lfs_segunlock(fs);
972 #endif
973 vfs_unbusy(mntp);
974
975 return (0);
976 }
977
978 /*
979 * sys_lfs_segwait:
980 *
981 * This will block until a segment in file system fsid is written. A timeout
982 * in milliseconds may be specified which will awake the cleaner automatically.
983 * An fsid of -1 means any file system, and a timeout of 0 means forever.
984 *
985 * 0 on success
986 * 1 on timeout
987 * -1/errno is return on error.
988 */
989 int
990 sys_lfs_segwait(struct proc *p, void *v, register_t *retval)
991 {
992 struct sys_lfs_segwait_args /* {
993 syscallarg(fsid_t *) fsidp;
994 syscallarg(struct timeval *) tv;
995 } */ *uap = v;
996 struct mount *mntp;
997 struct timeval atv;
998 fsid_t fsid;
999 void *addr;
1000 u_long timeout;
1001 int error, s;
1002
1003 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
1004 return (error);
1005 }
1006 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
1007 return (error);
1008 if ((mntp = vfs_getvfs(&fsid)) == NULL)
1009 addr = &lfs_allclean_wakeup;
1010 else
1011 addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
1012
1013 if (SCARG(uap, tv)) {
1014 error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
1015 if (error)
1016 return (error);
1017 if (itimerfix(&atv))
1018 return (EINVAL);
1019 /*
1020 * XXX THIS COULD SLEEP FOREVER IF TIMEOUT IS {0,0}!
1021 * XXX IS THAT WHAT IS INTENDED?
1022 */
1023 s = splclock();
1024 timeradd(&atv, &time, &atv);
1025 timeout = hzto(&atv);
1026 splx(s);
1027 } else
1028 timeout = 0;
1029
1030 error = tsleep(addr, PCATCH | PUSER, "segment", timeout);
1031 return (error == ERESTART ? EINTR : 0);
1032 }
1033
1034 /*
1035 * VFS_VGET call specialized for the cleaner. The cleaner already knows the
1036 * daddr from the ifile, so don't look it up again. If the cleaner is
1037 * processing IINFO structures, it may have the ondisk inode already, so
1038 * don't go retrieving it again.
1039 *
1040 * If we find the vnode on the hash chain, then it may be locked by another
1041 * process; so we set (*need_unlock) to zero.
1042 *
1043 * If we don't, we call ufs_ihashins, which locks the inode, and we set
1044 * (*need_unlock) to non-zero.
1045 *
1046 * In either case we lfs_vref, and it is the caller's responsibility to
1047 * lfs_vunref and VOP_UNLOCK (if necessary) when finished.
1048 */
1049 extern struct lock ufs_hashlock;
1050
1051 int
1052 lfs_fasthashget(dev_t dev, ino_t ino, int *need_unlock, struct vnode **vpp)
1053 {
1054 struct inode *ip;
1055
1056 /*
1057 * This is playing fast and loose. Someone may have the inode
1058 * locked, in which case they are going to be distinctly unhappy
1059 * if we trash something.
1060 */
1061 if ((*vpp = ufs_ihashlookup(dev, ino)) != NULL) {
1062 if ((*vpp)->v_flag & VXLOCK) {
1063 printf("lfs_fastvget: vnode VXLOCKed for ino %d\n",
1064 ino);
1065 clean_vnlocked++;
1066 #ifdef LFS_EAGAIN_FAIL
1067 return EAGAIN;
1068 #endif
1069 }
1070 ip = VTOI(*vpp);
1071 if (lfs_vref(*vpp)) {
1072 clean_inlocked++;
1073 return EAGAIN;
1074 }
1075 if (VOP_ISLOCKED(*vpp)) {
1076 #ifdef DEBUG_LFS
1077 printf("lfs_fastvget: ino %d inlocked by pid %d\n",
1078 ip->i_number, (*vpp)->v_lock.lk_lockholder);
1079 #endif
1080 clean_inlocked++;
1081 #ifdef LFS_EAGAIN_FAIL
1082 lfs_vunref(*vpp);
1083 return EAGAIN;
1084 #endif /* LFS_EAGAIN_FAIL */
1085 } else {
1086 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1087 *need_unlock |= FVG_UNLOCK;
1088 }
1089 } else
1090 *vpp = NULL;
1091
1092 return (0);
1093 }
1094
1095 int
1096 lfs_fastvget(struct mount *mp, ino_t ino, ufs_daddr_t daddr, struct vnode **vpp, struct dinode *dinp, int *need_unlock)
1097 {
1098 struct inode *ip;
1099 struct dinode *dip;
1100 struct vnode *vp;
1101 struct ufsmount *ump;
1102 dev_t dev;
1103 int i, error, retries;
1104 struct buf *bp;
1105 struct lfs *fs;
1106
1107 ump = VFSTOUFS(mp);
1108 dev = ump->um_dev;
1109 fs = ump->um_lfs;
1110 *need_unlock = 0;
1111
1112 /*
1113 * Wait until the filesystem is fully mounted before allowing vget
1114 * to complete. This prevents possible problems with roll-forward.
1115 */
1116 while (fs->lfs_flags & LFS_NOTYET) {
1117 tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_fnotyet", 0);
1118 }
1119 /*
1120 * This is playing fast and loose. Someone may have the inode
1121 * locked, in which case they are going to be distinctly unhappy
1122 * if we trash something.
1123 */
1124
1125 error = lfs_fasthashget(dev, ino, need_unlock, vpp);
1126 if (error != 0 || *vpp != NULL)
1127 return (error);
1128
1129 if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
1130 *vpp = NULL;
1131 return (error);
1132 }
1133
1134 do {
1135 error = lfs_fasthashget(dev, ino, need_unlock, vpp);
1136 if (error != 0 || *vpp != NULL) {
1137 ungetnewvnode(vp);
1138 return (error);
1139 }
1140 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1141
1142 /* Allocate new vnode/inode. */
1143 lfs_vcreate(mp, ino, vp);
1144
1145 /*
1146 * Put it onto its hash chain and lock it so that other requests for
1147 * this inode will block if they arrive while we are sleeping waiting
1148 * for old data structures to be purged or for the contents of the
1149 * disk portion of this inode to be read.
1150 */
1151 ip = VTOI(vp);
1152 ufs_ihashins(ip);
1153 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1154
1155 /*
1156 * XXX
1157 * This may not need to be here, logically it should go down with
1158 * the i_devvp initialization.
1159 * Ask Kirk.
1160 */
1161 ip->i_lfs = fs;
1162
1163 /* Read in the disk contents for the inode, copy into the inode. */
1164 if (dinp) {
1165 error = copyin(dinp, &ip->i_din.ffs_din, DINODE_SIZE);
1166 if (error) {
1167 printf("lfs_fastvget: dinode copyin failed for ino %d\n", ino);
1168 ufs_ihashrem(ip);
1169
1170 /* Unlock and discard unneeded inode. */
1171 lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
1172 lfs_vunref(vp);
1173 *vpp = NULL;
1174 return (error);
1175 }
1176 if (ip->i_number != ino)
1177 panic("lfs_fastvget: I was fed the wrong inode!");
1178 } else {
1179 retries = 0;
1180 again:
1181 error = bread(ump->um_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
1182 NOCRED, &bp);
1183 if (error) {
1184 printf("lfs_fastvget: bread failed with %d\n",error);
1185 /*
1186 * The inode does not contain anything useful, so it
1187 * would be misleading to leave it on its hash chain.
1188 * Iput() will return it to the free list.
1189 */
1190 ufs_ihashrem(ip);
1191
1192 /* Unlock and discard unneeded inode. */
1193 lockmgr(&vp->v_lock, LK_RELEASE, &vp->v_interlock);
1194 lfs_vunref(vp);
1195 brelse(bp);
1196 *vpp = NULL;
1197 return (error);
1198 }
1199 dip = lfs_ifind(ump->um_lfs, ino, bp);
1200 if (dip == NULL) {
1201 /* Assume write has not completed yet; try again */
1202 bp->b_flags |= B_INVAL;
1203 brelse(bp);
1204 ++retries;
1205 if (retries > LFS_IFIND_RETRIES)
1206 panic("lfs_fastvget: dinode not found");
1207 printf("lfs_fastvget: dinode not found, retrying...\n");
1208 goto again;
1209 }
1210 ip->i_din.ffs_din = *dip;
1211 brelse(bp);
1212 }
1213 ip->i_ffs_effnlink = ip->i_ffs_nlink;
1214 ip->i_lfs_effnblks = ip->i_ffs_blocks;
1215 ip->i_lfs_osize = ip->i_ffs_size;
1216
1217 memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
1218 for (i = 0; i < NDADDR; i++)
1219 if (ip->i_ffs_db[i] != 0)
1220 ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
1221
1222 /*
1223 * Initialize the vnode from the inode, check for aliases. In all
1224 * cases re-init ip, the underlying vnode/inode may have changed.
1225 */
1226 ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
1227 #ifdef DEBUG_LFS
1228 if (vp->v_type == VNON) {
1229 printf("lfs_fastvget: ino %d is type VNON! (ifmt=%o, dinp=%p)\n",
1230 ip->i_number, (ip->i_ffs_mode & IFMT) >> 12, dinp);
1231 lfs_dump_dinode(&ip->i_din.ffs_din);
1232 #ifdef DDB
1233 Debugger();
1234 #endif
1235 }
1236 #endif /* DEBUG_LFS */
1237 /*
1238 * Finish inode initialization now that aliasing has been resolved.
1239 */
1240
1241 genfs_node_init(vp, &lfs_genfsops);
1242 ip->i_devvp = ump->um_devvp;
1243 VREF(ip->i_devvp);
1244 *vpp = vp;
1245 *need_unlock |= FVG_PUT;
1246
1247 uvm_vnp_setsize(vp, ip->i_ffs_size);
1248
1249 return (0);
1250 }
1251
1252 static void
1253 lfs_fakebuf_iodone(struct buf *bp)
1254 {
1255 struct buf *obp = bp->b_saveaddr;
1256
1257 if (!(obp->b_flags & (B_DELWRI | B_DONE)))
1258 obp->b_flags |= B_INVAL;
1259 brelse(obp);
1260 lfs_callback(bp);
1261 }
1262
1263 struct buf *
1264 lfs_fakebuf(struct lfs *fs, struct vnode *vp, int lbn, size_t size, caddr_t uaddr)
1265 {
1266 struct buf *bp;
1267 int error;
1268
1269 struct buf *obp;
1270
1271 /*
1272 * make corresponding buffer busy to avoid
1273 * reading blocks that isn't written yet.
1274 * it's needed because we'll update metadatas in lfs_updatemeta
1275 * before data pointed by them is actually written to disk.
1276 * XXX no need to allocbuf.
1277 */
1278 obp = getblk(vp, lbn, size, 0, 0);
1279 if (obp == NULL)
1280 panic("lfs_fakebuf: getblk failed");
1281
1282 #ifndef ALLOW_VFLUSH_CORRUPTION
1283 bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, size);
1284 error = copyin(uaddr, bp->b_data, size);
1285 if (error) {
1286 lfs_freebuf(bp);
1287 return NULL;
1288 }
1289 bp->b_saveaddr = obp;
1290 KDASSERT(bp->b_iodone == lfs_callback);
1291 bp->b_iodone = lfs_fakebuf_iodone;
1292
1293 #ifdef DIAGNOSTIC
1294 if (obp->b_flags & B_GATHERED)
1295 panic("lfs_fakebuf: gathered bp: %p, ino=%u, lbn=%d",
1296 bp, VTOI(vp)->i_number, lbn);
1297 #endif
1298 #else
1299 bp = lfs_newbuf(VTOI(vp)->i_lfs, vp, lbn, 0);
1300 bp->b_flags |= B_INVAL;
1301 bp->b_saveaddr = uaddr;
1302 #endif
1303 #if 0
1304 bp->b_saveaddr = (caddr_t)fs;
1305 ++fs->lfs_iocount;
1306 #endif
1307 bp->b_bufsize = size;
1308 bp->b_bcount = size;
1309 return (bp);
1310 }
1311