ffs_inode.c revision 1.31 1 /* $NetBSD: ffs_inode.c,v 1.31 2000/05/13 23:43:13 perseant 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, vflags, blocksreleased = 0;
180 int i;
181 int aflags, error, allerror;
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 if ((error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT)) != 0)
329 allerror = error;
330 /*
331 * Having written the new inode to disk, save its new configuration
332 * and put back the old block pointers long enough to process them.
333 * Note that we save the new block configuration so we can check it
334 * when we are done.
335 */
336 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
337 memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
338 oip->i_ffs_size = osize;
339 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
340 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
341
342 /*
343 * Indirect blocks first.
344 */
345 indir_lbn[SINGLE] = -NDADDR;
346 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
347 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
348 for (level = TRIPLE; level >= SINGLE; level--) {
349 bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
350 if (bn != 0) {
351 error = ffs_indirtrunc(oip, indir_lbn[level],
352 fsbtodb(fs, bn), lastiblock[level], level, &count);
353 if (error)
354 allerror = error;
355 blocksreleased += count;
356 if (lastiblock[level] < 0) {
357 oip->i_ffs_ib[level] = 0;
358 ffs_blkfree(oip, bn, fs->fs_bsize);
359 blocksreleased += nblocks;
360 }
361 }
362 if (lastiblock[level] >= 0)
363 goto done;
364 }
365
366 /*
367 * All whole direct blocks or frags.
368 */
369 for (i = NDADDR - 1; i > lastblock; i--) {
370 long bsize;
371
372 bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
373 if (bn == 0)
374 continue;
375 oip->i_ffs_db[i] = 0;
376 bsize = blksize(fs, oip, i);
377 ffs_blkfree(oip, bn, bsize);
378 blocksreleased += btodb(bsize);
379 }
380 if (lastblock < 0)
381 goto done;
382
383 /*
384 * Finally, look for a change in size of the
385 * last direct block; release any frags.
386 */
387 bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
388 if (bn != 0) {
389 long oldspace, newspace;
390
391 /*
392 * Calculate amount of space we're giving
393 * back as old block size minus new block size.
394 */
395 oldspace = blksize(fs, oip, lastblock);
396 oip->i_ffs_size = length;
397 newspace = blksize(fs, oip, lastblock);
398 if (newspace == 0)
399 panic("itrunc: newspace");
400 if (oldspace - newspace > 0) {
401 /*
402 * Block number of space to be free'd is
403 * the old block # plus the number of frags
404 * required for the storage we're keeping.
405 */
406 bn += numfrags(fs, newspace);
407 ffs_blkfree(oip, bn, oldspace - newspace);
408 blocksreleased += btodb(oldspace - newspace);
409 }
410 }
411 done:
412 #ifdef DIAGNOSTIC
413 for (level = SINGLE; level <= TRIPLE; level++)
414 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
415 panic("itrunc1");
416 for (i = 0; i < NDADDR; i++)
417 if (newblks[i] != oip->i_ffs_db[i])
418 panic("itrunc2");
419 if (length == 0 &&
420 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
421 panic("itrunc3");
422 #endif /* DIAGNOSTIC */
423 /*
424 * Put back the real size.
425 */
426 oip->i_ffs_size = length;
427 oip->i_ffs_blocks -= blocksreleased;
428 if (oip->i_ffs_blocks < 0) /* sanity */
429 oip->i_ffs_blocks = 0;
430 oip->i_flag |= IN_CHANGE;
431 #ifdef QUOTA
432 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
433 #endif
434 return (allerror);
435 }
436
437 /*
438 * Release blocks associated with the inode ip and stored in the indirect
439 * block bn. Blocks are free'd in LIFO order up to (but not including)
440 * lastbn. If level is greater than SINGLE, the block is an indirect block
441 * and recursive calls to indirtrunc must be used to cleanse other indirect
442 * blocks.
443 *
444 * NB: triple indirect blocks are untested.
445 */
446 static int
447 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
448 struct inode *ip;
449 ufs_daddr_t lbn, lastbn;
450 ufs_daddr_t dbn;
451 int level;
452 long *countp;
453 {
454 int i;
455 struct buf *bp;
456 struct fs *fs = ip->i_fs;
457 ufs_daddr_t *bap;
458 struct vnode *vp;
459 ufs_daddr_t *copy = NULL, nb, nlbn, last;
460 long blkcount, factor;
461 int nblocks, blocksreleased = 0;
462 int error = 0, allerror = 0;
463
464 /*
465 * Calculate index in current block of last
466 * block to be kept. -1 indicates the entire
467 * block so we need not calculate the index.
468 */
469 factor = 1;
470 for (i = SINGLE; i < level; i++)
471 factor *= NINDIR(fs);
472 last = lastbn;
473 if (lastbn > 0)
474 last /= factor;
475 nblocks = btodb(fs->fs_bsize);
476 /*
477 * Get buffer of block pointers, zero those entries corresponding
478 * to blocks to be free'd, and update on disk copy first. Since
479 * double(triple) indirect before single(double) indirect, calls
480 * to bmap on these blocks will fail. However, we already have
481 * the on disk address, so we have to set the b_blkno field
482 * explicitly instead of letting bread do everything for us.
483 */
484 vp = ITOV(ip);
485 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
486 if (bp->b_flags & (B_DONE | B_DELWRI)) {
487 /* Braces must be here in case trace evaluates to nothing. */
488 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
489 } else {
490 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
491 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
492 bp->b_flags |= B_READ;
493 if (bp->b_bcount > bp->b_bufsize)
494 panic("ffs_indirtrunc: bad buffer size");
495 bp->b_blkno = dbn;
496 VOP_STRATEGY(bp);
497 error = biowait(bp);
498 }
499 if (error) {
500 brelse(bp);
501 *countp = 0;
502 return (error);
503 }
504
505 bap = (ufs_daddr_t *)bp->b_data;
506 if (lastbn != -1) {
507 MALLOC(copy, ufs_daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
508 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
509 memset((caddr_t)&bap[last + 1], 0,
510 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
511 error = bwrite(bp);
512 if (error)
513 allerror = error;
514 bap = copy;
515 }
516
517 /*
518 * Recursively free totally unused blocks.
519 */
520 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
521 i--, nlbn += factor) {
522 nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
523 if (nb == 0)
524 continue;
525 if (level > SINGLE) {
526 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
527 (ufs_daddr_t)-1, level - 1,
528 &blkcount);
529 if (error)
530 allerror = error;
531 blocksreleased += blkcount;
532 }
533 ffs_blkfree(ip, nb, fs->fs_bsize);
534 blocksreleased += nblocks;
535 }
536
537 /*
538 * Recursively free last partial block.
539 */
540 if (level > SINGLE && lastbn >= 0) {
541 last = lastbn % factor;
542 nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
543 if (nb != 0) {
544 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
545 last, level - 1, &blkcount);
546 if (error)
547 allerror = error;
548 blocksreleased += blkcount;
549 }
550 }
551
552 if (copy != NULL) {
553 FREE(copy, M_TEMP);
554 } else {
555 bp->b_flags |= B_INVAL;
556 brelse(bp);
557 }
558
559 *countp = blocksreleased;
560 return (allerror);
561 }
562