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