Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
ad787160 C |
2 | * Copyright (c) 1982, 1986, 1989, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
da7c5cc6 | 4 | * |
ad787160 C |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
7188ac27 | 20 | * |
ad787160 C |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
ed554bc5 | 33 | * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93 |
da7c5cc6 | 34 | */ |
52212483 | 35 | |
0cb5c176 KB |
36 | #include <sys/param.h> |
37 | #include <sys/systm.h> | |
38 | #include <sys/buf.h> | |
39 | #include <sys/proc.h> | |
40 | #include <sys/file.h> | |
41 | #include <sys/vnode.h> | |
c6f5111d | 42 | |
80e9de4e KM |
43 | #include <vm/vm.h> |
44 | ||
0cb5c176 KB |
45 | #include <ufs/ufs/quota.h> |
46 | #include <ufs/ufs/inode.h> | |
832b4d3b | 47 | #include <ufs/ufs/ufs_extern.h> |
0cb5c176 KB |
48 | |
49 | #include <ufs/ffs/fs.h> | |
50 | #include <ufs/ffs/ffs_extern.h> | |
52212483 | 51 | |
7188ac27 KM |
52 | /* |
53 | * Balloc defines the structure of file system storage | |
9b392e39 KM |
54 | * by allocating the physical blocks on a device given |
55 | * the inode and the logical block number in a file. | |
7188ac27 | 56 | */ |
9438887a | 57 | ffs_balloc(ip, bn, size, cred, bpp, flags) |
7188ac27 KM |
58 | register struct inode *ip; |
59 | register daddr_t bn; | |
60 | int size; | |
9438887a | 61 | struct ucred *cred; |
9b392e39 | 62 | struct buf **bpp; |
7188ac27 KM |
63 | int flags; |
64 | { | |
65 | register struct fs *fs; | |
66 | register daddr_t nb; | |
67 | struct buf *bp, *nbp; | |
9b392e39 | 68 | struct vnode *vp = ITOV(ip); |
832b4d3b | 69 | struct indir indirs[NIADDR + 2]; |
0cb5c176 | 70 | daddr_t newb, lbn, *bap, pref; |
175b63e7 | 71 | int osize, nsize, num, i, error; |
7188ac27 | 72 | |
175b63e7 | 73 | *bpp = NULL; |
7188ac27 KM |
74 | if (bn < 0) |
75 | return (EFBIG); | |
52212483 | 76 | fs = ip->i_fs; |
832b4d3b | 77 | lbn = bn; |
52212483 BJ |
78 | |
79 | /* | |
80 | * If the next write will extend the file into a new block, | |
81 | * and the file is currently composed of a fragment | |
82 | * this fragment has to be extended to be a full block. | |
83 | */ | |
84 | nb = lblkno(fs, ip->i_size); | |
7188ac27 | 85 | if (nb < NDADDR && nb < bn) { |
52212483 BJ |
86 | osize = blksize(fs, ip, nb); |
87 | if (osize < fs->fs_bsize && osize > 0) { | |
0cb5c176 KB |
88 | error = ffs_realloccg(ip, nb, |
89 | ffs_blkpref(ip, nb, (int)nb, &ip->i_db[0]), | |
9438887a | 90 | osize, (int)fs->fs_bsize, cred, &bp); |
9b392e39 | 91 | if (error) |
7188ac27 | 92 | return (error); |
52212483 | 93 | ip->i_size = (nb + 1) * fs->fs_bsize; |
995d2177 | 94 | vnode_pager_setsize(vp, (u_long)ip->i_size); |
52212483 | 95 | ip->i_db[nb] = dbtofsb(fs, bp->b_blkno); |
cf5ef508 | 96 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
9b392e39 KM |
97 | if (flags & B_SYNC) |
98 | bwrite(bp); | |
99 | else | |
100 | bawrite(bp); | |
52212483 BJ |
101 | } |
102 | } | |
103 | /* | |
104 | * The first NDADDR blocks are direct blocks | |
105 | */ | |
106 | if (bn < NDADDR) { | |
a6e7e3c6 | 107 | nb = ip->i_db[bn]; |
9b392e39 KM |
108 | if (nb != 0 && ip->i_size >= (bn + 1) * fs->fs_bsize) { |
109 | error = bread(vp, bn, fs->fs_bsize, NOCRED, &bp); | |
7188ac27 | 110 | if (error) { |
9b392e39 | 111 | brelse(bp); |
7188ac27 | 112 | return (error); |
52212483 | 113 | } |
9b392e39 KM |
114 | *bpp = bp; |
115 | return (0); | |
116 | } | |
117 | if (nb != 0) { | |
118 | /* | |
119 | * Consider need to reallocate a fragment. | |
120 | */ | |
121 | osize = fragroundup(fs, blkoff(fs, ip->i_size)); | |
122 | nsize = fragroundup(fs, size); | |
123 | if (nsize <= osize) { | |
124 | error = bread(vp, bn, osize, NOCRED, &bp); | |
125 | if (error) { | |
126 | brelse(bp); | |
127 | return (error); | |
128 | } | |
129 | } else { | |
0cb5c176 KB |
130 | error = ffs_realloccg(ip, bn, |
131 | ffs_blkpref(ip, bn, (int)bn, &ip->i_db[0]), | |
9438887a | 132 | osize, nsize, cred, &bp); |
9b392e39 KM |
133 | if (error) |
134 | return (error); | |
135 | } | |
136 | } else { | |
137 | if (ip->i_size < (bn + 1) * fs->fs_bsize) | |
138 | nsize = fragroundup(fs, size); | |
52212483 | 139 | else |
9b392e39 | 140 | nsize = fs->fs_bsize; |
0cb5c176 KB |
141 | error = ffs_alloc(ip, bn, |
142 | ffs_blkpref(ip, bn, (int)bn, &ip->i_db[0]), | |
9438887a | 143 | nsize, cred, &newb); |
9b392e39 KM |
144 | if (error) |
145 | return (error); | |
6adfd02b | 146 | bp = getblk(vp, bn, nsize, 0, 0); |
9b392e39 KM |
147 | bp->b_blkno = fsbtodb(fs, newb); |
148 | if (flags & B_CLRBUF) | |
149 | clrbuf(bp); | |
52212483 | 150 | } |
9b392e39 | 151 | ip->i_db[bn] = dbtofsb(fs, bp->b_blkno); |
cf5ef508 | 152 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
9b392e39 | 153 | *bpp = bp; |
7188ac27 | 154 | return (0); |
52212483 | 155 | } |
52212483 | 156 | /* |
9b392e39 | 157 | * Determine the number of levels of indirection. |
52212483 | 158 | */ |
a6e7e3c6 | 159 | pref = 0; |
832b4d3b MS |
160 | if (error = ufs_getlbns(vp, bn, indirs, &num)) |
161 | return(error); | |
162 | #ifdef DIAGNOSTIC | |
163 | if (num < 1) | |
164 | panic ("ffs_balloc: ufs_bmaparray returned indirect block\n"); | |
165 | #endif | |
52212483 | 166 | /* |
9b392e39 | 167 | * Fetch the first indirect block allocating if necessary. |
52212483 | 168 | */ |
832b4d3b MS |
169 | --num; |
170 | nb = ip->i_ib[indirs[0].in_off]; | |
52212483 | 171 | if (nb == 0) { |
0cb5c176 | 172 | pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0); |
9438887a KM |
173 | if (error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, |
174 | cred, &newb)) | |
7188ac27 | 175 | return (error); |
9b392e39 | 176 | nb = newb; |
6adfd02b | 177 | bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0); |
832b4d3b | 178 | bp->b_blkno = fsbtodb(fs, newb); |
9b392e39 | 179 | clrbuf(bp); |
52212483 BJ |
180 | /* |
181 | * Write synchronously so that indirect blocks | |
182 | * never point at garbage. | |
183 | */ | |
9b392e39 | 184 | if (error = bwrite(bp)) { |
0cb5c176 | 185 | ffs_blkfree(ip, nb, fs->fs_bsize); |
9b392e39 KM |
186 | return (error); |
187 | } | |
832b4d3b | 188 | ip->i_ib[indirs[0].in_off] = newb; |
cf5ef508 | 189 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
52212483 | 190 | } |
52212483 | 191 | /* |
9b392e39 | 192 | * Fetch through the indirect blocks, allocating as necessary. |
52212483 | 193 | */ |
175b63e7 KB |
194 | for (i = 1;;) { |
195 | error = bread(vp, | |
196 | indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp); | |
9b392e39 | 197 | if (error) { |
52212483 | 198 | brelse(bp); |
7188ac27 | 199 | return (error); |
52212483 | 200 | } |
dbb7672a | 201 | bap = (daddr_t *)bp->b_data; |
175b63e7 KB |
202 | nb = bap[indirs[i].in_off]; |
203 | if (i == num) | |
9b392e39 | 204 | break; |
175b63e7 | 205 | i += 1; |
9b392e39 KM |
206 | if (nb != 0) { |
207 | brelse(bp); | |
208 | continue; | |
209 | } | |
210 | if (pref == 0) | |
0cb5c176 KB |
211 | pref = ffs_blkpref(ip, lbn, 0, (daddr_t *)0); |
212 | if (error = | |
9438887a | 213 | ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred, &newb)) { |
9b392e39 KM |
214 | brelse(bp); |
215 | return (error); | |
216 | } | |
217 | nb = newb; | |
175b63e7 | 218 | nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0); |
832b4d3b | 219 | nbp->b_blkno = fsbtodb(fs, nb); |
9b392e39 | 220 | clrbuf(nbp); |
9b392e39 KM |
221 | /* |
222 | * Write synchronously so that indirect blocks | |
223 | * never point at garbage. | |
224 | */ | |
225 | if (error = bwrite(nbp)) { | |
0cb5c176 | 226 | ffs_blkfree(ip, nb, fs->fs_bsize); |
9b392e39 KM |
227 | brelse(bp); |
228 | return (error); | |
229 | } | |
175b63e7 | 230 | bap[indirs[i - 1].in_off] = nb; |
3cf3d516 KM |
231 | /* |
232 | * If required, write synchronously, otherwise use | |
832b4d3b | 233 | * delayed write. |
3cf3d516 KM |
234 | */ |
235 | if (flags & B_SYNC) { | |
9b392e39 | 236 | bwrite(bp); |
3cf3d516 KM |
237 | } else { |
238 | bdwrite(bp); | |
3cf3d516 | 239 | } |
9b392e39 KM |
240 | } |
241 | /* | |
242 | * Get the data block, allocating if necessary. | |
243 | */ | |
244 | if (nb == 0) { | |
175b63e7 KB |
245 | pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]); |
246 | if (error = ffs_alloc(ip, | |
247 | lbn, pref, (int)fs->fs_bsize, cred, &newb)) { | |
52212483 | 248 | brelse(bp); |
9b392e39 KM |
249 | return (error); |
250 | } | |
251 | nb = newb; | |
6adfd02b | 252 | nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); |
9b392e39 KM |
253 | nbp->b_blkno = fsbtodb(fs, nb); |
254 | if (flags & B_CLRBUF) | |
255 | clrbuf(nbp); | |
175b63e7 | 256 | bap[indirs[i].in_off] = nb; |
3cf3d516 KM |
257 | /* |
258 | * If required, write synchronously, otherwise use | |
832b4d3b | 259 | * delayed write. |
3cf3d516 KM |
260 | */ |
261 | if (flags & B_SYNC) { | |
9b392e39 | 262 | bwrite(bp); |
3cf3d516 KM |
263 | } else { |
264 | bdwrite(bp); | |
3cf3d516 | 265 | } |
9b392e39 KM |
266 | *bpp = nbp; |
267 | return (0); | |
52212483 | 268 | } |
9b392e39 | 269 | brelse(bp); |
0b4c7ad8 | 270 | if (flags & B_CLRBUF) { |
9b392e39 KM |
271 | error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp); |
272 | if (error) { | |
273 | brelse(nbp); | |
274 | return (error); | |
275 | } | |
0b4c7ad8 | 276 | } else { |
6adfd02b | 277 | nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0); |
0b4c7ad8 | 278 | nbp->b_blkno = fsbtodb(fs, nb); |
9b392e39 KM |
279 | } |
280 | *bpp = nbp; | |
7188ac27 | 281 | return (0); |
52212483 | 282 | } |