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