Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | /* * linux/fs/adfs/super.c * * Copyright (C) 1997 Russell King */ #include <linux/module.h> #include <linux/errno.h> #include <linux/fs.h> #include <linux/adfs_fs.h> #include <linux/malloc.h> #include <linux/sched.h> #include <linux/stat.h> #include <linux/string.h> #include <linux/locks.h> #include <linux/init.h> #include <asm/bitops.h> #include <asm/uaccess.h> #include <asm/system.h> #include <stdarg.h> static void adfs_put_super (struct super_block *sb); static int adfs_statfs (struct super_block *sb, struct statfs *buf, int bufsiz); void adfs_read_inode (struct inode *inode); void adfs_error (struct super_block *sb, const char *function, const char *fmt, ...) { char error_buf[128]; va_list args; va_start (args, fmt); vsprintf (error_buf, fmt, args); va_end (args); printk (KERN_CRIT "ADFS-fs error (device %s)%s%s: %s\n", kdevname (sb->s_dev), function ? ": " : "", function ? function : "", error_buf); } unsigned char adfs_calccrosscheck (struct super_block *sb, char *map) { unsigned int v0, v1, v2, v3; int i; v0 = v1 = v2 = v3 = 0; for (i = sb->s_blocksize - 4; i; i -= 4) { v0 += map[i] + (v3 >> 8); v3 &= 0xff; v1 += map[i + 1] + (v0 >> 8); v0 &= 0xff; v2 += map[i + 2] + (v1 >> 8); v1 &= 0xff; v3 += map[i + 3] + (v2 >> 8); v2 &= 0xff; } v0 += v3 >> 8; v1 += map[1] + (v0 >> 8); v2 += map[2] + (v1 >> 8); v3 += map[3] + (v2 >> 8); return v0 ^ v1 ^ v2 ^ v3; } static int adfs_checkmap (struct super_block *sb) { unsigned char crosscheck = 0, zonecheck = 1; int i; for (i = 0; i < sb->u.adfs_sb.s_map_size; i++) { char *map; map = sb->u.adfs_sb.s_map[i]->b_data; if (adfs_calccrosscheck (sb, map) != map[0]) { adfs_error (sb, "adfs_checkmap", "zone %d fails zonecheck", i); zonecheck = 0; } crosscheck ^= map[3]; } if (crosscheck != 0xff) adfs_error (sb, "adfs_checkmap", "crosscheck != 0xff"); return crosscheck == 0xff && zonecheck; } static struct super_operations adfs_sops = { adfs_read_inode, NULL, NULL, NULL, NULL, adfs_put_super, NULL, adfs_statfs, NULL }; static void adfs_put_super (struct super_block *sb) { int i; for (i = 0; i < sb->u.adfs_sb.s_map_size; i++) brelse (sb->u.adfs_sb.s_map[i]); kfree (sb->u.adfs_sb.s_map); brelse (sb->u.adfs_sb.s_sbh); MOD_DEC_USE_COUNT; } struct super_block *adfs_read_super (struct super_block *sb, void *data, int silent) { struct adfs_discrecord *dr; struct buffer_head *bh; unsigned char *b_data; kdev_t dev = sb->s_dev; int i, j; MOD_INC_USE_COUNT; lock_super (sb); set_blocksize (dev, BLOCK_SIZE); if (!(bh = bread (dev, ADFS_DISCRECORD / BLOCK_SIZE, BLOCK_SIZE))) { unlock_super (sb); adfs_error (sb, NULL, "unable to read superblock"); MOD_DEC_USE_COUNT; return NULL; } b_data = bh->b_data + (ADFS_DISCRECORD % BLOCK_SIZE); if (adfs_checkbblk (b_data)) { if (!silent) printk ("VFS: Can't find an adfs filesystem on dev " "%s.\n", kdevname(dev)); failed_mount: unlock_super (sb); if (bh) brelse (bh); MOD_DEC_USE_COUNT; return NULL; } dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET); sb->s_blocksize_bits = dr->log2secsize; sb->s_blocksize = 1 << sb->s_blocksize_bits; if (sb->s_blocksize != BLOCK_SIZE && (sb->s_blocksize == 512 || sb->s_blocksize == 1024 || sb->s_blocksize == 2048 || sb->s_blocksize == 4096)) { brelse (bh); set_blocksize (dev, sb->s_blocksize); bh = bread (dev, ADFS_DISCRECORD / sb->s_blocksize, sb->s_blocksize); if (!bh) { adfs_error (sb, NULL, "couldn't read superblock on " "2nd try."); goto failed_mount; } b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize); if (adfs_checkbblk (b_data)) { adfs_error (sb, NULL, "disc record mismatch, very weird!"); goto failed_mount; } dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET); } if (sb->s_blocksize != bh->b_size) { if (!silent) printk (KERN_ERR "VFS: Unsupported blocksize on dev " "%s.\n", kdevname (dev)); goto failed_mount; } /* blocksize on this device should now be set to the adfs log2secsize */ sb->u.adfs_sb.s_sbh = bh; sb->u.adfs_sb.s_dr = dr; /* s_zone_size = size of 1 zone (1 sector) * bits_in_byte - zone_spare => * number of map bits in a zone */ sb->u.adfs_sb.s_zone_size = (8 << dr->log2secsize) - dr->zone_spare; /* s_ids_per_zone = bit size of 1 zone / min. length of fragment block => * number of ids in one zone */ sb->u.adfs_sb.s_ids_per_zone = sb->u.adfs_sb.s_zone_size / (dr->idlen + 1); /* s_idlen = length of 1 id */ sb->u.adfs_sb.s_idlen = dr->idlen; /* map size (in sectors) = number of zones */ sb->u.adfs_sb.s_map_size = dr->nzones; /* zonesize = size of sector - zonespare */ sb->u.adfs_sb.s_zonesize = (sb->s_blocksize << 3) - dr->zone_spare; /* map start (in sectors) = start of zone (number of zones) / 2 */ sb->u.adfs_sb.s_map_block = (dr->nzones >> 1) * sb->u.adfs_sb.s_zone_size - ((dr->nzones > 1) ? 8 * ADFS_DR_SIZE : 0); /* (signed) number of bits to shift left a map address to a sector address */ sb->u.adfs_sb.s_map2blk = dr->log2bpmb - dr->log2secsize; if (sb->u.adfs_sb.s_map2blk >= 0) sb->u.adfs_sb.s_map_block <<= sb->u.adfs_sb.s_map2blk; else sb->u.adfs_sb.s_map_block >>= -sb->u.adfs_sb.s_map2blk; printk (KERN_DEBUG "ADFS: zone size %d, IDs per zone %d, map address %X size %d sectors\n", sb->u.adfs_sb.s_zone_size, sb->u.adfs_sb.s_ids_per_zone, sb->u.adfs_sb.s_map_block, sb->u.adfs_sb.s_map_size); printk (KERN_DEBUG "ADFS: sector size %d, map bit size %d\n", 1 << dr->log2secsize, 1 << dr->log2bpmb); sb->s_magic = ADFS_SUPER_MAGIC; sb->s_flags |= MS_RDONLY; /* we don't support writing yet */ sb->u.adfs_sb.s_map = kmalloc (sb->u.adfs_sb.s_map_size * sizeof (struct buffer_head *), GFP_KERNEL); if (sb->u.adfs_sb.s_map == NULL) { adfs_error (sb, NULL, "not enough memory"); goto failed_mount; } for (i = 0; i < sb->u.adfs_sb.s_map_size; i++) { sb->u.adfs_sb.s_map[i] = bread (dev, sb->u.adfs_sb.s_map_block + i, sb->s_blocksize); if (!sb->u.adfs_sb.s_map[i]) { for (j = 0; j < i; j++) brelse (sb->u.adfs_sb.s_map[j]); kfree (sb->u.adfs_sb.s_map); adfs_error (sb, NULL, "unable to read map"); goto failed_mount; } } if (!adfs_checkmap (sb)) { for (i = 0; i < sb->u.adfs_sb.s_map_size; i++) brelse (sb->u.adfs_sb.s_map[i]); adfs_error (sb, NULL, "map corrupted"); goto failed_mount; } dr = (struct adfs_discrecord *)(sb->u.adfs_sb.s_map[0]->b_data + 4); unlock_super (sb); /* * set up enough so that it can read an inode */ sb->s_op = &adfs_sops; sb->u.adfs_sb.s_root = adfs_inode_generate (dr->root, 0); sb->s_root = d_alloc_root(iget(sb, sb->u.adfs_sb.s_root), NULL); if (!sb->s_root) { sb->s_dev = 0; for (i = 0; i < sb->u.adfs_sb.s_map_size; i++) brelse (sb->u.adfs_sb.s_map[i]); brelse (bh); adfs_error (sb, NULL, "get root inode failed\n"); MOD_DEC_USE_COUNT; return NULL; } return sb; } static int adfs_statfs (struct super_block *sb, struct statfs *buf, int bufsiz) { struct statfs tmp; const unsigned int nidlen = sb->u.adfs_sb.s_idlen + 1; tmp.f_type = ADFS_SUPER_MAGIC; tmp.f_bsize = sb->s_blocksize; tmp.f_blocks = (sb->u.adfs_sb.s_dr->disc_size) >> (sb->s_blocksize_bits); tmp.f_files = tmp.f_blocks >> nidlen; { unsigned int i, j = 0; const unsigned mask = (1 << (nidlen - 1)) - 1; for (i = 0; i < sb->u.adfs_sb.s_map_size; i++) { const char *map = sb->u.adfs_sb.s_map[i]->b_data; unsigned freelink, mapindex = 24; j -= nidlen; do { unsigned char k, l, m; unsigned off = (mapindex - nidlen) >> 3; unsigned rem; const unsigned boff = mapindex & 7; /* get next freelink */ k = map[off++]; l = map[off++]; m = map[off++]; freelink = (m << 16) | (l << 8) | k; rem = freelink >> (boff + nidlen - 1); freelink = (freelink >> boff) & mask; mapindex += freelink; /* find its length and add it to running total */ while (rem == 0) { j += 8; rem = map[off++]; } if ((rem & 0xff) == 0) j+=8, rem>>=8; if ((rem & 0xf) == 0) j+=4, rem>>=4; if ((rem & 0x3) == 0) j+=2, rem>>=2; if ((rem & 0x1) == 0) j+=1; j += nidlen - boff; if (freelink <= nidlen) break; } while (mapindex < 8 * sb->s_blocksize); if (mapindex > 8 * sb->s_blocksize) adfs_error (sb, NULL, "oversized free fragment\n"); else if (freelink) adfs_error (sb, NULL, "undersized free fragment\n"); } tmp.f_bfree = tmp.f_bavail = j << (sb->u.adfs_sb.s_dr->log2bpmb - sb->s_blocksize_bits); } tmp.f_ffree = tmp.f_bfree >> nidlen; tmp.f_namelen = ADFS_NAME_LEN; return copy_to_user (buf, &tmp, bufsiz) ? -EFAULT : 0; } static struct file_system_type adfs_fs_type = { "adfs", FS_REQUIRES_DEV, adfs_read_super, NULL }; __initfunc(int init_adfs_fs (void)) { return register_filesystem (&adfs_fs_type); } #ifdef MODULE int init_module (void) { return init_adfs_fs(); } void cleanup_module (void) { unregister_filesystem (&adfs_fs_type); } #endif |