attrib.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  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 (error, index 0x%lx).", idx);
  2490. return PTR_ERR(page);
  2491. }
  2492. /*
  2493. * If the last page is the same as the first page, need to
  2494. * limit the write to the end offset.
  2495. */
  2496. size = PAGE_CACHE_SIZE;
  2497. if (idx == end)
  2498. size = end_ofs;
  2499. kaddr = kmap_atomic(page, KM_USER0);
  2500. memset(kaddr + start_ofs, val, size - start_ofs);
  2501. flush_dcache_page(page);
  2502. kunmap_atomic(kaddr, KM_USER0);
  2503. set_page_dirty(page);
  2504. page_cache_release(page);
  2505. if (idx == end)
  2506. goto done;
  2507. idx++;
  2508. }
  2509. /* Do the whole pages the fast way. */
  2510. for (; idx < end; idx++) {
  2511. /* Find or create the current page. (The page is locked.) */
  2512. page = grab_cache_page(mapping, idx);
  2513. if (unlikely(!page)) {
  2514. ntfs_error(vol->sb, "Insufficient memory to grab "
  2515. "page (index 0x%lx).", idx);
  2516. return -ENOMEM;
  2517. }
  2518. kaddr = kmap_atomic(page, KM_USER0);
  2519. memset(kaddr, val, PAGE_CACHE_SIZE);
  2520. flush_dcache_page(page);
  2521. kunmap_atomic(kaddr, KM_USER0);
  2522. /*
  2523. * If the page has buffers, mark them uptodate since buffer
  2524. * state and not page state is definitive in 2.6 kernels.
  2525. */
  2526. if (page_has_buffers(page)) {
  2527. struct buffer_head *bh, *head;
  2528. bh = head = page_buffers(page);
  2529. do {
  2530. set_buffer_uptodate(bh);
  2531. } while ((bh = bh->b_this_page) != head);
  2532. }
  2533. /* Now that buffers are uptodate, set the page uptodate, too. */
  2534. SetPageUptodate(page);
  2535. /*
  2536. * Set the page and all its buffers dirty and mark the inode
  2537. * dirty, too. The VM will write the page later on.
  2538. */
  2539. set_page_dirty(page);
  2540. /* Finally unlock and release the page. */
  2541. unlock_page(page);
  2542. page_cache_release(page);
  2543. balance_dirty_pages_ratelimited(mapping);
  2544. cond_resched();
  2545. }
  2546. /* If there is a last partial page, need to do it the slow way. */
  2547. if (end_ofs) {
  2548. page = read_mapping_page(mapping, idx, NULL);
  2549. if (IS_ERR(page)) {
  2550. ntfs_error(vol->sb, "Failed to read last partial page "
  2551. "(error, index 0x%lx).", idx);
  2552. return PTR_ERR(page);
  2553. }
  2554. kaddr = kmap_atomic(page, KM_USER0);
  2555. memset(kaddr, val, end_ofs);
  2556. flush_dcache_page(page);
  2557. kunmap_atomic(kaddr, KM_USER0);
  2558. set_page_dirty(page);
  2559. page_cache_release(page);
  2560. }
  2561. done:
  2562. ntfs_debug("Done.");
  2563. return 0;
  2564. }
  2565. #endif /* NTFS_RW */