mft.c 100 KB

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