mft.c 99 KB

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