super.c 99 KB

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