* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / minix / inode.c
blob6cc9522fafac18368b010ca25cbf2b2eb10444bc
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>
24 #include <asm/system.h>
25 #include <asm/uaccess.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);
32 static int minix_statfs(struct super_block *sb, struct statfs *buf, int bufsiz);
33 static int minix_remount (struct super_block * sb, int * flags, char * data);
35 static void minix_delete_inode(struct inode *inode)
37 inode->i_size = 0;
38 minix_truncate(inode);
39 minix_free_inode(inode);
42 static void minix_commit_super(struct super_block * sb)
44 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
45 sb->s_dirt = 0;
48 static void minix_write_super(struct super_block * sb)
50 struct minix_super_block * ms;
52 if (!(sb->s_flags & MS_RDONLY)) {
53 ms = sb->u.minix_sb.s_ms;
55 if (ms->s_state & MINIX_VALID_FS)
56 ms->s_state &= ~MINIX_VALID_FS;
57 minix_commit_super(sb);
59 sb->s_dirt = 0;
63 static void minix_put_super(struct super_block *sb)
65 int i;
67 if (!(sb->s_flags & MS_RDONLY)) {
68 sb->u.minix_sb.s_ms->s_state = sb->u.minix_sb.s_mount_state;
69 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
71 for (i = 0; i < sb->u.minix_sb.s_imap_blocks; i++)
72 brelse(sb->u.minix_sb.s_imap[i]);
73 for (i = 0; i < sb->u.minix_sb.s_zmap_blocks; i++)
74 brelse(sb->u.minix_sb.s_zmap[i]);
75 brelse (sb->u.minix_sb.s_sbh);
76 kfree(sb->u.minix_sb.s_imap);
78 MOD_DEC_USE_COUNT;
79 return;
82 static struct super_operations minix_sops = {
83 minix_read_inode,
84 minix_write_inode,
85 NULL, /* put_inode */
86 minix_delete_inode,
87 NULL, /* notify_change */
88 minix_put_super,
89 minix_write_super,
90 minix_statfs,
91 minix_remount
94 static int minix_remount (struct super_block * sb, int * flags, char * data)
96 struct minix_super_block * ms;
98 ms = sb->u.minix_sb.s_ms;
99 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
100 return 0;
101 if (*flags & MS_RDONLY) {
102 if (ms->s_state & MINIX_VALID_FS ||
103 !(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
104 return 0;
105 /* Mounting a rw partition read-only. */
106 ms->s_state = sb->u.minix_sb.s_mount_state;
107 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
108 sb->s_dirt = 1;
109 minix_commit_super(sb);
111 else {
112 /* Mount a partition which is read-only, read-write. */
113 sb->u.minix_sb.s_mount_state = ms->s_state;
114 ms->s_state &= ~MINIX_VALID_FS;
115 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
116 sb->s_dirt = 1;
118 if (!(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
119 printk ("MINIX-fs warning: remounting unchecked fs, "
120 "running fsck is recommended.\n");
121 else if ((sb->u.minix_sb.s_mount_state & MINIX_ERROR_FS))
122 printk ("MINIX-fs warning: remounting fs with errors, "
123 "running fsck is recommended.\n");
125 return 0;
129 * Check the root directory of the filesystem to make sure
130 * it really _is_ a Minix filesystem, and to check the size
131 * of the directory entry.
133 static const char * minix_checkroot(struct super_block *s, struct inode *dir)
135 struct buffer_head *bh;
136 struct minix_dir_entry *de;
137 const char * errmsg;
138 int dirsize;
140 if (!S_ISDIR(dir->i_mode))
141 return "root directory is not a directory";
143 bh = minix_bread(dir, 0, 0);
144 if (!bh)
145 return "unable to read root directory";
147 de = (struct minix_dir_entry *) bh->b_data;
148 errmsg = "bad root directory '.' entry";
149 dirsize = BLOCK_SIZE;
150 if (de->inode == MINIX_ROOT_INO && strcmp(de->name, ".") == 0) {
151 errmsg = "bad root directory '..' entry";
152 dirsize = 8;
155 while ((dirsize <<= 1) < BLOCK_SIZE) {
156 de = (struct minix_dir_entry *) (bh->b_data + dirsize);
157 if (de->inode != MINIX_ROOT_INO)
158 continue;
159 if (strcmp(de->name, ".."))
160 continue;
161 s->u.minix_sb.s_dirsize = dirsize;
162 s->u.minix_sb.s_namelen = dirsize - 2;
163 errmsg = NULL;
164 break;
166 brelse(bh);
167 return errmsg;
170 static struct super_block *minix_read_super(struct super_block *s, void *data,
171 int silent)
173 struct buffer_head *bh;
174 struct buffer_head **map;
175 struct minix_super_block *ms;
176 int i, block;
177 kdev_t dev = s->s_dev;
178 const char * errmsg;
179 struct inode *root_inode;
181 /* N.B. These should be compile-time tests.
182 Unfortunately that is impossible. */
183 if (32 != sizeof (struct minix_inode))
184 panic("bad V1 i-node size");
185 if (64 != sizeof(struct minix2_inode))
186 panic("bad V2 i-node size");
188 MOD_INC_USE_COUNT;
189 lock_super(s);
190 set_blocksize(dev, BLOCK_SIZE);
191 if (!(bh = bread(dev,1,BLOCK_SIZE)))
192 goto out_bad_sb;
194 ms = (struct minix_super_block *) bh->b_data;
195 s->u.minix_sb.s_ms = ms;
196 s->u.minix_sb.s_sbh = bh;
197 s->u.minix_sb.s_mount_state = ms->s_state;
198 s->s_blocksize = BLOCK_SIZE;
199 s->s_blocksize_bits = BLOCK_SIZE_BITS;
200 s->u.minix_sb.s_ninodes = ms->s_ninodes;
201 s->u.minix_sb.s_nzones = ms->s_nzones;
202 s->u.minix_sb.s_imap_blocks = ms->s_imap_blocks;
203 s->u.minix_sb.s_zmap_blocks = ms->s_zmap_blocks;
204 s->u.minix_sb.s_firstdatazone = ms->s_firstdatazone;
205 s->u.minix_sb.s_log_zone_size = ms->s_log_zone_size;
206 s->u.minix_sb.s_max_size = ms->s_max_size;
207 s->s_magic = ms->s_magic;
208 if (s->s_magic == MINIX_SUPER_MAGIC) {
209 s->u.minix_sb.s_version = MINIX_V1;
210 s->u.minix_sb.s_dirsize = 16;
211 s->u.minix_sb.s_namelen = 14;
212 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
213 } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
214 s->u.minix_sb.s_version = MINIX_V1;
215 s->u.minix_sb.s_dirsize = 32;
216 s->u.minix_sb.s_namelen = 30;
217 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
218 } else if (s->s_magic == MINIX2_SUPER_MAGIC) {
219 s->u.minix_sb.s_version = MINIX_V2;
220 s->u.minix_sb.s_nzones = ms->s_zones;
221 s->u.minix_sb.s_dirsize = 16;
222 s->u.minix_sb.s_namelen = 14;
223 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
224 } else if (s->s_magic == MINIX2_SUPER_MAGIC2) {
225 s->u.minix_sb.s_version = MINIX_V2;
226 s->u.minix_sb.s_nzones = ms->s_zones;
227 s->u.minix_sb.s_dirsize = 32;
228 s->u.minix_sb.s_namelen = 30;
229 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
230 } else
231 goto out_no_fs;
234 * Allocate the buffer map to keep the superblock small.
236 i = (s->u.minix_sb.s_imap_blocks + s->u.minix_sb.s_zmap_blocks) * sizeof(bh);
237 map = kmalloc(i, GFP_KERNEL);
238 if (!map)
239 goto out_no_map;
240 memset(map, 0, i);
241 s->u.minix_sb.s_imap = &map[0];
242 s->u.minix_sb.s_zmap = &map[s->u.minix_sb.s_imap_blocks];
244 block=2;
245 for (i=0 ; i < s->u.minix_sb.s_imap_blocks ; i++) {
246 if (!(s->u.minix_sb.s_imap[i]=bread(dev,block,BLOCK_SIZE)))
247 goto out_no_bitmap;
248 block++;
250 for (i=0 ; i < s->u.minix_sb.s_zmap_blocks ; i++) {
251 if (!(s->u.minix_sb.s_zmap[i]=bread(dev,block,BLOCK_SIZE)))
252 goto out_no_bitmap;
253 block++;
256 minix_set_bit(0,s->u.minix_sb.s_imap[0]->b_data);
257 minix_set_bit(0,s->u.minix_sb.s_zmap[0]->b_data);
258 /* set up enough so that it can read an inode */
259 s->s_op = &minix_sops;
260 root_inode = iget(s, MINIX_ROOT_INO);
261 if (!root_inode)
262 goto out_no_root;
264 * Check the fs before we get the root dentry ...
266 errmsg = minix_checkroot(s, root_inode);
267 if (errmsg)
268 goto out_bad_root;
270 s->s_root = d_alloc_root(root_inode);
271 if (!s->s_root)
272 goto out_iput;
274 s->s_root->d_op = &minix_dentry_operations;
276 if (!(s->s_flags & MS_RDONLY)) {
277 ms->s_state &= ~MINIX_VALID_FS;
278 mark_buffer_dirty(bh, 1);
279 s->s_dirt = 1;
281 unlock_super(s);
282 if (!(s->u.minix_sb.s_mount_state & MINIX_VALID_FS))
283 printk ("MINIX-fs: mounting unchecked file system, "
284 "running fsck is recommended.\n");
285 else if (s->u.minix_sb.s_mount_state & MINIX_ERROR_FS)
286 printk ("MINIX-fs: mounting file system with errors, "
287 "running fsck is recommended.\n");
288 return s;
290 out_bad_root:
291 if (!silent)
292 printk("MINIX-fs: %s\n", errmsg);
293 out_iput:
294 iput(root_inode);
295 goto out_freemap;
297 out_no_root:
298 if (!silent)
299 printk("MINIX-fs: get root inode failed\n");
300 goto out_freemap;
302 out_no_bitmap:
303 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
304 out_freemap:
305 for (i = 0; i < s->u.minix_sb.s_imap_blocks; i++)
306 brelse(s->u.minix_sb.s_imap[i]);
307 for (i = 0; i < s->u.minix_sb.s_zmap_blocks; i++)
308 brelse(s->u.minix_sb.s_zmap[i]);
309 kfree(s->u.minix_sb.s_imap);
310 goto out_release;
312 out_no_map:
313 if (!silent)
314 printk ("MINIX-fs: can't allocate map\n");
315 goto out_release;
317 out_no_fs:
318 if (!silent)
319 printk("VFS: Can't find a Minix or Minix V2 filesystem on device "
320 "%s.\n", kdevname(dev));
321 out_release:
322 brelse(bh);
323 goto out_unlock;
325 out_bad_sb:
326 printk("MINIX-fs: unable to read superblock\n");
327 out_unlock:
328 s->s_dev = 0;
329 unlock_super(s);
330 MOD_DEC_USE_COUNT;
331 return NULL;
334 static int minix_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
336 struct statfs tmp;
338 tmp.f_type = sb->s_magic;
339 tmp.f_bsize = sb->s_blocksize;
340 tmp.f_blocks = (sb->u.minix_sb.s_nzones - sb->u.minix_sb.s_firstdatazone) << sb->u.minix_sb.s_log_zone_size;
341 tmp.f_bfree = minix_count_free_blocks(sb);
342 tmp.f_bavail = tmp.f_bfree;
343 tmp.f_files = sb->u.minix_sb.s_ninodes;
344 tmp.f_ffree = minix_count_free_inodes(sb);
345 tmp.f_namelen = sb->u.minix_sb.s_namelen;
346 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
350 * The minix V1 fs bmap functions.
352 #define V1_inode_bmap(inode,nr) (((unsigned short *)(inode)->u.minix_i.u.i1_data)[(nr)])
354 static int V1_block_bmap(struct buffer_head * bh, int nr)
356 int tmp;
358 if (!bh)
359 return 0;
360 tmp = ((unsigned short *) bh->b_data)[nr];
361 brelse(bh);
362 return tmp;
365 static int V1_minix_block_map(struct inode * inode, long block)
367 int i, ret;
369 ret = 0;
370 lock_kernel();
371 if (block < 0) {
372 printk("minix_bmap: block<0");
373 goto out;
375 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
376 printk("minix_bmap: block>big");
377 goto out;
379 if (block < 7) {
380 ret = V1_inode_bmap(inode,block);
381 goto out;
383 block -= 7;
384 if (block < 512) {
385 i = V1_inode_bmap(inode,7);
386 if (!i)
387 goto out;
388 ret = V1_block_bmap(bread(inode->i_dev, i,
389 BLOCK_SIZE), block);
390 goto out;
392 block -= 512;
393 i = V1_inode_bmap(inode,8);
394 if (!i)
395 goto out;
396 i = V1_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block>>9);
397 if (!i)
398 goto out;
399 ret = V1_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
400 block & 511);
401 out:
402 unlock_kernel();
403 return ret;
407 * The minix V2 fs bmap functions.
409 #define V2_inode_bmap(inode,nr) (((unsigned int *)(inode)->u.minix_i.u.i2_data)[(nr)])
410 static int V2_block_bmap(struct buffer_head * bh, int nr)
412 int tmp;
414 if (!bh)
415 return 0;
416 tmp = ((unsigned int *) bh->b_data)[nr];
417 brelse(bh);
418 return tmp;
421 static int V2_minix_block_map(struct inode * inode, int block)
423 int i, ret;
425 ret = 0;
426 lock_kernel();
427 if (block < 0) {
428 printk("minix_bmap: block<0");
429 goto out;
431 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
432 printk("minix_bmap: block>big");
433 goto out;
435 if (block < 7) {
436 ret = V2_inode_bmap(inode,block);
437 goto out;
439 block -= 7;
440 if (block < 256) {
441 i = V2_inode_bmap(inode, 7);
442 if (!i)
443 goto out;
444 ret = V2_block_bmap(bread(inode->i_dev, i,
445 BLOCK_SIZE), block);
446 goto out;
448 block -= 256;
449 if (block < (256 * 256)) {
450 i = V2_inode_bmap(inode, 8);
451 if (!i)
452 goto out;
453 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
454 block >> 8);
455 if (!i)
456 goto out;
457 ret = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
458 block & 255);
459 goto out;
461 block -= (256 * 256);
462 i = V2_inode_bmap(inode, 9);
463 if (!i)
464 goto out;
465 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
466 block >> 16);
467 if (!i)
468 goto out;
469 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
470 (block >> 8) & 255);
471 if (!i)
472 goto out;
473 ret = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
474 block & 255);
475 out:
476 unlock_kernel();
477 return ret;
481 * The minix V1 fs getblk functions.
483 static struct buffer_head * V1_inode_getblk(struct inode * inode, int nr,
484 int new_block, int *err,
485 int metadata, int *phys, int *new)
487 int tmp;
488 unsigned short *p;
489 struct buffer_head * result;
491 p = inode->u.minix_i.u.i1_data + nr;
492 repeat:
493 tmp = *p;
494 if (tmp) {
495 if (metadata) {
496 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
497 if (tmp == *p)
498 return result;
499 brelse(result);
500 goto repeat;
501 } else {
502 *phys = tmp;
503 return NULL;
506 *err = -EFBIG;
508 /* Check file limits.. */
510 unsigned long limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
511 if (limit < RLIM_INFINITY) {
512 limit >>= BLOCK_SIZE_BITS;
513 if (new_block >= limit) {
514 send_sig(SIGXFSZ, current, 0);
515 *err = -EFBIG;
516 return NULL;
521 tmp = minix_new_block(inode);
522 if (!tmp) {
523 *err = -ENOSPC;
524 return NULL;
526 if (metadata) {
527 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
528 if (*p) {
529 minix_free_block(inode, tmp);
530 brelse(result);
531 goto repeat;
533 memset(result->b_data, 0, BLOCK_SIZE);
534 mark_buffer_uptodate(result, 1);
535 mark_buffer_dirty(result, 1);
536 } else {
537 if (*p) {
539 * Nobody is allowed to change block allocation
540 * state from under us:
542 BUG();
543 minix_free_block(inode, tmp);
544 goto repeat;
546 *phys = tmp;
547 result = NULL;
548 *err = 0;
549 *new = 1;
551 *p = tmp;
553 inode->i_ctime = CURRENT_TIME;
554 mark_inode_dirty(inode);
555 return result;
558 static struct buffer_head * V1_block_getblk(struct inode * inode,
559 struct buffer_head * bh, int nr, int new_block, int *err,
560 int metadata, int *phys, int *new)
562 int tmp;
563 unsigned short *p;
564 struct buffer_head * result;
565 unsigned long limit;
567 result = NULL;
568 if (!bh)
569 goto out;
570 if (!buffer_uptodate(bh)) {
571 ll_rw_block(READ, 1, &bh);
572 wait_on_buffer(bh);
573 if (!buffer_uptodate(bh))
574 goto out;
576 p = nr + (unsigned short *) bh->b_data;
577 repeat:
578 tmp = *p;
579 if (tmp) {
580 if (metadata) {
581 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
582 if (tmp == *p)
583 goto out;
584 brelse(result);
585 goto repeat;
586 } else {
587 *phys = tmp;
588 goto out;
591 *err = -EFBIG;
593 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
594 if (limit < RLIM_INFINITY) {
595 limit >>= BLOCK_SIZE_BITS;
596 if (new_block >= limit) {
597 send_sig(SIGXFSZ, current, 0);
598 goto out;
602 tmp = minix_new_block(inode);
603 if (!tmp)
604 goto out;
605 if (metadata) {
606 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
607 if (*p) {
608 minix_free_block(inode, tmp);
609 brelse(result);
610 goto repeat;
612 memset(result->b_data, 0, BLOCK_SIZE);
613 mark_buffer_uptodate(result, 1);
614 mark_buffer_dirty(result, 1);
615 } else {
616 *phys = tmp;
617 *new = 1;
619 if (*p) {
620 minix_free_block(inode, tmp);
621 brelse(result);
622 goto repeat;
625 *p = tmp;
626 mark_buffer_dirty(bh, 1);
627 *err = 0;
628 out:
629 brelse(bh);
630 return result;
633 static int V1_get_block(struct inode * inode, long block,
634 struct buffer_head *bh_result, int create)
636 int ret, err, new, phys, ptr;
637 struct buffer_head *bh;
639 if (!create) {
640 phys = V1_minix_block_map(inode, block);
641 if (phys) {
642 bh_result->b_dev = inode->i_dev;
643 bh_result->b_blocknr = phys;
644 bh_result->b_state |= (1UL << BH_Mapped);
646 return 0;
649 err = -EIO;
650 new = 0;
651 ret = 0;
652 bh = NULL;
654 lock_kernel();
655 if (block < 0)
656 goto abort_negative;
657 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)
658 goto abort_too_big;
660 err = 0;
661 ptr = block;
663 * ok, these macros clean the logic up a bit and make
664 * it much more readable:
666 #define GET_INODE_DATABLOCK(x) \
667 V1_inode_getblk(inode, x, block, &err, 0, &phys, &new)
668 #define GET_INODE_PTR(x) \
669 V1_inode_getblk(inode, x, block, &err, 1, NULL, NULL)
670 #define GET_INDIRECT_DATABLOCK(x) \
671 V1_block_getblk(inode, bh, x, block, &err, 0, &phys, &new)
672 #define GET_INDIRECT_PTR(x) \
673 V1_block_getblk(inode, bh, x, block, &err, 1, NULL, NULL)
675 if (ptr < 7) {
676 bh = GET_INODE_DATABLOCK(ptr);
677 goto out;
679 ptr -= 7;
680 if (ptr < 512) {
681 bh = GET_INODE_PTR(7);
682 goto get_indirect;
684 ptr -= 512;
685 bh = GET_INODE_PTR(8);
686 bh = GET_INDIRECT_PTR((ptr >> 9) & 511);
687 get_indirect:
688 bh = GET_INDIRECT_DATABLOCK(ptr & 511);
690 #undef GET_INODE_DATABLOCK
691 #undef GET_INODE_PTR
692 #undef GET_INDIRECT_DATABLOCK
693 #undef GET_INDIRECT_PTR
695 out:
696 if (err)
697 goto abort;
698 bh_result->b_dev = inode->i_dev;
699 bh_result->b_blocknr = phys;
700 bh_result->b_state |= (1UL << BH_Mapped);
701 if (new)
702 bh_result->b_state |= (1UL << BH_New);
703 abort:
704 unlock_kernel();
705 return err;
707 abort_negative:
708 printk("minix_getblk: block<0");
709 goto abort;
711 abort_too_big:
712 printk("minix_getblk: block>big");
713 goto abort;
717 * The minix V2 fs getblk functions.
719 static struct buffer_head * V2_inode_getblk(struct inode * inode, int nr,
720 int new_block, int *err,
721 int metadata, int *phys, int *new)
723 int tmp;
724 unsigned int *p;
725 struct buffer_head * result;
727 p = (unsigned int *) inode->u.minix_i.u.i2_data + nr;
728 repeat:
729 tmp = *p;
730 if (tmp) {
731 if (metadata) {
732 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
733 if (tmp == *p)
734 return result;
735 brelse(result);
736 goto repeat;
737 } else {
738 *phys = tmp;
739 return NULL;
742 *err = -EFBIG;
744 /* Check file limits.. */
746 unsigned long limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
747 if (limit < RLIM_INFINITY) {
748 limit >>= BLOCK_SIZE_BITS;
749 if (new_block >= limit) {
750 send_sig(SIGXFSZ, current, 0);
751 *err = -EFBIG;
752 return NULL;
757 tmp = minix_new_block(inode);
758 if (!tmp) {
759 *err = -ENOSPC;
760 return NULL;
762 if (metadata) {
763 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
764 if (*p) {
765 minix_free_block(inode, tmp);
766 brelse(result);
767 goto repeat;
769 memset(result->b_data, 0, BLOCK_SIZE);
770 mark_buffer_uptodate(result, 1);
771 mark_buffer_dirty(result, 1);
772 } else {
773 if (*p) {
775 * Nobody is allowed to change block allocation
776 * state from under us:
778 BUG();
779 minix_free_block(inode, tmp);
780 goto repeat;
782 *phys = tmp;
783 result = NULL;
784 *err = 0;
785 *new = 1;
787 *p = tmp;
789 inode->i_ctime = CURRENT_TIME;
790 mark_inode_dirty(inode);
791 return result;
794 static struct buffer_head * V2_block_getblk(struct inode * inode,
795 struct buffer_head * bh, int nr, int new_block, int *err,
796 int metadata, int *phys, int *new)
798 int tmp;
799 unsigned int *p;
800 struct buffer_head * result;
801 unsigned long limit;
803 result = NULL;
804 if (!bh)
805 goto out;
806 if (!buffer_uptodate(bh)) {
807 ll_rw_block(READ, 1, &bh);
808 wait_on_buffer(bh);
809 if (!buffer_uptodate(bh))
810 goto out;
812 p = nr + (unsigned int *) bh->b_data;
813 repeat:
814 tmp = *p;
815 if (tmp) {
816 if (metadata) {
817 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
818 if (tmp == *p)
819 goto out;
820 brelse(result);
821 goto repeat;
822 } else {
823 *phys = tmp;
824 goto out;
827 *err = -EFBIG;
829 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
830 if (limit < RLIM_INFINITY) {
831 limit >>= BLOCK_SIZE_BITS;
832 if (new_block >= limit) {
833 send_sig(SIGXFSZ, current, 0);
834 goto out;
838 tmp = minix_new_block(inode);
839 if (!tmp)
840 goto out;
841 if (metadata) {
842 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
843 if (*p) {
844 minix_free_block(inode, tmp);
845 brelse(result);
846 goto repeat;
848 memset(result->b_data, 0, BLOCK_SIZE);
849 mark_buffer_uptodate(result, 1);
850 mark_buffer_dirty(result, 1);
851 } else {
852 *phys = tmp;
853 *new = 1;
855 if (*p) {
856 minix_free_block(inode, tmp);
857 brelse(result);
858 goto repeat;
861 *p = tmp;
862 mark_buffer_dirty(bh, 1);
863 *err = 0;
864 out:
865 brelse(bh);
866 return result;
869 static int V2_get_block(struct inode * inode, long block,
870 struct buffer_head *bh_result, int create)
872 int ret, err, new, phys, ptr;
873 struct buffer_head * bh;
875 if (!create) {
876 phys = V2_minix_block_map(inode, block);
877 if (phys) {
878 bh_result->b_dev = inode->i_dev;
879 bh_result->b_blocknr = phys;
880 bh_result->b_state |= (1UL << BH_Mapped);
882 return 0;
885 err = -EIO;
886 new = 0;
887 ret = 0;
888 bh = NULL;
890 lock_kernel();
891 if (block < 0)
892 goto abort_negative;
893 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)
894 goto abort_too_big;
896 err = 0;
897 ptr = block;
899 * ok, these macros clean the logic up a bit and make
900 * it much more readable:
902 #define GET_INODE_DATABLOCK(x) \
903 V2_inode_getblk(inode, x, block, &err, 0, &phys, &new)
904 #define GET_INODE_PTR(x) \
905 V2_inode_getblk(inode, x, block, &err, 1, NULL, NULL)
906 #define GET_INDIRECT_DATABLOCK(x) \
907 V2_block_getblk(inode, bh, x, block, &err, 0, &phys, &new)
908 #define GET_INDIRECT_PTR(x) \
909 V2_block_getblk(inode, bh, x, block, &err, 1, NULL, NULL)
911 if (ptr < 7) {
912 bh = GET_INODE_DATABLOCK(ptr);
913 goto out;
915 ptr -= 7;
916 if (ptr < 256) {
917 bh = GET_INODE_PTR(7);
918 goto get_indirect;
920 ptr -= 256;
921 if (ptr < 256*256) {
922 bh = GET_INODE_PTR(8);
923 goto get_double;
925 ptr -= 256*256;
926 bh = GET_INODE_PTR(9);
927 bh = GET_INDIRECT_PTR((ptr >> 16) & 255);
928 get_double:
929 bh = GET_INDIRECT_PTR((ptr >> 8) & 255);
930 get_indirect:
931 bh = GET_INDIRECT_DATABLOCK(ptr & 255);
933 #undef GET_INODE_DATABLOCK
934 #undef GET_INODE_PTR
935 #undef GET_INDIRECT_DATABLOCK
936 #undef GET_INDIRECT_PTR
938 out:
939 if (err)
940 goto abort;
941 bh_result->b_dev = inode->i_dev;
942 bh_result->b_blocknr = phys;
943 bh_result->b_state |= (1UL << BH_Mapped);
944 if (new)
945 bh_result->b_state |= (1UL << BH_New);
946 abort:
947 unlock_kernel();
948 return err;
950 abort_negative:
951 printk("minix_getblk: block<0");
952 goto abort;
954 abort_too_big:
955 printk("minix_getblk: block>big");
956 goto abort;
959 int minix_get_block(struct inode *inode, long block,
960 struct buffer_head *bh_result, int create)
962 if (INODE_VERSION(inode) == MINIX_V1)
963 return V1_get_block(inode, block, bh_result, create);
964 else
965 return V2_get_block(inode, block, bh_result, create);
969 * the global minix fs getblk function.
971 struct buffer_head *minix_getblk(struct inode *inode, int block, int create)
973 struct buffer_head dummy;
974 int error;
976 dummy.b_state = 0;
977 dummy.b_blocknr = -1000;
978 error = minix_get_block(inode, block, &dummy, create);
979 if (!error && buffer_mapped(&dummy)) {
980 struct buffer_head *bh;
981 bh = getblk(dummy.b_dev, dummy.b_blocknr, BLOCK_SIZE);
982 if (buffer_new(&dummy)) {
983 memset(bh->b_data, 0, BLOCK_SIZE);
984 mark_buffer_uptodate(bh, 1);
985 mark_buffer_dirty(bh, 1);
987 return bh;
989 return NULL;
992 struct buffer_head * minix_bread(struct inode * inode, int block, int create)
994 struct buffer_head * bh;
996 bh = minix_getblk(inode, block, create);
997 if (!bh || buffer_uptodate(bh))
998 return bh;
999 ll_rw_block(READ, 1, &bh);
1000 wait_on_buffer(bh);
1001 if (buffer_uptodate(bh))
1002 return bh;
1003 brelse(bh);
1004 return NULL;
1008 * The minix V1 function to read an inode.
1010 static void V1_minix_read_inode(struct inode * inode)
1012 struct buffer_head * bh;
1013 struct minix_inode * raw_inode;
1014 int block, ino;
1016 ino = inode->i_ino;
1017 inode->i_op = NULL;
1018 inode->i_mode = 0;
1019 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1020 printk("Bad inode number on dev %s"
1021 ": %d is out of range\n",
1022 kdevname(inode->i_dev), ino);
1023 return;
1025 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
1026 inode->i_sb->u.minix_sb.s_zmap_blocks +
1027 (ino-1)/MINIX_INODES_PER_BLOCK;
1028 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
1029 printk("Major problem: unable to read inode from dev "
1030 "%s\n", kdevname(inode->i_dev));
1031 return;
1033 raw_inode = ((struct minix_inode *) bh->b_data) +
1034 (ino-1)%MINIX_INODES_PER_BLOCK;
1035 inode->i_mode = raw_inode->i_mode;
1036 inode->i_uid = raw_inode->i_uid;
1037 inode->i_gid = raw_inode->i_gid;
1038 inode->i_nlink = raw_inode->i_nlinks;
1039 inode->i_size = raw_inode->i_size;
1040 inode->i_mtime = inode->i_atime = inode->i_ctime = raw_inode->i_time;
1041 inode->i_blocks = inode->i_blksize = 0;
1042 for (block = 0; block < 9; block++)
1043 inode->u.minix_i.u.i1_data[block] = raw_inode->i_zone[block];
1044 if (S_ISREG(inode->i_mode))
1045 inode->i_op = &minix_file_inode_operations;
1046 else if (S_ISDIR(inode->i_mode))
1047 inode->i_op = &minix_dir_inode_operations;
1048 else if (S_ISLNK(inode->i_mode))
1049 inode->i_op = &minix_symlink_inode_operations;
1050 else
1051 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
1052 brelse(bh);
1056 * The minix V2 function to read an inode.
1058 static void V2_minix_read_inode(struct inode * inode)
1060 struct buffer_head * bh;
1061 struct minix2_inode * raw_inode;
1062 int block, ino;
1064 ino = inode->i_ino;
1065 inode->i_op = NULL;
1066 inode->i_mode = 0;
1067 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1068 printk("Bad inode number on dev %s"
1069 ": %d is out of range\n",
1070 kdevname(inode->i_dev), ino);
1071 return;
1073 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
1074 inode->i_sb->u.minix_sb.s_zmap_blocks +
1075 (ino-1)/MINIX2_INODES_PER_BLOCK;
1076 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
1077 printk("Major problem: unable to read inode from dev "
1078 "%s\n", kdevname(inode->i_dev));
1079 return;
1081 raw_inode = ((struct minix2_inode *) bh->b_data) +
1082 (ino-1)%MINIX2_INODES_PER_BLOCK;
1083 inode->i_mode = raw_inode->i_mode;
1084 inode->i_uid = raw_inode->i_uid;
1085 inode->i_gid = raw_inode->i_gid;
1086 inode->i_nlink = raw_inode->i_nlinks;
1087 inode->i_size = raw_inode->i_size;
1088 inode->i_mtime = raw_inode->i_mtime;
1089 inode->i_atime = raw_inode->i_atime;
1090 inode->i_ctime = raw_inode->i_ctime;
1091 inode->i_blocks = inode->i_blksize = 0;
1092 for (block = 0; block < 10; block++)
1093 inode->u.minix_i.u.i2_data[block] = raw_inode->i_zone[block];
1094 if (S_ISREG(inode->i_mode))
1095 inode->i_op = &minix_file_inode_operations;
1096 else if (S_ISDIR(inode->i_mode))
1097 inode->i_op = &minix_dir_inode_operations;
1098 else if (S_ISLNK(inode->i_mode))
1099 inode->i_op = &minix_symlink_inode_operations;
1100 else
1101 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
1102 brelse(bh);
1106 * The global function to read an inode.
1108 static void minix_read_inode(struct inode * inode)
1110 if (INODE_VERSION(inode) == MINIX_V1)
1111 V1_minix_read_inode(inode);
1112 else
1113 V2_minix_read_inode(inode);
1117 * The minix V1 function to synchronize an inode.
1119 static struct buffer_head * V1_minix_update_inode(struct inode * inode)
1121 struct buffer_head * bh;
1122 struct minix_inode * raw_inode;
1123 int ino, block;
1125 ino = inode->i_ino;
1126 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1127 printk("Bad inode number on dev %s"
1128 ": %d is out of range\n",
1129 kdevname(inode->i_dev), ino);
1130 return 0;
1132 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
1133 (ino-1)/MINIX_INODES_PER_BLOCK;
1134 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
1135 printk("unable to read i-node block\n");
1136 return 0;
1138 raw_inode = ((struct minix_inode *)bh->b_data) +
1139 (ino-1)%MINIX_INODES_PER_BLOCK;
1140 raw_inode->i_mode = inode->i_mode;
1141 raw_inode->i_uid = inode->i_uid;
1142 raw_inode->i_gid = inode->i_gid;
1143 raw_inode->i_nlinks = inode->i_nlink;
1144 raw_inode->i_size = inode->i_size;
1145 raw_inode->i_time = inode->i_mtime;
1146 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1147 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
1148 else for (block = 0; block < 9; block++)
1149 raw_inode->i_zone[block] = inode->u.minix_i.u.i1_data[block];
1150 mark_buffer_dirty(bh, 1);
1151 return bh;
1155 * The minix V2 function to synchronize an inode.
1157 static struct buffer_head * V2_minix_update_inode(struct inode * inode)
1159 struct buffer_head * bh;
1160 struct minix2_inode * raw_inode;
1161 int ino, block;
1163 ino = inode->i_ino;
1164 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1165 printk("Bad inode number on dev %s"
1166 ": %d is out of range\n",
1167 kdevname(inode->i_dev), ino);
1168 return 0;
1170 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
1171 (ino-1)/MINIX2_INODES_PER_BLOCK;
1172 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
1173 printk("unable to read i-node block\n");
1174 return 0;
1176 raw_inode = ((struct minix2_inode *)bh->b_data) +
1177 (ino-1)%MINIX2_INODES_PER_BLOCK;
1178 raw_inode->i_mode = inode->i_mode;
1179 raw_inode->i_uid = inode->i_uid;
1180 raw_inode->i_gid = inode->i_gid;
1181 raw_inode->i_nlinks = inode->i_nlink;
1182 raw_inode->i_size = inode->i_size;
1183 raw_inode->i_mtime = inode->i_mtime;
1184 raw_inode->i_atime = inode->i_atime;
1185 raw_inode->i_ctime = inode->i_ctime;
1186 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1187 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
1188 else for (block = 0; block < 10; block++)
1189 raw_inode->i_zone[block] = inode->u.minix_i.u.i2_data[block];
1190 mark_buffer_dirty(bh, 1);
1191 return bh;
1194 static struct buffer_head *minix_update_inode(struct inode *inode)
1196 if (INODE_VERSION(inode) == MINIX_V1)
1197 return V1_minix_update_inode(inode);
1198 else
1199 return V2_minix_update_inode(inode);
1202 static void minix_write_inode(struct inode * inode)
1204 struct buffer_head *bh;
1206 bh = minix_update_inode(inode);
1207 brelse(bh);
1210 int minix_sync_inode(struct inode * inode)
1212 int err = 0;
1213 struct buffer_head *bh;
1215 bh = minix_update_inode(inode);
1216 if (bh && buffer_dirty(bh))
1218 ll_rw_block(WRITE, 1, &bh);
1219 wait_on_buffer(bh);
1220 if (buffer_req(bh) && !buffer_uptodate(bh))
1222 printk ("IO error syncing minix inode ["
1223 "%s:%08lx]\n",
1224 kdevname(inode->i_dev), inode->i_ino);
1225 err = -1;
1228 else if (!bh)
1229 err = -1;
1230 brelse (bh);
1231 return err;
1234 static struct file_system_type minix_fs_type = {
1235 "minix",
1236 FS_REQUIRES_DEV,
1237 minix_read_super,
1238 NULL
1241 int __init init_minix_fs(void)
1243 return register_filesystem(&minix_fs_type);
1246 #ifdef MODULE
1247 EXPORT_NO_SYMBOLS;
1249 int init_module(void)
1251 return init_minix_fs();
1254 void cleanup_module(void)
1256 unregister_filesystem(&minix_fs_type);
1259 #endif