mft.c 99 KB

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