super.c 93 KB

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