attrib.c 45 KB

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