- pre5:
[davej-history.git] / fs / minix / inode.c
blob36d5be2131561ddf78f31ceace4367d2b0d3f08d
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>
22 #include <linux/smp_lock.h>
23 #include <linux/highuid.h>
25 #include <asm/system.h>
26 #include <asm/bitops.h>
28 #include <linux/minix_fs.h>
30 static void minix_read_inode(struct inode * inode);
31 static void minix_write_inode(struct inode * inode, int wait);
32 static int minix_statfs(struct super_block *sb, struct statfs *buf);
33 static int minix_remount (struct super_block * sb, int * flags, char * data);
35 static void minix_delete_inode(struct inode *inode)
37 lock_kernel();
39 inode->i_size = 0;
40 minix_truncate(inode);
41 minix_free_inode(inode);
43 unlock_kernel();
46 static void minix_commit_super(struct super_block * sb)
48 mark_buffer_dirty(sb->u.minix_sb.s_sbh);
49 sb->s_dirt = 0;
52 static void minix_write_super(struct super_block * sb)
54 struct minix_super_block * ms;
56 if (!(sb->s_flags & MS_RDONLY)) {
57 ms = sb->u.minix_sb.s_ms;
59 if (ms->s_state & MINIX_VALID_FS)
60 ms->s_state &= ~MINIX_VALID_FS;
61 minix_commit_super(sb);
63 sb->s_dirt = 0;
67 static void minix_put_super(struct super_block *sb)
69 int i;
71 if (!(sb->s_flags & MS_RDONLY)) {
72 sb->u.minix_sb.s_ms->s_state = sb->u.minix_sb.s_mount_state;
73 mark_buffer_dirty(sb->u.minix_sb.s_sbh);
75 for (i = 0; i < sb->u.minix_sb.s_imap_blocks; i++)
76 brelse(sb->u.minix_sb.s_imap[i]);
77 for (i = 0; i < sb->u.minix_sb.s_zmap_blocks; i++)
78 brelse(sb->u.minix_sb.s_zmap[i]);
79 brelse (sb->u.minix_sb.s_sbh);
80 kfree(sb->u.minix_sb.s_imap);
82 return;
85 static struct super_operations minix_sops = {
86 read_inode: minix_read_inode,
87 write_inode: minix_write_inode,
88 delete_inode: minix_delete_inode,
89 put_super: minix_put_super,
90 write_super: minix_write_super,
91 statfs: minix_statfs,
92 remount_fs: minix_remount,
95 static int minix_remount (struct super_block * sb, int * flags, char * data)
97 struct minix_super_block * ms;
99 ms = sb->u.minix_sb.s_ms;
100 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
101 return 0;
102 if (*flags & MS_RDONLY) {
103 if (ms->s_state & MINIX_VALID_FS ||
104 !(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
105 return 0;
106 /* Mounting a rw partition read-only. */
107 ms->s_state = sb->u.minix_sb.s_mount_state;
108 mark_buffer_dirty(sb->u.minix_sb.s_sbh);
109 sb->s_dirt = 1;
110 minix_commit_super(sb);
112 else {
113 /* Mount a partition which is read-only, read-write. */
114 sb->u.minix_sb.s_mount_state = ms->s_state;
115 ms->s_state &= ~MINIX_VALID_FS;
116 mark_buffer_dirty(sb->u.minix_sb.s_sbh);
117 sb->s_dirt = 1;
119 if (!(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
120 printk ("MINIX-fs warning: remounting unchecked fs, "
121 "running fsck is recommended.\n");
122 else if ((sb->u.minix_sb.s_mount_state & MINIX_ERROR_FS))
123 printk ("MINIX-fs warning: remounting fs with errors, "
124 "running fsck is recommended.\n");
126 return 0;
130 * Check the root directory of the filesystem to make sure
131 * it really _is_ a Minix filesystem, and to check the size
132 * of the directory entry.
134 static const char * minix_checkroot(struct super_block *s, struct inode *dir)
136 struct buffer_head *bh;
137 struct minix_dir_entry *de;
138 const char * errmsg;
139 int dirsize;
141 if (!S_ISDIR(dir->i_mode))
142 return "root directory is not a directory";
144 bh = minix_bread(dir, 0, 0);
145 if (!bh)
146 return "unable to read root directory";
148 de = (struct minix_dir_entry *) bh->b_data;
149 errmsg = "bad root directory '.' entry";
150 dirsize = BLOCK_SIZE;
151 if (de->inode == MINIX_ROOT_INO && strcmp(de->name, ".") == 0) {
152 errmsg = "bad root directory '..' entry";
153 dirsize = 8;
156 while ((dirsize <<= 1) < BLOCK_SIZE) {
157 de = (struct minix_dir_entry *) (bh->b_data + dirsize);
158 if (de->inode != MINIX_ROOT_INO)
159 continue;
160 if (strcmp(de->name, ".."))
161 continue;
162 s->u.minix_sb.s_dirsize = dirsize;
163 s->u.minix_sb.s_namelen = dirsize - 2;
164 errmsg = NULL;
165 break;
167 brelse(bh);
168 return errmsg;
171 static struct super_block *minix_read_super(struct super_block *s, void *data,
172 int silent)
174 struct buffer_head *bh;
175 struct buffer_head **map;
176 struct minix_super_block *ms;
177 int i, block;
178 kdev_t dev = s->s_dev;
179 const char * errmsg;
180 struct inode *root_inode;
181 unsigned int hblock;
183 /* N.B. These should be compile-time tests.
184 Unfortunately that is impossible. */
185 if (32 != sizeof (struct minix_inode))
186 panic("bad V1 i-node size");
187 if (64 != sizeof(struct minix2_inode))
188 panic("bad V2 i-node size");
190 hblock = get_hardblocksize(dev);
191 if (hblock && hblock > BLOCK_SIZE)
192 goto out_bad_hblock;
194 set_blocksize(dev, BLOCK_SIZE);
195 if (!(bh = bread(dev,1,BLOCK_SIZE)))
196 goto out_bad_sb;
198 ms = (struct minix_super_block *) bh->b_data;
199 s->u.minix_sb.s_ms = ms;
200 s->u.minix_sb.s_sbh = bh;
201 s->u.minix_sb.s_mount_state = ms->s_state;
202 s->s_blocksize = BLOCK_SIZE;
203 s->s_blocksize_bits = BLOCK_SIZE_BITS;
204 s->u.minix_sb.s_ninodes = ms->s_ninodes;
205 s->u.minix_sb.s_nzones = ms->s_nzones;
206 s->u.minix_sb.s_imap_blocks = ms->s_imap_blocks;
207 s->u.minix_sb.s_zmap_blocks = ms->s_zmap_blocks;
208 s->u.minix_sb.s_firstdatazone = ms->s_firstdatazone;
209 s->u.minix_sb.s_log_zone_size = ms->s_log_zone_size;
210 s->u.minix_sb.s_max_size = ms->s_max_size;
211 s->s_magic = ms->s_magic;
212 if (s->s_magic == MINIX_SUPER_MAGIC) {
213 s->u.minix_sb.s_version = MINIX_V1;
214 s->u.minix_sb.s_dirsize = 16;
215 s->u.minix_sb.s_namelen = 14;
216 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
217 } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
218 s->u.minix_sb.s_version = MINIX_V1;
219 s->u.minix_sb.s_dirsize = 32;
220 s->u.minix_sb.s_namelen = 30;
221 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
222 } else if (s->s_magic == MINIX2_SUPER_MAGIC) {
223 s->u.minix_sb.s_version = MINIX_V2;
224 s->u.minix_sb.s_nzones = ms->s_zones;
225 s->u.minix_sb.s_dirsize = 16;
226 s->u.minix_sb.s_namelen = 14;
227 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
228 } else if (s->s_magic == MINIX2_SUPER_MAGIC2) {
229 s->u.minix_sb.s_version = MINIX_V2;
230 s->u.minix_sb.s_nzones = ms->s_zones;
231 s->u.minix_sb.s_dirsize = 32;
232 s->u.minix_sb.s_namelen = 30;
233 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
234 } else
235 goto out_no_fs;
238 * Allocate the buffer map to keep the superblock small.
240 i = (s->u.minix_sb.s_imap_blocks + s->u.minix_sb.s_zmap_blocks) * sizeof(bh);
241 map = kmalloc(i, GFP_KERNEL);
242 if (!map)
243 goto out_no_map;
244 memset(map, 0, i);
245 s->u.minix_sb.s_imap = &map[0];
246 s->u.minix_sb.s_zmap = &map[s->u.minix_sb.s_imap_blocks];
248 block=2;
249 for (i=0 ; i < s->u.minix_sb.s_imap_blocks ; i++) {
250 if (!(s->u.minix_sb.s_imap[i]=bread(dev,block,BLOCK_SIZE)))
251 goto out_no_bitmap;
252 block++;
254 for (i=0 ; i < s->u.minix_sb.s_zmap_blocks ; i++) {
255 if (!(s->u.minix_sb.s_zmap[i]=bread(dev,block,BLOCK_SIZE)))
256 goto out_no_bitmap;
257 block++;
260 minix_set_bit(0,s->u.minix_sb.s_imap[0]->b_data);
261 minix_set_bit(0,s->u.minix_sb.s_zmap[0]->b_data);
262 /* set up enough so that it can read an inode */
263 s->s_op = &minix_sops;
264 root_inode = iget(s, MINIX_ROOT_INO);
265 if (!root_inode)
266 goto out_no_root;
268 * Check the fs before we get the root dentry ...
270 errmsg = minix_checkroot(s, root_inode);
271 if (errmsg)
272 goto out_bad_root;
274 s->s_root = d_alloc_root(root_inode);
275 if (!s->s_root)
276 goto out_iput;
278 s->s_root->d_op = &minix_dentry_operations;
280 if (!(s->s_flags & MS_RDONLY)) {
281 ms->s_state &= ~MINIX_VALID_FS;
282 mark_buffer_dirty(bh);
283 s->s_dirt = 1;
285 if (!(s->u.minix_sb.s_mount_state & MINIX_VALID_FS))
286 printk ("MINIX-fs: mounting unchecked file system, "
287 "running fsck is recommended.\n");
288 else if (s->u.minix_sb.s_mount_state & MINIX_ERROR_FS)
289 printk ("MINIX-fs: mounting file system with errors, "
290 "running fsck is recommended.\n");
291 return s;
293 out_bad_root:
294 if (!silent)
295 printk("MINIX-fs: %s\n", errmsg);
296 out_iput:
297 iput(root_inode);
298 goto out_freemap;
300 out_no_root:
301 if (!silent)
302 printk("MINIX-fs: get root inode failed\n");
303 goto out_freemap;
305 out_no_bitmap:
306 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
307 out_freemap:
308 for (i = 0; i < s->u.minix_sb.s_imap_blocks; i++)
309 brelse(s->u.minix_sb.s_imap[i]);
310 for (i = 0; i < s->u.minix_sb.s_zmap_blocks; i++)
311 brelse(s->u.minix_sb.s_zmap[i]);
312 kfree(s->u.minix_sb.s_imap);
313 goto out_release;
315 out_no_map:
316 if (!silent)
317 printk ("MINIX-fs: can't allocate map\n");
318 goto out_release;
320 out_no_fs:
321 if (!silent)
322 printk("VFS: Can't find a Minix or Minix V2 filesystem on device "
323 "%s.\n", kdevname(dev));
324 out_release:
325 brelse(bh);
326 goto out;
328 out_bad_hblock:
329 printk("MINIX-fs: blocksize too small for device.\n");
330 goto out;
332 out_bad_sb:
333 printk("MINIX-fs: unable to read superblock\n");
334 out:
335 return NULL;
338 static int minix_statfs(struct super_block *sb, struct statfs *buf)
340 buf->f_type = sb->s_magic;
341 buf->f_bsize = sb->s_blocksize;
342 buf->f_blocks = (sb->u.minix_sb.s_nzones - sb->u.minix_sb.s_firstdatazone) << sb->u.minix_sb.s_log_zone_size;
343 buf->f_bfree = minix_count_free_blocks(sb);
344 buf->f_bavail = buf->f_bfree;
345 buf->f_files = sb->u.minix_sb.s_ninodes;
346 buf->f_ffree = minix_count_free_inodes(sb);
347 buf->f_namelen = sb->u.minix_sb.s_namelen;
348 return 0;
352 * The minix V1 fs bmap functions.
354 #define V1_inode_bmap(inode,nr) (((unsigned short *)(inode)->u.minix_i.u.i1_data)[(nr)])
356 static int V1_block_bmap(struct buffer_head * bh, int nr)
358 int tmp;
360 if (!bh)
361 return 0;
362 tmp = ((unsigned short *) bh->b_data)[nr];
363 brelse(bh);
364 return tmp;
367 static int V1_minix_block_map(struct inode * inode, long block)
369 int i, ret;
371 ret = 0;
372 lock_kernel();
373 if (block < 0) {
374 printk("minix_bmap: block<0");
375 goto out;
377 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
378 printk("minix_bmap: block>big");
379 goto out;
381 if (block < 7) {
382 ret = V1_inode_bmap(inode,block);
383 goto out;
385 block -= 7;
386 if (block < 512) {
387 i = V1_inode_bmap(inode,7);
388 if (!i)
389 goto out;
390 ret = V1_block_bmap(bread(inode->i_dev, i,
391 BLOCK_SIZE), block);
392 goto out;
394 block -= 512;
395 i = V1_inode_bmap(inode,8);
396 if (!i)
397 goto out;
398 i = V1_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block>>9);
399 if (!i)
400 goto out;
401 ret = V1_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
402 block & 511);
403 out:
404 unlock_kernel();
405 return ret;
409 * The minix V2 fs bmap functions.
411 #define V2_inode_bmap(inode,nr) (((unsigned int *)(inode)->u.minix_i.u.i2_data)[(nr)])
412 static int V2_block_bmap(struct buffer_head * bh, int nr)
414 int tmp;
416 if (!bh)
417 return 0;
418 tmp = ((unsigned int *) bh->b_data)[nr];
419 brelse(bh);
420 return tmp;
423 static int V2_minix_block_map(struct inode * inode, int block)
425 int i, ret;
427 ret = 0;
428 lock_kernel();
429 if (block < 0) {
430 printk("minix_bmap: block<0");
431 goto out;
433 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
434 printk("minix_bmap: block>big");
435 goto out;
437 if (block < 7) {
438 ret = V2_inode_bmap(inode,block);
439 goto out;
441 block -= 7;
442 if (block < 256) {
443 i = V2_inode_bmap(inode, 7);
444 if (!i)
445 goto out;
446 ret = V2_block_bmap(bread(inode->i_dev, i,
447 BLOCK_SIZE), block);
448 goto out;
450 block -= 256;
451 if (block < (256 * 256)) {
452 i = V2_inode_bmap(inode, 8);
453 if (!i)
454 goto out;
455 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
456 block >> 8);
457 if (!i)
458 goto out;
459 ret = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
460 block & 255);
461 goto out;
463 block -= (256 * 256);
464 i = V2_inode_bmap(inode, 9);
465 if (!i)
466 goto out;
467 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
468 block >> 16);
469 if (!i)
470 goto out;
471 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
472 (block >> 8) & 255);
473 if (!i)
474 goto out;
475 ret = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
476 block & 255);
477 out:
478 unlock_kernel();
479 return ret;
483 * The minix V1 fs getblk functions.
485 static struct buffer_head * V1_inode_getblk(struct inode * inode, int nr,
486 int new_block, int *err,
487 int metadata, int *phys, int *new)
489 int tmp;
490 unsigned short *p;
491 struct buffer_head * result;
493 p = inode->u.minix_i.u.i1_data + nr;
494 repeat:
495 tmp = *p;
496 if (tmp) {
497 if (metadata) {
498 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
499 if (tmp == *p)
500 return result;
501 brelse(result);
502 goto repeat;
503 } else {
504 *phys = tmp;
505 return NULL;
509 tmp = minix_new_block(inode);
510 if (!tmp) {
511 *err = -ENOSPC;
512 return NULL;
514 if (metadata) {
515 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
516 if (*p) {
517 minix_free_block(inode, tmp);
518 brelse(result);
519 goto repeat;
521 memset(result->b_data, 0, BLOCK_SIZE);
522 mark_buffer_uptodate(result, 1);
523 mark_buffer_dirty(result);
524 } else {
525 if (*p) {
527 * Nobody is allowed to change block allocation
528 * state from under us:
530 BUG();
531 minix_free_block(inode, tmp);
532 goto repeat;
534 *phys = tmp;
535 result = NULL;
536 *err = 0;
537 *new = 1;
539 *p = tmp;
541 inode->i_ctime = CURRENT_TIME;
542 mark_inode_dirty(inode);
543 return result;
546 static struct buffer_head * V1_block_getblk(struct inode * inode,
547 struct buffer_head * bh, int nr, int new_block, int *err,
548 int metadata, int *phys, int *new)
550 int tmp;
551 unsigned short *p;
552 struct buffer_head * result;
554 result = NULL;
555 if (!bh)
556 goto out;
557 if (!buffer_uptodate(bh)) {
558 ll_rw_block(READ, 1, &bh);
559 wait_on_buffer(bh);
560 if (!buffer_uptodate(bh))
561 goto out;
563 p = nr + (unsigned short *) bh->b_data;
564 repeat:
565 tmp = *p;
566 if (tmp) {
567 if (metadata) {
568 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
569 if (tmp == *p)
570 goto out;
571 brelse(result);
572 goto repeat;
573 } else {
574 *phys = tmp;
575 goto out;
579 tmp = minix_new_block(inode);
580 if (!tmp)
581 goto out;
582 if (metadata) {
583 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
584 if (*p) {
585 minix_free_block(inode, tmp);
586 brelse(result);
587 goto repeat;
589 memset(result->b_data, 0, BLOCK_SIZE);
590 mark_buffer_uptodate(result, 1);
591 mark_buffer_dirty(result);
592 } else {
593 *phys = tmp;
594 *new = 1;
596 if (*p) {
597 minix_free_block(inode, tmp);
598 brelse(result);
599 goto repeat;
602 *p = tmp;
603 mark_buffer_dirty(bh);
604 *err = 0;
605 out:
606 brelse(bh);
607 return result;
610 static int V1_get_block(struct inode * inode, long block,
611 struct buffer_head *bh_result, int create)
613 int ret, err, new, phys, ptr;
614 struct buffer_head *bh;
616 if (!create) {
617 phys = V1_minix_block_map(inode, block);
618 if (phys) {
619 bh_result->b_dev = inode->i_dev;
620 bh_result->b_blocknr = phys;
621 bh_result->b_state |= (1UL << BH_Mapped);
623 return 0;
626 err = -EIO;
627 new = 0;
628 ret = 0;
629 bh = NULL;
631 lock_kernel();
632 if (block < 0)
633 goto abort_negative;
634 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)
635 goto abort_too_big;
637 err = 0;
638 ptr = block;
640 * ok, these macros clean the logic up a bit and make
641 * it much more readable:
643 #define GET_INODE_DATABLOCK(x) \
644 V1_inode_getblk(inode, x, block, &err, 0, &phys, &new)
645 #define GET_INODE_PTR(x) \
646 V1_inode_getblk(inode, x, block, &err, 1, NULL, NULL)
647 #define GET_INDIRECT_DATABLOCK(x) \
648 V1_block_getblk(inode, bh, x, block, &err, 0, &phys, &new)
649 #define GET_INDIRECT_PTR(x) \
650 V1_block_getblk(inode, bh, x, block, &err, 1, NULL, NULL)
652 if (ptr < 7) {
653 bh = GET_INODE_DATABLOCK(ptr);
654 goto out;
656 ptr -= 7;
657 if (ptr < 512) {
658 bh = GET_INODE_PTR(7);
659 goto get_indirect;
661 ptr -= 512;
662 bh = GET_INODE_PTR(8);
663 bh = GET_INDIRECT_PTR((ptr >> 9) & 511);
664 get_indirect:
665 bh = GET_INDIRECT_DATABLOCK(ptr & 511);
667 #undef GET_INODE_DATABLOCK
668 #undef GET_INODE_PTR
669 #undef GET_INDIRECT_DATABLOCK
670 #undef GET_INDIRECT_PTR
672 out:
673 if (err)
674 goto abort;
675 bh_result->b_dev = inode->i_dev;
676 bh_result->b_blocknr = phys;
677 bh_result->b_state |= (1UL << BH_Mapped);
678 if (new)
679 bh_result->b_state |= (1UL << BH_New);
680 abort:
681 unlock_kernel();
682 return err;
684 abort_negative:
685 printk("minix_getblk: block<0");
686 goto abort;
688 abort_too_big:
689 printk("minix_getblk: block>big");
690 goto abort;
694 * The minix V2 fs getblk functions.
696 static struct buffer_head * V2_inode_getblk(struct inode * inode, int nr,
697 int new_block, int *err,
698 int metadata, int *phys, int *new)
700 int tmp;
701 unsigned int *p;
702 struct buffer_head * result;
704 p = (unsigned int *) inode->u.minix_i.u.i2_data + nr;
705 repeat:
706 tmp = *p;
707 if (tmp) {
708 if (metadata) {
709 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
710 if (tmp == *p)
711 return result;
712 brelse(result);
713 goto repeat;
714 } else {
715 *phys = tmp;
716 return NULL;
720 tmp = minix_new_block(inode);
721 if (!tmp) {
722 *err = -ENOSPC;
723 return NULL;
725 if (metadata) {
726 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
727 if (*p) {
728 minix_free_block(inode, tmp);
729 brelse(result);
730 goto repeat;
732 memset(result->b_data, 0, BLOCK_SIZE);
733 mark_buffer_uptodate(result, 1);
734 mark_buffer_dirty(result);
735 } else {
736 if (*p) {
738 * Nobody is allowed to change block allocation
739 * state from under us:
741 BUG();
742 minix_free_block(inode, tmp);
743 goto repeat;
745 *phys = tmp;
746 result = NULL;
747 *err = 0;
748 *new = 1;
750 *p = tmp;
752 inode->i_ctime = CURRENT_TIME;
753 mark_inode_dirty(inode);
754 return result;
757 static struct buffer_head * V2_block_getblk(struct inode * inode,
758 struct buffer_head * bh, int nr, int new_block, int *err,
759 int metadata, int *phys, int *new)
761 int tmp;
762 unsigned int *p;
763 struct buffer_head * result;
765 result = NULL;
766 if (!bh)
767 goto out;
768 if (!buffer_uptodate(bh)) {
769 ll_rw_block(READ, 1, &bh);
770 wait_on_buffer(bh);
771 if (!buffer_uptodate(bh))
772 goto out;
774 p = nr + (unsigned int *) bh->b_data;
775 repeat:
776 tmp = *p;
777 if (tmp) {
778 if (metadata) {
779 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
780 if (tmp == *p)
781 goto out;
782 brelse(result);
783 goto repeat;
784 } else {
785 *phys = tmp;
786 goto out;
790 tmp = minix_new_block(inode);
791 if (!tmp)
792 goto out;
793 if (metadata) {
794 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
795 if (*p) {
796 minix_free_block(inode, tmp);
797 brelse(result);
798 goto repeat;
800 memset(result->b_data, 0, BLOCK_SIZE);
801 mark_buffer_uptodate(result, 1);
802 mark_buffer_dirty(result);
803 } else {
804 *phys = tmp;
805 *new = 1;
807 if (*p) {
808 minix_free_block(inode, tmp);
809 brelse(result);
810 goto repeat;
813 *p = tmp;
814 mark_buffer_dirty(bh);
815 *err = 0;
816 out:
817 brelse(bh);
818 return result;
821 static int V2_get_block(struct inode * inode, long block,
822 struct buffer_head *bh_result, int create)
824 int ret, err, new, phys, ptr;
825 struct buffer_head * bh;
827 if (!create) {
828 phys = V2_minix_block_map(inode, block);
829 if (phys) {
830 bh_result->b_dev = inode->i_dev;
831 bh_result->b_blocknr = phys;
832 bh_result->b_state |= (1UL << BH_Mapped);
834 return 0;
837 err = -EIO;
838 new = 0;
839 ret = 0;
840 bh = NULL;
842 lock_kernel();
843 if (block < 0)
844 goto abort_negative;
845 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)
846 goto abort_too_big;
848 err = 0;
849 ptr = block;
851 * ok, these macros clean the logic up a bit and make
852 * it much more readable:
854 #define GET_INODE_DATABLOCK(x) \
855 V2_inode_getblk(inode, x, block, &err, 0, &phys, &new)
856 #define GET_INODE_PTR(x) \
857 V2_inode_getblk(inode, x, block, &err, 1, NULL, NULL)
858 #define GET_INDIRECT_DATABLOCK(x) \
859 V2_block_getblk(inode, bh, x, block, &err, 0, &phys, &new)
860 #define GET_INDIRECT_PTR(x) \
861 V2_block_getblk(inode, bh, x, block, &err, 1, NULL, NULL)
863 if (ptr < 7) {
864 bh = GET_INODE_DATABLOCK(ptr);
865 goto out;
867 ptr -= 7;
868 if (ptr < 256) {
869 bh = GET_INODE_PTR(7);
870 goto get_indirect;
872 ptr -= 256;
873 if (ptr < 256*256) {
874 bh = GET_INODE_PTR(8);
875 goto get_double;
877 ptr -= 256*256;
878 bh = GET_INODE_PTR(9);
879 bh = GET_INDIRECT_PTR((ptr >> 16) & 255);
880 get_double:
881 bh = GET_INDIRECT_PTR((ptr >> 8) & 255);
882 get_indirect:
883 bh = GET_INDIRECT_DATABLOCK(ptr & 255);
885 #undef GET_INODE_DATABLOCK
886 #undef GET_INODE_PTR
887 #undef GET_INDIRECT_DATABLOCK
888 #undef GET_INDIRECT_PTR
890 out:
891 if (err)
892 goto abort;
893 bh_result->b_dev = inode->i_dev;
894 bh_result->b_blocknr = phys;
895 bh_result->b_state |= (1UL << BH_Mapped);
896 if (new)
897 bh_result->b_state |= (1UL << BH_New);
898 abort:
899 unlock_kernel();
900 return err;
902 abort_negative:
903 printk("minix_getblk: block<0");
904 goto abort;
906 abort_too_big:
907 printk("minix_getblk: block>big");
908 goto abort;
911 static int minix_get_block(struct inode *inode, long block,
912 struct buffer_head *bh_result, int create)
914 if (INODE_VERSION(inode) == MINIX_V1)
915 return V1_get_block(inode, block, bh_result, create);
916 else
917 return V2_get_block(inode, block, bh_result, create);
921 * the global minix fs getblk function.
923 struct buffer_head *minix_getblk(struct inode *inode, int block, int create)
925 struct buffer_head dummy;
926 int error;
928 dummy.b_state = 0;
929 dummy.b_blocknr = -1000;
930 error = minix_get_block(inode, block, &dummy, create);
931 if (!error && buffer_mapped(&dummy)) {
932 struct buffer_head *bh;
933 bh = getblk(dummy.b_dev, dummy.b_blocknr, BLOCK_SIZE);
934 if (buffer_new(&dummy)) {
935 memset(bh->b_data, 0, BLOCK_SIZE);
936 mark_buffer_uptodate(bh, 1);
937 mark_buffer_dirty(bh);
939 return bh;
941 return NULL;
944 struct buffer_head * minix_bread(struct inode * inode, int block, int create)
946 struct buffer_head * bh;
948 bh = minix_getblk(inode, block, create);
949 if (!bh || buffer_uptodate(bh))
950 return bh;
951 ll_rw_block(READ, 1, &bh);
952 wait_on_buffer(bh);
953 if (buffer_uptodate(bh))
954 return bh;
955 brelse(bh);
956 return NULL;
959 static int minix_writepage(struct file *file, struct page *page)
961 return block_write_full_page(page,minix_get_block);
963 static int minix_readpage(struct file *file, struct page *page)
965 return block_read_full_page(page,minix_get_block);
967 static int minix_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
969 return block_prepare_write(page,from,to,minix_get_block);
971 static int minix_bmap(struct address_space *mapping, long block)
973 return generic_block_bmap(mapping,block,minix_get_block);
975 struct address_space_operations minix_aops = {
976 readpage: minix_readpage,
977 writepage: minix_writepage,
978 sync_page: block_sync_page,
979 prepare_write: minix_prepare_write,
980 commit_write: generic_commit_write,
981 bmap: minix_bmap
985 * The minix V1 function to read an inode.
987 static void V1_minix_read_inode(struct inode * inode)
989 struct buffer_head * bh;
990 struct minix_inode * raw_inode;
991 int block, ino;
993 ino = inode->i_ino;
994 inode->i_mode = 0;
995 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
996 printk("Bad inode number on dev %s"
997 ": %d is out of range\n",
998 kdevname(inode->i_dev), ino);
999 return;
1001 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
1002 inode->i_sb->u.minix_sb.s_zmap_blocks +
1003 (ino-1)/MINIX_INODES_PER_BLOCK;
1004 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
1005 printk("Major problem: unable to read inode from dev "
1006 "%s\n", kdevname(inode->i_dev));
1007 return;
1009 raw_inode = ((struct minix_inode *) bh->b_data) +
1010 (ino-1)%MINIX_INODES_PER_BLOCK;
1011 inode->i_mode = raw_inode->i_mode;
1012 inode->i_uid = (uid_t)raw_inode->i_uid;
1013 inode->i_gid = (gid_t)raw_inode->i_gid;
1014 inode->i_nlink = raw_inode->i_nlinks;
1015 inode->i_size = raw_inode->i_size;
1016 inode->i_mtime = inode->i_atime = inode->i_ctime = raw_inode->i_time;
1017 inode->i_blocks = inode->i_blksize = 0;
1018 for (block = 0; block < 9; block++)
1019 inode->u.minix_i.u.i1_data[block] = raw_inode->i_zone[block];
1020 if (S_ISREG(inode->i_mode)) {
1021 inode->i_op = &minix_file_inode_operations;
1022 inode->i_fop = &minix_file_operations;
1023 inode->i_mapping->a_ops = &minix_aops;
1024 } else if (S_ISDIR(inode->i_mode)) {
1025 inode->i_op = &minix_dir_inode_operations;
1026 inode->i_fop = &minix_dir_operations;
1027 } else if (S_ISLNK(inode->i_mode)) {
1028 inode->i_op = &page_symlink_inode_operations;
1029 inode->i_mapping->a_ops = &minix_aops;
1030 } else
1031 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
1032 brelse(bh);
1036 * The minix V2 function to read an inode.
1038 static void V2_minix_read_inode(struct inode * inode)
1040 struct buffer_head * bh;
1041 struct minix2_inode * raw_inode;
1042 int block, ino;
1044 ino = inode->i_ino;
1045 inode->i_mode = 0;
1046 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1047 printk("Bad inode number on dev %s"
1048 ": %d is out of range\n",
1049 kdevname(inode->i_dev), ino);
1050 return;
1052 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
1053 inode->i_sb->u.minix_sb.s_zmap_blocks +
1054 (ino-1)/MINIX2_INODES_PER_BLOCK;
1055 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
1056 printk("Major problem: unable to read inode from dev "
1057 "%s\n", kdevname(inode->i_dev));
1058 return;
1060 raw_inode = ((struct minix2_inode *) bh->b_data) +
1061 (ino-1)%MINIX2_INODES_PER_BLOCK;
1062 inode->i_mode = raw_inode->i_mode;
1063 inode->i_uid = (uid_t)raw_inode->i_uid;
1064 inode->i_gid = (gid_t)raw_inode->i_gid;
1065 inode->i_nlink = raw_inode->i_nlinks;
1066 inode->i_size = raw_inode->i_size;
1067 inode->i_mtime = raw_inode->i_mtime;
1068 inode->i_atime = raw_inode->i_atime;
1069 inode->i_ctime = raw_inode->i_ctime;
1070 inode->i_blocks = inode->i_blksize = 0;
1071 for (block = 0; block < 10; block++)
1072 inode->u.minix_i.u.i2_data[block] = raw_inode->i_zone[block];
1073 if (S_ISREG(inode->i_mode)) {
1074 inode->i_op = &minix_file_inode_operations;
1075 inode->i_fop = &minix_file_operations;
1076 inode->i_mapping->a_ops = &minix_aops;
1077 } else if (S_ISDIR(inode->i_mode)) {
1078 inode->i_op = &minix_dir_inode_operations;
1079 inode->i_fop = &minix_dir_operations;
1080 } else if (S_ISLNK(inode->i_mode)) {
1081 inode->i_op = &page_symlink_inode_operations;
1082 inode->i_mapping->a_ops = &minix_aops;
1083 } else
1084 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
1085 brelse(bh);
1089 * The global function to read an inode.
1091 static void minix_read_inode(struct inode * inode)
1093 if (INODE_VERSION(inode) == MINIX_V1)
1094 V1_minix_read_inode(inode);
1095 else
1096 V2_minix_read_inode(inode);
1100 * The minix V1 function to synchronize an inode.
1102 static struct buffer_head * V1_minix_update_inode(struct inode * inode)
1104 struct buffer_head * bh;
1105 struct minix_inode * raw_inode;
1106 int ino, block;
1108 ino = inode->i_ino;
1109 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1110 printk("Bad inode number on dev %s"
1111 ": %d is out of range\n",
1112 kdevname(inode->i_dev), ino);
1113 return 0;
1115 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
1116 (ino-1)/MINIX_INODES_PER_BLOCK;
1117 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
1118 printk("unable to read i-node block\n");
1119 return 0;
1121 raw_inode = ((struct minix_inode *)bh->b_data) +
1122 (ino-1)%MINIX_INODES_PER_BLOCK;
1123 raw_inode->i_mode = inode->i_mode;
1124 raw_inode->i_uid = fs_high2lowuid(inode->i_uid);
1125 raw_inode->i_gid = fs_high2lowgid(inode->i_gid);
1126 raw_inode->i_nlinks = inode->i_nlink;
1127 raw_inode->i_size = inode->i_size;
1128 raw_inode->i_time = inode->i_mtime;
1129 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1130 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
1131 else for (block = 0; block < 9; block++)
1132 raw_inode->i_zone[block] = inode->u.minix_i.u.i1_data[block];
1133 mark_buffer_dirty(bh);
1134 return bh;
1138 * The minix V2 function to synchronize an inode.
1140 static struct buffer_head * V2_minix_update_inode(struct inode * inode)
1142 struct buffer_head * bh;
1143 struct minix2_inode * raw_inode;
1144 int ino, block;
1146 ino = inode->i_ino;
1147 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1148 printk("Bad inode number on dev %s"
1149 ": %d is out of range\n",
1150 kdevname(inode->i_dev), ino);
1151 return 0;
1153 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
1154 (ino-1)/MINIX2_INODES_PER_BLOCK;
1155 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
1156 printk("unable to read i-node block\n");
1157 return 0;
1159 raw_inode = ((struct minix2_inode *)bh->b_data) +
1160 (ino-1)%MINIX2_INODES_PER_BLOCK;
1161 raw_inode->i_mode = inode->i_mode;
1162 raw_inode->i_uid = fs_high2lowuid(inode->i_uid);
1163 raw_inode->i_gid = fs_high2lowgid(inode->i_gid);
1164 raw_inode->i_nlinks = inode->i_nlink;
1165 raw_inode->i_size = inode->i_size;
1166 raw_inode->i_mtime = inode->i_mtime;
1167 raw_inode->i_atime = inode->i_atime;
1168 raw_inode->i_ctime = inode->i_ctime;
1169 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1170 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
1171 else for (block = 0; block < 10; block++)
1172 raw_inode->i_zone[block] = inode->u.minix_i.u.i2_data[block];
1173 mark_buffer_dirty(bh);
1174 return bh;
1177 static struct buffer_head *minix_update_inode(struct inode *inode)
1179 if (INODE_VERSION(inode) == MINIX_V1)
1180 return V1_minix_update_inode(inode);
1181 else
1182 return V2_minix_update_inode(inode);
1185 static void minix_write_inode(struct inode * inode, int wait)
1187 struct buffer_head *bh;
1189 lock_kernel();
1190 bh = minix_update_inode(inode);
1191 unlock_kernel();
1192 brelse(bh);
1195 int minix_sync_inode(struct inode * inode)
1197 int err = 0;
1198 struct buffer_head *bh;
1200 bh = minix_update_inode(inode);
1201 if (bh && buffer_dirty(bh))
1203 ll_rw_block(WRITE, 1, &bh);
1204 wait_on_buffer(bh);
1205 if (buffer_req(bh) && !buffer_uptodate(bh))
1207 printk ("IO error syncing minix inode ["
1208 "%s:%08lx]\n",
1209 kdevname(inode->i_dev), inode->i_ino);
1210 err = -1;
1213 else if (!bh)
1214 err = -1;
1215 brelse (bh);
1216 return err;
1219 static DECLARE_FSTYPE_DEV(minix_fs_type,"minix",minix_read_super);
1221 static int __init init_minix_fs(void)
1223 return register_filesystem(&minix_fs_type);
1226 static void __exit exit_minix_fs(void)
1228 unregister_filesystem(&minix_fs_type);
1231 EXPORT_NO_SYMBOLS;
1233 module_init(init_minix_fs)
1234 module_exit(exit_minix_fs)