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