2 * linux/fs/hpfs/super.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * mouning, unmounting, error handling
9 #include <linux/string.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
14 /* Mark the filesystem dirty, so that chkdsk checks it when os/2 booted */
16 static void mark_dirty(struct super_block
*s
)
18 if (s
->s_hpfs_chkdsk
&& !(s
->s_flags
& MS_RDONLY
)) {
19 struct buffer_head
*bh
;
20 struct hpfs_spare_block
*sb
;
21 if ((sb
= hpfs_map_sector(s
, 17, &bh
, 0))) {
24 mark_buffer_dirty(bh
);
30 /* Mark the filesystem clean (mark it dirty for chkdsk if chkdsk==2 or if there
33 static void unmark_dirty(struct super_block
*s
)
35 struct buffer_head
*bh
;
36 struct hpfs_spare_block
*sb
;
37 if (s
->s_flags
& MS_RDONLY
) return;
38 if ((sb
= hpfs_map_sector(s
, 17, &bh
, 0))) {
39 sb
->dirty
= s
->s_hpfs_chkdsk
> 1 - s
->s_hpfs_was_error
;
40 sb
->old_wrote
= s
->s_hpfs_chkdsk
>= 2 && !s
->s_hpfs_was_error
;
41 mark_buffer_dirty(bh
);
46 /* Filesystem error... */
48 #define ERR_BUF_SIZE 1024
50 void hpfs_error(struct super_block
*s
, char *m
,...)
55 if (!(buf
= kmalloc(ERR_BUF_SIZE
, GFP_KERNEL
)))
56 printk("HPFS: No memory for error message '%s'\n",m
);
57 else if (vsprintf(buf
, m
, l
) >= ERR_BUF_SIZE
)
58 printk("HPFS: Grrrr... Kernel memory corrupted ... going on, but it'll crash very soon :-(\n");
59 printk("HPFS: filesystem error: ");
60 if (buf
) printk("%s", buf
);
61 else printk("%s\n",m
);
62 if (!s
->s_hpfs_was_error
) {
63 if (s
->s_hpfs_err
== 2) {
64 printk("; crashing the system because you wanted it\n");
67 } else if (s
->s_hpfs_err
== 1) {
68 if (s
->s_flags
& MS_RDONLY
) printk("; already mounted read-only\n");
70 printk("; remounting read-only\n");
72 s
->s_flags
|= MS_RDONLY
;
74 } else if (s
->s_flags
& MS_RDONLY
) printk("; going on - but anything won't be destroyed because it's read-only\n");
75 else printk("; corrupted filesystem mounted read/write - your computer will explode within 20 seconds ... but you wanted it so!\n");
78 s
->s_hpfs_was_error
= 1;
82 * A little trick to detect cycles in many hpfs structures and don't let the
83 * kernel crash on corrupted filesystem. When first called, set c2 to 0.
85 * BTW. chkdsk doesn't detect cycles correctly. When I had 2 lost directories
86 * nested each in other, chkdsk locked up happilly.
89 int hpfs_stop_cycles(struct super_block
*s
, int key
, int *c1
, int *c2
,
92 if (*c2
&& *c1
== key
) {
93 hpfs_error(s
, "cycle detected on key %08x in %s", key
, msg
);
97 if (!((*c2
- 1) & *c2
)) *c1
= key
;
101 void hpfs_put_super(struct super_block
*s
)
103 if (s
->s_hpfs_cp_table
) kfree(s
->s_hpfs_cp_table
);
104 if (s
->s_hpfs_bmp_dir
) kfree(s
->s_hpfs_bmp_dir
);
108 unsigned hpfs_count_one_bitmap(struct super_block
*s
, secno secno
)
110 struct quad_buffer_head qbh
;
113 if (!(bits
= hpfs_map_4sectors(s
, secno
, &qbh
, 4))) return 0;
115 for (i
= 0; i
< 2048 / sizeof(unsigned); i
++) {
117 if (!bits
[i
]) continue;
118 for (b
= bits
[i
]; b
; b
>>=1) count
+= b
& 1;
124 static unsigned count_bitmaps(struct super_block
*s
)
126 unsigned n
, count
, n_bands
;
127 n_bands
= (s
->s_hpfs_fs_size
+ 0x3fff) >> 14;
129 for (n
= 0; n
< n_bands
; n
++)
130 count
+= hpfs_count_one_bitmap(s
, s
->s_hpfs_bmp_dir
[n
]);
134 int hpfs_statfs(struct super_block
*s
, struct statfs
*buf
)
136 /*if (s->s_hpfs_n_free == -1) {*/
137 s
->s_hpfs_n_free
= count_bitmaps(s
);
138 s
->s_hpfs_n_free_dnodes
= hpfs_count_one_bitmap(s
, s
->s_hpfs_dmap
);
140 buf
->f_type
= s
->s_magic
;
142 buf
->f_blocks
= s
->s_hpfs_fs_size
;
143 buf
->f_bfree
= s
->s_hpfs_n_free
;
144 buf
->f_bavail
= s
->s_hpfs_n_free
;
145 buf
->f_files
= s
->s_hpfs_dirband_size
/ 4;
146 buf
->f_ffree
= s
->s_hpfs_n_free_dnodes
;
147 buf
->f_namelen
= 254;
151 /* Super operations */
153 static struct super_operations hpfs_sops
=
155 read_inode
: hpfs_read_inode
,
156 delete_inode
: hpfs_delete_inode
,
157 put_super
: hpfs_put_super
,
159 remount_fs
: hpfs_remount_fs
,
163 * A tiny parser for option strings, stolen from dosfs.
165 * Stolen again from read-only hpfs.
168 int parse_opts(char *opts
, uid_t
*uid
, gid_t
*gid
, umode_t
*umask
,
169 int *lowercase
, int *conv
, int *eas
, int *chk
, int *errs
,
170 int *chkdsk
, int *timeshift
)
177 /*printk("Parsing opts: '%s'\n",opts);*/
179 for (p
= strtok(opts
, ","); p
!= 0; p
= strtok(0, ",")) {
180 if ((rhs
= strchr(p
, '=')) != 0)
182 if (!strcmp(p
, "help")) return 2;
183 if (!strcmp(p
, "uid")) {
186 *uid
= simple_strtoul(rhs
, &rhs
, 0);
190 else if (!strcmp(p
, "gid")) {
193 *gid
= simple_strtoul(rhs
, &rhs
, 0);
197 else if (!strcmp(p
, "umask")) {
200 *umask
= simple_strtoul(rhs
, &rhs
, 8);
204 else if (!strcmp(p
, "timeshift")) {
208 if (*rhs
== '-') m
= -1;
209 if (*rhs
== '+' || *rhs
== '-') rhs
++;
210 *timeshift
= simple_strtoul(rhs
, &rhs
, 0) * m
;
214 else if (!strcmp(p
, "case")) {
215 if (!strcmp(rhs
, "lower"))
217 else if (!strcmp(rhs
, "asis"))
222 else if (!strcmp(p
, "conv")) {
223 if (!strcmp(rhs
, "binary"))
225 else if (!strcmp(rhs
, "text"))
227 else if (!strcmp(rhs
, "auto"))
232 else if (!strcmp(p
, "check")) {
233 if (!strcmp(rhs
, "none"))
235 else if (!strcmp(rhs
, "normal"))
237 else if (!strcmp(rhs
, "strict"))
242 else if (!strcmp(p
, "errors")) {
243 if (!strcmp(rhs
, "continue"))
245 else if (!strcmp(rhs
, "remount-ro"))
247 else if (!strcmp(rhs
, "panic"))
252 else if (!strcmp(p
, "eas")) {
253 if (!strcmp(rhs
, "no"))
255 else if (!strcmp(rhs
, "ro"))
257 else if (!strcmp(rhs
, "rw"))
262 else if (!strcmp(p
, "chkdsk")) {
263 if (!strcmp(rhs
, "no"))
265 else if (!strcmp(rhs
, "errors"))
267 else if (!strcmp(rhs
, "always"))
278 static inline void hpfs_help(void)
281 HPFS filesystem options:\n\
282 help do not mount and display this text\n\
283 uid=xxx set uid of files that don't have uid specified in eas\n\
284 gid=xxx set gid of files that don't have gid specified in eas\n\
285 umask=xxx set mode of files that don't have mode specified in eas\n\
286 case=lower lowercase all files\n\
287 case=asis do not lowercase files (default)\n\
288 conv=binary do not convert CR/LF -> LF (default)\n\
289 conv=auto convert only files with known text extensions\n\
290 conv=text convert all files\n\
291 check=none no fs checks - kernel may crash on corrupted filesystem\n\
292 check=normal do some checks - it should not crash (default)\n\
293 check=strict do extra time-consuming checks, used for debugging\n\
294 errors=continue continue on errors\n\
295 errors=remount-ro remount read-only if errors found (default)\n\
296 errors=panic panic on errors\n\
297 chkdsk=no do not mark fs for chkdsking even if there were errors\n\
298 chkdsk=errors mark fs dirty if errors found (default)\n\
299 chkdsk=always always mark fs dirty - used for debugging\n\
300 eas=no ignore extended attributes\n\
301 eas=ro read but do not write extended attributes\n\
302 eas=rw r/w eas => enables chmod, chown, mknod, ln -s (default)\n\
303 timeshift=nnn add nnn seconds to file times\n\
307 int hpfs_remount_fs(struct super_block
*s
, int *flags
, char *data
)
312 int lowercase
, conv
, eas
, chk
, errs
, chkdsk
, timeshift
;
315 *flags
|= MS_NOATIME
;
317 uid
= s
->s_hpfs_uid
; gid
= s
->s_hpfs_gid
;
318 umask
= 0777 & ~s
->s_hpfs_mode
;
319 lowercase
= s
->s_hpfs_lowercase
; conv
= s
->s_hpfs_conv
;
320 eas
= s
->s_hpfs_eas
; chk
= s
->s_hpfs_chk
; chkdsk
= s
->s_hpfs_chkdsk
;
321 errs
= s
->s_hpfs_err
; timeshift
= s
->s_hpfs_timeshift
;
323 if (!(o
= parse_opts(data
, &uid
, &gid
, &umask
, &lowercase
, &conv
,
324 &eas
, &chk
, &errs
, &chkdsk
, ×hift
))) {
325 printk("HPFS: bad mount options.\n");
332 if (timeshift
!= s
->s_hpfs_timeshift
) {
333 printk("HPFS: timeshift can't be changed using remount.\n");
339 s
->s_hpfs_uid
= uid
; s
->s_hpfs_gid
= gid
;
340 s
->s_hpfs_mode
= 0777 & ~umask
;
341 s
->s_hpfs_lowercase
= lowercase
; s
->s_hpfs_conv
= conv
;
342 s
->s_hpfs_eas
= eas
; s
->s_hpfs_chk
= chk
; s
->s_hpfs_chkdsk
= chkdsk
;
343 s
->s_hpfs_err
= errs
; s
->s_hpfs_timeshift
= timeshift
;
345 if (!(*flags
& MS_RDONLY
)) mark_dirty(s
);
350 struct super_block
*hpfs_read_super(struct super_block
*s
, void *options
,
354 struct buffer_head
*bh0
, *bh1
, *bh2
;
355 struct hpfs_boot_block
*bootblock
;
356 struct hpfs_super_block
*superblock
;
357 struct hpfs_spare_block
*spareblock
;
362 int lowercase
, conv
, eas
, chk
, errs
, chkdsk
, timeshift
;
364 dnode_secno root_dno
;
365 struct hpfs_dirent
*de
= NULL
;
366 struct quad_buffer_head qbh
;
370 s
->s_hpfs_bmp_dir
= NULL
;
371 s
->s_hpfs_cp_table
= NULL
;
373 s
->s_hpfs_creation_de_lock
= s
->s_hpfs_rd_inode
= 0;
374 init_waitqueue_head(&s
->s_hpfs_creation_de
);
375 init_waitqueue_head(&s
->s_hpfs_iget_q
);
379 umask
= current
->fs
->umask
;
388 if (!(o
= parse_opts(options
, &uid
, &gid
, &umask
, &lowercase
, &conv
,
389 &eas
, &chk
, &errs
, &chkdsk
, ×hift
))) {
390 printk("HPFS: bad mount options.\n");
398 /*s->s_hpfs_mounting = 1;*/
400 set_blocksize(dev
, 512);
401 s
->s_hpfs_fs_size
= -1;
402 if (!(bootblock
= hpfs_map_sector(s
, 0, &bh0
, 0))) goto bail1
;
403 if (!(superblock
= hpfs_map_sector(s
, 16, &bh1
, 1))) goto bail2
;
404 if (!(spareblock
= hpfs_map_sector(s
, 17, &bh2
, 0))) goto bail3
;
407 if (/*bootblock->magic != BB_MAGIC
408 ||*/ superblock
->magic
!= SB_MAGIC
409 || spareblock
->magic
!= SP_MAGIC
) {
410 if (!silent
) printk("HPFS: Bad magic ... probably not HPFS\n");
415 if (!(s
->s_flags
& MS_RDONLY
) &&
416 superblock
->funcversion
!= 2 && superblock
->funcversion
!= 3) {
417 printk("HPFS: Bad version %d,%d. Mount readonly to go around\n",
418 (int)superblock
->version
, (int)superblock
->funcversion
);
419 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");
423 s
->s_flags
|= MS_NOATIME
;
425 /* Fill superblock stuff */
426 s
->s_magic
= HPFS_SUPER_MAGIC
;
427 s
->s_blocksize
= 512;
428 s
->s_blocksize_bits
= 9;
429 s
->s_op
= &hpfs_sops
;
431 s
->s_hpfs_root
= superblock
->root
;
432 s
->s_hpfs_fs_size
= superblock
->n_sectors
;
433 s
->s_hpfs_bitmaps
= superblock
->bitmaps
;
434 s
->s_hpfs_dirband_start
= superblock
->dir_band_start
;
435 s
->s_hpfs_dirband_size
= superblock
->n_dir_band
;
436 s
->s_hpfs_dmap
= superblock
->dir_band_bitmap
;
439 s
->s_hpfs_mode
= 0777 & ~umask
;
440 s
->s_hpfs_n_free
= -1;
441 s
->s_hpfs_n_free_dnodes
= -1;
442 s
->s_hpfs_lowercase
= lowercase
;
443 s
->s_hpfs_conv
= conv
;
446 s
->s_hpfs_chkdsk
= chkdsk
;
447 s
->s_hpfs_err
= errs
;
448 s
->s_hpfs_timeshift
= timeshift
;
449 s
->s_hpfs_was_error
= 0;
450 s
->s_hpfs_cp_table
= NULL
;
451 s
->s_hpfs_c_bitmap
= -1;
453 /* Load bitmap directory */
454 if (!(s
->s_hpfs_bmp_dir
= hpfs_load_bitmap_directory(s
, superblock
->bitmaps
)))
457 /* Check for general fs errors*/
458 if (spareblock
->dirty
&& !spareblock
->old_wrote
) {
460 printk("HPFS: Improperly stopped, not mounted\n");
463 hpfs_error(s
, "improperly stopped");
466 if (!(s
->s_flags
& MS_RDONLY
)) {
467 spareblock
->dirty
= 1;
468 spareblock
->old_wrote
= 0;
469 mark_buffer_dirty(bh2
);
472 if (spareblock
->hotfixes_used
|| spareblock
->n_spares_used
) {
474 printk("HPFS: Hotfixes not supported here, try chkdsk\n");
478 hpfs_error(s
, "hotfixes not supported here, try chkdsk");
479 if (errs
== 0) printk("HPFS: Proceeding, but your filesystem will be probably corrupted by this driver...\n");
480 else printk("HPFS: This driver may read bad files or crash when operating on disk with hotfixes.\n");
482 if (spareblock
->n_dnode_spares
!= spareblock
->n_dnode_spares_free
) {
484 printk("HPFS: Spare dnodes used, try chkdsk\n");
488 hpfs_error(s
, "warning: spare dnodes used, try chkdsk");
489 if (errs
== 0) printk("HPFS: Proceeding, but your filesystem could be corrupted if you delete files or directories\n");
493 if (superblock
->dir_band_end
- superblock
->dir_band_start
+ 1 != superblock
->n_dir_band
||
494 superblock
->dir_band_end
< superblock
->dir_band_start
|| superblock
->n_dir_band
> 0x4000) {
495 hpfs_error(s
, "dir band size mismatch: dir_band_start==%08x, dir_band_end==%08x, n_dir_band==%08x",
496 superblock
->dir_band_start
, superblock
->dir_band_end
, superblock
->n_dir_band
);
499 a
= s
->s_hpfs_dirband_size
;
500 s
->s_hpfs_dirband_size
= 0;
501 if (hpfs_chk_sectors(s
, superblock
->dir_band_start
, superblock
->n_dir_band
, "dir_band") ||
502 hpfs_chk_sectors(s
, superblock
->dir_band_bitmap
, 4, "dir_band_bitmap") ||
503 hpfs_chk_sectors(s
, superblock
->bitmaps
, 4, "bitmaps")) {
507 s
->s_hpfs_dirband_size
= a
;
508 } else printk("HPFS: You really don't want any checks? You are crazy...\n");
510 /* Load code page table */
511 if (spareblock
->n_code_pages
)
512 if (!(s
->s_hpfs_cp_table
= hpfs_load_code_page(s
, spareblock
->code_page_dir
)))
513 printk("HPFS: Warning: code page support is disabled\n");
519 hpfs_lock_iget(s
, 1);
520 s
->s_root
= d_alloc_root(iget(s
, s
->s_hpfs_root
));
522 if (!s
->s_root
|| !s
->s_root
->d_inode
) {
523 printk("HPFS: iget failed. Why???\n");
526 hpfs_set_dentry_operations(s
->s_root
);
529 * find the root directory's . pointer & finish filling in the inode
532 root_dno
= hpfs_fnode_dno(s
, s
->s_hpfs_root
);
534 de
= map_dirent(s
->s_root
->d_inode
, root_dno
, "\001\001", 2, NULL
, &qbh
);
535 if (!root_dno
|| !de
) hpfs_error(s
, "unable to find root dir");
537 s
->s_root
->d_inode
->i_atime
= local_to_gmt(s
, de
->read_date
);
538 s
->s_root
->d_inode
->i_mtime
= local_to_gmt(s
, de
->write_date
);
539 s
->s_root
->d_inode
->i_ctime
= local_to_gmt(s
, de
->creation_date
);
540 s
->s_root
->d_inode
->i_hpfs_ea_size
= de
->ea_size
;
541 s
->s_root
->d_inode
->i_hpfs_parent_dir
= s
->s_root
->d_inode
->i_ino
;
542 if (s
->s_root
->d_inode
->i_size
== -1) s
->s_root
->d_inode
->i_size
= 2048;
543 if (s
->s_root
->d_inode
->i_blocks
== -1) s
->s_root
->d_inode
->i_blocks
= 5;
545 if (de
) hpfs_brelse4(&qbh
);
554 if (s
->s_hpfs_bmp_dir
) kfree(s
->s_hpfs_bmp_dir
);
555 if (s
->s_hpfs_cp_table
) kfree(s
->s_hpfs_cp_table
);
559 DECLARE_FSTYPE_DEV(hpfs_fs_type
, "hpfs", hpfs_read_super
);
561 static int __init
init_hpfs_fs(void)
563 return register_filesystem(&hpfs_fs_type
);
566 static void __exit
exit_hpfs_fs(void)
568 unregister_filesystem(&hpfs_fs_type
);
573 module_init(init_hpfs_fs
)
574 module_exit(exit_hpfs_fs
)