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