ffs_inode.c revision 1.21 1 /* $NetBSD: ffs_inode.c,v 1.21 1998/06/09 07:46:32 scottr 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_quota.h"
40 #include "opt_uvm.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 #if defined(UVM)
58 #include <uvm/uvm_extern.h>
59 #endif
60
61 #include <ufs/ufs/quota.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ufs/ufsmount.h>
64 #include <ufs/ufs/ufs_extern.h>
65 #include <ufs/ufs/ufs_bswap.h>
66
67 #include <ufs/ffs/fs.h>
68 #include <ufs/ffs/ffs_extern.h>
69
70 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
71 ufs_daddr_t, int, long *));
72
73 /*
74 * Update the access, modified, and inode change times as specified
75 * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
76 * The IN_MODIFIED flag is used to specify that the inode needs to be
77 * updated but that the times have already been set. The access
78 * and modified times are taken from the second and third parameters;
79 * the inode change time is always taken from the current time. If
80 * waitfor is set, then wait for the disk write of the inode to
81 * complete.
82 */
83
84 int
85 ffs_update(v)
86 void *v;
87 {
88 struct vop_update_args /* {
89 struct vnode *a_vp;
90 struct timespec *a_access;
91 struct timespec *a_modify;
92 int a_waitfor;
93 } */ *ap = v;
94 register struct fs *fs;
95 struct buf *bp;
96 struct inode *ip;
97 int error;
98 struct timespec ts;
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, ap->a_access, ap->a_modify, &ts);
105 if ((ip->i_flag & IN_MODIFIED) == 0)
106 return (0);
107 ip->i_flag &= ~IN_MODIFIED;
108 fs = ip->i_fs;
109 /*
110 * Ensure that uid and gid are correct. This is a temporary
111 * fix until fsck has been changed to do the update.
112 */
113 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
114 ip->i_din.ffs_din.di_ouid = ip->i_ffs_uid; /* XXX */
115 ip->i_din.ffs_din.di_ogid = ip->i_ffs_gid; /* XXX */
116 } /* XXX */
117 error = bread(ip->i_devvp,
118 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
119 (int)fs->fs_bsize, NOCRED, &bp);
120 if (error) {
121 brelse(bp);
122 return (error);
123 }
124 if (UFS_MPNEEDSWAP(ap->a_vp->v_mount))
125 ffs_dinode_swap(&ip->i_din.ffs_din,
126 (struct dinode *)bp->b_data + ino_to_fsbo(fs, ip->i_number));
127 else
128 *((struct dinode *)bp->b_data +
129 ino_to_fsbo(fs, ip->i_number)) = ip->i_din.ffs_din;
130 if (ap->a_waitfor && (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
131 return (bwrite(bp));
132 else {
133 bdwrite(bp);
134 return (0);
135 }
136 }
137
138 #define SINGLE 0 /* index of single indirect block */
139 #define DOUBLE 1 /* index of double indirect block */
140 #define TRIPLE 2 /* index of triple indirect block */
141 /*
142 * Truncate the inode oip to at most length size, freeing the
143 * disk blocks.
144 */
145 int
146 ffs_truncate(v)
147 void *v;
148 {
149 struct vop_truncate_args /* {
150 struct vnode *a_vp;
151 off_t a_length;
152 int a_flags;
153 struct ucred *a_cred;
154 struct proc *a_p;
155 } */ *ap = v;
156 register struct vnode *ovp = ap->a_vp;
157 register ufs_daddr_t lastblock;
158 register struct inode *oip;
159 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
160 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
161 off_t length = ap->a_length;
162 register struct fs *fs;
163 struct buf *bp;
164 int offset, size, level;
165 long count, nblocks, vflags, blocksreleased = 0;
166 struct timespec ts;
167 register int i;
168 int aflags, error, allerror;
169 off_t osize;
170
171 if (length < 0)
172 return (EINVAL);
173 oip = VTOI(ovp);
174 TIMEVAL_TO_TIMESPEC(&time, &ts);
175 if (ovp->v_type == VLNK &&
176 (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
177 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
178 oip->i_din.ffs_din.di_blocks == 0))) {
179 #ifdef DIAGNOSTIC
180 if (length != 0)
181 panic("ffs_truncate: partial truncate of symlink");
182 #endif
183 bzero((char *)&oip->i_ffs_shortlink, (u_int)oip->i_ffs_size);
184 oip->i_ffs_size = 0;
185 oip->i_flag |= IN_CHANGE | IN_UPDATE;
186 return (VOP_UPDATE(ovp, &ts, &ts, 1));
187 }
188 if (oip->i_ffs_size == length) {
189 oip->i_flag |= IN_CHANGE | IN_UPDATE;
190 return (VOP_UPDATE(ovp, &ts, &ts, 0));
191 }
192 #ifdef QUOTA
193 if ((error = getinoquota(oip)) != 0)
194 return (error);
195 #endif
196 fs = oip->i_fs;
197 osize = oip->i_ffs_size;
198 ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
199 /*
200 * Lengthen the size of the file. We must ensure that the
201 * last byte of the file is allocated. Since the smallest
202 * value of osize is 0, length will be at least 1.
203 */
204 if (osize < length) {
205 if (length > fs->fs_maxfilesize)
206 return (EFBIG);
207 offset = blkoff(fs, length - 1);
208 lbn = lblkno(fs, length - 1);
209 aflags = B_CLRBUF;
210 if (ap->a_flags & IO_SYNC)
211 aflags |= B_SYNC;
212 error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
213 aflags);
214 if (error)
215 return (error);
216 oip->i_ffs_size = length;
217 #if defined(UVM)
218 uvm_vnp_setsize(ovp, length);
219 (void) uvm_vnp_uncache(ovp);
220 #else
221 vnode_pager_setsize(ovp, length);
222 (void) vnode_pager_uncache(ovp);
223 #endif
224 if (aflags & B_SYNC)
225 bwrite(bp);
226 else
227 bawrite(bp);
228 oip->i_flag |= IN_CHANGE | IN_UPDATE;
229 return (VOP_UPDATE(ovp, &ts, &ts, 1));
230 }
231 /*
232 * Shorten the size of the file. If the file is not being
233 * truncated to a block boundry, the contents of the
234 * partial block following the end of the file must be
235 * zero'ed in case it ever become accessable again because
236 * of subsequent file growth.
237 */
238 offset = blkoff(fs, length);
239 if (offset == 0) {
240 oip->i_ffs_size = length;
241 } else {
242 lbn = lblkno(fs, length);
243 aflags = B_CLRBUF;
244 if (ap->a_flags & IO_SYNC)
245 aflags |= B_SYNC;
246 error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags);
247 if (error)
248 return (error);
249 oip->i_ffs_size = length;
250 size = blksize(fs, oip, lbn);
251 #if defined(UVM)
252 (void) uvm_vnp_uncache(ovp);
253 #else
254 (void) vnode_pager_uncache(ovp);
255 #endif
256 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
257 allocbuf(bp, size);
258 if (aflags & B_SYNC)
259 bwrite(bp);
260 else
261 bawrite(bp);
262 }
263 #if defined(UVM)
264 uvm_vnp_setsize(ovp, length);
265 #else
266 vnode_pager_setsize(ovp, length);
267 #endif
268 /*
269 * Calculate index into inode's block list of
270 * last direct and indirect blocks (if any)
271 * which we want to keep. Lastblock is -1 when
272 * the file is truncated to 0.
273 */
274 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
275 lastiblock[SINGLE] = lastblock - NDADDR;
276 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
277 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
278 nblocks = btodb(fs->fs_bsize);
279 /*
280 * Update file and block pointers on disk before we start freeing
281 * blocks. If we crash before free'ing blocks below, the blocks
282 * will be returned to the free list. lastiblock values are also
283 * normalized to -1 for calls to ffs_indirtrunc below.
284 */
285 bcopy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
286 for (level = TRIPLE; level >= SINGLE; level--)
287 if (lastiblock[level] < 0) {
288 oip->i_ffs_ib[level] = 0;
289 lastiblock[level] = -1;
290 }
291 for (i = NDADDR - 1; i > lastblock; i--)
292 oip->i_ffs_db[i] = 0;
293 oip->i_flag |= IN_CHANGE | IN_UPDATE;
294 if ((error = VOP_UPDATE(ovp, &ts, &ts, 1)) != 0)
295 allerror = error;
296 /*
297 * Having written the new inode to disk, save its new configuration
298 * and put back the old block pointers long enough to process them.
299 * Note that we save the new block configuration so we can check it
300 * when we are done.
301 */
302 bcopy((caddr_t)&oip->i_ffs_db[0], (caddr_t)newblks, sizeof newblks);
303 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
304 oip->i_ffs_size = osize;
305 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
306 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
307
308 /*
309 * Indirect blocks first.
310 */
311 indir_lbn[SINGLE] = -NDADDR;
312 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
313 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
314 for (level = TRIPLE; level >= SINGLE; level--) {
315 bn = ufs_rw32(oip->i_ffs_ib[level], UFS_MPNEEDSWAP(ovp->v_mount));
316 if (bn != 0) {
317 error = ffs_indirtrunc(oip, indir_lbn[level],
318 fsbtodb(fs, bn), lastiblock[level], level, &count);
319 if (error)
320 allerror = error;
321 blocksreleased += count;
322 if (lastiblock[level] < 0) {
323 oip->i_ffs_ib[level] = 0;
324 ffs_blkfree(oip, bn, fs->fs_bsize);
325 blocksreleased += nblocks;
326 }
327 }
328 if (lastiblock[level] >= 0)
329 goto done;
330 }
331
332 /*
333 * All whole direct blocks or frags.
334 */
335 for (i = NDADDR - 1; i > lastblock; i--) {
336 register long bsize;
337
338 bn = ufs_rw32(oip->i_ffs_db[i], UFS_MPNEEDSWAP(ovp->v_mount));
339 if (bn == 0)
340 continue;
341 oip->i_ffs_db[i] = 0;
342 bsize = blksize(fs, oip, i);
343 ffs_blkfree(oip, bn, bsize);
344 blocksreleased += btodb(bsize);
345 }
346 if (lastblock < 0)
347 goto done;
348
349 /*
350 * Finally, look for a change in size of the
351 * last direct block; release any frags.
352 */
353 bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_MPNEEDSWAP(ovp->v_mount));
354 if (bn != 0) {
355 long oldspace, newspace;
356
357 /*
358 * Calculate amount of space we're giving
359 * back as old block size minus new block size.
360 */
361 oldspace = blksize(fs, oip, lastblock);
362 oip->i_ffs_size = length;
363 newspace = blksize(fs, oip, lastblock);
364 if (newspace == 0)
365 panic("itrunc: newspace");
366 if (oldspace - newspace > 0) {
367 /*
368 * Block number of space to be free'd is
369 * the old block # plus the number of frags
370 * required for the storage we're keeping.
371 */
372 bn += numfrags(fs, newspace);
373 ffs_blkfree(oip, bn, oldspace - newspace);
374 blocksreleased += btodb(oldspace - newspace);
375 }
376 }
377 done:
378 #ifdef DIAGNOSTIC
379 for (level = SINGLE; level <= TRIPLE; level++)
380 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
381 panic("itrunc1");
382 for (i = 0; i < NDADDR; i++)
383 if (newblks[i] != oip->i_ffs_db[i])
384 panic("itrunc2");
385 if (length == 0 &&
386 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
387 panic("itrunc3");
388 #endif /* DIAGNOSTIC */
389 /*
390 * Put back the real size.
391 */
392 oip->i_ffs_size = length;
393 oip->i_ffs_blocks -= blocksreleased;
394 if (oip->i_ffs_blocks < 0) /* sanity */
395 oip->i_ffs_blocks = 0;
396 oip->i_flag |= IN_CHANGE;
397 #ifdef QUOTA
398 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
399 #endif
400 return (allerror);
401 }
402
403 /*
404 * Release blocks associated with the inode ip and stored in the indirect
405 * block bn. Blocks are free'd in LIFO order up to (but not including)
406 * lastbn. If level is greater than SINGLE, the block is an indirect block
407 * and recursive calls to indirtrunc must be used to cleanse other indirect
408 * blocks.
409 *
410 * NB: triple indirect blocks are untested.
411 */
412 static int
413 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
414 register struct inode *ip;
415 ufs_daddr_t lbn, lastbn;
416 ufs_daddr_t dbn;
417 int level;
418 long *countp;
419 {
420 register int i;
421 struct buf *bp;
422 register struct fs *fs = ip->i_fs;
423 register ufs_daddr_t *bap;
424 struct vnode *vp;
425 ufs_daddr_t *copy = NULL, nb, nlbn, last;
426 long blkcount, factor;
427 int nblocks, blocksreleased = 0;
428 int error = 0, allerror = 0;
429
430 /*
431 * Calculate index in current block of last
432 * block to be kept. -1 indicates the entire
433 * block so we need not calculate the index.
434 */
435 factor = 1;
436 for (i = SINGLE; i < level; i++)
437 factor *= NINDIR(fs);
438 last = lastbn;
439 if (lastbn > 0)
440 last /= factor;
441 nblocks = btodb(fs->fs_bsize);
442 /*
443 * Get buffer of block pointers, zero those entries corresponding
444 * to blocks to be free'd, and update on disk copy first. Since
445 * double(triple) indirect before single(double) indirect, calls
446 * to bmap on these blocks will fail. However, we already have
447 * the on disk address, so we have to set the b_blkno field
448 * explicitly instead of letting bread do everything for us.
449 */
450 vp = ITOV(ip);
451 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
452 if (bp->b_flags & (B_DONE | B_DELWRI)) {
453 /* Braces must be here in case trace evaluates to nothing. */
454 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
455 } else {
456 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
457 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
458 bp->b_flags |= B_READ;
459 if (bp->b_bcount > bp->b_bufsize)
460 panic("ffs_indirtrunc: bad buffer size");
461 bp->b_blkno = dbn;
462 VOP_STRATEGY(bp);
463 error = biowait(bp);
464 }
465 if (error) {
466 brelse(bp);
467 *countp = 0;
468 return (error);
469 }
470
471 bap = (ufs_daddr_t *)bp->b_data;
472 if (lastbn != -1) {
473 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
474 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
475 bzero((caddr_t)&bap[last + 1],
476 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
477 error = bwrite(bp);
478 if (error)
479 allerror = error;
480 bap = copy;
481 }
482
483 /*
484 * Recursively free totally unused blocks.
485 */
486 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
487 i--, nlbn += factor) {
488 nb = ufs_rw32(bap[i], UFS_MPNEEDSWAP(vp->v_mount));
489 if (nb == 0)
490 continue;
491 if (level > SINGLE) {
492 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
493 (ufs_daddr_t)-1, level - 1,
494 &blkcount);
495 if (error)
496 allerror = error;
497 blocksreleased += blkcount;
498 }
499 ffs_blkfree(ip, nb, fs->fs_bsize);
500 blocksreleased += nblocks;
501 }
502
503 /*
504 * Recursively free last partial block.
505 */
506 if (level > SINGLE && lastbn >= 0) {
507 last = lastbn % factor;
508 nb = ufs_rw32(bap[i], UFS_MPNEEDSWAP(vp->v_mount));
509 if (nb != 0) {
510 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
511 last, level - 1, &blkcount);
512 if (error)
513 allerror = error;
514 blocksreleased += blkcount;
515 }
516 }
517
518 if (copy != NULL) {
519 FREE(copy, M_TEMP);
520 } else {
521 bp->b_flags |= B_INVAL;
522 brelse(bp);
523 }
524
525 *countp = blocksreleased;
526 return (allerror);
527 }
528