super.c 22 KB

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