xref: /csrg-svn/sys/ufs/ffs/ffs_inode.c (revision 54460)
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.57 (Berkeley) 06/25/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 (ap)
48 	struct vop_vget_args *ap;
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(ap->a_mp);
61 	dev = ump->um_dev;
62 	if ((*ap->a_vpp = ufs_ihashget(dev, ap->a_ino)) != NULL)
63 		return (0);
64 
65 	/* Allocate a new vnode/inode. */
66 	if (error = getnewvnode(VT_UFS, ap->a_mp, ffs_vnodeop_p, &vp)) {
67 		*ap->a_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_fs = fs = ump->um_fs;
80 	ip->i_dev = dev;
81 	ip->i_number = ap->a_ino;
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 	ufs_ihashins(ip);
93 
94 	/* Read in the disk contents for the inode, copy into the inode. */
95 	if (error = bread(ump->um_devvp, fsbtodb(fs, itod(fs, ap->a_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 		*ap->a_vpp = NULL;
110 		return (error);
111 	}
112 	dp = bp->b_un.b_dino;
113 	dp += itoo(fs, ap->a_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(ap->a_mp, ffs_specop_p, FFS_FIFOOPS, &vp)) {
122 		ufs_iput(ip);
123 		*ap->a_vpp = NULL;
124 		return (error);
125 	}
126 	/*
127 	 * Finish inode initialization now that aliasing has been resolved.
128 	 */
129 	ip->i_devvp = ump->um_devvp;
130 	VREF(ip->i_devvp);
131 	/*
132 	 * Set up a generation number for this inode if it does not
133 	 * already have one. This should only happen on old filesystems.
134 	 */
135 	if (ip->i_gen == 0) {
136 		if (++nextgennumber < (u_long)time.tv_sec)
137 			nextgennumber = time.tv_sec;
138 		ip->i_gen = nextgennumber;
139 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
140 			ip->i_flag |= IMOD;
141 	}
142 	/*
143 	 * Ensure that uid and gid are correct. This is a temporary
144 	 * fix until fsck has been changed to do the update.
145 	 */
146 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
147 		ip->i_uid = ip->i_din.di_ouid;		/* XXX */
148 		ip->i_gid = ip->i_din.di_ogid;		/* XXX */
149 	}						/* XXX */
150 
151 	*ap->a_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 (ap)
166 	struct vop_update_args *ap;
167 {
168 	struct buf *bp;
169 	struct inode *ip;
170 	struct dinode *dp;
171 	register struct fs *fs;
172 	int error;
173 
174 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
175 		return (0);
176 	ip = VTOI(ap->a_vp);
177 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
178 		return (0);
179 	if (ip->i_flag&IACC)
180 		ip->i_atime.ts_sec = ap->a_ta->tv_sec;
181 	if (ip->i_flag&IUPD) {
182 		ip->i_mtime.ts_sec = ap->a_tm->tv_sec;
183 		ip->i_modrev++;
184 	}
185 	if (ip->i_flag&ICHG)
186 		ip->i_ctime.ts_sec = time.tv_sec;
187 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
188 	fs = ip->i_fs;
189 	/*
190 	 * Ensure that uid and gid are correct. This is a temporary
191 	 * fix until fsck has been changed to do the update.
192 	 */
193 	if (fs->fs_inodefmt < FS_44INODEFMT) {		/* XXX */
194 		ip->i_din.di_ouid = ip->i_uid;		/* XXX */
195 		ip->i_din.di_ogid = ip->i_gid;		/* XXX */
196 	}						/* XXX */
197 	if (error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
198 		(int)fs->fs_bsize, NOCRED, &bp)) {
199 		brelse(bp);
200 		return (error);
201 	}
202 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
203 	*dp = ip->i_din;
204 	if (ap->a_waitfor)
205 		return (bwrite(bp));
206 	else {
207 		bdwrite(bp);
208 		return (0);
209 	}
210 }
211 
212 #define	SINGLE	0	/* index of single indirect block */
213 #define	DOUBLE	1	/* index of double indirect block */
214 #define	TRIPLE	2	/* index of triple indirect block */
215 /*
216  * Truncate the inode ip to at most length size.  Free affected disk
217  * blocks -- the blocks of the file are removed in reverse order.
218  *
219  * NB: triple indirect blocks are untested.
220  */
221 ffs_truncate (ap)
222 	struct vop_truncate_args *ap;
223 {
224 	USES_VOP_UPDATE;
225 	register struct vnode *ovp = ap->a_vp;
226 	register daddr_t lastblock;
227 	register struct inode *oip;
228 	daddr_t bn, lbn, lastiblock[NIADDR];
229 	register struct fs *fs;
230 	register struct inode *ip;
231 	struct buf *bp;
232 	int offset, size, level;
233 	long count, nblocks, blocksreleased = 0;
234 	register int i;
235 	int aflags, error, allerror;
236 	struct inode tip;
237 	off_t osize;
238 
239 	vnode_pager_setsize(ovp, (u_long)ap->a_length);
240 	oip = VTOI(ovp);
241 	if (oip->i_size <= ap->a_length) {
242 		oip->i_flag |= ICHG|IUPD;
243 		error = VOP_UPDATE(ovp, &time, &time, 1);
244 		return (error);
245 	}
246 	/*
247 	 * Calculate index into inode's block list of
248 	 * last direct and indirect blocks (if any)
249 	 * which we want to keep.  Lastblock is -1 when
250 	 * the file is truncated to 0.
251 	 */
252 	fs = oip->i_fs;
253 	lastblock = lblkno(fs, ap->a_length + fs->fs_bsize - 1) - 1;
254 	lastiblock[SINGLE] = lastblock - NDADDR;
255 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
256 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
257 	nblocks = btodb(fs->fs_bsize);
258 	/*
259 	 * Update the size of the file. If the file is not being
260 	 * truncated to a block boundry, the contents of the
261 	 * partial block following the end of the file must be
262 	 * zero'ed in case it ever become accessable again because
263 	 * of subsequent file growth.
264 	 */
265 	osize = oip->i_size;
266 	offset = blkoff(fs, ap->a_length);
267 	if (offset == 0) {
268 		oip->i_size = ap->a_length;
269 	} else {
270 		lbn = lblkno(fs, ap->a_length);
271 		aflags = B_CLRBUF;
272 		if (ap->a_flags & IO_SYNC)
273 			aflags |= B_SYNC;
274 #ifdef QUOTA
275 		if (error = getinoquota(oip))
276 			return (error);
277 #endif
278 		if (error = ffs_balloc(oip, lbn, offset, ap->a_cred, &bp, aflags))
279 			return (error);
280 		oip->i_size = ap->a_length;
281 		size = blksize(fs, oip, lbn);
282 		(void) vnode_pager_uncache(ovp);
283 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
284 		allocbuf(bp, size);
285 		if (ap->a_flags & IO_SYNC)
286 			bwrite(bp);
287 		else
288 			bdwrite(bp);
289 	}
290 	/*
291 	 * Update file and block pointers on disk before we start freeing
292 	 * blocks.  If we crash before free'ing blocks below, the blocks
293 	 * will be returned to the free list.  lastiblock values are also
294 	 * normalized to -1 for calls to ffs_indirtrunc below.
295 	 */
296 	tip = *oip;
297 	tip.i_size = osize;
298 	for (level = TRIPLE; level >= SINGLE; level--)
299 		if (lastiblock[level] < 0) {
300 			oip->i_ib[level] = 0;
301 			lastiblock[level] = -1;
302 		}
303 	for (i = NDADDR - 1; i > lastblock; i--)
304 		oip->i_db[i] = 0;
305 	oip->i_flag |= ICHG|IUPD;
306 	allerror = vinvalbuf(ovp, ap->a_length > 0, ap->a_cred, ap->a_p);
307 	if (error = VOP_UPDATE(ovp, &time, &time, MNT_WAIT))
308 		allerror = error;
309 
310 	/*
311 	 * Indirect blocks first.
312 	 */
313 	ip = &tip;
314 	for (level = TRIPLE; level >= SINGLE; level--) {
315 		bn = ip->i_ib[level];
316 		if (bn != 0) {
317 			error = ffs_indirtrunc(ip,
318 			    bn, lastiblock[level], level, &count);
319 			if (error)
320 				allerror = error;
321 			blocksreleased += count;
322 			if (lastiblock[level] < 0) {
323 				ip->i_ib[level] = 0;
324 				ffs_blkfree(ip, bn, fs->fs_bsize);
325 				blocksreleased += nblocks;
326 			}
327 		}
328 		if (lastiblock[level] >= 0)
329 			goto done;
330 	}
331 
332 	/*
333 	 * All whole direct blocks or frags.
334 	 */
335 	for (i = NDADDR - 1; i > lastblock; i--) {
336 		register long bsize;
337 
338 		bn = ip->i_db[i];
339 		if (bn == 0)
340 			continue;
341 		ip->i_db[i] = 0;
342 		bsize = blksize(fs, ip, i);
343 		ffs_blkfree(ip, bn, bsize);
344 		blocksreleased += btodb(bsize);
345 	}
346 	if (lastblock < 0)
347 		goto done;
348 
349 	/*
350 	 * Finally, look for a change in size of the
351 	 * last direct block; release any frags.
352 	 */
353 	bn = ip->i_db[lastblock];
354 	if (bn != 0) {
355 		long oldspace, newspace;
356 
357 		/*
358 		 * Calculate amount of space we're giving
359 		 * back as old block size minus new block size.
360 		 */
361 		oldspace = blksize(fs, ip, lastblock);
362 		ip->i_size = ap->a_length;
363 		newspace = blksize(fs, ip, lastblock);
364 		if (newspace == 0)
365 			panic("itrunc: newspace");
366 		if (oldspace - newspace > 0) {
367 			/*
368 			 * Block number of space to be free'd is
369 			 * the old block # plus the number of frags
370 			 * required for the storage we're keeping.
371 			 */
372 			bn += numfrags(fs, newspace);
373 			ffs_blkfree(ip, bn, oldspace - newspace);
374 			blocksreleased += btodb(oldspace - newspace);
375 		}
376 	}
377 done:
378 /* BEGIN PARANOIA */
379 	for (level = SINGLE; level <= TRIPLE; level++)
380 		if (ip->i_ib[level] != oip->i_ib[level])
381 			panic("itrunc1");
382 	for (i = 0; i < NDADDR; i++)
383 		if (ip->i_db[i] != oip->i_db[i])
384 			panic("itrunc2");
385 /* END PARANOIA */
386 	oip->i_blocks -= blocksreleased;
387 	if (oip->i_blocks < 0)			/* sanity */
388 		oip->i_blocks = 0;
389 	oip->i_flag |= ICHG;
390 #ifdef QUOTA
391 	if (!getinoquota(oip))
392 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
393 #endif
394 	return (allerror);
395 }
396 
397 /*
398  * Release blocks associated with the inode ip and stored in the indirect
399  * block bn.  Blocks are free'd in LIFO order up to (but not including)
400  * lastbn.  If level is greater than SINGLE, the block is an indirect block
401  * and recursive calls to indirtrunc must be used to cleanse other indirect
402  * blocks.
403  *
404  * NB: triple indirect blocks are untested.
405  */
406 static int
407 ffs_indirtrunc(ip, bn, lastbn, level, countp)
408 	register struct inode *ip;
409 	daddr_t bn, lastbn;
410 	int level;
411 	long *countp;
412 {
413 	register int i;
414 	struct buf *bp;
415 	register struct fs *fs = ip->i_fs;
416 	register daddr_t *bap;
417 	daddr_t *copy, nb, last;
418 	long blkcount, factor;
419 	int nblocks, blocksreleased = 0;
420 	int error, allerror = 0;
421 
422 	/*
423 	 * Calculate index in current block of last
424 	 * block to be kept.  -1 indicates the entire
425 	 * block so we need not calculate the index.
426 	 */
427 	factor = 1;
428 	for (i = SINGLE; i < level; i++)
429 		factor *= NINDIR(fs);
430 	last = lastbn;
431 	if (lastbn > 0)
432 		last /= factor;
433 	nblocks = btodb(fs->fs_bsize);
434 	/*
435 	 * Get buffer of block pointers, zero those
436 	 * entries corresponding to blocks to be free'd,
437 	 * and update on disk copy first.
438 	 */
439 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
440 		NOCRED, &bp);
441 	if (error) {
442 		brelse(bp);
443 		*countp = 0;
444 		return (error);
445 	}
446 	bap = bp->b_un.b_daddr;
447 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
448 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
449 	bzero((caddr_t)&bap[last + 1],
450 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
451 	if (last == -1)
452 		bp->b_flags |= B_INVAL;
453 	error = bwrite(bp);
454 	if (error)
455 		allerror = error;
456 	bap = copy;
457 
458 	/*
459 	 * Recursively free totally unused blocks.
460 	 */
461 	for (i = NINDIR(fs) - 1; i > last; i--) {
462 		nb = bap[i];
463 		if (nb == 0)
464 			continue;
465 		if (level > SINGLE) {
466 			if (error = ffs_indirtrunc(ip,
467 			    nb, (daddr_t)-1, level - 1, &blkcount))
468 				allerror = error;
469 			blocksreleased += blkcount;
470 		}
471 		ffs_blkfree(ip, nb, fs->fs_bsize);
472 		blocksreleased += nblocks;
473 	}
474 
475 	/*
476 	 * Recursively free last partial block.
477 	 */
478 	if (level > SINGLE && lastbn >= 0) {
479 		last = lastbn % factor;
480 		nb = bap[i];
481 		if (nb != 0) {
482 			if (error =
483 			    ffs_indirtrunc(ip, nb, last, level - 1, &blkcount))
484 				allerror = error;
485 			blocksreleased += blkcount;
486 		}
487 	}
488 	FREE(copy, M_TEMP);
489 	*countp = blocksreleased;
490 	return (allerror);
491 }
492