Merge with 2.3.48.
[linux-2.6/linux-mips.git] / fs / minix / inode.c
blob1d9ebb062b855979b8e2ae9abc02f3ee87fdc169
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/uaccess.h>
27 #include <asm/bitops.h>
29 #include <linux/minix_fs.h>
31 static void minix_read_inode(struct inode * inode);
32 static void minix_write_inode(struct inode * inode);
33 static int minix_statfs(struct super_block *sb, struct statfs *buf, int bufsiz);
34 static int minix_remount (struct super_block * sb, int * flags, char * data);
36 static void minix_delete_inode(struct inode *inode)
38 lock_kernel();
40 inode->i_size = 0;
41 minix_truncate(inode);
42 minix_free_inode(inode);
44 unlock_kernel();
47 static void minix_commit_super(struct super_block * sb)
49 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
50 sb->s_dirt = 0;
53 static void minix_write_super(struct super_block * sb)
55 struct minix_super_block * ms;
57 if (!(sb->s_flags & MS_RDONLY)) {
58 ms = sb->u.minix_sb.s_ms;
60 if (ms->s_state & MINIX_VALID_FS)
61 ms->s_state &= ~MINIX_VALID_FS;
62 minix_commit_super(sb);
64 sb->s_dirt = 0;
68 static void minix_put_super(struct super_block *sb)
70 int i;
72 if (!(sb->s_flags & MS_RDONLY)) {
73 sb->u.minix_sb.s_ms->s_state = sb->u.minix_sb.s_mount_state;
74 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
76 for (i = 0; i < sb->u.minix_sb.s_imap_blocks; i++)
77 brelse(sb->u.minix_sb.s_imap[i]);
78 for (i = 0; i < sb->u.minix_sb.s_zmap_blocks; i++)
79 brelse(sb->u.minix_sb.s_zmap[i]);
80 brelse (sb->u.minix_sb.s_sbh);
81 kfree(sb->u.minix_sb.s_imap);
83 MOD_DEC_USE_COUNT;
84 return;
87 static struct super_operations minix_sops = {
88 read_inode: minix_read_inode,
89 write_inode: minix_write_inode,
90 delete_inode: minix_delete_inode,
91 put_super: minix_put_super,
92 write_super: minix_write_super,
93 statfs: minix_statfs,
94 remount_fs: minix_remount,
97 static int minix_remount (struct super_block * sb, int * flags, char * data)
99 struct minix_super_block * ms;
101 ms = sb->u.minix_sb.s_ms;
102 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
103 return 0;
104 if (*flags & MS_RDONLY) {
105 if (ms->s_state & MINIX_VALID_FS ||
106 !(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
107 return 0;
108 /* Mounting a rw partition read-only. */
109 ms->s_state = sb->u.minix_sb.s_mount_state;
110 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
111 sb->s_dirt = 1;
112 minix_commit_super(sb);
114 else {
115 /* Mount a partition which is read-only, read-write. */
116 sb->u.minix_sb.s_mount_state = ms->s_state;
117 ms->s_state &= ~MINIX_VALID_FS;
118 mark_buffer_dirty(sb->u.minix_sb.s_sbh, 1);
119 sb->s_dirt = 1;
121 if (!(sb->u.minix_sb.s_mount_state & MINIX_VALID_FS))
122 printk ("MINIX-fs warning: remounting unchecked fs, "
123 "running fsck is recommended.\n");
124 else if ((sb->u.minix_sb.s_mount_state & MINIX_ERROR_FS))
125 printk ("MINIX-fs warning: remounting fs with errors, "
126 "running fsck is recommended.\n");
128 return 0;
132 * Check the root directory of the filesystem to make sure
133 * it really _is_ a Minix filesystem, and to check the size
134 * of the directory entry.
136 static const char * minix_checkroot(struct super_block *s, struct inode *dir)
138 struct buffer_head *bh;
139 struct minix_dir_entry *de;
140 const char * errmsg;
141 int dirsize;
143 if (!S_ISDIR(dir->i_mode))
144 return "root directory is not a directory";
146 bh = minix_bread(dir, 0, 0);
147 if (!bh)
148 return "unable to read root directory";
150 de = (struct minix_dir_entry *) bh->b_data;
151 errmsg = "bad root directory '.' entry";
152 dirsize = BLOCK_SIZE;
153 if (de->inode == MINIX_ROOT_INO && strcmp(de->name, ".") == 0) {
154 errmsg = "bad root directory '..' entry";
155 dirsize = 8;
158 while ((dirsize <<= 1) < BLOCK_SIZE) {
159 de = (struct minix_dir_entry *) (bh->b_data + dirsize);
160 if (de->inode != MINIX_ROOT_INO)
161 continue;
162 if (strcmp(de->name, ".."))
163 continue;
164 s->u.minix_sb.s_dirsize = dirsize;
165 s->u.minix_sb.s_namelen = dirsize - 2;
166 errmsg = NULL;
167 break;
169 brelse(bh);
170 return errmsg;
173 static struct super_block *minix_read_super(struct super_block *s, void *data,
174 int silent)
176 struct buffer_head *bh;
177 struct buffer_head **map;
178 struct minix_super_block *ms;
179 int i, block;
180 kdev_t dev = s->s_dev;
181 const char * errmsg;
182 struct inode *root_inode;
183 unsigned int hblock;
185 /* N.B. These should be compile-time tests.
186 Unfortunately that is impossible. */
187 if (32 != sizeof (struct minix_inode))
188 panic("bad V1 i-node size");
189 if (64 != sizeof(struct minix2_inode))
190 panic("bad V2 i-node size");
192 MOD_INC_USE_COUNT;
194 hblock = get_hardblocksize(dev);
195 if (hblock && hblock > BLOCK_SIZE)
196 goto out_bad_hblock;
198 lock_super(s);
199 set_blocksize(dev, BLOCK_SIZE);
200 if (!(bh = bread(dev,1,BLOCK_SIZE)))
201 goto out_bad_sb;
203 ms = (struct minix_super_block *) bh->b_data;
204 s->u.minix_sb.s_ms = ms;
205 s->u.minix_sb.s_sbh = bh;
206 s->u.minix_sb.s_mount_state = ms->s_state;
207 s->s_blocksize = BLOCK_SIZE;
208 s->s_blocksize_bits = BLOCK_SIZE_BITS;
209 s->u.minix_sb.s_ninodes = ms->s_ninodes;
210 s->u.minix_sb.s_nzones = ms->s_nzones;
211 s->u.minix_sb.s_imap_blocks = ms->s_imap_blocks;
212 s->u.minix_sb.s_zmap_blocks = ms->s_zmap_blocks;
213 s->u.minix_sb.s_firstdatazone = ms->s_firstdatazone;
214 s->u.minix_sb.s_log_zone_size = ms->s_log_zone_size;
215 s->u.minix_sb.s_max_size = ms->s_max_size;
216 s->s_magic = ms->s_magic;
217 if (s->s_magic == MINIX_SUPER_MAGIC) {
218 s->u.minix_sb.s_version = MINIX_V1;
219 s->u.minix_sb.s_dirsize = 16;
220 s->u.minix_sb.s_namelen = 14;
221 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
222 } else if (s->s_magic == MINIX_SUPER_MAGIC2) {
223 s->u.minix_sb.s_version = MINIX_V1;
224 s->u.minix_sb.s_dirsize = 32;
225 s->u.minix_sb.s_namelen = 30;
226 s->u.minix_sb.s_link_max = MINIX_LINK_MAX;
227 } else if (s->s_magic == MINIX2_SUPER_MAGIC) {
228 s->u.minix_sb.s_version = MINIX_V2;
229 s->u.minix_sb.s_nzones = ms->s_zones;
230 s->u.minix_sb.s_dirsize = 16;
231 s->u.minix_sb.s_namelen = 14;
232 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
233 } else if (s->s_magic == MINIX2_SUPER_MAGIC2) {
234 s->u.minix_sb.s_version = MINIX_V2;
235 s->u.minix_sb.s_nzones = ms->s_zones;
236 s->u.minix_sb.s_dirsize = 32;
237 s->u.minix_sb.s_namelen = 30;
238 s->u.minix_sb.s_link_max = MINIX2_LINK_MAX;
239 } else
240 goto out_no_fs;
243 * Allocate the buffer map to keep the superblock small.
245 i = (s->u.minix_sb.s_imap_blocks + s->u.minix_sb.s_zmap_blocks) * sizeof(bh);
246 map = kmalloc(i, GFP_KERNEL);
247 if (!map)
248 goto out_no_map;
249 memset(map, 0, i);
250 s->u.minix_sb.s_imap = &map[0];
251 s->u.minix_sb.s_zmap = &map[s->u.minix_sb.s_imap_blocks];
253 block=2;
254 for (i=0 ; i < s->u.minix_sb.s_imap_blocks ; i++) {
255 if (!(s->u.minix_sb.s_imap[i]=bread(dev,block,BLOCK_SIZE)))
256 goto out_no_bitmap;
257 block++;
259 for (i=0 ; i < s->u.minix_sb.s_zmap_blocks ; i++) {
260 if (!(s->u.minix_sb.s_zmap[i]=bread(dev,block,BLOCK_SIZE)))
261 goto out_no_bitmap;
262 block++;
265 minix_set_bit(0,s->u.minix_sb.s_imap[0]->b_data);
266 minix_set_bit(0,s->u.minix_sb.s_zmap[0]->b_data);
267 /* set up enough so that it can read an inode */
268 s->s_op = &minix_sops;
269 root_inode = iget(s, MINIX_ROOT_INO);
270 if (!root_inode)
271 goto out_no_root;
273 * Check the fs before we get the root dentry ...
275 errmsg = minix_checkroot(s, root_inode);
276 if (errmsg)
277 goto out_bad_root;
279 s->s_root = d_alloc_root(root_inode);
280 if (!s->s_root)
281 goto out_iput;
283 s->s_root->d_op = &minix_dentry_operations;
285 if (!(s->s_flags & MS_RDONLY)) {
286 ms->s_state &= ~MINIX_VALID_FS;
287 mark_buffer_dirty(bh, 1);
288 s->s_dirt = 1;
290 unlock_super(s);
291 if (!(s->u.minix_sb.s_mount_state & MINIX_VALID_FS))
292 printk ("MINIX-fs: mounting unchecked file system, "
293 "running fsck is recommended.\n");
294 else if (s->u.minix_sb.s_mount_state & MINIX_ERROR_FS)
295 printk ("MINIX-fs: mounting file system with errors, "
296 "running fsck is recommended.\n");
297 return s;
299 out_bad_root:
300 if (!silent)
301 printk("MINIX-fs: %s\n", errmsg);
302 out_iput:
303 iput(root_inode);
304 goto out_freemap;
306 out_no_root:
307 if (!silent)
308 printk("MINIX-fs: get root inode failed\n");
309 goto out_freemap;
311 out_no_bitmap:
312 printk("MINIX-fs: bad superblock or unable to read bitmaps\n");
313 out_freemap:
314 for (i = 0; i < s->u.minix_sb.s_imap_blocks; i++)
315 brelse(s->u.minix_sb.s_imap[i]);
316 for (i = 0; i < s->u.minix_sb.s_zmap_blocks; i++)
317 brelse(s->u.minix_sb.s_zmap[i]);
318 kfree(s->u.minix_sb.s_imap);
319 goto out_release;
321 out_no_map:
322 if (!silent)
323 printk ("MINIX-fs: can't allocate map\n");
324 goto out_release;
326 out_no_fs:
327 if (!silent)
328 printk("VFS: Can't find a Minix or Minix V2 filesystem on device "
329 "%s.\n", kdevname(dev));
330 out_release:
331 brelse(bh);
332 goto out_unlock;
334 out_bad_hblock:
335 printk("MINIX-fs: blocksize too small for device.\n");
336 goto out;
338 out_bad_sb:
339 printk("MINIX-fs: unable to read superblock\n");
340 out_unlock:
341 unlock_super(s);
342 out:
343 s->s_dev = 0;
344 MOD_DEC_USE_COUNT;
345 return NULL;
348 static int minix_statfs(struct super_block *sb, struct statfs *buf, int bufsiz)
350 struct statfs tmp;
352 tmp.f_type = sb->s_magic;
353 tmp.f_bsize = sb->s_blocksize;
354 tmp.f_blocks = (sb->u.minix_sb.s_nzones - sb->u.minix_sb.s_firstdatazone) << sb->u.minix_sb.s_log_zone_size;
355 tmp.f_bfree = minix_count_free_blocks(sb);
356 tmp.f_bavail = tmp.f_bfree;
357 tmp.f_files = sb->u.minix_sb.s_ninodes;
358 tmp.f_ffree = minix_count_free_inodes(sb);
359 tmp.f_namelen = sb->u.minix_sb.s_namelen;
360 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
364 * The minix V1 fs bmap functions.
366 #define V1_inode_bmap(inode,nr) (((unsigned short *)(inode)->u.minix_i.u.i1_data)[(nr)])
368 static int V1_block_bmap(struct buffer_head * bh, int nr)
370 int tmp;
372 if (!bh)
373 return 0;
374 tmp = ((unsigned short *) bh->b_data)[nr];
375 brelse(bh);
376 return tmp;
379 static int V1_minix_block_map(struct inode * inode, long block)
381 int i, ret;
383 ret = 0;
384 lock_kernel();
385 if (block < 0) {
386 printk("minix_bmap: block<0");
387 goto out;
389 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
390 printk("minix_bmap: block>big");
391 goto out;
393 if (block < 7) {
394 ret = V1_inode_bmap(inode,block);
395 goto out;
397 block -= 7;
398 if (block < 512) {
399 i = V1_inode_bmap(inode,7);
400 if (!i)
401 goto out;
402 ret = V1_block_bmap(bread(inode->i_dev, i,
403 BLOCK_SIZE), block);
404 goto out;
406 block -= 512;
407 i = V1_inode_bmap(inode,8);
408 if (!i)
409 goto out;
410 i = V1_block_bmap(bread(inode->i_dev,i,BLOCK_SIZE),block>>9);
411 if (!i)
412 goto out;
413 ret = V1_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
414 block & 511);
415 out:
416 unlock_kernel();
417 return ret;
421 * The minix V2 fs bmap functions.
423 #define V2_inode_bmap(inode,nr) (((unsigned int *)(inode)->u.minix_i.u.i2_data)[(nr)])
424 static int V2_block_bmap(struct buffer_head * bh, int nr)
426 int tmp;
428 if (!bh)
429 return 0;
430 tmp = ((unsigned int *) bh->b_data)[nr];
431 brelse(bh);
432 return tmp;
435 static int V2_minix_block_map(struct inode * inode, int block)
437 int i, ret;
439 ret = 0;
440 lock_kernel();
441 if (block < 0) {
442 printk("minix_bmap: block<0");
443 goto out;
445 if (block >= (inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)) {
446 printk("minix_bmap: block>big");
447 goto out;
449 if (block < 7) {
450 ret = V2_inode_bmap(inode,block);
451 goto out;
453 block -= 7;
454 if (block < 256) {
455 i = V2_inode_bmap(inode, 7);
456 if (!i)
457 goto out;
458 ret = V2_block_bmap(bread(inode->i_dev, i,
459 BLOCK_SIZE), block);
460 goto out;
462 block -= 256;
463 if (block < (256 * 256)) {
464 i = V2_inode_bmap(inode, 8);
465 if (!i)
466 goto out;
467 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
468 block >> 8);
469 if (!i)
470 goto out;
471 ret = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
472 block & 255);
473 goto out;
475 block -= (256 * 256);
476 i = V2_inode_bmap(inode, 9);
477 if (!i)
478 goto out;
479 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
480 block >> 16);
481 if (!i)
482 goto out;
483 i = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
484 (block >> 8) & 255);
485 if (!i)
486 goto out;
487 ret = V2_block_bmap(bread(inode->i_dev, i, BLOCK_SIZE),
488 block & 255);
489 out:
490 unlock_kernel();
491 return ret;
495 * The minix V1 fs getblk functions.
497 static struct buffer_head * V1_inode_getblk(struct inode * inode, int nr,
498 int new_block, int *err,
499 int metadata, int *phys, int *new)
501 int tmp;
502 unsigned short *p;
503 struct buffer_head * result;
505 p = inode->u.minix_i.u.i1_data + nr;
506 repeat:
507 tmp = *p;
508 if (tmp) {
509 if (metadata) {
510 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
511 if (tmp == *p)
512 return result;
513 brelse(result);
514 goto repeat;
515 } else {
516 *phys = tmp;
517 return NULL;
520 *err = -EFBIG;
522 /* Check file limits.. */
524 unsigned long limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
525 if (limit < RLIM_INFINITY) {
526 limit >>= BLOCK_SIZE_BITS;
527 if (new_block >= limit) {
528 send_sig(SIGXFSZ, current, 0);
529 *err = -EFBIG;
530 return NULL;
535 tmp = minix_new_block(inode);
536 if (!tmp) {
537 *err = -ENOSPC;
538 return NULL;
540 if (metadata) {
541 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
542 if (*p) {
543 minix_free_block(inode, tmp);
544 brelse(result);
545 goto repeat;
547 memset(result->b_data, 0, BLOCK_SIZE);
548 mark_buffer_uptodate(result, 1);
549 mark_buffer_dirty(result, 1);
550 } else {
551 if (*p) {
553 * Nobody is allowed to change block allocation
554 * state from under us:
556 BUG();
557 minix_free_block(inode, tmp);
558 goto repeat;
560 *phys = tmp;
561 result = NULL;
562 *err = 0;
563 *new = 1;
565 *p = tmp;
567 inode->i_ctime = CURRENT_TIME;
568 mark_inode_dirty(inode);
569 return result;
572 static struct buffer_head * V1_block_getblk(struct inode * inode,
573 struct buffer_head * bh, int nr, int new_block, int *err,
574 int metadata, int *phys, int *new)
576 int tmp;
577 unsigned short *p;
578 struct buffer_head * result;
579 unsigned long limit;
581 result = NULL;
582 if (!bh)
583 goto out;
584 if (!buffer_uptodate(bh)) {
585 ll_rw_block(READ, 1, &bh);
586 wait_on_buffer(bh);
587 if (!buffer_uptodate(bh))
588 goto out;
590 p = nr + (unsigned short *) bh->b_data;
591 repeat:
592 tmp = *p;
593 if (tmp) {
594 if (metadata) {
595 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
596 if (tmp == *p)
597 goto out;
598 brelse(result);
599 goto repeat;
600 } else {
601 *phys = tmp;
602 goto out;
605 *err = -EFBIG;
607 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
608 if (limit < RLIM_INFINITY) {
609 limit >>= BLOCK_SIZE_BITS;
610 if (new_block >= limit) {
611 send_sig(SIGXFSZ, current, 0);
612 goto out;
616 tmp = minix_new_block(inode);
617 if (!tmp)
618 goto out;
619 if (metadata) {
620 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
621 if (*p) {
622 minix_free_block(inode, tmp);
623 brelse(result);
624 goto repeat;
626 memset(result->b_data, 0, BLOCK_SIZE);
627 mark_buffer_uptodate(result, 1);
628 mark_buffer_dirty(result, 1);
629 } else {
630 *phys = tmp;
631 *new = 1;
633 if (*p) {
634 minix_free_block(inode, tmp);
635 brelse(result);
636 goto repeat;
639 *p = tmp;
640 mark_buffer_dirty(bh, 1);
641 *err = 0;
642 out:
643 brelse(bh);
644 return result;
647 static int V1_get_block(struct inode * inode, long block,
648 struct buffer_head *bh_result, int create)
650 int ret, err, new, phys, ptr;
651 struct buffer_head *bh;
653 if (!create) {
654 phys = V1_minix_block_map(inode, block);
655 if (phys) {
656 bh_result->b_dev = inode->i_dev;
657 bh_result->b_blocknr = phys;
658 bh_result->b_state |= (1UL << BH_Mapped);
660 return 0;
663 err = -EIO;
664 new = 0;
665 ret = 0;
666 bh = NULL;
668 lock_kernel();
669 if (block < 0)
670 goto abort_negative;
671 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)
672 goto abort_too_big;
674 err = 0;
675 ptr = block;
677 * ok, these macros clean the logic up a bit and make
678 * it much more readable:
680 #define GET_INODE_DATABLOCK(x) \
681 V1_inode_getblk(inode, x, block, &err, 0, &phys, &new)
682 #define GET_INODE_PTR(x) \
683 V1_inode_getblk(inode, x, block, &err, 1, NULL, NULL)
684 #define GET_INDIRECT_DATABLOCK(x) \
685 V1_block_getblk(inode, bh, x, block, &err, 0, &phys, &new)
686 #define GET_INDIRECT_PTR(x) \
687 V1_block_getblk(inode, bh, x, block, &err, 1, NULL, NULL)
689 if (ptr < 7) {
690 bh = GET_INODE_DATABLOCK(ptr);
691 goto out;
693 ptr -= 7;
694 if (ptr < 512) {
695 bh = GET_INODE_PTR(7);
696 goto get_indirect;
698 ptr -= 512;
699 bh = GET_INODE_PTR(8);
700 bh = GET_INDIRECT_PTR((ptr >> 9) & 511);
701 get_indirect:
702 bh = GET_INDIRECT_DATABLOCK(ptr & 511);
704 #undef GET_INODE_DATABLOCK
705 #undef GET_INODE_PTR
706 #undef GET_INDIRECT_DATABLOCK
707 #undef GET_INDIRECT_PTR
709 out:
710 if (err)
711 goto abort;
712 bh_result->b_dev = inode->i_dev;
713 bh_result->b_blocknr = phys;
714 bh_result->b_state |= (1UL << BH_Mapped);
715 if (new)
716 bh_result->b_state |= (1UL << BH_New);
717 abort:
718 unlock_kernel();
719 return err;
721 abort_negative:
722 printk("minix_getblk: block<0");
723 goto abort;
725 abort_too_big:
726 printk("minix_getblk: block>big");
727 goto abort;
731 * The minix V2 fs getblk functions.
733 static struct buffer_head * V2_inode_getblk(struct inode * inode, int nr,
734 int new_block, int *err,
735 int metadata, int *phys, int *new)
737 int tmp;
738 unsigned int *p;
739 struct buffer_head * result;
741 p = (unsigned int *) inode->u.minix_i.u.i2_data + nr;
742 repeat:
743 tmp = *p;
744 if (tmp) {
745 if (metadata) {
746 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
747 if (tmp == *p)
748 return result;
749 brelse(result);
750 goto repeat;
751 } else {
752 *phys = tmp;
753 return NULL;
756 *err = -EFBIG;
758 /* Check file limits.. */
760 unsigned long limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
761 if (limit < RLIM_INFINITY) {
762 limit >>= BLOCK_SIZE_BITS;
763 if (new_block >= limit) {
764 send_sig(SIGXFSZ, current, 0);
765 *err = -EFBIG;
766 return NULL;
771 tmp = minix_new_block(inode);
772 if (!tmp) {
773 *err = -ENOSPC;
774 return NULL;
776 if (metadata) {
777 result = getblk(inode->i_dev, tmp, BLOCK_SIZE);
778 if (*p) {
779 minix_free_block(inode, tmp);
780 brelse(result);
781 goto repeat;
783 memset(result->b_data, 0, BLOCK_SIZE);
784 mark_buffer_uptodate(result, 1);
785 mark_buffer_dirty(result, 1);
786 } else {
787 if (*p) {
789 * Nobody is allowed to change block allocation
790 * state from under us:
792 BUG();
793 minix_free_block(inode, tmp);
794 goto repeat;
796 *phys = tmp;
797 result = NULL;
798 *err = 0;
799 *new = 1;
801 *p = tmp;
803 inode->i_ctime = CURRENT_TIME;
804 mark_inode_dirty(inode);
805 return result;
808 static struct buffer_head * V2_block_getblk(struct inode * inode,
809 struct buffer_head * bh, int nr, int new_block, int *err,
810 int metadata, int *phys, int *new)
812 int tmp;
813 unsigned int *p;
814 struct buffer_head * result;
815 unsigned long limit;
817 result = NULL;
818 if (!bh)
819 goto out;
820 if (!buffer_uptodate(bh)) {
821 ll_rw_block(READ, 1, &bh);
822 wait_on_buffer(bh);
823 if (!buffer_uptodate(bh))
824 goto out;
826 p = nr + (unsigned int *) bh->b_data;
827 repeat:
828 tmp = *p;
829 if (tmp) {
830 if (metadata) {
831 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
832 if (tmp == *p)
833 goto out;
834 brelse(result);
835 goto repeat;
836 } else {
837 *phys = tmp;
838 goto out;
841 *err = -EFBIG;
843 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
844 if (limit < RLIM_INFINITY) {
845 limit >>= BLOCK_SIZE_BITS;
846 if (new_block >= limit) {
847 send_sig(SIGXFSZ, current, 0);
848 goto out;
852 tmp = minix_new_block(inode);
853 if (!tmp)
854 goto out;
855 if (metadata) {
856 result = getblk(bh->b_dev, tmp, BLOCK_SIZE);
857 if (*p) {
858 minix_free_block(inode, tmp);
859 brelse(result);
860 goto repeat;
862 memset(result->b_data, 0, BLOCK_SIZE);
863 mark_buffer_uptodate(result, 1);
864 mark_buffer_dirty(result, 1);
865 } else {
866 *phys = tmp;
867 *new = 1;
869 if (*p) {
870 minix_free_block(inode, tmp);
871 brelse(result);
872 goto repeat;
875 *p = tmp;
876 mark_buffer_dirty(bh, 1);
877 *err = 0;
878 out:
879 brelse(bh);
880 return result;
883 static int V2_get_block(struct inode * inode, long block,
884 struct buffer_head *bh_result, int create)
886 int ret, err, new, phys, ptr;
887 struct buffer_head * bh;
889 if (!create) {
890 phys = V2_minix_block_map(inode, block);
891 if (phys) {
892 bh_result->b_dev = inode->i_dev;
893 bh_result->b_blocknr = phys;
894 bh_result->b_state |= (1UL << BH_Mapped);
896 return 0;
899 err = -EIO;
900 new = 0;
901 ret = 0;
902 bh = NULL;
904 lock_kernel();
905 if (block < 0)
906 goto abort_negative;
907 if (block >= inode->i_sb->u.minix_sb.s_max_size/BLOCK_SIZE)
908 goto abort_too_big;
910 err = 0;
911 ptr = block;
913 * ok, these macros clean the logic up a bit and make
914 * it much more readable:
916 #define GET_INODE_DATABLOCK(x) \
917 V2_inode_getblk(inode, x, block, &err, 0, &phys, &new)
918 #define GET_INODE_PTR(x) \
919 V2_inode_getblk(inode, x, block, &err, 1, NULL, NULL)
920 #define GET_INDIRECT_DATABLOCK(x) \
921 V2_block_getblk(inode, bh, x, block, &err, 0, &phys, &new)
922 #define GET_INDIRECT_PTR(x) \
923 V2_block_getblk(inode, bh, x, block, &err, 1, NULL, NULL)
925 if (ptr < 7) {
926 bh = GET_INODE_DATABLOCK(ptr);
927 goto out;
929 ptr -= 7;
930 if (ptr < 256) {
931 bh = GET_INODE_PTR(7);
932 goto get_indirect;
934 ptr -= 256;
935 if (ptr < 256*256) {
936 bh = GET_INODE_PTR(8);
937 goto get_double;
939 ptr -= 256*256;
940 bh = GET_INODE_PTR(9);
941 bh = GET_INDIRECT_PTR((ptr >> 16) & 255);
942 get_double:
943 bh = GET_INDIRECT_PTR((ptr >> 8) & 255);
944 get_indirect:
945 bh = GET_INDIRECT_DATABLOCK(ptr & 255);
947 #undef GET_INODE_DATABLOCK
948 #undef GET_INODE_PTR
949 #undef GET_INDIRECT_DATABLOCK
950 #undef GET_INDIRECT_PTR
952 out:
953 if (err)
954 goto abort;
955 bh_result->b_dev = inode->i_dev;
956 bh_result->b_blocknr = phys;
957 bh_result->b_state |= (1UL << BH_Mapped);
958 if (new)
959 bh_result->b_state |= (1UL << BH_New);
960 abort:
961 unlock_kernel();
962 return err;
964 abort_negative:
965 printk("minix_getblk: block<0");
966 goto abort;
968 abort_too_big:
969 printk("minix_getblk: block>big");
970 goto abort;
973 static int minix_get_block(struct inode *inode, long block,
974 struct buffer_head *bh_result, int create)
976 if (INODE_VERSION(inode) == MINIX_V1)
977 return V1_get_block(inode, block, bh_result, create);
978 else
979 return V2_get_block(inode, block, bh_result, create);
983 * the global minix fs getblk function.
985 struct buffer_head *minix_getblk(struct inode *inode, int block, int create)
987 struct buffer_head dummy;
988 int error;
990 dummy.b_state = 0;
991 dummy.b_blocknr = -1000;
992 error = minix_get_block(inode, block, &dummy, create);
993 if (!error && buffer_mapped(&dummy)) {
994 struct buffer_head *bh;
995 bh = getblk(dummy.b_dev, dummy.b_blocknr, BLOCK_SIZE);
996 if (buffer_new(&dummy)) {
997 memset(bh->b_data, 0, BLOCK_SIZE);
998 mark_buffer_uptodate(bh, 1);
999 mark_buffer_dirty(bh, 1);
1001 return bh;
1003 return NULL;
1006 struct buffer_head * minix_bread(struct inode * inode, int block, int create)
1008 struct buffer_head * bh;
1010 bh = minix_getblk(inode, block, create);
1011 if (!bh || buffer_uptodate(bh))
1012 return bh;
1013 ll_rw_block(READ, 1, &bh);
1014 wait_on_buffer(bh);
1015 if (buffer_uptodate(bh))
1016 return bh;
1017 brelse(bh);
1018 return NULL;
1021 static int minix_writepage(struct dentry *dentry, struct page *page)
1023 return block_write_full_page(page,minix_get_block);
1025 static int minix_readpage(struct dentry *dentry, struct page *page)
1027 return block_read_full_page(page,minix_get_block);
1029 static int minix_prepare_write(struct page *page, unsigned from, unsigned to)
1031 return block_prepare_write(page,from,to,minix_get_block);
1033 static int minix_bmap(struct address_space *mapping, long block)
1035 return generic_block_bmap(mapping,block,minix_get_block);
1037 struct address_space_operations minix_aops = {
1038 readpage: minix_readpage,
1039 writepage: minix_writepage,
1040 prepare_write: minix_prepare_write,
1041 commit_write: generic_commit_write,
1042 bmap: minix_bmap
1046 * The minix V1 function to read an inode.
1048 static void V1_minix_read_inode(struct inode * inode)
1050 struct buffer_head * bh;
1051 struct minix_inode * raw_inode;
1052 int block, ino;
1054 ino = inode->i_ino;
1055 inode->i_mode = 0;
1056 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1057 printk("Bad inode number on dev %s"
1058 ": %d is out of range\n",
1059 kdevname(inode->i_dev), ino);
1060 return;
1062 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
1063 inode->i_sb->u.minix_sb.s_zmap_blocks +
1064 (ino-1)/MINIX_INODES_PER_BLOCK;
1065 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
1066 printk("Major problem: unable to read inode from dev "
1067 "%s\n", kdevname(inode->i_dev));
1068 return;
1070 raw_inode = ((struct minix_inode *) bh->b_data) +
1071 (ino-1)%MINIX_INODES_PER_BLOCK;
1072 inode->i_mode = raw_inode->i_mode;
1073 inode->i_uid = (uid_t)raw_inode->i_uid;
1074 inode->i_gid = (gid_t)raw_inode->i_gid;
1075 inode->i_nlink = raw_inode->i_nlinks;
1076 inode->i_size = raw_inode->i_size;
1077 inode->i_mtime = inode->i_atime = inode->i_ctime = raw_inode->i_time;
1078 inode->i_blocks = inode->i_blksize = 0;
1079 for (block = 0; block < 9; block++)
1080 inode->u.minix_i.u.i1_data[block] = raw_inode->i_zone[block];
1081 if (S_ISREG(inode->i_mode)) {
1082 inode->i_op = &minix_file_inode_operations;
1083 inode->i_fop = &minix_file_operations;
1084 inode->i_mapping->a_ops = &minix_aops;
1085 } else if (S_ISDIR(inode->i_mode)) {
1086 inode->i_op = &minix_dir_inode_operations;
1087 inode->i_fop = &minix_dir_operations;
1088 } else if (S_ISLNK(inode->i_mode)) {
1089 inode->i_op = &page_symlink_inode_operations;
1090 inode->i_mapping->a_ops = &minix_aops;
1091 } else
1092 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
1093 brelse(bh);
1097 * The minix V2 function to read an inode.
1099 static void V2_minix_read_inode(struct inode * inode)
1101 struct buffer_head * bh;
1102 struct minix2_inode * raw_inode;
1103 int block, ino;
1105 ino = inode->i_ino;
1106 inode->i_mode = 0;
1107 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1108 printk("Bad inode number on dev %s"
1109 ": %d is out of range\n",
1110 kdevname(inode->i_dev), ino);
1111 return;
1113 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks +
1114 inode->i_sb->u.minix_sb.s_zmap_blocks +
1115 (ino-1)/MINIX2_INODES_PER_BLOCK;
1116 if (!(bh=bread(inode->i_dev,block, BLOCK_SIZE))) {
1117 printk("Major problem: unable to read inode from dev "
1118 "%s\n", kdevname(inode->i_dev));
1119 return;
1121 raw_inode = ((struct minix2_inode *) bh->b_data) +
1122 (ino-1)%MINIX2_INODES_PER_BLOCK;
1123 inode->i_mode = raw_inode->i_mode;
1124 inode->i_uid = (uid_t)raw_inode->i_uid;
1125 inode->i_gid = (gid_t)raw_inode->i_gid;
1126 inode->i_nlink = raw_inode->i_nlinks;
1127 inode->i_size = raw_inode->i_size;
1128 inode->i_mtime = raw_inode->i_mtime;
1129 inode->i_atime = raw_inode->i_atime;
1130 inode->i_ctime = raw_inode->i_ctime;
1131 inode->i_blocks = inode->i_blksize = 0;
1132 for (block = 0; block < 10; block++)
1133 inode->u.minix_i.u.i2_data[block] = raw_inode->i_zone[block];
1134 if (S_ISREG(inode->i_mode)) {
1135 inode->i_op = &minix_file_inode_operations;
1136 inode->i_fop = &minix_file_operations;
1137 inode->i_mapping->a_ops = &minix_aops;
1138 } else if (S_ISDIR(inode->i_mode)) {
1139 inode->i_op = &minix_dir_inode_operations;
1140 inode->i_fop = &minix_dir_operations;
1141 } else if (S_ISLNK(inode->i_mode)) {
1142 inode->i_op = &page_symlink_inode_operations;
1143 inode->i_mapping->a_ops = &minix_aops;
1144 } else
1145 init_special_inode(inode, inode->i_mode, raw_inode->i_zone[0]);
1146 brelse(bh);
1150 * The global function to read an inode.
1152 static void minix_read_inode(struct inode * inode)
1154 if (INODE_VERSION(inode) == MINIX_V1)
1155 V1_minix_read_inode(inode);
1156 else
1157 V2_minix_read_inode(inode);
1161 * The minix V1 function to synchronize an inode.
1163 static struct buffer_head * V1_minix_update_inode(struct inode * inode)
1165 struct buffer_head * bh;
1166 struct minix_inode * raw_inode;
1167 int ino, block;
1169 ino = inode->i_ino;
1170 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1171 printk("Bad inode number on dev %s"
1172 ": %d is out of range\n",
1173 kdevname(inode->i_dev), ino);
1174 return 0;
1176 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
1177 (ino-1)/MINIX_INODES_PER_BLOCK;
1178 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
1179 printk("unable to read i-node block\n");
1180 return 0;
1182 raw_inode = ((struct minix_inode *)bh->b_data) +
1183 (ino-1)%MINIX_INODES_PER_BLOCK;
1184 raw_inode->i_mode = inode->i_mode;
1185 raw_inode->i_uid = fs_high2lowuid(inode->i_uid);
1186 raw_inode->i_gid = fs_high2lowgid(inode->i_gid);
1187 raw_inode->i_nlinks = inode->i_nlink;
1188 raw_inode->i_size = inode->i_size;
1189 raw_inode->i_time = inode->i_mtime;
1190 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1191 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
1192 else for (block = 0; block < 9; block++)
1193 raw_inode->i_zone[block] = inode->u.minix_i.u.i1_data[block];
1194 mark_buffer_dirty(bh, 1);
1195 return bh;
1199 * The minix V2 function to synchronize an inode.
1201 static struct buffer_head * V2_minix_update_inode(struct inode * inode)
1203 struct buffer_head * bh;
1204 struct minix2_inode * raw_inode;
1205 int ino, block;
1207 ino = inode->i_ino;
1208 if (!ino || ino > inode->i_sb->u.minix_sb.s_ninodes) {
1209 printk("Bad inode number on dev %s"
1210 ": %d is out of range\n",
1211 kdevname(inode->i_dev), ino);
1212 return 0;
1214 block = 2 + inode->i_sb->u.minix_sb.s_imap_blocks + inode->i_sb->u.minix_sb.s_zmap_blocks +
1215 (ino-1)/MINIX2_INODES_PER_BLOCK;
1216 if (!(bh=bread(inode->i_dev, block, BLOCK_SIZE))) {
1217 printk("unable to read i-node block\n");
1218 return 0;
1220 raw_inode = ((struct minix2_inode *)bh->b_data) +
1221 (ino-1)%MINIX2_INODES_PER_BLOCK;
1222 raw_inode->i_mode = inode->i_mode;
1223 raw_inode->i_uid = fs_high2lowuid(inode->i_uid);
1224 raw_inode->i_gid = fs_high2lowgid(inode->i_gid);
1225 raw_inode->i_nlinks = inode->i_nlink;
1226 raw_inode->i_size = inode->i_size;
1227 raw_inode->i_mtime = inode->i_mtime;
1228 raw_inode->i_atime = inode->i_atime;
1229 raw_inode->i_ctime = inode->i_ctime;
1230 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1231 raw_inode->i_zone[0] = kdev_t_to_nr(inode->i_rdev);
1232 else for (block = 0; block < 10; block++)
1233 raw_inode->i_zone[block] = inode->u.minix_i.u.i2_data[block];
1234 mark_buffer_dirty(bh, 1);
1235 return bh;
1238 static struct buffer_head *minix_update_inode(struct inode *inode)
1240 if (INODE_VERSION(inode) == MINIX_V1)
1241 return V1_minix_update_inode(inode);
1242 else
1243 return V2_minix_update_inode(inode);
1246 static void minix_write_inode(struct inode * inode)
1248 struct buffer_head *bh;
1250 lock_kernel();
1251 bh = minix_update_inode(inode);
1252 unlock_kernel();
1253 brelse(bh);
1256 int minix_sync_inode(struct inode * inode)
1258 int err = 0;
1259 struct buffer_head *bh;
1261 bh = minix_update_inode(inode);
1262 if (bh && buffer_dirty(bh))
1264 ll_rw_block(WRITE, 1, &bh);
1265 wait_on_buffer(bh);
1266 if (buffer_req(bh) && !buffer_uptodate(bh))
1268 printk ("IO error syncing minix inode ["
1269 "%s:%08lx]\n",
1270 kdevname(inode->i_dev), inode->i_ino);
1271 err = -1;
1274 else if (!bh)
1275 err = -1;
1276 brelse (bh);
1277 return err;
1280 static struct file_system_type minix_fs_type = {
1281 "minix",
1282 FS_REQUIRES_DEV,
1283 minix_read_super,
1284 NULL
1287 int __init init_minix_fs(void)
1289 return register_filesystem(&minix_fs_type);
1292 #ifdef MODULE
1293 EXPORT_NO_SYMBOLS;
1295 int init_module(void)
1297 return init_minix_fs();
1300 void cleanup_module(void)
1302 unregister_filesystem(&minix_fs_type);
1305 #endif