inode.c 80 KB

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