ffs_inode.c revision 1.16 1 /* $NetBSD: ffs_inode.c,v 1.16 1998/02/05 08:00:34 mrg 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.8 (Berkeley) 10/19/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mount.h>
41 #include <sys/proc.h>
42 #include <sys/file.h>
43 #include <sys/buf.h>
44 #include <sys/vnode.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/trace.h>
48 #include <sys/resourcevar.h>
49
50 #include <vm/vm.h>
51
52 #if defined(UVM)
53 #include <uvm/uvm_extern.h>
54 #endif
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
61 #include <ufs/ffs/fs.h>
62 #include <ufs/ffs/ffs_extern.h>
63
64 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, daddr_t, int,
65 long *));
66
67 void
68 ffs_init()
69 {
70 ufs_init();
71 }
72
73 /*
74 * Update the access, modified, and inode change times as specified
75 * by the IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.
76 * The IN_MODIFIED flag is used to specify that the inode needs to be
77 * updated but that the times have already been set. The access
78 * and modified times are taken from the second and third parameters;
79 * the inode change time is always taken from the current time. If
80 * waitfor is set, then wait for the disk write of the inode to
81 * complete.
82 */
83
84 int
85 ffs_update(v)
86 void *v;
87 {
88 struct vop_update_args /* {
89 struct vnode *a_vp;
90 struct timespec *a_access;
91 struct timespec *a_modify;
92 int a_waitfor;
93 } */ *ap = v;
94 register struct fs *fs;
95 struct buf *bp;
96 struct inode *ip;
97 int error;
98 struct timespec ts;
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, ap->a_access, ap->a_modify, &ts);
105 if ((ip->i_flag & IN_MODIFIED) == 0)
106 return (0);
107 ip->i_flag &= ~IN_MODIFIED;
108 fs = ip->i_fs;
109 /*
110 * Ensure that uid and gid are correct. This is a temporary
111 * fix until fsck has been changed to do the update.
112 */
113 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
114 ip->i_din.ffs_din.di_ouid = ip->i_ffs_uid; /* XXX */
115 ip->i_din.ffs_din.di_ogid = ip->i_ffs_gid; /* XXX */
116 } /* XXX */
117 error = bread(ip->i_devvp,
118 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
119 (int)fs->fs_bsize, NOCRED, &bp);
120 if (error) {
121 brelse(bp);
122 return (error);
123 }
124 *((struct dinode *)bp->b_data +
125 ino_to_fsbo(fs, ip->i_number)) = ip->i_din.ffs_din;
126 if (ap->a_waitfor)
127 return (bwrite(bp));
128 else {
129 bdwrite(bp);
130 return (0);
131 }
132 }
133
134 #define SINGLE 0 /* index of single indirect block */
135 #define DOUBLE 1 /* index of double indirect block */
136 #define TRIPLE 2 /* index of triple indirect block */
137 /*
138 * Truncate the inode oip to at most length size, freeing the
139 * disk blocks.
140 */
141 int
142 ffs_truncate(v)
143 void *v;
144 {
145 struct vop_truncate_args /* {
146 struct vnode *a_vp;
147 off_t a_length;
148 int a_flags;
149 struct ucred *a_cred;
150 struct proc *a_p;
151 } */ *ap = v;
152 register struct vnode *ovp = ap->a_vp;
153 register daddr_t lastblock;
154 register struct inode *oip;
155 daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
156 daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
157 off_t length = ap->a_length;
158 register struct fs *fs;
159 struct buf *bp;
160 int offset, size, level;
161 long count, nblocks, vflags, blocksreleased = 0;
162 struct timespec ts;
163 register int i;
164 int aflags, error, allerror;
165 off_t osize;
166
167 if (length < 0)
168 return (EINVAL);
169 oip = VTOI(ovp);
170 TIMEVAL_TO_TIMESPEC(&time, &ts);
171 if (ovp->v_type == VLNK &&
172 (oip->i_ffs_size < ovp->v_mount->mnt_maxsymlinklen ||
173 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
174 oip->i_din.ffs_din.di_blocks == 0))) {
175 #ifdef DIAGNOSTIC
176 if (length != 0)
177 panic("ffs_truncate: partial truncate of symlink");
178 #endif
179 bzero((char *)&oip->i_ffs_shortlink, (u_int)oip->i_ffs_size);
180 oip->i_ffs_size = 0;
181 oip->i_flag |= IN_CHANGE | IN_UPDATE;
182 return (VOP_UPDATE(ovp, &ts, &ts, 1));
183 }
184 if (oip->i_ffs_size == length) {
185 oip->i_flag |= IN_CHANGE | IN_UPDATE;
186 return (VOP_UPDATE(ovp, &ts, &ts, 0));
187 }
188 #ifdef QUOTA
189 if ((error = getinoquota(oip)) != 0)
190 return (error);
191 #endif
192 #if defined(UVM)
193 uvm_vnp_setsize(ovp, length);
194 #else
195 vnode_pager_setsize(ovp, length);
196 #endif
197 fs = oip->i_fs;
198 osize = oip->i_ffs_size;
199 /*
200 * Lengthen the size of the file. We must ensure that the
201 * last byte of the file is allocated. Since the smallest
202 * value of osize is 0, length will be at least 1.
203 */
204 if (osize < length) {
205 if (length > fs->fs_maxfilesize)
206 return (EFBIG);
207 offset = blkoff(fs, length - 1);
208 lbn = lblkno(fs, length - 1);
209 aflags = B_CLRBUF;
210 if (ap->a_flags & IO_SYNC)
211 aflags |= B_SYNC;
212 error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
213 aflags);
214 if (error)
215 return (error);
216 oip->i_ffs_size = length;
217 #if defined(UVM)
218 (void) uvm_vnp_uncache(ovp);
219 #else
220 (void) vnode_pager_uncache(ovp);
221 #endif
222 if (aflags & B_SYNC)
223 bwrite(bp);
224 else
225 bawrite(bp);
226 oip->i_flag |= IN_CHANGE | IN_UPDATE;
227 return (VOP_UPDATE(ovp, &ts, &ts, 1));
228 }
229 /*
230 * Shorten the size of the file. If the file is not being
231 * truncated to a block boundry, the contents of the
232 * partial block following the end of the file must be
233 * zero'ed in case it ever become accessable again because
234 * of subsequent file growth.
235 */
236 offset = blkoff(fs, length);
237 if (offset == 0) {
238 oip->i_ffs_size = length;
239 } else {
240 lbn = lblkno(fs, length);
241 aflags = B_CLRBUF;
242 if (ap->a_flags & IO_SYNC)
243 aflags |= B_SYNC;
244 error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags);
245 if (error)
246 return (error);
247 oip->i_ffs_size = length;
248 size = blksize(fs, oip, lbn);
249 #if defined(UVM)
250 (void) uvm_vnp_uncache(ovp);
251 #else
252 (void) vnode_pager_uncache(ovp);
253 #endif
254 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
255 allocbuf(bp, size);
256 if (aflags & B_SYNC)
257 bwrite(bp);
258 else
259 bawrite(bp);
260 }
261 /*
262 * Calculate index into inode's block list of
263 * last direct and indirect blocks (if any)
264 * which we want to keep. Lastblock is -1 when
265 * the file is truncated to 0.
266 */
267 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
268 lastiblock[SINGLE] = lastblock - NDADDR;
269 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
270 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
271 nblocks = btodb(fs->fs_bsize);
272 /*
273 * Update file and block pointers on disk before we start freeing
274 * blocks. If we crash before free'ing blocks below, the blocks
275 * will be returned to the free list. lastiblock values are also
276 * normalized to -1 for calls to ffs_indirtrunc below.
277 */
278 bcopy((caddr_t)&oip->i_ffs_db[0], (caddr_t)oldblks, sizeof oldblks);
279 for (level = TRIPLE; level >= SINGLE; level--)
280 if (lastiblock[level] < 0) {
281 oip->i_ffs_ib[level] = 0;
282 lastiblock[level] = -1;
283 }
284 for (i = NDADDR - 1; i > lastblock; i--)
285 oip->i_ffs_db[i] = 0;
286 oip->i_flag |= IN_CHANGE | IN_UPDATE;
287 if ((error = VOP_UPDATE(ovp, &ts, &ts, 1)) != 0)
288 allerror = error;
289 /*
290 * Having written the new inode to disk, save its new configuration
291 * and put back the old block pointers long enough to process them.
292 * Note that we save the new block configuration so we can check it
293 * when we are done.
294 */
295 bcopy((caddr_t)&oip->i_ffs_db[0], (caddr_t)newblks, sizeof newblks);
296 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_ffs_db[0], sizeof oldblks);
297 oip->i_ffs_size = osize;
298 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
299 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
300
301 /*
302 * Indirect blocks first.
303 */
304 indir_lbn[SINGLE] = -NDADDR;
305 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
306 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
307 for (level = TRIPLE; level >= SINGLE; level--) {
308 bn = oip->i_ffs_ib[level];
309 if (bn != 0) {
310 error = ffs_indirtrunc(oip, indir_lbn[level],
311 fsbtodb(fs, bn), lastiblock[level], level, &count);
312 if (error)
313 allerror = error;
314 blocksreleased += count;
315 if (lastiblock[level] < 0) {
316 oip->i_ffs_ib[level] = 0;
317 ffs_blkfree(oip, bn, fs->fs_bsize);
318 blocksreleased += nblocks;
319 }
320 }
321 if (lastiblock[level] >= 0)
322 goto done;
323 }
324
325 /*
326 * All whole direct blocks or frags.
327 */
328 for (i = NDADDR - 1; i > lastblock; i--) {
329 register long bsize;
330
331 bn = oip->i_ffs_db[i];
332 if (bn == 0)
333 continue;
334 oip->i_ffs_db[i] = 0;
335 bsize = blksize(fs, oip, i);
336 ffs_blkfree(oip, bn, bsize);
337 blocksreleased += btodb(bsize);
338 }
339 if (lastblock < 0)
340 goto done;
341
342 /*
343 * Finally, look for a change in size of the
344 * last direct block; release any frags.
345 */
346 bn = oip->i_ffs_db[lastblock];
347 if (bn != 0) {
348 long oldspace, newspace;
349
350 /*
351 * Calculate amount of space we're giving
352 * back as old block size minus new block size.
353 */
354 oldspace = blksize(fs, oip, lastblock);
355 oip->i_ffs_size = length;
356 newspace = blksize(fs, oip, lastblock);
357 if (newspace == 0)
358 panic("itrunc: newspace");
359 if (oldspace - newspace > 0) {
360 /*
361 * Block number of space to be free'd is
362 * the old block # plus the number of frags
363 * required for the storage we're keeping.
364 */
365 bn += numfrags(fs, newspace);
366 ffs_blkfree(oip, bn, oldspace - newspace);
367 blocksreleased += btodb(oldspace - newspace);
368 }
369 }
370 done:
371 #ifdef DIAGNOSTIC
372 for (level = SINGLE; level <= TRIPLE; level++)
373 if (newblks[NDADDR + level] != oip->i_ffs_ib[level])
374 panic("itrunc1");
375 for (i = 0; i < NDADDR; i++)
376 if (newblks[i] != oip->i_ffs_db[i])
377 panic("itrunc2");
378 if (length == 0 &&
379 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
380 panic("itrunc3");
381 #endif /* DIAGNOSTIC */
382 /*
383 * Put back the real size.
384 */
385 oip->i_ffs_size = length;
386 oip->i_ffs_blocks -= blocksreleased;
387 if (oip->i_ffs_blocks < 0) /* sanity */
388 oip->i_ffs_blocks = 0;
389 oip->i_flag |= IN_CHANGE;
390 #ifdef QUOTA
391 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
392 #endif
393 return (allerror);
394 }
395
396 /*
397 * Release blocks associated with the inode ip and stored in the indirect
398 * block bn. Blocks are free'd in LIFO order up to (but not including)
399 * lastbn. If level is greater than SINGLE, the block is an indirect block
400 * and recursive calls to indirtrunc must be used to cleanse other indirect
401 * blocks.
402 *
403 * NB: triple indirect blocks are untested.
404 */
405 static int
406 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
407 register struct inode *ip;
408 daddr_t lbn, lastbn;
409 daddr_t dbn;
410 int level;
411 long *countp;
412 {
413 register int i;
414 struct buf *bp;
415 register struct fs *fs = ip->i_fs;
416 register daddr_t *bap;
417 struct vnode *vp;
418 daddr_t *copy = NULL, nb, nlbn, last;
419 long blkcount, factor;
420 int nblocks, blocksreleased = 0;
421 int error = 0, allerror = 0;
422
423 /*
424 * Calculate index in current block of last
425 * block to be kept. -1 indicates the entire
426 * block so we need not calculate the index.
427 */
428 factor = 1;
429 for (i = SINGLE; i < level; i++)
430 factor *= NINDIR(fs);
431 last = lastbn;
432 if (lastbn > 0)
433 last /= factor;
434 nblocks = btodb(fs->fs_bsize);
435 /*
436 * Get buffer of block pointers, zero those entries corresponding
437 * to blocks to be free'd, and update on disk copy first. Since
438 * double(triple) indirect before single(double) indirect, calls
439 * to bmap on these blocks will fail. However, we already have
440 * the on disk address, so we have to set the b_blkno field
441 * explicitly instead of letting bread do everything for us.
442 */
443 vp = ITOV(ip);
444 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
445 if (bp->b_flags & (B_DONE | B_DELWRI)) {
446 /* Braces must be here in case trace evaluates to nothing. */
447 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
448 } else {
449 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
450 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
451 bp->b_flags |= B_READ;
452 if (bp->b_bcount > bp->b_bufsize)
453 panic("ffs_indirtrunc: bad buffer size");
454 bp->b_blkno = dbn;
455 VOP_STRATEGY(bp);
456 error = biowait(bp);
457 }
458 if (error) {
459 brelse(bp);
460 *countp = 0;
461 return (error);
462 }
463
464 bap = (daddr_t *)bp->b_data;
465 if (lastbn != -1) {
466 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
467 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
468 bzero((caddr_t)&bap[last + 1],
469 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
470 error = bwrite(bp);
471 if (error)
472 allerror = error;
473 bap = copy;
474 }
475
476 /*
477 * Recursively free totally unused blocks.
478 */
479 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
480 i--, nlbn += factor) {
481 nb = bap[i];
482 if (nb == 0)
483 continue;
484 if (level > SINGLE) {
485 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
486 (daddr_t)-1, level - 1,
487 &blkcount);
488 if (error)
489 allerror = error;
490 blocksreleased += blkcount;
491 }
492 ffs_blkfree(ip, nb, fs->fs_bsize);
493 blocksreleased += nblocks;
494 }
495
496 /*
497 * Recursively free last partial block.
498 */
499 if (level > SINGLE && lastbn >= 0) {
500 last = lastbn % factor;
501 nb = bap[i];
502 if (nb != 0) {
503 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
504 last, level - 1, &blkcount);
505 if (error)
506 allerror = error;
507 blocksreleased += blkcount;
508 }
509 }
510
511 if (copy != NULL) {
512 FREE(copy, M_TEMP);
513 } else {
514 bp->b_flags |= B_INVAL;
515 brelse(bp);
516 }
517
518 *countp = blocksreleased;
519 return (allerror);
520 }
521