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