super.c 19.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  linux/fs/hpfs/super.c
 *
 *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
 *
 *  mounting, unmounting, error handling
 */

#include "hpfs_fn.h"
#include <linux/module.h>
#include <linux/parser.h>
#include <linux/init.h>
#include <linux/statfs.h>
14
#include <linux/magic.h>
A
Alexey Dobriyan 已提交
15
#include <linux/sched.h>
A
Akinobu Mita 已提交
16
#include <linux/bitmap.h>
17
#include <linux/slab.h>
L
Linus Torvalds 已提交
18 19 20

/* Mark the filesystem dirty, so that chkdsk checks it when os/2 booted */

21
static void mark_dirty(struct super_block *s, int remount)
L
Linus Torvalds 已提交
22
{
23
	if (hpfs_sb(s)->sb_chkdsk && (remount || !(s->s_flags & MS_RDONLY))) {
L
Linus Torvalds 已提交
24 25 26 27 28 29
		struct buffer_head *bh;
		struct hpfs_spare_block *sb;
		if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
			sb->dirty = 1;
			sb->old_wrote = 0;
			mark_buffer_dirty(bh);
30
			sync_dirty_buffer(bh);
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
			brelse(bh);
		}
	}
}

/* Mark the filesystem clean (mark it dirty for chkdsk if chkdsk==2 or if there
   were errors) */

static void unmark_dirty(struct super_block *s)
{
	struct buffer_head *bh;
	struct hpfs_spare_block *sb;
	if (s->s_flags & MS_RDONLY) return;
44
	sync_blockdev(s->s_bdev);
L
Linus Torvalds 已提交
45 46 47 48
	if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
		sb->dirty = hpfs_sb(s)->sb_chkdsk > 1 - hpfs_sb(s)->sb_was_error;
		sb->old_wrote = hpfs_sb(s)->sb_chkdsk >= 2 && !hpfs_sb(s)->sb_was_error;
		mark_buffer_dirty(bh);
49
		sync_dirty_buffer(bh);
L
Linus Torvalds 已提交
50 51 52 53 54
		brelse(bh);
	}
}

/* Filesystem error... */
55
static char err_buf[1024];
L
Linus Torvalds 已提交
56

57
void hpfs_error(struct super_block *s, const char *fmt, ...)
L
Linus Torvalds 已提交
58
{
59 60 61 62 63 64 65
	va_list args;

	va_start(args, fmt);
	vsnprintf(err_buf, sizeof(err_buf), fmt, args);
	va_end(args);

	printk("HPFS: filesystem error: %s", err_buf);
L
Linus Torvalds 已提交
66 67 68
	if (!hpfs_sb(s)->sb_was_error) {
		if (hpfs_sb(s)->sb_err == 2) {
			printk("; crashing the system because you wanted it\n");
69
			mark_dirty(s, 0);
L
Linus Torvalds 已提交
70 71 72 73 74
			panic("HPFS panic");
		} else if (hpfs_sb(s)->sb_err == 1) {
			if (s->s_flags & MS_RDONLY) printk("; already mounted read-only\n");
			else {
				printk("; remounting read-only\n");
75
				mark_dirty(s, 0);
L
Linus Torvalds 已提交
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
				s->s_flags |= MS_RDONLY;
			}
		} else if (s->s_flags & MS_RDONLY) printk("; going on - but anything won't be destroyed because it's read-only\n");
		else printk("; corrupted filesystem mounted read/write - your computer will explode within 20 seconds ... but you wanted it so!\n");
	} else printk("\n");
	hpfs_sb(s)->sb_was_error = 1;
}

/* 
 * A little trick to detect cycles in many hpfs structures and don't let the
 * kernel crash on corrupted filesystem. When first called, set c2 to 0.
 *
 * BTW. chkdsk doesn't detect cycles correctly. When I had 2 lost directories
 * nested each in other, chkdsk locked up happilly.
 */

int hpfs_stop_cycles(struct super_block *s, int key, int *c1, int *c2,
		char *msg)
{
	if (*c2 && *c1 == key) {
		hpfs_error(s, "cycle detected on key %08x in %s", key, msg);
		return 1;
	}
	(*c2)++;
	if (!((*c2 - 1) & *c2)) *c1 = key;
	return 0;
}

104
static void free_sbi(struct hpfs_sb_info *sbi)
L
Linus Torvalds 已提交
105
{
106 107 108 109
	kfree(sbi->sb_cp_table);
	kfree(sbi->sb_bmp_dir);
	kfree(sbi);
}
110

111 112 113 114 115 116 117
static void lazy_free_sbi(struct rcu_head *rcu)
{
	free_sbi(container_of(rcu, struct hpfs_sb_info, rcu));
}

static void hpfs_put_super(struct super_block *s)
{
118 119 120
	hpfs_lock(s);
	unmark_dirty(s);
	hpfs_unlock(s);
121
	call_rcu(&hpfs_sb(s)->rcu, lazy_free_sbi);
L
Linus Torvalds 已提交
122 123
}

M
Mikulas Patocka 已提交
124
static unsigned hpfs_count_one_bitmap(struct super_block *s, secno secno)
L
Linus Torvalds 已提交
125 126
{
	struct quad_buffer_head qbh;
A
Akinobu Mita 已提交
127 128 129
	unsigned long *bits;
	unsigned count;

130
	bits = hpfs_map_4sectors(s, secno, &qbh, 0);
A
Akinobu Mita 已提交
131
	if (!bits)
M
Mikulas Patocka 已提交
132
		return (unsigned)-1;
A
Akinobu Mita 已提交
133
	count = bitmap_weight(bits, 2048 * BITS_PER_BYTE);
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142
	hpfs_brelse4(&qbh);
	return count;
}

static unsigned count_bitmaps(struct super_block *s)
{
	unsigned n, count, n_bands;
	n_bands = (hpfs_sb(s)->sb_fs_size + 0x3fff) >> 14;
	count = 0;
143 144 145 146
	for (n = 0; n < COUNT_RD_AHEAD; n++) {
		hpfs_prefetch_bitmap(s, n);
	}
	for (n = 0; n < n_bands; n++) {
M
Mikulas Patocka 已提交
147
		unsigned c;
148
		hpfs_prefetch_bitmap(s, n + COUNT_RD_AHEAD);
M
Mikulas Patocka 已提交
149 150 151
		c = hpfs_count_one_bitmap(s, le32_to_cpu(hpfs_sb(s)->sb_bmp_dir[n]));
		if (c != (unsigned)-1)
			count += c;
152
	}
L
Linus Torvalds 已提交
153 154 155
	return count;
}

M
Mikulas Patocka 已提交
156 157 158 159 160 161 162 163 164 165 166 167
unsigned hpfs_get_free_dnodes(struct super_block *s)
{
	struct hpfs_sb_info *sbi = hpfs_sb(s);
	if (sbi->sb_n_free_dnodes == (unsigned)-1) {
		unsigned c = hpfs_count_one_bitmap(s, sbi->sb_dmap);
		if (c == (unsigned)-1)
			return 0;
		sbi->sb_n_free_dnodes = c;
	}
	return sbi->sb_n_free_dnodes;
}

168
static int hpfs_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
169
{
170
	struct super_block *s = dentry->d_sb;
L
Linus Torvalds 已提交
171
	struct hpfs_sb_info *sbi = hpfs_sb(s);
C
Coly Li 已提交
172
	u64 id = huge_encode_dev(s->s_bdev->bd_dev);
M
Mikulas Patocka 已提交
173

A
Arnd Bergmann 已提交
174
	hpfs_lock(s);
L
Linus Torvalds 已提交
175

M
Mikulas Patocka 已提交
176
	if (sbi->sb_n_free == (unsigned)-1)
L
Linus Torvalds 已提交
177
		sbi->sb_n_free = count_bitmaps(s);
M
Mikulas Patocka 已提交
178

L
Linus Torvalds 已提交
179 180 181 182 183 184
	buf->f_type = s->s_magic;
	buf->f_bsize = 512;
	buf->f_blocks = sbi->sb_fs_size;
	buf->f_bfree = sbi->sb_n_free;
	buf->f_bavail = sbi->sb_n_free;
	buf->f_files = sbi->sb_dirband_size / 4;
M
Mikulas Patocka 已提交
185
	buf->f_ffree = hpfs_get_free_dnodes(s);
C
Coly Li 已提交
186 187
	buf->f_fsid.val[0] = (u32)id;
	buf->f_fsid.val[1] = (u32)(id >> 32);
L
Linus Torvalds 已提交
188 189
	buf->f_namelen = 254;

A
Arnd Bergmann 已提交
190
	hpfs_unlock(s);
L
Linus Torvalds 已提交
191 192 193 194

	return 0;
}

195
static struct kmem_cache * hpfs_inode_cachep;
L
Linus Torvalds 已提交
196 197 198 199

static struct inode *hpfs_alloc_inode(struct super_block *sb)
{
	struct hpfs_inode_info *ei;
C
Christoph Lameter 已提交
200
	ei = (struct hpfs_inode_info *)kmem_cache_alloc(hpfs_inode_cachep, GFP_NOFS);
L
Linus Torvalds 已提交
201 202 203 204 205 206
	if (!ei)
		return NULL;
	ei->vfs_inode.i_version = 1;
	return &ei->vfs_inode;
}

N
Nick Piggin 已提交
207
static void hpfs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
208
{
N
Nick Piggin 已提交
209
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
210 211 212
	kmem_cache_free(hpfs_inode_cachep, hpfs_i(inode));
}

N
Nick Piggin 已提交
213 214 215 216 217
static void hpfs_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, hpfs_i_callback);
}

218
static void init_once(void *foo)
L
Linus Torvalds 已提交
219 220 221
{
	struct hpfs_inode_info *ei = (struct hpfs_inode_info *) foo;

C
Christoph Lameter 已提交
222
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
223
}
224

L
Linus Torvalds 已提交
225 226 227 228
static int init_inodecache(void)
{
	hpfs_inode_cachep = kmem_cache_create("hpfs_inode_cache",
					     sizeof(struct hpfs_inode_info),
229 230
					     0, (SLAB_RECLAIM_ACCOUNT|
						SLAB_MEM_SPREAD),
231
					     init_once);
L
Linus Torvalds 已提交
232 233 234 235 236 237 238
	if (hpfs_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
239 240 241 242 243
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
244
	kmem_cache_destroy(hpfs_inode_cachep);
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
}

/*
 * A tiny parser for option strings, stolen from dosfs.
 * Stolen again from read-only hpfs.
 * And updated for table-driven option parsing.
 */

enum {
	Opt_help, Opt_uid, Opt_gid, Opt_umask, Opt_case_lower, Opt_case_asis,
	Opt_check_none, Opt_check_normal, Opt_check_strict,
	Opt_err_cont, Opt_err_ro, Opt_err_panic,
	Opt_eas_no, Opt_eas_ro, Opt_eas_rw,
	Opt_chkdsk_no, Opt_chkdsk_errors, Opt_chkdsk_always,
	Opt_timeshift, Opt_err,
};

262
static const match_table_t tokens = {
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	{Opt_help, "help"},
	{Opt_uid, "uid=%u"},
	{Opt_gid, "gid=%u"},
	{Opt_umask, "umask=%o"},
	{Opt_case_lower, "case=lower"},
	{Opt_case_asis, "case=asis"},
	{Opt_check_none, "check=none"},
	{Opt_check_normal, "check=normal"},
	{Opt_check_strict, "check=strict"},
	{Opt_err_cont, "errors=continue"},
	{Opt_err_ro, "errors=remount-ro"},
	{Opt_err_panic, "errors=panic"},
	{Opt_eas_no, "eas=no"},
	{Opt_eas_ro, "eas=ro"},
	{Opt_eas_rw, "eas=rw"},
	{Opt_chkdsk_no, "chkdsk=no"},
	{Opt_chkdsk_errors, "chkdsk=errors"},
	{Opt_chkdsk_always, "chkdsk=always"},
	{Opt_timeshift, "timeshift=%d"},
	{Opt_err, NULL},
};

285
static int parse_opts(char *opts, kuid_t *uid, kgid_t *gid, umode_t *umask,
286
		      int *lowercase, int *eas, int *chk, int *errs,
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		      int *chkdsk, int *timeshift)
{
	char *p;
	int option;

	if (!opts)
		return 1;

	/*printk("Parsing opts: '%s'\n",opts);*/

	while ((p = strsep(&opts, ",")) != NULL) {
		substring_t args[MAX_OPT_ARGS];
		int token;
		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case Opt_help:
			return 2;
		case Opt_uid:
			if (match_int(args, &option))
				return 0;
310 311 312
			*uid = make_kuid(current_user_ns(), option);
			if (!uid_valid(*uid))
				return 0;
L
Linus Torvalds 已提交
313 314 315 316
			break;
		case Opt_gid:
			if (match_int(args, &option))
				return 0;
317 318 319
			*gid = make_kgid(current_user_ns(), option);
			if (!gid_valid(*gid))
				return 0;
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
			break;
		case Opt_umask:
			if (match_octal(args, &option))
				return 0;
			*umask = option;
			break;
		case Opt_case_lower:
			*lowercase = 1;
			break;
		case Opt_case_asis:
			*lowercase = 0;
			break;
		case Opt_check_none:
			*chk = 0;
			break;
		case Opt_check_normal:
			*chk = 1;
			break;
		case Opt_check_strict:
			*chk = 2;
			break;
		case Opt_err_cont:
			*errs = 0;
			break;
		case Opt_err_ro:
			*errs = 1;
			break;
		case Opt_err_panic:
			*errs = 2;
			break;
		case Opt_eas_no:
			*eas = 0;
			break;
		case Opt_eas_ro:
			*eas = 1;
			break;
		case Opt_eas_rw:
			*eas = 2;
			break;
		case Opt_chkdsk_no:
			*chkdsk = 0;
			break;
		case Opt_chkdsk_errors:
			*chkdsk = 1;
			break;
		case Opt_chkdsk_always:
			*chkdsk = 2;
			break;
		case Opt_timeshift:
		{
			int m = 1;
			char *rhs = args[0].from;
			if (!rhs || !*rhs)
				return 0;
			if (*rhs == '-') m = -1;
			if (*rhs == '+' || *rhs == '-') rhs++;
			*timeshift = simple_strtoul(rhs, &rhs, 0) * m;
			if (*rhs)
				return 0;
			break;
		}
		default:
			return 0;
		}
	}
	return 1;
}

static inline void hpfs_help(void)
{
	printk("\n\
HPFS filesystem options:\n\
      help              do not mount and display this text\n\
      uid=xxx           set uid of files that don't have uid specified in eas\n\
      gid=xxx           set gid of files that don't have gid specified in eas\n\
      umask=xxx         set mode of files that don't have mode specified in eas\n\
      case=lower        lowercase all files\n\
      case=asis         do not lowercase files (default)\n\
      check=none        no fs checks - kernel may crash on corrupted filesystem\n\
      check=normal      do some checks - it should not crash (default)\n\
      check=strict      do extra time-consuming checks, used for debugging\n\
      errors=continue   continue on errors\n\
      errors=remount-ro remount read-only if errors found (default)\n\
      errors=panic      panic on errors\n\
      chkdsk=no         do not mark fs for chkdsking even if there were errors\n\
      chkdsk=errors     mark fs dirty if errors found (default)\n\
      chkdsk=always     always mark fs dirty - used for debugging\n\
      eas=no            ignore extended attributes\n\
      eas=ro            read but do not write extended attributes\n\
      eas=rw            r/w eas => enables chmod, chown, mknod, ln -s (default)\n\
      timeshift=nnn	add nnn seconds to file times\n\
\n");
}

static int hpfs_remount_fs(struct super_block *s, int *flags, char *data)
{
416 417
	kuid_t uid;
	kgid_t gid;
L
Linus Torvalds 已提交
418
	umode_t umask;
419
	int lowercase, eas, chk, errs, chkdsk, timeshift;
L
Linus Torvalds 已提交
420 421
	int o;
	struct hpfs_sb_info *sbi = hpfs_sb(s);
M
Miklos Szeredi 已提交
422
	char *new_opts = kstrdup(data, GFP_KERNEL);
L
Linus Torvalds 已提交
423
	
424 425
	sync_filesystem(s);

L
Linus Torvalds 已提交
426 427
	*flags |= MS_NOATIME;
	
A
Arnd Bergmann 已提交
428
	hpfs_lock(s);
L
Linus Torvalds 已提交
429 430
	uid = sbi->sb_uid; gid = sbi->sb_gid;
	umask = 0777 & ~sbi->sb_mode;
431
	lowercase = sbi->sb_lowercase;
L
Linus Torvalds 已提交
432 433 434
	eas = sbi->sb_eas; chk = sbi->sb_chk; chkdsk = sbi->sb_chkdsk;
	errs = sbi->sb_err; timeshift = sbi->sb_timeshift;

435
	if (!(o = parse_opts(data, &uid, &gid, &umask, &lowercase,
L
Linus Torvalds 已提交
436 437
	    &eas, &chk, &errs, &chkdsk, &timeshift))) {
		printk("HPFS: bad mount options.\n");
M
Miklos Szeredi 已提交
438
		goto out_err;
L
Linus Torvalds 已提交
439 440 441
	}
	if (o == 2) {
		hpfs_help();
M
Miklos Szeredi 已提交
442
		goto out_err;
L
Linus Torvalds 已提交
443 444 445
	}
	if (timeshift != sbi->sb_timeshift) {
		printk("HPFS: timeshift can't be changed using remount.\n");
M
Miklos Szeredi 已提交
446
		goto out_err;
L
Linus Torvalds 已提交
447 448 449 450 451 452
	}

	unmark_dirty(s);

	sbi->sb_uid = uid; sbi->sb_gid = gid;
	sbi->sb_mode = 0777 & ~umask;
453
	sbi->sb_lowercase = lowercase;
L
Linus Torvalds 已提交
454 455 456
	sbi->sb_eas = eas; sbi->sb_chk = chk; sbi->sb_chkdsk = chkdsk;
	sbi->sb_err = errs; sbi->sb_timeshift = timeshift;

457
	if (!(*flags & MS_RDONLY)) mark_dirty(s, 1);
L
Linus Torvalds 已提交
458

459
	replace_mount_options(s, new_opts);
M
Miklos Szeredi 已提交
460

A
Arnd Bergmann 已提交
461
	hpfs_unlock(s);
L
Linus Torvalds 已提交
462
	return 0;
M
Miklos Szeredi 已提交
463 464

out_err:
A
Arnd Bergmann 已提交
465
	hpfs_unlock(s);
M
Miklos Szeredi 已提交
466 467
	kfree(new_opts);
	return -EINVAL;
L
Linus Torvalds 已提交
468 469 470 471
}

/* Super operations */

472
static const struct super_operations hpfs_sops =
L
Linus Torvalds 已提交
473 474 475
{
	.alloc_inode	= hpfs_alloc_inode,
	.destroy_inode	= hpfs_destroy_inode,
A
Al Viro 已提交
476
	.evict_inode	= hpfs_evict_inode,
L
Linus Torvalds 已提交
477 478 479
	.put_super	= hpfs_put_super,
	.statfs		= hpfs_statfs,
	.remount_fs	= hpfs_remount_fs,
M
Miklos Szeredi 已提交
480
	.show_options	= generic_show_options,
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488 489 490 491
};

static int hpfs_fill_super(struct super_block *s, void *options, int silent)
{
	struct buffer_head *bh0, *bh1, *bh2;
	struct hpfs_boot_block *bootblock;
	struct hpfs_super_block *superblock;
	struct hpfs_spare_block *spareblock;
	struct hpfs_sb_info *sbi;
	struct inode *root;

492 493
	kuid_t uid;
	kgid_t gid;
L
Linus Torvalds 已提交
494
	umode_t umask;
495
	int lowercase, eas, chk, errs, chkdsk, timeshift;
L
Linus Torvalds 已提交
496 497 498 499 500 501 502

	dnode_secno root_dno;
	struct hpfs_dirent *de = NULL;
	struct quad_buffer_head qbh;

	int o;

M
Miklos Szeredi 已提交
503 504
	save_mount_options(s, options);

505
	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
506
	if (!sbi) {
L
Linus Torvalds 已提交
507
		return -ENOMEM;
508
	}
L
Linus Torvalds 已提交
509 510
	s->s_fs_info = sbi;

511 512 513
	mutex_init(&sbi->hpfs_mutex);
	hpfs_lock(s);

514 515
	uid = current_uid();
	gid = current_gid();
A
Al Viro 已提交
516
	umask = current_umask();
L
Linus Torvalds 已提交
517 518 519 520 521 522 523
	lowercase = 0;
	eas = 2;
	chk = 1;
	errs = 1;
	chkdsk = 1;
	timeshift = 0;

524
	if (!(o = parse_opts(options, &uid, &gid, &umask, &lowercase,
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	    &eas, &chk, &errs, &chkdsk, &timeshift))) {
		printk("HPFS: bad mount options.\n");
		goto bail0;
	}
	if (o==2) {
		hpfs_help();
		goto bail0;
	}

	/*sbi->sb_mounting = 1;*/
	sb_set_blocksize(s, 512);
	sbi->sb_fs_size = -1;
	if (!(bootblock = hpfs_map_sector(s, 0, &bh0, 0))) goto bail1;
	if (!(superblock = hpfs_map_sector(s, 16, &bh1, 1))) goto bail2;
	if (!(spareblock = hpfs_map_sector(s, 17, &bh2, 0))) goto bail3;

	/* Check magics */
542 543 544
	if (/*le16_to_cpu(bootblock->magic) != BB_MAGIC
	    ||*/ le32_to_cpu(superblock->magic) != SB_MAGIC
	    || le32_to_cpu(spareblock->magic) != SP_MAGIC) {
L
Linus Torvalds 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
		if (!silent) printk("HPFS: Bad magic ... probably not HPFS\n");
		goto bail4;
	}

	/* Check version */
	if (!(s->s_flags & MS_RDONLY) &&
	      superblock->funcversion != 2 && superblock->funcversion != 3) {
		printk("HPFS: Bad version %d,%d. Mount readonly to go around\n",
			(int)superblock->version, (int)superblock->funcversion);
		printk("HPFS: please try recent version of HPFS driver at http://artax.karlin.mff.cuni.cz/~mikulas/vyplody/hpfs/index-e.cgi and if it still can't understand this format, contact author - mikulas@artax.karlin.mff.cuni.cz\n");
		goto bail4;
	}

	s->s_flags |= MS_NOATIME;

	/* Fill superblock stuff */
	s->s_magic = HPFS_SUPER_MAGIC;
	s->s_op = &hpfs_sops;
A
Al Viro 已提交
563
	s->s_d_op = &hpfs_dentry_operations;
L
Linus Torvalds 已提交
564

565 566 567 568 569 570
	sbi->sb_root = le32_to_cpu(superblock->root);
	sbi->sb_fs_size = le32_to_cpu(superblock->n_sectors);
	sbi->sb_bitmaps = le32_to_cpu(superblock->bitmaps);
	sbi->sb_dirband_start = le32_to_cpu(superblock->dir_band_start);
	sbi->sb_dirband_size = le32_to_cpu(superblock->n_dir_band);
	sbi->sb_dmap = le32_to_cpu(superblock->dir_band_bitmap);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	sbi->sb_uid = uid;
	sbi->sb_gid = gid;
	sbi->sb_mode = 0777 & ~umask;
	sbi->sb_n_free = -1;
	sbi->sb_n_free_dnodes = -1;
	sbi->sb_lowercase = lowercase;
	sbi->sb_eas = eas;
	sbi->sb_chk = chk;
	sbi->sb_chkdsk = chkdsk;
	sbi->sb_err = errs;
	sbi->sb_timeshift = timeshift;
	sbi->sb_was_error = 0;
	sbi->sb_cp_table = NULL;
	sbi->sb_c_bitmap = -1;
	sbi->sb_max_fwd_alloc = 0xffffff;
M
Mikulas Patocka 已提交
586 587 588 589 590 591 592

	if (sbi->sb_fs_size >= 0x80000000) {
		hpfs_error(s, "invalid size in superblock: %08x",
			(unsigned)sbi->sb_fs_size);
		goto bail4;
	}

L
Linus Torvalds 已提交
593
	/* Load bitmap directory */
594
	if (!(sbi->sb_bmp_dir = hpfs_load_bitmap_directory(s, le32_to_cpu(superblock->bitmaps))))
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
		goto bail4;
	
	/* Check for general fs errors*/
	if (spareblock->dirty && !spareblock->old_wrote) {
		if (errs == 2) {
			printk("HPFS: Improperly stopped, not mounted\n");
			goto bail4;
		}
		hpfs_error(s, "improperly stopped");
	}

	if (!(s->s_flags & MS_RDONLY)) {
		spareblock->dirty = 1;
		spareblock->old_wrote = 0;
		mark_buffer_dirty(bh2);
	}

A
Al Viro 已提交
612
	if (spareblock->hotfixes_used || spareblock->n_spares_used) {
L
Linus Torvalds 已提交
613 614
		if (errs >= 2) {
			printk("HPFS: Hotfixes not supported here, try chkdsk\n");
615
			mark_dirty(s, 0);
L
Linus Torvalds 已提交
616 617 618 619 620 621
			goto bail4;
		}
		hpfs_error(s, "hotfixes not supported here, try chkdsk");
		if (errs == 0) printk("HPFS: Proceeding, but your filesystem will be probably corrupted by this driver...\n");
		else printk("HPFS: This driver may read bad files or crash when operating on disk with hotfixes.\n");
	}
622
	if (le32_to_cpu(spareblock->n_dnode_spares) != le32_to_cpu(spareblock->n_dnode_spares_free)) {
L
Linus Torvalds 已提交
623 624
		if (errs >= 2) {
			printk("HPFS: Spare dnodes used, try chkdsk\n");
625
			mark_dirty(s, 0);
L
Linus Torvalds 已提交
626 627 628 629 630 631 632
			goto bail4;
		}
		hpfs_error(s, "warning: spare dnodes used, try chkdsk");
		if (errs == 0) printk("HPFS: Proceeding, but your filesystem could be corrupted if you delete files or directories\n");
	}
	if (chk) {
		unsigned a;
633 634
		if (le32_to_cpu(superblock->dir_band_end) - le32_to_cpu(superblock->dir_band_start) + 1 != le32_to_cpu(superblock->n_dir_band) ||
		    le32_to_cpu(superblock->dir_band_end) < le32_to_cpu(superblock->dir_band_start) || le32_to_cpu(superblock->n_dir_band) > 0x4000) {
L
Linus Torvalds 已提交
635
			hpfs_error(s, "dir band size mismatch: dir_band_start==%08x, dir_band_end==%08x, n_dir_band==%08x",
636
				le32_to_cpu(superblock->dir_band_start), le32_to_cpu(superblock->dir_band_end), le32_to_cpu(superblock->n_dir_band));
L
Linus Torvalds 已提交
637 638 639 640
			goto bail4;
		}
		a = sbi->sb_dirband_size;
		sbi->sb_dirband_size = 0;
641 642 643
		if (hpfs_chk_sectors(s, le32_to_cpu(superblock->dir_band_start), le32_to_cpu(superblock->n_dir_band), "dir_band") ||
		    hpfs_chk_sectors(s, le32_to_cpu(superblock->dir_band_bitmap), 4, "dir_band_bitmap") ||
		    hpfs_chk_sectors(s, le32_to_cpu(superblock->bitmaps), 4, "bitmaps")) {
644
			mark_dirty(s, 0);
L
Linus Torvalds 已提交
645 646 647 648 649 650
			goto bail4;
		}
		sbi->sb_dirband_size = a;
	} else printk("HPFS: You really don't want any checks? You are crazy...\n");

	/* Load code page table */
651 652
	if (le32_to_cpu(spareblock->n_code_pages))
		if (!(sbi->sb_cp_table = hpfs_load_code_page(s, le32_to_cpu(spareblock->code_page_dir))))
L
Linus Torvalds 已提交
653 654 655 656 657 658 659 660 661 662 663 664
			printk("HPFS: Warning: code page support is disabled\n");

	brelse(bh2);
	brelse(bh1);
	brelse(bh0);

	root = iget_locked(s, sbi->sb_root);
	if (!root)
		goto bail0;
	hpfs_init_inode(root);
	hpfs_read_inode(root);
	unlock_new_inode(root);
665 666
	s->s_root = d_make_root(root);
	if (!s->s_root)
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678
		goto bail0;

	/*
	 * find the root directory's . pointer & finish filling in the inode
	 */

	root_dno = hpfs_fnode_dno(s, sbi->sb_root);
	if (root_dno)
		de = map_dirent(root, root_dno, "\001\001", 2, NULL, &qbh);
	if (!de)
		hpfs_error(s, "unable to find root dir");
	else {
679
		root->i_atime.tv_sec = local_to_gmt(s, le32_to_cpu(de->read_date));
L
Linus Torvalds 已提交
680
		root->i_atime.tv_nsec = 0;
681
		root->i_mtime.tv_sec = local_to_gmt(s, le32_to_cpu(de->write_date));
L
Linus Torvalds 已提交
682
		root->i_mtime.tv_nsec = 0;
683
		root->i_ctime.tv_sec = local_to_gmt(s, le32_to_cpu(de->creation_date));
L
Linus Torvalds 已提交
684
		root->i_ctime.tv_nsec = 0;
A
Al Viro 已提交
685
		hpfs_i(root)->i_ea_size = le32_to_cpu(de->ea_size);
L
Linus Torvalds 已提交
686 687 688 689 690 691 692
		hpfs_i(root)->i_parent_dir = root->i_ino;
		if (root->i_size == -1)
			root->i_size = 2048;
		if (root->i_blocks == -1)
			root->i_blocks = 5;
		hpfs_brelse4(&qbh);
	}
693
	hpfs_unlock(s);
L
Linus Torvalds 已提交
694 695 696 697 698 699 700
	return 0;

bail4:	brelse(bh2);
bail3:	brelse(bh1);
bail2:	brelse(bh0);
bail1:
bail0:
701
	hpfs_unlock(s);
702
	free_sbi(sbi);
L
Linus Torvalds 已提交
703 704 705
	return -EINVAL;
}

A
Al Viro 已提交
706 707
static struct dentry *hpfs_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
708
{
A
Al Viro 已提交
709
	return mount_bdev(fs_type, flags, dev_name, data, hpfs_fill_super);
L
Linus Torvalds 已提交
710 711 712 713 714
}

static struct file_system_type hpfs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "hpfs",
A
Al Viro 已提交
715
	.mount		= hpfs_mount,
L
Linus Torvalds 已提交
716 717 718
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};
719
MODULE_ALIAS_FS("hpfs");
L
Linus Torvalds 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

static int __init init_hpfs_fs(void)
{
	int err = init_inodecache();
	if (err)
		goto out1;
	err = register_filesystem(&hpfs_fs_type);
	if (err)
		goto out;
	return 0;
out:
	destroy_inodecache();
out1:
	return err;
}

static void __exit exit_hpfs_fs(void)
{
	unregister_filesystem(&hpfs_fs_type);
	destroy_inodecache();
}

module_init(init_hpfs_fs)
module_exit(exit_hpfs_fs)
MODULE_LICENSE("GPL");