inode.c 97 KB

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