lfs_inode.c revision 1.18 1 1.18 perseant /* $NetBSD: lfs_inode.c,v 1.18 1999/03/10 00:20:00 perseant Exp $ */
2 1.2 cgd
3 1.18 perseant /*-
4 1.18 perseant * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.18 perseant * All rights reserved.
6 1.18 perseant *
7 1.18 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.18 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.18 perseant *
10 1.18 perseant * Redistribution and use in source and binary forms, with or without
11 1.18 perseant * modification, are permitted provided that the following conditions
12 1.18 perseant * are met:
13 1.18 perseant * 1. Redistributions of source code must retain the above copyright
14 1.18 perseant * notice, this list of conditions and the following disclaimer.
15 1.18 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.18 perseant * notice, this list of conditions and the following disclaimer in the
17 1.18 perseant * documentation and/or other materials provided with the distribution.
18 1.18 perseant * 3. All advertising materials mentioning features or use of this software
19 1.18 perseant * must display the following acknowledgement:
20 1.18 perseant * This product includes software developed by the NetBSD
21 1.18 perseant * Foundation, Inc. and its contributors.
22 1.18 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.18 perseant * contributors may be used to endorse or promote products derived
24 1.18 perseant * from this software without specific prior written permission.
25 1.18 perseant *
26 1.18 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.18 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.18 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.18 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.18 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.18 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.18 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.18 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.18 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.18 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.18 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.18 perseant */
38 1.1 mycroft /*
39 1.1 mycroft * Copyright (c) 1986, 1989, 1991, 1993
40 1.1 mycroft * The Regents of the University of California. All rights reserved.
41 1.1 mycroft *
42 1.1 mycroft * Redistribution and use in source and binary forms, with or without
43 1.1 mycroft * modification, are permitted provided that the following conditions
44 1.1 mycroft * are met:
45 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
46 1.1 mycroft * notice, this list of conditions and the following disclaimer.
47 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
49 1.1 mycroft * documentation and/or other materials provided with the distribution.
50 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
51 1.1 mycroft * must display the following acknowledgement:
52 1.1 mycroft * This product includes software developed by the University of
53 1.1 mycroft * California, Berkeley and its contributors.
54 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
55 1.1 mycroft * may be used to endorse or promote products derived from this software
56 1.1 mycroft * without specific prior written permission.
57 1.1 mycroft *
58 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 mycroft * SUCH DAMAGE.
69 1.1 mycroft *
70 1.12 fvdl * @(#)lfs_inode.c 8.9 (Berkeley) 5/8/95
71 1.1 mycroft */
72 1.13 scottr
73 1.14 scottr #if defined(_KERNEL) && !defined(_LKM)
74 1.13 scottr #include "opt_quota.h"
75 1.14 scottr #endif
76 1.1 mycroft
77 1.1 mycroft #include <sys/param.h>
78 1.1 mycroft #include <sys/systm.h>
79 1.1 mycroft #include <sys/mount.h>
80 1.1 mycroft #include <sys/proc.h>
81 1.1 mycroft #include <sys/file.h>
82 1.1 mycroft #include <sys/buf.h>
83 1.1 mycroft #include <sys/vnode.h>
84 1.1 mycroft #include <sys/kernel.h>
85 1.1 mycroft #include <sys/malloc.h>
86 1.1 mycroft
87 1.1 mycroft #include <vm/vm.h>
88 1.1 mycroft
89 1.1 mycroft #include <ufs/ufs/quota.h>
90 1.1 mycroft #include <ufs/ufs/inode.h>
91 1.1 mycroft #include <ufs/ufs/ufsmount.h>
92 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
93 1.1 mycroft
94 1.1 mycroft #include <ufs/lfs/lfs.h>
95 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
96 1.1 mycroft
97 1.1 mycroft /* Search a block for a specific dinode. */
98 1.1 mycroft struct dinode *
99 1.1 mycroft lfs_ifind(fs, ino, dip)
100 1.1 mycroft struct lfs *fs;
101 1.1 mycroft ino_t ino;
102 1.1 mycroft register struct dinode *dip;
103 1.1 mycroft {
104 1.1 mycroft register int cnt;
105 1.1 mycroft register struct dinode *ldip;
106 1.18 perseant
107 1.1 mycroft for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
108 1.1 mycroft if (ldip->di_inumber == ino)
109 1.1 mycroft return (ldip);
110 1.18 perseant
111 1.1 mycroft panic("lfs_ifind: dinode %u not found", ino);
112 1.1 mycroft /* NOTREACHED */
113 1.1 mycroft }
114 1.1 mycroft
115 1.1 mycroft int
116 1.4 christos lfs_update(v)
117 1.4 christos void *v;
118 1.4 christos {
119 1.1 mycroft struct vop_update_args /* {
120 1.18 perseant struct vnode *a_vp;
121 1.18 perseant struct timespec *a_access;
122 1.18 perseant struct timespec *a_modify;
123 1.18 perseant int a_waitfor;
124 1.18 perseant } */ *ap = v;
125 1.1 mycroft struct inode *ip;
126 1.12 fvdl struct vnode *vp = ap->a_vp;
127 1.18 perseant int mod, oflag;
128 1.6 mycroft struct timespec ts;
129 1.18 perseant
130 1.12 fvdl if (vp->v_mount->mnt_flag & MNT_RDONLY)
131 1.1 mycroft return (0);
132 1.12 fvdl ip = VTOI(vp);
133 1.18 perseant
134 1.18 perseant /*
135 1.18 perseant * If we are called from vinvalbuf, and the file's blocks have
136 1.18 perseant * already been scheduled for writing, but the writes have not
137 1.18 perseant * yet completed, lfs_vflush will not be called, and vinvalbuf
138 1.18 perseant * will cause a panic. So, we must wait until any pending write
139 1.18 perseant * for our inode completes, if we are called with LFS_SYNC set.
140 1.18 perseant */
141 1.18 perseant while((ap->a_waitfor & LFS_SYNC) && WRITEINPROG(vp)) {
142 1.18 perseant tsleep(vp, (PRIBIO+1), "lfs_update", 0);
143 1.18 perseant }
144 1.6 mycroft mod = ip->i_flag & IN_MODIFIED;
145 1.18 perseant oflag = ip->i_flag;
146 1.6 mycroft TIMEVAL_TO_TIMESPEC(&time, &ts);
147 1.18 perseant LFS_ITIMES(ip,
148 1.18 perseant ap->a_access ? ap->a_access : &ts,
149 1.18 perseant ap->a_modify ? ap->a_modify : &ts, &ts);
150 1.18 perseant if (!mod && (ip->i_flag & IN_MODIFIED))
151 1.6 mycroft ip->i_lfs->lfs_uinodes++;
152 1.18 perseant if ((ip->i_flag & (IN_MODIFIED|IN_CLEANING)) == 0) {
153 1.1 mycroft return (0);
154 1.18 perseant }
155 1.18 perseant
156 1.1 mycroft /* If sync, push back the vnode and any dirty blocks it may have. */
157 1.12 fvdl return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
158 1.1 mycroft }
159 1.1 mycroft
160 1.1 mycroft /* Update segment usage information when removing a block. */
161 1.1 mycroft #define UPDATE_SEGUSE \
162 1.1 mycroft if (lastseg != -1) { \
163 1.1 mycroft LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
164 1.18 perseant if (num > sup->su_nbytes) { \
165 1.1 mycroft panic("lfs_truncate: negative bytes in segment %d\n", \
166 1.18 perseant lastseg); \
167 1.18 perseant sup->su_nbytes = 0; \
168 1.18 perseant } else \
169 1.12 fvdl sup->su_nbytes -= num; \
170 1.1 mycroft e1 = VOP_BWRITE(sup_bp); \
171 1.12 fvdl fragsreleased += numfrags(fs, num); \
172 1.1 mycroft }
173 1.1 mycroft
174 1.12 fvdl #define SEGDEC(S) { \
175 1.1 mycroft if (daddr != 0) { \
176 1.1 mycroft if (lastseg != (seg = datosn(fs, daddr))) { \
177 1.1 mycroft UPDATE_SEGUSE; \
178 1.12 fvdl num = (S); \
179 1.1 mycroft lastseg = seg; \
180 1.1 mycroft } else \
181 1.12 fvdl num += (S); \
182 1.1 mycroft } \
183 1.1 mycroft }
184 1.1 mycroft
185 1.1 mycroft /*
186 1.1 mycroft * Truncate the inode ip to at most length size. Update segment usage
187 1.1 mycroft * table information.
188 1.1 mycroft */
189 1.1 mycroft /* ARGSUSED */
190 1.1 mycroft int
191 1.4 christos lfs_truncate(v)
192 1.4 christos void *v;
193 1.4 christos {
194 1.1 mycroft struct vop_truncate_args /* {
195 1.1 mycroft struct vnode *a_vp;
196 1.1 mycroft off_t a_length;
197 1.1 mycroft int a_flags;
198 1.1 mycroft struct ucred *a_cred;
199 1.1 mycroft struct proc *a_p;
200 1.4 christos } */ *ap = v;
201 1.1 mycroft register struct indir *inp;
202 1.1 mycroft register int i;
203 1.12 fvdl register ufs_daddr_t *daddrp;
204 1.1 mycroft register struct vnode *vp = ap->a_vp;
205 1.1 mycroft off_t length = ap->a_length;
206 1.1 mycroft struct buf *bp, *sup_bp;
207 1.1 mycroft struct ifile *ifp;
208 1.1 mycroft struct inode *ip;
209 1.1 mycroft struct lfs *fs;
210 1.1 mycroft struct indir a[NIADDR + 2], a_end[NIADDR + 2];
211 1.1 mycroft SEGUSE *sup;
212 1.12 fvdl ufs_daddr_t daddr, lastblock, lbn, olastblock;
213 1.12 fvdl ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
214 1.12 fvdl long off, a_released, fragsreleased, i_released;
215 1.12 fvdl int e1, e2, depth, lastseg, num, offset, seg, freesize;
216 1.18 perseant
217 1.18 perseant ip = VTOI(vp);
218 1.1 mycroft
219 1.1 mycroft if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
220 1.1 mycroft #ifdef DIAGNOSTIC
221 1.1 mycroft if (length != 0)
222 1.1 mycroft panic("lfs_truncate: partial truncate of symlink");
223 1.1 mycroft #endif
224 1.9 bouyer bzero((char *)&ip->i_ffs_shortlink, (u_int)ip->i_ffs_size);
225 1.9 bouyer ip->i_ffs_size = 0;
226 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
227 1.17 mycroft return (VOP_UPDATE(vp, NULL, NULL, 0));
228 1.1 mycroft }
229 1.11 chs #ifdef UVM
230 1.11 chs uvm_vnp_setsize(vp, length);
231 1.11 chs #else
232 1.10 drochner vnode_pager_setsize(vp, length);
233 1.11 chs #endif
234 1.18 perseant
235 1.1 mycroft fs = ip->i_lfs;
236 1.18 perseant lfs_imtime(fs);
237 1.18 perseant
238 1.1 mycroft /* If length is larger than the file, just update the times. */
239 1.9 bouyer if (ip->i_ffs_size <= length) {
240 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
241 1.17 mycroft return (VOP_UPDATE(vp, NULL, NULL, 0));
242 1.1 mycroft }
243 1.1 mycroft
244 1.18 perseant #if 0
245 1.18 perseant /*
246 1.18 perseant * Make sure no writes happen while we're truncating
247 1.18 perseant * XXX KS - I don't remember why....
248 1.18 perseant */
249 1.18 perseant while(fs->lfs_seglock) {
250 1.18 perseant tsleep(&fs->lfs_seglock, (PRIBIO+1), "lfs_truncate", 0);
251 1.18 perseant }
252 1.18 perseant #endif
253 1.18 perseant
254 1.1 mycroft /*
255 1.1 mycroft * Calculate index into inode's block list of last direct and indirect
256 1.1 mycroft * blocks (if any) which we want to keep. Lastblock is 0 when the
257 1.1 mycroft * file is truncated to 0.
258 1.1 mycroft */
259 1.1 mycroft lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
260 1.9 bouyer olastblock = lblkno(fs, ip->i_ffs_size + fs->lfs_bsize - 1) - 1;
261 1.1 mycroft
262 1.1 mycroft /*
263 1.1 mycroft * Update the size of the file. If the file is not being truncated to
264 1.1 mycroft * a block boundry, the contents of the partial block following the end
265 1.12 fvdl * because of subsequent file growth. For this part of the code,
266 1.12 fvdl * oldsize_newlast refers to the old size of the new last block in the
267 1.12 fvdl * file.
268 1.1 mycroft */
269 1.1 mycroft offset = blkoff(fs, length);
270 1.12 fvdl lbn = lblkno(fs, length);
271 1.12 fvdl oldsize_newlast = blksize(fs, ip, lbn);
272 1.12 fvdl
273 1.12 fvdl /* Now set oldsize to the current size of the current last block */
274 1.12 fvdl oldsize_lastblock = blksize(fs, ip, olastblock);
275 1.1 mycroft if (offset == 0)
276 1.9 bouyer ip->i_ffs_size = length;
277 1.1 mycroft else {
278 1.1 mycroft #ifdef QUOTA
279 1.4 christos if ((e1 = getinoquota(ip)) != 0)
280 1.1 mycroft return (e1);
281 1.1 mycroft #endif
282 1.15 bouyer if ((e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp)) != 0) {
283 1.18 perseant printf("lfs_truncate: bread: %d\n",e1);
284 1.15 bouyer brelse(bp);
285 1.1 mycroft return (e1);
286 1.15 bouyer }
287 1.9 bouyer ip->i_ffs_size = length;
288 1.12 fvdl #if defined(UVM)
289 1.11 chs (void)uvm_vnp_uncache(vp);
290 1.11 chs #else
291 1.1 mycroft (void)vnode_pager_uncache(vp);
292 1.11 chs #endif
293 1.12 fvdl newsize = blksize(fs, ip, lbn);
294 1.12 fvdl bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
295 1.18 perseant #ifdef DEBUG
296 1.18 perseant if(bp->b_flags & B_CALL)
297 1.18 perseant panic("Can't allocbuf malloced buffer!");
298 1.18 perseant else
299 1.18 perseant #endif
300 1.18 perseant allocbuf(bp, newsize);
301 1.18 perseant if(oldsize_newlast > newsize)
302 1.18 perseant ip->i_ffs_blocks -= btodb(oldsize_newlast - newsize);
303 1.18 perseant if ((e1 = VOP_BWRITE(bp)) != 0) {
304 1.18 perseant printf("lfs_truncate: bwrite: %d\n",e1);
305 1.1 mycroft return (e1);
306 1.18 perseant }
307 1.1 mycroft }
308 1.1 mycroft /*
309 1.1 mycroft * Modify sup->su_nbyte counters for each deleted block; keep track
310 1.9 bouyer * of number of blocks removed for ip->i_ffs_blocks.
311 1.1 mycroft */
312 1.12 fvdl fragsreleased = 0;
313 1.1 mycroft num = 0;
314 1.1 mycroft lastseg = -1;
315 1.1 mycroft
316 1.1 mycroft for (lbn = olastblock; lbn >= lastblock;) {
317 1.1 mycroft /* XXX use run length from bmap array to make this faster */
318 1.1 mycroft ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
319 1.12 fvdl if (lbn == olastblock) {
320 1.1 mycroft for (i = NIADDR + 2; i--;)
321 1.1 mycroft a_end[i] = a[i];
322 1.12 fvdl freesize = oldsize_lastblock;
323 1.12 fvdl } else
324 1.12 fvdl freesize = fs->lfs_bsize;
325 1.1 mycroft switch (depth) {
326 1.18 perseant case 0: /* Direct block. */
327 1.9 bouyer daddr = ip->i_ffs_db[lbn];
328 1.12 fvdl SEGDEC(freesize);
329 1.9 bouyer ip->i_ffs_db[lbn] = 0;
330 1.1 mycroft --lbn;
331 1.1 mycroft break;
332 1.1 mycroft #ifdef DIAGNOSTIC
333 1.18 perseant case 1: /* An indirect block. */
334 1.1 mycroft panic("lfs_truncate: ufs_bmaparray returned depth 1");
335 1.1 mycroft /* NOTREACHED */
336 1.1 mycroft #endif
337 1.18 perseant default: /* Chain of indirect blocks. */
338 1.1 mycroft inp = a + --depth;
339 1.1 mycroft if (inp->in_off > 0 && lbn != lastblock) {
340 1.1 mycroft lbn -= inp->in_off < lbn - lastblock ?
341 1.18 perseant inp->in_off : lbn - lastblock;
342 1.1 mycroft break;
343 1.1 mycroft }
344 1.1 mycroft for (; depth && (inp->in_off == 0 || lbn == lastblock);
345 1.18 perseant --inp, --depth) {
346 1.1 mycroft if (bread(vp,
347 1.18 perseant inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
348 1.1 mycroft panic("lfs_truncate: bread bno %d",
349 1.18 perseant inp->in_lbn);
350 1.12 fvdl daddrp = (ufs_daddr_t *)bp->b_data + inp->in_off;
351 1.1 mycroft for (i = inp->in_off;
352 1.18 perseant i++ <= a_end[depth].in_off;) {
353 1.1 mycroft daddr = *daddrp++;
354 1.12 fvdl SEGDEC(freesize);
355 1.1 mycroft }
356 1.1 mycroft a_end[depth].in_off = NINDIR(fs) - 1;
357 1.1 mycroft if (inp->in_off == 0)
358 1.1 mycroft brelse (bp);
359 1.1 mycroft else {
360 1.12 fvdl bzero((ufs_daddr_t *)bp->b_data +
361 1.18 perseant inp->in_off, fs->lfs_bsize -
362 1.18 perseant inp->in_off * sizeof(ufs_daddr_t));
363 1.18 perseant if ((e1 = VOP_BWRITE(bp)) != 0) {
364 1.18 perseant printf("lfs_truncate: indir bwrite: %d\n",e1);
365 1.1 mycroft return (e1);
366 1.18 perseant }
367 1.1 mycroft }
368 1.1 mycroft }
369 1.1 mycroft if (depth == 0 && a[1].in_off == 0) {
370 1.1 mycroft off = a[0].in_off;
371 1.9 bouyer daddr = ip->i_ffs_ib[off];
372 1.12 fvdl SEGDEC(freesize);
373 1.9 bouyer ip->i_ffs_ib[off] = 0;
374 1.1 mycroft }
375 1.1 mycroft if (lbn == lastblock || lbn <= NDADDR)
376 1.1 mycroft --lbn;
377 1.1 mycroft else {
378 1.1 mycroft lbn -= NINDIR(fs);
379 1.1 mycroft if (lbn < lastblock)
380 1.1 mycroft lbn = lastblock;
381 1.1 mycroft }
382 1.1 mycroft }
383 1.1 mycroft }
384 1.1 mycroft UPDATE_SEGUSE;
385 1.18 perseant
386 1.1 mycroft /* If truncating the file to 0, update the version number. */
387 1.1 mycroft if (length == 0) {
388 1.1 mycroft LFS_IENTRY(ifp, fs, ip->i_number, bp);
389 1.1 mycroft ++ifp->if_version;
390 1.1 mycroft (void) VOP_BWRITE(bp);
391 1.1 mycroft }
392 1.1 mycroft #ifdef DIAGNOSTIC
393 1.12 fvdl if (ip->i_ffs_blocks < fragstodb(fs, fragsreleased)) {
394 1.18 perseant panic("lfs_truncate: frag count < 0 (%d<%ld), ino %d\n",
395 1.18 perseant ip->i_ffs_blocks, fragstodb(fs, fragsreleased),
396 1.18 perseant ip->i_number);
397 1.12 fvdl fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
398 1.1 mycroft }
399 1.1 mycroft #endif
400 1.12 fvdl ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
401 1.12 fvdl fs->lfs_bfree += fragstodb(fs, fragsreleased);
402 1.1 mycroft ip->i_flag |= IN_CHANGE | IN_UPDATE;
403 1.1 mycroft /*
404 1.1 mycroft * Traverse dirty block list counting number of dirty buffers
405 1.1 mycroft * that are being deleted out of the cache, so that the lfs_avail
406 1.1 mycroft * field can be updated.
407 1.1 mycroft */
408 1.1 mycroft a_released = 0;
409 1.1 mycroft i_released = 0;
410 1.18 perseant for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
411 1.1 mycroft if (bp->b_flags & B_LOCKED) {
412 1.12 fvdl a_released += numfrags(fs, bp->b_bcount);
413 1.1 mycroft /*
414 1.1 mycroft * XXX
415 1.1 mycroft * When buffers are created in the cache, their block
416 1.1 mycroft * number is set equal to their logical block number.
417 1.1 mycroft * If that is still true, we are assuming that the
418 1.1 mycroft * blocks are new (not yet on disk) and weren't
419 1.1 mycroft * counted above. However, there is a slight chance
420 1.1 mycroft * that a block's disk address is equal to its logical
421 1.1 mycroft * block number in which case, we'll get an overcounting
422 1.1 mycroft * here.
423 1.1 mycroft */
424 1.18 perseant if (bp->b_blkno == bp->b_lblkno) {
425 1.12 fvdl i_released += numfrags(fs, bp->b_bcount);
426 1.18 perseant }
427 1.1 mycroft }
428 1.18 perseant }
429 1.12 fvdl fragsreleased = i_released;
430 1.1 mycroft #ifdef DIAGNOSTIC
431 1.12 fvdl if (fragsreleased > dbtofrags(fs, ip->i_ffs_blocks)) {
432 1.18 perseant printf("lfs_inode: %ld frags released > %d in inode %d\n",
433 1.18 perseant fragsreleased, dbtofrags(fs, ip->i_ffs_blocks),
434 1.18 perseant ip->i_number);
435 1.12 fvdl fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
436 1.1 mycroft }
437 1.1 mycroft #endif
438 1.12 fvdl fs->lfs_bfree += fragstodb(fs, fragsreleased);
439 1.12 fvdl ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
440 1.1 mycroft #ifdef DIAGNOSTIC
441 1.12 fvdl if (length == 0 && ip->i_ffs_blocks != 0) {
442 1.18 perseant printf("lfs_inode: trunc to zero, but %d blocks left on inode %d\n",
443 1.18 perseant ip->i_ffs_blocks, ip->i_number);
444 1.18 perseant panic("lfs_inode\n");
445 1.12 fvdl }
446 1.1 mycroft #endif
447 1.12 fvdl fs->lfs_avail += fragstodb(fs, a_released);
448 1.1 mycroft e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
449 1.18 perseant 0, 0);
450 1.17 mycroft e2 = VOP_UPDATE(vp, NULL, NULL, 0);
451 1.18 perseant if(e1)
452 1.18 perseant printf("lfs_truncate: vinvalbuf: %d\n",e1);
453 1.18 perseant if(e2)
454 1.18 perseant printf("lfs_truncate: update: %d\n",e2);
455 1.18 perseant
456 1.1 mycroft return (e1 ? e1 : e2 ? e2 : 0);
457 1.1 mycroft }
458