ext2fs_inode.c revision 1.11 1 /* $NetBSD: ext2fs_inode.c,v 1.11 1999/03/05 20:47:07 mycroft 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 #if defined(_KERNEL) && !defined(_LKM)
41 #include "opt_uvm.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mount.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 <vm/vm.h>
57 #if defined(UVM)
58 #include <uvm/uvm_extern.h>
59 #endif
60
61 #include <ufs/ufs/quota.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ufs/ufsmount.h>
64 #include <ufs/ufs/ufs_extern.h>
65
66 #include <ufs/ext2fs/ext2fs.h>
67 #include <ufs/ext2fs/ext2fs_extern.h>
68
69 static int ext2fs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
70 ufs_daddr_t, int, long *));
71
72 /*
73 * Last reference to an inode. If necessary, write or delete it.
74 */
75 int
76 ext2fs_inactive(v)
77 void *v;
78 {
79 struct vop_inactive_args /* {
80 struct vnode *a_vp;
81 struct proc *a_p;
82 } */ *ap = v;
83 struct vnode *vp = ap->a_vp;
84 struct inode *ip = VTOI(vp);
85 struct proc *p = ap->a_p;
86 struct timespec ts;
87 int error = 0;
88 extern int prtactive;
89
90 if (prtactive && vp->v_usecount != 0)
91 vprint("ext2fs_inactive: pushing active", vp);
92 /* Get rid of inodes related to stale file handles. */
93 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
94 goto out;
95
96 error = 0;
97 if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
98 error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
99 TIMEVAL_TO_TIMESPEC(&time, &ts);
100 ip->i_e2fs_dtime = ts.tv_sec;
101 ip->i_flag |= IN_CHANGE | IN_UPDATE;
102 VOP_VFREE(vp, ip->i_number, ip->i_e2fs_mode);
103 }
104 if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
105 TIMEVAL_TO_TIMESPEC(&time, &ts);
106 VOP_UPDATE(vp, &ts, &ts, 0);
107 }
108 out:
109 VOP_UNLOCK(vp, 0);
110 /*
111 * If we are done with the inode, reclaim it
112 * so that it can be reused immediately.
113 */
114 if (ip->i_e2fs_dtime != 0)
115 vrecycle(vp, (struct simplelock *)0, p);
116 return (error);
117 }
118
119
120 /*
121 * Update the access, modified, and inode change times as specified by the
122 * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
123 * used to specify that the inode needs to be updated but that the times have
124 * already been set. The access and modified times are taken from the second
125 * and third parameters; the inode change time is always taken from the current
126 * time. If waitfor is set, then wait for the disk write of the inode to
127 * complete.
128 */
129 int
130 ext2fs_update(v)
131 void *v;
132 {
133 struct vop_update_args /* {
134 struct vnode *a_vp;
135 struct timespec *a_access;
136 struct timespec *a_modify;
137 int a_waitfor;
138 } */ *ap = v;
139 register struct m_ext2fs *fs;
140 struct buf *bp;
141 struct inode *ip;
142 int error;
143 struct timespec ts;
144 caddr_t cp;
145
146 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
147 return (0);
148 ip = VTOI(ap->a_vp);
149 TIMEVAL_TO_TIMESPEC(&time, &ts);
150 EXT2FS_ITIMES(ip,
151 ap->a_access ? ap->a_access : &ts,
152 ap->a_modify ? ap->a_modify : &ts, &ts);
153 if ((ip->i_flag & IN_MODIFIED) == 0)
154 return (0);
155 ip->i_flag &= ~IN_MODIFIED;
156 fs = ip->i_e2fs;
157 error = bread(ip->i_devvp,
158 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
159 (int)fs->e2fs_bsize, NOCRED, &bp);
160 if (error) {
161 brelse(bp);
162 return (error);
163 }
164 cp = (caddr_t)bp->b_data +
165 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
166 e2fs_isave(&ip->i_din.e2fs_din, (struct ext2fs_dinode *)cp);
167 if (ap->a_waitfor && (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 register struct vnode *ovp = ap->a_vp;
194 register ufs_daddr_t lastblock;
195 register struct inode *oip;
196 ufs_daddr_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
197 ufs_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
198 off_t length = ap->a_length;
199 register struct m_ext2fs *fs;
200 struct buf *bp;
201 int offset, size, level;
202 long count, nblocks, vflags, blocksreleased = 0;
203 struct timespec ts;
204 register int i;
205 int aflags, error, allerror;
206 off_t osize;
207
208 if (length < 0)
209 return (EINVAL);
210
211 oip = VTOI(ovp);
212 TIMEVAL_TO_TIMESPEC(&time, &ts);
213 if (ovp->v_type == VLNK &&
214 (oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen ||
215 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
216 oip->i_e2fs_nblock == 0))) {
217 #ifdef DIAGNOSTIC
218 if (length != 0)
219 panic("ext2fs_truncate: partial truncate of symlink");
220 #endif
221 memset((char *)&oip->i_din.e2fs_din.e2di_shortlink, 0,
222 (u_int)oip->i_e2fs_size);
223 oip->i_e2fs_size = 0;
224 oip->i_flag |= IN_CHANGE | IN_UPDATE;
225 return (VOP_UPDATE(ovp, &ts, &ts, 1));
226 }
227 if (oip->i_e2fs_size == length) {
228 oip->i_flag |= IN_CHANGE | IN_UPDATE;
229 return (VOP_UPDATE(ovp, &ts, &ts, 0));
230 }
231 fs = oip->i_e2fs;
232 osize = oip->i_e2fs_size;
233 /*
234 * Lengthen the size of the file. We must ensure that the
235 * last byte of the file is allocated. Since the smallest
236 * value of osize is 0, length will be at least 1.
237 */
238 if (osize < length) {
239 #if 0 /* XXX */
240 if (length > fs->fs_maxfilesize)
241 return (EFBIG);
242 #endif
243 offset = blkoff(fs, length - 1);
244 lbn = lblkno(fs, length - 1);
245 aflags = B_CLRBUF;
246 if (ap->a_flags & IO_SYNC)
247 aflags |= B_SYNC;
248 error = ext2fs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
249 aflags);
250 if (error)
251 return (error);
252 oip->i_e2fs_size = length;
253 #if defined(UVM)
254 uvm_vnp_setsize(ovp, length);
255 (void) uvm_vnp_uncache(ovp);
256 #else
257 vnode_pager_setsize(ovp, length);
258 (void) vnode_pager_uncache(ovp);
259 #endif
260 if (aflags & B_SYNC)
261 bwrite(bp);
262 else
263 bawrite(bp);
264 oip->i_flag |= IN_CHANGE | IN_UPDATE;
265 return (VOP_UPDATE(ovp, &ts, &ts, 1));
266 }
267 /*
268 * Shorten the size of the file. If the file is not being
269 * truncated to a block boundry, the contents of the
270 * partial block following the end of the file must be
271 * zero'ed in case it ever become accessable again because
272 * of subsequent file growth.
273 */
274 offset = blkoff(fs, length);
275 if (offset == 0) {
276 oip->i_e2fs_size = length;
277 } else {
278 lbn = lblkno(fs, length);
279 aflags = B_CLRBUF;
280 if (ap->a_flags & IO_SYNC)
281 aflags |= B_SYNC;
282 error = ext2fs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags);
283 if (error)
284 return (error);
285 oip->i_e2fs_size = length;
286 size = fs->e2fs_bsize;
287 #if defined(UVM)
288 (void) uvm_vnp_uncache(ovp);
289 #else
290 (void) vnode_pager_uncache(ovp);
291 #endif
292 memset((char *)bp->b_data + offset, 0, (u_int)(size - offset));
293 allocbuf(bp, size);
294 if (aflags & B_SYNC)
295 bwrite(bp);
296 else
297 bawrite(bp);
298 }
299 #if defined(UVM)
300 uvm_vnp_setsize(ovp, length);
301 #else
302 vnode_pager_setsize(ovp, length);
303 #endif
304 /*
305 * Calculate index into inode's block list of
306 * last direct and indirect blocks (if any)
307 * which we want to keep. Lastblock is -1 when
308 * the file is truncated to 0.
309 */
310 lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
311 lastiblock[SINGLE] = lastblock - NDADDR;
312 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
313 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
314 nblocks = btodb(fs->e2fs_bsize);
315 /*
316 * Update file and block pointers on disk before we start freeing
317 * blocks. If we crash before free'ing blocks below, the blocks
318 * will be returned to the free list. lastiblock values are also
319 * normalized to -1 for calls to ext2fs_indirtrunc below.
320 */
321 memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
322 for (level = TRIPLE; level >= SINGLE; level--)
323 if (lastiblock[level] < 0) {
324 oip->i_e2fs_blocks[NDADDR + level] = 0;
325 lastiblock[level] = -1;
326 }
327 for (i = NDADDR - 1; i > lastblock; i--)
328 oip->i_e2fs_blocks[i] = 0;
329 oip->i_flag |= IN_CHANGE | IN_UPDATE;
330 if ((error = VOP_UPDATE(ovp, &ts, &ts, 1)) != 0)
331 allerror = error;
332 /*
333 * Having written the new inode to disk, save its new configuration
334 * and put back the old block pointers long enough to process them.
335 * Note that we save the new block configuration so we can check it
336 * when we are done.
337 */
338 memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
339 memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
340 oip->i_e2fs_size = osize;
341 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
342 allerror = vinvalbuf(ovp, vflags, ap->a_cred, ap->a_p, 0, 0);
343
344 /*
345 * Indirect blocks first.
346 */
347 indir_lbn[SINGLE] = -NDADDR;
348 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
349 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
350 for (level = TRIPLE; level >= SINGLE; level--) {
351 bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
352 if (bn != 0) {
353 error = ext2fs_indirtrunc(oip, indir_lbn[level],
354 fsbtodb(fs, bn), lastiblock[level], level, &count);
355 if (error)
356 allerror = error;
357 blocksreleased += count;
358 if (lastiblock[level] < 0) {
359 oip->i_e2fs_blocks[NDADDR + level] = 0;
360 ext2fs_blkfree(oip, bn);
361 blocksreleased += nblocks;
362 }
363 }
364 if (lastiblock[level] >= 0)
365 goto done;
366 }
367
368 /*
369 * All whole direct blocks or frags.
370 */
371 for (i = NDADDR - 1; i > lastblock; i--) {
372 bn = fs2h32(oip->i_e2fs_blocks[i]);
373 if (bn == 0)
374 continue;
375 oip->i_e2fs_blocks[i] = 0;
376 ext2fs_blkfree(oip, bn);
377 blocksreleased += btodb(fs->e2fs_bsize);
378 }
379 if (lastblock < 0)
380 goto done;
381
382 done:
383 #ifdef DIAGNOSTIC
384 for (level = SINGLE; level <= TRIPLE; level++)
385 if (newblks[NDADDR + level] !=
386 fs2h32(oip->i_e2fs_blocks[NDADDR + level]))
387 panic("itrunc1");
388 for (i = 0; i < NDADDR; i++)
389 if (newblks[i] != fs2h32(oip->i_e2fs_blocks[i]))
390 panic("itrunc2");
391 if (length == 0 &&
392 (ovp->v_dirtyblkhd.lh_first || ovp->v_cleanblkhd.lh_first))
393 panic("itrunc3");
394 #endif /* DIAGNOSTIC */
395 /*
396 * Put back the real size.
397 */
398 oip->i_e2fs_size = length;
399 oip->i_e2fs_nblock -= blocksreleased;
400 if (oip->i_e2fs_nblock < 0) /* sanity */
401 oip->i_e2fs_nblock = 0;
402 oip->i_flag |= IN_CHANGE;
403 return (allerror);
404 }
405
406 /*
407 * Release blocks associated with the inode ip and stored in the indirect
408 * block bn. Blocks are free'd in LIFO order up to (but not including)
409 * lastbn. If level is greater than SINGLE, the block is an indirect block
410 * and recursive calls to indirtrunc must be used to cleanse other indirect
411 * blocks.
412 *
413 * NB: triple indirect blocks are untested.
414 */
415 static int
416 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
417 register struct inode *ip;
418 ufs_daddr_t lbn, lastbn;
419 ufs_daddr_t dbn;
420 int level;
421 long *countp;
422 {
423 register int i;
424 struct buf *bp;
425 register struct m_ext2fs *fs = ip->i_e2fs;
426 register ufs_daddr_t *bap;
427 struct vnode *vp;
428 ufs_daddr_t *copy = NULL, nb, nlbn, last;
429 long blkcount, factor;
430 int nblocks, blocksreleased = 0;
431 int error = 0, allerror = 0;
432
433 /*
434 * Calculate index in current block of last
435 * block to be kept. -1 indicates the entire
436 * block so we need not calculate the index.
437 */
438 factor = 1;
439 for (i = SINGLE; i < level; i++)
440 factor *= NINDIR(fs);
441 last = lastbn;
442 if (lastbn > 0)
443 last /= factor;
444 nblocks = btodb(fs->e2fs_bsize);
445 /*
446 * Get buffer of block pointers, zero those entries corresponding
447 * to blocks to be free'd, and update on disk copy first. Since
448 * double(triple) indirect before single(double) indirect, calls
449 * to bmap on these blocks will fail. However, we already have
450 * the on disk address, so we have to set the b_blkno field
451 * explicitly instead of letting bread do everything for us.
452 */
453 vp = ITOV(ip);
454 bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
455 if (bp->b_flags & (B_DONE | B_DELWRI)) {
456 /* Braces must be here in case trace evaluates to nothing. */
457 trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
458 } else {
459 trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
460 curproc->p_stats->p_ru.ru_inblock++; /* pay for read */
461 bp->b_flags |= B_READ;
462 if (bp->b_bcount > bp->b_bufsize)
463 panic("ext2fs_indirtrunc: bad buffer size");
464 bp->b_blkno = dbn;
465 VOP_STRATEGY(bp);
466 error = biowait(bp);
467 }
468 if (error) {
469 brelse(bp);
470 *countp = 0;
471 return (error);
472 }
473
474 bap = (ufs_daddr_t *)bp->b_data;
475 if (lastbn != -1) {
476 MALLOC(copy, ufs_daddr_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
477 memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
478 memset((caddr_t)&bap[last + 1], 0,
479 (u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
480 error = bwrite(bp);
481 if (error)
482 allerror = error;
483 bap = copy;
484 }
485
486 /*
487 * Recursively free totally unused blocks.
488 */
489 for (i = NINDIR(fs) - 1,
490 nlbn = lbn + 1 - i * factor; i > last;
491 i--, nlbn += factor) {
492 nb = fs2h32(bap[i]);
493 if (nb == 0)
494 continue;
495 if (level > SINGLE) {
496 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
497 (ufs_daddr_t)-1, level - 1,
498 &blkcount);
499 if (error)
500 allerror = error;
501 blocksreleased += blkcount;
502 }
503 ext2fs_blkfree(ip, nb);
504 blocksreleased += nblocks;
505 }
506
507 /*
508 * Recursively free last partial block.
509 */
510 if (level > SINGLE && lastbn >= 0) {
511 last = lastbn % factor;
512 nb = fs2h32(bap[i]);
513 if (nb != 0) {
514 error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
515 last, level - 1, &blkcount);
516 if (error)
517 allerror = error;
518 blocksreleased += blkcount;
519 }
520 }
521
522 if (copy != NULL) {
523 FREE(copy, M_TEMP);
524 } else {
525 bp->b_flags |= B_INVAL;
526 brelse(bp);
527 }
528
529 *countp = blocksreleased;
530 return (allerror);
531 }
532