mft.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912
  1. /**
  2. * mft.c - NTFS kernel mft record operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2005 Anton Altaparmakov
  5. * Copyright (c) 2002 Richard Russon
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/buffer_head.h>
  23. #include <linux/swap.h>
  24. #include "attrib.h"
  25. #include "aops.h"
  26. #include "bitmap.h"
  27. #include "debug.h"
  28. #include "dir.h"
  29. #include "lcnalloc.h"
  30. #include "malloc.h"
  31. #include "mft.h"
  32. #include "ntfs.h"
  33. /**
  34. * map_mft_record_page - map the page in which a specific mft record resides
  35. * @ni: ntfs inode whose mft record page to map
  36. *
  37. * This maps the page in which the mft record of the ntfs inode @ni is situated
  38. * and returns a pointer to the mft record within the mapped page.
  39. *
  40. * Return value needs to be checked with IS_ERR() and if that is true PTR_ERR()
  41. * contains the negative error code returned.
  42. */
  43. static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni)
  44. {
  45. loff_t i_size;
  46. ntfs_volume *vol = ni->vol;
  47. struct inode *mft_vi = vol->mft_ino;
  48. struct page *page;
  49. unsigned long index, ofs, end_index;
  50. BUG_ON(ni->page);
  51. /*
  52. * The index into the page cache and the offset within the page cache
  53. * page of the wanted mft record. FIXME: We need to check for
  54. * overflowing the unsigned long, but I don't think we would ever get
  55. * here if the volume was that big...
  56. */
  57. index = ni->mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
  58. ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
  59. i_size = i_size_read(mft_vi);
  60. /* The maximum valid index into the page cache for $MFT's data. */
  61. end_index = i_size >> PAGE_CACHE_SHIFT;
  62. /* If the wanted index is out of bounds the mft record doesn't exist. */
  63. if (unlikely(index >= end_index)) {
  64. if (index > end_index || (i_size & ~PAGE_CACHE_MASK) < ofs +
  65. vol->mft_record_size) {
  66. page = ERR_PTR(-ENOENT);
  67. ntfs_error(vol->sb, "Attemt to read mft record 0x%lx, "
  68. "which is beyond the end of the mft. "
  69. "This is probably a bug in the ntfs "
  70. "driver.", ni->mft_no);
  71. goto err_out;
  72. }
  73. }
  74. /* Read, map, and pin the page. */
  75. page = ntfs_map_page(mft_vi->i_mapping, index);
  76. if (likely(!IS_ERR(page))) {
  77. /* Catch multi sector transfer fixup errors. */
  78. if (likely(ntfs_is_mft_recordp((le32*)(page_address(page) +
  79. ofs)))) {
  80. ni->page = page;
  81. ni->page_ofs = ofs;
  82. return page_address(page) + ofs;
  83. }
  84. ntfs_error(vol->sb, "Mft record 0x%lx is corrupt. "
  85. "Run chkdsk.", ni->mft_no);
  86. ntfs_unmap_page(page);
  87. page = ERR_PTR(-EIO);
  88. }
  89. err_out:
  90. ni->page = NULL;
  91. ni->page_ofs = 0;
  92. return (void*)page;
  93. }
  94. /**
  95. * map_mft_record - map, pin and lock an mft record
  96. * @ni: ntfs inode whose MFT record to map
  97. *
  98. * First, take the mrec_lock semaphore. We might now be sleeping, while waiting
  99. * for the semaphore if it was already locked by someone else.
  100. *
  101. * The page of the record is mapped using map_mft_record_page() before being
  102. * returned to the caller.
  103. *
  104. * This in turn uses ntfs_map_page() to get the page containing the wanted mft
  105. * record (it in turn calls read_cache_page() which reads it in from disk if
  106. * necessary, increments the use count on the page so that it cannot disappear
  107. * under us and returns a reference to the page cache page).
  108. *
  109. * If read_cache_page() invokes ntfs_readpage() to load the page from disk, it
  110. * sets PG_locked and clears PG_uptodate on the page. Once I/O has completed
  111. * and the post-read mst fixups on each mft record in the page have been
  112. * performed, the page gets PG_uptodate set and PG_locked cleared (this is done
  113. * in our asynchronous I/O completion handler end_buffer_read_mft_async()).
  114. * ntfs_map_page() waits for PG_locked to become clear and checks if
  115. * PG_uptodate is set and returns an error code if not. This provides
  116. * sufficient protection against races when reading/using the page.
  117. *
  118. * However there is the write mapping to think about. Doing the above described
  119. * checking here will be fine, because when initiating the write we will set
  120. * PG_locked and clear PG_uptodate making sure nobody is touching the page
  121. * contents. Doing the locking this way means that the commit to disk code in
  122. * the page cache code paths is automatically sufficiently locked with us as
  123. * we will not touch a page that has been locked or is not uptodate. The only
  124. * locking problem then is them locking the page while we are accessing it.
  125. *
  126. * So that code will end up having to own the mrec_lock of all mft
  127. * records/inodes present in the page before I/O can proceed. In that case we
  128. * wouldn't need to bother with PG_locked and PG_uptodate as nobody will be
  129. * accessing anything without owning the mrec_lock semaphore. But we do need
  130. * to use them because of the read_cache_page() invocation and the code becomes
  131. * so much simpler this way that it is well worth it.
  132. *
  133. * The mft record is now ours and we return a pointer to it. You need to check
  134. * the returned pointer with IS_ERR() and if that is true, PTR_ERR() will return
  135. * the error code.
  136. *
  137. * NOTE: Caller is responsible for setting the mft record dirty before calling
  138. * unmap_mft_record(). This is obviously only necessary if the caller really
  139. * modified the mft record...
  140. * Q: Do we want to recycle one of the VFS inode state bits instead?
  141. * A: No, the inode ones mean we want to change the mft record, not we want to
  142. * write it out.
  143. */
  144. MFT_RECORD *map_mft_record(ntfs_inode *ni)
  145. {
  146. MFT_RECORD *m;
  147. ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
  148. /* Make sure the ntfs inode doesn't go away. */
  149. atomic_inc(&ni->count);
  150. /* Serialize access to this mft record. */
  151. down(&ni->mrec_lock);
  152. m = map_mft_record_page(ni);
  153. if (likely(!IS_ERR(m)))
  154. return m;
  155. up(&ni->mrec_lock);
  156. atomic_dec(&ni->count);
  157. ntfs_error(ni->vol->sb, "Failed with error code %lu.", -PTR_ERR(m));
  158. return m;
  159. }
  160. /**
  161. * unmap_mft_record_page - unmap the page in which a specific mft record resides
  162. * @ni: ntfs inode whose mft record page to unmap
  163. *
  164. * This unmaps the page in which the mft record of the ntfs inode @ni is
  165. * situated and returns. This is a NOOP if highmem is not configured.
  166. *
  167. * The unmap happens via ntfs_unmap_page() which in turn decrements the use
  168. * count on the page thus releasing it from the pinned state.
  169. *
  170. * We do not actually unmap the page from memory of course, as that will be
  171. * done by the page cache code itself when memory pressure increases or
  172. * whatever.
  173. */
  174. static inline void unmap_mft_record_page(ntfs_inode *ni)
  175. {
  176. BUG_ON(!ni->page);
  177. // TODO: If dirty, blah...
  178. ntfs_unmap_page(ni->page);
  179. ni->page = NULL;
  180. ni->page_ofs = 0;
  181. return;
  182. }
  183. /**
  184. * unmap_mft_record - release a mapped mft record
  185. * @ni: ntfs inode whose MFT record to unmap
  186. *
  187. * We release the page mapping and the mrec_lock mutex which unmaps the mft
  188. * record and releases it for others to get hold of. We also release the ntfs
  189. * inode by decrementing the ntfs inode reference count.
  190. *
  191. * NOTE: If caller has modified the mft record, it is imperative to set the mft
  192. * record dirty BEFORE calling unmap_mft_record().
  193. */
  194. void unmap_mft_record(ntfs_inode *ni)
  195. {
  196. struct page *page = ni->page;
  197. BUG_ON(!page);
  198. ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
  199. unmap_mft_record_page(ni);
  200. up(&ni->mrec_lock);
  201. atomic_dec(&ni->count);
  202. /*
  203. * If pure ntfs_inode, i.e. no vfs inode attached, we leave it to
  204. * ntfs_clear_extent_inode() in the extent inode case, and to the
  205. * caller in the non-extent, yet pure ntfs inode case, to do the actual
  206. * tear down of all structures and freeing of all allocated memory.
  207. */
  208. return;
  209. }
  210. /**
  211. * map_extent_mft_record - load an extent inode and attach it to its base
  212. * @base_ni: base ntfs inode
  213. * @mref: mft reference of the extent inode to load
  214. * @ntfs_ino: on successful return, pointer to the ntfs_inode structure
  215. *
  216. * Load the extent mft record @mref and attach it to its base inode @base_ni.
  217. * Return the mapped extent mft record if IS_ERR(result) is false. Otherwise
  218. * PTR_ERR(result) gives the negative error code.
  219. *
  220. * On successful return, @ntfs_ino contains a pointer to the ntfs_inode
  221. * structure of the mapped extent inode.
  222. */
  223. MFT_RECORD *map_extent_mft_record(ntfs_inode *base_ni, MFT_REF mref,
  224. ntfs_inode **ntfs_ino)
  225. {
  226. MFT_RECORD *m;
  227. ntfs_inode *ni = NULL;
  228. ntfs_inode **extent_nis = NULL;
  229. int i;
  230. unsigned long mft_no = MREF(mref);
  231. u16 seq_no = MSEQNO(mref);
  232. BOOL destroy_ni = FALSE;
  233. ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).",
  234. mft_no, base_ni->mft_no);
  235. /* Make sure the base ntfs inode doesn't go away. */
  236. atomic_inc(&base_ni->count);
  237. /*
  238. * Check if this extent inode has already been added to the base inode,
  239. * in which case just return it. If not found, add it to the base
  240. * inode before returning it.
  241. */
  242. down(&base_ni->extent_lock);
  243. if (base_ni->nr_extents > 0) {
  244. extent_nis = base_ni->ext.extent_ntfs_inos;
  245. for (i = 0; i < base_ni->nr_extents; i++) {
  246. if (mft_no != extent_nis[i]->mft_no)
  247. continue;
  248. ni = extent_nis[i];
  249. /* Make sure the ntfs inode doesn't go away. */
  250. atomic_inc(&ni->count);
  251. break;
  252. }
  253. }
  254. if (likely(ni != NULL)) {
  255. up(&base_ni->extent_lock);
  256. atomic_dec(&base_ni->count);
  257. /* We found the record; just have to map and return it. */
  258. m = map_mft_record(ni);
  259. /* map_mft_record() has incremented this on success. */
  260. atomic_dec(&ni->count);
  261. if (likely(!IS_ERR(m))) {
  262. /* Verify the sequence number. */
  263. if (likely(le16_to_cpu(m->sequence_number) == seq_no)) {
  264. ntfs_debug("Done 1.");
  265. *ntfs_ino = ni;
  266. return m;
  267. }
  268. unmap_mft_record(ni);
  269. ntfs_error(base_ni->vol->sb, "Found stale extent mft "
  270. "reference! Corrupt filesystem. "
  271. "Run chkdsk.");
  272. return ERR_PTR(-EIO);
  273. }
  274. map_err_out:
  275. ntfs_error(base_ni->vol->sb, "Failed to map extent "
  276. "mft record, error code %ld.", -PTR_ERR(m));
  277. return m;
  278. }
  279. /* Record wasn't there. Get a new ntfs inode and initialize it. */
  280. ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no);
  281. if (unlikely(!ni)) {
  282. up(&base_ni->extent_lock);
  283. atomic_dec(&base_ni->count);
  284. return ERR_PTR(-ENOMEM);
  285. }
  286. ni->vol = base_ni->vol;
  287. ni->seq_no = seq_no;
  288. ni->nr_extents = -1;
  289. ni->ext.base_ntfs_ino = base_ni;
  290. /* Now map the record. */
  291. m = map_mft_record(ni);
  292. if (IS_ERR(m)) {
  293. up(&base_ni->extent_lock);
  294. atomic_dec(&base_ni->count);
  295. ntfs_clear_extent_inode(ni);
  296. goto map_err_out;
  297. }
  298. /* Verify the sequence number if it is present. */
  299. if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) {
  300. ntfs_error(base_ni->vol->sb, "Found stale extent mft "
  301. "reference! Corrupt filesystem. Run chkdsk.");
  302. destroy_ni = TRUE;
  303. m = ERR_PTR(-EIO);
  304. goto unm_err_out;
  305. }
  306. /* Attach extent inode to base inode, reallocating memory if needed. */
  307. if (!(base_ni->nr_extents & 3)) {
  308. ntfs_inode **tmp;
  309. int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
  310. tmp = (ntfs_inode **)kmalloc(new_size, GFP_NOFS);
  311. if (unlikely(!tmp)) {
  312. ntfs_error(base_ni->vol->sb, "Failed to allocate "
  313. "internal buffer.");
  314. destroy_ni = TRUE;
  315. m = ERR_PTR(-ENOMEM);
  316. goto unm_err_out;
  317. }
  318. if (base_ni->nr_extents) {
  319. BUG_ON(!base_ni->ext.extent_ntfs_inos);
  320. memcpy(tmp, base_ni->ext.extent_ntfs_inos, new_size -
  321. 4 * sizeof(ntfs_inode *));
  322. kfree(base_ni->ext.extent_ntfs_inos);
  323. }
  324. base_ni->ext.extent_ntfs_inos = tmp;
  325. }
  326. base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni;
  327. up(&base_ni->extent_lock);
  328. atomic_dec(&base_ni->count);
  329. ntfs_debug("Done 2.");
  330. *ntfs_ino = ni;
  331. return m;
  332. unm_err_out:
  333. unmap_mft_record(ni);
  334. up(&base_ni->extent_lock);
  335. atomic_dec(&base_ni->count);
  336. /*
  337. * If the extent inode was not attached to the base inode we need to
  338. * release it or we will leak memory.
  339. */
  340. if (destroy_ni)
  341. ntfs_clear_extent_inode(ni);
  342. return m;
  343. }
  344. #ifdef NTFS_RW
  345. /**
  346. * __mark_mft_record_dirty - set the mft record and the page containing it dirty
  347. * @ni: ntfs inode describing the mapped mft record
  348. *
  349. * Internal function. Users should call mark_mft_record_dirty() instead.
  350. *
  351. * Set the mapped (extent) mft record of the (base or extent) ntfs inode @ni,
  352. * as well as the page containing the mft record, dirty. Also, mark the base
  353. * vfs inode dirty. This ensures that any changes to the mft record are
  354. * written out to disk.
  355. *
  356. * NOTE: We only set I_DIRTY_SYNC and I_DIRTY_DATASYNC (and not I_DIRTY_PAGES)
  357. * on the base vfs inode, because even though file data may have been modified,
  358. * it is dirty in the inode meta data rather than the data page cache of the
  359. * inode, and thus there are no data pages that need writing out. Therefore, a
  360. * full mark_inode_dirty() is overkill. A mark_inode_dirty_sync(), on the
  361. * other hand, is not sufficient, because I_DIRTY_DATASYNC needs to be set to
  362. * ensure ->write_inode is called from generic_osync_inode() and this needs to
  363. * happen or the file data would not necessarily hit the device synchronously,
  364. * even though the vfs inode has the O_SYNC flag set. Also, I_DIRTY_DATASYNC
  365. * simply "feels" better than just I_DIRTY_SYNC, since the file data has not
  366. * actually hit the block device yet, which is not what I_DIRTY_SYNC on its own
  367. * would suggest.
  368. */
  369. void __mark_mft_record_dirty(ntfs_inode *ni)
  370. {
  371. ntfs_inode *base_ni;
  372. ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
  373. BUG_ON(NInoAttr(ni));
  374. mark_ntfs_record_dirty(ni->page, ni->page_ofs);
  375. /* Determine the base vfs inode and mark it dirty, too. */
  376. down(&ni->extent_lock);
  377. if (likely(ni->nr_extents >= 0))
  378. base_ni = ni;
  379. else
  380. base_ni = ni->ext.base_ntfs_ino;
  381. up(&ni->extent_lock);
  382. __mark_inode_dirty(VFS_I(base_ni), I_DIRTY_SYNC | I_DIRTY_DATASYNC);
  383. }
  384. static const char *ntfs_please_email = "Please email "
  385. "linux-ntfs-dev@lists.sourceforge.net and say that you saw "
  386. "this message. Thank you.";
  387. /**
  388. * ntfs_sync_mft_mirror_umount - synchronise an mft record to the mft mirror
  389. * @vol: ntfs volume on which the mft record to synchronize resides
  390. * @mft_no: mft record number of mft record to synchronize
  391. * @m: mapped, mst protected (extent) mft record to synchronize
  392. *
  393. * Write the mapped, mst protected (extent) mft record @m with mft record
  394. * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol,
  395. * bypassing the page cache and the $MFTMirr inode itself.
  396. *
  397. * This function is only for use at umount time when the mft mirror inode has
  398. * already been disposed off. We BUG() if we are called while the mft mirror
  399. * inode is still attached to the volume.
  400. *
  401. * On success return 0. On error return -errno.
  402. *
  403. * NOTE: This function is not implemented yet as I am not convinced it can
  404. * actually be triggered considering the sequence of commits we do in super.c::
  405. * ntfs_put_super(). But just in case we provide this place holder as the
  406. * alternative would be either to BUG() or to get a NULL pointer dereference
  407. * and Oops.
  408. */
  409. static int ntfs_sync_mft_mirror_umount(ntfs_volume *vol,
  410. const unsigned long mft_no, MFT_RECORD *m)
  411. {
  412. BUG_ON(vol->mftmirr_ino);
  413. ntfs_error(vol->sb, "Umount time mft mirror syncing is not "
  414. "implemented yet. %s", ntfs_please_email);
  415. return -EOPNOTSUPP;
  416. }
  417. /**
  418. * ntfs_sync_mft_mirror - synchronize an mft record to the mft mirror
  419. * @vol: ntfs volume on which the mft record to synchronize resides
  420. * @mft_no: mft record number of mft record to synchronize
  421. * @m: mapped, mst protected (extent) mft record to synchronize
  422. * @sync: if true, wait for i/o completion
  423. *
  424. * Write the mapped, mst protected (extent) mft record @m with mft record
  425. * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol.
  426. *
  427. * On success return 0. On error return -errno and set the volume errors flag
  428. * in the ntfs volume @vol.
  429. *
  430. * NOTE: We always perform synchronous i/o and ignore the @sync parameter.
  431. *
  432. * TODO: If @sync is false, want to do truly asynchronous i/o, i.e. just
  433. * schedule i/o via ->writepage or do it via kntfsd or whatever.
  434. */
  435. int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
  436. MFT_RECORD *m, int sync)
  437. {
  438. struct page *page;
  439. unsigned int blocksize = vol->sb->s_blocksize;
  440. int max_bhs = vol->mft_record_size / blocksize;
  441. struct buffer_head *bhs[max_bhs];
  442. struct buffer_head *bh, *head;
  443. u8 *kmirr;
  444. runlist_element *rl;
  445. unsigned int block_start, block_end, m_start, m_end, page_ofs;
  446. int i_bhs, nr_bhs, err = 0;
  447. unsigned char blocksize_bits = vol->mftmirr_ino->i_blkbits;
  448. ntfs_debug("Entering for inode 0x%lx.", mft_no);
  449. BUG_ON(!max_bhs);
  450. if (unlikely(!vol->mftmirr_ino)) {
  451. /* This could happen during umount... */
  452. err = ntfs_sync_mft_mirror_umount(vol, mft_no, m);
  453. if (likely(!err))
  454. return err;
  455. goto err_out;
  456. }
  457. /* Get the page containing the mirror copy of the mft record @m. */
  458. page = ntfs_map_page(vol->mftmirr_ino->i_mapping, mft_no >>
  459. (PAGE_CACHE_SHIFT - vol->mft_record_size_bits));
  460. if (IS_ERR(page)) {
  461. ntfs_error(vol->sb, "Failed to map mft mirror page.");
  462. err = PTR_ERR(page);
  463. goto err_out;
  464. }
  465. lock_page(page);
  466. BUG_ON(!PageUptodate(page));
  467. ClearPageUptodate(page);
  468. /* Offset of the mft mirror record inside the page. */
  469. page_ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
  470. /* The address in the page of the mirror copy of the mft record @m. */
  471. kmirr = page_address(page) + page_ofs;
  472. /* Copy the mst protected mft record to the mirror. */
  473. memcpy(kmirr, m, vol->mft_record_size);
  474. /* Create uptodate buffers if not present. */
  475. if (unlikely(!page_has_buffers(page))) {
  476. struct buffer_head *tail;
  477. bh = head = alloc_page_buffers(page, blocksize, 1);
  478. do {
  479. set_buffer_uptodate(bh);
  480. tail = bh;
  481. bh = bh->b_this_page;
  482. } while (bh);
  483. tail->b_this_page = head;
  484. attach_page_buffers(page, head);
  485. }
  486. bh = head = page_buffers(page);
  487. BUG_ON(!bh);
  488. rl = NULL;
  489. nr_bhs = 0;
  490. block_start = 0;
  491. m_start = kmirr - (u8*)page_address(page);
  492. m_end = m_start + vol->mft_record_size;
  493. do {
  494. block_end = block_start + blocksize;
  495. /* If the buffer is outside the mft record, skip it. */
  496. if (block_end <= m_start)
  497. continue;
  498. if (unlikely(block_start >= m_end))
  499. break;
  500. /* Need to map the buffer if it is not mapped already. */
  501. if (unlikely(!buffer_mapped(bh))) {
  502. VCN vcn;
  503. LCN lcn;
  504. unsigned int vcn_ofs;
  505. bh->b_bdev = vol->sb->s_bdev;
  506. /* Obtain the vcn and offset of the current block. */
  507. vcn = ((VCN)mft_no << vol->mft_record_size_bits) +
  508. (block_start - m_start);
  509. vcn_ofs = vcn & vol->cluster_size_mask;
  510. vcn >>= vol->cluster_size_bits;
  511. if (!rl) {
  512. down_read(&NTFS_I(vol->mftmirr_ino)->
  513. runlist.lock);
  514. rl = NTFS_I(vol->mftmirr_ino)->runlist.rl;
  515. /*
  516. * $MFTMirr always has the whole of its runlist
  517. * in memory.
  518. */
  519. BUG_ON(!rl);
  520. }
  521. /* Seek to element containing target vcn. */
  522. while (rl->length && rl[1].vcn <= vcn)
  523. rl++;
  524. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  525. /* For $MFTMirr, only lcn >= 0 is a successful remap. */
  526. if (likely(lcn >= 0)) {
  527. /* Setup buffer head to correct block. */
  528. bh->b_blocknr = ((lcn <<
  529. vol->cluster_size_bits) +
  530. vcn_ofs) >> blocksize_bits;
  531. set_buffer_mapped(bh);
  532. } else {
  533. bh->b_blocknr = -1;
  534. ntfs_error(vol->sb, "Cannot write mft mirror "
  535. "record 0x%lx because its "
  536. "location on disk could not "
  537. "be determined (error code "
  538. "%lli).", mft_no,
  539. (long long)lcn);
  540. err = -EIO;
  541. }
  542. }
  543. BUG_ON(!buffer_uptodate(bh));
  544. BUG_ON(!nr_bhs && (m_start != block_start));
  545. BUG_ON(nr_bhs >= max_bhs);
  546. bhs[nr_bhs++] = bh;
  547. BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
  548. } while (block_start = block_end, (bh = bh->b_this_page) != head);
  549. if (unlikely(rl))
  550. up_read(&NTFS_I(vol->mftmirr_ino)->runlist.lock);
  551. if (likely(!err)) {
  552. /* Lock buffers and start synchronous write i/o on them. */
  553. for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
  554. struct buffer_head *tbh = bhs[i_bhs];
  555. if (unlikely(test_set_buffer_locked(tbh)))
  556. BUG();
  557. BUG_ON(!buffer_uptodate(tbh));
  558. clear_buffer_dirty(tbh);
  559. get_bh(tbh);
  560. tbh->b_end_io = end_buffer_write_sync;
  561. submit_bh(WRITE, tbh);
  562. }
  563. /* Wait on i/o completion of buffers. */
  564. for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
  565. struct buffer_head *tbh = bhs[i_bhs];
  566. wait_on_buffer(tbh);
  567. if (unlikely(!buffer_uptodate(tbh))) {
  568. err = -EIO;
  569. /*
  570. * Set the buffer uptodate so the page and
  571. * buffer states do not become out of sync.
  572. */
  573. set_buffer_uptodate(tbh);
  574. }
  575. }
  576. } else /* if (unlikely(err)) */ {
  577. /* Clean the buffers. */
  578. for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
  579. clear_buffer_dirty(bhs[i_bhs]);
  580. }
  581. /* Current state: all buffers are clean, unlocked, and uptodate. */
  582. /* Remove the mst protection fixups again. */
  583. post_write_mst_fixup((NTFS_RECORD*)kmirr);
  584. flush_dcache_page(page);
  585. SetPageUptodate(page);
  586. unlock_page(page);
  587. ntfs_unmap_page(page);
  588. if (likely(!err)) {
  589. ntfs_debug("Done.");
  590. } else {
  591. ntfs_error(vol->sb, "I/O error while writing mft mirror "
  592. "record 0x%lx!", mft_no);
  593. err_out:
  594. ntfs_error(vol->sb, "Failed to synchronize $MFTMirr (error "
  595. "code %i). Volume will be left marked dirty "
  596. "on umount. Run ntfsfix on the partition "
  597. "after umounting to correct this.", -err);
  598. NVolSetErrors(vol);
  599. }
  600. return err;
  601. }
  602. /**
  603. * write_mft_record_nolock - write out a mapped (extent) mft record
  604. * @ni: ntfs inode describing the mapped (extent) mft record
  605. * @m: mapped (extent) mft record to write
  606. * @sync: if true, wait for i/o completion
  607. *
  608. * Write the mapped (extent) mft record @m described by the (regular or extent)
  609. * ntfs inode @ni to backing store. If the mft record @m has a counterpart in
  610. * the mft mirror, that is also updated.
  611. *
  612. * We only write the mft record if the ntfs inode @ni is dirty and the first
  613. * buffer belonging to its mft record is dirty, too. We ignore the dirty state
  614. * of subsequent buffers because we could have raced with
  615. * fs/ntfs/aops.c::mark_ntfs_record_dirty().
  616. *
  617. * On success, clean the mft record and return 0. On error, leave the mft
  618. * record dirty and return -errno. The caller should call make_bad_inode() on
  619. * the base inode to ensure no more access happens to this inode. We do not do
  620. * it here as the caller may want to finish writing other extent mft records
  621. * first to minimize on-disk metadata inconsistencies.
  622. *
  623. * NOTE: We always perform synchronous i/o and ignore the @sync parameter.
  624. * However, if the mft record has a counterpart in the mft mirror and @sync is
  625. * true, we write the mft record, wait for i/o completion, and only then write
  626. * the mft mirror copy. This ensures that if the system crashes either the mft
  627. * or the mft mirror will contain a self-consistent mft record @m. If @sync is
  628. * false on the other hand, we start i/o on both and then wait for completion
  629. * on them. This provides a speedup but no longer guarantees that you will end
  630. * up with a self-consistent mft record in the case of a crash but if you asked
  631. * for asynchronous writing you probably do not care about that anyway.
  632. *
  633. * TODO: If @sync is false, want to do truly asynchronous i/o, i.e. just
  634. * schedule i/o via ->writepage or do it via kntfsd or whatever.
  635. */
  636. int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync)
  637. {
  638. ntfs_volume *vol = ni->vol;
  639. struct page *page = ni->page;
  640. unsigned char blocksize_bits = vol->mft_ino->i_blkbits;
  641. unsigned int blocksize = 1 << blocksize_bits;
  642. int max_bhs = vol->mft_record_size / blocksize;
  643. struct buffer_head *bhs[max_bhs];
  644. struct buffer_head *bh, *head;
  645. runlist_element *rl;
  646. unsigned int block_start, block_end, m_start, m_end;
  647. int i_bhs, nr_bhs, err = 0;
  648. ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
  649. BUG_ON(NInoAttr(ni));
  650. BUG_ON(!max_bhs);
  651. BUG_ON(!PageLocked(page));
  652. /*
  653. * If the ntfs_inode is clean no need to do anything. If it is dirty,
  654. * mark it as clean now so that it can be redirtied later on if needed.
  655. * There is no danger of races since the caller is holding the locks
  656. * for the mft record @m and the page it is in.
  657. */
  658. if (!NInoTestClearDirty(ni))
  659. goto done;
  660. bh = head = page_buffers(page);
  661. BUG_ON(!bh);
  662. rl = NULL;
  663. nr_bhs = 0;
  664. block_start = 0;
  665. m_start = ni->page_ofs;
  666. m_end = m_start + vol->mft_record_size;
  667. do {
  668. block_end = block_start + blocksize;
  669. /* If the buffer is outside the mft record, skip it. */
  670. if (block_end <= m_start)
  671. continue;
  672. if (unlikely(block_start >= m_end))
  673. break;
  674. /*
  675. * If this block is not the first one in the record, we ignore
  676. * the buffer's dirty state because we could have raced with a
  677. * parallel mark_ntfs_record_dirty().
  678. */
  679. if (block_start == m_start) {
  680. /* This block is the first one in the record. */
  681. if (!buffer_dirty(bh)) {
  682. BUG_ON(nr_bhs);
  683. /* Clean records are not written out. */
  684. break;
  685. }
  686. }
  687. /* Need to map the buffer if it is not mapped already. */
  688. if (unlikely(!buffer_mapped(bh))) {
  689. VCN vcn;
  690. LCN lcn;
  691. unsigned int vcn_ofs;
  692. bh->b_bdev = vol->sb->s_bdev;
  693. /* Obtain the vcn and offset of the current block. */
  694. vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) +
  695. (block_start - m_start);
  696. vcn_ofs = vcn & vol->cluster_size_mask;
  697. vcn >>= vol->cluster_size_bits;
  698. if (!rl) {
  699. down_read(&NTFS_I(vol->mft_ino)->runlist.lock);
  700. rl = NTFS_I(vol->mft_ino)->runlist.rl;
  701. BUG_ON(!rl);
  702. }
  703. /* Seek to element containing target vcn. */
  704. while (rl->length && rl[1].vcn <= vcn)
  705. rl++;
  706. lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
  707. /* For $MFT, only lcn >= 0 is a successful remap. */
  708. if (likely(lcn >= 0)) {
  709. /* Setup buffer head to correct block. */
  710. bh->b_blocknr = ((lcn <<
  711. vol->cluster_size_bits) +
  712. vcn_ofs) >> blocksize_bits;
  713. set_buffer_mapped(bh);
  714. } else {
  715. bh->b_blocknr = -1;
  716. ntfs_error(vol->sb, "Cannot write mft record "
  717. "0x%lx because its location "
  718. "on disk could not be "
  719. "determined (error code %lli).",
  720. ni->mft_no, (long long)lcn);
  721. err = -EIO;
  722. }
  723. }
  724. BUG_ON(!buffer_uptodate(bh));
  725. BUG_ON(!nr_bhs && (m_start != block_start));
  726. BUG_ON(nr_bhs >= max_bhs);
  727. bhs[nr_bhs++] = bh;
  728. BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
  729. } while (block_start = block_end, (bh = bh->b_this_page) != head);
  730. if (unlikely(rl))
  731. up_read(&NTFS_I(vol->mft_ino)->runlist.lock);
  732. if (!nr_bhs)
  733. goto done;
  734. if (unlikely(err))
  735. goto cleanup_out;
  736. /* Apply the mst protection fixups. */
  737. err = pre_write_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size);
  738. if (err) {
  739. ntfs_error(vol->sb, "Failed to apply mst fixups!");
  740. goto cleanup_out;
  741. }
  742. flush_dcache_mft_record_page(ni);
  743. /* Lock buffers and start synchronous write i/o on them. */
  744. for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
  745. struct buffer_head *tbh = bhs[i_bhs];
  746. if (unlikely(test_set_buffer_locked(tbh)))
  747. BUG();
  748. BUG_ON(!buffer_uptodate(tbh));
  749. clear_buffer_dirty(tbh);
  750. get_bh(tbh);
  751. tbh->b_end_io = end_buffer_write_sync;
  752. submit_bh(WRITE, tbh);
  753. }
  754. /* Synchronize the mft mirror now if not @sync. */
  755. if (!sync && ni->mft_no < vol->mftmirr_size)
  756. ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
  757. /* Wait on i/o completion of buffers. */
  758. for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
  759. struct buffer_head *tbh = bhs[i_bhs];
  760. wait_on_buffer(tbh);
  761. if (unlikely(!buffer_uptodate(tbh))) {
  762. err = -EIO;
  763. /*
  764. * Set the buffer uptodate so the page and buffer
  765. * states do not become out of sync.
  766. */
  767. if (PageUptodate(page))
  768. set_buffer_uptodate(tbh);
  769. }
  770. }
  771. /* If @sync, now synchronize the mft mirror. */
  772. if (sync && ni->mft_no < vol->mftmirr_size)
  773. ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
  774. /* Remove the mst protection fixups again. */
  775. post_write_mst_fixup((NTFS_RECORD*)m);
  776. flush_dcache_mft_record_page(ni);
  777. if (unlikely(err)) {
  778. /* I/O error during writing. This is really bad! */
  779. ntfs_error(vol->sb, "I/O error while writing mft record "
  780. "0x%lx! Marking base inode as bad. You "
  781. "should unmount the volume and run chkdsk.",
  782. ni->mft_no);
  783. goto err_out;
  784. }
  785. done:
  786. ntfs_debug("Done.");
  787. return 0;
  788. cleanup_out:
  789. /* Clean the buffers. */
  790. for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
  791. clear_buffer_dirty(bhs[i_bhs]);
  792. err_out:
  793. /*
  794. * Current state: all buffers are clean, unlocked, and uptodate.
  795. * The caller should mark the base inode as bad so that no more i/o
  796. * happens. ->clear_inode() will still be invoked so all extent inodes
  797. * and other allocated memory will be freed.
  798. */
  799. if (err == -ENOMEM) {
  800. ntfs_error(vol->sb, "Not enough memory to write mft record. "
  801. "Redirtying so the write is retried later.");
  802. mark_mft_record_dirty(ni);
  803. err = 0;
  804. } else
  805. NVolSetErrors(vol);
  806. return err;
  807. }
  808. /**
  809. * ntfs_may_write_mft_record - check if an mft record may be written out
  810. * @vol: [IN] ntfs volume on which the mft record to check resides
  811. * @mft_no: [IN] mft record number of the mft record to check
  812. * @m: [IN] mapped mft record to check
  813. * @locked_ni: [OUT] caller has to unlock this ntfs inode if one is returned
  814. *
  815. * Check if the mapped (base or extent) mft record @m with mft record number
  816. * @mft_no belonging to the ntfs volume @vol may be written out. If necessary
  817. * and possible the ntfs inode of the mft record is locked and the base vfs
  818. * inode is pinned. The locked ntfs inode is then returned in @locked_ni. The
  819. * caller is responsible for unlocking the ntfs inode and unpinning the base
  820. * vfs inode.
  821. *
  822. * Return TRUE if the mft record may be written out and FALSE if not.
  823. *
  824. * The caller has locked the page and cleared the uptodate flag on it which
  825. * means that we can safely write out any dirty mft records that do not have
  826. * their inodes in icache as determined by ilookup5() as anyone
  827. * opening/creating such an inode would block when attempting to map the mft
  828. * record in read_cache_page() until we are finished with the write out.
  829. *
  830. * Here is a description of the tests we perform:
  831. *
  832. * If the inode is found in icache we know the mft record must be a base mft
  833. * record. If it is dirty, we do not write it and return FALSE as the vfs
  834. * inode write paths will result in the access times being updated which would
  835. * cause the base mft record to be redirtied and written out again. (We know
  836. * the access time update will modify the base mft record because Windows
  837. * chkdsk complains if the standard information attribute is not in the base
  838. * mft record.)
  839. *
  840. * If the inode is in icache and not dirty, we attempt to lock the mft record
  841. * and if we find the lock was already taken, it is not safe to write the mft
  842. * record and we return FALSE.
  843. *
  844. * If we manage to obtain the lock we have exclusive access to the mft record,
  845. * which also allows us safe writeout of the mft record. We then set
  846. * @locked_ni to the locked ntfs inode and return TRUE.
  847. *
  848. * Note we cannot just lock the mft record and sleep while waiting for the lock
  849. * because this would deadlock due to lock reversal (normally the mft record is
  850. * locked before the page is locked but we already have the page locked here
  851. * when we try to lock the mft record).
  852. *
  853. * If the inode is not in icache we need to perform further checks.
  854. *
  855. * If the mft record is not a FILE record or it is a base mft record, we can
  856. * safely write it and return TRUE.
  857. *
  858. * We now know the mft record is an extent mft record. We check if the inode
  859. * corresponding to its base mft record is in icache and obtain a reference to
  860. * it if it is. If it is not, we can safely write it and return TRUE.
  861. *
  862. * We now have the base inode for the extent mft record. We check if it has an
  863. * ntfs inode for the extent mft record attached and if not it is safe to write
  864. * the extent mft record and we return TRUE.
  865. *
  866. * The ntfs inode for the extent mft record is attached to the base inode so we
  867. * attempt to lock the extent mft record and if we find the lock was already
  868. * taken, it is not safe to write the extent mft record and we return FALSE.
  869. *
  870. * If we manage to obtain the lock we have exclusive access to the extent mft
  871. * record, which also allows us safe writeout of the extent mft record. We
  872. * set the ntfs inode of the extent mft record clean and then set @locked_ni to
  873. * the now locked ntfs inode and return TRUE.
  874. *
  875. * Note, the reason for actually writing dirty mft records here and not just
  876. * relying on the vfs inode dirty code paths is that we can have mft records
  877. * modified without them ever having actual inodes in memory. Also we can have
  878. * dirty mft records with clean ntfs inodes in memory. None of the described
  879. * cases would result in the dirty mft records being written out if we only
  880. * relied on the vfs inode dirty code paths. And these cases can really occur
  881. * during allocation of new mft records and in particular when the
  882. * initialized_size of the $MFT/$DATA attribute is extended and the new space
  883. * is initialized using ntfs_mft_record_format(). The clean inode can then
  884. * appear if the mft record is reused for a new inode before it got written
  885. * out.
  886. */
  887. BOOL ntfs_may_write_mft_record(ntfs_volume *vol, const unsigned long mft_no,
  888. const MFT_RECORD *m, ntfs_inode **locked_ni)
  889. {
  890. struct super_block *sb = vol->sb;
  891. struct inode *mft_vi = vol->mft_ino;
  892. struct inode *vi;
  893. ntfs_inode *ni, *eni, **extent_nis;
  894. int i;
  895. ntfs_attr na;
  896. ntfs_debug("Entering for inode 0x%lx.", mft_no);
  897. /*
  898. * Normally we do not return a locked inode so set @locked_ni to NULL.
  899. */
  900. BUG_ON(!locked_ni);
  901. *locked_ni = NULL;
  902. /*
  903. * Check if the inode corresponding to this mft record is in the VFS
  904. * inode cache and obtain a reference to it if it is.
  905. */
  906. ntfs_debug("Looking for inode 0x%lx in icache.", mft_no);
  907. na.mft_no = mft_no;
  908. na.name = NULL;
  909. na.name_len = 0;
  910. na.type = AT_UNUSED;
  911. /*
  912. * Optimize inode 0, i.e. $MFT itself, since we have it in memory and
  913. * we get here for it rather often.
  914. */
  915. if (!mft_no) {
  916. /* Balance the below iput(). */
  917. vi = igrab(mft_vi);
  918. BUG_ON(vi != mft_vi);
  919. } else {
  920. /*
  921. * Have to use ilookup5_nowait() since ilookup5() waits for the
  922. * inode lock which causes ntfs to deadlock when a concurrent
  923. * inode write via the inode dirty code paths and the page
  924. * dirty code path of the inode dirty code path when writing
  925. * $MFT occurs.
  926. */
  927. vi = ilookup5_nowait(sb, mft_no, (test_t)ntfs_test_inode, &na);
  928. }
  929. if (vi) {
  930. ntfs_debug("Base inode 0x%lx is in icache.", mft_no);
  931. /* The inode is in icache. */
  932. ni = NTFS_I(vi);
  933. /* Take a reference to the ntfs inode. */
  934. atomic_inc(&ni->count);
  935. /* If the inode is dirty, do not write this record. */
  936. if (NInoDirty(ni)) {
  937. ntfs_debug("Inode 0x%lx is dirty, do not write it.",
  938. mft_no);
  939. atomic_dec(&ni->count);
  940. iput(vi);
  941. return FALSE;
  942. }
  943. ntfs_debug("Inode 0x%lx is not dirty.", mft_no);
  944. /* The inode is not dirty, try to take the mft record lock. */
  945. if (unlikely(down_trylock(&ni->mrec_lock))) {
  946. ntfs_debug("Mft record 0x%lx is already locked, do "
  947. "not write it.", mft_no);
  948. atomic_dec(&ni->count);
  949. iput(vi);
  950. return FALSE;
  951. }
  952. ntfs_debug("Managed to lock mft record 0x%lx, write it.",
  953. mft_no);
  954. /*
  955. * The write has to occur while we hold the mft record lock so
  956. * return the locked ntfs inode.
  957. */
  958. *locked_ni = ni;
  959. return TRUE;
  960. }
  961. ntfs_debug("Inode 0x%lx is not in icache.", mft_no);
  962. /* The inode is not in icache. */
  963. /* Write the record if it is not a mft record (type "FILE"). */
  964. if (!ntfs_is_mft_record(m->magic)) {
  965. ntfs_debug("Mft record 0x%lx is not a FILE record, write it.",
  966. mft_no);
  967. return TRUE;
  968. }
  969. /* Write the mft record if it is a base inode. */
  970. if (!m->base_mft_record) {
  971. ntfs_debug("Mft record 0x%lx is a base record, write it.",
  972. mft_no);
  973. return TRUE;
  974. }
  975. /*
  976. * This is an extent mft record. Check if the inode corresponding to
  977. * its base mft record is in icache and obtain a reference to it if it
  978. * is.
  979. */
  980. na.mft_no = MREF_LE(m->base_mft_record);
  981. ntfs_debug("Mft record 0x%lx is an extent record. Looking for base "
  982. "inode 0x%lx in icache.", mft_no, na.mft_no);
  983. if (!na.mft_no) {
  984. /* Balance the below iput(). */
  985. vi = igrab(mft_vi);
  986. BUG_ON(vi != mft_vi);
  987. } else
  988. vi = ilookup5_nowait(sb, na.mft_no, (test_t)ntfs_test_inode,
  989. &na);
  990. if (!vi) {
  991. /*
  992. * The base inode is not in icache, write this extent mft
  993. * record.
  994. */
  995. ntfs_debug("Base inode 0x%lx is not in icache, write the "
  996. "extent record.", na.mft_no);
  997. return TRUE;
  998. }
  999. ntfs_debug("Base inode 0x%lx is in icache.", na.mft_no);
  1000. /*
  1001. * The base inode is in icache. Check if it has the extent inode
  1002. * corresponding to this extent mft record attached.
  1003. */
  1004. ni = NTFS_I(vi);
  1005. down(&ni->extent_lock);
  1006. if (ni->nr_extents <= 0) {
  1007. /*
  1008. * The base inode has no attached extent inodes, write this
  1009. * extent mft record.
  1010. */
  1011. up(&ni->extent_lock);
  1012. iput(vi);
  1013. ntfs_debug("Base inode 0x%lx has no attached extent inodes, "
  1014. "write the extent record.", na.mft_no);
  1015. return TRUE;
  1016. }
  1017. /* Iterate over the attached extent inodes. */
  1018. extent_nis = ni->ext.extent_ntfs_inos;
  1019. for (eni = NULL, i = 0; i < ni->nr_extents; ++i) {
  1020. if (mft_no == extent_nis[i]->mft_no) {
  1021. /*
  1022. * Found the extent inode corresponding to this extent
  1023. * mft record.
  1024. */
  1025. eni = extent_nis[i];
  1026. break;
  1027. }
  1028. }
  1029. /*
  1030. * If the extent inode was not attached to the base inode, write this
  1031. * extent mft record.
  1032. */
  1033. if (!eni) {
  1034. up(&ni->extent_lock);
  1035. iput(vi);
  1036. ntfs_debug("Extent inode 0x%lx is not attached to its base "
  1037. "inode 0x%lx, write the extent record.",
  1038. mft_no, na.mft_no);
  1039. return TRUE;
  1040. }
  1041. ntfs_debug("Extent inode 0x%lx is attached to its base inode 0x%lx.",
  1042. mft_no, na.mft_no);
  1043. /* Take a reference to the extent ntfs inode. */
  1044. atomic_inc(&eni->count);
  1045. up(&ni->extent_lock);
  1046. /*
  1047. * Found the extent inode coresponding to this extent mft record.
  1048. * Try to take the mft record lock.
  1049. */
  1050. if (unlikely(down_trylock(&eni->mrec_lock))) {
  1051. atomic_dec(&eni->count);
  1052. iput(vi);
  1053. ntfs_debug("Extent mft record 0x%lx is already locked, do "
  1054. "not write it.", mft_no);
  1055. return FALSE;
  1056. }
  1057. ntfs_debug("Managed to lock extent mft record 0x%lx, write it.",
  1058. mft_no);
  1059. if (NInoTestClearDirty(eni))
  1060. ntfs_debug("Extent inode 0x%lx is dirty, marking it clean.",
  1061. mft_no);
  1062. /*
  1063. * The write has to occur while we hold the mft record lock so return
  1064. * the locked extent ntfs inode.
  1065. */
  1066. *locked_ni = eni;
  1067. return TRUE;
  1068. }
  1069. static const char *es = " Leaving inconsistent metadata. Unmount and run "
  1070. "chkdsk.";
  1071. /**
  1072. * ntfs_mft_bitmap_find_and_alloc_free_rec_nolock - see name
  1073. * @vol: volume on which to search for a free mft record
  1074. * @base_ni: open base inode if allocating an extent mft record or NULL
  1075. *
  1076. * Search for a free mft record in the mft bitmap attribute on the ntfs volume
  1077. * @vol.
  1078. *
  1079. * If @base_ni is NULL start the search at the default allocator position.
  1080. *
  1081. * If @base_ni is not NULL start the search at the mft record after the base
  1082. * mft record @base_ni.
  1083. *
  1084. * Return the free mft record on success and -errno on error. An error code of
  1085. * -ENOSPC means that there are no free mft records in the currently
  1086. * initialized mft bitmap.
  1087. *
  1088. * Locking: Caller must hold vol->mftbmp_lock for writing.
  1089. */
  1090. static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
  1091. ntfs_inode *base_ni)
  1092. {
  1093. s64 pass_end, ll, data_pos, pass_start, ofs, bit;
  1094. unsigned long flags;
  1095. struct address_space *mftbmp_mapping;
  1096. u8 *buf, *byte;
  1097. struct page *page;
  1098. unsigned int page_ofs, size;
  1099. u8 pass, b;
  1100. ntfs_debug("Searching for free mft record in the currently "
  1101. "initialized mft bitmap.");
  1102. mftbmp_mapping = vol->mftbmp_ino->i_mapping;
  1103. /*
  1104. * Set the end of the pass making sure we do not overflow the mft
  1105. * bitmap.
  1106. */
  1107. read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags);
  1108. pass_end = NTFS_I(vol->mft_ino)->allocated_size >>
  1109. vol->mft_record_size_bits;
  1110. read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags);
  1111. read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
  1112. ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3;
  1113. read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
  1114. if (pass_end > ll)
  1115. pass_end = ll;
  1116. pass = 1;
  1117. if (!base_ni)
  1118. data_pos = vol->mft_data_pos;
  1119. else
  1120. data_pos = base_ni->mft_no + 1;
  1121. if (data_pos < 24)
  1122. data_pos = 24;
  1123. if (data_pos >= pass_end) {
  1124. data_pos = 24;
  1125. pass = 2;
  1126. /* This happens on a freshly formatted volume. */
  1127. if (data_pos >= pass_end)
  1128. return -ENOSPC;
  1129. }
  1130. pass_start = data_pos;
  1131. ntfs_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
  1132. "pass_end 0x%llx, data_pos 0x%llx.", pass,
  1133. (long long)pass_start, (long long)pass_end,
  1134. (long long)data_pos);
  1135. /* Loop until a free mft record is found. */
  1136. for (; pass <= 2;) {
  1137. /* Cap size to pass_end. */
  1138. ofs = data_pos >> 3;
  1139. page_ofs = ofs & ~PAGE_CACHE_MASK;
  1140. size = PAGE_CACHE_SIZE - page_ofs;
  1141. ll = ((pass_end + 7) >> 3) - ofs;
  1142. if (size > ll)
  1143. size = ll;
  1144. size <<= 3;
  1145. /*
  1146. * If we are still within the active pass, search the next page
  1147. * for a zero bit.
  1148. */
  1149. if (size) {
  1150. page = ntfs_map_page(mftbmp_mapping,
  1151. ofs >> PAGE_CACHE_SHIFT);
  1152. if (unlikely(IS_ERR(page))) {
  1153. ntfs_error(vol->sb, "Failed to read mft "
  1154. "bitmap, aborting.");
  1155. return PTR_ERR(page);
  1156. }
  1157. buf = (u8*)page_address(page) + page_ofs;
  1158. bit = data_pos & 7;
  1159. data_pos &= ~7ull;
  1160. ntfs_debug("Before inner for loop: size 0x%x, "
  1161. "data_pos 0x%llx, bit 0x%llx", size,
  1162. (long long)data_pos, (long long)bit);
  1163. for (; bit < size && data_pos + bit < pass_end;
  1164. bit &= ~7ull, bit += 8) {
  1165. byte = buf + (bit >> 3);
  1166. if (*byte == 0xff)
  1167. continue;
  1168. b = ffz((unsigned long)*byte);
  1169. if (b < 8 && b >= (bit & 7)) {
  1170. ll = data_pos + (bit & ~7ull) + b;
  1171. if (unlikely(ll > (1ll << 32))) {
  1172. ntfs_unmap_page(page);
  1173. return -ENOSPC;
  1174. }
  1175. *byte |= 1 << b;
  1176. flush_dcache_page(page);
  1177. set_page_dirty(page);
  1178. ntfs_unmap_page(page);
  1179. ntfs_debug("Done. (Found and "
  1180. "allocated mft record "
  1181. "0x%llx.)",
  1182. (long long)ll);
  1183. return ll;
  1184. }
  1185. }
  1186. ntfs_debug("After inner for loop: size 0x%x, "
  1187. "data_pos 0x%llx, bit 0x%llx", size,
  1188. (long long)data_pos, (long long)bit);
  1189. data_pos += size;
  1190. ntfs_unmap_page(page);
  1191. /*
  1192. * If the end of the pass has not been reached yet,
  1193. * continue searching the mft bitmap for a zero bit.
  1194. */
  1195. if (data_pos < pass_end)
  1196. continue;
  1197. }
  1198. /* Do the next pass. */
  1199. if (++pass == 2) {
  1200. /*
  1201. * Starting the second pass, in which we scan the first
  1202. * part of the zone which we omitted earlier.
  1203. */
  1204. pass_end = pass_start;
  1205. data_pos = pass_start = 24;
  1206. ntfs_debug("pass %i, pass_start 0x%llx, pass_end "
  1207. "0x%llx.", pass, (long long)pass_start,
  1208. (long long)pass_end);
  1209. if (data_pos >= pass_end)
  1210. break;
  1211. }
  1212. }
  1213. /* No free mft records in currently initialized mft bitmap. */
  1214. ntfs_debug("Done. (No free mft records left in currently initialized "
  1215. "mft bitmap.)");
  1216. return -ENOSPC;
  1217. }
  1218. /**
  1219. * ntfs_mft_bitmap_extend_allocation_nolock - extend mft bitmap by a cluster
  1220. * @vol: volume on which to extend the mft bitmap attribute
  1221. *
  1222. * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster.
  1223. *
  1224. * Note: Only changes allocated_size, i.e. does not touch initialized_size or
  1225. * data_size.
  1226. *
  1227. * Return 0 on success and -errno on error.
  1228. *
  1229. * Locking: - Caller must hold vol->mftbmp_lock for writing.
  1230. * - This function takes NTFS_I(vol->mftbmp_ino)->runlist.lock for
  1231. * writing and releases it before returning.
  1232. * - This function takes vol->lcnbmp_lock for writing and releases it
  1233. * before returning.
  1234. */
  1235. static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
  1236. {
  1237. LCN lcn;
  1238. s64 ll;
  1239. unsigned long flags;
  1240. struct page *page;
  1241. ntfs_inode *mft_ni, *mftbmp_ni;
  1242. runlist_element *rl, *rl2 = NULL;
  1243. ntfs_attr_search_ctx *ctx = NULL;
  1244. MFT_RECORD *mrec;
  1245. ATTR_RECORD *a = NULL;
  1246. int ret, mp_size;
  1247. u32 old_alen = 0;
  1248. u8 *b, tb;
  1249. struct {
  1250. u8 added_cluster:1;
  1251. u8 added_run:1;
  1252. u8 mp_rebuilt:1;
  1253. } status = { 0, 0, 0 };
  1254. ntfs_debug("Extending mft bitmap allocation.");
  1255. mft_ni = NTFS_I(vol->mft_ino);
  1256. mftbmp_ni = NTFS_I(vol->mftbmp_ino);
  1257. /*
  1258. * Determine the last lcn of the mft bitmap. The allocated size of the
  1259. * mft bitmap cannot be zero so we are ok to do this.
  1260. */
  1261. down_write(&mftbmp_ni->runlist.lock);
  1262. read_lock_irqsave(&mftbmp_ni->size_lock, flags);
  1263. ll = mftbmp_ni->allocated_size;
  1264. read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  1265. rl = ntfs_attr_find_vcn_nolock(mftbmp_ni,
  1266. (ll - 1) >> vol->cluster_size_bits, TRUE);
  1267. if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
  1268. up_write(&mftbmp_ni->runlist.lock);
  1269. ntfs_error(vol->sb, "Failed to determine last allocated "
  1270. "cluster of mft bitmap attribute.");
  1271. if (!IS_ERR(rl))
  1272. ret = -EIO;
  1273. else
  1274. ret = PTR_ERR(rl);
  1275. return ret;
  1276. }
  1277. lcn = rl->lcn + rl->length;
  1278. ntfs_debug("Last lcn of mft bitmap attribute is 0x%llx.",
  1279. (long long)lcn);
  1280. /*
  1281. * Attempt to get the cluster following the last allocated cluster by
  1282. * hand as it may be in the MFT zone so the allocator would not give it
  1283. * to us.
  1284. */
  1285. ll = lcn >> 3;
  1286. page = ntfs_map_page(vol->lcnbmp_ino->i_mapping,
  1287. ll >> PAGE_CACHE_SHIFT);
  1288. if (IS_ERR(page)) {
  1289. up_write(&mftbmp_ni->runlist.lock);
  1290. ntfs_error(vol->sb, "Failed to read from lcn bitmap.");
  1291. return PTR_ERR(page);
  1292. }
  1293. b = (u8*)page_address(page) + (ll & ~PAGE_CACHE_MASK);
  1294. tb = 1 << (lcn & 7ull);
  1295. down_write(&vol->lcnbmp_lock);
  1296. if (*b != 0xff && !(*b & tb)) {
  1297. /* Next cluster is free, allocate it. */
  1298. *b |= tb;
  1299. flush_dcache_page(page);
  1300. set_page_dirty(page);
  1301. up_write(&vol->lcnbmp_lock);
  1302. ntfs_unmap_page(page);
  1303. /* Update the mft bitmap runlist. */
  1304. rl->length++;
  1305. rl[1].vcn++;
  1306. status.added_cluster = 1;
  1307. ntfs_debug("Appending one cluster to mft bitmap.");
  1308. } else {
  1309. up_write(&vol->lcnbmp_lock);
  1310. ntfs_unmap_page(page);
  1311. /* Allocate a cluster from the DATA_ZONE. */
  1312. rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE);
  1313. if (IS_ERR(rl2)) {
  1314. up_write(&mftbmp_ni->runlist.lock);
  1315. ntfs_error(vol->sb, "Failed to allocate a cluster for "
  1316. "the mft bitmap.");
  1317. return PTR_ERR(rl2);
  1318. }
  1319. rl = ntfs_runlists_merge(mftbmp_ni->runlist.rl, rl2);
  1320. if (IS_ERR(rl)) {
  1321. up_write(&mftbmp_ni->runlist.lock);
  1322. ntfs_error(vol->sb, "Failed to merge runlists for mft "
  1323. "bitmap.");
  1324. if (ntfs_cluster_free_from_rl(vol, rl2)) {
  1325. ntfs_error(vol->sb, "Failed to dealocate "
  1326. "allocated cluster.%s", es);
  1327. NVolSetErrors(vol);
  1328. }
  1329. ntfs_free(rl2);
  1330. return PTR_ERR(rl);
  1331. }
  1332. mftbmp_ni->runlist.rl = rl;
  1333. status.added_run = 1;
  1334. ntfs_debug("Adding one run to mft bitmap.");
  1335. /* Find the last run in the new runlist. */
  1336. for (; rl[1].length; rl++)
  1337. ;
  1338. }
  1339. /*
  1340. * Update the attribute record as well. Note: @rl is the last
  1341. * (non-terminator) runlist element of mft bitmap.
  1342. */
  1343. mrec = map_mft_record(mft_ni);
  1344. if (IS_ERR(mrec)) {
  1345. ntfs_error(vol->sb, "Failed to map mft record.");
  1346. ret = PTR_ERR(mrec);
  1347. goto undo_alloc;
  1348. }
  1349. ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
  1350. if (unlikely(!ctx)) {
  1351. ntfs_error(vol->sb, "Failed to get search context.");
  1352. ret = -ENOMEM;
  1353. goto undo_alloc;
  1354. }
  1355. ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
  1356. mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
  1357. 0, ctx);
  1358. if (unlikely(ret)) {
  1359. ntfs_error(vol->sb, "Failed to find last attribute extent of "
  1360. "mft bitmap attribute.");
  1361. if (ret == -ENOENT)
  1362. ret = -EIO;
  1363. goto undo_alloc;
  1364. }
  1365. a = ctx->attr;
  1366. ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
  1367. /* Search back for the previous last allocated cluster of mft bitmap. */
  1368. for (rl2 = rl; rl2 > mftbmp_ni->runlist.rl; rl2--) {
  1369. if (ll >= rl2->vcn)
  1370. break;
  1371. }
  1372. BUG_ON(ll < rl2->vcn);
  1373. BUG_ON(ll >= rl2->vcn + rl2->length);
  1374. /* Get the size for the new mapping pairs array for this extent. */
  1375. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
  1376. if (unlikely(mp_size <= 0)) {
  1377. ntfs_error(vol->sb, "Get size for mapping pairs failed for "
  1378. "mft bitmap attribute extent.");
  1379. ret = mp_size;
  1380. if (!ret)
  1381. ret = -EIO;
  1382. goto undo_alloc;
  1383. }
  1384. /* Expand the attribute record if necessary. */
  1385. old_alen = le32_to_cpu(a->length);
  1386. ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
  1387. le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
  1388. if (unlikely(ret)) {
  1389. if (ret != -ENOSPC) {
  1390. ntfs_error(vol->sb, "Failed to resize attribute "
  1391. "record for mft bitmap attribute.");
  1392. goto undo_alloc;
  1393. }
  1394. // TODO: Deal with this by moving this extent to a new mft
  1395. // record or by starting a new extent in a new mft record or by
  1396. // moving other attributes out of this mft record.
  1397. // Note: It will need to be a special mft record and if none of
  1398. // those are available it gets rather complicated...
  1399. ntfs_error(vol->sb, "Not enough space in this mft record to "
  1400. "accomodate extended mft bitmap attribute "
  1401. "extent. Cannot handle this yet.");
  1402. ret = -EOPNOTSUPP;
  1403. goto undo_alloc;
  1404. }
  1405. status.mp_rebuilt = 1;
  1406. /* Generate the mapping pairs array directly into the attr record. */
  1407. ret = ntfs_mapping_pairs_build(vol, (u8*)a +
  1408. le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
  1409. mp_size, rl2, ll, -1, NULL);
  1410. if (unlikely(ret)) {
  1411. ntfs_error(vol->sb, "Failed to build mapping pairs array for "
  1412. "mft bitmap attribute.");
  1413. goto undo_alloc;
  1414. }
  1415. /* Update the highest_vcn. */
  1416. a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
  1417. /*
  1418. * We now have extended the mft bitmap allocated_size by one cluster.
  1419. * Reflect this in the ntfs_inode structure and the attribute record.
  1420. */
  1421. if (a->data.non_resident.lowest_vcn) {
  1422. /*
  1423. * We are not in the first attribute extent, switch to it, but
  1424. * first ensure the changes will make it to disk later.
  1425. */
  1426. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1427. mark_mft_record_dirty(ctx->ntfs_ino);
  1428. ntfs_attr_reinit_search_ctx(ctx);
  1429. ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
  1430. mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL,
  1431. 0, ctx);
  1432. if (unlikely(ret)) {
  1433. ntfs_error(vol->sb, "Failed to find first attribute "
  1434. "extent of mft bitmap attribute.");
  1435. goto restore_undo_alloc;
  1436. }
  1437. a = ctx->attr;
  1438. }
  1439. write_lock_irqsave(&mftbmp_ni->size_lock, flags);
  1440. mftbmp_ni->allocated_size += vol->cluster_size;
  1441. a->data.non_resident.allocated_size =
  1442. cpu_to_sle64(mftbmp_ni->allocated_size);
  1443. write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  1444. /* Ensure the changes make it to disk. */
  1445. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1446. mark_mft_record_dirty(ctx->ntfs_ino);
  1447. ntfs_attr_put_search_ctx(ctx);
  1448. unmap_mft_record(mft_ni);
  1449. up_write(&mftbmp_ni->runlist.lock);
  1450. ntfs_debug("Done.");
  1451. return 0;
  1452. restore_undo_alloc:
  1453. ntfs_attr_reinit_search_ctx(ctx);
  1454. if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
  1455. mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
  1456. 0, ctx)) {
  1457. ntfs_error(vol->sb, "Failed to find last attribute extent of "
  1458. "mft bitmap attribute.%s", es);
  1459. write_lock_irqsave(&mftbmp_ni->size_lock, flags);
  1460. mftbmp_ni->allocated_size += vol->cluster_size;
  1461. write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  1462. ntfs_attr_put_search_ctx(ctx);
  1463. unmap_mft_record(mft_ni);
  1464. up_write(&mftbmp_ni->runlist.lock);
  1465. /*
  1466. * The only thing that is now wrong is ->allocated_size of the
  1467. * base attribute extent which chkdsk should be able to fix.
  1468. */
  1469. NVolSetErrors(vol);
  1470. return ret;
  1471. }
  1472. a = ctx->attr;
  1473. a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 2);
  1474. undo_alloc:
  1475. if (status.added_cluster) {
  1476. /* Truncate the last run in the runlist by one cluster. */
  1477. rl->length--;
  1478. rl[1].vcn--;
  1479. } else if (status.added_run) {
  1480. lcn = rl->lcn;
  1481. /* Remove the last run from the runlist. */
  1482. rl->lcn = rl[1].lcn;
  1483. rl->length = 0;
  1484. }
  1485. /* Deallocate the cluster. */
  1486. down_write(&vol->lcnbmp_lock);
  1487. if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) {
  1488. ntfs_error(vol->sb, "Failed to free allocated cluster.%s", es);
  1489. NVolSetErrors(vol);
  1490. }
  1491. up_write(&vol->lcnbmp_lock);
  1492. if (status.mp_rebuilt) {
  1493. if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
  1494. a->data.non_resident.mapping_pairs_offset),
  1495. old_alen - le16_to_cpu(
  1496. a->data.non_resident.mapping_pairs_offset),
  1497. rl2, ll, -1, NULL)) {
  1498. ntfs_error(vol->sb, "Failed to restore mapping pairs "
  1499. "array.%s", es);
  1500. NVolSetErrors(vol);
  1501. }
  1502. if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
  1503. ntfs_error(vol->sb, "Failed to restore attribute "
  1504. "record.%s", es);
  1505. NVolSetErrors(vol);
  1506. }
  1507. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1508. mark_mft_record_dirty(ctx->ntfs_ino);
  1509. }
  1510. if (ctx)
  1511. ntfs_attr_put_search_ctx(ctx);
  1512. if (!IS_ERR(mrec))
  1513. unmap_mft_record(mft_ni);
  1514. up_write(&mftbmp_ni->runlist.lock);
  1515. return ret;
  1516. }
  1517. /**
  1518. * ntfs_mft_bitmap_extend_initialized_nolock - extend mftbmp initialized data
  1519. * @vol: volume on which to extend the mft bitmap attribute
  1520. *
  1521. * Extend the initialized portion of the mft bitmap attribute on the ntfs
  1522. * volume @vol by 8 bytes.
  1523. *
  1524. * Note: Only changes initialized_size and data_size, i.e. requires that
  1525. * allocated_size is big enough to fit the new initialized_size.
  1526. *
  1527. * Return 0 on success and -error on error.
  1528. *
  1529. * Locking: Caller must hold vol->mftbmp_lock for writing.
  1530. */
  1531. static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
  1532. {
  1533. s64 old_data_size, old_initialized_size;
  1534. unsigned long flags;
  1535. struct inode *mftbmp_vi;
  1536. ntfs_inode *mft_ni, *mftbmp_ni;
  1537. ntfs_attr_search_ctx *ctx;
  1538. MFT_RECORD *mrec;
  1539. ATTR_RECORD *a;
  1540. int ret;
  1541. ntfs_debug("Extending mft bitmap initiailized (and data) size.");
  1542. mft_ni = NTFS_I(vol->mft_ino);
  1543. mftbmp_vi = vol->mftbmp_ino;
  1544. mftbmp_ni = NTFS_I(mftbmp_vi);
  1545. /* Get the attribute record. */
  1546. mrec = map_mft_record(mft_ni);
  1547. if (IS_ERR(mrec)) {
  1548. ntfs_error(vol->sb, "Failed to map mft record.");
  1549. return PTR_ERR(mrec);
  1550. }
  1551. ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
  1552. if (unlikely(!ctx)) {
  1553. ntfs_error(vol->sb, "Failed to get search context.");
  1554. ret = -ENOMEM;
  1555. goto unm_err_out;
  1556. }
  1557. ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
  1558. mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx);
  1559. if (unlikely(ret)) {
  1560. ntfs_error(vol->sb, "Failed to find first attribute extent of "
  1561. "mft bitmap attribute.");
  1562. if (ret == -ENOENT)
  1563. ret = -EIO;
  1564. goto put_err_out;
  1565. }
  1566. a = ctx->attr;
  1567. write_lock_irqsave(&mftbmp_ni->size_lock, flags);
  1568. old_data_size = i_size_read(mftbmp_vi);
  1569. old_initialized_size = mftbmp_ni->initialized_size;
  1570. /*
  1571. * We can simply update the initialized_size before filling the space
  1572. * with zeroes because the caller is holding the mft bitmap lock for
  1573. * writing which ensures that no one else is trying to access the data.
  1574. */
  1575. mftbmp_ni->initialized_size += 8;
  1576. a->data.non_resident.initialized_size =
  1577. cpu_to_sle64(mftbmp_ni->initialized_size);
  1578. if (mftbmp_ni->initialized_size > old_data_size) {
  1579. i_size_write(mftbmp_vi, mftbmp_ni->initialized_size);
  1580. a->data.non_resident.data_size =
  1581. cpu_to_sle64(mftbmp_ni->initialized_size);
  1582. }
  1583. write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  1584. /* Ensure the changes make it to disk. */
  1585. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1586. mark_mft_record_dirty(ctx->ntfs_ino);
  1587. ntfs_attr_put_search_ctx(ctx);
  1588. unmap_mft_record(mft_ni);
  1589. /* Initialize the mft bitmap attribute value with zeroes. */
  1590. ret = ntfs_attr_set(mftbmp_ni, old_initialized_size, 8, 0);
  1591. if (likely(!ret)) {
  1592. ntfs_debug("Done. (Wrote eight initialized bytes to mft "
  1593. "bitmap.");
  1594. return 0;
  1595. }
  1596. ntfs_error(vol->sb, "Failed to write to mft bitmap.");
  1597. /* Try to recover from the error. */
  1598. mrec = map_mft_record(mft_ni);
  1599. if (IS_ERR(mrec)) {
  1600. ntfs_error(vol->sb, "Failed to map mft record.%s", es);
  1601. NVolSetErrors(vol);
  1602. return ret;
  1603. }
  1604. ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
  1605. if (unlikely(!ctx)) {
  1606. ntfs_error(vol->sb, "Failed to get search context.%s", es);
  1607. NVolSetErrors(vol);
  1608. goto unm_err_out;
  1609. }
  1610. if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
  1611. mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
  1612. ntfs_error(vol->sb, "Failed to find first attribute extent of "
  1613. "mft bitmap attribute.%s", es);
  1614. NVolSetErrors(vol);
  1615. put_err_out:
  1616. ntfs_attr_put_search_ctx(ctx);
  1617. unm_err_out:
  1618. unmap_mft_record(mft_ni);
  1619. goto err_out;
  1620. }
  1621. a = ctx->attr;
  1622. write_lock_irqsave(&mftbmp_ni->size_lock, flags);
  1623. mftbmp_ni->initialized_size = old_initialized_size;
  1624. a->data.non_resident.initialized_size =
  1625. cpu_to_sle64(old_initialized_size);
  1626. if (i_size_read(mftbmp_vi) != old_data_size) {
  1627. i_size_write(mftbmp_vi, old_data_size);
  1628. a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
  1629. }
  1630. write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  1631. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1632. mark_mft_record_dirty(ctx->ntfs_ino);
  1633. ntfs_attr_put_search_ctx(ctx);
  1634. unmap_mft_record(mft_ni);
  1635. #ifdef DEBUG
  1636. read_lock_irqsave(&mftbmp_ni->size_lock, flags);
  1637. ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
  1638. "data_size 0x%llx, initialized_size 0x%llx.",
  1639. (long long)mftbmp_ni->allocated_size,
  1640. (long long)i_size_read(mftbmp_vi),
  1641. (long long)mftbmp_ni->initialized_size);
  1642. read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  1643. #endif /* DEBUG */
  1644. err_out:
  1645. return ret;
  1646. }
  1647. /**
  1648. * ntfs_mft_data_extend_allocation_nolock - extend mft data attribute
  1649. * @vol: volume on which to extend the mft data attribute
  1650. *
  1651. * Extend the mft data attribute on the ntfs volume @vol by 16 mft records
  1652. * worth of clusters or if not enough space for this by one mft record worth
  1653. * of clusters.
  1654. *
  1655. * Note: Only changes allocated_size, i.e. does not touch initialized_size or
  1656. * data_size.
  1657. *
  1658. * Return 0 on success and -errno on error.
  1659. *
  1660. * Locking: - Caller must hold vol->mftbmp_lock for writing.
  1661. * - This function takes NTFS_I(vol->mft_ino)->runlist.lock for
  1662. * writing and releases it before returning.
  1663. * - This function calls functions which take vol->lcnbmp_lock for
  1664. * writing and release it before returning.
  1665. */
  1666. static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
  1667. {
  1668. LCN lcn;
  1669. VCN old_last_vcn;
  1670. s64 min_nr, nr, ll;
  1671. unsigned long flags;
  1672. ntfs_inode *mft_ni;
  1673. runlist_element *rl, *rl2;
  1674. ntfs_attr_search_ctx *ctx = NULL;
  1675. MFT_RECORD *mrec;
  1676. ATTR_RECORD *a = NULL;
  1677. int ret, mp_size;
  1678. u32 old_alen = 0;
  1679. BOOL mp_rebuilt = FALSE;
  1680. ntfs_debug("Extending mft data allocation.");
  1681. mft_ni = NTFS_I(vol->mft_ino);
  1682. /*
  1683. * Determine the preferred allocation location, i.e. the last lcn of
  1684. * the mft data attribute. The allocated size of the mft data
  1685. * attribute cannot be zero so we are ok to do this.
  1686. */
  1687. down_write(&mft_ni->runlist.lock);
  1688. read_lock_irqsave(&mft_ni->size_lock, flags);
  1689. ll = mft_ni->allocated_size;
  1690. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  1691. rl = ntfs_attr_find_vcn_nolock(mft_ni,
  1692. (ll - 1) >> vol->cluster_size_bits, TRUE);
  1693. if (unlikely(IS_ERR(rl) || !rl->length || rl->lcn < 0)) {
  1694. up_write(&mft_ni->runlist.lock);
  1695. ntfs_error(vol->sb, "Failed to determine last allocated "
  1696. "cluster of mft data attribute.");
  1697. if (!IS_ERR(rl))
  1698. ret = -EIO;
  1699. else
  1700. ret = PTR_ERR(rl);
  1701. return ret;
  1702. }
  1703. lcn = rl->lcn + rl->length;
  1704. ntfs_debug("Last lcn of mft data attribute is 0x%llx.", (long long)lcn);
  1705. /* Minimum allocation is one mft record worth of clusters. */
  1706. min_nr = vol->mft_record_size >> vol->cluster_size_bits;
  1707. if (!min_nr)
  1708. min_nr = 1;
  1709. /* Want to allocate 16 mft records worth of clusters. */
  1710. nr = vol->mft_record_size << 4 >> vol->cluster_size_bits;
  1711. if (!nr)
  1712. nr = min_nr;
  1713. /* Ensure we do not go above 2^32-1 mft records. */
  1714. read_lock_irqsave(&mft_ni->size_lock, flags);
  1715. ll = mft_ni->allocated_size;
  1716. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  1717. if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
  1718. vol->mft_record_size_bits >= (1ll << 32))) {
  1719. nr = min_nr;
  1720. if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
  1721. vol->mft_record_size_bits >= (1ll << 32))) {
  1722. ntfs_warning(vol->sb, "Cannot allocate mft record "
  1723. "because the maximum number of inodes "
  1724. "(2^32) has already been reached.");
  1725. up_write(&mft_ni->runlist.lock);
  1726. return -ENOSPC;
  1727. }
  1728. }
  1729. ntfs_debug("Trying mft data allocation with %s cluster count %lli.",
  1730. nr > min_nr ? "default" : "minimal", (long long)nr);
  1731. old_last_vcn = rl[1].vcn;
  1732. do {
  1733. rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE);
  1734. if (likely(!IS_ERR(rl2)))
  1735. break;
  1736. if (PTR_ERR(rl2) != -ENOSPC || nr == min_nr) {
  1737. ntfs_error(vol->sb, "Failed to allocate the minimal "
  1738. "number of clusters (%lli) for the "
  1739. "mft data attribute.", (long long)nr);
  1740. up_write(&mft_ni->runlist.lock);
  1741. return PTR_ERR(rl2);
  1742. }
  1743. /*
  1744. * There is not enough space to do the allocation, but there
  1745. * might be enough space to do a minimal allocation so try that
  1746. * before failing.
  1747. */
  1748. nr = min_nr;
  1749. ntfs_debug("Retrying mft data allocation with minimal cluster "
  1750. "count %lli.", (long long)nr);
  1751. } while (1);
  1752. rl = ntfs_runlists_merge(mft_ni->runlist.rl, rl2);
  1753. if (IS_ERR(rl)) {
  1754. up_write(&mft_ni->runlist.lock);
  1755. ntfs_error(vol->sb, "Failed to merge runlists for mft data "
  1756. "attribute.");
  1757. if (ntfs_cluster_free_from_rl(vol, rl2)) {
  1758. ntfs_error(vol->sb, "Failed to dealocate clusters "
  1759. "from the mft data attribute.%s", es);
  1760. NVolSetErrors(vol);
  1761. }
  1762. ntfs_free(rl2);
  1763. return PTR_ERR(rl);
  1764. }
  1765. mft_ni->runlist.rl = rl;
  1766. ntfs_debug("Allocated %lli clusters.", (long long)nr);
  1767. /* Find the last run in the new runlist. */
  1768. for (; rl[1].length; rl++)
  1769. ;
  1770. /* Update the attribute record as well. */
  1771. mrec = map_mft_record(mft_ni);
  1772. if (IS_ERR(mrec)) {
  1773. ntfs_error(vol->sb, "Failed to map mft record.");
  1774. ret = PTR_ERR(mrec);
  1775. goto undo_alloc;
  1776. }
  1777. ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
  1778. if (unlikely(!ctx)) {
  1779. ntfs_error(vol->sb, "Failed to get search context.");
  1780. ret = -ENOMEM;
  1781. goto undo_alloc;
  1782. }
  1783. ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
  1784. CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx);
  1785. if (unlikely(ret)) {
  1786. ntfs_error(vol->sb, "Failed to find last attribute extent of "
  1787. "mft data attribute.");
  1788. if (ret == -ENOENT)
  1789. ret = -EIO;
  1790. goto undo_alloc;
  1791. }
  1792. a = ctx->attr;
  1793. ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
  1794. /* Search back for the previous last allocated cluster of mft bitmap. */
  1795. for (rl2 = rl; rl2 > mft_ni->runlist.rl; rl2--) {
  1796. if (ll >= rl2->vcn)
  1797. break;
  1798. }
  1799. BUG_ON(ll < rl2->vcn);
  1800. BUG_ON(ll >= rl2->vcn + rl2->length);
  1801. /* Get the size for the new mapping pairs array for this extent. */
  1802. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
  1803. if (unlikely(mp_size <= 0)) {
  1804. ntfs_error(vol->sb, "Get size for mapping pairs failed for "
  1805. "mft data attribute extent.");
  1806. ret = mp_size;
  1807. if (!ret)
  1808. ret = -EIO;
  1809. goto undo_alloc;
  1810. }
  1811. /* Expand the attribute record if necessary. */
  1812. old_alen = le32_to_cpu(a->length);
  1813. ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
  1814. le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
  1815. if (unlikely(ret)) {
  1816. if (ret != -ENOSPC) {
  1817. ntfs_error(vol->sb, "Failed to resize attribute "
  1818. "record for mft data attribute.");
  1819. goto undo_alloc;
  1820. }
  1821. // TODO: Deal with this by moving this extent to a new mft
  1822. // record or by starting a new extent in a new mft record or by
  1823. // moving other attributes out of this mft record.
  1824. // Note: Use the special reserved mft records and ensure that
  1825. // this extent is not required to find the mft record in
  1826. // question. If no free special records left we would need to
  1827. // move an existing record away, insert ours in its place, and
  1828. // then place the moved record into the newly allocated space
  1829. // and we would then need to update all references to this mft
  1830. // record appropriately. This is rather complicated...
  1831. ntfs_error(vol->sb, "Not enough space in this mft record to "
  1832. "accomodate extended mft data attribute "
  1833. "extent. Cannot handle this yet.");
  1834. ret = -EOPNOTSUPP;
  1835. goto undo_alloc;
  1836. }
  1837. mp_rebuilt = TRUE;
  1838. /* Generate the mapping pairs array directly into the attr record. */
  1839. ret = ntfs_mapping_pairs_build(vol, (u8*)a +
  1840. le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
  1841. mp_size, rl2, ll, -1, NULL);
  1842. if (unlikely(ret)) {
  1843. ntfs_error(vol->sb, "Failed to build mapping pairs array of "
  1844. "mft data attribute.");
  1845. goto undo_alloc;
  1846. }
  1847. /* Update the highest_vcn. */
  1848. a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
  1849. /*
  1850. * We now have extended the mft data allocated_size by nr clusters.
  1851. * Reflect this in the ntfs_inode structure and the attribute record.
  1852. * @rl is the last (non-terminator) runlist element of mft data
  1853. * attribute.
  1854. */
  1855. if (a->data.non_resident.lowest_vcn) {
  1856. /*
  1857. * We are not in the first attribute extent, switch to it, but
  1858. * first ensure the changes will make it to disk later.
  1859. */
  1860. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1861. mark_mft_record_dirty(ctx->ntfs_ino);
  1862. ntfs_attr_reinit_search_ctx(ctx);
  1863. ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name,
  1864. mft_ni->name_len, CASE_SENSITIVE, 0, NULL, 0,
  1865. ctx);
  1866. if (unlikely(ret)) {
  1867. ntfs_error(vol->sb, "Failed to find first attribute "
  1868. "extent of mft data attribute.");
  1869. goto restore_undo_alloc;
  1870. }
  1871. a = ctx->attr;
  1872. }
  1873. write_lock_irqsave(&mft_ni->size_lock, flags);
  1874. mft_ni->allocated_size += nr << vol->cluster_size_bits;
  1875. a->data.non_resident.allocated_size =
  1876. cpu_to_sle64(mft_ni->allocated_size);
  1877. write_unlock_irqrestore(&mft_ni->size_lock, flags);
  1878. /* Ensure the changes make it to disk. */
  1879. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1880. mark_mft_record_dirty(ctx->ntfs_ino);
  1881. ntfs_attr_put_search_ctx(ctx);
  1882. unmap_mft_record(mft_ni);
  1883. up_write(&mft_ni->runlist.lock);
  1884. ntfs_debug("Done.");
  1885. return 0;
  1886. restore_undo_alloc:
  1887. ntfs_attr_reinit_search_ctx(ctx);
  1888. if (ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
  1889. CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) {
  1890. ntfs_error(vol->sb, "Failed to find last attribute extent of "
  1891. "mft data attribute.%s", es);
  1892. write_lock_irqsave(&mft_ni->size_lock, flags);
  1893. mft_ni->allocated_size += nr << vol->cluster_size_bits;
  1894. write_unlock_irqrestore(&mft_ni->size_lock, flags);
  1895. ntfs_attr_put_search_ctx(ctx);
  1896. unmap_mft_record(mft_ni);
  1897. up_write(&mft_ni->runlist.lock);
  1898. /*
  1899. * The only thing that is now wrong is ->allocated_size of the
  1900. * base attribute extent which chkdsk should be able to fix.
  1901. */
  1902. NVolSetErrors(vol);
  1903. return ret;
  1904. }
  1905. a = ctx->attr;
  1906. a->data.non_resident.highest_vcn = cpu_to_sle64(old_last_vcn - 1);
  1907. undo_alloc:
  1908. if (ntfs_cluster_free(vol->mft_ino, old_last_vcn, -1, TRUE) < 0) {
  1909. ntfs_error(vol->sb, "Failed to free clusters from mft data "
  1910. "attribute.%s", es);
  1911. NVolSetErrors(vol);
  1912. }
  1913. if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
  1914. ntfs_error(vol->sb, "Failed to truncate mft data attribute "
  1915. "runlist.%s", es);
  1916. NVolSetErrors(vol);
  1917. }
  1918. if (mp_rebuilt) {
  1919. if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
  1920. a->data.non_resident.mapping_pairs_offset),
  1921. old_alen - le16_to_cpu(
  1922. a->data.non_resident.mapping_pairs_offset),
  1923. rl2, ll, -1, NULL)) {
  1924. ntfs_error(vol->sb, "Failed to restore mapping pairs "
  1925. "array.%s", es);
  1926. NVolSetErrors(vol);
  1927. }
  1928. if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
  1929. ntfs_error(vol->sb, "Failed to restore attribute "
  1930. "record.%s", es);
  1931. NVolSetErrors(vol);
  1932. }
  1933. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1934. mark_mft_record_dirty(ctx->ntfs_ino);
  1935. }
  1936. if (ctx)
  1937. ntfs_attr_put_search_ctx(ctx);
  1938. if (!IS_ERR(mrec))
  1939. unmap_mft_record(mft_ni);
  1940. up_write(&mft_ni->runlist.lock);
  1941. return ret;
  1942. }
  1943. /**
  1944. * ntfs_mft_record_layout - layout an mft record into a memory buffer
  1945. * @vol: volume to which the mft record will belong
  1946. * @mft_no: mft reference specifying the mft record number
  1947. * @m: destination buffer of size >= @vol->mft_record_size bytes
  1948. *
  1949. * Layout an empty, unused mft record with the mft record number @mft_no into
  1950. * the buffer @m. The volume @vol is needed because the mft record structure
  1951. * was modified in NTFS 3.1 so we need to know which volume version this mft
  1952. * record will be used on.
  1953. *
  1954. * Return 0 on success and -errno on error.
  1955. */
  1956. static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
  1957. MFT_RECORD *m)
  1958. {
  1959. ATTR_RECORD *a;
  1960. ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
  1961. if (mft_no >= (1ll << 32)) {
  1962. ntfs_error(vol->sb, "Mft record number 0x%llx exceeds "
  1963. "maximum of 2^32.", (long long)mft_no);
  1964. return -ERANGE;
  1965. }
  1966. /* Start by clearing the whole mft record to gives us a clean slate. */
  1967. memset(m, 0, vol->mft_record_size);
  1968. /* Aligned to 2-byte boundary. */
  1969. if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver))
  1970. m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1);
  1971. else {
  1972. m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1);
  1973. /*
  1974. * Set the NTFS 3.1+ specific fields while we know that the
  1975. * volume version is 3.1+.
  1976. */
  1977. m->reserved = 0;
  1978. m->mft_record_number = cpu_to_le32((u32)mft_no);
  1979. }
  1980. m->magic = magic_FILE;
  1981. if (vol->mft_record_size >= NTFS_BLOCK_SIZE)
  1982. m->usa_count = cpu_to_le16(vol->mft_record_size /
  1983. NTFS_BLOCK_SIZE + 1);
  1984. else {
  1985. m->usa_count = cpu_to_le16(1);
  1986. ntfs_warning(vol->sb, "Sector size is bigger than mft record "
  1987. "size. Setting usa_count to 1. If chkdsk "
  1988. "reports this as corruption, please email "
  1989. "linux-ntfs-dev@lists.sourceforge.net stating "
  1990. "that you saw this message and that the "
  1991. "modified filesystem created was corrupt. "
  1992. "Thank you.");
  1993. }
  1994. /* Set the update sequence number to 1. */
  1995. *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = cpu_to_le16(1);
  1996. m->lsn = 0;
  1997. m->sequence_number = cpu_to_le16(1);
  1998. m->link_count = 0;
  1999. /*
  2000. * Place the attributes straight after the update sequence array,
  2001. * aligned to 8-byte boundary.
  2002. */
  2003. m->attrs_offset = cpu_to_le16((le16_to_cpu(m->usa_ofs) +
  2004. (le16_to_cpu(m->usa_count) << 1) + 7) & ~7);
  2005. m->flags = 0;
  2006. /*
  2007. * Using attrs_offset plus eight bytes (for the termination attribute).
  2008. * attrs_offset is already aligned to 8-byte boundary, so no need to
  2009. * align again.
  2010. */
  2011. m->bytes_in_use = cpu_to_le32(le16_to_cpu(m->attrs_offset) + 8);
  2012. m->bytes_allocated = cpu_to_le32(vol->mft_record_size);
  2013. m->base_mft_record = 0;
  2014. m->next_attr_instance = 0;
  2015. /* Add the termination attribute. */
  2016. a = (ATTR_RECORD*)((u8*)m + le16_to_cpu(m->attrs_offset));
  2017. a->type = AT_END;
  2018. a->length = 0;
  2019. ntfs_debug("Done.");
  2020. return 0;
  2021. }
  2022. /**
  2023. * ntfs_mft_record_format - format an mft record on an ntfs volume
  2024. * @vol: volume on which to format the mft record
  2025. * @mft_no: mft record number to format
  2026. *
  2027. * Format the mft record @mft_no in $MFT/$DATA, i.e. lay out an empty, unused
  2028. * mft record into the appropriate place of the mft data attribute. This is
  2029. * used when extending the mft data attribute.
  2030. *
  2031. * Return 0 on success and -errno on error.
  2032. */
  2033. static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
  2034. {
  2035. loff_t i_size;
  2036. struct inode *mft_vi = vol->mft_ino;
  2037. struct page *page;
  2038. MFT_RECORD *m;
  2039. pgoff_t index, end_index;
  2040. unsigned int ofs;
  2041. int err;
  2042. ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
  2043. /*
  2044. * The index into the page cache and the offset within the page cache
  2045. * page of the wanted mft record.
  2046. */
  2047. index = mft_no << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
  2048. ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
  2049. /* The maximum valid index into the page cache for $MFT's data. */
  2050. i_size = i_size_read(mft_vi);
  2051. end_index = i_size >> PAGE_CACHE_SHIFT;
  2052. if (unlikely(index >= end_index)) {
  2053. if (unlikely(index > end_index || ofs + vol->mft_record_size >=
  2054. (i_size & ~PAGE_CACHE_MASK))) {
  2055. ntfs_error(vol->sb, "Tried to format non-existing mft "
  2056. "record 0x%llx.", (long long)mft_no);
  2057. return -ENOENT;
  2058. }
  2059. }
  2060. /* Read, map, and pin the page containing the mft record. */
  2061. page = ntfs_map_page(mft_vi->i_mapping, index);
  2062. if (unlikely(IS_ERR(page))) {
  2063. ntfs_error(vol->sb, "Failed to map page containing mft record "
  2064. "to format 0x%llx.", (long long)mft_no);
  2065. return PTR_ERR(page);
  2066. }
  2067. lock_page(page);
  2068. BUG_ON(!PageUptodate(page));
  2069. ClearPageUptodate(page);
  2070. m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
  2071. err = ntfs_mft_record_layout(vol, mft_no, m);
  2072. if (unlikely(err)) {
  2073. ntfs_error(vol->sb, "Failed to layout mft record 0x%llx.",
  2074. (long long)mft_no);
  2075. SetPageUptodate(page);
  2076. unlock_page(page);
  2077. ntfs_unmap_page(page);
  2078. return err;
  2079. }
  2080. flush_dcache_page(page);
  2081. SetPageUptodate(page);
  2082. unlock_page(page);
  2083. /*
  2084. * Make sure the mft record is written out to disk. We could use
  2085. * ilookup5() to check if an inode is in icache and so on but this is
  2086. * unnecessary as ntfs_writepage() will write the dirty record anyway.
  2087. */
  2088. mark_ntfs_record_dirty(page, ofs);
  2089. ntfs_unmap_page(page);
  2090. ntfs_debug("Done.");
  2091. return 0;
  2092. }
  2093. /**
  2094. * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume
  2095. * @vol: [IN] volume on which to allocate the mft record
  2096. * @mode: [IN] mode if want a file or directory, i.e. base inode or 0
  2097. * @base_ni: [IN] open base inode if allocating an extent mft record or NULL
  2098. * @mrec: [OUT] on successful return this is the mapped mft record
  2099. *
  2100. * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol.
  2101. *
  2102. * If @base_ni is NULL make the mft record a base mft record, i.e. a file or
  2103. * direvctory inode, and allocate it at the default allocator position. In
  2104. * this case @mode is the file mode as given to us by the caller. We in
  2105. * particular use @mode to distinguish whether a file or a directory is being
  2106. * created (S_IFDIR(mode) and S_IFREG(mode), respectively).
  2107. *
  2108. * If @base_ni is not NULL make the allocated mft record an extent record,
  2109. * allocate it starting at the mft record after the base mft record and attach
  2110. * the allocated and opened ntfs inode to the base inode @base_ni. In this
  2111. * case @mode must be 0 as it is meaningless for extent inodes.
  2112. *
  2113. * You need to check the return value with IS_ERR(). If false, the function
  2114. * was successful and the return value is the now opened ntfs inode of the
  2115. * allocated mft record. *@mrec is then set to the allocated, mapped, pinned,
  2116. * and locked mft record. If IS_ERR() is true, the function failed and the
  2117. * error code is obtained from PTR_ERR(return value). *@mrec is undefined in
  2118. * this case.
  2119. *
  2120. * Allocation strategy:
  2121. *
  2122. * To find a free mft record, we scan the mft bitmap for a zero bit. To
  2123. * optimize this we start scanning at the place specified by @base_ni or if
  2124. * @base_ni is NULL we start where we last stopped and we perform wrap around
  2125. * when we reach the end. Note, we do not try to allocate mft records below
  2126. * number 24 because numbers 0 to 15 are the defined system files anyway and 16
  2127. * to 24 are special in that they are used for storing extension mft records
  2128. * for the $DATA attribute of $MFT. This is required to avoid the possibility
  2129. * of creating a runlist with a circular dependency which once written to disk
  2130. * can never be read in again. Windows will only use records 16 to 24 for
  2131. * normal files if the volume is completely out of space. We never use them
  2132. * which means that when the volume is really out of space we cannot create any
  2133. * more files while Windows can still create up to 8 small files. We can start
  2134. * doing this at some later time, it does not matter much for now.
  2135. *
  2136. * When scanning the mft bitmap, we only search up to the last allocated mft
  2137. * record. If there are no free records left in the range 24 to number of
  2138. * allocated mft records, then we extend the $MFT/$DATA attribute in order to
  2139. * create free mft records. We extend the allocated size of $MFT/$DATA by 16
  2140. * records at a time or one cluster, if cluster size is above 16kiB. If there
  2141. * is not sufficient space to do this, we try to extend by a single mft record
  2142. * or one cluster, if cluster size is above the mft record size.
  2143. *
  2144. * No matter how many mft records we allocate, we initialize only the first
  2145. * allocated mft record, incrementing mft data size and initialized size
  2146. * accordingly, open an ntfs_inode for it and return it to the caller, unless
  2147. * there are less than 24 mft records, in which case we allocate and initialize
  2148. * mft records until we reach record 24 which we consider as the first free mft
  2149. * record for use by normal files.
  2150. *
  2151. * If during any stage we overflow the initialized data in the mft bitmap, we
  2152. * extend the initialized size (and data size) by 8 bytes, allocating another
  2153. * cluster if required. The bitmap data size has to be at least equal to the
  2154. * number of mft records in the mft, but it can be bigger, in which case the
  2155. * superflous bits are padded with zeroes.
  2156. *
  2157. * Thus, when we return successfully (IS_ERR() is false), we will have:
  2158. * - initialized / extended the mft bitmap if necessary,
  2159. * - initialized / extended the mft data if necessary,
  2160. * - set the bit corresponding to the mft record being allocated in the
  2161. * mft bitmap,
  2162. * - opened an ntfs_inode for the allocated mft record, and we will have
  2163. * - returned the ntfs_inode as well as the allocated mapped, pinned, and
  2164. * locked mft record.
  2165. *
  2166. * On error, the volume will be left in a consistent state and no record will
  2167. * be allocated. If rolling back a partial operation fails, we may leave some
  2168. * inconsistent metadata in which case we set NVolErrors() so the volume is
  2169. * left dirty when unmounted.
  2170. *
  2171. * Note, this function cannot make use of most of the normal functions, like
  2172. * for example for attribute resizing, etc, because when the run list overflows
  2173. * the base mft record and an attribute list is used, it is very important that
  2174. * the extension mft records used to store the $DATA attribute of $MFT can be
  2175. * reached without having to read the information contained inside them, as
  2176. * this would make it impossible to find them in the first place after the
  2177. * volume is unmounted. $MFT/$BITMAP probably does not need to follow this
  2178. * rule because the bitmap is not essential for finding the mft records, but on
  2179. * the other hand, handling the bitmap in this special way would make life
  2180. * easier because otherwise there might be circular invocations of functions
  2181. * when reading the bitmap.
  2182. */
  2183. ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
  2184. ntfs_inode *base_ni, MFT_RECORD **mrec)
  2185. {
  2186. s64 ll, bit, old_data_initialized, old_data_size;
  2187. unsigned long flags;
  2188. struct inode *vi;
  2189. struct page *page;
  2190. ntfs_inode *mft_ni, *mftbmp_ni, *ni;
  2191. ntfs_attr_search_ctx *ctx;
  2192. MFT_RECORD *m;
  2193. ATTR_RECORD *a;
  2194. pgoff_t index;
  2195. unsigned int ofs;
  2196. int err;
  2197. le16 seq_no, usn;
  2198. BOOL record_formatted = FALSE;
  2199. if (base_ni) {
  2200. ntfs_debug("Entering (allocating an extent mft record for "
  2201. "base mft record 0x%llx).",
  2202. (long long)base_ni->mft_no);
  2203. /* @mode and @base_ni are mutually exclusive. */
  2204. BUG_ON(mode);
  2205. } else
  2206. ntfs_debug("Entering (allocating a base mft record).");
  2207. if (mode) {
  2208. /* @mode and @base_ni are mutually exclusive. */
  2209. BUG_ON(base_ni);
  2210. /* We only support creation of normal files and directories. */
  2211. if (!S_ISREG(mode) && !S_ISDIR(mode))
  2212. return ERR_PTR(-EOPNOTSUPP);
  2213. }
  2214. BUG_ON(!mrec);
  2215. mft_ni = NTFS_I(vol->mft_ino);
  2216. mftbmp_ni = NTFS_I(vol->mftbmp_ino);
  2217. down_write(&vol->mftbmp_lock);
  2218. bit = ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(vol, base_ni);
  2219. if (bit >= 0) {
  2220. ntfs_debug("Found and allocated free record (#1), bit 0x%llx.",
  2221. (long long)bit);
  2222. goto have_alloc_rec;
  2223. }
  2224. if (bit != -ENOSPC) {
  2225. up_write(&vol->mftbmp_lock);
  2226. return ERR_PTR(bit);
  2227. }
  2228. /*
  2229. * No free mft records left. If the mft bitmap already covers more
  2230. * than the currently used mft records, the next records are all free,
  2231. * so we can simply allocate the first unused mft record.
  2232. * Note: We also have to make sure that the mft bitmap at least covers
  2233. * the first 24 mft records as they are special and whilst they may not
  2234. * be in use, we do not allocate from them.
  2235. */
  2236. read_lock_irqsave(&mft_ni->size_lock, flags);
  2237. ll = mft_ni->initialized_size >> vol->mft_record_size_bits;
  2238. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  2239. read_lock_irqsave(&mftbmp_ni->size_lock, flags);
  2240. old_data_initialized = mftbmp_ni->initialized_size;
  2241. read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  2242. if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
  2243. bit = ll;
  2244. if (bit < 24)
  2245. bit = 24;
  2246. if (unlikely(bit >= (1ll << 32)))
  2247. goto max_err_out;
  2248. ntfs_debug("Found free record (#2), bit 0x%llx.",
  2249. (long long)bit);
  2250. goto found_free_rec;
  2251. }
  2252. /*
  2253. * The mft bitmap needs to be expanded until it covers the first unused
  2254. * mft record that we can allocate.
  2255. * Note: The smallest mft record we allocate is mft record 24.
  2256. */
  2257. bit = old_data_initialized << 3;
  2258. if (unlikely(bit >= (1ll << 32)))
  2259. goto max_err_out;
  2260. read_lock_irqsave(&mftbmp_ni->size_lock, flags);
  2261. old_data_size = mftbmp_ni->allocated_size;
  2262. ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
  2263. "data_size 0x%llx, initialized_size 0x%llx.",
  2264. (long long)old_data_size,
  2265. (long long)i_size_read(vol->mftbmp_ino),
  2266. (long long)old_data_initialized);
  2267. read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  2268. if (old_data_initialized + 8 > old_data_size) {
  2269. /* Need to extend bitmap by one more cluster. */
  2270. ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
  2271. err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
  2272. if (unlikely(err)) {
  2273. up_write(&vol->mftbmp_lock);
  2274. goto err_out;
  2275. }
  2276. #ifdef DEBUG
  2277. read_lock_irqsave(&mftbmp_ni->size_lock, flags);
  2278. ntfs_debug("Status of mftbmp after allocation extension: "
  2279. "allocated_size 0x%llx, data_size 0x%llx, "
  2280. "initialized_size 0x%llx.",
  2281. (long long)mftbmp_ni->allocated_size,
  2282. (long long)i_size_read(vol->mftbmp_ino),
  2283. (long long)mftbmp_ni->initialized_size);
  2284. read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  2285. #endif /* DEBUG */
  2286. }
  2287. /*
  2288. * We now have sufficient allocated space, extend the initialized_size
  2289. * as well as the data_size if necessary and fill the new space with
  2290. * zeroes.
  2291. */
  2292. err = ntfs_mft_bitmap_extend_initialized_nolock(vol);
  2293. if (unlikely(err)) {
  2294. up_write(&vol->mftbmp_lock);
  2295. goto err_out;
  2296. }
  2297. #ifdef DEBUG
  2298. read_lock_irqsave(&mftbmp_ni->size_lock, flags);
  2299. ntfs_debug("Status of mftbmp after initialized extention: "
  2300. "allocated_size 0x%llx, data_size 0x%llx, "
  2301. "initialized_size 0x%llx.",
  2302. (long long)mftbmp_ni->allocated_size,
  2303. (long long)i_size_read(vol->mftbmp_ino),
  2304. (long long)mftbmp_ni->initialized_size);
  2305. read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
  2306. #endif /* DEBUG */
  2307. ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit);
  2308. found_free_rec:
  2309. /* @bit is the found free mft record, allocate it in the mft bitmap. */
  2310. ntfs_debug("At found_free_rec.");
  2311. err = ntfs_bitmap_set_bit(vol->mftbmp_ino, bit);
  2312. if (unlikely(err)) {
  2313. ntfs_error(vol->sb, "Failed to allocate bit in mft bitmap.");
  2314. up_write(&vol->mftbmp_lock);
  2315. goto err_out;
  2316. }
  2317. ntfs_debug("Set bit 0x%llx in mft bitmap.", (long long)bit);
  2318. have_alloc_rec:
  2319. /*
  2320. * The mft bitmap is now uptodate. Deal with mft data attribute now.
  2321. * Note, we keep hold of the mft bitmap lock for writing until all
  2322. * modifications to the mft data attribute are complete, too, as they
  2323. * will impact decisions for mft bitmap and mft record allocation done
  2324. * by a parallel allocation and if the lock is not maintained a
  2325. * parallel allocation could allocate the same mft record as this one.
  2326. */
  2327. ll = (bit + 1) << vol->mft_record_size_bits;
  2328. read_lock_irqsave(&mft_ni->size_lock, flags);
  2329. old_data_initialized = mft_ni->initialized_size;
  2330. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  2331. if (ll <= old_data_initialized) {
  2332. ntfs_debug("Allocated mft record already initialized.");
  2333. goto mft_rec_already_initialized;
  2334. }
  2335. ntfs_debug("Initializing allocated mft record.");
  2336. /*
  2337. * The mft record is outside the initialized data. Extend the mft data
  2338. * attribute until it covers the allocated record. The loop is only
  2339. * actually traversed more than once when a freshly formatted volume is
  2340. * first written to so it optimizes away nicely in the common case.
  2341. */
  2342. read_lock_irqsave(&mft_ni->size_lock, flags);
  2343. ntfs_debug("Status of mft data before extension: "
  2344. "allocated_size 0x%llx, data_size 0x%llx, "
  2345. "initialized_size 0x%llx.",
  2346. (long long)mft_ni->allocated_size,
  2347. (long long)i_size_read(vol->mft_ino),
  2348. (long long)mft_ni->initialized_size);
  2349. while (ll > mft_ni->allocated_size) {
  2350. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  2351. err = ntfs_mft_data_extend_allocation_nolock(vol);
  2352. if (unlikely(err)) {
  2353. ntfs_error(vol->sb, "Failed to extend mft data "
  2354. "allocation.");
  2355. goto undo_mftbmp_alloc_nolock;
  2356. }
  2357. read_lock_irqsave(&mft_ni->size_lock, flags);
  2358. ntfs_debug("Status of mft data after allocation extension: "
  2359. "allocated_size 0x%llx, data_size 0x%llx, "
  2360. "initialized_size 0x%llx.",
  2361. (long long)mft_ni->allocated_size,
  2362. (long long)i_size_read(vol->mft_ino),
  2363. (long long)mft_ni->initialized_size);
  2364. }
  2365. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  2366. /*
  2367. * Extend mft data initialized size (and data size of course) to reach
  2368. * the allocated mft record, formatting the mft records allong the way.
  2369. * Note: We only modify the ntfs_inode structure as that is all that is
  2370. * needed by ntfs_mft_record_format(). We will update the attribute
  2371. * record itself in one fell swoop later on.
  2372. */
  2373. write_lock_irqsave(&mft_ni->size_lock, flags);
  2374. old_data_initialized = mft_ni->initialized_size;
  2375. old_data_size = vol->mft_ino->i_size;
  2376. while (ll > mft_ni->initialized_size) {
  2377. s64 new_initialized_size, mft_no;
  2378. new_initialized_size = mft_ni->initialized_size +
  2379. vol->mft_record_size;
  2380. mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits;
  2381. if (new_initialized_size > i_size_read(vol->mft_ino))
  2382. i_size_write(vol->mft_ino, new_initialized_size);
  2383. write_unlock_irqrestore(&mft_ni->size_lock, flags);
  2384. ntfs_debug("Initializing mft record 0x%llx.",
  2385. (long long)mft_no);
  2386. err = ntfs_mft_record_format(vol, mft_no);
  2387. if (unlikely(err)) {
  2388. ntfs_error(vol->sb, "Failed to format mft record.");
  2389. goto undo_data_init;
  2390. }
  2391. write_lock_irqsave(&mft_ni->size_lock, flags);
  2392. mft_ni->initialized_size = new_initialized_size;
  2393. }
  2394. write_unlock_irqrestore(&mft_ni->size_lock, flags);
  2395. record_formatted = TRUE;
  2396. /* Update the mft data attribute record to reflect the new sizes. */
  2397. m = map_mft_record(mft_ni);
  2398. if (IS_ERR(m)) {
  2399. ntfs_error(vol->sb, "Failed to map mft record.");
  2400. err = PTR_ERR(m);
  2401. goto undo_data_init;
  2402. }
  2403. ctx = ntfs_attr_get_search_ctx(mft_ni, m);
  2404. if (unlikely(!ctx)) {
  2405. ntfs_error(vol->sb, "Failed to get search context.");
  2406. err = -ENOMEM;
  2407. unmap_mft_record(mft_ni);
  2408. goto undo_data_init;
  2409. }
  2410. err = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
  2411. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2412. if (unlikely(err)) {
  2413. ntfs_error(vol->sb, "Failed to find first attribute extent of "
  2414. "mft data attribute.");
  2415. ntfs_attr_put_search_ctx(ctx);
  2416. unmap_mft_record(mft_ni);
  2417. goto undo_data_init;
  2418. }
  2419. a = ctx->attr;
  2420. read_lock_irqsave(&mft_ni->size_lock, flags);
  2421. a->data.non_resident.initialized_size =
  2422. cpu_to_sle64(mft_ni->initialized_size);
  2423. a->data.non_resident.data_size =
  2424. cpu_to_sle64(i_size_read(vol->mft_ino));
  2425. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  2426. /* Ensure the changes make it to disk. */
  2427. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2428. mark_mft_record_dirty(ctx->ntfs_ino);
  2429. ntfs_attr_put_search_ctx(ctx);
  2430. unmap_mft_record(mft_ni);
  2431. read_lock_irqsave(&mft_ni->size_lock, flags);
  2432. ntfs_debug("Status of mft data after mft record initialization: "
  2433. "allocated_size 0x%llx, data_size 0x%llx, "
  2434. "initialized_size 0x%llx.",
  2435. (long long)mft_ni->allocated_size,
  2436. (long long)i_size_read(vol->mft_ino),
  2437. (long long)mft_ni->initialized_size);
  2438. BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size);
  2439. BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino));
  2440. read_unlock_irqrestore(&mft_ni->size_lock, flags);
  2441. mft_rec_already_initialized:
  2442. /*
  2443. * We can finally drop the mft bitmap lock as the mft data attribute
  2444. * has been fully updated. The only disparity left is that the
  2445. * allocated mft record still needs to be marked as in use to match the
  2446. * set bit in the mft bitmap but this is actually not a problem since
  2447. * this mft record is not referenced from anywhere yet and the fact
  2448. * that it is allocated in the mft bitmap means that no-one will try to
  2449. * allocate it either.
  2450. */
  2451. up_write(&vol->mftbmp_lock);
  2452. /*
  2453. * We now have allocated and initialized the mft record. Calculate the
  2454. * index of and the offset within the page cache page the record is in.
  2455. */
  2456. index = bit << vol->mft_record_size_bits >> PAGE_CACHE_SHIFT;
  2457. ofs = (bit << vol->mft_record_size_bits) & ~PAGE_CACHE_MASK;
  2458. /* Read, map, and pin the page containing the mft record. */
  2459. page = ntfs_map_page(vol->mft_ino->i_mapping, index);
  2460. if (unlikely(IS_ERR(page))) {
  2461. ntfs_error(vol->sb, "Failed to map page containing allocated "
  2462. "mft record 0x%llx.", (long long)bit);
  2463. err = PTR_ERR(page);
  2464. goto undo_mftbmp_alloc;
  2465. }
  2466. lock_page(page);
  2467. BUG_ON(!PageUptodate(page));
  2468. ClearPageUptodate(page);
  2469. m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
  2470. /* If we just formatted the mft record no need to do it again. */
  2471. if (!record_formatted) {
  2472. /* Sanity check that the mft record is really not in use. */
  2473. if (ntfs_is_file_record(m->magic) &&
  2474. (m->flags & MFT_RECORD_IN_USE)) {
  2475. ntfs_error(vol->sb, "Mft record 0x%llx was marked "
  2476. "free in mft bitmap but is marked "
  2477. "used itself. Corrupt filesystem. "
  2478. "Unmount and run chkdsk.",
  2479. (long long)bit);
  2480. err = -EIO;
  2481. SetPageUptodate(page);
  2482. unlock_page(page);
  2483. ntfs_unmap_page(page);
  2484. NVolSetErrors(vol);
  2485. goto undo_mftbmp_alloc;
  2486. }
  2487. /*
  2488. * We need to (re-)format the mft record, preserving the
  2489. * sequence number if it is not zero as well as the update
  2490. * sequence number if it is not zero or -1 (0xffff). This
  2491. * means we do not need to care whether or not something went
  2492. * wrong with the previous mft record.
  2493. */
  2494. seq_no = m->sequence_number;
  2495. usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs));
  2496. err = ntfs_mft_record_layout(vol, bit, m);
  2497. if (unlikely(err)) {
  2498. ntfs_error(vol->sb, "Failed to layout allocated mft "
  2499. "record 0x%llx.", (long long)bit);
  2500. SetPageUptodate(page);
  2501. unlock_page(page);
  2502. ntfs_unmap_page(page);
  2503. goto undo_mftbmp_alloc;
  2504. }
  2505. if (seq_no)
  2506. m->sequence_number = seq_no;
  2507. if (usn && le16_to_cpu(usn) != 0xffff)
  2508. *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn;
  2509. }
  2510. /* Set the mft record itself in use. */
  2511. m->flags |= MFT_RECORD_IN_USE;
  2512. if (S_ISDIR(mode))
  2513. m->flags |= MFT_RECORD_IS_DIRECTORY;
  2514. flush_dcache_page(page);
  2515. SetPageUptodate(page);
  2516. if (base_ni) {
  2517. /*
  2518. * Setup the base mft record in the extent mft record. This
  2519. * completes initialization of the allocated extent mft record
  2520. * and we can simply use it with map_extent_mft_record().
  2521. */
  2522. m->base_mft_record = MK_LE_MREF(base_ni->mft_no,
  2523. base_ni->seq_no);
  2524. /*
  2525. * Allocate an extent inode structure for the new mft record,
  2526. * attach it to the base inode @base_ni and map, pin, and lock
  2527. * its, i.e. the allocated, mft record.
  2528. */
  2529. m = map_extent_mft_record(base_ni, bit, &ni);
  2530. if (IS_ERR(m)) {
  2531. ntfs_error(vol->sb, "Failed to map allocated extent "
  2532. "mft record 0x%llx.", (long long)bit);
  2533. err = PTR_ERR(m);
  2534. /* Set the mft record itself not in use. */
  2535. m->flags &= cpu_to_le16(
  2536. ~le16_to_cpu(MFT_RECORD_IN_USE));
  2537. flush_dcache_page(page);
  2538. /* Make sure the mft record is written out to disk. */
  2539. mark_ntfs_record_dirty(page, ofs);
  2540. unlock_page(page);
  2541. ntfs_unmap_page(page);
  2542. goto undo_mftbmp_alloc;
  2543. }
  2544. /*
  2545. * Make sure the allocated mft record is written out to disk.
  2546. * No need to set the inode dirty because the caller is going
  2547. * to do that anyway after finishing with the new extent mft
  2548. * record (e.g. at a minimum a new attribute will be added to
  2549. * the mft record.
  2550. */
  2551. mark_ntfs_record_dirty(page, ofs);
  2552. unlock_page(page);
  2553. /*
  2554. * Need to unmap the page since map_extent_mft_record() mapped
  2555. * it as well so we have it mapped twice at the moment.
  2556. */
  2557. ntfs_unmap_page(page);
  2558. } else {
  2559. /*
  2560. * Allocate a new VFS inode and set it up. NOTE: @vi->i_nlink
  2561. * is set to 1 but the mft record->link_count is 0. The caller
  2562. * needs to bear this in mind.
  2563. */
  2564. vi = new_inode(vol->sb);
  2565. if (unlikely(!vi)) {
  2566. err = -ENOMEM;
  2567. /* Set the mft record itself not in use. */
  2568. m->flags &= cpu_to_le16(
  2569. ~le16_to_cpu(MFT_RECORD_IN_USE));
  2570. flush_dcache_page(page);
  2571. /* Make sure the mft record is written out to disk. */
  2572. mark_ntfs_record_dirty(page, ofs);
  2573. unlock_page(page);
  2574. ntfs_unmap_page(page);
  2575. goto undo_mftbmp_alloc;
  2576. }
  2577. vi->i_ino = bit;
  2578. /*
  2579. * This is the optimal IO size (for stat), not the fs block
  2580. * size.
  2581. */
  2582. vi->i_blksize = PAGE_CACHE_SIZE;
  2583. /*
  2584. * This is for checking whether an inode has changed w.r.t. a
  2585. * file so that the file can be updated if necessary (compare
  2586. * with f_version).
  2587. */
  2588. vi->i_version = 1;
  2589. /* The owner and group come from the ntfs volume. */
  2590. vi->i_uid = vol->uid;
  2591. vi->i_gid = vol->gid;
  2592. /* Initialize the ntfs specific part of @vi. */
  2593. ntfs_init_big_inode(vi);
  2594. ni = NTFS_I(vi);
  2595. /*
  2596. * Set the appropriate mode, attribute type, and name. For
  2597. * directories, also setup the index values to the defaults.
  2598. */
  2599. if (S_ISDIR(mode)) {
  2600. vi->i_mode = S_IFDIR | S_IRWXUGO;
  2601. vi->i_mode &= ~vol->dmask;
  2602. NInoSetMstProtected(ni);
  2603. ni->type = AT_INDEX_ALLOCATION;
  2604. ni->name = I30;
  2605. ni->name_len = 4;
  2606. ni->itype.index.block_size = 4096;
  2607. ni->itype.index.block_size_bits = generic_ffs(4096) - 1;
  2608. ni->itype.index.collation_rule = COLLATION_FILE_NAME;
  2609. if (vol->cluster_size <= ni->itype.index.block_size) {
  2610. ni->itype.index.vcn_size = vol->cluster_size;
  2611. ni->itype.index.vcn_size_bits =
  2612. vol->cluster_size_bits;
  2613. } else {
  2614. ni->itype.index.vcn_size = vol->sector_size;
  2615. ni->itype.index.vcn_size_bits =
  2616. vol->sector_size_bits;
  2617. }
  2618. } else {
  2619. vi->i_mode = S_IFREG | S_IRWXUGO;
  2620. vi->i_mode &= ~vol->fmask;
  2621. ni->type = AT_DATA;
  2622. ni->name = NULL;
  2623. ni->name_len = 0;
  2624. }
  2625. if (IS_RDONLY(vi))
  2626. vi->i_mode &= ~S_IWUGO;
  2627. /* Set the inode times to the current time. */
  2628. vi->i_atime = vi->i_mtime = vi->i_ctime =
  2629. current_fs_time(vi->i_sb);
  2630. /*
  2631. * Set the file size to 0, the ntfs inode sizes are set to 0 by
  2632. * the call to ntfs_init_big_inode() below.
  2633. */
  2634. vi->i_size = 0;
  2635. vi->i_blocks = 0;
  2636. /* Set the sequence number. */
  2637. vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
  2638. /*
  2639. * Manually map, pin, and lock the mft record as we already
  2640. * have its page mapped and it is very easy to do.
  2641. */
  2642. atomic_inc(&ni->count);
  2643. down(&ni->mrec_lock);
  2644. ni->page = page;
  2645. ni->page_ofs = ofs;
  2646. /*
  2647. * Make sure the allocated mft record is written out to disk.
  2648. * NOTE: We do not set the ntfs inode dirty because this would
  2649. * fail in ntfs_write_inode() because the inode does not have a
  2650. * standard information attribute yet. Also, there is no need
  2651. * to set the inode dirty because the caller is going to do
  2652. * that anyway after finishing with the new mft record (e.g. at
  2653. * a minimum some new attributes will be added to the mft
  2654. * record.
  2655. */
  2656. mark_ntfs_record_dirty(page, ofs);
  2657. unlock_page(page);
  2658. /* Add the inode to the inode hash for the superblock. */
  2659. insert_inode_hash(vi);
  2660. /* Update the default mft allocation position. */
  2661. vol->mft_data_pos = bit + 1;
  2662. }
  2663. /*
  2664. * Return the opened, allocated inode of the allocated mft record as
  2665. * well as the mapped, pinned, and locked mft record.
  2666. */
  2667. ntfs_debug("Returning opened, allocated %sinode 0x%llx.",
  2668. base_ni ? "extent " : "", (long long)bit);
  2669. *mrec = m;
  2670. return ni;
  2671. undo_data_init:
  2672. write_lock_irqsave(&mft_ni->size_lock, flags);
  2673. mft_ni->initialized_size = old_data_initialized;
  2674. i_size_write(vol->mft_ino, old_data_size);
  2675. write_unlock_irqrestore(&mft_ni->size_lock, flags);
  2676. goto undo_mftbmp_alloc_nolock;
  2677. undo_mftbmp_alloc:
  2678. down_write(&vol->mftbmp_lock);
  2679. undo_mftbmp_alloc_nolock:
  2680. if (ntfs_bitmap_clear_bit(vol->mftbmp_ino, bit)) {
  2681. ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
  2682. NVolSetErrors(vol);
  2683. }
  2684. up_write(&vol->mftbmp_lock);
  2685. err_out:
  2686. return ERR_PTR(err);
  2687. max_err_out:
  2688. ntfs_warning(vol->sb, "Cannot allocate mft record because the maximum "
  2689. "number of inodes (2^32) has already been reached.");
  2690. up_write(&vol->mftbmp_lock);
  2691. return ERR_PTR(-ENOSPC);
  2692. }
  2693. /**
  2694. * ntfs_extent_mft_record_free - free an extent mft record on an ntfs volume
  2695. * @ni: ntfs inode of the mapped extent mft record to free
  2696. * @m: mapped extent mft record of the ntfs inode @ni
  2697. *
  2698. * Free the mapped extent mft record @m of the extent ntfs inode @ni.
  2699. *
  2700. * Note that this function unmaps the mft record and closes and destroys @ni
  2701. * internally and hence you cannot use either @ni nor @m any more after this
  2702. * function returns success.
  2703. *
  2704. * On success return 0 and on error return -errno. @ni and @m are still valid
  2705. * in this case and have not been freed.
  2706. *
  2707. * For some errors an error message is displayed and the success code 0 is
  2708. * returned and the volume is then left dirty on umount. This makes sense in
  2709. * case we could not rollback the changes that were already done since the
  2710. * caller no longer wants to reference this mft record so it does not matter to
  2711. * the caller if something is wrong with it as long as it is properly detached
  2712. * from the base inode.
  2713. */
  2714. int ntfs_extent_mft_record_free(ntfs_inode *ni, MFT_RECORD *m)
  2715. {
  2716. unsigned long mft_no = ni->mft_no;
  2717. ntfs_volume *vol = ni->vol;
  2718. ntfs_inode *base_ni;
  2719. ntfs_inode **extent_nis;
  2720. int i, err;
  2721. le16 old_seq_no;
  2722. u16 seq_no;
  2723. BUG_ON(NInoAttr(ni));
  2724. BUG_ON(ni->nr_extents != -1);
  2725. down(&ni->extent_lock);
  2726. base_ni = ni->ext.base_ntfs_ino;
  2727. up(&ni->extent_lock);
  2728. BUG_ON(base_ni->nr_extents <= 0);
  2729. ntfs_debug("Entering for extent inode 0x%lx, base inode 0x%lx.\n",
  2730. mft_no, base_ni->mft_no);
  2731. down(&base_ni->extent_lock);
  2732. /* Make sure we are holding the only reference to the extent inode. */
  2733. if (atomic_read(&ni->count) > 2) {
  2734. ntfs_error(vol->sb, "Tried to free busy extent inode 0x%lx, "
  2735. "not freeing.", base_ni->mft_no);
  2736. up(&base_ni->extent_lock);
  2737. return -EBUSY;
  2738. }
  2739. /* Dissociate the ntfs inode from the base inode. */
  2740. extent_nis = base_ni->ext.extent_ntfs_inos;
  2741. err = -ENOENT;
  2742. for (i = 0; i < base_ni->nr_extents; i++) {
  2743. if (ni != extent_nis[i])
  2744. continue;
  2745. extent_nis += i;
  2746. base_ni->nr_extents--;
  2747. memmove(extent_nis, extent_nis + 1, (base_ni->nr_extents - i) *
  2748. sizeof(ntfs_inode*));
  2749. err = 0;
  2750. break;
  2751. }
  2752. up(&base_ni->extent_lock);
  2753. if (unlikely(err)) {
  2754. ntfs_error(vol->sb, "Extent inode 0x%lx is not attached to "
  2755. "its base inode 0x%lx.", mft_no,
  2756. base_ni->mft_no);
  2757. BUG();
  2758. }
  2759. /*
  2760. * The extent inode is no longer attached to the base inode so no one
  2761. * can get a reference to it any more.
  2762. */
  2763. /* Mark the mft record as not in use. */
  2764. m->flags &= const_cpu_to_le16(~const_le16_to_cpu(MFT_RECORD_IN_USE));
  2765. /* Increment the sequence number, skipping zero, if it is not zero. */
  2766. old_seq_no = m->sequence_number;
  2767. seq_no = le16_to_cpu(old_seq_no);
  2768. if (seq_no == 0xffff)
  2769. seq_no = 1;
  2770. else if (seq_no)
  2771. seq_no++;
  2772. m->sequence_number = cpu_to_le16(seq_no);
  2773. /*
  2774. * Set the ntfs inode dirty and write it out. We do not need to worry
  2775. * about the base inode here since whatever caused the extent mft
  2776. * record to be freed is guaranteed to do it already.
  2777. */
  2778. NInoSetDirty(ni);
  2779. err = write_mft_record(ni, m, 0);
  2780. if (unlikely(err)) {
  2781. ntfs_error(vol->sb, "Failed to write mft record 0x%lx, not "
  2782. "freeing.", mft_no);
  2783. goto rollback;
  2784. }
  2785. rollback_error:
  2786. /* Unmap and throw away the now freed extent inode. */
  2787. unmap_extent_mft_record(ni);
  2788. ntfs_clear_extent_inode(ni);
  2789. /* Clear the bit in the $MFT/$BITMAP corresponding to this record. */
  2790. down_write(&vol->mftbmp_lock);
  2791. err = ntfs_bitmap_clear_bit(vol->mftbmp_ino, mft_no);
  2792. up_write(&vol->mftbmp_lock);
  2793. if (unlikely(err)) {
  2794. /*
  2795. * The extent inode is gone but we failed to deallocate it in
  2796. * the mft bitmap. Just emit a warning and leave the volume
  2797. * dirty on umount.
  2798. */
  2799. ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
  2800. NVolSetErrors(vol);
  2801. }
  2802. return 0;
  2803. rollback:
  2804. /* Rollback what we did... */
  2805. down(&base_ni->extent_lock);
  2806. extent_nis = base_ni->ext.extent_ntfs_inos;
  2807. if (!(base_ni->nr_extents & 3)) {
  2808. int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode*);
  2809. extent_nis = (ntfs_inode**)kmalloc(new_size, GFP_NOFS);
  2810. if (unlikely(!extent_nis)) {
  2811. ntfs_error(vol->sb, "Failed to allocate internal "
  2812. "buffer during rollback.%s", es);
  2813. up(&base_ni->extent_lock);
  2814. NVolSetErrors(vol);
  2815. goto rollback_error;
  2816. }
  2817. if (base_ni->nr_extents) {
  2818. BUG_ON(!base_ni->ext.extent_ntfs_inos);
  2819. memcpy(extent_nis, base_ni->ext.extent_ntfs_inos,
  2820. new_size - 4 * sizeof(ntfs_inode*));
  2821. kfree(base_ni->ext.extent_ntfs_inos);
  2822. }
  2823. base_ni->ext.extent_ntfs_inos = extent_nis;
  2824. }
  2825. m->flags |= MFT_RECORD_IN_USE;
  2826. m->sequence_number = old_seq_no;
  2827. extent_nis[base_ni->nr_extents++] = ni;
  2828. up(&base_ni->extent_lock);
  2829. mark_mft_record_dirty(ni);
  2830. return err;
  2831. }
  2832. #endif /* NTFS_RW */