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
16 * April 2009 (D.C. van Moolenbroek)
21 static struct inode inodes
[NUM_INODES
];
23 static TAILQ_HEAD(free_head
, inode
) free_list
;
25 /*===========================================================================*
27 *===========================================================================*/
28 struct inode
*init_inode(void)
30 /* Initialize inode table. Return the root inode.
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
++) {
44 LIST_INIT(&ino
->i_child
);
46 ino
->i_gen
= (unsigned short)-1; /* aesthetics */
49 TAILQ_INSERT_TAIL(&free_list
, ino
, i_free
);
52 /* Initialize and return the root inode. */
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 */
65 /*===========================================================================*
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.
75 /* Inode 0 (= index -1) is not a valid inode number. */
76 i
= INODE_INDEX(ino_nr
);
78 printf("%s: VFS passed invalid inode number!\n", sffs_name
);
83 assert(i
< NUM_INODES
);
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
);
94 /* The VFS/FS protocol only uses referenced inodes. */
96 printf("%s: VFS passed unused inode!\n", sffs_name
);
101 /*===========================================================================*
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
);
121 panic("inode reference count wrapped");
124 /*===========================================================================*
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
));
137 assert(ino
->i_ref
> 0);
141 /* If there are still references to this inode, we're done here. */
145 /* Close any file handle associated with this inode. */
148 /* Only add the inode to the free list if there are also no links to it. */
149 if (HAS_CHILDREN(ino
))
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
);
160 TAILQ_INSERT_TAIL(&free_list
, ino
, i_free
);
163 /*===========================================================================*
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 /*===========================================================================*
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
);
201 TAILQ_INSERT_TAIL(&free_list
, parent
, i_free
);
204 ino
->i_parent
= NULL
;
207 /*===========================================================================*
209 *===========================================================================*/
210 struct inode
*get_free_inode(void)
212 /* Return a free inode object (with reference count 1), if available.
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
);
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
)
235 assert(ino
->i_parent
== NULL
);
237 /* Initialize a subset of its fields */
244 /*===========================================================================*
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 /*===========================================================================*
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.
266 for (i
= 0; i
< NUM_INODES
; i
++)
267 if (inodes
[i
].i_ref
> 0)
273 /*===========================================================================*
275 *===========================================================================*/
276 int do_putnode(ino_t ino_nr
, unsigned int count
)
278 /* Decrease an inode's reference count.
282 if ((ino
= find_inode(ino_nr
)) == NULL
)
285 if (count
> ino
->i_ref
) return EINVAL
;
287 ino
->i_ref
-= count
- 1;