ffs_inode.c revision 1.5 1 /* $NetBSD: ffs_inode.c,v 1.5 1994/06/29 06:46:32 cgd 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 oszie is 0, length will be at least 1.
198 */
199 if (osize < length) {
200 offset = blkoff(fs, length - 1);
201 lbn = lblkno(fs, length - 1);
202 aflags = B_CLRBUF;
203 if (ap->a_flags & IO_SYNC)
204 aflags |= B_SYNC;
205 if (error = ffs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
206 aflags))
207 return (error);
208 oip->i_size = length;
209 (void) vnode_pager_uncache(ovp);
210 if (aflags & B_SYNC)
211 bwrite(bp);
212 else
213 bawrite(bp);
214 oip->i_flag |= IN_CHANGE | IN_UPDATE;
215 return (VOP_UPDATE(ovp, &tv, &tv, 1));
216 }
217 /*
218 * Shorten the size of the file. If the file is not being
219 * truncated to a block boundry, the contents of the
220 * partial block following the end of the file must be
221 * zero'ed in case it ever become accessable again because
222 * of subsequent file growth.
223 */
224 offset = blkoff(fs, length);
225 if (offset == 0) {
226 oip->i_size = length;
227 } else {
228 lbn = lblkno(fs, length);
229 aflags = B_CLRBUF;
230 if (ap->a_flags & IO_SYNC)
231 aflags |= B_SYNC;
232 if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp,
233 aflags))
234 return (error);
235 oip->i_size = length;
236 size = blksize(fs, oip, lbn);
237 (void) vnode_pager_uncache(ovp);
238 bzero((char *)bp->b_data + offset, (u_int)(size - offset));
239 allocbuf(bp, size);
240 if (aflags & B_SYNC)
241 bwrite(bp);
242 else
243 bawrite(bp);
244 }
245 /*
246 * Calculate index into inode's block list of
247 * last direct and indirect blocks (if any)
248 * which we want to keep. Lastblock is -1 when
249 * the file is truncated to 0.
250 */
251 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
252 lastiblock[SINGLE] = lastblock - NDADDR;
253 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
254 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
255 nblocks = btodb(fs->fs_bsize);
256 /*
257 * Update file and block pointers on disk before we start freeing
258 * blocks. If we crash before free'ing blocks below, the blocks
259 * will be returned to the free list. lastiblock values are also
260 * normalized to -1 for calls to ffs_indirtrunc below.
261 */
262 bcopy((caddr_t)&oip->i_db[0], (caddr_t)oldblks, sizeof oldblks);
263 for (level = TRIPLE; level >= SINGLE; level--)
264 if (lastiblock[level] < 0) {
265 oip->i_ib[level] = 0;
266 lastiblock[level] = -1;
267 }
268 for (i = NDADDR - 1; i > lastblock; i--)
269 oip->i_db[i] = 0;
270 oip->i_flag |= IN_CHANGE | IN_UPDATE;
271 if (error = VOP_UPDATE(ovp, &tv, &tv, MNT_WAIT))
272 allerror = error;
273 /*
274 * Having written the new inode to disk, save its new configuration
275 * and put back the old block pointers long enough to process them.
276 * Note that we save the new block configuration so we can check it
277 * when we are done.
278 */
279 bcopy((caddr_t)&oip->i_db[0], (caddr_t)newblks, sizeof newblks);
280 bcopy((caddr_t)oldblks, (caddr_t)&oip->i_db[0], sizeof oldblks);
281 oip->i_size = osize;
282 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
283 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
284
285 /*
286 * Indirect blocks first.
287 */
288 indir_lbn[SINGLE] = -NDADDR;
289 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
290 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
291 for (level = TRIPLE; level >= SINGLE; level--) {
292 bn = oip->i_ib[level];
293 if (bn != 0) {
294 error = ffs_indirtrunc(oip, indir_lbn[level],
295 fsbtodb(fs, bn), lastiblock[level], level, &count);
296 if (error)
297 allerror = error;
298 blocksreleased += count;
299 if (lastiblock[level] < 0) {
300 oip->i_ib[level] = 0;
301 ffs_blkfree(oip, bn, fs->fs_bsize);
302 blocksreleased += nblocks;
303 }
304 }
305 if (lastiblock[level] >= 0)
306 goto done;
307 }
308
309 /*
310 * All whole direct blocks or frags.
311 */
312 for (i = NDADDR - 1; i > lastblock; i--) {
313 register long bsize;
314
315 bn = oip->i_db[i];
316 if (bn == 0)
317 continue;
318 oip->i_db[i] = 0;
319 bsize = blksize(fs, oip, i);
320 ffs_blkfree(oip, bn, bsize);
321 blocksreleased += btodb(bsize);
322 }
323 if (lastblock < 0)
324 goto done;
325
326 /*
327 * Finally, look for a change in size of the
328 * last direct block; release any frags.
329 */
330 bn = oip->i_db[lastblock];
331 if (bn != 0) {
332 long oldspace, newspace;
333
334 /*
335 * Calculate amount of space we're giving
336 * back as old block size minus new block size.
337 */
338 oldspace = blksize(fs, oip, lastblock);
339 oip->i_size = length;
340 newspace = blksize(fs, oip, lastblock);
341 if (newspace == 0)
342 panic("itrunc: newspace");
343 if (oldspace - newspace > 0) {
344 /*
345 * Block number of space to be free'd is
346 * the old block # plus the number of frags
347 * required for the storage we're keeping.
348 */
349 bn += numfrags(fs, newspace);
350 ffs_blkfree(oip, bn, oldspace - newspace);
351 blocksreleased += btodb(oldspace - newspace);
352 }
353 }
354 done:
355 #ifdef DIAGNOSTIC
356 for (level = SINGLE; level <= TRIPLE; level++)
357 if (newblks[NDADDR + level] != oip->i_ib[level])
358 panic("itrunc1");
359 for (i = 0; i < NDADDR; i++)
360 if (newblks[i] != oip->i_db[i])
361 panic("itrunc2");
362 if (length == 0 &&
363 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
364 panic("itrunc3");
365 #endif /* DIAGNOSTIC */
366 /*
367 * Put back the real size.
368 */
369 oip->i_size = length;
370 oip->i_blocks -= blocksreleased;
371 if (oip->i_blocks < 0) /* sanity */
372 oip->i_blocks = 0;
373 oip->i_flag |= IN_CHANGE;
374 #ifdef QUOTA
375 (void) chkdq(oip, -blocksreleased, NOCRED, 0);
376 #endif
377 return (allerror);
378 }
379
380 /*
381 * Release blocks associated with the inode ip and stored in the indirect
382 * block bn. Blocks are free'd in LIFO order up to (but not including)
383 * lastbn. If level is greater than SINGLE, the block is an indirect block
384 * and recursive calls to indirtrunc must be used to cleanse other indirect
385 * blocks.
386 *
387 * NB: triple indirect blocks are untested.
388 */
389 static int
390 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
391 register struct inode *ip;
392 daddr_t lbn, lastbn;
393 daddr_t dbn;
394 int level;
395 long *countp;
396 {
397 register int i;
398 struct buf *bp;
399 register struct fs *fs = ip->i_fs;
400 register daddr_t *bap;
401 struct vnode *vp;
402 daddr_t *copy, nb, nlbn, last;
403 long blkcount, factor;
404 int nblocks, blocksreleased = 0;
405 int error = 0, allerror = 0;
406
407 /*
408 * Calculate index in current block of last
409 * block to be kept. -1 indicates the entire
410 * block so we need not calculate the index.
411 */
412 factor = 1;
413 for (i = SINGLE; i < level; i++)
414 factor *= NINDIR(fs);
415 last = lastbn;
416 if (lastbn > 0)
417 last /= factor;
418 nblocks = btodb(fs->fs_bsize);
419 /*
420 * Get buffer of block pointers, zero those entries corresponding
421 * to blocks to be free'd, and update on disk copy first. Since
422 * double(triple) indirect before single(double) indirect, calls
423 * to bmap on these blocks will fail. However, we already have
424 * the on disk address, so we have to set the b_blkno field
425 * explicitly instead of letting bread do everything for us.
426 */
427 vp = ITOV(ip);
428 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
429 if (bp->b_flags & (B_DONE | B_DELWRI)) {
430 /* Braces must be here in case trace evaluates to nothing. */
431 trace(TR_BREADHIT, pack(vp, fs->fs_bsize), lbn);
432 } else {
433 trace(TR_BREADMISS, pack(vp, fs->fs_bsize), lbn);
434 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
435 bp->b_flags |= B_READ;
436 if (bp->b_bcount > bp->b_bufsize)
437 panic("ffs_indirtrunc: bad buffer size");
438 bp->b_blkno = dbn;
439 VOP_STRATEGY(bp);
440 error = biowait(bp);
441 }
442 if (error) {
443 brelse(bp);
444 *countp = 0;
445 return (error);
446 }
447
448 bap = (daddr_t *)bp->b_data;
449 MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
450 bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
451 bzero((caddr_t)&bap[last + 1],
452 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
453 if (last == -1)
454 bp->b_flags |= B_INVAL;
455 error = bwrite(bp);
456 if (error)
457 allerror = error;
458 bap = copy;
459
460 /*
461 * Recursively free totally unused blocks.
462 */
463 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
464 i--, nlbn += factor) {
465 nb = bap[i];
466 if (nb == 0)
467 continue;
468 if (level > SINGLE) {
469 if (error = ffs_indirtrunc(ip, nlbn,
470 fsbtodb(fs, nb), (daddr_t)-1, level - 1, &blkcount))
471 allerror = error;
472 blocksreleased += blkcount;
473 }
474 ffs_blkfree(ip, nb, fs->fs_bsize);
475 blocksreleased += nblocks;
476 }
477
478 /*
479 * Recursively free last partial block.
480 */
481 if (level > SINGLE && lastbn >= 0) {
482 last = lastbn % factor;
483 nb = bap[i];
484 if (nb != 0) {
485 if (error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
486 last, level - 1, &blkcount))
487 allerror = error;
488 blocksreleased += blkcount;
489 }
490 }
491 FREE(copy, M_TEMP);
492 *countp = blocksreleased;
493 return (allerror);
494 }
495