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