inode.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /**
  3. * inode.c - NTFS kernel inode handling.
  4. *
  5. * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.
  6. */
  7. #include <linux/buffer_head.h>
  8. #include <linux/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/mount.h>
  11. #include <linux/mutex.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/quotaops.h>
  14. #include <linux/slab.h>
  15. #include <linux/log2.h>
  16. #include "aops.h"
  17. #include "attrib.h"
  18. #include "bitmap.h"
  19. #include "dir.h"
  20. #include "debug.h"
  21. #include "inode.h"
  22. #include "lcnalloc.h"
  23. #include "malloc.h"
  24. #include "mft.h"
  25. #include "time.h"
  26. #include "ntfs.h"
  27. /**
  28. * ntfs_test_inode - compare two (possibly fake) inodes for equality
  29. * @vi: vfs inode which to test
  30. * @data: data which is being tested with
  31. *
  32. * Compare the ntfs attribute embedded in the ntfs specific part of the vfs
  33. * inode @vi for equality with the ntfs attribute @data.
  34. *
  35. * If searching for the normal file/directory inode, set @na->type to AT_UNUSED.
  36. * @na->name and @na->name_len are then ignored.
  37. *
  38. * Return 1 if the attributes match and 0 if not.
  39. *
  40. * NOTE: This function runs with the inode_hash_lock spin lock held so it is not
  41. * allowed to sleep.
  42. */
  43. int ntfs_test_inode(struct inode *vi, void *data)
  44. {
  45. ntfs_attr *na = (ntfs_attr *)data;
  46. ntfs_inode *ni;
  47. if (vi->i_ino != na->mft_no)
  48. return 0;
  49. ni = NTFS_I(vi);
  50. /* If !NInoAttr(ni), @vi is a normal file or directory inode. */
  51. if (likely(!NInoAttr(ni))) {
  52. /* If not looking for a normal inode this is a mismatch. */
  53. if (unlikely(na->type != AT_UNUSED))
  54. return 0;
  55. } else {
  56. /* A fake inode describing an attribute. */
  57. if (ni->type != na->type)
  58. return 0;
  59. if (ni->name_len != na->name_len)
  60. return 0;
  61. if (na->name_len && memcmp(ni->name, na->name,
  62. na->name_len * sizeof(ntfschar)))
  63. return 0;
  64. }
  65. /* Match! */
  66. return 1;
  67. }
  68. /**
  69. * ntfs_init_locked_inode - initialize an inode
  70. * @vi: vfs inode to initialize
  71. * @data: data which to initialize @vi to
  72. *
  73. * Initialize the vfs inode @vi with the values from the ntfs attribute @data in
  74. * order to enable ntfs_test_inode() to do its work.
  75. *
  76. * If initializing the normal file/directory inode, set @na->type to AT_UNUSED.
  77. * In that case, @na->name and @na->name_len should be set to NULL and 0,
  78. * respectively. Although that is not strictly necessary as
  79. * ntfs_read_locked_inode() will fill them in later.
  80. *
  81. * Return 0 on success and -errno on error.
  82. *
  83. * NOTE: This function runs with the inode->i_lock spin lock held so it is not
  84. * allowed to sleep. (Hence the GFP_ATOMIC allocation.)
  85. */
  86. static int ntfs_init_locked_inode(struct inode *vi, void *data)
  87. {
  88. ntfs_attr *na = (ntfs_attr *)data;
  89. ntfs_inode *ni = NTFS_I(vi);
  90. vi->i_ino = na->mft_no;
  91. ni->type = na->type;
  92. if (na->type == AT_INDEX_ALLOCATION)
  93. NInoSetMstProtected(ni);
  94. ni->name = na->name;
  95. ni->name_len = na->name_len;
  96. /* If initializing a normal inode, we are done. */
  97. if (likely(na->type == AT_UNUSED)) {
  98. BUG_ON(na->name);
  99. BUG_ON(na->name_len);
  100. return 0;
  101. }
  102. /* It is a fake inode. */
  103. NInoSetAttr(ni);
  104. /*
  105. * We have I30 global constant as an optimization as it is the name
  106. * in >99.9% of named attributes! The other <0.1% incur a GFP_ATOMIC
  107. * allocation but that is ok. And most attributes are unnamed anyway,
  108. * thus the fraction of named attributes with name != I30 is actually
  109. * absolutely tiny.
  110. */
  111. if (na->name_len && na->name != I30) {
  112. unsigned int i;
  113. BUG_ON(!na->name);
  114. i = na->name_len * sizeof(ntfschar);
  115. ni->name = kmalloc(i + sizeof(ntfschar), GFP_ATOMIC);
  116. if (!ni->name)
  117. return -ENOMEM;
  118. memcpy(ni->name, na->name, i);
  119. ni->name[na->name_len] = 0;
  120. }
  121. return 0;
  122. }
  123. static int ntfs_read_locked_inode(struct inode *vi);
  124. static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi);
  125. static int ntfs_read_locked_index_inode(struct inode *base_vi,
  126. struct inode *vi);
  127. /**
  128. * ntfs_iget - obtain a struct inode corresponding to a specific normal inode
  129. * @sb: super block of mounted volume
  130. * @mft_no: mft record number / inode number to obtain
  131. *
  132. * Obtain the struct inode corresponding to a specific normal inode (i.e. a
  133. * file or directory).
  134. *
  135. * If the inode is in the cache, it is just returned with an increased
  136. * reference count. Otherwise, a new struct inode is allocated and initialized,
  137. * and finally ntfs_read_locked_inode() is called to read in the inode and
  138. * fill in the remainder of the inode structure.
  139. *
  140. * Return the struct inode on success. Check the return value with IS_ERR() and
  141. * if true, the function failed and the error code is obtained from PTR_ERR().
  142. */
  143. struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no)
  144. {
  145. struct inode *vi;
  146. int err;
  147. ntfs_attr na;
  148. na.mft_no = mft_no;
  149. na.type = AT_UNUSED;
  150. na.name = NULL;
  151. na.name_len = 0;
  152. vi = iget5_locked(sb, mft_no, ntfs_test_inode,
  153. ntfs_init_locked_inode, &na);
  154. if (unlikely(!vi))
  155. return ERR_PTR(-ENOMEM);
  156. err = 0;
  157. /* If this is a freshly allocated inode, need to read it now. */
  158. if (vi->i_state & I_NEW) {
  159. err = ntfs_read_locked_inode(vi);
  160. unlock_new_inode(vi);
  161. }
  162. /*
  163. * There is no point in keeping bad inodes around if the failure was
  164. * due to ENOMEM. We want to be able to retry again later.
  165. */
  166. if (unlikely(err == -ENOMEM)) {
  167. iput(vi);
  168. vi = ERR_PTR(err);
  169. }
  170. return vi;
  171. }
  172. /**
  173. * ntfs_attr_iget - obtain a struct inode corresponding to an attribute
  174. * @base_vi: vfs base inode containing the attribute
  175. * @type: attribute type
  176. * @name: Unicode name of the attribute (NULL if unnamed)
  177. * @name_len: length of @name in Unicode characters (0 if unnamed)
  178. *
  179. * Obtain the (fake) struct inode corresponding to the attribute specified by
  180. * @type, @name, and @name_len, which is present in the base mft record
  181. * specified by the vfs inode @base_vi.
  182. *
  183. * If the attribute inode is in the cache, it is just returned with an
  184. * increased reference count. Otherwise, a new struct inode is allocated and
  185. * initialized, and finally ntfs_read_locked_attr_inode() is called to read the
  186. * attribute and fill in the inode structure.
  187. *
  188. * Note, for index allocation attributes, you need to use ntfs_index_iget()
  189. * instead of ntfs_attr_iget() as working with indices is a lot more complex.
  190. *
  191. * Return the struct inode of the attribute inode on success. Check the return
  192. * value with IS_ERR() and if true, the function failed and the error code is
  193. * obtained from PTR_ERR().
  194. */
  195. struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
  196. ntfschar *name, u32 name_len)
  197. {
  198. struct inode *vi;
  199. int err;
  200. ntfs_attr na;
  201. /* Make sure no one calls ntfs_attr_iget() for indices. */
  202. BUG_ON(type == AT_INDEX_ALLOCATION);
  203. na.mft_no = base_vi->i_ino;
  204. na.type = type;
  205. na.name = name;
  206. na.name_len = name_len;
  207. vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode,
  208. ntfs_init_locked_inode, &na);
  209. if (unlikely(!vi))
  210. return ERR_PTR(-ENOMEM);
  211. err = 0;
  212. /* If this is a freshly allocated inode, need to read it now. */
  213. if (vi->i_state & I_NEW) {
  214. err = ntfs_read_locked_attr_inode(base_vi, vi);
  215. unlock_new_inode(vi);
  216. }
  217. /*
  218. * There is no point in keeping bad attribute inodes around. This also
  219. * simplifies things in that we never need to check for bad attribute
  220. * inodes elsewhere.
  221. */
  222. if (unlikely(err)) {
  223. iput(vi);
  224. vi = ERR_PTR(err);
  225. }
  226. return vi;
  227. }
  228. /**
  229. * ntfs_index_iget - obtain a struct inode corresponding to an index
  230. * @base_vi: vfs base inode containing the index related attributes
  231. * @name: Unicode name of the index
  232. * @name_len: length of @name in Unicode characters
  233. *
  234. * Obtain the (fake) struct inode corresponding to the index specified by @name
  235. * and @name_len, which is present in the base mft record specified by the vfs
  236. * inode @base_vi.
  237. *
  238. * If the index inode is in the cache, it is just returned with an increased
  239. * reference count. Otherwise, a new struct inode is allocated and
  240. * initialized, and finally ntfs_read_locked_index_inode() is called to read
  241. * the index related attributes and fill in the inode structure.
  242. *
  243. * Return the struct inode of the index inode on success. Check the return
  244. * value with IS_ERR() and if true, the function failed and the error code is
  245. * obtained from PTR_ERR().
  246. */
  247. struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
  248. u32 name_len)
  249. {
  250. struct inode *vi;
  251. int err;
  252. ntfs_attr na;
  253. na.mft_no = base_vi->i_ino;
  254. na.type = AT_INDEX_ALLOCATION;
  255. na.name = name;
  256. na.name_len = name_len;
  257. vi = iget5_locked(base_vi->i_sb, na.mft_no, ntfs_test_inode,
  258. ntfs_init_locked_inode, &na);
  259. if (unlikely(!vi))
  260. return ERR_PTR(-ENOMEM);
  261. err = 0;
  262. /* If this is a freshly allocated inode, need to read it now. */
  263. if (vi->i_state & I_NEW) {
  264. err = ntfs_read_locked_index_inode(base_vi, vi);
  265. unlock_new_inode(vi);
  266. }
  267. /*
  268. * There is no point in keeping bad index inodes around. This also
  269. * simplifies things in that we never need to check for bad index
  270. * inodes elsewhere.
  271. */
  272. if (unlikely(err)) {
  273. iput(vi);
  274. vi = ERR_PTR(err);
  275. }
  276. return vi;
  277. }
  278. struct inode *ntfs_alloc_big_inode(struct super_block *sb)
  279. {
  280. ntfs_inode *ni;
  281. ntfs_debug("Entering.");
  282. ni = kmem_cache_alloc(ntfs_big_inode_cache, GFP_NOFS);
  283. if (likely(ni != NULL)) {
  284. ni->state = 0;
  285. return VFS_I(ni);
  286. }
  287. ntfs_error(sb, "Allocation of NTFS big inode structure failed.");
  288. return NULL;
  289. }
  290. void ntfs_free_big_inode(struct inode *inode)
  291. {
  292. kmem_cache_free(ntfs_big_inode_cache, NTFS_I(inode));
  293. }
  294. static inline ntfs_inode *ntfs_alloc_extent_inode(void)
  295. {
  296. ntfs_inode *ni;
  297. ntfs_debug("Entering.");
  298. ni = kmem_cache_alloc(ntfs_inode_cache, GFP_NOFS);
  299. if (likely(ni != NULL)) {
  300. ni->state = 0;
  301. return ni;
  302. }
  303. ntfs_error(NULL, "Allocation of NTFS inode structure failed.");
  304. return NULL;
  305. }
  306. static void ntfs_destroy_extent_inode(ntfs_inode *ni)
  307. {
  308. ntfs_debug("Entering.");
  309. BUG_ON(ni->page);
  310. if (!atomic_dec_and_test(&ni->count))
  311. BUG();
  312. kmem_cache_free(ntfs_inode_cache, ni);
  313. }
  314. /*
  315. * The attribute runlist lock has separate locking rules from the
  316. * normal runlist lock, so split the two lock-classes:
  317. */
  318. static struct lock_class_key attr_list_rl_lock_class;
  319. /**
  320. * __ntfs_init_inode - initialize ntfs specific part of an inode
  321. * @sb: super block of mounted volume
  322. * @ni: freshly allocated ntfs inode which to initialize
  323. *
  324. * Initialize an ntfs inode to defaults.
  325. *
  326. * NOTE: ni->mft_no, ni->state, ni->type, ni->name, and ni->name_len are left
  327. * untouched. Make sure to initialize them elsewhere.
  328. *
  329. * Return zero on success and -ENOMEM on error.
  330. */
  331. void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni)
  332. {
  333. ntfs_debug("Entering.");
  334. rwlock_init(&ni->size_lock);
  335. ni->initialized_size = ni->allocated_size = 0;
  336. ni->seq_no = 0;
  337. atomic_set(&ni->count, 1);
  338. ni->vol = NTFS_SB(sb);
  339. ntfs_init_runlist(&ni->runlist);
  340. mutex_init(&ni->mrec_lock);
  341. ni->page = NULL;
  342. ni->page_ofs = 0;
  343. ni->attr_list_size = 0;
  344. ni->attr_list = NULL;
  345. ntfs_init_runlist(&ni->attr_list_rl);
  346. lockdep_set_class(&ni->attr_list_rl.lock,
  347. &attr_list_rl_lock_class);
  348. ni->itype.index.block_size = 0;
  349. ni->itype.index.vcn_size = 0;
  350. ni->itype.index.collation_rule = 0;
  351. ni->itype.index.block_size_bits = 0;
  352. ni->itype.index.vcn_size_bits = 0;
  353. mutex_init(&ni->extent_lock);
  354. ni->nr_extents = 0;
  355. ni->ext.base_ntfs_ino = NULL;
  356. }
  357. /*
  358. * Extent inodes get MFT-mapped in a nested way, while the base inode
  359. * is still mapped. Teach this nesting to the lock validator by creating
  360. * a separate class for nested inode's mrec_lock's:
  361. */
  362. static struct lock_class_key extent_inode_mrec_lock_key;
  363. inline ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
  364. unsigned long mft_no)
  365. {
  366. ntfs_inode *ni = ntfs_alloc_extent_inode();
  367. ntfs_debug("Entering.");
  368. if (likely(ni != NULL)) {
  369. __ntfs_init_inode(sb, ni);
  370. lockdep_set_class(&ni->mrec_lock, &extent_inode_mrec_lock_key);
  371. ni->mft_no = mft_no;
  372. ni->type = AT_UNUSED;
  373. ni->name = NULL;
  374. ni->name_len = 0;
  375. }
  376. return ni;
  377. }
  378. /**
  379. * ntfs_is_extended_system_file - check if a file is in the $Extend directory
  380. * @ctx: initialized attribute search context
  381. *
  382. * Search all file name attributes in the inode described by the attribute
  383. * search context @ctx and check if any of the names are in the $Extend system
  384. * directory.
  385. *
  386. * Return values:
  387. * 1: file is in $Extend directory
  388. * 0: file is not in $Extend directory
  389. * -errno: failed to determine if the file is in the $Extend directory
  390. */
  391. static int ntfs_is_extended_system_file(ntfs_attr_search_ctx *ctx)
  392. {
  393. int nr_links, err;
  394. /* Restart search. */
  395. ntfs_attr_reinit_search_ctx(ctx);
  396. /* Get number of hard links. */
  397. nr_links = le16_to_cpu(ctx->mrec->link_count);
  398. /* Loop through all hard links. */
  399. while (!(err = ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0,
  400. ctx))) {
  401. FILE_NAME_ATTR *file_name_attr;
  402. ATTR_RECORD *attr = ctx->attr;
  403. u8 *p, *p2;
  404. nr_links--;
  405. /*
  406. * Maximum sanity checking as we are called on an inode that
  407. * we suspect might be corrupt.
  408. */
  409. p = (u8*)attr + le32_to_cpu(attr->length);
  410. if (p < (u8*)ctx->mrec || (u8*)p > (u8*)ctx->mrec +
  411. le32_to_cpu(ctx->mrec->bytes_in_use)) {
  412. err_corrupt_attr:
  413. ntfs_error(ctx->ntfs_ino->vol->sb, "Corrupt file name "
  414. "attribute. You should run chkdsk.");
  415. return -EIO;
  416. }
  417. if (attr->non_resident) {
  418. ntfs_error(ctx->ntfs_ino->vol->sb, "Non-resident file "
  419. "name. You should run chkdsk.");
  420. return -EIO;
  421. }
  422. if (attr->flags) {
  423. ntfs_error(ctx->ntfs_ino->vol->sb, "File name with "
  424. "invalid flags. You should run "
  425. "chkdsk.");
  426. return -EIO;
  427. }
  428. if (!(attr->data.resident.flags & RESIDENT_ATTR_IS_INDEXED)) {
  429. ntfs_error(ctx->ntfs_ino->vol->sb, "Unindexed file "
  430. "name. You should run chkdsk.");
  431. return -EIO;
  432. }
  433. file_name_attr = (FILE_NAME_ATTR*)((u8*)attr +
  434. le16_to_cpu(attr->data.resident.value_offset));
  435. p2 = (u8 *)file_name_attr + le32_to_cpu(attr->data.resident.value_length);
  436. if (p2 < (u8*)attr || p2 > p)
  437. goto err_corrupt_attr;
  438. /* This attribute is ok, but is it in the $Extend directory? */
  439. if (MREF_LE(file_name_attr->parent_directory) == FILE_Extend)
  440. return 1; /* YES, it's an extended system file. */
  441. }
  442. if (unlikely(err != -ENOENT))
  443. return err;
  444. if (unlikely(nr_links)) {
  445. ntfs_error(ctx->ntfs_ino->vol->sb, "Inode hard link count "
  446. "doesn't match number of name attributes. You "
  447. "should run chkdsk.");
  448. return -EIO;
  449. }
  450. return 0; /* NO, it is not an extended system file. */
  451. }
  452. /**
  453. * ntfs_read_locked_inode - read an inode from its device
  454. * @vi: inode to read
  455. *
  456. * ntfs_read_locked_inode() is called from ntfs_iget() to read the inode
  457. * described by @vi into memory from the device.
  458. *
  459. * The only fields in @vi that we need to/can look at when the function is
  460. * called are i_sb, pointing to the mounted device's super block, and i_ino,
  461. * the number of the inode to load.
  462. *
  463. * ntfs_read_locked_inode() maps, pins and locks the mft record number i_ino
  464. * for reading and sets up the necessary @vi fields as well as initializing
  465. * the ntfs inode.
  466. *
  467. * Q: What locks are held when the function is called?
  468. * A: i_state has I_NEW set, hence the inode is locked, also
  469. * i_count is set to 1, so it is not going to go away
  470. * i_flags is set to 0 and we have no business touching it. Only an ioctl()
  471. * is allowed to write to them. We should of course be honouring them but
  472. * we need to do that using the IS_* macros defined in include/linux/fs.h.
  473. * In any case ntfs_read_locked_inode() has nothing to do with i_flags.
  474. *
  475. * Return 0 on success and -errno on error. In the error case, the inode will
  476. * have had make_bad_inode() executed on it.
  477. */
  478. static int ntfs_read_locked_inode(struct inode *vi)
  479. {
  480. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  481. ntfs_inode *ni;
  482. struct inode *bvi;
  483. MFT_RECORD *m;
  484. ATTR_RECORD *a;
  485. STANDARD_INFORMATION *si;
  486. ntfs_attr_search_ctx *ctx;
  487. int err = 0;
  488. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  489. /* Setup the generic vfs inode parts now. */
  490. vi->i_uid = vol->uid;
  491. vi->i_gid = vol->gid;
  492. vi->i_mode = 0;
  493. /*
  494. * Initialize the ntfs specific part of @vi special casing
  495. * FILE_MFT which we need to do at mount time.
  496. */
  497. if (vi->i_ino != FILE_MFT)
  498. ntfs_init_big_inode(vi);
  499. ni = NTFS_I(vi);
  500. m = map_mft_record(ni);
  501. if (IS_ERR(m)) {
  502. err = PTR_ERR(m);
  503. goto err_out;
  504. }
  505. ctx = ntfs_attr_get_search_ctx(ni, m);
  506. if (!ctx) {
  507. err = -ENOMEM;
  508. goto unm_err_out;
  509. }
  510. if (!(m->flags & MFT_RECORD_IN_USE)) {
  511. ntfs_error(vi->i_sb, "Inode is not in use!");
  512. goto unm_err_out;
  513. }
  514. if (m->base_mft_record) {
  515. ntfs_error(vi->i_sb, "Inode is an extent inode!");
  516. goto unm_err_out;
  517. }
  518. /* Transfer information from mft record into vfs and ntfs inodes. */
  519. vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
  520. /*
  521. * FIXME: Keep in mind that link_count is two for files which have both
  522. * a long file name and a short file name as separate entries, so if
  523. * we are hiding short file names this will be too high. Either we need
  524. * to account for the short file names by subtracting them or we need
  525. * to make sure we delete files even though i_nlink is not zero which
  526. * might be tricky due to vfs interactions. Need to think about this
  527. * some more when implementing the unlink command.
  528. */
  529. set_nlink(vi, le16_to_cpu(m->link_count));
  530. /*
  531. * FIXME: Reparse points can have the directory bit set even though
  532. * they would be S_IFLNK. Need to deal with this further below when we
  533. * implement reparse points / symbolic links but it will do for now.
  534. * Also if not a directory, it could be something else, rather than
  535. * a regular file. But again, will do for now.
  536. */
  537. /* Everyone gets all permissions. */
  538. vi->i_mode |= S_IRWXUGO;
  539. /* If read-only, no one gets write permissions. */
  540. if (IS_RDONLY(vi))
  541. vi->i_mode &= ~S_IWUGO;
  542. if (m->flags & MFT_RECORD_IS_DIRECTORY) {
  543. vi->i_mode |= S_IFDIR;
  544. /*
  545. * Apply the directory permissions mask set in the mount
  546. * options.
  547. */
  548. vi->i_mode &= ~vol->dmask;
  549. /* Things break without this kludge! */
  550. if (vi->i_nlink > 1)
  551. set_nlink(vi, 1);
  552. } else {
  553. vi->i_mode |= S_IFREG;
  554. /* Apply the file permissions mask set in the mount options. */
  555. vi->i_mode &= ~vol->fmask;
  556. }
  557. /*
  558. * Find the standard information attribute in the mft record. At this
  559. * stage we haven't setup the attribute list stuff yet, so this could
  560. * in fact fail if the standard information is in an extent record, but
  561. * I don't think this actually ever happens.
  562. */
  563. err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0, 0, 0, NULL, 0,
  564. ctx);
  565. if (unlikely(err)) {
  566. if (err == -ENOENT) {
  567. /*
  568. * TODO: We should be performing a hot fix here (if the
  569. * recover mount option is set) by creating a new
  570. * attribute.
  571. */
  572. ntfs_error(vi->i_sb, "$STANDARD_INFORMATION attribute "
  573. "is missing.");
  574. }
  575. goto unm_err_out;
  576. }
  577. a = ctx->attr;
  578. /* Get the standard information attribute value. */
  579. if ((u8 *)a + le16_to_cpu(a->data.resident.value_offset)
  580. + le32_to_cpu(a->data.resident.value_length) >
  581. (u8 *)ctx->mrec + vol->mft_record_size) {
  582. ntfs_error(vi->i_sb, "Corrupt standard information attribute in inode.");
  583. goto unm_err_out;
  584. }
  585. si = (STANDARD_INFORMATION*)((u8*)a +
  586. le16_to_cpu(a->data.resident.value_offset));
  587. /* Transfer information from the standard information into vi. */
  588. /*
  589. * Note: The i_?times do not quite map perfectly onto the NTFS times,
  590. * but they are close enough, and in the end it doesn't really matter
  591. * that much...
  592. */
  593. /*
  594. * mtime is the last change of the data within the file. Not changed
  595. * when only metadata is changed, e.g. a rename doesn't affect mtime.
  596. */
  597. vi->i_mtime = ntfs2utc(si->last_data_change_time);
  598. /*
  599. * ctime is the last change of the metadata of the file. This obviously
  600. * always changes, when mtime is changed. ctime can be changed on its
  601. * own, mtime is then not changed, e.g. when a file is renamed.
  602. */
  603. vi->i_ctime = ntfs2utc(si->last_mft_change_time);
  604. /*
  605. * Last access to the data within the file. Not changed during a rename
  606. * for example but changed whenever the file is written to.
  607. */
  608. vi->i_atime = ntfs2utc(si->last_access_time);
  609. /* Find the attribute list attribute if present. */
  610. ntfs_attr_reinit_search_ctx(ctx);
  611. err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
  612. if (err) {
  613. if (unlikely(err != -ENOENT)) {
  614. ntfs_error(vi->i_sb, "Failed to lookup attribute list "
  615. "attribute.");
  616. goto unm_err_out;
  617. }
  618. } else /* if (!err) */ {
  619. if (vi->i_ino == FILE_MFT)
  620. goto skip_attr_list_load;
  621. ntfs_debug("Attribute list found in inode 0x%lx.", vi->i_ino);
  622. NInoSetAttrList(ni);
  623. a = ctx->attr;
  624. if (a->flags & ATTR_COMPRESSION_MASK) {
  625. ntfs_error(vi->i_sb, "Attribute list attribute is "
  626. "compressed.");
  627. goto unm_err_out;
  628. }
  629. if (a->flags & ATTR_IS_ENCRYPTED ||
  630. a->flags & ATTR_IS_SPARSE) {
  631. if (a->non_resident) {
  632. ntfs_error(vi->i_sb, "Non-resident attribute "
  633. "list attribute is encrypted/"
  634. "sparse.");
  635. goto unm_err_out;
  636. }
  637. ntfs_warning(vi->i_sb, "Resident attribute list "
  638. "attribute in inode 0x%lx is marked "
  639. "encrypted/sparse which is not true. "
  640. "However, Windows allows this and "
  641. "chkdsk does not detect or correct it "
  642. "so we will just ignore the invalid "
  643. "flags and pretend they are not set.",
  644. vi->i_ino);
  645. }
  646. /* Now allocate memory for the attribute list. */
  647. ni->attr_list_size = (u32)ntfs_attr_size(a);
  648. ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
  649. if (!ni->attr_list) {
  650. ntfs_error(vi->i_sb, "Not enough memory to allocate "
  651. "buffer for attribute list.");
  652. err = -ENOMEM;
  653. goto unm_err_out;
  654. }
  655. if (a->non_resident) {
  656. NInoSetAttrListNonResident(ni);
  657. if (a->data.non_resident.lowest_vcn) {
  658. ntfs_error(vi->i_sb, "Attribute list has non "
  659. "zero lowest_vcn.");
  660. goto unm_err_out;
  661. }
  662. /*
  663. * Setup the runlist. No need for locking as we have
  664. * exclusive access to the inode at this time.
  665. */
  666. ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
  667. a, NULL);
  668. if (IS_ERR(ni->attr_list_rl.rl)) {
  669. err = PTR_ERR(ni->attr_list_rl.rl);
  670. ni->attr_list_rl.rl = NULL;
  671. ntfs_error(vi->i_sb, "Mapping pairs "
  672. "decompression failed.");
  673. goto unm_err_out;
  674. }
  675. /* Now load the attribute list. */
  676. if ((err = load_attribute_list(vol, &ni->attr_list_rl,
  677. ni->attr_list, ni->attr_list_size,
  678. sle64_to_cpu(a->data.non_resident.
  679. initialized_size)))) {
  680. ntfs_error(vi->i_sb, "Failed to load "
  681. "attribute list attribute.");
  682. goto unm_err_out;
  683. }
  684. } else /* if (!a->non_resident) */ {
  685. if ((u8*)a + le16_to_cpu(a->data.resident.value_offset)
  686. + le32_to_cpu(
  687. a->data.resident.value_length) >
  688. (u8*)ctx->mrec + vol->mft_record_size) {
  689. ntfs_error(vi->i_sb, "Corrupt attribute list "
  690. "in inode.");
  691. goto unm_err_out;
  692. }
  693. /* Now copy the attribute list. */
  694. memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
  695. a->data.resident.value_offset),
  696. le32_to_cpu(
  697. a->data.resident.value_length));
  698. }
  699. }
  700. skip_attr_list_load:
  701. /*
  702. * If an attribute list is present we now have the attribute list value
  703. * in ntfs_ino->attr_list and it is ntfs_ino->attr_list_size bytes.
  704. */
  705. if (S_ISDIR(vi->i_mode)) {
  706. loff_t bvi_size;
  707. ntfs_inode *bni;
  708. INDEX_ROOT *ir;
  709. u8 *ir_end, *index_end;
  710. /* It is a directory, find index root attribute. */
  711. ntfs_attr_reinit_search_ctx(ctx);
  712. err = ntfs_attr_lookup(AT_INDEX_ROOT, I30, 4, CASE_SENSITIVE,
  713. 0, NULL, 0, ctx);
  714. if (unlikely(err)) {
  715. if (err == -ENOENT) {
  716. // FIXME: File is corrupt! Hot-fix with empty
  717. // index root attribute if recovery option is
  718. // set.
  719. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute "
  720. "is missing.");
  721. }
  722. goto unm_err_out;
  723. }
  724. a = ctx->attr;
  725. /* Set up the state. */
  726. if (unlikely(a->non_resident)) {
  727. ntfs_error(vol->sb, "$INDEX_ROOT attribute is not "
  728. "resident.");
  729. goto unm_err_out;
  730. }
  731. /* Ensure the attribute name is placed before the value. */
  732. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  733. le16_to_cpu(a->data.resident.value_offset)))) {
  734. ntfs_error(vol->sb, "$INDEX_ROOT attribute name is "
  735. "placed after the attribute value.");
  736. goto unm_err_out;
  737. }
  738. /*
  739. * Compressed/encrypted index root just means that the newly
  740. * created files in that directory should be created compressed/
  741. * encrypted. However index root cannot be both compressed and
  742. * encrypted.
  743. */
  744. if (a->flags & ATTR_COMPRESSION_MASK)
  745. NInoSetCompressed(ni);
  746. if (a->flags & ATTR_IS_ENCRYPTED) {
  747. if (a->flags & ATTR_COMPRESSION_MASK) {
  748. ntfs_error(vi->i_sb, "Found encrypted and "
  749. "compressed attribute.");
  750. goto unm_err_out;
  751. }
  752. NInoSetEncrypted(ni);
  753. }
  754. if (a->flags & ATTR_IS_SPARSE)
  755. NInoSetSparse(ni);
  756. ir = (INDEX_ROOT*)((u8*)a +
  757. le16_to_cpu(a->data.resident.value_offset));
  758. ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
  759. if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
  760. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
  761. "corrupt.");
  762. goto unm_err_out;
  763. }
  764. index_end = (u8*)&ir->index +
  765. le32_to_cpu(ir->index.index_length);
  766. if (index_end > ir_end) {
  767. ntfs_error(vi->i_sb, "Directory index is corrupt.");
  768. goto unm_err_out;
  769. }
  770. if (ir->type != AT_FILE_NAME) {
  771. ntfs_error(vi->i_sb, "Indexed attribute is not "
  772. "$FILE_NAME.");
  773. goto unm_err_out;
  774. }
  775. if (ir->collation_rule != COLLATION_FILE_NAME) {
  776. ntfs_error(vi->i_sb, "Index collation rule is not "
  777. "COLLATION_FILE_NAME.");
  778. goto unm_err_out;
  779. }
  780. ni->itype.index.collation_rule = ir->collation_rule;
  781. ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
  782. if (ni->itype.index.block_size &
  783. (ni->itype.index.block_size - 1)) {
  784. ntfs_error(vi->i_sb, "Index block size (%u) is not a "
  785. "power of two.",
  786. ni->itype.index.block_size);
  787. goto unm_err_out;
  788. }
  789. if (ni->itype.index.block_size > PAGE_SIZE) {
  790. ntfs_error(vi->i_sb, "Index block size (%u) > "
  791. "PAGE_SIZE (%ld) is not "
  792. "supported. Sorry.",
  793. ni->itype.index.block_size,
  794. PAGE_SIZE);
  795. err = -EOPNOTSUPP;
  796. goto unm_err_out;
  797. }
  798. if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
  799. ntfs_error(vi->i_sb, "Index block size (%u) < "
  800. "NTFS_BLOCK_SIZE (%i) is not "
  801. "supported. Sorry.",
  802. ni->itype.index.block_size,
  803. NTFS_BLOCK_SIZE);
  804. err = -EOPNOTSUPP;
  805. goto unm_err_out;
  806. }
  807. ni->itype.index.block_size_bits =
  808. ffs(ni->itype.index.block_size) - 1;
  809. /* Determine the size of a vcn in the directory index. */
  810. if (vol->cluster_size <= ni->itype.index.block_size) {
  811. ni->itype.index.vcn_size = vol->cluster_size;
  812. ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
  813. } else {
  814. ni->itype.index.vcn_size = vol->sector_size;
  815. ni->itype.index.vcn_size_bits = vol->sector_size_bits;
  816. }
  817. /* Setup the index allocation attribute, even if not present. */
  818. NInoSetMstProtected(ni);
  819. ni->type = AT_INDEX_ALLOCATION;
  820. ni->name = I30;
  821. ni->name_len = 4;
  822. if (!(ir->index.flags & LARGE_INDEX)) {
  823. /* No index allocation. */
  824. vi->i_size = ni->initialized_size =
  825. ni->allocated_size = 0;
  826. /* We are done with the mft record, so we release it. */
  827. ntfs_attr_put_search_ctx(ctx);
  828. unmap_mft_record(ni);
  829. m = NULL;
  830. ctx = NULL;
  831. goto skip_large_dir_stuff;
  832. } /* LARGE_INDEX: Index allocation present. Setup state. */
  833. NInoSetIndexAllocPresent(ni);
  834. /* Find index allocation attribute. */
  835. ntfs_attr_reinit_search_ctx(ctx);
  836. err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, I30, 4,
  837. CASE_SENSITIVE, 0, NULL, 0, ctx);
  838. if (unlikely(err)) {
  839. if (err == -ENOENT)
  840. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION "
  841. "attribute is not present but "
  842. "$INDEX_ROOT indicated it is.");
  843. else
  844. ntfs_error(vi->i_sb, "Failed to lookup "
  845. "$INDEX_ALLOCATION "
  846. "attribute.");
  847. goto unm_err_out;
  848. }
  849. a = ctx->attr;
  850. if (!a->non_resident) {
  851. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  852. "is resident.");
  853. goto unm_err_out;
  854. }
  855. /*
  856. * Ensure the attribute name is placed before the mapping pairs
  857. * array.
  858. */
  859. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  860. le16_to_cpu(
  861. a->data.non_resident.mapping_pairs_offset)))) {
  862. ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name "
  863. "is placed after the mapping pairs "
  864. "array.");
  865. goto unm_err_out;
  866. }
  867. if (a->flags & ATTR_IS_ENCRYPTED) {
  868. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  869. "is encrypted.");
  870. goto unm_err_out;
  871. }
  872. if (a->flags & ATTR_IS_SPARSE) {
  873. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  874. "is sparse.");
  875. goto unm_err_out;
  876. }
  877. if (a->flags & ATTR_COMPRESSION_MASK) {
  878. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute "
  879. "is compressed.");
  880. goto unm_err_out;
  881. }
  882. if (a->data.non_resident.lowest_vcn) {
  883. ntfs_error(vi->i_sb, "First extent of "
  884. "$INDEX_ALLOCATION attribute has non "
  885. "zero lowest_vcn.");
  886. goto unm_err_out;
  887. }
  888. vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
  889. ni->initialized_size = sle64_to_cpu(
  890. a->data.non_resident.initialized_size);
  891. ni->allocated_size = sle64_to_cpu(
  892. a->data.non_resident.allocated_size);
  893. /*
  894. * We are done with the mft record, so we release it. Otherwise
  895. * we would deadlock in ntfs_attr_iget().
  896. */
  897. ntfs_attr_put_search_ctx(ctx);
  898. unmap_mft_record(ni);
  899. m = NULL;
  900. ctx = NULL;
  901. /* Get the index bitmap attribute inode. */
  902. bvi = ntfs_attr_iget(vi, AT_BITMAP, I30, 4);
  903. if (IS_ERR(bvi)) {
  904. ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
  905. err = PTR_ERR(bvi);
  906. goto unm_err_out;
  907. }
  908. bni = NTFS_I(bvi);
  909. if (NInoCompressed(bni) || NInoEncrypted(bni) ||
  910. NInoSparse(bni)) {
  911. ntfs_error(vi->i_sb, "$BITMAP attribute is compressed "
  912. "and/or encrypted and/or sparse.");
  913. goto iput_unm_err_out;
  914. }
  915. /* Consistency check bitmap size vs. index allocation size. */
  916. bvi_size = i_size_read(bvi);
  917. if ((bvi_size << 3) < (vi->i_size >>
  918. ni->itype.index.block_size_bits)) {
  919. ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) "
  920. "for index allocation (0x%llx).",
  921. bvi_size << 3, vi->i_size);
  922. goto iput_unm_err_out;
  923. }
  924. /* No longer need the bitmap attribute inode. */
  925. iput(bvi);
  926. skip_large_dir_stuff:
  927. /* Setup the operations for this inode. */
  928. vi->i_op = &ntfs_dir_inode_ops;
  929. vi->i_fop = &ntfs_dir_ops;
  930. vi->i_mapping->a_ops = &ntfs_mst_aops;
  931. } else {
  932. /* It is a file. */
  933. ntfs_attr_reinit_search_ctx(ctx);
  934. /* Setup the data attribute, even if not present. */
  935. ni->type = AT_DATA;
  936. ni->name = NULL;
  937. ni->name_len = 0;
  938. /* Find first extent of the unnamed data attribute. */
  939. err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, 0, NULL, 0, ctx);
  940. if (unlikely(err)) {
  941. vi->i_size = ni->initialized_size =
  942. ni->allocated_size = 0;
  943. if (err != -ENOENT) {
  944. ntfs_error(vi->i_sb, "Failed to lookup $DATA "
  945. "attribute.");
  946. goto unm_err_out;
  947. }
  948. /*
  949. * FILE_Secure does not have an unnamed $DATA
  950. * attribute, so we special case it here.
  951. */
  952. if (vi->i_ino == FILE_Secure)
  953. goto no_data_attr_special_case;
  954. /*
  955. * Most if not all the system files in the $Extend
  956. * system directory do not have unnamed data
  957. * attributes so we need to check if the parent
  958. * directory of the file is FILE_Extend and if it is
  959. * ignore this error. To do this we need to get the
  960. * name of this inode from the mft record as the name
  961. * contains the back reference to the parent directory.
  962. */
  963. if (ntfs_is_extended_system_file(ctx) > 0)
  964. goto no_data_attr_special_case;
  965. // FIXME: File is corrupt! Hot-fix with empty data
  966. // attribute if recovery option is set.
  967. ntfs_error(vi->i_sb, "$DATA attribute is missing.");
  968. goto unm_err_out;
  969. }
  970. a = ctx->attr;
  971. /* Setup the state. */
  972. if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
  973. if (a->flags & ATTR_COMPRESSION_MASK) {
  974. NInoSetCompressed(ni);
  975. if (vol->cluster_size > 4096) {
  976. ntfs_error(vi->i_sb, "Found "
  977. "compressed data but "
  978. "compression is "
  979. "disabled due to "
  980. "cluster size (%i) > "
  981. "4kiB.",
  982. vol->cluster_size);
  983. goto unm_err_out;
  984. }
  985. if ((a->flags & ATTR_COMPRESSION_MASK)
  986. != ATTR_IS_COMPRESSED) {
  987. ntfs_error(vi->i_sb, "Found unknown "
  988. "compression method "
  989. "or corrupt file.");
  990. goto unm_err_out;
  991. }
  992. }
  993. if (a->flags & ATTR_IS_SPARSE)
  994. NInoSetSparse(ni);
  995. }
  996. if (a->flags & ATTR_IS_ENCRYPTED) {
  997. if (NInoCompressed(ni)) {
  998. ntfs_error(vi->i_sb, "Found encrypted and "
  999. "compressed data.");
  1000. goto unm_err_out;
  1001. }
  1002. NInoSetEncrypted(ni);
  1003. }
  1004. if (a->non_resident) {
  1005. NInoSetNonResident(ni);
  1006. if (NInoCompressed(ni) || NInoSparse(ni)) {
  1007. if (NInoCompressed(ni) && a->data.non_resident.
  1008. compression_unit != 4) {
  1009. ntfs_error(vi->i_sb, "Found "
  1010. "non-standard "
  1011. "compression unit (%u "
  1012. "instead of 4). "
  1013. "Cannot handle this.",
  1014. a->data.non_resident.
  1015. compression_unit);
  1016. err = -EOPNOTSUPP;
  1017. goto unm_err_out;
  1018. }
  1019. if (a->data.non_resident.compression_unit) {
  1020. ni->itype.compressed.block_size = 1U <<
  1021. (a->data.non_resident.
  1022. compression_unit +
  1023. vol->cluster_size_bits);
  1024. ni->itype.compressed.block_size_bits =
  1025. ffs(ni->itype.
  1026. compressed.
  1027. block_size) - 1;
  1028. ni->itype.compressed.block_clusters =
  1029. 1U << a->data.
  1030. non_resident.
  1031. compression_unit;
  1032. } else {
  1033. ni->itype.compressed.block_size = 0;
  1034. ni->itype.compressed.block_size_bits =
  1035. 0;
  1036. ni->itype.compressed.block_clusters =
  1037. 0;
  1038. }
  1039. ni->itype.compressed.size = sle64_to_cpu(
  1040. a->data.non_resident.
  1041. compressed_size);
  1042. }
  1043. if (a->data.non_resident.lowest_vcn) {
  1044. ntfs_error(vi->i_sb, "First extent of $DATA "
  1045. "attribute has non zero "
  1046. "lowest_vcn.");
  1047. goto unm_err_out;
  1048. }
  1049. vi->i_size = sle64_to_cpu(
  1050. a->data.non_resident.data_size);
  1051. ni->initialized_size = sle64_to_cpu(
  1052. a->data.non_resident.initialized_size);
  1053. ni->allocated_size = sle64_to_cpu(
  1054. a->data.non_resident.allocated_size);
  1055. } else { /* Resident attribute. */
  1056. vi->i_size = ni->initialized_size = le32_to_cpu(
  1057. a->data.resident.value_length);
  1058. ni->allocated_size = le32_to_cpu(a->length) -
  1059. le16_to_cpu(
  1060. a->data.resident.value_offset);
  1061. if (vi->i_size > ni->allocated_size) {
  1062. ntfs_error(vi->i_sb, "Resident data attribute "
  1063. "is corrupt (size exceeds "
  1064. "allocation).");
  1065. goto unm_err_out;
  1066. }
  1067. }
  1068. no_data_attr_special_case:
  1069. /* We are done with the mft record, so we release it. */
  1070. ntfs_attr_put_search_ctx(ctx);
  1071. unmap_mft_record(ni);
  1072. m = NULL;
  1073. ctx = NULL;
  1074. /* Setup the operations for this inode. */
  1075. vi->i_op = &ntfs_file_inode_ops;
  1076. vi->i_fop = &ntfs_file_ops;
  1077. vi->i_mapping->a_ops = &ntfs_normal_aops;
  1078. if (NInoMstProtected(ni))
  1079. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1080. else if (NInoCompressed(ni))
  1081. vi->i_mapping->a_ops = &ntfs_compressed_aops;
  1082. }
  1083. /*
  1084. * The number of 512-byte blocks used on disk (for stat). This is in so
  1085. * far inaccurate as it doesn't account for any named streams or other
  1086. * special non-resident attributes, but that is how Windows works, too,
  1087. * so we are at least consistent with Windows, if not entirely
  1088. * consistent with the Linux Way. Doing it the Linux Way would cause a
  1089. * significant slowdown as it would involve iterating over all
  1090. * attributes in the mft record and adding the allocated/compressed
  1091. * sizes of all non-resident attributes present to give us the Linux
  1092. * correct size that should go into i_blocks (after division by 512).
  1093. */
  1094. if (S_ISREG(vi->i_mode) && (NInoCompressed(ni) || NInoSparse(ni)))
  1095. vi->i_blocks = ni->itype.compressed.size >> 9;
  1096. else
  1097. vi->i_blocks = ni->allocated_size >> 9;
  1098. ntfs_debug("Done.");
  1099. return 0;
  1100. iput_unm_err_out:
  1101. iput(bvi);
  1102. unm_err_out:
  1103. if (!err)
  1104. err = -EIO;
  1105. if (ctx)
  1106. ntfs_attr_put_search_ctx(ctx);
  1107. if (m)
  1108. unmap_mft_record(ni);
  1109. err_out:
  1110. ntfs_error(vol->sb, "Failed with error code %i. Marking corrupt "
  1111. "inode 0x%lx as bad. Run chkdsk.", err, vi->i_ino);
  1112. make_bad_inode(vi);
  1113. if (err != -EOPNOTSUPP && err != -ENOMEM)
  1114. NVolSetErrors(vol);
  1115. return err;
  1116. }
  1117. /**
  1118. * ntfs_read_locked_attr_inode - read an attribute inode from its base inode
  1119. * @base_vi: base inode
  1120. * @vi: attribute inode to read
  1121. *
  1122. * ntfs_read_locked_attr_inode() is called from ntfs_attr_iget() to read the
  1123. * attribute inode described by @vi into memory from the base mft record
  1124. * described by @base_ni.
  1125. *
  1126. * ntfs_read_locked_attr_inode() maps, pins and locks the base inode for
  1127. * reading and looks up the attribute described by @vi before setting up the
  1128. * necessary fields in @vi as well as initializing the ntfs inode.
  1129. *
  1130. * Q: What locks are held when the function is called?
  1131. * A: i_state has I_NEW set, hence the inode is locked, also
  1132. * i_count is set to 1, so it is not going to go away
  1133. *
  1134. * Return 0 on success and -errno on error. In the error case, the inode will
  1135. * have had make_bad_inode() executed on it.
  1136. *
  1137. * Note this cannot be called for AT_INDEX_ALLOCATION.
  1138. */
  1139. static int ntfs_read_locked_attr_inode(struct inode *base_vi, struct inode *vi)
  1140. {
  1141. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  1142. ntfs_inode *ni, *base_ni;
  1143. MFT_RECORD *m;
  1144. ATTR_RECORD *a;
  1145. ntfs_attr_search_ctx *ctx;
  1146. int err = 0;
  1147. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  1148. ntfs_init_big_inode(vi);
  1149. ni = NTFS_I(vi);
  1150. base_ni = NTFS_I(base_vi);
  1151. /* Just mirror the values from the base inode. */
  1152. vi->i_uid = base_vi->i_uid;
  1153. vi->i_gid = base_vi->i_gid;
  1154. set_nlink(vi, base_vi->i_nlink);
  1155. vi->i_mtime = base_vi->i_mtime;
  1156. vi->i_ctime = base_vi->i_ctime;
  1157. vi->i_atime = base_vi->i_atime;
  1158. vi->i_generation = ni->seq_no = base_ni->seq_no;
  1159. /* Set inode type to zero but preserve permissions. */
  1160. vi->i_mode = base_vi->i_mode & ~S_IFMT;
  1161. m = map_mft_record(base_ni);
  1162. if (IS_ERR(m)) {
  1163. err = PTR_ERR(m);
  1164. goto err_out;
  1165. }
  1166. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1167. if (!ctx) {
  1168. err = -ENOMEM;
  1169. goto unm_err_out;
  1170. }
  1171. /* Find the attribute. */
  1172. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1173. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1174. if (unlikely(err))
  1175. goto unm_err_out;
  1176. a = ctx->attr;
  1177. if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) {
  1178. if (a->flags & ATTR_COMPRESSION_MASK) {
  1179. NInoSetCompressed(ni);
  1180. if ((ni->type != AT_DATA) || (ni->type == AT_DATA &&
  1181. ni->name_len)) {
  1182. ntfs_error(vi->i_sb, "Found compressed "
  1183. "non-data or named data "
  1184. "attribute. Please report "
  1185. "you saw this message to "
  1186. "linux-ntfs-dev@lists."
  1187. "sourceforge.net");
  1188. goto unm_err_out;
  1189. }
  1190. if (vol->cluster_size > 4096) {
  1191. ntfs_error(vi->i_sb, "Found compressed "
  1192. "attribute but compression is "
  1193. "disabled due to cluster size "
  1194. "(%i) > 4kiB.",
  1195. vol->cluster_size);
  1196. goto unm_err_out;
  1197. }
  1198. if ((a->flags & ATTR_COMPRESSION_MASK) !=
  1199. ATTR_IS_COMPRESSED) {
  1200. ntfs_error(vi->i_sb, "Found unknown "
  1201. "compression method.");
  1202. goto unm_err_out;
  1203. }
  1204. }
  1205. /*
  1206. * The compressed/sparse flag set in an index root just means
  1207. * to compress all files.
  1208. */
  1209. if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
  1210. ntfs_error(vi->i_sb, "Found mst protected attribute "
  1211. "but the attribute is %s. Please "
  1212. "report you saw this message to "
  1213. "linux-ntfs-dev@lists.sourceforge.net",
  1214. NInoCompressed(ni) ? "compressed" :
  1215. "sparse");
  1216. goto unm_err_out;
  1217. }
  1218. if (a->flags & ATTR_IS_SPARSE)
  1219. NInoSetSparse(ni);
  1220. }
  1221. if (a->flags & ATTR_IS_ENCRYPTED) {
  1222. if (NInoCompressed(ni)) {
  1223. ntfs_error(vi->i_sb, "Found encrypted and compressed "
  1224. "data.");
  1225. goto unm_err_out;
  1226. }
  1227. /*
  1228. * The encryption flag set in an index root just means to
  1229. * encrypt all files.
  1230. */
  1231. if (NInoMstProtected(ni) && ni->type != AT_INDEX_ROOT) {
  1232. ntfs_error(vi->i_sb, "Found mst protected attribute "
  1233. "but the attribute is encrypted. "
  1234. "Please report you saw this message "
  1235. "to linux-ntfs-dev@lists.sourceforge."
  1236. "net");
  1237. goto unm_err_out;
  1238. }
  1239. if (ni->type != AT_DATA) {
  1240. ntfs_error(vi->i_sb, "Found encrypted non-data "
  1241. "attribute.");
  1242. goto unm_err_out;
  1243. }
  1244. NInoSetEncrypted(ni);
  1245. }
  1246. if (!a->non_resident) {
  1247. /* Ensure the attribute name is placed before the value. */
  1248. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1249. le16_to_cpu(a->data.resident.value_offset)))) {
  1250. ntfs_error(vol->sb, "Attribute name is placed after "
  1251. "the attribute value.");
  1252. goto unm_err_out;
  1253. }
  1254. if (NInoMstProtected(ni)) {
  1255. ntfs_error(vi->i_sb, "Found mst protected attribute "
  1256. "but the attribute is resident. "
  1257. "Please report you saw this message to "
  1258. "linux-ntfs-dev@lists.sourceforge.net");
  1259. goto unm_err_out;
  1260. }
  1261. vi->i_size = ni->initialized_size = le32_to_cpu(
  1262. a->data.resident.value_length);
  1263. ni->allocated_size = le32_to_cpu(a->length) -
  1264. le16_to_cpu(a->data.resident.value_offset);
  1265. if (vi->i_size > ni->allocated_size) {
  1266. ntfs_error(vi->i_sb, "Resident attribute is corrupt "
  1267. "(size exceeds allocation).");
  1268. goto unm_err_out;
  1269. }
  1270. } else {
  1271. NInoSetNonResident(ni);
  1272. /*
  1273. * Ensure the attribute name is placed before the mapping pairs
  1274. * array.
  1275. */
  1276. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1277. le16_to_cpu(
  1278. a->data.non_resident.mapping_pairs_offset)))) {
  1279. ntfs_error(vol->sb, "Attribute name is placed after "
  1280. "the mapping pairs array.");
  1281. goto unm_err_out;
  1282. }
  1283. if (NInoCompressed(ni) || NInoSparse(ni)) {
  1284. if (NInoCompressed(ni) && a->data.non_resident.
  1285. compression_unit != 4) {
  1286. ntfs_error(vi->i_sb, "Found non-standard "
  1287. "compression unit (%u instead "
  1288. "of 4). Cannot handle this.",
  1289. a->data.non_resident.
  1290. compression_unit);
  1291. err = -EOPNOTSUPP;
  1292. goto unm_err_out;
  1293. }
  1294. if (a->data.non_resident.compression_unit) {
  1295. ni->itype.compressed.block_size = 1U <<
  1296. (a->data.non_resident.
  1297. compression_unit +
  1298. vol->cluster_size_bits);
  1299. ni->itype.compressed.block_size_bits =
  1300. ffs(ni->itype.compressed.
  1301. block_size) - 1;
  1302. ni->itype.compressed.block_clusters = 1U <<
  1303. a->data.non_resident.
  1304. compression_unit;
  1305. } else {
  1306. ni->itype.compressed.block_size = 0;
  1307. ni->itype.compressed.block_size_bits = 0;
  1308. ni->itype.compressed.block_clusters = 0;
  1309. }
  1310. ni->itype.compressed.size = sle64_to_cpu(
  1311. a->data.non_resident.compressed_size);
  1312. }
  1313. if (a->data.non_resident.lowest_vcn) {
  1314. ntfs_error(vi->i_sb, "First extent of attribute has "
  1315. "non-zero lowest_vcn.");
  1316. goto unm_err_out;
  1317. }
  1318. vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
  1319. ni->initialized_size = sle64_to_cpu(
  1320. a->data.non_resident.initialized_size);
  1321. ni->allocated_size = sle64_to_cpu(
  1322. a->data.non_resident.allocated_size);
  1323. }
  1324. vi->i_mapping->a_ops = &ntfs_normal_aops;
  1325. if (NInoMstProtected(ni))
  1326. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1327. else if (NInoCompressed(ni))
  1328. vi->i_mapping->a_ops = &ntfs_compressed_aops;
  1329. if ((NInoCompressed(ni) || NInoSparse(ni)) && ni->type != AT_INDEX_ROOT)
  1330. vi->i_blocks = ni->itype.compressed.size >> 9;
  1331. else
  1332. vi->i_blocks = ni->allocated_size >> 9;
  1333. /*
  1334. * Make sure the base inode does not go away and attach it to the
  1335. * attribute inode.
  1336. */
  1337. igrab(base_vi);
  1338. ni->ext.base_ntfs_ino = base_ni;
  1339. ni->nr_extents = -1;
  1340. ntfs_attr_put_search_ctx(ctx);
  1341. unmap_mft_record(base_ni);
  1342. ntfs_debug("Done.");
  1343. return 0;
  1344. unm_err_out:
  1345. if (!err)
  1346. err = -EIO;
  1347. if (ctx)
  1348. ntfs_attr_put_search_ctx(ctx);
  1349. unmap_mft_record(base_ni);
  1350. err_out:
  1351. ntfs_error(vol->sb, "Failed with error code %i while reading attribute "
  1352. "inode (mft_no 0x%lx, type 0x%x, name_len %i). "
  1353. "Marking corrupt inode and base inode 0x%lx as bad. "
  1354. "Run chkdsk.", err, vi->i_ino, ni->type, ni->name_len,
  1355. base_vi->i_ino);
  1356. make_bad_inode(vi);
  1357. if (err != -ENOMEM)
  1358. NVolSetErrors(vol);
  1359. return err;
  1360. }
  1361. /**
  1362. * ntfs_read_locked_index_inode - read an index inode from its base inode
  1363. * @base_vi: base inode
  1364. * @vi: index inode to read
  1365. *
  1366. * ntfs_read_locked_index_inode() is called from ntfs_index_iget() to read the
  1367. * index inode described by @vi into memory from the base mft record described
  1368. * by @base_ni.
  1369. *
  1370. * ntfs_read_locked_index_inode() maps, pins and locks the base inode for
  1371. * reading and looks up the attributes relating to the index described by @vi
  1372. * before setting up the necessary fields in @vi as well as initializing the
  1373. * ntfs inode.
  1374. *
  1375. * Note, index inodes are essentially attribute inodes (NInoAttr() is true)
  1376. * with the attribute type set to AT_INDEX_ALLOCATION. Apart from that, they
  1377. * are setup like directory inodes since directories are a special case of
  1378. * indices ao they need to be treated in much the same way. Most importantly,
  1379. * for small indices the index allocation attribute might not actually exist.
  1380. * However, the index root attribute always exists but this does not need to
  1381. * have an inode associated with it and this is why we define a new inode type
  1382. * index. Also, like for directories, we need to have an attribute inode for
  1383. * the bitmap attribute corresponding to the index allocation attribute and we
  1384. * can store this in the appropriate field of the inode, just like we do for
  1385. * normal directory inodes.
  1386. *
  1387. * Q: What locks are held when the function is called?
  1388. * A: i_state has I_NEW set, hence the inode is locked, also
  1389. * i_count is set to 1, so it is not going to go away
  1390. *
  1391. * Return 0 on success and -errno on error. In the error case, the inode will
  1392. * have had make_bad_inode() executed on it.
  1393. */
  1394. static int ntfs_read_locked_index_inode(struct inode *base_vi, struct inode *vi)
  1395. {
  1396. loff_t bvi_size;
  1397. ntfs_volume *vol = NTFS_SB(vi->i_sb);
  1398. ntfs_inode *ni, *base_ni, *bni;
  1399. struct inode *bvi;
  1400. MFT_RECORD *m;
  1401. ATTR_RECORD *a;
  1402. ntfs_attr_search_ctx *ctx;
  1403. INDEX_ROOT *ir;
  1404. u8 *ir_end, *index_end;
  1405. int err = 0;
  1406. ntfs_debug("Entering for i_ino 0x%lx.", vi->i_ino);
  1407. ntfs_init_big_inode(vi);
  1408. ni = NTFS_I(vi);
  1409. base_ni = NTFS_I(base_vi);
  1410. /* Just mirror the values from the base inode. */
  1411. vi->i_uid = base_vi->i_uid;
  1412. vi->i_gid = base_vi->i_gid;
  1413. set_nlink(vi, base_vi->i_nlink);
  1414. vi->i_mtime = base_vi->i_mtime;
  1415. vi->i_ctime = base_vi->i_ctime;
  1416. vi->i_atime = base_vi->i_atime;
  1417. vi->i_generation = ni->seq_no = base_ni->seq_no;
  1418. /* Set inode type to zero but preserve permissions. */
  1419. vi->i_mode = base_vi->i_mode & ~S_IFMT;
  1420. /* Map the mft record for the base inode. */
  1421. m = map_mft_record(base_ni);
  1422. if (IS_ERR(m)) {
  1423. err = PTR_ERR(m);
  1424. goto err_out;
  1425. }
  1426. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1427. if (!ctx) {
  1428. err = -ENOMEM;
  1429. goto unm_err_out;
  1430. }
  1431. /* Find the index root attribute. */
  1432. err = ntfs_attr_lookup(AT_INDEX_ROOT, ni->name, ni->name_len,
  1433. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1434. if (unlikely(err)) {
  1435. if (err == -ENOENT)
  1436. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is "
  1437. "missing.");
  1438. goto unm_err_out;
  1439. }
  1440. a = ctx->attr;
  1441. /* Set up the state. */
  1442. if (unlikely(a->non_resident)) {
  1443. ntfs_error(vol->sb, "$INDEX_ROOT attribute is not resident.");
  1444. goto unm_err_out;
  1445. }
  1446. /* Ensure the attribute name is placed before the value. */
  1447. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1448. le16_to_cpu(a->data.resident.value_offset)))) {
  1449. ntfs_error(vol->sb, "$INDEX_ROOT attribute name is placed "
  1450. "after the attribute value.");
  1451. goto unm_err_out;
  1452. }
  1453. /*
  1454. * Compressed/encrypted/sparse index root is not allowed, except for
  1455. * directories of course but those are not dealt with here.
  1456. */
  1457. if (a->flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED |
  1458. ATTR_IS_SPARSE)) {
  1459. ntfs_error(vi->i_sb, "Found compressed/encrypted/sparse index "
  1460. "root attribute.");
  1461. goto unm_err_out;
  1462. }
  1463. ir = (INDEX_ROOT*)((u8*)a + le16_to_cpu(a->data.resident.value_offset));
  1464. ir_end = (u8*)ir + le32_to_cpu(a->data.resident.value_length);
  1465. if (ir_end > (u8*)ctx->mrec + vol->mft_record_size) {
  1466. ntfs_error(vi->i_sb, "$INDEX_ROOT attribute is corrupt.");
  1467. goto unm_err_out;
  1468. }
  1469. index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length);
  1470. if (index_end > ir_end) {
  1471. ntfs_error(vi->i_sb, "Index is corrupt.");
  1472. goto unm_err_out;
  1473. }
  1474. if (ir->type) {
  1475. ntfs_error(vi->i_sb, "Index type is not 0 (type is 0x%x).",
  1476. le32_to_cpu(ir->type));
  1477. goto unm_err_out;
  1478. }
  1479. ni->itype.index.collation_rule = ir->collation_rule;
  1480. ntfs_debug("Index collation rule is 0x%x.",
  1481. le32_to_cpu(ir->collation_rule));
  1482. ni->itype.index.block_size = le32_to_cpu(ir->index_block_size);
  1483. if (!is_power_of_2(ni->itype.index.block_size)) {
  1484. ntfs_error(vi->i_sb, "Index block size (%u) is not a power of "
  1485. "two.", ni->itype.index.block_size);
  1486. goto unm_err_out;
  1487. }
  1488. if (ni->itype.index.block_size > PAGE_SIZE) {
  1489. ntfs_error(vi->i_sb, "Index block size (%u) > PAGE_SIZE "
  1490. "(%ld) is not supported. Sorry.",
  1491. ni->itype.index.block_size, PAGE_SIZE);
  1492. err = -EOPNOTSUPP;
  1493. goto unm_err_out;
  1494. }
  1495. if (ni->itype.index.block_size < NTFS_BLOCK_SIZE) {
  1496. ntfs_error(vi->i_sb, "Index block size (%u) < NTFS_BLOCK_SIZE "
  1497. "(%i) is not supported. Sorry.",
  1498. ni->itype.index.block_size, NTFS_BLOCK_SIZE);
  1499. err = -EOPNOTSUPP;
  1500. goto unm_err_out;
  1501. }
  1502. ni->itype.index.block_size_bits = ffs(ni->itype.index.block_size) - 1;
  1503. /* Determine the size of a vcn in the index. */
  1504. if (vol->cluster_size <= ni->itype.index.block_size) {
  1505. ni->itype.index.vcn_size = vol->cluster_size;
  1506. ni->itype.index.vcn_size_bits = vol->cluster_size_bits;
  1507. } else {
  1508. ni->itype.index.vcn_size = vol->sector_size;
  1509. ni->itype.index.vcn_size_bits = vol->sector_size_bits;
  1510. }
  1511. /* Check for presence of index allocation attribute. */
  1512. if (!(ir->index.flags & LARGE_INDEX)) {
  1513. /* No index allocation. */
  1514. vi->i_size = ni->initialized_size = ni->allocated_size = 0;
  1515. /* We are done with the mft record, so we release it. */
  1516. ntfs_attr_put_search_ctx(ctx);
  1517. unmap_mft_record(base_ni);
  1518. m = NULL;
  1519. ctx = NULL;
  1520. goto skip_large_index_stuff;
  1521. } /* LARGE_INDEX: Index allocation present. Setup state. */
  1522. NInoSetIndexAllocPresent(ni);
  1523. /* Find index allocation attribute. */
  1524. ntfs_attr_reinit_search_ctx(ctx);
  1525. err = ntfs_attr_lookup(AT_INDEX_ALLOCATION, ni->name, ni->name_len,
  1526. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1527. if (unlikely(err)) {
  1528. if (err == -ENOENT)
  1529. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1530. "not present but $INDEX_ROOT "
  1531. "indicated it is.");
  1532. else
  1533. ntfs_error(vi->i_sb, "Failed to lookup "
  1534. "$INDEX_ALLOCATION attribute.");
  1535. goto unm_err_out;
  1536. }
  1537. a = ctx->attr;
  1538. if (!a->non_resident) {
  1539. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1540. "resident.");
  1541. goto unm_err_out;
  1542. }
  1543. /*
  1544. * Ensure the attribute name is placed before the mapping pairs array.
  1545. */
  1546. if (unlikely(a->name_length && (le16_to_cpu(a->name_offset) >=
  1547. le16_to_cpu(
  1548. a->data.non_resident.mapping_pairs_offset)))) {
  1549. ntfs_error(vol->sb, "$INDEX_ALLOCATION attribute name is "
  1550. "placed after the mapping pairs array.");
  1551. goto unm_err_out;
  1552. }
  1553. if (a->flags & ATTR_IS_ENCRYPTED) {
  1554. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1555. "encrypted.");
  1556. goto unm_err_out;
  1557. }
  1558. if (a->flags & ATTR_IS_SPARSE) {
  1559. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is sparse.");
  1560. goto unm_err_out;
  1561. }
  1562. if (a->flags & ATTR_COMPRESSION_MASK) {
  1563. ntfs_error(vi->i_sb, "$INDEX_ALLOCATION attribute is "
  1564. "compressed.");
  1565. goto unm_err_out;
  1566. }
  1567. if (a->data.non_resident.lowest_vcn) {
  1568. ntfs_error(vi->i_sb, "First extent of $INDEX_ALLOCATION "
  1569. "attribute has non zero lowest_vcn.");
  1570. goto unm_err_out;
  1571. }
  1572. vi->i_size = sle64_to_cpu(a->data.non_resident.data_size);
  1573. ni->initialized_size = sle64_to_cpu(
  1574. a->data.non_resident.initialized_size);
  1575. ni->allocated_size = sle64_to_cpu(a->data.non_resident.allocated_size);
  1576. /*
  1577. * We are done with the mft record, so we release it. Otherwise
  1578. * we would deadlock in ntfs_attr_iget().
  1579. */
  1580. ntfs_attr_put_search_ctx(ctx);
  1581. unmap_mft_record(base_ni);
  1582. m = NULL;
  1583. ctx = NULL;
  1584. /* Get the index bitmap attribute inode. */
  1585. bvi = ntfs_attr_iget(base_vi, AT_BITMAP, ni->name, ni->name_len);
  1586. if (IS_ERR(bvi)) {
  1587. ntfs_error(vi->i_sb, "Failed to get bitmap attribute.");
  1588. err = PTR_ERR(bvi);
  1589. goto unm_err_out;
  1590. }
  1591. bni = NTFS_I(bvi);
  1592. if (NInoCompressed(bni) || NInoEncrypted(bni) ||
  1593. NInoSparse(bni)) {
  1594. ntfs_error(vi->i_sb, "$BITMAP attribute is compressed and/or "
  1595. "encrypted and/or sparse.");
  1596. goto iput_unm_err_out;
  1597. }
  1598. /* Consistency check bitmap size vs. index allocation size. */
  1599. bvi_size = i_size_read(bvi);
  1600. if ((bvi_size << 3) < (vi->i_size >> ni->itype.index.block_size_bits)) {
  1601. ntfs_error(vi->i_sb, "Index bitmap too small (0x%llx) for "
  1602. "index allocation (0x%llx).", bvi_size << 3,
  1603. vi->i_size);
  1604. goto iput_unm_err_out;
  1605. }
  1606. iput(bvi);
  1607. skip_large_index_stuff:
  1608. /* Setup the operations for this index inode. */
  1609. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1610. vi->i_blocks = ni->allocated_size >> 9;
  1611. /*
  1612. * Make sure the base inode doesn't go away and attach it to the
  1613. * index inode.
  1614. */
  1615. igrab(base_vi);
  1616. ni->ext.base_ntfs_ino = base_ni;
  1617. ni->nr_extents = -1;
  1618. ntfs_debug("Done.");
  1619. return 0;
  1620. iput_unm_err_out:
  1621. iput(bvi);
  1622. unm_err_out:
  1623. if (!err)
  1624. err = -EIO;
  1625. if (ctx)
  1626. ntfs_attr_put_search_ctx(ctx);
  1627. if (m)
  1628. unmap_mft_record(base_ni);
  1629. err_out:
  1630. ntfs_error(vi->i_sb, "Failed with error code %i while reading index "
  1631. "inode (mft_no 0x%lx, name_len %i.", err, vi->i_ino,
  1632. ni->name_len);
  1633. make_bad_inode(vi);
  1634. if (err != -EOPNOTSUPP && err != -ENOMEM)
  1635. NVolSetErrors(vol);
  1636. return err;
  1637. }
  1638. /*
  1639. * The MFT inode has special locking, so teach the lock validator
  1640. * about this by splitting off the locking rules of the MFT from
  1641. * the locking rules of other inodes. The MFT inode can never be
  1642. * accessed from the VFS side (or even internally), only by the
  1643. * map_mft functions.
  1644. */
  1645. static struct lock_class_key mft_ni_runlist_lock_key, mft_ni_mrec_lock_key;
  1646. /**
  1647. * ntfs_read_inode_mount - special read_inode for mount time use only
  1648. * @vi: inode to read
  1649. *
  1650. * Read inode FILE_MFT at mount time, only called with super_block lock
  1651. * held from within the read_super() code path.
  1652. *
  1653. * This function exists because when it is called the page cache for $MFT/$DATA
  1654. * is not initialized and hence we cannot get at the contents of mft records
  1655. * by calling map_mft_record*().
  1656. *
  1657. * Further it needs to cope with the circular references problem, i.e. cannot
  1658. * load any attributes other than $ATTRIBUTE_LIST until $DATA is loaded, because
  1659. * we do not know where the other extent mft records are yet and again, because
  1660. * we cannot call map_mft_record*() yet. Obviously this applies only when an
  1661. * attribute list is actually present in $MFT inode.
  1662. *
  1663. * We solve these problems by starting with the $DATA attribute before anything
  1664. * else and iterating using ntfs_attr_lookup($DATA) over all extents. As each
  1665. * extent is found, we ntfs_mapping_pairs_decompress() including the implied
  1666. * ntfs_runlists_merge(). Each step of the iteration necessarily provides
  1667. * sufficient information for the next step to complete.
  1668. *
  1669. * This should work but there are two possible pit falls (see inline comments
  1670. * below), but only time will tell if they are real pits or just smoke...
  1671. */
  1672. int ntfs_read_inode_mount(struct inode *vi)
  1673. {
  1674. VCN next_vcn, last_vcn, highest_vcn;
  1675. s64 block;
  1676. struct super_block *sb = vi->i_sb;
  1677. ntfs_volume *vol = NTFS_SB(sb);
  1678. struct buffer_head *bh;
  1679. ntfs_inode *ni;
  1680. MFT_RECORD *m = NULL;
  1681. ATTR_RECORD *a;
  1682. ntfs_attr_search_ctx *ctx;
  1683. unsigned int i, nr_blocks;
  1684. int err;
  1685. ntfs_debug("Entering.");
  1686. /* Initialize the ntfs specific part of @vi. */
  1687. ntfs_init_big_inode(vi);
  1688. ni = NTFS_I(vi);
  1689. /* Setup the data attribute. It is special as it is mst protected. */
  1690. NInoSetNonResident(ni);
  1691. NInoSetMstProtected(ni);
  1692. NInoSetSparseDisabled(ni);
  1693. ni->type = AT_DATA;
  1694. ni->name = NULL;
  1695. ni->name_len = 0;
  1696. /*
  1697. * This sets up our little cheat allowing us to reuse the async read io
  1698. * completion handler for directories.
  1699. */
  1700. ni->itype.index.block_size = vol->mft_record_size;
  1701. ni->itype.index.block_size_bits = vol->mft_record_size_bits;
  1702. /* Very important! Needed to be able to call map_mft_record*(). */
  1703. vol->mft_ino = vi;
  1704. /* Allocate enough memory to read the first mft record. */
  1705. if (vol->mft_record_size > 64 * 1024) {
  1706. ntfs_error(sb, "Unsupported mft record size %i (max 64kiB).",
  1707. vol->mft_record_size);
  1708. goto err_out;
  1709. }
  1710. i = vol->mft_record_size;
  1711. if (i < sb->s_blocksize)
  1712. i = sb->s_blocksize;
  1713. m = (MFT_RECORD*)ntfs_malloc_nofs(i);
  1714. if (!m) {
  1715. ntfs_error(sb, "Failed to allocate buffer for $MFT record 0.");
  1716. goto err_out;
  1717. }
  1718. /* Determine the first block of the $MFT/$DATA attribute. */
  1719. block = vol->mft_lcn << vol->cluster_size_bits >>
  1720. sb->s_blocksize_bits;
  1721. nr_blocks = vol->mft_record_size >> sb->s_blocksize_bits;
  1722. if (!nr_blocks)
  1723. nr_blocks = 1;
  1724. /* Load $MFT/$DATA's first mft record. */
  1725. for (i = 0; i < nr_blocks; i++) {
  1726. bh = sb_bread(sb, block++);
  1727. if (!bh) {
  1728. ntfs_error(sb, "Device read failed.");
  1729. goto err_out;
  1730. }
  1731. memcpy((char*)m + (i << sb->s_blocksize_bits), bh->b_data,
  1732. sb->s_blocksize);
  1733. brelse(bh);
  1734. }
  1735. if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) {
  1736. ntfs_error(sb, "Incorrect mft record size %u in superblock, should be %u.",
  1737. le32_to_cpu(m->bytes_allocated), vol->mft_record_size);
  1738. goto err_out;
  1739. }
  1740. /* Apply the mst fixups. */
  1741. if (post_read_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size)) {
  1742. /* FIXME: Try to use the $MFTMirr now. */
  1743. ntfs_error(sb, "MST fixup failed. $MFT is corrupt.");
  1744. goto err_out;
  1745. }
  1746. /* Need this to sanity check attribute list references to $MFT. */
  1747. vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
  1748. /* Provides readpage() for map_mft_record(). */
  1749. vi->i_mapping->a_ops = &ntfs_mst_aops;
  1750. ctx = ntfs_attr_get_search_ctx(ni, m);
  1751. if (!ctx) {
  1752. err = -ENOMEM;
  1753. goto err_out;
  1754. }
  1755. /* Find the attribute list attribute if present. */
  1756. err = ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, 0, 0, NULL, 0, ctx);
  1757. if (err) {
  1758. if (unlikely(err != -ENOENT)) {
  1759. ntfs_error(sb, "Failed to lookup attribute list "
  1760. "attribute. You should run chkdsk.");
  1761. goto put_err_out;
  1762. }
  1763. } else /* if (!err) */ {
  1764. ATTR_LIST_ENTRY *al_entry, *next_al_entry;
  1765. u8 *al_end;
  1766. static const char *es = " Not allowed. $MFT is corrupt. "
  1767. "You should run chkdsk.";
  1768. ntfs_debug("Attribute list attribute found in $MFT.");
  1769. NInoSetAttrList(ni);
  1770. a = ctx->attr;
  1771. if (a->flags & ATTR_COMPRESSION_MASK) {
  1772. ntfs_error(sb, "Attribute list attribute is "
  1773. "compressed.%s", es);
  1774. goto put_err_out;
  1775. }
  1776. if (a->flags & ATTR_IS_ENCRYPTED ||
  1777. a->flags & ATTR_IS_SPARSE) {
  1778. if (a->non_resident) {
  1779. ntfs_error(sb, "Non-resident attribute list "
  1780. "attribute is encrypted/"
  1781. "sparse.%s", es);
  1782. goto put_err_out;
  1783. }
  1784. ntfs_warning(sb, "Resident attribute list attribute "
  1785. "in $MFT system file is marked "
  1786. "encrypted/sparse which is not true. "
  1787. "However, Windows allows this and "
  1788. "chkdsk does not detect or correct it "
  1789. "so we will just ignore the invalid "
  1790. "flags and pretend they are not set.");
  1791. }
  1792. /* Now allocate memory for the attribute list. */
  1793. ni->attr_list_size = (u32)ntfs_attr_size(a);
  1794. if (!ni->attr_list_size) {
  1795. ntfs_error(sb, "Attr_list_size is zero");
  1796. goto put_err_out;
  1797. }
  1798. ni->attr_list = ntfs_malloc_nofs(ni->attr_list_size);
  1799. if (!ni->attr_list) {
  1800. ntfs_error(sb, "Not enough memory to allocate buffer "
  1801. "for attribute list.");
  1802. goto put_err_out;
  1803. }
  1804. if (a->non_resident) {
  1805. NInoSetAttrListNonResident(ni);
  1806. if (a->data.non_resident.lowest_vcn) {
  1807. ntfs_error(sb, "Attribute list has non zero "
  1808. "lowest_vcn. $MFT is corrupt. "
  1809. "You should run chkdsk.");
  1810. goto put_err_out;
  1811. }
  1812. /* Setup the runlist. */
  1813. ni->attr_list_rl.rl = ntfs_mapping_pairs_decompress(vol,
  1814. a, NULL);
  1815. if (IS_ERR(ni->attr_list_rl.rl)) {
  1816. err = PTR_ERR(ni->attr_list_rl.rl);
  1817. ni->attr_list_rl.rl = NULL;
  1818. ntfs_error(sb, "Mapping pairs decompression "
  1819. "failed with error code %i.",
  1820. -err);
  1821. goto put_err_out;
  1822. }
  1823. /* Now load the attribute list. */
  1824. if ((err = load_attribute_list(vol, &ni->attr_list_rl,
  1825. ni->attr_list, ni->attr_list_size,
  1826. sle64_to_cpu(a->data.
  1827. non_resident.initialized_size)))) {
  1828. ntfs_error(sb, "Failed to load attribute list "
  1829. "attribute with error code %i.",
  1830. -err);
  1831. goto put_err_out;
  1832. }
  1833. } else /* if (!ctx.attr->non_resident) */ {
  1834. if ((u8*)a + le16_to_cpu(
  1835. a->data.resident.value_offset) +
  1836. le32_to_cpu(
  1837. a->data.resident.value_length) >
  1838. (u8*)ctx->mrec + vol->mft_record_size) {
  1839. ntfs_error(sb, "Corrupt attribute list "
  1840. "attribute.");
  1841. goto put_err_out;
  1842. }
  1843. /* Now copy the attribute list. */
  1844. memcpy(ni->attr_list, (u8*)a + le16_to_cpu(
  1845. a->data.resident.value_offset),
  1846. le32_to_cpu(
  1847. a->data.resident.value_length));
  1848. }
  1849. /* The attribute list is now setup in memory. */
  1850. /*
  1851. * FIXME: I don't know if this case is actually possible.
  1852. * According to logic it is not possible but I have seen too
  1853. * many weird things in MS software to rely on logic... Thus we
  1854. * perform a manual search and make sure the first $MFT/$DATA
  1855. * extent is in the base inode. If it is not we abort with an
  1856. * error and if we ever see a report of this error we will need
  1857. * to do some magic in order to have the necessary mft record
  1858. * loaded and in the right place in the page cache. But
  1859. * hopefully logic will prevail and this never happens...
  1860. */
  1861. al_entry = (ATTR_LIST_ENTRY*)ni->attr_list;
  1862. al_end = (u8*)al_entry + ni->attr_list_size;
  1863. for (;; al_entry = next_al_entry) {
  1864. /* Out of bounds check. */
  1865. if ((u8*)al_entry < ni->attr_list ||
  1866. (u8*)al_entry > al_end)
  1867. goto em_put_err_out;
  1868. /* Catch the end of the attribute list. */
  1869. if ((u8*)al_entry == al_end)
  1870. goto em_put_err_out;
  1871. if (!al_entry->length)
  1872. goto em_put_err_out;
  1873. if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
  1874. le16_to_cpu(al_entry->length) > al_end)
  1875. goto em_put_err_out;
  1876. next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
  1877. le16_to_cpu(al_entry->length));
  1878. if (le32_to_cpu(al_entry->type) > le32_to_cpu(AT_DATA))
  1879. goto em_put_err_out;
  1880. if (AT_DATA != al_entry->type)
  1881. continue;
  1882. /* We want an unnamed attribute. */
  1883. if (al_entry->name_length)
  1884. goto em_put_err_out;
  1885. /* Want the first entry, i.e. lowest_vcn == 0. */
  1886. if (al_entry->lowest_vcn)
  1887. goto em_put_err_out;
  1888. /* First entry has to be in the base mft record. */
  1889. if (MREF_LE(al_entry->mft_reference) != vi->i_ino) {
  1890. /* MFT references do not match, logic fails. */
  1891. ntfs_error(sb, "BUG: The first $DATA extent "
  1892. "of $MFT is not in the base "
  1893. "mft record. Please report "
  1894. "you saw this message to "
  1895. "linux-ntfs-dev@lists."
  1896. "sourceforge.net");
  1897. goto put_err_out;
  1898. } else {
  1899. /* Sequence numbers must match. */
  1900. if (MSEQNO_LE(al_entry->mft_reference) !=
  1901. ni->seq_no)
  1902. goto em_put_err_out;
  1903. /* Got it. All is ok. We can stop now. */
  1904. break;
  1905. }
  1906. }
  1907. }
  1908. ntfs_attr_reinit_search_ctx(ctx);
  1909. /* Now load all attribute extents. */
  1910. a = NULL;
  1911. next_vcn = last_vcn = highest_vcn = 0;
  1912. while (!(err = ntfs_attr_lookup(AT_DATA, NULL, 0, 0, next_vcn, NULL, 0,
  1913. ctx))) {
  1914. runlist_element *nrl;
  1915. /* Cache the current attribute. */
  1916. a = ctx->attr;
  1917. /* $MFT must be non-resident. */
  1918. if (!a->non_resident) {
  1919. ntfs_error(sb, "$MFT must be non-resident but a "
  1920. "resident extent was found. $MFT is "
  1921. "corrupt. Run chkdsk.");
  1922. goto put_err_out;
  1923. }
  1924. /* $MFT must be uncompressed and unencrypted. */
  1925. if (a->flags & ATTR_COMPRESSION_MASK ||
  1926. a->flags & ATTR_IS_ENCRYPTED ||
  1927. a->flags & ATTR_IS_SPARSE) {
  1928. ntfs_error(sb, "$MFT must be uncompressed, "
  1929. "non-sparse, and unencrypted but a "
  1930. "compressed/sparse/encrypted extent "
  1931. "was found. $MFT is corrupt. Run "
  1932. "chkdsk.");
  1933. goto put_err_out;
  1934. }
  1935. /*
  1936. * Decompress the mapping pairs array of this extent and merge
  1937. * the result into the existing runlist. No need for locking
  1938. * as we have exclusive access to the inode at this time and we
  1939. * are a mount in progress task, too.
  1940. */
  1941. nrl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
  1942. if (IS_ERR(nrl)) {
  1943. ntfs_error(sb, "ntfs_mapping_pairs_decompress() "
  1944. "failed with error code %ld. $MFT is "
  1945. "corrupt.", PTR_ERR(nrl));
  1946. goto put_err_out;
  1947. }
  1948. ni->runlist.rl = nrl;
  1949. /* Are we in the first extent? */
  1950. if (!next_vcn) {
  1951. if (a->data.non_resident.lowest_vcn) {
  1952. ntfs_error(sb, "First extent of $DATA "
  1953. "attribute has non zero "
  1954. "lowest_vcn. $MFT is corrupt. "
  1955. "You should run chkdsk.");
  1956. goto put_err_out;
  1957. }
  1958. /* Get the last vcn in the $DATA attribute. */
  1959. last_vcn = sle64_to_cpu(
  1960. a->data.non_resident.allocated_size)
  1961. >> vol->cluster_size_bits;
  1962. /* Fill in the inode size. */
  1963. vi->i_size = sle64_to_cpu(
  1964. a->data.non_resident.data_size);
  1965. ni->initialized_size = sle64_to_cpu(
  1966. a->data.non_resident.initialized_size);
  1967. ni->allocated_size = sle64_to_cpu(
  1968. a->data.non_resident.allocated_size);
  1969. /*
  1970. * Verify the number of mft records does not exceed
  1971. * 2^32 - 1.
  1972. */
  1973. if ((vi->i_size >> vol->mft_record_size_bits) >=
  1974. (1ULL << 32)) {
  1975. ntfs_error(sb, "$MFT is too big! Aborting.");
  1976. goto put_err_out;
  1977. }
  1978. /*
  1979. * We have got the first extent of the runlist for
  1980. * $MFT which means it is now relatively safe to call
  1981. * the normal ntfs_read_inode() function.
  1982. * Complete reading the inode, this will actually
  1983. * re-read the mft record for $MFT, this time entering
  1984. * it into the page cache with which we complete the
  1985. * kick start of the volume. It should be safe to do
  1986. * this now as the first extent of $MFT/$DATA is
  1987. * already known and we would hope that we don't need
  1988. * further extents in order to find the other
  1989. * attributes belonging to $MFT. Only time will tell if
  1990. * this is really the case. If not we will have to play
  1991. * magic at this point, possibly duplicating a lot of
  1992. * ntfs_read_inode() at this point. We will need to
  1993. * ensure we do enough of its work to be able to call
  1994. * ntfs_read_inode() on extents of $MFT/$DATA. But lets
  1995. * hope this never happens...
  1996. */
  1997. ntfs_read_locked_inode(vi);
  1998. if (is_bad_inode(vi)) {
  1999. ntfs_error(sb, "ntfs_read_inode() of $MFT "
  2000. "failed. BUG or corrupt $MFT. "
  2001. "Run chkdsk and if no errors "
  2002. "are found, please report you "
  2003. "saw this message to "
  2004. "linux-ntfs-dev@lists."
  2005. "sourceforge.net");
  2006. ntfs_attr_put_search_ctx(ctx);
  2007. /* Revert to the safe super operations. */
  2008. ntfs_free(m);
  2009. return -1;
  2010. }
  2011. /*
  2012. * Re-initialize some specifics about $MFT's inode as
  2013. * ntfs_read_inode() will have set up the default ones.
  2014. */
  2015. /* Set uid and gid to root. */
  2016. vi->i_uid = GLOBAL_ROOT_UID;
  2017. vi->i_gid = GLOBAL_ROOT_GID;
  2018. /* Regular file. No access for anyone. */
  2019. vi->i_mode = S_IFREG;
  2020. /* No VFS initiated operations allowed for $MFT. */
  2021. vi->i_op = &ntfs_empty_inode_ops;
  2022. vi->i_fop = &ntfs_empty_file_ops;
  2023. }
  2024. /* Get the lowest vcn for the next extent. */
  2025. highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  2026. next_vcn = highest_vcn + 1;
  2027. /* Only one extent or error, which we catch below. */
  2028. if (next_vcn <= 0)
  2029. break;
  2030. /* Avoid endless loops due to corruption. */
  2031. if (next_vcn < sle64_to_cpu(
  2032. a->data.non_resident.lowest_vcn)) {
  2033. ntfs_error(sb, "$MFT has corrupt attribute list "
  2034. "attribute. Run chkdsk.");
  2035. goto put_err_out;
  2036. }
  2037. }
  2038. if (err != -ENOENT) {
  2039. ntfs_error(sb, "Failed to lookup $MFT/$DATA attribute extent. "
  2040. "$MFT is corrupt. Run chkdsk.");
  2041. goto put_err_out;
  2042. }
  2043. if (!a) {
  2044. ntfs_error(sb, "$MFT/$DATA attribute not found. $MFT is "
  2045. "corrupt. Run chkdsk.");
  2046. goto put_err_out;
  2047. }
  2048. if (highest_vcn && highest_vcn != last_vcn - 1) {
  2049. ntfs_error(sb, "Failed to load the complete runlist for "
  2050. "$MFT/$DATA. Driver bug or corrupt $MFT. "
  2051. "Run chkdsk.");
  2052. ntfs_debug("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx",
  2053. (unsigned long long)highest_vcn,
  2054. (unsigned long long)last_vcn - 1);
  2055. goto put_err_out;
  2056. }
  2057. ntfs_attr_put_search_ctx(ctx);
  2058. ntfs_debug("Done.");
  2059. ntfs_free(m);
  2060. /*
  2061. * Split the locking rules of the MFT inode from the
  2062. * locking rules of other inodes:
  2063. */
  2064. lockdep_set_class(&ni->runlist.lock, &mft_ni_runlist_lock_key);
  2065. lockdep_set_class(&ni->mrec_lock, &mft_ni_mrec_lock_key);
  2066. return 0;
  2067. em_put_err_out:
  2068. ntfs_error(sb, "Couldn't find first extent of $DATA attribute in "
  2069. "attribute list. $MFT is corrupt. Run chkdsk.");
  2070. put_err_out:
  2071. ntfs_attr_put_search_ctx(ctx);
  2072. err_out:
  2073. ntfs_error(sb, "Failed. Marking inode as bad.");
  2074. make_bad_inode(vi);
  2075. ntfs_free(m);
  2076. return -1;
  2077. }
  2078. static void __ntfs_clear_inode(ntfs_inode *ni)
  2079. {
  2080. /* Free all alocated memory. */
  2081. down_write(&ni->runlist.lock);
  2082. if (ni->runlist.rl) {
  2083. ntfs_free(ni->runlist.rl);
  2084. ni->runlist.rl = NULL;
  2085. }
  2086. up_write(&ni->runlist.lock);
  2087. if (ni->attr_list) {
  2088. ntfs_free(ni->attr_list);
  2089. ni->attr_list = NULL;
  2090. }
  2091. down_write(&ni->attr_list_rl.lock);
  2092. if (ni->attr_list_rl.rl) {
  2093. ntfs_free(ni->attr_list_rl.rl);
  2094. ni->attr_list_rl.rl = NULL;
  2095. }
  2096. up_write(&ni->attr_list_rl.lock);
  2097. if (ni->name_len && ni->name != I30) {
  2098. /* Catch bugs... */
  2099. BUG_ON(!ni->name);
  2100. kfree(ni->name);
  2101. }
  2102. }
  2103. void ntfs_clear_extent_inode(ntfs_inode *ni)
  2104. {
  2105. ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
  2106. BUG_ON(NInoAttr(ni));
  2107. BUG_ON(ni->nr_extents != -1);
  2108. #ifdef NTFS_RW
  2109. if (NInoDirty(ni)) {
  2110. if (!is_bad_inode(VFS_I(ni->ext.base_ntfs_ino)))
  2111. ntfs_error(ni->vol->sb, "Clearing dirty extent inode! "
  2112. "Losing data! This is a BUG!!!");
  2113. // FIXME: Do something!!!
  2114. }
  2115. #endif /* NTFS_RW */
  2116. __ntfs_clear_inode(ni);
  2117. /* Bye, bye... */
  2118. ntfs_destroy_extent_inode(ni);
  2119. }
  2120. /**
  2121. * ntfs_evict_big_inode - clean up the ntfs specific part of an inode
  2122. * @vi: vfs inode pending annihilation
  2123. *
  2124. * When the VFS is going to remove an inode from memory, ntfs_clear_big_inode()
  2125. * is called, which deallocates all memory belonging to the NTFS specific part
  2126. * of the inode and returns.
  2127. *
  2128. * If the MFT record is dirty, we commit it before doing anything else.
  2129. */
  2130. void ntfs_evict_big_inode(struct inode *vi)
  2131. {
  2132. ntfs_inode *ni = NTFS_I(vi);
  2133. truncate_inode_pages_final(&vi->i_data);
  2134. clear_inode(vi);
  2135. #ifdef NTFS_RW
  2136. if (NInoDirty(ni)) {
  2137. bool was_bad = (is_bad_inode(vi));
  2138. /* Committing the inode also commits all extent inodes. */
  2139. ntfs_commit_inode(vi);
  2140. if (!was_bad && (is_bad_inode(vi) || NInoDirty(ni))) {
  2141. ntfs_error(vi->i_sb, "Failed to commit dirty inode "
  2142. "0x%lx. Losing data!", vi->i_ino);
  2143. // FIXME: Do something!!!
  2144. }
  2145. }
  2146. #endif /* NTFS_RW */
  2147. /* No need to lock at this stage as no one else has a reference. */
  2148. if (ni->nr_extents > 0) {
  2149. int i;
  2150. for (i = 0; i < ni->nr_extents; i++)
  2151. ntfs_clear_extent_inode(ni->ext.extent_ntfs_inos[i]);
  2152. kfree(ni->ext.extent_ntfs_inos);
  2153. }
  2154. __ntfs_clear_inode(ni);
  2155. if (NInoAttr(ni)) {
  2156. /* Release the base inode if we are holding it. */
  2157. if (ni->nr_extents == -1) {
  2158. iput(VFS_I(ni->ext.base_ntfs_ino));
  2159. ni->nr_extents = 0;
  2160. ni->ext.base_ntfs_ino = NULL;
  2161. }
  2162. }
  2163. BUG_ON(ni->page);
  2164. if (!atomic_dec_and_test(&ni->count))
  2165. BUG();
  2166. return;
  2167. }
  2168. /**
  2169. * ntfs_show_options - show mount options in /proc/mounts
  2170. * @sf: seq_file in which to write our mount options
  2171. * @root: root of the mounted tree whose mount options to display
  2172. *
  2173. * Called by the VFS once for each mounted ntfs volume when someone reads
  2174. * /proc/mounts in order to display the NTFS specific mount options of each
  2175. * mount. The mount options of fs specified by @root are written to the seq file
  2176. * @sf and success is returned.
  2177. */
  2178. int ntfs_show_options(struct seq_file *sf, struct dentry *root)
  2179. {
  2180. ntfs_volume *vol = NTFS_SB(root->d_sb);
  2181. int i;
  2182. seq_printf(sf, ",uid=%i", from_kuid_munged(&init_user_ns, vol->uid));
  2183. seq_printf(sf, ",gid=%i", from_kgid_munged(&init_user_ns, vol->gid));
  2184. if (vol->fmask == vol->dmask)
  2185. seq_printf(sf, ",umask=0%o", vol->fmask);
  2186. else {
  2187. seq_printf(sf, ",fmask=0%o", vol->fmask);
  2188. seq_printf(sf, ",dmask=0%o", vol->dmask);
  2189. }
  2190. seq_printf(sf, ",nls=%s", vol->nls_map->charset);
  2191. if (NVolCaseSensitive(vol))
  2192. seq_printf(sf, ",case_sensitive");
  2193. if (NVolShowSystemFiles(vol))
  2194. seq_printf(sf, ",show_sys_files");
  2195. if (!NVolSparseEnabled(vol))
  2196. seq_printf(sf, ",disable_sparse");
  2197. for (i = 0; on_errors_arr[i].val; i++) {
  2198. if (on_errors_arr[i].val & vol->on_errors)
  2199. seq_printf(sf, ",errors=%s", on_errors_arr[i].str);
  2200. }
  2201. seq_printf(sf, ",mft_zone_multiplier=%i", vol->mft_zone_multiplier);
  2202. return 0;
  2203. }
  2204. #ifdef NTFS_RW
  2205. static const char *es = " Leaving inconsistent metadata. Unmount and run "
  2206. "chkdsk.";
  2207. /**
  2208. * ntfs_truncate - called when the i_size of an ntfs inode is changed
  2209. * @vi: inode for which the i_size was changed
  2210. *
  2211. * We only support i_size changes for normal files at present, i.e. not
  2212. * compressed and not encrypted. This is enforced in ntfs_setattr(), see
  2213. * below.
  2214. *
  2215. * The kernel guarantees that @vi is a regular file (S_ISREG() is true) and
  2216. * that the change is allowed.
  2217. *
  2218. * This implies for us that @vi is a file inode rather than a directory, index,
  2219. * or attribute inode as well as that @vi is a base inode.
  2220. *
  2221. * Returns 0 on success or -errno on error.
  2222. *
  2223. * Called with ->i_mutex held.
  2224. */
  2225. int ntfs_truncate(struct inode *vi)
  2226. {
  2227. s64 new_size, old_size, nr_freed, new_alloc_size, old_alloc_size;
  2228. VCN highest_vcn;
  2229. unsigned long flags;
  2230. ntfs_inode *base_ni, *ni = NTFS_I(vi);
  2231. ntfs_volume *vol = ni->vol;
  2232. ntfs_attr_search_ctx *ctx;
  2233. MFT_RECORD *m;
  2234. ATTR_RECORD *a;
  2235. const char *te = " Leaving file length out of sync with i_size.";
  2236. int err, mp_size, size_change, alloc_change;
  2237. u32 attr_len;
  2238. ntfs_debug("Entering for inode 0x%lx.", vi->i_ino);
  2239. BUG_ON(NInoAttr(ni));
  2240. BUG_ON(S_ISDIR(vi->i_mode));
  2241. BUG_ON(NInoMstProtected(ni));
  2242. BUG_ON(ni->nr_extents < 0);
  2243. retry_truncate:
  2244. /*
  2245. * Lock the runlist for writing and map the mft record to ensure it is
  2246. * safe to mess with the attribute runlist and sizes.
  2247. */
  2248. down_write(&ni->runlist.lock);
  2249. if (!NInoAttr(ni))
  2250. base_ni = ni;
  2251. else
  2252. base_ni = ni->ext.base_ntfs_ino;
  2253. m = map_mft_record(base_ni);
  2254. if (IS_ERR(m)) {
  2255. err = PTR_ERR(m);
  2256. ntfs_error(vi->i_sb, "Failed to map mft record for inode 0x%lx "
  2257. "(error code %d).%s", vi->i_ino, err, te);
  2258. ctx = NULL;
  2259. m = NULL;
  2260. goto old_bad_out;
  2261. }
  2262. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  2263. if (unlikely(!ctx)) {
  2264. ntfs_error(vi->i_sb, "Failed to allocate a search context for "
  2265. "inode 0x%lx (not enough memory).%s",
  2266. vi->i_ino, te);
  2267. err = -ENOMEM;
  2268. goto old_bad_out;
  2269. }
  2270. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  2271. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2272. if (unlikely(err)) {
  2273. if (err == -ENOENT) {
  2274. ntfs_error(vi->i_sb, "Open attribute is missing from "
  2275. "mft record. Inode 0x%lx is corrupt. "
  2276. "Run chkdsk.%s", vi->i_ino, te);
  2277. err = -EIO;
  2278. } else
  2279. ntfs_error(vi->i_sb, "Failed to lookup attribute in "
  2280. "inode 0x%lx (error code %d).%s",
  2281. vi->i_ino, err, te);
  2282. goto old_bad_out;
  2283. }
  2284. m = ctx->mrec;
  2285. a = ctx->attr;
  2286. /*
  2287. * The i_size of the vfs inode is the new size for the attribute value.
  2288. */
  2289. new_size = i_size_read(vi);
  2290. /* The current size of the attribute value is the old size. */
  2291. old_size = ntfs_attr_size(a);
  2292. /* Calculate the new allocated size. */
  2293. if (NInoNonResident(ni))
  2294. new_alloc_size = (new_size + vol->cluster_size - 1) &
  2295. ~(s64)vol->cluster_size_mask;
  2296. else
  2297. new_alloc_size = (new_size + 7) & ~7;
  2298. /* The current allocated size is the old allocated size. */
  2299. read_lock_irqsave(&ni->size_lock, flags);
  2300. old_alloc_size = ni->allocated_size;
  2301. read_unlock_irqrestore(&ni->size_lock, flags);
  2302. /*
  2303. * The change in the file size. This will be 0 if no change, >0 if the
  2304. * size is growing, and <0 if the size is shrinking.
  2305. */
  2306. size_change = -1;
  2307. if (new_size - old_size >= 0) {
  2308. size_change = 1;
  2309. if (new_size == old_size)
  2310. size_change = 0;
  2311. }
  2312. /* As above for the allocated size. */
  2313. alloc_change = -1;
  2314. if (new_alloc_size - old_alloc_size >= 0) {
  2315. alloc_change = 1;
  2316. if (new_alloc_size == old_alloc_size)
  2317. alloc_change = 0;
  2318. }
  2319. /*
  2320. * If neither the size nor the allocation are being changed there is
  2321. * nothing to do.
  2322. */
  2323. if (!size_change && !alloc_change)
  2324. goto unm_done;
  2325. /* If the size is changing, check if new size is allowed in $AttrDef. */
  2326. if (size_change) {
  2327. err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
  2328. if (unlikely(err)) {
  2329. if (err == -ERANGE) {
  2330. ntfs_error(vol->sb, "Truncate would cause the "
  2331. "inode 0x%lx to %simum size "
  2332. "for its attribute type "
  2333. "(0x%x). Aborting truncate.",
  2334. vi->i_ino,
  2335. new_size > old_size ? "exceed "
  2336. "the max" : "go under the min",
  2337. le32_to_cpu(ni->type));
  2338. err = -EFBIG;
  2339. } else {
  2340. ntfs_error(vol->sb, "Inode 0x%lx has unknown "
  2341. "attribute type 0x%x. "
  2342. "Aborting truncate.",
  2343. vi->i_ino,
  2344. le32_to_cpu(ni->type));
  2345. err = -EIO;
  2346. }
  2347. /* Reset the vfs inode size to the old size. */
  2348. i_size_write(vi, old_size);
  2349. goto err_out;
  2350. }
  2351. }
  2352. if (NInoCompressed(ni) || NInoEncrypted(ni)) {
  2353. ntfs_warning(vi->i_sb, "Changes in inode size are not "
  2354. "supported yet for %s files, ignoring.",
  2355. NInoCompressed(ni) ? "compressed" :
  2356. "encrypted");
  2357. err = -EOPNOTSUPP;
  2358. goto bad_out;
  2359. }
  2360. if (a->non_resident)
  2361. goto do_non_resident_truncate;
  2362. BUG_ON(NInoNonResident(ni));
  2363. /* Resize the attribute record to best fit the new attribute size. */
  2364. if (new_size < vol->mft_record_size &&
  2365. !ntfs_resident_attr_value_resize(m, a, new_size)) {
  2366. /* The resize succeeded! */
  2367. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2368. mark_mft_record_dirty(ctx->ntfs_ino);
  2369. write_lock_irqsave(&ni->size_lock, flags);
  2370. /* Update the sizes in the ntfs inode and all is done. */
  2371. ni->allocated_size = le32_to_cpu(a->length) -
  2372. le16_to_cpu(a->data.resident.value_offset);
  2373. /*
  2374. * Note ntfs_resident_attr_value_resize() has already done any
  2375. * necessary data clearing in the attribute record. When the
  2376. * file is being shrunk vmtruncate() will already have cleared
  2377. * the top part of the last partial page, i.e. since this is
  2378. * the resident case this is the page with index 0. However,
  2379. * when the file is being expanded, the page cache page data
  2380. * between the old data_size, i.e. old_size, and the new_size
  2381. * has not been zeroed. Fortunately, we do not need to zero it
  2382. * either since on one hand it will either already be zero due
  2383. * to both readpage and writepage clearing partial page data
  2384. * beyond i_size in which case there is nothing to do or in the
  2385. * case of the file being mmap()ped at the same time, POSIX
  2386. * specifies that the behaviour is unspecified thus we do not
  2387. * have to do anything. This means that in our implementation
  2388. * in the rare case that the file is mmap()ped and a write
  2389. * occurred into the mmap()ped region just beyond the file size
  2390. * and writepage has not yet been called to write out the page
  2391. * (which would clear the area beyond the file size) and we now
  2392. * extend the file size to incorporate this dirty region
  2393. * outside the file size, a write of the page would result in
  2394. * this data being written to disk instead of being cleared.
  2395. * Given both POSIX and the Linux mmap(2) man page specify that
  2396. * this corner case is undefined, we choose to leave it like
  2397. * that as this is much simpler for us as we cannot lock the
  2398. * relevant page now since we are holding too many ntfs locks
  2399. * which would result in a lock reversal deadlock.
  2400. */
  2401. ni->initialized_size = new_size;
  2402. write_unlock_irqrestore(&ni->size_lock, flags);
  2403. goto unm_done;
  2404. }
  2405. /* If the above resize failed, this must be an attribute extension. */
  2406. BUG_ON(size_change < 0);
  2407. /*
  2408. * We have to drop all the locks so we can call
  2409. * ntfs_attr_make_non_resident(). This could be optimised by try-
  2410. * locking the first page cache page and only if that fails dropping
  2411. * the locks, locking the page, and redoing all the locking and
  2412. * lookups. While this would be a huge optimisation, it is not worth
  2413. * it as this is definitely a slow code path as it only ever can happen
  2414. * once for any given file.
  2415. */
  2416. ntfs_attr_put_search_ctx(ctx);
  2417. unmap_mft_record(base_ni);
  2418. up_write(&ni->runlist.lock);
  2419. /*
  2420. * Not enough space in the mft record, try to make the attribute
  2421. * non-resident and if successful restart the truncation process.
  2422. */
  2423. err = ntfs_attr_make_non_resident(ni, old_size);
  2424. if (likely(!err))
  2425. goto retry_truncate;
  2426. /*
  2427. * Could not make non-resident. If this is due to this not being
  2428. * permitted for this attribute type or there not being enough space,
  2429. * try to make other attributes non-resident. Otherwise fail.
  2430. */
  2431. if (unlikely(err != -EPERM && err != -ENOSPC)) {
  2432. ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, attribute "
  2433. "type 0x%x, because the conversion from "
  2434. "resident to non-resident attribute failed "
  2435. "with error code %i.", vi->i_ino,
  2436. (unsigned)le32_to_cpu(ni->type), err);
  2437. if (err != -ENOMEM)
  2438. err = -EIO;
  2439. goto conv_err_out;
  2440. }
  2441. /* TODO: Not implemented from here, abort. */
  2442. if (err == -ENOSPC)
  2443. ntfs_error(vol->sb, "Not enough space in the mft record/on "
  2444. "disk for the non-resident attribute value. "
  2445. "This case is not implemented yet.");
  2446. else /* if (err == -EPERM) */
  2447. ntfs_error(vol->sb, "This attribute type may not be "
  2448. "non-resident. This case is not implemented "
  2449. "yet.");
  2450. err = -EOPNOTSUPP;
  2451. goto conv_err_out;
  2452. #if 0
  2453. // TODO: Attempt to make other attributes non-resident.
  2454. if (!err)
  2455. goto do_resident_extend;
  2456. /*
  2457. * Both the attribute list attribute and the standard information
  2458. * attribute must remain in the base inode. Thus, if this is one of
  2459. * these attributes, we have to try to move other attributes out into
  2460. * extent mft records instead.
  2461. */
  2462. if (ni->type == AT_ATTRIBUTE_LIST ||
  2463. ni->type == AT_STANDARD_INFORMATION) {
  2464. // TODO: Attempt to move other attributes into extent mft
  2465. // records.
  2466. err = -EOPNOTSUPP;
  2467. if (!err)
  2468. goto do_resident_extend;
  2469. goto err_out;
  2470. }
  2471. // TODO: Attempt to move this attribute to an extent mft record, but
  2472. // only if it is not already the only attribute in an mft record in
  2473. // which case there would be nothing to gain.
  2474. err = -EOPNOTSUPP;
  2475. if (!err)
  2476. goto do_resident_extend;
  2477. /* There is nothing we can do to make enough space. )-: */
  2478. goto err_out;
  2479. #endif
  2480. do_non_resident_truncate:
  2481. BUG_ON(!NInoNonResident(ni));
  2482. if (alloc_change < 0) {
  2483. highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  2484. if (highest_vcn > 0 &&
  2485. old_alloc_size >> vol->cluster_size_bits >
  2486. highest_vcn + 1) {
  2487. /*
  2488. * This attribute has multiple extents. Not yet
  2489. * supported.
  2490. */
  2491. ntfs_error(vol->sb, "Cannot truncate inode 0x%lx, "
  2492. "attribute type 0x%x, because the "
  2493. "attribute is highly fragmented (it "
  2494. "consists of multiple extents) and "
  2495. "this case is not implemented yet.",
  2496. vi->i_ino,
  2497. (unsigned)le32_to_cpu(ni->type));
  2498. err = -EOPNOTSUPP;
  2499. goto bad_out;
  2500. }
  2501. }
  2502. /*
  2503. * If the size is shrinking, need to reduce the initialized_size and
  2504. * the data_size before reducing the allocation.
  2505. */
  2506. if (size_change < 0) {
  2507. /*
  2508. * Make the valid size smaller (i_size is already up-to-date).
  2509. */
  2510. write_lock_irqsave(&ni->size_lock, flags);
  2511. if (new_size < ni->initialized_size) {
  2512. ni->initialized_size = new_size;
  2513. a->data.non_resident.initialized_size =
  2514. cpu_to_sle64(new_size);
  2515. }
  2516. a->data.non_resident.data_size = cpu_to_sle64(new_size);
  2517. write_unlock_irqrestore(&ni->size_lock, flags);
  2518. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2519. mark_mft_record_dirty(ctx->ntfs_ino);
  2520. /* If the allocated size is not changing, we are done. */
  2521. if (!alloc_change)
  2522. goto unm_done;
  2523. /*
  2524. * If the size is shrinking it makes no sense for the
  2525. * allocation to be growing.
  2526. */
  2527. BUG_ON(alloc_change > 0);
  2528. } else /* if (size_change >= 0) */ {
  2529. /*
  2530. * The file size is growing or staying the same but the
  2531. * allocation can be shrinking, growing or staying the same.
  2532. */
  2533. if (alloc_change > 0) {
  2534. /*
  2535. * We need to extend the allocation and possibly update
  2536. * the data size. If we are updating the data size,
  2537. * since we are not touching the initialized_size we do
  2538. * not need to worry about the actual data on disk.
  2539. * And as far as the page cache is concerned, there
  2540. * will be no pages beyond the old data size and any
  2541. * partial region in the last page between the old and
  2542. * new data size (or the end of the page if the new
  2543. * data size is outside the page) does not need to be
  2544. * modified as explained above for the resident
  2545. * attribute truncate case. To do this, we simply drop
  2546. * the locks we hold and leave all the work to our
  2547. * friendly helper ntfs_attr_extend_allocation().
  2548. */
  2549. ntfs_attr_put_search_ctx(ctx);
  2550. unmap_mft_record(base_ni);
  2551. up_write(&ni->runlist.lock);
  2552. err = ntfs_attr_extend_allocation(ni, new_size,
  2553. size_change > 0 ? new_size : -1, -1);
  2554. /*
  2555. * ntfs_attr_extend_allocation() will have done error
  2556. * output already.
  2557. */
  2558. goto done;
  2559. }
  2560. if (!alloc_change)
  2561. goto alloc_done;
  2562. }
  2563. /* alloc_change < 0 */
  2564. /* Free the clusters. */
  2565. nr_freed = ntfs_cluster_free(ni, new_alloc_size >>
  2566. vol->cluster_size_bits, -1, ctx);
  2567. m = ctx->mrec;
  2568. a = ctx->attr;
  2569. if (unlikely(nr_freed < 0)) {
  2570. ntfs_error(vol->sb, "Failed to release cluster(s) (error code "
  2571. "%lli). Unmount and run chkdsk to recover "
  2572. "the lost cluster(s).", (long long)nr_freed);
  2573. NVolSetErrors(vol);
  2574. nr_freed = 0;
  2575. }
  2576. /* Truncate the runlist. */
  2577. err = ntfs_rl_truncate_nolock(vol, &ni->runlist,
  2578. new_alloc_size >> vol->cluster_size_bits);
  2579. /*
  2580. * If the runlist truncation failed and/or the search context is no
  2581. * longer valid, we cannot resize the attribute record or build the
  2582. * mapping pairs array thus we mark the inode bad so that no access to
  2583. * the freed clusters can happen.
  2584. */
  2585. if (unlikely(err || IS_ERR(m))) {
  2586. ntfs_error(vol->sb, "Failed to %s (error code %li).%s",
  2587. IS_ERR(m) ?
  2588. "restore attribute search context" :
  2589. "truncate attribute runlist",
  2590. IS_ERR(m) ? PTR_ERR(m) : err, es);
  2591. err = -EIO;
  2592. goto bad_out;
  2593. }
  2594. /* Get the size for the shrunk mapping pairs array for the runlist. */
  2595. mp_size = ntfs_get_size_for_mapping_pairs(vol, ni->runlist.rl, 0, -1);
  2596. if (unlikely(mp_size <= 0)) {
  2597. ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, "
  2598. "attribute type 0x%x, because determining the "
  2599. "size for the mapping pairs failed with error "
  2600. "code %i.%s", vi->i_ino,
  2601. (unsigned)le32_to_cpu(ni->type), mp_size, es);
  2602. err = -EIO;
  2603. goto bad_out;
  2604. }
  2605. /*
  2606. * Shrink the attribute record for the new mapping pairs array. Note,
  2607. * this cannot fail since we are making the attribute smaller thus by
  2608. * definition there is enough space to do so.
  2609. */
  2610. attr_len = le32_to_cpu(a->length);
  2611. err = ntfs_attr_record_resize(m, a, mp_size +
  2612. le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
  2613. BUG_ON(err);
  2614. /*
  2615. * Generate the mapping pairs array directly into the attribute record.
  2616. */
  2617. err = ntfs_mapping_pairs_build(vol, (u8*)a +
  2618. le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
  2619. mp_size, ni->runlist.rl, 0, -1, NULL);
  2620. if (unlikely(err)) {
  2621. ntfs_error(vol->sb, "Cannot shrink allocation of inode 0x%lx, "
  2622. "attribute type 0x%x, because building the "
  2623. "mapping pairs failed with error code %i.%s",
  2624. vi->i_ino, (unsigned)le32_to_cpu(ni->type),
  2625. err, es);
  2626. err = -EIO;
  2627. goto bad_out;
  2628. }
  2629. /* Update the allocated/compressed size as well as the highest vcn. */
  2630. a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
  2631. vol->cluster_size_bits) - 1);
  2632. write_lock_irqsave(&ni->size_lock, flags);
  2633. ni->allocated_size = new_alloc_size;
  2634. a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
  2635. if (NInoSparse(ni) || NInoCompressed(ni)) {
  2636. if (nr_freed) {
  2637. ni->itype.compressed.size -= nr_freed <<
  2638. vol->cluster_size_bits;
  2639. BUG_ON(ni->itype.compressed.size < 0);
  2640. a->data.non_resident.compressed_size = cpu_to_sle64(
  2641. ni->itype.compressed.size);
  2642. vi->i_blocks = ni->itype.compressed.size >> 9;
  2643. }
  2644. } else
  2645. vi->i_blocks = new_alloc_size >> 9;
  2646. write_unlock_irqrestore(&ni->size_lock, flags);
  2647. /*
  2648. * We have shrunk the allocation. If this is a shrinking truncate we
  2649. * have already dealt with the initialized_size and the data_size above
  2650. * and we are done. If the truncate is only changing the allocation
  2651. * and not the data_size, we are also done. If this is an extending
  2652. * truncate, need to extend the data_size now which is ensured by the
  2653. * fact that @size_change is positive.
  2654. */
  2655. alloc_done:
  2656. /*
  2657. * If the size is growing, need to update it now. If it is shrinking,
  2658. * we have already updated it above (before the allocation change).
  2659. */
  2660. if (size_change > 0)
  2661. a->data.non_resident.data_size = cpu_to_sle64(new_size);
  2662. /* Ensure the modified mft record is written out. */
  2663. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2664. mark_mft_record_dirty(ctx->ntfs_ino);
  2665. unm_done:
  2666. ntfs_attr_put_search_ctx(ctx);
  2667. unmap_mft_record(base_ni);
  2668. up_write(&ni->runlist.lock);
  2669. done:
  2670. /* Update the mtime and ctime on the base inode. */
  2671. /* normally ->truncate shouldn't update ctime or mtime,
  2672. * but ntfs did before so it got a copy & paste version
  2673. * of file_update_time. one day someone should fix this
  2674. * for real.
  2675. */
  2676. if (!IS_NOCMTIME(VFS_I(base_ni)) && !IS_RDONLY(VFS_I(base_ni))) {
  2677. struct timespec64 now = current_time(VFS_I(base_ni));
  2678. int sync_it = 0;
  2679. if (!timespec64_equal(&VFS_I(base_ni)->i_mtime, &now) ||
  2680. !timespec64_equal(&VFS_I(base_ni)->i_ctime, &now))
  2681. sync_it = 1;
  2682. VFS_I(base_ni)->i_mtime = now;
  2683. VFS_I(base_ni)->i_ctime = now;
  2684. if (sync_it)
  2685. mark_inode_dirty_sync(VFS_I(base_ni));
  2686. }
  2687. if (likely(!err)) {
  2688. NInoClearTruncateFailed(ni);
  2689. ntfs_debug("Done.");
  2690. }
  2691. return err;
  2692. old_bad_out:
  2693. old_size = -1;
  2694. bad_out:
  2695. if (err != -ENOMEM && err != -EOPNOTSUPP)
  2696. NVolSetErrors(vol);
  2697. if (err != -EOPNOTSUPP)
  2698. NInoSetTruncateFailed(ni);
  2699. else if (old_size >= 0)
  2700. i_size_write(vi, old_size);
  2701. err_out:
  2702. if (ctx)
  2703. ntfs_attr_put_search_ctx(ctx);
  2704. if (m)
  2705. unmap_mft_record(base_ni);
  2706. up_write(&ni->runlist.lock);
  2707. out:
  2708. ntfs_debug("Failed. Returning error code %i.", err);
  2709. return err;
  2710. conv_err_out:
  2711. if (err != -ENOMEM && err != -EOPNOTSUPP)
  2712. NVolSetErrors(vol);
  2713. if (err != -EOPNOTSUPP)
  2714. NInoSetTruncateFailed(ni);
  2715. else
  2716. i_size_write(vi, old_size);
  2717. goto out;
  2718. }
  2719. /**
  2720. * ntfs_truncate_vfs - wrapper for ntfs_truncate() that has no return value
  2721. * @vi: inode for which the i_size was changed
  2722. *
  2723. * Wrapper for ntfs_truncate() that has no return value.
  2724. *
  2725. * See ntfs_truncate() description above for details.
  2726. */
  2727. #ifdef NTFS_RW
  2728. void ntfs_truncate_vfs(struct inode *vi) {
  2729. ntfs_truncate(vi);
  2730. }
  2731. #endif
  2732. /**
  2733. * ntfs_setattr - called from notify_change() when an attribute is being changed
  2734. * @dentry: dentry whose attributes to change
  2735. * @attr: structure describing the attributes and the changes
  2736. *
  2737. * We have to trap VFS attempts to truncate the file described by @dentry as
  2738. * soon as possible, because we do not implement changes in i_size yet. So we
  2739. * abort all i_size changes here.
  2740. *
  2741. * We also abort all changes of user, group, and mode as we do not implement
  2742. * the NTFS ACLs yet.
  2743. *
  2744. * Called with ->i_mutex held.
  2745. */
  2746. int ntfs_setattr(struct dentry *dentry, struct iattr *attr)
  2747. {
  2748. struct inode *vi = d_inode(dentry);
  2749. int err;
  2750. unsigned int ia_valid = attr->ia_valid;
  2751. err = setattr_prepare(dentry, attr);
  2752. if (err)
  2753. goto out;
  2754. /* We do not support NTFS ACLs yet. */
  2755. if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
  2756. ntfs_warning(vi->i_sb, "Changes in user/group/mode are not "
  2757. "supported yet, ignoring.");
  2758. err = -EOPNOTSUPP;
  2759. goto out;
  2760. }
  2761. if (ia_valid & ATTR_SIZE) {
  2762. if (attr->ia_size != i_size_read(vi)) {
  2763. ntfs_inode *ni = NTFS_I(vi);
  2764. /*
  2765. * FIXME: For now we do not support resizing of
  2766. * compressed or encrypted files yet.
  2767. */
  2768. if (NInoCompressed(ni) || NInoEncrypted(ni)) {
  2769. ntfs_warning(vi->i_sb, "Changes in inode size "
  2770. "are not supported yet for "
  2771. "%s files, ignoring.",
  2772. NInoCompressed(ni) ?
  2773. "compressed" : "encrypted");
  2774. err = -EOPNOTSUPP;
  2775. } else {
  2776. truncate_setsize(vi, attr->ia_size);
  2777. ntfs_truncate_vfs(vi);
  2778. }
  2779. if (err || ia_valid == ATTR_SIZE)
  2780. goto out;
  2781. } else {
  2782. /*
  2783. * We skipped the truncate but must still update
  2784. * timestamps.
  2785. */
  2786. ia_valid |= ATTR_MTIME | ATTR_CTIME;
  2787. }
  2788. }
  2789. if (ia_valid & ATTR_ATIME)
  2790. vi->i_atime = attr->ia_atime;
  2791. if (ia_valid & ATTR_MTIME)
  2792. vi->i_mtime = attr->ia_mtime;
  2793. if (ia_valid & ATTR_CTIME)
  2794. vi->i_ctime = attr->ia_ctime;
  2795. mark_inode_dirty(vi);
  2796. out:
  2797. return err;
  2798. }
  2799. /**
  2800. * ntfs_write_inode - write out a dirty inode
  2801. * @vi: inode to write out
  2802. * @sync: if true, write out synchronously
  2803. *
  2804. * Write out a dirty inode to disk including any extent inodes if present.
  2805. *
  2806. * If @sync is true, commit the inode to disk and wait for io completion. This
  2807. * is done using write_mft_record().
  2808. *
  2809. * If @sync is false, just schedule the write to happen but do not wait for i/o
  2810. * completion. In 2.6 kernels, scheduling usually happens just by virtue of
  2811. * marking the page (and in this case mft record) dirty but we do not implement
  2812. * this yet as write_mft_record() largely ignores the @sync parameter and
  2813. * always performs synchronous writes.
  2814. *
  2815. * Return 0 on success and -errno on error.
  2816. */
  2817. int __ntfs_write_inode(struct inode *vi, int sync)
  2818. {
  2819. sle64 nt;
  2820. ntfs_inode *ni = NTFS_I(vi);
  2821. ntfs_attr_search_ctx *ctx;
  2822. MFT_RECORD *m;
  2823. STANDARD_INFORMATION *si;
  2824. int err = 0;
  2825. bool modified = false;
  2826. ntfs_debug("Entering for %sinode 0x%lx.", NInoAttr(ni) ? "attr " : "",
  2827. vi->i_ino);
  2828. /*
  2829. * Dirty attribute inodes are written via their real inodes so just
  2830. * clean them here. Access time updates are taken care off when the
  2831. * real inode is written.
  2832. */
  2833. if (NInoAttr(ni)) {
  2834. NInoClearDirty(ni);
  2835. ntfs_debug("Done.");
  2836. return 0;
  2837. }
  2838. /* Map, pin, and lock the mft record belonging to the inode. */
  2839. m = map_mft_record(ni);
  2840. if (IS_ERR(m)) {
  2841. err = PTR_ERR(m);
  2842. goto err_out;
  2843. }
  2844. /* Update the access times in the standard information attribute. */
  2845. ctx = ntfs_attr_get_search_ctx(ni, m);
  2846. if (unlikely(!ctx)) {
  2847. err = -ENOMEM;
  2848. goto unm_err_out;
  2849. }
  2850. err = ntfs_attr_lookup(AT_STANDARD_INFORMATION, NULL, 0,
  2851. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2852. if (unlikely(err)) {
  2853. ntfs_attr_put_search_ctx(ctx);
  2854. goto unm_err_out;
  2855. }
  2856. si = (STANDARD_INFORMATION*)((u8*)ctx->attr +
  2857. le16_to_cpu(ctx->attr->data.resident.value_offset));
  2858. /* Update the access times if they have changed. */
  2859. nt = utc2ntfs(vi->i_mtime);
  2860. if (si->last_data_change_time != nt) {
  2861. ntfs_debug("Updating mtime for inode 0x%lx: old = 0x%llx, "
  2862. "new = 0x%llx", vi->i_ino, (long long)
  2863. sle64_to_cpu(si->last_data_change_time),
  2864. (long long)sle64_to_cpu(nt));
  2865. si->last_data_change_time = nt;
  2866. modified = true;
  2867. }
  2868. nt = utc2ntfs(vi->i_ctime);
  2869. if (si->last_mft_change_time != nt) {
  2870. ntfs_debug("Updating ctime for inode 0x%lx: old = 0x%llx, "
  2871. "new = 0x%llx", vi->i_ino, (long long)
  2872. sle64_to_cpu(si->last_mft_change_time),
  2873. (long long)sle64_to_cpu(nt));
  2874. si->last_mft_change_time = nt;
  2875. modified = true;
  2876. }
  2877. nt = utc2ntfs(vi->i_atime);
  2878. if (si->last_access_time != nt) {
  2879. ntfs_debug("Updating atime for inode 0x%lx: old = 0x%llx, "
  2880. "new = 0x%llx", vi->i_ino,
  2881. (long long)sle64_to_cpu(si->last_access_time),
  2882. (long long)sle64_to_cpu(nt));
  2883. si->last_access_time = nt;
  2884. modified = true;
  2885. }
  2886. /*
  2887. * If we just modified the standard information attribute we need to
  2888. * mark the mft record it is in dirty. We do this manually so that
  2889. * mark_inode_dirty() is not called which would redirty the inode and
  2890. * hence result in an infinite loop of trying to write the inode.
  2891. * There is no need to mark the base inode nor the base mft record
  2892. * dirty, since we are going to write this mft record below in any case
  2893. * and the base mft record may actually not have been modified so it
  2894. * might not need to be written out.
  2895. * NOTE: It is not a problem when the inode for $MFT itself is being
  2896. * written out as mark_ntfs_record_dirty() will only set I_DIRTY_PAGES
  2897. * on the $MFT inode and hence ntfs_write_inode() will not be
  2898. * re-invoked because of it which in turn is ok since the dirtied mft
  2899. * record will be cleaned and written out to disk below, i.e. before
  2900. * this function returns.
  2901. */
  2902. if (modified) {
  2903. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2904. if (!NInoTestSetDirty(ctx->ntfs_ino))
  2905. mark_ntfs_record_dirty(ctx->ntfs_ino->page,
  2906. ctx->ntfs_ino->page_ofs);
  2907. }
  2908. ntfs_attr_put_search_ctx(ctx);
  2909. /* Now the access times are updated, write the base mft record. */
  2910. if (NInoDirty(ni))
  2911. err = write_mft_record(ni, m, sync);
  2912. /* Write all attached extent mft records. */
  2913. mutex_lock(&ni->extent_lock);
  2914. if (ni->nr_extents > 0) {
  2915. ntfs_inode **extent_nis = ni->ext.extent_ntfs_inos;
  2916. int i;
  2917. ntfs_debug("Writing %i extent inodes.", ni->nr_extents);
  2918. for (i = 0; i < ni->nr_extents; i++) {
  2919. ntfs_inode *tni = extent_nis[i];
  2920. if (NInoDirty(tni)) {
  2921. MFT_RECORD *tm = map_mft_record(tni);
  2922. int ret;
  2923. if (IS_ERR(tm)) {
  2924. if (!err || err == -ENOMEM)
  2925. err = PTR_ERR(tm);
  2926. continue;
  2927. }
  2928. ret = write_mft_record(tni, tm, sync);
  2929. unmap_mft_record(tni);
  2930. if (unlikely(ret)) {
  2931. if (!err || err == -ENOMEM)
  2932. err = ret;
  2933. }
  2934. }
  2935. }
  2936. }
  2937. mutex_unlock(&ni->extent_lock);
  2938. unmap_mft_record(ni);
  2939. if (unlikely(err))
  2940. goto err_out;
  2941. ntfs_debug("Done.");
  2942. return 0;
  2943. unm_err_out:
  2944. unmap_mft_record(ni);
  2945. err_out:
  2946. if (err == -ENOMEM) {
  2947. ntfs_warning(vi->i_sb, "Not enough memory to write inode. "
  2948. "Marking the inode dirty again, so the VFS "
  2949. "retries later.");
  2950. mark_inode_dirty(vi);
  2951. } else {
  2952. ntfs_error(vi->i_sb, "Failed (error %i): Run chkdsk.", -err);
  2953. NVolSetErrors(ni->vol);
  2954. }
  2955. return err;
  2956. }
  2957. #endif /* NTFS_RW */