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