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