2010-01-26 00:18:02 +01:00
|
|
|
/* This file deals with inode management.
|
|
|
|
*
|
|
|
|
* The entry points into this file are:
|
|
|
|
* init_inode initialize the inode table, return the root inode
|
|
|
|
* find_inode find an inode based on its inode number
|
|
|
|
* get_inode increase the reference count of an inode
|
|
|
|
* put_inode decrease the reference count of an inode
|
|
|
|
* link_inode link an inode as a directory entry to another inode
|
|
|
|
* unlink_inode unlink an inode from its parent directory
|
|
|
|
* get_free_inode return a free inode object
|
|
|
|
* have_free_inode check whether there is a free inode available
|
|
|
|
* have_used_inode check whether any inode is still in use
|
|
|
|
* do_putnode perform the PUTNODE file system call
|
|
|
|
*
|
|
|
|
* Created:
|
|
|
|
* April 2009 (D.C. van Moolenbroek)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "inc.h"
|
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct inode inodes[NUM_INODES];
|
2010-01-27 23:59:03 +01:00
|
|
|
|
2012-03-25 20:25:53 +02:00
|
|
|
static TAILQ_HEAD(free_head, inode) free_list;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* init_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
struct inode *init_inode(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Initialize inode table. Return the root inode.
|
|
|
|
*/
|
|
|
|
struct inode *ino;
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
TAILQ_INIT(&free_list);
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
dprintf(("%s: %d inodes, %u bytes each, equals %u bytes\n",
|
|
|
|
sffs_name, NUM_INODES, sizeof(struct inode), sizeof(inodes)));
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
/* Mark all inodes except the root inode as free. */
|
|
|
|
for (index = 1; index < NUM_INODES; index++) {
|
|
|
|
ino = &inodes[index];
|
2010-05-10 15:26:00 +02:00
|
|
|
ino->i_parent = NULL;
|
2010-01-26 00:18:02 +01:00
|
|
|
LIST_INIT(&ino->i_child);
|
|
|
|
ino->i_num = index + 1;
|
|
|
|
ino->i_gen = (unsigned short)-1; /* aesthetics */
|
|
|
|
ino->i_ref = 0;
|
|
|
|
ino->i_flags = 0;
|
|
|
|
TAILQ_INSERT_TAIL(&free_list, ino, i_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize and return the root inode. */
|
|
|
|
ino = &inodes[0];
|
|
|
|
ino->i_parent = ino; /* root inode is its own parent */
|
|
|
|
LIST_INIT(&ino->i_child);
|
|
|
|
ino->i_num = ROOT_INODE_NR;
|
|
|
|
ino->i_gen = 0; /* unused by root node */
|
|
|
|
ino->i_ref = 1; /* root inode is hereby in use */
|
|
|
|
ino->i_flags = I_DIR; /* root inode is a directory */
|
|
|
|
ino->i_name[0] = 0; /* root inode has empty name */
|
|
|
|
|
|
|
|
return ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* find_inode *
|
|
|
|
*===========================================================================*/
|
2013-03-07 15:46:21 +01:00
|
|
|
struct inode *find_inode(pino_t ino_nr)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Get an inode based on its inode number. Do not increase its reference count.
|
|
|
|
*/
|
|
|
|
struct inode *ino;
|
2010-01-27 23:59:03 +01:00
|
|
|
int index;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
/* Inode 0 (= index -1) is not a valid inode number. */
|
|
|
|
index = INODE_INDEX(ino_nr);
|
|
|
|
if (index < 0) {
|
2012-04-09 18:08:26 +02:00
|
|
|
printf("%s: VFS passed invalid inode number!\n", sffs_name);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
return NULL;
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(index < NUM_INODES);
|
|
|
|
|
|
|
|
ino = &inodes[index];
|
|
|
|
|
|
|
|
/* Make sure the generation number matches. */
|
|
|
|
if (INODE_GEN(ino_nr) != ino->i_gen) {
|
2012-04-09 18:08:26 +02:00
|
|
|
printf("%s: VFS passed outdated inode number!\n", sffs_name);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
return NULL;
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The VFS/FS protocol only uses referenced inodes. */
|
|
|
|
if (ino->i_ref == 0)
|
2012-04-09 18:08:26 +02:00
|
|
|
printf("%s: VFS passed unused inode!\n", sffs_name);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
return ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* get_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
void get_inode(struct inode *ino)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Increase the given inode's reference count. If both reference and link
|
|
|
|
* count were zero before, remove the inode from the free list.
|
|
|
|
*/
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
dprintf(("%s: get_inode(%p) ['%s']\n", sffs_name, ino, ino->i_name));
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
/* (INUSE, CACHED) -> INUSE */
|
|
|
|
|
|
|
|
/* If this is the first reference, remove the node from the free list. */
|
|
|
|
if (ino->i_ref == 0 && !HAS_CHILDREN(ino))
|
|
|
|
TAILQ_REMOVE(&free_list, ino, i_free);
|
|
|
|
|
|
|
|
ino->i_ref++;
|
|
|
|
|
|
|
|
if (ino->i_ref == 0)
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("inode reference count wrapped");
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* put_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
void put_inode(struct inode *ino)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Decrease an inode's reference count. If this count has reached zero, close
|
|
|
|
* the inode's file handle, if any. If both reference and link count have
|
|
|
|
* reached zero, mark the inode as cached or free.
|
|
|
|
*/
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
dprintf(("%s: put_inode(%p) ['%s']\n", sffs_name, ino, ino->i_name));
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
assert(ino != NULL);
|
2010-01-26 00:18:02 +01:00
|
|
|
assert(ino->i_ref > 0);
|
|
|
|
|
|
|
|
ino->i_ref--;
|
|
|
|
|
|
|
|
/* If there are still references to this inode, we're done here. */
|
|
|
|
if (ino->i_ref > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Close any file handle associated with this inode. */
|
|
|
|
put_handle(ino);
|
|
|
|
|
|
|
|
/* Only add the inode to the free list if there are also no links to it. */
|
|
|
|
if (HAS_CHILDREN(ino))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* INUSE -> CACHED, DELETED -> FREE */
|
|
|
|
|
|
|
|
/* Add the inode to the head or tail of the free list, depending on whether
|
|
|
|
* it is also deleted (and therefore can never be reused as is).
|
|
|
|
*/
|
2010-05-10 15:26:00 +02:00
|
|
|
if (ino->i_parent == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
TAILQ_INSERT_HEAD(&free_list, ino, i_free);
|
|
|
|
else
|
|
|
|
TAILQ_INSERT_TAIL(&free_list, ino, i_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* link_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
void link_inode(struct inode *parent, struct inode *ino)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Link an inode to a parent. If both reference and link count were zero
|
|
|
|
* before, remove the inode from the free list. This function should only be
|
|
|
|
* called from add_dentry().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This can never happen, right? */
|
|
|
|
if (parent->i_ref == 0 && !HAS_CHILDREN(parent))
|
|
|
|
TAILQ_REMOVE(&free_list, parent, i_free);
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&parent->i_child, ino, i_next);
|
|
|
|
|
|
|
|
ino->i_parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* unlink_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
void unlink_inode(struct inode *ino)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Unlink an inode from its parent. If both reference and link count have
|
|
|
|
* reached zero, mark the inode as cached or free. This function should only
|
|
|
|
* be used from del_dentry().
|
|
|
|
*/
|
|
|
|
struct inode *parent;
|
|
|
|
|
|
|
|
parent = ino->i_parent;
|
|
|
|
|
|
|
|
LIST_REMOVE(ino, i_next);
|
2012-04-09 18:08:26 +02:00
|
|
|
|
2010-01-26 00:18:02 +01:00
|
|
|
if (parent->i_ref == 0 && !HAS_CHILDREN(parent)) {
|
2010-05-10 15:26:00 +02:00
|
|
|
if (parent->i_parent == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
TAILQ_INSERT_HEAD(&free_list, parent, i_free);
|
|
|
|
else
|
|
|
|
TAILQ_INSERT_TAIL(&free_list, parent, i_free);
|
|
|
|
}
|
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
ino->i_parent = NULL;
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* get_free_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
struct inode *get_free_inode(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Return a free inode object (with reference count 1), if available.
|
|
|
|
*/
|
|
|
|
struct inode *ino;
|
|
|
|
|
|
|
|
/* [CACHED -> FREE,] FREE -> DELETED */
|
|
|
|
|
|
|
|
/* If there are no inodes on the free list, we cannot satisfy the request. */
|
|
|
|
if (TAILQ_EMPTY(&free_list)) {
|
2012-04-09 18:08:26 +02:00
|
|
|
printf("%s: out of inodes!\n", sffs_name);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
return NULL;
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ino = TAILQ_FIRST(&free_list);
|
|
|
|
TAILQ_REMOVE(&free_list, ino, i_free);
|
|
|
|
|
|
|
|
assert(ino->i_ref == 0);
|
|
|
|
assert(!HAS_CHILDREN(ino));
|
|
|
|
|
|
|
|
/* If this was a cached inode, free it first. */
|
2010-05-10 15:26:00 +02:00
|
|
|
if (ino->i_parent != NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
del_dentry(ino);
|
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
assert(ino->i_parent == NULL);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
/* Initialize a subset of its fields */
|
|
|
|
ino->i_gen++;
|
|
|
|
ino->i_ref = 1;
|
|
|
|
|
|
|
|
return ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* have_free_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
int have_free_inode(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Check whether there are any free inodes at the moment. Kind of lame, but
|
|
|
|
* this allows for easier error recovery in some places.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return !TAILQ_EMPTY(&free_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* have_used_inode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
int have_used_inode(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Check whether any inodes are still in use, that is, any of the inodes have
|
|
|
|
* a reference count larger than zero.
|
|
|
|
*/
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
for (index = 0; index < NUM_INODES; index++)
|
|
|
|
if (inodes[index].i_ref > 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_putnode *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
int do_putnode(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Decrease an inode's reference count.
|
|
|
|
*/
|
|
|
|
struct inode *ino;
|
|
|
|
int count;
|
|
|
|
|
2010-05-10 15:26:00 +02:00
|
|
|
if ((ino = find_inode(m_in.REQ_INODE_NR)) == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
count = m_in.REQ_COUNT;
|
|
|
|
|
|
|
|
if (count <= 0 || count > ino->i_ref) return EINVAL;
|
|
|
|
|
|
|
|
ino->i_ref -= count - 1;
|
|
|
|
|
|
|
|
put_inode(ino);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|