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