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