inode.c 80 KB

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