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