lfs_inode.c revision 1.22.2.1.2.3 1 /* $NetBSD: lfs_inode.c,v 1.22.2.1.2.3 1999/08/31 21:03:46 perseant 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 struct lfs *fs = VFSTOUFS(vp->v_mount)->um_lfs;
132
133 if (vp->v_mount->mnt_flag & MNT_RDONLY)
134 return (0);
135 ip = VTOI(vp);
136
137 /*
138 * If we are called from vinvalbuf, and the file's blocks have
139 * already been scheduled for writing, but the writes have not
140 * yet completed, lfs_vflush will not be called, and vinvalbuf
141 * will cause a panic. So, we must wait until any pending write
142 * for our inode completes, if we are called with LFS_SYNC set.
143 */
144 while((ap->a_waitfor & LFS_SYNC) && WRITEINPROG(vp)) {
145 #ifdef DEBUG_LFS
146 printf("lfs_update: sleeping on inode %d (in-progress)\n",ip->i_number);
147 #endif
148 tsleep(vp, (PRIBIO+1), "lfs_update", 0);
149 }
150 mod = ip->i_flag & IN_MODIFIED;
151 oflag = ip->i_flag;
152 TIMEVAL_TO_TIMESPEC(&time, &ts);
153 LFS_ITIMES(ip,
154 ap->a_access ? ap->a_access : &ts,
155 ap->a_modify ? ap->a_modify : &ts, &ts);
156 if (!mod && (ip->i_flag & IN_MODIFIED))
157 ip->i_lfs->lfs_uinodes++;
158 if ((ip->i_flag & (IN_MODIFIED|IN_CLEANING)) == 0) {
159 return (0);
160 }
161
162 /* If sync, push back the vnode and any dirty blocks it may have. */
163 if(ap->a_waitfor & LFS_SYNC) {
164 /* Avoid flushing VDIROP. */
165 ++fs->lfs_diropwait;
166 while(vp->v_flag & VDIROP) {
167 #ifdef DEBUG_LFS
168 printf("lfs_update: sleeping on inode %d (dirops)\n",ip->i_number);
169 #endif
170 if(fs->lfs_dirops==0)
171 lfs_flush_fs(vp->v_mount,0);
172 else
173 tsleep(&fs->lfs_writer, PRIBIO+1, "lfs_fsync", 0);
174 /* XXX KS - by falling out here, are we writing the vn
175 twice? */
176 }
177 --fs->lfs_diropwait;
178 return lfs_vflush(vp);
179 }
180 return 0;
181 }
182
183 /* Update segment usage information when removing a block. */
184 #define UPDATE_SEGUSE do {\
185 if (lastseg != -1) { \
186 LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
187 if (num > sup->su_nbytes) { \
188 printf("lfs_truncate: negative bytes: segment %d short by %d\n", \
189 lastseg, num - sup->su_nbytes); \
190 panic("lfs_truncate: negative bytes"); \
191 sup->su_nbytes = 0; \
192 } else \
193 sup->su_nbytes -= num; \
194 e1 = VOP_BWRITE(sup_bp); \
195 fragsreleased += numfrags(fs, num); \
196 } \
197 fragsreleased += numfrags(fs, unum); \
198 } while(0)
199
200 #define SEGDEC(S) do { \
201 if (daddr > 0) { \
202 if (lastseg != (seg = datosn(fs, daddr))) { \
203 UPDATE_SEGUSE; \
204 num = (S); \
205 lastseg = seg; \
206 } else \
207 num += (S); \
208 } else if (daddr == UNWRITTEN) { \
209 unum += (S); \
210 } \
211 } while(0)
212
213 /*
214 * Truncate the inode ip to at most length size. Update segment usage
215 * table information.
216 */
217 /* ARGSUSED */
218 int
219 lfs_truncate(v)
220 void *v;
221 {
222 struct vop_truncate_args /* {
223 struct vnode *a_vp;
224 off_t a_length;
225 int a_flags;
226 struct ucred *a_cred;
227 struct proc *a_p;
228 } */ *ap = v;
229 register struct indir *inp;
230 register int i;
231 register ufs_daddr_t *daddrp;
232 register struct vnode *vp = ap->a_vp;
233 off_t length = ap->a_length;
234 struct buf *bp, *sup_bp;
235 struct ifile *ifp;
236 struct inode *ip;
237 struct lfs *fs;
238 struct indir a[NIADDR + 2], a_end[NIADDR + 2];
239 SEGUSE *sup;
240 ufs_daddr_t daddr, lastblock, lbn, olastblock;
241 ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
242 long off, a_released, fragsreleased, i_released;
243 int e1, e2, depth, lastseg, num, unum, offset, seg, freesize;
244
245 ip = VTOI(vp);
246
247 if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
248 #ifdef DIAGNOSTIC
249 if (length != 0)
250 panic("lfs_truncate: partial truncate of symlink");
251 #endif
252 bzero((char *)&ip->i_ffs_shortlink, (u_int)ip->i_ffs_size);
253 ip->i_ffs_size = 0;
254 ip->i_flag |= IN_CHANGE | IN_UPDATE;
255 return (VOP_UPDATE(vp, NULL, NULL, 0));
256 }
257 uvm_vnp_setsize(vp, length);
258
259 fs = ip->i_lfs;
260 lfs_imtime(fs);
261
262 /* If length is larger than the file, just update the times. */
263 if (ip->i_ffs_size <= length) {
264 ip->i_flag |= IN_CHANGE | IN_UPDATE;
265 return (VOP_UPDATE(vp, NULL, NULL, 0));
266 }
267
268 /*
269 * Make sure no writes happen while we're truncating.
270 * Otherwise, blocks which are accounted for on the inode
271 * *and* which have been created for cleaning can coexist,
272 * and cause us to overcount, and panic below.
273 *
274 * XXX KS - too restrictive? Maybe only when cleaning?
275 */
276 while(fs->lfs_seglock) {
277 tsleep(&fs->lfs_seglock, (PRIBIO+1), "lfs_truncate", 0);
278 }
279
280 /*
281 * Calculate index into inode's block list of last direct and indirect
282 * blocks (if any) which we want to keep. Lastblock is 0 when the
283 * file is truncated to 0.
284 */
285 lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
286 olastblock = lblkno(fs, ip->i_ffs_size + fs->lfs_bsize - 1) - 1;
287
288 /*
289 * Update the size of the file. If the file is not being truncated to
290 * a block boundry, the contents of the partial block following the end
291 * must be zero'd in case it ever becomes accessible again
292 * because of subsequent file growth. For this part of the code,
293 * oldsize_newlast refers to the old size of the new last block in the
294 * file.
295 */
296 offset = blkoff(fs, length);
297 lbn = lblkno(fs, length);
298 oldsize_newlast = blksize(fs, ip, lbn);
299
300 /* Now set oldsize to the current size of the current last block */
301 oldsize_lastblock = blksize(fs, ip, olastblock);
302 if (offset == 0)
303 ip->i_ffs_size = length;
304 else {
305 #ifdef QUOTA
306 if ((e1 = getinoquota(ip)) != 0)
307 return (e1);
308 #endif
309 if ((e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp)) != 0) {
310 printf("lfs_truncate: bread: %d\n",e1);
311 brelse(bp);
312 return (e1);
313 }
314 ip->i_ffs_size = length;
315 newsize = blksize(fs, ip, lbn);
316 bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
317 #ifdef DEBUG
318 if(lfs_iscleaner_bp(bp))
319 panic("Can't allocbuf malloced buffer!");
320 else
321 #endif
322 allocbuf(bp, newsize);
323 if(oldsize_newlast > newsize)
324 ip->i_ffs_blocks -= btodb(oldsize_newlast - newsize);
325 if ((e1 = VOP_BWRITE(bp)) != 0) {
326 printf("lfs_truncate: bwrite: %d\n",e1);
327 return (e1);
328 }
329 }
330 /*
331 * Modify sup->su_nbyte counters for each deleted block; keep track
332 * of number of blocks removed for ip->i_ffs_blocks.
333 */
334 fragsreleased = 0;
335 num = 0;
336 unum = 0;
337 lastseg = -1;
338
339 for (lbn = olastblock; lbn >= lastblock;) {
340 /* XXX use run length from bmap array to make this faster */
341 ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
342 if (lbn == olastblock) {
343 for (i = NIADDR + 2; i--;)
344 a_end[i] = a[i];
345 freesize = oldsize_lastblock;
346 } else
347 freesize = fs->lfs_bsize;
348 switch (depth) {
349 case 0: /* Direct block. */
350 daddr = ip->i_ffs_db[lbn];
351 SEGDEC(freesize);
352 ip->i_ffs_db[lbn] = 0;
353 --lbn;
354 break;
355 #ifdef DIAGNOSTIC
356 case 1: /* An indirect block. */
357 panic("lfs_truncate: ufs_bmaparray returned depth 1");
358 /* NOTREACHED */
359 #endif
360 default: /* Chain of indirect blocks. */
361 inp = a + --depth;
362 if (inp->in_off > 0 && lbn != lastblock) {
363 lbn -= inp->in_off < lbn - lastblock ?
364 inp->in_off : lbn - lastblock;
365 break;
366 }
367 for (; depth && (inp->in_off == 0 || lbn == lastblock);
368 --inp, --depth) {
369 if (bread(vp,
370 inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
371 panic("lfs_truncate: bread bno %d",
372 inp->in_lbn);
373 daddrp = (ufs_daddr_t *)bp->b_data + inp->in_off;
374 for (i = inp->in_off;
375 i++ <= a_end[depth].in_off;) {
376 daddr = *daddrp++;
377 SEGDEC(freesize);
378 }
379 a_end[depth].in_off = NINDIR(fs) - 1;
380 if (inp->in_off == 0)
381 brelse (bp);
382 else {
383 bzero((ufs_daddr_t *)bp->b_data +
384 inp->in_off, fs->lfs_bsize -
385 inp->in_off * sizeof(ufs_daddr_t));
386 if ((e1 = VOP_BWRITE(bp)) != 0) {
387 printf("lfs_truncate: indir bwrite: %d\n",e1);
388 return (e1);
389 }
390 }
391 }
392 if (depth == 0 && a[1].in_off == 0) {
393 off = a[0].in_off;
394 daddr = ip->i_ffs_ib[off];
395 SEGDEC(freesize);
396 ip->i_ffs_ib[off] = 0;
397 }
398 if (lbn == lastblock || lbn <= NDADDR)
399 --lbn;
400 else {
401 lbn -= NINDIR(fs);
402 if (lbn < lastblock)
403 lbn = lastblock;
404 }
405 }
406 }
407 UPDATE_SEGUSE;
408
409 /* If truncating the file to 0, update the version number. */
410 if (length == 0) {
411 LFS_IENTRY(ifp, fs, ip->i_number, bp);
412 ++ifp->if_version;
413 (void) VOP_BWRITE(bp);
414 }
415 #ifdef DIAGNOSTIC
416 if (ip->i_ffs_blocks < fragstodb(fs, fragsreleased)) {
417 panic("lfs_truncate: frag count < 0 (%d<%ld), ino %d\n",
418 ip->i_ffs_blocks, fragstodb(fs, fragsreleased),
419 ip->i_number);
420 fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
421 }
422 #endif
423 ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
424 fs->lfs_bfree += fragstodb(fs, fragsreleased);
425 ip->i_flag |= IN_CHANGE | IN_UPDATE;
426 /*
427 * Traverse dirty block list counting number of dirty buffers
428 * that are being deleted out of the cache, so that the lfs_avail
429 * field can be updated.
430 */
431 a_released = 0;
432 i_released = 0;
433 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
434
435 /* XXX KS - Don't miscount if we're not truncating to zero. */
436 if(length>0 && !(bp->b_lblkno >= 0 && bp->b_lblkno > lastblock)
437 && !(bp->b_lblkno < 0 && bp->b_lblkno < -lastblock-NIADDR))
438 continue;
439
440 if (bp->b_flags & B_LOCKED) {
441 a_released += numfrags(fs, bp->b_bcount);
442 /*
443 * XXX
444 * When buffers are created in the cache, their block
445 * number is set equal to their logical block number.
446 * If that is still true, we are assuming that the
447 * blocks are new (not yet on disk) and weren't
448 * counted above. However, there is a slight chance
449 * that a block's disk address is equal to its logical
450 * block number in which case, we'll get an overcounting
451 * here.
452 */
453 if (bp->b_blkno == bp->b_lblkno) {
454 i_released += numfrags(fs, bp->b_bcount);
455 }
456 }
457 }
458 fragsreleased = i_released;
459 #ifdef DIAGNOSTIC
460 if (fragsreleased > dbtofrags(fs, ip->i_ffs_blocks)) {
461 printf("lfs_inode: %ld frags released > %d in inode %d\n",
462 fragsreleased, dbtofrags(fs, ip->i_ffs_blocks),
463 ip->i_number);
464 fragsreleased = dbtofrags(fs, ip->i_ffs_blocks);
465 }
466 #endif
467 fs->lfs_bfree += fragstodb(fs, fragsreleased);
468 ip->i_ffs_blocks -= fragstodb(fs, fragsreleased);
469 #ifdef DIAGNOSTIC
470 if (length == 0 && ip->i_ffs_blocks != 0) {
471 printf("lfs_inode: trunc to zero, but %d blocks left on inode %d\n",
472 ip->i_ffs_blocks, ip->i_number);
473 panic("lfs_inode\n");
474 }
475 #endif
476 fs->lfs_avail += fragstodb(fs, a_released);
477 if(length>0)
478 e1 = lfs_vinvalbuf(vp, ap->a_cred, ap->a_p, lastblock-1);
479 else
480 e1 = vinvalbuf(vp, 0, ap->a_cred, ap->a_p, 0, 0);
481 e2 = VOP_UPDATE(vp, NULL, NULL, 0);
482 if(e1)
483 printf("lfs_truncate: vinvalbuf: %d\n",e1);
484 if(e2)
485 printf("lfs_truncate: update: %d\n",e2);
486
487 return (e1 ? e1 : e2 ? e2 : 0);
488 }
489
490 /*
491 * Get rid of blocks a la vinvalbuf; but only blocks that are of a higher
492 * lblkno than the file size allows.
493 */
494 int
495 lfs_vinvalbuf(vp, cred, p, maxblk)
496 register struct vnode *vp;
497 struct ucred *cred;
498 struct proc *p;
499 ufs_daddr_t maxblk;
500 {
501 register struct buf *bp;
502 struct buf *nbp, *blist;
503 int i, s, error, dirty;
504
505 top:
506 dirty=0;
507 for (i=0;i<2;i++) {
508 if(i==0)
509 blist = vp->v_cleanblkhd.lh_first;
510 else /* i == 1 */
511 blist = vp->v_dirtyblkhd.lh_first;
512
513 for (bp = blist; bp; bp = nbp) {
514 nbp = bp->b_vnbufs.le_next;
515
516 s = splbio();
517 if (bp->b_flags & B_GATHERED) {
518 error = tsleep(vp, PRIBIO+1, "lfs_vin2", 0);
519 splx(s);
520 if(error)
521 return error;
522 goto top;
523 }
524 if (bp->b_flags & B_BUSY) {
525 bp->b_flags |= B_WANTED;
526 error = tsleep((caddr_t)bp,
527 (PRIBIO + 1), "lfs_vinval", 0);
528 splx(s);
529 if (error)
530 return (error);
531 goto top;
532 }
533
534 bp->b_flags |= B_BUSY;
535 splx(s);
536 if((bp->b_lblkno >= 0 && bp->b_lblkno > maxblk)
537 || (bp->b_lblkno < 0 && bp->b_lblkno < -maxblk-(NIADDR-1)))
538 {
539 bp->b_flags |= B_INVAL | B_VFLUSH;
540 if(lfs_iscleaner_bp(bp)) {
541 lfs_freebuf(bp);
542 } else {
543 brelse(bp);
544 }
545 ++dirty;
546 } else {
547 /*
548 * This buffer is still on its free list.
549 * So don't brelse, but wake up any sleepers.
550 */
551 bp->b_flags &= ~B_BUSY;
552 if(bp->b_flags & B_WANTED) {
553 bp->b_flags &= ~(B_WANTED|B_AGE);
554 wakeup(bp);
555 }
556 }
557 }
558 }
559 if(dirty)
560 goto top;
561 return (0);
562 }
563