ext2fs_inode.c revision 1.41 1 /* $NetBSD: ext2fs_inode.c,v 1.41 2004/08/14 01:08:02 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94
32 * Modified for ext2fs by Manuel Bouyer.
33 */
34
35 /*
36 * Copyright (c) 1997 Manuel Bouyer.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by Manuel Bouyer.
49 * 4. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *
63 * @(#)ffs_inode.c 8.8 (Berkeley) 10/19/94
64 * Modified for ext2fs by Manuel Bouyer.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_inode.c,v 1.41 2004/08/14 01:08:02 mycroft Exp $");
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/mount.h>
73 #include <sys/proc.h>
74 #include <sys/file.h>
75 #include <sys/buf.h>
76 #include <sys/vnode.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/trace.h>
80 #include <sys/resourcevar.h>
81
82 #include <ufs/ufs/inode.h>
83 #include <ufs/ufs/ufsmount.h>
84 #include <ufs/ufs/ufs_extern.h>
85
86 #include <ufs/ext2fs/ext2fs.h>
87 #include <ufs/ext2fs/ext2fs_extern.h>
88
89 extern int prtactive;
90
91 static int ext2fs_indirtrunc __P((struct inode *, daddr_t, daddr_t,
92 daddr_t, int, long *));
93
94 /*
95 * Last reference to an inode. If necessary, write or delete it.
96 */
97 int
98 ext2fs_inactive(v)
99 void *v;
100 {
101 struct vop_inactive_args /* {
102 struct vnode *a_vp;
103 struct proc *a_p;
104 } */ *ap = v;
105 struct vnode *vp = ap->a_vp;
106 struct inode *ip = VTOI(vp);
107 struct mount *mp;
108 struct proc *p = ap->a_p;
109 struct timespec ts;
110 int error = 0;
111
112 if (prtactive && vp->v_usecount != 0)
113 vprint("ext2fs_inactive: pushing active", vp);
114 /* Get rid of inodes related to stale file handles. */
115 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
116 goto out;
117
118 error = 0;
119 if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
120 vn_start_write(vp, &mp, V_WAIT | V_LOWER);
121 if (ip->i_e2fs_size != 0) {
122 error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
123 }
124 TIMEVAL_TO_TIMESPEC(&time, &ts);
125 ip->i_e2fs_dtime = ts.tv_sec;
126 ip->i_flag |= IN_CHANGE | IN_UPDATE;
127 VOP_VFREE(vp, ip->i_number, ip->i_e2fs_mode);
128 vn_finished_write(mp, V_LOWER);
129 }
130 if (ip->i_flag &
131 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) {
132 vn_start_write(vp, &mp, V_WAIT | V_LOWER);
133 VOP_UPDATE(vp, NULL, NULL, 0);
134 vn_finished_write(mp, V_LOWER);
135 }
136 out:
137 VOP_UNLOCK(vp, 0);
138 /*
139 * If we are done with the inode, reclaim it
140 * so that it can be reused immediately.
141 */
142 if (ip->i_e2fs_dtime != 0)
143 vrecycle(vp, NULL, p);
144 return (error);
145 }
146
147
148 /*
149 * Update the access, modified, and inode change times as specified by the
150 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
151 * used to specify that the inode needs to be updated but that the times have
152 * already been set. The access and modified times are taken from the second
153 * and third parameters; the inode change time is always taken from the current
154 * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk
155 * write of the inode to complete.
156 */
157 int
158 ext2fs_update(v)
159 void *v;
160 {
161 struct vop_update_args /* {
162 struct vnode *a_vp;
163 struct timespec *a_access;
164 struct timespec *a_modify;
165 int a_flags;
166 } */ *ap = v;
167 struct m_ext2fs *fs;
168 struct buf *bp;
169 struct inode *ip;
170 int error;
171 struct timespec ts;
172 caddr_t cp;
173 int flags;
174
175 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
176 return (0);
177 ip = VTOI(ap->a_vp);
178 TIMEVAL_TO_TIMESPEC(&time, &ts);
179 EXT2FS_ITIMES(ip,
180 ap->a_access ? ap->a_access : &ts,
181 ap->a_modify ? ap->a_modify : &ts, &ts);
182 if (ap->a_flags & UPDATE_CLOSE)
183 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
184 else
185 flags = ip->i_flag & IN_MODIFIED;
186 if (flags == 0)
187 return (0);
188 fs = ip->i_e2fs;
189
190 error = bread(ip->i_devvp,
191 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
192 (int)fs->e2fs_bsize, NOCRED, &bp);
193 if (error) {
194 brelse(bp);
195 return (error);
196 }
197 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
198 cp = (caddr_t)bp->b_data +
199 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
200 e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
201 if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
202 (flags & IN_MODIFIED) != 0 &&
203 (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
204 return (bwrite(bp));
205 else {
206 bdwrite(bp);
207 return (0);
208 }
209 }
210
211 #define SINGLE 0 /* index of single indirect block */
212 #define DOUBLE 1 /* index of double indirect block */
213 #define TRIPLE 2 /* index of triple indirect block */
214 /*
215 * Truncate the inode oip to at most length size, freeing the
216 * disk blocks.
217 */
218 int
219 ext2fs_truncate(v)
220 void *v;
221 {
222 struct vop_truncate_args /* {
223 struct vnode *a_vp;
224 off_t a_length;
225 int a_flags;
226 struct ucred *a_cred;
227 struct proc *a_p;
228 } */ *ap = v;
229 struct vnode *ovp = ap->a_vp;
230 daddr_t lastblock;
231 struct inode *oip;
232 daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
233 /* XXX ondisk32 */
234 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
235 off_t length = ap->a_length;
236 struct m_ext2fs *fs;
237 int offset, size, level;
238 long count, nblocks, blocksreleased = 0;
239 int i;
240 int error, allerror = 0;
241 off_t osize;
242
243 if (length < 0)
244 return (EINVAL);
245
246 oip = VTOI(ovp);
247 if (ovp->v_type == VLNK &&
248 (oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen ||
249 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
250 oip->i_e2fs_nblock == 0))) {
251 #ifdef DIAGNOSTIC
252 if (length != 0)
253 panic("ext2fs_truncate: partial truncate of symlink");
254 #endif
255 memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
256 (u_int)oip->i_e2fs_size);
257 oip->i_e2fs_size = 0;
258 oip->i_flag |= IN_CHANGE | IN_UPDATE;
259 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
260 }
261 if (oip->i_e2fs_size == length) {
262 oip->i_flag |= IN_CHANGE | IN_UPDATE;
263 return (VOP_UPDATE(ovp, NULL, NULL, 0));
264 }
265 fs = oip->i_e2fs;
266 osize = oip->i_e2fs_size;
267 /*
268 * Lengthen the size of the file. We must ensure that the
269 * last byte of the file is allocated. Since the smallest
270 * value of osize is 0, length will be at least 1.
271 */
272 if (osize < length) {
273 #if 0 /* XXX */
274 if (length > fs->fs_maxfilesize)
275 return (EFBIG);
276 #endif
277 ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
278 ap->a_flags & IO_SYNC ? B_SYNC : 0);
279 oip->i_flag |= IN_CHANGE | IN_UPDATE;
280 return (VOP_UPDATE(ovp, NULL, NULL, 1));
281 }
282 /*
283 * Shorten the size of the file. If the file is not being
284 * truncated to a block boundry, the contents of the
285 * partial block following the end of the file must be
286 * zero'ed in case it ever become accessible again because
287 * of subsequent file growth.
288 */
289 offset = blkoff(fs, length);
290 if (offset != 0) {
291 size = fs->e2fs_bsize;
292
293 /* XXXUBC we should handle more than just VREG */
294 uvm_vnp_zerorange(ovp, length, size - offset);
295 }
296 oip->i_e2fs_size = length;
297 uvm_vnp_setsize(ovp, length);
298
299 /*
300 * Calculate index into inode's block list of
301 * last direct and indirect blocks (if any)
302 * which we want to keep. Lastblock is -1 when
303 * the file is truncated to 0.
304 */
305 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
306 lastiblock[SINGLE] = lastblock - NDADDR;
307 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
308 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
309 nblocks = btodb(fs->e2fs_bsize);
310 /*
311 * Update file and block pointers on disk before we start freeing
312 * blocks. If we crash before free'ing blocks below, the blocks
313 * will be returned to the free list. lastiblock values are also
314 * normalized to -1 for calls to ext2fs_indirtrunc below.
315 */
316 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
317 for (level = TRIPLE; level >= SINGLE; level--)
318 if (lastiblock[level] < 0) {
319 oip->i_e2fs_blocks[NDADDR + level] = 0;
320 lastiblock[level] = -1;
321 }
322 for (i = NDADDR - 1; i > lastblock; i--)
323 oip->i_e2fs_blocks[i] = 0;
324 oip->i_flag |= IN_CHANGE | IN_UPDATE;
325 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
326 if (error && !allerror)
327 allerror = error;
328
329 /*
330 * Having written the new inode to disk, save its new configuration
331 * and put back the old block pointers long enough to process them.
332 * Note that we save the new block configuration so we can check it
333 * when we are done.
334 */
335
336 memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
337 memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
338 oip->i_e2fs_size = osize;
339 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
340 if (error && !allerror)
341 allerror = error;
342
343 /*
344 * Indirect blocks first.
345 */
346 indir_lbn[SINGLE] = -NDADDR;
347 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
348 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
349 for (level = TRIPLE; level >= SINGLE; level--) {
350 /* XXX ondisk32 */
351 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
352 if (bn != 0) {
353 error = ext2fs_indirtrunc(oip, indir_lbn[level],
354 fsbtodb(fs, bn), lastiblock[level], level, &count);
355 if (error)
356 allerror = error;
357 blocksreleased += count;
358 if (lastiblock[level] < 0) {
359 oip->i_e2fs_blocks[NDADDR + level] = 0;
360 ext2fs_blkfree(oip, bn);
361 blocksreleased += nblocks;
362 }
363 }
364 if (lastiblock[level] >= 0)
365 goto done;
366 }
367
368 /*
369 * All whole direct blocks or frags.
370 */
371 for (i = NDADDR - 1; i > lastblock; i--) {
372 /* XXX ondisk32 */
373 bn = fs2h32(oip->i_e2fs_blocks[i]);
374 if (bn == 0)
375 continue;
376 oip->i_e2fs_blocks[i] = 0;
377 ext2fs_blkfree(oip, bn);
378 blocksreleased += btodb(fs->e2fs_bsize);
379 }
380
381 done:
382 #ifdef DIAGNOSTIC
383 for (level = SINGLE; level <= TRIPLE; level++)
384 if (newblks[NDADDR + level] !=
385 oip->i_e2fs_blocks[NDADDR + level])
386 panic("ext2fs_truncate1");
387 for (i = 0; i < NDADDR; i++)
388 if (newblks[i] != oip->i_e2fs_blocks[i])
389 panic("ext2fs_truncate2");
390 if (length == 0 &&
391 (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
392 !LIST_EMPTY(&ovp->v_dirtyblkhd)))
393 panic("ext2fs_truncate3");
394 #endif /* DIAGNOSTIC */
395 /*
396 * Put back the real size.
397 */
398 oip->i_e2fs_size = length;
399 oip->i_e2fs_nblock -= blocksreleased;
400 oip->i_flag |= IN_CHANGE;
401 return (allerror);
402 }
403
404 /*
405 * Release blocks associated with the inode ip and stored in the indirect
406 * block bn. Blocks are free'd in LIFO order up to (but not including)
407 * lastbn. If level is greater than SINGLE, the block is an indirect block
408 * and recursive calls to indirtrunc must be used to cleanse other indirect
409 * blocks.
410 *
411 * NB: triple indirect blocks are untested.
412 */
413 static int
414 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
415 struct inode *ip;
416 daddr_t lbn, lastbn;
417 daddr_t dbn;
418 int level;
419 long *countp;
420 {
421 int i;
422 struct buf *bp;
423 struct m_ext2fs *fs = ip->i_e2fs;
424 int32_t *bap; /* XXX ondisk32 */
425 struct vnode *vp;
426 daddr_t nb, nlbn, last;
427 int32_t *copy = NULL; /* XXX ondisk32 */
428 long blkcount, factor;
429 int nblocks, blocksreleased = 0;
430 int error = 0, allerror = 0;
431
432 /*
433 * Calculate index in current block of last
434 * block to be kept. -1 indicates the entire
435 * block so we need not calculate the index.
436 */
437 factor = 1;
438 for (i = SINGLE; i < level; i++)
439 factor *= NINDIR(fs);
440 last = lastbn;
441 if (lastbn > 0)
442 last /= factor;
443 nblocks = btodb(fs->e2fs_bsize);
444 /*
445 * Get buffer of block pointers, zero those entries corresponding
446 * to blocks to be free'd, and update on disk copy first. Since
447 * double(triple) indirect before single(double) indirect, calls
448 * to bmap on these blocks will fail. However, we already have
449 * the on disk address, so we have to set the b_blkno field
450 * explicitly instead of letting bread do everything for us.
451 */
452 vp = ITOV(ip);
453 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
454 if (bp->b_flags & (B_DONE | B_DELWRI)) {
455 /* Braces must be here in case trace evaluates to nothing. */
456 trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
457 } else {
458 trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
459 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
460 bp->b_flags |= B_READ;
461 if (bp->b_bcount > bp->b_bufsize)
462 panic("ext2fs_indirtrunc: bad buffer size");
463 bp->b_blkno = dbn;
464 VOP_STRATEGY(vp, bp);
465 error = biowait(bp);
466 }
467 if (error) {
468 brelse(bp);
469 *countp = 0;
470 return (error);
471 }
472
473 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
474 if (lastbn >= 0) {
475 /* XXX ondisk32 */
476 MALLOC(copy, int32_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
477 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
478 memset((caddr_t)&bap[last + 1], 0,
479 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
480 error = bwrite(bp);
481 if (error)
482 allerror = error;
483 bap = copy;
484 }
485
486 /*
487 * Recursively free totally unused blocks.
488 */
489 for (i = NINDIR(fs) - 1,
490 nlbn = lbn + 1 - i * factor; i > last;
491 i--, nlbn += factor) {
492 /* XXX ondisk32 */
493 nb = fs2h32(bap[i]);
494 if (nb == 0)
495 continue;
496 if (level > SINGLE) {
497 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
498 (daddr_t)-1, level - 1,
499 &blkcount);
500 if (error)
501 allerror = error;
502 blocksreleased += blkcount;
503 }
504 ext2fs_blkfree(ip, nb);
505 blocksreleased += nblocks;
506 }
507
508 /*
509 * Recursively free last partial block.
510 */
511 if (level > SINGLE && lastbn >= 0) {
512 last = lastbn % factor;
513 /* XXX ondisk32 */
514 nb = fs2h32(bap[i]);
515 if (nb != 0) {
516 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
517 last, level - 1, &blkcount);
518 if (error)
519 allerror = error;
520 blocksreleased += blkcount;
521 }
522 }
523
524 if (copy != NULL) {
525 FREE(copy, M_TEMP);
526 } else {
527 bp->b_flags |= B_INVAL;
528 brelse(bp);
529 }
530
531 *countp = blocksreleased;
532 return (allerror);
533 }
534