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