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