super.c 99 KB

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