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