inode.c 98 KB

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