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