panic() cleanup.
[minix.git] / servers / pfs / inode.c
blob31ed005d16f9d77b8d8afad1992cdb6a93ba99ef
1 /* This file manages the inode table. There are procedures to allocate and
2 * deallocate inodes, acquire, erase, and release them, and read and write
3 * them from the disk.
5 * The entry points into this file are
6 * get_inode: search inode table for a given inode; if not there,
7 * read it
8 * put_inode: indicate that an inode is no longer needed in memory
9 * alloc_inode: allocate a new, unused inode
10 * wipe_inode: erase some fields of a newly allocated inode
11 * free_inode: mark an inode as available for a new file
12 * update_times: update atime, ctime, and mtime
13 * dup_inode: indicate that someone else is using an inode table entry
14 * find_inode: retrieve pointer to inode in inode cache
18 #include "fs.h"
19 #include "buf.h"
20 #include "inode.h"
21 #include <minix/vfsif.h>
23 FORWARD _PROTOTYPE( int addhash_inode, (struct inode *node) );
24 FORWARD _PROTOTYPE( int unhash_inode, (struct inode *node) );
27 /*===========================================================================*
28 * fs_putnode *
29 *===========================================================================*/
30 PUBLIC int fs_putnode()
32 /* Find the inode specified by the request message and decrease its counter.*/
34 struct inode *rip;
35 int count;
36 dev_t dev;
37 ino_t inum;
39 rip = find_inode(fs_m_in.REQ_INODE_NR);
41 if(!rip) {
42 printf("%s:%d put_inode: inode #%d dev: %d not found\n", __FILE__,
43 __LINE__, fs_m_in.REQ_INODE_NR, fs_m_in.REQ_DEV);
44 panic("fs_putnode failed");
47 count = fs_m_in.REQ_COUNT;
48 if (count <= 0) {
49 printf("%s:%d put_inode: bad value for count: %d\n", __FILE__,
50 __LINE__, count);
51 panic("fs_putnode failed");
52 } else if(count > rip->i_count) {
53 printf("%s:%d put_inode: count too high: %d > %d\n", __FILE__,
54 __LINE__, count, rip->i_count);
55 panic("fs_putnode failed");
58 /* Decrease reference counter, but keep one reference; it will be consumed by
59 * put_inode(). */
60 rip->i_count -= count - 1;
61 dev = rip->i_dev;
62 inum = rip->i_num;
63 put_inode(rip);
64 if (rip->i_count == 0) put_block(dev, inum);
65 return(OK);
69 /*===========================================================================*
70 * init_inode_cache *
71 *===========================================================================*/
72 PUBLIC void init_inode_cache()
74 struct inode *rip;
75 struct inodelist *rlp;
77 /* init free/unused list */
78 TAILQ_INIT(&unused_inodes);
80 /* init hash lists */
81 for (rlp = &hash_inodes[0]; rlp < &hash_inodes[INODE_HASH_SIZE]; ++rlp)
82 LIST_INIT(rlp);
84 /* add free inodes to unused/free list */
85 for (rip = &inode[0]; rip < &inode[NR_INODES]; ++rip) {
86 rip->i_num = 0;
87 TAILQ_INSERT_HEAD(&unused_inodes, rip, i_unused);
90 /* Reserve the first inode (bit 0) to prevent it from being allocated later*/
91 if (alloc_bit() != NO_BIT) printf("PFS could not reserve NO_BIT\n");
92 busy = 0; /* This bit does not make the server 'in use/busy'. */
96 /*===========================================================================*
97 * addhash_inode *
98 *===========================================================================*/
99 PRIVATE int addhash_inode(struct inode *node)
101 int hashi = node->i_num & INODE_HASH_MASK;
103 /* insert into hash table */
104 LIST_INSERT_HEAD(&hash_inodes[hashi], node, i_hash);
105 return(OK);
109 /*===========================================================================*
110 * unhash_inode *
111 *===========================================================================*/
112 PRIVATE int unhash_inode(struct inode *node)
114 /* remove from hash table */
115 LIST_REMOVE(node, i_hash);
116 return(OK);
120 /*===========================================================================*
121 * get_inode *
122 *===========================================================================*/
123 PUBLIC struct inode *get_inode(dev, numb)
124 dev_t dev; /* device on which inode resides */
125 int numb; /* inode number (ANSI: may not be unshort) */
127 /* Find the inode in the hash table. If it is not there, get a free inode
128 * load it from the disk if it's necessary and put on the hash list
130 register struct inode *rip, *xp;
131 int hashi;
133 hashi = numb & INODE_HASH_MASK;
135 /* Search inode in the hash table */
136 LIST_FOREACH(rip, &hash_inodes[hashi], i_hash) {
137 if (rip->i_num == numb && rip->i_dev == dev) {
138 /* If unused, remove it from the unused/free list */
139 if (rip->i_count == 0) {
140 TAILQ_REMOVE(&unused_inodes, rip, i_unused);
142 ++rip->i_count;
144 return(rip);
148 /* Inode is not on the hash, get a free one */
149 if (TAILQ_EMPTY(&unused_inodes)) {
150 err_code = ENFILE;
151 return(NIL_INODE);
153 rip = TAILQ_FIRST(&unused_inodes);
155 /* If not free unhash it */
156 if (rip->i_num != 0) unhash_inode(rip);
158 /* Inode is not unused any more */
159 TAILQ_REMOVE(&unused_inodes, rip, i_unused);
161 /* Load the inode. */
162 rip->i_dev = dev;
163 rip->i_num = numb;
164 rip->i_count = 1;
165 rip->i_update = 0; /* all the times are initially up-to-date */
167 /* Add to hash */
168 addhash_inode(rip);
171 return(rip);
175 /*===========================================================================*
176 * find_inode *
177 *===========================================================================*/
178 PUBLIC struct inode *find_inode(numb)
179 int numb; /* inode number (ANSI: may not be unshort) */
181 /* Find the inode specified by the inode and device number.
183 struct inode *rip;
184 int hashi;
186 hashi = numb & INODE_HASH_MASK;
188 /* Search inode in the hash table */
189 LIST_FOREACH(rip, &hash_inodes[hashi], i_hash) {
190 if (rip->i_count > 0 && rip->i_num == numb) {
191 return(rip);
195 return(NIL_INODE);
199 /*===========================================================================*
200 * put_inode *
201 *===========================================================================*/
202 PUBLIC void put_inode(rip)
203 register struct inode *rip; /* pointer to inode to be released */
205 /* The caller is no longer using this inode. If no one else is using it either
206 * write it back to the disk immediately. If it has no links, truncate it and
207 * return it to the pool of available inodes.
210 if (rip == NIL_INODE) return; /* checking here is easier than in caller */
212 if (rip->i_count < 1)
213 panic("put_inode: i_count already below 1: %d", rip->i_count);
215 if (--rip->i_count == 0) { /* i_count == 0 means no one is using it now */
216 if (rip->i_nlinks == 0) {
217 /* i_nlinks == 0 means free the inode. */
218 truncate_inode(rip, 0); /* return all the disk blocks */
219 rip->i_mode = I_NOT_ALLOC; /* clear I_TYPE field */
220 free_inode(rip);
221 } else {
222 truncate_inode(rip, 0);
225 if (rip->i_nlinks == 0) {
226 /* free, put at the front of the LRU list */
227 unhash_inode(rip);
228 rip->i_num = 0;
229 TAILQ_INSERT_HEAD(&unused_inodes, rip, i_unused);
230 } else {
231 /* unused, put at the back of the LRU (cache it) */
232 TAILQ_INSERT_TAIL(&unused_inodes, rip, i_unused);
238 /*===========================================================================*
239 * alloc_inode *
240 *===========================================================================*/
241 PUBLIC struct inode *alloc_inode(dev_t dev, mode_t bits)
243 /* Allocate a free inode on 'dev', and return a pointer to it. */
245 register struct inode *rip;
246 int major, minor;
247 bit_t b;
248 ino_t i_num;
250 b = alloc_bit();
251 if (b == NO_BIT) {
252 err_code = ENOSPC;
253 printf("PipeFS is out of inodes\n");
254 return(NIL_INODE);
256 i_num = (ino_t) b;
259 /* Try to acquire a slot in the inode table. */
260 if ((rip = get_inode(dev, i_num)) == NIL_INODE) {
261 /* No inode table slots available. Free the inode if just allocated.*/
262 if (dev == NO_DEV) free_bit(b);
263 } else {
264 /* An inode slot is available. */
266 rip->i_mode = bits; /* set up RWX bits */
267 rip->i_nlinks = 0; /* initial no links */
268 rip->i_uid = caller_uid; /* file's uid is owner's */
269 rip->i_gid = caller_gid; /* ditto group id */
271 /* Fields not cleared already are cleared in wipe_inode(). They have
272 * been put there because truncate() needs to clear the same fields if
273 * the file happens to be open while being truncated. It saves space
274 * not to repeat the code twice.
276 wipe_inode(rip);
279 return(rip);
283 /*===========================================================================*
284 * wipe_inode *
285 *===========================================================================*/
286 PUBLIC void wipe_inode(rip)
287 register struct inode *rip; /* the inode to be erased */
289 /* Erase some fields in the inode. This function is called from alloc_inode()
290 * when a new inode is to be allocated, and from truncate(), when an existing
291 * inode is to be truncated.
294 register int i;
296 rip->i_size = 0;
297 rip->i_update = ATIME | CTIME | MTIME; /* update all times later */
301 /*===========================================================================*
302 * free_inode *
303 *===========================================================================*/
304 PUBLIC void free_inode(rip)
305 struct inode *rip;
307 /* Return an inode to the pool of unallocated inodes. */
309 bit_t b;
311 if (rip->i_num <= 0 || rip->i_num >= NR_INODES) return;
312 b = rip->i_num;
313 free_bit(b);
317 /*===========================================================================*
318 * dup_inode *
319 *===========================================================================*/
320 PUBLIC void dup_inode(ip)
321 struct inode *ip; /* The inode to be duplicated. */
323 /* This routine is a simplified form of get_inode() for the case where
324 * the inode pointer is already known.
327 ip->i_count++;
331 /*===========================================================================*
332 * update_times *
333 *===========================================================================*/
334 PUBLIC void update_times(rip)
335 register struct inode *rip; /* pointer to inode to be read/written */
337 /* Various system calls are required by the standard to update atime, ctime,
338 * or mtime. Since updating a time requires sending a message to the clock
339 * task--an expensive business--the times are marked for update by setting
340 * bits in i_update. When a stat, fstat, or sync is done, or an inode is
341 * released, update_times() may be called to actually fill in the times.
344 time_t cur_time;
346 cur_time = clock_time();
347 if (rip->i_update & ATIME) rip->i_atime = cur_time;
348 if (rip->i_update & CTIME) rip->i_ctime = cur_time;
349 if (rip->i_update & MTIME) rip->i_mtime = cur_time;
350 rip->i_update = 0; /* they are all up-to-date now */