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