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 * find_inode: retrieve pointer to inode in inode cache
20 #include <minix/vfsif.h>
22 static void addhash_inode(struct inode
* const node
);
23 static void unhash_inode(struct inode
* const node
);
26 /*===========================================================================*
28 *===========================================================================*/
29 int fs_putnode(message
*fs_m_in
, message
*fs_m_out
)
31 /* Find the inode specified by the request message and decrease its counter.*/
38 rip
= find_inode(fs_m_in
->m_vfs_fs_putnode
.inode
);
41 printf("%s:%d put_inode: inode #%llu not found\n", __FILE__
,
42 __LINE__
, fs_m_in
->m_vfs_fs_putnode
.inode
);
43 panic("fs_putnode failed");
46 count
= fs_m_in
->m_vfs_fs_putnode
.count
;
48 printf("%s:%d put_inode: bad value for count: %d\n", __FILE__
,
50 panic("fs_putnode failed");
51 } else if(count
> rip
->i_count
) {
52 printf("%s:%d put_inode: count too high: %d > %d\n", __FILE__
,
53 __LINE__
, count
, rip
->i_count
);
54 panic("fs_putnode failed");
57 /* Decrease reference counter, but keep one reference; it will be consumed by
59 rip
->i_count
-= count
- 1;
63 if (rip
->i_count
== 0) put_block(dev
, inum
);
68 /*===========================================================================*
70 *===========================================================================*/
71 void init_inode_cache()
74 struct inodelist
*rlp
;
76 /* init free/unused list */
77 TAILQ_INIT(&unused_inodes
);
80 for (rlp
= &hash_inodes
[0]; rlp
< &hash_inodes
[INODE_HASH_SIZE
]; ++rlp
)
83 /* add free inodes to unused/free list */
84 for (rip
= &inode
[0]; rip
< &inode
[PFS_NR_INODES
]; ++rip
) {
85 rip
->i_num
= NO_ENTRY
;
86 TAILQ_INSERT_HEAD(&unused_inodes
, rip
, i_unused
);
89 /* Reserve the first inode (bit 0) to prevent it from being allocated later*/
90 if (alloc_bit() != NO_BIT
) printf("PFS could not reserve NO_BIT\n");
91 busy
= 0; /* This bit does not make the server 'in use/busy'. */
95 /*===========================================================================*
97 *===========================================================================*/
98 static void addhash_inode(struct inode
* const node
)
100 int hashi
= (int) (node
->i_num
& INODE_HASH_MASK
);
102 /* insert into hash table */
103 LIST_INSERT_HEAD(&hash_inodes
[hashi
], node
, i_hash
);
107 /*===========================================================================*
109 *===========================================================================*/
110 static void unhash_inode(struct inode
* const node
)
112 /* remove from hash table */
113 LIST_REMOVE(node
, i_hash
);
117 /*===========================================================================*
119 *===========================================================================*/
120 struct inode
*get_inode(
121 dev_t dev
, /* device on which inode resides */
122 ino_t numb
/* inode number */
125 /* Find the inode in the hash table. If it is not there, get a free inode
126 * load it from the disk if it's necessary and put on the hash list
128 register struct inode
*rip
;
131 hashi
= (int) (numb
& INODE_HASH_MASK
);
133 /* Search inode in the hash table */
134 LIST_FOREACH(rip
, &hash_inodes
[hashi
], i_hash
) {
135 if (rip
->i_num
== numb
&& rip
->i_dev
== dev
) {
136 /* If unused, remove it from the unused/free list */
137 if (rip
->i_count
== 0) {
138 TAILQ_REMOVE(&unused_inodes
, rip
, i_unused
);
146 /* Inode is not on the hash, get a free one */
147 if (TAILQ_EMPTY(&unused_inodes
)) {
151 rip
= TAILQ_FIRST(&unused_inodes
);
153 /* If not free unhash it */
154 if (rip
->i_num
!= NO_ENTRY
) unhash_inode(rip
);
156 /* Inode is not unused any more */
157 TAILQ_REMOVE(&unused_inodes
, rip
, i_unused
);
159 /* Load the inode. */
163 rip
->i_update
= 0; /* all the times are initially up-to-date */
173 /*===========================================================================*
175 *===========================================================================*/
176 struct inode
*find_inode(ino_t numb
/* inode number */)
178 /* Find the inode specified by the inode and device number.
183 hashi
= (int) (numb
& INODE_HASH_MASK
);
185 /* Search inode in the hash table */
186 LIST_FOREACH(rip
, &hash_inodes
[hashi
], i_hash
) {
187 if (rip
->i_count
> 0 && rip
->i_num
== numb
) {
196 /*===========================================================================*
198 *===========================================================================*/
200 struct inode
*rip
; /* pointer to inode to be released */
202 /* The caller is no longer using this inode. If no one else is using it either
203 * write it back to the disk immediately. If it has no links, truncate it and
204 * return it to the pool of available inodes.
207 if (rip
== NULL
) return; /* checking here is easier than in caller */
209 if (rip
->i_count
< 1)
210 panic("put_inode: i_count already below 1: %d", rip
->i_count
);
212 if (--rip
->i_count
== 0) { /* i_count == 0 means no one is using it now */
213 if (rip
->i_nlinks
== NO_LINK
) { /* Are there links to this file? */
214 /* no links, free the inode. */
215 truncate_inode(rip
, 0); /* return all the disk blocks */
216 rip
->i_mode
= I_NOT_ALLOC
; /* clear I_TYPE field */
219 truncate_inode(rip
, (off_t
) 0);
222 if (rip
->i_nlinks
== NO_LINK
) {
223 /* free, put at the front of the LRU list */
225 rip
->i_num
= NO_ENTRY
;
227 rip
->i_rdev
= NO_DEV
;
228 TAILQ_INSERT_HEAD(&unused_inodes
, rip
, i_unused
);
230 /* unused, put at the back of the LRU (cache it) */
231 TAILQ_INSERT_TAIL(&unused_inodes
, rip
, i_unused
);
237 /*===========================================================================*
239 *===========================================================================*/
240 struct inode
*alloc_inode(dev_t dev
, mode_t bits
, uid_t uid
, gid_t gid
)
242 /* Allocate a free inode on 'dev', and return a pointer to it. */
244 register struct inode
*rip
;
247 int print_oos_msg
= 1;
253 printf("PipeFS is out of inodes\n");
254 print_oos_msg
= 0; /* Don't repeat message */
261 /* Try to acquire a slot in the inode table. */
262 if ((rip
= get_inode(dev
, i_num
)) == NULL
) {
263 /* No inode table slots available. Free the inode if just allocated.*/
264 if (dev
== NO_DEV
) free_bit(b
);
266 /* An inode slot is available. */
268 rip
->i_mode
= bits
; /* set up RWX bits */
269 rip
->i_nlinks
= NO_LINK
; /* initial no links */
270 rip
->i_uid
= uid
; /* set file user id */
271 rip
->i_gid
= gid
; /* ditto group id */
273 /* Fields not cleared already are cleared in wipe_inode(). They have
274 * been put there because truncate() needs to clear the same fields if
275 * the file happens to be open while being truncated. It saves space
276 * not to repeat the code twice.
285 /*===========================================================================*
287 *===========================================================================*/
289 struct inode
*rip
; /* the inode to be erased */
291 /* Erase some fields in the inode. This function is called from alloc_inode()
292 * when a new inode is to be allocated, and from truncate(), when an existing
293 * inode is to be truncated.
297 rip
->i_update
= ATIME
| CTIME
| MTIME
; /* update all times later */
301 /*===========================================================================*
303 *===========================================================================*/
307 /* Return an inode to the pool of unallocated inodes. */
311 if (rip
->i_num
<= 0 || rip
->i_num
>= PFS_NR_INODES
) return;
312 b
= (bit_t
) rip
->i_num
;
317 /*===========================================================================*
319 *===========================================================================*/
320 void update_times(rip
)
321 struct inode
*rip
; /* pointer to inode to be read/written */
323 /* Various system calls are required by the standard to update atime, ctime,
324 * or mtime. Since updating a time requires sending a message to the clock
325 * task--an expensive business--the times are marked for update by setting
326 * bits in i_update. When a stat, fstat, or sync is done, or an inode is
327 * released, update_times() may be called to actually fill in the times.
332 cur_time
= clock_time();
333 if (rip
->i_update
& ATIME
) rip
->i_atime
= cur_time
;
334 if (rip
->i_update
& CTIME
) rip
->i_ctime
= cur_time
;
335 if (rip
->i_update
& MTIME
) rip
->i_mtime
= cur_time
;
336 rip
->i_update
= 0; /* they are all up-to-date now */