attrib.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. /**
  2. * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
  3. *
  4. * Copyright (c) 2001-2005 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/swap.h>
  24. #include "attrib.h"
  25. #include "debug.h"
  26. #include "layout.h"
  27. #include "lcnalloc.h"
  28. #include "malloc.h"
  29. #include "mft.h"
  30. #include "ntfs.h"
  31. #include "types.h"
  32. /**
  33. * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
  34. * @ni: ntfs inode for which to map (part of) a runlist
  35. * @vcn: map runlist part containing this vcn
  36. * @ctx: active attribute search context if present or NULL if not
  37. *
  38. * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
  39. *
  40. * If @ctx is specified, it is an active search context of @ni and its base mft
  41. * record. This is needed when ntfs_map_runlist_nolock() encounters unmapped
  42. * runlist fragments and allows their mapping. If you do not have the mft
  43. * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
  44. * will perform the necessary mapping and unmapping.
  45. *
  46. * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
  47. * restores it before returning. Thus, @ctx will be left pointing to the same
  48. * attribute on return as on entry. However, the actual pointers in @ctx may
  49. * point to different memory locations on return, so you must remember to reset
  50. * any cached pointers from the @ctx, i.e. after the call to
  51. * ntfs_map_runlist_nolock(), you will probably want to do:
  52. * m = ctx->mrec;
  53. * a = ctx->attr;
  54. * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
  55. * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
  56. *
  57. * Return 0 on success and -errno on error. There is one special error code
  58. * which is not an error as such. This is -ENOENT. It means that @vcn is out
  59. * of bounds of the runlist.
  60. *
  61. * Note the runlist can be NULL after this function returns if @vcn is zero and
  62. * the attribute has zero allocated size, i.e. there simply is no runlist.
  63. *
  64. * WARNING: If @ctx is supplied, regardless of whether success or failure is
  65. * returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
  66. * is no longer valid, i.e. you need to either call
  67. * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
  68. * In that case PTR_ERR(@ctx->mrec) will give you the error code for
  69. * why the mapping of the old inode failed.
  70. *
  71. * Locking: - The runlist described by @ni must be locked for writing on entry
  72. * and is locked on return. Note the runlist will be modified.
  73. * - If @ctx is NULL, the base mft record of @ni must not be mapped on
  74. * entry and it will be left unmapped on return.
  75. * - If @ctx is not NULL, the base mft record must be mapped on entry
  76. * and it will be left mapped on return.
  77. */
  78. int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
  79. {
  80. VCN end_vcn;
  81. unsigned long flags;
  82. ntfs_inode *base_ni;
  83. MFT_RECORD *m;
  84. ATTR_RECORD *a;
  85. runlist_element *rl;
  86. struct page *put_this_page = NULL;
  87. int err = 0;
  88. BOOL ctx_is_temporary, ctx_needs_reset;
  89. ntfs_attr_search_ctx old_ctx;
  90. ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
  91. (unsigned long long)vcn);
  92. if (!NInoAttr(ni))
  93. base_ni = ni;
  94. else
  95. base_ni = ni->ext.base_ntfs_ino;
  96. if (!ctx) {
  97. ctx_is_temporary = ctx_needs_reset = TRUE;
  98. m = map_mft_record(base_ni);
  99. if (IS_ERR(m))
  100. return PTR_ERR(m);
  101. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  102. if (unlikely(!ctx)) {
  103. err = -ENOMEM;
  104. goto err_out;
  105. }
  106. } else {
  107. VCN allocated_size_vcn;
  108. BUG_ON(IS_ERR(ctx->mrec));
  109. a = ctx->attr;
  110. BUG_ON(!a->non_resident);
  111. ctx_is_temporary = FALSE;
  112. end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
  113. read_lock_irqsave(&ni->size_lock, flags);
  114. allocated_size_vcn = ni->allocated_size >>
  115. ni->vol->cluster_size_bits;
  116. read_unlock_irqrestore(&ni->size_lock, flags);
  117. if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
  118. end_vcn = allocated_size_vcn - 1;
  119. /*
  120. * If we already have the attribute extent containing @vcn in
  121. * @ctx, no need to look it up again. We slightly cheat in
  122. * that if vcn exceeds the allocated size, we will refuse to
  123. * map the runlist below, so there is definitely no need to get
  124. * the right attribute extent.
  125. */
  126. if (vcn >= allocated_size_vcn || (a->type == ni->type &&
  127. a->name_length == ni->name_len &&
  128. !memcmp((u8*)a + le16_to_cpu(a->name_offset),
  129. ni->name, ni->name_len) &&
  130. sle64_to_cpu(a->data.non_resident.lowest_vcn)
  131. <= vcn && end_vcn >= vcn))
  132. ctx_needs_reset = FALSE;
  133. else {
  134. /* Save the old search context. */
  135. old_ctx = *ctx;
  136. /*
  137. * If the currently mapped (extent) inode is not the
  138. * base inode we will unmap it when we reinitialize the
  139. * search context which means we need to get a
  140. * reference to the page containing the mapped mft
  141. * record so we do not accidentally drop changes to the
  142. * mft record when it has not been marked dirty yet.
  143. */
  144. if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
  145. old_ctx.base_ntfs_ino) {
  146. put_this_page = old_ctx.ntfs_ino->page;
  147. page_cache_get(put_this_page);
  148. }
  149. /*
  150. * Reinitialize the search context so we can lookup the
  151. * needed attribute extent.
  152. */
  153. ntfs_attr_reinit_search_ctx(ctx);
  154. ctx_needs_reset = TRUE;
  155. }
  156. }
  157. if (ctx_needs_reset) {
  158. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  159. CASE_SENSITIVE, vcn, NULL, 0, ctx);
  160. if (unlikely(err)) {
  161. if (err == -ENOENT)
  162. err = -EIO;
  163. goto err_out;
  164. }
  165. BUG_ON(!ctx->attr->non_resident);
  166. }
  167. a = ctx->attr;
  168. /*
  169. * Only decompress the mapping pairs if @vcn is inside it. Otherwise
  170. * we get into problems when we try to map an out of bounds vcn because
  171. * we then try to map the already mapped runlist fragment and
  172. * ntfs_mapping_pairs_decompress() fails.
  173. */
  174. end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
  175. if (!a->data.non_resident.lowest_vcn && end_vcn == 1)
  176. end_vcn = sle64_to_cpu(a->data.non_resident.allocated_size) >>
  177. ni->vol->cluster_size_bits;
  178. if (unlikely(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 acomplished 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. * @write_locked: true if the runlist is locked for writing
  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 @write_locked is true the caller has locked the runlist for writing and
  409. * if false for reading.
  410. *
  411. * Note you need to distinguish between the lcn of the returned runlist element
  412. * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
  413. * read and allocate clusters on write.
  414. *
  415. * Return the runlist element containing the @vcn on success and
  416. * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR()
  417. * to decide if the return is success or failure and PTR_ERR() to get to the
  418. * error code if IS_ERR() is true.
  419. *
  420. * The possible error return codes are:
  421. * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
  422. * -ENOMEM - Not enough memory to map runlist.
  423. * -EIO - Critical error (runlist/file is corrupt, i/o error, etc).
  424. *
  425. * Locking: - The runlist must be locked on entry and is left locked on return.
  426. * - If @write_locked is FALSE, i.e. the runlist is locked for reading,
  427. * the lock may be dropped inside the function so you cannot rely on
  428. * the runlist still being the same when this function returns.
  429. */
  430. runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
  431. const BOOL write_locked)
  432. {
  433. unsigned long flags;
  434. runlist_element *rl;
  435. int err = 0;
  436. BOOL is_retry = FALSE;
  437. ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
  438. ni->mft_no, (unsigned long long)vcn,
  439. write_locked ? "write" : "read");
  440. BUG_ON(!ni);
  441. BUG_ON(!NInoNonResident(ni));
  442. BUG_ON(vcn < 0);
  443. if (!ni->runlist.rl) {
  444. read_lock_irqsave(&ni->size_lock, flags);
  445. if (!ni->allocated_size) {
  446. read_unlock_irqrestore(&ni->size_lock, flags);
  447. return ERR_PTR(-ENOENT);
  448. }
  449. read_unlock_irqrestore(&ni->size_lock, flags);
  450. }
  451. retry_remap:
  452. rl = ni->runlist.rl;
  453. if (likely(rl && vcn >= rl[0].vcn)) {
  454. while (likely(rl->length)) {
  455. if (unlikely(vcn < rl[1].vcn)) {
  456. if (likely(rl->lcn >= LCN_HOLE)) {
  457. ntfs_debug("Done.");
  458. return rl;
  459. }
  460. break;
  461. }
  462. rl++;
  463. }
  464. if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
  465. if (likely(rl->lcn == LCN_ENOENT))
  466. err = -ENOENT;
  467. else
  468. err = -EIO;
  469. }
  470. }
  471. if (!err && !is_retry) {
  472. /*
  473. * The @vcn is in an unmapped region, map the runlist and
  474. * retry.
  475. */
  476. if (!write_locked) {
  477. up_read(&ni->runlist.lock);
  478. down_write(&ni->runlist.lock);
  479. if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
  480. LCN_RL_NOT_MAPPED)) {
  481. up_write(&ni->runlist.lock);
  482. down_read(&ni->runlist.lock);
  483. goto retry_remap;
  484. }
  485. }
  486. err = ntfs_map_runlist_nolock(ni, vcn, NULL);
  487. if (!write_locked) {
  488. up_write(&ni->runlist.lock);
  489. down_read(&ni->runlist.lock);
  490. }
  491. if (likely(!err)) {
  492. is_retry = TRUE;
  493. goto retry_remap;
  494. }
  495. /*
  496. * -EINVAL coming from a failed mapping attempt is equivalent
  497. * to i/o error for us as it should not happen in our code
  498. * paths.
  499. */
  500. if (err == -EINVAL)
  501. err = -EIO;
  502. } else if (!err)
  503. err = -EIO;
  504. if (err != -ENOENT)
  505. ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
  506. return ERR_PTR(err);
  507. }
  508. /**
  509. * ntfs_attr_find - find (next) attribute in mft record
  510. * @type: attribute type to find
  511. * @name: attribute name to find (optional, i.e. NULL means don't care)
  512. * @name_len: attribute name length (only needed if @name present)
  513. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  514. * @val: attribute value to find (optional, resident attributes only)
  515. * @val_len: attribute value length
  516. * @ctx: search context with mft record and attribute to search from
  517. *
  518. * You should not need to call this function directly. Use ntfs_attr_lookup()
  519. * instead.
  520. *
  521. * ntfs_attr_find() takes a search context @ctx as parameter and searches the
  522. * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
  523. * attribute of @type, optionally @name and @val.
  524. *
  525. * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
  526. * point to the found attribute.
  527. *
  528. * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
  529. * @ctx->attr will point to the attribute before which the attribute being
  530. * searched for would need to be inserted if such an action were to be desired.
  531. *
  532. * On actual error, ntfs_attr_find() returns -EIO. In this case @ctx->attr is
  533. * undefined and in particular do not rely on it not changing.
  534. *
  535. * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it
  536. * is FALSE, the search begins after @ctx->attr.
  537. *
  538. * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
  539. * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
  540. * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at
  541. * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case
  542. * sensitive. When @name is present, @name_len is the @name length in Unicode
  543. * characters.
  544. *
  545. * If @name is not present (NULL), we assume that the unnamed attribute is
  546. * being searched for.
  547. *
  548. * Finally, the resident attribute value @val is looked for, if present. If
  549. * @val is not present (NULL), @val_len is ignored.
  550. *
  551. * ntfs_attr_find() only searches the specified mft record and it ignores the
  552. * presence of an attribute list attribute (unless it is the one being searched
  553. * for, obviously). If you need to take attribute lists into consideration,
  554. * use ntfs_attr_lookup() instead (see below). This also means that you cannot
  555. * use ntfs_attr_find() to search for extent records of non-resident
  556. * attributes, as extents with lowest_vcn != 0 are usually described by the
  557. * attribute list attribute only. - Note that it is possible that the first
  558. * extent is only in the attribute list while the last extent is in the base
  559. * mft record, so do not rely on being able to find the first extent in the
  560. * base mft record.
  561. *
  562. * Warning: Never use @val when looking for attribute types which can be
  563. * non-resident as this most likely will result in a crash!
  564. */
  565. static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
  566. const u32 name_len, const IGNORE_CASE_BOOL ic,
  567. const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
  568. {
  569. ATTR_RECORD *a;
  570. ntfs_volume *vol = ctx->ntfs_ino->vol;
  571. ntfschar *upcase = vol->upcase;
  572. u32 upcase_len = vol->upcase_len;
  573. /*
  574. * Iterate over attributes in mft record starting at @ctx->attr, or the
  575. * attribute following that, if @ctx->is_first is TRUE.
  576. */
  577. if (ctx->is_first) {
  578. a = ctx->attr;
  579. ctx->is_first = FALSE;
  580. } else
  581. a = (ATTR_RECORD*)((u8*)ctx->attr +
  582. le32_to_cpu(ctx->attr->length));
  583. for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
  584. if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
  585. le32_to_cpu(ctx->mrec->bytes_allocated))
  586. break;
  587. ctx->attr = a;
  588. if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
  589. a->type == AT_END))
  590. return -ENOENT;
  591. if (unlikely(!a->length))
  592. break;
  593. if (a->type != type)
  594. continue;
  595. /*
  596. * If @name is present, compare the two names. If @name is
  597. * missing, assume we want an unnamed attribute.
  598. */
  599. if (!name) {
  600. /* The search failed if the found attribute is named. */
  601. if (a->name_length)
  602. return -ENOENT;
  603. } else if (!ntfs_are_names_equal(name, name_len,
  604. (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
  605. a->name_length, ic, upcase, upcase_len)) {
  606. register int rc;
  607. rc = ntfs_collate_names(name, name_len,
  608. (ntfschar*)((u8*)a +
  609. le16_to_cpu(a->name_offset)),
  610. a->name_length, 1, IGNORE_CASE,
  611. upcase, upcase_len);
  612. /*
  613. * If @name collates before a->name, there is no
  614. * matching attribute.
  615. */
  616. if (rc == -1)
  617. return -ENOENT;
  618. /* If the strings are not equal, continue search. */
  619. if (rc)
  620. continue;
  621. rc = ntfs_collate_names(name, name_len,
  622. (ntfschar*)((u8*)a +
  623. le16_to_cpu(a->name_offset)),
  624. a->name_length, 1, CASE_SENSITIVE,
  625. upcase, upcase_len);
  626. if (rc == -1)
  627. return -ENOENT;
  628. if (rc)
  629. continue;
  630. }
  631. /*
  632. * The names match or @name not present and attribute is
  633. * unnamed. If no @val specified, we have found the attribute
  634. * and are done.
  635. */
  636. if (!val)
  637. return 0;
  638. /* @val is present; compare values. */
  639. else {
  640. register int rc;
  641. rc = memcmp(val, (u8*)a + le16_to_cpu(
  642. a->data.resident.value_offset),
  643. min_t(u32, val_len, le32_to_cpu(
  644. a->data.resident.value_length)));
  645. /*
  646. * If @val collates before the current attribute's
  647. * value, there is no matching attribute.
  648. */
  649. if (!rc) {
  650. register u32 avl;
  651. avl = le32_to_cpu(
  652. a->data.resident.value_length);
  653. if (val_len == avl)
  654. return 0;
  655. if (val_len < avl)
  656. return -ENOENT;
  657. } else if (rc < 0)
  658. return -ENOENT;
  659. }
  660. }
  661. ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
  662. NVolSetErrors(vol);
  663. return -EIO;
  664. }
  665. /**
  666. * load_attribute_list - load an attribute list into memory
  667. * @vol: ntfs volume from which to read
  668. * @runlist: runlist of the attribute list
  669. * @al_start: destination buffer
  670. * @size: size of the destination buffer in bytes
  671. * @initialized_size: initialized size of the attribute list
  672. *
  673. * Walk the runlist @runlist and load all clusters from it copying them into
  674. * the linear buffer @al. The maximum number of bytes copied to @al is @size
  675. * bytes. Note, @size does not need to be a multiple of the cluster size. If
  676. * @initialized_size is less than @size, the region in @al between
  677. * @initialized_size and @size will be zeroed and not read from disk.
  678. *
  679. * Return 0 on success or -errno on error.
  680. */
  681. int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
  682. const s64 size, const s64 initialized_size)
  683. {
  684. LCN lcn;
  685. u8 *al = al_start;
  686. u8 *al_end = al + initialized_size;
  687. runlist_element *rl;
  688. struct buffer_head *bh;
  689. struct super_block *sb;
  690. unsigned long block_size;
  691. unsigned long block, max_block;
  692. int err = 0;
  693. unsigned char block_size_bits;
  694. ntfs_debug("Entering.");
  695. if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
  696. initialized_size > size)
  697. return -EINVAL;
  698. if (!initialized_size) {
  699. memset(al, 0, size);
  700. return 0;
  701. }
  702. sb = vol->sb;
  703. block_size = sb->s_blocksize;
  704. block_size_bits = sb->s_blocksize_bits;
  705. down_read(&runlist->lock);
  706. rl = runlist->rl;
  707. if (!rl) {
  708. ntfs_error(sb, "Cannot read attribute list since runlist is "
  709. "missing.");
  710. goto err_out;
  711. }
  712. /* Read all clusters specified by the runlist one run at a time. */
  713. while (rl->length) {
  714. lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
  715. ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
  716. (unsigned long long)rl->vcn,
  717. (unsigned long long)lcn);
  718. /* The attribute list cannot be sparse. */
  719. if (lcn < 0) {
  720. ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
  721. "read attribute list.");
  722. goto err_out;
  723. }
  724. block = lcn << vol->cluster_size_bits >> block_size_bits;
  725. /* Read the run from device in chunks of block_size bytes. */
  726. max_block = block + (rl->length << vol->cluster_size_bits >>
  727. block_size_bits);
  728. ntfs_debug("max_block = 0x%lx.", max_block);
  729. do {
  730. ntfs_debug("Reading block = 0x%lx.", block);
  731. bh = sb_bread(sb, block);
  732. if (!bh) {
  733. ntfs_error(sb, "sb_bread() failed. Cannot "
  734. "read attribute list.");
  735. goto err_out;
  736. }
  737. if (al + block_size >= al_end)
  738. goto do_final;
  739. memcpy(al, bh->b_data, block_size);
  740. brelse(bh);
  741. al += block_size;
  742. } while (++block < max_block);
  743. rl++;
  744. }
  745. if (initialized_size < size) {
  746. initialize:
  747. memset(al_start + initialized_size, 0, size - initialized_size);
  748. }
  749. done:
  750. up_read(&runlist->lock);
  751. return err;
  752. do_final:
  753. if (al < al_end) {
  754. /*
  755. * Partial block.
  756. *
  757. * Note: The attribute list can be smaller than its allocation
  758. * by multiple clusters. This has been encountered by at least
  759. * two people running Windows XP, thus we cannot do any
  760. * truncation sanity checking here. (AIA)
  761. */
  762. memcpy(al, bh->b_data, al_end - al);
  763. brelse(bh);
  764. if (initialized_size < size)
  765. goto initialize;
  766. goto done;
  767. }
  768. brelse(bh);
  769. /* Real overflow! */
  770. ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
  771. "is truncated.");
  772. err_out:
  773. err = -EIO;
  774. goto done;
  775. }
  776. /**
  777. * ntfs_external_attr_find - find an attribute in the attribute list of an inode
  778. * @type: attribute type to find
  779. * @name: attribute name to find (optional, i.e. NULL means don't care)
  780. * @name_len: attribute name length (only needed if @name present)
  781. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  782. * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
  783. * @val: attribute value to find (optional, resident attributes only)
  784. * @val_len: attribute value length
  785. * @ctx: search context with mft record and attribute to search from
  786. *
  787. * You should not need to call this function directly. Use ntfs_attr_lookup()
  788. * instead.
  789. *
  790. * Find an attribute by searching the attribute list for the corresponding
  791. * attribute list entry. Having found the entry, map the mft record if the
  792. * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
  793. * in there and return it.
  794. *
  795. * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
  796. * have been obtained from a call to ntfs_attr_get_search_ctx(). On subsequent
  797. * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
  798. * then the base inode).
  799. *
  800. * After finishing with the attribute/mft record you need to call
  801. * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
  802. * mapped inodes, etc).
  803. *
  804. * If the attribute is found, ntfs_external_attr_find() returns 0 and
  805. * @ctx->attr will point to the found attribute. @ctx->mrec will point to the
  806. * mft record in which @ctx->attr is located and @ctx->al_entry will point to
  807. * the attribute list entry for the attribute.
  808. *
  809. * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
  810. * @ctx->attr will point to the attribute in the base mft record before which
  811. * the attribute being searched for would need to be inserted if such an action
  812. * were to be desired. @ctx->mrec will point to the mft record in which
  813. * @ctx->attr is located and @ctx->al_entry will point to the attribute list
  814. * entry of the attribute before which the attribute being searched for would
  815. * need to be inserted if such an action were to be desired.
  816. *
  817. * Thus to insert the not found attribute, one wants to add the attribute to
  818. * @ctx->mrec (the base mft record) and if there is not enough space, the
  819. * attribute should be placed in a newly allocated extent mft record. The
  820. * attribute list entry for the inserted attribute should be inserted in the
  821. * attribute list attribute at @ctx->al_entry.
  822. *
  823. * On actual error, ntfs_external_attr_find() returns -EIO. In this case
  824. * @ctx->attr is undefined and in particular do not rely on it not changing.
  825. */
  826. static int ntfs_external_attr_find(const ATTR_TYPE type,
  827. const ntfschar *name, const u32 name_len,
  828. const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
  829. const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
  830. {
  831. ntfs_inode *base_ni, *ni;
  832. ntfs_volume *vol;
  833. ATTR_LIST_ENTRY *al_entry, *next_al_entry;
  834. u8 *al_start, *al_end;
  835. ATTR_RECORD *a;
  836. ntfschar *al_name;
  837. u32 al_name_len;
  838. int err = 0;
  839. static const char *es = " Unmount and run chkdsk.";
  840. ni = ctx->ntfs_ino;
  841. base_ni = ctx->base_ntfs_ino;
  842. ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
  843. if (!base_ni) {
  844. /* First call happens with the base mft record. */
  845. base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
  846. ctx->base_mrec = ctx->mrec;
  847. }
  848. if (ni == base_ni)
  849. ctx->base_attr = ctx->attr;
  850. if (type == AT_END)
  851. goto not_found;
  852. vol = base_ni->vol;
  853. al_start = base_ni->attr_list;
  854. al_end = al_start + base_ni->attr_list_size;
  855. if (!ctx->al_entry)
  856. ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
  857. /*
  858. * Iterate over entries in attribute list starting at @ctx->al_entry,
  859. * or the entry following that, if @ctx->is_first is TRUE.
  860. */
  861. if (ctx->is_first) {
  862. al_entry = ctx->al_entry;
  863. ctx->is_first = FALSE;
  864. } else
  865. al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
  866. le16_to_cpu(ctx->al_entry->length));
  867. for (;; al_entry = next_al_entry) {
  868. /* Out of bounds check. */
  869. if ((u8*)al_entry < base_ni->attr_list ||
  870. (u8*)al_entry > al_end)
  871. break; /* Inode is corrupt. */
  872. ctx->al_entry = al_entry;
  873. /* Catch the end of the attribute list. */
  874. if ((u8*)al_entry == al_end)
  875. goto not_found;
  876. if (!al_entry->length)
  877. break;
  878. if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
  879. le16_to_cpu(al_entry->length) > al_end)
  880. break;
  881. next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
  882. le16_to_cpu(al_entry->length));
  883. if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
  884. goto not_found;
  885. if (type != al_entry->type)
  886. continue;
  887. /*
  888. * If @name is present, compare the two names. If @name is
  889. * missing, assume we want an unnamed attribute.
  890. */
  891. al_name_len = al_entry->name_length;
  892. al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
  893. if (!name) {
  894. if (al_name_len)
  895. goto not_found;
  896. } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
  897. name_len, ic, vol->upcase, vol->upcase_len)) {
  898. register int rc;
  899. rc = ntfs_collate_names(name, name_len, al_name,
  900. al_name_len, 1, IGNORE_CASE,
  901. vol->upcase, vol->upcase_len);
  902. /*
  903. * If @name collates before al_name, there is no
  904. * matching attribute.
  905. */
  906. if (rc == -1)
  907. goto not_found;
  908. /* If the strings are not equal, continue search. */
  909. if (rc)
  910. continue;
  911. /*
  912. * FIXME: Reverse engineering showed 0, IGNORE_CASE but
  913. * that is inconsistent with ntfs_attr_find(). The
  914. * subsequent rc checks were also different. Perhaps I
  915. * made a mistake in one of the two. Need to recheck
  916. * which is correct or at least see what is going on...
  917. * (AIA)
  918. */
  919. rc = ntfs_collate_names(name, name_len, al_name,
  920. al_name_len, 1, CASE_SENSITIVE,
  921. vol->upcase, vol->upcase_len);
  922. if (rc == -1)
  923. goto not_found;
  924. if (rc)
  925. continue;
  926. }
  927. /*
  928. * The names match or @name not present and attribute is
  929. * unnamed. Now check @lowest_vcn. Continue search if the
  930. * next attribute list entry still fits @lowest_vcn. Otherwise
  931. * we have reached the right one or the search has failed.
  932. */
  933. if (lowest_vcn && (u8*)next_al_entry >= al_start &&
  934. (u8*)next_al_entry + 6 < al_end &&
  935. (u8*)next_al_entry + le16_to_cpu(
  936. next_al_entry->length) <= al_end &&
  937. sle64_to_cpu(next_al_entry->lowest_vcn) <=
  938. lowest_vcn &&
  939. next_al_entry->type == al_entry->type &&
  940. next_al_entry->name_length == al_name_len &&
  941. ntfs_are_names_equal((ntfschar*)((u8*)
  942. next_al_entry +
  943. next_al_entry->name_offset),
  944. next_al_entry->name_length,
  945. al_name, al_name_len, CASE_SENSITIVE,
  946. vol->upcase, vol->upcase_len))
  947. continue;
  948. if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
  949. if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
  950. ntfs_error(vol->sb, "Found stale mft "
  951. "reference in attribute list "
  952. "of base inode 0x%lx.%s",
  953. base_ni->mft_no, es);
  954. err = -EIO;
  955. break;
  956. }
  957. } else { /* Mft references do not match. */
  958. /* If there is a mapped record unmap it first. */
  959. if (ni != base_ni)
  960. unmap_extent_mft_record(ni);
  961. /* Do we want the base record back? */
  962. if (MREF_LE(al_entry->mft_reference) ==
  963. base_ni->mft_no) {
  964. ni = ctx->ntfs_ino = base_ni;
  965. ctx->mrec = ctx->base_mrec;
  966. } else {
  967. /* We want an extent record. */
  968. ctx->mrec = map_extent_mft_record(base_ni,
  969. le64_to_cpu(
  970. al_entry->mft_reference), &ni);
  971. if (IS_ERR(ctx->mrec)) {
  972. ntfs_error(vol->sb, "Failed to map "
  973. "extent mft record "
  974. "0x%lx of base inode "
  975. "0x%lx.%s",
  976. MREF_LE(al_entry->
  977. mft_reference),
  978. base_ni->mft_no, es);
  979. err = PTR_ERR(ctx->mrec);
  980. if (err == -ENOENT)
  981. err = -EIO;
  982. /* Cause @ctx to be sanitized below. */
  983. ni = NULL;
  984. break;
  985. }
  986. ctx->ntfs_ino = ni;
  987. }
  988. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  989. le16_to_cpu(ctx->mrec->attrs_offset));
  990. }
  991. /*
  992. * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
  993. * mft record containing the attribute represented by the
  994. * current al_entry.
  995. */
  996. /*
  997. * We could call into ntfs_attr_find() to find the right
  998. * attribute in this mft record but this would be less
  999. * efficient and not quite accurate as ntfs_attr_find() ignores
  1000. * the attribute instance numbers for example which become
  1001. * important when one plays with attribute lists. Also,
  1002. * because a proper match has been found in the attribute list
  1003. * entry above, the comparison can now be optimized. So it is
  1004. * worth re-implementing a simplified ntfs_attr_find() here.
  1005. */
  1006. a = ctx->attr;
  1007. /*
  1008. * Use a manual loop so we can still use break and continue
  1009. * with the same meanings as above.
  1010. */
  1011. do_next_attr_loop:
  1012. if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
  1013. le32_to_cpu(ctx->mrec->bytes_allocated))
  1014. break;
  1015. if (a->type == AT_END)
  1016. continue;
  1017. if (!a->length)
  1018. break;
  1019. if (al_entry->instance != a->instance)
  1020. goto do_next_attr;
  1021. /*
  1022. * If the type and/or the name are mismatched between the
  1023. * attribute list entry and the attribute record, there is
  1024. * corruption so we break and return error EIO.
  1025. */
  1026. if (al_entry->type != a->type)
  1027. break;
  1028. if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
  1029. le16_to_cpu(a->name_offset)), a->name_length,
  1030. al_name, al_name_len, CASE_SENSITIVE,
  1031. vol->upcase, vol->upcase_len))
  1032. break;
  1033. ctx->attr = a;
  1034. /*
  1035. * If no @val specified or @val specified and it matches, we
  1036. * have found it!
  1037. */
  1038. if (!val || (!a->non_resident && le32_to_cpu(
  1039. a->data.resident.value_length) == val_len &&
  1040. !memcmp((u8*)a +
  1041. le16_to_cpu(a->data.resident.value_offset),
  1042. val, val_len))) {
  1043. ntfs_debug("Done, found.");
  1044. return 0;
  1045. }
  1046. do_next_attr:
  1047. /* Proceed to the next attribute in the current mft record. */
  1048. a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
  1049. goto do_next_attr_loop;
  1050. }
  1051. if (!err) {
  1052. ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
  1053. "attribute list attribute.%s", base_ni->mft_no,
  1054. es);
  1055. err = -EIO;
  1056. }
  1057. if (ni != base_ni) {
  1058. if (ni)
  1059. unmap_extent_mft_record(ni);
  1060. ctx->ntfs_ino = base_ni;
  1061. ctx->mrec = ctx->base_mrec;
  1062. ctx->attr = ctx->base_attr;
  1063. }
  1064. if (err != -ENOMEM)
  1065. NVolSetErrors(vol);
  1066. return err;
  1067. not_found:
  1068. /*
  1069. * If we were looking for AT_END, we reset the search context @ctx and
  1070. * use ntfs_attr_find() to seek to the end of the base mft record.
  1071. */
  1072. if (type == AT_END) {
  1073. ntfs_attr_reinit_search_ctx(ctx);
  1074. return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
  1075. ctx);
  1076. }
  1077. /*
  1078. * The attribute was not found. Before we return, we want to ensure
  1079. * @ctx->mrec and @ctx->attr indicate the position at which the
  1080. * attribute should be inserted in the base mft record. Since we also
  1081. * want to preserve @ctx->al_entry we cannot reinitialize the search
  1082. * context using ntfs_attr_reinit_search_ctx() as this would set
  1083. * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see
  1084. * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve
  1085. * @ctx->al_entry as the remaining fields (base_*) are identical to
  1086. * their non base_ counterparts and we cannot set @ctx->base_attr
  1087. * correctly yet as we do not know what @ctx->attr will be set to by
  1088. * the call to ntfs_attr_find() below.
  1089. */
  1090. if (ni != base_ni)
  1091. unmap_extent_mft_record(ni);
  1092. ctx->mrec = ctx->base_mrec;
  1093. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  1094. le16_to_cpu(ctx->mrec->attrs_offset));
  1095. ctx->is_first = TRUE;
  1096. ctx->ntfs_ino = base_ni;
  1097. ctx->base_ntfs_ino = NULL;
  1098. ctx->base_mrec = NULL;
  1099. ctx->base_attr = NULL;
  1100. /*
  1101. * In case there are multiple matches in the base mft record, need to
  1102. * keep enumerating until we get an attribute not found response (or
  1103. * another error), otherwise we would keep returning the same attribute
  1104. * over and over again and all programs using us for enumeration would
  1105. * lock up in a tight loop.
  1106. */
  1107. do {
  1108. err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
  1109. ctx);
  1110. } while (!err);
  1111. ntfs_debug("Done, not found.");
  1112. return err;
  1113. }
  1114. /**
  1115. * ntfs_attr_lookup - find an attribute in an ntfs inode
  1116. * @type: attribute type to find
  1117. * @name: attribute name to find (optional, i.e. NULL means don't care)
  1118. * @name_len: attribute name length (only needed if @name present)
  1119. * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
  1120. * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only)
  1121. * @val: attribute value to find (optional, resident attributes only)
  1122. * @val_len: attribute value length
  1123. * @ctx: search context with mft record and attribute to search from
  1124. *
  1125. * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must
  1126. * be the base mft record and @ctx must have been obtained from a call to
  1127. * ntfs_attr_get_search_ctx().
  1128. *
  1129. * This function transparently handles attribute lists and @ctx is used to
  1130. * continue searches where they were left off at.
  1131. *
  1132. * After finishing with the attribute/mft record you need to call
  1133. * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
  1134. * mapped inodes, etc).
  1135. *
  1136. * Return 0 if the search was successful and -errno if not.
  1137. *
  1138. * When 0, @ctx->attr is the found attribute and it is in mft record
  1139. * @ctx->mrec. If an attribute list attribute is present, @ctx->al_entry is
  1140. * the attribute list entry of the found attribute.
  1141. *
  1142. * When -ENOENT, @ctx->attr is the attribute which collates just after the
  1143. * attribute being searched for, i.e. if one wants to add the attribute to the
  1144. * mft record this is the correct place to insert it into. If an attribute
  1145. * list attribute is present, @ctx->al_entry is the attribute list entry which
  1146. * collates just after the attribute list entry of the attribute being searched
  1147. * for, i.e. if one wants to add the attribute to the mft record this is the
  1148. * correct place to insert its attribute list entry into.
  1149. *
  1150. * When -errno != -ENOENT, an error occured during the lookup. @ctx->attr is
  1151. * then undefined and in particular you should not rely on it not changing.
  1152. */
  1153. int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
  1154. const u32 name_len, const IGNORE_CASE_BOOL ic,
  1155. const VCN lowest_vcn, const u8 *val, const u32 val_len,
  1156. ntfs_attr_search_ctx *ctx)
  1157. {
  1158. ntfs_inode *base_ni;
  1159. ntfs_debug("Entering.");
  1160. if (ctx->base_ntfs_ino)
  1161. base_ni = ctx->base_ntfs_ino;
  1162. else
  1163. base_ni = ctx->ntfs_ino;
  1164. /* Sanity check, just for debugging really. */
  1165. BUG_ON(!base_ni);
  1166. if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
  1167. return ntfs_attr_find(type, name, name_len, ic, val, val_len,
  1168. ctx);
  1169. return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
  1170. val, val_len, ctx);
  1171. }
  1172. /**
  1173. * ntfs_attr_init_search_ctx - initialize an attribute search context
  1174. * @ctx: attribute search context to initialize
  1175. * @ni: ntfs inode with which to initialize the search context
  1176. * @mrec: mft record with which to initialize the search context
  1177. *
  1178. * Initialize the attribute search context @ctx with @ni and @mrec.
  1179. */
  1180. static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
  1181. ntfs_inode *ni, MFT_RECORD *mrec)
  1182. {
  1183. *ctx = (ntfs_attr_search_ctx) {
  1184. .mrec = mrec,
  1185. /* Sanity checks are performed elsewhere. */
  1186. .attr = (ATTR_RECORD*)((u8*)mrec +
  1187. le16_to_cpu(mrec->attrs_offset)),
  1188. .is_first = TRUE,
  1189. .ntfs_ino = ni,
  1190. };
  1191. }
  1192. /**
  1193. * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
  1194. * @ctx: attribute search context to reinitialize
  1195. *
  1196. * Reinitialize the attribute search context @ctx, unmapping an associated
  1197. * extent mft record if present, and initialize the search context again.
  1198. *
  1199. * This is used when a search for a new attribute is being started to reset
  1200. * the search context to the beginning.
  1201. */
  1202. void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
  1203. {
  1204. if (likely(!ctx->base_ntfs_ino)) {
  1205. /* No attribute list. */
  1206. ctx->is_first = TRUE;
  1207. /* Sanity checks are performed elsewhere. */
  1208. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  1209. le16_to_cpu(ctx->mrec->attrs_offset));
  1210. /*
  1211. * This needs resetting due to ntfs_external_attr_find() which
  1212. * can leave it set despite having zeroed ctx->base_ntfs_ino.
  1213. */
  1214. ctx->al_entry = NULL;
  1215. return;
  1216. } /* Attribute list. */
  1217. if (ctx->ntfs_ino != ctx->base_ntfs_ino)
  1218. unmap_extent_mft_record(ctx->ntfs_ino);
  1219. ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
  1220. return;
  1221. }
  1222. /**
  1223. * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
  1224. * @ni: ntfs inode with which to initialize the search context
  1225. * @mrec: mft record with which to initialize the search context
  1226. *
  1227. * Allocate a new attribute search context, initialize it with @ni and @mrec,
  1228. * and return it. Return NULL if allocation failed.
  1229. */
  1230. ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
  1231. {
  1232. ntfs_attr_search_ctx *ctx;
  1233. ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
  1234. if (ctx)
  1235. ntfs_attr_init_search_ctx(ctx, ni, mrec);
  1236. return ctx;
  1237. }
  1238. /**
  1239. * ntfs_attr_put_search_ctx - release an attribute search context
  1240. * @ctx: attribute search context to free
  1241. *
  1242. * Release the attribute search context @ctx, unmapping an associated extent
  1243. * mft record if present.
  1244. */
  1245. void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
  1246. {
  1247. if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
  1248. unmap_extent_mft_record(ctx->ntfs_ino);
  1249. kmem_cache_free(ntfs_attr_ctx_cache, ctx);
  1250. return;
  1251. }
  1252. #ifdef NTFS_RW
  1253. /**
  1254. * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
  1255. * @vol: ntfs volume to which the attribute belongs
  1256. * @type: attribute type which to find
  1257. *
  1258. * Search for the attribute definition record corresponding to the attribute
  1259. * @type in the $AttrDef system file.
  1260. *
  1261. * Return the attribute type definition record if found and NULL if not found.
  1262. */
  1263. static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
  1264. const ATTR_TYPE type)
  1265. {
  1266. ATTR_DEF *ad;
  1267. BUG_ON(!vol->attrdef);
  1268. BUG_ON(!type);
  1269. for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
  1270. vol->attrdef_size && ad->type; ++ad) {
  1271. /* We have not found it yet, carry on searching. */
  1272. if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
  1273. continue;
  1274. /* We found the attribute; return it. */
  1275. if (likely(ad->type == type))
  1276. return ad;
  1277. /* We have gone too far already. No point in continuing. */
  1278. break;
  1279. }
  1280. /* Attribute not found. */
  1281. ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
  1282. le32_to_cpu(type));
  1283. return NULL;
  1284. }
  1285. /**
  1286. * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
  1287. * @vol: ntfs volume to which the attribute belongs
  1288. * @type: attribute type which to check
  1289. * @size: size which to check
  1290. *
  1291. * Check whether the @size in bytes is valid for an attribute of @type on the
  1292. * ntfs volume @vol. This information is obtained from $AttrDef system file.
  1293. *
  1294. * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
  1295. * listed in $AttrDef.
  1296. */
  1297. int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
  1298. const s64 size)
  1299. {
  1300. ATTR_DEF *ad;
  1301. BUG_ON(size < 0);
  1302. /*
  1303. * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
  1304. * listed in $AttrDef.
  1305. */
  1306. if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
  1307. return -ERANGE;
  1308. /* Get the $AttrDef entry for the attribute @type. */
  1309. ad = ntfs_attr_find_in_attrdef(vol, type);
  1310. if (unlikely(!ad))
  1311. return -ENOENT;
  1312. /* Do the bounds check. */
  1313. if (((sle64_to_cpu(ad->min_size) > 0) &&
  1314. size < sle64_to_cpu(ad->min_size)) ||
  1315. ((sle64_to_cpu(ad->max_size) > 0) && size >
  1316. sle64_to_cpu(ad->max_size)))
  1317. return -ERANGE;
  1318. return 0;
  1319. }
  1320. /**
  1321. * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
  1322. * @vol: ntfs volume to which the attribute belongs
  1323. * @type: attribute type which to check
  1324. *
  1325. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1326. * be non-resident. This information is obtained from $AttrDef system file.
  1327. *
  1328. * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
  1329. * -ENOENT if the attribute is not listed in $AttrDef.
  1330. */
  1331. int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1332. {
  1333. ATTR_DEF *ad;
  1334. /* Find the attribute definition record in $AttrDef. */
  1335. ad = ntfs_attr_find_in_attrdef(vol, type);
  1336. if (unlikely(!ad))
  1337. return -ENOENT;
  1338. /* Check the flags and return the result. */
  1339. if (ad->flags & ATTR_DEF_RESIDENT)
  1340. return -EPERM;
  1341. return 0;
  1342. }
  1343. /**
  1344. * ntfs_attr_can_be_resident - check if an attribute can be resident
  1345. * @vol: ntfs volume to which the attribute belongs
  1346. * @type: attribute type which to check
  1347. *
  1348. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1349. * be resident. This information is derived from our ntfs knowledge and may
  1350. * not be completely accurate, especially when user defined attributes are
  1351. * present. Basically we allow everything to be resident except for index
  1352. * allocation and $EA attributes.
  1353. *
  1354. * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
  1355. *
  1356. * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
  1357. * otherwise windows will not boot (blue screen of death)! We cannot
  1358. * check for this here as we do not know which inode's $Bitmap is
  1359. * being asked about so the caller needs to special case this.
  1360. */
  1361. int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1362. {
  1363. if (type == AT_INDEX_ALLOCATION || type == AT_EA)
  1364. return -EPERM;
  1365. return 0;
  1366. }
  1367. /**
  1368. * ntfs_attr_record_resize - resize an attribute record
  1369. * @m: mft record containing attribute record
  1370. * @a: attribute record to resize
  1371. * @new_size: new size in bytes to which to resize the attribute record @a
  1372. *
  1373. * Resize the attribute record @a, i.e. the resident part of the attribute, in
  1374. * the mft record @m to @new_size bytes.
  1375. *
  1376. * Return 0 on success and -errno on error. The following error codes are
  1377. * defined:
  1378. * -ENOSPC - Not enough space in the mft record @m to perform the resize.
  1379. *
  1380. * Note: On error, no modifications have been performed whatsoever.
  1381. *
  1382. * Warning: If you make a record smaller without having copied all the data you
  1383. * are interested in the data may be overwritten.
  1384. */
  1385. int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
  1386. {
  1387. ntfs_debug("Entering for new_size %u.", new_size);
  1388. /* Align to 8 bytes if it is not already done. */
  1389. if (new_size & 7)
  1390. new_size = (new_size + 7) & ~7;
  1391. /* If the actual attribute length has changed, move things around. */
  1392. if (new_size != le32_to_cpu(a->length)) {
  1393. u32 new_muse = le32_to_cpu(m->bytes_in_use) -
  1394. le32_to_cpu(a->length) + new_size;
  1395. /* Not enough space in this mft record. */
  1396. if (new_muse > le32_to_cpu(m->bytes_allocated))
  1397. return -ENOSPC;
  1398. /* Move attributes following @a to their new location. */
  1399. memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
  1400. le32_to_cpu(m->bytes_in_use) - ((u8*)a -
  1401. (u8*)m) - le32_to_cpu(a->length));
  1402. /* Adjust @m to reflect the change in used space. */
  1403. m->bytes_in_use = cpu_to_le32(new_muse);
  1404. /* Adjust @a to reflect the new size. */
  1405. if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
  1406. a->length = cpu_to_le32(new_size);
  1407. }
  1408. return 0;
  1409. }
  1410. /**
  1411. * ntfs_resident_attr_value_resize - resize the value of a resident attribute
  1412. * @m: mft record containing attribute record
  1413. * @a: attribute record whose value to resize
  1414. * @new_size: new size in bytes to which to resize the attribute value of @a
  1415. *
  1416. * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
  1417. * If the value is made bigger, the newly allocated space is cleared.
  1418. *
  1419. * Return 0 on success and -errno on error. The following error codes are
  1420. * defined:
  1421. * -ENOSPC - Not enough space in the mft record @m to perform the resize.
  1422. *
  1423. * Note: On error, no modifications have been performed whatsoever.
  1424. *
  1425. * Warning: If you make a record smaller without having copied all the data you
  1426. * are interested in the data may be overwritten.
  1427. */
  1428. int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
  1429. const u32 new_size)
  1430. {
  1431. u32 old_size;
  1432. /* Resize the resident part of the attribute record. */
  1433. if (ntfs_attr_record_resize(m, a,
  1434. le16_to_cpu(a->data.resident.value_offset) + new_size))
  1435. return -ENOSPC;
  1436. /*
  1437. * The resize succeeded! If we made the attribute value bigger, clear
  1438. * the area between the old size and @new_size.
  1439. */
  1440. old_size = le32_to_cpu(a->data.resident.value_length);
  1441. if (new_size > old_size)
  1442. memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
  1443. old_size, 0, new_size - old_size);
  1444. /* Finally update the length of the attribute value. */
  1445. a->data.resident.value_length = cpu_to_le32(new_size);
  1446. return 0;
  1447. }
  1448. /**
  1449. * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
  1450. * @ni: ntfs inode describing the attribute to convert
  1451. *
  1452. * Convert the resident ntfs attribute described by the ntfs inode @ni to a
  1453. * non-resident one.
  1454. *
  1455. * Return 0 on success and -errno on error. The following error return codes
  1456. * are defined:
  1457. * -EPERM - The attribute is not allowed to be non-resident.
  1458. * -ENOMEM - Not enough memory.
  1459. * -ENOSPC - Not enough disk space.
  1460. * -EINVAL - Attribute not defined on the volume.
  1461. * -EIO - I/o error or other error.
  1462. * Note that -ENOSPC is also returned in the case that there is not enough
  1463. * space in the mft record to do the conversion. This can happen when the mft
  1464. * record is already very full. The caller is responsible for trying to make
  1465. * space in the mft record and trying again. FIXME: Do we need a separate
  1466. * error return code for this kind of -ENOSPC or is it always worth trying
  1467. * again in case the attribute may then fit in a resident state so no need to
  1468. * make it non-resident at all? Ho-hum... (AIA)
  1469. *
  1470. * NOTE to self: No changes in the attribute list are required to move from
  1471. * a resident to a non-resident attribute.
  1472. *
  1473. * Locking: - The caller must hold i_sem on the inode.
  1474. */
  1475. int ntfs_attr_make_non_resident(ntfs_inode *ni)
  1476. {
  1477. s64 new_size;
  1478. struct inode *vi = VFS_I(ni);
  1479. ntfs_volume *vol = ni->vol;
  1480. ntfs_inode *base_ni;
  1481. MFT_RECORD *m;
  1482. ATTR_RECORD *a;
  1483. ntfs_attr_search_ctx *ctx;
  1484. struct page *page;
  1485. runlist_element *rl;
  1486. u8 *kaddr;
  1487. unsigned long flags;
  1488. int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
  1489. u32 attr_size;
  1490. u8 old_res_attr_flags;
  1491. /* Check that the attribute is allowed to be non-resident. */
  1492. err = ntfs_attr_can_be_non_resident(vol, ni->type);
  1493. if (unlikely(err)) {
  1494. if (err == -EPERM)
  1495. ntfs_debug("Attribute is not allowed to be "
  1496. "non-resident.");
  1497. else
  1498. ntfs_debug("Attribute not defined on the NTFS "
  1499. "volume!");
  1500. return err;
  1501. }
  1502. /*
  1503. * FIXME: Compressed and encrypted attributes are not supported when
  1504. * writing and we should never have gotten here for them.
  1505. */
  1506. BUG_ON(NInoCompressed(ni));
  1507. BUG_ON(NInoEncrypted(ni));
  1508. /*
  1509. * The size needs to be aligned to a cluster boundary for allocation
  1510. * purposes.
  1511. */
  1512. new_size = (i_size_read(vi) + vol->cluster_size - 1) &
  1513. ~(vol->cluster_size - 1);
  1514. if (new_size > 0) {
  1515. runlist_element *rl2;
  1516. /*
  1517. * Will need the page later and since the page lock nests
  1518. * outside all ntfs locks, we need to get the page now.
  1519. */
  1520. page = find_or_create_page(vi->i_mapping, 0,
  1521. mapping_gfp_mask(vi->i_mapping));
  1522. if (unlikely(!page))
  1523. return -ENOMEM;
  1524. /* Start by allocating clusters to hold the attribute value. */
  1525. rl = ntfs_cluster_alloc(vol, 0, new_size >>
  1526. vol->cluster_size_bits, -1, DATA_ZONE);
  1527. if (IS_ERR(rl)) {
  1528. err = PTR_ERR(rl);
  1529. ntfs_debug("Failed to allocate cluster%s, error code "
  1530. "%i.", (new_size >>
  1531. vol->cluster_size_bits) > 1 ? "s" : "",
  1532. err);
  1533. goto page_err_out;
  1534. }
  1535. /* Change the runlist terminator to LCN_ENOENT. */
  1536. rl2 = rl;
  1537. while (rl2->length)
  1538. rl2++;
  1539. BUG_ON(rl2->lcn != LCN_RL_NOT_MAPPED);
  1540. rl2->lcn = LCN_ENOENT;
  1541. } else {
  1542. rl = NULL;
  1543. page = NULL;
  1544. }
  1545. /* Determine the size of the mapping pairs array. */
  1546. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
  1547. if (unlikely(mp_size < 0)) {
  1548. err = mp_size;
  1549. ntfs_debug("Failed to get size for mapping pairs array, error "
  1550. "code %i.", err);
  1551. goto rl_err_out;
  1552. }
  1553. down_write(&ni->runlist.lock);
  1554. if (!NInoAttr(ni))
  1555. base_ni = ni;
  1556. else
  1557. base_ni = ni->ext.base_ntfs_ino;
  1558. m = map_mft_record(base_ni);
  1559. if (IS_ERR(m)) {
  1560. err = PTR_ERR(m);
  1561. m = NULL;
  1562. ctx = NULL;
  1563. goto err_out;
  1564. }
  1565. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1566. if (unlikely(!ctx)) {
  1567. err = -ENOMEM;
  1568. goto err_out;
  1569. }
  1570. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1571. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1572. if (unlikely(err)) {
  1573. if (err == -ENOENT)
  1574. err = -EIO;
  1575. goto err_out;
  1576. }
  1577. m = ctx->mrec;
  1578. a = ctx->attr;
  1579. BUG_ON(NInoNonResident(ni));
  1580. BUG_ON(a->non_resident);
  1581. /*
  1582. * Calculate new offsets for the name and the mapping pairs array.
  1583. */
  1584. if (NInoSparse(ni) || NInoCompressed(ni))
  1585. name_ofs = (offsetof(ATTR_REC,
  1586. data.non_resident.compressed_size) +
  1587. sizeof(a->data.non_resident.compressed_size) +
  1588. 7) & ~7;
  1589. else
  1590. name_ofs = (offsetof(ATTR_REC,
  1591. data.non_resident.compressed_size) + 7) & ~7;
  1592. mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
  1593. /*
  1594. * Determine the size of the resident part of the now non-resident
  1595. * attribute record.
  1596. */
  1597. arec_size = (mp_ofs + mp_size + 7) & ~7;
  1598. /*
  1599. * If the page is not uptodate bring it uptodate by copying from the
  1600. * attribute value.
  1601. */
  1602. attr_size = le32_to_cpu(a->data.resident.value_length);
  1603. BUG_ON(attr_size != i_size_read(vi));
  1604. if (page && !PageUptodate(page)) {
  1605. kaddr = kmap_atomic(page, KM_USER0);
  1606. memcpy(kaddr, (u8*)a +
  1607. le16_to_cpu(a->data.resident.value_offset),
  1608. attr_size);
  1609. memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
  1610. kunmap_atomic(kaddr, KM_USER0);
  1611. flush_dcache_page(page);
  1612. SetPageUptodate(page);
  1613. }
  1614. /* Backup the attribute flag. */
  1615. old_res_attr_flags = a->data.resident.flags;
  1616. /* Resize the resident part of the attribute record. */
  1617. err = ntfs_attr_record_resize(m, a, arec_size);
  1618. if (unlikely(err))
  1619. goto err_out;
  1620. /*
  1621. * Convert the resident part of the attribute record to describe a
  1622. * non-resident attribute.
  1623. */
  1624. a->non_resident = 1;
  1625. /* Move the attribute name if it exists and update the offset. */
  1626. if (a->name_length)
  1627. memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
  1628. a->name_length * sizeof(ntfschar));
  1629. a->name_offset = cpu_to_le16(name_ofs);
  1630. /* Setup the fields specific to non-resident attributes. */
  1631. a->data.non_resident.lowest_vcn = 0;
  1632. a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
  1633. vol->cluster_size_bits);
  1634. a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
  1635. memset(&a->data.non_resident.reserved, 0,
  1636. sizeof(a->data.non_resident.reserved));
  1637. a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
  1638. a->data.non_resident.data_size =
  1639. a->data.non_resident.initialized_size =
  1640. cpu_to_sle64(attr_size);
  1641. if (NInoSparse(ni) || NInoCompressed(ni)) {
  1642. a->data.non_resident.compression_unit = 4;
  1643. a->data.non_resident.compressed_size =
  1644. a->data.non_resident.allocated_size;
  1645. } else
  1646. a->data.non_resident.compression_unit = 0;
  1647. /* Generate the mapping pairs array into the attribute record. */
  1648. err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
  1649. arec_size - mp_ofs, rl, 0, -1, NULL);
  1650. if (unlikely(err)) {
  1651. ntfs_debug("Failed to build mapping pairs, error code %i.",
  1652. err);
  1653. goto undo_err_out;
  1654. }
  1655. /* Setup the in-memory attribute structure to be non-resident. */
  1656. ni->runlist.rl = rl;
  1657. write_lock_irqsave(&ni->size_lock, flags);
  1658. ni->allocated_size = new_size;
  1659. if (NInoSparse(ni) || NInoCompressed(ni)) {
  1660. ni->itype.compressed.size = ni->allocated_size;
  1661. ni->itype.compressed.block_size = 1U <<
  1662. (a->data.non_resident.compression_unit +
  1663. vol->cluster_size_bits);
  1664. ni->itype.compressed.block_size_bits =
  1665. ffs(ni->itype.compressed.block_size) - 1;
  1666. ni->itype.compressed.block_clusters = 1U <<
  1667. a->data.non_resident.compression_unit;
  1668. }
  1669. write_unlock_irqrestore(&ni->size_lock, flags);
  1670. /*
  1671. * This needs to be last since the address space operations ->readpage
  1672. * and ->writepage can run concurrently with us as they are not
  1673. * serialized on i_sem. Note, we are not allowed to fail once we flip
  1674. * this switch, which is another reason to do this last.
  1675. */
  1676. NInoSetNonResident(ni);
  1677. /* Mark the mft record dirty, so it gets written back. */
  1678. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1679. mark_mft_record_dirty(ctx->ntfs_ino);
  1680. ntfs_attr_put_search_ctx(ctx);
  1681. unmap_mft_record(base_ni);
  1682. up_write(&ni->runlist.lock);
  1683. if (page) {
  1684. set_page_dirty(page);
  1685. unlock_page(page);
  1686. mark_page_accessed(page);
  1687. page_cache_release(page);
  1688. }
  1689. ntfs_debug("Done.");
  1690. return 0;
  1691. undo_err_out:
  1692. /* Convert the attribute back into a resident attribute. */
  1693. a->non_resident = 0;
  1694. /* Move the attribute name if it exists and update the offset. */
  1695. name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
  1696. sizeof(a->data.resident.reserved) + 7) & ~7;
  1697. if (a->name_length)
  1698. memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
  1699. a->name_length * sizeof(ntfschar));
  1700. mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
  1701. a->name_offset = cpu_to_le16(name_ofs);
  1702. arec_size = (mp_ofs + attr_size + 7) & ~7;
  1703. /* Resize the resident part of the attribute record. */
  1704. err2 = ntfs_attr_record_resize(m, a, arec_size);
  1705. if (unlikely(err2)) {
  1706. /*
  1707. * This cannot happen (well if memory corruption is at work it
  1708. * could happen in theory), but deal with it as well as we can.
  1709. * If the old size is too small, truncate the attribute,
  1710. * otherwise simply give it a larger allocated size.
  1711. * FIXME: Should check whether chkdsk complains when the
  1712. * allocated size is much bigger than the resident value size.
  1713. */
  1714. arec_size = le32_to_cpu(a->length);
  1715. if ((mp_ofs + attr_size) > arec_size) {
  1716. err2 = attr_size;
  1717. attr_size = arec_size - mp_ofs;
  1718. ntfs_error(vol->sb, "Failed to undo partial resident "
  1719. "to non-resident attribute "
  1720. "conversion. Truncating inode 0x%lx, "
  1721. "attribute type 0x%x from %i bytes to "
  1722. "%i bytes to maintain metadata "
  1723. "consistency. THIS MEANS YOU ARE "
  1724. "LOSING %i BYTES DATA FROM THIS %s.",
  1725. vi->i_ino,
  1726. (unsigned)le32_to_cpu(ni->type),
  1727. err2, attr_size, err2 - attr_size,
  1728. ((ni->type == AT_DATA) &&
  1729. !ni->name_len) ? "FILE": "ATTRIBUTE");
  1730. write_lock_irqsave(&ni->size_lock, flags);
  1731. ni->initialized_size = attr_size;
  1732. i_size_write(vi, attr_size);
  1733. write_unlock_irqrestore(&ni->size_lock, flags);
  1734. }
  1735. }
  1736. /* Setup the fields specific to resident attributes. */
  1737. a->data.resident.value_length = cpu_to_le32(attr_size);
  1738. a->data.resident.value_offset = cpu_to_le16(mp_ofs);
  1739. a->data.resident.flags = old_res_attr_flags;
  1740. memset(&a->data.resident.reserved, 0,
  1741. sizeof(a->data.resident.reserved));
  1742. /* Copy the data from the page back to the attribute value. */
  1743. if (page) {
  1744. kaddr = kmap_atomic(page, KM_USER0);
  1745. memcpy((u8*)a + mp_ofs, kaddr, attr_size);
  1746. kunmap_atomic(kaddr, KM_USER0);
  1747. }
  1748. /* Setup the allocated size in the ntfs inode in case it changed. */
  1749. write_lock_irqsave(&ni->size_lock, flags);
  1750. ni->allocated_size = arec_size - mp_ofs;
  1751. write_unlock_irqrestore(&ni->size_lock, flags);
  1752. /* Mark the mft record dirty, so it gets written back. */
  1753. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1754. mark_mft_record_dirty(ctx->ntfs_ino);
  1755. err_out:
  1756. if (ctx)
  1757. ntfs_attr_put_search_ctx(ctx);
  1758. if (m)
  1759. unmap_mft_record(base_ni);
  1760. ni->runlist.rl = NULL;
  1761. up_write(&ni->runlist.lock);
  1762. rl_err_out:
  1763. if (rl) {
  1764. if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
  1765. ntfs_error(vol->sb, "Failed to release allocated "
  1766. "cluster(s) in error code path. Run "
  1767. "chkdsk to recover the lost "
  1768. "cluster(s).");
  1769. NVolSetErrors(vol);
  1770. }
  1771. ntfs_free(rl);
  1772. page_err_out:
  1773. unlock_page(page);
  1774. page_cache_release(page);
  1775. }
  1776. if (err == -EINVAL)
  1777. err = -EIO;
  1778. return err;
  1779. }
  1780. /**
  1781. * ntfs_attr_set - fill (a part of) an attribute with a byte
  1782. * @ni: ntfs inode describing the attribute to fill
  1783. * @ofs: offset inside the attribute at which to start to fill
  1784. * @cnt: number of bytes to fill
  1785. * @val: the unsigned 8-bit value with which to fill the attribute
  1786. *
  1787. * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
  1788. * byte offset @ofs inside the attribute with the constant byte @val.
  1789. *
  1790. * This function is effectively like memset() applied to an ntfs attribute.
  1791. * Note thie function actually only operates on the page cache pages belonging
  1792. * to the ntfs attribute and it marks them dirty after doing the memset().
  1793. * Thus it relies on the vm dirty page write code paths to cause the modified
  1794. * pages to be written to the mft record/disk.
  1795. *
  1796. * Return 0 on success and -errno on error. An error code of -ESPIPE means
  1797. * that @ofs + @cnt were outside the end of the attribute and no write was
  1798. * performed.
  1799. */
  1800. int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
  1801. {
  1802. ntfs_volume *vol = ni->vol;
  1803. struct address_space *mapping;
  1804. struct page *page;
  1805. u8 *kaddr;
  1806. pgoff_t idx, end;
  1807. unsigned int start_ofs, end_ofs, size;
  1808. ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
  1809. (long long)ofs, (long long)cnt, val);
  1810. BUG_ON(ofs < 0);
  1811. BUG_ON(cnt < 0);
  1812. if (!cnt)
  1813. goto done;
  1814. /*
  1815. * FIXME: Compressed and encrypted attributes are not supported when
  1816. * writing and we should never have gotten here for them.
  1817. */
  1818. BUG_ON(NInoCompressed(ni));
  1819. BUG_ON(NInoEncrypted(ni));
  1820. mapping = VFS_I(ni)->i_mapping;
  1821. /* Work out the starting index and page offset. */
  1822. idx = ofs >> PAGE_CACHE_SHIFT;
  1823. start_ofs = ofs & ~PAGE_CACHE_MASK;
  1824. /* Work out the ending index and page offset. */
  1825. end = ofs + cnt;
  1826. end_ofs = end & ~PAGE_CACHE_MASK;
  1827. /* If the end is outside the inode size return -ESPIPE. */
  1828. if (unlikely(end > i_size_read(VFS_I(ni)))) {
  1829. ntfs_error(vol->sb, "Request exceeds end of attribute.");
  1830. return -ESPIPE;
  1831. }
  1832. end >>= PAGE_CACHE_SHIFT;
  1833. /* If there is a first partial page, need to do it the slow way. */
  1834. if (start_ofs) {
  1835. page = read_cache_page(mapping, idx,
  1836. (filler_t*)mapping->a_ops->readpage, NULL);
  1837. if (IS_ERR(page)) {
  1838. ntfs_error(vol->sb, "Failed to read first partial "
  1839. "page (sync error, index 0x%lx).", idx);
  1840. return PTR_ERR(page);
  1841. }
  1842. wait_on_page_locked(page);
  1843. if (unlikely(!PageUptodate(page))) {
  1844. ntfs_error(vol->sb, "Failed to read first partial page "
  1845. "(async error, index 0x%lx).", idx);
  1846. page_cache_release(page);
  1847. return PTR_ERR(page);
  1848. }
  1849. /*
  1850. * If the last page is the same as the first page, need to
  1851. * limit the write to the end offset.
  1852. */
  1853. size = PAGE_CACHE_SIZE;
  1854. if (idx == end)
  1855. size = end_ofs;
  1856. kaddr = kmap_atomic(page, KM_USER0);
  1857. memset(kaddr + start_ofs, val, size - start_ofs);
  1858. flush_dcache_page(page);
  1859. kunmap_atomic(kaddr, KM_USER0);
  1860. set_page_dirty(page);
  1861. page_cache_release(page);
  1862. if (idx == end)
  1863. goto done;
  1864. idx++;
  1865. }
  1866. /* Do the whole pages the fast way. */
  1867. for (; idx < end; idx++) {
  1868. /* Find or create the current page. (The page is locked.) */
  1869. page = grab_cache_page(mapping, idx);
  1870. if (unlikely(!page)) {
  1871. ntfs_error(vol->sb, "Insufficient memory to grab "
  1872. "page (index 0x%lx).", idx);
  1873. return -ENOMEM;
  1874. }
  1875. kaddr = kmap_atomic(page, KM_USER0);
  1876. memset(kaddr, val, PAGE_CACHE_SIZE);
  1877. flush_dcache_page(page);
  1878. kunmap_atomic(kaddr, KM_USER0);
  1879. /*
  1880. * If the page has buffers, mark them uptodate since buffer
  1881. * state and not page state is definitive in 2.6 kernels.
  1882. */
  1883. if (page_has_buffers(page)) {
  1884. struct buffer_head *bh, *head;
  1885. bh = head = page_buffers(page);
  1886. do {
  1887. set_buffer_uptodate(bh);
  1888. } while ((bh = bh->b_this_page) != head);
  1889. }
  1890. /* Now that buffers are uptodate, set the page uptodate, too. */
  1891. SetPageUptodate(page);
  1892. /*
  1893. * Set the page and all its buffers dirty and mark the inode
  1894. * dirty, too. The VM will write the page later on.
  1895. */
  1896. set_page_dirty(page);
  1897. /* Finally unlock and release the page. */
  1898. unlock_page(page);
  1899. page_cache_release(page);
  1900. }
  1901. /* If there is a last partial page, need to do it the slow way. */
  1902. if (end_ofs) {
  1903. page = read_cache_page(mapping, idx,
  1904. (filler_t*)mapping->a_ops->readpage, NULL);
  1905. if (IS_ERR(page)) {
  1906. ntfs_error(vol->sb, "Failed to read last partial page "
  1907. "(sync error, index 0x%lx).", idx);
  1908. return PTR_ERR(page);
  1909. }
  1910. wait_on_page_locked(page);
  1911. if (unlikely(!PageUptodate(page))) {
  1912. ntfs_error(vol->sb, "Failed to read last partial page "
  1913. "(async error, index 0x%lx).", idx);
  1914. page_cache_release(page);
  1915. return PTR_ERR(page);
  1916. }
  1917. kaddr = kmap_atomic(page, KM_USER0);
  1918. memset(kaddr, val, end_ofs);
  1919. flush_dcache_page(page);
  1920. kunmap_atomic(kaddr, KM_USER0);
  1921. set_page_dirty(page);
  1922. page_cache_release(page);
  1923. }
  1924. done:
  1925. ntfs_debug("Done.");
  1926. return 0;
  1927. }
  1928. #endif /* NTFS_RW */