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