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