xref: /csrg-svn/sys/ufs/ffs/ufs_inode.c (revision 16058)
1 /*	ufs_inode.c	6.3	84/02/15	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mount.h"
6 #include "../h/dir.h"
7 #include "../h/user.h"
8 #include "../h/inode.h"
9 #include "../h/fs.h"
10 #include "../h/conf.h"
11 #include "../h/buf.h"
12 #ifdef QUOTA
13 #include "../h/quota.h"
14 #endif
15 #include "../h/kernel.h"
16 
17 #define	INOHSZ	63
18 #if	((INOHSZ&(INOHSZ-1)) == 0)
19 #define	INOHASH(dev,ino)	(((dev)+(ino))&(INOHSZ-1))
20 #else
21 #define	INOHASH(dev,ino)	(((unsigned)((dev)+(ino)))%INOHSZ)
22 #endif
23 
24 union ihead {				/* inode LRU cache, Chris Maltby */
25 	union  ihead *ih_head[2];
26 	struct inode *ih_chain[2];
27 } ihead[INOHSZ];
28 
29 struct inode *ifreeh, **ifreet;
30 
31 /*
32  * Initialize hash links for inodes
33  * and build inode free list.
34  */
35 ihinit()
36 {
37 	register int i;
38 	register struct inode *ip = inode;
39 	register union  ihead *ih = ihead;
40 
41 	for (i = INOHSZ; --i >= 0; ih++) {
42 		ih->ih_head[0] = ih;
43 		ih->ih_head[1] = ih;
44 	}
45 	ifreeh = ip;
46 	ifreet = &ip->i_freef;
47 	ip->i_freeb = &ifreeh;
48 	ip->i_forw = ip;
49 	ip->i_back = ip;
50 	for (i = ninode; --i > 0; ) {
51 		++ip;
52 		ip->i_forw = ip;
53 		ip->i_back = ip;
54 		*ifreet = ip;
55 		ip->i_freeb = ifreet;
56 		ifreet = &ip->i_freef;
57 	}
58 	ip->i_freef = NULL;
59 }
60 
61 #ifdef notdef
62 /*
63  * Find an inode if it is incore.
64  * This is the equivalent, for inodes,
65  * of ``incore'' in bio.c or ``pfind'' in subr.c.
66  */
67 struct inode *
68 ifind(dev, ino)
69 	dev_t dev;
70 	ino_t ino;
71 {
72 	register struct inode *ip;
73 	register union  ihead *ih;
74 
75 	ih = &ihead[INOHASH(dev, ino)];
76 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
77 		if (ino==ip->i_number && dev==ip->i_dev)
78 			return (ip);
79 	return ((struct inode *)0);
80 }
81 #endif notdef
82 
83 /*
84  * Look up an inode by device,inumber.
85  * If it is in core (in the inode structure),
86  * honor the locking protocol.
87  * If it is not in core, read it in from the
88  * specified device.
89  * If the inode is mounted on, perform
90  * the indicated indirection.
91  * In all cases, a pointer to a locked
92  * inode structure is returned.
93  *
94  * panic: no imt -- if the mounted file
95  *	system is not in the mount table.
96  *	"cannot happen"
97  */
98 struct inode *
99 iget(dev, fs, ino)
100 	dev_t dev;
101 	register struct fs *fs;
102 	ino_t ino;
103 {
104 	register struct inode *ip;
105 	register union  ihead *ih;
106 	register struct mount *mp;
107 	register struct buf *bp;
108 	register struct dinode *dp;
109 	register struct inode *iq;
110 
111 loop:
112 	if (getfs(dev) != fs)
113 		panic("iget: bad fs");
114 	ih = &ihead[INOHASH(dev, ino)];
115 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
116 		if (ino == ip->i_number && dev == ip->i_dev) {
117 			if ((ip->i_flag&ILOCKED) != 0) {
118 				ip->i_flag |= IWANT;
119 				sleep((caddr_t)ip, PINOD);
120 				goto loop;
121 			}
122 			if ((ip->i_flag&IMOUNT) != 0) {
123 				for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++)
124 					if(mp->m_inodp == ip) {
125 						dev = mp->m_dev;
126 						fs = mp->m_bufp->b_un.b_fs;
127 						ino = ROOTINO;
128 						goto loop;
129 					}
130 				panic("no imt");
131 			}
132 			if (ip->i_count == 0) {		/* ino on free list */
133 				if (iq = ip->i_freef)
134 					iq->i_freeb = ip->i_freeb;
135 				else
136 					ifreet = ip->i_freeb;
137 				*ip->i_freeb = iq;
138 				ip->i_freef = NULL;
139 				ip->i_freeb = NULL;
140 			}
141 			ip->i_count++;
142 			ip->i_flag |= ILOCKED;
143 			return(ip);
144 		}
145 
146 	if ((ip = ifreeh) == NULL) {
147 		tablefull("inode");
148 		u.u_error = ENFILE;
149 		return(NULL);
150 	}
151 	if (iq = ip->i_freef)
152 		iq->i_freeb = &ifreeh;
153 	ifreeh = iq;
154 	ip->i_freef = NULL;
155 	ip->i_freeb = NULL;
156 	/*
157 	 * Now to take inode off the hash chain it was on
158 	 * (initially, or after an iflush, it is on a "hash chain"
159 	 * consisting entirely of itself, and pointed to by no-one,
160 	 * but that doesn't matter), and put it on the chain for
161 	 * its new (ino, dev) pair
162 	 */
163 	remque(ip);
164 	insque(ip, ih);
165 #ifdef QUOTA
166 	dqrele(ip->i_dquot);
167 #endif
168 	ip->i_dev = dev;
169 	ip->i_fs = fs;
170 	ip->i_number = ino;
171 	ip->i_flag = ILOCKED;
172 	ip->i_count++;
173 	ip->i_lastr = 0;
174 	bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize);
175 	/*
176 	 * Check I/O errors
177 	 */
178 	if ((bp->b_flags&B_ERROR) != 0) {
179 		brelse(bp);
180 		/*
181 		 * the inode doesn't contain anything useful, so it would
182 		 * be misleading to leave it on its hash chain.
183 		 * 'iput' will take care of putting it back on the free list.
184 		 */
185 		remque(ip);
186 		ip->i_forw = ip;
187 		ip->i_back = ip;
188 		/*
189 		 * we also loose its inumber, just in case (as iput
190 		 * doesn't do that any more) - but as it isn't on its
191 		 * hash chain, I doubt if this is really necessary .. kre
192 		 * (probably the two methods are interchangable)
193 		 */
194 		ip->i_number = 0;
195 #ifdef QUOTA
196 		ip->i_dquot = NODQUOT;
197 #endif
198 		iput(ip);
199 		return(NULL);
200 	}
201 	dp = bp->b_un.b_dino;
202 	dp += itoo(fs, ino);
203 	ip->i_ic = dp->di_ic;
204 	brelse(bp);
205 #ifdef QUOTA
206 	if (ip->i_mode == 0)
207 		ip->i_dquot = NODQUOT;
208 	else
209 		ip->i_dquot = inoquota(ip);
210 #endif
211 	return (ip);
212 }
213 
214 /*
215  * Decrement reference count of
216  * an inode structure.
217  * On the last reference,
218  * write the inode out and if necessary,
219  * truncate and deallocate the file.
220  */
221 iput(ip)
222 	register struct inode *ip;
223 {
224 
225 	if ((ip->i_flag & ILOCKED) == 0)
226 		panic("iput");
227 	iunlock(ip);
228 	irele(ip);
229 }
230 
231 irele(ip)
232 	register struct inode *ip;
233 {
234 	int mode;
235 
236 	if (ip->i_count == 1) {
237 		ip->i_flag |= ILOCKED;
238 		if (ip->i_nlink <= 0) {
239 			itrunc(ip, (u_long)0);
240 			mode = ip->i_mode;
241 			ip->i_mode = 0;
242 			ip->i_rdev = 0;
243 			ip->i_flag |= IUPD|ICHG;
244 			ifree(ip, ip->i_number, mode);
245 #ifdef QUOTA
246 			(void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
247 			dqrele(ip->i_dquot);
248 			ip->i_dquot = NODQUOT;
249 #endif
250 		}
251 		IUPDAT(ip, &time, &time, 0);
252 		iunlock(ip);
253 		ip->i_flag = 0;
254 		/*
255 		 * Put the inode on the end of the free list.
256 		 * Possibly in some cases it would be better to
257 		 * put the inode at the head of the free list,
258 		 * (eg: where i_mode == 0 || i_number == 0)
259 		 * but I will think about that later .. kre
260 		 * (i_number is rarely 0 - only after an i/o error in iget,
261 		 * where i_mode == 0, the inode will probably be wanted
262 		 * again soon for an ialloc, so possibly we should keep it)
263 		 */
264 		if (ifreeh) {
265 			*ifreet = ip;
266 			ip->i_freeb = ifreet;
267 		} else {
268 			ifreeh = ip;
269 			ip->i_freeb = &ifreeh;
270 		}
271 		ip->i_freef = NULL;
272 		ifreet = &ip->i_freef;
273 	} else if (!(ip->i_flag & ILOCKED))
274 		ITIMES(ip, &time, &time);
275 	ip->i_count--;
276 }
277 
278 /*
279  * Check accessed and update flags on
280  * an inode structure.
281  * If any is on, update the inode
282  * with the current time.
283  * If waitfor is given, then must insure
284  * i/o order so wait for write to complete.
285  */
286 iupdat(ip, ta, tm, waitfor)
287 	register struct inode *ip;
288 	struct timeval *ta, *tm;
289 	int waitfor;
290 {
291 	register struct buf *bp;
292 	struct dinode *dp;
293 	register struct fs *fp;
294 
295 	fp = ip->i_fs;
296 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) != 0) {
297 		if (fp->fs_ronly)
298 			return;
299 		bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
300 			(int)fp->fs_bsize);
301 		if (bp->b_flags & B_ERROR) {
302 			brelse(bp);
303 			return;
304 		}
305 		if (ip->i_flag&IACC)
306 			ip->i_atime = ta->tv_sec;
307 		if (ip->i_flag&IUPD)
308 			ip->i_mtime = tm->tv_sec;
309 		if (ip->i_flag&ICHG)
310 			ip->i_ctime = time.tv_sec;
311 		ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
312 		dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
313 		dp->di_ic = ip->i_ic;
314 		if (waitfor)
315 			bwrite(bp);
316 		else
317 			bdwrite(bp);
318 	}
319 }
320 
321 #define	SINGLE	0	/* index of single indirect block */
322 #define	DOUBLE	1	/* index of double indirect block */
323 #define	TRIPLE	2	/* index of triple indirect block */
324 /*
325  * Truncate the inode ip to at most
326  * length size.  Free affected disk
327  * blocks -- the blocks of the file
328  * are removed in reverse order.
329  *
330  * NB: triple indirect blocks are untested.
331  */
332 itrunc(oip, length)
333 	struct inode *oip;
334 	u_long length;
335 {
336 	register i;
337 	register daddr_t lastblock;
338 	daddr_t bn, lastiblock[NIADDR];
339 	register struct fs *fs;
340 	register struct inode *ip;
341 	struct inode tip;
342 	long blocksreleased = 0, nblocks;
343 	long indirtrunc();
344 	int level;
345 
346 	if (oip->i_size <= length) {
347 		oip->i_flag |= ICHG|IUPD;
348 		iupdat(oip, &time, &time, 1);
349 		return;
350 	}
351 	/*
352 	 * Calculate index into inode's block list of
353 	 * last direct and indirect blocks (if any)
354 	 * which we want to keep.  Lastblock is -1 when
355 	 * the file is truncated to 0.
356 	 */
357 	fs = oip->i_fs;
358 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
359 	lastiblock[SINGLE] = lastblock - NDADDR;
360 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
361 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
362 	nblocks = btodb(fs->fs_bsize);
363 	/*
364 	 * Update size of file and block pointers
365 	 * on disk before we start freeing blocks.
366 	 * If we crash before free'ing blocks below,
367 	 * the blocks will be returned to the free list.
368 	 * lastiblock values are also normalized to -1
369 	 * for calls to indirtrunc below.
370 	 * (? fsck doesn't check validity of pointers in indirect blocks)
371 	 */
372 	tip = *oip;
373 	for (level = TRIPLE; level >= SINGLE; level--)
374 		if (lastiblock[level] < 0) {
375 			oip->i_ib[level] = 0;
376 			lastiblock[level] = -1;
377 		}
378 	for (i = NDADDR - 1; i > lastblock; i--)
379 		oip->i_db[i] = 0;
380 	oip->i_size = length;
381 	oip->i_flag |= ICHG|IUPD;
382 	iupdat(oip, &time, &time, 1);
383 	ip = &tip;
384 
385 	/*
386 	 * Indirect blocks first.
387 	 */
388 	for (level = TRIPLE; level >= SINGLE; level--) {
389 		bn = ip->i_ib[level];
390 		if (bn != 0) {
391 			blocksreleased +=
392 			    indirtrunc(ip, bn, lastiblock[level], level);
393 			if (lastiblock[level] < 0) {
394 				ip->i_ib[level] = 0;
395 				free(ip, bn, (off_t)fs->fs_bsize);
396 				blocksreleased += nblocks;
397 			}
398 		}
399 		if (lastiblock[level] >= 0)
400 			goto done;
401 	}
402 
403 	/*
404 	 * All whole direct blocks or frags.
405 	 */
406 	for (i = NDADDR - 1; i > lastblock; i--) {
407 		register int size;
408 
409 		bn = ip->i_db[i];
410 		if (bn == 0)
411 			continue;
412 		ip->i_db[i] = 0;
413 		size = (off_t)blksize(fs, ip, i);
414 		free(ip, bn, size);
415 		blocksreleased += btodb(size);
416 	}
417 	if (lastblock < 0)
418 		goto done;
419 
420 	/*
421 	 * Finally, look for a change in size of the
422 	 * last direct block; release any frags.
423 	 */
424 	bn = ip->i_db[lastblock];
425 	if (bn != 0) {
426 		int oldspace, newspace;
427 
428 		/*
429 		 * Calculate amount of space we're giving
430 		 * back as old block size minus new block size.
431 		 */
432 		oldspace = blksize(fs, ip, lastblock);
433 		ip->i_size = length;
434 		newspace = blksize(fs, ip, lastblock);
435 		if (newspace == 0)
436 			panic("itrunc: newspace");
437 		if (oldspace - newspace > 0) {
438 			/*
439 			 * Block number of space to be free'd is
440 			 * the old block # plus the number of frags
441 			 * required for the storage we're keeping.
442 			 */
443 			bn += numfrags(fs, newspace);
444 			free(ip, bn, oldspace - newspace);
445 			blocksreleased += btodb(oldspace - newspace);
446 		}
447 	}
448 done:
449 /* BEGIN PARANOIA */
450 	for (level = SINGLE; level <= TRIPLE; level++)
451 		if (ip->i_ib[level] != oip->i_ib[level])
452 			panic("itrunc1");
453 	for (i = 0; i < NDADDR; i++)
454 		if (ip->i_db[i] != oip->i_db[i])
455 			panic("itrunc2");
456 /* END PARANOIA */
457 	oip->i_blocks -= blocksreleased;
458 	if (oip->i_blocks < 0)			/* sanity */
459 		oip->i_blocks = 0;
460 	oip->i_flag |= ICHG;
461 #ifdef QUOTA
462 	(void) chkdq(oip, -blocksreleased, 0);
463 #endif
464 }
465 
466 /*
467  * Release blocks associated with the inode ip and
468  * stored in the indirect block bn.  Blocks are free'd
469  * in LIFO order up to (but not including) lastbn.  If
470  * level is greater than SINGLE, the block is an indirect
471  * block and recursive calls to indirtrunc must be used to
472  * cleanse other indirect blocks.
473  *
474  * NB: triple indirect blocks are untested.
475  */
476 long
477 indirtrunc(ip, bn, lastbn, level)
478 	register struct inode *ip;
479 	daddr_t bn, lastbn;
480 	int level;
481 {
482 	register int i;
483 	struct buf *bp, *copy;
484 	register daddr_t *bap;
485 	register struct fs *fs = ip->i_fs;
486 	daddr_t nb, last;
487 	long factor;
488 	int blocksreleased = 0, nblocks;
489 
490 	/*
491 	 * Calculate index in current block of last
492 	 * block to be kept.  -1 indicates the entire
493 	 * block so we need not calculate the index.
494 	 */
495 	factor = 1;
496 	for (i = SINGLE; i < level; i++)
497 		factor *= NINDIR(fs);
498 	last = lastbn;
499 	if (lastbn > 0)
500 		last /= factor;
501 	nblocks = btodb(fs->fs_bsize);
502 	/*
503 	 * Get buffer of block pointers, zero those
504 	 * entries corresponding to blocks to be free'd,
505 	 * and update on disk copy first.
506 	 */
507 	copy = geteblk((int)fs->fs_bsize);
508 	bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize);
509 	if (bp->b_flags&B_ERROR) {
510 		brelse(copy);
511 		brelse(bp);
512 		return (0);
513 	}
514 	bap = bp->b_un.b_daddr;
515 	bcopy((caddr_t)bap, (caddr_t)copy->b_un.b_daddr, (u_int)fs->fs_bsize);
516 	bzero((caddr_t)&bap[last + 1],
517 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
518 	bwrite(bp);
519 	bp = copy, bap = bp->b_un.b_daddr;
520 
521 	/*
522 	 * Recursively free totally unused blocks.
523 	 */
524 	for (i = NINDIR(fs) - 1; i > last; i--) {
525 		nb = bap[i];
526 		if (nb == 0)
527 			continue;
528 		if (level > SINGLE)
529 			blocksreleased +=
530 			    indirtrunc(ip, nb, (daddr_t)-1, level - 1);
531 		free(ip, nb, (int)fs->fs_bsize);
532 		blocksreleased += nblocks;
533 	}
534 
535 	/*
536 	 * Recursively free last partial block.
537 	 */
538 	if (level > SINGLE && lastbn >= 0) {
539 		last = lastbn % factor;
540 		nb = bap[i];
541 		if (nb != 0)
542 			blocksreleased += indirtrunc(ip, nb, last, level - 1);
543 	}
544 	brelse(bp);
545 	return (blocksreleased);
546 }
547 
548 /*
549  * remove any inodes in the inode cache belonging to dev
550  *
551  * There should not be any active ones, return error if any are found
552  * (nb: this is a user error, not a system err)
553  *
554  * Also, count the references to dev by block devices - this really
555  * has nothing to do with the object of the procedure, but as we have
556  * to scan the inode table here anyway, we might as well get the
557  * extra benefit.
558  *
559  * this is called from sumount()/sys3.c when dev is being unmounted
560  */
561 #ifdef QUOTA
562 iflush(dev, iq)
563 	dev_t dev;
564 	struct inode *iq;
565 #else
566 iflush(dev)
567 	dev_t dev;
568 #endif
569 {
570 	register struct inode *ip;
571 	register open = 0;
572 
573 	for (ip = inode; ip < inodeNINODE; ip++) {
574 #ifdef QUOTA
575 		if (ip != iq && ip->i_dev == dev)
576 #else
577 		if (ip->i_dev == dev)
578 #endif
579 			if (ip->i_count)
580 				return(-1);
581 			else {
582 				remque(ip);
583 				ip->i_forw = ip;
584 				ip->i_back = ip;
585 				/*
586 				 * as i_count == 0, the inode was on the free
587 				 * list already, just leave it there, it will
588 				 * fall off the bottom eventually. We could
589 				 * perhaps move it to the head of the free
590 				 * list, but as umounts are done so
591 				 * infrequently, we would gain very little,
592 				 * while making the code bigger.
593 				 */
594 #ifdef QUOTA
595 				dqrele(ip->i_dquot);
596 				ip->i_dquot = NODQUOT;
597 #endif
598 			}
599 		else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
600 		    ip->i_rdev == dev)
601 			open++;
602 	}
603 	return (open);
604 }
605 
606 /*
607  * Lock an inode. If its already locked, set the WANT bit and sleep.
608  */
609 ilock(ip)
610 	register struct inode *ip;
611 {
612 
613 	ILOCK(ip);
614 }
615 
616 /*
617  * Unlock an inode.  If WANT bit is on, wakeup.
618  */
619 iunlock(ip)
620 	register struct inode *ip;
621 {
622 
623 	IUNLOCK(ip);
624 }
625