super.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/hpfs/super.c
  4. *
  5. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  6. *
  7. * mounting, unmounting, error handling
  8. */
  9. #include "hpfs_fn.h"
  10. #include <linux/module.h>
  11. #include <linux/fs_struct.h>
  12. #include <linux/fs_context.h>
  13. #include <linux/fs_parser.h>
  14. #include <linux/init.h>
  15. #include <linux/statfs.h>
  16. #include <linux/magic.h>
  17. #include <linux/sched.h>
  18. #include <linux/bitmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/seq_file.h>
  21. /* Mark the filesystem dirty, so that chkdsk checks it when os/2 booted */
  22. static void mark_dirty(struct super_block *s, int remount)
  23. {
  24. if (hpfs_sb(s)->sb_chkdsk && (remount || !sb_rdonly(s))) {
  25. struct buffer_head *bh;
  26. struct hpfs_spare_block *sb;
  27. if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
  28. sb->dirty = 1;
  29. sb->old_wrote = 0;
  30. mark_buffer_dirty(bh);
  31. sync_dirty_buffer(bh);
  32. brelse(bh);
  33. }
  34. }
  35. }
  36. /* Mark the filesystem clean (mark it dirty for chkdsk if chkdsk==2 or if there
  37. were errors) */
  38. static void unmark_dirty(struct super_block *s)
  39. {
  40. struct buffer_head *bh;
  41. struct hpfs_spare_block *sb;
  42. if (sb_rdonly(s)) return;
  43. sync_blockdev(s->s_bdev);
  44. if ((sb = hpfs_map_sector(s, 17, &bh, 0))) {
  45. sb->dirty = hpfs_sb(s)->sb_chkdsk > 1 - hpfs_sb(s)->sb_was_error;
  46. sb->old_wrote = hpfs_sb(s)->sb_chkdsk >= 2 && !hpfs_sb(s)->sb_was_error;
  47. mark_buffer_dirty(bh);
  48. sync_dirty_buffer(bh);
  49. brelse(bh);
  50. }
  51. }
  52. /* Filesystem error... */
  53. void hpfs_error(struct super_block *s, const char *fmt, ...)
  54. {
  55. struct va_format vaf;
  56. va_list args;
  57. va_start(args, fmt);
  58. vaf.fmt = fmt;
  59. vaf.va = &args;
  60. pr_err("filesystem error: %pV", &vaf);
  61. va_end(args);
  62. if (!hpfs_sb(s)->sb_was_error) {
  63. if (hpfs_sb(s)->sb_err == 2) {
  64. pr_cont("; crashing the system because you wanted it\n");
  65. mark_dirty(s, 0);
  66. panic("HPFS panic");
  67. } else if (hpfs_sb(s)->sb_err == 1) {
  68. if (sb_rdonly(s))
  69. pr_cont("; already mounted read-only\n");
  70. else {
  71. pr_cont("; remounting read-only\n");
  72. mark_dirty(s, 0);
  73. s->s_flags |= SB_RDONLY;
  74. }
  75. } else if (sb_rdonly(s))
  76. pr_cont("; going on - but anything won't be destroyed because it's read-only\n");
  77. else
  78. pr_cont("; corrupted filesystem mounted read/write - your computer will explode within 20 seconds ... but you wanted it so!\n");
  79. } else
  80. pr_cont("\n");
  81. hpfs_sb(s)->sb_was_error = 1;
  82. }
  83. /*
  84. * A little trick to detect cycles in many hpfs structures and don't let the
  85. * kernel crash on corrupted filesystem. When first called, set c2 to 0.
  86. *
  87. * BTW. chkdsk doesn't detect cycles correctly. When I had 2 lost directories
  88. * nested each in other, chkdsk locked up happilly.
  89. */
  90. int hpfs_stop_cycles(struct super_block *s, int key, int *c1, int *c2,
  91. char *msg)
  92. {
  93. if (*c2 && *c1 == key) {
  94. hpfs_error(s, "cycle detected on key %08x in %s", key, msg);
  95. return 1;
  96. }
  97. (*c2)++;
  98. if (!((*c2 - 1) & *c2)) *c1 = key;
  99. return 0;
  100. }
  101. static void free_sbi(struct hpfs_sb_info *sbi)
  102. {
  103. kfree(sbi->sb_cp_table);
  104. kfree(sbi->sb_bmp_dir);
  105. kfree(sbi);
  106. }
  107. static void lazy_free_sbi(struct rcu_head *rcu)
  108. {
  109. free_sbi(container_of(rcu, struct hpfs_sb_info, rcu));
  110. }
  111. static void hpfs_put_super(struct super_block *s)
  112. {
  113. hpfs_lock(s);
  114. unmark_dirty(s);
  115. hpfs_unlock(s);
  116. call_rcu(&hpfs_sb(s)->rcu, lazy_free_sbi);
  117. }
  118. static unsigned hpfs_count_one_bitmap(struct super_block *s, secno secno)
  119. {
  120. struct quad_buffer_head qbh;
  121. unsigned long *bits;
  122. unsigned count;
  123. bits = hpfs_map_4sectors(s, secno, &qbh, 0);
  124. if (!bits)
  125. return (unsigned)-1;
  126. count = bitmap_weight(bits, 2048 * BITS_PER_BYTE);
  127. hpfs_brelse4(&qbh);
  128. return count;
  129. }
  130. static unsigned count_bitmaps(struct super_block *s)
  131. {
  132. unsigned n, count, n_bands;
  133. n_bands = (hpfs_sb(s)->sb_fs_size + 0x3fff) >> 14;
  134. count = 0;
  135. for (n = 0; n < COUNT_RD_AHEAD; n++) {
  136. hpfs_prefetch_bitmap(s, n);
  137. }
  138. for (n = 0; n < n_bands; n++) {
  139. unsigned c;
  140. hpfs_prefetch_bitmap(s, n + COUNT_RD_AHEAD);
  141. c = hpfs_count_one_bitmap(s, le32_to_cpu(hpfs_sb(s)->sb_bmp_dir[n]));
  142. if (c != (unsigned)-1)
  143. count += c;
  144. }
  145. return count;
  146. }
  147. unsigned hpfs_get_free_dnodes(struct super_block *s)
  148. {
  149. struct hpfs_sb_info *sbi = hpfs_sb(s);
  150. if (sbi->sb_n_free_dnodes == (unsigned)-1) {
  151. unsigned c = hpfs_count_one_bitmap(s, sbi->sb_dmap);
  152. if (c == (unsigned)-1)
  153. return 0;
  154. sbi->sb_n_free_dnodes = c;
  155. }
  156. return sbi->sb_n_free_dnodes;
  157. }
  158. static int hpfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  159. {
  160. struct super_block *s = dentry->d_sb;
  161. struct hpfs_sb_info *sbi = hpfs_sb(s);
  162. u64 id = huge_encode_dev(s->s_bdev->bd_dev);
  163. hpfs_lock(s);
  164. if (sbi->sb_n_free == (unsigned)-1)
  165. sbi->sb_n_free = count_bitmaps(s);
  166. buf->f_type = s->s_magic;
  167. buf->f_bsize = 512;
  168. buf->f_blocks = sbi->sb_fs_size;
  169. buf->f_bfree = sbi->sb_n_free;
  170. buf->f_bavail = sbi->sb_n_free;
  171. buf->f_files = sbi->sb_dirband_size / 4;
  172. buf->f_ffree = hpfs_get_free_dnodes(s);
  173. buf->f_fsid = u64_to_fsid(id);
  174. buf->f_namelen = 254;
  175. hpfs_unlock(s);
  176. return 0;
  177. }
  178. long hpfs_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  179. {
  180. switch (cmd) {
  181. case FITRIM: {
  182. struct fstrim_range range;
  183. secno n_trimmed;
  184. int r;
  185. if (!capable(CAP_SYS_ADMIN))
  186. return -EPERM;
  187. if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
  188. return -EFAULT;
  189. r = hpfs_trim_fs(file_inode(file)->i_sb, range.start >> 9, (range.start + range.len) >> 9, (range.minlen + 511) >> 9, &n_trimmed);
  190. if (r)
  191. return r;
  192. range.len = (u64)n_trimmed << 9;
  193. if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
  194. return -EFAULT;
  195. return 0;
  196. }
  197. default: {
  198. return -ENOIOCTLCMD;
  199. }
  200. }
  201. }
  202. static struct kmem_cache * hpfs_inode_cachep;
  203. static struct inode *hpfs_alloc_inode(struct super_block *sb)
  204. {
  205. struct hpfs_inode_info *ei;
  206. ei = alloc_inode_sb(sb, hpfs_inode_cachep, GFP_NOFS);
  207. if (!ei)
  208. return NULL;
  209. return &ei->vfs_inode;
  210. }
  211. static void hpfs_free_inode(struct inode *inode)
  212. {
  213. kmem_cache_free(hpfs_inode_cachep, hpfs_i(inode));
  214. }
  215. static void init_once(void *foo)
  216. {
  217. struct hpfs_inode_info *ei = (struct hpfs_inode_info *) foo;
  218. inode_init_once(&ei->vfs_inode);
  219. }
  220. static int init_inodecache(void)
  221. {
  222. hpfs_inode_cachep = kmem_cache_create("hpfs_inode_cache",
  223. sizeof(struct hpfs_inode_info),
  224. 0, (SLAB_RECLAIM_ACCOUNT|
  225. SLAB_ACCOUNT),
  226. init_once);
  227. if (hpfs_inode_cachep == NULL)
  228. return -ENOMEM;
  229. return 0;
  230. }
  231. static void destroy_inodecache(void)
  232. {
  233. /*
  234. * Make sure all delayed rcu free inodes are flushed before we
  235. * destroy cache.
  236. */
  237. rcu_barrier();
  238. kmem_cache_destroy(hpfs_inode_cachep);
  239. }
  240. enum {
  241. Opt_help, Opt_uid, Opt_gid, Opt_umask, Opt_case,
  242. Opt_check, Opt_err, Opt_eas, Opt_chkdsk, Opt_timeshift,
  243. };
  244. static const struct constant_table hpfs_param_case[] = {
  245. {"asis", 0},
  246. {"lower", 1},
  247. {}
  248. };
  249. static const struct constant_table hpfs_param_check[] = {
  250. {"none", 0},
  251. {"normal", 1},
  252. {"strict", 2},
  253. {}
  254. };
  255. static const struct constant_table hpfs_param_err[] = {
  256. {"continue", 0},
  257. {"remount-ro", 1},
  258. {"panic", 2},
  259. {}
  260. };
  261. static const struct constant_table hpfs_param_eas[] = {
  262. {"no", 0},
  263. {"ro", 1},
  264. {"rw", 2},
  265. {}
  266. };
  267. static const struct constant_table hpfs_param_chkdsk[] = {
  268. {"no", 0},
  269. {"errors", 1},
  270. {"always", 2},
  271. {}
  272. };
  273. static const struct fs_parameter_spec hpfs_param_spec[] = {
  274. fsparam_flag ("help", Opt_help),
  275. fsparam_uid ("uid", Opt_uid),
  276. fsparam_gid ("gid", Opt_gid),
  277. fsparam_u32oct ("umask", Opt_umask),
  278. fsparam_enum ("case", Opt_case, hpfs_param_case),
  279. fsparam_enum ("check", Opt_check, hpfs_param_check),
  280. fsparam_enum ("errors", Opt_err, hpfs_param_err),
  281. fsparam_enum ("eas", Opt_eas, hpfs_param_eas),
  282. fsparam_enum ("chkdsk", Opt_chkdsk, hpfs_param_chkdsk),
  283. fsparam_s32 ("timeshift", Opt_timeshift),
  284. {}
  285. };
  286. struct hpfs_fc_context {
  287. kuid_t uid;
  288. kgid_t gid;
  289. umode_t umask;
  290. int lowercase;
  291. int eas;
  292. int chk;
  293. int errs;
  294. int chkdsk;
  295. int timeshift;
  296. };
  297. static inline void hpfs_help(void)
  298. {
  299. pr_info("\n\
  300. HPFS filesystem options:\n\
  301. help do not mount and display this text\n\
  302. uid=xxx set uid of files that don't have uid specified in eas\n\
  303. gid=xxx set gid of files that don't have gid specified in eas\n\
  304. umask=xxx set mode of files that don't have mode specified in eas\n\
  305. case=lower lowercase all files\n\
  306. case=asis do not lowercase files (default)\n\
  307. check=none no fs checks - kernel may crash on corrupted filesystem\n\
  308. check=normal do some checks - it should not crash (default)\n\
  309. check=strict do extra time-consuming checks, used for debugging\n\
  310. errors=continue continue on errors\n\
  311. errors=remount-ro remount read-only if errors found (default)\n\
  312. errors=panic panic on errors\n\
  313. chkdsk=no do not mark fs for chkdsking even if there were errors\n\
  314. chkdsk=errors mark fs dirty if errors found (default)\n\
  315. chkdsk=always always mark fs dirty - used for debugging\n\
  316. eas=no ignore extended attributes\n\
  317. eas=ro read but do not write extended attributes\n\
  318. eas=rw r/w eas => enables chmod, chown, mknod, ln -s (default)\n\
  319. timeshift=nnn add nnn seconds to file times\n\
  320. \n");
  321. }
  322. static int hpfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  323. {
  324. struct hpfs_fc_context *ctx = fc->fs_private;
  325. struct fs_parse_result result;
  326. int opt;
  327. opt = fs_parse(fc, hpfs_param_spec, param, &result);
  328. if (opt < 0)
  329. return opt;
  330. switch (opt) {
  331. case Opt_help:
  332. hpfs_help();
  333. return -EINVAL;
  334. case Opt_uid:
  335. ctx->uid = result.uid;
  336. break;
  337. case Opt_gid:
  338. ctx->gid = result.gid;
  339. break;
  340. case Opt_umask:
  341. ctx->umask = result.uint_32;
  342. break;
  343. case Opt_case:
  344. ctx->lowercase = result.uint_32;
  345. break;
  346. case Opt_check:
  347. ctx->chk = result.uint_32;
  348. break;
  349. case Opt_err:
  350. ctx->errs = result.uint_32;
  351. break;
  352. case Opt_eas:
  353. ctx->eas = result.uint_32;
  354. break;
  355. case Opt_chkdsk:
  356. ctx->chkdsk = result.uint_32;
  357. break;
  358. case Opt_timeshift:
  359. {
  360. char *rhs = param->string;
  361. int timeshift;
  362. if (kstrtoint(rhs, 0, &timeshift))
  363. return -EINVAL;
  364. ctx->timeshift = timeshift;
  365. break;
  366. }
  367. default:
  368. return -EINVAL;
  369. }
  370. return 0;
  371. }
  372. static int hpfs_reconfigure(struct fs_context *fc)
  373. {
  374. struct hpfs_fc_context *ctx = fc->fs_private;
  375. struct super_block *s = fc->root->d_sb;
  376. struct hpfs_sb_info *sbi = hpfs_sb(s);
  377. sync_filesystem(s);
  378. fc->sb_flags |= SB_NOATIME;
  379. hpfs_lock(s);
  380. if (ctx->timeshift != sbi->sb_timeshift) {
  381. pr_err("timeshift can't be changed using remount.\n");
  382. goto out_err;
  383. }
  384. unmark_dirty(s);
  385. sbi->sb_uid = ctx->uid; sbi->sb_gid = ctx->gid;
  386. sbi->sb_mode = 0777 & ~ctx->umask;
  387. sbi->sb_lowercase = ctx->lowercase;
  388. sbi->sb_eas = ctx->eas; sbi->sb_chk = ctx->chk;
  389. sbi->sb_chkdsk = ctx->chkdsk;
  390. sbi->sb_err = ctx->errs; sbi->sb_timeshift = ctx->timeshift;
  391. if (!(fc->sb_flags & SB_RDONLY)) mark_dirty(s, 1);
  392. hpfs_unlock(s);
  393. return 0;
  394. out_err:
  395. hpfs_unlock(s);
  396. return -EINVAL;
  397. }
  398. static int hpfs_show_options(struct seq_file *seq, struct dentry *root)
  399. {
  400. struct hpfs_sb_info *sbi = hpfs_sb(root->d_sb);
  401. seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, sbi->sb_uid));
  402. seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, sbi->sb_gid));
  403. seq_printf(seq, ",umask=%03o", (~sbi->sb_mode & 0777));
  404. if (sbi->sb_lowercase)
  405. seq_printf(seq, ",case=lower");
  406. if (!sbi->sb_chk)
  407. seq_printf(seq, ",check=none");
  408. if (sbi->sb_chk == 2)
  409. seq_printf(seq, ",check=strict");
  410. if (!sbi->sb_err)
  411. seq_printf(seq, ",errors=continue");
  412. if (sbi->sb_err == 2)
  413. seq_printf(seq, ",errors=panic");
  414. if (!sbi->sb_chkdsk)
  415. seq_printf(seq, ",chkdsk=no");
  416. if (sbi->sb_chkdsk == 2)
  417. seq_printf(seq, ",chkdsk=always");
  418. if (!sbi->sb_eas)
  419. seq_printf(seq, ",eas=no");
  420. if (sbi->sb_eas == 1)
  421. seq_printf(seq, ",eas=ro");
  422. if (sbi->sb_timeshift)
  423. seq_printf(seq, ",timeshift=%d", sbi->sb_timeshift);
  424. return 0;
  425. }
  426. /* Super operations */
  427. static const struct super_operations hpfs_sops =
  428. {
  429. .alloc_inode = hpfs_alloc_inode,
  430. .free_inode = hpfs_free_inode,
  431. .evict_inode = hpfs_evict_inode,
  432. .put_super = hpfs_put_super,
  433. .statfs = hpfs_statfs,
  434. .show_options = hpfs_show_options,
  435. };
  436. static int hpfs_fill_super(struct super_block *s, struct fs_context *fc)
  437. {
  438. struct hpfs_fc_context *ctx = fc->fs_private;
  439. struct buffer_head *bh0, *bh1, *bh2;
  440. struct hpfs_boot_block *bootblock;
  441. struct hpfs_super_block *superblock;
  442. struct hpfs_spare_block *spareblock;
  443. struct hpfs_sb_info *sbi;
  444. struct inode *root;
  445. int silent = fc->sb_flags & SB_SILENT;
  446. dnode_secno root_dno;
  447. struct hpfs_dirent *de = NULL;
  448. struct quad_buffer_head qbh;
  449. sbi = kzalloc_obj(*sbi);
  450. if (!sbi) {
  451. return -ENOMEM;
  452. }
  453. s->s_fs_info = sbi;
  454. mutex_init(&sbi->hpfs_mutex);
  455. hpfs_lock(s);
  456. /*sbi->sb_mounting = 1;*/
  457. sb_set_blocksize(s, 512);
  458. sbi->sb_fs_size = -1;
  459. if (!(bootblock = hpfs_map_sector(s, 0, &bh0, 0))) goto bail1;
  460. if (!(superblock = hpfs_map_sector(s, 16, &bh1, 1))) goto bail2;
  461. if (!(spareblock = hpfs_map_sector(s, 17, &bh2, 0))) goto bail3;
  462. /* Check magics */
  463. if (/*le16_to_cpu(bootblock->magic) != BB_MAGIC
  464. ||*/ le32_to_cpu(superblock->magic) != SB_MAGIC
  465. || le32_to_cpu(spareblock->magic) != SP_MAGIC) {
  466. if (!silent)
  467. pr_err("Bad magic ... probably not HPFS\n");
  468. goto bail4;
  469. }
  470. /* Check version */
  471. if (!sb_rdonly(s) && superblock->funcversion != 2 && superblock->funcversion != 3) {
  472. pr_err("Bad version %d,%d. Mount readonly to go around\n",
  473. (int)superblock->version, (int)superblock->funcversion);
  474. 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");
  475. goto bail4;
  476. }
  477. s->s_flags |= SB_NOATIME;
  478. /* Fill superblock stuff */
  479. s->s_magic = HPFS_SUPER_MAGIC;
  480. s->s_op = &hpfs_sops;
  481. set_default_d_op(s, &hpfs_dentry_operations);
  482. s->s_time_min = local_to_gmt(s, 0);
  483. s->s_time_max = local_to_gmt(s, U32_MAX);
  484. sbi->sb_root = le32_to_cpu(superblock->root);
  485. sbi->sb_fs_size = le32_to_cpu(superblock->n_sectors);
  486. sbi->sb_bitmaps = le32_to_cpu(superblock->bitmaps);
  487. sbi->sb_dirband_start = le32_to_cpu(superblock->dir_band_start);
  488. sbi->sb_dirband_size = le32_to_cpu(superblock->n_dir_band);
  489. sbi->sb_dmap = le32_to_cpu(superblock->dir_band_bitmap);
  490. sbi->sb_uid = ctx->uid;
  491. sbi->sb_gid = ctx->gid;
  492. sbi->sb_mode = 0777 & ~ctx->umask;
  493. sbi->sb_n_free = -1;
  494. sbi->sb_n_free_dnodes = -1;
  495. sbi->sb_lowercase = ctx->lowercase;
  496. sbi->sb_eas = ctx->eas;
  497. sbi->sb_chk = ctx->chk;
  498. sbi->sb_chkdsk = ctx->chkdsk;
  499. sbi->sb_err = ctx->errs;
  500. sbi->sb_timeshift = ctx->timeshift;
  501. sbi->sb_was_error = 0;
  502. sbi->sb_cp_table = NULL;
  503. sbi->sb_c_bitmap = -1;
  504. sbi->sb_max_fwd_alloc = 0xffffff;
  505. if (sbi->sb_fs_size >= 0x80000000) {
  506. hpfs_error(s, "invalid size in superblock: %08x",
  507. (unsigned)sbi->sb_fs_size);
  508. goto bail4;
  509. }
  510. if (spareblock->n_spares_used)
  511. hpfs_load_hotfix_map(s, spareblock);
  512. /* Load bitmap directory */
  513. if (!(sbi->sb_bmp_dir = hpfs_load_bitmap_directory(s, le32_to_cpu(superblock->bitmaps))))
  514. goto bail4;
  515. /* Check for general fs errors*/
  516. if (spareblock->dirty && !spareblock->old_wrote) {
  517. if (sbi->sb_err == 2) {
  518. pr_err("Improperly stopped, not mounted\n");
  519. goto bail4;
  520. }
  521. hpfs_error(s, "improperly stopped");
  522. }
  523. if (!sb_rdonly(s)) {
  524. spareblock->dirty = 1;
  525. spareblock->old_wrote = 0;
  526. mark_buffer_dirty(bh2);
  527. }
  528. if (le32_to_cpu(spareblock->n_dnode_spares) != le32_to_cpu(spareblock->n_dnode_spares_free)) {
  529. if (sbi->sb_err >= 2) {
  530. pr_err("Spare dnodes used, try chkdsk\n");
  531. mark_dirty(s, 0);
  532. goto bail4;
  533. }
  534. hpfs_error(s, "warning: spare dnodes used, try chkdsk");
  535. if (sbi->sb_err == 0)
  536. pr_err("Proceeding, but your filesystem could be corrupted if you delete files or directories\n");
  537. }
  538. if (sbi->sb_chk) {
  539. unsigned a;
  540. if (le32_to_cpu(superblock->dir_band_end) - le32_to_cpu(superblock->dir_band_start) + 1 != le32_to_cpu(superblock->n_dir_band) ||
  541. le32_to_cpu(superblock->dir_band_end) < le32_to_cpu(superblock->dir_band_start) || le32_to_cpu(superblock->n_dir_band) > 0x4000) {
  542. hpfs_error(s, "dir band size mismatch: dir_band_start==%08x, dir_band_end==%08x, n_dir_band==%08x",
  543. le32_to_cpu(superblock->dir_band_start), le32_to_cpu(superblock->dir_band_end), le32_to_cpu(superblock->n_dir_band));
  544. goto bail4;
  545. }
  546. a = sbi->sb_dirband_size;
  547. sbi->sb_dirband_size = 0;
  548. if (hpfs_chk_sectors(s, le32_to_cpu(superblock->dir_band_start), le32_to_cpu(superblock->n_dir_band), "dir_band") ||
  549. hpfs_chk_sectors(s, le32_to_cpu(superblock->dir_band_bitmap), 4, "dir_band_bitmap") ||
  550. hpfs_chk_sectors(s, le32_to_cpu(superblock->bitmaps), 4, "bitmaps")) {
  551. mark_dirty(s, 0);
  552. goto bail4;
  553. }
  554. sbi->sb_dirband_size = a;
  555. } else
  556. pr_err("You really don't want any checks? You are crazy...\n");
  557. /* Load code page table */
  558. if (le32_to_cpu(spareblock->n_code_pages))
  559. if (!(sbi->sb_cp_table = hpfs_load_code_page(s, le32_to_cpu(spareblock->code_page_dir))))
  560. pr_err("code page support is disabled\n");
  561. brelse(bh2);
  562. brelse(bh1);
  563. brelse(bh0);
  564. root = iget_locked(s, sbi->sb_root);
  565. if (!root)
  566. goto bail0;
  567. hpfs_init_inode(root);
  568. hpfs_read_inode(root);
  569. unlock_new_inode(root);
  570. s->s_root = d_make_root(root);
  571. if (!s->s_root)
  572. goto bail0;
  573. /*
  574. * find the root directory's . pointer & finish filling in the inode
  575. */
  576. root_dno = hpfs_fnode_dno(s, sbi->sb_root);
  577. if (root_dno)
  578. de = map_dirent(root, root_dno, "\001\001", 2, NULL, &qbh);
  579. if (!de)
  580. hpfs_error(s, "unable to find root dir");
  581. else {
  582. inode_set_atime(root,
  583. local_to_gmt(s, le32_to_cpu(de->read_date)),
  584. 0);
  585. inode_set_mtime(root,
  586. local_to_gmt(s, le32_to_cpu(de->write_date)),
  587. 0);
  588. inode_set_ctime(root,
  589. local_to_gmt(s, le32_to_cpu(de->creation_date)),
  590. 0);
  591. hpfs_i(root)->i_ea_size = le32_to_cpu(de->ea_size);
  592. hpfs_i(root)->i_parent_dir = root->i_ino;
  593. if (root->i_size == -1)
  594. root->i_size = 2048;
  595. if (root->i_blocks == -1)
  596. root->i_blocks = 5;
  597. hpfs_brelse4(&qbh);
  598. }
  599. hpfs_unlock(s);
  600. return 0;
  601. bail4: brelse(bh2);
  602. bail3: brelse(bh1);
  603. bail2: brelse(bh0);
  604. bail1:
  605. bail0:
  606. hpfs_unlock(s);
  607. free_sbi(sbi);
  608. return -EINVAL;
  609. }
  610. static int hpfs_get_tree(struct fs_context *fc)
  611. {
  612. return get_tree_bdev(fc, hpfs_fill_super);
  613. }
  614. static void hpfs_free_fc(struct fs_context *fc)
  615. {
  616. kfree(fc->fs_private);
  617. }
  618. static const struct fs_context_operations hpfs_fc_context_ops = {
  619. .parse_param = hpfs_parse_param,
  620. .get_tree = hpfs_get_tree,
  621. .reconfigure = hpfs_reconfigure,
  622. .free = hpfs_free_fc,
  623. };
  624. static int hpfs_init_fs_context(struct fs_context *fc)
  625. {
  626. struct hpfs_fc_context *ctx;
  627. ctx = kzalloc_obj(struct hpfs_fc_context);
  628. if (!ctx)
  629. return -ENOMEM;
  630. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  631. struct super_block *sb = fc->root->d_sb;
  632. struct hpfs_sb_info *sbi = hpfs_sb(sb);
  633. ctx->uid = sbi->sb_uid;
  634. ctx->gid = sbi->sb_gid;
  635. ctx->umask = 0777 & ~sbi->sb_mode;
  636. ctx->lowercase = sbi->sb_lowercase;
  637. ctx->eas = sbi->sb_eas;
  638. ctx->chk = sbi->sb_chk;
  639. ctx->chkdsk = sbi->sb_chkdsk;
  640. ctx->errs = sbi->sb_err;
  641. ctx->timeshift = sbi->sb_timeshift;
  642. } else {
  643. ctx->uid = current_uid();
  644. ctx->gid = current_gid();
  645. ctx->umask = current_umask();
  646. ctx->lowercase = 0;
  647. ctx->eas = 2;
  648. ctx->chk = 1;
  649. ctx->errs = 1;
  650. ctx->chkdsk = 1;
  651. ctx->timeshift = 0;
  652. }
  653. fc->fs_private = ctx;
  654. fc->ops = &hpfs_fc_context_ops;
  655. return 0;
  656. };
  657. static struct file_system_type hpfs_fs_type = {
  658. .owner = THIS_MODULE,
  659. .name = "hpfs",
  660. .kill_sb = kill_block_super,
  661. .fs_flags = FS_REQUIRES_DEV,
  662. .init_fs_context = hpfs_init_fs_context,
  663. .parameters = hpfs_param_spec,
  664. };
  665. MODULE_ALIAS_FS("hpfs");
  666. static int __init init_hpfs_fs(void)
  667. {
  668. int err = init_inodecache();
  669. if (err)
  670. goto out1;
  671. err = register_filesystem(&hpfs_fs_type);
  672. if (err)
  673. goto out;
  674. return 0;
  675. out:
  676. destroy_inodecache();
  677. out1:
  678. return err;
  679. }
  680. static void __exit exit_hpfs_fs(void)
  681. {
  682. unregister_filesystem(&hpfs_fs_type);
  683. destroy_inodecache();
  684. }
  685. module_init(init_hpfs_fs)
  686. module_exit(exit_hpfs_fs)
  687. MODULE_DESCRIPTION("OS/2 HPFS file system support");
  688. MODULE_LICENSE("GPL");