attrib.c 90 KB

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