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