attrib.c 90 KB

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