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
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 inode->i_count--;
433 if (IS_WRITABLE(inode)) {
434 if (inode->i_sb && inode->i_sb->dq_op)
435 inode->i_sb->dq_op->drop(inode);
436 }
437
438 if (inode->i_mmap) {
439 printk("iput: inode %lu on device %s still has mappings.\n",
440 inode->i_ino, kdevname(inode->i_dev));
441 inode->i_mmap = NULL;
442 }
443
444 nr_free_inodes++;
445 return;
446 }
447
448 static inline unsigned long value(struct inode * inode)
449 {
450 if (inode->i_lock)
451 return 1000;
452 if (inode->i_dirt)
453 return 1000;
454 return inode->i_nrpages;
455 }
456
457 struct inode * get_empty_inode(void)
458 {
459 static int ino = 0;
460 struct inode * inode, * best;
461 unsigned long badness = 1000;
462 int i;
463
464 if (nr_inodes < max_inodes && nr_free_inodes < (nr_inodes >> 1))
465 grow_inodes();
466 repeat:
467 inode = first_inode;
468 best = NULL;
469 for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
470 if (!inode->i_count) {
471 unsigned long i = value(inode);
472 if (i < badness) {
473 best = inode;
474 if ((badness = i) == 0)
475 break;
476 }
477 }
478 }
479 if (badness)
480 if (nr_inodes < max_inodes) {
481 if (grow_inodes() == 0)
482 goto repeat;
483 }
484 inode = best;
485 if (!inode) {
486 printk("VFS: No free inodes - contact Linus\n");
487 sleep_on(&inode_wait);
488 goto repeat;
489 }
490 if (inode->i_lock) {
491 wait_on_inode(inode);
492 goto repeat;
493 }
494 if (inode->i_dirt) {
495 write_inode(inode);
496 goto repeat;
497 }
498 if (inode->i_count)
499 goto repeat;
500 clear_inode(inode);
501 inode->i_count = 1;
502 inode->i_nlink = 1;
503 inode->i_version = ++event;
504 inode->i_sem.count = 1;
505 inode->i_ino = ++ino;
506 inode->i_dev = 0;
507 nr_free_inodes--;
508 if (nr_free_inodes < 0) {
509 printk ("VFS: get_empty_inode: bad free inode count.\n");
510 nr_free_inodes = 0;
511 }
512 return inode;
513 }
514
515 struct inode * get_pipe_inode(void)
516 {
517 struct inode * inode;
518 extern struct inode_operations pipe_inode_operations;
519
520 if (!(inode = get_empty_inode()))
521 return NULL;
522 if (!(PIPE_BASE(*inode) = (char*) __get_free_page(GFP_USER))) {
523 iput(inode);
524 return NULL;
525 }
526 inode->i_op = &pipe_inode_operations;
527 inode->i_count = 2;
528 PIPE_WAIT(*inode) = NULL;
529 PIPE_START(*inode) = PIPE_LEN(*inode) = 0;
530 PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
531 PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
532 PIPE_LOCK(*inode) = 0;
533 inode->i_pipe = 1;
534 inode->i_mode |= S_IFIFO | S_IRUSR | S_IWUSR;
535 inode->i_uid = current->fsuid;
536 inode->i_gid = current->fsgid;
537 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
538 inode->i_blksize = PAGE_SIZE;
539 return inode;
540 }
541
542 struct inode *__iget(struct super_block * sb, int nr, int crossmntp)
543 {
544 static struct wait_queue * update_wait = NULL;
545 struct inode_hash_entry * h;
546 struct inode * inode;
547 struct inode * empty = NULL;
548
549 if (!sb)
550 panic("VFS: iget with sb==NULL");
551 h = hash(sb->s_dev, nr);
552 repeat:
553 for (inode = h->inode; inode ; inode = inode->i_hash_next)
554 if (inode->i_dev == sb->s_dev && inode->i_ino == nr)
555 goto found_it;
556 if (!empty) {
557 h->updating++;
558 empty = get_empty_inode();
559 if (!--h->updating)
560 wake_up(&update_wait);
561 if (empty)
562 goto repeat;
563 return (NULL);
564 }
565 inode = empty;
566 inode->i_sb = sb;
567 inode->i_dev = sb->s_dev;
568 inode->i_ino = nr;
569 inode->i_flags = sb->s_flags;
570 put_last_free(inode);
571 insert_inode_hash(inode);
572 read_inode(inode);
573 goto return_it;
574
575 found_it:
576 if (!inode->i_count)
577 nr_free_inodes--;
578 inode->i_count++;
579 wait_on_inode(inode);
580 if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
581 printk("Whee.. inode changed from under us. Tell Linus\n");
582 iput(inode);
583 goto repeat;
584 }
585 if (crossmntp && inode->i_mount) {
586 struct inode * tmp = inode->i_mount;
587 tmp->i_count++;
588 iput(inode);
589 inode = tmp;
590 wait_on_inode(inode);
591 }
592 if (empty)
593 iput(empty);
594
595 return_it:
596 while (h->updating)
597 sleep_on(&update_wait);
598 return inode;
599 }
600
601
602
603
604
605
606
607 static void __wait_on_inode(struct inode * inode)
608 {
609 struct wait_queue wait = { current, NULL };
610
611 add_wait_queue(&inode->i_wait, &wait);
612 repeat:
613 current->state = TASK_UNINTERRUPTIBLE;
614 if (inode->i_lock) {
615 schedule();
616 goto repeat;
617 }
618 remove_wait_queue(&inode->i_wait, &wait);
619 current->state = TASK_RUNNING;
620 }