mft.c 99 KB

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