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