super.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  1. /*
  2. * super.c - NTFS kernel super block handling. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2004 Anton Altaparmakov
  5. * Copyright (c) 2001,2002 Richard Russon
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/stddef.h>
  23. #include <linux/init.h>
  24. #include <linux/string.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/blkdev.h> /* For bdev_hardsect_size(). */
  27. #include <linux/backing-dev.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/vfs.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/smp_lock.h>
  32. #include "sysctl.h"
  33. #include "logfile.h"
  34. #include "quota.h"
  35. #include "dir.h"
  36. #include "debug.h"
  37. #include "index.h"
  38. #include "aops.h"
  39. #include "malloc.h"
  40. #include "ntfs.h"
  41. /* Number of mounted file systems which have compression enabled. */
  42. static unsigned long ntfs_nr_compression_users;
  43. /* A global default upcase table and a corresponding reference count. */
  44. static ntfschar *default_upcase = NULL;
  45. static unsigned long ntfs_nr_upcase_users = 0;
  46. /* Error constants/strings used in inode.c::ntfs_show_options(). */
  47. typedef enum {
  48. /* One of these must be present, default is ON_ERRORS_CONTINUE. */
  49. ON_ERRORS_PANIC = 0x01,
  50. ON_ERRORS_REMOUNT_RO = 0x02,
  51. ON_ERRORS_CONTINUE = 0x04,
  52. /* Optional, can be combined with any of the above. */
  53. ON_ERRORS_RECOVER = 0x10,
  54. } ON_ERRORS_ACTIONS;
  55. const option_t on_errors_arr[] = {
  56. { ON_ERRORS_PANIC, "panic" },
  57. { ON_ERRORS_REMOUNT_RO, "remount-ro", },
  58. { ON_ERRORS_CONTINUE, "continue", },
  59. { ON_ERRORS_RECOVER, "recover" },
  60. { 0, NULL }
  61. };
  62. /**
  63. * simple_getbool -
  64. *
  65. * Copied from old ntfs driver (which copied from vfat driver).
  66. */
  67. static int simple_getbool(char *s, BOOL *setval)
  68. {
  69. if (s) {
  70. if (!strcmp(s, "1") || !strcmp(s, "yes") || !strcmp(s, "true"))
  71. *setval = TRUE;
  72. else if (!strcmp(s, "0") || !strcmp(s, "no") ||
  73. !strcmp(s, "false"))
  74. *setval = FALSE;
  75. else
  76. return 0;
  77. } else
  78. *setval = TRUE;
  79. return 1;
  80. }
  81. /**
  82. * parse_options - parse the (re)mount options
  83. * @vol: ntfs volume
  84. * @opt: string containing the (re)mount options
  85. *
  86. * Parse the recognized options in @opt for the ntfs volume described by @vol.
  87. */
  88. static BOOL parse_options(ntfs_volume *vol, char *opt)
  89. {
  90. char *p, *v, *ov;
  91. static char *utf8 = "utf8";
  92. int errors = 0, sloppy = 0;
  93. uid_t uid = (uid_t)-1;
  94. gid_t gid = (gid_t)-1;
  95. mode_t fmask = (mode_t)-1, dmask = (mode_t)-1;
  96. int mft_zone_multiplier = -1, on_errors = -1;
  97. int show_sys_files = -1, case_sensitive = -1;
  98. struct nls_table *nls_map = NULL, *old_nls;
  99. /* I am lazy... (-8 */
  100. #define NTFS_GETOPT_WITH_DEFAULT(option, variable, default_value) \
  101. if (!strcmp(p, option)) { \
  102. if (!v || !*v) \
  103. variable = default_value; \
  104. else { \
  105. variable = simple_strtoul(ov = v, &v, 0); \
  106. if (*v) \
  107. goto needs_val; \
  108. } \
  109. }
  110. #define NTFS_GETOPT(option, variable) \
  111. if (!strcmp(p, option)) { \
  112. if (!v || !*v) \
  113. goto needs_arg; \
  114. variable = simple_strtoul(ov = v, &v, 0); \
  115. if (*v) \
  116. goto needs_val; \
  117. }
  118. #define NTFS_GETOPT_BOOL(option, variable) \
  119. if (!strcmp(p, option)) { \
  120. BOOL val; \
  121. if (!simple_getbool(v, &val)) \
  122. goto needs_bool; \
  123. variable = val; \
  124. }
  125. #define NTFS_GETOPT_OPTIONS_ARRAY(option, variable, opt_array) \
  126. if (!strcmp(p, option)) { \
  127. int _i; \
  128. if (!v || !*v) \
  129. goto needs_arg; \
  130. ov = v; \
  131. if (variable == -1) \
  132. variable = 0; \
  133. for (_i = 0; opt_array[_i].str && *opt_array[_i].str; _i++) \
  134. if (!strcmp(opt_array[_i].str, v)) { \
  135. variable |= opt_array[_i].val; \
  136. break; \
  137. } \
  138. if (!opt_array[_i].str || !*opt_array[_i].str) \
  139. goto needs_val; \
  140. }
  141. if (!opt || !*opt)
  142. goto no_mount_options;
  143. ntfs_debug("Entering with mount options string: %s", opt);
  144. while ((p = strsep(&opt, ","))) {
  145. if ((v = strchr(p, '=')))
  146. *v++ = 0;
  147. NTFS_GETOPT("uid", uid)
  148. else NTFS_GETOPT("gid", gid)
  149. else NTFS_GETOPT("umask", fmask = dmask)
  150. else NTFS_GETOPT("fmask", fmask)
  151. else NTFS_GETOPT("dmask", dmask)
  152. else NTFS_GETOPT("mft_zone_multiplier", mft_zone_multiplier)
  153. else NTFS_GETOPT_WITH_DEFAULT("sloppy", sloppy, TRUE)
  154. else NTFS_GETOPT_BOOL("show_sys_files", show_sys_files)
  155. else NTFS_GETOPT_BOOL("case_sensitive", case_sensitive)
  156. else NTFS_GETOPT_OPTIONS_ARRAY("errors", on_errors,
  157. on_errors_arr)
  158. else if (!strcmp(p, "posix") || !strcmp(p, "show_inodes"))
  159. ntfs_warning(vol->sb, "Ignoring obsolete option %s.",
  160. p);
  161. else if (!strcmp(p, "nls") || !strcmp(p, "iocharset")) {
  162. if (!strcmp(p, "iocharset"))
  163. ntfs_warning(vol->sb, "Option iocharset is "
  164. "deprecated. Please use "
  165. "option nls=<charsetname> in "
  166. "the future.");
  167. if (!v || !*v)
  168. goto needs_arg;
  169. use_utf8:
  170. old_nls = nls_map;
  171. nls_map = load_nls(v);
  172. if (!nls_map) {
  173. if (!old_nls) {
  174. ntfs_error(vol->sb, "NLS character set "
  175. "%s not found.", v);
  176. return FALSE;
  177. }
  178. ntfs_error(vol->sb, "NLS character set %s not "
  179. "found. Using previous one %s.",
  180. v, old_nls->charset);
  181. nls_map = old_nls;
  182. } else /* nls_map */ {
  183. if (old_nls)
  184. unload_nls(old_nls);
  185. }
  186. } else if (!strcmp(p, "utf8")) {
  187. BOOL val = FALSE;
  188. ntfs_warning(vol->sb, "Option utf8 is no longer "
  189. "supported, using option nls=utf8. Please "
  190. "use option nls=utf8 in the future and "
  191. "make sure utf8 is compiled either as a "
  192. "module or into the kernel.");
  193. if (!v || !*v)
  194. val = TRUE;
  195. else if (!simple_getbool(v, &val))
  196. goto needs_bool;
  197. if (val) {
  198. v = utf8;
  199. goto use_utf8;
  200. }
  201. } else {
  202. ntfs_error(vol->sb, "Unrecognized mount option %s.", p);
  203. if (errors < INT_MAX)
  204. errors++;
  205. }
  206. #undef NTFS_GETOPT_OPTIONS_ARRAY
  207. #undef NTFS_GETOPT_BOOL
  208. #undef NTFS_GETOPT
  209. #undef NTFS_GETOPT_WITH_DEFAULT
  210. }
  211. no_mount_options:
  212. if (errors && !sloppy)
  213. return FALSE;
  214. if (sloppy)
  215. ntfs_warning(vol->sb, "Sloppy option given. Ignoring "
  216. "unrecognized mount option(s) and continuing.");
  217. /* Keep this first! */
  218. if (on_errors != -1) {
  219. if (!on_errors) {
  220. ntfs_error(vol->sb, "Invalid errors option argument "
  221. "or bug in options parser.");
  222. return FALSE;
  223. }
  224. }
  225. if (nls_map) {
  226. if (vol->nls_map && vol->nls_map != nls_map) {
  227. ntfs_error(vol->sb, "Cannot change NLS character set "
  228. "on remount.");
  229. return FALSE;
  230. } /* else (!vol->nls_map) */
  231. ntfs_debug("Using NLS character set %s.", nls_map->charset);
  232. vol->nls_map = nls_map;
  233. } else /* (!nls_map) */ {
  234. if (!vol->nls_map) {
  235. vol->nls_map = load_nls_default();
  236. if (!vol->nls_map) {
  237. ntfs_error(vol->sb, "Failed to load default "
  238. "NLS character set.");
  239. return FALSE;
  240. }
  241. ntfs_debug("Using default NLS character set (%s).",
  242. vol->nls_map->charset);
  243. }
  244. }
  245. if (mft_zone_multiplier != -1) {
  246. if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
  247. mft_zone_multiplier) {
  248. ntfs_error(vol->sb, "Cannot change mft_zone_multiplier "
  249. "on remount.");
  250. return FALSE;
  251. }
  252. if (mft_zone_multiplier < 1 || mft_zone_multiplier > 4) {
  253. ntfs_error(vol->sb, "Invalid mft_zone_multiplier. "
  254. "Using default value, i.e. 1.");
  255. mft_zone_multiplier = 1;
  256. }
  257. vol->mft_zone_multiplier = mft_zone_multiplier;
  258. }
  259. if (!vol->mft_zone_multiplier)
  260. vol->mft_zone_multiplier = 1;
  261. if (on_errors != -1)
  262. vol->on_errors = on_errors;
  263. if (!vol->on_errors || vol->on_errors == ON_ERRORS_RECOVER)
  264. vol->on_errors |= ON_ERRORS_CONTINUE;
  265. if (uid != (uid_t)-1)
  266. vol->uid = uid;
  267. if (gid != (gid_t)-1)
  268. vol->gid = gid;
  269. if (fmask != (mode_t)-1)
  270. vol->fmask = fmask;
  271. if (dmask != (mode_t)-1)
  272. vol->dmask = dmask;
  273. if (show_sys_files != -1) {
  274. if (show_sys_files)
  275. NVolSetShowSystemFiles(vol);
  276. else
  277. NVolClearShowSystemFiles(vol);
  278. }
  279. if (case_sensitive != -1) {
  280. if (case_sensitive)
  281. NVolSetCaseSensitive(vol);
  282. else
  283. NVolClearCaseSensitive(vol);
  284. }
  285. return TRUE;
  286. needs_arg:
  287. ntfs_error(vol->sb, "The %s option requires an argument.", p);
  288. return FALSE;
  289. needs_bool:
  290. ntfs_error(vol->sb, "The %s option requires a boolean argument.", p);
  291. return FALSE;
  292. needs_val:
  293. ntfs_error(vol->sb, "Invalid %s option argument: %s", p, ov);
  294. return FALSE;
  295. }
  296. #ifdef NTFS_RW
  297. /**
  298. * ntfs_write_volume_flags - write new flags to the volume information flags
  299. * @vol: ntfs volume on which to modify the flags
  300. * @flags: new flags value for the volume information flags
  301. *
  302. * Internal function. You probably want to use ntfs_{set,clear}_volume_flags()
  303. * instead (see below).
  304. *
  305. * Replace the volume information flags on the volume @vol with the value
  306. * supplied in @flags. Note, this overwrites the volume information flags, so
  307. * make sure to combine the flags you want to modify with the old flags and use
  308. * the result when calling ntfs_write_volume_flags().
  309. *
  310. * Return 0 on success and -errno on error.
  311. */
  312. static int ntfs_write_volume_flags(ntfs_volume *vol, const VOLUME_FLAGS flags)
  313. {
  314. ntfs_inode *ni = NTFS_I(vol->vol_ino);
  315. MFT_RECORD *m;
  316. VOLUME_INFORMATION *vi;
  317. ntfs_attr_search_ctx *ctx;
  318. int err;
  319. ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
  320. le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
  321. if (vol->vol_flags == flags)
  322. goto done;
  323. BUG_ON(!ni);
  324. m = map_mft_record(ni);
  325. if (IS_ERR(m)) {
  326. err = PTR_ERR(m);
  327. goto err_out;
  328. }
  329. ctx = ntfs_attr_get_search_ctx(ni, m);
  330. if (!ctx) {
  331. err = -ENOMEM;
  332. goto put_unm_err_out;
  333. }
  334. err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
  335. ctx);
  336. if (err)
  337. goto put_unm_err_out;
  338. vi = (VOLUME_INFORMATION*)((u8*)ctx->attr +
  339. le16_to_cpu(ctx->attr->data.resident.value_offset));
  340. vol->vol_flags = vi->flags = flags;
  341. flush_dcache_mft_record_page(ctx->ntfs_ino);
  342. mark_mft_record_dirty(ctx->ntfs_ino);
  343. ntfs_attr_put_search_ctx(ctx);
  344. unmap_mft_record(ni);
  345. done:
  346. ntfs_debug("Done.");
  347. return 0;
  348. put_unm_err_out:
  349. if (ctx)
  350. ntfs_attr_put_search_ctx(ctx);
  351. unmap_mft_record(ni);
  352. err_out:
  353. ntfs_error(vol->sb, "Failed with error code %i.", -err);
  354. return err;
  355. }
  356. /**
  357. * ntfs_set_volume_flags - set bits in the volume information flags
  358. * @vol: ntfs volume on which to modify the flags
  359. * @flags: flags to set on the volume
  360. *
  361. * Set the bits in @flags in the volume information flags on the volume @vol.
  362. *
  363. * Return 0 on success and -errno on error.
  364. */
  365. static inline int ntfs_set_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
  366. {
  367. flags &= VOLUME_FLAGS_MASK;
  368. return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
  369. }
  370. /**
  371. * ntfs_clear_volume_flags - clear bits in the volume information flags
  372. * @vol: ntfs volume on which to modify the flags
  373. * @flags: flags to clear on the volume
  374. *
  375. * Clear the bits in @flags in the volume information flags on the volume @vol.
  376. *
  377. * Return 0 on success and -errno on error.
  378. */
  379. static inline int ntfs_clear_volume_flags(ntfs_volume *vol, VOLUME_FLAGS flags)
  380. {
  381. flags &= VOLUME_FLAGS_MASK;
  382. flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
  383. return ntfs_write_volume_flags(vol, flags);
  384. }
  385. #endif /* NTFS_RW */
  386. /**
  387. * ntfs_remount - change the mount options of a mounted ntfs filesystem
  388. * @sb: superblock of mounted ntfs filesystem
  389. * @flags: remount flags
  390. * @opt: remount options string
  391. *
  392. * Change the mount options of an already mounted ntfs filesystem.
  393. *
  394. * NOTE: The VFS sets the @sb->s_flags remount flags to @flags after
  395. * ntfs_remount() returns successfully (i.e. returns 0). Otherwise,
  396. * @sb->s_flags are not changed.
  397. */
  398. static int ntfs_remount(struct super_block *sb, int *flags, char *opt)
  399. {
  400. ntfs_volume *vol = NTFS_SB(sb);
  401. ntfs_debug("Entering with remount options string: %s", opt);
  402. #ifndef NTFS_RW
  403. /* For read-only compiled driver, enforce all read-only flags. */
  404. *flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  405. #else /* NTFS_RW */
  406. /*
  407. * For the read-write compiled driver, if we are remounting read-write,
  408. * make sure there are no volume errors and that no unsupported volume
  409. * flags are set. Also, empty the logfile journal as it would become
  410. * stale as soon as something is written to the volume and mark the
  411. * volume dirty so that chkdsk is run if the volume is not umounted
  412. * cleanly. Finally, mark the quotas out of date so Windows rescans
  413. * the volume on boot and updates them.
  414. *
  415. * When remounting read-only, mark the volume clean if no volume errors
  416. * have occured.
  417. */
  418. if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
  419. static const char *es = ". Cannot remount read-write.";
  420. /* Remounting read-write. */
  421. if (NVolErrors(vol)) {
  422. ntfs_error(sb, "Volume has errors and is read-only%s",
  423. es);
  424. return -EROFS;
  425. }
  426. if (vol->vol_flags & VOLUME_IS_DIRTY) {
  427. ntfs_error(sb, "Volume is dirty and read-only%s", es);
  428. return -EROFS;
  429. }
  430. if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
  431. ntfs_error(sb, "Volume has unsupported flags set and "
  432. "is read-only%s", es);
  433. return -EROFS;
  434. }
  435. if (ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
  436. ntfs_error(sb, "Failed to set dirty bit in volume "
  437. "information flags%s", es);
  438. return -EROFS;
  439. }
  440. #if 0
  441. // TODO: Enable this code once we start modifying anything that
  442. // is different between NTFS 1.2 and 3.x...
  443. /* Set NT4 compatibility flag on newer NTFS version volumes. */
  444. if ((vol->major_ver > 1)) {
  445. if (ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
  446. ntfs_error(sb, "Failed to set NT4 "
  447. "compatibility flag%s", es);
  448. NVolSetErrors(vol);
  449. return -EROFS;
  450. }
  451. }
  452. #endif
  453. if (!ntfs_empty_logfile(vol->logfile_ino)) {
  454. ntfs_error(sb, "Failed to empty journal $LogFile%s",
  455. es);
  456. NVolSetErrors(vol);
  457. return -EROFS;
  458. }
  459. if (!ntfs_mark_quotas_out_of_date(vol)) {
  460. ntfs_error(sb, "Failed to mark quotas out of date%s",
  461. es);
  462. NVolSetErrors(vol);
  463. return -EROFS;
  464. }
  465. } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY)) {
  466. /* Remounting read-only. */
  467. if (!NVolErrors(vol)) {
  468. if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
  469. ntfs_warning(sb, "Failed to clear dirty bit "
  470. "in volume information "
  471. "flags. Run chkdsk.");
  472. }
  473. }
  474. #endif /* NTFS_RW */
  475. // TODO: Deal with *flags.
  476. if (!parse_options(vol, opt))
  477. return -EINVAL;
  478. ntfs_debug("Done.");
  479. return 0;
  480. }
  481. /**
  482. * is_boot_sector_ntfs - check whether a boot sector is a valid NTFS boot sector
  483. * @sb: Super block of the device to which @b belongs.
  484. * @b: Boot sector of device @sb to check.
  485. * @silent: If TRUE, all output will be silenced.
  486. *
  487. * is_boot_sector_ntfs() checks whether the boot sector @b is a valid NTFS boot
  488. * sector. Returns TRUE if it is valid and FALSE if not.
  489. *
  490. * @sb is only needed for warning/error output, i.e. it can be NULL when silent
  491. * is TRUE.
  492. */
  493. static BOOL is_boot_sector_ntfs(const struct super_block *sb,
  494. const NTFS_BOOT_SECTOR *b, const BOOL silent)
  495. {
  496. /*
  497. * Check that checksum == sum of u32 values from b to the checksum
  498. * field. If checksum is zero, no checking is done.
  499. */
  500. if ((void*)b < (void*)&b->checksum && b->checksum) {
  501. le32 *u;
  502. u32 i;
  503. for (i = 0, u = (le32*)b; u < (le32*)(&b->checksum); ++u)
  504. i += le32_to_cpup(u);
  505. if (le32_to_cpu(b->checksum) != i)
  506. goto not_ntfs;
  507. }
  508. /* Check OEMidentifier is "NTFS " */
  509. if (b->oem_id != magicNTFS)
  510. goto not_ntfs;
  511. /* Check bytes per sector value is between 256 and 4096. */
  512. if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
  513. le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
  514. goto not_ntfs;
  515. /* Check sectors per cluster value is valid. */
  516. switch (b->bpb.sectors_per_cluster) {
  517. case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128:
  518. break;
  519. default:
  520. goto not_ntfs;
  521. }
  522. /* Check the cluster size is not above 65536 bytes. */
  523. if ((u32)le16_to_cpu(b->bpb.bytes_per_sector) *
  524. b->bpb.sectors_per_cluster > 0x10000)
  525. goto not_ntfs;
  526. /* Check reserved/unused fields are really zero. */
  527. if (le16_to_cpu(b->bpb.reserved_sectors) ||
  528. le16_to_cpu(b->bpb.root_entries) ||
  529. le16_to_cpu(b->bpb.sectors) ||
  530. le16_to_cpu(b->bpb.sectors_per_fat) ||
  531. le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
  532. goto not_ntfs;
  533. /* Check clusters per file mft record value is valid. */
  534. if ((u8)b->clusters_per_mft_record < 0xe1 ||
  535. (u8)b->clusters_per_mft_record > 0xf7)
  536. switch (b->clusters_per_mft_record) {
  537. case 1: case 2: case 4: case 8: case 16: case 32: case 64:
  538. break;
  539. default:
  540. goto not_ntfs;
  541. }
  542. /* Check clusters per index block value is valid. */
  543. if ((u8)b->clusters_per_index_record < 0xe1 ||
  544. (u8)b->clusters_per_index_record > 0xf7)
  545. switch (b->clusters_per_index_record) {
  546. case 1: case 2: case 4: case 8: case 16: case 32: case 64:
  547. break;
  548. default:
  549. goto not_ntfs;
  550. }
  551. /*
  552. * Check for valid end of sector marker. We will work without it, but
  553. * many BIOSes will refuse to boot from a bootsector if the magic is
  554. * incorrect, so we emit a warning.
  555. */
  556. if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
  557. ntfs_warning(sb, "Invalid end of sector marker.");
  558. return TRUE;
  559. not_ntfs:
  560. return FALSE;
  561. }
  562. /**
  563. * read_ntfs_boot_sector - read the NTFS boot sector of a device
  564. * @sb: super block of device to read the boot sector from
  565. * @silent: if true, suppress all output
  566. *
  567. * Reads the boot sector from the device and validates it. If that fails, tries
  568. * to read the backup boot sector, first from the end of the device a-la NT4 and
  569. * later and then from the middle of the device a-la NT3.51 and before.
  570. *
  571. * If a valid boot sector is found but it is not the primary boot sector, we
  572. * repair the primary boot sector silently (unless the device is read-only or
  573. * the primary boot sector is not accessible).
  574. *
  575. * NOTE: To call this function, @sb must have the fields s_dev, the ntfs super
  576. * block (u.ntfs_sb), nr_blocks and the device flags (s_flags) initialized
  577. * to their respective values.
  578. *
  579. * Return the unlocked buffer head containing the boot sector or NULL on error.
  580. */
  581. static struct buffer_head *read_ntfs_boot_sector(struct super_block *sb,
  582. const int silent)
  583. {
  584. const char *read_err_str = "Unable to read %s boot sector.";
  585. struct buffer_head *bh_primary, *bh_backup;
  586. long nr_blocks = NTFS_SB(sb)->nr_blocks;
  587. /* Try to read primary boot sector. */
  588. if ((bh_primary = sb_bread(sb, 0))) {
  589. if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
  590. bh_primary->b_data, silent))
  591. return bh_primary;
  592. if (!silent)
  593. ntfs_error(sb, "Primary boot sector is invalid.");
  594. } else if (!silent)
  595. ntfs_error(sb, read_err_str, "primary");
  596. if (!(NTFS_SB(sb)->on_errors & ON_ERRORS_RECOVER)) {
  597. if (bh_primary)
  598. brelse(bh_primary);
  599. if (!silent)
  600. ntfs_error(sb, "Mount option errors=recover not used. "
  601. "Aborting without trying to recover.");
  602. return NULL;
  603. }
  604. /* Try to read NT4+ backup boot sector. */
  605. if ((bh_backup = sb_bread(sb, nr_blocks - 1))) {
  606. if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
  607. bh_backup->b_data, silent))
  608. goto hotfix_primary_boot_sector;
  609. brelse(bh_backup);
  610. } else if (!silent)
  611. ntfs_error(sb, read_err_str, "backup");
  612. /* Try to read NT3.51- backup boot sector. */
  613. if ((bh_backup = sb_bread(sb, nr_blocks >> 1))) {
  614. if (is_boot_sector_ntfs(sb, (NTFS_BOOT_SECTOR*)
  615. bh_backup->b_data, silent))
  616. goto hotfix_primary_boot_sector;
  617. if (!silent)
  618. ntfs_error(sb, "Could not find a valid backup boot "
  619. "sector.");
  620. brelse(bh_backup);
  621. } else if (!silent)
  622. ntfs_error(sb, read_err_str, "backup");
  623. /* We failed. Cleanup and return. */
  624. if (bh_primary)
  625. brelse(bh_primary);
  626. return NULL;
  627. hotfix_primary_boot_sector:
  628. if (bh_primary) {
  629. /*
  630. * If we managed to read sector zero and the volume is not
  631. * read-only, copy the found, valid backup boot sector to the
  632. * primary boot sector.
  633. */
  634. if (!(sb->s_flags & MS_RDONLY)) {
  635. ntfs_warning(sb, "Hot-fix: Recovering invalid primary "
  636. "boot sector from backup copy.");
  637. memcpy(bh_primary->b_data, bh_backup->b_data,
  638. sb->s_blocksize);
  639. mark_buffer_dirty(bh_primary);
  640. sync_dirty_buffer(bh_primary);
  641. if (buffer_uptodate(bh_primary)) {
  642. brelse(bh_backup);
  643. return bh_primary;
  644. }
  645. ntfs_error(sb, "Hot-fix: Device write error while "
  646. "recovering primary boot sector.");
  647. } else {
  648. ntfs_warning(sb, "Hot-fix: Recovery of primary boot "
  649. "sector failed: Read-only mount.");
  650. }
  651. brelse(bh_primary);
  652. }
  653. ntfs_warning(sb, "Using backup boot sector.");
  654. return bh_backup;
  655. }
  656. /**
  657. * parse_ntfs_boot_sector - parse the boot sector and store the data in @vol
  658. * @vol: volume structure to initialise with data from boot sector
  659. * @b: boot sector to parse
  660. *
  661. * Parse the ntfs boot sector @b and store all imporant information therein in
  662. * the ntfs super block @vol. Return TRUE on success and FALSE on error.
  663. */
  664. static BOOL parse_ntfs_boot_sector(ntfs_volume *vol, const NTFS_BOOT_SECTOR *b)
  665. {
  666. unsigned int sectors_per_cluster_bits, nr_hidden_sects;
  667. int clusters_per_mft_record, clusters_per_index_record;
  668. s64 ll;
  669. vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
  670. vol->sector_size_bits = ffs(vol->sector_size) - 1;
  671. ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
  672. vol->sector_size);
  673. ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
  674. vol->sector_size_bits);
  675. if (vol->sector_size != vol->sb->s_blocksize)
  676. ntfs_warning(vol->sb, "The boot sector indicates a sector size "
  677. "different from the device sector size.");
  678. ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
  679. sectors_per_cluster_bits = ffs(b->bpb.sectors_per_cluster) - 1;
  680. ntfs_debug("sectors_per_cluster_bits = 0x%x",
  681. sectors_per_cluster_bits);
  682. nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
  683. ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
  684. vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
  685. vol->cluster_size_mask = vol->cluster_size - 1;
  686. vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
  687. ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
  688. vol->cluster_size);
  689. ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
  690. ntfs_debug("vol->cluster_size_bits = %i (0x%x)",
  691. vol->cluster_size_bits, vol->cluster_size_bits);
  692. if (vol->sector_size > vol->cluster_size) {
  693. ntfs_error(vol->sb, "Sector sizes above the cluster size are "
  694. "not supported. Sorry.");
  695. return FALSE;
  696. }
  697. if (vol->sb->s_blocksize > vol->cluster_size) {
  698. ntfs_error(vol->sb, "Cluster sizes smaller than the device "
  699. "sector size are not supported. Sorry.");
  700. return FALSE;
  701. }
  702. clusters_per_mft_record = b->clusters_per_mft_record;
  703. ntfs_debug("clusters_per_mft_record = %i (0x%x)",
  704. clusters_per_mft_record, clusters_per_mft_record);
  705. if (clusters_per_mft_record > 0)
  706. vol->mft_record_size = vol->cluster_size <<
  707. (ffs(clusters_per_mft_record) - 1);
  708. else
  709. /*
  710. * When mft_record_size < cluster_size, clusters_per_mft_record
  711. * = -log2(mft_record_size) bytes. mft_record_size normaly is
  712. * 1024 bytes, which is encoded as 0xF6 (-10 in decimal).
  713. */
  714. vol->mft_record_size = 1 << -clusters_per_mft_record;
  715. vol->mft_record_size_mask = vol->mft_record_size - 1;
  716. vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
  717. ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
  718. vol->mft_record_size);
  719. ntfs_debug("vol->mft_record_size_mask = 0x%x",
  720. vol->mft_record_size_mask);
  721. ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
  722. vol->mft_record_size_bits, vol->mft_record_size_bits);
  723. /*
  724. * We cannot support mft record sizes above the PAGE_CACHE_SIZE since
  725. * we store $MFT/$DATA, the table of mft records in the page cache.
  726. */
  727. if (vol->mft_record_size > PAGE_CACHE_SIZE) {
  728. ntfs_error(vol->sb, "Mft record size %i (0x%x) exceeds the "
  729. "page cache size on your system %lu (0x%lx). "
  730. "This is not supported. Sorry.",
  731. vol->mft_record_size, vol->mft_record_size,
  732. PAGE_CACHE_SIZE, PAGE_CACHE_SIZE);
  733. return FALSE;
  734. }
  735. clusters_per_index_record = b->clusters_per_index_record;
  736. ntfs_debug("clusters_per_index_record = %i (0x%x)",
  737. clusters_per_index_record, clusters_per_index_record);
  738. if (clusters_per_index_record > 0)
  739. vol->index_record_size = vol->cluster_size <<
  740. (ffs(clusters_per_index_record) - 1);
  741. else
  742. /*
  743. * When index_record_size < cluster_size,
  744. * clusters_per_index_record = -log2(index_record_size) bytes.
  745. * index_record_size normaly equals 4096 bytes, which is
  746. * encoded as 0xF4 (-12 in decimal).
  747. */
  748. vol->index_record_size = 1 << -clusters_per_index_record;
  749. vol->index_record_size_mask = vol->index_record_size - 1;
  750. vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
  751. ntfs_debug("vol->index_record_size = %i (0x%x)",
  752. vol->index_record_size, vol->index_record_size);
  753. ntfs_debug("vol->index_record_size_mask = 0x%x",
  754. vol->index_record_size_mask);
  755. ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
  756. vol->index_record_size_bits,
  757. vol->index_record_size_bits);
  758. /*
  759. * Get the size of the volume in clusters and check for 64-bit-ness.
  760. * Windows currently only uses 32 bits to save the clusters so we do
  761. * the same as it is much faster on 32-bit CPUs.
  762. */
  763. ll = sle64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
  764. if ((u64)ll >= 1ULL << 32) {
  765. ntfs_error(vol->sb, "Cannot handle 64-bit clusters. Sorry.");
  766. return FALSE;
  767. }
  768. vol->nr_clusters = ll;
  769. ntfs_debug("vol->nr_clusters = 0x%llx", (long long)vol->nr_clusters);
  770. /*
  771. * On an architecture where unsigned long is 32-bits, we restrict the
  772. * volume size to 2TiB (2^41). On a 64-bit architecture, the compiler
  773. * will hopefully optimize the whole check away.
  774. */
  775. if (sizeof(unsigned long) < 8) {
  776. if ((ll << vol->cluster_size_bits) >= (1ULL << 41)) {
  777. ntfs_error(vol->sb, "Volume size (%lluTiB) is too "
  778. "large for this architecture. "
  779. "Maximum supported is 2TiB. Sorry.",
  780. (unsigned long long)ll >> (40 -
  781. vol->cluster_size_bits));
  782. return FALSE;
  783. }
  784. }
  785. ll = sle64_to_cpu(b->mft_lcn);
  786. if (ll >= vol->nr_clusters) {
  787. ntfs_error(vol->sb, "MFT LCN is beyond end of volume. Weird.");
  788. return FALSE;
  789. }
  790. vol->mft_lcn = ll;
  791. ntfs_debug("vol->mft_lcn = 0x%llx", (long long)vol->mft_lcn);
  792. ll = sle64_to_cpu(b->mftmirr_lcn);
  793. if (ll >= vol->nr_clusters) {
  794. ntfs_error(vol->sb, "MFTMirr LCN is beyond end of volume. "
  795. "Weird.");
  796. return FALSE;
  797. }
  798. vol->mftmirr_lcn = ll;
  799. ntfs_debug("vol->mftmirr_lcn = 0x%llx", (long long)vol->mftmirr_lcn);
  800. #ifdef NTFS_RW
  801. /*
  802. * Work out the size of the mft mirror in number of mft records. If the
  803. * cluster size is less than or equal to the size taken by four mft
  804. * records, the mft mirror stores the first four mft records. If the
  805. * cluster size is bigger than the size taken by four mft records, the
  806. * mft mirror contains as many mft records as will fit into one
  807. * cluster.
  808. */
  809. if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
  810. vol->mftmirr_size = 4;
  811. else
  812. vol->mftmirr_size = vol->cluster_size >>
  813. vol->mft_record_size_bits;
  814. ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
  815. #endif /* NTFS_RW */
  816. vol->serial_no = le64_to_cpu(b->volume_serial_number);
  817. ntfs_debug("vol->serial_no = 0x%llx",
  818. (unsigned long long)vol->serial_no);
  819. return TRUE;
  820. }
  821. /**
  822. * ntfs_setup_allocators - initialize the cluster and mft allocators
  823. * @vol: volume structure for which to setup the allocators
  824. *
  825. * Setup the cluster (lcn) and mft allocators to the starting values.
  826. */
  827. static void ntfs_setup_allocators(ntfs_volume *vol)
  828. {
  829. #ifdef NTFS_RW
  830. LCN mft_zone_size, mft_lcn;
  831. #endif /* NTFS_RW */
  832. ntfs_debug("vol->mft_zone_multiplier = 0x%x",
  833. vol->mft_zone_multiplier);
  834. #ifdef NTFS_RW
  835. /* Determine the size of the MFT zone. */
  836. mft_zone_size = vol->nr_clusters;
  837. switch (vol->mft_zone_multiplier) { /* % of volume size in clusters */
  838. case 4:
  839. mft_zone_size >>= 1; /* 50% */
  840. break;
  841. case 3:
  842. mft_zone_size = (mft_zone_size +
  843. (mft_zone_size >> 1)) >> 2; /* 37.5% */
  844. break;
  845. case 2:
  846. mft_zone_size >>= 2; /* 25% */
  847. break;
  848. /* case 1: */
  849. default:
  850. mft_zone_size >>= 3; /* 12.5% */
  851. break;
  852. }
  853. /* Setup the mft zone. */
  854. vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
  855. ntfs_debug("vol->mft_zone_pos = 0x%llx",
  856. (unsigned long long)vol->mft_zone_pos);
  857. /*
  858. * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs
  859. * source) and if the actual mft_lcn is in the expected place or even
  860. * further to the front of the volume, extend the mft_zone to cover the
  861. * beginning of the volume as well. This is in order to protect the
  862. * area reserved for the mft bitmap as well within the mft_zone itself.
  863. * On non-standard volumes we do not protect it as the overhead would
  864. * be higher than the speed increase we would get by doing it.
  865. */
  866. mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size;
  867. if (mft_lcn * vol->cluster_size < 16 * 1024)
  868. mft_lcn = (16 * 1024 + vol->cluster_size - 1) /
  869. vol->cluster_size;
  870. if (vol->mft_zone_start <= mft_lcn)
  871. vol->mft_zone_start = 0;
  872. ntfs_debug("vol->mft_zone_start = 0x%llx",
  873. (unsigned long long)vol->mft_zone_start);
  874. /*
  875. * Need to cap the mft zone on non-standard volumes so that it does
  876. * not point outside the boundaries of the volume. We do this by
  877. * halving the zone size until we are inside the volume.
  878. */
  879. vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
  880. while (vol->mft_zone_end >= vol->nr_clusters) {
  881. mft_zone_size >>= 1;
  882. vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
  883. }
  884. ntfs_debug("vol->mft_zone_end = 0x%llx",
  885. (unsigned long long)vol->mft_zone_end);
  886. /*
  887. * Set the current position within each data zone to the start of the
  888. * respective zone.
  889. */
  890. vol->data1_zone_pos = vol->mft_zone_end;
  891. ntfs_debug("vol->data1_zone_pos = 0x%llx",
  892. (unsigned long long)vol->data1_zone_pos);
  893. vol->data2_zone_pos = 0;
  894. ntfs_debug("vol->data2_zone_pos = 0x%llx",
  895. (unsigned long long)vol->data2_zone_pos);
  896. /* Set the mft data allocation position to mft record 24. */
  897. vol->mft_data_pos = 24;
  898. ntfs_debug("vol->mft_data_pos = 0x%llx",
  899. (unsigned long long)vol->mft_data_pos);
  900. #endif /* NTFS_RW */
  901. }
  902. #ifdef NTFS_RW
  903. /**
  904. * load_and_init_mft_mirror - load and setup the mft mirror inode for a volume
  905. * @vol: ntfs super block describing device whose mft mirror to load
  906. *
  907. * Return TRUE on success or FALSE on error.
  908. */
  909. static BOOL load_and_init_mft_mirror(ntfs_volume *vol)
  910. {
  911. struct inode *tmp_ino;
  912. ntfs_inode *tmp_ni;
  913. ntfs_debug("Entering.");
  914. /* Get mft mirror inode. */
  915. tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
  916. if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
  917. if (!IS_ERR(tmp_ino))
  918. iput(tmp_ino);
  919. /* Caller will display error message. */
  920. return FALSE;
  921. }
  922. /*
  923. * Re-initialize some specifics about $MFTMirr's inode as
  924. * ntfs_read_inode() will have set up the default ones.
  925. */
  926. /* Set uid and gid to root. */
  927. tmp_ino->i_uid = tmp_ino->i_gid = 0;
  928. /* Regular file. No access for anyone. */
  929. tmp_ino->i_mode = S_IFREG;
  930. /* No VFS initiated operations allowed for $MFTMirr. */
  931. tmp_ino->i_op = &ntfs_empty_inode_ops;
  932. tmp_ino->i_fop = &ntfs_empty_file_ops;
  933. /* Put in our special address space operations. */
  934. tmp_ino->i_mapping->a_ops = &ntfs_mst_aops;
  935. tmp_ni = NTFS_I(tmp_ino);
  936. /* The $MFTMirr, like the $MFT is multi sector transfer protected. */
  937. NInoSetMstProtected(tmp_ni);
  938. /*
  939. * Set up our little cheat allowing us to reuse the async read io
  940. * completion handler for directories.
  941. */
  942. tmp_ni->itype.index.block_size = vol->mft_record_size;
  943. tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
  944. vol->mftmirr_ino = tmp_ino;
  945. ntfs_debug("Done.");
  946. return TRUE;
  947. }
  948. /**
  949. * check_mft_mirror - compare contents of the mft mirror with the mft
  950. * @vol: ntfs super block describing device whose mft mirror to check
  951. *
  952. * Return TRUE on success or FALSE on error.
  953. *
  954. * Note, this function also results in the mft mirror runlist being completely
  955. * mapped into memory. The mft mirror write code requires this and will BUG()
  956. * should it find an unmapped runlist element.
  957. */
  958. static BOOL check_mft_mirror(ntfs_volume *vol)
  959. {
  960. unsigned long index;
  961. struct super_block *sb = vol->sb;
  962. ntfs_inode *mirr_ni;
  963. struct page *mft_page, *mirr_page;
  964. u8 *kmft, *kmirr;
  965. runlist_element *rl, rl2[2];
  966. int mrecs_per_page, i;
  967. ntfs_debug("Entering.");
  968. /* Compare contents of $MFT and $MFTMirr. */
  969. mrecs_per_page = PAGE_CACHE_SIZE / vol->mft_record_size;
  970. BUG_ON(!mrecs_per_page);
  971. BUG_ON(!vol->mftmirr_size);
  972. mft_page = mirr_page = NULL;
  973. kmft = kmirr = NULL;
  974. index = i = 0;
  975. do {
  976. u32 bytes;
  977. /* Switch pages if necessary. */
  978. if (!(i % mrecs_per_page)) {
  979. if (index) {
  980. ntfs_unmap_page(mft_page);
  981. ntfs_unmap_page(mirr_page);
  982. }
  983. /* Get the $MFT page. */
  984. mft_page = ntfs_map_page(vol->mft_ino->i_mapping,
  985. index);
  986. if (IS_ERR(mft_page)) {
  987. ntfs_error(sb, "Failed to read $MFT.");
  988. return FALSE;
  989. }
  990. kmft = page_address(mft_page);
  991. /* Get the $MFTMirr page. */
  992. mirr_page = ntfs_map_page(vol->mftmirr_ino->i_mapping,
  993. index);
  994. if (IS_ERR(mirr_page)) {
  995. ntfs_error(sb, "Failed to read $MFTMirr.");
  996. goto mft_unmap_out;
  997. }
  998. kmirr = page_address(mirr_page);
  999. ++index;
  1000. }
  1001. /* Make sure the record is ok. */
  1002. if (ntfs_is_baad_recordp((le32*)kmft)) {
  1003. ntfs_error(sb, "Incomplete multi sector transfer "
  1004. "detected in mft record %i.", i);
  1005. mm_unmap_out:
  1006. ntfs_unmap_page(mirr_page);
  1007. mft_unmap_out:
  1008. ntfs_unmap_page(mft_page);
  1009. return FALSE;
  1010. }
  1011. if (ntfs_is_baad_recordp((le32*)kmirr)) {
  1012. ntfs_error(sb, "Incomplete multi sector transfer "
  1013. "detected in mft mirror record %i.", i);
  1014. goto mm_unmap_out;
  1015. }
  1016. /* Get the amount of data in the current record. */
  1017. bytes = le32_to_cpu(((MFT_RECORD*)kmft)->bytes_in_use);
  1018. if (!bytes || bytes > vol->mft_record_size) {
  1019. bytes = le32_to_cpu(((MFT_RECORD*)kmirr)->bytes_in_use);
  1020. if (!bytes || bytes > vol->mft_record_size)
  1021. bytes = vol->mft_record_size;
  1022. }
  1023. /* Compare the two records. */
  1024. if (memcmp(kmft, kmirr, bytes)) {
  1025. ntfs_error(sb, "$MFT and $MFTMirr (record %i) do not "
  1026. "match. Run ntfsfix or chkdsk.", i);
  1027. goto mm_unmap_out;
  1028. }
  1029. kmft += vol->mft_record_size;
  1030. kmirr += vol->mft_record_size;
  1031. } while (++i < vol->mftmirr_size);
  1032. /* Release the last pages. */
  1033. ntfs_unmap_page(mft_page);
  1034. ntfs_unmap_page(mirr_page);
  1035. /* Construct the mft mirror runlist by hand. */
  1036. rl2[0].vcn = 0;
  1037. rl2[0].lcn = vol->mftmirr_lcn;
  1038. rl2[0].length = (vol->mftmirr_size * vol->mft_record_size +
  1039. vol->cluster_size - 1) / vol->cluster_size;
  1040. rl2[1].vcn = rl2[0].length;
  1041. rl2[1].lcn = LCN_ENOENT;
  1042. rl2[1].length = 0;
  1043. /*
  1044. * Because we have just read all of the mft mirror, we know we have
  1045. * mapped the full runlist for it.
  1046. */
  1047. mirr_ni = NTFS_I(vol->mftmirr_ino);
  1048. down_read(&mirr_ni->runlist.lock);
  1049. rl = mirr_ni->runlist.rl;
  1050. /* Compare the two runlists. They must be identical. */
  1051. i = 0;
  1052. do {
  1053. if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
  1054. rl2[i].length != rl[i].length) {
  1055. ntfs_error(sb, "$MFTMirr location mismatch. "
  1056. "Run chkdsk.");
  1057. up_read(&mirr_ni->runlist.lock);
  1058. return FALSE;
  1059. }
  1060. } while (rl2[i++].length);
  1061. up_read(&mirr_ni->runlist.lock);
  1062. ntfs_debug("Done.");
  1063. return TRUE;
  1064. }
  1065. /**
  1066. * load_and_check_logfile - load and check the logfile inode for a volume
  1067. * @vol: ntfs super block describing device whose logfile to load
  1068. *
  1069. * Return TRUE on success or FALSE on error.
  1070. */
  1071. static BOOL load_and_check_logfile(ntfs_volume *vol)
  1072. {
  1073. struct inode *tmp_ino;
  1074. ntfs_debug("Entering.");
  1075. tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
  1076. if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
  1077. if (!IS_ERR(tmp_ino))
  1078. iput(tmp_ino);
  1079. /* Caller will display error message. */
  1080. return FALSE;
  1081. }
  1082. if (!ntfs_check_logfile(tmp_ino)) {
  1083. iput(tmp_ino);
  1084. /* ntfs_check_logfile() will have displayed error output. */
  1085. return FALSE;
  1086. }
  1087. vol->logfile_ino = tmp_ino;
  1088. ntfs_debug("Done.");
  1089. return TRUE;
  1090. }
  1091. /**
  1092. * load_and_init_quota - load and setup the quota file for a volume if present
  1093. * @vol: ntfs super block describing device whose quota file to load
  1094. *
  1095. * Return TRUE on success or FALSE on error. If $Quota is not present, we
  1096. * leave vol->quota_ino as NULL and return success.
  1097. */
  1098. static BOOL load_and_init_quota(ntfs_volume *vol)
  1099. {
  1100. MFT_REF mref;
  1101. struct inode *tmp_ino;
  1102. ntfs_name *name = NULL;
  1103. static const ntfschar Quota[7] = { const_cpu_to_le16('$'),
  1104. const_cpu_to_le16('Q'), const_cpu_to_le16('u'),
  1105. const_cpu_to_le16('o'), const_cpu_to_le16('t'),
  1106. const_cpu_to_le16('a'), 0 };
  1107. static ntfschar Q[3] = { const_cpu_to_le16('$'),
  1108. const_cpu_to_le16('Q'), 0 };
  1109. ntfs_debug("Entering.");
  1110. /*
  1111. * Find the inode number for the quota file by looking up the filename
  1112. * $Quota in the extended system files directory $Extend.
  1113. */
  1114. down(&vol->extend_ino->i_sem);
  1115. mref = ntfs_lookup_inode_by_name(NTFS_I(vol->extend_ino), Quota, 6,
  1116. &name);
  1117. up(&vol->extend_ino->i_sem);
  1118. if (IS_ERR_MREF(mref)) {
  1119. /*
  1120. * If the file does not exist, quotas are disabled and have
  1121. * never been enabled on this volume, just return success.
  1122. */
  1123. if (MREF_ERR(mref) == -ENOENT) {
  1124. ntfs_debug("$Quota not present. Volume does not have "
  1125. "quotas enabled.");
  1126. /*
  1127. * No need to try to set quotas out of date if they are
  1128. * not enabled.
  1129. */
  1130. NVolSetQuotaOutOfDate(vol);
  1131. return TRUE;
  1132. }
  1133. /* A real error occured. */
  1134. ntfs_error(vol->sb, "Failed to find inode number for $Quota.");
  1135. return FALSE;
  1136. }
  1137. /* We do not care for the type of match that was found. */
  1138. if (name)
  1139. kfree(name);
  1140. /* Get the inode. */
  1141. tmp_ino = ntfs_iget(vol->sb, MREF(mref));
  1142. if (IS_ERR(tmp_ino) || is_bad_inode(tmp_ino)) {
  1143. if (!IS_ERR(tmp_ino))
  1144. iput(tmp_ino);
  1145. ntfs_error(vol->sb, "Failed to load $Quota.");
  1146. return FALSE;
  1147. }
  1148. vol->quota_ino = tmp_ino;
  1149. /* Get the $Q index allocation attribute. */
  1150. tmp_ino = ntfs_index_iget(vol->quota_ino, Q, 2);
  1151. if (IS_ERR(tmp_ino)) {
  1152. ntfs_error(vol->sb, "Failed to load $Quota/$Q index.");
  1153. return FALSE;
  1154. }
  1155. vol->quota_q_ino = tmp_ino;
  1156. ntfs_debug("Done.");
  1157. return TRUE;
  1158. }
  1159. /**
  1160. * load_and_init_attrdef - load the attribute definitions table for a volume
  1161. * @vol: ntfs super block describing device whose attrdef to load
  1162. *
  1163. * Return TRUE on success or FALSE on error.
  1164. */
  1165. static BOOL load_and_init_attrdef(ntfs_volume *vol)
  1166. {
  1167. struct super_block *sb = vol->sb;
  1168. struct inode *ino;
  1169. struct page *page;
  1170. unsigned long index, max_index;
  1171. unsigned int size;
  1172. ntfs_debug("Entering.");
  1173. /* Read attrdef table and setup vol->attrdef and vol->attrdef_size. */
  1174. ino = ntfs_iget(sb, FILE_AttrDef);
  1175. if (IS_ERR(ino) || is_bad_inode(ino)) {
  1176. if (!IS_ERR(ino))
  1177. iput(ino);
  1178. goto failed;
  1179. }
  1180. /* The size of FILE_AttrDef must be above 0 and fit inside 31 bits. */
  1181. if (!ino->i_size || ino->i_size > 0x7fffffff)
  1182. goto iput_failed;
  1183. vol->attrdef = (ATTR_DEF*)ntfs_malloc_nofs(ino->i_size);
  1184. if (!vol->attrdef)
  1185. goto iput_failed;
  1186. index = 0;
  1187. max_index = ino->i_size >> PAGE_CACHE_SHIFT;
  1188. size = PAGE_CACHE_SIZE;
  1189. while (index < max_index) {
  1190. /* Read the attrdef table and copy it into the linear buffer. */
  1191. read_partial_attrdef_page:
  1192. page = ntfs_map_page(ino->i_mapping, index);
  1193. if (IS_ERR(page))
  1194. goto free_iput_failed;
  1195. memcpy((u8*)vol->attrdef + (index++ << PAGE_CACHE_SHIFT),
  1196. page_address(page), size);
  1197. ntfs_unmap_page(page);
  1198. };
  1199. if (size == PAGE_CACHE_SIZE) {
  1200. size = ino->i_size & ~PAGE_CACHE_MASK;
  1201. if (size)
  1202. goto read_partial_attrdef_page;
  1203. }
  1204. vol->attrdef_size = ino->i_size;
  1205. ntfs_debug("Read %llu bytes from $AttrDef.", ino->i_size);
  1206. iput(ino);
  1207. return TRUE;
  1208. free_iput_failed:
  1209. ntfs_free(vol->attrdef);
  1210. vol->attrdef = NULL;
  1211. iput_failed:
  1212. iput(ino);
  1213. failed:
  1214. ntfs_error(sb, "Failed to initialize attribute definition table.");
  1215. return FALSE;
  1216. }
  1217. #endif /* NTFS_RW */
  1218. /**
  1219. * load_and_init_upcase - load the upcase table for an ntfs volume
  1220. * @vol: ntfs super block describing device whose upcase to load
  1221. *
  1222. * Return TRUE on success or FALSE on error.
  1223. */
  1224. static BOOL load_and_init_upcase(ntfs_volume *vol)
  1225. {
  1226. struct super_block *sb = vol->sb;
  1227. struct inode *ino;
  1228. struct page *page;
  1229. unsigned long index, max_index;
  1230. unsigned int size;
  1231. int i, max;
  1232. ntfs_debug("Entering.");
  1233. /* Read upcase table and setup vol->upcase and vol->upcase_len. */
  1234. ino = ntfs_iget(sb, FILE_UpCase);
  1235. if (IS_ERR(ino) || is_bad_inode(ino)) {
  1236. if (!IS_ERR(ino))
  1237. iput(ino);
  1238. goto upcase_failed;
  1239. }
  1240. /*
  1241. * The upcase size must not be above 64k Unicode characters, must not
  1242. * be zero and must be a multiple of sizeof(ntfschar).
  1243. */
  1244. if (!ino->i_size || ino->i_size & (sizeof(ntfschar) - 1) ||
  1245. ino->i_size > 64ULL * 1024 * sizeof(ntfschar))
  1246. goto iput_upcase_failed;
  1247. vol->upcase = (ntfschar*)ntfs_malloc_nofs(ino->i_size);
  1248. if (!vol->upcase)
  1249. goto iput_upcase_failed;
  1250. index = 0;
  1251. max_index = ino->i_size >> PAGE_CACHE_SHIFT;
  1252. size = PAGE_CACHE_SIZE;
  1253. while (index < max_index) {
  1254. /* Read the upcase table and copy it into the linear buffer. */
  1255. read_partial_upcase_page:
  1256. page = ntfs_map_page(ino->i_mapping, index);
  1257. if (IS_ERR(page))
  1258. goto iput_upcase_failed;
  1259. memcpy((char*)vol->upcase + (index++ << PAGE_CACHE_SHIFT),
  1260. page_address(page), size);
  1261. ntfs_unmap_page(page);
  1262. };
  1263. if (size == PAGE_CACHE_SIZE) {
  1264. size = ino->i_size & ~PAGE_CACHE_MASK;
  1265. if (size)
  1266. goto read_partial_upcase_page;
  1267. }
  1268. vol->upcase_len = ino->i_size >> UCHAR_T_SIZE_BITS;
  1269. ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
  1270. ino->i_size, 64 * 1024 * sizeof(ntfschar));
  1271. iput(ino);
  1272. down(&ntfs_lock);
  1273. if (!default_upcase) {
  1274. ntfs_debug("Using volume specified $UpCase since default is "
  1275. "not present.");
  1276. up(&ntfs_lock);
  1277. return TRUE;
  1278. }
  1279. max = default_upcase_len;
  1280. if (max > vol->upcase_len)
  1281. max = vol->upcase_len;
  1282. for (i = 0; i < max; i++)
  1283. if (vol->upcase[i] != default_upcase[i])
  1284. break;
  1285. if (i == max) {
  1286. ntfs_free(vol->upcase);
  1287. vol->upcase = default_upcase;
  1288. vol->upcase_len = max;
  1289. ntfs_nr_upcase_users++;
  1290. up(&ntfs_lock);
  1291. ntfs_debug("Volume specified $UpCase matches default. Using "
  1292. "default.");
  1293. return TRUE;
  1294. }
  1295. up(&ntfs_lock);
  1296. ntfs_debug("Using volume specified $UpCase since it does not match "
  1297. "the default.");
  1298. return TRUE;
  1299. iput_upcase_failed:
  1300. iput(ino);
  1301. ntfs_free(vol->upcase);
  1302. vol->upcase = NULL;
  1303. upcase_failed:
  1304. down(&ntfs_lock);
  1305. if (default_upcase) {
  1306. vol->upcase = default_upcase;
  1307. vol->upcase_len = default_upcase_len;
  1308. ntfs_nr_upcase_users++;
  1309. up(&ntfs_lock);
  1310. ntfs_error(sb, "Failed to load $UpCase from the volume. Using "
  1311. "default.");
  1312. return TRUE;
  1313. }
  1314. up(&ntfs_lock);
  1315. ntfs_error(sb, "Failed to initialize upcase table.");
  1316. return FALSE;
  1317. }
  1318. /**
  1319. * load_system_files - open the system files using normal functions
  1320. * @vol: ntfs super block describing device whose system files to load
  1321. *
  1322. * Open the system files with normal access functions and complete setting up
  1323. * the ntfs super block @vol.
  1324. *
  1325. * Return TRUE on success or FALSE on error.
  1326. */
  1327. static BOOL load_system_files(ntfs_volume *vol)
  1328. {
  1329. struct super_block *sb = vol->sb;
  1330. MFT_RECORD *m;
  1331. VOLUME_INFORMATION *vi;
  1332. ntfs_attr_search_ctx *ctx;
  1333. ntfs_debug("Entering.");
  1334. #ifdef NTFS_RW
  1335. /* Get mft mirror inode compare the contents of $MFT and $MFTMirr. */
  1336. if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
  1337. static const char *es1 = "Failed to load $MFTMirr";
  1338. static const char *es2 = "$MFTMirr does not match $MFT";
  1339. static const char *es3 = ". Run ntfsfix and/or chkdsk.";
  1340. /* If a read-write mount, convert it to a read-only mount. */
  1341. if (!(sb->s_flags & MS_RDONLY)) {
  1342. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1343. ON_ERRORS_CONTINUE))) {
  1344. ntfs_error(sb, "%s and neither on_errors="
  1345. "continue nor on_errors="
  1346. "remount-ro was specified%s",
  1347. !vol->mftmirr_ino ? es1 : es2,
  1348. es3);
  1349. goto iput_mirr_err_out;
  1350. }
  1351. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1352. ntfs_error(sb, "%s. Mounting read-only%s",
  1353. !vol->mftmirr_ino ? es1 : es2, es3);
  1354. } else
  1355. ntfs_warning(sb, "%s. Will not be able to remount "
  1356. "read-write%s",
  1357. !vol->mftmirr_ino ? es1 : es2, es3);
  1358. /* This will prevent a read-write remount. */
  1359. NVolSetErrors(vol);
  1360. }
  1361. #endif /* NTFS_RW */
  1362. /* Get mft bitmap attribute inode. */
  1363. vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
  1364. if (IS_ERR(vol->mftbmp_ino)) {
  1365. ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
  1366. goto iput_mirr_err_out;
  1367. }
  1368. /* Read upcase table and setup @vol->upcase and @vol->upcase_len. */
  1369. if (!load_and_init_upcase(vol))
  1370. goto iput_mftbmp_err_out;
  1371. #ifdef NTFS_RW
  1372. /*
  1373. * Read attribute definitions table and setup @vol->attrdef and
  1374. * @vol->attrdef_size.
  1375. */
  1376. if (!load_and_init_attrdef(vol))
  1377. goto iput_upcase_err_out;
  1378. #endif /* NTFS_RW */
  1379. /*
  1380. * Get the cluster allocation bitmap inode and verify the size, no
  1381. * need for any locking at this stage as we are already running
  1382. * exclusively as we are mount in progress task.
  1383. */
  1384. vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
  1385. if (IS_ERR(vol->lcnbmp_ino) || is_bad_inode(vol->lcnbmp_ino)) {
  1386. if (!IS_ERR(vol->lcnbmp_ino))
  1387. iput(vol->lcnbmp_ino);
  1388. goto bitmap_failed;
  1389. }
  1390. if ((vol->nr_clusters + 7) >> 3 > vol->lcnbmp_ino->i_size) {
  1391. iput(vol->lcnbmp_ino);
  1392. bitmap_failed:
  1393. ntfs_error(sb, "Failed to load $Bitmap.");
  1394. goto iput_attrdef_err_out;
  1395. }
  1396. /*
  1397. * Get the volume inode and setup our cache of the volume flags and
  1398. * version.
  1399. */
  1400. vol->vol_ino = ntfs_iget(sb, FILE_Volume);
  1401. if (IS_ERR(vol->vol_ino) || is_bad_inode(vol->vol_ino)) {
  1402. if (!IS_ERR(vol->vol_ino))
  1403. iput(vol->vol_ino);
  1404. volume_failed:
  1405. ntfs_error(sb, "Failed to load $Volume.");
  1406. goto iput_lcnbmp_err_out;
  1407. }
  1408. m = map_mft_record(NTFS_I(vol->vol_ino));
  1409. if (IS_ERR(m)) {
  1410. iput_volume_failed:
  1411. iput(vol->vol_ino);
  1412. goto volume_failed;
  1413. }
  1414. if (!(ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m))) {
  1415. ntfs_error(sb, "Failed to get attribute search context.");
  1416. goto get_ctx_vol_failed;
  1417. }
  1418. if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
  1419. ctx) || ctx->attr->non_resident || ctx->attr->flags) {
  1420. err_put_vol:
  1421. ntfs_attr_put_search_ctx(ctx);
  1422. get_ctx_vol_failed:
  1423. unmap_mft_record(NTFS_I(vol->vol_ino));
  1424. goto iput_volume_failed;
  1425. }
  1426. vi = (VOLUME_INFORMATION*)((char*)ctx->attr +
  1427. le16_to_cpu(ctx->attr->data.resident.value_offset));
  1428. /* Some bounds checks. */
  1429. if ((u8*)vi < (u8*)ctx->attr || (u8*)vi +
  1430. le32_to_cpu(ctx->attr->data.resident.value_length) >
  1431. (u8*)ctx->attr + le32_to_cpu(ctx->attr->length))
  1432. goto err_put_vol;
  1433. /* Copy the volume flags and version to the ntfs_volume structure. */
  1434. vol->vol_flags = vi->flags;
  1435. vol->major_ver = vi->major_ver;
  1436. vol->minor_ver = vi->minor_ver;
  1437. ntfs_attr_put_search_ctx(ctx);
  1438. unmap_mft_record(NTFS_I(vol->vol_ino));
  1439. printk(KERN_INFO "NTFS volume version %i.%i.\n", vol->major_ver,
  1440. vol->minor_ver);
  1441. #ifdef NTFS_RW
  1442. /* Make sure that no unsupported volume flags are set. */
  1443. if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
  1444. static const char *es1a = "Volume is dirty";
  1445. static const char *es1b = "Volume has unsupported flags set";
  1446. static const char *es2 = ". Run chkdsk and mount in Windows.";
  1447. const char *es1;
  1448. es1 = vol->vol_flags & VOLUME_IS_DIRTY ? es1a : es1b;
  1449. /* If a read-write mount, convert it to a read-only mount. */
  1450. if (!(sb->s_flags & MS_RDONLY)) {
  1451. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1452. ON_ERRORS_CONTINUE))) {
  1453. ntfs_error(sb, "%s and neither on_errors="
  1454. "continue nor on_errors="
  1455. "remount-ro was specified%s",
  1456. es1, es2);
  1457. goto iput_vol_err_out;
  1458. }
  1459. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1460. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1461. } else
  1462. ntfs_warning(sb, "%s. Will not be able to remount "
  1463. "read-write%s", es1, es2);
  1464. /*
  1465. * Do not set NVolErrors() because ntfs_remount() re-checks the
  1466. * flags which we need to do in case any flags have changed.
  1467. */
  1468. }
  1469. /*
  1470. * Get the inode for the logfile, check it and determine if the volume
  1471. * was shutdown cleanly.
  1472. */
  1473. if (!load_and_check_logfile(vol) ||
  1474. !ntfs_is_logfile_clean(vol->logfile_ino)) {
  1475. static const char *es1a = "Failed to load $LogFile";
  1476. static const char *es1b = "$LogFile is not clean";
  1477. static const char *es2 = ". Mount in Windows.";
  1478. const char *es1;
  1479. es1 = !vol->logfile_ino ? es1a : es1b;
  1480. /* If a read-write mount, convert it to a read-only mount. */
  1481. if (!(sb->s_flags & MS_RDONLY)) {
  1482. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1483. ON_ERRORS_CONTINUE))) {
  1484. ntfs_error(sb, "%s and neither on_errors="
  1485. "continue nor on_errors="
  1486. "remount-ro was specified%s",
  1487. es1, es2);
  1488. goto iput_logfile_err_out;
  1489. }
  1490. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1491. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1492. } else
  1493. ntfs_warning(sb, "%s. Will not be able to remount "
  1494. "read-write%s", es1, es2);
  1495. /* This will prevent a read-write remount. */
  1496. NVolSetErrors(vol);
  1497. }
  1498. /* If (still) a read-write mount, mark the volume dirty. */
  1499. if (!(sb->s_flags & MS_RDONLY) &&
  1500. ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY)) {
  1501. static const char *es1 = "Failed to set dirty bit in volume "
  1502. "information flags";
  1503. static const char *es2 = ". Run chkdsk.";
  1504. /* Convert to a read-only mount. */
  1505. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1506. ON_ERRORS_CONTINUE))) {
  1507. ntfs_error(sb, "%s and neither on_errors=continue nor "
  1508. "on_errors=remount-ro was specified%s",
  1509. es1, es2);
  1510. goto iput_logfile_err_out;
  1511. }
  1512. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1513. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1514. /*
  1515. * Do not set NVolErrors() because ntfs_remount() might manage
  1516. * to set the dirty flag in which case all would be well.
  1517. */
  1518. }
  1519. #if 0
  1520. // TODO: Enable this code once we start modifying anything that is
  1521. // different between NTFS 1.2 and 3.x...
  1522. /*
  1523. * If (still) a read-write mount, set the NT4 compatibility flag on
  1524. * newer NTFS version volumes.
  1525. */
  1526. if (!(sb->s_flags & MS_RDONLY) && (vol->major_ver > 1) &&
  1527. ntfs_set_volume_flags(vol, VOLUME_MOUNTED_ON_NT4)) {
  1528. static const char *es1 = "Failed to set NT4 compatibility flag";
  1529. static const char *es2 = ". Run chkdsk.";
  1530. /* Convert to a read-only mount. */
  1531. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1532. ON_ERRORS_CONTINUE))) {
  1533. ntfs_error(sb, "%s and neither on_errors=continue nor "
  1534. "on_errors=remount-ro was specified%s",
  1535. es1, es2);
  1536. goto iput_logfile_err_out;
  1537. }
  1538. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1539. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1540. NVolSetErrors(vol);
  1541. }
  1542. #endif
  1543. /* If (still) a read-write mount, empty the logfile. */
  1544. if (!(sb->s_flags & MS_RDONLY) &&
  1545. !ntfs_empty_logfile(vol->logfile_ino)) {
  1546. static const char *es1 = "Failed to empty $LogFile";
  1547. static const char *es2 = ". Mount in Windows.";
  1548. /* Convert to a read-only mount. */
  1549. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1550. ON_ERRORS_CONTINUE))) {
  1551. ntfs_error(sb, "%s and neither on_errors=continue nor "
  1552. "on_errors=remount-ro was specified%s",
  1553. es1, es2);
  1554. goto iput_logfile_err_out;
  1555. }
  1556. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1557. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1558. NVolSetErrors(vol);
  1559. }
  1560. #endif /* NTFS_RW */
  1561. /* Get the root directory inode. */
  1562. vol->root_ino = ntfs_iget(sb, FILE_root);
  1563. if (IS_ERR(vol->root_ino) || is_bad_inode(vol->root_ino)) {
  1564. if (!IS_ERR(vol->root_ino))
  1565. iput(vol->root_ino);
  1566. ntfs_error(sb, "Failed to load root directory.");
  1567. goto iput_logfile_err_out;
  1568. }
  1569. /* If on NTFS versions before 3.0, we are done. */
  1570. if (vol->major_ver < 3)
  1571. return TRUE;
  1572. /* NTFS 3.0+ specific initialization. */
  1573. /* Get the security descriptors inode. */
  1574. vol->secure_ino = ntfs_iget(sb, FILE_Secure);
  1575. if (IS_ERR(vol->secure_ino) || is_bad_inode(vol->secure_ino)) {
  1576. if (!IS_ERR(vol->secure_ino))
  1577. iput(vol->secure_ino);
  1578. ntfs_error(sb, "Failed to load $Secure.");
  1579. goto iput_root_err_out;
  1580. }
  1581. // FIXME: Initialize security.
  1582. /* Get the extended system files' directory inode. */
  1583. vol->extend_ino = ntfs_iget(sb, FILE_Extend);
  1584. if (IS_ERR(vol->extend_ino) || is_bad_inode(vol->extend_ino)) {
  1585. if (!IS_ERR(vol->extend_ino))
  1586. iput(vol->extend_ino);
  1587. ntfs_error(sb, "Failed to load $Extend.");
  1588. goto iput_sec_err_out;
  1589. }
  1590. #ifdef NTFS_RW
  1591. /* Find the quota file, load it if present, and set it up. */
  1592. if (!load_and_init_quota(vol)) {
  1593. static const char *es1 = "Failed to load $Quota";
  1594. static const char *es2 = ". Run chkdsk.";
  1595. /* If a read-write mount, convert it to a read-only mount. */
  1596. if (!(sb->s_flags & MS_RDONLY)) {
  1597. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1598. ON_ERRORS_CONTINUE))) {
  1599. ntfs_error(sb, "%s and neither on_errors="
  1600. "continue nor on_errors="
  1601. "remount-ro was specified%s",
  1602. es1, es2);
  1603. goto iput_quota_err_out;
  1604. }
  1605. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1606. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1607. } else
  1608. ntfs_warning(sb, "%s. Will not be able to remount "
  1609. "read-write%s", es1, es2);
  1610. /* This will prevent a read-write remount. */
  1611. NVolSetErrors(vol);
  1612. }
  1613. /* If (still) a read-write mount, mark the quotas out of date. */
  1614. if (!(sb->s_flags & MS_RDONLY) &&
  1615. !ntfs_mark_quotas_out_of_date(vol)) {
  1616. static const char *es1 = "Failed to mark quotas out of date";
  1617. static const char *es2 = ". Run chkdsk.";
  1618. /* Convert to a read-only mount. */
  1619. if (!(vol->on_errors & (ON_ERRORS_REMOUNT_RO |
  1620. ON_ERRORS_CONTINUE))) {
  1621. ntfs_error(sb, "%s and neither on_errors=continue nor "
  1622. "on_errors=remount-ro was specified%s",
  1623. es1, es2);
  1624. goto iput_quota_err_out;
  1625. }
  1626. ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
  1627. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  1628. NVolSetErrors(vol);
  1629. }
  1630. // TODO: Delete or checkpoint the $UsnJrnl if it exists.
  1631. #endif /* NTFS_RW */
  1632. return TRUE;
  1633. #ifdef NTFS_RW
  1634. iput_quota_err_out:
  1635. if (vol->quota_q_ino)
  1636. iput(vol->quota_q_ino);
  1637. if (vol->quota_ino)
  1638. iput(vol->quota_ino);
  1639. iput(vol->extend_ino);
  1640. #endif /* NTFS_RW */
  1641. iput_sec_err_out:
  1642. iput(vol->secure_ino);
  1643. iput_root_err_out:
  1644. iput(vol->root_ino);
  1645. iput_logfile_err_out:
  1646. #ifdef NTFS_RW
  1647. if (vol->logfile_ino)
  1648. iput(vol->logfile_ino);
  1649. iput_vol_err_out:
  1650. #endif /* NTFS_RW */
  1651. iput(vol->vol_ino);
  1652. iput_lcnbmp_err_out:
  1653. iput(vol->lcnbmp_ino);
  1654. iput_attrdef_err_out:
  1655. vol->attrdef_size = 0;
  1656. if (vol->attrdef) {
  1657. ntfs_free(vol->attrdef);
  1658. vol->attrdef = NULL;
  1659. }
  1660. #ifdef NTFS_RW
  1661. iput_upcase_err_out:
  1662. #endif /* NTFS_RW */
  1663. vol->upcase_len = 0;
  1664. down(&ntfs_lock);
  1665. if (vol->upcase == default_upcase) {
  1666. ntfs_nr_upcase_users--;
  1667. vol->upcase = NULL;
  1668. }
  1669. up(&ntfs_lock);
  1670. if (vol->upcase) {
  1671. ntfs_free(vol->upcase);
  1672. vol->upcase = NULL;
  1673. }
  1674. iput_mftbmp_err_out:
  1675. iput(vol->mftbmp_ino);
  1676. iput_mirr_err_out:
  1677. #ifdef NTFS_RW
  1678. if (vol->mftmirr_ino)
  1679. iput(vol->mftmirr_ino);
  1680. #endif /* NTFS_RW */
  1681. return FALSE;
  1682. }
  1683. /**
  1684. * ntfs_put_super - called by the vfs to unmount a volume
  1685. * @sb: vfs superblock of volume to unmount
  1686. *
  1687. * ntfs_put_super() is called by the VFS (from fs/super.c::do_umount()) when
  1688. * the volume is being unmounted (umount system call has been invoked) and it
  1689. * releases all inodes and memory belonging to the NTFS specific part of the
  1690. * super block.
  1691. */
  1692. static void ntfs_put_super(struct super_block *sb)
  1693. {
  1694. ntfs_volume *vol = NTFS_SB(sb);
  1695. ntfs_debug("Entering.");
  1696. #ifdef NTFS_RW
  1697. /*
  1698. * Commit all inodes while they are still open in case some of them
  1699. * cause others to be dirtied.
  1700. */
  1701. ntfs_commit_inode(vol->vol_ino);
  1702. /* NTFS 3.0+ specific. */
  1703. if (vol->major_ver >= 3) {
  1704. if (vol->quota_q_ino)
  1705. ntfs_commit_inode(vol->quota_q_ino);
  1706. if (vol->quota_ino)
  1707. ntfs_commit_inode(vol->quota_ino);
  1708. if (vol->extend_ino)
  1709. ntfs_commit_inode(vol->extend_ino);
  1710. if (vol->secure_ino)
  1711. ntfs_commit_inode(vol->secure_ino);
  1712. }
  1713. ntfs_commit_inode(vol->root_ino);
  1714. down_write(&vol->lcnbmp_lock);
  1715. ntfs_commit_inode(vol->lcnbmp_ino);
  1716. up_write(&vol->lcnbmp_lock);
  1717. down_write(&vol->mftbmp_lock);
  1718. ntfs_commit_inode(vol->mftbmp_ino);
  1719. up_write(&vol->mftbmp_lock);
  1720. if (vol->logfile_ino)
  1721. ntfs_commit_inode(vol->logfile_ino);
  1722. if (vol->mftmirr_ino)
  1723. ntfs_commit_inode(vol->mftmirr_ino);
  1724. ntfs_commit_inode(vol->mft_ino);
  1725. /*
  1726. * If a read-write mount and no volume errors have occured, mark the
  1727. * volume clean. Also, re-commit all affected inodes.
  1728. */
  1729. if (!(sb->s_flags & MS_RDONLY)) {
  1730. if (!NVolErrors(vol)) {
  1731. if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
  1732. ntfs_warning(sb, "Failed to clear dirty bit "
  1733. "in volume information "
  1734. "flags. Run chkdsk.");
  1735. ntfs_commit_inode(vol->vol_ino);
  1736. ntfs_commit_inode(vol->root_ino);
  1737. if (vol->mftmirr_ino)
  1738. ntfs_commit_inode(vol->mftmirr_ino);
  1739. ntfs_commit_inode(vol->mft_ino);
  1740. } else {
  1741. ntfs_warning(sb, "Volume has errors. Leaving volume "
  1742. "marked dirty. Run chkdsk.");
  1743. }
  1744. }
  1745. #endif /* NTFS_RW */
  1746. iput(vol->vol_ino);
  1747. vol->vol_ino = NULL;
  1748. /* NTFS 3.0+ specific clean up. */
  1749. if (vol->major_ver >= 3) {
  1750. #ifdef NTFS_RW
  1751. if (vol->quota_q_ino) {
  1752. iput(vol->quota_q_ino);
  1753. vol->quota_q_ino = NULL;
  1754. }
  1755. if (vol->quota_ino) {
  1756. iput(vol->quota_ino);
  1757. vol->quota_ino = NULL;
  1758. }
  1759. #endif /* NTFS_RW */
  1760. if (vol->extend_ino) {
  1761. iput(vol->extend_ino);
  1762. vol->extend_ino = NULL;
  1763. }
  1764. if (vol->secure_ino) {
  1765. iput(vol->secure_ino);
  1766. vol->secure_ino = NULL;
  1767. }
  1768. }
  1769. iput(vol->root_ino);
  1770. vol->root_ino = NULL;
  1771. down_write(&vol->lcnbmp_lock);
  1772. iput(vol->lcnbmp_ino);
  1773. vol->lcnbmp_ino = NULL;
  1774. up_write(&vol->lcnbmp_lock);
  1775. down_write(&vol->mftbmp_lock);
  1776. iput(vol->mftbmp_ino);
  1777. vol->mftbmp_ino = NULL;
  1778. up_write(&vol->mftbmp_lock);
  1779. #ifdef NTFS_RW
  1780. if (vol->logfile_ino) {
  1781. iput(vol->logfile_ino);
  1782. vol->logfile_ino = NULL;
  1783. }
  1784. if (vol->mftmirr_ino) {
  1785. /* Re-commit the mft mirror and mft just in case. */
  1786. ntfs_commit_inode(vol->mftmirr_ino);
  1787. ntfs_commit_inode(vol->mft_ino);
  1788. iput(vol->mftmirr_ino);
  1789. vol->mftmirr_ino = NULL;
  1790. }
  1791. /*
  1792. * If any dirty inodes are left, throw away all mft data page cache
  1793. * pages to allow a clean umount. This should never happen any more
  1794. * due to mft.c::ntfs_mft_writepage() cleaning all the dirty pages as
  1795. * the underlying mft records are written out and cleaned. If it does,
  1796. * happen anyway, we want to know...
  1797. */
  1798. ntfs_commit_inode(vol->mft_ino);
  1799. write_inode_now(vol->mft_ino, 1);
  1800. if (!list_empty(&sb->s_dirty)) {
  1801. const char *s1, *s2;
  1802. down(&vol->mft_ino->i_sem);
  1803. truncate_inode_pages(vol->mft_ino->i_mapping, 0);
  1804. up(&vol->mft_ino->i_sem);
  1805. write_inode_now(vol->mft_ino, 1);
  1806. if (!list_empty(&sb->s_dirty)) {
  1807. static const char *_s1 = "inodes";
  1808. static const char *_s2 = "";
  1809. s1 = _s1;
  1810. s2 = _s2;
  1811. } else {
  1812. static const char *_s1 = "mft pages";
  1813. static const char *_s2 = "They have been thrown "
  1814. "away. ";
  1815. s1 = _s1;
  1816. s2 = _s2;
  1817. }
  1818. ntfs_error(sb, "Dirty %s found at umount time. %sYou should "
  1819. "run chkdsk. Please email "
  1820. "linux-ntfs-dev@lists.sourceforge.net and say "
  1821. "that you saw this message. Thank you.", s1,
  1822. s2);
  1823. }
  1824. #endif /* NTFS_RW */
  1825. iput(vol->mft_ino);
  1826. vol->mft_ino = NULL;
  1827. /* Throw away the table of attribute definitions. */
  1828. vol->attrdef_size = 0;
  1829. if (vol->attrdef) {
  1830. ntfs_free(vol->attrdef);
  1831. vol->attrdef = NULL;
  1832. }
  1833. vol->upcase_len = 0;
  1834. /*
  1835. * Destroy the global default upcase table if necessary. Also decrease
  1836. * the number of upcase users if we are a user.
  1837. */
  1838. down(&ntfs_lock);
  1839. if (vol->upcase == default_upcase) {
  1840. ntfs_nr_upcase_users--;
  1841. vol->upcase = NULL;
  1842. }
  1843. if (!ntfs_nr_upcase_users && default_upcase) {
  1844. ntfs_free(default_upcase);
  1845. default_upcase = NULL;
  1846. }
  1847. if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
  1848. free_compression_buffers();
  1849. up(&ntfs_lock);
  1850. if (vol->upcase) {
  1851. ntfs_free(vol->upcase);
  1852. vol->upcase = NULL;
  1853. }
  1854. if (vol->nls_map) {
  1855. unload_nls(vol->nls_map);
  1856. vol->nls_map = NULL;
  1857. }
  1858. sb->s_fs_info = NULL;
  1859. kfree(vol);
  1860. return;
  1861. }
  1862. /**
  1863. * get_nr_free_clusters - return the number of free clusters on a volume
  1864. * @vol: ntfs volume for which to obtain free cluster count
  1865. *
  1866. * Calculate the number of free clusters on the mounted NTFS volume @vol. We
  1867. * actually calculate the number of clusters in use instead because this
  1868. * allows us to not care about partial pages as these will be just zero filled
  1869. * and hence not be counted as allocated clusters.
  1870. *
  1871. * The only particularity is that clusters beyond the end of the logical ntfs
  1872. * volume will be marked as allocated to prevent errors which means we have to
  1873. * discount those at the end. This is important as the cluster bitmap always
  1874. * has a size in multiples of 8 bytes, i.e. up to 63 clusters could be outside
  1875. * the logical volume and marked in use when they are not as they do not exist.
  1876. *
  1877. * If any pages cannot be read we assume all clusters in the erroring pages are
  1878. * in use. This means we return an underestimate on errors which is better than
  1879. * an overestimate.
  1880. */
  1881. static s64 get_nr_free_clusters(ntfs_volume *vol)
  1882. {
  1883. s64 nr_free = vol->nr_clusters;
  1884. u32 *kaddr;
  1885. struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
  1886. filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
  1887. struct page *page;
  1888. unsigned long index, max_index;
  1889. unsigned int max_size;
  1890. ntfs_debug("Entering.");
  1891. /* Serialize accesses to the cluster bitmap. */
  1892. down_read(&vol->lcnbmp_lock);
  1893. /*
  1894. * Convert the number of bits into bytes rounded up, then convert into
  1895. * multiples of PAGE_CACHE_SIZE, rounding up so that if we have one
  1896. * full and one partial page max_index = 2.
  1897. */
  1898. max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_CACHE_SIZE - 1) >>
  1899. PAGE_CACHE_SHIFT;
  1900. /* Use multiples of 4 bytes. */
  1901. max_size = PAGE_CACHE_SIZE >> 2;
  1902. ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%x.",
  1903. max_index, max_size);
  1904. for (index = 0UL; index < max_index; index++) {
  1905. unsigned int i;
  1906. /*
  1907. * Read the page from page cache, getting it from backing store
  1908. * if necessary, and increment the use count.
  1909. */
  1910. page = read_cache_page(mapping, index, (filler_t*)readpage,
  1911. NULL);
  1912. /* Ignore pages which errored synchronously. */
  1913. if (IS_ERR(page)) {
  1914. ntfs_debug("Sync read_cache_page() error. Skipping "
  1915. "page (index 0x%lx).", index);
  1916. nr_free -= PAGE_CACHE_SIZE * 8;
  1917. continue;
  1918. }
  1919. wait_on_page_locked(page);
  1920. /* Ignore pages which errored asynchronously. */
  1921. if (!PageUptodate(page)) {
  1922. ntfs_debug("Async read_cache_page() error. Skipping "
  1923. "page (index 0x%lx).", index);
  1924. page_cache_release(page);
  1925. nr_free -= PAGE_CACHE_SIZE * 8;
  1926. continue;
  1927. }
  1928. kaddr = (u32*)kmap_atomic(page, KM_USER0);
  1929. /*
  1930. * For each 4 bytes, subtract the number of set bits. If this
  1931. * is the last page and it is partial we don't really care as
  1932. * it just means we do a little extra work but it won't affect
  1933. * the result as all out of range bytes are set to zero by
  1934. * ntfs_readpage().
  1935. */
  1936. for (i = 0; i < max_size; i++)
  1937. nr_free -= (s64)hweight32(kaddr[i]);
  1938. kunmap_atomic(kaddr, KM_USER0);
  1939. page_cache_release(page);
  1940. }
  1941. ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
  1942. /*
  1943. * Fixup for eventual bits outside logical ntfs volume (see function
  1944. * description above).
  1945. */
  1946. if (vol->nr_clusters & 63)
  1947. nr_free += 64 - (vol->nr_clusters & 63);
  1948. up_read(&vol->lcnbmp_lock);
  1949. /* If errors occured we may well have gone below zero, fix this. */
  1950. if (nr_free < 0)
  1951. nr_free = 0;
  1952. ntfs_debug("Exiting.");
  1953. return nr_free;
  1954. }
  1955. /**
  1956. * __get_nr_free_mft_records - return the number of free inodes on a volume
  1957. * @vol: ntfs volume for which to obtain free inode count
  1958. *
  1959. * Calculate the number of free mft records (inodes) on the mounted NTFS
  1960. * volume @vol. We actually calculate the number of mft records in use instead
  1961. * because this allows us to not care about partial pages as these will be just
  1962. * zero filled and hence not be counted as allocated mft record.
  1963. *
  1964. * If any pages cannot be read we assume all mft records in the erroring pages
  1965. * are in use. This means we return an underestimate on errors which is better
  1966. * than an overestimate.
  1967. *
  1968. * NOTE: Caller must hold mftbmp_lock rw_semaphore for reading or writing.
  1969. */
  1970. static unsigned long __get_nr_free_mft_records(ntfs_volume *vol)
  1971. {
  1972. s64 nr_free;
  1973. u32 *kaddr;
  1974. struct address_space *mapping = vol->mftbmp_ino->i_mapping;
  1975. filler_t *readpage = (filler_t*)mapping->a_ops->readpage;
  1976. struct page *page;
  1977. unsigned long index, max_index;
  1978. unsigned int max_size;
  1979. ntfs_debug("Entering.");
  1980. /* Number of mft records in file system (at this point in time). */
  1981. nr_free = vol->mft_ino->i_size >> vol->mft_record_size_bits;
  1982. /*
  1983. * Convert the maximum number of set bits into bytes rounded up, then
  1984. * convert into multiples of PAGE_CACHE_SIZE, rounding up so that if we
  1985. * have one full and one partial page max_index = 2.
  1986. */
  1987. max_index = ((((NTFS_I(vol->mft_ino)->initialized_size >>
  1988. vol->mft_record_size_bits) + 7) >> 3) +
  1989. PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1990. /* Use multiples of 4 bytes. */
  1991. max_size = PAGE_CACHE_SIZE >> 2;
  1992. ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = "
  1993. "0x%x.", max_index, max_size);
  1994. for (index = 0UL; index < max_index; index++) {
  1995. unsigned int i;
  1996. /*
  1997. * Read the page from page cache, getting it from backing store
  1998. * if necessary, and increment the use count.
  1999. */
  2000. page = read_cache_page(mapping, index, (filler_t*)readpage,
  2001. NULL);
  2002. /* Ignore pages which errored synchronously. */
  2003. if (IS_ERR(page)) {
  2004. ntfs_debug("Sync read_cache_page() error. Skipping "
  2005. "page (index 0x%lx).", index);
  2006. nr_free -= PAGE_CACHE_SIZE * 8;
  2007. continue;
  2008. }
  2009. wait_on_page_locked(page);
  2010. /* Ignore pages which errored asynchronously. */
  2011. if (!PageUptodate(page)) {
  2012. ntfs_debug("Async read_cache_page() error. Skipping "
  2013. "page (index 0x%lx).", index);
  2014. page_cache_release(page);
  2015. nr_free -= PAGE_CACHE_SIZE * 8;
  2016. continue;
  2017. }
  2018. kaddr = (u32*)kmap_atomic(page, KM_USER0);
  2019. /*
  2020. * For each 4 bytes, subtract the number of set bits. If this
  2021. * is the last page and it is partial we don't really care as
  2022. * it just means we do a little extra work but it won't affect
  2023. * the result as all out of range bytes are set to zero by
  2024. * ntfs_readpage().
  2025. */
  2026. for (i = 0; i < max_size; i++)
  2027. nr_free -= (s64)hweight32(kaddr[i]);
  2028. kunmap_atomic(kaddr, KM_USER0);
  2029. page_cache_release(page);
  2030. }
  2031. ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
  2032. index - 1);
  2033. /* If errors occured we may well have gone below zero, fix this. */
  2034. if (nr_free < 0)
  2035. nr_free = 0;
  2036. ntfs_debug("Exiting.");
  2037. return nr_free;
  2038. }
  2039. /**
  2040. * ntfs_statfs - return information about mounted NTFS volume
  2041. * @sb: super block of mounted volume
  2042. * @sfs: statfs structure in which to return the information
  2043. *
  2044. * Return information about the mounted NTFS volume @sb in the statfs structure
  2045. * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is
  2046. * called). We interpret the values to be correct of the moment in time at
  2047. * which we are called. Most values are variable otherwise and this isn't just
  2048. * the free values but the totals as well. For example we can increase the
  2049. * total number of file nodes if we run out and we can keep doing this until
  2050. * there is no more space on the volume left at all.
  2051. *
  2052. * Called from vfs_statfs which is used to handle the statfs, fstatfs, and
  2053. * ustat system calls.
  2054. *
  2055. * Return 0 on success or -errno on error.
  2056. */
  2057. static int ntfs_statfs(struct super_block *sb, struct kstatfs *sfs)
  2058. {
  2059. ntfs_volume *vol = NTFS_SB(sb);
  2060. s64 size;
  2061. ntfs_debug("Entering.");
  2062. /* Type of filesystem. */
  2063. sfs->f_type = NTFS_SB_MAGIC;
  2064. /* Optimal transfer block size. */
  2065. sfs->f_bsize = PAGE_CACHE_SIZE;
  2066. /*
  2067. * Total data blocks in file system in units of f_bsize and since
  2068. * inodes are also stored in data blocs ($MFT is a file) this is just
  2069. * the total clusters.
  2070. */
  2071. sfs->f_blocks = vol->nr_clusters << vol->cluster_size_bits >>
  2072. PAGE_CACHE_SHIFT;
  2073. /* Free data blocks in file system in units of f_bsize. */
  2074. size = get_nr_free_clusters(vol) << vol->cluster_size_bits >>
  2075. PAGE_CACHE_SHIFT;
  2076. if (size < 0LL)
  2077. size = 0LL;
  2078. /* Free blocks avail to non-superuser, same as above on NTFS. */
  2079. sfs->f_bavail = sfs->f_bfree = size;
  2080. /* Serialize accesses to the inode bitmap. */
  2081. down_read(&vol->mftbmp_lock);
  2082. /* Number of inodes in file system (at this point in time). */
  2083. sfs->f_files = vol->mft_ino->i_size >> vol->mft_record_size_bits;
  2084. /* Free inodes in fs (based on current total count). */
  2085. sfs->f_ffree = __get_nr_free_mft_records(vol);
  2086. up_read(&vol->mftbmp_lock);
  2087. /*
  2088. * File system id. This is extremely *nix flavour dependent and even
  2089. * within Linux itself all fs do their own thing. I interpret this to
  2090. * mean a unique id associated with the mounted fs and not the id
  2091. * associated with the file system driver, the latter is already given
  2092. * by the file system type in sfs->f_type. Thus we use the 64-bit
  2093. * volume serial number splitting it into two 32-bit parts. We enter
  2094. * the least significant 32-bits in f_fsid[0] and the most significant
  2095. * 32-bits in f_fsid[1].
  2096. */
  2097. sfs->f_fsid.val[0] = vol->serial_no & 0xffffffff;
  2098. sfs->f_fsid.val[1] = (vol->serial_no >> 32) & 0xffffffff;
  2099. /* Maximum length of filenames. */
  2100. sfs->f_namelen = NTFS_MAX_NAME_LEN;
  2101. return 0;
  2102. }
  2103. /**
  2104. * The complete super operations.
  2105. */
  2106. static struct super_operations ntfs_sops = {
  2107. .alloc_inode = ntfs_alloc_big_inode, /* VFS: Allocate new inode. */
  2108. .destroy_inode = ntfs_destroy_big_inode, /* VFS: Deallocate inode. */
  2109. .put_inode = ntfs_put_inode, /* VFS: Called just before
  2110. the inode reference count
  2111. is decreased. */
  2112. #ifdef NTFS_RW
  2113. //.dirty_inode = NULL, /* VFS: Called from
  2114. // __mark_inode_dirty(). */
  2115. .write_inode = ntfs_write_inode, /* VFS: Write dirty inode to
  2116. disk. */
  2117. //.drop_inode = NULL, /* VFS: Called just after the
  2118. // inode reference count has
  2119. // been decreased to zero.
  2120. // NOTE: The inode lock is
  2121. // held. See fs/inode.c::
  2122. // generic_drop_inode(). */
  2123. //.delete_inode = NULL, /* VFS: Delete inode from disk.
  2124. // Called when i_count becomes
  2125. // 0 and i_nlink is also 0. */
  2126. //.write_super = NULL, /* Flush dirty super block to
  2127. // disk. */
  2128. //.sync_fs = NULL, /* ? */
  2129. //.write_super_lockfs = NULL, /* ? */
  2130. //.unlockfs = NULL, /* ? */
  2131. #endif /* NTFS_RW */
  2132. .put_super = ntfs_put_super, /* Syscall: umount. */
  2133. .statfs = ntfs_statfs, /* Syscall: statfs */
  2134. .remount_fs = ntfs_remount, /* Syscall: mount -o remount. */
  2135. .clear_inode = ntfs_clear_big_inode, /* VFS: Called when an inode is
  2136. removed from memory. */
  2137. //.umount_begin = NULL, /* Forced umount. */
  2138. .show_options = ntfs_show_options, /* Show mount options in
  2139. proc. */
  2140. };
  2141. /**
  2142. * Declarations for NTFS specific export operations (fs/ntfs/namei.c).
  2143. */
  2144. extern struct dentry *ntfs_get_parent(struct dentry *child_dent);
  2145. extern struct dentry *ntfs_get_dentry(struct super_block *sb, void *fh);
  2146. /**
  2147. * Export operations allowing NFS exporting of mounted NTFS partitions.
  2148. *
  2149. * We use the default ->decode_fh() and ->encode_fh() for now. Note that they
  2150. * use 32 bits to store the inode number which is an unsigned long so on 64-bit
  2151. * architectures is usually 64 bits so it would all fail horribly on huge
  2152. * volumes. I guess we need to define our own encode and decode fh functions
  2153. * that store 64-bit inode numbers at some point but for now we will ignore the
  2154. * problem...
  2155. *
  2156. * We also use the default ->get_name() helper (used by ->decode_fh() via
  2157. * fs/exportfs/expfs.c::find_exported_dentry()) as that is completely fs
  2158. * independent.
  2159. *
  2160. * The default ->get_parent() just returns -EACCES so we have to provide our
  2161. * own and the default ->get_dentry() is incompatible with NTFS due to not
  2162. * allowing the inode number 0 which is used in NTFS for the system file $MFT
  2163. * and due to using iget() whereas NTFS needs ntfs_iget().
  2164. */
  2165. static struct export_operations ntfs_export_ops = {
  2166. .get_parent = ntfs_get_parent, /* Find the parent of a given
  2167. directory. */
  2168. .get_dentry = ntfs_get_dentry, /* Find a dentry for the inode
  2169. given a file handle
  2170. sub-fragment. */
  2171. };
  2172. /**
  2173. * ntfs_fill_super - mount an ntfs files system
  2174. * @sb: super block of ntfs file system to mount
  2175. * @opt: string containing the mount options
  2176. * @silent: silence error output
  2177. *
  2178. * ntfs_fill_super() is called by the VFS to mount the device described by @sb
  2179. * with the mount otions in @data with the NTFS file system.
  2180. *
  2181. * If @silent is true, remain silent even if errors are detected. This is used
  2182. * during bootup, when the kernel tries to mount the root file system with all
  2183. * registered file systems one after the other until one succeeds. This implies
  2184. * that all file systems except the correct one will quite correctly and
  2185. * expectedly return an error, but nobody wants to see error messages when in
  2186. * fact this is what is supposed to happen.
  2187. *
  2188. * NOTE: @sb->s_flags contains the mount options flags.
  2189. */
  2190. static int ntfs_fill_super(struct super_block *sb, void *opt, const int silent)
  2191. {
  2192. ntfs_volume *vol;
  2193. struct buffer_head *bh;
  2194. struct inode *tmp_ino;
  2195. int result;
  2196. ntfs_debug("Entering.");
  2197. #ifndef NTFS_RW
  2198. sb->s_flags |= MS_RDONLY | MS_NOATIME | MS_NODIRATIME;
  2199. #endif /* ! NTFS_RW */
  2200. /* Allocate a new ntfs_volume and place it in sb->s_fs_info. */
  2201. sb->s_fs_info = kmalloc(sizeof(ntfs_volume), GFP_NOFS);
  2202. vol = NTFS_SB(sb);
  2203. if (!vol) {
  2204. if (!silent)
  2205. ntfs_error(sb, "Allocation of NTFS volume structure "
  2206. "failed. Aborting mount...");
  2207. return -ENOMEM;
  2208. }
  2209. /* Initialize ntfs_volume structure. */
  2210. memset(vol, 0, sizeof(ntfs_volume));
  2211. vol->sb = sb;
  2212. vol->upcase = NULL;
  2213. vol->attrdef = NULL;
  2214. vol->mft_ino = NULL;
  2215. vol->mftbmp_ino = NULL;
  2216. init_rwsem(&vol->mftbmp_lock);
  2217. #ifdef NTFS_RW
  2218. vol->mftmirr_ino = NULL;
  2219. vol->logfile_ino = NULL;
  2220. #endif /* NTFS_RW */
  2221. vol->lcnbmp_ino = NULL;
  2222. init_rwsem(&vol->lcnbmp_lock);
  2223. vol->vol_ino = NULL;
  2224. vol->root_ino = NULL;
  2225. vol->secure_ino = NULL;
  2226. vol->extend_ino = NULL;
  2227. #ifdef NTFS_RW
  2228. vol->quota_ino = NULL;
  2229. vol->quota_q_ino = NULL;
  2230. #endif /* NTFS_RW */
  2231. vol->nls_map = NULL;
  2232. /*
  2233. * Default is group and other don't have any access to files or
  2234. * directories while owner has full access. Further, files by default
  2235. * are not executable but directories are of course browseable.
  2236. */
  2237. vol->fmask = 0177;
  2238. vol->dmask = 0077;
  2239. unlock_kernel();
  2240. /* Important to get the mount options dealt with now. */
  2241. if (!parse_options(vol, (char*)opt))
  2242. goto err_out_now;
  2243. /*
  2244. * TODO: Fail safety check. In the future we should really be able to
  2245. * cope with this being the case, but for now just bail out.
  2246. */
  2247. if (bdev_hardsect_size(sb->s_bdev) > NTFS_BLOCK_SIZE) {
  2248. if (!silent)
  2249. ntfs_error(sb, "Device has unsupported hardsect_size.");
  2250. goto err_out_now;
  2251. }
  2252. /* Setup the device access block size to NTFS_BLOCK_SIZE. */
  2253. if (sb_set_blocksize(sb, NTFS_BLOCK_SIZE) != NTFS_BLOCK_SIZE) {
  2254. if (!silent)
  2255. ntfs_error(sb, "Unable to set block size.");
  2256. goto err_out_now;
  2257. }
  2258. /* Get the size of the device in units of NTFS_BLOCK_SIZE bytes. */
  2259. vol->nr_blocks = sb->s_bdev->bd_inode->i_size >> NTFS_BLOCK_SIZE_BITS;
  2260. /* Read the boot sector and return unlocked buffer head to it. */
  2261. if (!(bh = read_ntfs_boot_sector(sb, silent))) {
  2262. if (!silent)
  2263. ntfs_error(sb, "Not an NTFS volume.");
  2264. goto err_out_now;
  2265. }
  2266. /*
  2267. * Extract the data from the boot sector and setup the ntfs super block
  2268. * using it.
  2269. */
  2270. result = parse_ntfs_boot_sector(vol, (NTFS_BOOT_SECTOR*)bh->b_data);
  2271. /* Initialize the cluster and mft allocators. */
  2272. ntfs_setup_allocators(vol);
  2273. brelse(bh);
  2274. if (!result) {
  2275. if (!silent)
  2276. ntfs_error(sb, "Unsupported NTFS filesystem.");
  2277. goto err_out_now;
  2278. }
  2279. /*
  2280. * TODO: When we start coping with sector sizes different from
  2281. * NTFS_BLOCK_SIZE, we now probably need to set the blocksize of the
  2282. * device (probably to NTFS_BLOCK_SIZE).
  2283. */
  2284. /* Setup remaining fields in the super block. */
  2285. sb->s_magic = NTFS_SB_MAGIC;
  2286. /*
  2287. * Ntfs allows 63 bits for the file size, i.e. correct would be:
  2288. * sb->s_maxbytes = ~0ULL >> 1;
  2289. * But the kernel uses a long as the page cache page index which on
  2290. * 32-bit architectures is only 32-bits. MAX_LFS_FILESIZE is kernel
  2291. * defined to the maximum the page cache page index can cope with
  2292. * without overflowing the index or to 2^63 - 1, whichever is smaller.
  2293. */
  2294. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2295. sb->s_time_gran = 100;
  2296. /*
  2297. * Now load the metadata required for the page cache and our address
  2298. * space operations to function. We do this by setting up a specialised
  2299. * read_inode method and then just calling the normal iget() to obtain
  2300. * the inode for $MFT which is sufficient to allow our normal inode
  2301. * operations and associated address space operations to function.
  2302. */
  2303. sb->s_op = &ntfs_sops;
  2304. tmp_ino = new_inode(sb);
  2305. if (!tmp_ino) {
  2306. if (!silent)
  2307. ntfs_error(sb, "Failed to load essential metadata.");
  2308. goto err_out_now;
  2309. }
  2310. tmp_ino->i_ino = FILE_MFT;
  2311. insert_inode_hash(tmp_ino);
  2312. if (ntfs_read_inode_mount(tmp_ino) < 0) {
  2313. if (!silent)
  2314. ntfs_error(sb, "Failed to load essential metadata.");
  2315. goto iput_tmp_ino_err_out_now;
  2316. }
  2317. down(&ntfs_lock);
  2318. /*
  2319. * The current mount is a compression user if the cluster size is
  2320. * less than or equal 4kiB.
  2321. */
  2322. if (vol->cluster_size <= 4096 && !ntfs_nr_compression_users++) {
  2323. result = allocate_compression_buffers();
  2324. if (result) {
  2325. ntfs_error(NULL, "Failed to allocate buffers "
  2326. "for compression engine.");
  2327. ntfs_nr_compression_users--;
  2328. up(&ntfs_lock);
  2329. goto iput_tmp_ino_err_out_now;
  2330. }
  2331. }
  2332. /*
  2333. * Generate the global default upcase table if necessary. Also
  2334. * temporarily increment the number of upcase users to avoid race
  2335. * conditions with concurrent (u)mounts.
  2336. */
  2337. if (!default_upcase)
  2338. default_upcase = generate_default_upcase();
  2339. ntfs_nr_upcase_users++;
  2340. up(&ntfs_lock);
  2341. /*
  2342. * From now on, ignore @silent parameter. If we fail below this line,
  2343. * it will be due to a corrupt fs or a system error, so we report it.
  2344. */
  2345. /*
  2346. * Open the system files with normal access functions and complete
  2347. * setting up the ntfs super block.
  2348. */
  2349. if (!load_system_files(vol)) {
  2350. ntfs_error(sb, "Failed to load system files.");
  2351. goto unl_upcase_iput_tmp_ino_err_out_now;
  2352. }
  2353. if ((sb->s_root = d_alloc_root(vol->root_ino))) {
  2354. /* We increment i_count simulating an ntfs_iget(). */
  2355. atomic_inc(&vol->root_ino->i_count);
  2356. ntfs_debug("Exiting, status successful.");
  2357. /* Release the default upcase if it has no users. */
  2358. down(&ntfs_lock);
  2359. if (!--ntfs_nr_upcase_users && default_upcase) {
  2360. ntfs_free(default_upcase);
  2361. default_upcase = NULL;
  2362. }
  2363. up(&ntfs_lock);
  2364. sb->s_export_op = &ntfs_export_ops;
  2365. lock_kernel();
  2366. return 0;
  2367. }
  2368. ntfs_error(sb, "Failed to allocate root directory.");
  2369. /* Clean up after the successful load_system_files() call from above. */
  2370. // TODO: Use ntfs_put_super() instead of repeating all this code...
  2371. // FIXME: Should mark the volume clean as the error is most likely
  2372. // -ENOMEM.
  2373. iput(vol->vol_ino);
  2374. vol->vol_ino = NULL;
  2375. /* NTFS 3.0+ specific clean up. */
  2376. if (vol->major_ver >= 3) {
  2377. #ifdef NTFS_RW
  2378. if (vol->quota_q_ino) {
  2379. iput(vol->quota_q_ino);
  2380. vol->quota_q_ino = NULL;
  2381. }
  2382. if (vol->quota_ino) {
  2383. iput(vol->quota_ino);
  2384. vol->quota_ino = NULL;
  2385. }
  2386. #endif /* NTFS_RW */
  2387. if (vol->extend_ino) {
  2388. iput(vol->extend_ino);
  2389. vol->extend_ino = NULL;
  2390. }
  2391. if (vol->secure_ino) {
  2392. iput(vol->secure_ino);
  2393. vol->secure_ino = NULL;
  2394. }
  2395. }
  2396. iput(vol->root_ino);
  2397. vol->root_ino = NULL;
  2398. iput(vol->lcnbmp_ino);
  2399. vol->lcnbmp_ino = NULL;
  2400. iput(vol->mftbmp_ino);
  2401. vol->mftbmp_ino = NULL;
  2402. #ifdef NTFS_RW
  2403. if (vol->logfile_ino) {
  2404. iput(vol->logfile_ino);
  2405. vol->logfile_ino = NULL;
  2406. }
  2407. if (vol->mftmirr_ino) {
  2408. iput(vol->mftmirr_ino);
  2409. vol->mftmirr_ino = NULL;
  2410. }
  2411. #endif /* NTFS_RW */
  2412. /* Throw away the table of attribute definitions. */
  2413. vol->attrdef_size = 0;
  2414. if (vol->attrdef) {
  2415. ntfs_free(vol->attrdef);
  2416. vol->attrdef = NULL;
  2417. }
  2418. vol->upcase_len = 0;
  2419. down(&ntfs_lock);
  2420. if (vol->upcase == default_upcase) {
  2421. ntfs_nr_upcase_users--;
  2422. vol->upcase = NULL;
  2423. }
  2424. up(&ntfs_lock);
  2425. if (vol->upcase) {
  2426. ntfs_free(vol->upcase);
  2427. vol->upcase = NULL;
  2428. }
  2429. if (vol->nls_map) {
  2430. unload_nls(vol->nls_map);
  2431. vol->nls_map = NULL;
  2432. }
  2433. /* Error exit code path. */
  2434. unl_upcase_iput_tmp_ino_err_out_now:
  2435. /*
  2436. * Decrease the number of upcase users and destroy the global default
  2437. * upcase table if necessary.
  2438. */
  2439. down(&ntfs_lock);
  2440. if (!--ntfs_nr_upcase_users && default_upcase) {
  2441. ntfs_free(default_upcase);
  2442. default_upcase = NULL;
  2443. }
  2444. if (vol->cluster_size <= 4096 && !--ntfs_nr_compression_users)
  2445. free_compression_buffers();
  2446. up(&ntfs_lock);
  2447. iput_tmp_ino_err_out_now:
  2448. iput(tmp_ino);
  2449. if (vol->mft_ino && vol->mft_ino != tmp_ino)
  2450. iput(vol->mft_ino);
  2451. vol->mft_ino = NULL;
  2452. /*
  2453. * This is needed to get ntfs_clear_extent_inode() called for each
  2454. * inode we have ever called ntfs_iget()/iput() on, otherwise we A)
  2455. * leak resources and B) a subsequent mount fails automatically due to
  2456. * ntfs_iget() never calling down into our ntfs_read_locked_inode()
  2457. * method again... FIXME: Do we need to do this twice now because of
  2458. * attribute inodes? I think not, so leave as is for now... (AIA)
  2459. */
  2460. if (invalidate_inodes(sb)) {
  2461. ntfs_error(sb, "Busy inodes left. This is most likely a NTFS "
  2462. "driver bug.");
  2463. /* Copied from fs/super.c. I just love this message. (-; */
  2464. printk("NTFS: Busy inodes after umount. Self-destruct in 5 "
  2465. "seconds. Have a nice day...\n");
  2466. }
  2467. /* Errors at this stage are irrelevant. */
  2468. err_out_now:
  2469. lock_kernel();
  2470. sb->s_fs_info = NULL;
  2471. kfree(vol);
  2472. ntfs_debug("Failed, returning -EINVAL.");
  2473. return -EINVAL;
  2474. }
  2475. /*
  2476. * This is a slab cache to optimize allocations and deallocations of Unicode
  2477. * strings of the maximum length allowed by NTFS, which is NTFS_MAX_NAME_LEN
  2478. * (255) Unicode characters + a terminating NULL Unicode character.
  2479. */
  2480. kmem_cache_t *ntfs_name_cache;
  2481. /* Slab caches for efficient allocation/deallocation of of inodes. */
  2482. kmem_cache_t *ntfs_inode_cache;
  2483. kmem_cache_t *ntfs_big_inode_cache;
  2484. /* Init once constructor for the inode slab cache. */
  2485. static void ntfs_big_inode_init_once(void *foo, kmem_cache_t *cachep,
  2486. unsigned long flags)
  2487. {
  2488. ntfs_inode *ni = (ntfs_inode *)foo;
  2489. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  2490. SLAB_CTOR_CONSTRUCTOR)
  2491. inode_init_once(VFS_I(ni));
  2492. }
  2493. /*
  2494. * Slab caches to optimize allocations and deallocations of attribute search
  2495. * contexts and index contexts, respectively.
  2496. */
  2497. kmem_cache_t *ntfs_attr_ctx_cache;
  2498. kmem_cache_t *ntfs_index_ctx_cache;
  2499. /* Driver wide semaphore. */
  2500. DECLARE_MUTEX(ntfs_lock);
  2501. static struct super_block *ntfs_get_sb(struct file_system_type *fs_type,
  2502. int flags, const char *dev_name, void *data)
  2503. {
  2504. return get_sb_bdev(fs_type, flags, dev_name, data, ntfs_fill_super);
  2505. }
  2506. static struct file_system_type ntfs_fs_type = {
  2507. .owner = THIS_MODULE,
  2508. .name = "ntfs",
  2509. .get_sb = ntfs_get_sb,
  2510. .kill_sb = kill_block_super,
  2511. .fs_flags = FS_REQUIRES_DEV,
  2512. };
  2513. /* Stable names for the slab caches. */
  2514. static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
  2515. static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
  2516. static const char ntfs_name_cache_name[] = "ntfs_name_cache";
  2517. static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
  2518. static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
  2519. static int __init init_ntfs_fs(void)
  2520. {
  2521. int err = 0;
  2522. /* This may be ugly but it results in pretty output so who cares. (-8 */
  2523. printk(KERN_INFO "NTFS driver " NTFS_VERSION " [Flags: R/"
  2524. #ifdef NTFS_RW
  2525. "W"
  2526. #else
  2527. "O"
  2528. #endif
  2529. #ifdef DEBUG
  2530. " DEBUG"
  2531. #endif
  2532. #ifdef MODULE
  2533. " MODULE"
  2534. #endif
  2535. "].\n");
  2536. ntfs_debug("Debug messages are enabled.");
  2537. ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
  2538. sizeof(ntfs_index_context), 0 /* offset */,
  2539. SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
  2540. if (!ntfs_index_ctx_cache) {
  2541. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  2542. ntfs_index_ctx_cache_name);
  2543. goto ictx_err_out;
  2544. }
  2545. ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
  2546. sizeof(ntfs_attr_search_ctx), 0 /* offset */,
  2547. SLAB_HWCACHE_ALIGN, NULL /* ctor */, NULL /* dtor */);
  2548. if (!ntfs_attr_ctx_cache) {
  2549. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  2550. ntfs_attr_ctx_cache_name);
  2551. goto actx_err_out;
  2552. }
  2553. ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
  2554. (NTFS_MAX_NAME_LEN+1) * sizeof(ntfschar), 0,
  2555. SLAB_HWCACHE_ALIGN, NULL, NULL);
  2556. if (!ntfs_name_cache) {
  2557. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  2558. ntfs_name_cache_name);
  2559. goto name_err_out;
  2560. }
  2561. ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
  2562. sizeof(ntfs_inode), 0,
  2563. SLAB_RECLAIM_ACCOUNT, NULL, NULL);
  2564. if (!ntfs_inode_cache) {
  2565. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  2566. ntfs_inode_cache_name);
  2567. goto inode_err_out;
  2568. }
  2569. ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
  2570. sizeof(big_ntfs_inode), 0,
  2571. SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
  2572. ntfs_big_inode_init_once, NULL);
  2573. if (!ntfs_big_inode_cache) {
  2574. printk(KERN_CRIT "NTFS: Failed to create %s!\n",
  2575. ntfs_big_inode_cache_name);
  2576. goto big_inode_err_out;
  2577. }
  2578. /* Register the ntfs sysctls. */
  2579. err = ntfs_sysctl(1);
  2580. if (err) {
  2581. printk(KERN_CRIT "NTFS: Failed to register NTFS sysctls!\n");
  2582. goto sysctl_err_out;
  2583. }
  2584. err = register_filesystem(&ntfs_fs_type);
  2585. if (!err) {
  2586. ntfs_debug("NTFS driver registered successfully.");
  2587. return 0; /* Success! */
  2588. }
  2589. printk(KERN_CRIT "NTFS: Failed to register NTFS file system driver!\n");
  2590. sysctl_err_out:
  2591. kmem_cache_destroy(ntfs_big_inode_cache);
  2592. big_inode_err_out:
  2593. kmem_cache_destroy(ntfs_inode_cache);
  2594. inode_err_out:
  2595. kmem_cache_destroy(ntfs_name_cache);
  2596. name_err_out:
  2597. kmem_cache_destroy(ntfs_attr_ctx_cache);
  2598. actx_err_out:
  2599. kmem_cache_destroy(ntfs_index_ctx_cache);
  2600. ictx_err_out:
  2601. if (!err) {
  2602. printk(KERN_CRIT "NTFS: Aborting NTFS file system driver "
  2603. "registration...\n");
  2604. err = -ENOMEM;
  2605. }
  2606. return err;
  2607. }
  2608. static void __exit exit_ntfs_fs(void)
  2609. {
  2610. int err = 0;
  2611. ntfs_debug("Unregistering NTFS driver.");
  2612. unregister_filesystem(&ntfs_fs_type);
  2613. if (kmem_cache_destroy(ntfs_big_inode_cache) && (err = 1))
  2614. printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
  2615. ntfs_big_inode_cache_name);
  2616. if (kmem_cache_destroy(ntfs_inode_cache) && (err = 1))
  2617. printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
  2618. ntfs_inode_cache_name);
  2619. if (kmem_cache_destroy(ntfs_name_cache) && (err = 1))
  2620. printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
  2621. ntfs_name_cache_name);
  2622. if (kmem_cache_destroy(ntfs_attr_ctx_cache) && (err = 1))
  2623. printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
  2624. ntfs_attr_ctx_cache_name);
  2625. if (kmem_cache_destroy(ntfs_index_ctx_cache) && (err = 1))
  2626. printk(KERN_CRIT "NTFS: Failed to destory %s.\n",
  2627. ntfs_index_ctx_cache_name);
  2628. if (err)
  2629. printk(KERN_CRIT "NTFS: This causes memory to leak! There is "
  2630. "probably a BUG in the driver! Please report "
  2631. "you saw this message to "
  2632. "linux-ntfs-dev@lists.sourceforge.net\n");
  2633. /* Unregister the ntfs sysctls. */
  2634. ntfs_sysctl(0);
  2635. }
  2636. MODULE_AUTHOR("Anton Altaparmakov <aia21@cantab.net>");
  2637. MODULE_DESCRIPTION("NTFS 1.2/3.x driver - Copyright (c) 2001-2004 Anton Altaparmakov");
  2638. MODULE_VERSION(NTFS_VERSION);
  2639. MODULE_LICENSE("GPL");
  2640. #ifdef DEBUG
  2641. module_param(debug_msgs, bool, 0);
  2642. MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
  2643. #endif
  2644. module_init(init_ntfs_fs)
  2645. module_exit(exit_ntfs_fs)