ext2fs_inode.c revision 1.43 1 /* $NetBSD: ext2fs_inode.c,v 1.43 2004/08/15 07:19:56 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.43 2004/08/15 07:19:56 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 = VTOI(ovp);
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, blocksreleased = 0;
238 int i, ioflag, nblocks;
239 int error, allerror = 0;
240 off_t osize;
241 int sync;
242 struct ufsmount *ump = oip->i_ump;
243
244 if (length < 0)
245 return (EINVAL);
246
247 if (ovp->v_type == VLNK &&
248 (oip->i_e2fs_size < ump->um_maxsymlinklen ||
249 (ump->um_maxsymlinklen == 0 && oip->i_e2fs_nblock == 0))) {
250 KDASSERT(length == 0);
251 memset((char *)&oip->i_din.e2fs_din->e2di_shortlink, 0,
252 (u_int)oip->i_e2fs_size);
253 oip->i_e2fs_size = 0;
254 oip->i_flag |= IN_CHANGE | IN_UPDATE;
255 return (VOP_UPDATE(ovp, NULL, NULL, 0));
256 }
257 if (oip->i_e2fs_size == length) {
258 oip->i_flag |= IN_CHANGE | IN_UPDATE;
259 return (VOP_UPDATE(ovp, NULL, NULL, 0));
260 }
261 fs = oip->i_e2fs;
262 if (length > ump->um_maxfilesize)
263 return (EFBIG);
264
265 osize = oip->i_e2fs_size;
266 ioflag = ap->a_flags;
267
268 /*
269 * Lengthen the size of the file. We must ensure that the
270 * last byte of the file is allocated. Since the smallest
271 * value of osize is 0, length will be at least 1.
272 */
273 if (osize < length) {
274 error = ufs_balloc_range(ovp, length - 1, 1, ap->a_cred,
275 ioflag & IO_SYNC ? B_SYNC : 0);
276 if (error) {
277 (void) VOP_TRUNCATE(ovp, osize, ioflag & IO_SYNC,
278 ap->a_cred, ap->a_p);
279 return (error);
280 }
281 uvm_vnp_setsize(ovp, length);
282 oip->i_flag |= IN_CHANGE | IN_UPDATE;
283 KASSERT(ovp->v_size == oip->i_size);
284 return (VOP_UPDATE(ovp, NULL, NULL, 0));
285 }
286 /*
287 * Shorten the size of the file. If the file is not being
288 * truncated to a block boundry, the contents of the
289 * partial block following the end of the file must be
290 * zero'ed in case it ever become accessible again because
291 * of subsequent file growth.
292 */
293 offset = blkoff(fs, length);
294 if (offset != 0) {
295 size = fs->e2fs_bsize;
296
297 /* XXXUBC we should handle more than just VREG */
298 uvm_vnp_zerorange(ovp, length, size - offset);
299 }
300 oip->i_e2fs_size = length;
301 uvm_vnp_setsize(ovp, length);
302 /*
303 * Calculate index into inode's block list of
304 * last direct and indirect blocks (if any)
305 * which we want to keep. Lastblock is -1 when
306 * the file is truncated to 0.
307 */
308 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
309 lastiblock[SINGLE] = lastblock - NDADDR;
310 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
311 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
312 nblocks = btodb(fs->e2fs_bsize);
313 /*
314 * Update file and block pointers on disk before we start freeing
315 * blocks. If we crash before free'ing blocks below, the blocks
316 * will be returned to the free list. lastiblock values are also
317 * normalized to -1 for calls to ext2fs_indirtrunc below.
318 */
319 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
320 sync = 0;
321 for (level = TRIPLE; level >= SINGLE; level--) {
322 if (lastiblock[level] < 0 && oldblks[NDADDR + level] != 0) {
323 sync = 1;
324 oip->i_e2fs_blocks[NDADDR + level] = 0;
325 lastiblock[level] = -1;
326 }
327 }
328 for (i = 0; i < NDADDR; i++) {
329 if (i > lastblock && oldblks[i] != 0) {
330 sync = 1;
331 oip->i_e2fs_blocks[i] = 0;
332 }
333 }
334 oip->i_flag |= IN_CHANGE | IN_UPDATE;
335 if (sync) {
336 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
337 if (error && !allerror)
338 allerror = error;
339 }
340
341 /*
342 * Having written the new inode to disk, save its new configuration
343 * and put back the old block pointers long enough to process them.
344 * Note that we save the new block configuration so we can check it
345 * when we are done.
346 */
347 memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
348 memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
349
350 oip->i_e2fs_size = osize;
351 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
352 if (error && !allerror)
353 allerror = error;
354
355 /*
356 * Indirect blocks first.
357 */
358 indir_lbn[SINGLE] = -NDADDR;
359 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
360 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
361 for (level = TRIPLE; level >= SINGLE; level--) {
362 /* XXX ondisk32 */
363 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
364 if (bn != 0) {
365 error = ext2fs_indirtrunc(oip, indir_lbn[level],
366 fsbtodb(fs, bn), lastiblock[level], level, &count);
367 if (error)
368 allerror = error;
369 blocksreleased += count;
370 if (lastiblock[level] < 0) {
371 oip->i_e2fs_blocks[NDADDR + level] = 0;
372 ext2fs_blkfree(oip, bn);
373 blocksreleased += nblocks;
374 }
375 }
376 if (lastiblock[level] >= 0)
377 goto done;
378 }
379
380 /*
381 * All whole direct blocks or frags.
382 */
383 for (i = NDADDR - 1; i > lastblock; i--) {
384 /* XXX ondisk32 */
385 bn = fs2h32(oip->i_e2fs_blocks[i]);
386 if (bn == 0)
387 continue;
388 oip->i_e2fs_blocks[i] = 0;
389 ext2fs_blkfree(oip, bn);
390 blocksreleased += btodb(fs->e2fs_bsize);
391 }
392
393 done:
394 #ifdef DIAGNOSTIC
395 for (level = SINGLE; level <= TRIPLE; level++)
396 if (newblks[NDADDR + level] !=
397 oip->i_e2fs_blocks[NDADDR + level])
398 panic("ext2fs_truncate1");
399 for (i = 0; i < NDADDR; i++)
400 if (newblks[i] != oip->i_e2fs_blocks[i])
401 panic("ext2fs_truncate2");
402 if (length == 0 &&
403 (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
404 !LIST_EMPTY(&ovp->v_dirtyblkhd)))
405 panic("ext2fs_truncate3");
406 #endif /* DIAGNOSTIC */
407 /*
408 * Put back the real size.
409 */
410 oip->i_e2fs_size = length;
411 oip->i_e2fs_nblock -= blocksreleased;
412 oip->i_flag |= IN_CHANGE;
413 KASSERT(ovp->v_type != VREG || ovp->v_size == oip->i_size);
414 return (allerror);
415 }
416
417 /*
418 * Release blocks associated with the inode ip and stored in the indirect
419 * block bn. Blocks are free'd in LIFO order up to (but not including)
420 * lastbn. If level is greater than SINGLE, the block is an indirect block
421 * and recursive calls to indirtrunc must be used to cleanse other indirect
422 * blocks.
423 *
424 * NB: triple indirect blocks are untested.
425 */
426 static int
427 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
428 struct inode *ip;
429 daddr_t lbn, lastbn;
430 daddr_t dbn;
431 int level;
432 long *countp;
433 {
434 int i;
435 struct buf *bp;
436 struct m_ext2fs *fs = ip->i_e2fs;
437 int32_t *bap; /* XXX ondisk32 */
438 struct vnode *vp;
439 daddr_t nb, nlbn, last;
440 int32_t *copy = NULL; /* XXX ondisk32 */
441 long blkcount, factor;
442 int nblocks, blocksreleased = 0;
443 int error = 0, allerror = 0;
444
445 /*
446 * Calculate index in current block of last
447 * block to be kept. -1 indicates the entire
448 * block so we need not calculate the index.
449 */
450 factor = 1;
451 for (i = SINGLE; i < level; i++)
452 factor *= NINDIR(fs);
453 last = lastbn;
454 if (lastbn > 0)
455 last /= factor;
456 nblocks = btodb(fs->e2fs_bsize);
457 /*
458 * Get buffer of block pointers, zero those entries corresponding
459 * to blocks to be free'd, and update on disk copy first. Since
460 * double(triple) indirect before single(double) indirect, calls
461 * to bmap on these blocks will fail. However, we already have
462 * the on disk address, so we have to set the b_blkno field
463 * explicitly instead of letting bread do everything for us.
464 */
465 vp = ITOV(ip);
466 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
467 if (bp->b_flags & (B_DONE | B_DELWRI)) {
468 /* Braces must be here in case trace evaluates to nothing. */
469 trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
470 } else {
471 trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
472 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
473 bp->b_flags |= B_READ;
474 if (bp->b_bcount > bp->b_bufsize)
475 panic("ext2fs_indirtrunc: bad buffer size");
476 bp->b_blkno = dbn;
477 VOP_STRATEGY(vp, bp);
478 error = biowait(bp);
479 }
480 if (error) {
481 brelse(bp);
482 *countp = 0;
483 return (error);
484 }
485
486 bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
487 if (lastbn >= 0) {
488 /* XXX ondisk32 */
489 MALLOC(copy, int32_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
490 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
491 memset((caddr_t)&bap[last + 1], 0,
492 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
493 error = bwrite(bp);
494 if (error)
495 allerror = error;
496 bap = copy;
497 }
498
499 /*
500 * Recursively free totally unused blocks.
501 */
502 for (i = NINDIR(fs) - 1,
503 nlbn = lbn + 1 - i * factor; i > last;
504 i--, nlbn += factor) {
505 /* XXX ondisk32 */
506 nb = fs2h32(bap[i]);
507 if (nb == 0)
508 continue;
509 if (level > SINGLE) {
510 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
511 (daddr_t)-1, level - 1,
512 &blkcount);
513 if (error)
514 allerror = error;
515 blocksreleased += blkcount;
516 }
517 ext2fs_blkfree(ip, nb);
518 blocksreleased += nblocks;
519 }
520
521 /*
522 * Recursively free last partial block.
523 */
524 if (level > SINGLE && lastbn >= 0) {
525 last = lastbn % factor;
526 /* XXX ondisk32 */
527 nb = fs2h32(bap[i]);
528 if (nb != 0) {
529 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
530 last, level - 1, &blkcount);
531 if (error)
532 allerror = error;
533 blocksreleased += blkcount;
534 }
535 }
536
537 if (copy != NULL) {
538 FREE(copy, M_TEMP);
539 } else {
540 bp->b_flags |= B_INVAL;
541 brelse(bp);
542 }
543
544 *countp = blocksreleased;
545 return (allerror);
546 }
547