super.c 99 KB

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