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