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