inode.c 97 KB

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