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