1 //=============================================================================
5 // Implementation of inode cache
7 //=============================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //=============================================================================
41 //#####DESCRIPTIONBEGIN####
43 // Author(s): jlarmour
49 // #include <cyg/fileio/inode.h>
53 //####DESCRIPTIONEND####
55 //=============================================================================
57 #include <pkgconf/system.h>
58 #include <cyg/infra/cyg_type.h>
60 #include <cyg/fileio/inode.h>
63 # include <pkgconf/kernel.h>
64 # include <cyg/kernel/mutex.hxx>
66 # define LOCK_ICACHE(_ic_) \
68 (_ic_)->icmutex.lock(); \
71 # define UNLOCK_ICACHE(_ic_) \
73 (_ic_)->icmutex.unlock(); \
75 # define LOCK_INO(_ic_,_ino_) \
77 (_ic_)->icmutex.lock(); \
79 # define UNLOCK_INO(_ic_,_ino_) \
81 (_ic_)->icmutex.unlock(); \
86 # define LOCK_ICACHE(_ic_) CYG_EMPTY_STATEMENT
87 # define UNLOCK_ICACHE(_ic_) CYG_EMPTY_STATEMENT
88 # define LOCK_INO(_ic_,_ino_) CYG_EMPTY_STATEMENT
89 # define UNLOCK_INO(_ic_,_ino_) CYG_EMPTY_STATEMENT
91 // Tried to make this implementation use tables, but the requirement to
92 // allow for extra space makes this difficult
95 cyg_inodecache_destroy( cyg_inodecache *ic )
98 for ( tmp=ic->head; tmp != NULL; tmp=tmp->i_cache_next )
100 #if CYGNUM_IO_FILEIO_MAX_INODE_CACHE_DEAD > 0
101 for ( tmp=ic->freeable; tmp != NULL; tmp=tmp->i_cache_next )
104 } // cyg_inodecache_destroy()
106 static __inline__ void
107 insert_in_list( cyg_inode **i, cyg_inode *ino )
109 cyg_inode *here = *i;
110 if ( here == NULL ) {
111 ino->i_cache_prev = ino->i_cache_next = ino;
113 ino->i_cache_prev = here->i_cache_prev;
114 ino->i_cache_next = here;
115 here->i_cache_prev = ino;
116 ino->i_cache_prev->i_cache_next = ino;
118 // put at start, as this is more likely to come off sooner than later
122 // Create an inode. Returns a negative error code on error.
123 __externC cyg_inode *
124 cyg_inode_create( cyg_inodecache *ic )
128 ni = (cyg_inode *)malloc( sizeof(cyg_inode)+ic->privatespace );
134 insert_in_list( &ic->head, ni );
138 } // cyg_inode_create()
140 __externC cyg_inode *
141 cyg_inode_get( cyg_inodecache *ic, cyg_uint32 ino )
143 cyg_inode *head = ic->head;
144 // first try the (live) cache
148 if ( tmp->i_ino == ino ) {
152 tmp = tmp->i_cache_next;
157 #if CYGNUM_IO_FILEIO_MAX_INODE_CACHE_DEAD > 0
158 // now try the cache of dead inodes
163 if ( tmp->i_ino == ino ) {
166 ic->freeablelistlen--;
167 if ( ic->freeablelistlen ) {
168 tmp->i_cache_prev->i_cache_next = tmp->i_cache_next;
169 tmp->i_cache_next->i_cache_prev = tmp->i_cache_prev;
172 insert_in_list( &ic->head, tmp );
176 tmp = tmp->i_cache_next;
182 // not found so make it
183 return cyg_inode_create( ic );
188 cyg_inode_put( cyg_inodecache *ic, cyg_inode *ino )
190 if ( --ino->i_count == 0 )
193 if ( ino->i_cache_next == ino ) {
196 ino->i_cache_prev->i_cache_next = ino->i_cache_next;
197 ino->i_cache_next->i_cache_prev = ino->i_cache_prev;
199 #if CYGNUM_IO_FILEIO_MAX_INODE_CACHE_DEAD > 0
200 ic->freeablelistlen++;
201 insert_in_list( &ic->freeable, ino );
202 if ( ic->freeablelistlen > CYGNUM_IO_FILEIO_MAX_INODE_CACHE_DEAD ) {
203 cyg_inode *prev = ino->i_cache_prev;
204 prev->i_cache_prev->i_cache_next = ino;
205 ino->i_cache_prev = prev->i_cache_prev;
206 ic->freecallback(prev);
210 ic->freecallback(ino);