inode.c 97 KB

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