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