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