inode.c 99 KB

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