lfs_balloc.c revision 1.8 1 /* $NetBSD: lfs_balloc.c,v 1.8 1998/06/08 04:27:51 scottr 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.4 (Berkeley) 5/8/95
36 */
37
38 #include "opt_quota.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/buf.h>
43 #include <sys/proc.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
46 #include <sys/resourcevar.h>
47 #include <sys/trace.h>
48
49 #include <miscfs/specfs/specdev.h>
50
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/ufsmount.h>
54 #include <ufs/ufs/ufs_extern.h>
55
56 #include <ufs/lfs/lfs.h>
57 #include <ufs/lfs/lfs_extern.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_extern.h>
61
62 int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
63
64 int
65 lfs_balloc(vp, offset, iosize, lbn, bpp)
66 struct vnode *vp;
67 int offset;
68 u_long iosize;
69 ufs_daddr_t lbn;
70 struct buf **bpp;
71 {
72 struct buf *ibp, *bp;
73 struct inode *ip;
74 struct lfs *fs;
75 struct indir indirs[NIADDR+2];
76 ufs_daddr_t daddr, lastblock;
77 int bb; /* number of disk blocks in a block disk blocks */
78 int error, frags, i, nsize, osize, num;
79
80 ip = VTOI(vp);
81 fs = ip->i_lfs;
82
83 /*
84 * Three cases: it's a block beyond the end of file, it's a block in
85 * the file that may or may not have been assigned a disk address or
86 * we're writing an entire block. Note, if the daddr is unassigned,
87 * the block might still have existed in the cache (if it was read
88 * or written earlier). If it did, make sure we don't count it as a
89 * new block or zero out its contents. If it did not, make sure
90 * we allocate any necessary indirect blocks.
91 * If we are writing a block beyond the end of the file, we need to
92 * check if the old last block was a fragment. If it was, we need
93 * to rewrite it.
94 */
95
96 *bpp = NULL;
97 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
98 if (error)
99 return (error);
100
101 /* Check for block beyond end of file and fragment extension needed. */
102 lastblock = lblkno(fs, ip->i_ffs_size);
103 if (lastblock < NDADDR && lastblock < lbn) {
104 osize = blksize(fs, ip, lastblock);
105 if (osize < fs->lfs_bsize && osize > 0) {
106 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
107 lastblock, &bp)))
108 return(error);
109 ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
110 #if defined(UVM)
111 uvm_vnp_setsize(vp, ip->i_ffs_size);
112 #else
113 vnode_pager_setsize(vp, ip->i_ffs_size);
114 #endif
115 ip->i_flag |= IN_CHANGE | IN_UPDATE;
116 VOP_BWRITE(bp);
117 }
118 }
119
120 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
121 if (daddr == UNASSIGNED)
122 /* May need to allocate indirect blocks */
123 for (i = 1; i < num; ++i)
124 if (!indirs[i].in_exists) {
125 ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
126 0, 0);
127 if ((ibp->b_flags & (B_DONE | B_DELWRI)))
128 panic ("Indirect block should not exist");
129
130 if (!ISSPACE(fs, bb, curproc->p_ucred)){
131 ibp->b_flags |= B_INVAL;
132 brelse(ibp);
133 return(ENOSPC);
134 } else {
135 ip->i_ffs_blocks += bb;
136 ip->i_lfs->lfs_bfree -= bb;
137 clrbuf(ibp);
138 if ((error = VOP_BWRITE(ibp)))
139 return(error);
140 }
141 }
142
143 /*
144 * If the block we are writing is a direct block, it's the last
145 * block in the file, and offset + iosize is less than a full
146 * block, we can write one or more fragments. There are two cases:
147 * the block is brand new and we should allocate it the correct
148 * size or it already exists and contains some fragments and
149 * may need to extend it.
150 */
151 if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) == lbn) {
152 nsize = fragroundup(fs, offset + iosize);
153 frags = numfrags(fs, nsize);
154 bb = fragstodb(fs, frags);
155 if (lblktosize(fs, lbn) == ip->i_ffs_size)
156 /* Brand new block or fragment */
157 *bpp = bp = getblk(vp, lbn, nsize, 0, 0);
158 else {
159 /* Extend existing block */
160 if ((error =
161 lfs_fragextend(vp, (int)blksize(fs, ip, lbn),
162 nsize, lbn, &bp)))
163 return(error);
164 *bpp = bp;
165 }
166 } else {
167 /*
168 * Get the existing block from the cache either because the
169 * block is 1) not a direct block or because it's not the last
170 * block in the file.
171 */
172 frags = dbtofrags(fs, bb);
173 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
174 }
175
176 /*
177 * The block we are writing may be a brand new block
178 * in which case we need to do accounting (i.e. check
179 * for free space and update the inode number of blocks.
180 */
181 if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
182 if (daddr == UNASSIGNED)
183 if (!ISSPACE(fs, bb, curproc->p_ucred)) {
184 bp->b_flags |= B_INVAL;
185 brelse(bp);
186 return(ENOSPC);
187 } else {
188 ip->i_ffs_blocks += bb;
189 ip->i_lfs->lfs_bfree -= bb;
190 if (iosize != fs->lfs_bsize)
191 clrbuf(bp);
192 }
193 else if (iosize == fs->lfs_bsize)
194 /* Optimization: I/O is unnecessary. */
195 bp->b_blkno = daddr;
196 else {
197 /*
198 * We need to read the block to preserve the
199 * existing bytes.
200 */
201 bp->b_blkno = daddr;
202 bp->b_flags |= B_READ;
203 VOP_STRATEGY(bp);
204 return(biowait(bp));
205 }
206 }
207 return (0);
208 }
209
210 int
211 lfs_fragextend(vp, osize, nsize, lbn, bpp)
212 struct vnode *vp;
213 int osize;
214 int nsize;
215 ufs_daddr_t lbn;
216 struct buf **bpp;
217 {
218 struct inode *ip;
219 struct lfs *fs;
220 long bb;
221 int error;
222
223 ip = VTOI(vp);
224 fs = ip->i_lfs;
225 bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
226 if (!ISSPACE(fs, bb, curproc->p_ucred)) {
227 return(ENOSPC);
228 }
229
230 if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
231 brelse(*bpp);
232 return(error);
233 }
234 #ifdef QUOTA
235 if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
236 brelse(*bpp);
237 return (error);
238 }
239 #endif
240 ip->i_ffs_blocks += bb;
241 ip->i_flag |= IN_CHANGE | IN_UPDATE;
242 fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
243 allocbuf(*bpp, nsize);
244 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
245 return(0);
246 }
247