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