inode.c 80 KB

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