tools/llvm: Do not build with symbols
[minix3.git] / minix / fs / pfs / inode.c
blobe988cd8dc2c0120a43d1ed783e8718ae4f154e82
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(fs_m_in->m_vfs_fs_putnode.inode);
40 if(!rip) {
41 printf("%s:%d put_inode: inode #%llu not found\n", __FILE__,
42 __LINE__, fs_m_in->m_vfs_fs_putnode.inode);
43 panic("fs_putnode failed");
46 count = fs_m_in->m_vfs_fs_putnode.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[PFS_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(ino_t numb /* inode number */)
178 /* Find the inode specified by the inode and device number.
180 struct inode *rip;
181 int hashi;
183 hashi = (int) (numb & INODE_HASH_MASK);
185 /* Search inode in the hash table */
186 LIST_FOREACH(rip, &hash_inodes[hashi], i_hash) {
187 if (rip->i_count > 0 && rip->i_num == numb) {
188 return(rip);
192 return(NULL);
196 /*===========================================================================*
197 * put_inode *
198 *===========================================================================*/
199 void put_inode(rip)
200 struct inode *rip; /* pointer to inode to be released */
202 /* The caller is no longer using this inode. If no one else is using it either
203 * write it back to the disk immediately. If it has no links, truncate it and
204 * return it to the pool of available inodes.
207 if (rip == NULL) return; /* checking here is easier than in caller */
209 if (rip->i_count < 1)
210 panic("put_inode: i_count already below 1: %d", rip->i_count);
212 if (--rip->i_count == 0) { /* i_count == 0 means no one is using it now */
213 if (rip->i_nlinks == NO_LINK) { /* Are there links to this file? */
214 /* no links, free the inode. */
215 truncate_inode(rip, 0); /* return all the disk blocks */
216 rip->i_mode = I_NOT_ALLOC; /* clear I_TYPE field */
217 free_inode(rip);
218 } else {
219 truncate_inode(rip, (off_t) 0);
222 if (rip->i_nlinks == NO_LINK) {
223 /* free, put at the front of the LRU list */
224 unhash_inode(rip);
225 rip->i_num = NO_ENTRY;
226 rip->i_dev = NO_DEV;
227 rip->i_rdev = NO_DEV;
228 TAILQ_INSERT_HEAD(&unused_inodes, rip, i_unused);
229 } else {
230 /* unused, put at the back of the LRU (cache it) */
231 TAILQ_INSERT_TAIL(&unused_inodes, rip, i_unused);
237 /*===========================================================================*
238 * alloc_inode *
239 *===========================================================================*/
240 struct inode *alloc_inode(dev_t dev, mode_t bits, uid_t uid, gid_t gid)
242 /* Allocate a free inode on 'dev', and return a pointer to it. */
244 register struct inode *rip;
245 bit_t b;
246 ino_t i_num;
247 int print_oos_msg = 1;
249 b = alloc_bit();
250 if (b == NO_BIT) {
251 err_code = ENOSPC;
252 if (print_oos_msg)
253 printf("PipeFS is out of inodes\n");
254 print_oos_msg = 0; /* Don't repeat message */
255 return(NULL);
257 i_num = (ino_t) b;
258 print_oos_msg = 1;
261 /* Try to acquire a slot in the inode table. */
262 if ((rip = get_inode(dev, i_num)) == NULL) {
263 /* No inode table slots available. Free the inode if just allocated.*/
264 if (dev == NO_DEV) free_bit(b);
265 } else {
266 /* An inode slot is available. */
268 rip->i_mode = bits; /* set up RWX bits */
269 rip->i_nlinks = NO_LINK; /* initial no links */
270 rip->i_uid = uid; /* set file user id */
271 rip->i_gid = gid; /* ditto group id */
273 /* Fields not cleared already are cleared in wipe_inode(). They have
274 * been put there because truncate() needs to clear the same fields if
275 * the file happens to be open while being truncated. It saves space
276 * not to repeat the code twice.
278 wipe_inode(rip);
281 return(rip);
285 /*===========================================================================*
286 * wipe_inode *
287 *===========================================================================*/
288 void wipe_inode(rip)
289 struct inode *rip; /* the inode to be erased */
291 /* Erase some fields in the inode. This function is called from alloc_inode()
292 * when a new inode is to be allocated, and from truncate(), when an existing
293 * inode is to be truncated.
296 rip->i_size = 0;
297 rip->i_update = ATIME | CTIME | MTIME; /* update all times later */
301 /*===========================================================================*
302 * free_inode *
303 *===========================================================================*/
304 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 >= PFS_NR_INODES) return;
312 b = (bit_t) rip->i_num;
313 free_bit(b);
317 /*===========================================================================*
318 * update_times *
319 *===========================================================================*/
320 void update_times(rip)
321 struct inode *rip; /* pointer to inode to be read/written */
323 /* Various system calls are required by the standard to update atime, ctime,
324 * or mtime. Since updating a time requires sending a message to the clock
325 * task--an expensive business--the times are marked for update by setting
326 * bits in i_update. When a stat, fstat, or sync is done, or an inode is
327 * released, update_times() may be called to actually fill in the times.
330 time_t cur_time;
332 cur_time = clock_time();
333 if (rip->i_update & ATIME) rip->i_atime = cur_time;
334 if (rip->i_update & CTIME) rip->i_ctime = cur_time;
335 if (rip->i_update & MTIME) rip->i_mtime = cur_time;
336 rip->i_update = 0; /* they are all up-to-date now */