attrib.c 59 KB

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