mft.c 96 KB

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