arm: make signal handlers work
[minix.git] / servers / pfs / inode.c
blob333d56567189f2a68a92c1d8c7241a015082cd62
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 * find_inode: retrieve pointer to inode in inode cache
17 #include "fs.h"
18 #include "buf.h"
19 #include "inode.h"
20 #include <minix/vfsif.h>
22 static void addhash_inode(struct inode * const node);
23 static void unhash_inode(struct inode * const node);
26 /*===========================================================================*
27 * fs_putnode *
28 *===========================================================================*/
29 int fs_putnode(message *fs_m_in, message *fs_m_out)
31 /* Find the inode specified by the request message and decrease its counter.*/
33 struct inode *rip;
34 int count;
35 dev_t dev;
36 ino_t inum;
38 rip = find_inode( (ino_t) fs_m_in->REQ_INODE_NR);
40 if(!rip) {
41 printf("%s:%d put_inode: inode #%ld dev: %d not found\n", __FILE__,
42 __LINE__, fs_m_in->REQ_INODE_NR, (dev_t) fs_m_in->REQ_DEV);
43 panic("fs_putnode failed");
46 count = fs_m_in->REQ_COUNT;
47 if (count <= 0) {
48 printf("%s:%d put_inode: bad value for count: %d\n", __FILE__,
49 __LINE__, count);
50 panic("fs_putnode failed");
51 } else if(count > rip->i_count) {
52 printf("%s:%d put_inode: count too high: %d > %d\n", __FILE__,
53 __LINE__, count, rip->i_count);
54 panic("fs_putnode failed");
57 /* Decrease reference counter, but keep one reference; it will be consumed by
58 * put_inode(). */
59 rip->i_count -= count - 1;
60 dev = rip->i_dev;
61 inum = rip->i_num;
62 put_inode(rip);
63 if (rip->i_count == 0) put_block(dev, inum);
64 return(OK);
68 /*===========================================================================*
69 * init_inode_cache *
70 *===========================================================================*/
71 void init_inode_cache()
73 struct inode *rip;
74 struct inodelist *rlp;
76 /* init free/unused list */
77 TAILQ_INIT(&unused_inodes);
79 /* init hash lists */
80 for (rlp = &hash_inodes[0]; rlp < &hash_inodes[INODE_HASH_SIZE]; ++rlp)
81 LIST_INIT(rlp);
83 /* add free inodes to unused/free list */
84 for (rip = &inode[0]; rip < &inode[NR_INODES]; ++rip) {
85 rip->i_num = NO_ENTRY;
86 TAILQ_INSERT_HEAD(&unused_inodes, rip, i_unused);
89 /* Reserve the first inode (bit 0) to prevent it from being allocated later*/
90 if (alloc_bit() != NO_BIT) printf("PFS could not reserve NO_BIT\n");
91 busy = 0; /* This bit does not make the server 'in use/busy'. */
95 /*===========================================================================*
96 * addhash_inode *
97 *===========================================================================*/
98 static void addhash_inode(struct inode * const node)
100 int hashi = (int) (node->i_num & INODE_HASH_MASK);
102 /* insert into hash table */
103 LIST_INSERT_HEAD(&hash_inodes[hashi], node, i_hash);
107 /*===========================================================================*
108 * unhash_inode *
109 *===========================================================================*/
110 static void unhash_inode(struct inode * const node)
112 /* remove from hash table */
113 LIST_REMOVE(node, i_hash);
117 /*===========================================================================*
118 * get_inode *
119 *===========================================================================*/
120 struct inode *get_inode(
121 dev_t dev, /* device on which inode resides */
122 ino_t numb /* inode number */
125 /* Find the inode in the hash table. If it is not there, get a free inode
126 * load it from the disk if it's necessary and put on the hash list
128 register struct inode *rip;
129 int hashi;
131 hashi = (int) (numb & INODE_HASH_MASK);
133 /* Search inode in the hash table */
134 LIST_FOREACH(rip, &hash_inodes[hashi], i_hash) {
135 if (rip->i_num == numb && rip->i_dev == dev) {
136 /* If unused, remove it from the unused/free list */
137 if (rip->i_count == 0) {
138 TAILQ_REMOVE(&unused_inodes, rip, i_unused);
140 ++rip->i_count;
142 return(rip);
146 /* Inode is not on the hash, get a free one */
147 if (TAILQ_EMPTY(&unused_inodes)) {
148 err_code = ENFILE;
149 return(NULL);
151 rip = TAILQ_FIRST(&unused_inodes);
153 /* If not free unhash it */
154 if (rip->i_num != NO_ENTRY) unhash_inode(rip);
156 /* Inode is not unused any more */
157 TAILQ_REMOVE(&unused_inodes, rip, i_unused);
159 /* Load the inode. */
160 rip->i_dev = dev;
161 rip->i_num = numb;
162 rip->i_count = 1;
163 rip->i_update = 0; /* all the times are initially up-to-date */
165 /* Add to hash */
166 addhash_inode(rip);
169 return(rip);
173 /*===========================================================================*
174 * find_inode *
175 *===========================================================================*/
176 struct inode *find_inode(numb)
177 ino_t numb; /* inode number */
179 /* Find the inode specified by the inode and device number.
181 struct inode *rip;
182 int hashi;
184 hashi = (int) (numb & INODE_HASH_MASK);
186 /* Search inode in the hash table */
187 LIST_FOREACH(rip, &hash_inodes[hashi], i_hash) {
188 if (rip->i_count > 0 && rip->i_num == numb) {
189 return(rip);
193 return(NULL);
197 /*===========================================================================*
198 * put_inode *
199 *===========================================================================*/
200 void put_inode(rip)
201 struct inode *rip; /* pointer to inode to be released */
203 /* The caller is no longer using this inode. If no one else is using it either
204 * write it back to the disk immediately. If it has no links, truncate it and
205 * return it to the pool of available inodes.
208 if (rip == NULL) return; /* checking here is easier than in caller */
210 if (rip->i_count < 1)
211 panic("put_inode: i_count already below 1: %d", rip->i_count);
213 if (--rip->i_count == 0) { /* i_count == 0 means no one is using it now */
214 if (rip->i_nlinks == NO_LINK) { /* Are there links to this file? */
215 /* no links, free the inode. */
216 truncate_inode(rip, 0); /* return all the disk blocks */
217 rip->i_mode = I_NOT_ALLOC; /* clear I_TYPE field */
218 free_inode(rip);
219 } else {
220 truncate_inode(rip, (off_t) 0);
223 if (rip->i_nlinks == NO_LINK) {
224 /* free, put at the front of the LRU list */
225 unhash_inode(rip);
226 rip->i_num = NO_ENTRY;
227 rip->i_dev = NO_DEV;
228 rip->i_rdev = NO_DEV;
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 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 bit_t b;
247 ino_t i_num;
248 int print_oos_msg = 1;
250 b = alloc_bit();
251 if (b == NO_BIT) {
252 err_code = ENOSPC;
253 if (print_oos_msg)
254 printf("PipeFS is out of inodes\n");
255 print_oos_msg = 0; /* Don't repeat message */
256 return(NULL);
258 i_num = (ino_t) b;
259 print_oos_msg = 1;
262 /* Try to acquire a slot in the inode table. */
263 if ((rip = get_inode(dev, i_num)) == NULL) {
264 /* No inode table slots available. Free the inode if just allocated.*/
265 if (dev == NO_DEV) free_bit(b);
266 } else {
267 /* An inode slot is available. */
269 rip->i_mode = bits; /* set up RWX bits */
270 rip->i_nlinks = NO_LINK; /* initial no links */
271 rip->i_uid = caller_uid; /* file's uid is owner's */
272 rip->i_gid = caller_gid; /* ditto group id */
274 /* Fields not cleared already are cleared in wipe_inode(). They have
275 * been put there because truncate() needs to clear the same fields if
276 * the file happens to be open while being truncated. It saves space
277 * not to repeat the code twice.
279 wipe_inode(rip);
282 return(rip);
286 /*===========================================================================*
287 * wipe_inode *
288 *===========================================================================*/
289 void wipe_inode(rip)
290 struct inode *rip; /* the inode to be erased */
292 /* Erase some fields in the inode. This function is called from alloc_inode()
293 * when a new inode is to be allocated, and from truncate(), when an existing
294 * inode is to be truncated.
297 rip->i_size = 0;
298 rip->i_update = ATIME | CTIME | MTIME; /* update all times later */
302 /*===========================================================================*
303 * free_inode *
304 *===========================================================================*/
305 void free_inode(rip)
306 struct inode *rip;
308 /* Return an inode to the pool of unallocated inodes. */
310 bit_t b;
312 if (rip->i_num <= (ino_t) 0 || rip->i_num >= (ino_t) NR_INODES) return;
313 b = (bit_t) rip->i_num;
314 free_bit(b);
318 /*===========================================================================*
319 * update_times *
320 *===========================================================================*/
321 void update_times(rip)
322 struct inode *rip; /* pointer to inode to be read/written */
324 /* Various system calls are required by the standard to update atime, ctime,
325 * or mtime. Since updating a time requires sending a message to the clock
326 * task--an expensive business--the times are marked for update by setting
327 * bits in i_update. When a stat, fstat, or sync is done, or an inode is
328 * released, update_times() may be called to actually fill in the times.
331 time_t cur_time;
333 cur_time = clock_time();
334 if (rip->i_update & ATIME) rip->i_atime = cur_time;
335 if (rip->i_update & CTIME) rip->i_ctime = cur_time;
336 if (rip->i_update & MTIME) rip->i_mtime = cur_time;
337 rip->i_update = 0; /* they are all up-to-date now */