attrib.c 55 KB

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