This source file includes following definitions.
- hashfn
- hash
- insert_inode_free
- remove_inode_free
- insert_inode_hash
- remove_inode_hash
- put_last_free
- grow_inodes
- inode_init
- wait_on_inode
- lock_inode
- unlock_inode
- clear_inode
- fs_may_mount
- fs_may_umount
- fs_may_remount_ro
- write_inode
- read_inode
- notify_change
- bmap
- invalidate_inodes
- sync_inodes
- iput
- get_empty_inode
- get_pipe_inode
- iget
- __iget
- __wait_on_inode
1
2
3
4
5
6
7 #include <linux/stat.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/mm.h>
11 #include <linux/string.h>
12
13 #include <asm/system.h>
14
15 static struct inode * hash_table[NR_IHASH];
16 static struct inode * first_inode;
17 static struct wait_queue * inode_wait = NULL;
18 static int nr_inodes = 0, nr_free_inodes = 0;
19
20 static inline int const hashfn(dev_t dev, unsigned int i)
21 {
22 return (dev ^ i) % NR_IHASH;
23 }
24
25 static inline struct inode ** const hash(dev_t dev, int i)
26 {
27 return hash_table + hashfn(dev, i);
28 }
29
30 static void insert_inode_free(struct inode *inode)
31 {
32 inode->i_next = first_inode;
33 inode->i_prev = first_inode->i_prev;
34 inode->i_next->i_prev = inode;
35 inode->i_prev->i_next = inode;
36 first_inode = inode;
37 }
38
39 static void remove_inode_free(struct inode *inode)
40 {
41 if (first_inode == inode)
42 first_inode = first_inode->i_next;
43 if (inode->i_next)
44 inode->i_next->i_prev = inode->i_prev;
45 if (inode->i_prev)
46 inode->i_prev->i_next = inode->i_next;
47 inode->i_next = inode->i_prev = NULL;
48 }
49
50 void insert_inode_hash(struct inode *inode)
51 {
52 struct inode **h;
53 h = hash(inode->i_dev, inode->i_ino);
54
55 inode->i_hash_next = *h;
56 inode->i_hash_prev = NULL;
57 if (inode->i_hash_next)
58 inode->i_hash_next->i_hash_prev = inode;
59 *h = inode;
60 }
61
62 static void remove_inode_hash(struct inode *inode)
63 {
64 struct inode **h;
65 h = hash(inode->i_dev, inode->i_ino);
66
67 if (*h == inode)
68 *h = inode->i_hash_next;
69 if (inode->i_hash_next)
70 inode->i_hash_next->i_hash_prev = inode->i_hash_prev;
71 if (inode->i_hash_prev)
72 inode->i_hash_prev->i_hash_next = inode->i_hash_next;
73 inode->i_hash_prev = inode->i_hash_next = NULL;
74 }
75
76 static void put_last_free(struct inode *inode)
77 {
78 remove_inode_free(inode);
79 inode->i_prev = first_inode->i_prev;
80 inode->i_prev->i_next = inode;
81 inode->i_next = first_inode;
82 inode->i_next->i_prev = inode;
83 }
84
85 void grow_inodes(void)
86 {
87 struct inode * inode;
88 int i;
89
90 if (!(inode = (struct inode*) get_free_page(GFP_KERNEL)))
91 return;
92
93 i=PAGE_SIZE / sizeof(struct inode);
94 nr_inodes += i;
95 nr_free_inodes += i;
96
97 if (!first_inode)
98 inode->i_next = inode->i_prev = first_inode = inode++, i--;
99
100 for ( ; i ; i-- )
101 insert_inode_free(inode++);
102 }
103
104 unsigned long inode_init(unsigned long start, unsigned long end)
105 {
106 memset(hash_table, 0, sizeof(hash_table));
107 first_inode = NULL;
108 return start;
109 }
110
111 static void __wait_on_inode(struct inode *);
112
113 static inline void wait_on_inode(struct inode * inode)
114 {
115 if (inode->i_lock)
116 __wait_on_inode(inode);
117 }
118
119 static inline void lock_inode(struct inode * inode)
120 {
121 wait_on_inode(inode);
122 inode->i_lock = 1;
123 }
124
125 static inline void unlock_inode(struct inode * inode)
126 {
127 inode->i_lock = 0;
128 wake_up(&inode->i_wait);
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143 void clear_inode(struct inode * inode)
144 {
145 struct wait_queue * wait;
146
147 wait_on_inode(inode);
148 remove_inode_hash(inode);
149 remove_inode_free(inode);
150 wait = ((volatile struct inode *) inode)->i_wait;
151 if (inode->i_count)
152 nr_free_inodes++;
153 memset(inode,0,sizeof(*inode));
154 ((volatile struct inode *) inode)->i_wait = wait;
155 insert_inode_free(inode);
156 }
157
158 int fs_may_mount(dev_t dev)
159 {
160 struct inode * inode, * next;
161 int i;
162
163 next = first_inode;
164 for (i = nr_inodes ; i > 0 ; i--) {
165 inode = next;
166 next = inode->i_next;
167 if (inode->i_dev != dev)
168 continue;
169 if (inode->i_count || inode->i_dirt || inode->i_lock)
170 return 0;
171 clear_inode(inode);
172 }
173 return 1;
174 }
175
176 int fs_may_umount(dev_t dev, struct inode * mount_root)
177 {
178 struct inode * inode;
179 int i;
180
181 inode = first_inode;
182 for (i=0 ; i < nr_inodes ; i++, inode = inode->i_next) {
183 if (inode->i_dev != dev || !inode->i_count)
184 continue;
185 if (inode == mount_root && inode->i_count == 1)
186 continue;
187 return 0;
188 }
189 return 1;
190 }
191
192 int fs_may_remount_ro(dev_t dev)
193 {
194 struct file * file;
195 int i;
196
197
198 for (file = first_file, i=0; i<nr_files; i++, file=file->f_next) {
199 if (!file->f_count || !file->f_inode ||
200 file->f_inode->i_dev != dev)
201 continue;
202 if (S_ISREG(file->f_inode->i_mode) && (file->f_mode & 2))
203 return 0;
204 }
205 return 1;
206 }
207
208 static void write_inode(struct inode * inode)
209 {
210 if (!inode->i_dirt)
211 return;
212 wait_on_inode(inode);
213 if (!inode->i_dirt)
214 return;
215 if (!inode->i_sb || !inode->i_sb->s_op || !inode->i_sb->s_op->write_inode) {
216 inode->i_dirt = 0;
217 return;
218 }
219 inode->i_lock = 1;
220 inode->i_sb->s_op->write_inode(inode);
221 unlock_inode(inode);
222 }
223
224 static void read_inode(struct inode * inode)
225 {
226 lock_inode(inode);
227 if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode)
228 inode->i_sb->s_op->read_inode(inode);
229 unlock_inode(inode);
230 }
231
232
233
234
235
236
237
238
239
240 int notify_change(int flags, struct inode * inode)
241 {
242 if (inode->i_sb && inode->i_sb->s_op &&
243 inode->i_sb->s_op->notify_change)
244 return inode->i_sb->s_op->notify_change(flags, inode);
245 return 0;
246 }
247
248
249
250
251
252
253
254
255
256
257
258 int bmap(struct inode * inode, int block)
259 {
260 if (inode->i_op && inode->i_op->bmap)
261 return inode->i_op->bmap(inode,block);
262 return 0;
263 }
264
265 void invalidate_inodes(dev_t dev)
266 {
267 struct inode * inode, * next;
268 int i;
269
270 next = first_inode;
271 for(i = nr_inodes ; i > 0 ; i--) {
272 inode = next;
273 next = inode->i_next;
274 if (inode->i_dev != dev)
275 continue;
276 if (inode->i_count || inode->i_dirt || inode->i_lock) {
277 printk("VFS: inode busy on removed device %d/%d\n", MAJOR(dev), MINOR(dev));
278 continue;
279 }
280 clear_inode(inode);
281 }
282 }
283
284 void sync_inodes(dev_t dev)
285 {
286 int i;
287 struct inode * inode;
288
289 inode = first_inode;
290 for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
291 if (dev && inode->i_dev != dev)
292 continue;
293 wait_on_inode(inode);
294 if (inode->i_dirt)
295 write_inode(inode);
296 }
297 }
298
299 void iput(struct inode * inode)
300 {
301 if (!inode)
302 return;
303 wait_on_inode(inode);
304 if (!inode->i_count) {
305 printk("VFS: iput: trying to free free inode\n");
306 printk("VFS: device %d/%d, inode %d, mode=0%07o\n",
307 MAJOR(inode->i_rdev), MINOR(inode->i_rdev),
308 inode->i_ino, inode->i_mode);
309 return;
310 }
311 if (inode->i_pipe) {
312 wake_up(&PIPE_READ_WAIT(*inode));
313 wake_up(&PIPE_WRITE_WAIT(*inode));
314 }
315 repeat:
316 if (inode->i_count>1) {
317 inode->i_count--;
318 return;
319 }
320 wake_up(&inode_wait);
321 if (inode->i_pipe) {
322 unsigned long page = (unsigned long) PIPE_BASE(*inode);
323 PIPE_BASE(*inode) = NULL;
324 free_page(page);
325 }
326 if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
327 inode->i_sb->s_op->put_inode(inode);
328 if (!inode->i_nlink)
329 return;
330 }
331 if (inode->i_dirt) {
332 write_inode(inode);
333 wait_on_inode(inode);
334 goto repeat;
335 }
336 inode->i_count--;
337 nr_free_inodes++;
338 return;
339 }
340
341 struct inode * get_empty_inode(void)
342 {
343 struct inode * inode, * best;
344 int i;
345
346 if (nr_inodes < NR_INODE && nr_free_inodes < (nr_inodes >> 2))
347 grow_inodes();
348 repeat:
349 inode = first_inode;
350 best = NULL;
351 for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
352 if (!inode->i_count) {
353 if (!best)
354 best = inode;
355 if (!inode->i_dirt && !inode->i_lock) {
356 best = inode;
357 break;
358 }
359 }
360 }
361 if (!best || best->i_dirt || best->i_lock)
362 if (nr_inodes < NR_INODE) {
363 grow_inodes();
364 goto repeat;
365 }
366 inode = best;
367 if (!inode) {
368 printk("VFS: No free inodes - contact Linus\n");
369 sleep_on(&inode_wait);
370 goto repeat;
371 }
372 if (inode->i_lock) {
373 wait_on_inode(inode);
374 goto repeat;
375 }
376 if (inode->i_dirt) {
377 write_inode(inode);
378 goto repeat;
379 }
380 if (inode->i_count)
381 goto repeat;
382 clear_inode(inode);
383 inode->i_count = 1;
384 inode->i_nlink = 1;
385 nr_free_inodes--;
386 if (nr_free_inodes < 0) {
387 printk ("VFS: get_empty_inode: bad free inode count.\n");
388 nr_free_inodes = 0;
389 }
390 return inode;
391 }
392
393 struct inode * get_pipe_inode(void)
394 {
395 struct inode * inode;
396 extern struct inode_operations pipe_inode_operations;
397
398 if (!(inode = get_empty_inode()))
399 return NULL;
400 if (!(PIPE_BASE(*inode) = (char*) __get_free_page(GFP_USER))) {
401 iput(inode);
402 return NULL;
403 }
404 inode->i_op = &pipe_inode_operations;
405 inode->i_count = 2;
406 PIPE_READ_WAIT(*inode) = PIPE_WRITE_WAIT(*inode) = NULL;
407 PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
408 PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
409 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
410 inode->i_pipe = 1;
411 inode->i_mode |= S_IFIFO | S_IRUSR | S_IWUSR;
412 inode->i_uid = current->euid;
413 inode->i_gid = current->egid;
414 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
415 return inode;
416 }
417
418 struct inode * iget(struct super_block * sb,int nr)
419 {
420 return __iget(sb,nr,1);
421 }
422
423 struct inode * __iget(struct super_block * sb, int nr, int crossmntp)
424 {
425 struct inode * inode, * empty;
426
427 if (!sb)
428 panic("VFS: iget with sb==NULL");
429 empty = get_empty_inode();
430 repeat:
431 inode = *(hash(sb->s_dev,nr));
432 while (inode) {
433 if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
434 inode = inode->i_hash_next;
435 continue;
436 }
437 wait_on_inode(inode);
438 if (inode->i_dev != sb->s_dev || inode->i_ino != nr)
439 goto repeat;
440 if (!inode->i_count)
441 nr_free_inodes--;
442 inode->i_count++;
443 if (crossmntp && inode->i_mount) {
444 int i;
445
446 for (i = 0 ; i<NR_SUPER ; i++)
447 if (super_blocks[i].s_covered==inode)
448 break;
449 if (i >= NR_SUPER) {
450 printk("VFS: Mounted inode hasn't got sb\n");
451 if (empty)
452 iput(empty);
453 return inode;
454 }
455 iput(inode);
456 if (!(inode = super_blocks[i].s_mounted))
457 printk("VFS: Mounted device %d/%d has no rootinode\n",
458 MAJOR(inode->i_dev), MINOR(inode->i_dev));
459 else {
460 if (!inode->i_count)
461 nr_free_inodes--;
462 inode->i_count++;
463 wait_on_inode(inode);
464 }
465 }
466 if (empty)
467 iput(empty);
468 return inode;
469 }
470 if (!empty)
471 return (NULL);
472 inode = empty;
473 inode->i_sb = sb;
474 inode->i_dev = sb->s_dev;
475 inode->i_ino = nr;
476 inode->i_flags = sb->s_flags;
477 put_last_free(inode);
478 insert_inode_hash(inode);
479 read_inode(inode);
480 return inode;
481 }
482
483
484
485
486
487
488
489 static void __wait_on_inode(struct inode * inode)
490 {
491 struct wait_queue wait = { current, NULL };
492
493 add_wait_queue(&inode->i_wait, &wait);
494 repeat:
495 current->state = TASK_UNINTERRUPTIBLE;
496 if (inode->i_lock) {
497 schedule();
498 goto repeat;
499 }
500 remove_wait_queue(&inode->i_wait, &wait);
501 current->state = TASK_RUNNING;
502 }