xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_inode.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: ext2fs_inode.c,v 1.19 2000/05/30 19:09:44 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 #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 <uvm/uvm_extern.h>
55 
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60 
61 #include <ufs/ext2fs/ext2fs.h>
62 #include <ufs/ext2fs/ext2fs_extern.h>
63 
64 static int ext2fs_indirtrunc __P((struct inode *, ufs_daddr_t, ufs_daddr_t,
65 				  ufs_daddr_t, int, long *));
66 
67 /*
68  * Last reference to an inode.  If necessary, write or delete it.
69  */
70 int
71 ext2fs_inactive(v)
72 	void *v;
73 {
74 	struct vop_inactive_args /* {
75 		struct vnode *a_vp;
76 		struct proc *a_p;
77 	} */ *ap = v;
78 	struct vnode *vp = ap->a_vp;
79 	struct inode *ip = VTOI(vp);
80 	struct proc *p = ap->a_p;
81 	struct timespec ts;
82 	int error = 0;
83 	extern int prtactive;
84 
85 	if (prtactive && vp->v_usecount != 0)
86 		vprint("ext2fs_inactive: pushing active", vp);
87 	/* Get rid of inodes related to stale file handles. */
88 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0)
89 		goto out;
90 
91 	error = 0;
92 	if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
93 		error = VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, NULL);
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, (struct simplelock *)0, 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, lbn, 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 	struct buf *bp;
199 	int offset, size, level;
200 	long count, nblocks, blocksreleased = 0;
201 	int i;
202 	int aflags, error, allerror = 0;
203 	off_t osize;
204 
205 	if (length < 0)
206 		return (EINVAL);
207 
208 	oip = VTOI(ovp);
209 	if (ovp->v_type == VLNK &&
210 		(oip->i_e2fs_size < ovp->v_mount->mnt_maxsymlinklen ||
211 		 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
212 		  oip->i_e2fs_nblock == 0))) {
213 #ifdef DIAGNOSTIC
214 		if (length != 0)
215 			panic("ext2fs_truncate: partial truncate of symlink");
216 #endif
217 		memset((char *)&oip->i_din.e2fs_din.e2di_shortlink, 0,
218 			(u_int)oip->i_e2fs_size);
219 		oip->i_e2fs_size = 0;
220 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
221 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
222 	}
223 	if (oip->i_e2fs_size == length) {
224 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
225 		return (VOP_UPDATE(ovp, NULL, NULL, 0));
226 	}
227 	fs = oip->i_e2fs;
228 	osize = oip->i_e2fs_size;
229 	/*
230 	 * Lengthen the size of the file. We must ensure that the
231 	 * last byte of the file is allocated. Since the smallest
232 	 * value of osize is 0, length will be at least 1.
233 	 */
234 	if (osize < length) {
235 #if 0 /* XXX */
236 		if (length > fs->fs_maxfilesize)
237 			return (EFBIG);
238 #endif
239 		offset = blkoff(fs, length - 1);
240 		lbn = lblkno(fs, length - 1);
241 		aflags = B_CLRBUF;
242 		if (ap->a_flags & IO_SYNC)
243 			aflags |= B_SYNC;
244 		error = ext2fs_balloc(oip, lbn, offset + 1, ap->a_cred, &bp,
245 				   aflags);
246 		if (error)
247 			return (error);
248 		oip->i_e2fs_size = length;
249 		uvm_vnp_setsize(ovp, length);
250 		(void) uvm_vnp_uncache(ovp);
251 		if (aflags & B_SYNC)
252 			bwrite(bp);
253 		else
254 			bawrite(bp);
255 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
256 		return (VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT));
257 	}
258 	/*
259 	 * Shorten the size of the file. If the file is not being
260 	 * truncated to a block boundry, the contents of the
261 	 * partial block following the end of the file must be
262 	 * zero'ed in case it ever become accessable again because
263 	 * of subsequent file growth.
264 	 */
265 	offset = blkoff(fs, length);
266 	if (offset == 0) {
267 		oip->i_e2fs_size = length;
268 	} else {
269 		lbn = lblkno(fs, length);
270 		aflags = B_CLRBUF;
271 		if (ap->a_flags & IO_SYNC)
272 			aflags |= B_SYNC;
273 		error = ext2fs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags);
274 		if (error)
275 			return (error);
276 		oip->i_e2fs_size = length;
277 		size = fs->e2fs_bsize;
278 		(void) uvm_vnp_uncache(ovp);
279 		memset((char *)bp->b_data + offset, 0,  (u_int)(size - offset));
280 		allocbuf(bp, size);
281 		if (aflags & B_SYNC)
282 			bwrite(bp);
283 		else
284 			bawrite(bp);
285 	}
286 	uvm_vnp_setsize(ovp, length);
287 
288 	/*
289 	 * Calculate index into inode's block list of
290 	 * last direct and indirect blocks (if any)
291 	 * which we want to keep.  Lastblock is -1 when
292 	 * the file is truncated to 0.
293 	 */
294 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
295 	lastiblock[SINGLE] = lastblock - NDADDR;
296 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
297 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
298 	nblocks = btodb(fs->e2fs_bsize);
299 	/*
300 	 * Update file and block pointers on disk before we start freeing
301 	 * blocks.  If we crash before free'ing blocks below, the blocks
302 	 * will be returned to the free list.  lastiblock values are also
303 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
304 	 */
305 	memcpy((caddr_t)oldblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof oldblks);
306 	for (level = TRIPLE; level >= SINGLE; level--)
307 		if (lastiblock[level] < 0) {
308 			oip->i_e2fs_blocks[NDADDR + level] = 0;
309 			lastiblock[level] = -1;
310 		}
311 	for (i = NDADDR - 1; i > lastblock; i--)
312 		oip->i_e2fs_blocks[i] = 0;
313 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
314 	error = VOP_UPDATE(ovp, NULL, NULL, UPDATE_WAIT);
315 	if (error && !allerror)
316 		allerror = error;
317 
318 	/*
319 	 * Having written the new inode to disk, save its new configuration
320 	 * and put back the old block pointers long enough to process them.
321 	 * Note that we save the new block configuration so we can check it
322 	 * when we are done.
323 	 */
324 	memcpy((caddr_t)newblks, (caddr_t)&oip->i_e2fs_blocks[0], sizeof newblks);
325 	memcpy((caddr_t)&oip->i_e2fs_blocks[0], (caddr_t)oldblks, sizeof oldblks);
326 	oip->i_e2fs_size = osize;
327 	error = vtruncbuf(ovp, lastblock + 1, 0, 0);
328 	if (error && !allerror)
329 		allerror = error;
330 
331 	/*
332 	 * Indirect blocks first.
333 	 */
334 	indir_lbn[SINGLE] = -NDADDR;
335 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
336 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
337 	for (level = TRIPLE; level >= SINGLE; level--) {
338 		bn = fs2h32(oip->i_e2fs_blocks[NDADDR + level]);
339 		if (bn != 0) {
340 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
341 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
342 			if (error)
343 				allerror = error;
344 			blocksreleased += count;
345 			if (lastiblock[level] < 0) {
346 				oip->i_e2fs_blocks[NDADDR + level] = 0;
347 				ext2fs_blkfree(oip, bn);
348 				blocksreleased += nblocks;
349 			}
350 		}
351 		if (lastiblock[level] >= 0)
352 			goto done;
353 	}
354 
355 	/*
356 	 * All whole direct blocks or frags.
357 	 */
358 	for (i = NDADDR - 1; i > lastblock; i--) {
359 		bn = fs2h32(oip->i_e2fs_blocks[i]);
360 		if (bn == 0)
361 			continue;
362 		oip->i_e2fs_blocks[i] = 0;
363 		ext2fs_blkfree(oip, bn);
364 		blocksreleased += btodb(fs->e2fs_bsize);
365 	}
366 	if (lastblock < 0)
367 		goto done;
368 
369 done:
370 #ifdef DIAGNOSTIC
371 	for (level = SINGLE; level <= TRIPLE; level++)
372 		if (newblks[NDADDR + level] != oip->i_e2fs_blocks[NDADDR + level])
373 			panic("itrunc1");
374 	for (i = 0; i < NDADDR; i++)
375 		if (newblks[i] != oip->i_e2fs_blocks[i])
376 			panic("itrunc2");
377 	if (length == 0 &&
378 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) || !LIST_EMPTY(&ovp->v_dirtyblkhd)))
379 		panic("itrunc3");
380 #endif /* DIAGNOSTIC */
381 	/*
382 	 * Put back the real size.
383 	 */
384 	oip->i_e2fs_size = length;
385 	oip->i_e2fs_nblock -= blocksreleased;
386 	if (oip->i_e2fs_nblock < 0)			/* sanity */
387 		oip->i_e2fs_nblock = 0;
388 	oip->i_flag |= IN_CHANGE;
389 	return (allerror);
390 }
391 
392 /*
393  * Release blocks associated with the inode ip and stored in the indirect
394  * block bn.  Blocks are free'd in LIFO order up to (but not including)
395  * lastbn.  If level is greater than SINGLE, the block is an indirect block
396  * and recursive calls to indirtrunc must be used to cleanse other indirect
397  * blocks.
398  *
399  * NB: triple indirect blocks are untested.
400  */
401 static int
402 ext2fs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
403 	struct inode *ip;
404 	ufs_daddr_t lbn, lastbn;
405 	ufs_daddr_t dbn;
406 	int level;
407 	long *countp;
408 {
409 	int i;
410 	struct buf *bp;
411 	struct m_ext2fs *fs = ip->i_e2fs;
412 	ufs_daddr_t *bap;
413 	struct vnode *vp;
414 	ufs_daddr_t *copy = NULL, nb, nlbn, last;
415 	long blkcount, factor;
416 	int nblocks, blocksreleased = 0;
417 	int error = 0, allerror = 0;
418 
419 	/*
420 	 * Calculate index in current block of last
421 	 * block to be kept.  -1 indicates the entire
422 	 * block so we need not calculate the index.
423 	 */
424 	factor = 1;
425 	for (i = SINGLE; i < level; i++)
426 		factor *= NINDIR(fs);
427 	last = lastbn;
428 	if (lastbn > 0)
429 		last /= factor;
430 	nblocks = btodb(fs->e2fs_bsize);
431 	/*
432 	 * Get buffer of block pointers, zero those entries corresponding
433 	 * to blocks to be free'd, and update on disk copy first.  Since
434 	 * double(triple) indirect before single(double) indirect, calls
435 	 * to bmap on these blocks will fail.  However, we already have
436 	 * the on disk address, so we have to set the b_blkno field
437 	 * explicitly instead of letting bread do everything for us.
438 	 */
439 	vp = ITOV(ip);
440 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0);
441 	if (bp->b_flags & (B_DONE | B_DELWRI)) {
442 		/* Braces must be here in case trace evaluates to nothing. */
443 		trace(TR_BREADHIT, pack(vp, fs->e2fs_bsize), lbn);
444 	} else {
445 		trace(TR_BREADMISS, pack(vp, fs->e2fs_bsize), lbn);
446 		curproc->p_stats->p_ru.ru_inblock++;	/* pay for read */
447 		bp->b_flags |= B_READ;
448 		if (bp->b_bcount > bp->b_bufsize)
449 			panic("ext2fs_indirtrunc: bad buffer size");
450 		bp->b_blkno = dbn;
451 		VOP_STRATEGY(bp);
452 		error = biowait(bp);
453 	}
454 	if (error) {
455 		brelse(bp);
456 		*countp = 0;
457 		return (error);
458 	}
459 
460 	bap = (ufs_daddr_t *)bp->b_data;
461 	if (lastbn >= 0) {
462 		MALLOC(copy, ufs_daddr_t *, fs->e2fs_bsize, M_TEMP, M_WAITOK);
463 		memcpy((caddr_t)copy, (caddr_t)bap, (u_int)fs->e2fs_bsize);
464 		memset((caddr_t)&bap[last + 1], 0,
465 			(u_int)(NINDIR(fs) - (last + 1)) * sizeof (u_int32_t));
466 		error = bwrite(bp);
467 		if (error)
468 			allerror = error;
469 		bap = copy;
470 	}
471 
472 	/*
473 	 * Recursively free totally unused blocks.
474 	 */
475 	for (i = NINDIR(fs) - 1,
476 		nlbn = lbn + 1 - i * factor; i > last;
477 		i--, nlbn += factor) {
478 		nb = fs2h32(bap[i]);
479 		if (nb == 0)
480 			continue;
481 		if (level > SINGLE) {
482 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
483 						   (ufs_daddr_t)-1, level - 1,
484 						   &blkcount);
485 			if (error)
486 				allerror = error;
487 			blocksreleased += blkcount;
488 		}
489 		ext2fs_blkfree(ip, nb);
490 		blocksreleased += nblocks;
491 	}
492 
493 	/*
494 	 * Recursively free last partial block.
495 	 */
496 	if (level > SINGLE && lastbn >= 0) {
497 		last = lastbn % factor;
498 		nb = fs2h32(bap[i]);
499 		if (nb != 0) {
500 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
501 						   last, level - 1, &blkcount);
502 			if (error)
503 				allerror = error;
504 			blocksreleased += blkcount;
505 		}
506 	}
507 
508 	if (copy != NULL) {
509 		FREE(copy, M_TEMP);
510 	} else {
511 		bp->b_flags |= B_INVAL;
512 		brelse(bp);
513 	}
514 
515 	*countp = blocksreleased;
516 	return (allerror);
517 }
518