Import 2.3.1pre2
[davej-history.git] / fs / minix / inode.c
blobf454c1eadc70d29a8d6ae9bfc9457592d2dd9616
1 /*
2 * linux/fs/minix/inode.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 1996 Gertjan van Wingerde (gertjan@cs.vu.nl)
7 * Minix V2 fs support.
9 * Modified for 680x0 by Andreas Schwab
12 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/mm.h>
17 #include <linux/malloc.h>
18 #include <linux/string.h>
19 #include <linux/stat.h>
20 #include <linux/locks.h>
21 #include <linux/init.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
25 #include <asm/bitops.h>
27 #include <linux/minix_fs.h>
29 static void minix_read_inode(struct inode * inode);
30 static void minix_write_inode(struct inode * inode);
31 static int minix_statfs(struct super_block *sb, struct statfs *buf, int bufsiz);
32 static int minix_remount (struct super_block * sb, int * flags, char * data);
34 static void minix_delete_inode(struct inode *inode)
36 inode->i_size = 0;
37 minix_truncate(inode);
38 minix_free_inode(inode);
41 static void minix_commit_super(struct super_block * sb)
43 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
44 sb->s_dirt = 0;
47 static void minix_write_super(struct super_block * sb)
49 struct minix_super_block * ms;
51 if (!(sb->s_flags & MS_RDONLY)) {
52 ms = sb->u.minix_sb.s_ms;
54 if (ms->s_state & MINIX_VALID_FS)
55 ms->s_state &= ~MINIX_VALID_FS;
56 minix_commit_super(sb);
58 sb->s_dirt = 0;
62 static void minix_put_super(struct super_block *sb)
64 int i;
66 if (!(sb->s_flags & MS_RDONLY)) {
67 sb->u.minix_sb.s_ms->s_state = sb->u.minix_sb.s_mount_state;
68 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
70 for (i = 0; i < sb->u.minix_sb.s_imap_blocks; i++)
71 brelse(sb->u.minix_sb.s_imap[i]);
72 for (i = 0; i < sb->u.minix_sb.s_zmap_blocks; i++)
73 brelse(sb->u.minix_sb.s_zmap[i]);
74 brelse (sb->u.minix_sb.s_sbh);
75 kfree(sb->u.minix_sb.s_imap);
77 MOD_DEC_USE_COUNT;
78 return;
81 static struct super_operations minix_sops = {
82 minix_read_inode,
83 minix_write_inode,
84 NULL, /* put_inode */
85 minix_delete_inode,
86 NULL, /* notify_change */
87 minix_put_super,
88 minix_write_super,
89 minix_statfs,
90 minix_remount
93 static int minix_remount (struct super_block * sb, int * flags, char * data)
95 struct minix_super_block * ms;
97 ms = sb->u.minix_sb.s_ms;
98 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
99 return 0;
100 if (*flags & MS_RDONLY) {
101 if (ms->s_state & MINIX_VALID_FS ||
102 !(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
103 return 0;
104 /* Mounting a rw partition read-only. */
105 ms->s_state = sb->u.minix_sb.s_mount_state;
106 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
107 sb->s_dirt = 1;
108 minix_commit_super(sb);
110 else {
111 /* Mount a partition which is read-only, read-write. */
112 sb->u.minix_sb.s_mount_state = ms->s_state;
113 ms->s_state &= ~MINIX_VALID_FS;
114 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
115 sb->s_dirt = 1;
117 if (!(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
118 printk ("MINIX-fs warning: remounting unchecked fs, "
119 "running fsck is recommended.\n");
120 else if ((sb->u.minix_sb.s_mount_state & MINIX_ERROR_FS))
121 printk ("MINIX-fs warning: remounting fs with errors, "
122 "running fsck is recommended.\n");
124 return 0;
128 * Check the root directory of the filesystem to make sure
129 * it really _is_ a Minix filesystem, and to check the size
130 * of the directory entry.
132 static const char * minix_checkroot(struct super_block *s, struct inode *dir)
134 struct buffer_head *bh;
135 struct minix_dir_entry *de;
136 const char * errmsg;
137 int dirsize;
139 if (!S_ISDIR(dir->i_mode))
140 return "root directory is not a directory";
142 bh = minix_bread(dir, 0, 0);
143 if (!bh)
144 return "unable to read root directory";
146 de = (struct minix_dir_entry *) bh->b_data;
147 errmsg = "bad root directory '.' entry";
148 dirsize = BLOCK_SIZE;
149 if (de->inode == MINIX_ROOT_INO && strcmp(de->name, ".") == 0) {
150 errmsg = "bad root directory '..' entry";
151 dirsize = 8;
154 while ((dirsize <<= 1) < BLOCK_SIZE) {
155 de = (struct minix_dir_entry *) (bh->b_data + dirsize);
156 if (de->inode != MINIX_ROOT_INO)
157 continue;
158 if (strcmp(de->name, ".."))
159 continue;
160 s->u.minix_sb.s_dirsize = dirsize;
161 s->u.minix_sb.s_namelen = dirsize - 2;
162 errmsg = NULL;
163 break;
165 brelse(bh);
166 return errmsg;
169 static struct super_block *minix_read_super(struct super_block *s, void *data,
170 int silent)
172 struct buffer_head *bh;
173 struct buffer_head **map;
174 struct minix_super_block *ms;
175 int i, block;
176 kdev_t dev = s->s_dev;
177 const char * errmsg;
178 struct inode *root_inode;
180 /* N.B. These should be compile-time tests.
181 Unfortunately that is impossible. */
182 if (32 != sizeof (struct minix_inode))
183 panic("bad V1 i-node size");
184 if (64 != sizeof(struct minix2_inode))
185 panic("bad V2 i-node size");
187 MOD_INC_USE_COUNT;
188 lock_super(s);
189 set_blocksize(dev, BLOCK_SIZE);
190 if (!(bh = bread(dev,1,BLOCK_SIZE)))
191 goto out_bad_sb;
193 ms = (struct minix_super_block *) bh->b_data;
194 s->u.minix_sb.s_ms = ms;
195 s->u.minix_sb.s_sbh = bh;
196 s->u.minix_sb.s_mount_state = ms->s_state;
197 s->s_blocksize = BLOCK_SIZE;
198 s->s_blocksize_bits = BLOCK_SIZE_BITS;
199 s->u.minix_sb.s_ninodes = ms->s_ninodes;
200 s->u.minix_sb.s_nzones = ms->s_nzones;
201 s->u.minix_sb.s_imap_blocks = ms->s_imap_blocks;
202 s->u.minix_sb.s_zmap_blocks = ms->s_zmap_blocks;
203 s->u.minix_sb.s_firstdatazone = ms->s_firstdatazone;
204 s->u.minix_sb.s_log_zone_size = ms->s_log_zone_size;
205 s->u.minix_sb.s_max_size = ms->s_max_size;
206 s->s_magic = ms->s_magic;
207 if (s->s_magic == MINIX_SUPER_MAGIC) {
208 s->u.minix_sb.s_version = MINIX_V1;
209 s->u.minix_sb.s_dirsize = 16;
210 s->u.minix_sb.s_namelen = 14;
211 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
212 } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
213 s->u.minix_sb.s_version = MINIX_V1;
214 s->u.minix_sb.s_dirsize = 32;
215 s->u.minix_sb.s_namelen = 30;
216 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
217 } else if (s->s_magic == MINIX2_SUPER_MAGIC) {
218 s->u.minix_sb.s_version = MINIX_V2;
219 s->u.minix_sb.s_nzones = ms->s_zones;
220 s->u.minix_sb.s_dirsize = 16;
221 s->u.minix_sb.s_namelen = 14;
222 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
223 } else if (s->s_magic == MINIX2_SUPER_MAGIC2) {
224 s->u.minix_sb.s_version = MINIX_V2;
225 s->u.minix_sb.s_nzones = ms->s_zones;
226 s->u.minix_sb.s_dirsize = 32;
227 s->u.minix_sb.s_namelen = 30;
228 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
229 } else
230 goto out_no_fs;
233 * Allocate the buffer map to keep the superblock small.
235 i = (s->u.minix_sb.s_imap_blocks + s->u.minix_sb.s_zmap_blocks) * sizeof(bh);
236 map = kmalloc(i, GFP_KERNEL);
237 if (!map)
238 goto out_no_map;
239 memset(map, 0, i);
240 s->u.minix_sb.s_imap = &map[0];
241 s->u.minix_sb.s_zmap = &map[s->u.minix_sb.s_imap_blocks];
243 block=2;
244 for (i=0 ; i < s->u.minix_sb.s_imap_blocks ; i++) {
245 if (!(s->u.minix_sb.s_imap[i]=bread(dev,block,BLOCK_SIZE)))
246 goto out_no_bitmap;
247 block++;
249 for (i=0 ; i < s->u.minix_sb.s_zmap_blocks ; i++) {
250 if (!(s->u.minix_sb.s_zmap[i]=bread(dev,block,BLOCK_SIZE)))
251 goto out_no_bitmap;
252 block++;
255 minix_set_bit(0,s->u.minix_sb.s_imap[0]->b_data);
256 minix_set_bit(0,s->u.minix_sb.s_zmap[0]->b_data);
257 /* set up enough so that it can read an inode */
258 s->s_op = &minix_sops;
259 root_inode = iget(s, MINIX_ROOT_INO);
260 if (!root_inode)
261 goto out_no_root;
263 * Check the fs before we get the root dentry ...
265 errmsg = minix_checkroot(s, root_inode);
266 if (errmsg)
267 goto out_bad_root;
269 s->s_root = d_alloc_root(root_inode, NULL);
270 if (!s->s_root)
271 goto out_iput;
273 s->s_root->d_op = &minix_dentry_operations;
275 if (!(s->s_flags & MS_RDONLY)) {
276 ms->s_state &= ~MINIX_VALID_FS;
277 mark_buffer_dirty(bh, 1);
278 s->s_dirt = 1;
280 unlock_super(s);
281 if (!(s->u.minix_sb.s_mount_state & MINIX_VALID_FS))
282 printk ("MINIX-fs: mounting unchecked file system, "
283 "running fsck is recommended.\n");
284 else if (s->u.minix_sb.s_mount_state & MINIX_ERROR_FS)
285 printk ("MINIX-fs: mounting file system with errors, "
286 "running fsck is recommended.\n");
287 return s;
289 out_bad_root:
290 if (!silent)
291 printk("MINIX-fs: %s\n", errmsg);
292 out_iput:
293 iput(root_inode);
294 goto out_freemap;
296 out_no_root:
297 if (!silent)
298 printk("MINIX-fs: get root inode failed\n");
299 goto out_freemap;
301 out_no_bitmap:
302 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
303 out_freemap:
304 for (i = 0; i < s->u.minix_sb.s_imap_blocks; i++)
305 brelse(s->u.minix_sb.s_imap[i]);
306 for (i = 0; i < s->u.minix_sb.s_zmap_blocks; i++)
307 brelse(s->u.minix_sb.s_zmap[i]);
308 kfree(s->u.minix_sb.s_imap);
309 goto out_release;
311 out_no_map:
312 if (!silent)
313 printk ("MINIX-fs: can't allocate map\n");
314 goto out_release;
316 out_no_fs:
317 if (!silent)
318 printk("VFS: Can't find a Minix or Minix V2 filesystem on device "
319 "%s.\n", kdevname(dev));
320 out_release:
321 brelse(bh);
322 goto out_unlock;
324 out_bad_sb:
325 printk("MINIX-fs: unable to read superblock\n");
326 out_unlock:
327 s->s_dev = 0;
328 unlock_super(s);
329 MOD_DEC_USE_COUNT;
330 return NULL;
333 static int minix_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
335 struct statfs tmp;
337 tmp.f_type = sb->s_magic;
338 tmp.f_bsize = sb->s_blocksize;
339 tmp.f_blocks = (sb->u.minix_sb.s_nzones - sb->u.minix_sb.s_firstdatazone) << sb->u.minix_sb.s_log_zone_size;
340 tmp.f_bfree = minix_count_free_blocks(sb);
341 tmp.f_bavail = tmp.f_bfree;
342 tmp.f_files = sb->u.minix_sb.s_ninodes;
343 tmp.f_ffree = minix_count_free_inodes(sb);
344 tmp.f_namelen = sb->u.minix_sb.s_namelen;
345 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
349 * The minix V1 fs bmap functions.
351 #define V1_inode_bmap(inode,nr) (((unsigned short *)(inode)->u.minix_i.u.i1_data)[(nr)])
353 static int V1_block_bmap(struct buffer_head * bh, int nr)
355 int tmp;
357 if (!bh)
358 return 0;
359 tmp = ((unsigned short *) bh->b_data)[nr];
360 brelse(bh);
361 return tmp;
364 static int V1_minix_bmap(struct inode * inode,int block)
366 int i;
368 if (block<0) {
369 printk("minix_bmap: block<0");
370 return 0;
372 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
373 printk("minix_bmap: block>big");
374 return 0;
376 if (block < 7)
377 return V1_inode_bmap(inode,block);
378 block -= 7;
379 if (block < 512) {
380 i = V1_inode_bmap(inode,7);
381 if (!i)
382 return 0;
383 return V1_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block);
385 block -= 512;
386 i = V1_inode_bmap(inode,8);
387 if (!i)
388 return 0;
389 i = V1_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block>>9);
390 if (!i)
391 return 0;
392 return V1_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block & 511);
396 * The minix V2 fs bmap functions.
398 #define V2_inode_bmap(inode,nr) (((unsigned long *)(inode)->u.minix_i.u.i2_data)[(nr)])
399 static int V2_block_bmap(struct buffer_head * bh, int nr)
401 int tmp;
403 if (!bh)
404 return 0;
405 tmp = ((unsigned long *) bh->b_data)[nr];
406 brelse(bh);
407 return tmp;
410 static int V2_minix_bmap(struct inode * inode,int block)
412 int i;
414 if (block<0) {
415 printk("minix_bmap: block<0");
416 return 0;
418 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
419 printk("minix_bmap: block>big");
420 return 0;
422 if (block < 7)
423 return V2_inode_bmap(inode,block);
424 block -= 7;
425 if (block < 256) {
426 i = V2_inode_bmap(inode,7);
427 if (!i)
428 return 0;
429 return V2_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block);
431 block -= 256;
432 if (block < 256*256) {
433 i = V2_inode_bmap(inode,8);
434 if (!i)
435 return 0;
436 i = V2_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block >> 8);
437 if (!i)
438 return 0;
439 return V2_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block & 255);
441 block -= 256*256;
442 i = V2_inode_bmap(inode,9);
443 if (!i)
444 return 0;
445 i = V2_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block >> 16);
446 if (!i)
447 return 0;
448 i = V2_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),(block >> 8) & 255);
449 if (!i)
450 return 0;
451 return V2_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block & 255);
455 * The global minix fs bmap function.
457 int minix_bmap(struct inode * inode,int block)
459 if (INODE_VERSION(inode) == MINIX_V1)
460 return V1_minix_bmap(inode, block);
461 else
462 return V2_minix_bmap(inode, block);
466 * The minix V1 fs getblk functions.
468 static struct buffer_head * V1_inode_getblk(struct inode * inode, int nr,
469 int create)
471 int tmp;
472 unsigned short *p;
473 struct buffer_head * result;
475 p = inode->u.minix_i.u.i1_data + nr;
476 repeat:
477 tmp = *p;
478 if (tmp) {
479 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
480 if (tmp == *p)
481 return result;
482 brelse(result);
483 goto repeat;
485 if (!create)
486 return NULL;
487 tmp = minix_new_block(inode->i_sb);
488 if (!tmp)
489 return NULL;
490 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
491 if (*p) {
492 minix_free_block(inode->i_sb,tmp);
493 brelse(result);
494 goto repeat;
496 *p = tmp;
497 inode->i_ctime = CURRENT_TIME;
498 mark_inode_dirty(inode);
499 return result;
502 static struct buffer_head * V1_block_getblk(struct inode * inode,
503 struct buffer_head * bh, int nr, int create)
505 int tmp;
506 unsigned short *p;
507 struct buffer_head * result;
509 if (!bh)
510 return NULL;
511 if (!buffer_uptodate(bh)) {
512 ll_rw_block(READ, 1, &bh);
513 wait_on_buffer(bh);
514 if (!buffer_uptodate(bh)) {
515 brelse(bh);
516 return NULL;
519 p = nr + (unsigned short *) bh->b_data;
520 repeat:
521 tmp = *p;
522 if (tmp) {
523 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
524 if (tmp == *p) {
525 brelse(bh);
526 return result;
528 brelse(result);
529 goto repeat;
531 if (!create) {
532 brelse(bh);
533 return NULL;
535 tmp = minix_new_block(inode->i_sb);
536 if (!tmp) {
537 brelse(bh);
538 return NULL;
540 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
541 if (*p) {
542 minix_free_block(inode->i_sb,tmp);
543 brelse(result);
544 goto repeat;
546 *p = tmp;
547 mark_buffer_dirty(bh, 1);
548 brelse(bh);
549 return result;
552 static struct buffer_head * V1_minix_getblk(struct inode * inode, int block,
553 int create)
555 struct buffer_head * bh;
557 if (block<0) {
558 printk("minix_getblk: block<0");
559 return NULL;
561 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE) {
562 printk("minix_getblk: block>big");
563 return NULL;
565 if (block < 7)
566 return V1_inode_getblk(inode,block,create);
567 block -= 7;
568 if (block < 512) {
569 bh = V1_inode_getblk(inode,7,create);
570 return V1_block_getblk(inode, bh, block, create);
572 block -= 512;
573 bh = V1_inode_getblk(inode,8,create);
574 bh = V1_block_getblk(inode, bh, (block>>9) & 511, create);
575 return V1_block_getblk(inode, bh, block & 511, create);
579 * The minix V2 fs getblk functions.
581 static struct buffer_head * V2_inode_getblk(struct inode * inode, int nr,
582 int create)
584 int tmp;
585 unsigned long *p;
586 struct buffer_head * result;
588 p = (unsigned long *) inode->u.minix_i.u.i2_data + nr;
589 repeat:
590 tmp = *p;
591 if (tmp) {
592 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
593 if (tmp == *p)
594 return result;
595 brelse(result);
596 goto repeat;
598 if (!create)
599 return NULL;
600 tmp = minix_new_block(inode->i_sb);
601 if (!tmp)
602 return NULL;
603 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
604 if (*p) {
605 minix_free_block(inode->i_sb,tmp);
606 brelse(result);
607 goto repeat;
609 *p = tmp;
610 inode->i_ctime = CURRENT_TIME;
611 mark_inode_dirty(inode);
612 return result;
615 static struct buffer_head * V2_block_getblk(struct inode * inode,
616 struct buffer_head * bh, int nr, int create)
618 int tmp;
619 unsigned long *p;
620 struct buffer_head * result;
622 if (!bh)
623 return NULL;
624 if (!buffer_uptodate(bh)) {
625 ll_rw_block(READ, 1, &bh);
626 wait_on_buffer(bh);
627 if (!buffer_uptodate(bh)) {
628 brelse(bh);
629 return NULL;
632 p = nr + (unsigned long *) bh->b_data;
633 repeat:
634 tmp = *p;
635 if (tmp) {
636 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
637 if (tmp == *p) {
638 brelse(bh);
639 return result;
641 brelse(result);
642 goto repeat;
644 if (!create) {
645 brelse(bh);
646 return NULL;
648 tmp = minix_new_block(inode->i_sb);
649 if (!tmp) {
650 brelse(bh);
651 return NULL;
653 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
654 if (*p) {
655 minix_free_block(inode->i_sb,tmp);
656 brelse(result);
657 goto repeat;
659 *p = tmp;
660 mark_buffer_dirty(bh, 1);
661 brelse(bh);
662 return result;
665 static struct buffer_head * V2_minix_getblk(struct inode * inode, int block,
666 int create)
668 struct buffer_head * bh;
670 if (block<0) {
671 printk("minix_getblk: block<0");
672 return NULL;
674 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE) {
675 printk("minix_getblk: block>big");
676 return NULL;
678 if (block < 7)
679 return V2_inode_getblk(inode,block,create);
680 block -= 7;
681 if (block < 256) {
682 bh = V2_inode_getblk(inode,7,create);
683 return V2_block_getblk(inode, bh, block, create);
685 block -= 256;
686 if (block < 256*256) {
687 bh = V2_inode_getblk(inode,8,create);
688 bh = V2_block_getblk(inode, bh, (block>>8) & 255, create);
689 return V2_block_getblk(inode, bh, block & 255, create);
691 block -= 256*256;
692 bh = V2_inode_getblk(inode,9,create);
693 bh = V2_block_getblk(inode, bh, (block >> 16) & 255, create);
694 bh = V2_block_getblk(inode, bh, (block >> 8) & 255, create);
695 return V2_block_getblk(inode, bh, block & 255, create);
699 * the global minix fs getblk function.
701 struct buffer_head * minix_getblk(struct inode * inode, int block, int create)
703 if (INODE_VERSION(inode) == MINIX_V1)
704 return V1_minix_getblk(inode,block,create);
705 else
706 return V2_minix_getblk(inode,block,create);
709 struct buffer_head * minix_bread(struct inode * inode, int block, int create)
711 struct buffer_head * bh;
713 bh = minix_getblk(inode,block,create);
714 if (!bh || buffer_uptodate(bh))
715 return bh;
716 ll_rw_block(READ, 1, &bh);
717 wait_on_buffer(bh);
718 if (buffer_uptodate(bh))
719 return bh;
720 brelse(bh);
721 return NULL;
725 * The minix V1 function to read an inode.
727 static void V1_minix_read_inode(struct inode * inode)
729 struct buffer_head * bh;
730 struct minix_inode * raw_inode;
731 int block, ino;
733 ino = inode->i_ino;
734 inode->i_op = NULL;
735 inode->i_mode = 0;
736 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
737 printk("Bad inode number on dev %s"
738 ": %d is out of range\n",
739 kdevname(inode->i_dev), ino);
740 return;
742 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
743 inode->i_sb->u.minix_sb.s_zmap_blocks +
744 (ino-1)/MINIX_INODES_PER_BLOCK;
745 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
746 printk("Major problem: unable to read inode from dev "
747 "%s\n", kdevname(inode->i_dev));
748 return;
750 raw_inode = ((struct minix_inode *) bh->b_data) +
751 (ino-1)%MINIX_INODES_PER_BLOCK;
752 inode->i_mode = raw_inode->i_mode;
753 inode->i_uid = raw_inode->i_uid;
754 inode->i_gid = raw_inode->i_gid;
755 inode->i_nlink = raw_inode->i_nlinks;
756 inode->i_size = raw_inode->i_size;
757 inode->i_mtime = inode->i_atime = inode->i_ctime = raw_inode->i_time;
758 inode->i_blocks = inode->i_blksize = 0;
759 for (block = 0; block < 9; block++)
760 inode->u.minix_i.u.i1_data[block] = raw_inode->i_zone[block];
761 if (S_ISREG(inode->i_mode))
762 inode->i_op = &minix_file_inode_operations;
763 else if (S_ISDIR(inode->i_mode))
764 inode->i_op = &minix_dir_inode_operations;
765 else if (S_ISLNK(inode->i_mode))
766 inode->i_op = &minix_symlink_inode_operations;
767 else
768 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
769 brelse(bh);
773 * The minix V2 function to read an inode.
775 static void V2_minix_read_inode(struct inode * inode)
777 struct buffer_head * bh;
778 struct minix2_inode * raw_inode;
779 int block, ino;
781 ino = inode->i_ino;
782 inode->i_op = NULL;
783 inode->i_mode = 0;
784 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
785 printk("Bad inode number on dev %s"
786 ": %d is out of range\n",
787 kdevname(inode->i_dev), ino);
788 return;
790 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
791 inode->i_sb->u.minix_sb.s_zmap_blocks +
792 (ino-1)/MINIX2_INODES_PER_BLOCK;
793 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
794 printk("Major problem: unable to read inode from dev "
795 "%s\n", kdevname(inode->i_dev));
796 return;
798 raw_inode = ((struct minix2_inode *) bh->b_data) +
799 (ino-1)%MINIX2_INODES_PER_BLOCK;
800 inode->i_mode = raw_inode->i_mode;
801 inode->i_uid = raw_inode->i_uid;
802 inode->i_gid = raw_inode->i_gid;
803 inode->i_nlink = raw_inode->i_nlinks;
804 inode->i_size = raw_inode->i_size;
805 inode->i_mtime = raw_inode->i_mtime;
806 inode->i_atime = raw_inode->i_atime;
807 inode->i_ctime = raw_inode->i_ctime;
808 inode->i_blocks = inode->i_blksize = 0;
809 for (block = 0; block < 10; block++)
810 inode->u.minix_i.u.i2_data[block] = raw_inode->i_zone[block];
811 if (S_ISREG(inode->i_mode))
812 inode->i_op = &minix_file_inode_operations;
813 else if (S_ISDIR(inode->i_mode))
814 inode->i_op = &minix_dir_inode_operations;
815 else if (S_ISLNK(inode->i_mode))
816 inode->i_op = &minix_symlink_inode_operations;
817 else
818 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
819 brelse(bh);
823 * The global function to read an inode.
825 static void minix_read_inode(struct inode * inode)
827 if (INODE_VERSION(inode) == MINIX_V1)
828 V1_minix_read_inode(inode);
829 else
830 V2_minix_read_inode(inode);
834 * The minix V1 function to synchronize an inode.
836 static struct buffer_head * V1_minix_update_inode(struct inode * inode)
838 struct buffer_head * bh;
839 struct minix_inode * raw_inode;
840 int ino, block;
842 ino = inode->i_ino;
843 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
844 printk("Bad inode number on dev %s"
845 ": %d is out of range\n",
846 kdevname(inode->i_dev), ino);
847 return 0;
849 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
850 (ino-1)/MINIX_INODES_PER_BLOCK;
851 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
852 printk("unable to read i-node block\n");
853 return 0;
855 raw_inode = ((struct minix_inode *)bh->b_data) +
856 (ino-1)%MINIX_INODES_PER_BLOCK;
857 raw_inode->i_mode = inode->i_mode;
858 raw_inode->i_uid = inode->i_uid;
859 raw_inode->i_gid = inode->i_gid;
860 raw_inode->i_nlinks = inode->i_nlink;
861 raw_inode->i_size = inode->i_size;
862 raw_inode->i_time = inode->i_mtime;
863 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
864 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
865 else for (block = 0; block < 9; block++)
866 raw_inode->i_zone[block] = inode->u.minix_i.u.i1_data[block];
867 mark_buffer_dirty(bh, 1);
868 return bh;
872 * The minix V2 function to synchronize an inode.
874 static struct buffer_head * V2_minix_update_inode(struct inode * inode)
876 struct buffer_head * bh;
877 struct minix2_inode * raw_inode;
878 int ino, block;
880 ino = inode->i_ino;
881 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
882 printk("Bad inode number on dev %s"
883 ": %d is out of range\n",
884 kdevname(inode->i_dev), ino);
885 return 0;
887 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
888 (ino-1)/MINIX2_INODES_PER_BLOCK;
889 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
890 printk("unable to read i-node block\n");
891 return 0;
893 raw_inode = ((struct minix2_inode *)bh->b_data) +
894 (ino-1)%MINIX2_INODES_PER_BLOCK;
895 raw_inode->i_mode = inode->i_mode;
896 raw_inode->i_uid = inode->i_uid;
897 raw_inode->i_gid = inode->i_gid;
898 raw_inode->i_nlinks = inode->i_nlink;
899 raw_inode->i_size = inode->i_size;
900 raw_inode->i_mtime = inode->i_mtime;
901 raw_inode->i_atime = inode->i_atime;
902 raw_inode->i_ctime = inode->i_ctime;
903 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
904 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
905 else for (block = 0; block < 10; block++)
906 raw_inode->i_zone[block] = inode->u.minix_i.u.i2_data[block];
907 mark_buffer_dirty(bh, 1);
908 return bh;
911 static struct buffer_head *minix_update_inode(struct inode *inode)
913 if (INODE_VERSION(inode) == MINIX_V1)
914 return V1_minix_update_inode(inode);
915 else
916 return V2_minix_update_inode(inode);
919 static void minix_write_inode(struct inode * inode)
921 struct buffer_head *bh;
923 bh = minix_update_inode(inode);
924 brelse(bh);
927 int minix_sync_inode(struct inode * inode)
929 int err = 0;
930 struct buffer_head *bh;
932 bh = minix_update_inode(inode);
933 if (bh && buffer_dirty(bh))
935 ll_rw_block(WRITE, 1, &bh);
936 wait_on_buffer(bh);
937 if (buffer_req(bh) && !buffer_uptodate(bh))
939 printk ("IO error syncing minix inode ["
940 "%s:%08lx]\n",
941 kdevname(inode->i_dev), inode->i_ino);
942 err = -1;
945 else if (!bh)
946 err = -1;
947 brelse (bh);
948 return err;
951 static struct file_system_type minix_fs_type = {
952 "minix",
953 FS_REQUIRES_DEV,
954 minix_read_super,
955 NULL
958 __initfunc(int init_minix_fs(void))
960 return register_filesystem(&minix_fs_type);
963 #ifdef MODULE
964 EXPORT_NO_SYMBOLS;
966 int init_module(void)
968 return init_minix_fs();
971 void cleanup_module(void)
973 unregister_filesystem(&minix_fs_type);
976 #endif