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->mrec = mrec;
  962. /* Sanity checks are performed elsewhere. */
  963. ctx->attr = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset));
  964. ctx->is_first = TRUE;
  965. ctx->ntfs_ino = ni;
  966. ctx->al_entry = NULL;
  967. ctx->base_ntfs_ino = NULL;
  968. ctx->base_mrec = NULL;
  969. ctx->base_attr = NULL;
  970. }
  971. /**
  972. * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
  973. * @ctx: attribute search context to reinitialize
  974. *
  975. * Reinitialize the attribute search context @ctx, unmapping an associated
  976. * extent mft record if present, and initialize the search context again.
  977. *
  978. * This is used when a search for a new attribute is being started to reset
  979. * the search context to the beginning.
  980. */
  981. void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
  982. {
  983. if (likely(!ctx->base_ntfs_ino)) {
  984. /* No attribute list. */
  985. ctx->is_first = TRUE;
  986. /* Sanity checks are performed elsewhere. */
  987. ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
  988. le16_to_cpu(ctx->mrec->attrs_offset));
  989. /*
  990. * This needs resetting due to ntfs_external_attr_find() which
  991. * can leave it set despite having zeroed ctx->base_ntfs_ino.
  992. */
  993. ctx->al_entry = NULL;
  994. return;
  995. } /* Attribute list. */
  996. if (ctx->ntfs_ino != ctx->base_ntfs_ino)
  997. unmap_extent_mft_record(ctx->ntfs_ino);
  998. ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
  999. return;
  1000. }
  1001. /**
  1002. * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
  1003. * @ni: ntfs inode with which to initialize the search context
  1004. * @mrec: mft record with which to initialize the search context
  1005. *
  1006. * Allocate a new attribute search context, initialize it with @ni and @mrec,
  1007. * and return it. Return NULL if allocation failed.
  1008. */
  1009. ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
  1010. {
  1011. ntfs_attr_search_ctx *ctx;
  1012. ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, SLAB_NOFS);
  1013. if (ctx)
  1014. ntfs_attr_init_search_ctx(ctx, ni, mrec);
  1015. return ctx;
  1016. }
  1017. /**
  1018. * ntfs_attr_put_search_ctx - release an attribute search context
  1019. * @ctx: attribute search context to free
  1020. *
  1021. * Release the attribute search context @ctx, unmapping an associated extent
  1022. * mft record if present.
  1023. */
  1024. void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
  1025. {
  1026. if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
  1027. unmap_extent_mft_record(ctx->ntfs_ino);
  1028. kmem_cache_free(ntfs_attr_ctx_cache, ctx);
  1029. return;
  1030. }
  1031. #ifdef NTFS_RW
  1032. /**
  1033. * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
  1034. * @vol: ntfs volume to which the attribute belongs
  1035. * @type: attribute type which to find
  1036. *
  1037. * Search for the attribute definition record corresponding to the attribute
  1038. * @type in the $AttrDef system file.
  1039. *
  1040. * Return the attribute type definition record if found and NULL if not found.
  1041. */
  1042. static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
  1043. const ATTR_TYPE type)
  1044. {
  1045. ATTR_DEF *ad;
  1046. BUG_ON(!vol->attrdef);
  1047. BUG_ON(!type);
  1048. for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
  1049. vol->attrdef_size && ad->type; ++ad) {
  1050. /* We have not found it yet, carry on searching. */
  1051. if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
  1052. continue;
  1053. /* We found the attribute; return it. */
  1054. if (likely(ad->type == type))
  1055. return ad;
  1056. /* We have gone too far already. No point in continuing. */
  1057. break;
  1058. }
  1059. /* Attribute not found. */
  1060. ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
  1061. le32_to_cpu(type));
  1062. return NULL;
  1063. }
  1064. /**
  1065. * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
  1066. * @vol: ntfs volume to which the attribute belongs
  1067. * @type: attribute type which to check
  1068. * @size: size which to check
  1069. *
  1070. * Check whether the @size in bytes is valid for an attribute of @type on the
  1071. * ntfs volume @vol. This information is obtained from $AttrDef system file.
  1072. *
  1073. * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
  1074. * listed in $AttrDef.
  1075. */
  1076. int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
  1077. const s64 size)
  1078. {
  1079. ATTR_DEF *ad;
  1080. BUG_ON(size < 0);
  1081. /*
  1082. * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
  1083. * listed in $AttrDef.
  1084. */
  1085. if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
  1086. return -ERANGE;
  1087. /* Get the $AttrDef entry for the attribute @type. */
  1088. ad = ntfs_attr_find_in_attrdef(vol, type);
  1089. if (unlikely(!ad))
  1090. return -ENOENT;
  1091. /* Do the bounds check. */
  1092. if (((sle64_to_cpu(ad->min_size) > 0) &&
  1093. size < sle64_to_cpu(ad->min_size)) ||
  1094. ((sle64_to_cpu(ad->max_size) > 0) && size >
  1095. sle64_to_cpu(ad->max_size)))
  1096. return -ERANGE;
  1097. return 0;
  1098. }
  1099. /**
  1100. * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
  1101. * @vol: ntfs volume to which the attribute belongs
  1102. * @type: attribute type which to check
  1103. *
  1104. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1105. * be non-resident. This information is obtained from $AttrDef system file.
  1106. *
  1107. * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, or
  1108. * -ENOENT if the attribute is not listed in $AttrDef.
  1109. */
  1110. int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1111. {
  1112. ATTR_DEF *ad;
  1113. /*
  1114. * $DATA and $EA are always allowed to be non-resident even if $AttrDef
  1115. * does not specify this in the flags of the $DATA attribute definition
  1116. * record.
  1117. */
  1118. if (type == AT_DATA || type == AT_EA)
  1119. return 0;
  1120. /* Find the attribute definition record in $AttrDef. */
  1121. ad = ntfs_attr_find_in_attrdef(vol, type);
  1122. if (unlikely(!ad))
  1123. return -ENOENT;
  1124. /* Check the flags and return the result. */
  1125. if (ad->flags & CAN_BE_NON_RESIDENT)
  1126. return 0;
  1127. return -EPERM;
  1128. }
  1129. /**
  1130. * ntfs_attr_can_be_resident - check if an attribute can be resident
  1131. * @vol: ntfs volume to which the attribute belongs
  1132. * @type: attribute type which to check
  1133. *
  1134. * Check whether the attribute of @type on the ntfs volume @vol is allowed to
  1135. * be resident. This information is derived from our ntfs knowledge and may
  1136. * not be completely accurate, especially when user defined attributes are
  1137. * present. Basically we allow everything to be resident except for index
  1138. * allocation and $EA attributes.
  1139. *
  1140. * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
  1141. *
  1142. * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
  1143. * otherwise windows will not boot (blue screen of death)! We cannot
  1144. * check for this here as we do not know which inode's $Bitmap is
  1145. * being asked about so the caller needs to special case this.
  1146. */
  1147. int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
  1148. {
  1149. if (type != AT_INDEX_ALLOCATION && type != AT_EA)
  1150. return 0;
  1151. return -EPERM;
  1152. }
  1153. /**
  1154. * ntfs_attr_record_resize - resize an attribute record
  1155. * @m: mft record containing attribute record
  1156. * @a: attribute record to resize
  1157. * @new_size: new size in bytes to which to resize the attribute record @a
  1158. *
  1159. * Resize the attribute record @a, i.e. the resident part of the attribute, in
  1160. * the mft record @m to @new_size bytes.
  1161. *
  1162. * Return 0 on success and -errno on error. The following error codes are
  1163. * defined:
  1164. * -ENOSPC - Not enough space in the mft record @m to perform the resize.
  1165. *
  1166. * Note: On error, no modifications have been performed whatsoever.
  1167. *
  1168. * Warning: If you make a record smaller without having copied all the data you
  1169. * are interested in the data may be overwritten.
  1170. */
  1171. int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
  1172. {
  1173. ntfs_debug("Entering for new_size %u.", new_size);
  1174. /* Align to 8 bytes if it is not already done. */
  1175. if (new_size & 7)
  1176. new_size = (new_size + 7) & ~7;
  1177. /* If the actual attribute length has changed, move things around. */
  1178. if (new_size != le32_to_cpu(a->length)) {
  1179. u32 new_muse = le32_to_cpu(m->bytes_in_use) -
  1180. le32_to_cpu(a->length) + new_size;
  1181. /* Not enough space in this mft record. */
  1182. if (new_muse > le32_to_cpu(m->bytes_allocated))
  1183. return -ENOSPC;
  1184. /* Move attributes following @a to their new location. */
  1185. memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
  1186. le32_to_cpu(m->bytes_in_use) - ((u8*)a -
  1187. (u8*)m) - le32_to_cpu(a->length));
  1188. /* Adjust @m to reflect the change in used space. */
  1189. m->bytes_in_use = cpu_to_le32(new_muse);
  1190. /* Adjust @a to reflect the new size. */
  1191. if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
  1192. a->length = cpu_to_le32(new_size);
  1193. }
  1194. return 0;
  1195. }
  1196. /**
  1197. * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
  1198. * @ni: ntfs inode describing the attribute to convert
  1199. *
  1200. * Convert the resident ntfs attribute described by the ntfs inode @ni to a
  1201. * non-resident one.
  1202. *
  1203. * Return 0 on success and -errno on error. The following error return codes
  1204. * are defined:
  1205. * -EPERM - The attribute is not allowed to be non-resident.
  1206. * -ENOMEM - Not enough memory.
  1207. * -ENOSPC - Not enough disk space.
  1208. * -EINVAL - Attribute not defined on the volume.
  1209. * -EIO - I/o error or other error.
  1210. * Note that -ENOSPC is also returned in the case that there is not enough
  1211. * space in the mft record to do the conversion. This can happen when the mft
  1212. * record is already very full. The caller is responsible for trying to make
  1213. * space in the mft record and trying again. FIXME: Do we need a separate
  1214. * error return code for this kind of -ENOSPC or is it always worth trying
  1215. * again in case the attribute may then fit in a resident state so no need to
  1216. * make it non-resident at all? Ho-hum... (AIA)
  1217. *
  1218. * NOTE to self: No changes in the attribute list are required to move from
  1219. * a resident to a non-resident attribute.
  1220. *
  1221. * Locking: - The caller must hold i_sem on the inode.
  1222. */
  1223. int ntfs_attr_make_non_resident(ntfs_inode *ni)
  1224. {
  1225. s64 new_size;
  1226. struct inode *vi = VFS_I(ni);
  1227. ntfs_volume *vol = ni->vol;
  1228. ntfs_inode *base_ni;
  1229. MFT_RECORD *m;
  1230. ATTR_RECORD *a;
  1231. ntfs_attr_search_ctx *ctx;
  1232. struct page *page;
  1233. runlist_element *rl;
  1234. u8 *kaddr;
  1235. unsigned long flags;
  1236. int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
  1237. u32 attr_size;
  1238. u8 old_res_attr_flags;
  1239. /* Check that the attribute is allowed to be non-resident. */
  1240. err = ntfs_attr_can_be_non_resident(vol, ni->type);
  1241. if (unlikely(err)) {
  1242. if (err == -EPERM)
  1243. ntfs_debug("Attribute is not allowed to be "
  1244. "non-resident.");
  1245. else
  1246. ntfs_debug("Attribute not defined on the NTFS "
  1247. "volume!");
  1248. return err;
  1249. }
  1250. /*
  1251. * The size needs to be aligned to a cluster boundary for allocation
  1252. * purposes.
  1253. */
  1254. new_size = (i_size_read(vi) + vol->cluster_size - 1) &
  1255. ~(vol->cluster_size - 1);
  1256. if (new_size > 0) {
  1257. /*
  1258. * Will need the page later and since the page lock nests
  1259. * outside all ntfs locks, we need to get the page now.
  1260. */
  1261. page = find_or_create_page(vi->i_mapping, 0,
  1262. mapping_gfp_mask(vi->i_mapping));
  1263. if (unlikely(!page))
  1264. return -ENOMEM;
  1265. /* Start by allocating clusters to hold the attribute value. */
  1266. rl = ntfs_cluster_alloc(vol, 0, new_size >>
  1267. vol->cluster_size_bits, -1, DATA_ZONE);
  1268. if (IS_ERR(rl)) {
  1269. err = PTR_ERR(rl);
  1270. ntfs_debug("Failed to allocate cluster%s, error code "
  1271. "%i.\n", (new_size >>
  1272. vol->cluster_size_bits) > 1 ? "s" : "",
  1273. err);
  1274. goto page_err_out;
  1275. }
  1276. } else {
  1277. rl = NULL;
  1278. page = NULL;
  1279. }
  1280. /* Determine the size of the mapping pairs array. */
  1281. mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0);
  1282. if (unlikely(mp_size < 0)) {
  1283. err = mp_size;
  1284. ntfs_debug("Failed to get size for mapping pairs array, error "
  1285. "code %i.", err);
  1286. goto rl_err_out;
  1287. }
  1288. down_write(&ni->runlist.lock);
  1289. if (!NInoAttr(ni))
  1290. base_ni = ni;
  1291. else
  1292. base_ni = ni->ext.base_ntfs_ino;
  1293. m = map_mft_record(base_ni);
  1294. if (IS_ERR(m)) {
  1295. err = PTR_ERR(m);
  1296. m = NULL;
  1297. ctx = NULL;
  1298. goto err_out;
  1299. }
  1300. ctx = ntfs_attr_get_search_ctx(base_ni, m);
  1301. if (unlikely(!ctx)) {
  1302. err = -ENOMEM;
  1303. goto err_out;
  1304. }
  1305. err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
  1306. CASE_SENSITIVE, 0, NULL, 0, ctx);
  1307. if (unlikely(err)) {
  1308. if (err == -ENOENT)
  1309. err = -EIO;
  1310. goto err_out;
  1311. }
  1312. m = ctx->mrec;
  1313. a = ctx->attr;
  1314. BUG_ON(NInoNonResident(ni));
  1315. BUG_ON(a->non_resident);
  1316. /*
  1317. * Calculate new offsets for the name and the mapping pairs array.
  1318. * We assume the attribute is not compressed or sparse.
  1319. */
  1320. name_ofs = (offsetof(ATTR_REC,
  1321. data.non_resident.compressed_size) + 7) & ~7;
  1322. mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
  1323. /*
  1324. * Determine the size of the resident part of the now non-resident
  1325. * attribute record.
  1326. */
  1327. arec_size = (mp_ofs + mp_size + 7) & ~7;
  1328. /*
  1329. * If the page is not uptodate bring it uptodate by copying from the
  1330. * attribute value.
  1331. */
  1332. attr_size = le32_to_cpu(a->data.resident.value_length);
  1333. BUG_ON(attr_size != i_size_read(vi));
  1334. if (page && !PageUptodate(page)) {
  1335. kaddr = kmap_atomic(page, KM_USER0);
  1336. memcpy(kaddr, (u8*)a +
  1337. le16_to_cpu(a->data.resident.value_offset),
  1338. attr_size);
  1339. memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
  1340. kunmap_atomic(kaddr, KM_USER0);
  1341. flush_dcache_page(page);
  1342. SetPageUptodate(page);
  1343. }
  1344. /* Backup the attribute flag. */
  1345. old_res_attr_flags = a->data.resident.flags;
  1346. /* Resize the resident part of the attribute record. */
  1347. err = ntfs_attr_record_resize(m, a, arec_size);
  1348. if (unlikely(err))
  1349. goto err_out;
  1350. /*
  1351. * Convert the resident part of the attribute record to describe a
  1352. * non-resident attribute.
  1353. */
  1354. a->non_resident = 1;
  1355. /* Move the attribute name if it exists and update the offset. */
  1356. if (a->name_length)
  1357. memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
  1358. a->name_length * sizeof(ntfschar));
  1359. a->name_offset = cpu_to_le16(name_ofs);
  1360. /*
  1361. * FIXME: For now just clear all of these as we do not support them
  1362. * when writing.
  1363. */
  1364. a->flags &= cpu_to_le16(0xffff & ~le16_to_cpu(ATTR_IS_SPARSE |
  1365. ATTR_IS_ENCRYPTED | ATTR_COMPRESSION_MASK));
  1366. /* Setup the fields specific to non-resident attributes. */
  1367. a->data.non_resident.lowest_vcn = 0;
  1368. a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
  1369. vol->cluster_size_bits);
  1370. a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
  1371. a->data.non_resident.compression_unit = 0;
  1372. memset(&a->data.non_resident.reserved, 0,
  1373. sizeof(a->data.non_resident.reserved));
  1374. a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
  1375. a->data.non_resident.data_size =
  1376. a->data.non_resident.initialized_size =
  1377. cpu_to_sle64(attr_size);
  1378. /* Generate the mapping pairs array into the attribute record. */
  1379. err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
  1380. arec_size - mp_ofs, rl, 0, NULL);
  1381. if (unlikely(err)) {
  1382. ntfs_debug("Failed to build mapping pairs, error code %i.",
  1383. err);
  1384. goto undo_err_out;
  1385. }
  1386. /* Setup the in-memory attribute structure to be non-resident. */
  1387. /*
  1388. * FIXME: For now just clear all of these as we do not support them
  1389. * when writing.
  1390. */
  1391. NInoClearSparse(ni);
  1392. NInoClearEncrypted(ni);
  1393. NInoClearCompressed(ni);
  1394. ni->runlist.rl = rl;
  1395. write_lock_irqsave(&ni->size_lock, flags);
  1396. ni->allocated_size = new_size;
  1397. write_unlock_irqrestore(&ni->size_lock, flags);
  1398. /*
  1399. * This needs to be last since the address space operations ->readpage
  1400. * and ->writepage can run concurrently with us as they are not
  1401. * serialized on i_sem. Note, we are not allowed to fail once we flip
  1402. * this switch, which is another reason to do this last.
  1403. */
  1404. NInoSetNonResident(ni);
  1405. /* Mark the mft record dirty, so it gets written back. */
  1406. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1407. mark_mft_record_dirty(ctx->ntfs_ino);
  1408. ntfs_attr_put_search_ctx(ctx);
  1409. unmap_mft_record(base_ni);
  1410. up_write(&ni->runlist.lock);
  1411. if (page) {
  1412. set_page_dirty(page);
  1413. unlock_page(page);
  1414. mark_page_accessed(page);
  1415. page_cache_release(page);
  1416. }
  1417. ntfs_debug("Done.");
  1418. return 0;
  1419. undo_err_out:
  1420. /* Convert the attribute back into a resident attribute. */
  1421. a->non_resident = 0;
  1422. /* Move the attribute name if it exists and update the offset. */
  1423. name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
  1424. sizeof(a->data.resident.reserved) + 7) & ~7;
  1425. if (a->name_length)
  1426. memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
  1427. a->name_length * sizeof(ntfschar));
  1428. mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
  1429. a->name_offset = cpu_to_le16(name_ofs);
  1430. arec_size = (mp_ofs + attr_size + 7) & ~7;
  1431. /* Resize the resident part of the attribute record. */
  1432. err2 = ntfs_attr_record_resize(m, a, arec_size);
  1433. if (unlikely(err2)) {
  1434. /*
  1435. * This cannot happen (well if memory corruption is at work it
  1436. * could happen in theory), but deal with it as well as we can.
  1437. * If the old size is too small, truncate the attribute,
  1438. * otherwise simply give it a larger allocated size.
  1439. * FIXME: Should check whether chkdsk complains when the
  1440. * allocated size is much bigger than the resident value size.
  1441. */
  1442. arec_size = le32_to_cpu(a->length);
  1443. if ((mp_ofs + attr_size) > arec_size) {
  1444. err2 = attr_size;
  1445. attr_size = arec_size - mp_ofs;
  1446. ntfs_error(vol->sb, "Failed to undo partial resident "
  1447. "to non-resident attribute "
  1448. "conversion. Truncating inode 0x%lx, "
  1449. "attribute type 0x%x from %i bytes to "
  1450. "%i bytes to maintain metadata "
  1451. "consistency. THIS MEANS YOU ARE "
  1452. "LOSING %i BYTES DATA FROM THIS %s.",
  1453. vi->i_ino,
  1454. (unsigned)le32_to_cpu(ni->type),
  1455. err2, attr_size, err2 - attr_size,
  1456. ((ni->type == AT_DATA) &&
  1457. !ni->name_len) ? "FILE": "ATTRIBUTE");
  1458. write_lock_irqsave(&ni->size_lock, flags);
  1459. ni->initialized_size = attr_size;
  1460. i_size_write(vi, attr_size);
  1461. write_unlock_irqrestore(&ni->size_lock, flags);
  1462. }
  1463. }
  1464. /* Setup the fields specific to resident attributes. */
  1465. a->data.resident.value_length = cpu_to_le32(attr_size);
  1466. a->data.resident.value_offset = cpu_to_le16(mp_ofs);
  1467. a->data.resident.flags = old_res_attr_flags;
  1468. memset(&a->data.resident.reserved, 0,
  1469. sizeof(a->data.resident.reserved));
  1470. /* Copy the data from the page back to the attribute value. */
  1471. if (page) {
  1472. kaddr = kmap_atomic(page, KM_USER0);
  1473. memcpy((u8*)a + mp_ofs, kaddr, attr_size);
  1474. kunmap_atomic(kaddr, KM_USER0);
  1475. }
  1476. /* Setup the allocated size in the ntfs inode in case it changed. */
  1477. write_lock_irqsave(&ni->size_lock, flags);
  1478. ni->allocated_size = arec_size - mp_ofs;
  1479. write_unlock_irqrestore(&ni->size_lock, flags);
  1480. /* Mark the mft record dirty, so it gets written back. */
  1481. flush_dcache_mft_record_page(ctx->ntfs_ino);
  1482. mark_mft_record_dirty(ctx->ntfs_ino);
  1483. err_out:
  1484. if (ctx)
  1485. ntfs_attr_put_search_ctx(ctx);
  1486. if (m)
  1487. unmap_mft_record(base_ni);
  1488. ni->runlist.rl = NULL;
  1489. up_write(&ni->runlist.lock);
  1490. rl_err_out:
  1491. if (rl) {
  1492. if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
  1493. ntfs_error(vol->sb, "Failed to release allocated "
  1494. "cluster(s) in error code path. Run "
  1495. "chkdsk to recover the lost "
  1496. "cluster(s).");
  1497. NVolSetErrors(vol);
  1498. }
  1499. ntfs_free(rl);
  1500. page_err_out:
  1501. unlock_page(page);
  1502. page_cache_release(page);
  1503. }
  1504. if (err == -EINVAL)
  1505. err = -EIO;
  1506. return err;
  1507. }
  1508. /**
  1509. * ntfs_attr_set - fill (a part of) an attribute with a byte
  1510. * @ni: ntfs inode describing the attribute to fill
  1511. * @ofs: offset inside the attribute at which to start to fill
  1512. * @cnt: number of bytes to fill
  1513. * @val: the unsigned 8-bit value with which to fill the attribute
  1514. *
  1515. * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
  1516. * byte offset @ofs inside the attribute with the constant byte @val.
  1517. *
  1518. * This function is effectively like memset() applied to an ntfs attribute.
  1519. * Note thie function actually only operates on the page cache pages belonging
  1520. * to the ntfs attribute and it marks them dirty after doing the memset().
  1521. * Thus it relies on the vm dirty page write code paths to cause the modified
  1522. * pages to be written to the mft record/disk.
  1523. *
  1524. * Return 0 on success and -errno on error. An error code of -ESPIPE means
  1525. * that @ofs + @cnt were outside the end of the attribute and no write was
  1526. * performed.
  1527. */
  1528. int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
  1529. {
  1530. ntfs_volume *vol = ni->vol;
  1531. struct address_space *mapping;
  1532. struct page *page;
  1533. u8 *kaddr;
  1534. pgoff_t idx, end;
  1535. unsigned int start_ofs, end_ofs, size;
  1536. ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
  1537. (long long)ofs, (long long)cnt, val);
  1538. BUG_ON(ofs < 0);
  1539. BUG_ON(cnt < 0);
  1540. if (!cnt)
  1541. goto done;
  1542. mapping = VFS_I(ni)->i_mapping;
  1543. /* Work out the starting index and page offset. */
  1544. idx = ofs >> PAGE_CACHE_SHIFT;
  1545. start_ofs = ofs & ~PAGE_CACHE_MASK;
  1546. /* Work out the ending index and page offset. */
  1547. end = ofs + cnt;
  1548. end_ofs = end & ~PAGE_CACHE_MASK;
  1549. /* If the end is outside the inode size return -ESPIPE. */
  1550. if (unlikely(end > i_size_read(VFS_I(ni)))) {
  1551. ntfs_error(vol->sb, "Request exceeds end of attribute.");
  1552. return -ESPIPE;
  1553. }
  1554. end >>= PAGE_CACHE_SHIFT;
  1555. /* If there is a first partial page, need to do it the slow way. */
  1556. if (start_ofs) {
  1557. page = read_cache_page(mapping, idx,
  1558. (filler_t*)mapping->a_ops->readpage, NULL);
  1559. if (IS_ERR(page)) {
  1560. ntfs_error(vol->sb, "Failed to read first partial "
  1561. "page (sync error, index 0x%lx).", idx);
  1562. return PTR_ERR(page);
  1563. }
  1564. wait_on_page_locked(page);
  1565. if (unlikely(!PageUptodate(page))) {
  1566. ntfs_error(vol->sb, "Failed to read first partial page "
  1567. "(async error, index 0x%lx).", idx);
  1568. page_cache_release(page);
  1569. return PTR_ERR(page);
  1570. }
  1571. /*
  1572. * If the last page is the same as the first page, need to
  1573. * limit the write to the end offset.
  1574. */
  1575. size = PAGE_CACHE_SIZE;
  1576. if (idx == end)
  1577. size = end_ofs;
  1578. kaddr = kmap_atomic(page, KM_USER0);
  1579. memset(kaddr + start_ofs, val, size - start_ofs);
  1580. flush_dcache_page(page);
  1581. kunmap_atomic(kaddr, KM_USER0);
  1582. set_page_dirty(page);
  1583. page_cache_release(page);
  1584. if (idx == end)
  1585. goto done;
  1586. idx++;
  1587. }
  1588. /* Do the whole pages the fast way. */
  1589. for (; idx < end; idx++) {
  1590. /* Find or create the current page. (The page is locked.) */
  1591. page = grab_cache_page(mapping, idx);
  1592. if (unlikely(!page)) {
  1593. ntfs_error(vol->sb, "Insufficient memory to grab "
  1594. "page (index 0x%lx).", idx);
  1595. return -ENOMEM;
  1596. }
  1597. kaddr = kmap_atomic(page, KM_USER0);
  1598. memset(kaddr, val, PAGE_CACHE_SIZE);
  1599. flush_dcache_page(page);
  1600. kunmap_atomic(kaddr, KM_USER0);
  1601. /*
  1602. * If the page has buffers, mark them uptodate since buffer
  1603. * state and not page state is definitive in 2.6 kernels.
  1604. */
  1605. if (page_has_buffers(page)) {
  1606. struct buffer_head *bh, *head;
  1607. bh = head = page_buffers(page);
  1608. do {
  1609. set_buffer_uptodate(bh);
  1610. } while ((bh = bh->b_this_page) != head);
  1611. }
  1612. /* Now that buffers are uptodate, set the page uptodate, too. */
  1613. SetPageUptodate(page);
  1614. /*
  1615. * Set the page and all its buffers dirty and mark the inode
  1616. * dirty, too. The VM will write the page later on.
  1617. */
  1618. set_page_dirty(page);
  1619. /* Finally unlock and release the page. */
  1620. unlock_page(page);
  1621. page_cache_release(page);
  1622. }
  1623. /* If there is a last partial page, need to do it the slow way. */
  1624. if (end_ofs) {
  1625. page = read_cache_page(mapping, idx,
  1626. (filler_t*)mapping->a_ops->readpage, NULL);
  1627. if (IS_ERR(page)) {
  1628. ntfs_error(vol->sb, "Failed to read last partial page "
  1629. "(sync error, index 0x%lx).", idx);
  1630. return PTR_ERR(page);
  1631. }
  1632. wait_on_page_locked(page);
  1633. if (unlikely(!PageUptodate(page))) {
  1634. ntfs_error(vol->sb, "Failed to read last partial page "
  1635. "(async error, index 0x%lx).", idx);
  1636. page_cache_release(page);
  1637. return PTR_ERR(page);
  1638. }
  1639. kaddr = kmap_atomic(page, KM_USER0);
  1640. memset(kaddr, val, end_ofs);
  1641. flush_dcache_page(page);
  1642. kunmap_atomic(kaddr, KM_USER0);
  1643. set_page_dirty(page);
  1644. page_cache_release(page);
  1645. }
  1646. done:
  1647. ntfs_debug("Done.");
  1648. return 0;
  1649. }
  1650. #endif /* NTFS_RW */