lfs_syscalls.c revision 1.18 1 /* $NetBSD: lfs_syscalls.c,v 1.18 1998/06/24 20:58:48 sommerfe Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)lfs_syscalls.c 8.10 (Berkeley) 5/14/95
36 */
37
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "fs_lfs.h" /* for prototypes in syscallargs.h */
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/buf.h>
46 #include <sys/mount.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50
51 #include <sys/syscallargs.h>
52
53 #include <ufs/ufs/quota.h>
54 #include <ufs/ufs/inode.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/ufs_extern.h>
57
58 #include <ufs/lfs/lfs.h>
59 #include <ufs/lfs/lfs_extern.h>
60
61 #define BUMP_FIP(SP) \
62 (SP)->fip = (FINFO *) (&(SP)->fip->fi_blocks[(SP)->fip->fi_nblocks])
63
64 #define INC_FINFO(SP) ++((SEGSUM *)((SP)->segsum))->ss_nfinfo
65 #define DEC_FINFO(SP) --((SEGSUM *)((SP)->segsum))->ss_nfinfo
66
67 /*
68 * Before committing to add something to a segment summary, make sure there
69 * is enough room. S is the bytes added to the summary.
70 */
71 #define CHECK_SEG(s) \
72 if (sp->sum_bytes_left < (s)) { \
73 (void) lfs_writeseg(fs, sp); \
74 }
75 struct buf *lfs_fakebuf __P((struct vnode *, int, size_t, caddr_t));
76
77 int debug_cleaner = 0;
78 int clean_vnlocked = 0;
79 int clean_inlocked = 0;
80
81 /*
82 * lfs_markv:
83 *
84 * This will mark inodes and blocks dirty, so they are written into the log.
85 * It will block until all the blocks have been written. The segment create
86 * time passed in the block_info and inode_info structures is used to decide
87 * if the data is valid for each block (in case some process dirtied a block
88 * or inode that is being cleaned between the determination that a block is
89 * live and the lfs_markv call).
90 *
91 * 0 on success
92 * -1/errno is return on error.
93 */
94 int
95 lfs_markv(p, v, retval)
96 struct proc *p;
97 void *v;
98 register_t *retval;
99 {
100 struct lfs_markv_args /* {
101 syscallarg(fsid_t *) fsidp;
102 syscallarg(struct block_info *) blkiov;
103 syscallarg(int) blkcnt;
104 } */ *uap = v;
105 struct segment *sp;
106 BLOCK_INFO *blkp;
107 IFILE *ifp;
108 struct buf *bp, **bpp;
109 struct inode *ip = NULL;
110 struct lfs *fs;
111 struct mount *mntp;
112 struct vnode *vp;
113 fsid_t fsid;
114 void *start;
115 ino_t lastino;
116 ufs_daddr_t b_daddr, v_daddr;
117 u_long bsize;
118 int cnt, error;
119
120 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
121 return (error);
122
123 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
124 return (error);
125 if ((mntp = vfs_getvfs(&fsid)) == NULL)
126 return (EINVAL);
127
128 cnt = SCARG(uap, blkcnt);
129 start = malloc(cnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
130 error = copyin(SCARG(uap, blkiov), start, cnt * sizeof(BLOCK_INFO));
131 if (error)
132 goto err1;
133
134 /* Mark blocks/inodes dirty. */
135 fs = VFSTOUFS(mntp)->um_lfs;
136 bsize = fs->lfs_bsize;
137 error = 0;
138
139 lfs_seglock(fs, SEGM_SYNC | SEGM_CLEAN);
140 sp = fs->lfs_sp;
141 for (v_daddr = LFS_UNUSED_DADDR, lastino = LFS_UNUSED_INUM,
142 blkp = start; cnt--; ++blkp) {
143 /*
144 * Get the IFILE entry (only once) and see if the file still
145 * exists.
146 */
147 if (lastino != blkp->bi_inode) {
148 if (lastino != LFS_UNUSED_INUM) {
149 /* Finish up last file */
150 if (sp->fip->fi_nblocks == 0) {
151 DEC_FINFO(sp);
152 sp->sum_bytes_left +=
153 sizeof(FINFO) - sizeof(ufs_daddr_t);
154 } else {
155 lfs_updatemeta(sp);
156 BUMP_FIP(sp);
157 }
158
159 lfs_writeinode(fs, sp, ip);
160 lfs_vunref(vp);
161 }
162
163 /* Start a new file */
164 CHECK_SEG(sizeof(FINFO));
165 sp->sum_bytes_left -= sizeof(FINFO) - sizeof(ufs_daddr_t);
166 INC_FINFO(sp);
167 sp->start_lbp = &sp->fip->fi_blocks[0];
168 sp->vp = NULL;
169 sp->fip->fi_version = blkp->bi_version;
170 sp->fip->fi_nblocks = 0;
171 sp->fip->fi_ino = blkp->bi_inode;
172 lastino = blkp->bi_inode;
173 if (blkp->bi_inode == LFS_IFILE_INUM)
174 v_daddr = fs->lfs_idaddr;
175 else {
176 LFS_IENTRY(ifp, fs, blkp->bi_inode, bp);
177 v_daddr = ifp->if_daddr;
178 brelse(bp);
179 }
180 if (v_daddr == LFS_UNUSED_DADDR)
181 continue;
182
183 /* Get the vnode/inode. */
184 if (lfs_fastvget(mntp, blkp->bi_inode, v_daddr, &vp,
185 blkp->bi_lbn == LFS_UNUSED_LBN ?
186 blkp->bi_bp : NULL)) {
187 #ifdef DIAGNOSTIC
188 printf("lfs_markv: VFS_VGET failed (%d)\n",
189 blkp->bi_inode);
190 panic("lfs_markv VFS_VGET FAILED");
191 #endif
192 lastino = LFS_UNUSED_INUM;
193 v_daddr = LFS_UNUSED_DADDR;
194 continue;
195 }
196 sp->vp = vp;
197 ip = VTOI(vp);
198 } else if (v_daddr == LFS_UNUSED_DADDR)
199 continue;
200
201 /* If this BLOCK_INFO didn't contain a block, keep going. */
202 if (blkp->bi_lbn == LFS_UNUSED_LBN)
203 continue;
204 if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &b_daddr, NULL) ||
205 b_daddr != blkp->bi_daddr)
206 continue;
207 /*
208 * If we got to here, then we are keeping the block. If it
209 * is an indirect block, we want to actually put it in the
210 * buffer cache so that it can be updated in the finish_meta
211 * section. If it's not, we need to allocate a fake buffer
212 * so that writeseg can perform the copyin and write the buffer.
213 */
214 if (blkp->bi_lbn >= 0) /* Data Block */
215 bp = lfs_fakebuf(vp, blkp->bi_lbn, bsize,
216 blkp->bi_bp);
217 else {
218 bp = getblk(vp, blkp->bi_lbn, bsize, 0, 0);
219 if (!(bp->b_flags & (B_DELWRI | B_DONE | B_CACHE)) &&
220 (error = copyin(blkp->bi_bp, bp->b_data,
221 blkp->bi_size)))
222 goto err2;
223 if ((error = VOP_BWRITE(bp)) != 0)
224 goto err2;
225 }
226 while (lfs_gatherblock(sp, bp, NULL));
227 }
228 if (sp->vp) {
229 if (sp->fip->fi_nblocks == 0) {
230 DEC_FINFO(sp);
231 sp->sum_bytes_left +=
232 sizeof(FINFO) - sizeof(ufs_daddr_t);
233 } else
234 lfs_updatemeta(sp);
235
236 lfs_writeinode(fs, sp, ip);
237 lfs_vunref(vp);
238 }
239 (void) lfs_writeseg(fs, sp);
240 lfs_segunlock(fs);
241 free(start, M_SEGMENT);
242 return (error);
243
244 /*
245 * XXX
246 * If we come in to error 2, we might have indirect blocks that were
247 * updated and now have bad block pointers. I don't know what to do
248 * about this.
249 */
250
251 err2: lfs_vunref(vp);
252 /* Free up fakebuffers */
253 for (bpp = --sp->cbpp; bpp >= sp->bpp; --bpp)
254 if ((*bpp)->b_flags & B_CALL) {
255 brelvp(*bpp);
256 free(*bpp, M_SEGMENT);
257 } else
258 brelse(*bpp);
259 lfs_segunlock(fs);
260 err1:
261 free(start, M_SEGMENT);
262 return (error);
263 }
264
265 /*
266 * lfs_bmapv:
267 *
268 * This will fill in the current disk address for arrays of blocks.
269 *
270 * 0 on success
271 * -1/errno is return on error.
272 */
273 int
274 lfs_bmapv(p, v, retval)
275 struct proc *p;
276 void *v;
277 register_t *retval;
278 {
279 struct lfs_bmapv_args /* {
280 syscallarg(fsid_t *) fsidp;
281 syscallarg(struct block_info *) blkiov;
282 syscallarg(int) blkcnt;
283 } */ *uap = v;
284 BLOCK_INFO *blkp;
285 struct mount *mntp;
286 struct ufsmount *ump;
287 struct vnode *vp;
288 fsid_t fsid;
289 void *start;
290 ufs_daddr_t daddr;
291 int cnt, error, step;
292
293 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
294 return (error);
295
296 error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t));
297 if (error)
298 return (error);
299 if ((mntp = vfs_getvfs(&fsid)) == NULL)
300 return (EINVAL);
301
302 cnt = SCARG(uap, blkcnt);
303 start = blkp = malloc(cnt * sizeof(BLOCK_INFO), M_SEGMENT, M_WAITOK);
304 error = copyin(SCARG(uap, blkiov), blkp, cnt * sizeof(BLOCK_INFO));
305 if (error) {
306 free(blkp, M_SEGMENT);
307 return (error);
308 }
309
310 for (step = cnt; step--; ++blkp) {
311 if (blkp->bi_lbn == LFS_UNUSED_LBN)
312 continue;
313 /*
314 * A regular call to VFS_VGET could deadlock
315 * here. Instead, we try an unlocked access.
316 */
317 ump = VFSTOUFS(mntp);
318 if ((vp =
319 ufs_ihashlookup(ump->um_dev, blkp->bi_inode)) != NULL) {
320 if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &daddr, NULL))
321 daddr = LFS_UNUSED_DADDR;
322 } else if (VFS_VGET(mntp, blkp->bi_inode, &vp))
323 daddr = LFS_UNUSED_DADDR;
324 else {
325 if (VOP_BMAP(vp, blkp->bi_lbn, NULL, &daddr, NULL))
326 daddr = LFS_UNUSED_DADDR;
327 vput(vp);
328 }
329 blkp->bi_daddr = daddr;
330 }
331 copyout(start, SCARG(uap, blkiov), cnt * sizeof(BLOCK_INFO));
332 free(start, M_SEGMENT);
333 return (0);
334 }
335
336 /*
337 * lfs_segclean:
338 *
339 * Mark the segment clean.
340 *
341 * 0 on success
342 * -1/errno is return on error.
343 */
344 int
345 lfs_segclean(p, v, retval)
346 struct proc *p;
347 void *v;
348 register_t *retval;
349 {
350 struct lfs_segclean_args /* {
351 syscallarg(fsid_t *) fsidp;
352 syscallarg(u_long) segment;
353 } */ *uap = v;
354 CLEANERINFO *cip;
355 SEGUSE *sup;
356 struct buf *bp;
357 struct mount *mntp;
358 struct lfs *fs;
359 fsid_t fsid;
360 int error;
361
362 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
363 return (error);
364
365 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
366 return (error);
367 if ((mntp = vfs_getvfs(&fsid)) == NULL)
368 return (EINVAL);
369
370 fs = VFSTOUFS(mntp)->um_lfs;
371
372 if (datosn(fs, fs->lfs_curseg) == SCARG(uap, segment))
373 return (EBUSY);
374
375 LFS_SEGENTRY(sup, fs, SCARG(uap, segment), bp);
376 if (sup->su_flags & SEGUSE_ACTIVE) {
377 brelse(bp);
378 return (EBUSY);
379 }
380 fs->lfs_avail += fsbtodb(fs, fs->lfs_ssize) - 1;
381 fs->lfs_bfree += (sup->su_nsums * LFS_SUMMARY_SIZE / DEV_BSIZE) +
382 sup->su_ninos * btodb(fs->lfs_bsize);
383 sup->su_flags &= ~SEGUSE_DIRTY;
384 (void) VOP_BWRITE(bp);
385
386 LFS_CLEANERINFO(cip, fs, bp);
387 ++cip->clean;
388 --cip->dirty;
389 (void) VOP_BWRITE(bp);
390 wakeup(&fs->lfs_avail);
391 return (0);
392 }
393
394 /*
395 * lfs_segwait:
396 *
397 * This will block until a segment in file system fsid is written. A timeout
398 * in milliseconds may be specified which will awake the cleaner automatically.
399 * An fsid of -1 means any file system, and a timeout of 0 means forever.
400 *
401 * 0 on success
402 * 1 on timeout
403 * -1/errno is return on error.
404 */
405 int
406 lfs_segwait(p, v, retval)
407 struct proc *p;
408 void *v;
409 register_t *retval;
410 {
411 struct lfs_segwait_args /* {
412 syscallarg(fsid_t *) fsidp;
413 syscallarg(struct timeval *) tv;
414 } */ *uap = v;
415 extern int lfs_allclean_wakeup;
416 struct mount *mntp;
417 struct timeval atv;
418 fsid_t fsid;
419 void *addr;
420 u_long timeout;
421 int error, s;
422
423 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
424 return (error);
425 }
426 #ifdef WHEN_QUADS_WORK
427 if (error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t)))
428 return (error);
429 if (fsid == (fsid_t)-1)
430 addr = &lfs_allclean_wakeup;
431 else {
432 if ((mntp = vfs_getvfs(&fsid)) == NULL)
433 return (EINVAL);
434 addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
435 }
436 #else
437 if ((error = copyin(SCARG(uap, fsidp), &fsid, sizeof(fsid_t))) != 0)
438 return (error);
439 if ((mntp = vfs_getvfs(&fsid)) == NULL)
440 addr = &lfs_allclean_wakeup;
441 else
442 addr = &VFSTOUFS(mntp)->um_lfs->lfs_nextseg;
443 #endif
444
445 if (SCARG(uap, tv)) {
446 error = copyin(SCARG(uap, tv), &atv, sizeof(struct timeval));
447 if (error)
448 return (error);
449 if (itimerfix(&atv))
450 return (EINVAL);
451 s = splclock();
452 timeradd(&atv, &time, &atv);
453 timeout = hzto(&atv);
454 splx(s);
455 } else
456 timeout = 0;
457
458 error = tsleep(addr, PCATCH | PUSER, "segment", timeout);
459 return (error == ERESTART ? EINTR : 0);
460 }
461
462 /*
463 * VFS_VGET call specialized for the cleaner. The cleaner already knows the
464 * daddr from the ifile, so don't look it up again. If the cleaner is
465 * processing IINFO structures, it may have the ondisk inode already, so
466 * don't go retrieving it again.
467 */
468 int
469 lfs_fastvget(mp, ino, daddr, vpp, dinp)
470 struct mount *mp;
471 ino_t ino;
472 ufs_daddr_t daddr;
473 struct vnode **vpp;
474 struct dinode *dinp;
475 {
476 register struct inode *ip;
477 struct vnode *vp;
478 struct ufsmount *ump;
479 struct buf *bp;
480 dev_t dev;
481 int error;
482
483 ump = VFSTOUFS(mp);
484 dev = ump->um_dev;
485 /*
486 * This is playing fast and loose. Someone may have the inode
487 * locked, in which case they are going to be distinctly unhappy
488 * if we trash something.
489 */
490 if ((*vpp = ufs_ihashlookup(dev, ino)) != NULL) {
491 lfs_vref(*vpp);
492 if ((*vpp)->v_flag & VXLOCK)
493 clean_vnlocked++;
494 ip = VTOI(*vpp);
495 if (lockstatus(&ip->i_lock))
496 clean_inlocked++;
497 if (!(ip->i_flag & IN_MODIFIED))
498 ++ump->um_lfs->lfs_uinodes;
499 ip->i_flag |= IN_MODIFIED;
500 return (0);
501 }
502
503 /* Allocate new vnode/inode. */
504 if ((error = lfs_vcreate(mp, ino, &vp)) != 0) {
505 *vpp = NULL;
506 return (error);
507 }
508
509 /*
510 * Put it onto its hash chain and lock it so that other requests for
511 * this inode will block if they arrive while we are sleeping waiting
512 * for old data structures to be purged or for the contents of the
513 * disk portion of this inode to be read.
514 */
515 ip = VTOI(vp);
516 ufs_ihashins(ip);
517
518 /*
519 * XXX
520 * This may not need to be here, logically it should go down with
521 * the i_devvp initialization.
522 * Ask Kirk.
523 */
524 ip->i_lfs = ump->um_lfs;
525
526 /* Read in the disk contents for the inode, copy into the inode. */
527 if (dinp) {
528 error = copyin(dinp, &ip->i_din.ffs_din, sizeof(struct dinode));
529 if (error)
530 return (error);
531 }
532 else {
533 error = bread(ump->um_devvp, daddr,
534 (int)ump->um_lfs->lfs_bsize, NOCRED, &bp);
535 if (error) {
536 /*
537 * The inode does not contain anything useful, so it
538 * would be misleading to leave it on its hash chain.
539 * Iput() will return it to the free list.
540 */
541 ufs_ihashrem(ip);
542
543 /* Unlock and discard unneeded inode. */
544 lfs_vunref(vp);
545 brelse(bp);
546 *vpp = NULL;
547 return (error);
548 }
549 ip->i_din.ffs_din =
550 *lfs_ifind(ump->um_lfs, ino, (struct dinode *)bp->b_data);
551 brelse(bp);
552 }
553
554 /*
555 * Initialize the vnode from the inode, check for aliases. In all
556 * cases re-init ip, the underlying vnode/inode may have changed.
557 */
558 error = ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
559 if (error) {
560 lfs_vunref(vp);
561 *vpp = NULL;
562 return (error);
563 }
564 /*
565 * Finish inode initialization now that aliasing has been resolved.
566 */
567 ip->i_devvp = ump->um_devvp;
568 ip->i_flag |= IN_MODIFIED;
569 ++ump->um_lfs->lfs_uinodes;
570 VREF(ip->i_devvp);
571 *vpp = vp;
572 return (0);
573 }
574 struct buf *
575 lfs_fakebuf(vp, lbn, size, uaddr)
576 struct vnode *vp;
577 int lbn;
578 size_t size;
579 caddr_t uaddr;
580 {
581 struct buf *bp;
582
583 bp = lfs_newbuf(vp, lbn, 0);
584 bp->b_saveaddr = uaddr;
585 bp->b_bufsize = size;
586 bp->b_bcount = size;
587 bp->b_flags |= B_INVAL;
588 return (bp);
589 }
590