mft.c 100 KB

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