inode.c 80 KB

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