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