-
Notifications
You must be signed in to change notification settings - Fork 1
/
ialloc.c
218 lines (175 loc) · 5.11 KB
/
ialloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Inode allocation routines.
Copyright (C) 1995 Free Software Foundation, Inc.
Converted to work under the hurd by Miles Bader <miles@gnu.org>
Modified for the MINIX file system from the original for the ext2
file system by Roberto Reale <rober.reale@gmail.com>.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "minixfs.h"
#include "bitmap.c"
/*
* linux/fs/minix/bitmap.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/* Free node NP; the on disk copy has already been synced with
diskfs_node_update (where NP->dn_stat.st_mode was 0). Its
mode used to be OLD_MODE. */
void
diskfs_free_node (struct node *np, mode_t old_mode)
{
char *bh;
ino_t inum = np->cache_id;
block_t imap_block;
assert (!diskfs_readonly);
spin_lock (&global_lock);
if (inum < MINIXFS_FIRST_INO || inum > sblock->s_ninodes)
{
minixfs_error ("trying to free a reserved or nonexistent inode: %Lu",
inum);
spin_unlock (&global_lock);
return;
}
minixfs_debug ("freeing inode %Lu", inum);
imap_block = inum >> LOG2_BITS_PER_BLOCK;
if (imap_block >= sblock->s_imap_blocks)
{
minixfs_error ("nonexistent imap in superblock: %u", imap_block);
spin_unlock (&global_lock);
return;
}
bh = bptr (I_MAP_BOFFS + imap_block);
if (! clear_bit (inum & (BITS_PER_BLOCK - 1), bh))
minixfs_warning ("bit already cleared for inode %Lu", inum);
else
{
record_global_poke (bh);
sblock_info->s_free_inodes_count++;
}
spin_unlock (&global_lock);
alloc_sync (0);
}
ino_t
minixfs_new_inode (void)
{
char *bh = bptr (I_MAP_BOFFS);
ino_t inum;
unsigned long bits = sblock->s_ninodes;
spin_lock (&global_lock);
repeat:
inum = find_first_zero_bit ((unsigned long *) bh, bits);
if (inum < bits)
{
assert (sblock_info->s_free_inodes_count > 0);
minixfs_debug ("eligible bit found at position %Lu", inum);
if (set_bit (inum, bh))
{
/* shouldn't happen */
minixfs_warning ("bit already set for inode %Lu", inum);
goto repeat;
}
record_global_poke (bh);
}
else
{
assert (sblock_info->s_free_inodes_count == 0);
minixfs_debug ("no more free inodes");
inum = 0;
goto sync_out;
}
if (inum < MINIXFS_FIRST_INO)
{
minixfs_error ("reserved inode");
inum = 0;
goto sync_out;
}
sblock_info->s_free_inodes_count--;
sync_out:
spin_unlock (&global_lock);
alloc_sync (0);
return inum;
}
/* ---------------------------------------------------------------- */
/* The user must define this function. Allocate a new node to be of
mode MODE in locked directory DP (don't actually set the mode or
modify the dir, that will be done by the caller); the user
responsible for the request can be identified with CRED. Set *NP
to be the newly allocated node. */
error_t
diskfs_alloc_node (struct node *dir, mode_t mode, struct node **node)
{
error_t err;
int zone_ptr, nzones = sblock_info->s_n_zones;
struct node *np;
struct stat *st;
ino_t inum;
assert (!diskfs_readonly);
inum = minixfs_new_inode ();
if (inum == 0)
return ENOSPC;
err = diskfs_cached_lookup (inum, &np);
if (err)
return err;
st = &np->dn_stat;
if (st->st_blocks)
{
st->st_blocks = 0;
np->dn_set_ctime = 1;
}
/* Zero out the zone pointers in case there's some noise left on disk. */
if (sblock_info->s_version == MINIX_V1)
{
for (zone_ptr = 0; zone_ptr < nzones; zone_ptr++)
if (np->dn->info.i_zone_V1[zone_ptr] != 0)
{
np->dn->info.i_zone_V1[zone_ptr] = 0;
np->dn_set_ctime = 1;
}
}
else
{
for (zone_ptr = 0; zone_ptr < nzones; zone_ptr++)
if (np->dn->info.i_zone_V2[zone_ptr] != 0)
{
np->dn->info.i_zone_V2[zone_ptr] = 0;
np->dn_set_ctime = 1;
}
}
/* if (np->dn->info_i_translator != 0) */
/* { */
/* np->dn->info_i_translator = 0; */
/* np->dn_set_ctime = 1; */
/* } */
/* st->st_mode &= ~S_IPTRANS; */
if (np->allocsize)
{
st->st_size = 0;
np->allocsize = 0;
np->dn_set_ctime = 1;
}
st->st_flags = 0;
st->st_gen = 0;
alloc_sync (np);
*node = np;
return 0;
}
/* ---------------------------------------------------------------- */
unsigned long
minixfs_count_free_inodes ()
{
unsigned long bitmap_count;
/*spin_lock (&global_lock);*/
bitmap_count = count_free (bptr (I_MAP_BOFFS),
sblock->s_imap_blocks << log2_block_size);
/*spin_unlock (&global_lock);*/
return bitmap_count;
}