inode.c 97 KB

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