super.c 100 KB

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