mft.c 99 KB

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