xref: /freebsd/sys/fs/ext2fs/ext2_inode.c (revision 5944f899a2519c6321bac3c17cc076418643a088)
1 /*-
2  *  modified for Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*-
8  * Copyright (c) 1982, 1986, 1989, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)ffs_inode.c	8.5 (Berkeley) 12/30/93
36  * $FreeBSD$
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mount.h>
42 #include <sys/bio.h>
43 #include <sys/buf.h>
44 #include <sys/vnode.h>
45 #include <sys/malloc.h>
46 #include <sys/rwlock.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_extern.h>
50 
51 #include <fs/ext2fs/inode.h>
52 #include <fs/ext2fs/ext2_mount.h>
53 #include <fs/ext2fs/ext2fs.h>
54 #include <fs/ext2fs/fs.h>
55 #include <fs/ext2fs/ext2_extern.h>
56 
57 static int ext2_indirtrunc(struct inode *, daddr_t, daddr_t,
58 	    daddr_t, int, e4fs_daddr_t *);
59 
60 /*
61  * Update the access, modified, and inode change times as specified by the
62  * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.  Write the inode
63  * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
64  * the timestamp update).  The IN_LAZYMOD flag is set to force a write
65  * later if not now.  If we write now, then clear both IN_MODIFIED and
66  * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
67  * set, then wait for the write to complete.
68  */
69 int
70 ext2_update(struct vnode *vp, int waitfor)
71 {
72 	struct m_ext2fs *fs;
73 	struct buf *bp;
74 	struct inode *ip;
75 	int error;
76 
77 	ASSERT_VOP_ELOCKED(vp, "ext2_update");
78 	ext2_itimes(vp);
79 	ip = VTOI(vp);
80 	if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
81 		return (0);
82 	ip->i_flag &= ~(IN_LAZYACCESS | IN_LAZYMOD | IN_MODIFIED);
83 	fs = ip->i_e2fs;
84 	if (fs->e2fs_ronly)
85 		return (0);
86 	if ((error = bread(ip->i_devvp,
87 	    fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
88 	    (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
89 		brelse(bp);
90 		return (error);
91 	}
92 	ext2_i2ei(ip, (struct ext2fs_dinode *)((char *)bp->b_data +
93 	    EXT2_INODE_SIZE(fs) * ino_to_fsbo(fs, ip->i_number)));
94 	if (waitfor && !DOINGASYNC(vp))
95 		return (bwrite(bp));
96 	else {
97 		bdwrite(bp);
98 		return (0);
99 	}
100 }
101 
102 #define	SINGLE	0	/* index of single indirect block */
103 #define	DOUBLE	1	/* index of double indirect block */
104 #define	TRIPLE	2	/* index of triple indirect block */
105 /*
106  * Truncate the inode oip to at most length size, freeing the
107  * disk blocks.
108  */
109 int
110 ext2_truncate(struct vnode *vp, off_t length, int flags, struct ucred *cred,
111     struct thread *td)
112 {
113 	struct vnode *ovp = vp;
114 	int32_t lastblock;
115 	struct inode *oip;
116 	int32_t bn, lbn, lastiblock[EXT2_NIADDR], indir_lbn[EXT2_NIADDR];
117 	uint32_t oldblks[EXT2_NDADDR + EXT2_NIADDR];
118 	uint32_t newblks[EXT2_NDADDR + EXT2_NIADDR];
119 	struct m_ext2fs *fs;
120 	struct buf *bp;
121 	int offset, size, level;
122 	e4fs_daddr_t count, nblocks, blocksreleased = 0;
123 	int error, i, allerror;
124 	off_t osize;
125 #ifdef INVARIANTS
126 	struct bufobj *bo;
127 #endif
128 
129 	oip = VTOI(ovp);
130 #ifdef INVARIANTS
131 	bo = &ovp->v_bufobj;
132 #endif
133 
134 	ASSERT_VOP_LOCKED(vp, "ext2_truncate");
135 
136 	if (length < 0)
137 		return (EINVAL);
138 
139 	if (ovp->v_type == VLNK &&
140 	    oip->i_size < ovp->v_mount->mnt_maxsymlinklen) {
141 #ifdef INVARIANTS
142 		if (length != 0)
143 			panic("ext2_truncate: partial truncate of symlink");
144 #endif
145 		bzero((char *)&oip->i_shortlink, (u_int)oip->i_size);
146 		oip->i_size = 0;
147 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
148 		return (ext2_update(ovp, 1));
149 	}
150 	if (oip->i_size == length) {
151 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
152 		return (ext2_update(ovp, 0));
153 	}
154 	fs = oip->i_e2fs;
155 	osize = oip->i_size;
156 	/*
157 	 * Lengthen the size of the file. We must ensure that the
158 	 * last byte of the file is allocated. Since the smallest
159 	 * value of osize is 0, length will be at least 1.
160 	 */
161 	if (osize < length) {
162 		if (length > oip->i_e2fs->e2fs_maxfilesize)
163 			return (EFBIG);
164 		vnode_pager_setsize(ovp, length);
165 		offset = blkoff(fs, length - 1);
166 		lbn = lblkno(fs, length - 1);
167 		flags |= BA_CLRBUF;
168 		error = ext2_balloc(oip, lbn, offset + 1, cred, &bp, flags);
169 		if (error) {
170 			vnode_pager_setsize(vp, osize);
171 			return (error);
172 		}
173 		oip->i_size = length;
174 		if (bp->b_bufsize == fs->e2fs_bsize)
175 			bp->b_flags |= B_CLUSTEROK;
176 		if (flags & IO_SYNC)
177 			bwrite(bp);
178 		else if (DOINGASYNC(ovp))
179 			bdwrite(bp);
180 		else
181 			bawrite(bp);
182 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
183 		return (ext2_update(ovp, !DOINGASYNC(ovp)));
184 	}
185 	/*
186 	 * Shorten the size of the file. If the file is not being
187 	 * truncated to a block boundary, the contents of the
188 	 * partial block following the end of the file must be
189 	 * zero'ed in case it ever become accessible again because
190 	 * of subsequent file growth.
191 	 */
192 	/* I don't understand the comment above */
193 	offset = blkoff(fs, length);
194 	if (offset == 0) {
195 		oip->i_size = length;
196 	} else {
197 		lbn = lblkno(fs, length);
198 		flags |= BA_CLRBUF;
199 		error = ext2_balloc(oip, lbn, offset, cred, &bp, flags);
200 		if (error)
201 			return (error);
202 		oip->i_size = length;
203 		size = blksize(fs, oip, lbn);
204 		bzero((char *)bp->b_data + offset, (u_int)(size - offset));
205 		allocbuf(bp, size);
206 		if (bp->b_bufsize == fs->e2fs_bsize)
207 			bp->b_flags |= B_CLUSTEROK;
208 		if (flags & IO_SYNC)
209 			bwrite(bp);
210 		else if (DOINGASYNC(ovp))
211 			bdwrite(bp);
212 		else
213 			bawrite(bp);
214 	}
215 	/*
216 	 * Calculate index into inode's block list of
217 	 * last direct and indirect blocks (if any)
218 	 * which we want to keep.  Lastblock is -1 when
219 	 * the file is truncated to 0.
220 	 */
221 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
222 	lastiblock[SINGLE] = lastblock - EXT2_NDADDR;
223 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
224 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
225 	nblocks = btodb(fs->e2fs_bsize);
226 	/*
227 	 * Update file and block pointers on disk before we start freeing
228 	 * blocks.  If we crash before free'ing blocks below, the blocks
229 	 * will be returned to the free list.  lastiblock values are also
230 	 * normalized to -1 for calls to ext2_indirtrunc below.
231 	 */
232 	for (level = TRIPLE; level >= SINGLE; level--) {
233 		oldblks[EXT2_NDADDR + level] = oip->i_ib[level];
234 		if (lastiblock[level] < 0) {
235 			oip->i_ib[level] = 0;
236 			lastiblock[level] = -1;
237 		}
238 	}
239 	for (i = 0; i < EXT2_NDADDR; i++) {
240 		oldblks[i] = oip->i_db[i];
241 		if (i > lastblock)
242 			oip->i_db[i] = 0;
243 	}
244 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
245 	allerror = ext2_update(ovp, !DOINGASYNC(ovp));
246 
247 	/*
248 	 * Having written the new inode to disk, save its new configuration
249 	 * and put back the old block pointers long enough to process them.
250 	 * Note that we save the new block configuration so we can check it
251 	 * when we are done.
252 	 */
253 	for (i = 0; i < EXT2_NDADDR; i++) {
254 		newblks[i] = oip->i_db[i];
255 		oip->i_db[i] = oldblks[i];
256 	}
257 	for (i = 0; i < EXT2_NIADDR; i++) {
258 		newblks[EXT2_NDADDR + i] = oip->i_ib[i];
259 		oip->i_ib[i] = oldblks[EXT2_NDADDR + i];
260 	}
261 	oip->i_size = osize;
262 	error = vtruncbuf(ovp, cred, length, (int)fs->e2fs_bsize);
263 	if (error && (allerror == 0))
264 		allerror = error;
265 	vnode_pager_setsize(ovp, length);
266 
267 	/*
268 	 * Indirect blocks first.
269 	 */
270 	indir_lbn[SINGLE] = -EXT2_NDADDR;
271 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
272 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
273 	for (level = TRIPLE; level >= SINGLE; level--) {
274 		bn = oip->i_ib[level];
275 		if (bn != 0) {
276 			error = ext2_indirtrunc(oip, indir_lbn[level],
277 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
278 			if (error)
279 				allerror = error;
280 			blocksreleased += count;
281 			if (lastiblock[level] < 0) {
282 				oip->i_ib[level] = 0;
283 				ext2_blkfree(oip, bn, fs->e2fs_fsize);
284 				blocksreleased += nblocks;
285 			}
286 		}
287 		if (lastiblock[level] >= 0)
288 			goto done;
289 	}
290 
291 	/*
292 	 * All whole direct blocks or frags.
293 	 */
294 	for (i = EXT2_NDADDR - 1; i > lastblock; i--) {
295 		long bsize;
296 
297 		bn = oip->i_db[i];
298 		if (bn == 0)
299 			continue;
300 		oip->i_db[i] = 0;
301 		bsize = blksize(fs, oip, i);
302 		ext2_blkfree(oip, bn, bsize);
303 		blocksreleased += btodb(bsize);
304 	}
305 	if (lastblock < 0)
306 		goto done;
307 
308 	/*
309 	 * Finally, look for a change in size of the
310 	 * last direct block; release any frags.
311 	 */
312 	bn = oip->i_db[lastblock];
313 	if (bn != 0) {
314 		long oldspace, newspace;
315 
316 		/*
317 		 * Calculate amount of space we're giving
318 		 * back as old block size minus new block size.
319 		 */
320 		oldspace = blksize(fs, oip, lastblock);
321 		oip->i_size = length;
322 		newspace = blksize(fs, oip, lastblock);
323 		if (newspace == 0)
324 			panic("ext2_truncate: newspace");
325 		if (oldspace - newspace > 0) {
326 			/*
327 			 * Block number of space to be free'd is
328 			 * the old block # plus the number of frags
329 			 * required for the storage we're keeping.
330 			 */
331 			bn += numfrags(fs, newspace);
332 			ext2_blkfree(oip, bn, oldspace - newspace);
333 			blocksreleased += btodb(oldspace - newspace);
334 		}
335 	}
336 done:
337 #ifdef INVARIANTS
338 	for (level = SINGLE; level <= TRIPLE; level++)
339 		if (newblks[EXT2_NDADDR + level] != oip->i_ib[level])
340 			panic("itrunc1");
341 	for (i = 0; i < EXT2_NDADDR; i++)
342 		if (newblks[i] != oip->i_db[i])
343 			panic("itrunc2");
344 	BO_LOCK(bo);
345 	if (length == 0 && (bo->bo_dirty.bv_cnt != 0 ||
346 	    bo->bo_clean.bv_cnt != 0))
347 		panic("itrunc3");
348 	BO_UNLOCK(bo);
349 #endif	/* INVARIANTS */
350 	/*
351 	 * Put back the real size.
352 	 */
353 	oip->i_size = length;
354 	if (oip->i_blocks >= blocksreleased)
355 		oip->i_blocks -= blocksreleased;
356 	else				/* sanity */
357 		oip->i_blocks = 0;
358 	oip->i_flag |= IN_CHANGE;
359 	vnode_pager_setsize(ovp, length);
360 	return (allerror);
361 }
362 
363 /*
364  * Release blocks associated with the inode ip and stored in the indirect
365  * block bn.  Blocks are free'd in LIFO order up to (but not including)
366  * lastbn.  If level is greater than SINGLE, the block is an indirect block
367  * and recursive calls to indirtrunc must be used to cleanse other indirect
368  * blocks.
369  *
370  * NB: triple indirect blocks are untested.
371  */
372 
373 static int
374 ext2_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn,
375     daddr_t lastbn, int level, e4fs_daddr_t *countp)
376 {
377 	struct buf *bp;
378 	struct m_ext2fs *fs = ip->i_e2fs;
379 	struct vnode *vp;
380 	e2fs_daddr_t *bap, *copy;
381 	int i, nblocks, error = 0, allerror = 0;
382 	e2fs_lbn_t nb, nlbn, last;
383 	e4fs_daddr_t blkcount, factor, blocksreleased = 0;
384 
385 	/*
386 	 * Calculate index in current block of last
387 	 * block to be kept.  -1 indicates the entire
388 	 * block so we need not calculate the index.
389 	 */
390 	factor = 1;
391 	for (i = SINGLE; i < level; i++)
392 		factor *= NINDIR(fs);
393 	last = lastbn;
394 	if (lastbn > 0)
395 		last /= factor;
396 	nblocks = btodb(fs->e2fs_bsize);
397 	/*
398 	 * Get buffer of block pointers, zero those entries corresponding
399 	 * to blocks to be free'd, and update on disk copy first.  Since
400 	 * double(triple) indirect before single(double) indirect, calls
401 	 * to bmap on these blocks will fail.  However, we already have
402 	 * the on disk address, so we have to set the b_blkno field
403 	 * explicitly instead of letting bread do everything for us.
404 	 */
405 	vp = ITOV(ip);
406 	bp = getblk(vp, lbn, (int)fs->e2fs_bsize, 0, 0, 0);
407 	if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {
408 		bp->b_iocmd = BIO_READ;
409 		if (bp->b_bcount > bp->b_bufsize)
410 			panic("ext2_indirtrunc: bad buffer size");
411 		bp->b_blkno = dbn;
412 		vfs_busy_pages(bp, 0);
413 		bp->b_iooffset = dbtob(bp->b_blkno);
414 		bstrategy(bp);
415 		error = bufwait(bp);
416 	}
417 	if (error) {
418 		brelse(bp);
419 		*countp = 0;
420 		return (error);
421 	}
422 	bap = (e2fs_daddr_t *)bp->b_data;
423 	copy = malloc(fs->e2fs_bsize, M_TEMP, M_WAITOK);
424 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->e2fs_bsize);
425 	bzero((caddr_t)&bap[last + 1],
426 	    (NINDIR(fs) - (last + 1)) * sizeof(e2fs_daddr_t));
427 	if (last == -1)
428 		bp->b_flags |= B_INVAL;
429 	if (DOINGASYNC(vp)) {
430 		bdwrite(bp);
431 	} else {
432 		error = bwrite(bp);
433 		if (error)
434 			allerror = error;
435 	}
436 	bap = copy;
437 
438 	/*
439 	 * Recursively free totally unused blocks.
440 	 */
441 	for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
442 	    i--, nlbn += factor) {
443 		nb = bap[i];
444 		if (nb == 0)
445 			continue;
446 		if (level > SINGLE) {
447 			if ((error = ext2_indirtrunc(ip, nlbn,
448 			    fsbtodb(fs, nb), (int32_t)-1, level - 1, &blkcount)) != 0)
449 				allerror = error;
450 			blocksreleased += blkcount;
451 		}
452 		ext2_blkfree(ip, nb, fs->e2fs_bsize);
453 		blocksreleased += nblocks;
454 	}
455 
456 	/*
457 	 * Recursively free last partial block.
458 	 */
459 	if (level > SINGLE && lastbn >= 0) {
460 		last = lastbn % factor;
461 		nb = bap[i];
462 		if (nb != 0) {
463 			if ((error = ext2_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
464 			    last, level - 1, &blkcount)) != 0)
465 				allerror = error;
466 			blocksreleased += blkcount;
467 		}
468 	}
469 	free(copy, M_TEMP);
470 	*countp = blocksreleased;
471 	return (allerror);
472 }
473 
474 /*
475  *	discard preallocated blocks
476  */
477 int
478 ext2_inactive(struct vop_inactive_args *ap)
479 {
480 	struct vnode *vp = ap->a_vp;
481 	struct inode *ip = VTOI(vp);
482 	struct thread *td = ap->a_td;
483 	int mode, error = 0;
484 
485 	/*
486 	 * Ignore inodes related to stale file handles.
487 	 */
488 	if (ip->i_mode == 0)
489 		goto out;
490 	if (ip->i_nlink <= 0) {
491 		error = ext2_truncate(vp, (off_t)0, 0, NOCRED, td);
492 		ip->i_rdev = 0;
493 		mode = ip->i_mode;
494 		ip->i_mode = 0;
495 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
496 		ext2_vfree(vp, ip->i_number, mode);
497 	}
498 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE))
499 		ext2_update(vp, 0);
500 out:
501 	/*
502 	 * If we are done with the inode, reclaim it
503 	 * so that it can be reused immediately.
504 	 */
505 	if (ip->i_mode == 0)
506 		vrecycle(vp);
507 	return (error);
508 }
509 
510 /*
511  * Reclaim an inode so that it can be used for other purposes.
512  */
513 int
514 ext2_reclaim(struct vop_reclaim_args *ap)
515 {
516 	struct inode *ip;
517 	struct vnode *vp = ap->a_vp;
518 
519 	ip = VTOI(vp);
520 	if (ip->i_flag & IN_LAZYMOD) {
521 		ip->i_flag |= IN_MODIFIED;
522 		ext2_update(vp, 0);
523 	}
524 	vfs_hash_remove(vp);
525 	free(vp->v_data, M_EXT2NODE);
526 	vp->v_data = 0;
527 	vnode_destroy_vobject(vp);
528 	return (0);
529 }
530