ffs_inode.c revision 1.32 1 /* $NetBSD: ffs_inode.c,v 1.32 2000/05/28 04:13:58 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
36 */
37
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_ffs.h"
40 #include "opt_quota.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/file.h>
48 #include <sys/buf.h>
49 #include <sys/vnode.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/trace.h>
53 #include <sys/resourcevar.h>
54
55 #include <vm/vm.h>
56
57 #include <uvm/uvm_extern.h>
58
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/inode.h>
61 #include <ufs/ufs/ufsmount.h>
62 #include <ufs/ufs/ufs_extern.h>
63 #include <ufs/ufs/ufs_bswap.h>
64
65 #include <ufs/ffs/fs.h>
66 #include <ufs/ffs/ffs_extern.h>
67
68 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
69 ufs_daddr_t, int, long *));
70
71 /*
72 * Update the access, modified, and inode change times as specified
73 * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
74 * The IN_MODIFIED flag is used to specify that the inode needs to be
75 * updated but that the times have already been set. The access
76 * and modified times are taken from the second and third parameters;
77 * the inode change time is always taken from the current time. If
78 * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
79 * softupdates, then wait for the disk write of the inode to complete.
80 */
81
82 int
83 ffs_update(v)
84 void *v;
85 {
86 struct vop_update_args /* {
87 struct vnode *a_vp;
88 struct timespec *a_access;
89 struct timespec *a_modify;
90 int a_flags;
91 } */ *ap = v;
92 struct fs *fs;
93 struct buf *bp;
94 struct inode *ip;
95 int error;
96 struct timespec ts;
97 caddr_t cp;
98 int waitfor;
99
100 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
101 return (0);
102 ip = VTOI(ap->a_vp);
103 TIMEVAL_TO_TIMESPEC(&time, &ts);
104 FFS_ITIMES(ip,
105 ap->a_access ? ap->a_access : &ts,
106 ap->a_modify ? ap->a_modify : &ts, &ts);
107 if ((ip->i_flag & IN_MODIFIED) == 0 && (ap->a_flags & UPDATE_WAIT) == 0)
108 return (0);
109 ip->i_flag &= ~IN_MODIFIED;
110 fs = ip->i_fs;
111
112 waitfor=0;
113 if ((ap->a_flags & UPDATE_DIROP) && !DOINGSOFTDEP(ap->a_vp))
114 waitfor = UPDATE_WAIT;
115 waitfor |= ap->a_flags & UPDATE_WAIT;
116
117 /*
118 * Ensure that uid and gid are correct. This is a temporary
119 * fix until fsck has been changed to do the update.
120 */
121 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
122 ip->i_din.ffs_din.di_ouid = ip->i_ffs_uid; /* XXX */
123 ip->i_din.ffs_din.di_ogid = ip->i_ffs_gid; /* XXX */
124 } /* XXX */
125 error = bread(ip->i_devvp,
126 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
127 (int)fs->fs_bsize, NOCRED, &bp);
128 if (error) {
129 brelse(bp);
130 return (error);
131 }
132 if (DOINGSOFTDEP(ap->a_vp))
133 softdep_update_inodeblock(ip, bp, waitfor);
134 else if (ip->i_ffs_effnlink != ip->i_ffs_nlink)
135 panic("ffs_update: bad link cnt");
136 cp = (caddr_t)bp->b_data +
137 (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
138 #ifdef FFS_EI
139 if (UFS_FSNEEDSWAP(fs))
140 ffs_dinode_swap(&ip->i_din.ffs_din, (struct dinode *)cp);
141 else
142 #endif
143 memcpy(cp, &ip->i_din.ffs_din, DINODE_SIZE);
144 if (waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
145 return (bwrite(bp));
146 } else {
147 bdwrite(bp);
148 return (0);
149 }
150 }
151
152 #define SINGLE 0 /* index of single indirect block */
153 #define DOUBLE 1 /* index of double indirect block */
154 #define TRIPLE 2 /* index of triple indirect block */
155 /*
156 * Truncate the inode oip to at most length size, freeing the
157 * disk blocks.
158 */
159 int
160 ffs_truncate(v)
161 void *v;
162 {
163 struct vop_truncate_args /* {
164 struct vnode *a_vp;
165 off_t a_length;
166 int a_flags;
167 struct ucred *a_cred;
168 struct proc *a_p;
169 } */ *ap = v;
170 struct vnode *ovp = ap->a_vp;
171 ufs_daddr_t lastblock;
172 struct inode *oip;
173 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
174 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
175 off_t length = ap->a_length;
176 struct fs *fs;
177 struct buf *bp;
178 int offset, size, level;
179 long count, nblocks, blocksreleased = 0;
180 int i;
181 int aflags, error, allerror = 0;
182 off_t osize;
183
184 if (length < 0)
185 return (EINVAL);
186 oip = VTOI(ovp);
187 #if 1
188 /*
189 * XXX. Was in Kirk's patches. Is it good behavior to just
190 * return and not update modification times?
191 */
192 if (oip->i_ffs_size == length)
193 return (0);
194 #endif
195 if (ovp->v_type == VLNK &&
196 (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
197 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
198 oip->i_din.ffs_din.di_blocks == 0))) {
199 #ifdef DIAGNOSTIC
200 if (length != 0)
201 panic("ffs_truncate: partial truncate of symlink");
202 #endif
203 memset((char *)&oip->i_ffs_shortlink, 0, (u_int)oip->i_ffs_size);
204 oip->i_ffs_size = 0;
205 oip->i_flag |= IN_CHANGE | IN_UPDATE;
206 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
207 }
208 if (oip->i_ffs_size == length) {
209 oip->i_flag |= IN_CHANGE | IN_UPDATE;
210 return (VOP_UPDATE(ovp, NULL, NULL, 0));
211 }
212 #ifdef QUOTA
213 if ((error = getinoquota(oip)) != 0)
214 return (error);
215 #endif
216 fs = oip->i_fs;
217 osize = oip->i_ffs_size;
218 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
219
220 if (DOINGSOFTDEP(ovp)) {
221 uvm_vnp_setsize(ovp, length);
222 (void) uvm_vnp_uncache(ovp);
223 if (length > 0) {
224 /*
225 * If a file is only partially truncated, then
226 * we have to clean up the data structures
227 * describing the allocation past the truncation
228 * point. Finding and deallocating those structures
229 * is a lot of work. Since partial truncation occurs
230 * rarely, we solve the problem by syncing the file
231 * so that it will have no data structures left.
232 */
233 if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
234 ap->a_p)) != 0)
235 return (error);
236 } else {
237 #ifdef QUOTA
238 (void) chkdq(oip, -oip->i_ffs_blocks, NOCRED, 0);
239 #endif
240 softdep_setup_freeblocks(oip, length);
241 (void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
242 oip->i_flag |= IN_CHANGE | IN_UPDATE;
243 return (VOP_UPDATE(ovp, NULL, NULL, 0));
244 }
245 }
246 /*
247 * Lengthen the size of the file. We must ensure that the
248 * last byte of the file is allocated. Since the smallest
249 * value of osize is 0, length will be at least 1.
250 */
251 if (osize < length) {
252 if (length > fs->fs_maxfilesize)
253 return (EFBIG);
254 aflags = B_CLRBUF;
255 if (ap->a_flags & IO_SYNC)
256 aflags |= B_SYNC;
257 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
258 if (error)
259 return (error);
260 oip->i_ffs_size = length;
261 uvm_vnp_setsize(ovp, length);
262 (void) uvm_vnp_uncache(ovp);
263 if (aflags & B_SYNC)
264 bwrite(bp);
265 else
266 bawrite(bp);
267 oip->i_flag |= IN_CHANGE | IN_UPDATE;
268 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
269 }
270 /*
271 * Shorten the size of the file. If the file is not being
272 * truncated to a block boundary, the contents of the
273 * partial block following the end of the file must be
274 * zero'ed in case it ever becomes accessible again because
275 * of subsequent file growth. Directories however are not
276 * zero'ed as they should grow back initialized to empty.
277 */
278 offset = blkoff(fs, length);
279 if (offset == 0) {
280 oip->i_ffs_size = length;
281 } else {
282 lbn = lblkno(fs, length);
283 aflags = B_CLRBUF;
284 if (ap->a_flags & IO_SYNC)
285 aflags |= B_SYNC;
286 error = VOP_BALLOC(ovp, length - 1, 1, ap->a_cred, aflags, &bp);
287 if (error)
288 return (error);
289 oip->i_ffs_size = length;
290 size = blksize(fs, oip, lbn);
291 (void) uvm_vnp_uncache(ovp);
292 if (ovp->v_type != VDIR)
293 memset((char *)bp->b_data + offset, 0,
294 (u_int)(size - offset));
295 allocbuf(bp, size);
296 if (aflags & B_SYNC)
297 bwrite(bp);
298 else
299 bawrite(bp);
300 }
301 uvm_vnp_setsize(ovp, length);
302 /*
303 * Calculate index into inode's block list of
304 * last direct and indirect blocks (if any)
305 * which we want to keep. Lastblock is -1 when
306 * the file is truncated to 0.
307 */
308 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
309 lastiblock[SINGLE] = lastblock - NDADDR;
310 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
311 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
312 nblocks = btodb(fs->fs_bsize);
313 /*
314 * Update file and block pointers on disk before we start freeing
315 * blocks. If we crash before free'ing blocks below, the blocks
316 * will be returned to the free list. lastiblock values are also
317 * normalized to -1 for calls to ffs_indirtrunc below.
318 */
319 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
320 for (level = TRIPLE; level >= SINGLE; level--)
321 if (lastiblock[level] < 0) {
322 oip->i_ffs_ib[level] = 0;
323 lastiblock[level] = -1;
324 }
325 for (i = NDADDR - 1; i > lastblock; i--)
326 oip->i_ffs_db[i] = 0;
327 oip->i_flag |= IN_CHANGE | IN_UPDATE;
328 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
329 if (error && !allerror)
330 allerror = error;
331
332 /*
333 * Having written the new inode to disk, save its new configuration
334 * and put back the old block pointers long enough to process them.
335 * Note that we save the new block configuration so we can check it
336 * when we are done.
337 */
338 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
339 memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
340 oip->i_ffs_size = osize;
341 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
342 if (error && !allerror)
343 allerror = error;
344
345 /*
346 * Indirect blocks first.
347 */
348 indir_lbn[SINGLE] = -NDADDR;
349 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
350 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
351 for (level = TRIPLE; level >= SINGLE; level--) {
352 bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
353 if (bn != 0) {
354 error = ffs_indirtrunc(oip, indir_lbn[level],
355 fsbtodb(fs, bn), lastiblock[level], level, &count);
356 if (error)
357 allerror = error;
358 blocksreleased += count;
359 if (lastiblock[level] < 0) {
360 oip->i_ffs_ib[level] = 0;
361 ffs_blkfree(oip, bn, fs->fs_bsize);
362 blocksreleased += nblocks;
363 }
364 }
365 if (lastiblock[level] >= 0)
366 goto done;
367 }
368
369 /*
370 * All whole direct blocks or frags.
371 */
372 for (i = NDADDR - 1; i > lastblock; i--) {
373 long bsize;
374
375 bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
376 if (bn == 0)
377 continue;
378 oip->i_ffs_db[i] = 0;
379 bsize = blksize(fs, oip, i);
380 ffs_blkfree(oip, bn, bsize);
381 blocksreleased += btodb(bsize);
382 }
383 if (lastblock < 0)
384 goto done;
385
386 /*
387 * Finally, look for a change in size of the
388 * last direct block; release any frags.
389 */
390 bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
391 if (bn != 0) {
392 long oldspace, newspace;
393
394 /*
395 * Calculate amount of space we're giving
396 * back as old block size minus new block size.
397 */
398 oldspace = blksize(fs, oip, lastblock);
399 oip->i_ffs_size = length;
400 newspace = blksize(fs, oip, lastblock);
401 if (newspace == 0)
402 panic("itrunc: newspace");
403 if (oldspace - newspace > 0) {
404 /*
405 * Block number of space to be free'd is
406 * the old block # plus the number of frags
407 * required for the storage we're keeping.
408 */
409 bn += numfrags(fs, newspace);
410 ffs_blkfree(oip, bn, oldspace - newspace);
411 blocksreleased += btodb(oldspace - newspace);
412 }
413 }
414
415 done:
416 #ifdef DIAGNOSTIC
417 for (level = SINGLE; level <= TRIPLE; level++)
418 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
419 panic("itrunc1");
420 for (i = 0; i < NDADDR; i++)
421 if (newblks[i] != oip->i_ffs_db[i])
422 panic("itrunc2");
423 if (length == 0 &&
424 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
425 panic("itrunc3");
426 #endif /* DIAGNOSTIC */
427 /*
428 * Put back the real size.
429 */
430 oip->i_ffs_size = length;
431 oip->i_ffs_blocks -= blocksreleased;
432 if (oip->i_ffs_blocks < 0) /* sanity */
433 oip->i_ffs_blocks = 0;
434 oip->i_flag |= IN_CHANGE;
435 #ifdef QUOTA
436 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
437 #endif
438 return (allerror);
439 }
440
441 /*
442 * Release blocks associated with the inode ip and stored in the indirect
443 * block bn. Blocks are free'd in LIFO order up to (but not including)
444 * lastbn. If level is greater than SINGLE, the block is an indirect block
445 * and recursive calls to indirtrunc must be used to cleanse other indirect
446 * blocks.
447 *
448 * NB: triple indirect blocks are untested.
449 */
450 static int
451 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
452 struct inode *ip;
453 ufs_daddr_t lbn, lastbn;
454 ufs_daddr_t dbn;
455 int level;
456 long *countp;
457 {
458 int i;
459 struct buf *bp;
460 struct fs *fs = ip->i_fs;
461 ufs_daddr_t *bap;
462 struct vnode *vp;
463 ufs_daddr_t *copy = NULL, nb, nlbn, last;
464 long blkcount, factor;
465 int nblocks, blocksreleased = 0;
466 int error = 0, allerror = 0;
467
468 /*
469 * Calculate index in current block of last
470 * block to be kept. -1 indicates the entire
471 * block so we need not calculate the index.
472 */
473 factor = 1;
474 for (i = SINGLE; i < level; i++)
475 factor *= NINDIR(fs);
476 last = lastbn;
477 if (lastbn > 0)
478 last /= factor;
479 nblocks = btodb(fs->fs_bsize);
480 /*
481 * Get buffer of block pointers, zero those entries corresponding
482 * to blocks to be free'd, and update on disk copy first. Since
483 * double(triple) indirect before single(double) indirect, calls
484 * to bmap on these blocks will fail. However, we already have
485 * the on disk address, so we have to set the b_blkno field
486 * explicitly instead of letting bread do everything for us.
487 */
488 vp = ITOV(ip);
489 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
490 if (bp->b_flags & (B_DONE | B_DELWRI)) {
491 /* Braces must be here in case trace evaluates to nothing. */
492 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
493 } else {
494 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
495 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
496 bp->b_flags |= B_READ;
497 if (bp->b_bcount > bp->b_bufsize)
498 panic("ffs_indirtrunc: bad buffer size");
499 bp->b_blkno = dbn;
500 VOP_STRATEGY(bp);
501 error = biowait(bp);
502 }
503 if (error) {
504 brelse(bp);
505 *countp = 0;
506 return (error);
507 }
508
509 bap = (ufs_daddr_t *)bp->b_data;
510 if (lastbn != -1) {
511 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
512 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
513 memset((caddr_t)&bap[last + 1], 0,
514 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
515 error = bwrite(bp);
516 if (error)
517 allerror = error;
518 bap = copy;
519 }
520
521 /*
522 * Recursively free totally unused blocks.
523 */
524 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
525 i--, nlbn += factor) {
526 nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
527 if (nb == 0)
528 continue;
529 if (level > SINGLE) {
530 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
531 (ufs_daddr_t)-1, level - 1,
532 &blkcount);
533 if (error)
534 allerror = error;
535 blocksreleased += blkcount;
536 }
537 ffs_blkfree(ip, nb, fs->fs_bsize);
538 blocksreleased += nblocks;
539 }
540
541 /*
542 * Recursively free last partial block.
543 */
544 if (level > SINGLE && lastbn >= 0) {
545 last = lastbn % factor;
546 nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
547 if (nb != 0) {
548 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
549 last, level - 1, &blkcount);
550 if (error)
551 allerror = error;
552 blocksreleased += blkcount;
553 }
554 }
555
556 if (copy != NULL) {
557 FREE(copy, M_TEMP);
558 } else {
559 bp->b_flags |= B_INVAL;
560 brelse(bp);
561 }
562
563 *countp = blocksreleased;
564 return (allerror);
565 }
566