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