super.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4. */
  5. #include <linux/fs_context.h>
  6. #include <linux/fs_parser.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/time.h>
  10. #include <linux/mount.h>
  11. #include <linux/cred.h>
  12. #include <linux/statfs.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/fs_struct.h>
  16. #include <linux/iversion.h>
  17. #include <linux/nls.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/magic.h>
  20. #include "exfat_raw.h"
  21. #include "exfat_fs.h"
  22. static char exfat_default_iocharset[] = CONFIG_EXFAT_DEFAULT_IOCHARSET;
  23. static struct kmem_cache *exfat_inode_cachep;
  24. static void exfat_free_iocharset(struct exfat_sb_info *sbi)
  25. {
  26. if (sbi->options.iocharset != exfat_default_iocharset)
  27. kfree(sbi->options.iocharset);
  28. }
  29. static void exfat_set_iocharset(struct exfat_mount_options *opts,
  30. char *iocharset)
  31. {
  32. opts->iocharset = iocharset;
  33. if (!strcmp(opts->iocharset, "utf8"))
  34. opts->utf8 = 1;
  35. else
  36. opts->utf8 = 0;
  37. }
  38. static void exfat_put_super(struct super_block *sb)
  39. {
  40. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  41. mutex_lock(&sbi->s_lock);
  42. exfat_clear_volume_dirty(sb);
  43. exfat_free_bitmap(sbi);
  44. brelse(sbi->boot_bh);
  45. mutex_unlock(&sbi->s_lock);
  46. }
  47. static int exfat_statfs(struct dentry *dentry, struct kstatfs *buf)
  48. {
  49. struct super_block *sb = dentry->d_sb;
  50. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  51. unsigned long long id = huge_encode_dev(sb->s_bdev->bd_dev);
  52. buf->f_type = sb->s_magic;
  53. buf->f_bsize = sbi->cluster_size;
  54. buf->f_blocks = sbi->num_clusters - 2; /* clu 0 & 1 */
  55. buf->f_bfree = buf->f_blocks - sbi->used_clusters;
  56. buf->f_bavail = buf->f_bfree;
  57. buf->f_fsid = u64_to_fsid(id);
  58. /* Unicode utf16 255 characters */
  59. buf->f_namelen = EXFAT_MAX_FILE_LEN * NLS_MAX_CHARSET_SIZE;
  60. return 0;
  61. }
  62. static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags)
  63. {
  64. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  65. struct boot_sector *p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
  66. /* retain persistent-flags */
  67. new_flags |= sbi->vol_flags_persistent;
  68. /* flags are not changed */
  69. if (sbi->vol_flags == new_flags)
  70. return 0;
  71. sbi->vol_flags = new_flags;
  72. /* skip updating volume dirty flag,
  73. * if this volume has been mounted with read-only
  74. */
  75. if (sb_rdonly(sb))
  76. return 0;
  77. p_boot->vol_flags = cpu_to_le16(new_flags);
  78. set_buffer_uptodate(sbi->boot_bh);
  79. mark_buffer_dirty(sbi->boot_bh);
  80. __sync_dirty_buffer(sbi->boot_bh, REQ_SYNC | REQ_FUA | REQ_PREFLUSH);
  81. return 0;
  82. }
  83. int exfat_set_volume_dirty(struct super_block *sb)
  84. {
  85. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  86. return exfat_set_vol_flags(sb, sbi->vol_flags | VOLUME_DIRTY);
  87. }
  88. int exfat_clear_volume_dirty(struct super_block *sb)
  89. {
  90. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  91. return exfat_set_vol_flags(sb, sbi->vol_flags & ~VOLUME_DIRTY);
  92. }
  93. static int exfat_show_options(struct seq_file *m, struct dentry *root)
  94. {
  95. struct super_block *sb = root->d_sb;
  96. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  97. struct exfat_mount_options *opts = &sbi->options;
  98. /* Show partition info */
  99. if (!uid_eq(opts->fs_uid, GLOBAL_ROOT_UID))
  100. seq_printf(m, ",uid=%u",
  101. from_kuid_munged(&init_user_ns, opts->fs_uid));
  102. if (!gid_eq(opts->fs_gid, GLOBAL_ROOT_GID))
  103. seq_printf(m, ",gid=%u",
  104. from_kgid_munged(&init_user_ns, opts->fs_gid));
  105. seq_printf(m, ",fmask=%04o,dmask=%04o", opts->fs_fmask, opts->fs_dmask);
  106. if (opts->allow_utime)
  107. seq_printf(m, ",allow_utime=%04o", opts->allow_utime);
  108. if (opts->utf8)
  109. seq_puts(m, ",iocharset=utf8");
  110. else if (sbi->nls_io)
  111. seq_printf(m, ",iocharset=%s", sbi->nls_io->charset);
  112. if (opts->errors == EXFAT_ERRORS_CONT)
  113. seq_puts(m, ",errors=continue");
  114. else if (opts->errors == EXFAT_ERRORS_PANIC)
  115. seq_puts(m, ",errors=panic");
  116. else
  117. seq_puts(m, ",errors=remount-ro");
  118. if (opts->discard)
  119. seq_puts(m, ",discard");
  120. if (opts->keep_last_dots)
  121. seq_puts(m, ",keep_last_dots");
  122. if (opts->sys_tz)
  123. seq_puts(m, ",sys_tz");
  124. else if (opts->time_offset)
  125. seq_printf(m, ",time_offset=%d", opts->time_offset);
  126. if (opts->zero_size_dir)
  127. seq_puts(m, ",zero_size_dir");
  128. return 0;
  129. }
  130. int exfat_force_shutdown(struct super_block *sb, u32 flags)
  131. {
  132. int ret;
  133. struct exfat_sb_info *sbi = sb->s_fs_info;
  134. struct exfat_mount_options *opts = &sbi->options;
  135. if (exfat_forced_shutdown(sb))
  136. return 0;
  137. switch (flags) {
  138. case EXFAT_GOING_DOWN_DEFAULT:
  139. case EXFAT_GOING_DOWN_FULLSYNC:
  140. ret = bdev_freeze(sb->s_bdev);
  141. if (ret)
  142. return ret;
  143. bdev_thaw(sb->s_bdev);
  144. set_bit(EXFAT_FLAGS_SHUTDOWN, &sbi->s_exfat_flags);
  145. break;
  146. case EXFAT_GOING_DOWN_NOSYNC:
  147. set_bit(EXFAT_FLAGS_SHUTDOWN, &sbi->s_exfat_flags);
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. if (opts->discard)
  153. opts->discard = 0;
  154. return 0;
  155. }
  156. static void exfat_shutdown(struct super_block *sb)
  157. {
  158. exfat_force_shutdown(sb, EXFAT_GOING_DOWN_NOSYNC);
  159. }
  160. static struct inode *exfat_alloc_inode(struct super_block *sb)
  161. {
  162. struct exfat_inode_info *ei;
  163. ei = alloc_inode_sb(sb, exfat_inode_cachep, GFP_NOFS);
  164. if (!ei)
  165. return NULL;
  166. init_rwsem(&ei->truncate_lock);
  167. return &ei->vfs_inode;
  168. }
  169. static void exfat_free_inode(struct inode *inode)
  170. {
  171. kmem_cache_free(exfat_inode_cachep, EXFAT_I(inode));
  172. }
  173. static const struct super_operations exfat_sops = {
  174. .alloc_inode = exfat_alloc_inode,
  175. .free_inode = exfat_free_inode,
  176. .write_inode = exfat_write_inode,
  177. .evict_inode = exfat_evict_inode,
  178. .put_super = exfat_put_super,
  179. .statfs = exfat_statfs,
  180. .show_options = exfat_show_options,
  181. .shutdown = exfat_shutdown,
  182. };
  183. enum {
  184. Opt_uid,
  185. Opt_gid,
  186. Opt_umask,
  187. Opt_dmask,
  188. Opt_fmask,
  189. Opt_allow_utime,
  190. Opt_charset,
  191. Opt_errors,
  192. Opt_discard,
  193. Opt_keep_last_dots,
  194. Opt_sys_tz,
  195. Opt_time_offset,
  196. Opt_zero_size_dir,
  197. /* Deprecated options */
  198. Opt_utf8,
  199. Opt_debug,
  200. Opt_namecase,
  201. Opt_codepage,
  202. };
  203. static const struct constant_table exfat_param_enums[] = {
  204. { "continue", EXFAT_ERRORS_CONT },
  205. { "panic", EXFAT_ERRORS_PANIC },
  206. { "remount-ro", EXFAT_ERRORS_RO },
  207. {}
  208. };
  209. static const struct fs_parameter_spec exfat_parameters[] = {
  210. fsparam_uid("uid", Opt_uid),
  211. fsparam_gid("gid", Opt_gid),
  212. fsparam_u32oct("umask", Opt_umask),
  213. fsparam_u32oct("dmask", Opt_dmask),
  214. fsparam_u32oct("fmask", Opt_fmask),
  215. fsparam_u32oct("allow_utime", Opt_allow_utime),
  216. fsparam_string("iocharset", Opt_charset),
  217. fsparam_enum("errors", Opt_errors, exfat_param_enums),
  218. fsparam_flag_no("discard", Opt_discard),
  219. fsparam_flag("keep_last_dots", Opt_keep_last_dots),
  220. fsparam_flag("sys_tz", Opt_sys_tz),
  221. fsparam_s32("time_offset", Opt_time_offset),
  222. fsparam_flag_no("zero_size_dir", Opt_zero_size_dir),
  223. __fsparam(NULL, "utf8", Opt_utf8, fs_param_deprecated,
  224. NULL),
  225. __fsparam(NULL, "debug", Opt_debug, fs_param_deprecated,
  226. NULL),
  227. __fsparam(fs_param_is_u32, "namecase", Opt_namecase,
  228. fs_param_deprecated, NULL),
  229. __fsparam(fs_param_is_u32, "codepage", Opt_codepage,
  230. fs_param_deprecated, NULL),
  231. {}
  232. };
  233. static int exfat_parse_param(struct fs_context *fc, struct fs_parameter *param)
  234. {
  235. struct exfat_sb_info *sbi = fc->s_fs_info;
  236. struct exfat_mount_options *opts = &sbi->options;
  237. struct fs_parse_result result;
  238. int opt;
  239. opt = fs_parse(fc, exfat_parameters, param, &result);
  240. if (opt < 0)
  241. return opt;
  242. switch (opt) {
  243. case Opt_uid:
  244. opts->fs_uid = result.uid;
  245. break;
  246. case Opt_gid:
  247. opts->fs_gid = result.gid;
  248. break;
  249. case Opt_umask:
  250. opts->fs_fmask = result.uint_32;
  251. opts->fs_dmask = result.uint_32;
  252. break;
  253. case Opt_dmask:
  254. opts->fs_dmask = result.uint_32;
  255. break;
  256. case Opt_fmask:
  257. opts->fs_fmask = result.uint_32;
  258. break;
  259. case Opt_allow_utime:
  260. opts->allow_utime = result.uint_32 & 0022;
  261. break;
  262. case Opt_charset:
  263. exfat_free_iocharset(sbi);
  264. exfat_set_iocharset(opts, param->string);
  265. param->string = NULL;
  266. break;
  267. case Opt_errors:
  268. opts->errors = result.uint_32;
  269. break;
  270. case Opt_discard:
  271. opts->discard = !result.negated;
  272. break;
  273. case Opt_keep_last_dots:
  274. opts->keep_last_dots = 1;
  275. break;
  276. case Opt_sys_tz:
  277. opts->sys_tz = 1;
  278. break;
  279. case Opt_time_offset:
  280. /*
  281. * Make the limit 24 just in case someone invents something
  282. * unusual.
  283. */
  284. if (result.int_32 < -24 * 60 || result.int_32 > 24 * 60)
  285. return -EINVAL;
  286. opts->time_offset = result.int_32;
  287. break;
  288. case Opt_zero_size_dir:
  289. opts->zero_size_dir = !result.negated;
  290. break;
  291. case Opt_utf8:
  292. case Opt_debug:
  293. case Opt_namecase:
  294. case Opt_codepage:
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. return 0;
  300. }
  301. static void exfat_hash_init(struct super_block *sb)
  302. {
  303. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  304. int i;
  305. spin_lock_init(&sbi->inode_hash_lock);
  306. for (i = 0; i < EXFAT_HASH_SIZE; i++)
  307. INIT_HLIST_HEAD(&sbi->inode_hashtable[i]);
  308. }
  309. static int exfat_read_root(struct inode *inode, struct exfat_chain *root_clu)
  310. {
  311. struct super_block *sb = inode->i_sb;
  312. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  313. struct exfat_inode_info *ei = EXFAT_I(inode);
  314. int num_subdirs;
  315. exfat_chain_set(&ei->dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
  316. ei->entry = -1;
  317. ei->start_clu = sbi->root_dir;
  318. ei->flags = ALLOC_FAT_CHAIN;
  319. ei->type = TYPE_DIR;
  320. ei->version = 0;
  321. ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
  322. ei->hint_stat.eidx = 0;
  323. ei->hint_stat.clu = sbi->root_dir;
  324. ei->hint_femp.eidx = EXFAT_HINT_NONE;
  325. i_size_write(inode, EXFAT_CLU_TO_B(root_clu->size, sbi));
  326. num_subdirs = exfat_count_dir_entries(sb, root_clu);
  327. if (num_subdirs < 0)
  328. return -EIO;
  329. set_nlink(inode, num_subdirs + EXFAT_MIN_SUBDIR);
  330. inode->i_uid = sbi->options.fs_uid;
  331. inode->i_gid = sbi->options.fs_gid;
  332. inode_inc_iversion(inode);
  333. inode->i_generation = 0;
  334. inode->i_mode = exfat_make_mode(sbi, EXFAT_ATTR_SUBDIR, 0777);
  335. inode->i_op = &exfat_dir_inode_operations;
  336. inode->i_fop = &exfat_dir_operations;
  337. inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
  338. ei->i_pos = ((loff_t)sbi->root_dir << 32) | 0xffffffff;
  339. exfat_save_attr(inode, EXFAT_ATTR_SUBDIR);
  340. ei->i_crtime = simple_inode_init_ts(inode);
  341. exfat_truncate_inode_atime(inode);
  342. return 0;
  343. }
  344. static int exfat_calibrate_blocksize(struct super_block *sb, int logical_sect)
  345. {
  346. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  347. if (!is_power_of_2(logical_sect)) {
  348. exfat_err(sb, "bogus logical sector size %u", logical_sect);
  349. return -EIO;
  350. }
  351. if (logical_sect < sb->s_blocksize) {
  352. exfat_err(sb, "logical sector size too small for device (logical sector size = %u)",
  353. logical_sect);
  354. return -EIO;
  355. }
  356. if (logical_sect > sb->s_blocksize) {
  357. brelse(sbi->boot_bh);
  358. sbi->boot_bh = NULL;
  359. if (!sb_set_blocksize(sb, logical_sect)) {
  360. exfat_err(sb, "unable to set blocksize %u",
  361. logical_sect);
  362. return -EIO;
  363. }
  364. sbi->boot_bh = sb_bread(sb, 0);
  365. if (!sbi->boot_bh) {
  366. exfat_err(sb, "unable to read boot sector (logical sector size = %lu)",
  367. sb->s_blocksize);
  368. return -EIO;
  369. }
  370. }
  371. return 0;
  372. }
  373. static int exfat_read_boot_sector(struct super_block *sb)
  374. {
  375. struct boot_sector *p_boot;
  376. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  377. /* set block size to read super block */
  378. if (!sb_min_blocksize(sb, 512)) {
  379. exfat_err(sb, "unable to set blocksize");
  380. return -EINVAL;
  381. }
  382. /* read boot sector */
  383. sbi->boot_bh = sb_bread(sb, 0);
  384. if (!sbi->boot_bh) {
  385. exfat_err(sb, "unable to read boot sector");
  386. return -EIO;
  387. }
  388. p_boot = (struct boot_sector *)sbi->boot_bh->b_data;
  389. /* check the validity of BOOT */
  390. if (le16_to_cpu((p_boot->signature)) != BOOT_SIGNATURE) {
  391. exfat_err(sb, "invalid boot record signature");
  392. return -EINVAL;
  393. }
  394. if (memcmp(p_boot->fs_name, STR_EXFAT, BOOTSEC_FS_NAME_LEN)) {
  395. exfat_err(sb, "invalid fs_name"); /* fs_name may unprintable */
  396. return -EINVAL;
  397. }
  398. /*
  399. * must_be_zero field must be filled with zero to prevent mounting
  400. * from FAT volume.
  401. */
  402. if (memchr_inv(p_boot->must_be_zero, 0, sizeof(p_boot->must_be_zero)))
  403. return -EINVAL;
  404. if (p_boot->num_fats != 1 && p_boot->num_fats != 2) {
  405. exfat_err(sb, "bogus number of FAT structure");
  406. return -EINVAL;
  407. }
  408. /*
  409. * sect_size_bits could be at least 9 and at most 12.
  410. */
  411. if (p_boot->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS ||
  412. p_boot->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) {
  413. exfat_err(sb, "bogus sector size bits : %u",
  414. p_boot->sect_size_bits);
  415. return -EINVAL;
  416. }
  417. /*
  418. * sect_per_clus_bits could be at least 0 and at most 25 - sect_size_bits.
  419. */
  420. if (p_boot->sect_per_clus_bits > EXFAT_MAX_SECT_PER_CLUS_BITS(p_boot)) {
  421. exfat_err(sb, "bogus sectors bits per cluster : %u",
  422. p_boot->sect_per_clus_bits);
  423. return -EINVAL;
  424. }
  425. sbi->sect_per_clus = 1 << p_boot->sect_per_clus_bits;
  426. sbi->sect_per_clus_bits = p_boot->sect_per_clus_bits;
  427. sbi->cluster_size_bits = p_boot->sect_per_clus_bits +
  428. p_boot->sect_size_bits;
  429. sbi->cluster_size = 1 << sbi->cluster_size_bits;
  430. sbi->num_FAT_sectors = le32_to_cpu(p_boot->fat_length);
  431. sbi->FAT1_start_sector = le32_to_cpu(p_boot->fat_offset);
  432. sbi->FAT2_start_sector = le32_to_cpu(p_boot->fat_offset);
  433. if (p_boot->num_fats == 2)
  434. sbi->FAT2_start_sector += sbi->num_FAT_sectors;
  435. sbi->data_start_sector = le32_to_cpu(p_boot->clu_offset);
  436. sbi->num_sectors = le64_to_cpu(p_boot->vol_length);
  437. /* because the cluster index starts with 2 */
  438. sbi->num_clusters = le32_to_cpu(p_boot->clu_count) +
  439. EXFAT_RESERVED_CLUSTERS;
  440. sbi->root_dir = le32_to_cpu(p_boot->root_cluster);
  441. sbi->dentries_per_clu = 1 <<
  442. (sbi->cluster_size_bits - DENTRY_SIZE_BITS);
  443. sbi->vol_flags = le16_to_cpu(p_boot->vol_flags);
  444. sbi->vol_flags_persistent = sbi->vol_flags & (VOLUME_DIRTY | MEDIA_FAILURE);
  445. sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
  446. /* check consistencies */
  447. if ((u64)sbi->num_FAT_sectors << p_boot->sect_size_bits <
  448. (u64)sbi->num_clusters * 4) {
  449. exfat_err(sb, "bogus fat length");
  450. return -EINVAL;
  451. }
  452. if (sbi->data_start_sector <
  453. (u64)sbi->FAT1_start_sector +
  454. (u64)sbi->num_FAT_sectors * p_boot->num_fats) {
  455. exfat_err(sb, "bogus data start sector");
  456. return -EINVAL;
  457. }
  458. if (sbi->vol_flags & VOLUME_DIRTY)
  459. exfat_warn(sb, "Volume was not properly unmounted. Some data may be corrupt. Please run fsck.");
  460. if (sbi->vol_flags & MEDIA_FAILURE)
  461. exfat_warn(sb, "Medium has reported failures. Some data may be lost.");
  462. /* exFAT file size is limited by a disk volume size */
  463. sb->s_maxbytes = (u64)(sbi->num_clusters - EXFAT_RESERVED_CLUSTERS) <<
  464. sbi->cluster_size_bits;
  465. /* check logical sector size */
  466. if (exfat_calibrate_blocksize(sb, 1 << p_boot->sect_size_bits))
  467. return -EIO;
  468. return 0;
  469. }
  470. static int exfat_verify_boot_region(struct super_block *sb)
  471. {
  472. struct buffer_head *bh = NULL;
  473. u32 chksum = 0;
  474. __le32 *p_sig, *p_chksum;
  475. int sn, i;
  476. /* read boot sector sub-regions */
  477. for (sn = 0; sn < 11; sn++) {
  478. bh = sb_bread(sb, sn);
  479. if (!bh)
  480. return -EIO;
  481. if (sn != 0 && sn <= 8) {
  482. /* extended boot sector sub-regions */
  483. p_sig = (__le32 *)&bh->b_data[sb->s_blocksize - 4];
  484. if (le32_to_cpu(*p_sig) != EXBOOT_SIGNATURE)
  485. exfat_warn(sb, "Invalid exboot-signature(sector = %d): 0x%08x",
  486. sn, le32_to_cpu(*p_sig));
  487. }
  488. chksum = exfat_calc_chksum32(bh->b_data, sb->s_blocksize,
  489. chksum, sn ? CS_DEFAULT : CS_BOOT_SECTOR);
  490. brelse(bh);
  491. }
  492. /* boot checksum sub-regions */
  493. bh = sb_bread(sb, sn);
  494. if (!bh)
  495. return -EIO;
  496. for (i = 0; i < sb->s_blocksize; i += sizeof(u32)) {
  497. p_chksum = (__le32 *)&bh->b_data[i];
  498. if (le32_to_cpu(*p_chksum) != chksum) {
  499. exfat_err(sb, "Invalid boot checksum (boot checksum : 0x%08x, checksum : 0x%08x)",
  500. le32_to_cpu(*p_chksum), chksum);
  501. brelse(bh);
  502. return -EINVAL;
  503. }
  504. }
  505. brelse(bh);
  506. return 0;
  507. }
  508. /* mount the file system volume */
  509. static int __exfat_fill_super(struct super_block *sb,
  510. struct exfat_chain *root_clu)
  511. {
  512. int ret;
  513. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  514. ret = exfat_read_boot_sector(sb);
  515. if (ret) {
  516. exfat_err(sb, "failed to read boot sector");
  517. goto free_bh;
  518. }
  519. ret = exfat_verify_boot_region(sb);
  520. if (ret) {
  521. exfat_err(sb, "invalid boot region");
  522. goto free_bh;
  523. }
  524. /*
  525. * Call exfat_count_num_cluster() before searching for up-case and
  526. * bitmap directory entries to avoid infinite loop if they are missing
  527. * and the cluster chain includes a loop.
  528. */
  529. exfat_chain_set(root_clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
  530. ret = exfat_count_num_clusters(sb, root_clu, &root_clu->size);
  531. if (ret) {
  532. exfat_err(sb, "failed to count the number of clusters in root");
  533. goto free_bh;
  534. }
  535. ret = exfat_create_upcase_table(sb);
  536. if (ret) {
  537. exfat_err(sb, "failed to load upcase table");
  538. goto free_bh;
  539. }
  540. ret = exfat_load_bitmap(sb);
  541. if (ret) {
  542. exfat_err(sb, "failed to load alloc-bitmap");
  543. goto free_bh;
  544. }
  545. if (!exfat_test_bitmap(sb, sbi->root_dir)) {
  546. exfat_warn(sb, "failed to test first cluster bit of root dir(%u)",
  547. sbi->root_dir);
  548. /*
  549. * The first cluster bit of the root directory should never
  550. * be unset except when storage is corrupted. This bit is
  551. * set to allow operations after mount.
  552. */
  553. exfat_set_bitmap(sb, sbi->root_dir, false);
  554. }
  555. ret = exfat_count_used_clusters(sb, &sbi->used_clusters);
  556. if (ret) {
  557. exfat_err(sb, "failed to scan clusters");
  558. goto free_alloc_bitmap;
  559. }
  560. return 0;
  561. free_alloc_bitmap:
  562. exfat_free_bitmap(sbi);
  563. free_bh:
  564. brelse(sbi->boot_bh);
  565. return ret;
  566. }
  567. static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
  568. {
  569. struct exfat_sb_info *sbi = sb->s_fs_info;
  570. struct exfat_mount_options *opts = &sbi->options;
  571. struct inode *root_inode;
  572. struct exfat_chain root_clu;
  573. int err;
  574. if (opts->allow_utime == (unsigned short)-1)
  575. opts->allow_utime = ~opts->fs_dmask & 0022;
  576. if (opts->discard && !bdev_max_discard_sectors(sb->s_bdev)) {
  577. exfat_warn(sb, "mounting with \"discard\" option, but the device does not support discard");
  578. opts->discard = 0;
  579. }
  580. sb->s_flags |= SB_NODIRATIME;
  581. sb->s_magic = EXFAT_SUPER_MAGIC;
  582. sb->s_op = &exfat_sops;
  583. sb->s_time_gran = 10 * NSEC_PER_MSEC;
  584. sb->s_time_min = EXFAT_MIN_TIMESTAMP_SECS;
  585. sb->s_time_max = EXFAT_MAX_TIMESTAMP_SECS;
  586. err = __exfat_fill_super(sb, &root_clu);
  587. if (err) {
  588. exfat_err(sb, "failed to recognize exfat type");
  589. goto check_nls_io;
  590. }
  591. /* set up enough so that it can read an inode */
  592. exfat_hash_init(sb);
  593. if (sbi->options.utf8)
  594. set_default_d_op(sb, &exfat_utf8_dentry_ops);
  595. else {
  596. sbi->nls_io = load_nls(sbi->options.iocharset);
  597. if (!sbi->nls_io) {
  598. exfat_err(sb, "IO charset %s not found",
  599. sbi->options.iocharset);
  600. err = -EINVAL;
  601. goto free_table;
  602. }
  603. set_default_d_op(sb, &exfat_dentry_ops);
  604. }
  605. root_inode = new_inode(sb);
  606. if (!root_inode) {
  607. exfat_err(sb, "failed to allocate root inode");
  608. err = -ENOMEM;
  609. goto free_table;
  610. }
  611. root_inode->i_ino = EXFAT_ROOT_INO;
  612. inode_set_iversion(root_inode, 1);
  613. err = exfat_read_root(root_inode, &root_clu);
  614. if (err) {
  615. exfat_err(sb, "failed to initialize root inode");
  616. goto put_inode;
  617. }
  618. exfat_hash_inode(root_inode, EXFAT_I(root_inode)->i_pos);
  619. insert_inode_hash(root_inode);
  620. sb->s_root = d_make_root(root_inode);
  621. if (!sb->s_root) {
  622. exfat_err(sb, "failed to get the root dentry");
  623. err = -ENOMEM;
  624. goto free_table;
  625. }
  626. return 0;
  627. put_inode:
  628. iput(root_inode);
  629. sb->s_root = NULL;
  630. free_table:
  631. exfat_free_bitmap(sbi);
  632. brelse(sbi->boot_bh);
  633. check_nls_io:
  634. return err;
  635. }
  636. static int exfat_get_tree(struct fs_context *fc)
  637. {
  638. return get_tree_bdev(fc, exfat_fill_super);
  639. }
  640. static void exfat_free_sbi(struct exfat_sb_info *sbi)
  641. {
  642. exfat_free_iocharset(sbi);
  643. kfree(sbi);
  644. }
  645. static void exfat_free(struct fs_context *fc)
  646. {
  647. struct exfat_sb_info *sbi = fc->s_fs_info;
  648. if (sbi)
  649. exfat_free_sbi(sbi);
  650. }
  651. static int exfat_reconfigure(struct fs_context *fc)
  652. {
  653. struct super_block *sb = fc->root->d_sb;
  654. struct exfat_sb_info *remount_sbi = fc->s_fs_info;
  655. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  656. struct exfat_mount_options *new_opts = &remount_sbi->options;
  657. struct exfat_mount_options *cur_opts = &sbi->options;
  658. fc->sb_flags |= SB_NODIRATIME;
  659. sync_filesystem(sb);
  660. mutex_lock(&sbi->s_lock);
  661. exfat_clear_volume_dirty(sb);
  662. mutex_unlock(&sbi->s_lock);
  663. if (new_opts->allow_utime == (unsigned short)-1)
  664. new_opts->allow_utime = ~new_opts->fs_dmask & 0022;
  665. /*
  666. * Since the old settings of these mount options are cached in
  667. * inodes or dentries, they cannot be modified dynamically.
  668. */
  669. if (strcmp(new_opts->iocharset, cur_opts->iocharset) ||
  670. new_opts->keep_last_dots != cur_opts->keep_last_dots ||
  671. new_opts->sys_tz != cur_opts->sys_tz ||
  672. new_opts->time_offset != cur_opts->time_offset ||
  673. !uid_eq(new_opts->fs_uid, cur_opts->fs_uid) ||
  674. !gid_eq(new_opts->fs_gid, cur_opts->fs_gid) ||
  675. new_opts->fs_fmask != cur_opts->fs_fmask ||
  676. new_opts->fs_dmask != cur_opts->fs_dmask ||
  677. new_opts->allow_utime != cur_opts->allow_utime)
  678. return -EINVAL;
  679. if (new_opts->discard != cur_opts->discard &&
  680. new_opts->discard &&
  681. !bdev_max_discard_sectors(sb->s_bdev)) {
  682. exfat_warn(sb, "remounting with \"discard\" option, but the device does not support discard");
  683. return -EINVAL;
  684. }
  685. swap(*cur_opts, *new_opts);
  686. return 0;
  687. }
  688. static const struct fs_context_operations exfat_context_ops = {
  689. .parse_param = exfat_parse_param,
  690. .get_tree = exfat_get_tree,
  691. .free = exfat_free,
  692. .reconfigure = exfat_reconfigure,
  693. };
  694. static int exfat_init_fs_context(struct fs_context *fc)
  695. {
  696. struct exfat_sb_info *sbi;
  697. sbi = kzalloc_obj(struct exfat_sb_info);
  698. if (!sbi)
  699. return -ENOMEM;
  700. mutex_init(&sbi->s_lock);
  701. mutex_init(&sbi->bitmap_lock);
  702. ratelimit_state_init(&sbi->ratelimit, DEFAULT_RATELIMIT_INTERVAL,
  703. DEFAULT_RATELIMIT_BURST);
  704. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE && fc->root) {
  705. struct super_block *sb = fc->root->d_sb;
  706. struct exfat_mount_options *cur_opts = &EXFAT_SB(sb)->options;
  707. sbi->options.fs_uid = cur_opts->fs_uid;
  708. sbi->options.fs_gid = cur_opts->fs_gid;
  709. sbi->options.fs_fmask = cur_opts->fs_fmask;
  710. sbi->options.fs_dmask = cur_opts->fs_dmask;
  711. } else {
  712. sbi->options.fs_uid = current_uid();
  713. sbi->options.fs_gid = current_gid();
  714. sbi->options.fs_fmask = current->fs->umask;
  715. sbi->options.fs_dmask = current->fs->umask;
  716. }
  717. sbi->options.allow_utime = -1;
  718. sbi->options.errors = EXFAT_ERRORS_RO;
  719. exfat_set_iocharset(&sbi->options, exfat_default_iocharset);
  720. fc->s_fs_info = sbi;
  721. fc->ops = &exfat_context_ops;
  722. return 0;
  723. }
  724. static void delayed_free(struct rcu_head *p)
  725. {
  726. struct exfat_sb_info *sbi = container_of(p, struct exfat_sb_info, rcu);
  727. unload_nls(sbi->nls_io);
  728. exfat_free_upcase_table(sbi);
  729. exfat_free_sbi(sbi);
  730. }
  731. static void exfat_kill_sb(struct super_block *sb)
  732. {
  733. struct exfat_sb_info *sbi = sb->s_fs_info;
  734. kill_block_super(sb);
  735. if (sbi)
  736. call_rcu(&sbi->rcu, delayed_free);
  737. }
  738. static struct file_system_type exfat_fs_type = {
  739. .owner = THIS_MODULE,
  740. .name = "exfat",
  741. .init_fs_context = exfat_init_fs_context,
  742. .parameters = exfat_parameters,
  743. .kill_sb = exfat_kill_sb,
  744. .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
  745. };
  746. static void exfat_inode_init_once(void *foo)
  747. {
  748. struct exfat_inode_info *ei = (struct exfat_inode_info *)foo;
  749. spin_lock_init(&ei->cache_lru_lock);
  750. ei->nr_caches = 0;
  751. ei->cache_valid_id = EXFAT_CACHE_VALID + 1;
  752. INIT_LIST_HEAD(&ei->cache_lru);
  753. INIT_HLIST_NODE(&ei->i_hash_fat);
  754. inode_init_once(&ei->vfs_inode);
  755. }
  756. static int __init init_exfat_fs(void)
  757. {
  758. int err;
  759. err = exfat_cache_init();
  760. if (err)
  761. return err;
  762. exfat_inode_cachep = kmem_cache_create("exfat_inode_cache",
  763. sizeof(struct exfat_inode_info),
  764. 0, SLAB_RECLAIM_ACCOUNT,
  765. exfat_inode_init_once);
  766. if (!exfat_inode_cachep) {
  767. err = -ENOMEM;
  768. goto shutdown_cache;
  769. }
  770. err = register_filesystem(&exfat_fs_type);
  771. if (err)
  772. goto destroy_cache;
  773. return 0;
  774. destroy_cache:
  775. kmem_cache_destroy(exfat_inode_cachep);
  776. shutdown_cache:
  777. exfat_cache_shutdown();
  778. return err;
  779. }
  780. static void __exit exit_exfat_fs(void)
  781. {
  782. /*
  783. * Make sure all delayed rcu free inodes are flushed before we
  784. * destroy cache.
  785. */
  786. rcu_barrier();
  787. kmem_cache_destroy(exfat_inode_cachep);
  788. unregister_filesystem(&exfat_fs_type);
  789. exfat_cache_shutdown();
  790. }
  791. module_init(init_exfat_fs);
  792. module_exit(exit_exfat_fs);
  793. MODULE_ALIAS_FS("exfat");
  794. MODULE_LICENSE("GPL");
  795. MODULE_DESCRIPTION("exFAT filesystem support");
  796. MODULE_AUTHOR("Samsung Electronics Co., Ltd.");