This source file includes following definitions.
- hashfn
- 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
- inode_change_ok
- inode_setattr
- notify_change
- bmap
- invalidate_inodes
- sync_inodes
- iput
- value
- get_empty_inode
- get_pipe_inode
- __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_entry {
16 struct inode * inode;
17 int updating;
18 } hash_table[NR_IHASH];
19
20 static struct inode * first_inode;
21 static struct wait_queue * inode_wait = NULL;
22 static int nr_inodes = 0, nr_free_inodes = 0;
23
24 static inline int const hashfn(kdev_t dev, unsigned int i)
25 {
26 return (HASHDEV(dev) ^ i) % NR_IHASH;
27 }
28
29 static inline struct inode_hash_entry * const hash(kdev_t dev, int i)
30 {
31 return hash_table + hashfn(dev, i);
32 }
33
34 static inline void insert_inode_free(struct inode *inode)
35 {
36 inode->i_next = first_inode;
37 inode->i_prev = first_inode->i_prev;
38 inode->i_next->i_prev = inode;
39 inode->i_prev->i_next = inode;
40 first_inode = inode;
41 }
42
43 static inline void remove_inode_free(struct inode *inode)
44 {
45 if (first_inode == inode)
46 first_inode = first_inode->i_next;
47 if (inode->i_next)
48 inode->i_next->i_prev = inode->i_prev;
49 if (inode->i_prev)
50 inode->i_prev->i_next = inode->i_next;
51 inode->i_next = inode->i_prev = NULL;
52 }
53
54 void insert_inode_hash(struct inode *inode)
55 {
56 struct inode_hash_entry *h;
57 h = hash(inode->i_dev, inode->i_ino);
58
59 inode->i_hash_next = h->inode;
60 inode->i_hash_prev = NULL;
61 if (inode->i_hash_next)
62 inode->i_hash_next->i_hash_prev = inode;
63 h->inode = inode;
64 }
65
66 static inline void remove_inode_hash(struct inode *inode)
67 {
68 struct inode_hash_entry *h;
69 h = hash(inode->i_dev, inode->i_ino);
70
71 if (h->inode == inode)
72 h->inode = inode->i_hash_next;
73 if (inode->i_hash_next)
74 inode->i_hash_next->i_hash_prev = inode->i_hash_prev;
75 if (inode->i_hash_prev)
76 inode->i_hash_prev->i_hash_next = inode->i_hash_next;
77 inode->i_hash_prev = inode->i_hash_next = NULL;
78 }
79
80 static inline void put_last_free(struct inode *inode)
81 {
82 remove_inode_free(inode);
83 inode->i_prev = first_inode->i_prev;
84 inode->i_prev->i_next = inode;
85 inode->i_next = first_inode;
86 inode->i_next->i_prev = inode;
87 }
88
89 int grow_inodes(void)
90 {
91 struct inode * inode;
92 int i;
93
94 if (!(inode = (struct inode*) get_free_page(GFP_KERNEL)))
95 return -ENOMEM;
96
97 i=PAGE_SIZE / sizeof(struct inode);
98 nr_inodes += i;
99 nr_free_inodes += i;
100
101 if (!first_inode)
102 inode->i_next = inode->i_prev = first_inode = inode++, i--;
103
104 for ( ; i ; i-- )
105 insert_inode_free(inode++);
106 return 0;
107 }
108
109 unsigned long inode_init(unsigned long start, unsigned long end)
110 {
111 memset(hash_table, 0, sizeof(hash_table));
112 first_inode = NULL;
113 return start;
114 }
115
116 static void __wait_on_inode(struct inode *);
117
118 static inline void wait_on_inode(struct inode * inode)
119 {
120 if (inode->i_lock)
121 __wait_on_inode(inode);
122 }
123
124 static inline void lock_inode(struct inode * inode)
125 {
126 wait_on_inode(inode);
127 inode->i_lock = 1;
128 }
129
130 static inline void unlock_inode(struct inode * inode)
131 {
132 inode->i_lock = 0;
133 wake_up(&inode->i_wait);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148 void clear_inode(struct inode * inode)
149 {
150 struct wait_queue * wait;
151
152 invalidate_inode_pages(inode, 0);
153 wait_on_inode(inode);
154 if (IS_WRITABLE(inode)) {
155 if (inode->i_sb && inode->i_sb->dq_op)
156 inode->i_sb->dq_op->drop(inode);
157 }
158 remove_inode_hash(inode);
159 remove_inode_free(inode);
160 wait = ((volatile struct inode *) inode)->i_wait;
161 if (inode->i_count)
162 nr_free_inodes++;
163 memset(inode,0,sizeof(*inode));
164 ((volatile struct inode *) inode)->i_wait = wait;
165 insert_inode_free(inode);
166 }
167
168 int fs_may_mount(kdev_t dev)
169 {
170 struct inode * inode, * next;
171 int i;
172
173 next = first_inode;
174 for (i = nr_inodes ; i > 0 ; i--) {
175 inode = next;
176 next = inode->i_next;
177 if (inode->i_dev != dev)
178 continue;
179 if (inode->i_count || inode->i_dirt || inode->i_lock)
180 return 0;
181 clear_inode(inode);
182 }
183 return 1;
184 }
185
186 int fs_may_umount(kdev_t dev, struct inode * mount_root)
187 {
188 struct inode * inode;
189 int i;
190
191 inode = first_inode;
192 for (i=0 ; i < nr_inodes ; i++, inode = inode->i_next) {
193 if (inode->i_dev != dev || !inode->i_count)
194 continue;
195 if (inode == mount_root && inode->i_count == 1)
196 continue;
197 return 0;
198 }
199 return 1;
200 }
201
202 int fs_may_remount_ro(kdev_t dev)
203 {
204 struct file * file;
205 int i;
206
207
208 for (file = first_file, i=0; i<nr_files; i++, file=file->f_next) {
209 if (!file->f_count || !file->f_inode ||
210 file->f_inode->i_dev != dev)
211 continue;
212 if (S_ISREG(file->f_inode->i_mode) && (file->f_mode & 2))
213 return 0;
214 }
215 return 1;
216 }
217
218 static void write_inode(struct inode * inode)
219 {
220 if (!inode->i_dirt)
221 return;
222 wait_on_inode(inode);
223 if (!inode->i_dirt)
224 return;
225 if (!inode->i_sb || !inode->i_sb->s_op || !inode->i_sb->s_op->write_inode) {
226 inode->i_dirt = 0;
227 return;
228 }
229 inode->i_lock = 1;
230 inode->i_sb->s_op->write_inode(inode);
231 unlock_inode(inode);
232 }
233
234 static inline void read_inode(struct inode * inode)
235 {
236 lock_inode(inode);
237 if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode)
238 inode->i_sb->s_op->read_inode(inode);
239 unlock_inode(inode);
240 }
241
242
243 int inode_change_ok(struct inode *inode, struct iattr *attr)
244 {
245
246 if ((attr->ia_valid & ATTR_UID) &&
247 (current->fsuid != inode->i_uid ||
248 attr->ia_uid != inode->i_uid) && !fsuser())
249 return -EPERM;
250
251
252 if ((attr->ia_valid & ATTR_GID) &&
253 (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid) &&
254 !fsuser())
255 return -EPERM;
256
257
258 if (attr->ia_valid & ATTR_MODE) {
259 if ((current->fsuid != inode->i_uid) && !fsuser())
260 return -EPERM;
261
262 if (!fsuser() && !in_group_p((attr->ia_valid & ATTR_GID) ? attr->ia_gid :
263 inode->i_gid))
264 attr->ia_mode &= ~S_ISGID;
265 }
266
267
268 if ((attr->ia_valid & ATTR_ATIME_SET) &&
269 ((current->fsuid != inode->i_uid) && !fsuser()))
270 return -EPERM;
271 if ((attr->ia_valid & ATTR_MTIME_SET) &&
272 ((current->fsuid != inode->i_uid) && !fsuser()))
273 return -EPERM;
274
275
276 return 0;
277 }
278
279
280
281
282
283 void inode_setattr(struct inode *inode, struct iattr *attr)
284 {
285 if (attr->ia_valid & ATTR_UID)
286 inode->i_uid = attr->ia_uid;
287 if (attr->ia_valid & ATTR_GID)
288 inode->i_gid = attr->ia_gid;
289 if (attr->ia_valid & ATTR_SIZE)
290 inode->i_size = attr->ia_size;
291 if (attr->ia_valid & ATTR_ATIME)
292 inode->i_atime = attr->ia_atime;
293 if (attr->ia_valid & ATTR_MTIME)
294 inode->i_mtime = attr->ia_mtime;
295 if (attr->ia_valid & ATTR_CTIME)
296 inode->i_ctime = attr->ia_ctime;
297 if (attr->ia_valid & ATTR_MODE) {
298 inode->i_mode = attr->ia_mode;
299 if (!fsuser() && !in_group_p(inode->i_gid))
300 inode->i_mode &= ~S_ISGID;
301 }
302 inode->i_dirt = 1;
303 }
304
305
306
307
308
309
310
311
312 int notify_change(struct inode * inode, struct iattr *attr)
313 {
314 int retval;
315
316 if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) {
317 unsigned long now = CURRENT_TIME;
318 if (!(attr->ia_valid & ATTR_ATIME_SET))
319 attr->ia_atime = now;
320 if (!(attr->ia_valid & ATTR_MTIME_SET))
321 attr->ia_mtime = now;
322 attr->ia_ctime = now;
323 }
324
325 if (inode->i_sb && inode->i_sb->s_op &&
326 inode->i_sb->s_op->notify_change)
327 return inode->i_sb->s_op->notify_change(inode, attr);
328
329 if ((retval = inode_change_ok(inode, attr)) != 0)
330 return retval;
331
332 inode_setattr(inode, attr);
333 return 0;
334 }
335
336
337
338
339
340
341
342
343
344
345
346 int bmap(struct inode * inode, int block)
347 {
348 if (inode->i_op && inode->i_op->bmap)
349 return inode->i_op->bmap(inode,block);
350 return 0;
351 }
352
353 void invalidate_inodes(kdev_t dev)
354 {
355 struct inode * inode, * next;
356 int i;
357
358 next = first_inode;
359 for(i = nr_inodes ; i > 0 ; i--) {
360 inode = next;
361 next = inode->i_next;
362 if (inode->i_dev != dev)
363 continue;
364 if (inode->i_count || inode->i_dirt || inode->i_lock) {
365 printk("VFS: inode busy on removed device %s\n",
366 kdevname(dev));
367 continue;
368 }
369 clear_inode(inode);
370 }
371 }
372
373 void sync_inodes(kdev_t dev)
374 {
375 int i;
376 struct inode * inode;
377
378 inode = first_inode;
379 for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
380 if (dev && inode->i_dev != dev)
381 continue;
382 wait_on_inode(inode);
383 if (inode->i_dirt)
384 write_inode(inode);
385 }
386 }
387
388 void iput(struct inode * inode)
389 {
390 if (!inode)
391 return;
392 wait_on_inode(inode);
393 if (!inode->i_count) {
394 printk("VFS: iput: trying to free free inode\n");
395 printk("VFS: device %s, inode %lu, mode=0%07o\n",
396 kdevname(inode->i_rdev), inode->i_ino, inode->i_mode);
397 return;
398 }
399 if (inode->i_pipe)
400 wake_up_interruptible(&PIPE_WAIT(*inode));
401 repeat:
402 if (inode->i_count>1) {
403 inode->i_count--;
404 return;
405 }
406
407 wake_up(&inode_wait);
408 if (inode->i_pipe) {
409 unsigned long page = (unsigned long) PIPE_BASE(*inode);
410 PIPE_BASE(*inode) = NULL;
411 free_page(page);
412 }
413
414 if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
415 inode->i_sb->s_op->put_inode(inode);
416 if (!inode->i_nlink)
417 return;
418 }
419
420 if (inode->i_dirt) {
421 write_inode(inode);
422 wait_on_inode(inode);
423 goto repeat;
424 }
425
426 inode->i_count--;
427 if (IS_WRITABLE(inode)) {
428 if (inode->i_sb && inode->i_sb->dq_op)
429 inode->i_sb->dq_op->drop(inode);
430 }
431
432 if (inode->i_mmap) {
433 printk("iput: inode %lu on device %s still has mappings.\n",
434 inode->i_ino, kdevname(inode->i_dev));
435 inode->i_mmap = NULL;
436 }
437
438 nr_free_inodes++;
439 return;
440 }
441
442 static inline unsigned long value(struct inode * inode)
443 {
444 if (inode->i_lock)
445 return 1000;
446 if (inode->i_dirt)
447 return 1000;
448 return inode->i_nrpages;
449 }
450
451 struct inode * get_empty_inode(void)
452 {
453 static int ino = 0;
454 struct inode * inode, * best;
455 unsigned long badness = 1000;
456 int i;
457
458 if (nr_inodes < NR_INODE && nr_free_inodes < (nr_inodes >> 1))
459 grow_inodes();
460 repeat:
461 inode = first_inode;
462 best = NULL;
463 for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
464 if (!inode->i_count) {
465 unsigned long i = value(inode);
466 if (i < badness) {
467 best = inode;
468 if ((badness = i) == 0)
469 break;
470 }
471 }
472 }
473 if (badness)
474 if (nr_inodes < NR_INODE) {
475 if (grow_inodes() == 0)
476 goto repeat;
477 }
478 inode = best;
479 if (!inode) {
480 printk("VFS: No free inodes - contact Linus\n");
481 sleep_on(&inode_wait);
482 goto repeat;
483 }
484 if (inode->i_lock) {
485 wait_on_inode(inode);
486 goto repeat;
487 }
488 if (inode->i_dirt) {
489 write_inode(inode);
490 goto repeat;
491 }
492 if (inode->i_count)
493 goto repeat;
494 clear_inode(inode);
495 inode->i_count = 1;
496 inode->i_nlink = 1;
497 inode->i_version = ++event;
498 inode->i_sem.count = 1;
499 inode->i_ino = ++ino;
500 inode->i_dev = 0;
501 nr_free_inodes--;
502 if (nr_free_inodes < 0) {
503 printk ("VFS: get_empty_inode: bad free inode count.\n");
504 nr_free_inodes = 0;
505 }
506 return inode;
507 }
508
509 struct inode * get_pipe_inode(void)
510 {
511 struct inode * inode;
512 extern struct inode_operations pipe_inode_operations;
513
514 if (!(inode = get_empty_inode()))
515 return NULL;
516 if (!(PIPE_BASE(*inode) = (char*) __get_free_page(GFP_USER))) {
517 iput(inode);
518 return NULL;
519 }
520 inode->i_op = &pipe_inode_operations;
521 inode->i_count = 2;
522 PIPE_WAIT(*inode) = NULL;
523 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
524 PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
525 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
526 PIPE_LOCK(*inode) = 0;
527 inode->i_pipe = 1;
528 inode->i_mode |= S_IFIFO | S_IRUSR | S_IWUSR;
529 inode->i_uid = current->fsuid;
530 inode->i_gid = current->fsgid;
531 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
532 inode->i_blksize = PAGE_SIZE;
533 return inode;
534 }
535
536 struct inode *__iget(struct super_block * sb, int nr, int crossmntp)
537 {
538 static struct wait_queue * update_wait = NULL;
539 struct inode_hash_entry * h;
540 struct inode * inode;
541 struct inode * empty = NULL;
542
543 if (!sb)
544 panic("VFS: iget with sb==NULL");
545 h = hash(sb->s_dev, nr);
546 repeat:
547 for (inode = h->inode; inode ; inode = inode->i_hash_next)
548 if (inode->i_dev == sb->s_dev && inode->i_ino == nr)
549 goto found_it;
550 if (!empty) {
551 h->updating++;
552 empty = get_empty_inode();
553 if (!--h->updating)
554 wake_up(&update_wait);
555 if (empty)
556 goto repeat;
557 return (NULL);
558 }
559 inode = empty;
560 inode->i_sb = sb;
561 inode->i_dev = sb->s_dev;
562 inode->i_ino = nr;
563 inode->i_flags = sb->s_flags;
564 put_last_free(inode);
565 insert_inode_hash(inode);
566 read_inode(inode);
567 goto return_it;
568
569 found_it:
570 if (!inode->i_count)
571 nr_free_inodes--;
572 inode->i_count++;
573 wait_on_inode(inode);
574 if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
575 printk("Whee.. inode changed from under us. Tell Linus\n");
576 iput(inode);
577 goto repeat;
578 }
579 if (crossmntp && inode->i_mount) {
580 struct inode * tmp = inode->i_mount;
581 tmp->i_count++;
582 iput(inode);
583 inode = tmp;
584 wait_on_inode(inode);
585 }
586 if (empty)
587 iput(empty);
588
589 return_it:
590 while (h->updating)
591 sleep_on(&update_wait);
592 return inode;
593 }
594
595
596
597
598
599
600
601 static void __wait_on_inode(struct inode * inode)
602 {
603 struct wait_queue wait = { current, NULL };
604
605 add_wait_queue(&inode->i_wait, &wait);
606 repeat:
607 current->state = TASK_UNINTERRUPTIBLE;
608 if (inode->i_lock) {
609 schedule();
610 goto repeat;
611 }
612 remove_wait_queue(&inode->i_wait, &wait);
613 current->state = TASK_RUNNING;
614 }