mft.c 99 KB

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