inode.c 97 KB

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