super.c 100 KB

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