super.c 86 KB

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