super.c 100 KB

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