inode.c 97 KB

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