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