super.c 99 KB

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