xref: /openbsd/sys/ufs/ext2fs/ext2fs_inode.c (revision 264ca280)
1 /*	$OpenBSD: ext2fs_inode.c,v 1.58 2016/03/19 12:04:16 natano Exp $	*/
2 /*	$NetBSD: ext2fs_inode.c,v 1.24 2001/06/19 12:59:18 wiz Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ffs_inode.c	8.8 (Berkeley) 10/19/94
34  * Modified for ext2fs by Manuel Bouyer.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/proc.h>
41 #include <sys/buf.h>
42 #include <sys/vnode.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/resourcevar.h>
46 
47 #include <ufs/ufs/quota.h>
48 #include <ufs/ufs/inode.h>
49 #include <ufs/ufs/ufsmount.h>
50 #include <ufs/ufs/ufs_extern.h>
51 
52 #include <ufs/ext2fs/ext2fs.h>
53 #include <ufs/ext2fs/ext2fs_extern.h>
54 
55 static int ext2fs_indirtrunc(struct inode *, int32_t, int32_t,
56 				int32_t, int, long *);
57 
58 /*
59  * Get the size of an inode.
60  */
61 u_int64_t
62 ext2fs_size(struct inode *ip)
63 {
64 	u_int64_t size = ip->i_e2fs_size;
65 
66 	if ((ip->i_e2fs_mode & IFMT) == IFREG)
67 		size |= (u_int64_t)ip->i_e2fs_size_hi << 32;
68 
69 	return (size);
70 }
71 
72 int
73 ext2fs_setsize(struct inode *ip, u_int64_t size)
74 {
75 	struct m_ext2fs *fs = ip->i_e2fs;
76 
77 	if (size <= fs->e2fs_maxfilesize) {
78 		/* If HUGE_FILEs are off, e2fs_maxfilesize will protect us. */
79 		if ((ip->i_e2fs_mode & IFMT) == IFREG || ip->i_e2fs_mode == 0)
80 			ip->i_e2fs_size_hi = size >> 32;
81 
82 		ip->i_e2fs_size = size;
83 		return (0);
84 	}
85 
86 	/* Linux automagically upgrades to REV1 here! */
87 	if (fs->e2fs.e2fs_rev <= E2FS_REV0)
88 		return (EFBIG);
89 
90 	if ((fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE) == 0) {
91 		fs->e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
92 		fs->e2fs_fmod = 1;
93 	}
94 	return (EFBIG);
95 }
96 
97 
98 /*
99  * Last reference to an inode.  If necessary, write or delete it.
100  */
101 int
102 ext2fs_inactive(void *v)
103 {
104 	struct vop_inactive_args *ap = v;
105 	struct vnode *vp = ap->a_vp;
106 	struct inode *ip = VTOI(vp);
107 	struct proc *p = ap->a_p;
108 	struct timespec ts;
109 	int error = 0;
110 #ifdef DIAGNOSTIC
111 	extern int prtactive;
112 
113 	if (prtactive && vp->v_usecount != 0)
114 		vprint("ext2fs_inactive: pushing active", vp);
115 #endif
116 
117 	/* Get rid of inodes related to stale file handles. */
118 	if (ip->i_e2din == NULL || ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime)
119 		goto out;
120 
121 	error = 0;
122 	if (ip->i_e2fs_nlink == 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
123 		if (ext2fs_size(ip) != 0) {
124 			error = ext2fs_truncate(ip, (off_t)0, 0, NOCRED);
125 		}
126 		getnanotime(&ts);
127 		ip->i_e2fs_dtime = ts.tv_sec;
128 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
129 		ext2fs_inode_free(ip, ip->i_number, ip->i_e2fs_mode);
130 	}
131 	if (ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) {
132 		ext2fs_update(ip, 0);
133 	}
134 out:
135 	VOP_UNLOCK(vp, p);
136 	/*
137 	 * If we are done with the inode, reclaim it
138 	 * so that it can be reused immediately.
139 	 */
140 	if (ip->i_e2din == NULL || ip->i_e2fs_dtime != 0)
141 		vrecycle(vp, p);
142 	return (error);
143 }
144 
145 
146 /*
147  * Update the access, modified, and inode change times as specified by the
148  * IACCESS, IUPDATE, and ICHANGE flags respectively. The IMODIFIED flag is
149  * used to specify that the inode needs to be updated but that the times have
150  * already been set. The access and modified times are taken from the second
151  * and third parameters; the inode change time is always taken from the current
152  * time. If waitfor is set, then wait for the disk write of the inode to
153  * complete.
154  */
155 int
156 ext2fs_update(struct inode *ip, int waitfor)
157 {
158 	struct m_ext2fs *fs;
159 	struct buf *bp;
160 	int error;
161 	caddr_t cp;
162 
163 	if (ITOV(ip)->v_mount->mnt_flag & MNT_RDONLY)
164 		return (0);
165 	EXT2FS_ITIMES(ip);
166 	if ((ip->i_flag & IN_MODIFIED) == 0)
167 		return (0);
168 	ip->i_flag &= ~IN_MODIFIED;
169 	fs = ip->i_e2fs;
170 	error = bread(ip->i_devvp,
171 			  fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
172 			  (int)fs->e2fs_bsize, &bp);
173 	if (error) {
174 		brelse(bp);
175 		return (error);
176 	}
177 	ip->i_flag &= ~(IN_MODIFIED);
178 	cp = (caddr_t)bp->b_data +
179 	    (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
180 
181 	/*
182 	 * See note about 16-bit UID/GID limitation in ext2fs_vget(). Now
183 	 * that we are about to write the inode, construct the split UID and
184 	 * GID fields out of the two 32-bit fields we kept in memory.
185 	 */
186 	ip->i_e2fs_uid_low = (u_int16_t)ip->i_e2fs_uid;
187 	ip->i_e2fs_gid_low = (u_int16_t)ip->i_e2fs_gid;
188 	ip->i_e2fs_uid_high = ip->i_e2fs_uid >> 16;
189 	ip->i_e2fs_gid_high = ip->i_e2fs_gid >> 16;
190 
191 	e2fs_isave(fs, ip->i_e2din, (struct ext2fs_dinode *)cp);
192 	if (waitfor)
193 		return (bwrite(bp));
194 	else {
195 		bdwrite(bp);
196 		return (0);
197 	}
198 }
199 
200 #define	SINGLE	0	/* index of single indirect block */
201 #define	DOUBLE	1	/* index of double indirect block */
202 #define	TRIPLE	2	/* index of triple indirect block */
203 /*
204  * Truncate the inode oip to at most length size, freeing the
205  * disk blocks.
206  */
207 int
208 ext2fs_truncate(struct inode *oip, off_t length, int flags, struct ucred *cred)
209 {
210 	struct vnode *ovp = ITOV(oip);
211 	int32_t lastblock;
212 	int32_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
213 	int32_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
214 	struct m_ext2fs *fs;
215 	struct buf *bp;
216 	int offset, size, level;
217 	long count, nblocks, vflags, blocksreleased = 0;
218 	int i;
219 	int aflags, error, allerror;
220 	off_t osize;
221 
222 	if (length < 0)
223 		return (EINVAL);
224 
225 	if (ovp->v_type != VREG &&
226 	    ovp->v_type != VDIR &&
227 	    ovp->v_type != VLNK)
228 		return (0);
229 
230 	if (ovp->v_type == VLNK && ext2fs_size(oip) < EXT2_MAXSYMLINKLEN) {
231 #ifdef DIAGNOSTIC
232 		if (length != 0)
233 			panic("ext2fs_truncate: partial truncate of symlink");
234 #endif
235 		memset(&oip->i_e2din->e2di_shortlink, 0, ext2fs_size(oip));
236 		(void)ext2fs_setsize(oip, 0);
237 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
238 		return (ext2fs_update(oip, 1));
239 	}
240 
241 	if (ext2fs_size(oip) == length) {
242 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
243 		return (ext2fs_update(oip, 0));
244 	}
245 	fs = oip->i_e2fs;
246 	osize = ext2fs_size(oip);
247 	/*
248 	 * Lengthen the size of the file. We must ensure that the
249 	 * last byte of the file is allocated. Since the smallest
250 	 * value of osize is 0, length will be at least 1.
251 	 */
252 	if (osize < length) {
253 #if 0 /* XXX */
254 		if (length > fs->fs_maxfilesize)
255 			return (EFBIG);
256 #endif
257 		offset = blkoff(fs, length - 1);
258 		lbn = lblkno(fs, length - 1);
259 		aflags = B_CLRBUF;
260 		if (flags & IO_SYNC)
261 			aflags |= B_SYNC;
262 		error = ext2fs_buf_alloc(oip, lbn, offset + 1, cred, &bp,
263 		    aflags);
264 		if (error)
265 			return (error);
266 		(void)ext2fs_setsize(oip, length);
267 		uvm_vnp_setsize(ovp, length);
268 		uvm_vnp_uncache(ovp);
269 		if (aflags & B_SYNC)
270 			bwrite(bp);
271 		else
272 			bawrite(bp);
273 		oip->i_flag |= IN_CHANGE | IN_UPDATE;
274 		return (ext2fs_update(oip, 1));
275 	}
276 	/*
277 	 * Shorten the size of the file. If the file is not being
278 	 * truncated to a block boundry, the contents of the
279 	 * partial block following the end of the file must be
280 	 * zero'ed in case it ever become accessible again because
281 	 * of subsequent file growth.
282 	 */
283 	offset = blkoff(fs, length);
284 	if (offset == 0) {
285 		(void)ext2fs_setsize(oip, length);
286 	} else {
287 		lbn = lblkno(fs, length);
288 		aflags = B_CLRBUF;
289 		if (flags & IO_SYNC)
290 			aflags |= B_SYNC;
291 		error = ext2fs_buf_alloc(oip, lbn, offset, cred, &bp,
292 		    aflags);
293 		if (error)
294 			return (error);
295 		(void)ext2fs_setsize(oip, length);
296 		size = fs->e2fs_bsize;
297 		uvm_vnp_setsize(ovp, length);
298 		uvm_vnp_uncache(ovp);
299 		memset(bp->b_data + offset, 0, size - offset);
300 		bp->b_bcount = size;
301 		if (aflags & B_SYNC)
302 			bwrite(bp);
303 		else
304 			bawrite(bp);
305 	}
306 	/*
307 	 * Calculate index into inode's block list of
308 	 * last direct and indirect blocks (if any)
309 	 * which we want to keep.  Lastblock is -1 when
310 	 * the file is truncated to 0.
311 	 */
312 	lastblock = lblkno(fs, length + fs->e2fs_bsize - 1) - 1;
313 	lastiblock[SINGLE] = lastblock - NDADDR;
314 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
315 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
316 	nblocks = btodb(fs->e2fs_bsize);
317 	/*
318 	 * Update file and block pointers on disk before we start freeing
319 	 * blocks.  If we crash before free'ing blocks below, the blocks
320 	 * will be returned to the free list.  lastiblock values are also
321 	 * normalized to -1 for calls to ext2fs_indirtrunc below.
322 	 */
323 	memcpy(oldblks, &oip->i_e2fs_blocks[0], sizeof(oldblks));
324 	for (level = TRIPLE; level >= SINGLE; level--)
325 		if (lastiblock[level] < 0) {
326 			oip->i_e2fs_blocks[NDADDR + level] = 0;
327 			lastiblock[level] = -1;
328 		}
329 	for (i = NDADDR - 1; i > lastblock; i--)
330 		oip->i_e2fs_blocks[i] = 0;
331 	oip->i_flag |= IN_CHANGE | IN_UPDATE;
332 	if ((error = ext2fs_update(oip, 1)) != 0)
333 		allerror = error;
334 	/*
335 	 * Having written the new inode to disk, save its new configuration
336 	 * and put back the old block pointers long enough to process them.
337 	 * Note that we save the new block configuration so we can check it
338 	 * when we are done.
339 	 */
340 	memcpy(newblks, &oip->i_e2fs_blocks[0], sizeof(newblks));
341 	memcpy(&oip->i_e2fs_blocks[0], oldblks, sizeof(oldblks));
342 	(void)ext2fs_setsize(oip, osize);
343 	vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
344 	allerror = vinvalbuf(ovp, vflags, cred, curproc, 0, 0);
345 
346 	/*
347 	 * Indirect blocks first.
348 	 */
349 	indir_lbn[SINGLE] = -NDADDR;
350 	indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) -1;
351 	indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
352 	for (level = TRIPLE; level >= SINGLE; level--) {
353 		bn = letoh32(oip->i_e2fs_blocks[NDADDR + level]);
354 		if (bn != 0) {
355 			error = ext2fs_indirtrunc(oip, indir_lbn[level],
356 			    fsbtodb(fs, bn), lastiblock[level], level, &count);
357 			if (error)
358 				allerror = error;
359 			blocksreleased += count;
360 			if (lastiblock[level] < 0) {
361 				oip->i_e2fs_blocks[NDADDR + level] = 0;
362 				ext2fs_blkfree(oip, bn);
363 				blocksreleased += nblocks;
364 			}
365 		}
366 		if (lastiblock[level] >= 0)
367 			goto done;
368 	}
369 
370 	/*
371 	 * All whole direct blocks or frags.
372 	 */
373 	for (i = NDADDR - 1; i > lastblock; i--) {
374 		bn = letoh32(oip->i_e2fs_blocks[i]);
375 		if (bn == 0)
376 			continue;
377 		oip->i_e2fs_blocks[i] = 0;
378 		ext2fs_blkfree(oip, bn);
379 		blocksreleased += btodb(fs->e2fs_bsize);
380 	}
381 
382 done:
383 #ifdef DIAGNOSTIC
384 	for (level = SINGLE; level <= TRIPLE; level++)
385 		if (newblks[NDADDR + level] !=
386 		    oip->i_e2fs_blocks[NDADDR + level])
387 			panic("ext2fs_truncate1");
388 	for (i = 0; i < NDADDR; i++)
389 		if (newblks[i] != oip->i_e2fs_blocks[i])
390 			panic("ext2fs_truncate2");
391 	if (length == 0 &&
392 	    (!LIST_EMPTY(&ovp->v_cleanblkhd) ||
393 	     !LIST_EMPTY(&ovp->v_dirtyblkhd)))
394 		panic("ext2fs_truncate3");
395 #endif /* DIAGNOSTIC */
396 	/*
397 	 * Put back the real size.
398 	 */
399 	(void)ext2fs_setsize(oip, length);
400 	if (blocksreleased >= oip->i_e2fs_nblock)
401 		oip->i_e2fs_nblock = 0;
402 	else
403 		oip->i_e2fs_nblock -= blocksreleased;
404 	oip->i_flag |= IN_CHANGE;
405 	return (allerror);
406 }
407 
408 /*
409  * Release blocks associated with the inode ip and stored in the indirect
410  * block bn.  Blocks are free'd in LIFO order up to (but not including)
411  * lastbn.  If level is greater than SINGLE, the block is an indirect block
412  * and recursive calls to indirtrunc must be used to cleanse other indirect
413  * blocks.
414  *
415  * NB: triple indirect blocks are untested.
416  */
417 static int
418 ext2fs_indirtrunc(struct inode *ip, int32_t lbn, int32_t dbn, int32_t lastbn, int level, long *countp)
419 {
420 	int i;
421 	struct buf *bp;
422 	struct m_ext2fs *fs = ip->i_e2fs;
423 	int32_t *bap;
424 	struct vnode *vp;
425 	int32_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 		curproc->p_ru.ru_inblock++;		/* pay for read */
454 		bcstats.pendingreads++;
455 		bcstats.numreads++;
456 		bp->b_flags |= B_READ;
457 		if (bp->b_bcount > bp->b_bufsize)
458 			panic("ext2fs_indirtrunc: bad buffer size");
459 		bp->b_blkno = dbn;
460 		VOP_STRATEGY(bp);
461 		error = biowait(bp);
462 	}
463 	if (error) {
464 		brelse(bp);
465 		*countp = 0;
466 		return (error);
467 	}
468 
469 	bap = (int32_t *)bp->b_data;
470 	if (lastbn >= 0) {
471 		copy = malloc(fs->e2fs_bsize, M_TEMP, M_WAITOK);
472 		memcpy(copy, bap, fs->e2fs_bsize);
473 		memset(&bap[last + 1], 0,
474 		    (NINDIR(fs) - (last + 1)) * sizeof(u_int32_t));
475 		error = bwrite(bp);
476 		if (error)
477 			allerror = error;
478 		bap = copy;
479 	}
480 
481 	/*
482 	 * Recursively free totally unused blocks.
483 	 */
484 	for (i = NINDIR(fs) - 1,
485 		nlbn = lbn + 1 - i * factor; i > last;
486 		i--, nlbn += factor) {
487 		nb = letoh32(bap[i]);
488 		if (nb == 0)
489 			continue;
490 		if (level > SINGLE) {
491 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
492 						   (int32_t)-1, level - 1,
493 						   &blkcount);
494 			if (error)
495 				allerror = error;
496 			blocksreleased += blkcount;
497 		}
498 		ext2fs_blkfree(ip, nb);
499 		blocksreleased += nblocks;
500 	}
501 
502 	/*
503 	 * Recursively free last partial block.
504 	 */
505 	if (level > SINGLE && lastbn >= 0) {
506 		last = lastbn % factor;
507 		nb = letoh32(bap[i]);
508 		if (nb != 0) {
509 			error = ext2fs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
510 						   last, level - 1, &blkcount);
511 			if (error)
512 				allerror = error;
513 			blocksreleased += blkcount;
514 		}
515 	}
516 
517 	if (copy != NULL) {
518 		free(copy, M_TEMP, fs->e2fs_bsize);
519 	} else {
520 		bp->b_flags |= B_INVAL;
521 		brelse(bp);
522 	}
523 
524 	*countp = blocksreleased;
525 	return (allerror);
526 }
527