xref: /csrg-svn/sys/ufs/ffs/ffs_inode.c (revision 51472)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ffs_inode.c	7.41 (Berkeley) 11/01/91
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/mount.h>
13 #include <sys/proc.h>
14 #include <sys/file.h>
15 #include <sys/buf.h>
16 #include <sys/vnode.h>
17 #include <sys/kernel.h>
18 #include <sys/malloc.h>
19 
20 #include <ufs/ufs/quota.h>
21 #include <ufs/ufs/inode.h>
22 #include <ufs/ufs/ufsmount.h>
23 #include <ufs/ufs/ufs_extern.h>
24 
25 #include <ufs/ffs/fs.h>
26 #include <ufs/ffs/ffs_extern.h>
27 
28 static int ffs_indirtrunc __P((struct inode *, daddr_t, daddr_t, int, long *));
29 
30 extern u_long nextgennumber;
31 
32 int
33 ffs_init()
34 {
35 	return (ufs_init());
36 }
37 
38 /*
39  * Look up a UFS dinode number to find its incore vnode.
40  * If it is not in core, read it in from the specified device.
41  * If it is in core, wait for the lock bit to clear, then
42  * return the inode locked. Detection and handling of mount
43  * points must be done by the calling routine.
44  */
45 ffs_iget(pip, ino, ipp)
46 	struct inode *pip;
47 	ino_t ino;
48 	struct inode **ipp;
49 {
50 	extern struct vnodeops ffs_vnodeops;
51 	register struct fs *fs;
52 	register struct inode *ip;
53 	struct buf *bp;
54 	struct dinode *dp;
55 	struct mount *mntp;
56 	struct vnode *vp;
57 	union ihead *ih;
58 	dev_t dev;
59 	int i, error;
60 
61 	mntp = ITOV(pip)->v_mount;
62 	fs = VFSTOUFS(mntp)->um_fs;
63 	if (ino < ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg)
64 		return (EINVAL);
65 
66 	dev = pip->i_dev;
67 	if ((*ipp = ufs_ihashget(dev, ino)) != NULL)
68 		return (0);
69 
70 	/* Allocate a new vnode/inode. */
71 	if (error = getnewvnode(VT_UFS, mntp, &ffs_vnodeops, &vp)) {
72 		*ipp = NULL;
73 		return (error);
74 	}
75 	ip = VTOI(vp);
76 	ip->i_vnode = vp;
77 	ip->i_flag = 0;
78 	ip->i_devvp = 0;
79 	ip->i_mode = 0;
80 	ip->i_diroff = 0;
81 	ip->i_lockf = 0;
82 #ifdef QUOTA
83 	for (i = 0; i < MAXQUOTAS; i++)
84 		ip->i_dquot[i] = NODQUOT;
85 #endif
86 	/*
87 	 * Put it onto its hash chain and lock it so that other requests for
88 	 * this inode will block if they arrive while we are sleeping waiting
89 	 * for old data structures to be purged or for the contents of the
90 	 * disk portion of this inode to be read.
91 	 */
92 	ip->i_dev = dev;
93 	ip->i_number = ino;
94 	ip->i_fs = fs;					/* XXX KIRK?? */
95 	ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
96 	ufs_ihashins(ip);
97 
98 	/* Read in the disk contents for the inode, copy into the inode. */
99 	if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
100 	    (int)fs->fs_bsize, NOCRED, &bp)) {
101 		/*
102 		 * The inode does not contain anything useful, so it would
103 		 * be misleading to leave it on its hash chain.  Iput() will
104 		 * return it to the free list.
105 		 */
106 		remque(ip);
107 		ip->i_forw = ip;
108 		ip->i_back = ip;
109 
110 		/* Unlock and discard unneeded inode. */
111 		ufs_iput(ip);
112 		brelse(bp);
113 		*ipp = NULL;
114 		return (error);
115 	}
116 	dp = bp->b_un.b_dino;
117 	dp += itoo(fs, ino);
118 	ip->i_din = *dp;
119 	brelse(bp);
120 
121 	/*
122 	 * Initialize the vnode from the inode, check for aliases.  In all
123 	 * cases re-init ip, the underlying vnode/inode may have changed.
124 	 */
125 	if (error = ufs_vinit(mntp, &vp)) {
126 		ufs_iput(ip);
127 		*ipp = NULL;
128 		return (error);
129 	}
130 	ip = VTOI(vp);
131 
132 	/*
133 	 * Set up a generation number for this inode if it does not
134 	 * already have one. This should only happen on old filesystems.
135 	 */
136 	if (ip->i_gen == 0) {
137 		if (++nextgennumber < (u_long)time.tv_sec)
138 			nextgennumber = time.tv_sec;
139 		ip->i_gen = nextgennumber;
140 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
141 			ip->i_flag |= IMOD;
142 	}
143 	*ipp = ip;
144 	return (0);
145 }
146 
147 /*
148  * Update the access, modified, and inode change times as specified
149  * by the IACC, IMOD, and ICHG flags respectively. The IUPD flag
150  * is used to specify that the inode needs to be updated but that
151  * the times have already been set. The access and modified times
152  * are taken from the second and third parameters; the inode change
153  * time is always taken from the current time. If waitfor is set,
154  * then wait for the disk write of the inode to complete.
155  */
156 int
157 ffs_iupdat(ip, ta, tm, waitfor)
158 	register struct inode *ip;
159 	struct timeval *ta, *tm;
160 	int waitfor;
161 {
162 	struct buf *bp;
163 	struct vnode *vp = ITOV(ip);
164 	struct dinode *dp;
165 	register struct fs *fs;
166 	int error;
167 
168 	fs = ip->i_fs;
169 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
170 		return (0);
171 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
172 		return (0);
173 	error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
174 		(int)fs->fs_bsize, NOCRED, &bp);
175 	if (error) {
176 		brelse(bp);
177 		return (error);
178 	}
179 	if (ip->i_flag&IACC)
180 		ip->i_atime = ta->tv_sec;
181 	if (ip->i_flag&IUPD)
182 		ip->i_mtime = tm->tv_sec;
183 	if (ip->i_flag&ICHG)
184 		ip->i_ctime = time.tv_sec;
185 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
186 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
187 	*dp = ip->i_din;
188 	if (waitfor) {
189 		return (bwrite(bp));
190 	} else {
191 		bdwrite(bp);
192 		return (0);
193 	}
194 }
195 
196 #define	SINGLE	0	/* index of single indirect block */
197 #define	DOUBLE	1	/* index of double indirect block */
198 #define	TRIPLE	2	/* index of triple indirect block */
199 /*
200  * Truncate the inode ip to at most length size.  Free affected disk
201  * blocks -- the blocks of the file are removed in reverse order.
202  *
203  * NB: triple indirect blocks are untested.
204  */
205 ffs_itrunc(oip, length, flags)
206 	register struct inode *oip;
207 	u_long length;
208 	int flags;
209 {
210 	register daddr_t lastblock;
211 	daddr_t bn, lbn, lastiblock[NIADDR];
212 	register struct fs *fs;
213 	register struct inode *ip;
214 	struct buf *bp;
215 	int offset, osize, size, level;
216 	long count, nblocks, blocksreleased = 0;
217 	register int i;
218 	int aflags, error, allerror;
219 	struct inode tip;
220 
221 	vnode_pager_setsize(ITOV(oip), length);
222 	if (oip->i_size <= length) {
223 		oip->i_flag |= ICHG|IUPD;
224 		error = ffs_iupdat(oip, &time, &time, 1);
225 		return (error);
226 	}
227 	/*
228 	 * Calculate index into inode's block list of
229 	 * last direct and indirect blocks (if any)
230 	 * which we want to keep.  Lastblock is -1 when
231 	 * the file is truncated to 0.
232 	 */
233 	fs = oip->i_fs;
234 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
235 	lastiblock[SINGLE] = lastblock - NDADDR;
236 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
237 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
238 	nblocks = btodb(fs->fs_bsize);
239 	/*
240 	 * Update the size of the file. If the file is not being
241 	 * truncated to a block boundry, the contents of the
242 	 * partial block following the end of the file must be
243 	 * zero'ed in case it ever become accessable again because
244 	 * of subsequent file growth.
245 	 */
246 	osize = oip->i_size;
247 	offset = blkoff(fs, length);
248 	if (offset == 0) {
249 		oip->i_size = length;
250 	} else {
251 		lbn = lblkno(fs, length);
252 		aflags = B_CLRBUF;
253 		if (flags & IO_SYNC)
254 			aflags |= B_SYNC;
255 #ifdef QUOTA
256 		if (error = getinoquota(oip))
257 			return (error);
258 #endif
259 		if (error = ffs_balloc(oip, lbn, offset, &bp, aflags))
260 			return (error);
261 		oip->i_size = length;
262 		size = blksize(fs, oip, lbn);
263 		(void) vnode_pager_uncache(ITOV(oip));
264 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
265 		allocbuf(bp, size);
266 		if (flags & IO_SYNC)
267 			bwrite(bp);
268 		else
269 			bdwrite(bp);
270 	}
271 	/*
272 	 * Update file and block pointers on disk before we start freeing
273 	 * blocks.  If we crash before free'ing blocks below, the blocks
274 	 * will be returned to the free list.  lastiblock values are also
275 	 * normalized to -1 for calls to ffs_indirtrunc below.
276 	 */
277 	tip = *oip;
278 	tip.i_size = osize;
279 	for (level = TRIPLE; level >= SINGLE; level--)
280 		if (lastiblock[level] < 0) {
281 			oip->i_ib[level] = 0;
282 			lastiblock[level] = -1;
283 		}
284 	for (i = NDADDR - 1; i > lastblock; i--)
285 		oip->i_db[i] = 0;
286 	oip->i_flag |= ICHG|IUPD;
287 	vinvalbuf(ITOV(oip), (length > 0));
288 	allerror = ffs_iupdat(oip, &time, &time, MNT_WAIT);
289 
290 	/*
291 	 * Indirect blocks first.
292 	 */
293 	ip = &tip;
294 	for (level = TRIPLE; level >= SINGLE; level--) {
295 		bn = ip->i_ib[level];
296 		if (bn != 0) {
297 			error = ffs_indirtrunc(ip,
298 			    bn, lastiblock[level], level, &count);
299 			if (error)
300 				allerror = error;
301 			blocksreleased += count;
302 			if (lastiblock[level] < 0) {
303 				ip->i_ib[level] = 0;
304 				ffs_blkfree(ip, bn, (off_t)fs->fs_bsize);
305 				blocksreleased += nblocks;
306 			}
307 		}
308 		if (lastiblock[level] >= 0)
309 			goto done;
310 	}
311 
312 	/*
313 	 * All whole direct blocks or frags.
314 	 */
315 	for (i = NDADDR - 1; i > lastblock; i--) {
316 		register off_t bsize;
317 
318 		bn = ip->i_db[i];
319 		if (bn == 0)
320 			continue;
321 		ip->i_db[i] = 0;
322 		bsize = (off_t)blksize(fs, ip, i);
323 		ffs_blkfree(ip, bn, bsize);
324 		blocksreleased += btodb(bsize);
325 	}
326 	if (lastblock < 0)
327 		goto done;
328 
329 	/*
330 	 * Finally, look for a change in size of the
331 	 * last direct block; release any frags.
332 	 */
333 	bn = ip->i_db[lastblock];
334 	if (bn != 0) {
335 		off_t oldspace, newspace;
336 
337 		/*
338 		 * Calculate amount of space we're giving
339 		 * back as old block size minus new block size.
340 		 */
341 		oldspace = blksize(fs, ip, lastblock);
342 		ip->i_size = length;
343 		newspace = blksize(fs, ip, lastblock);
344 		if (newspace == 0)
345 			panic("itrunc: newspace");
346 		if (oldspace - newspace > 0) {
347 			/*
348 			 * Block number of space to be free'd is
349 			 * the old block # plus the number of frags
350 			 * required for the storage we're keeping.
351 			 */
352 			bn += numfrags(fs, newspace);
353 			ffs_blkfree(ip, bn, oldspace - newspace);
354 			blocksreleased += btodb(oldspace - newspace);
355 		}
356 	}
357 done:
358 /* BEGIN PARANOIA */
359 	for (level = SINGLE; level <= TRIPLE; level++)
360 		if (ip->i_ib[level] != oip->i_ib[level])
361 			panic("itrunc1");
362 	for (i = 0; i < NDADDR; i++)
363 		if (ip->i_db[i] != oip->i_db[i])
364 			panic("itrunc2");
365 /* END PARANOIA */
366 	oip->i_blocks -= blocksreleased;
367 	if (oip->i_blocks < 0)			/* sanity */
368 		oip->i_blocks = 0;
369 	oip->i_flag |= ICHG;
370 #ifdef QUOTA
371 	if (!getinoquota(oip))
372 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
373 #endif
374 	return (allerror);
375 }
376 
377 /*
378  * Release blocks associated with the inode ip and stored in the indirect
379  * block bn.  Blocks are free'd in LIFO order up to (but not including)
380  * lastbn.  If level is greater than SINGLE, the block is an indirect block
381  * and recursive calls to indirtrunc must be used to cleanse other indirect
382  * blocks.
383  *
384  * NB: triple indirect blocks are untested.
385  */
386 static int
387 ffs_indirtrunc(ip, bn, lastbn, level, countp)
388 	register struct inode *ip;
389 	daddr_t bn, lastbn;
390 	int level;
391 	long *countp;
392 {
393 	register int i;
394 	struct buf *bp;
395 	register struct fs *fs = ip->i_fs;
396 	register daddr_t *bap;
397 	daddr_t *copy, nb, last;
398 	long blkcount, factor;
399 	int nblocks, blocksreleased = 0;
400 	int error, allerror = 0;
401 
402 	/*
403 	 * Calculate index in current block of last
404 	 * block to be kept.  -1 indicates the entire
405 	 * block so we need not calculate the index.
406 	 */
407 	factor = 1;
408 	for (i = SINGLE; i < level; i++)
409 		factor *= NINDIR(fs);
410 	last = lastbn;
411 	if (lastbn > 0)
412 		last /= factor;
413 	nblocks = btodb(fs->fs_bsize);
414 	/*
415 	 * Get buffer of block pointers, zero those
416 	 * entries corresponding to blocks to be free'd,
417 	 * and update on disk copy first.
418 	 */
419 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
420 		NOCRED, &bp);
421 	if (error) {
422 		brelse(bp);
423 		*countp = 0;
424 		return (error);
425 	}
426 	bap = bp->b_un.b_daddr;
427 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
428 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
429 	bzero((caddr_t)&bap[last + 1],
430 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
431 	if (last == -1)
432 		bp->b_flags |= B_INVAL;
433 	error = bwrite(bp);
434 	if (error)
435 		allerror = error;
436 	bap = copy;
437 
438 	/*
439 	 * Recursively free totally unused blocks.
440 	 */
441 	for (i = NINDIR(fs) - 1; i > last; i--) {
442 		nb = bap[i];
443 		if (nb == 0)
444 			continue;
445 		if (level > SINGLE) {
446 			if (error = ffs_indirtrunc(ip,
447 			    nb, (daddr_t)-1, level - 1, &blkcount))
448 				allerror = error;
449 			blocksreleased += blkcount;
450 		}
451 		ffs_blkfree(ip, nb, (off_t)fs->fs_bsize);
452 		blocksreleased += nblocks;
453 	}
454 
455 	/*
456 	 * Recursively free last partial block.
457 	 */
458 	if (level > SINGLE && lastbn >= 0) {
459 		last = lastbn % factor;
460 		nb = bap[i];
461 		if (nb != 0) {
462 			if (error =
463 			    ffs_indirtrunc(ip, nb, last, level - 1, &blkcount))
464 				allerror = error;
465 			blocksreleased += blkcount;
466 		}
467 	}
468 	FREE(copy, M_TEMP);
469 	*countp = blocksreleased;
470 	return (allerror);
471 }
472