super.c 100 KB

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