lfs_balloc.c revision 1.3 1 /* $NetBSD: lfs_balloc.c,v 1.3 1996/02/09 22:28:48 christos Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993
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_balloc.c 8.1 (Berkeley) 6/11/93
36 */
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/mount.h>
43 #include <sys/resourcevar.h>
44 #include <sys/trace.h>
45
46 #include <miscfs/specfs/specdev.h>
47
48 #include <ufs/ufs/quota.h>
49 #include <ufs/ufs/inode.h>
50 #include <ufs/ufs/ufsmount.h>
51 #include <ufs/ufs/ufs_extern.h>
52
53 #include <ufs/lfs/lfs.h>
54 #include <ufs/lfs/lfs_extern.h>
55
56 int
57 lfs_balloc(vp, iosize, lbn, bpp)
58 struct vnode *vp;
59 u_long iosize;
60 daddr_t lbn;
61 struct buf **bpp;
62 {
63 struct buf *ibp, *bp;
64 struct inode *ip;
65 struct lfs *fs;
66 struct indir indirs[NIADDR+2];
67 daddr_t daddr;
68 int bb, error, i, num;
69
70 ip = VTOI(vp);
71 fs = ip->i_lfs;
72
73 /*
74 * Three cases: it's a block beyond the end of file, it's a block in
75 * the file that may or may not have been assigned a disk address or
76 * we're writing an entire block. Note, if the daddr is unassigned,
77 * the block might still have existed in the cache (if it was read
78 * or written earlier). If it did, make sure we don't count it as a
79 * new block or zero out its contents. If it did not, make sure
80 * we allocate any necessary indirect blocks.
81 */
82
83 *bpp = NULL;
84 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL);
85 if (error)
86 return (error);
87
88 *bpp = bp = getblk(vp, lbn, fs->lfs_bsize, 0, 0);
89 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
90 if (daddr == UNASSIGNED)
91 /* May need to allocate indirect blocks */
92 for (i = 1; i < num; ++i)
93 if (!indirs[i].in_exists) {
94 ibp =
95 getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
96 0, 0);
97 if (!(ibp->b_flags & (B_DONE | B_DELWRI))) {
98 if (!ISSPACE(fs, bb, curproc->p_ucred)){
99 ibp->b_flags |= B_INVAL;
100 brelse(ibp);
101 error = ENOSPC;
102 } else {
103 ip->i_blocks += bb;
104 ip->i_lfs->lfs_bfree -= bb;
105 clrbuf(ibp);
106 error = VOP_BWRITE(ibp);
107 }
108 } else
109 panic ("Indirect block should not exist");
110 }
111 if (error) {
112 if (bp)
113 brelse(bp);
114 return(error);
115 }
116
117
118 /* Now, we may need to allocate the data block */
119 if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
120 if (daddr == UNASSIGNED)
121 if (!ISSPACE(fs, bb, curproc->p_ucred)) {
122 bp->b_flags |= B_INVAL;
123 brelse(bp);
124 return(ENOSPC);
125 } else {
126 ip->i_blocks += bb;
127 ip->i_lfs->lfs_bfree -= bb;
128 if (iosize != fs->lfs_bsize)
129 clrbuf(bp);
130 }
131 else if (iosize == fs->lfs_bsize)
132 bp->b_blkno = daddr; /* Skip the I/O */
133 else {
134 bp->b_blkno = daddr;
135 bp->b_flags |= B_READ;
136 VOP_STRATEGY(bp);
137 return(biowait(bp));
138 }
139 }
140 return (error);
141 }
142