super.c 102 KB

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