super.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. *
  7. * terminology
  8. *
  9. * cluster - allocation unit - 512,1K,2K,4K,...,2M
  10. * vcn - virtual cluster number - Offset inside the file in clusters.
  11. * vbo - virtual byte offset - Offset inside the file in bytes.
  12. * lcn - logical cluster number - 0 based cluster in clusters heap.
  13. * lbo - logical byte offset - Absolute position inside volume.
  14. * run - maps VCN to LCN - Stored in attributes in packed form.
  15. * attr - attribute segment - std/name/data etc records inside MFT.
  16. * mi - MFT inode - One MFT record(usually 1024 bytes or 4K), consists of attributes.
  17. * ni - NTFS inode - Extends linux inode. consists of one or more mft inodes.
  18. * index - unit inside directory - 2K, 4K, <=page size, does not depend on cluster size.
  19. *
  20. * WSL - Windows Subsystem for Linux
  21. * https://docs.microsoft.com/en-us/windows/wsl/file-permissions
  22. * It stores uid/gid/mode/dev in xattr
  23. *
  24. * ntfs allows up to 2^64 clusters per volume.
  25. * It means you should use 64 bits lcn to operate with ntfs.
  26. * Implementation of ntfs.sys uses only 32 bits lcn.
  27. * Default ntfs3 uses 32 bits lcn too.
  28. * ntfs3 built with CONFIG_NTFS3_64BIT_CLUSTER (ntfs3_64) uses 64 bits per lcn.
  29. *
  30. *
  31. * ntfs limits, cluster size is 4K (2^12)
  32. * -----------------------------------------------------------------------------
  33. * | Volume size | Clusters | ntfs.sys | ntfs3 | ntfs3_64 | mkntfs | chkdsk |
  34. * -----------------------------------------------------------------------------
  35. * | < 16T, 2^44 | < 2^32 | yes | yes | yes | yes | yes |
  36. * | > 16T, 2^44 | > 2^32 | no | no | yes | yes | yes |
  37. * ----------------------------------------------------------|------------------
  38. *
  39. * To mount large volumes as ntfs one should use large cluster size (up to 2M)
  40. * The maximum volume size in this case is 2^32 * 2^21 = 2^53 = 8P
  41. *
  42. * ntfs limits, cluster size is 2M (2^21)
  43. * -----------------------------------------------------------------------------
  44. * | < 8P, 2^53 | < 2^32 | yes | yes | yes | yes | yes |
  45. * | > 8P, 2^53 | > 2^32 | no | no | yes | yes | yes |
  46. * ----------------------------------------------------------|------------------
  47. *
  48. */
  49. #include <linux/blkdev.h>
  50. #include <linux/buffer_head.h>
  51. #include <linux/exportfs.h>
  52. #include <linux/fs.h>
  53. #include <linux/fs_context.h>
  54. #include <linux/fs_parser.h>
  55. #include <linux/log2.h>
  56. #include <linux/minmax.h>
  57. #include <linux/module.h>
  58. #include <linux/nls.h>
  59. #include <linux/proc_fs.h>
  60. #include <linux/seq_file.h>
  61. #include <linux/statfs.h>
  62. #include "debug.h"
  63. #include "ntfs.h"
  64. #include "ntfs_fs.h"
  65. #ifdef CONFIG_NTFS3_LZX_XPRESS
  66. #include "lib/lib.h"
  67. #endif
  68. #ifdef CONFIG_PRINTK
  69. /*
  70. * ntfs_printk - Trace warnings/notices/errors.
  71. *
  72. * Thanks Joe Perches <joe@perches.com> for implementation
  73. */
  74. void ntfs_printk(const struct super_block *sb, const char *fmt, ...)
  75. {
  76. struct va_format vaf;
  77. va_list args;
  78. int level;
  79. struct ntfs_sb_info *sbi = sb->s_fs_info;
  80. /* Should we use different ratelimits for warnings/notices/errors? */
  81. if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
  82. return;
  83. va_start(args, fmt);
  84. level = printk_get_level(fmt);
  85. vaf.fmt = printk_skip_level(fmt);
  86. vaf.va = &args;
  87. printk("%c%cntfs3(%s): %pV\n", KERN_SOH_ASCII, level, sb->s_id, &vaf);
  88. va_end(args);
  89. }
  90. static char s_name_buf[512];
  91. static atomic_t s_name_buf_cnt = ATOMIC_INIT(1); // 1 means 'free s_name_buf'.
  92. /*
  93. * ntfs_inode_printk
  94. *
  95. * Print warnings/notices/errors about inode using name or inode number.
  96. */
  97. void ntfs_inode_printk(struct inode *inode, const char *fmt, ...)
  98. {
  99. struct super_block *sb = inode->i_sb;
  100. struct ntfs_sb_info *sbi = sb->s_fs_info;
  101. char *name;
  102. va_list args;
  103. struct va_format vaf;
  104. int level;
  105. if (!___ratelimit(&sbi->msg_ratelimit, "ntfs3"))
  106. return;
  107. /* Use static allocated buffer, if possible. */
  108. name = atomic_dec_and_test(&s_name_buf_cnt) ?
  109. s_name_buf :
  110. kmalloc(sizeof(s_name_buf), GFP_NOFS);
  111. if (name) {
  112. struct dentry *de = d_find_alias(inode);
  113. if (de) {
  114. int len;
  115. spin_lock(&de->d_lock);
  116. len = snprintf(name, sizeof(s_name_buf), " \"%s\"",
  117. de->d_name.name);
  118. spin_unlock(&de->d_lock);
  119. if (len <= 0)
  120. name[0] = 0;
  121. else if (len >= sizeof(s_name_buf))
  122. name[sizeof(s_name_buf) - 1] = 0;
  123. } else {
  124. name[0] = 0;
  125. }
  126. dput(de); /* Cocci warns if placed in branch "if (de)" */
  127. }
  128. va_start(args, fmt);
  129. level = printk_get_level(fmt);
  130. vaf.fmt = printk_skip_level(fmt);
  131. vaf.va = &args;
  132. printk("%c%cntfs3(%s): ino=%lx,%s %pV\n", KERN_SOH_ASCII, level,
  133. sb->s_id, inode->i_ino, name ? name : "", &vaf);
  134. va_end(args);
  135. atomic_inc(&s_name_buf_cnt);
  136. if (name != s_name_buf)
  137. kfree(name);
  138. }
  139. #endif
  140. /*
  141. * Shared memory struct.
  142. *
  143. * On-disk ntfs's upcase table is created by ntfs formatter.
  144. * 'upcase' table is 128K bytes of memory.
  145. * We should read it into memory when mounting.
  146. * Several ntfs volumes likely use the same 'upcase' table.
  147. * It is good idea to share in-memory 'upcase' table between different volumes.
  148. * Unfortunately winxp/vista/win7 use different upcase tables.
  149. */
  150. static DEFINE_SPINLOCK(s_shared_lock);
  151. static struct {
  152. void *ptr;
  153. u32 len;
  154. int cnt;
  155. } s_shared[8];
  156. /*
  157. * ntfs_set_shared
  158. *
  159. * Return:
  160. * * @ptr - If pointer was saved in shared memory.
  161. * * NULL - If pointer was not shared.
  162. */
  163. void *ntfs_set_shared(void *ptr, u32 bytes)
  164. {
  165. void *ret = NULL;
  166. int i, j = -1;
  167. spin_lock(&s_shared_lock);
  168. for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
  169. if (!s_shared[i].cnt) {
  170. j = i;
  171. } else if (bytes == s_shared[i].len &&
  172. !memcmp(s_shared[i].ptr, ptr, bytes)) {
  173. s_shared[i].cnt += 1;
  174. ret = s_shared[i].ptr;
  175. break;
  176. }
  177. }
  178. if (!ret && j != -1) {
  179. s_shared[j].ptr = ptr;
  180. s_shared[j].len = bytes;
  181. s_shared[j].cnt = 1;
  182. ret = ptr;
  183. }
  184. spin_unlock(&s_shared_lock);
  185. return ret;
  186. }
  187. /*
  188. * ntfs_put_shared
  189. *
  190. * Return:
  191. * * @ptr - If pointer is not shared anymore.
  192. * * NULL - If pointer is still shared.
  193. */
  194. void *ntfs_put_shared(void *ptr)
  195. {
  196. void *ret = ptr;
  197. int i;
  198. spin_lock(&s_shared_lock);
  199. for (i = 0; i < ARRAY_SIZE(s_shared); i++) {
  200. if (s_shared[i].cnt && s_shared[i].ptr == ptr) {
  201. if (--s_shared[i].cnt)
  202. ret = NULL;
  203. break;
  204. }
  205. }
  206. spin_unlock(&s_shared_lock);
  207. return ret;
  208. }
  209. static inline void put_mount_options(struct ntfs_mount_options *options)
  210. {
  211. kfree(options->nls_name);
  212. unload_nls(options->nls);
  213. kfree(options);
  214. }
  215. enum Opt {
  216. Opt_uid,
  217. Opt_gid,
  218. Opt_umask,
  219. Opt_dmask,
  220. Opt_fmask,
  221. Opt_immutable,
  222. Opt_discard,
  223. Opt_force,
  224. Opt_sparse,
  225. Opt_nohidden,
  226. Opt_hide_dot_files,
  227. Opt_windows_names,
  228. Opt_showmeta,
  229. Opt_acl,
  230. Opt_iocharset,
  231. Opt_prealloc,
  232. Opt_nocase,
  233. Opt_err,
  234. };
  235. // clang-format off
  236. static const struct fs_parameter_spec ntfs_fs_parameters[] = {
  237. fsparam_uid("uid", Opt_uid),
  238. fsparam_gid("gid", Opt_gid),
  239. fsparam_u32oct("umask", Opt_umask),
  240. fsparam_u32oct("dmask", Opt_dmask),
  241. fsparam_u32oct("fmask", Opt_fmask),
  242. fsparam_flag("sys_immutable", Opt_immutable),
  243. fsparam_flag("discard", Opt_discard),
  244. fsparam_flag("force", Opt_force),
  245. fsparam_flag("sparse", Opt_sparse),
  246. fsparam_flag("nohidden", Opt_nohidden),
  247. fsparam_flag("hide_dot_files", Opt_hide_dot_files),
  248. fsparam_flag("windows_names", Opt_windows_names),
  249. fsparam_flag("showmeta", Opt_showmeta),
  250. fsparam_flag("acl", Opt_acl),
  251. fsparam_string("iocharset", Opt_iocharset),
  252. fsparam_flag("prealloc", Opt_prealloc),
  253. fsparam_flag("nocase", Opt_nocase),
  254. {}
  255. };
  256. // clang-format on
  257. /*
  258. * Load nls table or if @nls is utf8 then return NULL.
  259. *
  260. * It is good idea to use here "const char *nls".
  261. * But load_nls accepts "char*".
  262. */
  263. static struct nls_table *ntfs_load_nls(char *nls)
  264. {
  265. struct nls_table *ret;
  266. if (!nls)
  267. nls = CONFIG_NLS_DEFAULT;
  268. if (strcmp(nls, "utf8") == 0)
  269. return NULL;
  270. if (strcmp(nls, CONFIG_NLS_DEFAULT) == 0)
  271. return load_nls_default();
  272. ret = load_nls(nls);
  273. if (ret)
  274. return ret;
  275. return ERR_PTR(-EINVAL);
  276. }
  277. static int ntfs_fs_parse_param(struct fs_context *fc,
  278. struct fs_parameter *param)
  279. {
  280. struct ntfs_mount_options *opts = fc->fs_private;
  281. struct fs_parse_result result;
  282. int opt;
  283. opt = fs_parse(fc, ntfs_fs_parameters, param, &result);
  284. if (opt < 0)
  285. return opt;
  286. switch (opt) {
  287. case Opt_uid:
  288. opts->fs_uid = result.uid;
  289. break;
  290. case Opt_gid:
  291. opts->fs_gid = result.gid;
  292. break;
  293. case Opt_umask:
  294. if (result.uint_32 & ~07777)
  295. return invalf(fc, "ntfs3: Invalid value for umask.");
  296. opts->fs_fmask_inv = ~result.uint_32;
  297. opts->fs_dmask_inv = ~result.uint_32;
  298. opts->fmask = 1;
  299. opts->dmask = 1;
  300. break;
  301. case Opt_dmask:
  302. if (result.uint_32 & ~07777)
  303. return invalf(fc, "ntfs3: Invalid value for dmask.");
  304. opts->fs_dmask_inv = ~result.uint_32;
  305. opts->dmask = 1;
  306. break;
  307. case Opt_fmask:
  308. if (result.uint_32 & ~07777)
  309. return invalf(fc, "ntfs3: Invalid value for fmask.");
  310. opts->fs_fmask_inv = ~result.uint_32;
  311. opts->fmask = 1;
  312. break;
  313. case Opt_immutable:
  314. opts->sys_immutable = 1;
  315. break;
  316. case Opt_discard:
  317. opts->discard = 1;
  318. break;
  319. case Opt_force:
  320. opts->force = 1;
  321. break;
  322. case Opt_sparse:
  323. opts->sparse = 1;
  324. break;
  325. case Opt_nohidden:
  326. opts->nohidden = 1;
  327. break;
  328. case Opt_hide_dot_files:
  329. opts->hide_dot_files = 1;
  330. break;
  331. case Opt_windows_names:
  332. opts->windows_names = 1;
  333. break;
  334. case Opt_showmeta:
  335. opts->showmeta = 1;
  336. break;
  337. case Opt_acl:
  338. if (!result.negated)
  339. #ifdef CONFIG_NTFS3_FS_POSIX_ACL
  340. fc->sb_flags |= SB_POSIXACL;
  341. #else
  342. return invalf(
  343. fc, "ntfs3: Support for ACL not compiled in!");
  344. #endif
  345. else
  346. fc->sb_flags &= ~SB_POSIXACL;
  347. break;
  348. case Opt_iocharset:
  349. kfree(opts->nls_name);
  350. opts->nls_name = param->string;
  351. param->string = NULL;
  352. break;
  353. case Opt_prealloc:
  354. opts->prealloc = 1;
  355. break;
  356. case Opt_nocase:
  357. opts->nocase = 1;
  358. break;
  359. default:
  360. /* Should not be here unless we forget add case. */
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static int ntfs_fs_reconfigure(struct fs_context *fc)
  366. {
  367. struct super_block *sb = fc->root->d_sb;
  368. struct ntfs_sb_info *sbi = sb->s_fs_info;
  369. struct ntfs_mount_options *new_opts = fc->fs_private;
  370. int ro_rw;
  371. /* If ntfs3 is used as legacy ntfs enforce read-only mode. */
  372. if (is_legacy_ntfs(sb)) {
  373. fc->sb_flags |= SB_RDONLY;
  374. goto out;
  375. }
  376. ro_rw = sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY);
  377. if (ro_rw && (sbi->flags & NTFS_FLAGS_NEED_REPLAY)) {
  378. errorf(fc,
  379. "ntfs3: Couldn't remount rw because journal is not replayed. Please umount/remount instead\n");
  380. return -EINVAL;
  381. }
  382. new_opts->nls = ntfs_load_nls(new_opts->nls_name);
  383. if (IS_ERR(new_opts->nls)) {
  384. new_opts->nls = NULL;
  385. errorf(fc, "ntfs3: Cannot load iocharset %s",
  386. new_opts->nls_name);
  387. return -EINVAL;
  388. }
  389. if (new_opts->nls != sbi->options->nls)
  390. return invalf(
  391. fc,
  392. "ntfs3: Cannot use different iocharset when remounting!");
  393. if (ro_rw && (sbi->volume.flags & VOLUME_FLAG_DIRTY) &&
  394. !new_opts->force) {
  395. errorf(fc,
  396. "ntfs3: Volume is dirty and \"force\" flag is not set!");
  397. return -EINVAL;
  398. }
  399. out:
  400. sync_filesystem(sb);
  401. swap(sbi->options, fc->fs_private);
  402. return 0;
  403. }
  404. #ifdef CONFIG_PROC_FS
  405. static struct proc_dir_entry *proc_info_root;
  406. /*
  407. * ntfs3_volinfo:
  408. *
  409. * The content of /proc/fs/ntfs3/<dev>/volinfo
  410. *
  411. * ntfs3.1
  412. * cluster size
  413. * number of clusters
  414. * total number of mft records
  415. * number of used mft records ~= number of files + folders
  416. * real state of ntfs "dirty"/"clean"
  417. * current state of ntfs "dirty"/"clean"
  418. */
  419. static int ntfs3_volinfo(struct seq_file *m, void *o)
  420. {
  421. struct super_block *sb = m->private;
  422. struct ntfs_sb_info *sbi = sb->s_fs_info;
  423. seq_printf(m, "ntfs%d.%d\n%u\n%zu\n%zu\n%zu\n%s\n%s\n",
  424. sbi->volume.major_ver, sbi->volume.minor_ver,
  425. sbi->cluster_size, sbi->used.bitmap.nbits,
  426. sbi->mft.bitmap.nbits,
  427. sbi->mft.bitmap.nbits - wnd_zeroes(&sbi->mft.bitmap),
  428. sbi->volume.real_dirty ? "dirty" : "clean",
  429. (sbi->volume.flags & VOLUME_FLAG_DIRTY) ? "dirty" : "clean");
  430. return 0;
  431. }
  432. static int ntfs3_volinfo_open(struct inode *inode, struct file *file)
  433. {
  434. return single_open(file, ntfs3_volinfo, pde_data(inode));
  435. }
  436. /* read /proc/fs/ntfs3/<dev>/label */
  437. static int ntfs3_label_show(struct seq_file *m, void *o)
  438. {
  439. struct super_block *sb = m->private;
  440. struct ntfs_sb_info *sbi = sb->s_fs_info;
  441. seq_printf(m, "%s\n", sbi->volume.label);
  442. return 0;
  443. }
  444. /* write /proc/fs/ntfs3/<dev>/label */
  445. static ssize_t ntfs3_label_write(struct file *file, const char __user *buffer,
  446. size_t count, loff_t *ppos)
  447. {
  448. int err;
  449. struct super_block *sb = pde_data(file_inode(file));
  450. ssize_t ret = count;
  451. u8 *label;
  452. if (sb_rdonly(sb))
  453. return -EROFS;
  454. label = kmalloc(count, GFP_NOFS);
  455. if (!label)
  456. return -ENOMEM;
  457. if (copy_from_user(label, buffer, ret)) {
  458. ret = -EFAULT;
  459. goto out;
  460. }
  461. while (ret > 0 && label[ret - 1] == '\n')
  462. ret -= 1;
  463. err = ntfs_set_label(sb->s_fs_info, label, ret);
  464. if (err < 0) {
  465. ntfs_err(sb, "failed (%d) to write label", err);
  466. ret = err;
  467. goto out;
  468. }
  469. *ppos += count;
  470. ret = count;
  471. out:
  472. kfree(label);
  473. return ret;
  474. }
  475. static int ntfs3_label_open(struct inode *inode, struct file *file)
  476. {
  477. return single_open(file, ntfs3_label_show, pde_data(inode));
  478. }
  479. static const struct proc_ops ntfs3_volinfo_fops = {
  480. .proc_read = seq_read,
  481. .proc_lseek = seq_lseek,
  482. .proc_release = single_release,
  483. .proc_open = ntfs3_volinfo_open,
  484. };
  485. static const struct proc_ops ntfs3_label_fops = {
  486. .proc_read = seq_read,
  487. .proc_lseek = seq_lseek,
  488. .proc_release = single_release,
  489. .proc_open = ntfs3_label_open,
  490. .proc_write = ntfs3_label_write,
  491. };
  492. #endif
  493. static struct kmem_cache *ntfs_inode_cachep;
  494. static struct inode *ntfs_alloc_inode(struct super_block *sb)
  495. {
  496. struct ntfs_inode *ni = alloc_inode_sb(sb, ntfs_inode_cachep, GFP_NOFS);
  497. if (!ni)
  498. return NULL;
  499. memset(ni, 0, offsetof(struct ntfs_inode, vfs_inode));
  500. mutex_init(&ni->ni_lock);
  501. return &ni->vfs_inode;
  502. }
  503. static void ntfs_free_inode(struct inode *inode)
  504. {
  505. struct ntfs_inode *ni = ntfs_i(inode);
  506. mutex_destroy(&ni->ni_lock);
  507. kmem_cache_free(ntfs_inode_cachep, ni);
  508. }
  509. static void init_once(void *foo)
  510. {
  511. struct ntfs_inode *ni = foo;
  512. inode_init_once(&ni->vfs_inode);
  513. }
  514. /*
  515. * Noinline to reduce binary size.
  516. */
  517. static noinline void ntfs3_put_sbi(struct ntfs_sb_info *sbi)
  518. {
  519. wnd_close(&sbi->mft.bitmap);
  520. wnd_close(&sbi->used.bitmap);
  521. if (sbi->mft.ni) {
  522. iput(&sbi->mft.ni->vfs_inode);
  523. sbi->mft.ni = NULL;
  524. }
  525. if (sbi->security.ni) {
  526. iput(&sbi->security.ni->vfs_inode);
  527. sbi->security.ni = NULL;
  528. }
  529. if (sbi->reparse.ni) {
  530. iput(&sbi->reparse.ni->vfs_inode);
  531. sbi->reparse.ni = NULL;
  532. }
  533. if (sbi->objid.ni) {
  534. iput(&sbi->objid.ni->vfs_inode);
  535. sbi->objid.ni = NULL;
  536. }
  537. if (sbi->volume.ni) {
  538. iput(&sbi->volume.ni->vfs_inode);
  539. sbi->volume.ni = NULL;
  540. }
  541. ntfs_update_mftmirr(sbi, 0);
  542. indx_clear(&sbi->security.index_sii);
  543. indx_clear(&sbi->security.index_sdh);
  544. indx_clear(&sbi->reparse.index_r);
  545. indx_clear(&sbi->objid.index_o);
  546. }
  547. static void ntfs3_free_sbi(struct ntfs_sb_info *sbi)
  548. {
  549. kfree(sbi->new_rec);
  550. kvfree(ntfs_put_shared(sbi->upcase));
  551. kvfree(sbi->def_table);
  552. kfree(sbi->compress.lznt);
  553. #ifdef CONFIG_NTFS3_LZX_XPRESS
  554. xpress_free_decompressor(sbi->compress.xpress);
  555. lzx_free_decompressor(sbi->compress.lzx);
  556. #endif
  557. kfree(sbi);
  558. }
  559. static void ntfs_put_super(struct super_block *sb)
  560. {
  561. struct ntfs_sb_info *sbi = sb->s_fs_info;
  562. #ifdef CONFIG_PROC_FS
  563. // Remove /proc/fs/ntfs3/..
  564. if (sbi->procdir) {
  565. remove_proc_entry("label", sbi->procdir);
  566. remove_proc_entry("volinfo", sbi->procdir);
  567. remove_proc_entry(sb->s_id, proc_info_root);
  568. sbi->procdir = NULL;
  569. }
  570. #endif
  571. /* Mark rw ntfs as clear, if possible. */
  572. ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
  573. ntfs3_put_sbi(sbi);
  574. }
  575. static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  576. {
  577. struct super_block *sb = dentry->d_sb;
  578. struct ntfs_sb_info *sbi = sb->s_fs_info;
  579. struct wnd_bitmap *wnd = &sbi->used.bitmap;
  580. buf->f_type = sb->s_magic;
  581. buf->f_bsize = sbi->cluster_size;
  582. buf->f_blocks = wnd->nbits;
  583. buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd);
  584. buf->f_fsid.val[0] = sbi->volume.ser_num;
  585. buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32);
  586. buf->f_namelen = NTFS_NAME_LEN;
  587. return 0;
  588. }
  589. static int ntfs_show_options(struct seq_file *m, struct dentry *root)
  590. {
  591. struct super_block *sb = root->d_sb;
  592. struct ntfs_sb_info *sbi = sb->s_fs_info;
  593. struct ntfs_mount_options *opts = sbi->options;
  594. struct user_namespace *user_ns = seq_user_ns(m);
  595. seq_printf(m, ",uid=%u", from_kuid_munged(user_ns, opts->fs_uid));
  596. seq_printf(m, ",gid=%u", from_kgid_munged(user_ns, opts->fs_gid));
  597. if (opts->dmask)
  598. seq_printf(m, ",dmask=%04o", opts->fs_dmask_inv ^ 0xffff);
  599. if (opts->fmask)
  600. seq_printf(m, ",fmask=%04o", opts->fs_fmask_inv ^ 0xffff);
  601. if (opts->sys_immutable)
  602. seq_puts(m, ",sys_immutable");
  603. if (opts->discard)
  604. seq_puts(m, ",discard");
  605. if (opts->force)
  606. seq_puts(m, ",force");
  607. if (opts->sparse)
  608. seq_puts(m, ",sparse");
  609. if (opts->nohidden)
  610. seq_puts(m, ",nohidden");
  611. if (opts->hide_dot_files)
  612. seq_puts(m, ",hide_dot_files");
  613. if (opts->windows_names)
  614. seq_puts(m, ",windows_names");
  615. if (opts->showmeta)
  616. seq_puts(m, ",showmeta");
  617. if (sb->s_flags & SB_POSIXACL)
  618. seq_puts(m, ",acl");
  619. if (opts->nls)
  620. seq_printf(m, ",iocharset=%s", opts->nls->charset);
  621. else
  622. seq_puts(m, ",iocharset=utf8");
  623. if (opts->prealloc)
  624. seq_puts(m, ",prealloc");
  625. if (opts->nocase)
  626. seq_puts(m, ",nocase");
  627. return 0;
  628. }
  629. /*
  630. * ntfs_shutdown - super_operations::shutdown
  631. */
  632. static void ntfs_shutdown(struct super_block *sb)
  633. {
  634. set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags);
  635. }
  636. /*
  637. * ntfs_sync_fs - super_operations::sync_fs
  638. */
  639. static int ntfs_sync_fs(struct super_block *sb, int wait)
  640. {
  641. int err = 0, err2;
  642. struct ntfs_sb_info *sbi = sb->s_fs_info;
  643. struct ntfs_inode *ni;
  644. struct inode *inode;
  645. if (unlikely(ntfs3_forced_shutdown(sb)))
  646. return -EIO;
  647. ni = sbi->security.ni;
  648. if (ni) {
  649. inode = &ni->vfs_inode;
  650. err2 = _ni_write_inode(inode, wait);
  651. if (err2 && !err)
  652. err = err2;
  653. }
  654. ni = sbi->objid.ni;
  655. if (ni) {
  656. inode = &ni->vfs_inode;
  657. err2 = _ni_write_inode(inode, wait);
  658. if (err2 && !err)
  659. err = err2;
  660. }
  661. ni = sbi->reparse.ni;
  662. if (ni) {
  663. inode = &ni->vfs_inode;
  664. err2 = _ni_write_inode(inode, wait);
  665. if (err2 && !err)
  666. err = err2;
  667. }
  668. if (!err)
  669. ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
  670. ntfs_update_mftmirr(sbi, wait);
  671. return err;
  672. }
  673. static const struct super_operations ntfs_sops = {
  674. .alloc_inode = ntfs_alloc_inode,
  675. .free_inode = ntfs_free_inode,
  676. .evict_inode = ntfs_evict_inode,
  677. .put_super = ntfs_put_super,
  678. .statfs = ntfs_statfs,
  679. .show_options = ntfs_show_options,
  680. .shutdown = ntfs_shutdown,
  681. .sync_fs = ntfs_sync_fs,
  682. .write_inode = ntfs3_write_inode,
  683. };
  684. static struct inode *ntfs_export_get_inode(struct super_block *sb, u64 ino,
  685. u32 generation)
  686. {
  687. struct MFT_REF ref;
  688. struct inode *inode;
  689. ref.low = cpu_to_le32(ino);
  690. #ifdef CONFIG_NTFS3_64BIT_CLUSTER
  691. ref.high = cpu_to_le16(ino >> 32);
  692. #else
  693. ref.high = 0;
  694. #endif
  695. ref.seq = cpu_to_le16(generation);
  696. inode = ntfs_iget5(sb, &ref, NULL);
  697. if (!IS_ERR(inode) && is_bad_inode(inode)) {
  698. iput(inode);
  699. inode = ERR_PTR(-ESTALE);
  700. }
  701. return inode;
  702. }
  703. static struct dentry *ntfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  704. int fh_len, int fh_type)
  705. {
  706. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  707. ntfs_export_get_inode);
  708. }
  709. static struct dentry *ntfs_fh_to_parent(struct super_block *sb, struct fid *fid,
  710. int fh_len, int fh_type)
  711. {
  712. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  713. ntfs_export_get_inode);
  714. }
  715. /* TODO: == ntfs_sync_inode */
  716. static int ntfs_nfs_commit_metadata(struct inode *inode)
  717. {
  718. return _ni_write_inode(inode, 1);
  719. }
  720. static const struct export_operations ntfs_export_ops = {
  721. .encode_fh = generic_encode_ino32_fh,
  722. .fh_to_dentry = ntfs_fh_to_dentry,
  723. .fh_to_parent = ntfs_fh_to_parent,
  724. .get_parent = ntfs3_get_parent,
  725. .commit_metadata = ntfs_nfs_commit_metadata,
  726. };
  727. /*
  728. * format_size_gb - Return Gb,Mb to print with "%u.%02u Gb".
  729. */
  730. static u32 format_size_gb(const u64 bytes, u32 *mb)
  731. {
  732. /* Do simple right 30 bit shift of 64 bit value. */
  733. u64 kbytes = bytes >> 10;
  734. u32 kbytes32 = kbytes;
  735. *mb = (100 * (kbytes32 & 0xfffff) + 0x7ffff) >> 20;
  736. if (*mb >= 100)
  737. *mb = 99;
  738. return (kbytes32 >> 20) | (((u32)(kbytes >> 32)) << 12);
  739. }
  740. static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
  741. {
  742. if (boot->sectors_per_clusters <= 0x80)
  743. return boot->sectors_per_clusters;
  744. if (boot->sectors_per_clusters >= 0xf4) /* limit shift to 2MB max */
  745. return 1U << (-(s8)boot->sectors_per_clusters);
  746. return -EINVAL;
  747. }
  748. /*
  749. * ntfs_init_from_boot - Init internal info from on-disk boot sector.
  750. *
  751. * NTFS mount begins from boot - special formatted 512 bytes.
  752. * There are two boots: the first and the last 512 bytes of volume.
  753. * The content of boot is not changed during ntfs life.
  754. *
  755. * NOTE: ntfs.sys checks only first (primary) boot.
  756. * chkdsk checks both boots.
  757. */
  758. static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
  759. u64 dev_size, struct NTFS_BOOT **boot2)
  760. {
  761. struct ntfs_sb_info *sbi = sb->s_fs_info;
  762. int err;
  763. u32 mb, gb, boot_sector_size, sct_per_clst, record_size;
  764. u64 sectors, clusters, mlcn, mlcn2, dev_size0;
  765. struct NTFS_BOOT *boot;
  766. struct buffer_head *bh;
  767. struct MFT_REC *rec;
  768. u16 fn, ao;
  769. u8 cluster_bits;
  770. u32 boot_off = 0;
  771. sector_t boot_block = 0;
  772. const char *hint = "Primary boot";
  773. /* Save original dev_size. Used with alternative boot. */
  774. dev_size0 = dev_size;
  775. sbi->volume.blocks = dev_size >> PAGE_SHIFT;
  776. read_boot:
  777. bh = ntfs_bread(sb, boot_block);
  778. if (!bh)
  779. return boot_block ? -EINVAL : -EIO;
  780. err = -EINVAL;
  781. /* Corrupted image; do not read OOB */
  782. if (bh->b_size - sizeof(*boot) < boot_off)
  783. goto out;
  784. boot = (struct NTFS_BOOT *)Add2Ptr(bh->b_data, boot_off);
  785. if (memcmp(boot->system_id, "NTFS ", sizeof("NTFS ") - 1)) {
  786. ntfs_err(sb, "%s signature is not NTFS.", hint);
  787. goto out;
  788. }
  789. /* 0x55AA is not mandaroty. Thanks Maxim Suhanov*/
  790. /*if (0x55 != boot->boot_magic[0] || 0xAA != boot->boot_magic[1])
  791. * goto out;
  792. */
  793. boot_sector_size = ((u32)boot->bytes_per_sector[1] << 8) |
  794. boot->bytes_per_sector[0];
  795. if (boot_sector_size < SECTOR_SIZE ||
  796. !is_power_of_2(boot_sector_size)) {
  797. ntfs_err(sb, "%s: invalid bytes per sector %u.", hint,
  798. boot_sector_size);
  799. goto out;
  800. }
  801. /* cluster size: 512, 1K, 2K, 4K, ... 2M */
  802. sct_per_clst = true_sectors_per_clst(boot);
  803. if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) {
  804. ntfs_err(sb, "%s: invalid sectors per cluster %u.", hint,
  805. sct_per_clst);
  806. goto out;
  807. }
  808. sbi->cluster_size = boot_sector_size * sct_per_clst;
  809. sbi->cluster_bits = cluster_bits = blksize_bits(sbi->cluster_size);
  810. sbi->cluster_mask = sbi->cluster_size - 1;
  811. sbi->cluster_mask_inv = ~(u64)sbi->cluster_mask;
  812. mlcn = le64_to_cpu(boot->mft_clst);
  813. mlcn2 = le64_to_cpu(boot->mft2_clst);
  814. sectors = le64_to_cpu(boot->sectors_per_volume);
  815. if (mlcn * sct_per_clst >= sectors || mlcn2 * sct_per_clst >= sectors) {
  816. ntfs_err(
  817. sb,
  818. "%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
  819. hint, mlcn, mlcn2, sectors);
  820. goto out;
  821. }
  822. if (boot->record_size >= 0) {
  823. record_size = (u32)boot->record_size << cluster_bits;
  824. } else if (-boot->record_size <= MAXIMUM_SHIFT_BYTES_PER_MFT) {
  825. record_size = 1u << (-boot->record_size);
  826. } else {
  827. ntfs_err(sb, "%s: invalid record size %d.", hint,
  828. boot->record_size);
  829. goto out;
  830. }
  831. sbi->record_size = record_size;
  832. sbi->record_bits = blksize_bits(record_size);
  833. sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
  834. /* Check MFT record size. */
  835. if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) {
  836. ntfs_err(sb, "%s: invalid bytes per MFT record %u (%d).", hint,
  837. record_size, boot->record_size);
  838. goto out;
  839. }
  840. if (record_size > MAXIMUM_BYTES_PER_MFT) {
  841. ntfs_err(sb, "Unsupported bytes per MFT record %u.",
  842. record_size);
  843. goto out;
  844. }
  845. if (boot->index_size >= 0) {
  846. sbi->index_size = (u32)boot->index_size << cluster_bits;
  847. } else if (-boot->index_size <= MAXIMUM_SHIFT_BYTES_PER_INDEX) {
  848. sbi->index_size = 1u << (-boot->index_size);
  849. } else {
  850. ntfs_err(sb, "%s: invalid index size %d.", hint,
  851. boot->index_size);
  852. goto out;
  853. }
  854. /* Check index record size. */
  855. if (sbi->index_size < SECTOR_SIZE || !is_power_of_2(sbi->index_size)) {
  856. ntfs_err(sb, "%s: invalid bytes per index %u(%d).", hint,
  857. sbi->index_size, boot->index_size);
  858. goto out;
  859. }
  860. if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) {
  861. ntfs_err(sb, "%s: unsupported bytes per index %u.", hint,
  862. sbi->index_size);
  863. goto out;
  864. }
  865. sbi->volume.size = sectors * boot_sector_size;
  866. gb = format_size_gb(sbi->volume.size + boot_sector_size, &mb);
  867. /*
  868. * - Volume formatted and mounted with the same sector size.
  869. * - Volume formatted 4K and mounted as 512.
  870. * - Volume formatted 512 and mounted as 4K.
  871. */
  872. if (boot_sector_size != sector_size) {
  873. ntfs_warn(
  874. sb,
  875. "Different NTFS sector size (%u) and media sector size (%u).",
  876. boot_sector_size, sector_size);
  877. dev_size += sector_size - 1;
  878. }
  879. sbi->mft.lbo = mlcn << cluster_bits;
  880. sbi->mft.lbo2 = mlcn2 << cluster_bits;
  881. /* Compare boot's cluster and sector. */
  882. if (sbi->cluster_size < boot_sector_size) {
  883. ntfs_err(sb, "%s: invalid bytes per cluster (%u).", hint,
  884. sbi->cluster_size);
  885. goto out;
  886. }
  887. /* Compare boot's cluster and media sector. */
  888. if (sbi->cluster_size < sector_size) {
  889. /* No way to use ntfs_get_block in this case. */
  890. ntfs_err(
  891. sb,
  892. "Failed to mount 'cause NTFS's cluster size (%u) is less than media sector size (%u).",
  893. sbi->cluster_size, sector_size);
  894. goto out;
  895. }
  896. sbi->max_bytes_per_attr =
  897. record_size - ALIGN(MFTRECORD_FIXUP_OFFSET, 8) -
  898. ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
  899. ALIGN(sizeof(enum ATTR_TYPE), 8);
  900. sbi->volume.ser_num = le64_to_cpu(boot->serial_num);
  901. /* Warning if RAW volume. */
  902. if (dev_size < sbi->volume.size + boot_sector_size) {
  903. u32 mb0, gb0;
  904. gb0 = format_size_gb(dev_size, &mb0);
  905. ntfs_warn(
  906. sb,
  907. "RAW NTFS volume: Filesystem size %u.%02u Gb > volume size %u.%02u Gb. Mount in read-only.",
  908. gb, mb, gb0, mb0);
  909. sb->s_flags |= SB_RDONLY;
  910. }
  911. clusters = sbi->volume.size >> cluster_bits;
  912. #ifndef CONFIG_NTFS3_64BIT_CLUSTER
  913. /* 32 bits per cluster. */
  914. if (clusters >> 32) {
  915. ntfs_notice(
  916. sb,
  917. "NTFS %u.%02u Gb is too big to use 32 bits per cluster.",
  918. gb, mb);
  919. goto out;
  920. }
  921. #elif BITS_PER_LONG < 64
  922. #error "CONFIG_NTFS3_64BIT_CLUSTER incompatible in 32 bit OS"
  923. #endif
  924. sbi->used.bitmap.nbits = clusters;
  925. rec = kzalloc(record_size, GFP_NOFS);
  926. if (!rec) {
  927. err = -ENOMEM;
  928. goto out;
  929. }
  930. sbi->new_rec = rec;
  931. rec->rhdr.sign = NTFS_FILE_SIGNATURE;
  932. rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET);
  933. fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
  934. rec->rhdr.fix_num = cpu_to_le16(fn);
  935. ao = ALIGN(MFTRECORD_FIXUP_OFFSET + sizeof(short) * fn, 8);
  936. rec->attr_off = cpu_to_le16(ao);
  937. rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
  938. rec->total = cpu_to_le32(sbi->record_size);
  939. ((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;
  940. sb_set_blocksize(sb, min_t(u32, sbi->cluster_size, PAGE_SIZE));
  941. sbi->block_mask = sb->s_blocksize - 1;
  942. sbi->blocks_per_cluster = sbi->cluster_size >> sb->s_blocksize_bits;
  943. sbi->volume.blocks = sbi->volume.size >> sb->s_blocksize_bits;
  944. /* Maximum size for normal files. */
  945. sbi->maxbytes = (clusters << cluster_bits) - 1;
  946. #ifdef CONFIG_NTFS3_64BIT_CLUSTER
  947. if (clusters >= (1ull << (64 - cluster_bits)))
  948. sbi->maxbytes = -1;
  949. sbi->maxbytes_sparse = -1;
  950. sb->s_maxbytes = MAX_LFS_FILESIZE;
  951. #else
  952. /* Maximum size for sparse file. */
  953. sbi->maxbytes_sparse = (1ull << (cluster_bits + 32)) - 1;
  954. sb->s_maxbytes = 0xFFFFFFFFull << cluster_bits;
  955. #endif
  956. /*
  957. * Compute the MFT zone at two steps.
  958. * It would be nice if we are able to allocate 1/8 of
  959. * total clusters for MFT but not more then 512 MB.
  960. */
  961. sbi->zone_max = min_t(CLST, 0x20000000 >> cluster_bits, clusters >> 3);
  962. err = 0;
  963. if (bh->b_blocknr && !sb_rdonly(sb)) {
  964. /*
  965. * Alternative boot is ok but primary is not ok.
  966. * Do not update primary boot here 'cause it may be faked boot.
  967. * Let ntfs to be mounted and update boot later.
  968. */
  969. *boot2 = kmemdup(boot, sizeof(*boot), GFP_NOFS | __GFP_NOWARN);
  970. }
  971. out:
  972. brelse(bh);
  973. if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) {
  974. u32 block_size = min_t(u32, sector_size, PAGE_SIZE);
  975. u64 lbo = dev_size0 - sizeof(*boot);
  976. boot_block = lbo >> blksize_bits(block_size);
  977. boot_off = lbo & (block_size - 1);
  978. if (boot_block && block_size >= boot_off + sizeof(*boot)) {
  979. /*
  980. * Try alternative boot (last sector)
  981. */
  982. sb_set_blocksize(sb, block_size);
  983. hint = "Alternative boot";
  984. dev_size = dev_size0; /* restore original size. */
  985. goto read_boot;
  986. }
  987. }
  988. return err;
  989. }
  990. /*
  991. * ntfs_fill_super - Try to mount.
  992. */
  993. static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
  994. {
  995. int err;
  996. struct ntfs_sb_info *sbi = sb->s_fs_info;
  997. struct block_device *bdev = sb->s_bdev;
  998. struct ntfs_mount_options *options;
  999. struct inode *inode;
  1000. struct ntfs_inode *ni;
  1001. size_t i, tt, bad_len, bad_frags;
  1002. CLST vcn, lcn, len;
  1003. struct ATTRIB *attr;
  1004. const struct VOLUME_INFO *info;
  1005. u32 done, bytes;
  1006. struct ATTR_DEF_ENTRY *t;
  1007. u16 *shared;
  1008. struct MFT_REF ref;
  1009. bool ro = sb_rdonly(sb);
  1010. struct NTFS_BOOT *boot2 = NULL;
  1011. ref.high = 0;
  1012. sbi->sb = sb;
  1013. sbi->options = options = fc->fs_private;
  1014. fc->fs_private = NULL;
  1015. sb->s_flags |= SB_NODIRATIME;
  1016. sb->s_magic = 0x7366746e; // "ntfs"
  1017. sb->s_op = &ntfs_sops;
  1018. sb->s_export_op = &ntfs_export_ops;
  1019. sb->s_time_gran = NTFS_TIME_GRAN; // 100 nsec
  1020. sb->s_xattr = ntfs_xattr_handlers;
  1021. sb->s_d_op = options->nocase ? &ntfs_dentry_ops : NULL;
  1022. options->nls = ntfs_load_nls(options->nls_name);
  1023. if (IS_ERR(options->nls)) {
  1024. options->nls = NULL;
  1025. errorf(fc, "Cannot load nls %s", options->nls_name);
  1026. err = -EINVAL;
  1027. goto out;
  1028. }
  1029. if (bdev_max_discard_sectors(bdev) && bdev_discard_granularity(bdev)) {
  1030. sbi->discard_granularity = bdev_discard_granularity(bdev);
  1031. sbi->discard_granularity_mask_inv =
  1032. ~(u64)(sbi->discard_granularity - 1);
  1033. }
  1034. /* Parse boot. */
  1035. err = ntfs_init_from_boot(sb, bdev_logical_block_size(bdev),
  1036. bdev_nr_bytes(bdev), &boot2);
  1037. if (err)
  1038. goto out;
  1039. /*
  1040. * Load $Volume. This should be done before $LogFile
  1041. * 'cause 'sbi->volume.ni' is used in 'ntfs_set_state'.
  1042. */
  1043. ref.low = cpu_to_le32(MFT_REC_VOL);
  1044. ref.seq = cpu_to_le16(MFT_REC_VOL);
  1045. inode = ntfs_iget5(sb, &ref, &NAME_VOLUME);
  1046. if (IS_ERR(inode)) {
  1047. err = PTR_ERR(inode);
  1048. ntfs_err(sb, "Failed to load $Volume (%d).", err);
  1049. goto out;
  1050. }
  1051. ni = ntfs_i(inode);
  1052. /* Load and save label (not necessary). */
  1053. attr = ni_find_attr(ni, NULL, NULL, ATTR_LABEL, NULL, 0, NULL, NULL);
  1054. if (!attr) {
  1055. /* It is ok if no ATTR_LABEL */
  1056. } else if (!attr->non_res && !is_attr_ext(attr)) {
  1057. /* $AttrDef allows labels to be up to 128 symbols. */
  1058. err = utf16s_to_utf8s(resident_data(attr),
  1059. le32_to_cpu(attr->res.data_size) >> 1,
  1060. UTF16_LITTLE_ENDIAN, sbi->volume.label,
  1061. sizeof(sbi->volume.label));
  1062. if (err < 0)
  1063. sbi->volume.label[0] = 0;
  1064. } else {
  1065. /* Should we break mounting here? */
  1066. //err = -EINVAL;
  1067. //goto put_inode_out;
  1068. }
  1069. attr = ni_find_attr(ni, attr, NULL, ATTR_VOL_INFO, NULL, 0, NULL, NULL);
  1070. if (!attr || is_attr_ext(attr) ||
  1071. !(info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO))) {
  1072. ntfs_err(sb, "$Volume is corrupted.");
  1073. err = -EINVAL;
  1074. goto put_inode_out;
  1075. }
  1076. sbi->volume.major_ver = info->major_ver;
  1077. sbi->volume.minor_ver = info->minor_ver;
  1078. sbi->volume.flags = info->flags;
  1079. sbi->volume.ni = ni;
  1080. if (info->flags & VOLUME_FLAG_DIRTY) {
  1081. sbi->volume.real_dirty = true;
  1082. ntfs_info(sb, "It is recommened to use chkdsk.");
  1083. }
  1084. /* Load $MFTMirr to estimate recs_mirr. */
  1085. ref.low = cpu_to_le32(MFT_REC_MIRR);
  1086. ref.seq = cpu_to_le16(MFT_REC_MIRR);
  1087. inode = ntfs_iget5(sb, &ref, &NAME_MIRROR);
  1088. if (IS_ERR(inode)) {
  1089. err = PTR_ERR(inode);
  1090. ntfs_err(sb, "Failed to load $MFTMirr (%d).", err);
  1091. goto out;
  1092. }
  1093. sbi->mft.recs_mirr = ntfs_up_cluster(sbi, inode->i_size) >>
  1094. sbi->record_bits;
  1095. iput(inode);
  1096. /* Load LogFile to replay. */
  1097. ref.low = cpu_to_le32(MFT_REC_LOG);
  1098. ref.seq = cpu_to_le16(MFT_REC_LOG);
  1099. inode = ntfs_iget5(sb, &ref, &NAME_LOGFILE);
  1100. if (IS_ERR(inode)) {
  1101. err = PTR_ERR(inode);
  1102. ntfs_err(sb, "Failed to load \x24LogFile (%d).", err);
  1103. goto out;
  1104. }
  1105. ni = ntfs_i(inode);
  1106. err = ntfs_loadlog_and_replay(ni, sbi);
  1107. if (err)
  1108. goto put_inode_out;
  1109. iput(inode);
  1110. if ((sbi->flags & NTFS_FLAGS_NEED_REPLAY) && !ro) {
  1111. ntfs_warn(sb, "failed to replay log file. Can't mount rw!");
  1112. err = -EINVAL;
  1113. goto out;
  1114. }
  1115. if ((sbi->volume.flags & VOLUME_FLAG_DIRTY) && !ro && !options->force) {
  1116. ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!");
  1117. err = -EINVAL;
  1118. goto out;
  1119. }
  1120. /* Load $MFT. */
  1121. ref.low = cpu_to_le32(MFT_REC_MFT);
  1122. ref.seq = cpu_to_le16(1);
  1123. inode = ntfs_iget5(sb, &ref, &NAME_MFT);
  1124. if (IS_ERR(inode)) {
  1125. err = PTR_ERR(inode);
  1126. ntfs_err(sb, "Failed to load $MFT (%d).", err);
  1127. goto out;
  1128. }
  1129. ni = ntfs_i(inode);
  1130. sbi->mft.used = ni->i_valid >> sbi->record_bits;
  1131. tt = inode->i_size >> sbi->record_bits;
  1132. sbi->mft.next_free = MFT_REC_USER;
  1133. err = wnd_init(&sbi->mft.bitmap, sb, tt);
  1134. if (err)
  1135. goto put_inode_out;
  1136. err = ni_load_all_mi(ni);
  1137. if (err) {
  1138. ntfs_err(sb, "Failed to load $MFT's subrecords (%d).", err);
  1139. goto put_inode_out;
  1140. }
  1141. sbi->mft.ni = ni;
  1142. /* Load $Bitmap. */
  1143. ref.low = cpu_to_le32(MFT_REC_BITMAP);
  1144. ref.seq = cpu_to_le16(MFT_REC_BITMAP);
  1145. inode = ntfs_iget5(sb, &ref, &NAME_BITMAP);
  1146. if (IS_ERR(inode)) {
  1147. err = PTR_ERR(inode);
  1148. ntfs_err(sb, "Failed to load $Bitmap (%d).", err);
  1149. goto out;
  1150. }
  1151. #ifndef CONFIG_NTFS3_64BIT_CLUSTER
  1152. if (inode->i_size >> 32) {
  1153. err = -EINVAL;
  1154. goto put_inode_out;
  1155. }
  1156. #endif
  1157. /* Check bitmap boundary. */
  1158. tt = sbi->used.bitmap.nbits;
  1159. if (inode->i_size < ntfs3_bitmap_size(tt)) {
  1160. ntfs_err(sb, "$Bitmap is corrupted.");
  1161. err = -EINVAL;
  1162. goto put_inode_out;
  1163. }
  1164. err = wnd_init(&sbi->used.bitmap, sb, tt);
  1165. if (err) {
  1166. ntfs_err(sb, "Failed to initialize $Bitmap (%d).", err);
  1167. goto put_inode_out;
  1168. }
  1169. iput(inode);
  1170. /* Compute the MFT zone. */
  1171. err = ntfs_refresh_zone(sbi);
  1172. if (err) {
  1173. ntfs_err(sb, "Failed to initialize MFT zone (%d).", err);
  1174. goto out;
  1175. }
  1176. /* Load $BadClus. */
  1177. ref.low = cpu_to_le32(MFT_REC_BADCLUST);
  1178. ref.seq = cpu_to_le16(MFT_REC_BADCLUST);
  1179. inode = ntfs_iget5(sb, &ref, &NAME_BADCLUS);
  1180. if (IS_ERR(inode)) {
  1181. err = PTR_ERR(inode);
  1182. ntfs_err(sb, "Failed to load $BadClus (%d).", err);
  1183. goto out;
  1184. }
  1185. ni = ntfs_i(inode);
  1186. bad_len = bad_frags = 0;
  1187. for (i = 0; run_get_entry(&ni->file.run, i, &vcn, &lcn, &len); i++) {
  1188. if (lcn == SPARSE_LCN)
  1189. continue;
  1190. bad_len += len;
  1191. bad_frags += 1;
  1192. if (ro)
  1193. continue;
  1194. if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
  1195. /* Bad blocks marked as free in bitmap. */
  1196. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  1197. }
  1198. }
  1199. if (bad_len) {
  1200. /*
  1201. * Notice about bad blocks.
  1202. * In normal cases these blocks are marked as used in bitmap.
  1203. * And we never allocate space in it.
  1204. */
  1205. ntfs_notice(sb,
  1206. "Volume contains %zu bad blocks in %zu fragments.",
  1207. bad_len, bad_frags);
  1208. }
  1209. iput(inode);
  1210. /* Load $AttrDef. */
  1211. ref.low = cpu_to_le32(MFT_REC_ATTR);
  1212. ref.seq = cpu_to_le16(MFT_REC_ATTR);
  1213. inode = ntfs_iget5(sb, &ref, &NAME_ATTRDEF);
  1214. if (IS_ERR(inode)) {
  1215. err = PTR_ERR(inode);
  1216. ntfs_err(sb, "Failed to load $AttrDef (%d)", err);
  1217. goto out;
  1218. }
  1219. /*
  1220. * Typical $AttrDef contains up to 20 entries.
  1221. * Check for extremely large/small size.
  1222. */
  1223. if (inode->i_size < sizeof(struct ATTR_DEF_ENTRY) ||
  1224. inode->i_size > 100 * sizeof(struct ATTR_DEF_ENTRY)) {
  1225. ntfs_err(sb, "Looks like $AttrDef is corrupted (size=%llu).",
  1226. inode->i_size);
  1227. err = -EINVAL;
  1228. goto put_inode_out;
  1229. }
  1230. bytes = inode->i_size;
  1231. sbi->def_table = t = kvmalloc(bytes, GFP_KERNEL);
  1232. if (!t) {
  1233. err = -ENOMEM;
  1234. goto put_inode_out;
  1235. }
  1236. /* Read the entire file. */
  1237. err = inode_read_data(inode, sbi->def_table, bytes);
  1238. if (err) {
  1239. ntfs_err(sb, "Failed to read $AttrDef (%d).", err);
  1240. goto put_inode_out;
  1241. }
  1242. if (ATTR_STD != t->type) {
  1243. ntfs_err(sb, "$AttrDef is corrupted.");
  1244. err = -EINVAL;
  1245. goto put_inode_out;
  1246. }
  1247. t += 1;
  1248. sbi->def_entries = 1;
  1249. done = sizeof(struct ATTR_DEF_ENTRY);
  1250. while (done + sizeof(struct ATTR_DEF_ENTRY) <= bytes) {
  1251. u32 t32 = le32_to_cpu(t->type);
  1252. u64 sz = le64_to_cpu(t->max_sz);
  1253. if ((t32 & 0xF) || le32_to_cpu(t[-1].type) >= t32)
  1254. break;
  1255. if (t->type == ATTR_REPARSE)
  1256. sbi->reparse.max_size = sz;
  1257. else if (t->type == ATTR_EA)
  1258. sbi->ea_max_size = sz;
  1259. done += sizeof(struct ATTR_DEF_ENTRY);
  1260. t += 1;
  1261. sbi->def_entries += 1;
  1262. }
  1263. iput(inode);
  1264. /* Load $UpCase. */
  1265. ref.low = cpu_to_le32(MFT_REC_UPCASE);
  1266. ref.seq = cpu_to_le16(MFT_REC_UPCASE);
  1267. inode = ntfs_iget5(sb, &ref, &NAME_UPCASE);
  1268. if (IS_ERR(inode)) {
  1269. err = PTR_ERR(inode);
  1270. ntfs_err(sb, "Failed to load $UpCase (%d).", err);
  1271. goto out;
  1272. }
  1273. if (inode->i_size != 0x10000 * sizeof(short)) {
  1274. err = -EINVAL;
  1275. ntfs_err(sb, "$UpCase is corrupted.");
  1276. goto put_inode_out;
  1277. }
  1278. /* Read the entire file. */
  1279. err = inode_read_data(inode, sbi->upcase, 0x10000 * sizeof(short));
  1280. if (err) {
  1281. ntfs_err(sb, "Failed to read $UpCase (%d).", err);
  1282. goto put_inode_out;
  1283. }
  1284. #ifdef __BIG_ENDIAN
  1285. {
  1286. u16 *dst = sbi->upcase;
  1287. for (i = 0; i < 0x10000; i++)
  1288. __swab16s(dst++);
  1289. }
  1290. #endif
  1291. shared = ntfs_set_shared(sbi->upcase, 0x10000 * sizeof(short));
  1292. if (shared && sbi->upcase != shared) {
  1293. kvfree(sbi->upcase);
  1294. sbi->upcase = shared;
  1295. }
  1296. iput(inode);
  1297. if (is_ntfs3(sbi)) {
  1298. /* Load $Secure. */
  1299. err = ntfs_security_init(sbi);
  1300. if (err) {
  1301. ntfs_err(sb, "Failed to initialize $Secure (%d).", err);
  1302. goto out;
  1303. }
  1304. /* Load $Extend. */
  1305. err = ntfs_extend_init(sbi);
  1306. if (err) {
  1307. ntfs_warn(sb, "Failed to initialize $Extend.");
  1308. goto load_root;
  1309. }
  1310. /* Load $Extend/$Reparse. */
  1311. err = ntfs_reparse_init(sbi);
  1312. if (err) {
  1313. ntfs_warn(sb, "Failed to initialize $Extend/$Reparse.");
  1314. goto load_root;
  1315. }
  1316. /* Load $Extend/$ObjId. */
  1317. err = ntfs_objid_init(sbi);
  1318. if (err) {
  1319. ntfs_warn(sb, "Failed to initialize $Extend/$ObjId.");
  1320. goto load_root;
  1321. }
  1322. }
  1323. load_root:
  1324. /* Load root. */
  1325. ref.low = cpu_to_le32(MFT_REC_ROOT);
  1326. ref.seq = cpu_to_le16(MFT_REC_ROOT);
  1327. inode = ntfs_iget5(sb, &ref, &NAME_ROOT);
  1328. if (IS_ERR(inode)) {
  1329. err = PTR_ERR(inode);
  1330. ntfs_err(sb, "Failed to load root (%d).", err);
  1331. goto out;
  1332. }
  1333. /*
  1334. * Final check. Looks like this case should never occurs.
  1335. */
  1336. if (!inode->i_op) {
  1337. err = -EINVAL;
  1338. ntfs_err(sb, "Failed to load root (%d).", err);
  1339. goto put_inode_out;
  1340. }
  1341. sb->s_root = d_make_root(inode);
  1342. if (!sb->s_root) {
  1343. err = -ENOMEM;
  1344. goto put_inode_out;
  1345. }
  1346. if (boot2) {
  1347. /*
  1348. * Alternative boot is ok but primary is not ok.
  1349. * Volume is recognized as NTFS. Update primary boot.
  1350. */
  1351. struct buffer_head *bh0 = sb_getblk(sb, 0);
  1352. if (bh0) {
  1353. if (buffer_locked(bh0))
  1354. __wait_on_buffer(bh0);
  1355. lock_buffer(bh0);
  1356. memcpy(bh0->b_data, boot2, sizeof(*boot2));
  1357. set_buffer_uptodate(bh0);
  1358. mark_buffer_dirty(bh0);
  1359. unlock_buffer(bh0);
  1360. if (!sync_dirty_buffer(bh0))
  1361. ntfs_warn(sb, "primary boot is updated");
  1362. put_bh(bh0);
  1363. }
  1364. kfree(boot2);
  1365. }
  1366. #ifdef CONFIG_PROC_FS
  1367. /* Create /proc/fs/ntfs3/.. */
  1368. if (proc_info_root) {
  1369. struct proc_dir_entry *e = proc_mkdir(sb->s_id, proc_info_root);
  1370. static_assert((S_IRUGO | S_IWUSR) == 0644);
  1371. if (e) {
  1372. proc_create_data("volinfo", S_IRUGO, e,
  1373. &ntfs3_volinfo_fops, sb);
  1374. proc_create_data("label", S_IRUGO | S_IWUSR, e,
  1375. &ntfs3_label_fops, sb);
  1376. sbi->procdir = e;
  1377. }
  1378. }
  1379. #endif
  1380. if (is_legacy_ntfs(sb))
  1381. sb->s_flags |= SB_RDONLY;
  1382. return 0;
  1383. put_inode_out:
  1384. iput(inode);
  1385. out:
  1386. ntfs3_put_sbi(sbi);
  1387. kfree(boot2);
  1388. ntfs3_put_sbi(sbi);
  1389. return err;
  1390. }
  1391. void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len)
  1392. {
  1393. struct ntfs_sb_info *sbi = sb->s_fs_info;
  1394. struct block_device *bdev = sb->s_bdev;
  1395. sector_t devblock = (u64)lcn * sbi->blocks_per_cluster;
  1396. unsigned long blocks = (u64)len * sbi->blocks_per_cluster;
  1397. unsigned long cnt = 0;
  1398. unsigned long limit = global_zone_page_state(NR_FREE_PAGES)
  1399. << (PAGE_SHIFT - sb->s_blocksize_bits);
  1400. if (limit >= 0x2000)
  1401. limit -= 0x1000;
  1402. else if (limit < 32)
  1403. limit = 32;
  1404. else
  1405. limit >>= 1;
  1406. while (blocks--) {
  1407. clean_bdev_aliases(bdev, devblock++, 1);
  1408. if (cnt++ >= limit) {
  1409. sync_blockdev(bdev);
  1410. cnt = 0;
  1411. }
  1412. }
  1413. }
  1414. /*
  1415. * ntfs_discard - Issue a discard request (trim for SSD).
  1416. */
  1417. int ntfs_discard(struct ntfs_sb_info *sbi, CLST lcn, CLST len)
  1418. {
  1419. int err;
  1420. u64 lbo, bytes, start, end;
  1421. struct super_block *sb;
  1422. if (sbi->used.next_free_lcn == lcn + len)
  1423. sbi->used.next_free_lcn = lcn;
  1424. if (sbi->flags & NTFS_FLAGS_NODISCARD)
  1425. return -EOPNOTSUPP;
  1426. if (!sbi->options->discard)
  1427. return -EOPNOTSUPP;
  1428. lbo = (u64)lcn << sbi->cluster_bits;
  1429. bytes = (u64)len << sbi->cluster_bits;
  1430. /* Align up 'start' on discard_granularity. */
  1431. start = (lbo + sbi->discard_granularity - 1) &
  1432. sbi->discard_granularity_mask_inv;
  1433. /* Align down 'end' on discard_granularity. */
  1434. end = (lbo + bytes) & sbi->discard_granularity_mask_inv;
  1435. sb = sbi->sb;
  1436. if (start >= end)
  1437. return 0;
  1438. err = blkdev_issue_discard(sb->s_bdev, start >> 9, (end - start) >> 9,
  1439. GFP_NOFS);
  1440. if (err == -EOPNOTSUPP)
  1441. sbi->flags |= NTFS_FLAGS_NODISCARD;
  1442. return err;
  1443. }
  1444. static int ntfs_fs_get_tree(struct fs_context *fc)
  1445. {
  1446. return get_tree_bdev(fc, ntfs_fill_super);
  1447. }
  1448. /*
  1449. * ntfs_fs_free - Free fs_context.
  1450. *
  1451. * Note that this will be called after fill_super and reconfigure
  1452. * even when they pass. So they have to take pointers if they pass.
  1453. */
  1454. static void ntfs_fs_free(struct fs_context *fc)
  1455. {
  1456. struct ntfs_mount_options *opts = fc->fs_private;
  1457. struct ntfs_sb_info *sbi = fc->s_fs_info;
  1458. if (sbi) {
  1459. ntfs3_put_sbi(sbi);
  1460. ntfs3_free_sbi(sbi);
  1461. }
  1462. if (opts)
  1463. put_mount_options(opts);
  1464. }
  1465. // clang-format off
  1466. static const struct fs_context_operations ntfs_context_ops = {
  1467. .parse_param = ntfs_fs_parse_param,
  1468. .get_tree = ntfs_fs_get_tree,
  1469. .reconfigure = ntfs_fs_reconfigure,
  1470. .free = ntfs_fs_free,
  1471. };
  1472. // clang-format on
  1473. /*
  1474. * ntfs_init_fs_context - Initialize sbi and opts
  1475. *
  1476. * This will called when mount/remount. We will first initialize
  1477. * options so that if remount we can use just that.
  1478. */
  1479. static int __ntfs_init_fs_context(struct fs_context *fc)
  1480. {
  1481. struct ntfs_mount_options *opts;
  1482. struct ntfs_sb_info *sbi;
  1483. opts = kzalloc(sizeof(struct ntfs_mount_options), GFP_NOFS);
  1484. if (!opts)
  1485. return -ENOMEM;
  1486. /* Default options. */
  1487. opts->fs_uid = current_uid();
  1488. opts->fs_gid = current_gid();
  1489. opts->fs_fmask_inv = ~current_umask();
  1490. opts->fs_dmask_inv = ~current_umask();
  1491. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
  1492. goto ok;
  1493. sbi = kzalloc(sizeof(struct ntfs_sb_info), GFP_NOFS);
  1494. if (!sbi)
  1495. goto free_opts;
  1496. sbi->upcase = kvmalloc(0x10000 * sizeof(short), GFP_KERNEL);
  1497. if (!sbi->upcase)
  1498. goto free_sbi;
  1499. ratelimit_state_init(&sbi->msg_ratelimit, DEFAULT_RATELIMIT_INTERVAL,
  1500. DEFAULT_RATELIMIT_BURST);
  1501. mutex_init(&sbi->compress.mtx_lznt);
  1502. #ifdef CONFIG_NTFS3_LZX_XPRESS
  1503. mutex_init(&sbi->compress.mtx_xpress);
  1504. mutex_init(&sbi->compress.mtx_lzx);
  1505. #endif
  1506. fc->s_fs_info = sbi;
  1507. ok:
  1508. fc->fs_private = opts;
  1509. fc->ops = &ntfs_context_ops;
  1510. return 0;
  1511. free_sbi:
  1512. kfree(sbi);
  1513. free_opts:
  1514. kfree(opts);
  1515. return -ENOMEM;
  1516. }
  1517. static int ntfs_init_fs_context(struct fs_context *fc)
  1518. {
  1519. return __ntfs_init_fs_context(fc);
  1520. }
  1521. static void ntfs3_kill_sb(struct super_block *sb)
  1522. {
  1523. struct ntfs_sb_info *sbi = sb->s_fs_info;
  1524. kill_block_super(sb);
  1525. if (sbi->options)
  1526. put_mount_options(sbi->options);
  1527. ntfs3_free_sbi(sbi);
  1528. }
  1529. // clang-format off
  1530. static struct file_system_type ntfs_fs_type = {
  1531. .owner = THIS_MODULE,
  1532. .name = "ntfs3",
  1533. .init_fs_context = ntfs_init_fs_context,
  1534. .parameters = ntfs_fs_parameters,
  1535. .kill_sb = ntfs3_kill_sb,
  1536. .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
  1537. };
  1538. #if IS_ENABLED(CONFIG_NTFS_FS)
  1539. static int ntfs_legacy_init_fs_context(struct fs_context *fc)
  1540. {
  1541. int ret;
  1542. ret = __ntfs_init_fs_context(fc);
  1543. /* If ntfs3 is used as legacy ntfs enforce read-only mode. */
  1544. fc->sb_flags |= SB_RDONLY;
  1545. return ret;
  1546. }
  1547. static struct file_system_type ntfs_legacy_fs_type = {
  1548. .owner = THIS_MODULE,
  1549. .name = "ntfs",
  1550. .init_fs_context = ntfs_legacy_init_fs_context,
  1551. .parameters = ntfs_fs_parameters,
  1552. .kill_sb = ntfs3_kill_sb,
  1553. .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
  1554. };
  1555. MODULE_ALIAS_FS("ntfs");
  1556. static inline void register_as_ntfs_legacy(void)
  1557. {
  1558. int err = register_filesystem(&ntfs_legacy_fs_type);
  1559. if (err)
  1560. pr_warn("ntfs3: Failed to register legacy ntfs filesystem driver: %d\n", err);
  1561. }
  1562. static inline void unregister_as_ntfs_legacy(void)
  1563. {
  1564. unregister_filesystem(&ntfs_legacy_fs_type);
  1565. }
  1566. bool is_legacy_ntfs(struct super_block *sb)
  1567. {
  1568. return sb->s_type == &ntfs_legacy_fs_type;
  1569. }
  1570. #else
  1571. static inline void register_as_ntfs_legacy(void) {}
  1572. static inline void unregister_as_ntfs_legacy(void) {}
  1573. #endif
  1574. // clang-format on
  1575. static int __init init_ntfs_fs(void)
  1576. {
  1577. int err;
  1578. if (IS_ENABLED(CONFIG_NTFS3_FS_POSIX_ACL))
  1579. pr_info("ntfs3: Enabled Linux POSIX ACLs support\n");
  1580. if (IS_ENABLED(CONFIG_NTFS3_64BIT_CLUSTER))
  1581. pr_notice(
  1582. "ntfs3: Warning: Activated 64 bits per cluster. Windows does not support this\n");
  1583. if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
  1584. pr_info("ntfs3: Read-only LZX/Xpress compression included\n");
  1585. #ifdef CONFIG_PROC_FS
  1586. /* Create "/proc/fs/ntfs3" */
  1587. proc_info_root = proc_mkdir("fs/ntfs3", NULL);
  1588. #endif
  1589. err = ntfs3_init_bitmap();
  1590. if (err)
  1591. return err;
  1592. ntfs_inode_cachep = kmem_cache_create(
  1593. "ntfs_inode_cache", sizeof(struct ntfs_inode), 0,
  1594. (SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT), init_once);
  1595. if (!ntfs_inode_cachep) {
  1596. err = -ENOMEM;
  1597. goto out1;
  1598. }
  1599. register_as_ntfs_legacy();
  1600. err = register_filesystem(&ntfs_fs_type);
  1601. if (err)
  1602. goto out;
  1603. return 0;
  1604. out:
  1605. kmem_cache_destroy(ntfs_inode_cachep);
  1606. out1:
  1607. ntfs3_exit_bitmap();
  1608. return err;
  1609. }
  1610. static void __exit exit_ntfs_fs(void)
  1611. {
  1612. rcu_barrier();
  1613. kmem_cache_destroy(ntfs_inode_cachep);
  1614. unregister_filesystem(&ntfs_fs_type);
  1615. unregister_as_ntfs_legacy();
  1616. ntfs3_exit_bitmap();
  1617. #ifdef CONFIG_PROC_FS
  1618. if (proc_info_root)
  1619. remove_proc_entry("fs/ntfs3", NULL);
  1620. #endif
  1621. }
  1622. MODULE_LICENSE("GPL");
  1623. MODULE_DESCRIPTION("ntfs3 read/write filesystem");
  1624. #ifdef CONFIG_NTFS3_FS_POSIX_ACL
  1625. MODULE_INFO(behaviour, "Enabled Linux POSIX ACLs support");
  1626. #endif
  1627. #ifdef CONFIG_NTFS3_64BIT_CLUSTER
  1628. MODULE_INFO(
  1629. cluster,
  1630. "Warning: Activated 64 bits per cluster. Windows does not support this");
  1631. #endif
  1632. #ifdef CONFIG_NTFS3_LZX_XPRESS
  1633. MODULE_INFO(compression, "Read-only lzx/xpress compression included");
  1634. #endif
  1635. MODULE_AUTHOR("Konstantin Komarov");
  1636. MODULE_ALIAS_FS("ntfs3");
  1637. module_init(init_ntfs_fs);
  1638. module_exit(exit_ntfs_fs);