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