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