attrib.c 42 KB

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