inode.c 80 KB

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