attrib.c 60 KB

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