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
5 * The entry points into this file are
6 * get_inode: search inode table for a given inode; if not there,
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 * dup_inode: indicate that someone else is using an inode table entry
14 * find_inode: retrieve pointer to inode in inode cache
21 #include <minix/vfsif.h>
23 FORWARD
_PROTOTYPE( int addhash_inode
, (struct inode
*node
) );
24 FORWARD
_PROTOTYPE( int unhash_inode
, (struct inode
*node
) );
27 /*===========================================================================*
29 *===========================================================================*/
30 PUBLIC
int fs_putnode()
32 /* Find the inode specified by the request message and decrease its counter.*/
39 rip
= find_inode(fs_m_in
.REQ_INODE_NR
);
42 printf("%s:%d put_inode: inode #%d dev: %d not found\n", __FILE__
,
43 __LINE__
, fs_m_in
.REQ_INODE_NR
, fs_m_in
.REQ_DEV
);
44 panic("fs_putnode failed");
47 count
= fs_m_in
.REQ_COUNT
;
49 printf("%s:%d put_inode: bad value for count: %d\n", __FILE__
,
51 panic("fs_putnode failed");
52 } else if(count
> rip
->i_count
) {
53 printf("%s:%d put_inode: count too high: %d > %d\n", __FILE__
,
54 __LINE__
, count
, rip
->i_count
);
55 panic("fs_putnode failed");
58 /* Decrease reference counter, but keep one reference; it will be consumed by
60 rip
->i_count
-= count
- 1;
64 if (rip
->i_count
== 0) put_block(dev
, inum
);
69 /*===========================================================================*
71 *===========================================================================*/
72 PUBLIC
void init_inode_cache()
75 struct inodelist
*rlp
;
77 /* init free/unused list */
78 TAILQ_INIT(&unused_inodes
);
81 for (rlp
= &hash_inodes
[0]; rlp
< &hash_inodes
[INODE_HASH_SIZE
]; ++rlp
)
84 /* add free inodes to unused/free list */
85 for (rip
= &inode
[0]; rip
< &inode
[NR_INODES
]; ++rip
) {
87 TAILQ_INSERT_HEAD(&unused_inodes
, rip
, i_unused
);
90 /* Reserve the first inode (bit 0) to prevent it from being allocated later*/
91 if (alloc_bit() != NO_BIT
) printf("PFS could not reserve NO_BIT\n");
92 busy
= 0; /* This bit does not make the server 'in use/busy'. */
96 /*===========================================================================*
98 *===========================================================================*/
99 PRIVATE
int addhash_inode(struct inode
*node
)
101 int hashi
= node
->i_num
& INODE_HASH_MASK
;
103 /* insert into hash table */
104 LIST_INSERT_HEAD(&hash_inodes
[hashi
], node
, i_hash
);
109 /*===========================================================================*
111 *===========================================================================*/
112 PRIVATE
int unhash_inode(struct inode
*node
)
114 /* remove from hash table */
115 LIST_REMOVE(node
, i_hash
);
120 /*===========================================================================*
122 *===========================================================================*/
123 PUBLIC
struct inode
*get_inode(dev
, numb
)
124 dev_t dev
; /* device on which inode resides */
125 int numb
; /* inode number (ANSI: may not be unshort) */
127 /* Find the inode in the hash table. If it is not there, get a free inode
128 * load it from the disk if it's necessary and put on the hash list
130 register struct inode
*rip
, *xp
;
133 hashi
= numb
& INODE_HASH_MASK
;
135 /* Search inode in the hash table */
136 LIST_FOREACH(rip
, &hash_inodes
[hashi
], i_hash
) {
137 if (rip
->i_num
== numb
&& rip
->i_dev
== dev
) {
138 /* If unused, remove it from the unused/free list */
139 if (rip
->i_count
== 0) {
140 TAILQ_REMOVE(&unused_inodes
, rip
, i_unused
);
148 /* Inode is not on the hash, get a free one */
149 if (TAILQ_EMPTY(&unused_inodes
)) {
153 rip
= TAILQ_FIRST(&unused_inodes
);
155 /* If not free unhash it */
156 if (rip
->i_num
!= 0) unhash_inode(rip
);
158 /* Inode is not unused any more */
159 TAILQ_REMOVE(&unused_inodes
, rip
, i_unused
);
161 /* Load the inode. */
165 rip
->i_update
= 0; /* all the times are initially up-to-date */
175 /*===========================================================================*
177 *===========================================================================*/
178 PUBLIC
struct inode
*find_inode(numb
)
179 int numb
; /* inode number (ANSI: may not be unshort) */
181 /* Find the inode specified by the inode and device number.
186 hashi
= numb
& INODE_HASH_MASK
;
188 /* Search inode in the hash table */
189 LIST_FOREACH(rip
, &hash_inodes
[hashi
], i_hash
) {
190 if (rip
->i_count
> 0 && rip
->i_num
== numb
) {
199 /*===========================================================================*
201 *===========================================================================*/
202 PUBLIC
void put_inode(rip
)
203 register struct inode
*rip
; /* pointer to inode to be released */
205 /* The caller is no longer using this inode. If no one else is using it either
206 * write it back to the disk immediately. If it has no links, truncate it and
207 * return it to the pool of available inodes.
210 if (rip
== NIL_INODE
) return; /* checking here is easier than in caller */
212 if (rip
->i_count
< 1)
213 panic("put_inode: i_count already below 1: %d", rip
->i_count
);
215 if (--rip
->i_count
== 0) { /* i_count == 0 means no one is using it now */
216 if (rip
->i_nlinks
== 0) {
217 /* i_nlinks == 0 means free the inode. */
218 truncate_inode(rip
, 0); /* return all the disk blocks */
219 rip
->i_mode
= I_NOT_ALLOC
; /* clear I_TYPE field */
222 truncate_inode(rip
, 0);
225 if (rip
->i_nlinks
== 0) {
226 /* free, put at the front of the LRU list */
229 TAILQ_INSERT_HEAD(&unused_inodes
, rip
, i_unused
);
231 /* unused, put at the back of the LRU (cache it) */
232 TAILQ_INSERT_TAIL(&unused_inodes
, rip
, i_unused
);
238 /*===========================================================================*
240 *===========================================================================*/
241 PUBLIC
struct inode
*alloc_inode(dev_t dev
, mode_t bits
)
243 /* Allocate a free inode on 'dev', and return a pointer to it. */
245 register struct inode
*rip
;
253 printf("PipeFS is out of inodes\n");
259 /* Try to acquire a slot in the inode table. */
260 if ((rip
= get_inode(dev
, i_num
)) == NIL_INODE
) {
261 /* No inode table slots available. Free the inode if just allocated.*/
262 if (dev
== NO_DEV
) free_bit(b
);
264 /* An inode slot is available. */
266 rip
->i_mode
= bits
; /* set up RWX bits */
267 rip
->i_nlinks
= 0; /* initial no links */
268 rip
->i_uid
= caller_uid
; /* file's uid is owner's */
269 rip
->i_gid
= caller_gid
; /* ditto group id */
271 /* Fields not cleared already are cleared in wipe_inode(). They have
272 * been put there because truncate() needs to clear the same fields if
273 * the file happens to be open while being truncated. It saves space
274 * not to repeat the code twice.
283 /*===========================================================================*
285 *===========================================================================*/
286 PUBLIC
void wipe_inode(rip
)
287 register struct inode
*rip
; /* the inode to be erased */
289 /* Erase some fields in the inode. This function is called from alloc_inode()
290 * when a new inode is to be allocated, and from truncate(), when an existing
291 * inode is to be truncated.
297 rip
->i_update
= ATIME
| CTIME
| MTIME
; /* update all times later */
301 /*===========================================================================*
303 *===========================================================================*/
304 PUBLIC
void free_inode(rip
)
307 /* Return an inode to the pool of unallocated inodes. */
311 if (rip
->i_num
<= 0 || rip
->i_num
>= NR_INODES
) return;
317 /*===========================================================================*
319 *===========================================================================*/
320 PUBLIC
void dup_inode(ip
)
321 struct inode
*ip
; /* The inode to be duplicated. */
323 /* This routine is a simplified form of get_inode() for the case where
324 * the inode pointer is already known.
331 /*===========================================================================*
333 *===========================================================================*/
334 PUBLIC
void update_times(rip
)
335 register struct inode
*rip
; /* pointer to inode to be read/written */
337 /* Various system calls are required by the standard to update atime, ctime,
338 * or mtime. Since updating a time requires sending a message to the clock
339 * task--an expensive business--the times are marked for update by setting
340 * bits in i_update. When a stat, fstat, or sync is done, or an inode is
341 * released, update_times() may be called to actually fill in the times.
346 cur_time
= clock_time();
347 if (rip
->i_update
& ATIME
) rip
->i_atime
= cur_time
;
348 if (rip
->i_update
& CTIME
) rip
->i_ctime
= cur_time
;
349 if (rip
->i_update
& MTIME
) rip
->i_mtime
= cur_time
;
350 rip
->i_update
= 0; /* they are all up-to-date now */