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