super.c 99 KB

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