etc/services - sync with NetBSD-8
[minix.git] / minix / lib / libsffs / inode.c
blob499e941124bf7fe3729d472a87a4f5962a5a1265
1 /* This file deals with inode management.
3 * The entry points into this file are:
4 * init_inode initialize the inode table, return the root inode
5 * find_inode find an inode based on its inode number
6 * get_inode increase the reference count of an inode
7 * put_inode decrease the reference count of an inode
8 * link_inode link an inode as a directory entry to another inode
9 * unlink_inode unlink an inode from its parent directory
10 * get_free_inode return a free inode object
11 * have_free_inode check whether there is a free inode available
12 * have_used_inode check whether any inode is still in use
13 * do_putnode perform the PUTNODE file system call
15 * Created:
16 * April 2009 (D.C. van Moolenbroek)
19 #include "inc.h"
21 static struct inode inodes[NUM_INODES];
23 static TAILQ_HEAD(free_head, inode) free_list;
25 /*===========================================================================*
26 * init_inode *
27 *===========================================================================*/
28 struct inode *init_inode(void)
30 /* Initialize inode table. Return the root inode.
32 struct inode *ino;
33 unsigned int i;
35 TAILQ_INIT(&free_list);
37 dprintf(("%s: %d inodes, %u bytes each, equals %u bytes\n",
38 sffs_name, NUM_INODES, sizeof(struct inode), sizeof(inodes)));
40 /* Mark all inodes except the root inode as free. */
41 for (i = 1; i < NUM_INODES; i++) {
42 ino = &inodes[i];
43 ino->i_parent = NULL;
44 LIST_INIT(&ino->i_child);
45 ino->i_num = i + 1;
46 ino->i_gen = (unsigned short)-1; /* aesthetics */
47 ino->i_ref = 0;
48 ino->i_flags = 0;
49 TAILQ_INSERT_TAIL(&free_list, ino, i_free);
52 /* Initialize and return the root inode. */
53 ino = &inodes[0];
54 ino->i_parent = ino; /* root inode is its own parent */
55 LIST_INIT(&ino->i_child);
56 ino->i_num = ROOT_INODE_NR;
57 ino->i_gen = 0; /* unused by root node */
58 ino->i_ref = 1; /* root inode is hereby in use */
59 ino->i_flags = I_DIR; /* root inode is a directory */
60 ino->i_name[0] = 0; /* root inode has empty name */
62 return ino;
65 /*===========================================================================*
66 * find_inode *
67 *===========================================================================*/
68 struct inode *find_inode(ino_t ino_nr)
70 /* Get an inode based on its inode number. Do not increase its reference count.
72 struct inode *ino;
73 int i;
75 /* Inode 0 (= index -1) is not a valid inode number. */
76 i = INODE_INDEX(ino_nr);
77 if (i < 0) {
78 printf("%s: VFS passed invalid inode number!\n", sffs_name);
80 return NULL;
83 assert(i < NUM_INODES);
85 ino = &inodes[i];
87 /* Make sure the generation number matches. */
88 if (INODE_GEN(ino_nr) != ino->i_gen) {
89 printf("%s: VFS passed outdated inode number!\n", sffs_name);
91 return NULL;
94 /* The VFS/FS protocol only uses referenced inodes. */
95 if (ino->i_ref == 0)
96 printf("%s: VFS passed unused inode!\n", sffs_name);
98 return ino;
101 /*===========================================================================*
102 * get_inode *
103 *===========================================================================*/
104 void get_inode(struct inode *ino)
106 /* Increase the given inode's reference count. If both reference and link
107 * count were zero before, remove the inode from the free list.
110 dprintf(("%s: get_inode(%p) ['%s']\n", sffs_name, ino, ino->i_name));
112 /* (INUSE, CACHED) -> INUSE */
114 /* If this is the first reference, remove the node from the free list. */
115 if (ino->i_ref == 0 && !HAS_CHILDREN(ino))
116 TAILQ_REMOVE(&free_list, ino, i_free);
118 ino->i_ref++;
120 if (ino->i_ref == 0)
121 panic("inode reference count wrapped");
124 /*===========================================================================*
125 * put_inode *
126 *===========================================================================*/
127 void put_inode(struct inode *ino)
129 /* Decrease an inode's reference count. If this count has reached zero, close
130 * the inode's file handle, if any. If both reference and link count have
131 * reached zero, mark the inode as cached or free.
134 dprintf(("%s: put_inode(%p) ['%s']\n", sffs_name, ino, ino->i_name));
136 assert(ino != NULL);
137 assert(ino->i_ref > 0);
139 ino->i_ref--;
141 /* If there are still references to this inode, we're done here. */
142 if (ino->i_ref > 0)
143 return;
145 /* Close any file handle associated with this inode. */
146 put_handle(ino);
148 /* Only add the inode to the free list if there are also no links to it. */
149 if (HAS_CHILDREN(ino))
150 return;
152 /* INUSE -> CACHED, DELETED -> FREE */
154 /* Add the inode to the head or tail of the free list, depending on whether
155 * it is also deleted (and therefore can never be reused as is).
157 if (ino->i_parent == NULL)
158 TAILQ_INSERT_HEAD(&free_list, ino, i_free);
159 else
160 TAILQ_INSERT_TAIL(&free_list, ino, i_free);
163 /*===========================================================================*
164 * link_inode *
165 *===========================================================================*/
166 void link_inode(struct inode *parent, struct inode *ino)
168 /* Link an inode to a parent. If both reference and link count were zero
169 * before, remove the inode from the free list. This function should only be
170 * called from add_dentry().
173 /* This can never happen, right? */
174 if (parent->i_ref == 0 && !HAS_CHILDREN(parent))
175 TAILQ_REMOVE(&free_list, parent, i_free);
177 LIST_INSERT_HEAD(&parent->i_child, ino, i_next);
179 ino->i_parent = parent;
182 /*===========================================================================*
183 * unlink_inode *
184 *===========================================================================*/
185 void unlink_inode(struct inode *ino)
187 /* Unlink an inode from its parent. If both reference and link count have
188 * reached zero, mark the inode as cached or free. This function should only
189 * be used from del_dentry().
191 struct inode *parent;
193 parent = ino->i_parent;
195 LIST_REMOVE(ino, i_next);
197 if (parent->i_ref == 0 && !HAS_CHILDREN(parent)) {
198 if (parent->i_parent == NULL)
199 TAILQ_INSERT_HEAD(&free_list, parent, i_free);
200 else
201 TAILQ_INSERT_TAIL(&free_list, parent, i_free);
204 ino->i_parent = NULL;
207 /*===========================================================================*
208 * get_free_inode *
209 *===========================================================================*/
210 struct inode *get_free_inode(void)
212 /* Return a free inode object (with reference count 1), if available.
214 struct inode *ino;
216 /* [CACHED -> FREE,] FREE -> DELETED */
218 /* If there are no inodes on the free list, we cannot satisfy the request. */
219 if (TAILQ_EMPTY(&free_list)) {
220 printf("%s: out of inodes!\n", sffs_name);
222 return NULL;
225 ino = TAILQ_FIRST(&free_list);
226 TAILQ_REMOVE(&free_list, ino, i_free);
228 assert(ino->i_ref == 0);
229 assert(!HAS_CHILDREN(ino));
231 /* If this was a cached inode, free it first. */
232 if (ino->i_parent != NULL)
233 del_dentry(ino);
235 assert(ino->i_parent == NULL);
237 /* Initialize a subset of its fields */
238 ino->i_gen++;
239 ino->i_ref = 1;
241 return ino;
244 /*===========================================================================*
245 * have_free_inode *
246 *===========================================================================*/
247 int have_free_inode(void)
249 /* Check whether there are any free inodes at the moment. Kind of lame, but
250 * this allows for easier error recovery in some places.
253 return !TAILQ_EMPTY(&free_list);
256 /*===========================================================================*
257 * have_used_inode *
258 *===========================================================================*/
259 int have_used_inode(void)
261 /* Check whether any inodes are still in use, that is, any of the inodes have
262 * a reference count larger than zero.
264 unsigned int i;
266 for (i = 0; i < NUM_INODES; i++)
267 if (inodes[i].i_ref > 0)
268 return TRUE;
270 return FALSE;
273 /*===========================================================================*
274 * do_putnode *
275 *===========================================================================*/
276 int do_putnode(ino_t ino_nr, unsigned int count)
278 /* Decrease an inode's reference count.
280 struct inode *ino;
282 if ((ino = find_inode(ino_nr)) == NULL)
283 return EINVAL;
285 if (count > ino->i_ref) return EINVAL;
287 ino->i_ref -= count - 1;
289 put_inode(ino);
291 return OK;