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
313 int notify_change(struct inode * inode, struct iattr *attr)
314 {
315 int retval;
316
317 if (inode->i_sb && inode->i_sb->s_op &&
318 inode->i_sb->s_op->notify_change)
319 return inode->i_sb->s_op->notify_change(inode, attr);
320
321 if ((retval = inode_change_ok(inode, attr)) != 0)
322 return retval;
323
324 inode_setattr(inode, attr);
325 return 0;
326 }
327
328
329
330
331
332
333
334
335
336
337
338 int bmap(struct inode * inode, int block)
339 {
340 if (inode->i_op && inode->i_op->bmap)
341 return inode->i_op->bmap(inode,block);
342 return 0;
343 }
344
345 void invalidate_inodes(kdev_t dev)
346 {
347 struct inode * inode, * next;
348 int i;
349
350 next = first_inode;
351 for(i = nr_inodes ; i > 0 ; i--) {
352 inode = next;
353 next = inode->i_next;
354 if (inode->i_dev != dev)
355 continue;
356 if (inode->i_count || inode->i_dirt || inode->i_lock) {
357 printk("VFS: inode busy on removed device %s\n",
358 kdevname(dev));
359 continue;
360 }
361 clear_inode(inode);
362 }
363 }
364
365 void sync_inodes(kdev_t dev)
366 {
367 int i;
368 struct inode * inode;
369
370 inode = first_inode;
371 for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
372 if (dev && inode->i_dev != dev)
373 continue;
374 wait_on_inode(inode);
375 if (inode->i_dirt)
376 write_inode(inode);
377 }
378 }
379
380 void iput(struct inode * inode)
381 {
382 if (!inode)
383 return;
384 wait_on_inode(inode);
385 if (!inode->i_count) {
386 printk("VFS: iput: trying to free free inode\n");
387 printk("VFS: device %s, inode %lu, mode=0%07o\n",
388 kdevname(inode->i_rdev), inode->i_ino, inode->i_mode);
389 return;
390 }
391 if (inode->i_pipe)
392 wake_up_interruptible(&PIPE_WAIT(*inode));
393 repeat:
394 if (inode->i_count>1) {
395 inode->i_count--;
396 return;
397 }
398
399 wake_up(&inode_wait);
400 if (inode->i_pipe) {
401 unsigned long page = (unsigned long) PIPE_BASE(*inode);
402 PIPE_BASE(*inode) = NULL;
403 free_page(page);
404 }
405
406 if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
407 inode->i_sb->s_op->put_inode(inode);
408 if (!inode->i_nlink)
409 return;
410 }
411
412 if (inode->i_dirt) {
413 write_inode(inode);
414 wait_on_inode(inode);
415 goto repeat;
416 }
417
418 inode->i_count--;
419 if (IS_WRITABLE(inode)) {
420 if (inode->i_sb && inode->i_sb->dq_op)
421 inode->i_sb->dq_op->drop(inode);
422 }
423
424 if (inode->i_mmap) {
425 printk("iput: inode %lu on device %s still has mappings.\n",
426 inode->i_ino, kdevname(inode->i_dev));
427 inode->i_mmap = NULL;
428 }
429
430 nr_free_inodes++;
431 return;
432 }
433
434 static inline unsigned long value(struct inode * inode)
435 {
436 if (inode->i_lock)
437 return 1000;
438 if (inode->i_dirt)
439 return 1000;
440 return inode->i_nrpages;
441 }
442
443 struct inode * get_empty_inode(void)
444 {
445 static int ino = 0;
446 struct inode * inode, * best;
447 unsigned long badness = 1000;
448 int i;
449
450 if (nr_inodes < NR_INODE && nr_free_inodes < (nr_inodes >> 1))
451 grow_inodes();
452 repeat:
453 inode = first_inode;
454 best = NULL;
455 for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
456 if (!inode->i_count) {
457 unsigned long i = value(inode);
458 if (i < badness) {
459 best = inode;
460 if ((badness = i) == 0)
461 break;
462 }
463 }
464 }
465 if (badness)
466 if (nr_inodes < NR_INODE) {
467 if (grow_inodes() == 0)
468 goto repeat;
469 }
470 inode = best;
471 if (!inode) {
472 printk("VFS: No free inodes - contact Linus\n");
473 sleep_on(&inode_wait);
474 goto repeat;
475 }
476 if (inode->i_lock) {
477 wait_on_inode(inode);
478 goto repeat;
479 }
480 if (inode->i_dirt) {
481 write_inode(inode);
482 goto repeat;
483 }
484 if (inode->i_count)
485 goto repeat;
486 clear_inode(inode);
487 inode->i_count = 1;
488 inode->i_nlink = 1;
489 inode->i_version = ++event;
490 inode->i_sem.count = 1;
491 inode->i_ino = ++ino;
492 inode->i_dev = 0;
493 nr_free_inodes--;
494 if (nr_free_inodes < 0) {
495 printk ("VFS: get_empty_inode: bad free inode count.\n");
496 nr_free_inodes = 0;
497 }
498 return inode;
499 }
500
501 struct inode * get_pipe_inode(void)
502 {
503 struct inode * inode;
504 extern struct inode_operations pipe_inode_operations;
505
506 if (!(inode = get_empty_inode()))
507 return NULL;
508 if (!(PIPE_BASE(*inode) = (char*) __get_free_page(GFP_USER))) {
509 iput(inode);
510 return NULL;
511 }
512 inode->i_op = &pipe_inode_operations;
513 inode->i_count = 2;
514 PIPE_WAIT(*inode) = NULL;
515 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
516 PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
517 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
518 PIPE_LOCK(*inode) = 0;
519 inode->i_pipe = 1;
520 inode->i_mode |= S_IFIFO | S_IRUSR | S_IWUSR;
521 inode->i_uid = current->fsuid;
522 inode->i_gid = current->fsgid;
523 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
524 inode->i_blksize = PAGE_SIZE;
525 return inode;
526 }
527
528 struct inode *__iget(struct super_block * sb, int nr, int crossmntp)
529 {
530 static struct wait_queue * update_wait = NULL;
531 struct inode_hash_entry * h;
532 struct inode * inode;
533 struct inode * empty = NULL;
534
535 if (!sb)
536 panic("VFS: iget with sb==NULL");
537 h = hash(sb->s_dev, nr);
538 repeat:
539 for (inode = h->inode; inode ; inode = inode->i_hash_next)
540 if (inode->i_dev == sb->s_dev && inode->i_ino == nr)
541 goto found_it;
542 if (!empty) {
543 h->updating++;
544 empty = get_empty_inode();
545 if (!--h->updating)
546 wake_up(&update_wait);
547 if (empty)
548 goto repeat;
549 return (NULL);
550 }
551 inode = empty;
552 inode->i_sb = sb;
553 inode->i_dev = sb->s_dev;
554 inode->i_ino = nr;
555 inode->i_flags = sb->s_flags;
556 put_last_free(inode);
557 insert_inode_hash(inode);
558 read_inode(inode);
559 goto return_it;
560
561 found_it:
562 if (!inode->i_count)
563 nr_free_inodes--;
564 inode->i_count++;
565 wait_on_inode(inode);
566 if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
567 printk("Whee.. inode changed from under us. Tell Linus\n");
568 iput(inode);
569 goto repeat;
570 }
571 if (crossmntp && inode->i_mount) {
572 struct inode * tmp = inode->i_mount;
573 tmp->i_count++;
574 iput(inode);
575 inode = tmp;
576 wait_on_inode(inode);
577 }
578 if (empty)
579 iput(empty);
580
581 return_it:
582 while (h->updating)
583 sleep_on(&update_wait);
584 return inode;
585 }
586
587
588
589
590
591
592
593 static void __wait_on_inode(struct inode * inode)
594 {
595 struct wait_queue wait = { current, NULL };
596
597 add_wait_queue(&inode->i_wait, &wait);
598 repeat:
599 current->state = TASK_UNINTERRUPTIBLE;
600 if (inode->i_lock) {
601 schedule();
602 goto repeat;
603 }
604 remove_wait_queue(&inode->i_wait, &wait);
605 current->state = TASK_RUNNING;
606 }