super.c 86 KB

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