lfs_balloc.c revision 1.89 1 1.89 dholland /* $NetBSD: lfs_balloc.c,v 1.89 2016/08/07 00:25:22 dholland Exp $ */
2 1.2 cgd
3 1.11 perseant /*-
4 1.36 perseant * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 1.11 perseant * All rights reserved.
6 1.11 perseant *
7 1.11 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.11 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.11 perseant *
10 1.11 perseant * Redistribution and use in source and binary forms, with or without
11 1.11 perseant * modification, are permitted provided that the following conditions
12 1.11 perseant * are met:
13 1.11 perseant * 1. Redistributions of source code must retain the above copyright
14 1.11 perseant * notice, this list of conditions and the following disclaimer.
15 1.11 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.11 perseant * notice, this list of conditions and the following disclaimer in the
17 1.11 perseant * documentation and/or other materials provided with the distribution.
18 1.11 perseant *
19 1.11 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.11 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.11 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.11 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.11 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.11 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.11 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.11 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.11 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.11 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.11 perseant * POSSIBILITY OF SUCH DAMAGE.
30 1.11 perseant */
31 1.1 mycroft /*
32 1.1 mycroft * Copyright (c) 1989, 1991, 1993
33 1.1 mycroft * The Regents of the University of California. All rights reserved.
34 1.1 mycroft *
35 1.1 mycroft * Redistribution and use in source and binary forms, with or without
36 1.1 mycroft * modification, are permitted provided that the following conditions
37 1.1 mycroft * are met:
38 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
39 1.1 mycroft * notice, this list of conditions and the following disclaimer.
40 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
41 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
42 1.1 mycroft * documentation and/or other materials provided with the distribution.
43 1.43 agc * 3. Neither the name of the University nor the names of its contributors
44 1.1 mycroft * may be used to endorse or promote products derived from this software
45 1.1 mycroft * without specific prior written permission.
46 1.1 mycroft *
47 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 1.1 mycroft * SUCH DAMAGE.
58 1.1 mycroft *
59 1.5 fvdl * @(#)lfs_balloc.c 8.4 (Berkeley) 5/8/95
60 1.1 mycroft */
61 1.30 lukem
62 1.30 lukem #include <sys/cdefs.h>
63 1.89 dholland __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.89 2016/08/07 00:25:22 dholland Exp $");
64 1.8 scottr
65 1.28 mrg #if defined(_KERNEL_OPT)
66 1.8 scottr #include "opt_quota.h"
67 1.9 scottr #endif
68 1.8 scottr
69 1.1 mycroft #include <sys/param.h>
70 1.3 christos #include <sys/systm.h>
71 1.1 mycroft #include <sys/buf.h>
72 1.1 mycroft #include <sys/proc.h>
73 1.1 mycroft #include <sys/vnode.h>
74 1.1 mycroft #include <sys/mount.h>
75 1.1 mycroft #include <sys/resourcevar.h>
76 1.55 perseant #include <sys/tree.h>
77 1.1 mycroft #include <sys/trace.h>
78 1.61 elad #include <sys/kauth.h>
79 1.1 mycroft
80 1.1 mycroft #include <miscfs/specfs/specdev.h>
81 1.1 mycroft
82 1.73 dholland #include <ufs/lfs/ulfs_quotacommon.h>
83 1.73 dholland #include <ufs/lfs/ulfs_inode.h>
84 1.73 dholland #include <ufs/lfs/ulfsmount.h>
85 1.73 dholland #include <ufs/lfs/ulfs_extern.h>
86 1.1 mycroft
87 1.1 mycroft #include <ufs/lfs/lfs.h>
88 1.84 dholland #include <ufs/lfs/lfs_accessors.h>
89 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
90 1.78 dholland #include <ufs/lfs/lfs_kernel.h>
91 1.5 fvdl
92 1.36 perseant #include <uvm/uvm.h>
93 1.36 perseant
94 1.89 dholland static int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **,
95 1.89 dholland kauth_cred_t);
96 1.5 fvdl
97 1.49 perseant u_int64_t locked_fakequeue_count;
98 1.49 perseant
99 1.16 perseant /*
100 1.16 perseant * Allocate a block, and to inode and filesystem block accounting for it
101 1.16 perseant * and for any indirect blocks the may need to be created in order for
102 1.16 perseant * this block to be created.
103 1.16 perseant *
104 1.16 perseant * Blocks which have never been accounted for (i.e., which "do not exist")
105 1.75 dholland * have disk address 0, which is translated by ulfs_bmap to the special value
106 1.75 dholland * UNASSIGNED == -1, as in the historical ULFS.
107 1.50 perry *
108 1.16 perseant * Blocks which have been accounted for but which have not yet been written
109 1.16 perseant * to disk are given the new special disk address UNWRITTEN == -2, so that
110 1.16 perseant * they can be differentiated from completely new blocks.
111 1.16 perseant */
112 1.75 dholland /* VOP_BWRITE ULFS_NIADDR+2 times */
113 1.1 mycroft int
114 1.61 elad lfs_balloc(struct vnode *vp, off_t startoffset, int iosize, kauth_cred_t cred,
115 1.57 yamt int flags, struct buf **bpp)
116 1.14 fvdl {
117 1.5 fvdl int offset;
118 1.46 mycroft daddr_t daddr, idaddr;
119 1.57 yamt struct buf *ibp, *bp;
120 1.1 mycroft struct inode *ip;
121 1.1 mycroft struct lfs *fs;
122 1.75 dholland struct indir indirs[ULFS_NIADDR+2], *idp;
123 1.35 fvdl daddr_t lbn, lastblock;
124 1.69 mlelstv int bcount;
125 1.11 perseant int error, frags, i, nsize, osize, num;
126 1.14 fvdl
127 1.1 mycroft ip = VTOI(vp);
128 1.1 mycroft fs = ip->i_lfs;
129 1.77 christos offset = lfs_blkoff(fs, startoffset);
130 1.82 dholland KASSERT(iosize <= lfs_sb_getbsize(fs));
131 1.77 christos lbn = lfs_lblkno(fs, startoffset);
132 1.36 perseant /* (void)lfs_check(vp, lbn, 0); */
133 1.36 perseant
134 1.52 perseant ASSERT_MAYBE_SEGLOCK(fs);
135 1.52 perseant
136 1.50 perry /*
137 1.1 mycroft * Three cases: it's a block beyond the end of file, it's a block in
138 1.1 mycroft * the file that may or may not have been assigned a disk address or
139 1.19 perseant * we're writing an entire block.
140 1.19 perseant *
141 1.19 perseant * Note, if the daddr is UNWRITTEN, the block already exists in
142 1.37 perseant * the cache (it was read or written earlier). If so, make sure
143 1.19 perseant * we don't count it as a new block or zero out its contents. If
144 1.19 perseant * it did not, make sure we allocate any necessary indirect
145 1.19 perseant * blocks.
146 1.19 perseant *
147 1.5 fvdl * If we are writing a block beyond the end of the file, we need to
148 1.11 perseant * check if the old last block was a fragment. If it was, we need
149 1.5 fvdl * to rewrite it.
150 1.1 mycroft */
151 1.50 perry
152 1.36 perseant if (bpp)
153 1.36 perseant *bpp = NULL;
154 1.50 perry
155 1.5 fvdl /* Check for block beyond end of file and fragment extension needed. */
156 1.77 christos lastblock = lfs_lblkno(fs, ip->i_size);
157 1.75 dholland if (lastblock < ULFS_NDADDR && lastblock < lbn) {
158 1.77 christos osize = lfs_blksize(fs, ip, lastblock);
159 1.82 dholland if (osize < lfs_sb_getbsize(fs) && osize > 0) {
160 1.82 dholland if ((error = lfs_fragextend(vp, osize, lfs_sb_getbsize(fs),
161 1.36 perseant lastblock,
162 1.57 yamt (bpp ? &bp : NULL), cred)))
163 1.31 chs return (error);
164 1.87 dholland ip->i_size = (lastblock + 1) * lfs_sb_getbsize(fs);
165 1.87 dholland lfs_dino_setsize(fs, ip->i_din, ip->i_size);
166 1.40 fvdl uvm_vnp_setsize(vp, ip->i_size);
167 1.5 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
168 1.36 perseant if (bpp)
169 1.70 hannken (void) VOP_BWRITE(bp->b_vp, bp);
170 1.5 fvdl }
171 1.5 fvdl }
172 1.5 fvdl
173 1.5 fvdl /*
174 1.5 fvdl * If the block we are writing is a direct block, it's the last
175 1.5 fvdl * block in the file, and offset + iosize is less than a full
176 1.5 fvdl * block, we can write one or more fragments. There are two cases:
177 1.5 fvdl * the block is brand new and we should allocate it the correct
178 1.5 fvdl * size or it already exists and contains some fragments and
179 1.5 fvdl * may need to extend it.
180 1.5 fvdl */
181 1.77 christos if (lbn < ULFS_NDADDR && lfs_lblkno(fs, ip->i_size) <= lbn) {
182 1.77 christos osize = lfs_blksize(fs, ip, lbn);
183 1.77 christos nsize = lfs_fragroundup(fs, offset + iosize);
184 1.77 christos if (lfs_lblktosize(fs, lbn) >= ip->i_size) {
185 1.5 fvdl /* Brand new block or fragment */
186 1.77 christos frags = lfs_numfrags(fs, nsize);
187 1.69 mlelstv if (!ISSPACE(fs, frags, cred))
188 1.51 perseant return ENOSPC;
189 1.36 perseant if (bpp) {
190 1.57 yamt *bpp = bp = getblk(vp, lbn, nsize, 0, 0);
191 1.36 perseant bp->b_blkno = UNWRITTEN;
192 1.57 yamt if (flags & B_CLRBUF)
193 1.38 perseant clrbuf(bp);
194 1.36 perseant }
195 1.69 mlelstv ip->i_lfs_effnblks += frags;
196 1.64 ad mutex_enter(&lfs_lock);
197 1.85 dholland lfs_sb_subbfree(fs, frags);
198 1.64 ad mutex_exit(&lfs_lock);
199 1.87 dholland lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN);
200 1.19 perseant } else {
201 1.11 perseant if (nsize <= osize) {
202 1.11 perseant /* No need to extend */
203 1.67 hannken if (bpp && (error = bread(vp, lbn, osize,
204 1.81 maxv 0, &bp)))
205 1.11 perseant return error;
206 1.11 perseant } else {
207 1.11 perseant /* Extend existing block */
208 1.11 perseant if ((error =
209 1.36 perseant lfs_fragextend(vp, osize, nsize, lbn,
210 1.57 yamt (bpp ? &bp : NULL), cred)))
211 1.19 perseant return error;
212 1.11 perseant }
213 1.36 perseant if (bpp)
214 1.36 perseant *bpp = bp;
215 1.5 fvdl }
216 1.19 perseant return 0;
217 1.19 perseant }
218 1.19 perseant
219 1.75 dholland error = ulfs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
220 1.19 perseant if (error)
221 1.19 perseant return (error);
222 1.49 perseant
223 1.86 dholland KASSERT(daddr <= LFS_MAX_DADDR(fs));
224 1.49 perseant
225 1.19 perseant /*
226 1.19 perseant * Do byte accounting all at once, so we can gracefully fail *before*
227 1.19 perseant * we start assigning blocks.
228 1.19 perseant */
229 1.79 dholland frags = fs->um_seqinc;
230 1.19 perseant bcount = 0;
231 1.19 perseant if (daddr == UNASSIGNED) {
232 1.69 mlelstv bcount = frags;
233 1.19 perseant }
234 1.19 perseant for (i = 1; i < num; ++i) {
235 1.19 perseant if (!indirs[i].in_exists) {
236 1.69 mlelstv bcount += frags;
237 1.19 perseant }
238 1.19 perseant }
239 1.57 yamt if (ISSPACE(fs, bcount, cred)) {
240 1.64 ad mutex_enter(&lfs_lock);
241 1.85 dholland lfs_sb_subbfree(fs, bcount);
242 1.64 ad mutex_exit(&lfs_lock);
243 1.21 perseant ip->i_lfs_effnblks += bcount;
244 1.5 fvdl } else {
245 1.19 perseant return ENOSPC;
246 1.19 perseant }
247 1.19 perseant
248 1.19 perseant if (daddr == UNASSIGNED) {
249 1.87 dholland if (num > 0 && lfs_dino_getib(fs, ip->i_din, indirs[0].in_off) == 0) {
250 1.87 dholland lfs_dino_setib(fs, ip->i_din, indirs[0].in_off, UNWRITTEN);
251 1.19 perseant }
252 1.19 perseant
253 1.5 fvdl /*
254 1.19 perseant * Create new indirect blocks if necessary
255 1.5 fvdl */
256 1.46 mycroft if (num > 1) {
257 1.87 dholland idaddr = lfs_dino_getib(fs, ip->i_din, indirs[0].in_off);
258 1.46 mycroft for (i = 1; i < num; ++i) {
259 1.46 mycroft ibp = getblk(vp, indirs[i].in_lbn,
260 1.82 dholland lfs_sb_getbsize(fs), 0,0);
261 1.46 mycroft if (!indirs[i].in_exists) {
262 1.46 mycroft clrbuf(ibp);
263 1.46 mycroft ibp->b_blkno = UNWRITTEN;
264 1.64 ad } else if (!(ibp->b_oflags & (BO_DELWRI | BO_DONE))) {
265 1.77 christos ibp->b_blkno = LFS_FSBTODB(fs, idaddr);
266 1.46 mycroft ibp->b_flags |= B_READ;
267 1.48 hannken VOP_STRATEGY(vp, ibp);
268 1.46 mycroft biowait(ibp);
269 1.46 mycroft }
270 1.46 mycroft /*
271 1.46 mycroft * This block exists, but the next one may not.
272 1.46 mycroft * If that is the case mark it UNWRITTEN to keep
273 1.46 mycroft * the accounting straight.
274 1.46 mycroft */
275 1.88 dholland if (lfs_iblock_get(fs, ibp->b_data, indirs[i].in_off) == 0)
276 1.88 dholland lfs_iblock_set(fs, ibp->b_data, indirs[i].in_off,
277 1.88 dholland UNWRITTEN);
278 1.88 dholland idaddr = lfs_iblock_get(fs, ibp->b_data, indirs[i].in_off);
279 1.52 perseant #ifdef DEBUG
280 1.52 perseant if (vp == fs->lfs_ivnode) {
281 1.52 perseant LFS_ENTER_LOG("balloc", __FILE__,
282 1.52 perseant __LINE__, indirs[i].in_lbn,
283 1.52 perseant ibp->b_flags, curproc->p_pid);
284 1.52 perseant }
285 1.52 perseant #endif
286 1.70 hannken if ((error = VOP_BWRITE(ibp->b_vp, ibp)))
287 1.46 mycroft return error;
288 1.19 perseant }
289 1.19 perseant }
290 1.50 perry }
291 1.19 perseant
292 1.19 perseant
293 1.19 perseant /*
294 1.36 perseant * Get the existing block from the cache, if requested.
295 1.19 perseant */
296 1.36 perseant if (bpp)
297 1.77 christos *bpp = bp = getblk(vp, lbn, lfs_blksize(fs, ip, lbn), 0, 0);
298 1.50 perry
299 1.49 perseant /*
300 1.49 perseant * Do accounting on blocks that represent pages.
301 1.49 perseant */
302 1.49 perseant if (!bpp)
303 1.49 perseant lfs_register_block(vp, lbn);
304 1.49 perseant
305 1.50 perry /*
306 1.5 fvdl * The block we are writing may be a brand new block
307 1.19 perseant * in which case we need to do accounting.
308 1.15 perseant *
309 1.75 dholland * We can tell a truly new block because ulfs_bmaparray will say
310 1.19 perseant * it is UNASSIGNED. Once we allocate it we will assign it the
311 1.19 perseant * disk address UNWRITTEN.
312 1.5 fvdl */
313 1.16 perseant if (daddr == UNASSIGNED) {
314 1.36 perseant if (bpp) {
315 1.57 yamt if (flags & B_CLRBUF)
316 1.36 perseant clrbuf(bp);
317 1.50 perry
318 1.36 perseant /* Note the new address */
319 1.36 perseant bp->b_blkno = UNWRITTEN;
320 1.36 perseant }
321 1.50 perry
322 1.19 perseant switch (num) {
323 1.19 perseant case 0:
324 1.87 dholland lfs_dino_setdb(fs, ip->i_din, lbn, UNWRITTEN);
325 1.19 perseant break;
326 1.19 perseant case 1:
327 1.87 dholland lfs_dino_setib(fs, ip->i_din, indirs[0].in_off, UNWRITTEN);
328 1.19 perseant break;
329 1.19 perseant default:
330 1.19 perseant idp = &indirs[num - 1];
331 1.82 dholland if (bread(vp, idp->in_lbn, lfs_sb_getbsize(fs),
332 1.67 hannken B_MODIFY, &ibp))
333 1.35 fvdl panic("lfs_balloc: bread bno %lld",
334 1.35 fvdl (long long)idp->in_lbn);
335 1.88 dholland lfs_iblock_set(fs, ibp->b_data, idp->in_off, UNWRITTEN);
336 1.52 perseant #ifdef DEBUG
337 1.52 perseant if (vp == fs->lfs_ivnode) {
338 1.52 perseant LFS_ENTER_LOG("balloc", __FILE__,
339 1.52 perseant __LINE__, idp->in_lbn,
340 1.52 perseant ibp->b_flags, curproc->p_pid);
341 1.52 perseant }
342 1.52 perseant #endif
343 1.70 hannken VOP_BWRITE(ibp->b_vp, ibp);
344 1.15 perseant }
345 1.64 ad } else if (bpp && !(bp->b_oflags & (BO_DONE|BO_DELWRI))) {
346 1.15 perseant /*
347 1.15 perseant * Not a brand new block, also not in the cache;
348 1.15 perseant * read it in from disk.
349 1.15 perseant */
350 1.82 dholland if (iosize == lfs_sb_getbsize(fs))
351 1.5 fvdl /* Optimization: I/O is unnecessary. */
352 1.5 fvdl bp->b_blkno = daddr;
353 1.15 perseant else {
354 1.5 fvdl /*
355 1.5 fvdl * We need to read the block to preserve the
356 1.5 fvdl * existing bytes.
357 1.5 fvdl */
358 1.1 mycroft bp->b_blkno = daddr;
359 1.1 mycroft bp->b_flags |= B_READ;
360 1.48 hannken VOP_STRATEGY(vp, bp);
361 1.31 chs return (biowait(bp));
362 1.1 mycroft }
363 1.1 mycroft }
364 1.50 perry
365 1.5 fvdl return (0);
366 1.5 fvdl }
367 1.5 fvdl
368 1.25 perseant /* VOP_BWRITE 1 time */
369 1.89 dholland static int
370 1.89 dholland lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn,
371 1.89 dholland struct buf **bpp, kauth_cred_t cred)
372 1.5 fvdl {
373 1.5 fvdl struct inode *ip;
374 1.5 fvdl struct lfs *fs;
375 1.69 mlelstv long frags;
376 1.5 fvdl int error;
377 1.11 perseant extern long locked_queue_bytes;
378 1.26 perseant size_t obufsize;
379 1.5 fvdl
380 1.5 fvdl ip = VTOI(vp);
381 1.5 fvdl fs = ip->i_lfs;
382 1.77 christos frags = (long)lfs_numfrags(fs, nsize - osize);
383 1.18 perseant error = 0;
384 1.18 perseant
385 1.60 perseant ASSERT_NO_SEGLOCK(fs);
386 1.52 perseant
387 1.18 perseant /*
388 1.36 perseant * Get the seglock so we don't enlarge blocks while a segment
389 1.36 perseant * is being written. If we're called with bpp==NULL, though,
390 1.36 perseant * we are only pretending to change a buffer, so we don't have to
391 1.36 perseant * lock.
392 1.18 perseant */
393 1.26 perseant top:
394 1.36 perseant if (bpp) {
395 1.62 ad rw_enter(&fs->lfs_fraglock, RW_READER);
396 1.44 yamt LFS_DEBUG_COUNTLOCKED("frag");
397 1.36 perseant }
398 1.36 perseant
399 1.69 mlelstv if (!ISSPACE(fs, frags, cred)) {
400 1.18 perseant error = ENOSPC;
401 1.18 perseant goto out;
402 1.5 fvdl }
403 1.36 perseant
404 1.36 perseant /*
405 1.36 perseant * If we are not asked to actually return the block, all we need
406 1.36 perseant * to do is allocate space for it. UBC will handle dirtying the
407 1.36 perseant * appropriate things and making sure it all goes to disk.
408 1.36 perseant * Don't bother to read in that case.
409 1.36 perseant */
410 1.81 maxv if (bpp && (error = bread(vp, lbn, osize, 0, bpp))) {
411 1.18 perseant goto out;
412 1.5 fvdl }
413 1.80 dholland #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
414 1.76 dholland if ((error = lfs_chkdq(ip, frags, cred, 0))) {
415 1.36 perseant if (bpp)
416 1.63 ad brelse(*bpp, 0);
417 1.19 perseant goto out;
418 1.19 perseant }
419 1.19 perseant #endif
420 1.13 perseant /*
421 1.26 perseant * Adjust accounting for lfs_avail. If there's not enough room,
422 1.26 perseant * we will have to wait for the cleaner, which we can't do while
423 1.26 perseant * holding a block busy or while holding the seglock. In that case,
424 1.26 perseant * release both and start over after waiting.
425 1.26 perseant */
426 1.33 perseant
427 1.64 ad if (bpp && ((*bpp)->b_oflags & BO_DELWRI)) {
428 1.69 mlelstv if (!lfs_fits(fs, frags)) {
429 1.36 perseant if (bpp)
430 1.63 ad brelse(*bpp, 0);
431 1.80 dholland #if defined(LFS_QUOTA) || defined(LFS_QUOTA2)
432 1.76 dholland lfs_chkdq(ip, -frags, cred, 0);
433 1.26 perseant #endif
434 1.62 ad rw_exit(&fs->lfs_fraglock);
435 1.69 mlelstv lfs_availwait(fs, frags);
436 1.26 perseant goto top;
437 1.26 perseant }
438 1.82 dholland lfs_sb_subavail(fs, frags);
439 1.26 perseant }
440 1.26 perseant
441 1.64 ad mutex_enter(&lfs_lock);
442 1.85 dholland lfs_sb_subbfree(fs, frags);
443 1.64 ad mutex_exit(&lfs_lock);
444 1.69 mlelstv ip->i_lfs_effnblks += frags;
445 1.5 fvdl ip->i_flag |= IN_CHANGE | IN_UPDATE;
446 1.26 perseant
447 1.36 perseant if (bpp) {
448 1.36 perseant obufsize = (*bpp)->b_bufsize;
449 1.47 pk allocbuf(*bpp, nsize, 1);
450 1.26 perseant
451 1.36 perseant /* Adjust locked-list accounting */
452 1.65 ad if (((*bpp)->b_flags & B_LOCKED) != 0 &&
453 1.64 ad (*bpp)->b_iodone == NULL) {
454 1.64 ad mutex_enter(&lfs_lock);
455 1.36 perseant locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
456 1.64 ad mutex_exit(&lfs_lock);
457 1.52 perseant }
458 1.26 perseant
459 1.68 cegger memset((char *)((*bpp)->b_data) + osize, 0, (u_int)(nsize - osize));
460 1.36 perseant }
461 1.18 perseant
462 1.18 perseant out:
463 1.36 perseant if (bpp) {
464 1.62 ad rw_exit(&fs->lfs_fraglock);
465 1.36 perseant }
466 1.18 perseant return (error);
467 1.1 mycroft }
468 1.49 perseant
469 1.59 perry static inline int
470 1.55 perseant lge(struct lbnentry *a, struct lbnentry *b)
471 1.53 perseant {
472 1.55 perseant return a->lbn - b->lbn;
473 1.53 perseant }
474 1.53 perseant
475 1.55 perseant SPLAY_PROTOTYPE(lfs_splay, lbnentry, entry, lge);
476 1.55 perseant
477 1.55 perseant SPLAY_GENERATE(lfs_splay, lbnentry, entry, lge);
478 1.53 perseant
479 1.49 perseant /*
480 1.49 perseant * Record this lbn as being "write pending". We used to have this information
481 1.49 perseant * on the buffer headers, but since pages don't have buffer headers we
482 1.49 perseant * record it here instead.
483 1.49 perseant */
484 1.49 perseant void
485 1.49 perseant lfs_register_block(struct vnode *vp, daddr_t lbn)
486 1.49 perseant {
487 1.49 perseant struct lfs *fs;
488 1.49 perseant struct inode *ip;
489 1.49 perseant struct lbnentry *lbp;
490 1.53 perseant
491 1.53 perseant ip = VTOI(vp);
492 1.49 perseant
493 1.49 perseant /* Don't count metadata */
494 1.53 perseant if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
495 1.49 perseant return;
496 1.49 perseant
497 1.49 perseant fs = ip->i_lfs;
498 1.49 perseant
499 1.52 perseant ASSERT_NO_SEGLOCK(fs);
500 1.52 perseant
501 1.49 perseant /* If no space, wait for the cleaner */
502 1.83 dholland lfs_availwait(fs, lfs_btofsb(fs, 1 << lfs_sb_getbshift(fs)));
503 1.49 perseant
504 1.49 perseant lbp = (struct lbnentry *)pool_get(&lfs_lbnentry_pool, PR_WAITOK);
505 1.49 perseant lbp->lbn = lbn;
506 1.64 ad mutex_enter(&lfs_lock);
507 1.55 perseant if (SPLAY_INSERT(lfs_splay, &ip->i_lfs_lbtree, lbp) != NULL) {
508 1.64 ad mutex_exit(&lfs_lock);
509 1.55 perseant /* Already there */
510 1.55 perseant pool_put(&lfs_lbnentry_pool, lbp);
511 1.55 perseant return;
512 1.55 perseant }
513 1.52 perseant
514 1.56 perseant ++ip->i_lfs_nbtree;
515 1.83 dholland fs->lfs_favail += lfs_btofsb(fs, (1 << lfs_sb_getbshift(fs)));
516 1.82 dholland fs->lfs_pages += lfs_sb_getbsize(fs) >> PAGE_SHIFT;
517 1.49 perseant ++locked_fakequeue_count;
518 1.82 dholland lfs_subsys_pages += lfs_sb_getbsize(fs) >> PAGE_SHIFT;
519 1.64 ad mutex_exit(&lfs_lock);
520 1.49 perseant }
521 1.49 perseant
522 1.49 perseant static void
523 1.53 perseant lfs_do_deregister(struct lfs *fs, struct inode *ip, struct lbnentry *lbp)
524 1.49 perseant {
525 1.52 perseant ASSERT_MAYBE_SEGLOCK(fs);
526 1.52 perseant
527 1.64 ad mutex_enter(&lfs_lock);
528 1.56 perseant --ip->i_lfs_nbtree;
529 1.55 perseant SPLAY_REMOVE(lfs_splay, &ip->i_lfs_lbtree, lbp);
530 1.83 dholland if (fs->lfs_favail > lfs_btofsb(fs, (1 << lfs_sb_getbshift(fs))))
531 1.83 dholland fs->lfs_favail -= lfs_btofsb(fs, (1 << lfs_sb_getbshift(fs)));
532 1.82 dholland fs->lfs_pages -= lfs_sb_getbsize(fs) >> PAGE_SHIFT;
533 1.49 perseant if (locked_fakequeue_count > 0)
534 1.49 perseant --locked_fakequeue_count;
535 1.82 dholland lfs_subsys_pages -= lfs_sb_getbsize(fs) >> PAGE_SHIFT;
536 1.64 ad mutex_exit(&lfs_lock);
537 1.64 ad
538 1.64 ad pool_put(&lfs_lbnentry_pool, lbp);
539 1.49 perseant }
540 1.49 perseant
541 1.49 perseant void
542 1.49 perseant lfs_deregister_block(struct vnode *vp, daddr_t lbn)
543 1.49 perseant {
544 1.49 perseant struct lfs *fs;
545 1.49 perseant struct inode *ip;
546 1.49 perseant struct lbnentry *lbp;
547 1.55 perseant struct lbnentry tmp;
548 1.53 perseant
549 1.53 perseant ip = VTOI(vp);
550 1.49 perseant
551 1.49 perseant /* Don't count metadata */
552 1.53 perseant if (lbn < 0 || vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM)
553 1.49 perseant return;
554 1.49 perseant
555 1.49 perseant fs = ip->i_lfs;
556 1.55 perseant tmp.lbn = lbn;
557 1.55 perseant lbp = SPLAY_FIND(lfs_splay, &ip->i_lfs_lbtree, &tmp);
558 1.49 perseant if (lbp == NULL)
559 1.49 perseant return;
560 1.49 perseant
561 1.53 perseant lfs_do_deregister(fs, ip, lbp);
562 1.49 perseant }
563 1.55 perseant
564 1.55 perseant void
565 1.55 perseant lfs_deregister_all(struct vnode *vp)
566 1.55 perseant {
567 1.55 perseant struct lbnentry *lbp, *nlbp;
568 1.55 perseant struct lfs_splay *hd;
569 1.55 perseant struct lfs *fs;
570 1.55 perseant struct inode *ip;
571 1.55 perseant
572 1.55 perseant ip = VTOI(vp);
573 1.55 perseant fs = ip->i_lfs;
574 1.55 perseant hd = &ip->i_lfs_lbtree;
575 1.55 perseant
576 1.55 perseant for (lbp = SPLAY_MIN(lfs_splay, hd); lbp != NULL; lbp = nlbp) {
577 1.55 perseant nlbp = SPLAY_NEXT(lfs_splay, hd, lbp);
578 1.55 perseant lfs_do_deregister(fs, ip, lbp);
579 1.55 perseant }
580 1.55 perseant }
581