ext2fs_inode.c revision 1.42 1 /* $NetBSD: ext2fs_inode.c,v 1.42 2004/08/14 14:32:04 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.42 2004/08/14 14:32:04 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 & (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) {
131 vn_start_write(vp, &mp, V_WAIT | V_LOWER);
132 VOP_UPDATE(vp, NULL, NULL, 0);
133 vn_finished_write(mp, V_LOWER);
134 }
135 out:
136 VOP_UNLOCK(vp, 0);
137 /*
138 * If we are done with the inode, reclaim it
139 * so that it can be reused immediately.
140 */
141 if (ip->i_e2fs_dtime != 0)
142 vrecycle(vp, NULL, p);
143 return (error);
144 }
145
146
147 /*
148 * Update the access, modified, and inode change times as specified by the
149 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
150 * used to specify that the inode needs to be updated but that the times have
151 * already been set. The access and modified times are taken from the second
152 * and third parameters; the inode change time is always taken from the current
153 * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk
154 * write of the inode to complete.
155 */
156 int
157 ext2fs_update(v)
158 void *v;
159 {
160 struct vop_update_args /* {
161 struct vnode *a_vp;
162 struct timespec *a_access;
163 struct timespec *a_modify;
164 int a_flags;
165 } */ *ap = v;
166 struct m_ext2fs *fs;
167 struct buf *bp;
168 struct inode *ip;
169 int error;
170 struct timespec ts;
171 caddr_t cp;
172 int flags;
173
174 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
175 return (0);
176 ip = VTOI(ap->a_vp);
177 TIMEVAL_TO_TIMESPEC(&time, &ts);
178 EXT2FS_ITIMES(ip,
179 ap->a_access ? ap->a_access : &ts,
180 ap->a_modify ? ap->a_modify : &ts, &ts);
181 if (ap->a_flags & UPDATE_CLOSE)
182 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
183 else
184 flags = ip->i_flag & IN_MODIFIED;
185 if (flags == 0)
186 return (0);
187 fs = ip->i_e2fs;
188
189 error = bread(ip->i_devvp,
190 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
191 (int)fs->e2fs_bsize, NOCRED, &bp);
192 if (error) {
193 brelse(bp);
194 return (error);
195 }
196 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
197 cp = (caddr_t)bp->b_data +
198 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
199 e2fs_isave(ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
200 if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
201 (flags & IN_MODIFIED) != 0 &&
202 (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
203 return (bwrite(bp));
204 else {
205 bdwrite(bp);
206 return (0);
207 }
208 }
209
210 #define SINGLE 0 /* index of single indirect block */
211 #define DOUBLE 1 /* index of double indirect block */
212 #define TRIPLE 2 /* index of triple indirect block */
213 /*
214 * Truncate the inode oip to at most length size, freeing the
215 * disk blocks.
216 */
217 int
218 ext2fs_truncate(v)
219 void *v;
220 {
221 struct vop_truncate_args /* {
222 struct vnode *a_vp;
223 off_t a_length;
224 int a_flags;
225 struct ucred *a_cred;
226 struct proc *a_p;
227 } */ *ap = v;
228 struct vnode *ovp = ap->a_vp;
229 daddr_t lastblock;
230 struct inode *oip;
231 daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
232 /* XXX ondisk32 */
233 int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
234 off_t length = ap->a_length;
235 struct m_ext2fs *fs;
236 int offset, size, level;
237 long count, nblocks, blocksreleased = 0;
238 int i;
239 int error, allerror = 0;
240 off_t osize;
241
242 if (length < 0)
243 return (EINVAL);
244
245 oip = VTOI(ovp);
246 if (ovp->v_type == VLNK &&
247 (oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen ||
248 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
249 oip->i_e2fs_nblock == 0))) {
250 #ifdef DIAGNOSTIC
251 if (length != 0)
252 panic("ext2fs_truncate: partial truncate of symlink");
253 #endif
254 memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
255 (u_int)oip->i_e2fs_size);
256 oip->i_e2fs_size = 0;
257 oip->i_flag |= IN_CHANGE | IN_UPDATE;
258 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
259 }
260 if (oip->i_e2fs_size == length) {
261 oip->i_flag |= IN_CHANGE | IN_UPDATE;
262 return (VOP_UPDATE(ovp, NULL, NULL, 0));
263 }
264 fs = oip->i_e2fs;
265 osize = oip->i_e2fs_size;
266 /*
267 * Lengthen the size of the file. We must ensure that the
268 * last byte of the file is allocated. Since the smallest
269 * value of osize is 0, length will be at least 1.
270 */
271 if (osize < length) {
272 #if 0 /* XXX */
273 if (length > fs->fs_maxfilesize)
274 return (EFBIG);
275 #endif
276 ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
277 ap->a_flags & IO_SYNC ? B_SYNC : 0);
278 oip->i_flag |= IN_CHANGE | IN_UPDATE;
279 return (VOP_UPDATE(ovp, NULL, NULL, 1));
280 }
281 /*
282 * Shorten the size of the file. If the file is not being
283 * truncated to a block boundry, the contents of the
284 * partial block following the end of the file must be
285 * zero'ed in case it ever become accessible again because
286 * of subsequent file growth.
287 */
288 offset = blkoff(fs, length);
289 if (offset != 0) {
290 size = fs->e2fs_bsize;
291
292 /* XXXUBC we should handle more than just VREG */
293 uvm_vnp_zerorange(ovp, length, size - offset);
294 }
295 oip->i_e2fs_size = length;
296 uvm_vnp_setsize(ovp, length);
297
298 /*
299 * Calculate index into inode's block list of
300 * last direct and indirect blocks (if any)
301 * which we want to keep. Lastblock is -1 when
302 * the file is truncated to 0.
303 */
304 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
305 lastiblock[SINGLE] = lastblock - NDADDR;
306 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
307 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
308 nblocks = btodb(fs->e2fs_bsize);
309 /*
310 * Update file and block pointers on disk before we start freeing
311 * blocks. If we crash before free'ing blocks below, the blocks
312 * will be returned to the free list. lastiblock values are also
313 * normalized to -1 for calls to ext2fs_indirtrunc below.
314 */
315 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
316 for (level = TRIPLE; level >= SINGLE; level--)
317 if (lastiblock[level] < 0) {
318 oip->i_e2fs_blocks[NDADDR + level] = 0;
319 lastiblock[level] = -1;
320 }
321 for (i = NDADDR - 1; i > lastblock; i--)
322 oip->i_e2fs_blocks[i] = 0;
323 oip->i_flag |= IN_CHANGE | IN_UPDATE;
324 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
325 if (error && !allerror)
326 allerror = error;
327
328 /*
329 * Having written the new inode to disk, save its new configuration
330 * and put back the old block pointers long enough to process them.
331 * Note that we save the new block configuration so we can check it
332 * when we are done.
333 */
334
335 memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
336 memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
337 oip->i_e2fs_size = osize;
338 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
339 if (error && !allerror)
340 allerror = error;
341
342 /*
343 * Indirect blocks first.
344 */
345 indir_lbn[SINGLE] = -NDADDR;
346 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
347 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
348 for (level = TRIPLE; level >= SINGLE; level--) {
349 /* XXX ondisk32 */
350 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
351 if (bn != 0) {
352 error = ext2fs_indirtrunc(oip, indir_lbn[level],
353 fsbtodb(fs, bn), lastiblock[level], level, &count);
354 if (error)
355 allerror = error;
356 blocksreleased += count;
357 if (lastiblock[level] < 0) {
358 oip->i_e2fs_blocks[NDADDR + level] = 0;
359 ext2fs_blkfree(oip, bn);
360 blocksreleased += nblocks;
361 }
362 }
363 if (lastiblock[level] >= 0)
364 goto done;
365 }
366
367 /*
368 * All whole direct blocks or frags.
369 */
370 for (i = NDADDR - 1; i > lastblock; i--) {
371 /* XXX ondisk32 */
372 bn = fs2h32(oip->i_e2fs_blocks[i]);
373 if (bn == 0)
374 continue;
375 oip->i_e2fs_blocks[i] = 0;
376 ext2fs_blkfree(oip, bn);
377 blocksreleased += btodb(fs->e2fs_bsize);
378 }
379
380 done:
381 #ifdef DIAGNOSTIC
382 for (level = SINGLE; level <= TRIPLE; level++)
383 if (newblks[NDADDR + level] !=
384 oip->i_e2fs_blocks[NDADDR + level])
385 panic("ext2fs_truncate1");
386 for (i = 0; i < NDADDR; i++)
387 if (newblks[i] != oip->i_e2fs_blocks[i])
388 panic("ext2fs_truncate2");
389 if (length == 0 &&
390 (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
391 !LIST_EMPTY(&ovp->v_dirtyblkhd)))
392 panic("ext2fs_truncate3");
393 #endif /* DIAGNOSTIC */
394 /*
395 * Put back the real size.
396 */
397 oip->i_e2fs_size = length;
398 oip->i_e2fs_nblock -= blocksreleased;
399 oip->i_flag |= IN_CHANGE;
400 return (allerror);
401 }
402
403 /*
404 * Release blocks associated with the inode ip and stored in the indirect
405 * block bn. Blocks are free'd in LIFO order up to (but not including)
406 * lastbn. If level is greater than SINGLE, the block is an indirect block
407 * and recursive calls to indirtrunc must be used to cleanse other indirect
408 * blocks.
409 *
410 * NB: triple indirect blocks are untested.
411 */
412 static int
413 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
414 struct inode *ip;
415 daddr_t lbn, lastbn;
416 daddr_t dbn;
417 int level;
418 long *countp;
419 {
420 int i;
421 struct buf *bp;
422 struct m_ext2fs *fs = ip->i_e2fs;
423 int32_t *bap; /* XXX ondisk32 */
424 struct vnode *vp;
425 daddr_t nb, nlbn, last;
426 int32_t *copy = NULL; /* XXX ondisk32 */
427 long blkcount, factor;
428 int nblocks, blocksreleased = 0;
429 int error = 0, allerror = 0;
430
431 /*
432 * Calculate index in current block of last
433 * block to be kept. -1 indicates the entire
434 * block so we need not calculate the index.
435 */
436 factor = 1;
437 for (i = SINGLE; i < level; i++)
438 factor *= NINDIR(fs);
439 last = lastbn;
440 if (lastbn > 0)
441 last /= factor;
442 nblocks = btodb(fs->e2fs_bsize);
443 /*
444 * Get buffer of block pointers, zero those entries corresponding
445 * to blocks to be free'd, and update on disk copy first. Since
446 * double(triple) indirect before single(double) indirect, calls
447 * to bmap on these blocks will fail. However, we already have
448 * the on disk address, so we have to set the b_blkno field
449 * explicitly instead of letting bread do everything for us.
450 */
451 vp = ITOV(ip);
452 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
453 if (bp->b_flags & (B_DONE | B_DELWRI)) {
454 /* Braces must be here in case trace evaluates to nothing. */
455 trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
456 } else {
457 trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
458 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
459 bp->b_flags |= B_READ;
460 if (bp->b_bcount > bp->b_bufsize)
461 panic("ext2fs_indirtrunc: bad buffer size");
462 bp->b_blkno = dbn;
463 VOP_STRATEGY(vp, bp);
464 error = biowait(bp);
465 }
466 if (error) {
467 brelse(bp);
468 *countp = 0;
469 return (error);
470 }
471
472 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
473 if (lastbn >= 0) {
474 /* XXX ondisk32 */
475 MALLOC(copy, int32_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
476 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
477 memset((caddr_t)&bap[last + 1], 0,
478 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
479 error = bwrite(bp);
480 if (error)
481 allerror = error;
482 bap = copy;
483 }
484
485 /*
486 * Recursively free totally unused blocks.
487 */
488 for (i = NINDIR(fs) - 1,
489 nlbn = lbn + 1 - i * factor; i > last;
490 i--, nlbn += factor) {
491 /* XXX ondisk32 */
492 nb = fs2h32(bap[i]);
493 if (nb == 0)
494 continue;
495 if (level > SINGLE) {
496 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
497 (daddr_t)-1, level - 1,
498 &blkcount);
499 if (error)
500 allerror = error;
501 blocksreleased += blkcount;
502 }
503 ext2fs_blkfree(ip, nb);
504 blocksreleased += nblocks;
505 }
506
507 /*
508 * Recursively free last partial block.
509 */
510 if (level > SINGLE && lastbn >= 0) {
511 last = lastbn % factor;
512 /* XXX ondisk32 */
513 nb = fs2h32(bap[i]);
514 if (nb != 0) {
515 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
516 last, level - 1, &blkcount);
517 if (error)
518 allerror = error;
519 blocksreleased += blkcount;
520 }
521 }
522
523 if (copy != NULL) {
524 FREE(copy, M_TEMP);
525 } else {
526 bp->b_flags |= B_INVAL;
527 brelse(bp);
528 }
529
530 *countp = blocksreleased;
531 return (allerror);
532 }
533