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