ext2fs_inode.c revision 1.23.2.1 1 /* $NetBSD: ext2fs_inode.c,v 1.23.2.1 2001/03/05 22:50:05 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/param.h>
41 #include <sys/systm.h>
42 #include <sys/mount.h>
43 #include <sys/lwp.h>
44 #include <sys/proc.h>
45 #include <sys/file.h>
46 #include <sys/buf.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/trace.h>
51 #include <sys/resourcevar.h>
52
53 #include <ufs/ufs/quota.h>
54 #include <ufs/ufs/inode.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/ufs_extern.h>
57
58 #include <ufs/ext2fs/ext2fs.h>
59 #include <ufs/ext2fs/ext2fs_extern.h>
60
61 extern int prtactive;
62
63 static int ext2fs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
64 ufs_daddr_t, int, long *));
65
66 /*
67 * Last reference to an inode. If necessary, write or delete it.
68 */
69 int
70 ext2fs_inactive(v)
71 void *v;
72 {
73 struct vop_inactive_args /* {
74 struct vnode *a_vp;
75 struct proc *a_p;
76 } */ *ap = v;
77 struct vnode *vp = ap->a_vp;
78 struct inode *ip = VTOI(vp);
79 struct proc *p = ap->a_p;
80 struct timespec ts;
81 int error = 0;
82
83 if (prtactive && vp->v_usecount != 0)
84 vprint("ext2fs_inactive: pushing active", vp);
85 /* Get rid of inodes related to stale file handles. */
86 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
87 goto out;
88
89 error = 0;
90 if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
91 if (ip->i_e2fs_size != 0) {
92 error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
93 }
94 TIMEVAL_TO_TIMESPEC(&time, &ts);
95 ip->i_e2fs_dtime = ts.tv_sec;
96 ip->i_flag |= IN_CHANGE | IN_UPDATE;
97 VOP_VFREE(vp, ip->i_number, ip->i_e2fs_mode);
98 }
99 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED))
100 VOP_UPDATE(vp, NULL, NULL, 0);
101 out:
102 VOP_UNLOCK(vp, 0);
103 /*
104 * If we are done with the inode, reclaim it
105 * so that it can be reused immediately.
106 */
107 if (ip->i_e2fs_dtime != 0)
108 vrecycle(vp, NULL, p);
109 return (error);
110 }
111
112
113 /*
114 * Update the access, modified, and inode change times as specified by the
115 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
116 * used to specify that the inode needs to be updated but that the times have
117 * already been set. The access and modified times are taken from the second
118 * and third parameters; the inode change time is always taken from the current
119 * time. If UPDATE_WAIT or UPDATE_DIROP is set, then wait for the disk
120 * write of the inode to complete.
121 */
122 int
123 ext2fs_update(v)
124 void *v;
125 {
126 struct vop_update_args /* {
127 struct vnode *a_vp;
128 struct timespec *a_access;
129 struct timespec *a_modify;
130 int a_flags;
131 } */ *ap = v;
132 struct m_ext2fs *fs;
133 struct buf *bp;
134 struct inode *ip;
135 int error;
136 struct timespec ts;
137 caddr_t cp;
138 int flags;
139
140 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
141 return (0);
142 ip = VTOI(ap->a_vp);
143 TIMEVAL_TO_TIMESPEC(&time, &ts);
144 EXT2FS_ITIMES(ip,
145 ap->a_access ? ap->a_access : &ts,
146 ap->a_modify ? ap->a_modify : &ts, &ts);
147 flags = ip->i_flag & (IN_MODIFIED | IN_ACCESSED);
148 if (flags == 0)
149 return (0);
150 fs = ip->i_e2fs;
151
152 error = bread(ip->i_devvp,
153 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
154 (int)fs->e2fs_bsize, NOCRED, &bp);
155 if (error) {
156 brelse(bp);
157 return (error);
158 }
159 ip->i_flag &= ~(IN_MODIFIED | IN_ACCESSED);
160 cp = (caddr_t)bp->b_data +
161 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
162 e2fs_isave(&ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
163 if ((ap->a_flags & (UPDATE_WAIT|UPDATE_DIROP)) != 0 &&
164 (flags & IN_MODIFIED) != 0 &&
165 (ap->a_vp->v_mount->mnt_flag & MNT_ASYNC) == 0)
166 return (bwrite(bp));
167 else {
168 bdwrite(bp);
169 return (0);
170 }
171 }
172
173 #define SINGLE 0 /* index of single indirect block */
174 #define DOUBLE 1 /* index of double indirect block */
175 #define TRIPLE 2 /* index of triple indirect block */
176 /*
177 * Truncate the inode oip to at most length size, freeing the
178 * disk blocks.
179 */
180 int
181 ext2fs_truncate(v)
182 void *v;
183 {
184 struct vop_truncate_args /* {
185 struct vnode *a_vp;
186 off_t a_length;
187 int a_flags;
188 struct ucred *a_cred;
189 struct proc *a_p;
190 } */ *ap = v;
191 struct vnode *ovp = ap->a_vp;
192 ufs_daddr_t lastblock;
193 struct inode *oip;
194 ufs_daddr_t bn, lastiblock[NIADDR], indir_lbn[NIADDR];
195 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
196 off_t length = ap->a_length;
197 struct m_ext2fs *fs;
198 int offset, size, level;
199 long count, nblocks, blocksreleased = 0;
200 int i;
201 int error, allerror = 0;
202 off_t osize;
203
204 if (length < 0)
205 return (EINVAL);
206
207 oip = VTOI(ovp);
208 if (ovp->v_type == VLNK &&
209 (oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen ||
210 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
211 oip->i_e2fs_nblock == 0))) {
212 #ifdef DIAGNOSTIC
213 if (length != 0)
214 panic("ext2fs_truncate: partial truncate of symlink");
215 #endif
216 memset((char *)&oip->i_din.e2fs_din.e2di_shortlink, 0,
217 (u_int)oip->i_e2fs_size);
218 oip->i_e2fs_size = 0;
219 oip->i_flag |= IN_CHANGE | IN_UPDATE;
220 return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
221 }
222 if (oip->i_e2fs_size == length) {
223 oip->i_flag |= IN_CHANGE | IN_UPDATE;
224 return (VOP_UPDATE(ovp, NULL, NULL, 0));
225 }
226 fs = oip->i_e2fs;
227 osize = oip->i_e2fs_size;
228 /*
229 * Lengthen the size of the file. We must ensure that the
230 * last byte of the file is allocated. Since the smallest
231 * value of osize is 0, length will be at least 1.
232 */
233 if (osize < length) {
234 #if 0 /* XXX */
235 if (length > fs->fs_maxfilesize)
236 return (EFBIG);
237 #endif
238 ext2fs_balloc_range(ovp, length - 1, 1, ap->a_cred,
239 ap->a_flags & IO_SYNC ? B_SYNC : 0);
240 oip->i_flag |= IN_CHANGE | IN_UPDATE;
241 return (VOP_UPDATE(ovp, NULL, NULL, 1));
242 }
243 /*
244 * Shorten the size of the file. If the file is not being
245 * truncated to a block boundry, the contents of the
246 * partial block following the end of the file must be
247 * zero'ed in case it ever become accessable again because
248 * of subsequent file growth.
249 */
250 offset = blkoff(fs, length);
251 if (offset != 0) {
252 size = fs->e2fs_bsize;
253
254 /* XXXUBC we should handle more than just VREG */
255 uvm_vnp_zerorange(ovp, length, size - offset);
256 }
257 oip->i_e2fs_size = length;
258 uvm_vnp_setsize(ovp, length);
259
260 /*
261 * Calculate index into inode's block list of
262 * last direct and indirect blocks (if any)
263 * which we want to keep. Lastblock is -1 when
264 * the file is truncated to 0.
265 */
266 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
267 lastiblock[SINGLE] = lastblock - NDADDR;
268 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
269 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
270 nblocks = btodb(fs->e2fs_bsize);
271 /*
272 * Update file and block pointers on disk before we start freeing
273 * blocks. If we crash before free'ing blocks below, the blocks
274 * will be returned to the free list. lastiblock values are also
275 * normalized to -1 for calls to ext2fs_indirtrunc below.
276 */
277 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
278 for (level = TRIPLE; level >= SINGLE; level--)
279 if (lastiblock[level] < 0) {
280 oip->i_e2fs_blocks[NDADDR + level] = 0;
281 lastiblock[level] = -1;
282 }
283 for (i = NDADDR - 1; i > lastblock; i--)
284 oip->i_e2fs_blocks[i] = 0;
285 oip->i_flag |= IN_CHANGE | IN_UPDATE;
286 error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
287 if (error && !allerror)
288 allerror = error;
289
290 /*
291 * Having written the new inode to disk, save its new configuration
292 * and put back the old block pointers long enough to process them.
293 * Note that we save the new block configuration so we can check it
294 * when we are done.
295 */
296
297 memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
298 memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
299 oip->i_e2fs_size = osize;
300 error = vtruncbuf(ovp, lastblock + 1, 0, 0);
301 if (error && !allerror)
302 allerror = error;
303
304 /*
305 * Indirect blocks first.
306 */
307 indir_lbn[SINGLE] = -NDADDR;
308 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
309 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
310 for (level = TRIPLE; level >= SINGLE; level--) {
311 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
312 if (bn != 0) {
313 error = ext2fs_indirtrunc(oip, indir_lbn[level],
314 fsbtodb(fs, bn), lastiblock[level], level, &count);
315 if (error)
316 allerror = error;
317 blocksreleased += count;
318 if (lastiblock[level] < 0) {
319 oip->i_e2fs_blocks[NDADDR + level] = 0;
320 ext2fs_blkfree(oip, bn);
321 blocksreleased += nblocks;
322 }
323 }
324 if (lastiblock[level] >= 0)
325 goto done;
326 }
327
328 /*
329 * All whole direct blocks or frags.
330 */
331 for (i = NDADDR - 1; i > lastblock; i--) {
332 bn = fs2h32(oip->i_e2fs_blocks[i]);
333 if (bn == 0)
334 continue;
335 oip->i_e2fs_blocks[i] = 0;
336 ext2fs_blkfree(oip, bn);
337 blocksreleased += btodb(fs->e2fs_bsize);
338 }
339
340 done:
341 #ifdef DIAGNOSTIC
342 for (level = SINGLE; level <= TRIPLE; level++)
343 if (newblks[NDADDR + level] !=
344 oip->i_e2fs_blocks[NDADDR + level])
345 panic("ext2fs_truncate1");
346 for (i = 0; i < NDADDR; i++)
347 if (newblks[i] != oip->i_e2fs_blocks[i])
348 panic("ext2fs_truncate2");
349 if (length == 0 &&
350 (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
351 !LIST_EMPTY(&ovp->v_dirtyblkhd)))
352 panic("ext2fs_truncate3");
353 #endif /* DIAGNOSTIC */
354 /*
355 * Put back the real size.
356 */
357 oip->i_e2fs_size = length;
358 oip->i_e2fs_nblock -= blocksreleased;
359 if (oip->i_e2fs_nblock < 0) /* sanity */
360 oip->i_e2fs_nblock = 0;
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