ffs_inode.c revision 1.46 1 /* $NetBSD: ffs_inode.c,v 1.46 2001/10/30 01:11:53 lukem 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 <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: ffs_inode.c,v 1.46 2001/10/30 01:11:53 lukem Exp $");
40
41 #if defined(_KERNEL_OPT)
42 #include "opt_ffs.h"
43 #include "opt_quota.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mount.h>
49 #include <sys/proc.h>
50 #include <sys/file.h>
51 #include <sys/buf.h>
52 #include <sys/vnode.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/trace.h>
56 #include <sys/resourcevar.h>
57
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/ufs_extern.h>
62 #include <ufs/ufs/ufs_bswap.h>
63
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
66
67 static int ffs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
68 ufs_daddr_t, int, long *));
69
70 /*
71 * Update the access, modified, and inode change times as specified
72 * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
73 * The IN_MODIFIED flag is used to specify that the inode needs to be
74 * updated but that the times have already been set. The access
75 * and modified times are taken from the second and third parameters;
76 * the inode change time is always taken from the current time. If
77 * UPDATE_WAIT flag is set, or UPDATE_DIROP is set and we are not doing
78 * softupdates, then wait for the disk write of the inode to complete.
79 */
80
81 int
82 ffs_update(v)
83 void *v;
84 {
85 struct vop_update_args /* {
86 struct vnode *a_vp;
87 struct timespec *a_access;
88 struct timespec *a_modify;
89 int a_flags;
90 } */ *ap = v;
91 struct fs *fs;
92 struct buf *bp;
93 struct inode *ip;
94 int error;
95 struct timespec ts;
96 caddr_t cp;
97 int waitfor, flags;
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 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
107 if (flags == 0)
108 return (0);
109 fs = ip->i_fs;
110
111 if ((flags & IN_MODIFIED) != 0 &&
112 (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0) {
113 waitfor = ap->a_flags & UPDATE_WAIT;
114 if ((ap->a_flags & UPDATE_DIROP) && !DOINGSOFTDEP(ap->a_vp))
115 waitfor |= UPDATE_WAIT;
116 } else
117 waitfor = 0;
118
119 /*
120 * Ensure that uid and gid are correct. This is a temporary
121 * fix until fsck has been changed to do the update.
122 */
123 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
124 ip->i_din.ffs_din.di_ouid = ip->i_ffs_uid; /* XXX */
125 ip->i_din.ffs_din.di_ogid = ip->i_ffs_gid; /* XXX */
126 } /* XXX */
127 error = bread(ip->i_devvp,
128 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
129 (int)fs->fs_bsize, NOCRED, &bp);
130 if (error) {
131 brelse(bp);
132 return (error);
133 }
134 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
135 if (DOINGSOFTDEP(ap->a_vp))
136 softdep_update_inodeblock(ip, bp, waitfor);
137 else if (ip->i_ffs_effnlink != ip->i_ffs_nlink)
138 panic("ffs_update: bad link cnt");
139 cp = (caddr_t)bp->b_data +
140 (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
141 #ifdef FFS_EI
142 if (UFS_FSNEEDSWAP(fs))
143 ffs_dinode_swap(&ip->i_din.ffs_din, (struct dinode *)cp);
144 else
145 #endif
146 memcpy(cp, &ip->i_din.ffs_din, DINODE_SIZE);
147 if (waitfor) {
148 return (bwrite(bp));
149 } else {
150 bdwrite(bp);
151 return (0);
152 }
153 }
154
155 #define SINGLE 0 /* index of single indirect block */
156 #define DOUBLE 1 /* index of double indirect block */
157 #define TRIPLE 2 /* index of triple indirect block */
158 /*
159 * Truncate the inode oip to at most length size, freeing the
160 * disk blocks.
161 */
162 int
163 ffs_truncate(v)
164 void *v;
165 {
166 struct vop_truncate_args /* {
167 struct vnode *a_vp;
168 off_t a_length;
169 int a_flags;
170 struct ucred *a_cred;
171 struct proc *a_p;
172 } */ *ap = v;
173 struct vnode *ovp = ap->a_vp;
174 struct genfs_node *gp = VTOG(ovp);
175 ufs_daddr_t lastblock;
176 struct inode *oip;
177 ufs_daddr_t bn, 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 int offset, size, level;
182 long count, nblocks, blocksreleased = 0;
183 int i;
184 int error, allerror = 0;
185 off_t osize;
186
187 if (length < 0)
188 return (EINVAL);
189 oip = VTOI(ovp);
190 if (ovp->v_type == VLNK &&
191 (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
192 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
193 oip->i_din.ffs_din.di_blocks == 0))) {
194 KDASSERT(length == 0);
195 memset(&oip->i_ffs_shortlink, 0, (size_t)oip->i_ffs_size);
196 oip->i_ffs_size = 0;
197 oip->i_flag |= IN_CHANGE | IN_UPDATE;
198 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
199 }
200 if (oip->i_ffs_size == length) {
201 oip->i_flag |= IN_CHANGE | IN_UPDATE;
202 return (VOP_UPDATE(ovp, NULL, NULL, 0));
203 }
204 #ifdef QUOTA
205 if ((error = getinoquota(oip)) != 0)
206 return (error);
207 #endif
208 fs = oip->i_fs;
209 if (length > fs->fs_maxfilesize)
210 return (EFBIG);
211
212 osize = oip->i_ffs_size;
213
214 /*
215 * Lengthen the size of the file. We must ensure that the
216 * last byte of the file is allocated. Since the smallest
217 * value of osize is 0, length will be at least 1.
218 */
219
220 if (osize < length) {
221 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
222 ap->a_flags & IO_SYNC ? B_SYNC : 0);
223 if (error) {
224 return error;
225 }
226 uvm_vnp_setsize(ovp, length);
227 oip->i_flag |= IN_CHANGE | IN_UPDATE;
228 KASSERT(ovp->v_size == oip->i_ffs_size);
229 return (VOP_UPDATE(ovp, NULL, NULL, 1));
230 }
231
232 /*
233 * When truncating a regular file down to a non-block-aligned size,
234 * we must zero the part of last block which is past the new EOF.
235 * We must synchronously flush the zeroed pages to disk
236 * since the new pages will be invalidated as soon as we
237 * inform the VM system of the new, smaller size.
238 * We must to this before acquiring the GLOCK, since fetching
239 * the pages will acquire the GLOCK internally.
240 * So there is a window where another thread could see a whole
241 * zeroed page past EOF, but that's life.
242 */
243
244 offset = blkoff(fs, length);
245 if (ovp->v_type == VREG && length < osize && offset != 0) {
246 struct uvm_object *uobj;
247 voff_t eoz;
248
249 size = blksize(fs, oip, lblkno(fs, length));
250 eoz = MIN(lblktosize(fs, lblkno(fs, length)) + size, osize);
251 uvm_vnp_zerorange(ovp, length, eoz - length);
252 uobj = &ovp->v_uobj;
253 simple_lock(&uobj->vmobjlock);
254 error = (uobj->pgops->pgo_put)(uobj, trunc_page(length),
255 round_page(eoz), PGO_CLEANIT|PGO_DEACTIVATE|PGO_SYNCIO);
256 if (error) {
257 return error;
258 }
259 }
260
261 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
262
263 if (DOINGSOFTDEP(ovp)) {
264 if (length > 0) {
265 /*
266 * If a file is only partially truncated, then
267 * we have to clean up the data structures
268 * describing the allocation past the truncation
269 * point. Finding and deallocating those structures
270 * is a lot of work. Since partial truncation occurs
271 * rarely, we solve the problem by syncing the file
272 * so that it will have no data structures left.
273 */
274 if ((error = VOP_FSYNC(ovp, ap->a_cred, FSYNC_WAIT,
275 0, 0, ap->a_p)) != 0) {
276 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
277 return (error);
278 }
279 } else {
280 uvm_vnp_setsize(ovp, length);
281 #ifdef QUOTA
282 (void) chkdq(oip, -oip->i_ffs_blocks, NOCRED, 0);
283 #endif
284 softdep_setup_freeblocks(oip, length);
285 (void) vinvalbuf(ovp, 0, ap->a_cred, ap->a_p, 0, 0);
286 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
287 oip->i_flag |= IN_CHANGE | IN_UPDATE;
288 return (VOP_UPDATE(ovp, NULL, NULL, 0));
289 }
290 }
291
292 /*
293 * Reduce the size of the file.
294 */
295 oip->i_ffs_size = length;
296 uvm_vnp_setsize(ovp, length);
297 /*
298 * Calculate index into inode's block list of
299 * last direct and indirect blocks (if any)
300 * which we want to keep. Lastblock is -1 when
301 * the file is truncated to 0.
302 */
303 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
304 lastiblock[SINGLE] = lastblock - NDADDR;
305 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
306 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
307 nblocks = btodb(fs->fs_bsize);
308 /*
309 * Update file and block pointers on disk before we start freeing
310 * blocks. If we crash before free'ing blocks below, the blocks
311 * will be returned to the free list. lastiblock values are also
312 * normalized to -1 for calls to ffs_indirtrunc below.
313 */
314 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
315 for (level = TRIPLE; level >= SINGLE; level--)
316 if (lastiblock[level] < 0) {
317 oip->i_ffs_ib[level] = 0;
318 lastiblock[level] = -1;
319 }
320 for (i = NDADDR - 1; i > lastblock; i--)
321 oip->i_ffs_db[i] = 0;
322 oip->i_flag |= IN_CHANGE | IN_UPDATE;
323 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
324 if (error && !allerror)
325 allerror = error;
326
327 /*
328 * Having written the new inode to disk, save its new configuration
329 * and put back the old block pointers long enough to process them.
330 * Note that we save the new block configuration so we can check it
331 * when we are done.
332 */
333 memcpy((caddr_t)newblks, (caddr_t)&oip->i_ffs_db[0], sizeof newblks);
334 memcpy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
335 oip->i_ffs_size = osize;
336 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
337 if (error && !allerror)
338 allerror = error;
339
340 /*
341 * Indirect blocks first.
342 */
343 indir_lbn[SINGLE] = -NDADDR;
344 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
345 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
346 for (level = TRIPLE; level >= SINGLE; level--) {
347 bn = ufs_rw32(oip->i_ffs_ib[level], UFS_FSNEEDSWAP(fs));
348 if (bn != 0) {
349 error = ffs_indirtrunc(oip, indir_lbn[level],
350 fsbtodb(fs, bn), lastiblock[level], level, &count);
351 if (error)
352 allerror = error;
353 blocksreleased += count;
354 if (lastiblock[level] < 0) {
355 oip->i_ffs_ib[level] = 0;
356 ffs_blkfree(oip, bn, fs->fs_bsize);
357 blocksreleased += nblocks;
358 }
359 }
360 if (lastiblock[level] >= 0)
361 goto done;
362 }
363
364 /*
365 * All whole direct blocks or frags.
366 */
367 for (i = NDADDR - 1; i > lastblock; i--) {
368 long bsize;
369
370 bn = ufs_rw32(oip->i_ffs_db[i], UFS_FSNEEDSWAP(fs));
371 if (bn == 0)
372 continue;
373 oip->i_ffs_db[i] = 0;
374 bsize = blksize(fs, oip, i);
375 ffs_blkfree(oip, bn, bsize);
376 blocksreleased += btodb(bsize);
377 }
378 if (lastblock < 0)
379 goto done;
380
381 /*
382 * Finally, look for a change in size of the
383 * last direct block; release any frags.
384 */
385 bn = ufs_rw32(oip->i_ffs_db[lastblock], UFS_FSNEEDSWAP(fs));
386 if (bn != 0) {
387 long oldspace, newspace;
388
389 /*
390 * Calculate amount of space we're giving
391 * back as old block size minus new block size.
392 */
393 oldspace = blksize(fs, oip, lastblock);
394 oip->i_ffs_size = length;
395 newspace = blksize(fs, oip, lastblock);
396 if (newspace == 0)
397 panic("itrunc: newspace");
398 if (oldspace - newspace > 0) {
399 /*
400 * Block number of space to be free'd is
401 * the old block # plus the number of frags
402 * required for the storage we're keeping.
403 */
404 bn += numfrags(fs, newspace);
405 ffs_blkfree(oip, bn, oldspace - newspace);
406 blocksreleased += btodb(oldspace - newspace);
407 }
408 }
409
410 done:
411 #ifdef DIAGNOSTIC
412 for (level = SINGLE; level <= TRIPLE; level++)
413 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
414 panic("itrunc1");
415 for (i = 0; i < NDADDR; i++)
416 if (newblks[i] != oip->i_ffs_db[i])
417 panic("itrunc2");
418 if (length == 0 &&
419 (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
420 panic("itrunc3");
421 #endif /* DIAGNOSTIC */
422 /*
423 * Put back the real size.
424 */
425 oip->i_ffs_size = length;
426 oip->i_ffs_blocks -= blocksreleased;
427 if (oip->i_ffs_blocks < 0) /* sanity */
428 oip->i_ffs_blocks = 0;
429 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
430 oip->i_flag |= IN_CHANGE;
431 #ifdef QUOTA
432 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
433 #endif
434 KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_ffs_size);
435 return (allerror);
436 }
437
438 /*
439 * Release blocks associated with the inode ip and stored in the indirect
440 * block bn. Blocks are free'd in LIFO order up to (but not including)
441 * lastbn. If level is greater than SINGLE, the block is an indirect block
442 * and recursive calls to indirtrunc must be used to cleanse other indirect
443 * blocks.
444 *
445 * NB: triple indirect blocks are untested.
446 */
447 static int
448 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
449 struct inode *ip;
450 ufs_daddr_t lbn, lastbn;
451 ufs_daddr_t dbn;
452 int level;
453 long *countp;
454 {
455 int i;
456 struct buf *bp;
457 struct fs *fs = ip->i_fs;
458 ufs_daddr_t *bap;
459 struct vnode *vp;
460 ufs_daddr_t *copy = NULL, nb, nlbn, last;
461 long blkcount, factor;
462 int nblocks, blocksreleased = 0;
463 int error = 0, allerror = 0;
464
465 /*
466 * Calculate index in current block of last
467 * block to be kept. -1 indicates the entire
468 * block so we need not calculate the index.
469 */
470 factor = 1;
471 for (i = SINGLE; i < level; i++)
472 factor *= NINDIR(fs);
473 last = lastbn;
474 if (lastbn > 0)
475 last /= factor;
476 nblocks = btodb(fs->fs_bsize);
477 /*
478 * Get buffer of block pointers, zero those entries corresponding
479 * to blocks to be free'd, and update on disk copy first. Since
480 * double(triple) indirect before single(double) indirect, calls
481 * to bmap on these blocks will fail. However, we already have
482 * the on disk address, so we have to set the b_blkno field
483 * explicitly instead of letting bread do everything for us.
484 */
485 vp = ITOV(ip);
486 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
487 if (bp->b_flags & (B_DONE | B_DELWRI)) {
488 /* Braces must be here in case trace evaluates to nothing. */
489 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
490 } else {
491 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
492 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
493 bp->b_flags |= B_READ;
494 if (bp->b_bcount > bp->b_bufsize)
495 panic("ffs_indirtrunc: bad buffer size");
496 bp->b_blkno = dbn;
497 VOP_STRATEGY(bp);
498 error = biowait(bp);
499 }
500 if (error) {
501 brelse(bp);
502 *countp = 0;
503 return (error);
504 }
505
506 bap = (ufs_daddr_t *)bp->b_data;
507 if (lastbn >= 0) {
508 copy = (ufs_daddr_t *) malloc(fs->fs_bsize, M_TEMP, M_WAITOK);
509 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->fs_bsize);
510 memset((caddr_t)&bap[last + 1], 0,
511 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (ufs_daddr_t));
512 error = bwrite(bp);
513 if (error)
514 allerror = error;
515 bap = copy;
516 }
517
518 /*
519 * Recursively free totally unused blocks.
520 */
521 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
522 i--, nlbn += factor) {
523 nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
524 if (nb == 0)
525 continue;
526 if (level > SINGLE) {
527 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
528 (ufs_daddr_t)-1, level - 1,
529 &blkcount);
530 if (error)
531 allerror = error;
532 blocksreleased += blkcount;
533 }
534 ffs_blkfree(ip, nb, fs->fs_bsize);
535 blocksreleased += nblocks;
536 }
537
538 /*
539 * Recursively free last partial block.
540 */
541 if (level > SINGLE && lastbn >= 0) {
542 last = lastbn % factor;
543 nb = ufs_rw32(bap[i], UFS_FSNEEDSWAP(fs));
544 if (nb != 0) {
545 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
546 last, level - 1, &blkcount);
547 if (error)
548 allerror = error;
549 blocksreleased += blkcount;
550 }
551 }
552
553 if (copy != NULL) {
554 FREE(copy, M_TEMP);
555 } else {
556 bp->b_flags |= B_INVAL;
557 brelse(bp);
558 }
559
560 *countp = blocksreleased;
561 return (allerror);
562 }
563