attrib.c 89 KB

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