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