index.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. */
  7. #include <linux/blkdev.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/fs.h>
  10. #include <linux/kernel.h>
  11. #include "debug.h"
  12. #include "ntfs.h"
  13. #include "ntfs_fs.h"
  14. static const struct INDEX_NAMES {
  15. const __le16 *name;
  16. u8 name_len;
  17. } s_index_names[INDEX_MUTEX_TOTAL] = {
  18. { I30_NAME, ARRAY_SIZE(I30_NAME) }, { SII_NAME, ARRAY_SIZE(SII_NAME) },
  19. { SDH_NAME, ARRAY_SIZE(SDH_NAME) }, { SO_NAME, ARRAY_SIZE(SO_NAME) },
  20. { SQ_NAME, ARRAY_SIZE(SQ_NAME) }, { SR_NAME, ARRAY_SIZE(SR_NAME) },
  21. };
  22. /*
  23. * cmp_fnames - Compare two names in index.
  24. *
  25. * if l1 != 0
  26. * Both names are little endian on-disk ATTR_FILE_NAME structs.
  27. * else
  28. * key1 - cpu_str, key2 - ATTR_FILE_NAME
  29. */
  30. static int cmp_fnames(const void *key1, size_t l1, const void *key2, size_t l2,
  31. const void *data)
  32. {
  33. const struct ATTR_FILE_NAME *f2 = key2;
  34. const struct ntfs_sb_info *sbi = data;
  35. const struct ATTR_FILE_NAME *f1;
  36. u16 fsize2;
  37. bool both_case;
  38. if (l2 <= offsetof(struct ATTR_FILE_NAME, name))
  39. return -1;
  40. fsize2 = fname_full_size(f2);
  41. if (l2 < fsize2)
  42. return -1;
  43. both_case = f2->type != FILE_NAME_DOS && !sbi->options->nocase;
  44. if (!l1) {
  45. const struct le_str *s2 = (struct le_str *)&f2->name_len;
  46. /*
  47. * If names are equal (case insensitive)
  48. * try to compare it case sensitive.
  49. */
  50. return ntfs_cmp_names_cpu(key1, s2, sbi->upcase, both_case);
  51. }
  52. f1 = key1;
  53. return ntfs_cmp_names(f1->name, f1->name_len, f2->name, f2->name_len,
  54. sbi->upcase, both_case);
  55. }
  56. /*
  57. * cmp_uint - $SII of $Secure and $Q of Quota
  58. */
  59. static int cmp_uint(const void *key1, size_t l1, const void *key2, size_t l2,
  60. const void *data)
  61. {
  62. const u32 *k1 = key1;
  63. const u32 *k2 = key2;
  64. if (l2 < sizeof(u32))
  65. return -1;
  66. if (*k1 < *k2)
  67. return -1;
  68. if (*k1 > *k2)
  69. return 1;
  70. return 0;
  71. }
  72. /*
  73. * cmp_sdh - $SDH of $Secure
  74. */
  75. static int cmp_sdh(const void *key1, size_t l1, const void *key2, size_t l2,
  76. const void *data)
  77. {
  78. const struct SECURITY_KEY *k1 = key1;
  79. const struct SECURITY_KEY *k2 = key2;
  80. u32 t1, t2;
  81. if (l2 < sizeof(struct SECURITY_KEY))
  82. return -1;
  83. t1 = le32_to_cpu(k1->hash);
  84. t2 = le32_to_cpu(k2->hash);
  85. /* First value is a hash value itself. */
  86. if (t1 < t2)
  87. return -1;
  88. if (t1 > t2)
  89. return 1;
  90. /* Second value is security Id. */
  91. if (data) {
  92. t1 = le32_to_cpu(k1->sec_id);
  93. t2 = le32_to_cpu(k2->sec_id);
  94. if (t1 < t2)
  95. return -1;
  96. if (t1 > t2)
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. /*
  102. * cmp_uints - $O of ObjId and "$R" for Reparse.
  103. */
  104. static int cmp_uints(const void *key1, size_t l1, const void *key2, size_t l2,
  105. const void *data)
  106. {
  107. const __le32 *k1 = key1;
  108. const __le32 *k2 = key2;
  109. size_t count;
  110. if ((size_t)data == 1) {
  111. /*
  112. * ni_delete_all -> ntfs_remove_reparse ->
  113. * delete all with this reference.
  114. * k1, k2 - pointers to REPARSE_KEY
  115. */
  116. k1 += 1; // Skip REPARSE_KEY.ReparseTag
  117. k2 += 1; // Skip REPARSE_KEY.ReparseTag
  118. if (l2 <= sizeof(int))
  119. return -1;
  120. l2 -= sizeof(int);
  121. if (l1 <= sizeof(int))
  122. return 1;
  123. l1 -= sizeof(int);
  124. }
  125. if (l2 < sizeof(int))
  126. return -1;
  127. for (count = min(l1, l2) >> 2; count > 0; --count, ++k1, ++k2) {
  128. u32 t1 = le32_to_cpu(*k1);
  129. u32 t2 = le32_to_cpu(*k2);
  130. if (t1 > t2)
  131. return 1;
  132. if (t1 < t2)
  133. return -1;
  134. }
  135. if (l1 > l2)
  136. return 1;
  137. if (l1 < l2)
  138. return -1;
  139. return 0;
  140. }
  141. static inline NTFS_CMP_FUNC get_cmp_func(const struct INDEX_ROOT *root)
  142. {
  143. switch (root->type) {
  144. case ATTR_NAME:
  145. if (root->rule == NTFS_COLLATION_TYPE_FILENAME)
  146. return &cmp_fnames;
  147. break;
  148. case ATTR_ZERO:
  149. switch (root->rule) {
  150. case NTFS_COLLATION_TYPE_UINT:
  151. return &cmp_uint;
  152. case NTFS_COLLATION_TYPE_SECURITY_HASH:
  153. return &cmp_sdh;
  154. case NTFS_COLLATION_TYPE_UINTS:
  155. return &cmp_uints;
  156. default:
  157. break;
  158. }
  159. break;
  160. default:
  161. break;
  162. }
  163. return NULL;
  164. }
  165. struct bmp_buf {
  166. struct ATTRIB *b;
  167. struct mft_inode *mi;
  168. struct buffer_head *bh;
  169. ulong *buf;
  170. size_t bit;
  171. u32 nbits;
  172. u64 new_valid;
  173. };
  174. static int bmp_buf_get(struct ntfs_index *indx, struct ntfs_inode *ni,
  175. size_t bit, struct bmp_buf *bbuf)
  176. {
  177. struct ATTRIB *b;
  178. size_t data_size, valid_size, vbo, off = bit >> 3;
  179. struct ntfs_sb_info *sbi = ni->mi.sbi;
  180. CLST vcn = off >> sbi->cluster_bits;
  181. struct ATTR_LIST_ENTRY *le = NULL;
  182. struct buffer_head *bh;
  183. struct super_block *sb;
  184. u32 blocksize;
  185. const struct INDEX_NAMES *in = &s_index_names[indx->type];
  186. bbuf->bh = NULL;
  187. b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
  188. &vcn, &bbuf->mi);
  189. bbuf->b = b;
  190. if (!b)
  191. return -EINVAL;
  192. if (!b->non_res) {
  193. data_size = le32_to_cpu(b->res.data_size);
  194. if (off >= data_size)
  195. return -EINVAL;
  196. bbuf->buf = (ulong *)resident_data(b);
  197. bbuf->bit = 0;
  198. bbuf->nbits = data_size * 8;
  199. return 0;
  200. }
  201. data_size = le64_to_cpu(b->nres.data_size);
  202. if (WARN_ON(off >= data_size)) {
  203. /* Looks like filesystem error. */
  204. return -EINVAL;
  205. }
  206. valid_size = le64_to_cpu(b->nres.valid_size);
  207. bh = ntfs_bread_run(sbi, &indx->bitmap_run, off);
  208. if (!bh)
  209. return -EIO;
  210. if (IS_ERR(bh))
  211. return PTR_ERR(bh);
  212. bbuf->bh = bh;
  213. if (buffer_locked(bh))
  214. __wait_on_buffer(bh);
  215. lock_buffer(bh);
  216. sb = sbi->sb;
  217. blocksize = sb->s_blocksize;
  218. vbo = off & ~(size_t)sbi->block_mask;
  219. bbuf->new_valid = vbo + blocksize;
  220. if (bbuf->new_valid <= valid_size)
  221. bbuf->new_valid = 0;
  222. else if (bbuf->new_valid > data_size)
  223. bbuf->new_valid = data_size;
  224. if (vbo >= valid_size) {
  225. memset(bh->b_data, 0, blocksize);
  226. } else if (vbo + blocksize > valid_size) {
  227. u32 voff = valid_size & sbi->block_mask;
  228. memset(bh->b_data + voff, 0, blocksize - voff);
  229. }
  230. bbuf->buf = (ulong *)bh->b_data;
  231. bbuf->bit = 8 * (off & ~(size_t)sbi->block_mask);
  232. bbuf->nbits = 8 * blocksize;
  233. return 0;
  234. }
  235. static void bmp_buf_put(struct bmp_buf *bbuf, bool dirty)
  236. {
  237. struct buffer_head *bh = bbuf->bh;
  238. struct ATTRIB *b = bbuf->b;
  239. if (!bh) {
  240. if (b && !b->non_res && dirty)
  241. bbuf->mi->dirty = true;
  242. return;
  243. }
  244. if (!dirty)
  245. goto out;
  246. if (bbuf->new_valid) {
  247. b->nres.valid_size = cpu_to_le64(bbuf->new_valid);
  248. bbuf->mi->dirty = true;
  249. }
  250. set_buffer_uptodate(bh);
  251. mark_buffer_dirty(bh);
  252. out:
  253. unlock_buffer(bh);
  254. put_bh(bh);
  255. }
  256. /*
  257. * indx_mark_used - Mark the bit @bit as used.
  258. */
  259. static int indx_mark_used(struct ntfs_index *indx, struct ntfs_inode *ni,
  260. size_t bit)
  261. {
  262. int err;
  263. struct bmp_buf bbuf;
  264. err = bmp_buf_get(indx, ni, bit, &bbuf);
  265. if (err)
  266. return err;
  267. __set_bit_le(bit - bbuf.bit, bbuf.buf);
  268. bmp_buf_put(&bbuf, true);
  269. return 0;
  270. }
  271. /*
  272. * indx_mark_free - Mark the bit @bit as free.
  273. */
  274. static int indx_mark_free(struct ntfs_index *indx, struct ntfs_inode *ni,
  275. size_t bit)
  276. {
  277. int err;
  278. struct bmp_buf bbuf;
  279. err = bmp_buf_get(indx, ni, bit, &bbuf);
  280. if (err)
  281. return err;
  282. __clear_bit_le(bit - bbuf.bit, bbuf.buf);
  283. bmp_buf_put(&bbuf, true);
  284. return 0;
  285. }
  286. /*
  287. * scan_nres_bitmap
  288. *
  289. * If ntfs_readdir calls this function (indx_used_bit -> scan_nres_bitmap),
  290. * inode is shared locked and no ni_lock.
  291. * Use rw_semaphore for read/write access to bitmap_run.
  292. */
  293. static int scan_nres_bitmap(struct ntfs_inode *ni, struct ATTRIB *bitmap,
  294. struct ntfs_index *indx, size_t from,
  295. bool (*fn)(const ulong *buf, u32 bit, u32 bits,
  296. size_t *ret),
  297. size_t *ret)
  298. {
  299. struct ntfs_sb_info *sbi = ni->mi.sbi;
  300. struct super_block *sb = sbi->sb;
  301. struct runs_tree *run = &indx->bitmap_run;
  302. struct rw_semaphore *lock = &indx->run_lock;
  303. u32 nbits = sb->s_blocksize * 8;
  304. u32 blocksize = sb->s_blocksize;
  305. u64 valid_size = le64_to_cpu(bitmap->nres.valid_size);
  306. u64 data_size = le64_to_cpu(bitmap->nres.data_size);
  307. sector_t eblock = bytes_to_block(sb, data_size);
  308. size_t vbo = from >> 3;
  309. sector_t blk = (vbo & sbi->cluster_mask) >> sb->s_blocksize_bits;
  310. sector_t vblock = vbo >> sb->s_blocksize_bits;
  311. sector_t blen, block;
  312. CLST lcn, clen, vcn, vcn_next;
  313. size_t idx;
  314. struct buffer_head *bh;
  315. bool ok;
  316. *ret = MINUS_ONE_T;
  317. if (vblock >= eblock)
  318. return 0;
  319. from &= nbits - 1;
  320. vcn = vbo >> sbi->cluster_bits;
  321. down_read(lock);
  322. ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
  323. up_read(lock);
  324. next_run:
  325. if (!ok) {
  326. int err;
  327. const struct INDEX_NAMES *name = &s_index_names[indx->type];
  328. down_write(lock);
  329. err = attr_load_runs_vcn(ni, ATTR_BITMAP, name->name,
  330. name->name_len, run, vcn);
  331. up_write(lock);
  332. if (err)
  333. return err;
  334. down_read(lock);
  335. ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
  336. up_read(lock);
  337. if (!ok)
  338. return -EINVAL;
  339. }
  340. blen = (sector_t)clen * sbi->blocks_per_cluster;
  341. block = (sector_t)lcn * sbi->blocks_per_cluster;
  342. for (; blk < blen; blk++, from = 0) {
  343. bh = ntfs_bread(sb, block + blk);
  344. if (!bh)
  345. return -EIO;
  346. vbo = (u64)vblock << sb->s_blocksize_bits;
  347. if (vbo >= valid_size) {
  348. memset(bh->b_data, 0, blocksize);
  349. } else if (vbo + blocksize > valid_size) {
  350. u32 voff = valid_size & sbi->block_mask;
  351. memset(bh->b_data + voff, 0, blocksize - voff);
  352. }
  353. if (vbo + blocksize > data_size)
  354. nbits = 8 * (data_size - vbo);
  355. ok = nbits > from ?
  356. (*fn)((ulong *)bh->b_data, from, nbits, ret) :
  357. false;
  358. put_bh(bh);
  359. if (ok) {
  360. *ret += 8 * vbo;
  361. return 0;
  362. }
  363. if (++vblock >= eblock) {
  364. *ret = MINUS_ONE_T;
  365. return 0;
  366. }
  367. }
  368. blk = 0;
  369. vcn_next = vcn + clen;
  370. down_read(lock);
  371. ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) && vcn == vcn_next;
  372. if (!ok)
  373. vcn = vcn_next;
  374. up_read(lock);
  375. goto next_run;
  376. }
  377. static bool scan_for_free(const ulong *buf, u32 bit, u32 bits, size_t *ret)
  378. {
  379. size_t pos = find_next_zero_bit_le(buf, bits, bit);
  380. if (pos >= bits)
  381. return false;
  382. *ret = pos;
  383. return true;
  384. }
  385. /*
  386. * indx_find_free - Look for free bit.
  387. *
  388. * Return: -1 if no free bits.
  389. */
  390. static int indx_find_free(struct ntfs_index *indx, struct ntfs_inode *ni,
  391. size_t *bit, struct ATTRIB **bitmap)
  392. {
  393. struct ATTRIB *b;
  394. struct ATTR_LIST_ENTRY *le = NULL;
  395. const struct INDEX_NAMES *in = &s_index_names[indx->type];
  396. int err;
  397. b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
  398. NULL, NULL);
  399. if (!b)
  400. return -ENOENT;
  401. *bitmap = b;
  402. *bit = MINUS_ONE_T;
  403. if (!b->non_res) {
  404. u32 nbits = 8 * le32_to_cpu(b->res.data_size);
  405. size_t pos = find_next_zero_bit_le(resident_data(b), nbits, 0);
  406. if (pos < nbits)
  407. *bit = pos;
  408. } else {
  409. err = scan_nres_bitmap(ni, b, indx, 0, &scan_for_free, bit);
  410. if (err)
  411. return err;
  412. }
  413. return 0;
  414. }
  415. static bool scan_for_used(const ulong *buf, u32 bit, u32 bits, size_t *ret)
  416. {
  417. size_t pos = find_next_bit_le(buf, bits, bit);
  418. if (pos >= bits)
  419. return false;
  420. *ret = pos;
  421. return true;
  422. }
  423. /*
  424. * indx_used_bit - Look for used bit.
  425. *
  426. * Return: MINUS_ONE_T if no used bits.
  427. */
  428. int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit)
  429. {
  430. struct ATTRIB *b;
  431. struct ATTR_LIST_ENTRY *le = NULL;
  432. size_t from = *bit;
  433. const struct INDEX_NAMES *in = &s_index_names[indx->type];
  434. int err;
  435. b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
  436. NULL, NULL);
  437. if (!b)
  438. return -ENOENT;
  439. *bit = MINUS_ONE_T;
  440. if (!b->non_res) {
  441. u32 nbits = le32_to_cpu(b->res.data_size) * 8;
  442. size_t pos = find_next_bit_le(resident_data(b), nbits, from);
  443. if (pos < nbits)
  444. *bit = pos;
  445. } else {
  446. err = scan_nres_bitmap(ni, b, indx, from, &scan_for_used, bit);
  447. if (err)
  448. return err;
  449. }
  450. return 0;
  451. }
  452. /*
  453. * hdr_find_split
  454. *
  455. * Find a point at which the index allocation buffer would like to be split.
  456. * NOTE: This function should never return 'END' entry NULL returns on error.
  457. */
  458. static const struct NTFS_DE *hdr_find_split(const struct INDEX_HDR *hdr)
  459. {
  460. size_t o;
  461. const struct NTFS_DE *e = hdr_first_de(hdr);
  462. u32 used_2 = le32_to_cpu(hdr->used) >> 1;
  463. u16 esize;
  464. if (!e || de_is_last(e))
  465. return NULL;
  466. esize = le16_to_cpu(e->size);
  467. for (o = le32_to_cpu(hdr->de_off) + esize; o < used_2; o += esize) {
  468. const struct NTFS_DE *p = e;
  469. e = Add2Ptr(hdr, o);
  470. /* We must not return END entry. */
  471. if (de_is_last(e))
  472. return p;
  473. esize = le16_to_cpu(e->size);
  474. }
  475. return e;
  476. }
  477. /*
  478. * hdr_insert_head - Insert some entries at the beginning of the buffer.
  479. *
  480. * It is used to insert entries into a newly-created buffer.
  481. */
  482. static const struct NTFS_DE *hdr_insert_head(struct INDEX_HDR *hdr,
  483. const void *ins, u32 ins_bytes)
  484. {
  485. u32 to_move;
  486. struct NTFS_DE *e = hdr_first_de(hdr);
  487. u32 used = le32_to_cpu(hdr->used);
  488. if (!e)
  489. return NULL;
  490. /* Now we just make room for the inserted entries and jam it in. */
  491. to_move = used - le32_to_cpu(hdr->de_off);
  492. memmove(Add2Ptr(e, ins_bytes), e, to_move);
  493. memcpy(e, ins, ins_bytes);
  494. hdr->used = cpu_to_le32(used + ins_bytes);
  495. return e;
  496. }
  497. /*
  498. * index_hdr_check
  499. *
  500. * return true if INDEX_HDR is valid
  501. */
  502. static bool index_hdr_check(const struct INDEX_HDR *hdr, u32 bytes)
  503. {
  504. u32 end = le32_to_cpu(hdr->used);
  505. u32 tot = le32_to_cpu(hdr->total);
  506. u32 off = le32_to_cpu(hdr->de_off);
  507. if (!IS_ALIGNED(off, 8) || tot > bytes || end > tot ||
  508. size_add(off, sizeof(struct NTFS_DE)) > end) {
  509. /* incorrect index buffer. */
  510. return false;
  511. }
  512. return true;
  513. }
  514. /*
  515. * index_buf_check
  516. *
  517. * return true if INDEX_BUFFER seems is valid
  518. */
  519. static bool index_buf_check(const struct INDEX_BUFFER *ib, u32 bytes,
  520. const CLST *vbn)
  521. {
  522. const struct NTFS_RECORD_HEADER *rhdr = &ib->rhdr;
  523. u16 fo = le16_to_cpu(rhdr->fix_off);
  524. u16 fn = le16_to_cpu(rhdr->fix_num);
  525. if (bytes <= offsetof(struct INDEX_BUFFER, ihdr) ||
  526. rhdr->sign != NTFS_INDX_SIGNATURE ||
  527. fo < sizeof(struct INDEX_BUFFER)
  528. /* Check index buffer vbn. */
  529. || (vbn && *vbn != le64_to_cpu(ib->vbn)) || (fo % sizeof(short)) ||
  530. fo + fn * sizeof(short) >= bytes ||
  531. fn != ((bytes >> SECTOR_SHIFT) + 1)) {
  532. /* incorrect index buffer. */
  533. return false;
  534. }
  535. return index_hdr_check(&ib->ihdr,
  536. bytes - offsetof(struct INDEX_BUFFER, ihdr));
  537. }
  538. void fnd_clear(struct ntfs_fnd *fnd)
  539. {
  540. int i;
  541. for (i = fnd->level - 1; i >= 0; i--) {
  542. struct indx_node *n = fnd->nodes[i];
  543. if (!n)
  544. continue;
  545. put_indx_node(n);
  546. fnd->nodes[i] = NULL;
  547. }
  548. fnd->level = 0;
  549. fnd->root_de = NULL;
  550. }
  551. static int fnd_push(struct ntfs_fnd *fnd, struct indx_node *n,
  552. struct NTFS_DE *e)
  553. {
  554. int i = fnd->level;
  555. if (i < 0 || i >= ARRAY_SIZE(fnd->nodes))
  556. return -EINVAL;
  557. fnd->nodes[i] = n;
  558. fnd->de[i] = e;
  559. fnd->level += 1;
  560. return 0;
  561. }
  562. static struct indx_node *fnd_pop(struct ntfs_fnd *fnd)
  563. {
  564. struct indx_node *n;
  565. int i = fnd->level;
  566. i -= 1;
  567. n = fnd->nodes[i];
  568. fnd->nodes[i] = NULL;
  569. fnd->level = i;
  570. return n;
  571. }
  572. static bool fnd_is_empty(struct ntfs_fnd *fnd)
  573. {
  574. if (!fnd->level)
  575. return !fnd->root_de;
  576. return !fnd->de[fnd->level - 1];
  577. }
  578. /*
  579. * hdr_find_e - Locate an entry the index buffer.
  580. *
  581. * If no matching entry is found, it returns the first entry which is greater
  582. * than the desired entry If the search key is greater than all the entries the
  583. * buffer, it returns the 'end' entry. This function does a binary search of the
  584. * current index buffer, for the first entry that is <= to the search value.
  585. *
  586. * Return: NULL if error.
  587. */
  588. static struct NTFS_DE *hdr_find_e(const struct ntfs_index *indx,
  589. const struct INDEX_HDR *hdr, const void *key,
  590. size_t key_len, const void *ctx, int *diff)
  591. {
  592. struct NTFS_DE *e, *found = NULL;
  593. NTFS_CMP_FUNC cmp = indx->cmp;
  594. int min_idx = 0, mid_idx, max_idx = 0;
  595. int diff2;
  596. int table_size = 8;
  597. u32 e_size, e_key_len;
  598. u32 end = le32_to_cpu(hdr->used);
  599. u32 off = le32_to_cpu(hdr->de_off);
  600. u32 total = le32_to_cpu(hdr->total);
  601. u16 offs[128];
  602. if (unlikely(!cmp))
  603. return NULL;
  604. fill_table:
  605. if (end > total)
  606. return NULL;
  607. if (size_add(off, sizeof(struct NTFS_DE)) > end)
  608. return NULL;
  609. e = Add2Ptr(hdr, off);
  610. e_size = le16_to_cpu(e->size);
  611. if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
  612. return NULL;
  613. if (!de_is_last(e)) {
  614. offs[max_idx] = off;
  615. off += e_size;
  616. max_idx++;
  617. if (max_idx < table_size)
  618. goto fill_table;
  619. max_idx--;
  620. }
  621. binary_search:
  622. e_key_len = le16_to_cpu(e->key_size);
  623. diff2 = (*cmp)(key, key_len, e + 1, e_key_len, ctx);
  624. if (diff2 > 0) {
  625. if (found) {
  626. min_idx = mid_idx + 1;
  627. } else {
  628. if (de_is_last(e))
  629. return NULL;
  630. max_idx = 0;
  631. table_size = min(table_size * 2, (int)ARRAY_SIZE(offs));
  632. goto fill_table;
  633. }
  634. } else if (diff2 < 0) {
  635. if (found)
  636. max_idx = mid_idx - 1;
  637. else
  638. max_idx--;
  639. found = e;
  640. } else {
  641. *diff = 0;
  642. return e;
  643. }
  644. if (min_idx > max_idx) {
  645. *diff = -1;
  646. return found;
  647. }
  648. mid_idx = (min_idx + max_idx) >> 1;
  649. e = Add2Ptr(hdr, offs[mid_idx]);
  650. goto binary_search;
  651. }
  652. /*
  653. * hdr_insert_de - Insert an index entry into the buffer.
  654. *
  655. * 'before' should be a pointer previously returned from hdr_find_e.
  656. */
  657. static struct NTFS_DE *hdr_insert_de(const struct ntfs_index *indx,
  658. struct INDEX_HDR *hdr,
  659. const struct NTFS_DE *de,
  660. struct NTFS_DE *before, const void *ctx)
  661. {
  662. int diff;
  663. size_t off = PtrOffset(hdr, before);
  664. u32 used = le32_to_cpu(hdr->used);
  665. u32 total = le32_to_cpu(hdr->total);
  666. u16 de_size = le16_to_cpu(de->size);
  667. /* First, check to see if there's enough room. */
  668. if (used + de_size > total)
  669. return NULL;
  670. /* We know there's enough space, so we know we'll succeed. */
  671. if (before) {
  672. /* Check that before is inside Index. */
  673. if (off >= used || off < le32_to_cpu(hdr->de_off) ||
  674. off + le16_to_cpu(before->size) > total) {
  675. return NULL;
  676. }
  677. goto ok;
  678. }
  679. /* No insert point is applied. Get it manually. */
  680. before = hdr_find_e(indx, hdr, de + 1, le16_to_cpu(de->key_size), ctx,
  681. &diff);
  682. if (!before)
  683. return NULL;
  684. off = PtrOffset(hdr, before);
  685. ok:
  686. /* Now we just make room for the entry and jam it in. */
  687. memmove(Add2Ptr(before, de_size), before, used - off);
  688. hdr->used = cpu_to_le32(used + de_size);
  689. memcpy(before, de, de_size);
  690. return before;
  691. }
  692. /*
  693. * hdr_delete_de - Remove an entry from the index buffer.
  694. */
  695. static inline struct NTFS_DE *hdr_delete_de(struct INDEX_HDR *hdr,
  696. struct NTFS_DE *re)
  697. {
  698. u32 used = le32_to_cpu(hdr->used);
  699. u16 esize = le16_to_cpu(re->size);
  700. u32 off = PtrOffset(hdr, re);
  701. int bytes = used - (off + esize);
  702. /* check INDEX_HDR valid before using INDEX_HDR */
  703. if (!check_index_header(hdr, le32_to_cpu(hdr->total)))
  704. return NULL;
  705. if (off >= used || esize < sizeof(struct NTFS_DE) ||
  706. bytes < sizeof(struct NTFS_DE))
  707. return NULL;
  708. hdr->used = cpu_to_le32(used - esize);
  709. memmove(re, Add2Ptr(re, esize), bytes);
  710. return re;
  711. }
  712. void indx_clear(struct ntfs_index *indx)
  713. {
  714. run_close(&indx->alloc_run);
  715. run_close(&indx->bitmap_run);
  716. }
  717. int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
  718. const struct ATTRIB *attr, enum index_mutex_classed type)
  719. {
  720. u32 t32;
  721. const struct INDEX_ROOT *root = resident_data(attr);
  722. t32 = le32_to_cpu(attr->res.data_size);
  723. if (t32 <= offsetof(struct INDEX_ROOT, ihdr) ||
  724. !index_hdr_check(&root->ihdr,
  725. t32 - offsetof(struct INDEX_ROOT, ihdr))) {
  726. goto out;
  727. }
  728. /* Check root fields. */
  729. if (!root->index_block_clst)
  730. goto out;
  731. indx->type = type;
  732. indx->idx2vbn_bits = __ffs(root->index_block_clst);
  733. t32 = le32_to_cpu(root->index_block_size);
  734. indx->index_bits = blksize_bits(t32);
  735. /* Check index record size. */
  736. if (t32 < sbi->cluster_size) {
  737. /* Index record is smaller than a cluster, use 512 blocks. */
  738. if (t32 != root->index_block_clst * SECTOR_SIZE)
  739. goto out;
  740. /* Check alignment to a cluster. */
  741. if ((sbi->cluster_size >> SECTOR_SHIFT) &
  742. (root->index_block_clst - 1)) {
  743. goto out;
  744. }
  745. indx->vbn2vbo_bits = SECTOR_SHIFT;
  746. } else {
  747. /* Index record must be a multiple of cluster size. */
  748. if (t32 != root->index_block_clst << sbi->cluster_bits)
  749. goto out;
  750. indx->vbn2vbo_bits = sbi->cluster_bits;
  751. }
  752. init_rwsem(&indx->run_lock);
  753. indx->cmp = get_cmp_func(root);
  754. if (!indx->cmp)
  755. goto out;
  756. return 0;
  757. out:
  758. ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
  759. return -EINVAL;
  760. }
  761. static struct indx_node *indx_new(struct ntfs_index *indx,
  762. struct ntfs_inode *ni, CLST vbn,
  763. const __le64 *sub_vbn)
  764. {
  765. int err;
  766. struct NTFS_DE *e;
  767. struct indx_node *r;
  768. struct INDEX_HDR *hdr;
  769. struct INDEX_BUFFER *index;
  770. u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
  771. u32 bytes = 1u << indx->index_bits;
  772. u16 fn;
  773. u32 eo;
  774. r = kzalloc(sizeof(struct indx_node), GFP_NOFS);
  775. if (!r)
  776. return ERR_PTR(-ENOMEM);
  777. index = kzalloc(bytes, GFP_NOFS);
  778. if (!index) {
  779. kfree(r);
  780. return ERR_PTR(-ENOMEM);
  781. }
  782. err = ntfs_get_bh(ni->mi.sbi, &indx->alloc_run, vbo, bytes, &r->nb);
  783. if (err) {
  784. kfree(index);
  785. kfree(r);
  786. return ERR_PTR(err);
  787. }
  788. /* Create header. */
  789. index->rhdr.sign = NTFS_INDX_SIGNATURE;
  790. index->rhdr.fix_off = cpu_to_le16(sizeof(struct INDEX_BUFFER)); // 0x28
  791. fn = (bytes >> SECTOR_SHIFT) + 1; // 9
  792. index->rhdr.fix_num = cpu_to_le16(fn);
  793. index->vbn = cpu_to_le64(vbn);
  794. hdr = &index->ihdr;
  795. eo = ALIGN(sizeof(struct INDEX_BUFFER) + fn * sizeof(short), 8);
  796. hdr->de_off = cpu_to_le32(eo);
  797. e = Add2Ptr(hdr, eo);
  798. if (sub_vbn) {
  799. e->flags = NTFS_IE_LAST | NTFS_IE_HAS_SUBNODES;
  800. e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
  801. hdr->used =
  802. cpu_to_le32(eo + sizeof(struct NTFS_DE) + sizeof(u64));
  803. de_set_vbn_le(e, *sub_vbn);
  804. hdr->flags = NTFS_INDEX_HDR_HAS_SUBNODES;
  805. } else {
  806. e->size = cpu_to_le16(sizeof(struct NTFS_DE));
  807. hdr->used = cpu_to_le32(eo + sizeof(struct NTFS_DE));
  808. e->flags = NTFS_IE_LAST;
  809. }
  810. hdr->total = cpu_to_le32(bytes - offsetof(struct INDEX_BUFFER, ihdr));
  811. r->index = index;
  812. return r;
  813. }
  814. struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
  815. struct ATTRIB **attr, struct mft_inode **mi)
  816. {
  817. struct ATTR_LIST_ENTRY *le = NULL;
  818. struct ATTRIB *a;
  819. const struct INDEX_NAMES *in = &s_index_names[indx->type];
  820. struct INDEX_ROOT *root;
  821. a = ni_find_attr(ni, NULL, &le, ATTR_ROOT, in->name, in->name_len, NULL,
  822. mi);
  823. if (!a)
  824. return NULL;
  825. if (attr)
  826. *attr = a;
  827. root = resident_data_ex(a, sizeof(struct INDEX_ROOT));
  828. /* length check */
  829. if (root &&
  830. offsetof(struct INDEX_ROOT, ihdr) + le32_to_cpu(root->ihdr.used) >
  831. le32_to_cpu(a->res.data_size)) {
  832. return NULL;
  833. }
  834. return root;
  835. }
  836. static int indx_write(struct ntfs_index *indx, struct ntfs_inode *ni,
  837. struct indx_node *node, int sync)
  838. {
  839. struct INDEX_BUFFER *ib = node->index;
  840. return ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &node->nb, sync);
  841. }
  842. /*
  843. * indx_read
  844. *
  845. * If ntfs_readdir calls this function
  846. * inode is shared locked and no ni_lock.
  847. * Use rw_semaphore for read/write access to alloc_run.
  848. */
  849. int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
  850. struct indx_node **node)
  851. {
  852. int err;
  853. struct INDEX_BUFFER *ib;
  854. struct runs_tree *run = &indx->alloc_run;
  855. struct rw_semaphore *lock = &indx->run_lock;
  856. u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
  857. u32 bytes = 1u << indx->index_bits;
  858. struct indx_node *in = *node;
  859. const struct INDEX_NAMES *name;
  860. if (!in) {
  861. in = kzalloc(sizeof(struct indx_node), GFP_NOFS);
  862. if (!in)
  863. return -ENOMEM;
  864. } else {
  865. nb_put(&in->nb);
  866. }
  867. ib = in->index;
  868. if (!ib) {
  869. ib = kmalloc(bytes, GFP_NOFS);
  870. if (!ib) {
  871. err = -ENOMEM;
  872. goto out;
  873. }
  874. }
  875. down_read(lock);
  876. err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
  877. up_read(lock);
  878. if (!err)
  879. goto ok;
  880. if (err == -E_NTFS_FIXUP)
  881. goto ok;
  882. if (err != -ENOENT)
  883. goto out;
  884. name = &s_index_names[indx->type];
  885. down_write(lock);
  886. err = attr_load_runs_range(ni, ATTR_ALLOC, name->name, name->name_len,
  887. run, vbo, vbo + bytes);
  888. up_write(lock);
  889. if (err)
  890. goto out;
  891. down_read(lock);
  892. err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
  893. up_read(lock);
  894. if (err == -E_NTFS_FIXUP)
  895. goto ok;
  896. if (err)
  897. goto out;
  898. ok:
  899. if (!index_buf_check(ib, bytes, &vbn)) {
  900. _ntfs_bad_inode(&ni->vfs_inode);
  901. err = -EINVAL;
  902. goto out;
  903. }
  904. if (err == -E_NTFS_FIXUP) {
  905. ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &in->nb, 0);
  906. err = 0;
  907. }
  908. /* check for index header length */
  909. if (offsetof(struct INDEX_BUFFER, ihdr) + le32_to_cpu(ib->ihdr.used) >
  910. bytes) {
  911. err = -EINVAL;
  912. goto out;
  913. }
  914. in->index = ib;
  915. *node = in;
  916. out:
  917. if (err == -E_NTFS_CORRUPT) {
  918. _ntfs_bad_inode(&ni->vfs_inode);
  919. err = -EINVAL;
  920. }
  921. if (ib != in->index)
  922. kfree(ib);
  923. if (*node != in) {
  924. nb_put(&in->nb);
  925. kfree(in);
  926. }
  927. return err;
  928. }
  929. /*
  930. * indx_find - Scan NTFS directory for given entry.
  931. */
  932. int indx_find(struct ntfs_index *indx, struct ntfs_inode *ni,
  933. const struct INDEX_ROOT *root, const void *key, size_t key_len,
  934. const void *ctx, int *diff, struct NTFS_DE **entry,
  935. struct ntfs_fnd *fnd)
  936. {
  937. int err;
  938. struct NTFS_DE *e;
  939. struct indx_node *node;
  940. if (!root)
  941. root = indx_get_root(&ni->dir, ni, NULL, NULL);
  942. if (!root) {
  943. /* Should not happen. */
  944. return -EINVAL;
  945. }
  946. /* Check cache. */
  947. e = fnd->level ? fnd->de[fnd->level - 1] : fnd->root_de;
  948. if (e && !de_is_last(e) &&
  949. !(*indx->cmp)(key, key_len, e + 1, le16_to_cpu(e->key_size), ctx)) {
  950. *entry = e;
  951. *diff = 0;
  952. return 0;
  953. }
  954. /* Soft finder reset. */
  955. fnd_clear(fnd);
  956. /* Lookup entry that is <= to the search value. */
  957. e = hdr_find_e(indx, &root->ihdr, key, key_len, ctx, diff);
  958. if (!e)
  959. return -EINVAL;
  960. fnd->root_de = e;
  961. for (;;) {
  962. node = NULL;
  963. if (*diff >= 0 || !de_has_vcn_ex(e))
  964. break;
  965. /* Read next level. */
  966. err = indx_read(indx, ni, de_get_vbn(e), &node);
  967. if (err) {
  968. /* io error? */
  969. return err;
  970. }
  971. /* Lookup entry that is <= to the search value. */
  972. e = hdr_find_e(indx, &node->index->ihdr, key, key_len, ctx,
  973. diff);
  974. if (!e) {
  975. put_indx_node(node);
  976. return -EINVAL;
  977. }
  978. fnd_push(fnd, node, e);
  979. }
  980. *entry = e;
  981. return 0;
  982. }
  983. int indx_find_sort(struct ntfs_index *indx, struct ntfs_inode *ni,
  984. const struct INDEX_ROOT *root, struct NTFS_DE **entry,
  985. struct ntfs_fnd *fnd)
  986. {
  987. int err;
  988. struct indx_node *n = NULL;
  989. struct NTFS_DE *e;
  990. size_t iter = 0;
  991. int level = fnd->level;
  992. if (!*entry) {
  993. /* Start find. */
  994. e = hdr_first_de(&root->ihdr);
  995. if (!e)
  996. return 0;
  997. fnd_clear(fnd);
  998. fnd->root_de = e;
  999. } else if (!level) {
  1000. if (de_is_last(fnd->root_de)) {
  1001. *entry = NULL;
  1002. return 0;
  1003. }
  1004. e = hdr_next_de(&root->ihdr, fnd->root_de);
  1005. if (!e)
  1006. return -EINVAL;
  1007. fnd->root_de = e;
  1008. } else {
  1009. n = fnd->nodes[level - 1];
  1010. e = fnd->de[level - 1];
  1011. if (de_is_last(e))
  1012. goto pop_level;
  1013. e = hdr_next_de(&n->index->ihdr, e);
  1014. if (!e)
  1015. return -EINVAL;
  1016. fnd->de[level - 1] = e;
  1017. }
  1018. /* Just to avoid tree cycle. */
  1019. next_iter:
  1020. if (iter++ >= 1000)
  1021. return -EINVAL;
  1022. while (de_has_vcn_ex(e)) {
  1023. if (le16_to_cpu(e->size) <
  1024. sizeof(struct NTFS_DE) + sizeof(u64)) {
  1025. if (n) {
  1026. fnd_pop(fnd);
  1027. kfree(n);
  1028. }
  1029. return -EINVAL;
  1030. }
  1031. /* Read next level. */
  1032. err = indx_read(indx, ni, de_get_vbn(e), &n);
  1033. if (err)
  1034. return err;
  1035. /* Try next level. */
  1036. e = hdr_first_de(&n->index->ihdr);
  1037. if (!e) {
  1038. kfree(n);
  1039. return -EINVAL;
  1040. }
  1041. fnd_push(fnd, n, e);
  1042. }
  1043. if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
  1044. *entry = e;
  1045. return 0;
  1046. }
  1047. pop_level:
  1048. for (;;) {
  1049. if (!de_is_last(e))
  1050. goto next_iter;
  1051. /* Pop one level. */
  1052. if (n) {
  1053. fnd_pop(fnd);
  1054. kfree(n);
  1055. }
  1056. level = fnd->level;
  1057. if (level) {
  1058. n = fnd->nodes[level - 1];
  1059. e = fnd->de[level - 1];
  1060. } else if (fnd->root_de) {
  1061. n = NULL;
  1062. e = fnd->root_de;
  1063. fnd->root_de = NULL;
  1064. } else {
  1065. *entry = NULL;
  1066. return 0;
  1067. }
  1068. if (le16_to_cpu(e->size) > sizeof(struct NTFS_DE)) {
  1069. *entry = e;
  1070. if (!fnd->root_de)
  1071. fnd->root_de = e;
  1072. return 0;
  1073. }
  1074. }
  1075. }
  1076. int indx_find_raw(struct ntfs_index *indx, struct ntfs_inode *ni,
  1077. const struct INDEX_ROOT *root, struct NTFS_DE **entry,
  1078. size_t *off, struct ntfs_fnd *fnd)
  1079. {
  1080. int err;
  1081. struct indx_node *n = NULL;
  1082. struct NTFS_DE *e = NULL;
  1083. struct NTFS_DE *e2;
  1084. size_t bit;
  1085. CLST next_used_vbn;
  1086. CLST next_vbn;
  1087. u32 record_size = ni->mi.sbi->record_size;
  1088. /* Use non sorted algorithm. */
  1089. if (!*entry) {
  1090. /* This is the first call. */
  1091. e = hdr_first_de(&root->ihdr);
  1092. if (!e)
  1093. return 0;
  1094. fnd_clear(fnd);
  1095. fnd->root_de = e;
  1096. /* The first call with setup of initial element. */
  1097. if (*off >= record_size) {
  1098. next_vbn = (((*off - record_size) >> indx->index_bits))
  1099. << indx->idx2vbn_bits;
  1100. /* Jump inside cycle 'for'. */
  1101. goto next;
  1102. }
  1103. /* Start enumeration from root. */
  1104. *off = 0;
  1105. } else if (!fnd->root_de)
  1106. return -EINVAL;
  1107. for (;;) {
  1108. /* Check if current entry can be used. */
  1109. if (e && le16_to_cpu(e->size) > sizeof(struct NTFS_DE))
  1110. goto ok;
  1111. if (!fnd->level) {
  1112. /* Continue to enumerate root. */
  1113. if (!de_is_last(fnd->root_de)) {
  1114. e = hdr_next_de(&root->ihdr, fnd->root_de);
  1115. if (!e)
  1116. return -EINVAL;
  1117. fnd->root_de = e;
  1118. continue;
  1119. }
  1120. /* Start to enumerate indexes from 0. */
  1121. next_vbn = 0;
  1122. } else {
  1123. /* Continue to enumerate indexes. */
  1124. e2 = fnd->de[fnd->level - 1];
  1125. n = fnd->nodes[fnd->level - 1];
  1126. if (!de_is_last(e2)) {
  1127. e = hdr_next_de(&n->index->ihdr, e2);
  1128. if (!e)
  1129. return -EINVAL;
  1130. fnd->de[fnd->level - 1] = e;
  1131. continue;
  1132. }
  1133. /* Continue with next index. */
  1134. next_vbn = le64_to_cpu(n->index->vbn) +
  1135. root->index_block_clst;
  1136. }
  1137. next:
  1138. /* Release current index. */
  1139. if (n) {
  1140. fnd_pop(fnd);
  1141. put_indx_node(n);
  1142. n = NULL;
  1143. }
  1144. /* Skip all free indexes. */
  1145. bit = next_vbn >> indx->idx2vbn_bits;
  1146. err = indx_used_bit(indx, ni, &bit);
  1147. if (err == -ENOENT || bit == MINUS_ONE_T) {
  1148. /* No used indexes. */
  1149. *entry = NULL;
  1150. return 0;
  1151. }
  1152. next_used_vbn = bit << indx->idx2vbn_bits;
  1153. /* Read buffer into memory. */
  1154. err = indx_read(indx, ni, next_used_vbn, &n);
  1155. if (err)
  1156. return err;
  1157. e = hdr_first_de(&n->index->ihdr);
  1158. fnd_push(fnd, n, e);
  1159. if (!e)
  1160. return -EINVAL;
  1161. }
  1162. ok:
  1163. /* Return offset to restore enumerator if necessary. */
  1164. if (!n) {
  1165. /* 'e' points in root, */
  1166. *off = PtrOffset(&root->ihdr, e);
  1167. } else {
  1168. /* 'e' points in index, */
  1169. *off = (le64_to_cpu(n->index->vbn) << indx->vbn2vbo_bits) +
  1170. record_size + PtrOffset(&n->index->ihdr, e);
  1171. }
  1172. *entry = e;
  1173. return 0;
  1174. }
  1175. /*
  1176. * indx_create_allocate - Create "Allocation + Bitmap" attributes.
  1177. */
  1178. static int indx_create_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
  1179. CLST *vbn)
  1180. {
  1181. int err;
  1182. struct ntfs_sb_info *sbi = ni->mi.sbi;
  1183. struct ATTRIB *bitmap;
  1184. struct ATTRIB *alloc;
  1185. u32 data_size = 1u << indx->index_bits;
  1186. u32 alloc_size = ntfs_up_cluster(sbi, data_size);
  1187. CLST len = alloc_size >> sbi->cluster_bits;
  1188. const struct INDEX_NAMES *in = &s_index_names[indx->type];
  1189. CLST alen;
  1190. struct runs_tree run;
  1191. run_init(&run);
  1192. err = attr_allocate_clusters(sbi, &run, 0, 0, len, NULL, ALLOCATE_DEF,
  1193. &alen, 0, NULL, NULL);
  1194. if (err)
  1195. goto out;
  1196. err = ni_insert_nonresident(ni, ATTR_ALLOC, in->name, in->name_len,
  1197. &run, 0, len, 0, &alloc, NULL, NULL);
  1198. if (err)
  1199. goto out1;
  1200. alloc->nres.valid_size = alloc->nres.data_size = cpu_to_le64(data_size);
  1201. err = ni_insert_resident(ni, ntfs3_bitmap_size(1), ATTR_BITMAP,
  1202. in->name, in->name_len, &bitmap, NULL, NULL);
  1203. if (err)
  1204. goto out2;
  1205. if (in->name == I30_NAME) {
  1206. i_size_write(&ni->vfs_inode, data_size);
  1207. inode_set_bytes(&ni->vfs_inode, alloc_size);
  1208. }
  1209. memcpy(&indx->alloc_run, &run, sizeof(run));
  1210. *vbn = 0;
  1211. return 0;
  1212. out2:
  1213. mi_remove_attr(NULL, &ni->mi, alloc);
  1214. out1:
  1215. run_deallocate(sbi, &run, false);
  1216. out:
  1217. return err;
  1218. }
  1219. /*
  1220. * indx_add_allocate - Add clusters to index.
  1221. */
  1222. static int indx_add_allocate(struct ntfs_index *indx, struct ntfs_inode *ni,
  1223. CLST *vbn)
  1224. {
  1225. int err;
  1226. size_t bit;
  1227. u64 data_size;
  1228. u64 bmp_size, bmp_size_v;
  1229. struct ATTRIB *bmp, *alloc;
  1230. struct mft_inode *mi;
  1231. const struct INDEX_NAMES *in = &s_index_names[indx->type];
  1232. err = indx_find_free(indx, ni, &bit, &bmp);
  1233. if (err)
  1234. goto out1;
  1235. if (bit != MINUS_ONE_T) {
  1236. bmp = NULL;
  1237. } else {
  1238. if (bmp->non_res) {
  1239. bmp_size = le64_to_cpu(bmp->nres.data_size);
  1240. bmp_size_v = le64_to_cpu(bmp->nres.valid_size);
  1241. } else {
  1242. bmp_size = bmp_size_v = le32_to_cpu(bmp->res.data_size);
  1243. }
  1244. /*
  1245. * Index blocks exist, but $BITMAP has zero valid bits.
  1246. * This implies an on-disk corruption and must be rejected.
  1247. */
  1248. if (in->name == I30_NAME &&
  1249. unlikely(bmp_size_v == 0 && indx->alloc_run.count)) {
  1250. err = -EINVAL;
  1251. goto out1;
  1252. }
  1253. bit = bmp_size << 3;
  1254. }
  1255. data_size = (u64)(bit + 1) << indx->index_bits;
  1256. if (bmp) {
  1257. /* Increase bitmap. */
  1258. err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
  1259. &indx->bitmap_run,
  1260. ntfs3_bitmap_size(bit + 1), NULL, true,
  1261. NULL);
  1262. if (err)
  1263. goto out1;
  1264. }
  1265. alloc = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, in->name, in->name_len,
  1266. NULL, &mi);
  1267. if (!alloc) {
  1268. err = -EINVAL;
  1269. if (bmp)
  1270. goto out2;
  1271. goto out1;
  1272. }
  1273. if (data_size <= le64_to_cpu(alloc->nres.data_size)) {
  1274. /* Reuse index. */
  1275. goto out;
  1276. }
  1277. /* Increase allocation. */
  1278. err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
  1279. &indx->alloc_run, data_size, &data_size, true,
  1280. NULL);
  1281. if (err) {
  1282. if (bmp)
  1283. goto out2;
  1284. goto out1;
  1285. }
  1286. if (in->name == I30_NAME)
  1287. i_size_write(&ni->vfs_inode, data_size);
  1288. out:
  1289. *vbn = bit << indx->idx2vbn_bits;
  1290. return 0;
  1291. out2:
  1292. /* Ops. No space? */
  1293. attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
  1294. &indx->bitmap_run, bmp_size, &bmp_size_v, false, NULL);
  1295. out1:
  1296. return err;
  1297. }
  1298. /*
  1299. * indx_insert_into_root - Attempt to insert an entry into the index root.
  1300. *
  1301. * @undo - True if we undoing previous remove.
  1302. * If necessary, it will twiddle the index b-tree.
  1303. */
  1304. static int indx_insert_into_root(struct ntfs_index *indx, struct ntfs_inode *ni,
  1305. const struct NTFS_DE *new_de,
  1306. struct NTFS_DE *root_de, const void *ctx,
  1307. struct ntfs_fnd *fnd, bool undo)
  1308. {
  1309. int err = 0;
  1310. struct NTFS_DE *e, *e0, *re;
  1311. struct mft_inode *mi;
  1312. struct ATTRIB *attr;
  1313. struct INDEX_HDR *hdr;
  1314. struct indx_node *n;
  1315. CLST new_vbn;
  1316. __le64 *sub_vbn, t_vbn;
  1317. u16 new_de_size;
  1318. u32 hdr_used, hdr_total, asize, to_move;
  1319. u32 root_size, new_root_size;
  1320. struct ntfs_sb_info *sbi;
  1321. int ds_root;
  1322. struct INDEX_ROOT *root, *a_root;
  1323. /* Get the record this root placed in. */
  1324. root = indx_get_root(indx, ni, &attr, &mi);
  1325. if (!root)
  1326. return -EINVAL;
  1327. /*
  1328. * Try easy case:
  1329. * hdr_insert_de will succeed if there's
  1330. * room the root for the new entry.
  1331. */
  1332. hdr = &root->ihdr;
  1333. sbi = ni->mi.sbi;
  1334. new_de_size = le16_to_cpu(new_de->size);
  1335. hdr_used = le32_to_cpu(hdr->used);
  1336. hdr_total = le32_to_cpu(hdr->total);
  1337. asize = le32_to_cpu(attr->size);
  1338. root_size = le32_to_cpu(attr->res.data_size);
  1339. ds_root = new_de_size + hdr_used - hdr_total;
  1340. /* If 'undo' is set then reduce requirements. */
  1341. if ((undo || asize + ds_root < sbi->max_bytes_per_attr) &&
  1342. mi_resize_attr(mi, attr, ds_root)) {
  1343. hdr->total = cpu_to_le32(hdr_total + ds_root);
  1344. e = hdr_insert_de(indx, hdr, new_de, root_de, ctx);
  1345. WARN_ON(!e);
  1346. fnd_clear(fnd);
  1347. fnd->root_de = e;
  1348. return 0;
  1349. }
  1350. /* Make a copy of root attribute to restore if error. */
  1351. a_root = kmemdup(attr, asize, GFP_NOFS);
  1352. if (!a_root)
  1353. return -ENOMEM;
  1354. /*
  1355. * Copy all the non-end entries from
  1356. * the index root to the new buffer.
  1357. */
  1358. to_move = 0;
  1359. e0 = hdr_first_de(hdr);
  1360. /* Calculate the size to copy. */
  1361. for (e = e0;; e = hdr_next_de(hdr, e)) {
  1362. if (!e) {
  1363. err = -EINVAL;
  1364. goto out_free_root;
  1365. }
  1366. if (de_is_last(e))
  1367. break;
  1368. to_move += le16_to_cpu(e->size);
  1369. }
  1370. if (!to_move) {
  1371. re = NULL;
  1372. } else {
  1373. re = kmemdup(e0, to_move, GFP_NOFS);
  1374. if (!re) {
  1375. err = -ENOMEM;
  1376. goto out_free_root;
  1377. }
  1378. }
  1379. sub_vbn = NULL;
  1380. if (de_has_vcn(e)) {
  1381. t_vbn = de_get_vbn_le(e);
  1382. sub_vbn = &t_vbn;
  1383. }
  1384. new_root_size = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE) +
  1385. sizeof(u64);
  1386. ds_root = new_root_size - root_size;
  1387. if (ds_root > 0 && asize + ds_root > sbi->max_bytes_per_attr) {
  1388. /* Make root external. */
  1389. err = -EOPNOTSUPP;
  1390. goto out_free_re;
  1391. }
  1392. if (ds_root)
  1393. mi_resize_attr(mi, attr, ds_root);
  1394. /* Fill first entry (vcn will be set later). */
  1395. e = (struct NTFS_DE *)(root + 1);
  1396. memset(e, 0, sizeof(struct NTFS_DE));
  1397. e->size = cpu_to_le16(sizeof(struct NTFS_DE) + sizeof(u64));
  1398. e->flags = NTFS_IE_HAS_SUBNODES | NTFS_IE_LAST;
  1399. hdr->flags = NTFS_INDEX_HDR_HAS_SUBNODES;
  1400. hdr->used = hdr->total =
  1401. cpu_to_le32(new_root_size - offsetof(struct INDEX_ROOT, ihdr));
  1402. fnd->root_de = hdr_first_de(hdr);
  1403. mi->dirty = true;
  1404. /* Create alloc and bitmap attributes (if not). */
  1405. err = run_is_empty(&indx->alloc_run) ?
  1406. indx_create_allocate(indx, ni, &new_vbn) :
  1407. indx_add_allocate(indx, ni, &new_vbn);
  1408. /* Layout of record may be changed, so rescan root. */
  1409. root = indx_get_root(indx, ni, &attr, &mi);
  1410. if (!root) {
  1411. /* Bug? */
  1412. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  1413. err = -EINVAL;
  1414. goto out_free_re;
  1415. }
  1416. if (err) {
  1417. /* Restore root. */
  1418. if (mi_resize_attr(mi, attr, -ds_root)) {
  1419. memcpy(attr, a_root, asize);
  1420. } else {
  1421. /* Bug? */
  1422. ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
  1423. }
  1424. goto out_free_re;
  1425. }
  1426. e = (struct NTFS_DE *)(root + 1);
  1427. *(__le64 *)(e + 1) = cpu_to_le64(new_vbn);
  1428. mi->dirty = true;
  1429. /* Now we can create/format the new buffer and copy the entries into. */
  1430. n = indx_new(indx, ni, new_vbn, sub_vbn);
  1431. if (IS_ERR(n)) {
  1432. err = PTR_ERR(n);
  1433. goto out_free_re;
  1434. }
  1435. hdr = &n->index->ihdr;
  1436. hdr_used = le32_to_cpu(hdr->used);
  1437. hdr_total = le32_to_cpu(hdr->total);
  1438. /* Copy root entries into new buffer. */
  1439. hdr_insert_head(hdr, re, to_move);
  1440. /* Update bitmap attribute. */
  1441. indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
  1442. /* Check if we can insert new entry new index buffer. */
  1443. if (hdr_used + new_de_size > hdr_total) {
  1444. /*
  1445. * This occurs if MFT record is the same or bigger than index
  1446. * buffer. Move all root new index and have no space to add
  1447. * new entry classic case when MFT record is 1K and index
  1448. * buffer 4K the problem should not occurs.
  1449. */
  1450. kfree(re);
  1451. indx_write(indx, ni, n, 0);
  1452. put_indx_node(n);
  1453. fnd_clear(fnd);
  1454. err = indx_insert_entry(indx, ni, new_de, ctx, fnd, undo);
  1455. goto out_free_root;
  1456. }
  1457. /*
  1458. * Now root is a parent for new index buffer.
  1459. * Insert NewEntry a new buffer.
  1460. */
  1461. e = hdr_insert_de(indx, hdr, new_de, NULL, ctx);
  1462. if (!e) {
  1463. err = -EINVAL;
  1464. goto out_put_n;
  1465. }
  1466. fnd_push(fnd, n, e);
  1467. /* Just write updates index into disk. */
  1468. indx_write(indx, ni, n, 0);
  1469. n = NULL;
  1470. out_put_n:
  1471. put_indx_node(n);
  1472. out_free_re:
  1473. kfree(re);
  1474. out_free_root:
  1475. kfree(a_root);
  1476. return err;
  1477. }
  1478. /*
  1479. * indx_insert_into_buffer
  1480. *
  1481. * Attempt to insert an entry into an Index Allocation Buffer.
  1482. * If necessary, it will split the buffer.
  1483. */
  1484. static int
  1485. indx_insert_into_buffer(struct ntfs_index *indx, struct ntfs_inode *ni,
  1486. struct INDEX_ROOT *root, const struct NTFS_DE *new_de,
  1487. const void *ctx, int level, struct ntfs_fnd *fnd)
  1488. {
  1489. int err;
  1490. const struct NTFS_DE *sp;
  1491. struct NTFS_DE *e, *de_t, *up_e;
  1492. struct indx_node *n2;
  1493. struct indx_node *n1 = fnd->nodes[level];
  1494. struct INDEX_HDR *hdr1 = &n1->index->ihdr;
  1495. struct INDEX_HDR *hdr2;
  1496. u32 to_copy, used, used1;
  1497. CLST new_vbn;
  1498. __le64 t_vbn, *sub_vbn;
  1499. u16 sp_size;
  1500. void *hdr1_saved = NULL;
  1501. /* Try the most easy case. */
  1502. e = fnd->level - 1 == level ? fnd->de[level] : NULL;
  1503. e = hdr_insert_de(indx, hdr1, new_de, e, ctx);
  1504. fnd->de[level] = e;
  1505. if (e) {
  1506. /* Just write updated index into disk. */
  1507. indx_write(indx, ni, n1, 0);
  1508. return 0;
  1509. }
  1510. /*
  1511. * No space to insert into buffer. Split it.
  1512. * To split we:
  1513. * - Save split point ('cause index buffers will be changed)
  1514. * - Allocate NewBuffer and copy all entries <= sp into new buffer
  1515. * - Remove all entries (sp including) from TargetBuffer
  1516. * - Insert NewEntry into left or right buffer (depending on sp <=>
  1517. * NewEntry)
  1518. * - Insert sp into parent buffer (or root)
  1519. * - Make sp a parent for new buffer
  1520. */
  1521. sp = hdr_find_split(hdr1);
  1522. if (!sp)
  1523. return -EINVAL;
  1524. sp_size = le16_to_cpu(sp->size);
  1525. up_e = kmalloc(sp_size + sizeof(u64), GFP_NOFS);
  1526. if (!up_e)
  1527. return -ENOMEM;
  1528. memcpy(up_e, sp, sp_size);
  1529. used1 = le32_to_cpu(hdr1->used);
  1530. hdr1_saved = kmemdup(hdr1, used1, GFP_NOFS);
  1531. if (!hdr1_saved) {
  1532. err = -ENOMEM;
  1533. goto out;
  1534. }
  1535. if (!hdr1->flags) {
  1536. up_e->flags |= NTFS_IE_HAS_SUBNODES;
  1537. up_e->size = cpu_to_le16(sp_size + sizeof(u64));
  1538. sub_vbn = NULL;
  1539. } else {
  1540. t_vbn = de_get_vbn_le(up_e);
  1541. sub_vbn = &t_vbn;
  1542. }
  1543. /* Allocate on disk a new index allocation buffer. */
  1544. err = indx_add_allocate(indx, ni, &new_vbn);
  1545. if (err)
  1546. goto out;
  1547. /* Allocate and format memory a new index buffer. */
  1548. n2 = indx_new(indx, ni, new_vbn, sub_vbn);
  1549. if (IS_ERR(n2)) {
  1550. err = PTR_ERR(n2);
  1551. goto out;
  1552. }
  1553. hdr2 = &n2->index->ihdr;
  1554. /* Make sp a parent for new buffer. */
  1555. de_set_vbn(up_e, new_vbn);
  1556. /* Copy all the entries <= sp into the new buffer. */
  1557. de_t = hdr_first_de(hdr1);
  1558. to_copy = PtrOffset(de_t, sp);
  1559. hdr_insert_head(hdr2, de_t, to_copy);
  1560. /* Remove all entries (sp including) from hdr1. */
  1561. used = used1 - to_copy - sp_size;
  1562. memmove(de_t, Add2Ptr(sp, sp_size), used - le32_to_cpu(hdr1->de_off));
  1563. hdr1->used = cpu_to_le32(used);
  1564. /*
  1565. * Insert new entry into left or right buffer
  1566. * (depending on sp <=> new_de).
  1567. */
  1568. hdr_insert_de(indx,
  1569. (*indx->cmp)(new_de + 1, le16_to_cpu(new_de->key_size),
  1570. up_e + 1, le16_to_cpu(up_e->key_size),
  1571. ctx) < 0 ?
  1572. hdr2 :
  1573. hdr1,
  1574. new_de, NULL, ctx);
  1575. indx_mark_used(indx, ni, new_vbn >> indx->idx2vbn_bits);
  1576. indx_write(indx, ni, n1, 0);
  1577. indx_write(indx, ni, n2, 0);
  1578. put_indx_node(n2);
  1579. /*
  1580. * We've finished splitting everybody, so we are ready to
  1581. * insert the promoted entry into the parent.
  1582. */
  1583. if (!level) {
  1584. /* Insert in root. */
  1585. err = indx_insert_into_root(indx, ni, up_e, NULL, ctx, fnd, 0);
  1586. } else {
  1587. /*
  1588. * The target buffer's parent is another index buffer.
  1589. * TODO: Remove recursion.
  1590. */
  1591. err = indx_insert_into_buffer(indx, ni, root, up_e, ctx,
  1592. level - 1, fnd);
  1593. }
  1594. if (err) {
  1595. /*
  1596. * Undo critical operations.
  1597. */
  1598. indx_mark_free(indx, ni, new_vbn >> indx->idx2vbn_bits);
  1599. memcpy(hdr1, hdr1_saved, used1);
  1600. indx_write(indx, ni, n1, 0);
  1601. }
  1602. out:
  1603. kfree(up_e);
  1604. kfree(hdr1_saved);
  1605. return err;
  1606. }
  1607. /*
  1608. * indx_insert_entry - Insert new entry into index.
  1609. *
  1610. * @undo - True if we undoing previous remove.
  1611. */
  1612. int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
  1613. const struct NTFS_DE *new_de, const void *ctx,
  1614. struct ntfs_fnd *fnd, bool undo)
  1615. {
  1616. int err;
  1617. int diff;
  1618. struct NTFS_DE *e;
  1619. struct ntfs_fnd *fnd_a = NULL;
  1620. struct INDEX_ROOT *root;
  1621. if (!fnd) {
  1622. fnd_a = fnd_get();
  1623. if (!fnd_a) {
  1624. err = -ENOMEM;
  1625. goto out1;
  1626. }
  1627. fnd = fnd_a;
  1628. }
  1629. root = indx_get_root(indx, ni, NULL, NULL);
  1630. if (!root) {
  1631. err = -EINVAL;
  1632. goto out;
  1633. }
  1634. if (fnd_is_empty(fnd)) {
  1635. /*
  1636. * Find the spot the tree where we want to
  1637. * insert the new entry.
  1638. */
  1639. err = indx_find(indx, ni, root, new_de + 1,
  1640. le16_to_cpu(new_de->key_size), ctx, &diff, &e,
  1641. fnd);
  1642. if (err)
  1643. goto out;
  1644. if (!diff) {
  1645. err = -EEXIST;
  1646. goto out;
  1647. }
  1648. }
  1649. if (!fnd->level) {
  1650. /*
  1651. * The root is also a leaf, so we'll insert the
  1652. * new entry into it.
  1653. */
  1654. err = indx_insert_into_root(indx, ni, new_de, fnd->root_de, ctx,
  1655. fnd, undo);
  1656. } else {
  1657. /*
  1658. * Found a leaf buffer, so we'll insert the new entry into it.
  1659. */
  1660. err = indx_insert_into_buffer(indx, ni, root, new_de, ctx,
  1661. fnd->level - 1, fnd);
  1662. }
  1663. out:
  1664. fnd_put(fnd_a);
  1665. out1:
  1666. return err;
  1667. }
  1668. /*
  1669. * indx_find_buffer - Locate a buffer from the tree.
  1670. */
  1671. static struct indx_node *indx_find_buffer(struct ntfs_index *indx,
  1672. struct ntfs_inode *ni,
  1673. const struct INDEX_ROOT *root,
  1674. __le64 vbn, struct indx_node *n)
  1675. {
  1676. int err;
  1677. const struct NTFS_DE *e;
  1678. struct indx_node *r;
  1679. const struct INDEX_HDR *hdr = n ? &n->index->ihdr : &root->ihdr;
  1680. /* Step 1: Scan one level. */
  1681. for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
  1682. if (!e)
  1683. return ERR_PTR(-EINVAL);
  1684. if (de_has_vcn(e) && vbn == de_get_vbn_le(e))
  1685. return n;
  1686. if (de_is_last(e))
  1687. break;
  1688. }
  1689. /* Step2: Do recursion. */
  1690. e = Add2Ptr(hdr, le32_to_cpu(hdr->de_off));
  1691. for (;;) {
  1692. if (de_has_vcn_ex(e)) {
  1693. err = indx_read(indx, ni, de_get_vbn(e), &n);
  1694. if (err)
  1695. return ERR_PTR(err);
  1696. r = indx_find_buffer(indx, ni, root, vbn, n);
  1697. if (r)
  1698. return r;
  1699. }
  1700. if (de_is_last(e))
  1701. break;
  1702. e = Add2Ptr(e, le16_to_cpu(e->size));
  1703. }
  1704. return NULL;
  1705. }
  1706. /*
  1707. * indx_shrink - Deallocate unused tail indexes.
  1708. */
  1709. static int indx_shrink(struct ntfs_index *indx, struct ntfs_inode *ni,
  1710. size_t bit)
  1711. {
  1712. int err = 0;
  1713. u64 bpb, new_data;
  1714. size_t nbits;
  1715. struct ATTRIB *b;
  1716. struct ATTR_LIST_ENTRY *le = NULL;
  1717. const struct INDEX_NAMES *in = &s_index_names[indx->type];
  1718. b = ni_find_attr(ni, NULL, &le, ATTR_BITMAP, in->name, in->name_len,
  1719. NULL, NULL);
  1720. if (!b)
  1721. return -ENOENT;
  1722. if (!b->non_res) {
  1723. unsigned long pos;
  1724. const unsigned long *bm = resident_data(b);
  1725. nbits = (size_t)le32_to_cpu(b->res.data_size) * 8;
  1726. if (bit >= nbits)
  1727. return 0;
  1728. pos = find_next_bit_le(bm, nbits, bit);
  1729. if (pos < nbits)
  1730. return 0;
  1731. } else {
  1732. size_t used = MINUS_ONE_T;
  1733. nbits = le64_to_cpu(b->nres.data_size) * 8;
  1734. if (bit >= nbits)
  1735. return 0;
  1736. err = scan_nres_bitmap(ni, b, indx, bit, &scan_for_used, &used);
  1737. if (err)
  1738. return err;
  1739. if (used != MINUS_ONE_T)
  1740. return 0;
  1741. }
  1742. new_data = (u64)bit << indx->index_bits;
  1743. err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
  1744. &indx->alloc_run, new_data, &new_data, false, NULL);
  1745. if (err)
  1746. return err;
  1747. if (in->name == I30_NAME)
  1748. i_size_write(&ni->vfs_inode, new_data);
  1749. bpb = ntfs3_bitmap_size(bit);
  1750. if (bpb * 8 == nbits)
  1751. return 0;
  1752. err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
  1753. &indx->bitmap_run, bpb, &bpb, false, NULL);
  1754. return err;
  1755. }
  1756. static int indx_free_children(struct ntfs_index *indx, struct ntfs_inode *ni,
  1757. const struct NTFS_DE *e, bool trim)
  1758. {
  1759. int err;
  1760. struct indx_node *n = NULL;
  1761. struct INDEX_HDR *hdr;
  1762. CLST vbn = de_get_vbn(e);
  1763. size_t i;
  1764. err = indx_read(indx, ni, vbn, &n);
  1765. if (err)
  1766. return err;
  1767. hdr = &n->index->ihdr;
  1768. /* First, recurse into the children, if any. */
  1769. if (hdr_has_subnode(hdr)) {
  1770. for (e = hdr_first_de(hdr); e; e = hdr_next_de(hdr, e)) {
  1771. indx_free_children(indx, ni, e, false);
  1772. if (de_is_last(e))
  1773. break;
  1774. }
  1775. }
  1776. put_indx_node(n);
  1777. i = vbn >> indx->idx2vbn_bits;
  1778. /*
  1779. * We've gotten rid of the children; add this buffer to the free list.
  1780. */
  1781. indx_mark_free(indx, ni, i);
  1782. if (!trim)
  1783. return 0;
  1784. /*
  1785. * If there are no used indexes after current free index
  1786. * then we can truncate allocation and bitmap.
  1787. * Use bitmap to estimate the case.
  1788. */
  1789. indx_shrink(indx, ni, i + 1);
  1790. return 0;
  1791. }
  1792. /*
  1793. * indx_get_entry_to_replace
  1794. *
  1795. * Find a replacement entry for a deleted entry.
  1796. * Always returns a node entry:
  1797. * NTFS_IE_HAS_SUBNODES is set the flags and the size includes the sub_vcn.
  1798. */
  1799. static int indx_get_entry_to_replace(struct ntfs_index *indx,
  1800. struct ntfs_inode *ni,
  1801. const struct NTFS_DE *de_next,
  1802. struct NTFS_DE **de_to_replace,
  1803. struct ntfs_fnd *fnd)
  1804. {
  1805. int err;
  1806. int level = -1;
  1807. CLST vbn;
  1808. struct NTFS_DE *e, *te, *re;
  1809. struct indx_node *n;
  1810. struct INDEX_BUFFER *ib;
  1811. *de_to_replace = NULL;
  1812. /* Find first leaf entry down from de_next. */
  1813. vbn = de_get_vbn(de_next);
  1814. for (;;) {
  1815. n = NULL;
  1816. err = indx_read(indx, ni, vbn, &n);
  1817. if (err)
  1818. goto out;
  1819. e = hdr_first_de(&n->index->ihdr);
  1820. fnd_push(fnd, n, e);
  1821. if (!e) {
  1822. err = -EINVAL;
  1823. goto out;
  1824. }
  1825. if (!de_is_last(e)) {
  1826. /*
  1827. * This buffer is non-empty, so its first entry
  1828. * could be used as the replacement entry.
  1829. */
  1830. level = fnd->level - 1;
  1831. }
  1832. if (!de_has_vcn(e))
  1833. break;
  1834. /* This buffer is a node. Continue to go down. */
  1835. vbn = de_get_vbn(e);
  1836. }
  1837. if (level == -1)
  1838. goto out;
  1839. n = fnd->nodes[level];
  1840. te = hdr_first_de(&n->index->ihdr);
  1841. if (!te) {
  1842. err = -EINVAL;
  1843. goto out;
  1844. }
  1845. /* Copy the candidate entry into the replacement entry buffer. */
  1846. re = kmalloc(le16_to_cpu(te->size) + sizeof(u64), GFP_NOFS);
  1847. if (!re) {
  1848. err = -ENOMEM;
  1849. goto out;
  1850. }
  1851. *de_to_replace = re;
  1852. memcpy(re, te, le16_to_cpu(te->size));
  1853. if (!de_has_vcn(re)) {
  1854. /*
  1855. * The replacement entry we found doesn't have a sub_vcn.
  1856. * increase its size to hold one.
  1857. */
  1858. le16_add_cpu(&re->size, sizeof(u64));
  1859. re->flags |= NTFS_IE_HAS_SUBNODES;
  1860. } else {
  1861. /*
  1862. * The replacement entry we found was a node entry, which
  1863. * means that all its child buffers are empty. Return them
  1864. * to the free pool.
  1865. */
  1866. indx_free_children(indx, ni, te, true);
  1867. }
  1868. /*
  1869. * Expunge the replacement entry from its former location,
  1870. * and then write that buffer.
  1871. */
  1872. ib = n->index;
  1873. e = hdr_delete_de(&ib->ihdr, te);
  1874. fnd->de[level] = e;
  1875. indx_write(indx, ni, n, 0);
  1876. if (ib_is_leaf(ib) && ib_is_empty(ib)) {
  1877. /* An empty leaf. */
  1878. return 0;
  1879. }
  1880. out:
  1881. fnd_clear(fnd);
  1882. return err;
  1883. }
  1884. /*
  1885. * indx_delete_entry - Delete an entry from the index.
  1886. */
  1887. int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
  1888. const void *key, u32 key_len, const void *ctx)
  1889. {
  1890. int err, diff;
  1891. struct INDEX_ROOT *root;
  1892. struct INDEX_HDR *hdr;
  1893. struct ntfs_fnd *fnd, *fnd2;
  1894. struct INDEX_BUFFER *ib;
  1895. struct NTFS_DE *e, *re, *next, *prev, *me;
  1896. struct indx_node *n, *n2d = NULL;
  1897. __le64 sub_vbn;
  1898. int level, level2;
  1899. struct ATTRIB *attr;
  1900. struct mft_inode *mi;
  1901. u32 e_size, root_size, new_root_size;
  1902. size_t trim_bit;
  1903. const struct INDEX_NAMES *in;
  1904. fnd = fnd_get();
  1905. if (!fnd) {
  1906. err = -ENOMEM;
  1907. goto out2;
  1908. }
  1909. fnd2 = fnd_get();
  1910. if (!fnd2) {
  1911. err = -ENOMEM;
  1912. goto out1;
  1913. }
  1914. root = indx_get_root(indx, ni, &attr, &mi);
  1915. if (!root) {
  1916. err = -EINVAL;
  1917. goto out;
  1918. }
  1919. /* Locate the entry to remove. */
  1920. err = indx_find(indx, ni, root, key, key_len, ctx, &diff, &e, fnd);
  1921. if (err)
  1922. goto out;
  1923. if (!e || diff) {
  1924. err = -ENOENT;
  1925. goto out;
  1926. }
  1927. level = fnd->level;
  1928. if (level) {
  1929. n = fnd->nodes[level - 1];
  1930. e = fnd->de[level - 1];
  1931. ib = n->index;
  1932. hdr = &ib->ihdr;
  1933. } else {
  1934. hdr = &root->ihdr;
  1935. e = fnd->root_de;
  1936. n = NULL;
  1937. }
  1938. e_size = le16_to_cpu(e->size);
  1939. if (!de_has_vcn_ex(e)) {
  1940. /* The entry to delete is a leaf, so we can just rip it out. */
  1941. hdr_delete_de(hdr, e);
  1942. if (!level) {
  1943. hdr->total = hdr->used;
  1944. /* Shrink resident root attribute. */
  1945. mi_resize_attr(mi, attr, 0 - e_size);
  1946. goto out;
  1947. }
  1948. indx_write(indx, ni, n, 0);
  1949. /*
  1950. * Check to see if removing that entry made
  1951. * the leaf empty.
  1952. */
  1953. if (ib_is_leaf(ib) && ib_is_empty(ib)) {
  1954. fnd_pop(fnd);
  1955. fnd_push(fnd2, n, e);
  1956. }
  1957. } else {
  1958. /*
  1959. * The entry we wish to delete is a node buffer, so we
  1960. * have to find a replacement for it.
  1961. */
  1962. next = de_get_next(e);
  1963. err = indx_get_entry_to_replace(indx, ni, next, &re, fnd2);
  1964. if (err)
  1965. goto out;
  1966. if (re) {
  1967. de_set_vbn_le(re, de_get_vbn_le(e));
  1968. hdr_delete_de(hdr, e);
  1969. err = level ? indx_insert_into_buffer(indx, ni, root,
  1970. re, ctx,
  1971. fnd->level - 1,
  1972. fnd) :
  1973. indx_insert_into_root(indx, ni, re, e,
  1974. ctx, fnd, 0);
  1975. kfree(re);
  1976. if (err)
  1977. goto out;
  1978. } else {
  1979. /*
  1980. * There is no replacement for the current entry.
  1981. * This means that the subtree rooted at its node
  1982. * is empty, and can be deleted, which turn means
  1983. * that the node can just inherit the deleted
  1984. * entry sub_vcn.
  1985. */
  1986. indx_free_children(indx, ni, next, true);
  1987. de_set_vbn_le(next, de_get_vbn_le(e));
  1988. hdr_delete_de(hdr, e);
  1989. if (level) {
  1990. indx_write(indx, ni, n, 0);
  1991. } else {
  1992. hdr->total = hdr->used;
  1993. /* Shrink resident root attribute. */
  1994. mi_resize_attr(mi, attr, 0 - e_size);
  1995. }
  1996. }
  1997. }
  1998. /* Delete a branch of tree. */
  1999. if (!fnd2 || !fnd2->level)
  2000. goto out;
  2001. /* Reinit root 'cause it can be changed. */
  2002. root = indx_get_root(indx, ni, &attr, &mi);
  2003. if (!root) {
  2004. err = -EINVAL;
  2005. goto out;
  2006. }
  2007. n2d = NULL;
  2008. sub_vbn = fnd2->nodes[0]->index->vbn;
  2009. level2 = 0;
  2010. level = fnd->level;
  2011. hdr = level ? &fnd->nodes[level - 1]->index->ihdr : &root->ihdr;
  2012. /* Scan current level. */
  2013. for (e = hdr_first_de(hdr);; e = hdr_next_de(hdr, e)) {
  2014. if (!e) {
  2015. err = -EINVAL;
  2016. goto out;
  2017. }
  2018. if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
  2019. break;
  2020. if (de_is_last(e)) {
  2021. e = NULL;
  2022. break;
  2023. }
  2024. }
  2025. if (!e) {
  2026. /* Do slow search from root. */
  2027. struct indx_node *in;
  2028. fnd_clear(fnd);
  2029. in = indx_find_buffer(indx, ni, root, sub_vbn, NULL);
  2030. if (IS_ERR(in)) {
  2031. err = PTR_ERR(in);
  2032. goto out;
  2033. }
  2034. if (in)
  2035. fnd_push(fnd, in, NULL);
  2036. }
  2037. /* Merge fnd2 -> fnd. */
  2038. for (level = 0; level < fnd2->level; level++) {
  2039. fnd_push(fnd, fnd2->nodes[level], fnd2->de[level]);
  2040. fnd2->nodes[level] = NULL;
  2041. }
  2042. fnd2->level = 0;
  2043. hdr = NULL;
  2044. for (level = fnd->level; level; level--) {
  2045. struct indx_node *in = fnd->nodes[level - 1];
  2046. ib = in->index;
  2047. if (ib_is_empty(ib)) {
  2048. sub_vbn = ib->vbn;
  2049. } else {
  2050. hdr = &ib->ihdr;
  2051. n2d = in;
  2052. level2 = level;
  2053. break;
  2054. }
  2055. }
  2056. if (!hdr)
  2057. hdr = &root->ihdr;
  2058. e = hdr_first_de(hdr);
  2059. if (!e) {
  2060. err = -EINVAL;
  2061. goto out;
  2062. }
  2063. if (hdr != &root->ihdr || !de_is_last(e)) {
  2064. prev = NULL;
  2065. while (!de_is_last(e)) {
  2066. if (de_has_vcn(e) && sub_vbn == de_get_vbn_le(e))
  2067. break;
  2068. prev = e;
  2069. e = hdr_next_de(hdr, e);
  2070. if (!e) {
  2071. err = -EINVAL;
  2072. goto out;
  2073. }
  2074. }
  2075. if (sub_vbn != de_get_vbn_le(e)) {
  2076. /*
  2077. * Didn't find the parent entry, although this buffer
  2078. * is the parent trail. Something is corrupt.
  2079. */
  2080. err = -EINVAL;
  2081. goto out;
  2082. }
  2083. if (de_is_last(e)) {
  2084. /*
  2085. * Since we can't remove the end entry, we'll remove
  2086. * its predecessor instead. This means we have to
  2087. * transfer the predecessor's sub_vcn to the end entry.
  2088. * Note: This index block is not empty, so the
  2089. * predecessor must exist.
  2090. */
  2091. if (!prev) {
  2092. err = -EINVAL;
  2093. goto out;
  2094. }
  2095. if (de_has_vcn(prev)) {
  2096. de_set_vbn_le(e, de_get_vbn_le(prev));
  2097. } else if (de_has_vcn(e)) {
  2098. le16_sub_cpu(&e->size, sizeof(u64));
  2099. e->flags &= ~NTFS_IE_HAS_SUBNODES;
  2100. le32_sub_cpu(&hdr->used, sizeof(u64));
  2101. }
  2102. e = prev;
  2103. }
  2104. /*
  2105. * Copy the current entry into a temporary buffer (stripping
  2106. * off its down-pointer, if any) and delete it from the current
  2107. * buffer or root, as appropriate.
  2108. */
  2109. e_size = le16_to_cpu(e->size);
  2110. me = kmemdup(e, e_size, GFP_NOFS);
  2111. if (!me) {
  2112. err = -ENOMEM;
  2113. goto out;
  2114. }
  2115. if (de_has_vcn(me)) {
  2116. me->flags &= ~NTFS_IE_HAS_SUBNODES;
  2117. le16_sub_cpu(&me->size, sizeof(u64));
  2118. }
  2119. hdr_delete_de(hdr, e);
  2120. if (hdr == &root->ihdr) {
  2121. level = 0;
  2122. hdr->total = hdr->used;
  2123. /* Shrink resident root attribute. */
  2124. mi_resize_attr(mi, attr, 0 - e_size);
  2125. } else {
  2126. indx_write(indx, ni, n2d, 0);
  2127. level = level2;
  2128. }
  2129. /* Mark unused buffers as free. */
  2130. trim_bit = -1;
  2131. for (; level < fnd->level; level++) {
  2132. ib = fnd->nodes[level]->index;
  2133. if (ib_is_empty(ib)) {
  2134. size_t k = le64_to_cpu(ib->vbn) >>
  2135. indx->idx2vbn_bits;
  2136. indx_mark_free(indx, ni, k);
  2137. if (k < trim_bit)
  2138. trim_bit = k;
  2139. }
  2140. }
  2141. fnd_clear(fnd);
  2142. /*fnd->root_de = NULL;*/
  2143. /*
  2144. * Re-insert the entry into the tree.
  2145. * Find the spot the tree where we want to insert the new entry.
  2146. */
  2147. err = indx_insert_entry(indx, ni, me, ctx, fnd, 0);
  2148. kfree(me);
  2149. if (err)
  2150. goto out;
  2151. if (trim_bit != -1)
  2152. indx_shrink(indx, ni, trim_bit);
  2153. } else {
  2154. /*
  2155. * This tree needs to be collapsed down to an empty root.
  2156. * Recreate the index root as an empty leaf and free all
  2157. * the bits the index allocation bitmap.
  2158. */
  2159. fnd_clear(fnd);
  2160. fnd_clear(fnd2);
  2161. in = &s_index_names[indx->type];
  2162. err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len,
  2163. &indx->alloc_run, 0, NULL, false, NULL);
  2164. if (in->name == I30_NAME)
  2165. i_size_write(&ni->vfs_inode, 0);
  2166. err = ni_remove_attr(ni, ATTR_ALLOC, in->name, in->name_len,
  2167. false, NULL);
  2168. run_close(&indx->alloc_run);
  2169. err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len,
  2170. &indx->bitmap_run, 0, NULL, false, NULL);
  2171. err = ni_remove_attr(ni, ATTR_BITMAP, in->name, in->name_len,
  2172. false, NULL);
  2173. run_close(&indx->bitmap_run);
  2174. root = indx_get_root(indx, ni, &attr, &mi);
  2175. if (!root) {
  2176. err = -EINVAL;
  2177. goto out;
  2178. }
  2179. root_size = le32_to_cpu(attr->res.data_size);
  2180. new_root_size =
  2181. sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
  2182. if (new_root_size != root_size &&
  2183. !mi_resize_attr(mi, attr, new_root_size - root_size)) {
  2184. err = -EINVAL;
  2185. goto out;
  2186. }
  2187. /* Fill first entry. */
  2188. e = (struct NTFS_DE *)(root + 1);
  2189. e->ref.low = 0;
  2190. e->ref.high = 0;
  2191. e->ref.seq = 0;
  2192. e->size = cpu_to_le16(sizeof(struct NTFS_DE));
  2193. e->flags = NTFS_IE_LAST; // 0x02
  2194. e->key_size = 0;
  2195. e->res = 0;
  2196. hdr = &root->ihdr;
  2197. hdr->flags = 0;
  2198. hdr->used = hdr->total = cpu_to_le32(
  2199. new_root_size - offsetof(struct INDEX_ROOT, ihdr));
  2200. mi->dirty = true;
  2201. }
  2202. out:
  2203. fnd_put(fnd2);
  2204. out1:
  2205. fnd_put(fnd);
  2206. out2:
  2207. return err;
  2208. }
  2209. /*
  2210. * Update duplicated information in directory entry
  2211. * 'dup' - info from MFT record
  2212. */
  2213. int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
  2214. const struct ATTR_FILE_NAME *fname,
  2215. const struct NTFS_DUP_INFO *dup, int sync)
  2216. {
  2217. int err, diff;
  2218. struct NTFS_DE *e = NULL;
  2219. struct ATTR_FILE_NAME *e_fname;
  2220. struct ntfs_fnd *fnd;
  2221. struct INDEX_ROOT *root;
  2222. struct mft_inode *mi;
  2223. struct ntfs_index *indx = &ni->dir;
  2224. fnd = fnd_get();
  2225. if (!fnd)
  2226. return -ENOMEM;
  2227. root = indx_get_root(indx, ni, NULL, &mi);
  2228. if (!root) {
  2229. err = -EINVAL;
  2230. goto out;
  2231. }
  2232. /* Find entry in directory. */
  2233. err = indx_find(indx, ni, root, fname, fname_full_size(fname), sbi,
  2234. &diff, &e, fnd);
  2235. if (err)
  2236. goto out;
  2237. if (!e) {
  2238. err = -EINVAL;
  2239. goto out;
  2240. }
  2241. if (diff) {
  2242. err = -EINVAL;
  2243. goto out;
  2244. }
  2245. e_fname = (struct ATTR_FILE_NAME *)(e + 1);
  2246. if (!memcmp(&e_fname->dup, dup, sizeof(*dup))) {
  2247. /*
  2248. * Nothing to update in index! Try to avoid this call.
  2249. */
  2250. goto out;
  2251. }
  2252. memcpy(&e_fname->dup, dup, sizeof(*dup));
  2253. if (fnd->level) {
  2254. /* Directory entry in index. */
  2255. err = indx_write(indx, ni, fnd->nodes[fnd->level - 1], sync);
  2256. } else {
  2257. /* Directory entry in directory MFT record. */
  2258. mi->dirty = true;
  2259. if (sync)
  2260. err = mi_write(mi, 1);
  2261. else
  2262. mark_inode_dirty(&ni->vfs_inode);
  2263. }
  2264. out:
  2265. fnd_put(fnd);
  2266. return err;
  2267. }