1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/hpfs/super.c
5 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
7 * mounting, unmounting, error handling
11 #include <linux/module.h>
12 #include <linux/parser.h>
13 #include <linux/init.h>
14 #include <linux/statfs.h>
15 #include <linux/magic.h>
16 #include <linux/sched.h>
17 #include <linux/bitmap.h>
18 #include <linux/slab.h>
19 #include <linux/seq_file.h>
21 /* Mark the filesystem dirty, so that chkdsk checks it when os/2 booted */
23 static void mark_dirty(struct super_block *s, int remount)
25 if (hpfs_sb(s)->sb_chkdsk && (remount || !sb_rdonly(s))) {
26 struct buffer_head *bh;
27 struct hpfs_spare_block *sb;
28 if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
31 mark_buffer_dirty(bh);
32 sync_dirty_buffer(bh);
38 /* Mark the filesystem clean (mark it dirty for chkdsk if chkdsk==2 or if there
41 static void unmark_dirty(struct super_block *s)
43 struct buffer_head *bh;
44 struct hpfs_spare_block *sb;
45 if (sb_rdonly(s)) return;
46 sync_blockdev(s->s_bdev);
47 if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
48 sb->dirty = hpfs_sb(s)->sb_chkdsk > 1 - hpfs_sb(s)->sb_was_error;
49 sb->old_wrote = hpfs_sb(s)->sb_chkdsk >= 2 && !hpfs_sb(s)->sb_was_error;
50 mark_buffer_dirty(bh);
51 sync_dirty_buffer(bh);
56 /* Filesystem error... */
57 void hpfs_error(struct super_block *s, const char *fmt, ...)
67 pr_err("filesystem error: %pV", &vaf);
71 if (!hpfs_sb(s)->sb_was_error) {
72 if (hpfs_sb(s)->sb_err == 2) {
73 pr_cont("; crashing the system because you wanted it\n");
76 } else if (hpfs_sb(s)->sb_err == 1) {
78 pr_cont("; already mounted read-only\n");
80 pr_cont("; remounting read-only\n");
82 s->s_flags |= SB_RDONLY;
84 } else if (sb_rdonly(s))
85 pr_cont("; going on - but anything won't be destroyed because it's read-only\n");
87 pr_cont("; corrupted filesystem mounted read/write - your computer will explode within 20 seconds ... but you wanted it so!\n");
90 hpfs_sb(s)->sb_was_error = 1;
94 * A little trick to detect cycles in many hpfs structures and don't let the
95 * kernel crash on corrupted filesystem. When first called, set c2 to 0.
97 * BTW. chkdsk doesn't detect cycles correctly. When I had 2 lost directories
98 * nested each in other, chkdsk locked up happilly.
101 int hpfs_stop_cycles(struct super_block *s, int key, int *c1, int *c2,
104 if (*c2 && *c1 == key) {
105 hpfs_error(s, "cycle detected on key %08x in %s", key, msg);
109 if (!((*c2 - 1) & *c2)) *c1 = key;
113 static void free_sbi(struct hpfs_sb_info *sbi)
115 kfree(sbi->sb_cp_table);
116 kfree(sbi->sb_bmp_dir);
120 static void lazy_free_sbi(struct rcu_head *rcu)
122 free_sbi(container_of(rcu, struct hpfs_sb_info, rcu));
125 static void hpfs_put_super(struct super_block *s)
130 call_rcu(&hpfs_sb(s)->rcu, lazy_free_sbi);
133 static unsigned hpfs_count_one_bitmap(struct super_block *s, secno secno)
135 struct quad_buffer_head qbh;
139 bits = hpfs_map_4sectors(s, secno, &qbh, 0);
142 count = bitmap_weight(bits, 2048 * BITS_PER_BYTE);
147 static unsigned count_bitmaps(struct super_block *s)
149 unsigned n, count, n_bands;
150 n_bands = (hpfs_sb(s)->sb_fs_size + 0x3fff) >> 14;
152 for (n = 0; n < COUNT_RD_AHEAD; n++) {
153 hpfs_prefetch_bitmap(s, n);
155 for (n = 0; n < n_bands; n++) {
157 hpfs_prefetch_bitmap(s, n + COUNT_RD_AHEAD);
158 c = hpfs_count_one_bitmap(s, le32_to_cpu(hpfs_sb(s)->sb_bmp_dir[n]));
159 if (c != (unsigned)-1)
165 unsigned hpfs_get_free_dnodes(struct super_block *s)
167 struct hpfs_sb_info *sbi = hpfs_sb(s);
168 if (sbi->sb_n_free_dnodes == (unsigned)-1) {
169 unsigned c = hpfs_count_one_bitmap(s, sbi->sb_dmap);
170 if (c == (unsigned)-1)
172 sbi->sb_n_free_dnodes = c;
174 return sbi->sb_n_free_dnodes;
177 static int hpfs_statfs(struct dentry *dentry, struct kstatfs *buf)
179 struct super_block *s = dentry->d_sb;
180 struct hpfs_sb_info *sbi = hpfs_sb(s);
181 u64 id = huge_encode_dev(s->s_bdev->bd_dev);
185 if (sbi->sb_n_free == (unsigned)-1)
186 sbi->sb_n_free = count_bitmaps(s);
188 buf->f_type = s->s_magic;
190 buf->f_blocks = sbi->sb_fs_size;
191 buf->f_bfree = sbi->sb_n_free;
192 buf->f_bavail = sbi->sb_n_free;
193 buf->f_files = sbi->sb_dirband_size / 4;
194 buf->f_ffree = hpfs_get_free_dnodes(s);
195 buf->f_fsid = u64_to_fsid(id);
196 buf->f_namelen = 254;
204 long hpfs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
208 struct fstrim_range range;
211 if (!capable(CAP_SYS_ADMIN))
213 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
215 r = hpfs_trim_fs(file_inode(file)->i_sb, range.start >> 9, (range.start + range.len) >> 9, (range.minlen + 511) >> 9, &n_trimmed);
218 range.len = (u64)n_trimmed << 9;
219 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
230 static struct kmem_cache * hpfs_inode_cachep;
232 static struct inode *hpfs_alloc_inode(struct super_block *sb)
234 struct hpfs_inode_info *ei;
235 ei = alloc_inode_sb(sb, hpfs_inode_cachep, GFP_NOFS);
238 return &ei->vfs_inode;
241 static void hpfs_free_inode(struct inode *inode)
243 kmem_cache_free(hpfs_inode_cachep, hpfs_i(inode));
246 static void init_once(void *foo)
248 struct hpfs_inode_info *ei = (struct hpfs_inode_info *) foo;
250 inode_init_once(&ei->vfs_inode);
253 static int init_inodecache(void)
255 hpfs_inode_cachep = kmem_cache_create("hpfs_inode_cache",
256 sizeof(struct hpfs_inode_info),
257 0, (SLAB_RECLAIM_ACCOUNT|
258 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
260 if (hpfs_inode_cachep == NULL)
265 static void destroy_inodecache(void)
268 * Make sure all delayed rcu free inodes are flushed before we
272 kmem_cache_destroy(hpfs_inode_cachep);
276 * A tiny parser for option strings, stolen from dosfs.
277 * Stolen again from read-only hpfs.
278 * And updated for table-driven option parsing.
282 Opt_help, Opt_uid, Opt_gid, Opt_umask, Opt_case_lower, Opt_case_asis,
283 Opt_check_none, Opt_check_normal, Opt_check_strict,
284 Opt_err_cont, Opt_err_ro, Opt_err_panic,
285 Opt_eas_no, Opt_eas_ro, Opt_eas_rw,
286 Opt_chkdsk_no, Opt_chkdsk_errors, Opt_chkdsk_always,
287 Opt_timeshift, Opt_err,
290 static const match_table_t tokens = {
294 {Opt_umask, "umask=%o"},
295 {Opt_case_lower, "case=lower"},
296 {Opt_case_asis, "case=asis"},
297 {Opt_check_none, "check=none"},
298 {Opt_check_normal, "check=normal"},
299 {Opt_check_strict, "check=strict"},
300 {Opt_err_cont, "errors=continue"},
301 {Opt_err_ro, "errors=remount-ro"},
302 {Opt_err_panic, "errors=panic"},
303 {Opt_eas_no, "eas=no"},
304 {Opt_eas_ro, "eas=ro"},
305 {Opt_eas_rw, "eas=rw"},
306 {Opt_chkdsk_no, "chkdsk=no"},
307 {Opt_chkdsk_errors, "chkdsk=errors"},
308 {Opt_chkdsk_always, "chkdsk=always"},
309 {Opt_timeshift, "timeshift=%d"},
313 static int parse_opts(char *opts, kuid_t *uid, kgid_t *gid, umode_t *umask,
314 int *lowercase, int *eas, int *chk, int *errs,
315 int *chkdsk, int *timeshift)
323 /*pr_info("Parsing opts: '%s'\n",opts);*/
325 while ((p = strsep(&opts, ",")) != NULL) {
326 substring_t args[MAX_OPT_ARGS];
331 token = match_token(p, tokens, args);
336 if (match_int(args, &option))
338 *uid = make_kuid(current_user_ns(), option);
339 if (!uid_valid(*uid))
343 if (match_int(args, &option))
345 *gid = make_kgid(current_user_ns(), option);
346 if (!gid_valid(*gid))
350 if (match_octal(args, &option))
363 case Opt_check_normal:
366 case Opt_check_strict:
390 case Opt_chkdsk_errors:
393 case Opt_chkdsk_always:
399 char *rhs = args[0].from;
402 if (*rhs == '-') m = -1;
403 if (*rhs == '+' || *rhs == '-') rhs++;
404 *timeshift = simple_strtoul(rhs, &rhs, 0) * m;
416 static inline void hpfs_help(void)
419 HPFS filesystem options:\n\
420 help do not mount and display this text\n\
421 uid=xxx set uid of files that don't have uid specified in eas\n\
422 gid=xxx set gid of files that don't have gid specified in eas\n\
423 umask=xxx set mode of files that don't have mode specified in eas\n\
424 case=lower lowercase all files\n\
425 case=asis do not lowercase files (default)\n\
426 check=none no fs checks - kernel may crash on corrupted filesystem\n\
427 check=normal do some checks - it should not crash (default)\n\
428 check=strict do extra time-consuming checks, used for debugging\n\
429 errors=continue continue on errors\n\
430 errors=remount-ro remount read-only if errors found (default)\n\
431 errors=panic panic on errors\n\
432 chkdsk=no do not mark fs for chkdsking even if there were errors\n\
433 chkdsk=errors mark fs dirty if errors found (default)\n\
434 chkdsk=always always mark fs dirty - used for debugging\n\
435 eas=no ignore extended attributes\n\
436 eas=ro read but do not write extended attributes\n\
437 eas=rw r/w eas => enables chmod, chown, mknod, ln -s (default)\n\
438 timeshift=nnn add nnn seconds to file times\n\
442 static int hpfs_remount_fs(struct super_block *s, int *flags, char *data)
447 int lowercase, eas, chk, errs, chkdsk, timeshift;
449 struct hpfs_sb_info *sbi = hpfs_sb(s);
453 *flags |= SB_NOATIME;
456 uid = sbi->sb_uid; gid = sbi->sb_gid;
457 umask = 0777 & ~sbi->sb_mode;
458 lowercase = sbi->sb_lowercase;
459 eas = sbi->sb_eas; chk = sbi->sb_chk; chkdsk = sbi->sb_chkdsk;
460 errs = sbi->sb_err; timeshift = sbi->sb_timeshift;
462 if (!(o = parse_opts(data, &uid, &gid, &umask, &lowercase,
463 &eas, &chk, &errs, &chkdsk, ×hift))) {
464 pr_err("bad mount options.\n");
471 if (timeshift != sbi->sb_timeshift) {
472 pr_err("timeshift can't be changed using remount.\n");
478 sbi->sb_uid = uid; sbi->sb_gid = gid;
479 sbi->sb_mode = 0777 & ~umask;
480 sbi->sb_lowercase = lowercase;
481 sbi->sb_eas = eas; sbi->sb_chk = chk; sbi->sb_chkdsk = chkdsk;
482 sbi->sb_err = errs; sbi->sb_timeshift = timeshift;
484 if (!(*flags & SB_RDONLY)) mark_dirty(s, 1);
494 static int hpfs_show_options(struct seq_file *seq, struct dentry *root)
496 struct hpfs_sb_info *sbi = hpfs_sb(root->d_sb);
498 seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, sbi->sb_uid));
499 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, sbi->sb_gid));
500 seq_printf(seq, ",umask=%03o", (~sbi->sb_mode & 0777));
501 if (sbi->sb_lowercase)
502 seq_printf(seq, ",case=lower");
504 seq_printf(seq, ",check=none");
505 if (sbi->sb_chk == 2)
506 seq_printf(seq, ",check=strict");
508 seq_printf(seq, ",errors=continue");
509 if (sbi->sb_err == 2)
510 seq_printf(seq, ",errors=panic");
512 seq_printf(seq, ",chkdsk=no");
513 if (sbi->sb_chkdsk == 2)
514 seq_printf(seq, ",chkdsk=always");
516 seq_printf(seq, ",eas=no");
517 if (sbi->sb_eas == 1)
518 seq_printf(seq, ",eas=ro");
519 if (sbi->sb_timeshift)
520 seq_printf(seq, ",timeshift=%d", sbi->sb_timeshift);
524 /* Super operations */
526 static const struct super_operations hpfs_sops =
528 .alloc_inode = hpfs_alloc_inode,
529 .free_inode = hpfs_free_inode,
530 .evict_inode = hpfs_evict_inode,
531 .put_super = hpfs_put_super,
532 .statfs = hpfs_statfs,
533 .remount_fs = hpfs_remount_fs,
534 .show_options = hpfs_show_options,
537 static int hpfs_fill_super(struct super_block *s, void *options, int silent)
539 struct buffer_head *bh0, *bh1, *bh2;
540 struct hpfs_boot_block *bootblock;
541 struct hpfs_super_block *superblock;
542 struct hpfs_spare_block *spareblock;
543 struct hpfs_sb_info *sbi;
549 int lowercase, eas, chk, errs, chkdsk, timeshift;
551 dnode_secno root_dno;
552 struct hpfs_dirent *de = NULL;
553 struct quad_buffer_head qbh;
557 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
563 mutex_init(&sbi->hpfs_mutex);
568 umask = current_umask();
576 if (!(o = parse_opts(options, &uid, &gid, &umask, &lowercase,
577 &eas, &chk, &errs, &chkdsk, ×hift))) {
578 pr_err("bad mount options.\n");
586 /*sbi->sb_mounting = 1;*/
587 sb_set_blocksize(s, 512);
588 sbi->sb_fs_size = -1;
589 if (!(bootblock = hpfs_map_sector(s, 0, &bh0, 0))) goto bail1;
590 if (!(superblock = hpfs_map_sector(s, 16, &bh1, 1))) goto bail2;
591 if (!(spareblock = hpfs_map_sector(s, 17, &bh2, 0))) goto bail3;
594 if (/*le16_to_cpu(bootblock->magic) != BB_MAGIC
595 ||*/ le32_to_cpu(superblock->magic) != SB_MAGIC
596 || le32_to_cpu(spareblock->magic) != SP_MAGIC) {
598 pr_err("Bad magic ... probably not HPFS\n");
603 if (!sb_rdonly(s) && superblock->funcversion != 2 && superblock->funcversion != 3) {
604 pr_err("Bad version %d,%d. Mount readonly to go around\n",
605 (int)superblock->version, (int)superblock->funcversion);
606 pr_err("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");
610 s->s_flags |= SB_NOATIME;
612 /* Fill superblock stuff */
613 s->s_magic = HPFS_SUPER_MAGIC;
614 s->s_op = &hpfs_sops;
615 s->s_d_op = &hpfs_dentry_operations;
616 s->s_time_min = local_to_gmt(s, 0);
617 s->s_time_max = local_to_gmt(s, U32_MAX);
619 sbi->sb_root = le32_to_cpu(superblock->root);
620 sbi->sb_fs_size = le32_to_cpu(superblock->n_sectors);
621 sbi->sb_bitmaps = le32_to_cpu(superblock->bitmaps);
622 sbi->sb_dirband_start = le32_to_cpu(superblock->dir_band_start);
623 sbi->sb_dirband_size = le32_to_cpu(superblock->n_dir_band);
624 sbi->sb_dmap = le32_to_cpu(superblock->dir_band_bitmap);
627 sbi->sb_mode = 0777 & ~umask;
629 sbi->sb_n_free_dnodes = -1;
630 sbi->sb_lowercase = lowercase;
633 sbi->sb_chkdsk = chkdsk;
635 sbi->sb_timeshift = timeshift;
636 sbi->sb_was_error = 0;
637 sbi->sb_cp_table = NULL;
638 sbi->sb_c_bitmap = -1;
639 sbi->sb_max_fwd_alloc = 0xffffff;
641 if (sbi->sb_fs_size >= 0x80000000) {
642 hpfs_error(s, "invalid size in superblock: %08x",
643 (unsigned)sbi->sb_fs_size);
647 if (spareblock->n_spares_used)
648 hpfs_load_hotfix_map(s, spareblock);
650 /* Load bitmap directory */
651 if (!(sbi->sb_bmp_dir = hpfs_load_bitmap_directory(s, le32_to_cpu(superblock->bitmaps))))
654 /* Check for general fs errors*/
655 if (spareblock->dirty && !spareblock->old_wrote) {
657 pr_err("Improperly stopped, not mounted\n");
660 hpfs_error(s, "improperly stopped");
664 spareblock->dirty = 1;
665 spareblock->old_wrote = 0;
666 mark_buffer_dirty(bh2);
669 if (le32_to_cpu(spareblock->n_dnode_spares) != le32_to_cpu(spareblock->n_dnode_spares_free)) {
671 pr_err("Spare dnodes used, try chkdsk\n");
675 hpfs_error(s, "warning: spare dnodes used, try chkdsk");
677 pr_err("Proceeding, but your filesystem could be corrupted if you delete files or directories\n");
681 if (le32_to_cpu(superblock->dir_band_end) - le32_to_cpu(superblock->dir_band_start) + 1 != le32_to_cpu(superblock->n_dir_band) ||
682 le32_to_cpu(superblock->dir_band_end) < le32_to_cpu(superblock->dir_band_start) || le32_to_cpu(superblock->n_dir_band) > 0x4000) {
683 hpfs_error(s, "dir band size mismatch: dir_band_start==%08x, dir_band_end==%08x, n_dir_band==%08x",
684 le32_to_cpu(superblock->dir_band_start), le32_to_cpu(superblock->dir_band_end), le32_to_cpu(superblock->n_dir_band));
687 a = sbi->sb_dirband_size;
688 sbi->sb_dirband_size = 0;
689 if (hpfs_chk_sectors(s, le32_to_cpu(superblock->dir_band_start), le32_to_cpu(superblock->n_dir_band), "dir_band") ||
690 hpfs_chk_sectors(s, le32_to_cpu(superblock->dir_band_bitmap), 4, "dir_band_bitmap") ||
691 hpfs_chk_sectors(s, le32_to_cpu(superblock->bitmaps), 4, "bitmaps")) {
695 sbi->sb_dirband_size = a;
697 pr_err("You really don't want any checks? You are crazy...\n");
699 /* Load code page table */
700 if (le32_to_cpu(spareblock->n_code_pages))
701 if (!(sbi->sb_cp_table = hpfs_load_code_page(s, le32_to_cpu(spareblock->code_page_dir))))
702 pr_err("code page support is disabled\n");
708 root = iget_locked(s, sbi->sb_root);
711 hpfs_init_inode(root);
712 hpfs_read_inode(root);
713 unlock_new_inode(root);
714 s->s_root = d_make_root(root);
719 * find the root directory's . pointer & finish filling in the inode
722 root_dno = hpfs_fnode_dno(s, sbi->sb_root);
724 de = map_dirent(root, root_dno, "\001\001", 2, NULL, &qbh);
726 hpfs_error(s, "unable to find root dir");
728 root->i_atime.tv_sec = local_to_gmt(s, le32_to_cpu(de->read_date));
729 root->i_atime.tv_nsec = 0;
730 root->i_mtime.tv_sec = local_to_gmt(s, le32_to_cpu(de->write_date));
731 root->i_mtime.tv_nsec = 0;
732 root->i_ctime.tv_sec = local_to_gmt(s, le32_to_cpu(de->creation_date));
733 root->i_ctime.tv_nsec = 0;
734 hpfs_i(root)->i_ea_size = le32_to_cpu(de->ea_size);
735 hpfs_i(root)->i_parent_dir = root->i_ino;
736 if (root->i_size == -1)
738 if (root->i_blocks == -1)
755 static struct dentry *hpfs_mount(struct file_system_type *fs_type,
756 int flags, const char *dev_name, void *data)
758 return mount_bdev(fs_type, flags, dev_name, data, hpfs_fill_super);
761 static struct file_system_type hpfs_fs_type = {
762 .owner = THIS_MODULE,
765 .kill_sb = kill_block_super,
766 .fs_flags = FS_REQUIRES_DEV,
768 MODULE_ALIAS_FS("hpfs");
770 static int __init init_hpfs_fs(void)
772 int err = init_inodecache();
775 err = register_filesystem(&hpfs_fs_type);
780 destroy_inodecache();
785 static void __exit exit_hpfs_fs(void)
787 unregister_filesystem(&hpfs_fs_type);
788 destroy_inodecache();
791 module_init(init_hpfs_fs)
792 module_exit(exit_hpfs_fs)
793 MODULE_LICENSE("GPL");