attrib.c 56 KB

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