attrib.c 89 KB

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