xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 7702)
1 /*	lfs_inode.c	4.23	82/08/10	*/
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 #include "../h/inline.h"
13 #ifdef QUOTA
14 #include "../h/quota.h"
15 #endif
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)	(((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&ILOCK) != 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 |= ILOCK;
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 = ILOCK;
172 	ip->i_count++;
173 	ip->i_lastr = 0;
174 	bp = bread(dev, fsbtodb(fs, itod(fs, ino)), 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 & ILOCK) == 0)
226 		panic("iput");
227 	iunlock(ip);
228 	irele(ip);
229 }
230 
231 irele(ip)
232 	register struct inode *ip;
233 {
234 	register int i, x;
235 	register struct inode *jp;
236 	int mode;
237 
238 	if (ip->i_count == 1) {
239 		ip->i_flag |= ILOCK;
240 		if (ip->i_nlink <= 0) {
241 			itrunc(ip, 0);
242 			mode = ip->i_mode;
243 			ip->i_mode = 0;
244 			ip->i_rdev = 0;
245 			ip->i_flag |= IUPD|ICHG;
246 			ifree(ip, ip->i_number, mode);
247 #ifdef QUOTA
248 			chkiq(ip->i_dev, ip, ip->i_uid, 0);
249 			dqrele(ip->i_dquot);
250 			ip->i_dquot = NODQUOT;
251 #endif
252 		}
253 		IUPDAT(ip, &time, &time, 0);
254 		iunlock(ip);
255 		ip->i_flag = 0;
256 		/*
257 		 * Put the inode on the end of the free list.
258 		 * Possibly in some cases it would be better to
259 		 * put the inode at the head of the free list,
260 		 * (eg: where i_mode == 0 || i_number == 0)
261 		 * but I will think about that later .. kre
262 		 * (i_number is rarely 0 - only after an i/o error in iget,
263 		 * where i_mode == 0, the inode will probably be wanted
264 		 * again soon for an ialloc, so possibly we should keep it)
265 		 */
266 		if (ifreeh) {
267 			*ifreet = ip;
268 			ip->i_freeb = ifreet;
269 		} else {
270 			ifreeh = ip;
271 			ip->i_freeb = &ifreeh;
272 		}
273 		ip->i_freef = NULL;
274 		ifreet = &ip->i_freef;
275 	}
276 	ip->i_count--;
277 }
278 
279 /*
280  * Check accessed and update flags on
281  * an inode structure.
282  * If any is on, update the inode
283  * with the current time.
284  * If waitfor is given, then must insure
285  * i/o order so wait for write to complete.
286  */
287 iupdat(ip, ta, tm, waitfor)
288 	register struct inode *ip;
289 	time_t *ta, *tm;
290 	int waitfor;
291 {
292 	register struct buf *bp;
293 	struct dinode *dp;
294 	register struct fs *fp;
295 
296 	fp = ip->i_fs;
297 	if ((ip->i_flag & (IUPD|IACC|ICHG)) != 0) {
298 		if (fp->fs_ronly)
299 			return;
300 		bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
301 			fp->fs_bsize);
302 		if (bp->b_flags & B_ERROR) {
303 			brelse(bp);
304 			return;
305 		}
306 		if (ip->i_flag&IACC)
307 			ip->i_atime = *ta;
308 		if (ip->i_flag&IUPD)
309 			ip->i_mtime = *tm;
310 		if (ip->i_flag&ICHG)
311 			ip->i_ctime = time;
312 		ip->i_flag &= ~(IUPD|IACC|ICHG);
313 		dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
314 		dp->di_ic = ip->i_ic;
315 		if (waitfor)
316 			bwrite(bp);
317 		else
318 			bdwrite(bp);
319 	}
320 }
321 
322 /*
323  * Truncate the inode ip to at most
324  * length size.  Free affected disk
325  * blocks -- the blocks of the file
326  * are removed in reverse order.
327  */
328 itrunc(ip, length)
329 	register struct inode *ip;
330 	register int length;
331 {
332 	register i;
333 	dev_t dev;
334 	daddr_t bn;
335 	struct inode itmp;
336 	register struct fs *fs;
337 #ifdef QUOTA
338 	register long cnt = 0;
339 	long tloop();
340 #endif
341 	/*
342 	 * Only plain files, directories and symbolic
343 	 * links contain blocks.
344 	 */
345 	i = ip->i_mode & IFMT;
346 	if (i != IFREG && i != IFDIR && i != IFLNK)
347 		return;
348 	if (ip->i_size <= length)
349 		return;
350 
351 	/*
352 	 * Clean inode on disk before freeing blocks
353 	 * to insure no duplicates if system crashes.
354 	 */
355 	itmp = *ip;
356 	itmp.i_size = length;
357 	for (i = 0; i < NDADDR; i++)
358 		itmp.i_db[i] = 0;
359 	for (i = 0; i < NIADDR; i++)
360 		itmp.i_ib[i] = 0;
361 	itmp.i_flag |= ICHG|IUPD;
362 	iupdat(&itmp, &time, &time, 1);
363 	ip->i_flag &= ~(IUPD|IACC|ICHG);
364 
365 	/*
366 	 * Now return blocks to free list... if machine
367 	 * crashes, they will be harmless MISSING blocks.
368 	 */
369 	dev = ip->i_dev;
370 	fs = ip->i_fs;
371 	/*
372 	 * release double indirect block first
373 	 */
374 	bn = ip->i_ib[NIADDR-1];
375 	if (bn != (daddr_t)0) {
376 		ip->i_ib[NIADDR - 1] = (daddr_t)0;
377 #ifdef QUOTA
378 		cnt +=
379 #endif
380 			tloop(ip, bn, 1);
381 	}
382 	/*
383 	 * release single indirect blocks second
384 	 */
385 	for (i = NIADDR - 2; i >= 0; i--) {
386 		bn = ip->i_ib[i];
387 		if (bn != (daddr_t)0) {
388 			ip->i_ib[i] = (daddr_t)0;
389 #ifdef QUOTA
390 			cnt +=
391 #endif
392 				tloop(ip, bn, 0);
393 		}
394 	}
395 	/*
396 	 * finally release direct blocks
397 	 */
398 	for (i = NDADDR - 1; i>=0; i--) {
399 		register size;
400 
401 		bn = ip->i_db[i];
402 		if (bn == (daddr_t)0)
403 			continue;
404 		ip->i_db[i] = (daddr_t)0;
405 		fre(ip, bn, size = (off_t)blksize(fs, ip, i));
406 #ifdef QUOTA
407 		cnt += size / DEV_BSIZE;
408 #endif
409 	}
410 	ip->i_size = 0;
411 	/*
412 	 * Inode was written and flags updated above.
413 	 * No need to modify flags here.
414 	 */
415 #ifdef QUOTA
416 	(void) chkdq(ip, -cnt, 0);
417 #endif
418 }
419 
420 #ifdef QUOTA
421 long
422 #endif
423 tloop(ip, bn, indflg)
424 	register struct inode *ip;
425 	daddr_t bn;
426 	int indflg;
427 {
428 	register i;
429 	register struct buf *bp;
430 	register daddr_t *bap;
431 	register struct fs *fs;
432 	daddr_t nb;
433 #ifdef QUOTA
434 	register long cnt = 0;
435 #endif
436 
437 	bp = NULL;
438 	fs = ip->i_fs;
439 	for (i = NINDIR(fs) - 1; i >= 0; i--) {
440 		if (bp == NULL) {
441 			bp = bread(ip->i_dev, fsbtodb(fs, bn), fs->fs_bsize);
442 			if (bp->b_flags & B_ERROR) {
443 				brelse(bp);
444 				return;
445 			}
446 			bap = bp->b_un.b_daddr;
447 		}
448 		nb = bap[i];
449 		if (nb == (daddr_t)0)
450 			continue;
451 		if (indflg) {
452 #ifdef QUOTA
453 			cnt +=
454 #endif
455 				tloop(ip, nb, 0);
456 		} else {
457 			fre(ip, nb, fs->fs_bsize);
458 #ifdef QUOTA
459 			cnt += fs->fs_bsize / DEV_BSIZE;
460 #endif
461 		}
462 	}
463 	if (bp != NULL)
464 		brelse(bp);
465 	fre(ip, bn, fs->fs_bsize);
466 #ifdef QUOTA
467 	cnt += fs->fs_bsize / DEV_BSIZE;
468 	return(cnt);
469 #endif
470 }
471 
472 /*
473  * remove any inodes in the inode cache belonging to dev
474  *
475  * There should not be any active ones, return error if any are found
476  * (nb: this is a user error, not a system err)
477  *
478  * Also, count the references to dev by block devices - this really
479  * has nothing to do with the object of the procedure, but as we have
480  * to scan the inode table here anyway, we might as well get the
481  * extra benefit.
482  *
483  * this is called from sumount()/sys3.c when dev is being unmounted
484  */
485 #ifdef QUOTA
486 iflush(dev, iq)
487 	dev_t dev;
488 	struct inode *iq;
489 #else
490 iflush(dev)
491 	dev_t dev;
492 #endif
493 {
494 	register struct inode *ip;
495 	register open = 0;
496 
497 	for (ip = inode; ip < inodeNINODE; ip++) {
498 #ifdef QUOTA
499 		if (ip != iq && ip->i_dev == dev)
500 #else
501 		if (ip->i_dev == dev)
502 #endif
503 			if (ip->i_count)
504 				return(-1);
505 			else {
506 				remque(ip);
507 				ip->i_forw = ip;
508 				ip->i_back = ip;
509 				/*
510 				 * as i_count == 0, the inode was on the free
511 				 * list already, just leave it there, it will
512 				 * fall off the bottom eventually. We could
513 				 * perhaps move it to the head of the free
514 				 * list, but as umounts are done so
515 				 * infrequently, we would gain very little,
516 				 * while making the code bigger.
517 				 */
518 #ifdef QUOTA
519 				dqrele(ip->i_dquot);
520 				ip->i_dquot = NODQUOT;
521 #endif
522 			}
523 		else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
524 		    ip->i_rdev == dev)
525 			open++;
526 	}
527 	return (open);
528 }
529 
530 #ifdef ilock
531 #undef ilock
532 #endif
533 #ifdef iunlock
534 #undef iunlock
535 #endif
536 /*
537  * Lock an inode. If its already locked, set the WANT bit and sleep.
538  */
539 ilock(ip)
540 	register struct inode *ip;
541 {
542 
543 	while (ip->i_flag&ILOCK) {
544 		ip->i_flag |= IWANT;
545 		sleep((caddr_t)ip, PINOD);
546 	}
547 	ip->i_flag |= ILOCK;
548 }
549 
550 /*
551  * Unlock an inode.  If WANT bit is on, wakeup.
552  */
553 iunlock(ip)
554 	register struct inode *ip;
555 {
556 
557 	ip->i_flag &= ~ILOCK;
558 	if (ip->i_flag&IWANT) {
559 		ip->i_flag &= ~IWANT;
560 		wakeup((caddr_t)ip);
561 	}
562 }
563