super.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (C) 2005 - Alejandro Liu Ly <alejandro_liu@hotmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * @project mfstool
  20. * @module super
  21. * @section 3
  22. * @doc Superblock functions
  23. */
  24. #include "minix_fs.h"
  25. #include "protos.h"
  26. #include "bitops.h"
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <time.h>
  31. /**
  32. * Find a free bit in map
  33. * @param bmap - bitmap to scan
  34. * @param bsize - bitmap size in blocks
  35. * @return the bit number of the found block
  36. */
  37. unsigned long get_free_bit(u8 *bmap,int bsize) {
  38. int i;
  39. for (i = 0; i < bsize * BLOCK_SIZE; i++) {
  40. if (bmap[i] != 0xff) {
  41. int j;
  42. for (j = 0; j < 8 ; j++) {
  43. if ((bmap[i] & (1<<j)) == 0) return (i<<3) + j;
  44. }
  45. die("Internal error!");
  46. }
  47. }
  48. fatalmsg("No free slots in bitmap found");
  49. return -1;
  50. }
  51. /**
  52. * Initializes a new filesystem
  53. * @param fn - file name for new filesystem
  54. * @param magic - superblock magic number
  55. * @param fsize - size of filesystem in blocks
  56. * @param inodes - number of inodes to allocate (0 for auto)
  57. * @return pointer to a minix_fs_dat structure
  58. * @effect the file will be created or truncated.
  59. */
  60. struct minix_fs_dat *new_fs(const char *fn,int magic,unsigned long fsize,int inodes) {
  61. struct minix_fs_dat *fs = domalloc(sizeof(struct minix_fs_dat),0);
  62. u32 rootblkp;
  63. char root_block[BLOCK_SIZE];
  64. int i;
  65. if (magic != MINIX_SUPER_MAGIC && magic != MINIX_SUPER_MAGIC2 &&
  66. magic != MINIX2_SUPER_MAGIC && magic != MINIX2_SUPER_MAGIC2) {
  67. fatalmsg("invalid magic fs-type %x",magic);
  68. }
  69. fs->msb.s_magic = magic;
  70. if (VERSION_2(fs)) {
  71. fs->msb.s_zones = fsize;
  72. } else {
  73. fs->msb.s_zones = fsize;
  74. }
  75. fs->msb.s_state = MINIX_VALID_FS;
  76. fs->msb.s_max_size = VERSION_2(fs) ? 0x7fffffff : (7+512+512*512) * 1024;
  77. /* Manage inodes */
  78. if (!inodes) inodes = fsize / 3; /* Default number inodes to 1/3 blocks */
  79. /* Round up inode count */
  80. if (VERSION_2(fs))
  81. inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
  82. ~(MINIX2_INODES_PER_BLOCK - 1));
  83. else
  84. inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
  85. ~(MINIX_INODES_PER_BLOCK - 1));
  86. if (inodes > 65535) inodes = 65535;
  87. INODES(fs) = inodes;
  88. if (INODE_BLOCKS(fs) > fsize * 9 / 10 + 5)
  89. fatalmsg("Too many inodes requested");
  90. /*
  91. * Initialise bitmaps
  92. */
  93. IMAPS(fs) =UPPER(inodes + 1,BITS_PER_BLOCK);
  94. ZMAPS(fs)=UPPER(fsize-(1+fs->msb.s_imap_blocks+INODE_BLOCKS(fs)),
  95. BITS_PER_BLOCK+1);
  96. FIRSTZONE(fs) = NORM_FIRSTZONE(fs);
  97. fs->inode_bmap = domalloc(IMAPS(fs) * BLOCK_SIZE,0xff);
  98. fs->zone_bmap = domalloc(ZMAPS(fs) * BLOCK_SIZE,0xff);
  99. for (i = FIRSTZONE(fs) ; i < fsize ; i++) unmark_zone(fs,i);
  100. for (i = MINIX_ROOT_INO; i<=INODES(fs) ; i++) unmark_inode(fs,i);
  101. /*
  102. * Initialize inode tables
  103. */
  104. fs->ino.v1 = domalloc(INODE_BUFFER_SIZE(fs),0);
  105. mark_inode(fs,MINIX_ROOT_INO);
  106. set_inode(fs,MINIX_ROOT_INO,S_IFDIR | 0755, 2,
  107. opt_squash ? 0 : getuid(), opt_squash ? 0 : getgid(),
  108. 2 * DIRSIZE(fs),NOW,NOW,NOW,0);
  109. rootblkp = get_free_block(fs);
  110. if (VERSION_2(fs)) {
  111. INODE2(fs,MINIX_ROOT_INO)->i_zone[0] = rootblkp;
  112. } else {
  113. INODE2(fs,MINIX_ROOT_INO)->i_zone[0] = rootblkp;
  114. }
  115. mark_zone(fs,rootblkp);
  116. /*
  117. * Initialise file
  118. */
  119. fs->fp = fopen(fn,"w+b");
  120. if (!fs->fp) die(fn);
  121. if (fseek(fs->fp,fsize * BLOCK_SIZE-1,SEEK_SET)) die("fseek");
  122. putc(0,fs->fp);
  123. fflush(fs->fp);
  124. /*
  125. * Create a root block
  126. */
  127. memset(root_block,0,sizeof(root_block));
  128. *((short *)root_block) =MINIX_ROOT_INO;
  129. strcpy(root_block+2,".");
  130. *((short *)(root_block+DIRSIZE(fs))) = MINIX_ROOT_INO;
  131. strcpy(root_block+2+DIRSIZE(fs),"..");
  132. // printf("WRITE TO : %d %08x\n",rootblkp,rootblkp * BLOCK_SIZE);
  133. // printf("%3d) FIRSTZONE: %d\n",__LINE__,FIRSTZONE(fs));
  134. dofwrite(goto_blk(fs->fp,rootblkp),root_block,sizeof(root_block));
  135. return fs;
  136. }
  137. /**
  138. * Initializes a new filesystem
  139. * @param fn - file name for new filesystem
  140. * @param chk - stop if filesystem is not clean
  141. * @return pointer to a minix_fs_dat structure
  142. */
  143. struct minix_fs_dat *open_fs(const char *fn,int chk) {
  144. struct minix_fs_dat *fs = domalloc(sizeof(struct minix_fs_dat),0);
  145. fs->fp = fopen(fn,"r+b");
  146. if (!fs->fp) die(fn);
  147. /*
  148. * Read super block
  149. */
  150. goto_blk(fs->fp,MINIX_SUPER_BLOCK);
  151. dofread(goto_blk(fs->fp,MINIX_SUPER_BLOCK),
  152. &(fs->msb),sizeof(struct minix_super_block));
  153. /*
  154. * Sanity checks ...
  155. */
  156. if (FSMAGIC(fs) != MINIX_SUPER_MAGIC && FSMAGIC(fs) != MINIX_SUPER_MAGIC2 &&
  157. FSMAGIC(fs) != MINIX2_SUPER_MAGIC && FSMAGIC(fs) != MINIX2_SUPER_MAGIC2) {
  158. fatalmsg("invalid magic fs-type %x",FSMAGIC(fs));
  159. }
  160. if (MINIX_VALID_FS != fs->msb.s_state) {
  161. if (chk) die("Filesystem in an unknown state");
  162. fprintf(stderr,"Warning: %s in an unknown state\n",fn);
  163. }
  164. /*
  165. * Read tables...
  166. */
  167. fs->inode_bmap = domalloc(IMAPS(fs) * BLOCK_SIZE,-1);
  168. fs->zone_bmap = domalloc(ZMAPS(fs) * BLOCK_SIZE,-1);
  169. fs->ino.v1 = domalloc(INODE_BUFFER_SIZE(fs),-1);
  170. dofread(goto_blk(fs->fp,MINIX_SUPER_BLOCK+1),
  171. fs->inode_bmap,IMAPS(fs) * BLOCK_SIZE);
  172. dofread(fs->fp,fs->zone_bmap,ZMAPS(fs) * BLOCK_SIZE);
  173. dofread(fs->fp,fs->ino.v1,INODE_BUFFER_SIZE(fs));
  174. return fs;
  175. }
  176. /**
  177. * Closes filesystem
  178. * @param fs - pointer to filesystem structure
  179. * @return NULL
  180. */
  181. struct minix_fs_dat *close_fs(struct minix_fs_dat *fs) {
  182. goto_blk(fs->fp,MINIX_SUPER_BLOCK);
  183. dofwrite(goto_blk(fs->fp,MINIX_SUPER_BLOCK),
  184. &fs->msb,sizeof(struct minix_super_block));
  185. dofwrite(goto_blk(fs->fp,MINIX_SUPER_BLOCK+1),
  186. fs->inode_bmap,IMAPS(fs) * BLOCK_SIZE);
  187. // printf("ZMAPSIZE=%d\n",ZMAPS(fs));
  188. // printf("%4d) FTELL=%d - %x\n",__LINE__,ftell(fs->fp),ftell(fs->fp));
  189. dofwrite(fs->fp,fs->zone_bmap,ZMAPS(fs) * BLOCK_SIZE);
  190. // printf("%4d) FTELL=%d - %x\n",__LINE__,ftell(fs->fp),ftell(fs->fp));
  191. if (VERSION_2(fs))
  192. dofwrite(fs->fp,fs->ino.v2,INODE_BUFFER_SIZE(fs));
  193. else
  194. dofwrite(fs->fp,fs->ino.v1,INODE_BUFFER_SIZE(fs));
  195. return 0;
  196. }