attrib.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /**
  2. * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2007 Anton Altaparmakov
  5. * Copyright (c) 2002 Richard Russon
  6. *
  7. * This program/include file is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program/include file is distributed in the hope that it will be
  13. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program (in the main directory of the Linux-NTFS
  19. * distribution in the file COPYING); if not, write to the Free Software
  20. * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/buffer_head.h>
  23. #include <linux/sched.h>
  24. #include <linux/swap.h>
  25. #include <linux/writeback.h>
  26. #include "attrib.h"
  27. #include "debug.h"
  28. #include "layout.h"
  29. #include "lcnalloc.h"
  30. #include "malloc.h"
  31. #include "mft.h"
  32. #include "ntfs.h"
  33. #include "types.h"
  34. /**
  35. * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
  36. * @ni: ntfs inode for which to map (part of) a runlist
  37. * @vcn: map runlist part containing this vcn
  38. * @ctx: active attribute search context if present or NULL if not
  39. *
  40. * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
  41. *
  42. * If @ctx is specified, it is an active search context of @ni and its base mft
  43. * record. This is needed when ntfs_map_runlist_nolock() encounters unmapped
  44. * runlist fragments and allows their mapping. If you do not have the mft
  45. * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
  46. * will perform the necessary mapping and unmapping.
  47. *
  48. * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
  49. * restores it before returning. Thus, @ctx will be left pointing to the same
  50. * attribute on return as on entry. However, the actual pointers in @ctx may
  51. * point to different memory locations on return, so you must remember to reset
  52. * any cached pointers from the @ctx, i.e. after the call to
  53. * ntfs_map_runlist_nolock(), you will probably want to do:
  54. * m = ctx->mrec;
  55. * a = ctx->attr;
  56. * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
  57. * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
  58. *
  59. * Return 0 on success and -errno on error. There is one special error code
  60. * which is not an error as such. This is -ENOENT. It means that @vcn is out
  61. * of bounds of the runlist.
  62. *
  63. * Note the runlist can be NULL after this function returns if @vcn is zero and
  64. * the attribute has zero allocated size, i.e. there simply is no runlist.
  65. *
  66. * WARNING: If @ctx is supplied, regardless of whether success or failure is
  67. * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
  68. * is no longer valid, i.e. you need to either call
  69. * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
  70. * In that case PTR_ERR(@ctx->mrec) will give you the error code for
  71. * why the mapping of the old inode failed.
  72. *
  73. * Locking: - The runlist described by @ni must be locked for writing on entry
  74. * and is locked on return. Note the runlist will be modified.
  75. * - If @ctx is NULL, the base mft record of @ni must not be mapped on
  76. * entry and it will be left unmapped on return.
  77. * - If @ctx is not NULL, the base mft record must be mapped on entry
  78. * and it will be left mapped on return.
  79. */
  80. int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
  81. {
  82. VCN end_vcn;
  83. unsigned long flags;
  84. ntfs_inode *base_ni;
  85. MFT_RECORD *m;
  86. ATTR_RECORD *a;
  87. runlist_element *rl;
  88. struct page *put_this_page = NULL;
  89. int err = 0;
  90. bool ctx_is_temporary, ctx_needs_reset;
  91. ntfs_attr_search_ctx old_ctx = { NULL, };
  92. ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
  93. (unsigned long long)vcn);
  94. if (!NInoAttr(ni))
  95. base_ni = ni;
  96. else
  97. base_ni = ni->ext.base_ntfs_ino;
  98. if (!ctx) {
  99. ctx_is_temporary = ctx_needs_reset = true;
  100. m = map_mft_record(base_ni);
  101. if (IS_ERR(m))
  102. return PTR_ERR(m);
  103. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  104. if (unlikely(!ctx)) {
  105. err = -ENOMEM;
  106. goto err_out;
  107. }
  108. } else {
  109. VCN allocated_size_vcn;
  110. BUG_ON(IS_ERR(ctx->mrec));
  111. a = ctx->attr;
  112. BUG_ON(!a->non_resident);
  113. ctx_is_temporary = false;
  114. end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  115. read_lock_irqsave(&ni->size_lock, flags);
  116. allocated_size_vcn = ni->allocated_size >>
  117. ni->vol->cluster_size_bits;
  118. read_unlock_irqrestore(&ni->size_lock, flags);
  119. if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
  120. end_vcn = allocated_size_vcn - 1;
  121. /*
  122. * If we already have the attribute extent containing @vcn in
  123. * @ctx, no need to look it up again. We slightly cheat in
  124. * that if vcn exceeds the allocated size, we will refuse to
  125. * map the runlist below, so there is definitely no need to get
  126. * the right attribute extent.
  127. */
  128. if (vcn >= allocated_size_vcn || (a->type == ni->type &&
  129. a->name_length == ni->name_len &&
  130. !memcmp((u8*)a + le16_to_cpu(a->name_offset),
  131. ni->name, ni->name_len) &&
  132. sle64_to_cpu(a->data.non_resident.lowest_vcn)
  133. <= vcn && end_vcn >= vcn))
  134. ctx_needs_reset = false;
  135. else {
  136. /* Save the old search context. */
  137. old_ctx = *ctx;
  138. /*
  139. * If the currently mapped (extent) inode is not the
  140. * base inode we will unmap it when we reinitialize the
  141. * search context which means we need to get a
  142. * reference to the page containing the mapped mft
  143. * record so we do not accidentally drop changes to the
  144. * mft record when it has not been marked dirty yet.
  145. */
  146. if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
  147. old_ctx.base_ntfs_ino) {
  148. put_this_page = old_ctx.ntfs_ino->page;
  149. page_cache_get(put_this_page);
  150. }
  151. /*
  152. * Reinitialize the search context so we can lookup the
  153. * needed attribute extent.
  154. */
  155. ntfs_attr_reinit_search_ctx(ctx);
  156. ctx_needs_reset = true;
  157. }
  158. }
  159. if (ctx_needs_reset) {
  160. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  161. CASE_SENSITIVE, vcn, NULL, 0, ctx);
  162. if (unlikely(err)) {
  163. if (err == -ENOENT)
  164. err = -EIO;
  165. goto err_out;
  166. }
  167. BUG_ON(!ctx->attr->non_resident);
  168. }
  169. a = ctx->attr;
  170. /*
  171. * Only decompress the mapping pairs if @vcn is inside it. Otherwise
  172. * we get into problems when we try to map an out of bounds vcn because
  173. * we then try to map the already mapped runlist fragment and
  174. * ntfs_mapping_pairs_decompress() fails.
  175. */
  176. end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
  177. if (unlikely(vcn && vcn >= end_vcn)) {
  178. err = -ENOENT;
  179. goto err_out;
  180. }
  181. rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
  182. if (IS_ERR(rl))
  183. err = PTR_ERR(rl);
  184. else
  185. ni->runlist.rl = rl;
  186. err_out:
  187. if (ctx_is_temporary) {
  188. if (likely(ctx))
  189. ntfs_attr_put_search_ctx(ctx);
  190. unmap_mft_record(base_ni);
  191. } else if (ctx_needs_reset) {
  192. /*
  193. * If there is no attribute list, restoring the search context
  194. * is acomplished simply by copying the saved context back over
  195. * the caller supplied context. If there is an attribute list,
  196. * things are more complicated as we need to deal with mapping
  197. * of mft records and resulting potential changes in pointers.
  198. */
  199. if (NInoAttrList(base_ni)) {
  200. /*
  201. * If the currently mapped (extent) inode is not the
  202. * one we had before, we need to unmap it and map the
  203. * old one.
  204. */
  205. if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
  206. /*
  207. * If the currently mapped inode is not the
  208. * base inode, unmap it.
  209. */
  210. if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
  211. ctx->base_ntfs_ino) {
  212. unmap_extent_mft_record(ctx->ntfs_ino);
  213. ctx->mrec = ctx->base_mrec;
  214. BUG_ON(!ctx->mrec);
  215. }
  216. /*
  217. * If the old mapped inode is not the base
  218. * inode, map it.
  219. */
  220. if (old_ctx.base_ntfs_ino &&
  221. old_ctx.ntfs_ino !=
  222. old_ctx.base_ntfs_ino) {
  223. retry_map:
  224. ctx->mrec = map_mft_record(
  225. old_ctx.ntfs_ino);
  226. /*
  227. * Something bad has happened. If out
  228. * of memory retry till it succeeds.
  229. * Any other errors are fatal and we
  230. * return the error code in ctx->mrec.
  231. * Let the caller deal with it... We
  232. * just need to fudge things so the
  233. * caller can reinit and/or put the
  234. * search context safely.
  235. */
  236. if (IS_ERR(ctx->mrec)) {
  237. if (PTR_ERR(ctx->mrec) ==
  238. -ENOMEM) {
  239. schedule();
  240. goto retry_map;
  241. } else
  242. old_ctx.ntfs_ino =
  243. old_ctx.
  244. base_ntfs_ino;
  245. }
  246. }
  247. }
  248. /* Update the changed pointers in the saved context. */
  249. if (ctx->mrec != old_ctx.mrec) {
  250. if (!IS_ERR(ctx->mrec))
  251. old_ctx.attr = (ATTR_RECORD*)(
  252. (u8*)ctx->mrec +
  253. ((u8*)old_ctx.attr -
  254. (u8*)old_ctx.mrec));
  255. old_ctx.mrec = ctx->mrec;
  256. }
  257. }
  258. /* Restore the search context to the saved one. */
  259. *ctx = old_ctx;
  260. /*
  261. * We drop the reference on the page we took earlier. In the
  262. * case that IS_ERR(ctx->mrec) is true this means we might lose
  263. * some changes to the mft record that had been made between
  264. * the last time it was marked dirty/written out and now. This
  265. * at this stage is not a problem as the mapping error is fatal
  266. * enough that the mft record cannot be written out anyway and
  267. * the caller is very likely to shutdown the whole inode
  268. * immediately and mark the volume dirty for chkdsk to pick up
  269. * the pieces anyway.
  270. */
  271. if (put_this_page)
  272. page_cache_release(put_this_page);
  273. }
  274. return err;
  275. }
  276. /**
  277. * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
  278. * @ni: ntfs inode for which to map (part of) a runlist
  279. * @vcn: map runlist part containing this vcn
  280. *
  281. * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
  282. *
  283. * Return 0 on success and -errno on error. There is one special error code
  284. * which is not an error as such. This is -ENOENT. It means that @vcn is out
  285. * of bounds of the runlist.
  286. *
  287. * Locking: - The runlist must be unlocked on entry and is unlocked on return.
  288. * - This function takes the runlist lock for writing and may modify
  289. * the runlist.
  290. */
  291. int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
  292. {
  293. int err = 0;
  294. down_write(&ni->runlist.lock);
  295. /* Make sure someone else didn't do the work while we were sleeping. */
  296. if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
  297. LCN_RL_NOT_MAPPED))
  298. err = ntfs_map_runlist_nolock(ni, vcn, NULL);
  299. up_write(&ni->runlist.lock);
  300. return err;
  301. }
  302. /**
  303. * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
  304. * @ni: ntfs inode of the attribute whose runlist to search
  305. * @vcn: vcn to convert
  306. * @write_locked: true if the runlist is locked for writing
  307. *
  308. * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
  309. * described by the ntfs inode @ni and return the corresponding logical cluster
  310. * number (lcn).
  311. *
  312. * If the @vcn is not mapped yet, the attempt is made to map the attribute
  313. * extent containing the @vcn and the vcn to lcn conversion is retried.
  314. *
  315. * If @write_locked is true the caller has locked the runlist for writing and
  316. * if false for reading.
  317. *
  318. * Since lcns must be >= 0, we use negative return codes with special meaning:
  319. *
  320. * Return code Meaning / Description
  321. * ==========================================
  322. * LCN_HOLE Hole / not allocated on disk.
  323. * LCN_ENOENT There is no such vcn in the runlist, i.e. @vcn is out of bounds.
  324. * LCN_ENOMEM Not enough memory to map runlist.
  325. * LCN_EIO Critical error (runlist/file is corrupt, i/o error, etc).
  326. *
  327. * Locking: - The runlist must be locked on entry and is left locked on return.
  328. * - If @write_locked is 'false', i.e. the runlist is locked for reading,
  329. * the lock may be dropped inside the function so you cannot rely on
  330. * the runlist still being the same when this function returns.
  331. */
  332. LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
  333. const bool write_locked)
  334. {
  335. LCN lcn;
  336. unsigned long flags;
  337. bool is_retry = false;
  338. ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
  339. ni->mft_no, (unsigned long long)vcn,
  340. write_locked ? "write" : "read");
  341. BUG_ON(!ni);
  342. BUG_ON(!NInoNonResident(ni));
  343. BUG_ON(vcn < 0);
  344. if (!ni->runlist.rl) {
  345. read_lock_irqsave(&ni->size_lock, flags);
  346. if (!ni->allocated_size) {
  347. read_unlock_irqrestore(&ni->size_lock, flags);
  348. return LCN_ENOENT;
  349. }
  350. read_unlock_irqrestore(&ni->size_lock, flags);
  351. }
  352. retry_remap:
  353. /* Convert vcn to lcn. If that fails map the runlist and retry once. */
  354. lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
  355. if (likely(lcn >= LCN_HOLE)) {
  356. ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
  357. return lcn;
  358. }
  359. if (lcn != LCN_RL_NOT_MAPPED) {
  360. if (lcn != LCN_ENOENT)
  361. lcn = LCN_EIO;
  362. } else if (!is_retry) {
  363. int err;
  364. if (!write_locked) {
  365. up_read(&ni->runlist.lock);
  366. down_write(&ni->runlist.lock);
  367. if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
  368. LCN_RL_NOT_MAPPED)) {
  369. up_write(&ni->runlist.lock);
  370. down_read(&ni->runlist.lock);
  371. goto retry_remap;
  372. }
  373. }
  374. err = ntfs_map_runlist_nolock(ni, vcn, NULL);
  375. if (!write_locked) {
  376. up_write(&ni->runlist.lock);
  377. down_read(&ni->runlist.lock);
  378. }
  379. if (likely(!err)) {
  380. is_retry = true;
  381. goto retry_remap;
  382. }
  383. if (err == -ENOENT)
  384. lcn = LCN_ENOENT;
  385. else if (err == -ENOMEM)
  386. lcn = LCN_ENOMEM;
  387. else
  388. lcn = LCN_EIO;
  389. }
  390. if (lcn != LCN_ENOENT)
  391. ntfs_error(ni->vol->sb, "Failed with error code %lli.",
  392. (long long)lcn);
  393. return lcn;
  394. }
  395. /**
  396. * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
  397. * @ni: ntfs inode describing the runlist to search
  398. * @vcn: vcn to find
  399. * @ctx: active attribute search context if present or NULL if not
  400. *
  401. * Find the virtual cluster number @vcn in the runlist described by the ntfs
  402. * inode @ni and return the address of the runlist element containing the @vcn.
  403. *
  404. * If the @vcn is not mapped yet, the attempt is made to map the attribute
  405. * extent containing the @vcn and the vcn to lcn conversion is retried.
  406. *
  407. * If @ctx is specified, it is an active search context of @ni and its base mft
  408. * record. This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped
  409. * runlist fragments and allows their mapping. If you do not have the mft
  410. * record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock()
  411. * will perform the necessary mapping and unmapping.
  412. *
  413. * Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and
  414. * restores it before returning. Thus, @ctx will be left pointing to the same
  415. * attribute on return as on entry. However, the actual pointers in @ctx may
  416. * point to different memory locations on return, so you must remember to reset
  417. * any cached pointers from the @ctx, i.e. after the call to
  418. * ntfs_attr_find_vcn_nolock(), you will probably want to do:
  419. * m = ctx->mrec;
  420. * a = ctx->attr;
  421. * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
  422. * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
  423. * Note you need to distinguish between the lcn of the returned runlist element
  424. * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
  425. * read and allocate clusters on write.
  426. *
  427. * Return the runlist element containing the @vcn on success and
  428. * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
  429. * to decide if the return is success or failure and PTR_ERR() to get to the
  430. * error code if IS_ERR() is true.
  431. *
  432. * The possible error return codes are:
  433. * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
  434. * -ENOMEM - Not enough memory to map runlist.
  435. * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
  436. *
  437. * WARNING: If @ctx is supplied, regardless of whether success or failure is
  438. * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
  439. * is no longer valid, i.e. you need to either call
  440. * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
  441. * In that case PTR_ERR(@ctx->mrec) will give you the error code for
  442. * why the mapping of the old inode failed.
  443. *
  444. * Locking: - The runlist described by @ni must be locked for writing on entry
  445. * and is locked on return. Note the runlist may be modified when
  446. * needed runlist fragments need to be mapped.
  447. * - If @ctx is NULL, the base mft record of @ni must not be mapped on
  448. * entry and it will be left unmapped on return.
  449. * - If @ctx is not NULL, the base mft record must be mapped on entry
  450. * and it will be left mapped on return.
  451. */
  452. runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
  453. ntfs_attr_search_ctx *ctx)
  454. {
  455. unsigned long flags;
  456. runlist_element *rl;
  457. int err = 0;
  458. bool is_retry = false;
  459. ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
  460. ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
  461. BUG_ON(!ni);
  462. BUG_ON(!NInoNonResident(ni));
  463. BUG_ON(vcn < 0);
  464. if (!ni->runlist.rl) {
  465. read_lock_irqsave(&ni->size_lock, flags);
  466. if (!ni->allocated_size) {
  467. read_unlock_irqrestore(&ni->size_lock, flags);
  468. return ERR_PTR(-ENOENT);
  469. }
  470. read_unlock_irqrestore(&ni->size_lock, flags);
  471. }
  472. retry_remap:
  473. rl = ni->runlist.rl;
  474. if (likely(rl && vcn >= rl[0].vcn)) {
  475. while (likely(rl->length)) {
  476. if (unlikely(vcn < rl[1].vcn)) {
  477. if (likely(rl->lcn >= LCN_HOLE)) {
  478. ntfs_debug("Done.");
  479. return rl;
  480. }
  481. break;
  482. }
  483. rl++;
  484. }
  485. if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
  486. if (likely(rl->lcn == LCN_ENOENT))
  487. err = -ENOENT;
  488. else
  489. err = -EIO;
  490. }
  491. }
  492. if (!err && !is_retry) {
  493. /*
  494. * If the search context is invalid we cannot map the unmapped
  495. * region.
  496. */
  497. if (IS_ERR(ctx->mrec))
  498. err = PTR_ERR(ctx->mrec);
  499. else {
  500. /*
  501. * The @vcn is in an unmapped region, map the runlist
  502. * and retry.
  503. */
  504. err = ntfs_map_runlist_nolock(ni, vcn, ctx);
  505. if (likely(!err)) {
  506. is_retry = true;
  507. goto retry_remap;
  508. }
  509. }
  510. if (err == -EINVAL)
  511. err = -EIO;
  512. } else if (!err)
  513. err = -EIO;
  514. if (err != -ENOENT)
  515. ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
  516. return ERR_PTR(err);
  517. }
  518. /**
  519. * ntfs_attr_find - find (next) attribute in mft record
  520. * @type: attribute type to find
  521. * @name: attribute name to find (optional, i.e. NULL means don't care)
  522. * @name_len: attribute name length (only needed if @name present)
  523. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  524. * @val: attribute value to find (optional, resident attributes only)
  525. * @val_len: attribute value length
  526. * @ctx: search context with mft record and attribute to search from
  527. *
  528. * You should not need to call this function directly. Use ntfs_attr_lookup()
  529. * instead.
  530. *
  531. * ntfs_attr_find() takes a search context @ctx as parameter and searches the
  532. * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
  533. * attribute of @type, optionally @name and @val.
  534. *
  535. * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
  536. * point to the found attribute.
  537. *
  538. * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
  539. * @ctx->attr will point to the attribute before which the attribute being
  540. * searched for would need to be inserted if such an action were to be desired.
  541. *
  542. * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
  543. * undefined and in particular do not rely on it not changing.
  544. *
  545. * If @ctx->is_first is 'true', the search begins with @ctx->attr itself. If it
  546. * is 'false', the search begins after @ctx->attr.
  547. *
  548. * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
  549. * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
  550. * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
  551. * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
  552. * sensitive. When @name is present, @name_len is the @name length in Unicode
  553. * characters.
  554. *
  555. * If @name is not present (NULL), we assume that the unnamed attribute is
  556. * being searched for.
  557. *
  558. * Finally, the resident attribute value @val is looked for, if present. If
  559. * @val is not present (NULL), @val_len is ignored.
  560. *
  561. * ntfs_attr_find() only searches the specified mft record and it ignores the
  562. * presence of an attribute list attribute (unless it is the one being searched
  563. * for, obviously). If you need to take attribute lists into consideration,
  564. * use ntfs_attr_lookup() instead (see below). This also means that you cannot
  565. * use ntfs_attr_find() to search for extent records of non-resident
  566. * attributes, as extents with lowest_vcn != 0 are usually described by the
  567. * attribute list attribute only. - Note that it is possible that the first
  568. * extent is only in the attribute list while the last extent is in the base
  569. * mft record, so do not rely on being able to find the first extent in the
  570. * base mft record.
  571. *
  572. * Warning: Never use @val when looking for attribute types which can be
  573. * non-resident as this most likely will result in a crash!
  574. */
  575. static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
  576. const u32 name_len, const IGNORE_CASE_BOOL ic,
  577. const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
  578. {
  579. ATTR_RECORD *a;
  580. ntfs_volume *vol = ctx->ntfs_ino->vol;
  581. ntfschar *upcase = vol->upcase;
  582. u32 upcase_len = vol->upcase_len;
  583. /*
  584. * Iterate over attributes in mft record starting at @ctx->attr, or the
  585. * attribute following that, if @ctx->is_first is 'true'.
  586. */
  587. if (ctx->is_first) {
  588. a = ctx->attr;
  589. ctx->is_first = false;
  590. } else
  591. a = (ATTR_RECORD*)((u8*)ctx->attr +
  592. le32_to_cpu(ctx->attr->length));
  593. for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
  594. if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
  595. le32_to_cpu(ctx->mrec->bytes_allocated))
  596. break;
  597. ctx->attr = a;
  598. if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
  599. a->type == AT_END))
  600. return -ENOENT;
  601. if (unlikely(!a->length))
  602. break;
  603. if (a->type != type)
  604. continue;
  605. /*
  606. * If @name is present, compare the two names. If @name is
  607. * missing, assume we want an unnamed attribute.
  608. */
  609. if (!name) {
  610. /* The search failed if the found attribute is named. */
  611. if (a->name_length)
  612. return -ENOENT;
  613. } else if (!ntfs_are_names_equal(name, name_len,
  614. (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
  615. a->name_length, ic, upcase, upcase_len)) {
  616. register int rc;
  617. rc = ntfs_collate_names(name, name_len,
  618. (ntfschar*)((u8*)a +
  619. le16_to_cpu(a->name_offset)),
  620. a->name_length, 1, IGNORE_CASE,
  621. upcase, upcase_len);
  622. /*
  623. * If @name collates before a->name, there is no
  624. * matching attribute.
  625. */
  626. if (rc == -1)
  627. return -ENOENT;
  628. /* If the strings are not equal, continue search. */
  629. if (rc)
  630. continue;
  631. rc = ntfs_collate_names(name, name_len,
  632. (ntfschar*)((u8*)a +
  633. le16_to_cpu(a->name_offset)),
  634. a->name_length, 1, CASE_SENSITIVE,
  635. upcase, upcase_len);
  636. if (rc == -1)
  637. return -ENOENT;
  638. if (rc)
  639. continue;
  640. }
  641. /*
  642. * The names match or @name not present and attribute is
  643. * unnamed. If no @val specified, we have found the attribute
  644. * and are done.
  645. */
  646. if (!val)
  647. return 0;
  648. /* @val is present; compare values. */
  649. else {
  650. register int rc;
  651. rc = memcmp(val, (u8*)a + le16_to_cpu(
  652. a->data.resident.value_offset),
  653. min_t(u32, val_len, le32_to_cpu(
  654. a->data.resident.value_length)));
  655. /*
  656. * If @val collates before the current attribute's
  657. * value, there is no matching attribute.
  658. */
  659. if (!rc) {
  660. register u32 avl;
  661. avl = le32_to_cpu(
  662. a->data.resident.value_length);
  663. if (val_len == avl)
  664. return 0;
  665. if (val_len < avl)
  666. return -ENOENT;
  667. } else if (rc < 0)
  668. return -ENOENT;
  669. }
  670. }
  671. ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
  672. NVolSetErrors(vol);
  673. return -EIO;
  674. }
  675. /**
  676. * load_attribute_list - load an attribute list into memory
  677. * @vol: ntfs volume from which to read
  678. * @runlist: runlist of the attribute list
  679. * @al_start: destination buffer
  680. * @size: size of the destination buffer in bytes
  681. * @initialized_size: initialized size of the attribute list
  682. *
  683. * Walk the runlist @runlist and load all clusters from it copying them into
  684. * the linear buffer @al. The maximum number of bytes copied to @al is @size
  685. * bytes. Note, @size does not need to be a multiple of the cluster size. If
  686. * @initialized_size is less than @size, the region in @al between
  687. * @initialized_size and @size will be zeroed and not read from disk.
  688. *
  689. * Return 0 on success or -errno on error.
  690. */
  691. int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
  692. const s64 size, const s64 initialized_size)
  693. {
  694. LCN lcn;
  695. u8 *al = al_start;
  696. u8 *al_end = al + initialized_size;
  697. runlist_element *rl;
  698. struct buffer_head *bh;
  699. struct super_block *sb;
  700. unsigned long block_size;
  701. unsigned long block, max_block;
  702. int err = 0;
  703. unsigned char block_size_bits;
  704. ntfs_debug("Entering.");
  705. if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
  706. initialized_size > size)
  707. return -EINVAL;
  708. if (!initialized_size) {
  709. memset(al, 0, size);
  710. return 0;
  711. }
  712. sb = vol->sb;
  713. block_size = sb->s_blocksize;
  714. block_size_bits = sb->s_blocksize_bits;
  715. down_read(&runlist->lock);
  716. rl = runlist->rl;
  717. if (!rl) {
  718. ntfs_error(sb, "Cannot read attribute list since runlist is "
  719. "missing.");
  720. goto err_out;
  721. }
  722. /* Read all clusters specified by the runlist one run at a time. */
  723. while (rl->length) {
  724. lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
  725. ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
  726. (unsigned long long)rl->vcn,
  727. (unsigned long long)lcn);
  728. /* The attribute list cannot be sparse. */
  729. if (lcn < 0) {
  730. ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
  731. "read attribute list.");
  732. goto err_out;
  733. }
  734. block = lcn << vol->cluster_size_bits >> block_size_bits;
  735. /* Read the run from device in chunks of block_size bytes. */
  736. max_block = block + (rl->length << vol->cluster_size_bits >>
  737. block_size_bits);
  738. ntfs_debug("max_block = 0x%lx.", max_block);
  739. do {
  740. ntfs_debug("Reading block = 0x%lx.", block);
  741. bh = sb_bread(sb, block);
  742. if (!bh) {
  743. ntfs_error(sb, "sb_bread() failed. Cannot "
  744. "read attribute list.");
  745. goto err_out;
  746. }
  747. if (al + block_size >= al_end)
  748. goto do_final;
  749. memcpy(al, bh->b_data, block_size);
  750. brelse(bh);
  751. al += block_size;
  752. } while (++block < max_block);
  753. rl++;
  754. }
  755. if (initialized_size < size) {
  756. initialize:
  757. memset(al_start + initialized_size, 0, size - initialized_size);
  758. }
  759. done:
  760. up_read(&runlist->lock);
  761. return err;
  762. do_final:
  763. if (al < al_end) {
  764. /*
  765. * Partial block.
  766. *
  767. * Note: The attribute list can be smaller than its allocation
  768. * by multiple clusters. This has been encountered by at least
  769. * two people running Windows XP, thus we cannot do any
  770. * truncation sanity checking here. (AIA)
  771. */
  772. memcpy(al, bh->b_data, al_end - al);
  773. brelse(bh);
  774. if (initialized_size < size)
  775. goto initialize;
  776. goto done;
  777. }
  778. brelse(bh);
  779. /* Real overflow! */
  780. ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
  781. "is truncated.");
  782. err_out:
  783. err = -EIO;
  784. goto done;
  785. }
  786. /**
  787. * ntfs_external_attr_find - find an attribute in the attribute list of an inode
  788. * @type: attribute type to find
  789. * @name: attribute name to find (optional, i.e. NULL means don't care)
  790. * @name_len: attribute name length (only needed if @name present)
  791. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  792. * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
  793. * @val: attribute value to find (optional, resident attributes only)
  794. * @val_len: attribute value length
  795. * @ctx: search context with mft record and attribute to search from
  796. *
  797. * You should not need to call this function directly. Use ntfs_attr_lookup()
  798. * instead.
  799. *
  800. * Find an attribute by searching the attribute list for the corresponding
  801. * attribute list entry. Having found the entry, map the mft record if the
  802. * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
  803. * in there and return it.
  804. *
  805. * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
  806. * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
  807. * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
  808. * then the base inode).
  809. *
  810. * After finishing with the attribute/mft record you need to call
  811. * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
  812. * mapped inodes, etc).
  813. *
  814. * If the attribute is found, ntfs_external_attr_find() returns 0 and
  815. * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
  816. * mft record in which @ctx->attr is located and @ctx->al_entry will point to
  817. * the attribute list entry for the attribute.
  818. *
  819. * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
  820. * @ctx->attr will point to the attribute in the base mft record before which
  821. * the attribute being searched for would need to be inserted if such an action
  822. * were to be desired. @ctx->mrec will point to the mft record in which
  823. * @ctx->attr is located and @ctx->al_entry will point to the attribute list
  824. * entry of the attribute before which the attribute being searched for would
  825. * need to be inserted if such an action were to be desired.
  826. *
  827. * Thus to insert the not found attribute, one wants to add the attribute to
  828. * @ctx->mrec (the base mft record) and if there is not enough space, the
  829. * attribute should be placed in a newly allocated extent mft record. The
  830. * attribute list entry for the inserted attribute should be inserted in the
  831. * attribute list attribute at @ctx->al_entry.
  832. *
  833. * On actual error, ntfs_external_attr_find() returns -EIO. In this case
  834. * @ctx->attr is undefined and in particular do not rely on it not changing.
  835. */
  836. static int ntfs_external_attr_find(const ATTR_TYPE type,
  837. const ntfschar *name, const u32 name_len,
  838. const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
  839. const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
  840. {
  841. ntfs_inode *base_ni, *ni;
  842. ntfs_volume *vol;
  843. ATTR_LIST_ENTRY *al_entry, *next_al_entry;
  844. u8 *al_start, *al_end;
  845. ATTR_RECORD *a;
  846. ntfschar *al_name;
  847. u32 al_name_len;
  848. int err = 0;
  849. static const char *es = " Unmount and run chkdsk.";
  850. ni = ctx->ntfs_ino;
  851. base_ni = ctx->base_ntfs_ino;
  852. ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
  853. if (!base_ni) {
  854. /* First call happens with the base mft record. */
  855. base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
  856. ctx->base_mrec = ctx->mrec;
  857. }
  858. if (ni == base_ni)
  859. ctx->base_attr = ctx->attr;
  860. if (type == AT_END)
  861. goto not_found;
  862. vol = base_ni->vol;
  863. al_start = base_ni->attr_list;
  864. al_end = al_start + base_ni->attr_list_size;
  865. if (!ctx->al_entry)
  866. ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
  867. /*
  868. * Iterate over entries in attribute list starting at @ctx->al_entry,
  869. * or the entry following that, if @ctx->is_first is 'true'.
  870. */
  871. if (ctx->is_first) {
  872. al_entry = ctx->al_entry;
  873. ctx->is_first = false;
  874. } else
  875. al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
  876. le16_to_cpu(ctx->al_entry->length));
  877. for (;; al_entry = next_al_entry) {
  878. /* Out of bounds check. */
  879. if ((u8*)al_entry < base_ni->attr_list ||
  880. (u8*)al_entry > al_end)
  881. break; /* Inode is corrupt. */
  882. ctx->al_entry = al_entry;
  883. /* Catch the end of the attribute list. */
  884. if ((u8*)al_entry == al_end)
  885. goto not_found;
  886. if (!al_entry->length)
  887. break;
  888. if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
  889. le16_to_cpu(al_entry->length) > al_end)
  890. break;
  891. next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
  892. le16_to_cpu(al_entry->length));
  893. if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
  894. goto not_found;
  895. if (type != al_entry->type)
  896. continue;
  897. /*
  898. * If @name is present, compare the two names. If @name is
  899. * missing, assume we want an unnamed attribute.
  900. */
  901. al_name_len = al_entry->name_length;
  902. al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
  903. if (!name) {
  904. if (al_name_len)
  905. goto not_found;
  906. } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
  907. name_len, ic, vol->upcase, vol->upcase_len)) {
  908. register int rc;
  909. rc = ntfs_collate_names(name, name_len, al_name,
  910. al_name_len, 1, IGNORE_CASE,
  911. vol->upcase, vol->upcase_len);
  912. /*
  913. * If @name collates before al_name, there is no
  914. * matching attribute.
  915. */
  916. if (rc == -1)
  917. goto not_found;
  918. /* If the strings are not equal, continue search. */
  919. if (rc)
  920. continue;
  921. /*
  922. * FIXME: Reverse engineering showed 0, IGNORE_CASE but
  923. * that is inconsistent with ntfs_attr_find(). The
  924. * subsequent rc checks were also different. Perhaps I
  925. * made a mistake in one of the two. Need to recheck
  926. * which is correct or at least see what is going on...
  927. * (AIA)
  928. */
  929. rc = ntfs_collate_names(name, name_len, al_name,
  930. al_name_len, 1, CASE_SENSITIVE,
  931. vol->upcase, vol->upcase_len);
  932. if (rc == -1)
  933. goto not_found;
  934. if (rc)
  935. continue;
  936. }
  937. /*
  938. * The names match or @name not present and attribute is
  939. * unnamed. Now check @lowest_vcn. Continue search if the
  940. * next attribute list entry still fits @lowest_vcn. Otherwise
  941. * we have reached the right one or the search has failed.
  942. */
  943. if (lowest_vcn && (u8*)next_al_entry >= al_start &&
  944. (u8*)next_al_entry + 6 < al_end &&
  945. (u8*)next_al_entry + le16_to_cpu(
  946. next_al_entry->length) <= al_end &&
  947. sle64_to_cpu(next_al_entry->lowest_vcn) <=
  948. lowest_vcn &&
  949. next_al_entry->type == al_entry->type &&
  950. next_al_entry->name_length == al_name_len &&
  951. ntfs_are_names_equal((ntfschar*)((u8*)
  952. next_al_entry +
  953. next_al_entry->name_offset),
  954. next_al_entry->name_length,
  955. al_name, al_name_len, CASE_SENSITIVE,
  956. vol->upcase, vol->upcase_len))
  957. continue;
  958. if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
  959. if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
  960. ntfs_error(vol->sb, "Found stale mft "
  961. "reference in attribute list "
  962. "of base inode 0x%lx.%s",
  963. base_ni->mft_no, es);
  964. err = -EIO;
  965. break;
  966. }
  967. } else { /* Mft references do not match. */
  968. /* If there is a mapped record unmap it first. */
  969. if (ni != base_ni)
  970. unmap_extent_mft_record(ni);
  971. /* Do we want the base record back? */
  972. if (MREF_LE(al_entry->mft_reference) ==
  973. base_ni->mft_no) {
  974. ni = ctx->ntfs_ino = base_ni;
  975. ctx->mrec = ctx->base_mrec;
  976. } else {
  977. /* We want an extent record. */
  978. ctx->mrec = map_extent_mft_record(base_ni,
  979. le64_to_cpu(
  980. al_entry->mft_reference), &ni);
  981. if (IS_ERR(ctx->mrec)) {
  982. ntfs_error(vol->sb, "Failed to map "
  983. "extent mft record "
  984. "0x%lx of base inode "
  985. "0x%lx.%s",
  986. MREF_LE(al_entry->
  987. mft_reference),
  988. base_ni->mft_no, es);
  989. err = PTR_ERR(ctx->mrec);
  990. if (err == -ENOENT)
  991. err = -EIO;
  992. /* Cause @ctx to be sanitized below. */
  993. ni = NULL;
  994. break;
  995. }
  996. ctx->ntfs_ino = ni;
  997. }
  998. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  999. le16_to_cpu(ctx->mrec->attrs_offset));
  1000. }
  1001. /*
  1002. * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
  1003. * mft record containing the attribute represented by the
  1004. * current al_entry.
  1005. */
  1006. /*
  1007. * We could call into ntfs_attr_find() to find the right
  1008. * attribute in this mft record but this would be less
  1009. * efficient and not quite accurate as ntfs_attr_find() ignores
  1010. * the attribute instance numbers for example which become
  1011. * important when one plays with attribute lists. Also,
  1012. * because a proper match has been found in the attribute list
  1013. * entry above, the comparison can now be optimized. So it is
  1014. * worth re-implementing a simplified ntfs_attr_find() here.
  1015. */
  1016. a = ctx->attr;
  1017. /*
  1018. * Use a manual loop so we can still use break and continue
  1019. * with the same meanings as above.
  1020. */
  1021. do_next_attr_loop:
  1022. if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
  1023. le32_to_cpu(ctx->mrec->bytes_allocated))
  1024. break;
  1025. if (a->type == AT_END)
  1026. break;
  1027. if (!a->length)
  1028. break;
  1029. if (al_entry->instance != a->instance)
  1030. goto do_next_attr;
  1031. /*
  1032. * If the type and/or the name are mismatched between the
  1033. * attribute list entry and the attribute record, there is
  1034. * corruption so we break and return error EIO.
  1035. */
  1036. if (al_entry->type != a->type)
  1037. break;
  1038. if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
  1039. le16_to_cpu(a->name_offset)), a->name_length,
  1040. al_name, al_name_len, CASE_SENSITIVE,
  1041. vol->upcase, vol->upcase_len))
  1042. break;
  1043. ctx->attr = a;
  1044. /*
  1045. * If no @val specified or @val specified and it matches, we
  1046. * have found it!
  1047. */
  1048. if (!val || (!a->non_resident && le32_to_cpu(
  1049. a->data.resident.value_length) == val_len &&
  1050. !memcmp((u8*)a +
  1051. le16_to_cpu(a->data.resident.value_offset),
  1052. val, val_len))) {
  1053. ntfs_debug("Done, found.");
  1054. return 0;
  1055. }
  1056. do_next_attr:
  1057. /* Proceed to the next attribute in the current mft record. */
  1058. a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
  1059. goto do_next_attr_loop;
  1060. }
  1061. if (!err) {
  1062. ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
  1063. "attribute list attribute.%s", base_ni->mft_no,
  1064. es);
  1065. err = -EIO;
  1066. }
  1067. if (ni != base_ni) {
  1068. if (ni)
  1069. unmap_extent_mft_record(ni);
  1070. ctx->ntfs_ino = base_ni;
  1071. ctx->mrec = ctx->base_mrec;
  1072. ctx->attr = ctx->base_attr;
  1073. }
  1074. if (err != -ENOMEM)
  1075. NVolSetErrors(vol);
  1076. return err;
  1077. not_found:
  1078. /*
  1079. * If we were looking for AT_END, we reset the search context @ctx and
  1080. * use ntfs_attr_find() to seek to the end of the base mft record.
  1081. */
  1082. if (type == AT_END) {
  1083. ntfs_attr_reinit_search_ctx(ctx);
  1084. return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
  1085. ctx);
  1086. }
  1087. /*
  1088. * The attribute was not found. Before we return, we want to ensure
  1089. * @ctx->mrec and @ctx->attr indicate the position at which the
  1090. * attribute should be inserted in the base mft record. Since we also
  1091. * want to preserve @ctx->al_entry we cannot reinitialize the search
  1092. * context using ntfs_attr_reinit_search_ctx() as this would set
  1093. * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
  1094. * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
  1095. * @ctx->al_entry as the remaining fields (base_*) are identical to
  1096. * their non base_ counterparts and we cannot set @ctx->base_attr
  1097. * correctly yet as we do not know what @ctx->attr will be set to by
  1098. * the call to ntfs_attr_find() below.
  1099. */
  1100. if (ni != base_ni)
  1101. unmap_extent_mft_record(ni);
  1102. ctx->mrec = ctx->base_mrec;
  1103. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  1104. le16_to_cpu(ctx->mrec->attrs_offset));
  1105. ctx->is_first = true;
  1106. ctx->ntfs_ino = base_ni;
  1107. ctx->base_ntfs_ino = NULL;
  1108. ctx->base_mrec = NULL;
  1109. ctx->base_attr = NULL;
  1110. /*
  1111. * In case there are multiple matches in the base mft record, need to
  1112. * keep enumerating until we get an attribute not found response (or
  1113. * another error), otherwise we would keep returning the same attribute
  1114. * over and over again and all programs using us for enumeration would
  1115. * lock up in a tight loop.
  1116. */
  1117. do {
  1118. err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
  1119. ctx);
  1120. } while (!err);
  1121. ntfs_debug("Done, not found.");
  1122. return err;
  1123. }
  1124. /**
  1125. * ntfs_attr_lookup - find an attribute in an ntfs inode
  1126. * @type: attribute type to find
  1127. * @name: attribute name to find (optional, i.e. NULL means don't care)
  1128. * @name_len: attribute name length (only needed if @name present)
  1129. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  1130. * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
  1131. * @val: attribute value to find (optional, resident attributes only)
  1132. * @val_len: attribute value length
  1133. * @ctx: search context with mft record and attribute to search from
  1134. *
  1135. * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
  1136. * be the base mft record and @ctx must have been obtained from a call to
  1137. * ntfs_attr_get_search_ctx().
  1138. *
  1139. * This function transparently handles attribute lists and @ctx is used to
  1140. * continue searches where they were left off at.
  1141. *
  1142. * After finishing with the attribute/mft record you need to call
  1143. * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
  1144. * mapped inodes, etc).
  1145. *
  1146. * Return 0 if the search was successful and -errno if not.
  1147. *
  1148. * When 0, @ctx->attr is the found attribute and it is in mft record
  1149. * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
  1150. * the attribute list entry of the found attribute.
  1151. *
  1152. * When -ENOENT, @ctx->attr is the attribute which collates just after the
  1153. * attribute being searched for, i.e. if one wants to add the attribute to the
  1154. * mft record this is the correct place to insert it into. If an attribute
  1155. * list attribute is present, @ctx->al_entry is the attribute list entry which
  1156. * collates just after the attribute list entry of the attribute being searched
  1157. * for, i.e. if one wants to add the attribute to the mft record this is the
  1158. * correct place to insert its attribute list entry into.
  1159. *
  1160. * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
  1161. * then undefined and in particular you should not rely on it not changing.
  1162. */
  1163. int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
  1164. const u32 name_len, const IGNORE_CASE_BOOL ic,
  1165. const VCN lowest_vcn, const u8 *val, const u32 val_len,
  1166. ntfs_attr_search_ctx *ctx)
  1167. {
  1168. ntfs_inode *base_ni;
  1169. ntfs_debug("Entering.");
  1170. BUG_ON(IS_ERR(ctx->mrec));
  1171. if (ctx->base_ntfs_ino)
  1172. base_ni = ctx->base_ntfs_ino;
  1173. else
  1174. base_ni = ctx->ntfs_ino;
  1175. /* Sanity check, just for debugging really. */
  1176. BUG_ON(!base_ni);
  1177. if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
  1178. return ntfs_attr_find(type, name, name_len, ic, val, val_len,
  1179. ctx);
  1180. return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
  1181. val, val_len, ctx);
  1182. }
  1183. /**
  1184. * ntfs_attr_init_search_ctx - initialize an attribute search context
  1185. * @ctx: attribute search context to initialize
  1186. * @ni: ntfs inode with which to initialize the search context
  1187. * @mrec: mft record with which to initialize the search context
  1188. *
  1189. * Initialize the attribute search context @ctx with @ni and @mrec.
  1190. */
  1191. static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
  1192. ntfs_inode *ni, MFT_RECORD *mrec)
  1193. {
  1194. *ctx = (ntfs_attr_search_ctx) {
  1195. .mrec = mrec,
  1196. /* Sanity checks are performed elsewhere. */
  1197. .attr = (ATTR_RECORD*)((u8*)mrec +
  1198. le16_to_cpu(mrec->attrs_offset)),
  1199. .is_first = true,
  1200. .ntfs_ino = ni,
  1201. };
  1202. }
  1203. /**
  1204. * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
  1205. * @ctx: attribute search context to reinitialize
  1206. *
  1207. * Reinitialize the attribute search context @ctx, unmapping an associated
  1208. * extent mft record if present, and initialize the search context again.
  1209. *
  1210. * This is used when a search for a new attribute is being started to reset
  1211. * the search context to the beginning.
  1212. */
  1213. void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
  1214. {
  1215. if (likely(!ctx->base_ntfs_ino)) {
  1216. /* No attribute list. */
  1217. ctx->is_first = true;
  1218. /* Sanity checks are performed elsewhere. */
  1219. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  1220. le16_to_cpu(ctx->mrec->attrs_offset));
  1221. /*
  1222. * This needs resetting due to ntfs_external_attr_find() which
  1223. * can leave it set despite having zeroed ctx->base_ntfs_ino.
  1224. */
  1225. ctx->al_entry = NULL;
  1226. return;
  1227. } /* Attribute list. */
  1228. if (ctx->ntfs_ino != ctx->base_ntfs_ino)
  1229. unmap_extent_mft_record(ctx->ntfs_ino);
  1230. ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
  1231. return;
  1232. }
  1233. /**
  1234. * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
  1235. * @ni: ntfs inode with which to initialize the search context
  1236. * @mrec: mft record with which to initialize the search context
  1237. *
  1238. * Allocate a new attribute search context, initialize it with @ni and @mrec,
  1239. * and return it. Return NULL if allocation failed.
  1240. */
  1241. ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
  1242. {
  1243. ntfs_attr_search_ctx *ctx;
  1244. ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
  1245. if (ctx)
  1246. ntfs_attr_init_search_ctx(ctx, ni, mrec);
  1247. return ctx;
  1248. }
  1249. /**
  1250. * ntfs_attr_put_search_ctx - release an attribute search context
  1251. * @ctx: attribute search context to free
  1252. *
  1253. * Release the attribute search context @ctx, unmapping an associated extent
  1254. * mft record if present.
  1255. */
  1256. void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
  1257. {
  1258. if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
  1259. unmap_extent_mft_record(ctx->ntfs_ino);
  1260. kmem_cache_free(ntfs_attr_ctx_cache, ctx);
  1261. return;
  1262. }
  1263. #ifdef NTFS_RW
  1264. /**
  1265. * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
  1266. * @vol: ntfs volume to which the attribute belongs
  1267. * @type: attribute type which to find
  1268. *
  1269. * Search for the attribute definition record corresponding to the attribute
  1270. * @type in the $AttrDef system file.
  1271. *
  1272. * Return the attribute type definition record if found and NULL if not found.
  1273. */
  1274. static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
  1275. const ATTR_TYPE type)
  1276. {
  1277. ATTR_DEF *ad;
  1278. BUG_ON(!vol->attrdef);
  1279. BUG_ON(!type);
  1280. for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
  1281. vol->attrdef_size && ad->type; ++ad) {
  1282. /* We have not found it yet, carry on searching. */
  1283. if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
  1284. continue;
  1285. /* We found the attribute; return it. */
  1286. if (likely(ad->type == type))
  1287. return ad;
  1288. /* We have gone too far already. No point in continuing. */
  1289. break;
  1290. }
  1291. /* Attribute not found. */
  1292. ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
  1293. le32_to_cpu(type));
  1294. return NULL;
  1295. }
  1296. /**
  1297. * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
  1298. * @vol: ntfs volume to which the attribute belongs
  1299. * @type: attribute type which to check
  1300. * @size: size which to check
  1301. *
  1302. * Check whether the @size in bytes is valid for an attribute of @type on the
  1303. * ntfs volume @vol. This information is obtained from $AttrDef system file.
  1304. *
  1305. * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
  1306. * listed in $AttrDef.
  1307. */
  1308. int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
  1309. const s64 size)
  1310. {
  1311. ATTR_DEF *ad;
  1312. BUG_ON(size < 0);
  1313. /*
  1314. * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
  1315. * listed in $AttrDef.
  1316. */
  1317. if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
  1318. return -ERANGE;
  1319. /* Get the $AttrDef entry for the attribute @type. */
  1320. ad = ntfs_attr_find_in_attrdef(vol, type);
  1321. if (unlikely(!ad))
  1322. return -ENOENT;
  1323. /* Do the bounds check. */
  1324. if (((sle64_to_cpu(ad->min_size) > 0) &&
  1325. size < sle64_to_cpu(ad->min_size)) ||
  1326. ((sle64_to_cpu(ad->max_size) > 0) && size >
  1327. sle64_to_cpu(ad->max_size)))
  1328. return -ERANGE;
  1329. return 0;
  1330. }
  1331. /**
  1332. * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
  1333. * @vol: ntfs volume to which the attribute belongs
  1334. * @type: attribute type which to check
  1335. *
  1336. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1337. * be non-resident. This information is obtained from $AttrDef system file.
  1338. *
  1339. * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
  1340. * -ENOENT if the attribute is not listed in $AttrDef.
  1341. */
  1342. int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1343. {
  1344. ATTR_DEF *ad;
  1345. /* Find the attribute definition record in $AttrDef. */
  1346. ad = ntfs_attr_find_in_attrdef(vol, type);
  1347. if (unlikely(!ad))
  1348. return -ENOENT;
  1349. /* Check the flags and return the result. */
  1350. if (ad->flags & ATTR_DEF_RESIDENT)
  1351. return -EPERM;
  1352. return 0;
  1353. }
  1354. /**
  1355. * ntfs_attr_can_be_resident - check if an attribute can be resident
  1356. * @vol: ntfs volume to which the attribute belongs
  1357. * @type: attribute type which to check
  1358. *
  1359. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1360. * be resident. This information is derived from our ntfs knowledge and may
  1361. * not be completely accurate, especially when user defined attributes are
  1362. * present. Basically we allow everything to be resident except for index
  1363. * allocation and $EA attributes.
  1364. *
  1365. * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
  1366. *
  1367. * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
  1368. * otherwise windows will not boot (blue screen of death)! We cannot
  1369. * check for this here as we do not know which inode's $Bitmap is
  1370. * being asked about so the caller needs to special case this.
  1371. */
  1372. int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1373. {
  1374. if (type == AT_INDEX_ALLOCATION)
  1375. return -EPERM;
  1376. return 0;
  1377. }
  1378. /**
  1379. * ntfs_attr_record_resize - resize an attribute record
  1380. * @m: mft record containing attribute record
  1381. * @a: attribute record to resize
  1382. * @new_size: new size in bytes to which to resize the attribute record @a
  1383. *
  1384. * Resize the attribute record @a, i.e. the resident part of the attribute, in
  1385. * the mft record @m to @new_size bytes.
  1386. *
  1387. * Return 0 on success and -errno on error. The following error codes are
  1388. * defined:
  1389. * -ENOSPC - Not enough space in the mft record @m to perform the resize.
  1390. *
  1391. * Note: On error, no modifications have been performed whatsoever.
  1392. *
  1393. * Warning: If you make a record smaller without having copied all the data you
  1394. * are interested in the data may be overwritten.
  1395. */
  1396. int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
  1397. {
  1398. ntfs_debug("Entering for new_size %u.", new_size);
  1399. /* Align to 8 bytes if it is not already done. */
  1400. if (new_size & 7)
  1401. new_size = (new_size + 7) & ~7;
  1402. /* If the actual attribute length has changed, move things around. */
  1403. if (new_size != le32_to_cpu(a->length)) {
  1404. u32 new_muse = le32_to_cpu(m->bytes_in_use) -
  1405. le32_to_cpu(a->length) + new_size;
  1406. /* Not enough space in this mft record. */
  1407. if (new_muse > le32_to_cpu(m->bytes_allocated))
  1408. return -ENOSPC;
  1409. /* Move attributes following @a to their new location. */
  1410. memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
  1411. le32_to_cpu(m->bytes_in_use) - ((u8*)a -
  1412. (u8*)m) - le32_to_cpu(a->length));
  1413. /* Adjust @m to reflect the change in used space. */
  1414. m->bytes_in_use = cpu_to_le32(new_muse);
  1415. /* Adjust @a to reflect the new size. */
  1416. if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
  1417. a->length = cpu_to_le32(new_size);
  1418. }
  1419. return 0;
  1420. }
  1421. /**
  1422. * ntfs_resident_attr_value_resize - resize the value of a resident attribute
  1423. * @m: mft record containing attribute record
  1424. * @a: attribute record whose value to resize
  1425. * @new_size: new size in bytes to which to resize the attribute value of @a
  1426. *
  1427. * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
  1428. * If the value is made bigger, the newly allocated space is cleared.
  1429. *
  1430. * Return 0 on success and -errno on error. The following error codes are
  1431. * defined:
  1432. * -ENOSPC - Not enough space in the mft record @m to perform the resize.
  1433. *
  1434. * Note: On error, no modifications have been performed whatsoever.
  1435. *
  1436. * Warning: If you make a record smaller without having copied all the data you
  1437. * are interested in the data may be overwritten.
  1438. */
  1439. int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
  1440. const u32 new_size)
  1441. {
  1442. u32 old_size;
  1443. /* Resize the resident part of the attribute record. */
  1444. if (ntfs_attr_record_resize(m, a,
  1445. le16_to_cpu(a->data.resident.value_offset) + new_size))
  1446. return -ENOSPC;
  1447. /*
  1448. * The resize succeeded! If we made the attribute value bigger, clear
  1449. * the area between the old size and @new_size.
  1450. */
  1451. old_size = le32_to_cpu(a->data.resident.value_length);
  1452. if (new_size > old_size)
  1453. memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
  1454. old_size, 0, new_size - old_size);
  1455. /* Finally update the length of the attribute value. */
  1456. a->data.resident.value_length = cpu_to_le32(new_size);
  1457. return 0;
  1458. }
  1459. /**
  1460. * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
  1461. * @ni: ntfs inode describing the attribute to convert
  1462. * @data_size: size of the resident data to copy to the non-resident attribute
  1463. *
  1464. * Convert the resident ntfs attribute described by the ntfs inode @ni to a
  1465. * non-resident one.
  1466. *
  1467. * @data_size must be equal to the attribute value size. This is needed since
  1468. * we need to know the size before we can map the mft record and our callers
  1469. * always know it. The reason we cannot simply read the size from the vfs
  1470. * inode i_size is that this is not necessarily uptodate. This happens when
  1471. * ntfs_attr_make_non_resident() is called in the ->truncate call path(s).
  1472. *
  1473. * Return 0 on success and -errno on error. The following error return codes
  1474. * are defined:
  1475. * -EPERM - The attribute is not allowed to be non-resident.
  1476. * -ENOMEM - Not enough memory.
  1477. * -ENOSPC - Not enough disk space.
  1478. * -EINVAL - Attribute not defined on the volume.
  1479. * -EIO - I/o error or other error.
  1480. * Note that -ENOSPC is also returned in the case that there is not enough
  1481. * space in the mft record to do the conversion. This can happen when the mft
  1482. * record is already very full. The caller is responsible for trying to make
  1483. * space in the mft record and trying again. FIXME: Do we need a separate
  1484. * error return code for this kind of -ENOSPC or is it always worth trying
  1485. * again in case the attribute may then fit in a resident state so no need to
  1486. * make it non-resident at all? Ho-hum... (AIA)
  1487. *
  1488. * NOTE to self: No changes in the attribute list are required to move from
  1489. * a resident to a non-resident attribute.
  1490. *
  1491. * Locking: - The caller must hold i_mutex on the inode.
  1492. */
  1493. int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
  1494. {
  1495. s64 new_size;
  1496. struct inode *vi = VFS_I(ni);
  1497. ntfs_volume *vol = ni->vol;
  1498. ntfs_inode *base_ni;
  1499. MFT_RECORD *m;
  1500. ATTR_RECORD *a;
  1501. ntfs_attr_search_ctx *ctx;
  1502. struct page *page;
  1503. runlist_element *rl;
  1504. u8 *kaddr;
  1505. unsigned long flags;
  1506. int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
  1507. u32 attr_size;
  1508. u8 old_res_attr_flags;
  1509. /* Check that the attribute is allowed to be non-resident. */
  1510. err = ntfs_attr_can_be_non_resident(vol, ni->type);
  1511. if (unlikely(err)) {
  1512. if (err == -EPERM)
  1513. ntfs_debug("Attribute is not allowed to be "
  1514. "non-resident.");
  1515. else
  1516. ntfs_debug("Attribute not defined on the NTFS "
  1517. "volume!");
  1518. return err;
  1519. }
  1520. /*
  1521. * FIXME: Compressed and encrypted attributes are not supported when
  1522. * writing and we should never have gotten here for them.
  1523. */
  1524. BUG_ON(NInoCompressed(ni));
  1525. BUG_ON(NInoEncrypted(ni));
  1526. /*
  1527. * The size needs to be aligned to a cluster boundary for allocation
  1528. * purposes.
  1529. */
  1530. new_size = (data_size + vol->cluster_size - 1) &
  1531. ~(vol->cluster_size - 1);
  1532. if (new_size > 0) {
  1533. /*
  1534. * Will need the page later and since the page lock nests
  1535. * outside all ntfs locks, we need to get the page now.
  1536. */
  1537. page = find_or_create_page(vi->i_mapping, 0,
  1538. mapping_gfp_mask(vi->i_mapping));
  1539. if (unlikely(!page))
  1540. return -ENOMEM;
  1541. /* Start by allocating clusters to hold the attribute value. */
  1542. rl = ntfs_cluster_alloc(vol, 0, new_size >>
  1543. vol->cluster_size_bits, -1, DATA_ZONE, true);
  1544. if (IS_ERR(rl)) {
  1545. err = PTR_ERR(rl);
  1546. ntfs_debug("Failed to allocate cluster%s, error code "
  1547. "%i.", (new_size >>
  1548. vol->cluster_size_bits) > 1 ? "s" : "",
  1549. err);
  1550. goto page_err_out;
  1551. }
  1552. } else {
  1553. rl = NULL;
  1554. page = NULL;
  1555. }
  1556. /* Determine the size of the mapping pairs array. */
  1557. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
  1558. if (unlikely(mp_size < 0)) {
  1559. err = mp_size;
  1560. ntfs_debug("Failed to get size for mapping pairs array, error "
  1561. "code %i.", err);
  1562. goto rl_err_out;
  1563. }
  1564. down_write(&ni->runlist.lock);
  1565. if (!NInoAttr(ni))
  1566. base_ni = ni;
  1567. else
  1568. base_ni = ni->ext.base_ntfs_ino;
  1569. m = map_mft_record(base_ni);
  1570. if (IS_ERR(m)) {
  1571. err = PTR_ERR(m);
  1572. m = NULL;
  1573. ctx = NULL;
  1574. goto err_out;
  1575. }
  1576. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1577. if (unlikely(!ctx)) {
  1578. err = -ENOMEM;
  1579. goto err_out;
  1580. }
  1581. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1582. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1583. if (unlikely(err)) {
  1584. if (err == -ENOENT)
  1585. err = -EIO;
  1586. goto err_out;
  1587. }
  1588. m = ctx->mrec;
  1589. a = ctx->attr;
  1590. BUG_ON(NInoNonResident(ni));
  1591. BUG_ON(a->non_resident);
  1592. /*
  1593. * Calculate new offsets for the name and the mapping pairs array.
  1594. */
  1595. if (NInoSparse(ni) || NInoCompressed(ni))
  1596. name_ofs = (offsetof(ATTR_REC,
  1597. data.non_resident.compressed_size) +
  1598. sizeof(a->data.non_resident.compressed_size) +
  1599. 7) & ~7;
  1600. else
  1601. name_ofs = (offsetof(ATTR_REC,
  1602. data.non_resident.compressed_size) + 7) & ~7;
  1603. mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
  1604. /*
  1605. * Determine the size of the resident part of the now non-resident
  1606. * attribute record.
  1607. */
  1608. arec_size = (mp_ofs + mp_size + 7) & ~7;
  1609. /*
  1610. * If the page is not uptodate bring it uptodate by copying from the
  1611. * attribute value.
  1612. */
  1613. attr_size = le32_to_cpu(a->data.resident.value_length);
  1614. BUG_ON(attr_size != data_size);
  1615. if (page && !PageUptodate(page)) {
  1616. kaddr = kmap_atomic(page, KM_USER0);
  1617. memcpy(kaddr, (u8*)a +
  1618. le16_to_cpu(a->data.resident.value_offset),
  1619. attr_size);
  1620. memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
  1621. kunmap_atomic(kaddr, KM_USER0);
  1622. flush_dcache_page(page);
  1623. SetPageUptodate(page);
  1624. }
  1625. /* Backup the attribute flag. */
  1626. old_res_attr_flags = a->data.resident.flags;
  1627. /* Resize the resident part of the attribute record. */
  1628. err = ntfs_attr_record_resize(m, a, arec_size);
  1629. if (unlikely(err))
  1630. goto err_out;
  1631. /*
  1632. * Convert the resident part of the attribute record to describe a
  1633. * non-resident attribute.
  1634. */
  1635. a->non_resident = 1;
  1636. /* Move the attribute name if it exists and update the offset. */
  1637. if (a->name_length)
  1638. memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
  1639. a->name_length * sizeof(ntfschar));
  1640. a->name_offset = cpu_to_le16(name_ofs);
  1641. /* Setup the fields specific to non-resident attributes. */
  1642. a->data.non_resident.lowest_vcn = 0;
  1643. a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
  1644. vol->cluster_size_bits);
  1645. a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
  1646. memset(&a->data.non_resident.reserved, 0,
  1647. sizeof(a->data.non_resident.reserved));
  1648. a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
  1649. a->data.non_resident.data_size =
  1650. a->data.non_resident.initialized_size =
  1651. cpu_to_sle64(attr_size);
  1652. if (NInoSparse(ni) || NInoCompressed(ni)) {
  1653. a->data.non_resident.compression_unit = 0;
  1654. if (NInoCompressed(ni) || vol->major_ver < 3)
  1655. a->data.non_resident.compression_unit = 4;
  1656. a->data.non_resident.compressed_size =
  1657. a->data.non_resident.allocated_size;
  1658. } else
  1659. a->data.non_resident.compression_unit = 0;
  1660. /* Generate the mapping pairs array into the attribute record. */
  1661. err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
  1662. arec_size - mp_ofs, rl, 0, -1, NULL);
  1663. if (unlikely(err)) {
  1664. ntfs_debug("Failed to build mapping pairs, error code %i.",
  1665. err);
  1666. goto undo_err_out;
  1667. }
  1668. /* Setup the in-memory attribute structure to be non-resident. */
  1669. ni->runlist.rl = rl;
  1670. write_lock_irqsave(&ni->size_lock, flags);
  1671. ni->allocated_size = new_size;
  1672. if (NInoSparse(ni) || NInoCompressed(ni)) {
  1673. ni->itype.compressed.size = ni->allocated_size;
  1674. if (a->data.non_resident.compression_unit) {
  1675. ni->itype.compressed.block_size = 1U << (a->data.
  1676. non_resident.compression_unit +
  1677. vol->cluster_size_bits);
  1678. ni->itype.compressed.block_size_bits =
  1679. ffs(ni->itype.compressed.block_size) -
  1680. 1;
  1681. ni->itype.compressed.block_clusters = 1U <<
  1682. a->data.non_resident.compression_unit;
  1683. } else {
  1684. ni->itype.compressed.block_size = 0;
  1685. ni->itype.compressed.block_size_bits = 0;
  1686. ni->itype.compressed.block_clusters = 0;
  1687. }
  1688. vi->i_blocks = ni->itype.compressed.size >> 9;
  1689. } else
  1690. vi->i_blocks = ni->allocated_size >> 9;
  1691. write_unlock_irqrestore(&ni->size_lock, flags);
  1692. /*
  1693. * This needs to be last since the address space operations ->readpage
  1694. * and ->writepage can run concurrently with us as they are not
  1695. * serialized on i_mutex. Note, we are not allowed to fail once we flip
  1696. * this switch, which is another reason to do this last.
  1697. */
  1698. NInoSetNonResident(ni);
  1699. /* Mark the mft record dirty, so it gets written back. */
  1700. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1701. mark_mft_record_dirty(ctx->ntfs_ino);
  1702. ntfs_attr_put_search_ctx(ctx);
  1703. unmap_mft_record(base_ni);
  1704. up_write(&ni->runlist.lock);
  1705. if (page) {
  1706. set_page_dirty(page);
  1707. unlock_page(page);
  1708. mark_page_accessed(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, KM_USER0);
  1767. memcpy((u8*)a + mp_ofs, kaddr, attr_size);
  1768. kunmap_atomic(kaddr, KM_USER0);
  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, KM_USER0);
  2497. memset(kaddr + start_ofs, val, size - start_ofs);
  2498. flush_dcache_page(page);
  2499. kunmap_atomic(kaddr, KM_USER0);
  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, KM_USER0);
  2518. memset(kaddr, val, PAGE_CACHE_SIZE);
  2519. flush_dcache_page(page);
  2520. kunmap_atomic(kaddr, KM_USER0);
  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, KM_USER0);
  2554. memset(kaddr, val, end_ofs);
  2555. flush_dcache_page(page);
  2556. kunmap_atomic(kaddr, KM_USER0);
  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 */