super.c 101 KB

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