attrib.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /**
  2. * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
  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/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/swap.h>
  26. #include <linux/writeback.h>
  27. #include "attrib.h"
  28. #include "debug.h"
  29. #include "layout.h"
  30. #include "lcnalloc.h"
  31. #include "malloc.h"
  32. #include "mft.h"
  33. #include "ntfs.h"
  34. #include "types.h"
  35. /**
  36. * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
  37. * @ni: ntfs inode for which to map (part of) a runlist
  38. * @vcn: map runlist part containing this vcn
  39. * @ctx: active attribute search context if present or NULL if not
  40. *
  41. * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
  42. *
  43. * If @ctx is specified, it is an active search context of @ni and its base mft
  44. * record. This is needed when ntfs_map_runlist_nolock() encounters unmapped
  45. * runlist fragments and allows their mapping. If you do not have the mft
  46. * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
  47. * will perform the necessary mapping and unmapping.
  48. *
  49. * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
  50. * restores it before returning. Thus, @ctx will be left pointing to the same
  51. * attribute on return as on entry. However, the actual pointers in @ctx may
  52. * point to different memory locations on return, so you must remember to reset
  53. * any cached pointers from the @ctx, i.e. after the call to
  54. * ntfs_map_runlist_nolock(), you will probably want to do:
  55. * m = ctx->mrec;
  56. * a = ctx->attr;
  57. * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
  58. * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
  59. *
  60. * Return 0 on success and -errno on error. There is one special error code
  61. * which is not an error as such. This is -ENOENT. It means that @vcn is out
  62. * of bounds of the runlist.
  63. *
  64. * Note the runlist can be NULL after this function returns if @vcn is zero and
  65. * the attribute has zero allocated size, i.e. there simply is no runlist.
  66. *
  67. * WARNING: If @ctx is supplied, regardless of whether success or failure is
  68. * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
  69. * is no longer valid, i.e. you need to either call
  70. * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
  71. * In that case PTR_ERR(@ctx->mrec) will give you the error code for
  72. * why the mapping of the old inode failed.
  73. *
  74. * Locking: - The runlist described by @ni must be locked for writing on entry
  75. * and is locked on return. Note the runlist will be modified.
  76. * - If @ctx is NULL, the base mft record of @ni must not be mapped on
  77. * entry and it will be left unmapped on return.
  78. * - If @ctx is not NULL, the base mft record must be mapped on entry
  79. * and it will be left mapped on return.
  80. */
  81. int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
  82. {
  83. VCN end_vcn;
  84. unsigned long flags;
  85. ntfs_inode *base_ni;
  86. MFT_RECORD *m;
  87. ATTR_RECORD *a;
  88. runlist_element *rl;
  89. struct page *put_this_page = NULL;
  90. int err = 0;
  91. bool ctx_is_temporary, ctx_needs_reset;
  92. ntfs_attr_search_ctx old_ctx = { NULL, };
  93. ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
  94. (unsigned long long)vcn);
  95. if (!NInoAttr(ni))
  96. base_ni = ni;
  97. else
  98. base_ni = ni->ext.base_ntfs_ino;
  99. if (!ctx) {
  100. ctx_is_temporary = ctx_needs_reset = true;
  101. m = map_mft_record(base_ni);
  102. if (IS_ERR(m))
  103. return PTR_ERR(m);
  104. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  105. if (unlikely(!ctx)) {
  106. err = -ENOMEM;
  107. goto err_out;
  108. }
  109. } else {
  110. VCN allocated_size_vcn;
  111. BUG_ON(IS_ERR(ctx->mrec));
  112. a = ctx->attr;
  113. BUG_ON(!a->non_resident);
  114. ctx_is_temporary = false;
  115. end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  116. read_lock_irqsave(&ni->size_lock, flags);
  117. allocated_size_vcn = ni->allocated_size >>
  118. ni->vol->cluster_size_bits;
  119. read_unlock_irqrestore(&ni->size_lock, flags);
  120. if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
  121. end_vcn = allocated_size_vcn - 1;
  122. /*
  123. * If we already have the attribute extent containing @vcn in
  124. * @ctx, no need to look it up again. We slightly cheat in
  125. * that if vcn exceeds the allocated size, we will refuse to
  126. * map the runlist below, so there is definitely no need to get
  127. * the right attribute extent.
  128. */
  129. if (vcn >= allocated_size_vcn || (a->type == ni->type &&
  130. a->name_length == ni->name_len &&
  131. !memcmp((u8*)a + le16_to_cpu(a->name_offset),
  132. ni->name, ni->name_len) &&
  133. sle64_to_cpu(a->data.non_resident.lowest_vcn)
  134. <= vcn && end_vcn >= vcn))
  135. ctx_needs_reset = false;
  136. else {
  137. /* Save the old search context. */
  138. old_ctx = *ctx;
  139. /*
  140. * If the currently mapped (extent) inode is not the
  141. * base inode we will unmap it when we reinitialize the
  142. * search context which means we need to get a
  143. * reference to the page containing the mapped mft
  144. * record so we do not accidentally drop changes to the
  145. * mft record when it has not been marked dirty yet.
  146. */
  147. if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
  148. old_ctx.base_ntfs_ino) {
  149. put_this_page = old_ctx.ntfs_ino->page;
  150. page_cache_get(put_this_page);
  151. }
  152. /*
  153. * Reinitialize the search context so we can lookup the
  154. * needed attribute extent.
  155. */
  156. ntfs_attr_reinit_search_ctx(ctx);
  157. ctx_needs_reset = true;
  158. }
  159. }
  160. if (ctx_needs_reset) {
  161. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  162. CASE_SENSITIVE, vcn, NULL, 0, ctx);
  163. if (unlikely(err)) {
  164. if (err == -ENOENT)
  165. err = -EIO;
  166. goto err_out;
  167. }
  168. BUG_ON(!ctx->attr->non_resident);
  169. }
  170. a = ctx->attr;
  171. /*
  172. * Only decompress the mapping pairs if @vcn is inside it. Otherwise
  173. * we get into problems when we try to map an out of bounds vcn because
  174. * we then try to map the already mapped runlist fragment and
  175. * ntfs_mapping_pairs_decompress() fails.
  176. */
  177. end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
  178. if (unlikely(vcn && vcn >= end_vcn)) {
  179. err = -ENOENT;
  180. goto err_out;
  181. }
  182. rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
  183. if (IS_ERR(rl))
  184. err = PTR_ERR(rl);
  185. else
  186. ni->runlist.rl = rl;
  187. err_out:
  188. if (ctx_is_temporary) {
  189. if (likely(ctx))
  190. ntfs_attr_put_search_ctx(ctx);
  191. unmap_mft_record(base_ni);
  192. } else if (ctx_needs_reset) {
  193. /*
  194. * If there is no attribute list, restoring the search context
  195. * is accomplished simply by copying the saved context back over
  196. * the caller supplied context. If there is an attribute list,
  197. * things are more complicated as we need to deal with mapping
  198. * of mft records and resulting potential changes in pointers.
  199. */
  200. if (NInoAttrList(base_ni)) {
  201. /*
  202. * If the currently mapped (extent) inode is not the
  203. * one we had before, we need to unmap it and map the
  204. * old one.
  205. */
  206. if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
  207. /*
  208. * If the currently mapped inode is not the
  209. * base inode, unmap it.
  210. */
  211. if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
  212. ctx->base_ntfs_ino) {
  213. unmap_extent_mft_record(ctx->ntfs_ino);
  214. ctx->mrec = ctx->base_mrec;
  215. BUG_ON(!ctx->mrec);
  216. }
  217. /*
  218. * If the old mapped inode is not the base
  219. * inode, map it.
  220. */
  221. if (old_ctx.base_ntfs_ino &&
  222. old_ctx.ntfs_ino !=
  223. old_ctx.base_ntfs_ino) {
  224. retry_map:
  225. ctx->mrec = map_mft_record(
  226. old_ctx.ntfs_ino);
  227. /*
  228. * Something bad has happened. If out
  229. * of memory retry till it succeeds.
  230. * Any other errors are fatal and we
  231. * return the error code in ctx->mrec.
  232. * Let the caller deal with it... We
  233. * just need to fudge things so the
  234. * caller can reinit and/or put the
  235. * search context safely.
  236. */
  237. if (IS_ERR(ctx->mrec)) {
  238. if (PTR_ERR(ctx->mrec) ==
  239. -ENOMEM) {
  240. schedule();
  241. goto retry_map;
  242. } else
  243. old_ctx.ntfs_ino =
  244. old_ctx.
  245. base_ntfs_ino;
  246. }
  247. }
  248. }
  249. /* Update the changed pointers in the saved context. */
  250. if (ctx->mrec != old_ctx.mrec) {
  251. if (!IS_ERR(ctx->mrec))
  252. old_ctx.attr = (ATTR_RECORD*)(
  253. (u8*)ctx->mrec +
  254. ((u8*)old_ctx.attr -
  255. (u8*)old_ctx.mrec));
  256. old_ctx.mrec = ctx->mrec;
  257. }
  258. }
  259. /* Restore the search context to the saved one. */
  260. *ctx = old_ctx;
  261. /*
  262. * We drop the reference on the page we took earlier. In the
  263. * case that IS_ERR(ctx->mrec) is true this means we might lose
  264. * some changes to the mft record that had been made between
  265. * the last time it was marked dirty/written out and now. This
  266. * at this stage is not a problem as the mapping error is fatal
  267. * enough that the mft record cannot be written out anyway and
  268. * the caller is very likely to shutdown the whole inode
  269. * immediately and mark the volume dirty for chkdsk to pick up
  270. * the pieces anyway.
  271. */
  272. if (put_this_page)
  273. page_cache_release(put_this_page);
  274. }
  275. return err;
  276. }
  277. /**
  278. * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
  279. * @ni: ntfs inode for which to map (part of) a runlist
  280. * @vcn: map runlist part containing this vcn
  281. *
  282. * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
  283. *
  284. * Return 0 on success and -errno on error. There is one special error code
  285. * which is not an error as such. This is -ENOENT. It means that @vcn is out
  286. * of bounds of the runlist.
  287. *
  288. * Locking: - The runlist must be unlocked on entry and is unlocked on return.
  289. * - This function takes the runlist lock for writing and may modify
  290. * the runlist.
  291. */
  292. int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
  293. {
  294. int err = 0;
  295. down_write(&ni->runlist.lock);
  296. /* Make sure someone else didn't do the work while we were sleeping. */
  297. if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
  298. LCN_RL_NOT_MAPPED))
  299. err = ntfs_map_runlist_nolock(ni, vcn, NULL);
  300. up_write(&ni->runlist.lock);
  301. return err;
  302. }
  303. /**
  304. * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
  305. * @ni: ntfs inode of the attribute whose runlist to search
  306. * @vcn: vcn to convert
  307. * @write_locked: true if the runlist is locked for writing
  308. *
  309. * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
  310. * described by the ntfs inode @ni and return the corresponding logical cluster
  311. * number (lcn).
  312. *
  313. * If the @vcn is not mapped yet, the attempt is made to map the attribute
  314. * extent containing the @vcn and the vcn to lcn conversion is retried.
  315. *
  316. * If @write_locked is true the caller has locked the runlist for writing and
  317. * if false for reading.
  318. *
  319. * Since lcns must be >= 0, we use negative return codes with special meaning:
  320. *
  321. * Return code Meaning / Description
  322. * ==========================================
  323. * LCN_HOLE Hole / not allocated on disk.
  324. * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
  325. * LCN_ENOMEM Not enough memory to map runlist.
  326. * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
  327. *
  328. * Locking: - The runlist must be locked on entry and is left locked on return.
  329. * - If @write_locked is 'false', i.e. the runlist is locked for reading,
  330. * the lock may be dropped inside the function so you cannot rely on
  331. * the runlist still being the same when this function returns.
  332. */
  333. LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
  334. const bool write_locked)
  335. {
  336. LCN lcn;
  337. unsigned long flags;
  338. bool is_retry = false;
  339. BUG_ON(!ni);
  340. ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
  341. ni->mft_no, (unsigned long long)vcn,
  342. write_locked ? "write" : "read");
  343. BUG_ON(!NInoNonResident(ni));
  344. BUG_ON(vcn < 0);
  345. if (!ni->runlist.rl) {
  346. read_lock_irqsave(&ni->size_lock, flags);
  347. if (!ni->allocated_size) {
  348. read_unlock_irqrestore(&ni->size_lock, flags);
  349. return LCN_ENOENT;
  350. }
  351. read_unlock_irqrestore(&ni->size_lock, flags);
  352. }
  353. retry_remap:
  354. /* Convert vcn to lcn. If that fails map the runlist and retry once. */
  355. lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
  356. if (likely(lcn >= LCN_HOLE)) {
  357. ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
  358. return lcn;
  359. }
  360. if (lcn != LCN_RL_NOT_MAPPED) {
  361. if (lcn != LCN_ENOENT)
  362. lcn = LCN_EIO;
  363. } else if (!is_retry) {
  364. int err;
  365. if (!write_locked) {
  366. up_read(&ni->runlist.lock);
  367. down_write(&ni->runlist.lock);
  368. if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
  369. LCN_RL_NOT_MAPPED)) {
  370. up_write(&ni->runlist.lock);
  371. down_read(&ni->runlist.lock);
  372. goto retry_remap;
  373. }
  374. }
  375. err = ntfs_map_runlist_nolock(ni, vcn, NULL);
  376. if (!write_locked) {
  377. up_write(&ni->runlist.lock);
  378. down_read(&ni->runlist.lock);
  379. }
  380. if (likely(!err)) {
  381. is_retry = true;
  382. goto retry_remap;
  383. }
  384. if (err == -ENOENT)
  385. lcn = LCN_ENOENT;
  386. else if (err == -ENOMEM)
  387. lcn = LCN_ENOMEM;
  388. else
  389. lcn = LCN_EIO;
  390. }
  391. if (lcn != LCN_ENOENT)
  392. ntfs_error(ni->vol->sb, "Failed with error code %lli.",
  393. (long long)lcn);
  394. return lcn;
  395. }
  396. /**
  397. * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
  398. * @ni: ntfs inode describing the runlist to search
  399. * @vcn: vcn to find
  400. * @ctx: active attribute search context if present or NULL if not
  401. *
  402. * Find the virtual cluster number @vcn in the runlist described by the ntfs
  403. * inode @ni and return the address of the runlist element containing the @vcn.
  404. *
  405. * If the @vcn is not mapped yet, the attempt is made to map the attribute
  406. * extent containing the @vcn and the vcn to lcn conversion is retried.
  407. *
  408. * If @ctx is specified, it is an active search context of @ni and its base mft
  409. * record. This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped
  410. * runlist fragments and allows their mapping. If you do not have the mft
  411. * record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock()
  412. * will perform the necessary mapping and unmapping.
  413. *
  414. * Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and
  415. * restores it before returning. Thus, @ctx will be left pointing to the same
  416. * attribute on return as on entry. However, the actual pointers in @ctx may
  417. * point to different memory locations on return, so you must remember to reset
  418. * any cached pointers from the @ctx, i.e. after the call to
  419. * ntfs_attr_find_vcn_nolock(), you will probably want to do:
  420. * m = ctx->mrec;
  421. * a = ctx->attr;
  422. * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
  423. * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
  424. * Note you need to distinguish between the lcn of the returned runlist element
  425. * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
  426. * read and allocate clusters on write.
  427. *
  428. * Return the runlist element containing the @vcn on success and
  429. * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
  430. * to decide if the return is success or failure and PTR_ERR() to get to the
  431. * error code if IS_ERR() is true.
  432. *
  433. * The possible error return codes are:
  434. * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
  435. * -ENOMEM - Not enough memory to map runlist.
  436. * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
  437. *
  438. * WARNING: If @ctx is supplied, regardless of whether success or failure is
  439. * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
  440. * is no longer valid, i.e. you need to either call
  441. * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
  442. * In that case PTR_ERR(@ctx->mrec) will give you the error code for
  443. * why the mapping of the old inode failed.
  444. *
  445. * Locking: - The runlist described by @ni must be locked for writing on entry
  446. * and is locked on return. Note the runlist may be modified when
  447. * needed runlist fragments need to be mapped.
  448. * - If @ctx is NULL, the base mft record of @ni must not be mapped on
  449. * entry and it will be left unmapped on return.
  450. * - If @ctx is not NULL, the base mft record must be mapped on entry
  451. * and it will be left mapped on return.
  452. */
  453. runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
  454. ntfs_attr_search_ctx *ctx)
  455. {
  456. unsigned long flags;
  457. runlist_element *rl;
  458. int err = 0;
  459. bool is_retry = false;
  460. BUG_ON(!ni);
  461. ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
  462. ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
  463. BUG_ON(!NInoNonResident(ni));
  464. BUG_ON(vcn < 0);
  465. if (!ni->runlist.rl) {
  466. read_lock_irqsave(&ni->size_lock, flags);
  467. if (!ni->allocated_size) {
  468. read_unlock_irqrestore(&ni->size_lock, flags);
  469. return ERR_PTR(-ENOENT);
  470. }
  471. read_unlock_irqrestore(&ni->size_lock, flags);
  472. }
  473. retry_remap:
  474. rl = ni->runlist.rl;
  475. if (likely(rl && vcn >= rl[0].vcn)) {
  476. while (likely(rl->length)) {
  477. if (unlikely(vcn < rl[1].vcn)) {
  478. if (likely(rl->lcn >= LCN_HOLE)) {
  479. ntfs_debug("Done.");
  480. return rl;
  481. }
  482. break;
  483. }
  484. rl++;
  485. }
  486. if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
  487. if (likely(rl->lcn == LCN_ENOENT))
  488. err = -ENOENT;
  489. else
  490. err = -EIO;
  491. }
  492. }
  493. if (!err && !is_retry) {
  494. /*
  495. * If the search context is invalid we cannot map the unmapped
  496. * region.
  497. */
  498. if (IS_ERR(ctx->mrec))
  499. err = PTR_ERR(ctx->mrec);
  500. else {
  501. /*
  502. * The @vcn is in an unmapped region, map the runlist
  503. * and retry.
  504. */
  505. err = ntfs_map_runlist_nolock(ni, vcn, ctx);
  506. if (likely(!err)) {
  507. is_retry = true;
  508. goto retry_remap;
  509. }
  510. }
  511. if (err == -EINVAL)
  512. err = -EIO;
  513. } else if (!err)
  514. err = -EIO;
  515. if (err != -ENOENT)
  516. ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
  517. return ERR_PTR(err);
  518. }
  519. /**
  520. * ntfs_attr_find - find (next) attribute in mft record
  521. * @type: attribute type to find
  522. * @name: attribute name to find (optional, i.e. NULL means don't care)
  523. * @name_len: attribute name length (only needed if @name present)
  524. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  525. * @val: attribute value to find (optional, resident attributes only)
  526. * @val_len: attribute value length
  527. * @ctx: search context with mft record and attribute to search from
  528. *
  529. * You should not need to call this function directly. Use ntfs_attr_lookup()
  530. * instead.
  531. *
  532. * ntfs_attr_find() takes a search context @ctx as parameter and searches the
  533. * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
  534. * attribute of @type, optionally @name and @val.
  535. *
  536. * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
  537. * point to the found attribute.
  538. *
  539. * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
  540. * @ctx->attr will point to the attribute before which the attribute being
  541. * searched for would need to be inserted if such an action were to be desired.
  542. *
  543. * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
  544. * undefined and in particular do not rely on it not changing.
  545. *
  546. * If @ctx->is_first is 'true', the search begins with @ctx->attr itself. If it
  547. * is 'false', the search begins after @ctx->attr.
  548. *
  549. * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
  550. * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
  551. * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
  552. * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
  553. * sensitive. When @name is present, @name_len is the @name length in Unicode
  554. * characters.
  555. *
  556. * If @name is not present (NULL), we assume that the unnamed attribute is
  557. * being searched for.
  558. *
  559. * Finally, the resident attribute value @val is looked for, if present. If
  560. * @val is not present (NULL), @val_len is ignored.
  561. *
  562. * ntfs_attr_find() only searches the specified mft record and it ignores the
  563. * presence of an attribute list attribute (unless it is the one being searched
  564. * for, obviously). If you need to take attribute lists into consideration,
  565. * use ntfs_attr_lookup() instead (see below). This also means that you cannot
  566. * use ntfs_attr_find() to search for extent records of non-resident
  567. * attributes, as extents with lowest_vcn != 0 are usually described by the
  568. * attribute list attribute only. - Note that it is possible that the first
  569. * extent is only in the attribute list while the last extent is in the base
  570. * mft record, so do not rely on being able to find the first extent in the
  571. * base mft record.
  572. *
  573. * Warning: Never use @val when looking for attribute types which can be
  574. * non-resident as this most likely will result in a crash!
  575. */
  576. static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
  577. const u32 name_len, const IGNORE_CASE_BOOL ic,
  578. const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
  579. {
  580. ATTR_RECORD *a;
  581. ntfs_volume *vol = ctx->ntfs_ino->vol;
  582. ntfschar *upcase = vol->upcase;
  583. u32 upcase_len = vol->upcase_len;
  584. /*
  585. * Iterate over attributes in mft record starting at @ctx->attr, or the
  586. * attribute following that, if @ctx->is_first is 'true'.
  587. */
  588. if (ctx->is_first) {
  589. a = ctx->attr;
  590. ctx->is_first = false;
  591. } else
  592. a = (ATTR_RECORD*)((u8*)ctx->attr +
  593. le32_to_cpu(ctx->attr->length));
  594. for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
  595. if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
  596. le32_to_cpu(ctx->mrec->bytes_allocated))
  597. break;
  598. ctx->attr = a;
  599. if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
  600. a->type == AT_END))
  601. return -ENOENT;
  602. if (unlikely(!a->length))
  603. break;
  604. if (a->type != type)
  605. continue;
  606. /*
  607. * If @name is present, compare the two names. If @name is
  608. * missing, assume we want an unnamed attribute.
  609. */
  610. if (!name) {
  611. /* The search failed if the found attribute is named. */
  612. if (a->name_length)
  613. return -ENOENT;
  614. } else if (!ntfs_are_names_equal(name, name_len,
  615. (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
  616. a->name_length, ic, upcase, upcase_len)) {
  617. register int rc;
  618. rc = ntfs_collate_names(name, name_len,
  619. (ntfschar*)((u8*)a +
  620. le16_to_cpu(a->name_offset)),
  621. a->name_length, 1, IGNORE_CASE,
  622. upcase, upcase_len);
  623. /*
  624. * If @name collates before a->name, there is no
  625. * matching attribute.
  626. */
  627. if (rc == -1)
  628. return -ENOENT;
  629. /* If the strings are not equal, continue search. */
  630. if (rc)
  631. continue;
  632. rc = ntfs_collate_names(name, name_len,
  633. (ntfschar*)((u8*)a +
  634. le16_to_cpu(a->name_offset)),
  635. a->name_length, 1, CASE_SENSITIVE,
  636. upcase, upcase_len);
  637. if (rc == -1)
  638. return -ENOENT;
  639. if (rc)
  640. continue;
  641. }
  642. /*
  643. * The names match or @name not present and attribute is
  644. * unnamed. If no @val specified, we have found the attribute
  645. * and are done.
  646. */
  647. if (!val)
  648. return 0;
  649. /* @val is present; compare values. */
  650. else {
  651. register int rc;
  652. rc = memcmp(val, (u8*)a + le16_to_cpu(
  653. a->data.resident.value_offset),
  654. min_t(u32, val_len, le32_to_cpu(
  655. a->data.resident.value_length)));
  656. /*
  657. * If @val collates before the current attribute's
  658. * value, there is no matching attribute.
  659. */
  660. if (!rc) {
  661. register u32 avl;
  662. avl = le32_to_cpu(
  663. a->data.resident.value_length);
  664. if (val_len == avl)
  665. return 0;
  666. if (val_len < avl)
  667. return -ENOENT;
  668. } else if (rc < 0)
  669. return -ENOENT;
  670. }
  671. }
  672. ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
  673. NVolSetErrors(vol);
  674. return -EIO;
  675. }
  676. /**
  677. * load_attribute_list - load an attribute list into memory
  678. * @vol: ntfs volume from which to read
  679. * @runlist: runlist of the attribute list
  680. * @al_start: destination buffer
  681. * @size: size of the destination buffer in bytes
  682. * @initialized_size: initialized size of the attribute list
  683. *
  684. * Walk the runlist @runlist and load all clusters from it copying them into
  685. * the linear buffer @al. The maximum number of bytes copied to @al is @size
  686. * bytes. Note, @size does not need to be a multiple of the cluster size. If
  687. * @initialized_size is less than @size, the region in @al between
  688. * @initialized_size and @size will be zeroed and not read from disk.
  689. *
  690. * Return 0 on success or -errno on error.
  691. */
  692. int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
  693. const s64 size, const s64 initialized_size)
  694. {
  695. LCN lcn;
  696. u8 *al = al_start;
  697. u8 *al_end = al + initialized_size;
  698. runlist_element *rl;
  699. struct buffer_head *bh;
  700. struct super_block *sb;
  701. unsigned long block_size;
  702. unsigned long block, max_block;
  703. int err = 0;
  704. unsigned char block_size_bits;
  705. ntfs_debug("Entering.");
  706. if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
  707. initialized_size > size)
  708. return -EINVAL;
  709. if (!initialized_size) {
  710. memset(al, 0, size);
  711. return 0;
  712. }
  713. sb = vol->sb;
  714. block_size = sb->s_blocksize;
  715. block_size_bits = sb->s_blocksize_bits;
  716. down_read(&runlist->lock);
  717. rl = runlist->rl;
  718. if (!rl) {
  719. ntfs_error(sb, "Cannot read attribute list since runlist is "
  720. "missing.");
  721. goto err_out;
  722. }
  723. /* Read all clusters specified by the runlist one run at a time. */
  724. while (rl->length) {
  725. lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
  726. ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
  727. (unsigned long long)rl->vcn,
  728. (unsigned long long)lcn);
  729. /* The attribute list cannot be sparse. */
  730. if (lcn < 0) {
  731. ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
  732. "read attribute list.");
  733. goto err_out;
  734. }
  735. block = lcn << vol->cluster_size_bits >> block_size_bits;
  736. /* Read the run from device in chunks of block_size bytes. */
  737. max_block = block + (rl->length << vol->cluster_size_bits >>
  738. block_size_bits);
  739. ntfs_debug("max_block = 0x%lx.", max_block);
  740. do {
  741. ntfs_debug("Reading block = 0x%lx.", block);
  742. bh = sb_bread(sb, block);
  743. if (!bh) {
  744. ntfs_error(sb, "sb_bread() failed. Cannot "
  745. "read attribute list.");
  746. goto err_out;
  747. }
  748. if (al + block_size >= al_end)
  749. goto do_final;
  750. memcpy(al, bh->b_data, block_size);
  751. brelse(bh);
  752. al += block_size;
  753. } while (++block < max_block);
  754. rl++;
  755. }
  756. if (initialized_size < size) {
  757. initialize:
  758. memset(al_start + initialized_size, 0, size - initialized_size);
  759. }
  760. done:
  761. up_read(&runlist->lock);
  762. return err;
  763. do_final:
  764. if (al < al_end) {
  765. /*
  766. * Partial block.
  767. *
  768. * Note: The attribute list can be smaller than its allocation
  769. * by multiple clusters. This has been encountered by at least
  770. * two people running Windows XP, thus we cannot do any
  771. * truncation sanity checking here. (AIA)
  772. */
  773. memcpy(al, bh->b_data, al_end - al);
  774. brelse(bh);
  775. if (initialized_size < size)
  776. goto initialize;
  777. goto done;
  778. }
  779. brelse(bh);
  780. /* Real overflow! */
  781. ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
  782. "is truncated.");
  783. err_out:
  784. err = -EIO;
  785. goto done;
  786. }
  787. /**
  788. * ntfs_external_attr_find - find an attribute in the attribute list of an inode
  789. * @type: attribute type to find
  790. * @name: attribute name to find (optional, i.e. NULL means don't care)
  791. * @name_len: attribute name length (only needed if @name present)
  792. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  793. * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
  794. * @val: attribute value to find (optional, resident attributes only)
  795. * @val_len: attribute value length
  796. * @ctx: search context with mft record and attribute to search from
  797. *
  798. * You should not need to call this function directly. Use ntfs_attr_lookup()
  799. * instead.
  800. *
  801. * Find an attribute by searching the attribute list for the corresponding
  802. * attribute list entry. Having found the entry, map the mft record if the
  803. * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
  804. * in there and return it.
  805. *
  806. * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
  807. * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
  808. * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
  809. * then the base inode).
  810. *
  811. * After finishing with the attribute/mft record you need to call
  812. * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
  813. * mapped inodes, etc).
  814. *
  815. * If the attribute is found, ntfs_external_attr_find() returns 0 and
  816. * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
  817. * mft record in which @ctx->attr is located and @ctx->al_entry will point to
  818. * the attribute list entry for the attribute.
  819. *
  820. * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
  821. * @ctx->attr will point to the attribute in the base mft record before which
  822. * the attribute being searched for would need to be inserted if such an action
  823. * were to be desired. @ctx->mrec will point to the mft record in which
  824. * @ctx->attr is located and @ctx->al_entry will point to the attribute list
  825. * entry of the attribute before which the attribute being searched for would
  826. * need to be inserted if such an action were to be desired.
  827. *
  828. * Thus to insert the not found attribute, one wants to add the attribute to
  829. * @ctx->mrec (the base mft record) and if there is not enough space, the
  830. * attribute should be placed in a newly allocated extent mft record. The
  831. * attribute list entry for the inserted attribute should be inserted in the
  832. * attribute list attribute at @ctx->al_entry.
  833. *
  834. * On actual error, ntfs_external_attr_find() returns -EIO. In this case
  835. * @ctx->attr is undefined and in particular do not rely on it not changing.
  836. */
  837. static int ntfs_external_attr_find(const ATTR_TYPE type,
  838. const ntfschar *name, const u32 name_len,
  839. const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
  840. const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
  841. {
  842. ntfs_inode *base_ni, *ni;
  843. ntfs_volume *vol;
  844. ATTR_LIST_ENTRY *al_entry, *next_al_entry;
  845. u8 *al_start, *al_end;
  846. ATTR_RECORD *a;
  847. ntfschar *al_name;
  848. u32 al_name_len;
  849. int err = 0;
  850. static const char *es = " Unmount and run chkdsk.";
  851. ni = ctx->ntfs_ino;
  852. base_ni = ctx->base_ntfs_ino;
  853. ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
  854. if (!base_ni) {
  855. /* First call happens with the base mft record. */
  856. base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
  857. ctx->base_mrec = ctx->mrec;
  858. }
  859. if (ni == base_ni)
  860. ctx->base_attr = ctx->attr;
  861. if (type == AT_END)
  862. goto not_found;
  863. vol = base_ni->vol;
  864. al_start = base_ni->attr_list;
  865. al_end = al_start + base_ni->attr_list_size;
  866. if (!ctx->al_entry)
  867. ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
  868. /*
  869. * Iterate over entries in attribute list starting at @ctx->al_entry,
  870. * or the entry following that, if @ctx->is_first is 'true'.
  871. */
  872. if (ctx->is_first) {
  873. al_entry = ctx->al_entry;
  874. ctx->is_first = false;
  875. } else
  876. al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
  877. le16_to_cpu(ctx->al_entry->length));
  878. for (;; al_entry = next_al_entry) {
  879. /* Out of bounds check. */
  880. if ((u8*)al_entry < base_ni->attr_list ||
  881. (u8*)al_entry > al_end)
  882. break; /* Inode is corrupt. */
  883. ctx->al_entry = al_entry;
  884. /* Catch the end of the attribute list. */
  885. if ((u8*)al_entry == al_end)
  886. goto not_found;
  887. if (!al_entry->length)
  888. break;
  889. if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
  890. le16_to_cpu(al_entry->length) > al_end)
  891. break;
  892. next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
  893. le16_to_cpu(al_entry->length));
  894. if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
  895. goto not_found;
  896. if (type != al_entry->type)
  897. continue;
  898. /*
  899. * If @name is present, compare the two names. If @name is
  900. * missing, assume we want an unnamed attribute.
  901. */
  902. al_name_len = al_entry->name_length;
  903. al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
  904. if (!name) {
  905. if (al_name_len)
  906. goto not_found;
  907. } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
  908. name_len, ic, vol->upcase, vol->upcase_len)) {
  909. register int rc;
  910. rc = ntfs_collate_names(name, name_len, al_name,
  911. al_name_len, 1, IGNORE_CASE,
  912. vol->upcase, vol->upcase_len);
  913. /*
  914. * If @name collates before al_name, there is no
  915. * matching attribute.
  916. */
  917. if (rc == -1)
  918. goto not_found;
  919. /* If the strings are not equal, continue search. */
  920. if (rc)
  921. continue;
  922. /*
  923. * FIXME: Reverse engineering showed 0, IGNORE_CASE but
  924. * that is inconsistent with ntfs_attr_find(). The
  925. * subsequent rc checks were also different. Perhaps I
  926. * made a mistake in one of the two. Need to recheck
  927. * which is correct or at least see what is going on...
  928. * (AIA)
  929. */
  930. rc = ntfs_collate_names(name, name_len, al_name,
  931. al_name_len, 1, CASE_SENSITIVE,
  932. vol->upcase, vol->upcase_len);
  933. if (rc == -1)
  934. goto not_found;
  935. if (rc)
  936. continue;
  937. }
  938. /*
  939. * The names match or @name not present and attribute is
  940. * unnamed. Now check @lowest_vcn. Continue search if the
  941. * next attribute list entry still fits @lowest_vcn. Otherwise
  942. * we have reached the right one or the search has failed.
  943. */
  944. if (lowest_vcn && (u8*)next_al_entry >= al_start &&
  945. (u8*)next_al_entry + 6 < al_end &&
  946. (u8*)next_al_entry + le16_to_cpu(
  947. next_al_entry->length) <= al_end &&
  948. sle64_to_cpu(next_al_entry->lowest_vcn) <=
  949. lowest_vcn &&
  950. next_al_entry->type == al_entry->type &&
  951. next_al_entry->name_length == al_name_len &&
  952. ntfs_are_names_equal((ntfschar*)((u8*)
  953. next_al_entry +
  954. next_al_entry->name_offset),
  955. next_al_entry->name_length,
  956. al_name, al_name_len, CASE_SENSITIVE,
  957. vol->upcase, vol->upcase_len))
  958. continue;
  959. if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
  960. if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
  961. ntfs_error(vol->sb, "Found stale mft "
  962. "reference in attribute list "
  963. "of base inode 0x%lx.%s",
  964. base_ni->mft_no, es);
  965. err = -EIO;
  966. break;
  967. }
  968. } else { /* Mft references do not match. */
  969. /* If there is a mapped record unmap it first. */
  970. if (ni != base_ni)
  971. unmap_extent_mft_record(ni);
  972. /* Do we want the base record back? */
  973. if (MREF_LE(al_entry->mft_reference) ==
  974. base_ni->mft_no) {
  975. ni = ctx->ntfs_ino = base_ni;
  976. ctx->mrec = ctx->base_mrec;
  977. } else {
  978. /* We want an extent record. */
  979. ctx->mrec = map_extent_mft_record(base_ni,
  980. le64_to_cpu(
  981. al_entry->mft_reference), &ni);
  982. if (IS_ERR(ctx->mrec)) {
  983. ntfs_error(vol->sb, "Failed to map "
  984. "extent mft record "
  985. "0x%lx of base inode "
  986. "0x%lx.%s",
  987. MREF_LE(al_entry->
  988. mft_reference),
  989. base_ni->mft_no, es);
  990. err = PTR_ERR(ctx->mrec);
  991. if (err == -ENOENT)
  992. err = -EIO;
  993. /* Cause @ctx to be sanitized below. */
  994. ni = NULL;
  995. break;
  996. }
  997. ctx->ntfs_ino = ni;
  998. }
  999. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  1000. le16_to_cpu(ctx->mrec->attrs_offset));
  1001. }
  1002. /*
  1003. * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
  1004. * mft record containing the attribute represented by the
  1005. * current al_entry.
  1006. */
  1007. /*
  1008. * We could call into ntfs_attr_find() to find the right
  1009. * attribute in this mft record but this would be less
  1010. * efficient and not quite accurate as ntfs_attr_find() ignores
  1011. * the attribute instance numbers for example which become
  1012. * important when one plays with attribute lists. Also,
  1013. * because a proper match has been found in the attribute list
  1014. * entry above, the comparison can now be optimized. So it is
  1015. * worth re-implementing a simplified ntfs_attr_find() here.
  1016. */
  1017. a = ctx->attr;
  1018. /*
  1019. * Use a manual loop so we can still use break and continue
  1020. * with the same meanings as above.
  1021. */
  1022. do_next_attr_loop:
  1023. if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
  1024. le32_to_cpu(ctx->mrec->bytes_allocated))
  1025. break;
  1026. if (a->type == AT_END)
  1027. break;
  1028. if (!a->length)
  1029. break;
  1030. if (al_entry->instance != a->instance)
  1031. goto do_next_attr;
  1032. /*
  1033. * If the type and/or the name are mismatched between the
  1034. * attribute list entry and the attribute record, there is
  1035. * corruption so we break and return error EIO.
  1036. */
  1037. if (al_entry->type != a->type)
  1038. break;
  1039. if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
  1040. le16_to_cpu(a->name_offset)), a->name_length,
  1041. al_name, al_name_len, CASE_SENSITIVE,
  1042. vol->upcase, vol->upcase_len))
  1043. break;
  1044. ctx->attr = a;
  1045. /*
  1046. * If no @val specified or @val specified and it matches, we
  1047. * have found it!
  1048. */
  1049. if (!val || (!a->non_resident && le32_to_cpu(
  1050. a->data.resident.value_length) == val_len &&
  1051. !memcmp((u8*)a +
  1052. le16_to_cpu(a->data.resident.value_offset),
  1053. val, val_len))) {
  1054. ntfs_debug("Done, found.");
  1055. return 0;
  1056. }
  1057. do_next_attr:
  1058. /* Proceed to the next attribute in the current mft record. */
  1059. a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
  1060. goto do_next_attr_loop;
  1061. }
  1062. if (!err) {
  1063. ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
  1064. "attribute list attribute.%s", base_ni->mft_no,
  1065. es);
  1066. err = -EIO;
  1067. }
  1068. if (ni != base_ni) {
  1069. if (ni)
  1070. unmap_extent_mft_record(ni);
  1071. ctx->ntfs_ino = base_ni;
  1072. ctx->mrec = ctx->base_mrec;
  1073. ctx->attr = ctx->base_attr;
  1074. }
  1075. if (err != -ENOMEM)
  1076. NVolSetErrors(vol);
  1077. return err;
  1078. not_found:
  1079. /*
  1080. * If we were looking for AT_END, we reset the search context @ctx and
  1081. * use ntfs_attr_find() to seek to the end of the base mft record.
  1082. */
  1083. if (type == AT_END) {
  1084. ntfs_attr_reinit_search_ctx(ctx);
  1085. return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
  1086. ctx);
  1087. }
  1088. /*
  1089. * The attribute was not found. Before we return, we want to ensure
  1090. * @ctx->mrec and @ctx->attr indicate the position at which the
  1091. * attribute should be inserted in the base mft record. Since we also
  1092. * want to preserve @ctx->al_entry we cannot reinitialize the search
  1093. * context using ntfs_attr_reinit_search_ctx() as this would set
  1094. * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
  1095. * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
  1096. * @ctx->al_entry as the remaining fields (base_*) are identical to
  1097. * their non base_ counterparts and we cannot set @ctx->base_attr
  1098. * correctly yet as we do not know what @ctx->attr will be set to by
  1099. * the call to ntfs_attr_find() below.
  1100. */
  1101. if (ni != base_ni)
  1102. unmap_extent_mft_record(ni);
  1103. ctx->mrec = ctx->base_mrec;
  1104. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  1105. le16_to_cpu(ctx->mrec->attrs_offset));
  1106. ctx->is_first = true;
  1107. ctx->ntfs_ino = base_ni;
  1108. ctx->base_ntfs_ino = NULL;
  1109. ctx->base_mrec = NULL;
  1110. ctx->base_attr = NULL;
  1111. /*
  1112. * In case there are multiple matches in the base mft record, need to
  1113. * keep enumerating until we get an attribute not found response (or
  1114. * another error), otherwise we would keep returning the same attribute
  1115. * over and over again and all programs using us for enumeration would
  1116. * lock up in a tight loop.
  1117. */
  1118. do {
  1119. err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
  1120. ctx);
  1121. } while (!err);
  1122. ntfs_debug("Done, not found.");
  1123. return err;
  1124. }
  1125. /**
  1126. * ntfs_attr_lookup - find an attribute in an ntfs inode
  1127. * @type: attribute type to find
  1128. * @name: attribute name to find (optional, i.e. NULL means don't care)
  1129. * @name_len: attribute name length (only needed if @name present)
  1130. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  1131. * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
  1132. * @val: attribute value to find (optional, resident attributes only)
  1133. * @val_len: attribute value length
  1134. * @ctx: search context with mft record and attribute to search from
  1135. *
  1136. * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
  1137. * be the base mft record and @ctx must have been obtained from a call to
  1138. * ntfs_attr_get_search_ctx().
  1139. *
  1140. * This function transparently handles attribute lists and @ctx is used to
  1141. * continue searches where they were left off at.
  1142. *
  1143. * After finishing with the attribute/mft record you need to call
  1144. * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
  1145. * mapped inodes, etc).
  1146. *
  1147. * Return 0 if the search was successful and -errno if not.
  1148. *
  1149. * When 0, @ctx->attr is the found attribute and it is in mft record
  1150. * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
  1151. * the attribute list entry of the found attribute.
  1152. *
  1153. * When -ENOENT, @ctx->attr is the attribute which collates just after the
  1154. * attribute being searched for, i.e. if one wants to add the attribute to the
  1155. * mft record this is the correct place to insert it into. If an attribute
  1156. * list attribute is present, @ctx->al_entry is the attribute list entry which
  1157. * collates just after the attribute list entry of the attribute being searched
  1158. * for, i.e. if one wants to add the attribute to the mft record this is the
  1159. * correct place to insert its attribute list entry into.
  1160. *
  1161. * When -errno != -ENOENT, an error occurred during the lookup. @ctx->attr is
  1162. * then undefined and in particular you should not rely on it not changing.
  1163. */
  1164. int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
  1165. const u32 name_len, const IGNORE_CASE_BOOL ic,
  1166. const VCN lowest_vcn, const u8 *val, const u32 val_len,
  1167. ntfs_attr_search_ctx *ctx)
  1168. {
  1169. ntfs_inode *base_ni;
  1170. ntfs_debug("Entering.");
  1171. BUG_ON(IS_ERR(ctx->mrec));
  1172. if (ctx->base_ntfs_ino)
  1173. base_ni = ctx->base_ntfs_ino;
  1174. else
  1175. base_ni = ctx->ntfs_ino;
  1176. /* Sanity check, just for debugging really. */
  1177. BUG_ON(!base_ni);
  1178. if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
  1179. return ntfs_attr_find(type, name, name_len, ic, val, val_len,
  1180. ctx);
  1181. return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
  1182. val, val_len, ctx);
  1183. }
  1184. /**
  1185. * ntfs_attr_init_search_ctx - initialize an attribute search context
  1186. * @ctx: attribute search context to initialize
  1187. * @ni: ntfs inode with which to initialize the search context
  1188. * @mrec: mft record with which to initialize the search context
  1189. *
  1190. * Initialize the attribute search context @ctx with @ni and @mrec.
  1191. */
  1192. static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
  1193. ntfs_inode *ni, MFT_RECORD *mrec)
  1194. {
  1195. *ctx = (ntfs_attr_search_ctx) {
  1196. .mrec = mrec,
  1197. /* Sanity checks are performed elsewhere. */
  1198. .attr = (ATTR_RECORD*)((u8*)mrec +
  1199. le16_to_cpu(mrec->attrs_offset)),
  1200. .is_first = true,
  1201. .ntfs_ino = ni,
  1202. };
  1203. }
  1204. /**
  1205. * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
  1206. * @ctx: attribute search context to reinitialize
  1207. *
  1208. * Reinitialize the attribute search context @ctx, unmapping an associated
  1209. * extent mft record if present, and initialize the search context again.
  1210. *
  1211. * This is used when a search for a new attribute is being started to reset
  1212. * the search context to the beginning.
  1213. */
  1214. void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
  1215. {
  1216. if (likely(!ctx->base_ntfs_ino)) {
  1217. /* No attribute list. */
  1218. ctx->is_first = true;
  1219. /* Sanity checks are performed elsewhere. */
  1220. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  1221. le16_to_cpu(ctx->mrec->attrs_offset));
  1222. /*
  1223. * This needs resetting due to ntfs_external_attr_find() which
  1224. * can leave it set despite having zeroed ctx->base_ntfs_ino.
  1225. */
  1226. ctx->al_entry = NULL;
  1227. return;
  1228. } /* Attribute list. */
  1229. if (ctx->ntfs_ino != ctx->base_ntfs_ino)
  1230. unmap_extent_mft_record(ctx->ntfs_ino);
  1231. ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
  1232. return;
  1233. }
  1234. /**
  1235. * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
  1236. * @ni: ntfs inode with which to initialize the search context
  1237. * @mrec: mft record with which to initialize the search context
  1238. *
  1239. * Allocate a new attribute search context, initialize it with @ni and @mrec,
  1240. * and return it. Return NULL if allocation failed.
  1241. */
  1242. ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
  1243. {
  1244. ntfs_attr_search_ctx *ctx;
  1245. ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
  1246. if (ctx)
  1247. ntfs_attr_init_search_ctx(ctx, ni, mrec);
  1248. return ctx;
  1249. }
  1250. /**
  1251. * ntfs_attr_put_search_ctx - release an attribute search context
  1252. * @ctx: attribute search context to free
  1253. *
  1254. * Release the attribute search context @ctx, unmapping an associated extent
  1255. * mft record if present.
  1256. */
  1257. void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
  1258. {
  1259. if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
  1260. unmap_extent_mft_record(ctx->ntfs_ino);
  1261. kmem_cache_free(ntfs_attr_ctx_cache, ctx);
  1262. return;
  1263. }
  1264. #ifdef NTFS_RW
  1265. /**
  1266. * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
  1267. * @vol: ntfs volume to which the attribute belongs
  1268. * @type: attribute type which to find
  1269. *
  1270. * Search for the attribute definition record corresponding to the attribute
  1271. * @type in the $AttrDef system file.
  1272. *
  1273. * Return the attribute type definition record if found and NULL if not found.
  1274. */
  1275. static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
  1276. const ATTR_TYPE type)
  1277. {
  1278. ATTR_DEF *ad;
  1279. BUG_ON(!vol->attrdef);
  1280. BUG_ON(!type);
  1281. for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
  1282. vol->attrdef_size && ad->type; ++ad) {
  1283. /* We have not found it yet, carry on searching. */
  1284. if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
  1285. continue;
  1286. /* We found the attribute; return it. */
  1287. if (likely(ad->type == type))
  1288. return ad;
  1289. /* We have gone too far already. No point in continuing. */
  1290. break;
  1291. }
  1292. /* Attribute not found. */
  1293. ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
  1294. le32_to_cpu(type));
  1295. return NULL;
  1296. }
  1297. /**
  1298. * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
  1299. * @vol: ntfs volume to which the attribute belongs
  1300. * @type: attribute type which to check
  1301. * @size: size which to check
  1302. *
  1303. * Check whether the @size in bytes is valid for an attribute of @type on the
  1304. * ntfs volume @vol. This information is obtained from $AttrDef system file.
  1305. *
  1306. * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
  1307. * listed in $AttrDef.
  1308. */
  1309. int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
  1310. const s64 size)
  1311. {
  1312. ATTR_DEF *ad;
  1313. BUG_ON(size < 0);
  1314. /*
  1315. * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
  1316. * listed in $AttrDef.
  1317. */
  1318. if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
  1319. return -ERANGE;
  1320. /* Get the $AttrDef entry for the attribute @type. */
  1321. ad = ntfs_attr_find_in_attrdef(vol, type);
  1322. if (unlikely(!ad))
  1323. return -ENOENT;
  1324. /* Do the bounds check. */
  1325. if (((sle64_to_cpu(ad->min_size) > 0) &&
  1326. size < sle64_to_cpu(ad->min_size)) ||
  1327. ((sle64_to_cpu(ad->max_size) > 0) && size >
  1328. sle64_to_cpu(ad->max_size)))
  1329. return -ERANGE;
  1330. return 0;
  1331. }
  1332. /**
  1333. * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
  1334. * @vol: ntfs volume to which the attribute belongs
  1335. * @type: attribute type which to check
  1336. *
  1337. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1338. * be non-resident. This information is obtained from $AttrDef system file.
  1339. *
  1340. * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
  1341. * -ENOENT if the attribute is not listed in $AttrDef.
  1342. */
  1343. int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1344. {
  1345. ATTR_DEF *ad;
  1346. /* Find the attribute definition record in $AttrDef. */
  1347. ad = ntfs_attr_find_in_attrdef(vol, type);
  1348. if (unlikely(!ad))
  1349. return -ENOENT;
  1350. /* Check the flags and return the result. */
  1351. if (ad->flags & ATTR_DEF_RESIDENT)
  1352. return -EPERM;
  1353. return 0;
  1354. }
  1355. /**
  1356. * ntfs_attr_can_be_resident - check if an attribute can be resident
  1357. * @vol: ntfs volume to which the attribute belongs
  1358. * @type: attribute type which to check
  1359. *
  1360. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1361. * be resident. This information is derived from our ntfs knowledge and may
  1362. * not be completely accurate, especially when user defined attributes are
  1363. * present. Basically we allow everything to be resident except for index
  1364. * allocation and $EA attributes.
  1365. *
  1366. * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
  1367. *
  1368. * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
  1369. * otherwise windows will not boot (blue screen of death)! We cannot
  1370. * check for this here as we do not know which inode's $Bitmap is
  1371. * being asked about so the caller needs to special case this.
  1372. */
  1373. int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1374. {
  1375. if (type == AT_INDEX_ALLOCATION)
  1376. return -EPERM;
  1377. return 0;
  1378. }
  1379. /**
  1380. * ntfs_attr_record_resize - resize an attribute record
  1381. * @m: mft record containing attribute record
  1382. * @a: attribute record to resize
  1383. * @new_size: new size in bytes to which to resize the attribute record @a
  1384. *
  1385. * Resize the attribute record @a, i.e. the resident part of the attribute, in
  1386. * the mft record @m to @new_size bytes.
  1387. *
  1388. * Return 0 on success and -errno on error. The following error codes are
  1389. * defined:
  1390. * -ENOSPC - Not enough space in the mft record @m to perform the resize.
  1391. *
  1392. * Note: On error, no modifications have been performed whatsoever.
  1393. *
  1394. * Warning: If you make a record smaller without having copied all the data you
  1395. * are interested in the data may be overwritten.
  1396. */
  1397. int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
  1398. {
  1399. ntfs_debug("Entering for new_size %u.", new_size);
  1400. /* Align to 8 bytes if it is not already done. */
  1401. if (new_size & 7)
  1402. new_size = (new_size + 7) & ~7;
  1403. /* If the actual attribute length has changed, move things around. */
  1404. if (new_size != le32_to_cpu(a->length)) {
  1405. u32 new_muse = le32_to_cpu(m->bytes_in_use) -
  1406. le32_to_cpu(a->length) + new_size;
  1407. /* Not enough space in this mft record. */
  1408. if (new_muse > le32_to_cpu(m->bytes_allocated))
  1409. return -ENOSPC;
  1410. /* Move attributes following @a to their new location. */
  1411. memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
  1412. le32_to_cpu(m->bytes_in_use) - ((u8*)a -
  1413. (u8*)m) - le32_to_cpu(a->length));
  1414. /* Adjust @m to reflect the change in used space. */
  1415. m->bytes_in_use = cpu_to_le32(new_muse);
  1416. /* Adjust @a to reflect the new size. */
  1417. if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
  1418. a->length = cpu_to_le32(new_size);
  1419. }
  1420. return 0;
  1421. }
  1422. /**
  1423. * ntfs_resident_attr_value_resize - resize the value of a resident attribute
  1424. * @m: mft record containing attribute record
  1425. * @a: attribute record whose value to resize
  1426. * @new_size: new size in bytes to which to resize the attribute value of @a
  1427. *
  1428. * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
  1429. * If the value is made bigger, the newly allocated space is cleared.
  1430. *
  1431. * Return 0 on success and -errno on error. The following error codes are
  1432. * defined:
  1433. * -ENOSPC - Not enough space in the mft record @m to perform the resize.
  1434. *
  1435. * Note: On error, no modifications have been performed whatsoever.
  1436. *
  1437. * Warning: If you make a record smaller without having copied all the data you
  1438. * are interested in the data may be overwritten.
  1439. */
  1440. int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
  1441. const u32 new_size)
  1442. {
  1443. u32 old_size;
  1444. /* Resize the resident part of the attribute record. */
  1445. if (ntfs_attr_record_resize(m, a,
  1446. le16_to_cpu(a->data.resident.value_offset) + new_size))
  1447. return -ENOSPC;
  1448. /*
  1449. * The resize succeeded! If we made the attribute value bigger, clear
  1450. * the area between the old size and @new_size.
  1451. */
  1452. old_size = le32_to_cpu(a->data.resident.value_length);
  1453. if (new_size > old_size)
  1454. memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
  1455. old_size, 0, new_size - old_size);
  1456. /* Finally update the length of the attribute value. */
  1457. a->data.resident.value_length = cpu_to_le32(new_size);
  1458. return 0;
  1459. }
  1460. /**
  1461. * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
  1462. * @ni: ntfs inode describing the attribute to convert
  1463. * @data_size: size of the resident data to copy to the non-resident attribute
  1464. *
  1465. * Convert the resident ntfs attribute described by the ntfs inode @ni to a
  1466. * non-resident one.
  1467. *
  1468. * @data_size must be equal to the attribute value size. This is needed since
  1469. * we need to know the size before we can map the mft record and our callers
  1470. * always know it. The reason we cannot simply read the size from the vfs
  1471. * inode i_size is that this is not necessarily uptodate. This happens when
  1472. * ntfs_attr_make_non_resident() is called in the ->truncate call path(s).
  1473. *
  1474. * Return 0 on success and -errno on error. The following error return codes
  1475. * are defined:
  1476. * -EPERM - The attribute is not allowed to be non-resident.
  1477. * -ENOMEM - Not enough memory.
  1478. * -ENOSPC - Not enough disk space.
  1479. * -EINVAL - Attribute not defined on the volume.
  1480. * -EIO - I/o error or other error.
  1481. * Note that -ENOSPC is also returned in the case that there is not enough
  1482. * space in the mft record to do the conversion. This can happen when the mft
  1483. * record is already very full. The caller is responsible for trying to make
  1484. * space in the mft record and trying again. FIXME: Do we need a separate
  1485. * error return code for this kind of -ENOSPC or is it always worth trying
  1486. * again in case the attribute may then fit in a resident state so no need to
  1487. * make it non-resident at all? Ho-hum... (AIA)
  1488. *
  1489. * NOTE to self: No changes in the attribute list are required to move from
  1490. * a resident to a non-resident attribute.
  1491. *
  1492. * Locking: - The caller must hold i_mutex on the inode.
  1493. */
  1494. int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
  1495. {
  1496. s64 new_size;
  1497. struct inode *vi = VFS_I(ni);
  1498. ntfs_volume *vol = ni->vol;
  1499. ntfs_inode *base_ni;
  1500. MFT_RECORD *m;
  1501. ATTR_RECORD *a;
  1502. ntfs_attr_search_ctx *ctx;
  1503. struct page *page;
  1504. runlist_element *rl;
  1505. u8 *kaddr;
  1506. unsigned long flags;
  1507. int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
  1508. u32 attr_size;
  1509. u8 old_res_attr_flags;
  1510. /* Check that the attribute is allowed to be non-resident. */
  1511. err = ntfs_attr_can_be_non_resident(vol, ni->type);
  1512. if (unlikely(err)) {
  1513. if (err == -EPERM)
  1514. ntfs_debug("Attribute is not allowed to be "
  1515. "non-resident.");
  1516. else
  1517. ntfs_debug("Attribute not defined on the NTFS "
  1518. "volume!");
  1519. return err;
  1520. }
  1521. /*
  1522. * FIXME: Compressed and encrypted attributes are not supported when
  1523. * writing and we should never have gotten here for them.
  1524. */
  1525. BUG_ON(NInoCompressed(ni));
  1526. BUG_ON(NInoEncrypted(ni));
  1527. /*
  1528. * The size needs to be aligned to a cluster boundary for allocation
  1529. * purposes.
  1530. */
  1531. new_size = (data_size + vol->cluster_size - 1) &
  1532. ~(vol->cluster_size - 1);
  1533. if (new_size > 0) {
  1534. /*
  1535. * Will need the page later and since the page lock nests
  1536. * outside all ntfs locks, we need to get the page now.
  1537. */
  1538. page = find_or_create_page(vi->i_mapping, 0,
  1539. mapping_gfp_mask(vi->i_mapping));
  1540. if (unlikely(!page))
  1541. return -ENOMEM;
  1542. /* Start by allocating clusters to hold the attribute value. */
  1543. rl = ntfs_cluster_alloc(vol, 0, new_size >>
  1544. vol->cluster_size_bits, -1, DATA_ZONE, true);
  1545. if (IS_ERR(rl)) {
  1546. err = PTR_ERR(rl);
  1547. ntfs_debug("Failed to allocate cluster%s, error code "
  1548. "%i.", (new_size >>
  1549. vol->cluster_size_bits) > 1 ? "s" : "",
  1550. err);
  1551. goto page_err_out;
  1552. }
  1553. } else {
  1554. rl = NULL;
  1555. page = NULL;
  1556. }
  1557. /* Determine the size of the mapping pairs array. */
  1558. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
  1559. if (unlikely(mp_size < 0)) {
  1560. err = mp_size;
  1561. ntfs_debug("Failed to get size for mapping pairs array, error "
  1562. "code %i.", err);
  1563. goto rl_err_out;
  1564. }
  1565. down_write(&ni->runlist.lock);
  1566. if (!NInoAttr(ni))
  1567. base_ni = ni;
  1568. else
  1569. base_ni = ni->ext.base_ntfs_ino;
  1570. m = map_mft_record(base_ni);
  1571. if (IS_ERR(m)) {
  1572. err = PTR_ERR(m);
  1573. m = NULL;
  1574. ctx = NULL;
  1575. goto err_out;
  1576. }
  1577. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1578. if (unlikely(!ctx)) {
  1579. err = -ENOMEM;
  1580. goto err_out;
  1581. }
  1582. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1583. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1584. if (unlikely(err)) {
  1585. if (err == -ENOENT)
  1586. err = -EIO;
  1587. goto err_out;
  1588. }
  1589. m = ctx->mrec;
  1590. a = ctx->attr;
  1591. BUG_ON(NInoNonResident(ni));
  1592. BUG_ON(a->non_resident);
  1593. /*
  1594. * Calculate new offsets for the name and the mapping pairs array.
  1595. */
  1596. if (NInoSparse(ni) || NInoCompressed(ni))
  1597. name_ofs = (offsetof(ATTR_REC,
  1598. data.non_resident.compressed_size) +
  1599. sizeof(a->data.non_resident.compressed_size) +
  1600. 7) & ~7;
  1601. else
  1602. name_ofs = (offsetof(ATTR_REC,
  1603. data.non_resident.compressed_size) + 7) & ~7;
  1604. mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
  1605. /*
  1606. * Determine the size of the resident part of the now non-resident
  1607. * attribute record.
  1608. */
  1609. arec_size = (mp_ofs + mp_size + 7) & ~7;
  1610. /*
  1611. * If the page is not uptodate bring it uptodate by copying from the
  1612. * attribute value.
  1613. */
  1614. attr_size = le32_to_cpu(a->data.resident.value_length);
  1615. BUG_ON(attr_size != data_size);
  1616. if (page && !PageUptodate(page)) {
  1617. kaddr = kmap_atomic(page);
  1618. memcpy(kaddr, (u8*)a +
  1619. le16_to_cpu(a->data.resident.value_offset),
  1620. attr_size);
  1621. memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
  1622. kunmap_atomic(kaddr);
  1623. flush_dcache_page(page);
  1624. SetPageUptodate(page);
  1625. }
  1626. /* Backup the attribute flag. */
  1627. old_res_attr_flags = a->data.resident.flags;
  1628. /* Resize the resident part of the attribute record. */
  1629. err = ntfs_attr_record_resize(m, a, arec_size);
  1630. if (unlikely(err))
  1631. goto err_out;
  1632. /*
  1633. * Convert the resident part of the attribute record to describe a
  1634. * non-resident attribute.
  1635. */
  1636. a->non_resident = 1;
  1637. /* Move the attribute name if it exists and update the offset. */
  1638. if (a->name_length)
  1639. memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
  1640. a->name_length * sizeof(ntfschar));
  1641. a->name_offset = cpu_to_le16(name_ofs);
  1642. /* Setup the fields specific to non-resident attributes. */
  1643. a->data.non_resident.lowest_vcn = 0;
  1644. a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
  1645. vol->cluster_size_bits);
  1646. a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
  1647. memset(&a->data.non_resident.reserved, 0,
  1648. sizeof(a->data.non_resident.reserved));
  1649. a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
  1650. a->data.non_resident.data_size =
  1651. a->data.non_resident.initialized_size =
  1652. cpu_to_sle64(attr_size);
  1653. if (NInoSparse(ni) || NInoCompressed(ni)) {
  1654. a->data.non_resident.compression_unit = 0;
  1655. if (NInoCompressed(ni) || vol->major_ver < 3)
  1656. a->data.non_resident.compression_unit = 4;
  1657. a->data.non_resident.compressed_size =
  1658. a->data.non_resident.allocated_size;
  1659. } else
  1660. a->data.non_resident.compression_unit = 0;
  1661. /* Generate the mapping pairs array into the attribute record. */
  1662. err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
  1663. arec_size - mp_ofs, rl, 0, -1, NULL);
  1664. if (unlikely(err)) {
  1665. ntfs_debug("Failed to build mapping pairs, error code %i.",
  1666. err);
  1667. goto undo_err_out;
  1668. }
  1669. /* Setup the in-memory attribute structure to be non-resident. */
  1670. ni->runlist.rl = rl;
  1671. write_lock_irqsave(&ni->size_lock, flags);
  1672. ni->allocated_size = new_size;
  1673. if (NInoSparse(ni) || NInoCompressed(ni)) {
  1674. ni->itype.compressed.size = ni->allocated_size;
  1675. if (a->data.non_resident.compression_unit) {
  1676. ni->itype.compressed.block_size = 1U << (a->data.
  1677. non_resident.compression_unit +
  1678. vol->cluster_size_bits);
  1679. ni->itype.compressed.block_size_bits =
  1680. ffs(ni->itype.compressed.block_size) -
  1681. 1;
  1682. ni->itype.compressed.block_clusters = 1U <<
  1683. a->data.non_resident.compression_unit;
  1684. } else {
  1685. ni->itype.compressed.block_size = 0;
  1686. ni->itype.compressed.block_size_bits = 0;
  1687. ni->itype.compressed.block_clusters = 0;
  1688. }
  1689. vi->i_blocks = ni->itype.compressed.size >> 9;
  1690. } else
  1691. vi->i_blocks = ni->allocated_size >> 9;
  1692. write_unlock_irqrestore(&ni->size_lock, flags);
  1693. /*
  1694. * This needs to be last since the address space operations ->readpage
  1695. * and ->writepage can run concurrently with us as they are not
  1696. * serialized on i_mutex. Note, we are not allowed to fail once we flip
  1697. * this switch, which is another reason to do this last.
  1698. */
  1699. NInoSetNonResident(ni);
  1700. /* Mark the mft record dirty, so it gets written back. */
  1701. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1702. mark_mft_record_dirty(ctx->ntfs_ino);
  1703. ntfs_attr_put_search_ctx(ctx);
  1704. unmap_mft_record(base_ni);
  1705. up_write(&ni->runlist.lock);
  1706. if (page) {
  1707. set_page_dirty(page);
  1708. unlock_page(page);
  1709. page_cache_release(page);
  1710. }
  1711. ntfs_debug("Done.");
  1712. return 0;
  1713. undo_err_out:
  1714. /* Convert the attribute back into a resident attribute. */
  1715. a->non_resident = 0;
  1716. /* Move the attribute name if it exists and update the offset. */
  1717. name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
  1718. sizeof(a->data.resident.reserved) + 7) & ~7;
  1719. if (a->name_length)
  1720. memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
  1721. a->name_length * sizeof(ntfschar));
  1722. mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
  1723. a->name_offset = cpu_to_le16(name_ofs);
  1724. arec_size = (mp_ofs + attr_size + 7) & ~7;
  1725. /* Resize the resident part of the attribute record. */
  1726. err2 = ntfs_attr_record_resize(m, a, arec_size);
  1727. if (unlikely(err2)) {
  1728. /*
  1729. * This cannot happen (well if memory corruption is at work it
  1730. * could happen in theory), but deal with it as well as we can.
  1731. * If the old size is too small, truncate the attribute,
  1732. * otherwise simply give it a larger allocated size.
  1733. * FIXME: Should check whether chkdsk complains when the
  1734. * allocated size is much bigger than the resident value size.
  1735. */
  1736. arec_size = le32_to_cpu(a->length);
  1737. if ((mp_ofs + attr_size) > arec_size) {
  1738. err2 = attr_size;
  1739. attr_size = arec_size - mp_ofs;
  1740. ntfs_error(vol->sb, "Failed to undo partial resident "
  1741. "to non-resident attribute "
  1742. "conversion. Truncating inode 0x%lx, "
  1743. "attribute type 0x%x from %i bytes to "
  1744. "%i bytes to maintain metadata "
  1745. "consistency. THIS MEANS YOU ARE "
  1746. "LOSING %i BYTES DATA FROM THIS %s.",
  1747. vi->i_ino,
  1748. (unsigned)le32_to_cpu(ni->type),
  1749. err2, attr_size, err2 - attr_size,
  1750. ((ni->type == AT_DATA) &&
  1751. !ni->name_len) ? "FILE": "ATTRIBUTE");
  1752. write_lock_irqsave(&ni->size_lock, flags);
  1753. ni->initialized_size = attr_size;
  1754. i_size_write(vi, attr_size);
  1755. write_unlock_irqrestore(&ni->size_lock, flags);
  1756. }
  1757. }
  1758. /* Setup the fields specific to resident attributes. */
  1759. a->data.resident.value_length = cpu_to_le32(attr_size);
  1760. a->data.resident.value_offset = cpu_to_le16(mp_ofs);
  1761. a->data.resident.flags = old_res_attr_flags;
  1762. memset(&a->data.resident.reserved, 0,
  1763. sizeof(a->data.resident.reserved));
  1764. /* Copy the data from the page back to the attribute value. */
  1765. if (page) {
  1766. kaddr = kmap_atomic(page);
  1767. memcpy((u8*)a + mp_ofs, kaddr, attr_size);
  1768. kunmap_atomic(kaddr);
  1769. }
  1770. /* Setup the allocated size in the ntfs inode in case it changed. */
  1771. write_lock_irqsave(&ni->size_lock, flags);
  1772. ni->allocated_size = arec_size - mp_ofs;
  1773. write_unlock_irqrestore(&ni->size_lock, flags);
  1774. /* Mark the mft record dirty, so it gets written back. */
  1775. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1776. mark_mft_record_dirty(ctx->ntfs_ino);
  1777. err_out:
  1778. if (ctx)
  1779. ntfs_attr_put_search_ctx(ctx);
  1780. if (m)
  1781. unmap_mft_record(base_ni);
  1782. ni->runlist.rl = NULL;
  1783. up_write(&ni->runlist.lock);
  1784. rl_err_out:
  1785. if (rl) {
  1786. if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
  1787. ntfs_error(vol->sb, "Failed to release allocated "
  1788. "cluster(s) in error code path. Run "
  1789. "chkdsk to recover the lost "
  1790. "cluster(s).");
  1791. NVolSetErrors(vol);
  1792. }
  1793. ntfs_free(rl);
  1794. page_err_out:
  1795. unlock_page(page);
  1796. page_cache_release(page);
  1797. }
  1798. if (err == -EINVAL)
  1799. err = -EIO;
  1800. return err;
  1801. }
  1802. /**
  1803. * ntfs_attr_extend_allocation - extend the allocated space of an attribute
  1804. * @ni: ntfs inode of the attribute whose allocation to extend
  1805. * @new_alloc_size: new size in bytes to which to extend the allocation to
  1806. * @new_data_size: new size in bytes to which to extend the data to
  1807. * @data_start: beginning of region which is required to be non-sparse
  1808. *
  1809. * Extend the allocated space of an attribute described by the ntfs inode @ni
  1810. * to @new_alloc_size bytes. If @data_start is -1, the whole extension may be
  1811. * implemented as a hole in the file (as long as both the volume and the ntfs
  1812. * inode @ni have sparse support enabled). If @data_start is >= 0, then the
  1813. * region between the old allocated size and @data_start - 1 may be made sparse
  1814. * but the regions between @data_start and @new_alloc_size must be backed by
  1815. * actual clusters.
  1816. *
  1817. * If @new_data_size is -1, it is ignored. If it is >= 0, then the data size
  1818. * of the attribute is extended to @new_data_size. Note that the i_size of the
  1819. * vfs inode is not updated. Only the data size in the base attribute record
  1820. * is updated. The caller has to update i_size separately if this is required.
  1821. * WARNING: It is a BUG() for @new_data_size to be smaller than the old data
  1822. * size as well as for @new_data_size to be greater than @new_alloc_size.
  1823. *
  1824. * For resident attributes this involves resizing the attribute record and if
  1825. * necessary moving it and/or other attributes into extent mft records and/or
  1826. * converting the attribute to a non-resident attribute which in turn involves
  1827. * extending the allocation of a non-resident attribute as described below.
  1828. *
  1829. * For non-resident attributes this involves allocating clusters in the data
  1830. * zone on the volume (except for regions that are being made sparse) and
  1831. * extending the run list to describe the allocated clusters as well as
  1832. * updating the mapping pairs array of the attribute. This in turn involves
  1833. * resizing the attribute record and if necessary moving it and/or other
  1834. * attributes into extent mft records and/or splitting the attribute record
  1835. * into multiple extent attribute records.
  1836. *
  1837. * Also, the attribute list attribute is updated if present and in some of the
  1838. * above cases (the ones where extent mft records/attributes come into play),
  1839. * an attribute list attribute is created if not already present.
  1840. *
  1841. * Return the new allocated size on success and -errno on error. In the case
  1842. * that an error is encountered but a partial extension at least up to
  1843. * @data_start (if present) is possible, the allocation is partially extended
  1844. * and this is returned. This means the caller must check the returned size to
  1845. * determine if the extension was partial. If @data_start is -1 then partial
  1846. * allocations are not performed.
  1847. *
  1848. * WARNING: Do not call ntfs_attr_extend_allocation() for $MFT/$DATA.
  1849. *
  1850. * Locking: This function takes the runlist lock of @ni for writing as well as
  1851. * locking the mft record of the base ntfs inode. These locks are maintained
  1852. * throughout execution of the function. These locks are required so that the
  1853. * attribute can be resized safely and so that it can for example be converted
  1854. * from resident to non-resident safely.
  1855. *
  1856. * TODO: At present attribute list attribute handling is not implemented.
  1857. *
  1858. * TODO: At present it is not safe to call this function for anything other
  1859. * than the $DATA attribute(s) of an uncompressed and unencrypted file.
  1860. */
  1861. s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
  1862. const s64 new_data_size, const s64 data_start)
  1863. {
  1864. VCN vcn;
  1865. s64 ll, allocated_size, start = data_start;
  1866. struct inode *vi = VFS_I(ni);
  1867. ntfs_volume *vol = ni->vol;
  1868. ntfs_inode *base_ni;
  1869. MFT_RECORD *m;
  1870. ATTR_RECORD *a;
  1871. ntfs_attr_search_ctx *ctx;
  1872. runlist_element *rl, *rl2;
  1873. unsigned long flags;
  1874. int err, mp_size;
  1875. u32 attr_len = 0; /* Silence stupid gcc warning. */
  1876. bool mp_rebuilt;
  1877. #ifdef DEBUG
  1878. read_lock_irqsave(&ni->size_lock, flags);
  1879. allocated_size = ni->allocated_size;
  1880. read_unlock_irqrestore(&ni->size_lock, flags);
  1881. ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
  1882. "old_allocated_size 0x%llx, "
  1883. "new_allocated_size 0x%llx, new_data_size 0x%llx, "
  1884. "data_start 0x%llx.", vi->i_ino,
  1885. (unsigned)le32_to_cpu(ni->type),
  1886. (unsigned long long)allocated_size,
  1887. (unsigned long long)new_alloc_size,
  1888. (unsigned long long)new_data_size,
  1889. (unsigned long long)start);
  1890. #endif
  1891. retry_extend:
  1892. /*
  1893. * For non-resident attributes, @start and @new_size need to be aligned
  1894. * to cluster boundaries for allocation purposes.
  1895. */
  1896. if (NInoNonResident(ni)) {
  1897. if (start > 0)
  1898. start &= ~(s64)vol->cluster_size_mask;
  1899. new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
  1900. ~(s64)vol->cluster_size_mask;
  1901. }
  1902. BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
  1903. /* Check if new size is allowed in $AttrDef. */
  1904. err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
  1905. if (unlikely(err)) {
  1906. /* Only emit errors when the write will fail completely. */
  1907. read_lock_irqsave(&ni->size_lock, flags);
  1908. allocated_size = ni->allocated_size;
  1909. read_unlock_irqrestore(&ni->size_lock, flags);
  1910. if (start < 0 || start >= allocated_size) {
  1911. if (err == -ERANGE) {
  1912. ntfs_error(vol->sb, "Cannot extend allocation "
  1913. "of inode 0x%lx, attribute "
  1914. "type 0x%x, because the new "
  1915. "allocation would exceed the "
  1916. "maximum allowed size for "
  1917. "this attribute type.",
  1918. vi->i_ino, (unsigned)
  1919. le32_to_cpu(ni->type));
  1920. } else {
  1921. ntfs_error(vol->sb, "Cannot extend allocation "
  1922. "of inode 0x%lx, attribute "
  1923. "type 0x%x, because this "
  1924. "attribute type is not "
  1925. "defined on the NTFS volume. "
  1926. "Possible corruption! You "
  1927. "should run chkdsk!",
  1928. vi->i_ino, (unsigned)
  1929. le32_to_cpu(ni->type));
  1930. }
  1931. }
  1932. /* Translate error code to be POSIX conformant for write(2). */
  1933. if (err == -ERANGE)
  1934. err = -EFBIG;
  1935. else
  1936. err = -EIO;
  1937. return err;
  1938. }
  1939. if (!NInoAttr(ni))
  1940. base_ni = ni;
  1941. else
  1942. base_ni = ni->ext.base_ntfs_ino;
  1943. /*
  1944. * We will be modifying both the runlist (if non-resident) and the mft
  1945. * record so lock them both down.
  1946. */
  1947. down_write(&ni->runlist.lock);
  1948. m = map_mft_record(base_ni);
  1949. if (IS_ERR(m)) {
  1950. err = PTR_ERR(m);
  1951. m = NULL;
  1952. ctx = NULL;
  1953. goto err_out;
  1954. }
  1955. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1956. if (unlikely(!ctx)) {
  1957. err = -ENOMEM;
  1958. goto err_out;
  1959. }
  1960. read_lock_irqsave(&ni->size_lock, flags);
  1961. allocated_size = ni->allocated_size;
  1962. read_unlock_irqrestore(&ni->size_lock, flags);
  1963. /*
  1964. * If non-resident, seek to the last extent. If resident, there is
  1965. * only one extent, so seek to that.
  1966. */
  1967. vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
  1968. 0;
  1969. /*
  1970. * Abort if someone did the work whilst we waited for the locks. If we
  1971. * just converted the attribute from resident to non-resident it is
  1972. * likely that exactly this has happened already. We cannot quite
  1973. * abort if we need to update the data size.
  1974. */
  1975. if (unlikely(new_alloc_size <= allocated_size)) {
  1976. ntfs_debug("Allocated size already exceeds requested size.");
  1977. new_alloc_size = allocated_size;
  1978. if (new_data_size < 0)
  1979. goto done;
  1980. /*
  1981. * We want the first attribute extent so that we can update the
  1982. * data size.
  1983. */
  1984. vcn = 0;
  1985. }
  1986. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1987. CASE_SENSITIVE, vcn, NULL, 0, ctx);
  1988. if (unlikely(err)) {
  1989. if (err == -ENOENT)
  1990. err = -EIO;
  1991. goto err_out;
  1992. }
  1993. m = ctx->mrec;
  1994. a = ctx->attr;
  1995. /* Use goto to reduce indentation. */
  1996. if (a->non_resident)
  1997. goto do_non_resident_extend;
  1998. BUG_ON(NInoNonResident(ni));
  1999. /* The total length of the attribute value. */
  2000. attr_len = le32_to_cpu(a->data.resident.value_length);
  2001. /*
  2002. * Extend the attribute record to be able to store the new attribute
  2003. * size. ntfs_attr_record_resize() will not do anything if the size is
  2004. * not changing.
  2005. */
  2006. if (new_alloc_size < vol->mft_record_size &&
  2007. !ntfs_attr_record_resize(m, a,
  2008. le16_to_cpu(a->data.resident.value_offset) +
  2009. new_alloc_size)) {
  2010. /* The resize succeeded! */
  2011. write_lock_irqsave(&ni->size_lock, flags);
  2012. ni->allocated_size = le32_to_cpu(a->length) -
  2013. le16_to_cpu(a->data.resident.value_offset);
  2014. write_unlock_irqrestore(&ni->size_lock, flags);
  2015. if (new_data_size >= 0) {
  2016. BUG_ON(new_data_size < attr_len);
  2017. a->data.resident.value_length =
  2018. cpu_to_le32((u32)new_data_size);
  2019. }
  2020. goto flush_done;
  2021. }
  2022. /*
  2023. * We have to drop all the locks so we can call
  2024. * ntfs_attr_make_non_resident(). This could be optimised by try-
  2025. * locking the first page cache page and only if that fails dropping
  2026. * the locks, locking the page, and redoing all the locking and
  2027. * lookups. While this would be a huge optimisation, it is not worth
  2028. * it as this is definitely a slow code path.
  2029. */
  2030. ntfs_attr_put_search_ctx(ctx);
  2031. unmap_mft_record(base_ni);
  2032. up_write(&ni->runlist.lock);
  2033. /*
  2034. * Not enough space in the mft record, try to make the attribute
  2035. * non-resident and if successful restart the extension process.
  2036. */
  2037. err = ntfs_attr_make_non_resident(ni, attr_len);
  2038. if (likely(!err))
  2039. goto retry_extend;
  2040. /*
  2041. * Could not make non-resident. If this is due to this not being
  2042. * permitted for this attribute type or there not being enough space,
  2043. * try to make other attributes non-resident. Otherwise fail.
  2044. */
  2045. if (unlikely(err != -EPERM && err != -ENOSPC)) {
  2046. /* Only emit errors when the write will fail completely. */
  2047. read_lock_irqsave(&ni->size_lock, flags);
  2048. allocated_size = ni->allocated_size;
  2049. read_unlock_irqrestore(&ni->size_lock, flags);
  2050. if (start < 0 || start >= allocated_size)
  2051. ntfs_error(vol->sb, "Cannot extend allocation of "
  2052. "inode 0x%lx, attribute type 0x%x, "
  2053. "because the conversion from resident "
  2054. "to non-resident attribute failed "
  2055. "with error code %i.", vi->i_ino,
  2056. (unsigned)le32_to_cpu(ni->type), err);
  2057. if (err != -ENOMEM)
  2058. err = -EIO;
  2059. goto conv_err_out;
  2060. }
  2061. /* TODO: Not implemented from here, abort. */
  2062. read_lock_irqsave(&ni->size_lock, flags);
  2063. allocated_size = ni->allocated_size;
  2064. read_unlock_irqrestore(&ni->size_lock, flags);
  2065. if (start < 0 || start >= allocated_size) {
  2066. if (err == -ENOSPC)
  2067. ntfs_error(vol->sb, "Not enough space in the mft "
  2068. "record/on disk for the non-resident "
  2069. "attribute value. This case is not "
  2070. "implemented yet.");
  2071. else /* if (err == -EPERM) */
  2072. ntfs_error(vol->sb, "This attribute type may not be "
  2073. "non-resident. This case is not "
  2074. "implemented yet.");
  2075. }
  2076. err = -EOPNOTSUPP;
  2077. goto conv_err_out;
  2078. #if 0
  2079. // TODO: Attempt to make other attributes non-resident.
  2080. if (!err)
  2081. goto do_resident_extend;
  2082. /*
  2083. * Both the attribute list attribute and the standard information
  2084. * attribute must remain in the base inode. Thus, if this is one of
  2085. * these attributes, we have to try to move other attributes out into
  2086. * extent mft records instead.
  2087. */
  2088. if (ni->type == AT_ATTRIBUTE_LIST ||
  2089. ni->type == AT_STANDARD_INFORMATION) {
  2090. // TODO: Attempt to move other attributes into extent mft
  2091. // records.
  2092. err = -EOPNOTSUPP;
  2093. if (!err)
  2094. goto do_resident_extend;
  2095. goto err_out;
  2096. }
  2097. // TODO: Attempt to move this attribute to an extent mft record, but
  2098. // only if it is not already the only attribute in an mft record in
  2099. // which case there would be nothing to gain.
  2100. err = -EOPNOTSUPP;
  2101. if (!err)
  2102. goto do_resident_extend;
  2103. /* There is nothing we can do to make enough space. )-: */
  2104. goto err_out;
  2105. #endif
  2106. do_non_resident_extend:
  2107. BUG_ON(!NInoNonResident(ni));
  2108. if (new_alloc_size == allocated_size) {
  2109. BUG_ON(vcn);
  2110. goto alloc_done;
  2111. }
  2112. /*
  2113. * If the data starts after the end of the old allocation, this is a
  2114. * $DATA attribute and sparse attributes are enabled on the volume and
  2115. * for this inode, then create a sparse region between the old
  2116. * allocated size and the start of the data. Otherwise simply proceed
  2117. * with filling the whole space between the old allocated size and the
  2118. * new allocated size with clusters.
  2119. */
  2120. if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
  2121. !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
  2122. goto skip_sparse;
  2123. // TODO: This is not implemented yet. We just fill in with real
  2124. // clusters for now...
  2125. ntfs_debug("Inserting holes is not-implemented yet. Falling back to "
  2126. "allocating real clusters instead.");
  2127. skip_sparse:
  2128. rl = ni->runlist.rl;
  2129. if (likely(rl)) {
  2130. /* Seek to the end of the runlist. */
  2131. while (rl->length)
  2132. rl++;
  2133. }
  2134. /* If this attribute extent is not mapped, map it now. */
  2135. if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
  2136. (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
  2137. (rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
  2138. if (!rl && !allocated_size)
  2139. goto first_alloc;
  2140. rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
  2141. if (IS_ERR(rl)) {
  2142. err = PTR_ERR(rl);
  2143. if (start < 0 || start >= allocated_size)
  2144. ntfs_error(vol->sb, "Cannot extend allocation "
  2145. "of inode 0x%lx, attribute "
  2146. "type 0x%x, because the "
  2147. "mapping of a runlist "
  2148. "fragment failed with error "
  2149. "code %i.", vi->i_ino,
  2150. (unsigned)le32_to_cpu(ni->type),
  2151. err);
  2152. if (err != -ENOMEM)
  2153. err = -EIO;
  2154. goto err_out;
  2155. }
  2156. ni->runlist.rl = rl;
  2157. /* Seek to the end of the runlist. */
  2158. while (rl->length)
  2159. rl++;
  2160. }
  2161. /*
  2162. * We now know the runlist of the last extent is mapped and @rl is at
  2163. * the end of the runlist. We want to begin allocating clusters
  2164. * starting at the last allocated cluster to reduce fragmentation. If
  2165. * there are no valid LCNs in the attribute we let the cluster
  2166. * allocator choose the starting cluster.
  2167. */
  2168. /* If the last LCN is a hole or simillar seek back to last real LCN. */
  2169. while (rl->lcn < 0 && rl > ni->runlist.rl)
  2170. rl--;
  2171. first_alloc:
  2172. // FIXME: Need to implement partial allocations so at least part of the
  2173. // write can be performed when start >= 0. (Needed for POSIX write(2)
  2174. // conformance.)
  2175. rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
  2176. (new_alloc_size - allocated_size) >>
  2177. vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
  2178. rl->lcn + rl->length : -1, DATA_ZONE, true);
  2179. if (IS_ERR(rl2)) {
  2180. err = PTR_ERR(rl2);
  2181. if (start < 0 || start >= allocated_size)
  2182. ntfs_error(vol->sb, "Cannot extend allocation of "
  2183. "inode 0x%lx, attribute type 0x%x, "
  2184. "because the allocation of clusters "
  2185. "failed with error code %i.", vi->i_ino,
  2186. (unsigned)le32_to_cpu(ni->type), err);
  2187. if (err != -ENOMEM && err != -ENOSPC)
  2188. err = -EIO;
  2189. goto err_out;
  2190. }
  2191. rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
  2192. if (IS_ERR(rl)) {
  2193. err = PTR_ERR(rl);
  2194. if (start < 0 || start >= allocated_size)
  2195. ntfs_error(vol->sb, "Cannot extend allocation of "
  2196. "inode 0x%lx, attribute type 0x%x, "
  2197. "because the runlist merge failed "
  2198. "with error code %i.", vi->i_ino,
  2199. (unsigned)le32_to_cpu(ni->type), err);
  2200. if (err != -ENOMEM)
  2201. err = -EIO;
  2202. if (ntfs_cluster_free_from_rl(vol, rl2)) {
  2203. ntfs_error(vol->sb, "Failed to release allocated "
  2204. "cluster(s) in error code path. Run "
  2205. "chkdsk to recover the lost "
  2206. "cluster(s).");
  2207. NVolSetErrors(vol);
  2208. }
  2209. ntfs_free(rl2);
  2210. goto err_out;
  2211. }
  2212. ni->runlist.rl = rl;
  2213. ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
  2214. allocated_size) >> vol->cluster_size_bits);
  2215. /* Find the runlist element with which the attribute extent starts. */
  2216. ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
  2217. rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
  2218. BUG_ON(!rl2);
  2219. BUG_ON(!rl2->length);
  2220. BUG_ON(rl2->lcn < LCN_HOLE);
  2221. mp_rebuilt = false;
  2222. /* Get the size for the new mapping pairs array for this extent. */
  2223. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
  2224. if (unlikely(mp_size <= 0)) {
  2225. err = mp_size;
  2226. if (start < 0 || start >= allocated_size)
  2227. ntfs_error(vol->sb, "Cannot extend allocation of "
  2228. "inode 0x%lx, attribute type 0x%x, "
  2229. "because determining the size for the "
  2230. "mapping pairs failed with error code "
  2231. "%i.", vi->i_ino,
  2232. (unsigned)le32_to_cpu(ni->type), err);
  2233. err = -EIO;
  2234. goto undo_alloc;
  2235. }
  2236. /* Extend the attribute record to fit the bigger mapping pairs array. */
  2237. attr_len = le32_to_cpu(a->length);
  2238. err = ntfs_attr_record_resize(m, a, mp_size +
  2239. le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
  2240. if (unlikely(err)) {
  2241. BUG_ON(err != -ENOSPC);
  2242. // TODO: Deal with this by moving this extent to a new mft
  2243. // record or by starting a new extent in a new mft record,
  2244. // possibly by extending this extent partially and filling it
  2245. // and creating a new extent for the remainder, or by making
  2246. // other attributes non-resident and/or by moving other
  2247. // attributes out of this mft record.
  2248. if (start < 0 || start >= allocated_size)
  2249. ntfs_error(vol->sb, "Not enough space in the mft "
  2250. "record for the extended attribute "
  2251. "record. This case is not "
  2252. "implemented yet.");
  2253. err = -EOPNOTSUPP;
  2254. goto undo_alloc;
  2255. }
  2256. mp_rebuilt = true;
  2257. /* Generate the mapping pairs array directly into the attr record. */
  2258. err = ntfs_mapping_pairs_build(vol, (u8*)a +
  2259. le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
  2260. mp_size, rl2, ll, -1, NULL);
  2261. if (unlikely(err)) {
  2262. if (start < 0 || start >= allocated_size)
  2263. ntfs_error(vol->sb, "Cannot extend allocation of "
  2264. "inode 0x%lx, attribute type 0x%x, "
  2265. "because building the mapping pairs "
  2266. "failed with error code %i.", vi->i_ino,
  2267. (unsigned)le32_to_cpu(ni->type), err);
  2268. err = -EIO;
  2269. goto undo_alloc;
  2270. }
  2271. /* Update the highest_vcn. */
  2272. a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
  2273. vol->cluster_size_bits) - 1);
  2274. /*
  2275. * We now have extended the allocated size of the attribute. Reflect
  2276. * this in the ntfs_inode structure and the attribute record.
  2277. */
  2278. if (a->data.non_resident.lowest_vcn) {
  2279. /*
  2280. * We are not in the first attribute extent, switch to it, but
  2281. * first ensure the changes will make it to disk later.
  2282. */
  2283. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2284. mark_mft_record_dirty(ctx->ntfs_ino);
  2285. ntfs_attr_reinit_search_ctx(ctx);
  2286. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  2287. CASE_SENSITIVE, 0, NULL, 0, ctx);
  2288. if (unlikely(err))
  2289. goto restore_undo_alloc;
  2290. /* @m is not used any more so no need to set it. */
  2291. a = ctx->attr;
  2292. }
  2293. write_lock_irqsave(&ni->size_lock, flags);
  2294. ni->allocated_size = new_alloc_size;
  2295. a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
  2296. /*
  2297. * FIXME: This would fail if @ni is a directory, $MFT, or an index,
  2298. * since those can have sparse/compressed set. For example can be
  2299. * set compressed even though it is not compressed itself and in that
  2300. * case the bit means that files are to be created compressed in the
  2301. * directory... At present this is ok as this code is only called for
  2302. * regular files, and only for their $DATA attribute(s).
  2303. * FIXME: The calculation is wrong if we created a hole above. For now
  2304. * it does not matter as we never create holes.
  2305. */
  2306. if (NInoSparse(ni) || NInoCompressed(ni)) {
  2307. ni->itype.compressed.size += new_alloc_size - allocated_size;
  2308. a->data.non_resident.compressed_size =
  2309. cpu_to_sle64(ni->itype.compressed.size);
  2310. vi->i_blocks = ni->itype.compressed.size >> 9;
  2311. } else
  2312. vi->i_blocks = new_alloc_size >> 9;
  2313. write_unlock_irqrestore(&ni->size_lock, flags);
  2314. alloc_done:
  2315. if (new_data_size >= 0) {
  2316. BUG_ON(new_data_size <
  2317. sle64_to_cpu(a->data.non_resident.data_size));
  2318. a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
  2319. }
  2320. flush_done:
  2321. /* Ensure the changes make it to disk. */
  2322. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2323. mark_mft_record_dirty(ctx->ntfs_ino);
  2324. done:
  2325. ntfs_attr_put_search_ctx(ctx);
  2326. unmap_mft_record(base_ni);
  2327. up_write(&ni->runlist.lock);
  2328. ntfs_debug("Done, new_allocated_size 0x%llx.",
  2329. (unsigned long long)new_alloc_size);
  2330. return new_alloc_size;
  2331. restore_undo_alloc:
  2332. if (start < 0 || start >= allocated_size)
  2333. ntfs_error(vol->sb, "Cannot complete extension of allocation "
  2334. "of inode 0x%lx, attribute type 0x%x, because "
  2335. "lookup of first attribute extent failed with "
  2336. "error code %i.", vi->i_ino,
  2337. (unsigned)le32_to_cpu(ni->type), err);
  2338. if (err == -ENOENT)
  2339. err = -EIO;
  2340. ntfs_attr_reinit_search_ctx(ctx);
  2341. if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
  2342. allocated_size >> vol->cluster_size_bits, NULL, 0,
  2343. ctx)) {
  2344. ntfs_error(vol->sb, "Failed to find last attribute extent of "
  2345. "attribute in error code path. Run chkdsk to "
  2346. "recover.");
  2347. write_lock_irqsave(&ni->size_lock, flags);
  2348. ni->allocated_size = new_alloc_size;
  2349. /*
  2350. * FIXME: This would fail if @ni is a directory... See above.
  2351. * FIXME: The calculation is wrong if we created a hole above.
  2352. * For now it does not matter as we never create holes.
  2353. */
  2354. if (NInoSparse(ni) || NInoCompressed(ni)) {
  2355. ni->itype.compressed.size += new_alloc_size -
  2356. allocated_size;
  2357. vi->i_blocks = ni->itype.compressed.size >> 9;
  2358. } else
  2359. vi->i_blocks = new_alloc_size >> 9;
  2360. write_unlock_irqrestore(&ni->size_lock, flags);
  2361. ntfs_attr_put_search_ctx(ctx);
  2362. unmap_mft_record(base_ni);
  2363. up_write(&ni->runlist.lock);
  2364. /*
  2365. * The only thing that is now wrong is the allocated size of the
  2366. * base attribute extent which chkdsk should be able to fix.
  2367. */
  2368. NVolSetErrors(vol);
  2369. return err;
  2370. }
  2371. ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
  2372. (allocated_size >> vol->cluster_size_bits) - 1);
  2373. undo_alloc:
  2374. ll = allocated_size >> vol->cluster_size_bits;
  2375. if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
  2376. ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
  2377. "in error code path. Run chkdsk to recover "
  2378. "the lost cluster(s).");
  2379. NVolSetErrors(vol);
  2380. }
  2381. m = ctx->mrec;
  2382. a = ctx->attr;
  2383. /*
  2384. * If the runlist truncation fails and/or the search context is no
  2385. * longer valid, we cannot resize the attribute record or build the
  2386. * mapping pairs array thus we mark the inode bad so that no access to
  2387. * the freed clusters can happen.
  2388. */
  2389. if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
  2390. ntfs_error(vol->sb, "Failed to %s in error code path. Run "
  2391. "chkdsk to recover.", IS_ERR(m) ?
  2392. "restore attribute search context" :
  2393. "truncate attribute runlist");
  2394. NVolSetErrors(vol);
  2395. } else if (mp_rebuilt) {
  2396. if (ntfs_attr_record_resize(m, a, attr_len)) {
  2397. ntfs_error(vol->sb, "Failed to restore attribute "
  2398. "record in error code path. Run "
  2399. "chkdsk to recover.");
  2400. NVolSetErrors(vol);
  2401. } else /* if (success) */ {
  2402. if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
  2403. a->data.non_resident.
  2404. mapping_pairs_offset), attr_len -
  2405. le16_to_cpu(a->data.non_resident.
  2406. mapping_pairs_offset), rl2, ll, -1,
  2407. NULL)) {
  2408. ntfs_error(vol->sb, "Failed to restore "
  2409. "mapping pairs array in error "
  2410. "code path. Run chkdsk to "
  2411. "recover.");
  2412. NVolSetErrors(vol);
  2413. }
  2414. flush_dcache_mft_record_page(ctx->ntfs_ino);
  2415. mark_mft_record_dirty(ctx->ntfs_ino);
  2416. }
  2417. }
  2418. err_out:
  2419. if (ctx)
  2420. ntfs_attr_put_search_ctx(ctx);
  2421. if (m)
  2422. unmap_mft_record(base_ni);
  2423. up_write(&ni->runlist.lock);
  2424. conv_err_out:
  2425. ntfs_debug("Failed. Returning error code %i.", err);
  2426. return err;
  2427. }
  2428. /**
  2429. * ntfs_attr_set - fill (a part of) an attribute with a byte
  2430. * @ni: ntfs inode describing the attribute to fill
  2431. * @ofs: offset inside the attribute at which to start to fill
  2432. * @cnt: number of bytes to fill
  2433. * @val: the unsigned 8-bit value with which to fill the attribute
  2434. *
  2435. * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
  2436. * byte offset @ofs inside the attribute with the constant byte @val.
  2437. *
  2438. * This function is effectively like memset() applied to an ntfs attribute.
  2439. * Note thie function actually only operates on the page cache pages belonging
  2440. * to the ntfs attribute and it marks them dirty after doing the memset().
  2441. * Thus it relies on the vm dirty page write code paths to cause the modified
  2442. * pages to be written to the mft record/disk.
  2443. *
  2444. * Return 0 on success and -errno on error. An error code of -ESPIPE means
  2445. * that @ofs + @cnt were outside the end of the attribute and no write was
  2446. * performed.
  2447. */
  2448. int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
  2449. {
  2450. ntfs_volume *vol = ni->vol;
  2451. struct address_space *mapping;
  2452. struct page *page;
  2453. u8 *kaddr;
  2454. pgoff_t idx, end;
  2455. unsigned start_ofs, end_ofs, size;
  2456. ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
  2457. (long long)ofs, (long long)cnt, val);
  2458. BUG_ON(ofs < 0);
  2459. BUG_ON(cnt < 0);
  2460. if (!cnt)
  2461. goto done;
  2462. /*
  2463. * FIXME: Compressed and encrypted attributes are not supported when
  2464. * writing and we should never have gotten here for them.
  2465. */
  2466. BUG_ON(NInoCompressed(ni));
  2467. BUG_ON(NInoEncrypted(ni));
  2468. mapping = VFS_I(ni)->i_mapping;
  2469. /* Work out the starting index and page offset. */
  2470. idx = ofs >> PAGE_CACHE_SHIFT;
  2471. start_ofs = ofs & ~PAGE_CACHE_MASK;
  2472. /* Work out the ending index and page offset. */
  2473. end = ofs + cnt;
  2474. end_ofs = end & ~PAGE_CACHE_MASK;
  2475. /* If the end is outside the inode size return -ESPIPE. */
  2476. if (unlikely(end > i_size_read(VFS_I(ni)))) {
  2477. ntfs_error(vol->sb, "Request exceeds end of attribute.");
  2478. return -ESPIPE;
  2479. }
  2480. end >>= PAGE_CACHE_SHIFT;
  2481. /* If there is a first partial page, need to do it the slow way. */
  2482. if (start_ofs) {
  2483. page = read_mapping_page(mapping, idx, NULL);
  2484. if (IS_ERR(page)) {
  2485. ntfs_error(vol->sb, "Failed to read first partial "
  2486. "page (error, index 0x%lx).", idx);
  2487. return PTR_ERR(page);
  2488. }
  2489. /*
  2490. * If the last page is the same as the first page, need to
  2491. * limit the write to the end offset.
  2492. */
  2493. size = PAGE_CACHE_SIZE;
  2494. if (idx == end)
  2495. size = end_ofs;
  2496. kaddr = kmap_atomic(page);
  2497. memset(kaddr + start_ofs, val, size - start_ofs);
  2498. flush_dcache_page(page);
  2499. kunmap_atomic(kaddr);
  2500. set_page_dirty(page);
  2501. page_cache_release(page);
  2502. balance_dirty_pages_ratelimited(mapping);
  2503. cond_resched();
  2504. if (idx == end)
  2505. goto done;
  2506. idx++;
  2507. }
  2508. /* Do the whole pages the fast way. */
  2509. for (; idx < end; idx++) {
  2510. /* Find or create the current page. (The page is locked.) */
  2511. page = grab_cache_page(mapping, idx);
  2512. if (unlikely(!page)) {
  2513. ntfs_error(vol->sb, "Insufficient memory to grab "
  2514. "page (index 0x%lx).", idx);
  2515. return -ENOMEM;
  2516. }
  2517. kaddr = kmap_atomic(page);
  2518. memset(kaddr, val, PAGE_CACHE_SIZE);
  2519. flush_dcache_page(page);
  2520. kunmap_atomic(kaddr);
  2521. /*
  2522. * If the page has buffers, mark them uptodate since buffer
  2523. * state and not page state is definitive in 2.6 kernels.
  2524. */
  2525. if (page_has_buffers(page)) {
  2526. struct buffer_head *bh, *head;
  2527. bh = head = page_buffers(page);
  2528. do {
  2529. set_buffer_uptodate(bh);
  2530. } while ((bh = bh->b_this_page) != head);
  2531. }
  2532. /* Now that buffers are uptodate, set the page uptodate, too. */
  2533. SetPageUptodate(page);
  2534. /*
  2535. * Set the page and all its buffers dirty and mark the inode
  2536. * dirty, too. The VM will write the page later on.
  2537. */
  2538. set_page_dirty(page);
  2539. /* Finally unlock and release the page. */
  2540. unlock_page(page);
  2541. page_cache_release(page);
  2542. balance_dirty_pages_ratelimited(mapping);
  2543. cond_resched();
  2544. }
  2545. /* If there is a last partial page, need to do it the slow way. */
  2546. if (end_ofs) {
  2547. page = read_mapping_page(mapping, idx, NULL);
  2548. if (IS_ERR(page)) {
  2549. ntfs_error(vol->sb, "Failed to read last partial page "
  2550. "(error, index 0x%lx).", idx);
  2551. return PTR_ERR(page);
  2552. }
  2553. kaddr = kmap_atomic(page);
  2554. memset(kaddr, val, end_ofs);
  2555. flush_dcache_page(page);
  2556. kunmap_atomic(kaddr);
  2557. set_page_dirty(page);
  2558. page_cache_release(page);
  2559. balance_dirty_pages_ratelimited(mapping);
  2560. cond_resched();
  2561. }
  2562. done:
  2563. ntfs_debug("Done.");
  2564. return 0;
  2565. }
  2566. #endif /* NTFS_RW */