tnc.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Adrian Hunter
  8. * Artem Bityutskiy (Битюцкий Артём)
  9. */
  10. /*
  11. * This file implements TNC (Tree Node Cache) which caches indexing nodes of
  12. * the UBIFS B-tree.
  13. *
  14. * At the moment the locking rules of the TNC tree are quite simple and
  15. * straightforward. We just have a mutex and lock it when we traverse the
  16. * tree. If a znode is not in memory, we read it from flash while still having
  17. * the mutex locked.
  18. */
  19. #include <linux/crc32.h>
  20. #include <linux/slab.h>
  21. #include "ubifs.h"
  22. static int try_read_node(const struct ubifs_info *c, void *buf, int type,
  23. struct ubifs_zbranch *zbr);
  24. static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
  25. struct ubifs_zbranch *zbr, void *node);
  26. /*
  27. * Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
  28. * @NAME_LESS: name corresponding to the first argument is less than second
  29. * @NAME_MATCHES: names match
  30. * @NAME_GREATER: name corresponding to the second argument is greater than
  31. * first
  32. * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
  33. *
  34. * These constants were introduce to improve readability.
  35. */
  36. enum {
  37. NAME_LESS = 0,
  38. NAME_MATCHES = 1,
  39. NAME_GREATER = 2,
  40. NOT_ON_MEDIA = 3,
  41. };
  42. static void do_insert_old_idx(struct ubifs_info *c,
  43. struct ubifs_old_idx *old_idx)
  44. {
  45. struct ubifs_old_idx *o;
  46. struct rb_node **p, *parent = NULL;
  47. p = &c->old_idx.rb_node;
  48. while (*p) {
  49. parent = *p;
  50. o = rb_entry(parent, struct ubifs_old_idx, rb);
  51. if (old_idx->lnum < o->lnum)
  52. p = &(*p)->rb_left;
  53. else if (old_idx->lnum > o->lnum)
  54. p = &(*p)->rb_right;
  55. else if (old_idx->offs < o->offs)
  56. p = &(*p)->rb_left;
  57. else if (old_idx->offs > o->offs)
  58. p = &(*p)->rb_right;
  59. else {
  60. ubifs_err(c, "old idx added twice!");
  61. kfree(old_idx);
  62. }
  63. }
  64. rb_link_node(&old_idx->rb, parent, p);
  65. rb_insert_color(&old_idx->rb, &c->old_idx);
  66. }
  67. /**
  68. * insert_old_idx - record an index node obsoleted since the last commit start.
  69. * @c: UBIFS file-system description object
  70. * @lnum: LEB number of obsoleted index node
  71. * @offs: offset of obsoleted index node
  72. *
  73. * Returns %0 on success, and a negative error code on failure.
  74. *
  75. * For recovery, there must always be a complete intact version of the index on
  76. * flash at all times. That is called the "old index". It is the index as at the
  77. * time of the last successful commit. Many of the index nodes in the old index
  78. * may be dirty, but they must not be erased until the next successful commit
  79. * (at which point that index becomes the old index).
  80. *
  81. * That means that the garbage collection and the in-the-gaps method of
  82. * committing must be able to determine if an index node is in the old index.
  83. * Most of the old index nodes can be found by looking up the TNC using the
  84. * 'lookup_znode()' function. However, some of the old index nodes may have
  85. * been deleted from the current index or may have been changed so much that
  86. * they cannot be easily found. In those cases, an entry is added to an RB-tree.
  87. * That is what this function does. The RB-tree is ordered by LEB number and
  88. * offset because they uniquely identify the old index node.
  89. */
  90. static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
  91. {
  92. struct ubifs_old_idx *old_idx;
  93. old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
  94. if (unlikely(!old_idx))
  95. return -ENOMEM;
  96. old_idx->lnum = lnum;
  97. old_idx->offs = offs;
  98. do_insert_old_idx(c, old_idx);
  99. return 0;
  100. }
  101. /**
  102. * insert_old_idx_znode - record a znode obsoleted since last commit start.
  103. * @c: UBIFS file-system description object
  104. * @znode: znode of obsoleted index node
  105. *
  106. * Returns %0 on success, and a negative error code on failure.
  107. */
  108. int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
  109. {
  110. if (znode->parent) {
  111. struct ubifs_zbranch *zbr;
  112. zbr = &znode->parent->zbranch[znode->iip];
  113. if (zbr->len)
  114. return insert_old_idx(c, zbr->lnum, zbr->offs);
  115. } else
  116. if (c->zroot.len)
  117. return insert_old_idx(c, c->zroot.lnum,
  118. c->zroot.offs);
  119. return 0;
  120. }
  121. /**
  122. * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
  123. * @c: UBIFS file-system description object
  124. * @znode: znode of obsoleted index node
  125. *
  126. * Returns %0 on success, and a negative error code on failure.
  127. */
  128. static int ins_clr_old_idx_znode(struct ubifs_info *c,
  129. struct ubifs_znode *znode)
  130. {
  131. int err;
  132. if (znode->parent) {
  133. struct ubifs_zbranch *zbr;
  134. zbr = &znode->parent->zbranch[znode->iip];
  135. if (zbr->len) {
  136. err = insert_old_idx(c, zbr->lnum, zbr->offs);
  137. if (err)
  138. return err;
  139. zbr->lnum = 0;
  140. zbr->offs = 0;
  141. zbr->len = 0;
  142. }
  143. } else
  144. if (c->zroot.len) {
  145. err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
  146. if (err)
  147. return err;
  148. c->zroot.lnum = 0;
  149. c->zroot.offs = 0;
  150. c->zroot.len = 0;
  151. }
  152. return 0;
  153. }
  154. /**
  155. * destroy_old_idx - destroy the old_idx RB-tree.
  156. * @c: UBIFS file-system description object
  157. *
  158. * During start commit, the old_idx RB-tree is used to avoid overwriting index
  159. * nodes that were in the index last commit but have since been deleted. This
  160. * is necessary for recovery i.e. the old index must be kept intact until the
  161. * new index is successfully written. The old-idx RB-tree is used for the
  162. * in-the-gaps method of writing index nodes and is destroyed every commit.
  163. */
  164. void destroy_old_idx(struct ubifs_info *c)
  165. {
  166. struct ubifs_old_idx *old_idx, *n;
  167. rbtree_postorder_for_each_entry_safe(old_idx, n, &c->old_idx, rb)
  168. kfree(old_idx);
  169. c->old_idx = RB_ROOT;
  170. }
  171. /**
  172. * copy_znode - copy a dirty znode.
  173. * @c: UBIFS file-system description object
  174. * @znode: znode to copy
  175. *
  176. * A dirty znode being committed may not be changed, so it is copied.
  177. */
  178. static struct ubifs_znode *copy_znode(struct ubifs_info *c,
  179. struct ubifs_znode *znode)
  180. {
  181. struct ubifs_znode *zn;
  182. zn = kmemdup(znode, c->max_znode_sz, GFP_NOFS);
  183. if (unlikely(!zn))
  184. return ERR_PTR(-ENOMEM);
  185. zn->cnext = NULL;
  186. __set_bit(DIRTY_ZNODE, &zn->flags);
  187. __clear_bit(COW_ZNODE, &zn->flags);
  188. return zn;
  189. }
  190. /**
  191. * add_idx_dirt - add dirt due to a dirty znode.
  192. * @c: UBIFS file-system description object
  193. * @lnum: LEB number of index node
  194. * @dirt: size of index node
  195. *
  196. * This function updates lprops dirty space and the new size of the index.
  197. */
  198. static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
  199. {
  200. c->calc_idx_sz -= ALIGN(dirt, 8);
  201. return ubifs_add_dirt(c, lnum, dirt);
  202. }
  203. /**
  204. * replace_znode - replace old znode with new znode.
  205. * @c: UBIFS file-system description object
  206. * @new_zn: new znode
  207. * @old_zn: old znode
  208. * @zbr: the branch of parent znode
  209. *
  210. * Replace old znode with new znode in TNC.
  211. */
  212. static void replace_znode(struct ubifs_info *c, struct ubifs_znode *new_zn,
  213. struct ubifs_znode *old_zn, struct ubifs_zbranch *zbr)
  214. {
  215. ubifs_assert(c, !ubifs_zn_obsolete(old_zn));
  216. __set_bit(OBSOLETE_ZNODE, &old_zn->flags);
  217. if (old_zn->level != 0) {
  218. int i;
  219. const int n = new_zn->child_cnt;
  220. /* The children now have new parent */
  221. for (i = 0; i < n; i++) {
  222. struct ubifs_zbranch *child = &new_zn->zbranch[i];
  223. if (child->znode)
  224. child->znode->parent = new_zn;
  225. }
  226. }
  227. zbr->znode = new_zn;
  228. zbr->lnum = 0;
  229. zbr->offs = 0;
  230. zbr->len = 0;
  231. atomic_long_inc(&c->dirty_zn_cnt);
  232. }
  233. /**
  234. * dirty_cow_znode - ensure a znode is not being committed.
  235. * @c: UBIFS file-system description object
  236. * @zbr: branch of znode to check
  237. *
  238. * Returns dirtied znode on success or negative error code on failure.
  239. */
  240. static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
  241. struct ubifs_zbranch *zbr)
  242. {
  243. struct ubifs_znode *znode = zbr->znode;
  244. struct ubifs_znode *zn;
  245. int err;
  246. if (!ubifs_zn_cow(znode)) {
  247. /* znode is not being committed */
  248. if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
  249. atomic_long_inc(&c->dirty_zn_cnt);
  250. atomic_long_dec(&c->clean_zn_cnt);
  251. atomic_long_dec(&ubifs_clean_zn_cnt);
  252. err = add_idx_dirt(c, zbr->lnum, zbr->len);
  253. if (unlikely(err))
  254. return ERR_PTR(err);
  255. }
  256. return znode;
  257. }
  258. zn = copy_znode(c, znode);
  259. if (IS_ERR(zn))
  260. return zn;
  261. if (zbr->len) {
  262. struct ubifs_old_idx *old_idx;
  263. old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
  264. if (unlikely(!old_idx)) {
  265. err = -ENOMEM;
  266. goto out;
  267. }
  268. old_idx->lnum = zbr->lnum;
  269. old_idx->offs = zbr->offs;
  270. err = add_idx_dirt(c, zbr->lnum, zbr->len);
  271. if (err) {
  272. kfree(old_idx);
  273. goto out;
  274. }
  275. do_insert_old_idx(c, old_idx);
  276. }
  277. replace_znode(c, zn, znode, zbr);
  278. return zn;
  279. out:
  280. kfree(zn);
  281. return ERR_PTR(err);
  282. }
  283. /**
  284. * lnc_add - add a leaf node to the leaf node cache.
  285. * @c: UBIFS file-system description object
  286. * @zbr: zbranch of leaf node
  287. * @node: leaf node
  288. *
  289. * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
  290. * purpose of the leaf node cache is to save re-reading the same leaf node over
  291. * and over again. Most things are cached by VFS, however the file system must
  292. * cache directory entries for readdir and for resolving hash collisions. The
  293. * present implementation of the leaf node cache is extremely simple, and
  294. * allows for error returns that are not used but that may be needed if a more
  295. * complex implementation is created.
  296. *
  297. * Note, this function does not add the @node object to LNC directly, but
  298. * allocates a copy of the object and adds the copy to LNC. The reason for this
  299. * is that @node has been allocated outside of the TNC subsystem and will be
  300. * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
  301. * may be changed at any time, e.g. freed by the shrinker.
  302. */
  303. static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  304. const void *node)
  305. {
  306. int err;
  307. void *lnc_node;
  308. const struct ubifs_dent_node *dent = node;
  309. ubifs_assert(c, !zbr->leaf);
  310. ubifs_assert(c, zbr->len != 0);
  311. ubifs_assert(c, is_hash_key(c, &zbr->key));
  312. err = ubifs_validate_entry(c, dent);
  313. if (err) {
  314. dump_stack();
  315. ubifs_dump_node(c, dent, zbr->len);
  316. return err;
  317. }
  318. lnc_node = kmemdup(node, zbr->len, GFP_NOFS);
  319. if (!lnc_node)
  320. /* We don't have to have the cache, so no error */
  321. return 0;
  322. zbr->leaf = lnc_node;
  323. return 0;
  324. }
  325. /**
  326. * lnc_add_directly - add a leaf node to the leaf-node-cache.
  327. * @c: UBIFS file-system description object
  328. * @zbr: zbranch of leaf node
  329. * @node: leaf node
  330. *
  331. * This function is similar to 'lnc_add()', but it does not create a copy of
  332. * @node but inserts @node to TNC directly.
  333. */
  334. static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  335. void *node)
  336. {
  337. int err;
  338. ubifs_assert(c, !zbr->leaf);
  339. ubifs_assert(c, zbr->len != 0);
  340. err = ubifs_validate_entry(c, node);
  341. if (err) {
  342. dump_stack();
  343. ubifs_dump_node(c, node, zbr->len);
  344. return err;
  345. }
  346. zbr->leaf = node;
  347. return 0;
  348. }
  349. /**
  350. * lnc_free - remove a leaf node from the leaf node cache.
  351. * @zbr: zbranch of leaf node
  352. */
  353. static void lnc_free(struct ubifs_zbranch *zbr)
  354. {
  355. if (!zbr->leaf)
  356. return;
  357. kfree(zbr->leaf);
  358. zbr->leaf = NULL;
  359. }
  360. /**
  361. * tnc_read_hashed_node - read a "hashed" leaf node.
  362. * @c: UBIFS file-system description object
  363. * @zbr: key and position of the node
  364. * @node: node is returned here
  365. *
  366. * This function reads a "hashed" node defined by @zbr from the leaf node cache
  367. * (in it is there) or from the hash media, in which case the node is also
  368. * added to LNC. Returns zero in case of success or a negative error
  369. * code in case of failure.
  370. */
  371. static int tnc_read_hashed_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  372. void *node)
  373. {
  374. int err;
  375. ubifs_assert(c, is_hash_key(c, &zbr->key));
  376. if (zbr->leaf) {
  377. /* Read from the leaf node cache */
  378. ubifs_assert(c, zbr->len != 0);
  379. memcpy(node, zbr->leaf, zbr->len);
  380. return 0;
  381. }
  382. if (c->replaying) {
  383. err = fallible_read_node(c, &zbr->key, zbr, node);
  384. /*
  385. * When the node was not found, return -ENOENT, 0 otherwise.
  386. * Negative return codes stay as-is.
  387. */
  388. if (err == 0)
  389. err = -ENOENT;
  390. else if (err == 1)
  391. err = 0;
  392. } else {
  393. err = ubifs_tnc_read_node(c, zbr, node);
  394. }
  395. if (err)
  396. return err;
  397. /* Add the node to the leaf node cache */
  398. err = lnc_add(c, zbr, node);
  399. return err;
  400. }
  401. /**
  402. * try_read_node - read a node if it is a node.
  403. * @c: UBIFS file-system description object
  404. * @buf: buffer to read to
  405. * @type: node type
  406. * @zbr: the zbranch describing the node to read
  407. *
  408. * This function tries to read a node of known type and length, checks it and
  409. * stores it in @buf. This function returns %1 if a node is present and %0 if
  410. * a node is not present. A negative error code is returned for I/O errors.
  411. * This function performs that same function as ubifs_read_node except that
  412. * it does not require that there is actually a node present and instead
  413. * the return code indicates if a node was read.
  414. *
  415. * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
  416. * is true (it is controlled by corresponding mount option). However, if
  417. * @c->mounting or @c->remounting_rw is true (we are mounting or re-mounting to
  418. * R/W mode), @c->no_chk_data_crc is ignored and CRC is checked. This is
  419. * because during mounting or re-mounting from R/O mode to R/W mode we may read
  420. * journal nodes (when replying the journal or doing the recovery) and the
  421. * journal nodes may potentially be corrupted, so checking is required.
  422. */
  423. static int try_read_node(const struct ubifs_info *c, void *buf, int type,
  424. struct ubifs_zbranch *zbr)
  425. {
  426. int len = zbr->len;
  427. int lnum = zbr->lnum;
  428. int offs = zbr->offs;
  429. int err, node_len;
  430. struct ubifs_ch *ch = buf;
  431. uint32_t crc, node_crc;
  432. dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
  433. err = ubifs_leb_read(c, lnum, buf, offs, len, 1);
  434. if (err) {
  435. ubifs_err(c, "cannot read node type %d from LEB %d:%d, error %d",
  436. type, lnum, offs, err);
  437. return err;
  438. }
  439. if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
  440. return 0;
  441. if (ch->node_type != type)
  442. return 0;
  443. node_len = le32_to_cpu(ch->len);
  444. if (node_len != len)
  445. return 0;
  446. if (type != UBIFS_DATA_NODE || !c->no_chk_data_crc || c->mounting ||
  447. c->remounting_rw) {
  448. crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
  449. node_crc = le32_to_cpu(ch->crc);
  450. if (crc != node_crc)
  451. return 0;
  452. }
  453. err = ubifs_node_check_hash(c, buf, zbr->hash);
  454. if (err) {
  455. ubifs_bad_hash(c, buf, zbr->hash, lnum, offs);
  456. return 0;
  457. }
  458. return 1;
  459. }
  460. /**
  461. * fallible_read_node - try to read a leaf node.
  462. * @c: UBIFS file-system description object
  463. * @key: key of node to read
  464. * @zbr: position of node
  465. * @node: node returned
  466. *
  467. * This function tries to read a node and returns %1 if the node is read, %0
  468. * if the node is not present, and a negative error code in the case of error.
  469. */
  470. static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
  471. struct ubifs_zbranch *zbr, void *node)
  472. {
  473. int ret;
  474. dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs);
  475. ret = try_read_node(c, node, key_type(c, key), zbr);
  476. if (ret == 1) {
  477. union ubifs_key node_key;
  478. struct ubifs_dent_node *dent = node;
  479. /* All nodes have key in the same place */
  480. key_read(c, &dent->key, &node_key);
  481. if (keys_cmp(c, key, &node_key) != 0)
  482. ret = 0;
  483. }
  484. if (ret == 0 && c->replaying)
  485. dbg_mntk(key, "dangling branch LEB %d:%d len %d, key ",
  486. zbr->lnum, zbr->offs, zbr->len);
  487. return ret;
  488. }
  489. /**
  490. * matches_name - determine if a direntry or xattr entry matches a given name.
  491. * @c: UBIFS file-system description object
  492. * @zbr: zbranch of dent
  493. * @nm: name to match
  494. *
  495. * This function checks if xentry/direntry referred by zbranch @zbr matches name
  496. * @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
  497. * @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
  498. * of failure, a negative error code is returned.
  499. */
  500. static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
  501. const struct fscrypt_name *nm)
  502. {
  503. struct ubifs_dent_node *dent;
  504. int nlen, err;
  505. /* If possible, match against the dent in the leaf node cache */
  506. if (!zbr->leaf) {
  507. dent = kmalloc(zbr->len, GFP_NOFS);
  508. if (!dent)
  509. return -ENOMEM;
  510. err = ubifs_tnc_read_node(c, zbr, dent);
  511. if (err)
  512. goto out_free;
  513. /* Add the node to the leaf node cache */
  514. err = lnc_add_directly(c, zbr, dent);
  515. if (err)
  516. goto out_free;
  517. } else
  518. dent = zbr->leaf;
  519. nlen = le16_to_cpu(dent->nlen);
  520. err = memcmp(dent->name, fname_name(nm), min_t(int, nlen, fname_len(nm)));
  521. if (err == 0) {
  522. if (nlen == fname_len(nm))
  523. return NAME_MATCHES;
  524. else if (nlen < fname_len(nm))
  525. return NAME_LESS;
  526. else
  527. return NAME_GREATER;
  528. } else if (err < 0)
  529. return NAME_LESS;
  530. else
  531. return NAME_GREATER;
  532. out_free:
  533. kfree(dent);
  534. return err;
  535. }
  536. /**
  537. * get_znode - get a TNC znode that may not be loaded yet.
  538. * @c: UBIFS file-system description object
  539. * @znode: parent znode
  540. * @n: znode branch slot number
  541. *
  542. * This function returns the znode or a negative error code.
  543. */
  544. static struct ubifs_znode *get_znode(struct ubifs_info *c,
  545. struct ubifs_znode *znode, int n)
  546. {
  547. struct ubifs_zbranch *zbr;
  548. zbr = &znode->zbranch[n];
  549. if (zbr->znode)
  550. znode = zbr->znode;
  551. else
  552. znode = ubifs_load_znode(c, zbr, znode, n);
  553. return znode;
  554. }
  555. /**
  556. * tnc_next - find next TNC entry.
  557. * @c: UBIFS file-system description object
  558. * @zn: znode is passed and returned here
  559. * @n: znode branch slot number is passed and returned here
  560. *
  561. * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
  562. * no next entry, or a negative error code otherwise.
  563. */
  564. static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
  565. {
  566. struct ubifs_znode *znode = *zn;
  567. int nn = *n;
  568. nn += 1;
  569. if (nn < znode->child_cnt) {
  570. *n = nn;
  571. return 0;
  572. }
  573. while (1) {
  574. struct ubifs_znode *zp;
  575. zp = znode->parent;
  576. if (!zp)
  577. return -ENOENT;
  578. nn = znode->iip + 1;
  579. znode = zp;
  580. if (nn < znode->child_cnt) {
  581. znode = get_znode(c, znode, nn);
  582. if (IS_ERR(znode))
  583. return PTR_ERR(znode);
  584. while (znode->level != 0) {
  585. znode = get_znode(c, znode, 0);
  586. if (IS_ERR(znode))
  587. return PTR_ERR(znode);
  588. }
  589. nn = 0;
  590. break;
  591. }
  592. }
  593. *zn = znode;
  594. *n = nn;
  595. return 0;
  596. }
  597. /**
  598. * tnc_prev - find previous TNC entry.
  599. * @c: UBIFS file-system description object
  600. * @zn: znode is returned here
  601. * @n: znode branch slot number is passed and returned here
  602. *
  603. * This function returns %0 if the previous TNC entry is found, %-ENOENT if
  604. * there is no next entry, or a negative error code otherwise.
  605. */
  606. static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
  607. {
  608. struct ubifs_znode *znode = *zn;
  609. int nn = *n;
  610. if (nn > 0) {
  611. *n = nn - 1;
  612. return 0;
  613. }
  614. while (1) {
  615. struct ubifs_znode *zp;
  616. zp = znode->parent;
  617. if (!zp)
  618. return -ENOENT;
  619. nn = znode->iip - 1;
  620. znode = zp;
  621. if (nn >= 0) {
  622. znode = get_znode(c, znode, nn);
  623. if (IS_ERR(znode))
  624. return PTR_ERR(znode);
  625. while (znode->level != 0) {
  626. nn = znode->child_cnt - 1;
  627. znode = get_znode(c, znode, nn);
  628. if (IS_ERR(znode))
  629. return PTR_ERR(znode);
  630. }
  631. nn = znode->child_cnt - 1;
  632. break;
  633. }
  634. }
  635. *zn = znode;
  636. *n = nn;
  637. return 0;
  638. }
  639. /**
  640. * resolve_collision - resolve a collision.
  641. * @c: UBIFS file-system description object
  642. * @key: key of a directory or extended attribute entry
  643. * @zn: znode is returned here
  644. * @n: zbranch number is passed and returned here
  645. * @nm: name of the entry
  646. *
  647. * This function is called for "hashed" keys to make sure that the found key
  648. * really corresponds to the looked up node (directory or extended attribute
  649. * entry). It returns %1 and sets @zn and @n if the collision is resolved.
  650. * %0 is returned if @nm is not found and @zn and @n are set to the previous
  651. * entry, i.e. to the entry after which @nm could follow if it were in TNC.
  652. * This means that @n may be set to %-1 if the leftmost key in @zn is the
  653. * previous one. A negative error code is returned on failures.
  654. */
  655. static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
  656. struct ubifs_znode **zn, int *n,
  657. const struct fscrypt_name *nm)
  658. {
  659. int err;
  660. err = matches_name(c, &(*zn)->zbranch[*n], nm);
  661. if (unlikely(err < 0))
  662. return err;
  663. if (err == NAME_MATCHES)
  664. return 1;
  665. if (err == NAME_GREATER) {
  666. /* Look left */
  667. while (1) {
  668. err = tnc_prev(c, zn, n);
  669. if (err == -ENOENT) {
  670. ubifs_assert(c, *n == 0);
  671. *n = -1;
  672. return 0;
  673. }
  674. if (err < 0)
  675. return err;
  676. if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
  677. /*
  678. * We have found the branch after which we would
  679. * like to insert, but inserting in this znode
  680. * may still be wrong. Consider the following 3
  681. * znodes, in the case where we are resolving a
  682. * collision with Key2.
  683. *
  684. * znode zp
  685. * ----------------------
  686. * level 1 | Key0 | Key1 |
  687. * -----------------------
  688. * | |
  689. * znode za | | znode zb
  690. * ------------ ------------
  691. * level 0 | Key0 | | Key2 |
  692. * ------------ ------------
  693. *
  694. * The lookup finds Key2 in znode zb. Lets say
  695. * there is no match and the name is greater so
  696. * we look left. When we find Key0, we end up
  697. * here. If we return now, we will insert into
  698. * znode za at slot n = 1. But that is invalid
  699. * according to the parent's keys. Key2 must
  700. * be inserted into znode zb.
  701. *
  702. * Note, this problem is not relevant for the
  703. * case when we go right, because
  704. * 'tnc_insert()' would correct the parent key.
  705. */
  706. if (*n == (*zn)->child_cnt - 1) {
  707. err = tnc_next(c, zn, n);
  708. if (err) {
  709. /* Should be impossible */
  710. ubifs_assert(c, 0);
  711. if (err == -ENOENT)
  712. err = -EINVAL;
  713. return err;
  714. }
  715. ubifs_assert(c, *n == 0);
  716. *n = -1;
  717. }
  718. return 0;
  719. }
  720. err = matches_name(c, &(*zn)->zbranch[*n], nm);
  721. if (err < 0)
  722. return err;
  723. if (err == NAME_LESS)
  724. return 0;
  725. if (err == NAME_MATCHES)
  726. return 1;
  727. ubifs_assert(c, err == NAME_GREATER);
  728. }
  729. } else {
  730. int nn = *n;
  731. struct ubifs_znode *znode = *zn;
  732. /* Look right */
  733. while (1) {
  734. err = tnc_next(c, &znode, &nn);
  735. if (err == -ENOENT)
  736. return 0;
  737. if (err < 0)
  738. return err;
  739. if (keys_cmp(c, &znode->zbranch[nn].key, key))
  740. return 0;
  741. err = matches_name(c, &znode->zbranch[nn], nm);
  742. if (err < 0)
  743. return err;
  744. if (err == NAME_GREATER)
  745. return 0;
  746. *zn = znode;
  747. *n = nn;
  748. if (err == NAME_MATCHES)
  749. return 1;
  750. ubifs_assert(c, err == NAME_LESS);
  751. }
  752. }
  753. }
  754. /**
  755. * fallible_matches_name - determine if a dent matches a given name.
  756. * @c: UBIFS file-system description object
  757. * @zbr: zbranch of dent
  758. * @nm: name to match
  759. *
  760. * This is a "fallible" version of 'matches_name()' function which does not
  761. * panic if the direntry/xentry referred by @zbr does not exist on the media.
  762. *
  763. * This function checks if xentry/direntry referred by zbranch @zbr matches name
  764. * @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
  765. * is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
  766. * if xentry/direntry referred by @zbr does not exist on the media. A negative
  767. * error code is returned in case of failure.
  768. */
  769. static int fallible_matches_name(struct ubifs_info *c,
  770. struct ubifs_zbranch *zbr,
  771. const struct fscrypt_name *nm)
  772. {
  773. struct ubifs_dent_node *dent;
  774. int nlen, err;
  775. /* If possible, match against the dent in the leaf node cache */
  776. if (!zbr->leaf) {
  777. dent = kmalloc(zbr->len, GFP_NOFS);
  778. if (!dent)
  779. return -ENOMEM;
  780. err = fallible_read_node(c, &zbr->key, zbr, dent);
  781. if (err < 0)
  782. goto out_free;
  783. if (err == 0) {
  784. /* The node was not present */
  785. err = NOT_ON_MEDIA;
  786. goto out_free;
  787. }
  788. ubifs_assert(c, err == 1);
  789. err = lnc_add_directly(c, zbr, dent);
  790. if (err)
  791. goto out_free;
  792. } else
  793. dent = zbr->leaf;
  794. nlen = le16_to_cpu(dent->nlen);
  795. err = memcmp(dent->name, fname_name(nm), min_t(int, nlen, fname_len(nm)));
  796. if (err == 0) {
  797. if (nlen == fname_len(nm))
  798. return NAME_MATCHES;
  799. else if (nlen < fname_len(nm))
  800. return NAME_LESS;
  801. else
  802. return NAME_GREATER;
  803. } else if (err < 0)
  804. return NAME_LESS;
  805. else
  806. return NAME_GREATER;
  807. out_free:
  808. kfree(dent);
  809. return err;
  810. }
  811. /**
  812. * fallible_resolve_collision - resolve a collision even if nodes are missing.
  813. * @c: UBIFS file-system description object
  814. * @key: key
  815. * @zn: znode is returned here
  816. * @n: branch number is passed and returned here
  817. * @nm: name of directory entry
  818. * @adding: indicates caller is adding a key to the TNC
  819. *
  820. * This is a "fallible" version of the 'resolve_collision()' function which
  821. * does not panic if one of the nodes referred to by TNC does not exist on the
  822. * media. This may happen when replaying the journal if a deleted node was
  823. * Garbage-collected and the commit was not done. A branch that refers to a node
  824. * that is not present is called a dangling branch. The following are the return
  825. * codes for this function:
  826. * o if @nm was found, %1 is returned and @zn and @n are set to the found
  827. * branch;
  828. * o if we are @adding and @nm was not found, %0 is returned;
  829. * o if we are not @adding and @nm was not found, but a dangling branch was
  830. * found, then %1 is returned and @zn and @n are set to the dangling branch;
  831. * o a negative error code is returned in case of failure.
  832. */
  833. static int fallible_resolve_collision(struct ubifs_info *c,
  834. const union ubifs_key *key,
  835. struct ubifs_znode **zn, int *n,
  836. const struct fscrypt_name *nm,
  837. int adding)
  838. {
  839. struct ubifs_znode *o_znode = NULL, *znode = *zn;
  840. int o_n, err, cmp, unsure = 0, nn = *n;
  841. cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
  842. if (unlikely(cmp < 0))
  843. return cmp;
  844. if (cmp == NAME_MATCHES)
  845. return 1;
  846. if (cmp == NOT_ON_MEDIA) {
  847. o_znode = znode;
  848. o_n = nn;
  849. /*
  850. * We are unlucky and hit a dangling branch straight away.
  851. * Now we do not really know where to go to find the needed
  852. * branch - to the left or to the right. Well, let's try left.
  853. */
  854. unsure = 1;
  855. } else if (!adding)
  856. unsure = 1; /* Remove a dangling branch wherever it is */
  857. if (cmp == NAME_GREATER || unsure) {
  858. /* Look left */
  859. while (1) {
  860. err = tnc_prev(c, zn, n);
  861. if (err == -ENOENT) {
  862. ubifs_assert(c, *n == 0);
  863. *n = -1;
  864. break;
  865. }
  866. if (err < 0)
  867. return err;
  868. if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
  869. /* See comments in 'resolve_collision()' */
  870. if (*n == (*zn)->child_cnt - 1) {
  871. err = tnc_next(c, zn, n);
  872. if (err) {
  873. /* Should be impossible */
  874. ubifs_assert(c, 0);
  875. if (err == -ENOENT)
  876. err = -EINVAL;
  877. return err;
  878. }
  879. ubifs_assert(c, *n == 0);
  880. *n = -1;
  881. }
  882. break;
  883. }
  884. err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
  885. if (err < 0)
  886. return err;
  887. if (err == NAME_MATCHES)
  888. return 1;
  889. if (err == NOT_ON_MEDIA) {
  890. o_znode = *zn;
  891. o_n = *n;
  892. continue;
  893. }
  894. if (!adding)
  895. continue;
  896. if (err == NAME_LESS)
  897. break;
  898. else
  899. unsure = 0;
  900. }
  901. }
  902. if (cmp == NAME_LESS || unsure) {
  903. /* Look right */
  904. *zn = znode;
  905. *n = nn;
  906. while (1) {
  907. err = tnc_next(c, &znode, &nn);
  908. if (err == -ENOENT)
  909. break;
  910. if (err < 0)
  911. return err;
  912. if (keys_cmp(c, &znode->zbranch[nn].key, key))
  913. break;
  914. err = fallible_matches_name(c, &znode->zbranch[nn], nm);
  915. if (err < 0)
  916. return err;
  917. if (err == NAME_GREATER)
  918. break;
  919. *zn = znode;
  920. *n = nn;
  921. if (err == NAME_MATCHES)
  922. return 1;
  923. if (err == NOT_ON_MEDIA) {
  924. o_znode = znode;
  925. o_n = nn;
  926. }
  927. }
  928. }
  929. /* Never match a dangling branch when adding */
  930. if (adding || !o_znode)
  931. return 0;
  932. dbg_mntk(key, "dangling match LEB %d:%d len %d key ",
  933. o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
  934. o_znode->zbranch[o_n].len);
  935. *zn = o_znode;
  936. *n = o_n;
  937. return 1;
  938. }
  939. /**
  940. * matches_position - determine if a zbranch matches a given position.
  941. * @zbr: zbranch of dent
  942. * @lnum: LEB number of dent to match
  943. * @offs: offset of dent to match
  944. *
  945. * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
  946. */
  947. static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
  948. {
  949. if (zbr->lnum == lnum && zbr->offs == offs)
  950. return 1;
  951. else
  952. return 0;
  953. }
  954. /**
  955. * resolve_collision_directly - resolve a collision directly.
  956. * @c: UBIFS file-system description object
  957. * @key: key of directory entry
  958. * @zn: znode is passed and returned here
  959. * @n: zbranch number is passed and returned here
  960. * @lnum: LEB number of dent node to match
  961. * @offs: offset of dent node to match
  962. *
  963. * This function is used for "hashed" keys to make sure the found directory or
  964. * extended attribute entry node is what was looked for. It is used when the
  965. * flash address of the right node is known (@lnum:@offs) which makes it much
  966. * easier to resolve collisions (no need to read entries and match full
  967. * names). This function returns %1 and sets @zn and @n if the collision is
  968. * resolved, %0 if @lnum:@offs is not found and @zn and @n are set to the
  969. * previous directory entry. Otherwise a negative error code is returned.
  970. */
  971. static int resolve_collision_directly(struct ubifs_info *c,
  972. const union ubifs_key *key,
  973. struct ubifs_znode **zn, int *n,
  974. int lnum, int offs)
  975. {
  976. struct ubifs_znode *znode;
  977. int nn, err;
  978. znode = *zn;
  979. nn = *n;
  980. if (matches_position(&znode->zbranch[nn], lnum, offs))
  981. return 1;
  982. /* Look left */
  983. while (1) {
  984. err = tnc_prev(c, &znode, &nn);
  985. if (err == -ENOENT)
  986. break;
  987. if (err < 0)
  988. return err;
  989. if (keys_cmp(c, &znode->zbranch[nn].key, key))
  990. break;
  991. if (matches_position(&znode->zbranch[nn], lnum, offs)) {
  992. *zn = znode;
  993. *n = nn;
  994. return 1;
  995. }
  996. }
  997. /* Look right */
  998. znode = *zn;
  999. nn = *n;
  1000. while (1) {
  1001. err = tnc_next(c, &znode, &nn);
  1002. if (err == -ENOENT)
  1003. return 0;
  1004. if (err < 0)
  1005. return err;
  1006. if (keys_cmp(c, &znode->zbranch[nn].key, key))
  1007. return 0;
  1008. *zn = znode;
  1009. *n = nn;
  1010. if (matches_position(&znode->zbranch[nn], lnum, offs))
  1011. return 1;
  1012. }
  1013. }
  1014. /**
  1015. * dirty_cow_bottom_up - dirty a znode and its ancestors.
  1016. * @c: UBIFS file-system description object
  1017. * @znode: znode to dirty
  1018. *
  1019. * If we do not have a unique key that resides in a znode, then we cannot
  1020. * dirty that znode from the top down (i.e. by using lookup_level0_dirty)
  1021. * This function records the path back to the last dirty ancestor, and then
  1022. * dirties the znodes on that path.
  1023. */
  1024. static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
  1025. struct ubifs_znode *znode)
  1026. {
  1027. struct ubifs_znode *zp;
  1028. int *path = c->bottom_up_buf, p = 0;
  1029. ubifs_assert(c, c->zroot.znode);
  1030. ubifs_assert(c, znode);
  1031. if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
  1032. kfree(c->bottom_up_buf);
  1033. c->bottom_up_buf = kmalloc_array(c->zroot.znode->level,
  1034. sizeof(int),
  1035. GFP_NOFS);
  1036. if (!c->bottom_up_buf)
  1037. return ERR_PTR(-ENOMEM);
  1038. path = c->bottom_up_buf;
  1039. }
  1040. if (c->zroot.znode->level) {
  1041. /* Go up until parent is dirty */
  1042. while (1) {
  1043. int n;
  1044. zp = znode->parent;
  1045. if (!zp)
  1046. break;
  1047. n = znode->iip;
  1048. ubifs_assert(c, p < c->zroot.znode->level);
  1049. path[p++] = n;
  1050. if (!zp->cnext && ubifs_zn_dirty(znode))
  1051. break;
  1052. znode = zp;
  1053. }
  1054. }
  1055. /* Come back down, dirtying as we go */
  1056. while (1) {
  1057. struct ubifs_zbranch *zbr;
  1058. zp = znode->parent;
  1059. if (zp) {
  1060. ubifs_assert(c, path[p - 1] >= 0);
  1061. ubifs_assert(c, path[p - 1] < zp->child_cnt);
  1062. zbr = &zp->zbranch[path[--p]];
  1063. znode = dirty_cow_znode(c, zbr);
  1064. } else {
  1065. ubifs_assert(c, znode == c->zroot.znode);
  1066. znode = dirty_cow_znode(c, &c->zroot);
  1067. }
  1068. if (IS_ERR(znode) || !p)
  1069. break;
  1070. ubifs_assert(c, path[p - 1] >= 0);
  1071. ubifs_assert(c, path[p - 1] < znode->child_cnt);
  1072. znode = znode->zbranch[path[p - 1]].znode;
  1073. }
  1074. return znode;
  1075. }
  1076. /**
  1077. * ubifs_lookup_level0 - search for zero-level znode.
  1078. * @c: UBIFS file-system description object
  1079. * @key: key to lookup
  1080. * @zn: znode is returned here
  1081. * @n: znode branch slot number is returned here
  1082. *
  1083. * This function looks up the TNC tree and search for zero-level znode which
  1084. * refers key @key. The found zero-level znode is returned in @zn. There are 3
  1085. * cases:
  1086. * o exact match, i.e. the found zero-level znode contains key @key, then %1
  1087. * is returned and slot number of the matched branch is stored in @n;
  1088. * o not exact match, which means that zero-level znode does not contain
  1089. * @key, then %0 is returned and slot number of the closest branch or %-1
  1090. * is stored in @n; In this case calling tnc_next() is mandatory.
  1091. * o @key is so small that it is even less than the lowest key of the
  1092. * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
  1093. *
  1094. * Note, when the TNC tree is traversed, some znodes may be absent, then this
  1095. * function reads corresponding indexing nodes and inserts them to TNC. In
  1096. * case of failure, a negative error code is returned.
  1097. */
  1098. int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
  1099. struct ubifs_znode **zn, int *n)
  1100. {
  1101. int err, exact;
  1102. struct ubifs_znode *znode;
  1103. time64_t time = ktime_get_seconds();
  1104. dbg_tnck(key, "search key ");
  1105. ubifs_assert(c, key_type(c, key) < UBIFS_INVALID_KEY);
  1106. znode = c->zroot.znode;
  1107. if (unlikely(!znode)) {
  1108. znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
  1109. if (IS_ERR(znode))
  1110. return PTR_ERR(znode);
  1111. }
  1112. znode->time = time;
  1113. while (1) {
  1114. struct ubifs_zbranch *zbr;
  1115. exact = ubifs_search_zbranch(c, znode, key, n);
  1116. if (znode->level == 0)
  1117. break;
  1118. if (*n < 0)
  1119. *n = 0;
  1120. zbr = &znode->zbranch[*n];
  1121. if (zbr->znode) {
  1122. znode->time = time;
  1123. znode = zbr->znode;
  1124. continue;
  1125. }
  1126. /* znode is not in TNC cache, load it from the media */
  1127. znode = ubifs_load_znode(c, zbr, znode, *n);
  1128. if (IS_ERR(znode))
  1129. return PTR_ERR(znode);
  1130. }
  1131. *zn = znode;
  1132. if (exact || !is_hash_key(c, key) || *n != -1) {
  1133. dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
  1134. return exact;
  1135. }
  1136. /*
  1137. * Here is a tricky place. We have not found the key and this is a
  1138. * "hashed" key, which may collide. The rest of the code deals with
  1139. * situations like this:
  1140. *
  1141. * | 3 | 5 |
  1142. * / \
  1143. * | 3 | 5 | | 6 | 7 | (x)
  1144. *
  1145. * Or more a complex example:
  1146. *
  1147. * | 1 | 5 |
  1148. * / \
  1149. * | 1 | 3 | | 5 | 8 |
  1150. * \ /
  1151. * | 5 | 5 | | 6 | 7 | (x)
  1152. *
  1153. * In the examples, if we are looking for key "5", we may reach nodes
  1154. * marked with "(x)". In this case what we have do is to look at the
  1155. * left and see if there is "5" key there. If there is, we have to
  1156. * return it.
  1157. *
  1158. * Note, this whole situation is possible because we allow to have
  1159. * elements which are equivalent to the next key in the parent in the
  1160. * children of current znode. For example, this happens if we split a
  1161. * znode like this: | 3 | 5 | 5 | 6 | 7 |, which results in something
  1162. * like this:
  1163. * | 3 | 5 |
  1164. * / \
  1165. * | 3 | 5 | | 5 | 6 | 7 |
  1166. * ^
  1167. * And this becomes what is at the first "picture" after key "5" marked
  1168. * with "^" is removed. What could be done is we could prohibit
  1169. * splitting in the middle of the colliding sequence. Also, when
  1170. * removing the leftmost key, we would have to correct the key of the
  1171. * parent node, which would introduce additional complications. Namely,
  1172. * if we changed the leftmost key of the parent znode, the garbage
  1173. * collector would be unable to find it (GC is doing this when GC'ing
  1174. * indexing LEBs). Although we already have an additional RB-tree where
  1175. * we save such changed znodes (see 'ins_clr_old_idx_znode()') until
  1176. * after the commit. But anyway, this does not look easy to implement
  1177. * so we did not try this.
  1178. */
  1179. err = tnc_prev(c, &znode, n);
  1180. if (err == -ENOENT) {
  1181. dbg_tnc("found 0, lvl %d, n -1", znode->level);
  1182. *n = -1;
  1183. return 0;
  1184. }
  1185. if (unlikely(err < 0))
  1186. return err;
  1187. if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
  1188. dbg_tnc("found 0, lvl %d, n -1", znode->level);
  1189. *n = -1;
  1190. return 0;
  1191. }
  1192. dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
  1193. *zn = znode;
  1194. return 1;
  1195. }
  1196. /**
  1197. * lookup_level0_dirty - search for zero-level znode dirtying.
  1198. * @c: UBIFS file-system description object
  1199. * @key: key to lookup
  1200. * @zn: znode is returned here
  1201. * @n: znode branch slot number is returned here
  1202. *
  1203. * This function looks up the TNC tree and search for zero-level znode which
  1204. * refers key @key. The found zero-level znode is returned in @zn. There are 3
  1205. * cases:
  1206. * o exact match, i.e. the found zero-level znode contains key @key, then %1
  1207. * is returned and slot number of the matched branch is stored in @n;
  1208. * o not exact match, which means that zero-level znode does not contain @key
  1209. * then %0 is returned and slot number of the closed branch is stored in
  1210. * @n;
  1211. * o @key is so small that it is even less than the lowest key of the
  1212. * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
  1213. *
  1214. * Additionally all znodes in the path from the root to the located zero-level
  1215. * znode are marked as dirty.
  1216. *
  1217. * Note, when the TNC tree is traversed, some znodes may be absent, then this
  1218. * function reads corresponding indexing nodes and inserts them to TNC. In
  1219. * case of failure, a negative error code is returned.
  1220. */
  1221. static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
  1222. struct ubifs_znode **zn, int *n)
  1223. {
  1224. int err, exact;
  1225. struct ubifs_znode *znode;
  1226. time64_t time = ktime_get_seconds();
  1227. dbg_tnck(key, "search and dirty key ");
  1228. znode = c->zroot.znode;
  1229. if (unlikely(!znode)) {
  1230. znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
  1231. if (IS_ERR(znode))
  1232. return PTR_ERR(znode);
  1233. }
  1234. znode = dirty_cow_znode(c, &c->zroot);
  1235. if (IS_ERR(znode))
  1236. return PTR_ERR(znode);
  1237. znode->time = time;
  1238. while (1) {
  1239. struct ubifs_zbranch *zbr;
  1240. exact = ubifs_search_zbranch(c, znode, key, n);
  1241. if (znode->level == 0)
  1242. break;
  1243. if (*n < 0)
  1244. *n = 0;
  1245. zbr = &znode->zbranch[*n];
  1246. if (zbr->znode) {
  1247. znode->time = time;
  1248. znode = dirty_cow_znode(c, zbr);
  1249. if (IS_ERR(znode))
  1250. return PTR_ERR(znode);
  1251. continue;
  1252. }
  1253. /* znode is not in TNC cache, load it from the media */
  1254. znode = ubifs_load_znode(c, zbr, znode, *n);
  1255. if (IS_ERR(znode))
  1256. return PTR_ERR(znode);
  1257. znode = dirty_cow_znode(c, zbr);
  1258. if (IS_ERR(znode))
  1259. return PTR_ERR(znode);
  1260. }
  1261. *zn = znode;
  1262. if (exact || !is_hash_key(c, key) || *n != -1) {
  1263. dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
  1264. return exact;
  1265. }
  1266. /*
  1267. * See huge comment at 'lookup_level0_dirty()' what is the rest of the
  1268. * code.
  1269. */
  1270. err = tnc_prev(c, &znode, n);
  1271. if (err == -ENOENT) {
  1272. *n = -1;
  1273. dbg_tnc("found 0, lvl %d, n -1", znode->level);
  1274. return 0;
  1275. }
  1276. if (unlikely(err < 0))
  1277. return err;
  1278. if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
  1279. *n = -1;
  1280. dbg_tnc("found 0, lvl %d, n -1", znode->level);
  1281. return 0;
  1282. }
  1283. if (znode->cnext || !ubifs_zn_dirty(znode)) {
  1284. znode = dirty_cow_bottom_up(c, znode);
  1285. if (IS_ERR(znode))
  1286. return PTR_ERR(znode);
  1287. }
  1288. dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
  1289. *zn = znode;
  1290. return 1;
  1291. }
  1292. /**
  1293. * maybe_leb_gced - determine if a LEB may have been garbage collected.
  1294. * @c: UBIFS file-system description object
  1295. * @lnum: LEB number
  1296. * @gc_seq1: garbage collection sequence number
  1297. *
  1298. * This function determines if @lnum may have been garbage collected since
  1299. * sequence number @gc_seq1. If it may have been then %1 is returned, otherwise
  1300. * %0 is returned.
  1301. */
  1302. static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
  1303. {
  1304. int gc_seq2, gced_lnum;
  1305. gced_lnum = c->gced_lnum;
  1306. smp_rmb();
  1307. gc_seq2 = c->gc_seq;
  1308. /* Same seq means no GC */
  1309. if (gc_seq1 == gc_seq2)
  1310. return 0;
  1311. /* Different by more than 1 means we don't know */
  1312. if (gc_seq1 + 1 != gc_seq2)
  1313. return 1;
  1314. /*
  1315. * We have seen the sequence number has increased by 1. Now we need to
  1316. * be sure we read the right LEB number, so read it again.
  1317. */
  1318. smp_rmb();
  1319. if (gced_lnum != c->gced_lnum)
  1320. return 1;
  1321. /* Finally we can check lnum */
  1322. if (gced_lnum == lnum)
  1323. return 1;
  1324. return 0;
  1325. }
  1326. /**
  1327. * ubifs_tnc_locate - look up a file-system node and return it and its location.
  1328. * @c: UBIFS file-system description object
  1329. * @key: node key to lookup
  1330. * @node: the node is returned here
  1331. * @lnum: LEB number is returned here
  1332. * @offs: offset is returned here
  1333. *
  1334. * This function looks up and reads node with key @key. The caller has to make
  1335. * sure the @node buffer is large enough to fit the node. Returns zero in case
  1336. * of success, %-ENOENT if the node was not found, and a negative error code in
  1337. * case of failure. The node location can be returned in @lnum and @offs.
  1338. */
  1339. int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
  1340. void *node, int *lnum, int *offs)
  1341. {
  1342. int found, n, err, safely = 0, gc_seq1;
  1343. struct ubifs_znode *znode;
  1344. struct ubifs_zbranch zbr, *zt;
  1345. again:
  1346. mutex_lock(&c->tnc_mutex);
  1347. found = ubifs_lookup_level0(c, key, &znode, &n);
  1348. if (!found) {
  1349. err = -ENOENT;
  1350. goto out;
  1351. } else if (found < 0) {
  1352. err = found;
  1353. goto out;
  1354. }
  1355. zt = &znode->zbranch[n];
  1356. if (lnum) {
  1357. *lnum = zt->lnum;
  1358. *offs = zt->offs;
  1359. }
  1360. if (is_hash_key(c, key)) {
  1361. /*
  1362. * In this case the leaf node cache gets used, so we pass the
  1363. * address of the zbranch and keep the mutex locked
  1364. */
  1365. err = tnc_read_hashed_node(c, zt, node);
  1366. goto out;
  1367. }
  1368. if (safely) {
  1369. err = ubifs_tnc_read_node(c, zt, node);
  1370. goto out;
  1371. }
  1372. /* Drop the TNC mutex prematurely and race with garbage collection */
  1373. zbr = znode->zbranch[n];
  1374. gc_seq1 = c->gc_seq;
  1375. mutex_unlock(&c->tnc_mutex);
  1376. if (ubifs_get_wbuf(c, zbr.lnum)) {
  1377. /* We do not GC journal heads */
  1378. err = ubifs_tnc_read_node(c, &zbr, node);
  1379. return err;
  1380. }
  1381. err = fallible_read_node(c, key, &zbr, node);
  1382. if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
  1383. /*
  1384. * The node may have been GC'ed out from under us so try again
  1385. * while keeping the TNC mutex locked.
  1386. */
  1387. safely = 1;
  1388. goto again;
  1389. }
  1390. return 0;
  1391. out:
  1392. mutex_unlock(&c->tnc_mutex);
  1393. return err;
  1394. }
  1395. /**
  1396. * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
  1397. * @c: UBIFS file-system description object
  1398. * @bu: bulk-read parameters and results
  1399. *
  1400. * Lookup consecutive data node keys for the same inode that reside
  1401. * consecutively in the same LEB. This function returns zero in case of success
  1402. * and a negative error code in case of failure.
  1403. *
  1404. * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
  1405. * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
  1406. * maximum possible amount of nodes for bulk-read.
  1407. */
  1408. int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
  1409. {
  1410. int n, err = 0, lnum = -1, offs;
  1411. int len;
  1412. unsigned int block = key_block(c, &bu->key);
  1413. struct ubifs_znode *znode;
  1414. bu->cnt = 0;
  1415. bu->blk_cnt = 0;
  1416. bu->eof = 0;
  1417. mutex_lock(&c->tnc_mutex);
  1418. /* Find first key */
  1419. err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
  1420. if (err < 0)
  1421. goto out;
  1422. if (err) {
  1423. /* Key found */
  1424. len = znode->zbranch[n].len;
  1425. /* The buffer must be big enough for at least 1 node */
  1426. if (len > bu->buf_len) {
  1427. err = -EINVAL;
  1428. goto out;
  1429. }
  1430. /* Add this key */
  1431. bu->zbranch[bu->cnt++] = znode->zbranch[n];
  1432. bu->blk_cnt += 1;
  1433. lnum = znode->zbranch[n].lnum;
  1434. offs = ALIGN(znode->zbranch[n].offs + len, 8);
  1435. }
  1436. while (1) {
  1437. struct ubifs_zbranch *zbr;
  1438. union ubifs_key *key;
  1439. unsigned int next_block;
  1440. /* Find next key */
  1441. err = tnc_next(c, &znode, &n);
  1442. if (err)
  1443. goto out;
  1444. zbr = &znode->zbranch[n];
  1445. key = &zbr->key;
  1446. /* See if there is another data key for this file */
  1447. if (key_inum(c, key) != key_inum(c, &bu->key) ||
  1448. key_type(c, key) != UBIFS_DATA_KEY) {
  1449. err = -ENOENT;
  1450. goto out;
  1451. }
  1452. if (lnum < 0) {
  1453. /* First key found */
  1454. lnum = zbr->lnum;
  1455. offs = ALIGN(zbr->offs + zbr->len, 8);
  1456. len = zbr->len;
  1457. if (len > bu->buf_len) {
  1458. err = -EINVAL;
  1459. goto out;
  1460. }
  1461. } else {
  1462. /*
  1463. * The data nodes must be in consecutive positions in
  1464. * the same LEB.
  1465. */
  1466. if (zbr->lnum != lnum || zbr->offs != offs)
  1467. goto out;
  1468. offs += ALIGN(zbr->len, 8);
  1469. len = ALIGN(len, 8) + zbr->len;
  1470. /* Must not exceed buffer length */
  1471. if (len > bu->buf_len)
  1472. goto out;
  1473. }
  1474. /* Allow for holes */
  1475. next_block = key_block(c, key);
  1476. bu->blk_cnt += (next_block - block - 1);
  1477. if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
  1478. goto out;
  1479. block = next_block;
  1480. /* Add this key */
  1481. bu->zbranch[bu->cnt++] = *zbr;
  1482. bu->blk_cnt += 1;
  1483. /* See if we have room for more */
  1484. if (bu->cnt >= UBIFS_MAX_BULK_READ)
  1485. goto out;
  1486. if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
  1487. goto out;
  1488. }
  1489. out:
  1490. if (err == -ENOENT) {
  1491. bu->eof = 1;
  1492. err = 0;
  1493. }
  1494. bu->gc_seq = c->gc_seq;
  1495. mutex_unlock(&c->tnc_mutex);
  1496. if (err)
  1497. return err;
  1498. /*
  1499. * An enormous hole could cause bulk-read to encompass too many
  1500. * page cache pages, so limit the number here.
  1501. */
  1502. if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
  1503. bu->blk_cnt = UBIFS_MAX_BULK_READ;
  1504. /*
  1505. * Ensure that bulk-read covers a whole number of page cache
  1506. * pages.
  1507. */
  1508. if (UBIFS_BLOCKS_PER_PAGE == 1 ||
  1509. !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
  1510. return 0;
  1511. if (bu->eof) {
  1512. /* At the end of file we can round up */
  1513. bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
  1514. return 0;
  1515. }
  1516. /* Exclude data nodes that do not make up a whole page cache page */
  1517. block = key_block(c, &bu->key) + bu->blk_cnt;
  1518. block &= ~(UBIFS_BLOCKS_PER_PAGE - 1);
  1519. while (bu->cnt) {
  1520. if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
  1521. break;
  1522. bu->cnt -= 1;
  1523. }
  1524. return 0;
  1525. }
  1526. /**
  1527. * read_wbuf - bulk-read from a LEB with a wbuf.
  1528. * @wbuf: wbuf that may overlap the read
  1529. * @buf: buffer into which to read
  1530. * @len: read length
  1531. * @lnum: LEB number from which to read
  1532. * @offs: offset from which to read
  1533. *
  1534. * This functions returns %0 on success or a negative error code on failure.
  1535. */
  1536. static int read_wbuf(struct ubifs_wbuf *wbuf, void *buf, int len, int lnum,
  1537. int offs)
  1538. {
  1539. const struct ubifs_info *c = wbuf->c;
  1540. int rlen, overlap;
  1541. dbg_io("LEB %d:%d, length %d", lnum, offs, len);
  1542. ubifs_assert(c, wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
  1543. ubifs_assert(c, !(offs & 7) && offs < c->leb_size);
  1544. ubifs_assert(c, offs + len <= c->leb_size);
  1545. spin_lock(&wbuf->lock);
  1546. overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
  1547. if (!overlap) {
  1548. /* We may safely unlock the write-buffer and read the data */
  1549. spin_unlock(&wbuf->lock);
  1550. return ubifs_leb_read(c, lnum, buf, offs, len, 0);
  1551. }
  1552. /* Don't read under wbuf */
  1553. rlen = wbuf->offs - offs;
  1554. if (rlen < 0)
  1555. rlen = 0;
  1556. /* Copy the rest from the write-buffer */
  1557. memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
  1558. spin_unlock(&wbuf->lock);
  1559. if (rlen > 0)
  1560. /* Read everything that goes before write-buffer */
  1561. return ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
  1562. return 0;
  1563. }
  1564. /**
  1565. * validate_data_node - validate data nodes for bulk-read.
  1566. * @c: UBIFS file-system description object
  1567. * @buf: buffer containing data node to validate
  1568. * @zbr: zbranch of data node to validate
  1569. *
  1570. * This functions returns %0 on success or a negative error code on failure.
  1571. */
  1572. static int validate_data_node(struct ubifs_info *c, void *buf,
  1573. struct ubifs_zbranch *zbr)
  1574. {
  1575. union ubifs_key key1;
  1576. struct ubifs_ch *ch = buf;
  1577. int err, len;
  1578. if (ch->node_type != UBIFS_DATA_NODE) {
  1579. ubifs_err(c, "bad node type (%d but expected %d)",
  1580. ch->node_type, UBIFS_DATA_NODE);
  1581. goto out_err;
  1582. }
  1583. err = ubifs_check_node(c, buf, zbr->len, zbr->lnum, zbr->offs, 0, 0);
  1584. if (err) {
  1585. ubifs_err(c, "expected node type %d", UBIFS_DATA_NODE);
  1586. goto out;
  1587. }
  1588. err = ubifs_node_check_hash(c, buf, zbr->hash);
  1589. if (err) {
  1590. ubifs_bad_hash(c, buf, zbr->hash, zbr->lnum, zbr->offs);
  1591. return err;
  1592. }
  1593. len = le32_to_cpu(ch->len);
  1594. if (len != zbr->len) {
  1595. ubifs_err(c, "bad node length %d, expected %d", len, zbr->len);
  1596. goto out_err;
  1597. }
  1598. /* Make sure the key of the read node is correct */
  1599. key_read(c, buf + UBIFS_KEY_OFFSET, &key1);
  1600. if (!keys_eq(c, &zbr->key, &key1)) {
  1601. ubifs_err(c, "bad key in node at LEB %d:%d",
  1602. zbr->lnum, zbr->offs);
  1603. dbg_tnck(&zbr->key, "looked for key ");
  1604. dbg_tnck(&key1, "found node's key ");
  1605. goto out_err;
  1606. }
  1607. return 0;
  1608. out_err:
  1609. err = -EINVAL;
  1610. out:
  1611. ubifs_err(c, "bad node at LEB %d:%d", zbr->lnum, zbr->offs);
  1612. ubifs_dump_node(c, buf, zbr->len);
  1613. dump_stack();
  1614. return err;
  1615. }
  1616. /**
  1617. * ubifs_tnc_bulk_read - read a number of data nodes in one go.
  1618. * @c: UBIFS file-system description object
  1619. * @bu: bulk-read parameters and results
  1620. *
  1621. * This functions reads and validates the data nodes that were identified by the
  1622. * 'ubifs_tnc_get_bu_keys()' function. This functions returns %0 on success,
  1623. * -EAGAIN to indicate a race with GC, or another negative error code on
  1624. * failure.
  1625. */
  1626. int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
  1627. {
  1628. int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
  1629. struct ubifs_wbuf *wbuf;
  1630. void *buf;
  1631. len = bu->zbranch[bu->cnt - 1].offs;
  1632. len += bu->zbranch[bu->cnt - 1].len - offs;
  1633. if (len > bu->buf_len) {
  1634. ubifs_err(c, "buffer too small %d vs %d", bu->buf_len, len);
  1635. return -EINVAL;
  1636. }
  1637. /* Do the read */
  1638. wbuf = ubifs_get_wbuf(c, lnum);
  1639. if (wbuf)
  1640. err = read_wbuf(wbuf, bu->buf, len, lnum, offs);
  1641. else
  1642. err = ubifs_leb_read(c, lnum, bu->buf, offs, len, 0);
  1643. /* Check for a race with GC */
  1644. if (maybe_leb_gced(c, lnum, bu->gc_seq))
  1645. return -EAGAIN;
  1646. if (err && err != -EBADMSG) {
  1647. ubifs_err(c, "failed to read from LEB %d:%d, error %d",
  1648. lnum, offs, err);
  1649. dump_stack();
  1650. dbg_tnck(&bu->key, "key ");
  1651. return err;
  1652. }
  1653. /* Validate the nodes read */
  1654. buf = bu->buf;
  1655. for (i = 0; i < bu->cnt; i++) {
  1656. err = validate_data_node(c, buf, &bu->zbranch[i]);
  1657. if (err)
  1658. return err;
  1659. buf = buf + ALIGN(bu->zbranch[i].len, 8);
  1660. }
  1661. return 0;
  1662. }
  1663. /**
  1664. * do_lookup_nm- look up a "hashed" node.
  1665. * @c: UBIFS file-system description object
  1666. * @key: node key to lookup
  1667. * @node: the node is returned here
  1668. * @nm: node name
  1669. *
  1670. * This function looks up and reads a node which contains name hash in the key.
  1671. * Since the hash may have collisions, there may be many nodes with the same
  1672. * key, so we have to sequentially look to all of them until the needed one is
  1673. * found. This function returns zero in case of success, %-ENOENT if the node
  1674. * was not found, and a negative error code in case of failure.
  1675. */
  1676. static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
  1677. void *node, const struct fscrypt_name *nm)
  1678. {
  1679. int found, n, err;
  1680. struct ubifs_znode *znode;
  1681. dbg_tnck(key, "key ");
  1682. mutex_lock(&c->tnc_mutex);
  1683. found = ubifs_lookup_level0(c, key, &znode, &n);
  1684. if (!found) {
  1685. err = -ENOENT;
  1686. goto out_unlock;
  1687. } else if (found < 0) {
  1688. err = found;
  1689. goto out_unlock;
  1690. }
  1691. ubifs_assert(c, n >= 0);
  1692. err = resolve_collision(c, key, &znode, &n, nm);
  1693. dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
  1694. if (unlikely(err < 0))
  1695. goto out_unlock;
  1696. if (err == 0) {
  1697. err = -ENOENT;
  1698. goto out_unlock;
  1699. }
  1700. err = tnc_read_hashed_node(c, &znode->zbranch[n], node);
  1701. out_unlock:
  1702. mutex_unlock(&c->tnc_mutex);
  1703. return err;
  1704. }
  1705. /**
  1706. * ubifs_tnc_lookup_nm - look up a "hashed" node.
  1707. * @c: UBIFS file-system description object
  1708. * @key: node key to lookup
  1709. * @node: the node is returned here
  1710. * @nm: node name
  1711. *
  1712. * This function looks up and reads a node which contains name hash in the key.
  1713. * Since the hash may have collisions, there may be many nodes with the same
  1714. * key, so we have to sequentially look to all of them until the needed one is
  1715. * found. This function returns zero in case of success, %-ENOENT if the node
  1716. * was not found, and a negative error code in case of failure.
  1717. */
  1718. int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
  1719. void *node, const struct fscrypt_name *nm)
  1720. {
  1721. int err, len;
  1722. const struct ubifs_dent_node *dent = node;
  1723. /*
  1724. * We assume that in most of the cases there are no name collisions and
  1725. * 'ubifs_tnc_lookup()' returns us the right direntry.
  1726. */
  1727. err = ubifs_tnc_lookup(c, key, node);
  1728. if (err)
  1729. return err;
  1730. len = le16_to_cpu(dent->nlen);
  1731. if (fname_len(nm) == len && !memcmp(dent->name, fname_name(nm), len))
  1732. return 0;
  1733. /*
  1734. * Unluckily, there are hash collisions and we have to iterate over
  1735. * them look at each direntry with colliding name hash sequentially.
  1736. */
  1737. return do_lookup_nm(c, key, node, nm);
  1738. }
  1739. static int search_dh_cookie(struct ubifs_info *c, const union ubifs_key *key,
  1740. struct ubifs_dent_node *dent, uint32_t cookie,
  1741. struct ubifs_znode **zn, int *n, int exact)
  1742. {
  1743. int err;
  1744. struct ubifs_znode *znode = *zn;
  1745. struct ubifs_zbranch *zbr;
  1746. union ubifs_key *dkey;
  1747. if (!exact) {
  1748. err = tnc_next(c, &znode, n);
  1749. if (err)
  1750. return err;
  1751. }
  1752. for (;;) {
  1753. zbr = &znode->zbranch[*n];
  1754. dkey = &zbr->key;
  1755. if (key_inum(c, dkey) != key_inum(c, key) ||
  1756. key_type(c, dkey) != key_type(c, key)) {
  1757. return -ENOENT;
  1758. }
  1759. err = tnc_read_hashed_node(c, zbr, dent);
  1760. if (err)
  1761. return err;
  1762. if (key_hash(c, key) == key_hash(c, dkey) &&
  1763. le32_to_cpu(dent->cookie) == cookie) {
  1764. *zn = znode;
  1765. return 0;
  1766. }
  1767. err = tnc_next(c, &znode, n);
  1768. if (err)
  1769. return err;
  1770. }
  1771. }
  1772. static int do_lookup_dh(struct ubifs_info *c, const union ubifs_key *key,
  1773. struct ubifs_dent_node *dent, uint32_t cookie)
  1774. {
  1775. int n, err;
  1776. struct ubifs_znode *znode;
  1777. union ubifs_key start_key;
  1778. ubifs_assert(c, is_hash_key(c, key));
  1779. lowest_dent_key(c, &start_key, key_inum(c, key));
  1780. mutex_lock(&c->tnc_mutex);
  1781. err = ubifs_lookup_level0(c, &start_key, &znode, &n);
  1782. if (unlikely(err < 0))
  1783. goto out_unlock;
  1784. err = search_dh_cookie(c, key, dent, cookie, &znode, &n, err);
  1785. out_unlock:
  1786. mutex_unlock(&c->tnc_mutex);
  1787. return err;
  1788. }
  1789. /**
  1790. * ubifs_tnc_lookup_dh - look up a "double hashed" node.
  1791. * @c: UBIFS file-system description object
  1792. * @key: node key to lookup
  1793. * @node: the node is returned here
  1794. * @cookie: node cookie for collision resolution
  1795. *
  1796. * This function looks up and reads a node which contains name hash in the key.
  1797. * Since the hash may have collisions, there may be many nodes with the same
  1798. * key, so we have to sequentially look to all of them until the needed one
  1799. * with the same cookie value is found.
  1800. * This function returns zero in case of success, %-ENOENT if the node
  1801. * was not found, and a negative error code in case of failure.
  1802. */
  1803. int ubifs_tnc_lookup_dh(struct ubifs_info *c, const union ubifs_key *key,
  1804. void *node, uint32_t cookie)
  1805. {
  1806. int err;
  1807. const struct ubifs_dent_node *dent = node;
  1808. if (!c->double_hash)
  1809. return -EOPNOTSUPP;
  1810. /*
  1811. * We assume that in most of the cases there are no name collisions and
  1812. * 'ubifs_tnc_lookup()' returns us the right direntry.
  1813. */
  1814. err = ubifs_tnc_lookup(c, key, node);
  1815. if (err)
  1816. return err;
  1817. if (le32_to_cpu(dent->cookie) == cookie)
  1818. return 0;
  1819. /*
  1820. * Unluckily, there are hash collisions and we have to iterate over
  1821. * them look at each direntry with colliding name hash sequentially.
  1822. */
  1823. return do_lookup_dh(c, key, node, cookie);
  1824. }
  1825. /**
  1826. * correct_parent_keys - correct parent znodes' keys.
  1827. * @c: UBIFS file-system description object
  1828. * @znode: znode to correct parent znodes for
  1829. *
  1830. * This is a helper function for 'tnc_insert()'. When the key of the leftmost
  1831. * zbranch changes, keys of parent znodes have to be corrected. This helper
  1832. * function is called in such situations and corrects the keys if needed.
  1833. */
  1834. static void correct_parent_keys(const struct ubifs_info *c,
  1835. struct ubifs_znode *znode)
  1836. {
  1837. union ubifs_key *key, *key1;
  1838. ubifs_assert(c, znode->parent);
  1839. ubifs_assert(c, znode->iip == 0);
  1840. key = &znode->zbranch[0].key;
  1841. key1 = &znode->parent->zbranch[0].key;
  1842. while (keys_cmp(c, key, key1) < 0) {
  1843. key_copy(c, key, key1);
  1844. znode = znode->parent;
  1845. znode->alt = 1;
  1846. if (!znode->parent || znode->iip)
  1847. break;
  1848. key1 = &znode->parent->zbranch[0].key;
  1849. }
  1850. }
  1851. /**
  1852. * insert_zbranch - insert a zbranch into a znode.
  1853. * @c: UBIFS file-system description object
  1854. * @znode: znode into which to insert
  1855. * @zbr: zbranch to insert
  1856. * @n: slot number to insert to
  1857. *
  1858. * This is a helper function for 'tnc_insert()'. UBIFS does not allow "gaps" in
  1859. * znode's array of zbranches and keeps zbranches consolidated, so when a new
  1860. * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
  1861. * slot, zbranches starting from @n have to be moved right.
  1862. */
  1863. static void insert_zbranch(struct ubifs_info *c, struct ubifs_znode *znode,
  1864. const struct ubifs_zbranch *zbr, int n)
  1865. {
  1866. int i;
  1867. ubifs_assert(c, ubifs_zn_dirty(znode));
  1868. if (znode->level) {
  1869. for (i = znode->child_cnt; i > n; i--) {
  1870. znode->zbranch[i] = znode->zbranch[i - 1];
  1871. if (znode->zbranch[i].znode)
  1872. znode->zbranch[i].znode->iip = i;
  1873. }
  1874. if (zbr->znode)
  1875. zbr->znode->iip = n;
  1876. } else
  1877. for (i = znode->child_cnt; i > n; i--)
  1878. znode->zbranch[i] = znode->zbranch[i - 1];
  1879. znode->zbranch[n] = *zbr;
  1880. znode->child_cnt += 1;
  1881. /*
  1882. * After inserting at slot zero, the lower bound of the key range of
  1883. * this znode may have changed. If this znode is subsequently split
  1884. * then the upper bound of the key range may change, and furthermore
  1885. * it could change to be lower than the original lower bound. If that
  1886. * happens, then it will no longer be possible to find this znode in the
  1887. * TNC using the key from the index node on flash. That is bad because
  1888. * if it is not found, we will assume it is obsolete and may overwrite
  1889. * it. Then if there is an unclean unmount, we will start using the
  1890. * old index which will be broken.
  1891. *
  1892. * So we first mark znodes that have insertions at slot zero, and then
  1893. * if they are split we add their lnum/offs to the old_idx tree.
  1894. */
  1895. if (n == 0)
  1896. znode->alt = 1;
  1897. }
  1898. /**
  1899. * tnc_insert - insert a node into TNC.
  1900. * @c: UBIFS file-system description object
  1901. * @znode: znode to insert into
  1902. * @zbr: branch to insert
  1903. * @n: slot number to insert new zbranch to
  1904. *
  1905. * This function inserts a new node described by @zbr into znode @znode. If
  1906. * znode does not have a free slot for new zbranch, it is split. Parent znodes
  1907. * are splat as well if needed. Returns zero in case of success or a negative
  1908. * error code in case of failure.
  1909. */
  1910. static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
  1911. struct ubifs_zbranch *zbr, int n)
  1912. {
  1913. struct ubifs_znode *zn, *zi, *zp;
  1914. int i, keep, move, appending = 0;
  1915. union ubifs_key *key = &zbr->key, *key1;
  1916. ubifs_assert(c, n >= 0 && n <= c->fanout);
  1917. /* Implement naive insert for now */
  1918. again:
  1919. zp = znode->parent;
  1920. if (znode->child_cnt < c->fanout) {
  1921. ubifs_assert(c, n != c->fanout);
  1922. dbg_tnck(key, "inserted at %d level %d, key ", n, znode->level);
  1923. insert_zbranch(c, znode, zbr, n);
  1924. /* Ensure parent's key is correct */
  1925. if (n == 0 && zp && znode->iip == 0)
  1926. correct_parent_keys(c, znode);
  1927. return 0;
  1928. }
  1929. /*
  1930. * Unfortunately, @znode does not have more empty slots and we have to
  1931. * split it.
  1932. */
  1933. dbg_tnck(key, "splitting level %d, key ", znode->level);
  1934. if (znode->alt)
  1935. /*
  1936. * We can no longer be sure of finding this znode by key, so we
  1937. * record it in the old_idx tree.
  1938. */
  1939. ins_clr_old_idx_znode(c, znode);
  1940. zn = kzalloc(c->max_znode_sz, GFP_NOFS);
  1941. if (!zn)
  1942. return -ENOMEM;
  1943. zn->parent = zp;
  1944. zn->level = znode->level;
  1945. /* Decide where to split */
  1946. if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) {
  1947. /* Try not to split consecutive data keys */
  1948. if (n == c->fanout) {
  1949. key1 = &znode->zbranch[n - 1].key;
  1950. if (key_inum(c, key1) == key_inum(c, key) &&
  1951. key_type(c, key1) == UBIFS_DATA_KEY)
  1952. appending = 1;
  1953. } else
  1954. goto check_split;
  1955. } else if (appending && n != c->fanout) {
  1956. /* Try not to split consecutive data keys */
  1957. appending = 0;
  1958. check_split:
  1959. if (n >= (c->fanout + 1) / 2) {
  1960. key1 = &znode->zbranch[0].key;
  1961. if (key_inum(c, key1) == key_inum(c, key) &&
  1962. key_type(c, key1) == UBIFS_DATA_KEY) {
  1963. key1 = &znode->zbranch[n].key;
  1964. if (key_inum(c, key1) != key_inum(c, key) ||
  1965. key_type(c, key1) != UBIFS_DATA_KEY) {
  1966. keep = n;
  1967. move = c->fanout - keep;
  1968. zi = znode;
  1969. goto do_split;
  1970. }
  1971. }
  1972. }
  1973. }
  1974. if (appending) {
  1975. keep = c->fanout;
  1976. move = 0;
  1977. } else {
  1978. keep = (c->fanout + 1) / 2;
  1979. move = c->fanout - keep;
  1980. }
  1981. /*
  1982. * Although we don't at present, we could look at the neighbors and see
  1983. * if we can move some zbranches there.
  1984. */
  1985. if (n < keep) {
  1986. /* Insert into existing znode */
  1987. zi = znode;
  1988. move += 1;
  1989. keep -= 1;
  1990. } else {
  1991. /* Insert into new znode */
  1992. zi = zn;
  1993. n -= keep;
  1994. /* Re-parent */
  1995. if (zn->level != 0)
  1996. zbr->znode->parent = zn;
  1997. }
  1998. do_split:
  1999. __set_bit(DIRTY_ZNODE, &zn->flags);
  2000. atomic_long_inc(&c->dirty_zn_cnt);
  2001. zn->child_cnt = move;
  2002. znode->child_cnt = keep;
  2003. dbg_tnc("moving %d, keeping %d", move, keep);
  2004. /* Move zbranch */
  2005. for (i = 0; i < move; i++) {
  2006. zn->zbranch[i] = znode->zbranch[keep + i];
  2007. /* Re-parent */
  2008. if (zn->level != 0)
  2009. if (zn->zbranch[i].znode) {
  2010. zn->zbranch[i].znode->parent = zn;
  2011. zn->zbranch[i].znode->iip = i;
  2012. }
  2013. }
  2014. /* Insert new key and branch */
  2015. dbg_tnck(key, "inserting at %d level %d, key ", n, zn->level);
  2016. insert_zbranch(c, zi, zbr, n);
  2017. /* Insert new znode (produced by spitting) into the parent */
  2018. if (zp) {
  2019. if (n == 0 && zi == znode && znode->iip == 0)
  2020. correct_parent_keys(c, znode);
  2021. /* Locate insertion point */
  2022. n = znode->iip + 1;
  2023. /* Tail recursion */
  2024. zbr->key = zn->zbranch[0].key;
  2025. zbr->znode = zn;
  2026. zbr->lnum = 0;
  2027. zbr->offs = 0;
  2028. zbr->len = 0;
  2029. znode = zp;
  2030. goto again;
  2031. }
  2032. /* We have to split root znode */
  2033. dbg_tnc("creating new zroot at level %d", znode->level + 1);
  2034. zi = kzalloc(c->max_znode_sz, GFP_NOFS);
  2035. if (!zi)
  2036. return -ENOMEM;
  2037. zi->child_cnt = 2;
  2038. zi->level = znode->level + 1;
  2039. __set_bit(DIRTY_ZNODE, &zi->flags);
  2040. atomic_long_inc(&c->dirty_zn_cnt);
  2041. zi->zbranch[0].key = znode->zbranch[0].key;
  2042. zi->zbranch[0].znode = znode;
  2043. zi->zbranch[0].lnum = c->zroot.lnum;
  2044. zi->zbranch[0].offs = c->zroot.offs;
  2045. zi->zbranch[0].len = c->zroot.len;
  2046. zi->zbranch[1].key = zn->zbranch[0].key;
  2047. zi->zbranch[1].znode = zn;
  2048. c->zroot.lnum = 0;
  2049. c->zroot.offs = 0;
  2050. c->zroot.len = 0;
  2051. c->zroot.znode = zi;
  2052. zn->parent = zi;
  2053. zn->iip = 1;
  2054. znode->parent = zi;
  2055. znode->iip = 0;
  2056. return 0;
  2057. }
  2058. /**
  2059. * ubifs_tnc_add - add a node to TNC.
  2060. * @c: UBIFS file-system description object
  2061. * @key: key to add
  2062. * @lnum: LEB number of node
  2063. * @offs: node offset
  2064. * @len: node length
  2065. * @hash: The hash over the node
  2066. *
  2067. * This function adds a node with key @key to TNC. The node may be new or it may
  2068. * obsolete some existing one. Returns %0 on success or negative error code on
  2069. * failure.
  2070. */
  2071. int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
  2072. int offs, int len, const u8 *hash)
  2073. {
  2074. int found, n, err = 0;
  2075. struct ubifs_znode *znode;
  2076. mutex_lock(&c->tnc_mutex);
  2077. dbg_tnck(key, "%d:%d, len %d, key ", lnum, offs, len);
  2078. found = lookup_level0_dirty(c, key, &znode, &n);
  2079. if (!found) {
  2080. struct ubifs_zbranch zbr;
  2081. zbr.znode = NULL;
  2082. zbr.lnum = lnum;
  2083. zbr.offs = offs;
  2084. zbr.len = len;
  2085. ubifs_copy_hash(c, hash, zbr.hash);
  2086. key_copy(c, key, &zbr.key);
  2087. err = tnc_insert(c, znode, &zbr, n + 1);
  2088. } else if (found == 1) {
  2089. struct ubifs_zbranch *zbr = &znode->zbranch[n];
  2090. lnc_free(zbr);
  2091. err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
  2092. zbr->lnum = lnum;
  2093. zbr->offs = offs;
  2094. zbr->len = len;
  2095. ubifs_copy_hash(c, hash, zbr->hash);
  2096. } else
  2097. err = found;
  2098. if (!err)
  2099. err = dbg_check_tnc(c, 0);
  2100. mutex_unlock(&c->tnc_mutex);
  2101. return err;
  2102. }
  2103. /**
  2104. * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
  2105. * @c: UBIFS file-system description object
  2106. * @key: key to add
  2107. * @old_lnum: LEB number of old node
  2108. * @old_offs: old node offset
  2109. * @lnum: LEB number of node
  2110. * @offs: node offset
  2111. * @len: node length
  2112. *
  2113. * This function replaces a node with key @key in the TNC only if the old node
  2114. * is found. This function is called by garbage collection when node are moved.
  2115. * Returns %0 on success or negative error code on failure.
  2116. */
  2117. int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
  2118. int old_lnum, int old_offs, int lnum, int offs, int len)
  2119. {
  2120. int found, n, err = 0;
  2121. struct ubifs_znode *znode;
  2122. mutex_lock(&c->tnc_mutex);
  2123. dbg_tnck(key, "old LEB %d:%d, new LEB %d:%d, len %d, key ", old_lnum,
  2124. old_offs, lnum, offs, len);
  2125. found = lookup_level0_dirty(c, key, &znode, &n);
  2126. if (found < 0) {
  2127. err = found;
  2128. goto out_unlock;
  2129. }
  2130. if (found == 1) {
  2131. struct ubifs_zbranch *zbr = &znode->zbranch[n];
  2132. found = 0;
  2133. if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
  2134. lnc_free(zbr);
  2135. err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
  2136. if (err)
  2137. goto out_unlock;
  2138. zbr->lnum = lnum;
  2139. zbr->offs = offs;
  2140. zbr->len = len;
  2141. found = 1;
  2142. } else if (is_hash_key(c, key)) {
  2143. found = resolve_collision_directly(c, key, &znode, &n,
  2144. old_lnum, old_offs);
  2145. dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
  2146. found, znode, n, old_lnum, old_offs);
  2147. if (found < 0) {
  2148. err = found;
  2149. goto out_unlock;
  2150. }
  2151. if (found) {
  2152. /* Ensure the znode is dirtied */
  2153. if (znode->cnext || !ubifs_zn_dirty(znode)) {
  2154. znode = dirty_cow_bottom_up(c, znode);
  2155. if (IS_ERR(znode)) {
  2156. err = PTR_ERR(znode);
  2157. goto out_unlock;
  2158. }
  2159. }
  2160. zbr = &znode->zbranch[n];
  2161. lnc_free(zbr);
  2162. err = ubifs_add_dirt(c, zbr->lnum,
  2163. zbr->len);
  2164. if (err)
  2165. goto out_unlock;
  2166. zbr->lnum = lnum;
  2167. zbr->offs = offs;
  2168. zbr->len = len;
  2169. }
  2170. }
  2171. }
  2172. if (!found)
  2173. err = ubifs_add_dirt(c, lnum, len);
  2174. if (!err)
  2175. err = dbg_check_tnc(c, 0);
  2176. out_unlock:
  2177. mutex_unlock(&c->tnc_mutex);
  2178. return err;
  2179. }
  2180. /**
  2181. * ubifs_tnc_add_nm - add a "hashed" node to TNC.
  2182. * @c: UBIFS file-system description object
  2183. * @key: key to add
  2184. * @lnum: LEB number of node
  2185. * @offs: node offset
  2186. * @len: node length
  2187. * @hash: The hash over the node
  2188. * @nm: node name
  2189. *
  2190. * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
  2191. * may have collisions, like directory entry keys.
  2192. */
  2193. int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
  2194. int lnum, int offs, int len, const u8 *hash,
  2195. const struct fscrypt_name *nm)
  2196. {
  2197. int found, n, err = 0;
  2198. struct ubifs_znode *znode;
  2199. mutex_lock(&c->tnc_mutex);
  2200. dbg_tnck(key, "LEB %d:%d, key ", lnum, offs);
  2201. found = lookup_level0_dirty(c, key, &znode, &n);
  2202. if (found < 0) {
  2203. err = found;
  2204. goto out_unlock;
  2205. }
  2206. if (found == 1) {
  2207. if (c->replaying)
  2208. found = fallible_resolve_collision(c, key, &znode, &n,
  2209. nm, 1);
  2210. else
  2211. found = resolve_collision(c, key, &znode, &n, nm);
  2212. dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
  2213. if (found < 0) {
  2214. err = found;
  2215. goto out_unlock;
  2216. }
  2217. /* Ensure the znode is dirtied */
  2218. if (znode->cnext || !ubifs_zn_dirty(znode)) {
  2219. znode = dirty_cow_bottom_up(c, znode);
  2220. if (IS_ERR(znode)) {
  2221. err = PTR_ERR(znode);
  2222. goto out_unlock;
  2223. }
  2224. }
  2225. if (found == 1) {
  2226. struct ubifs_zbranch *zbr = &znode->zbranch[n];
  2227. lnc_free(zbr);
  2228. err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
  2229. zbr->lnum = lnum;
  2230. zbr->offs = offs;
  2231. zbr->len = len;
  2232. ubifs_copy_hash(c, hash, zbr->hash);
  2233. goto out_unlock;
  2234. }
  2235. }
  2236. if (!found) {
  2237. struct ubifs_zbranch zbr;
  2238. zbr.znode = NULL;
  2239. zbr.lnum = lnum;
  2240. zbr.offs = offs;
  2241. zbr.len = len;
  2242. ubifs_copy_hash(c, hash, zbr.hash);
  2243. key_copy(c, key, &zbr.key);
  2244. err = tnc_insert(c, znode, &zbr, n + 1);
  2245. if (err)
  2246. goto out_unlock;
  2247. if (c->replaying) {
  2248. /*
  2249. * We did not find it in the index so there may be a
  2250. * dangling branch still in the index. So we remove it
  2251. * by passing 'ubifs_tnc_remove_nm()' the same key but
  2252. * an unmatchable name.
  2253. */
  2254. struct fscrypt_name noname = { .disk_name = { .name = "", .len = 1 } };
  2255. err = dbg_check_tnc(c, 0);
  2256. mutex_unlock(&c->tnc_mutex);
  2257. if (err)
  2258. return err;
  2259. return ubifs_tnc_remove_nm(c, key, &noname);
  2260. }
  2261. }
  2262. out_unlock:
  2263. if (!err)
  2264. err = dbg_check_tnc(c, 0);
  2265. mutex_unlock(&c->tnc_mutex);
  2266. return err;
  2267. }
  2268. /**
  2269. * tnc_delete - delete a znode form TNC.
  2270. * @c: UBIFS file-system description object
  2271. * @znode: znode to delete from
  2272. * @n: zbranch slot number to delete
  2273. *
  2274. * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
  2275. * case of success and a negative error code in case of failure.
  2276. */
  2277. static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
  2278. {
  2279. struct ubifs_zbranch *zbr;
  2280. struct ubifs_znode *zp;
  2281. int i, err;
  2282. /* Delete without merge for now */
  2283. ubifs_assert(c, znode->level == 0);
  2284. ubifs_assert(c, n >= 0 && n < c->fanout);
  2285. dbg_tnck(&znode->zbranch[n].key, "deleting key ");
  2286. zbr = &znode->zbranch[n];
  2287. lnc_free(zbr);
  2288. err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
  2289. if (err) {
  2290. ubifs_dump_znode(c, znode);
  2291. return err;
  2292. }
  2293. /* We do not "gap" zbranch slots */
  2294. for (i = n; i < znode->child_cnt - 1; i++)
  2295. znode->zbranch[i] = znode->zbranch[i + 1];
  2296. znode->child_cnt -= 1;
  2297. if (znode->child_cnt > 0)
  2298. return 0;
  2299. /*
  2300. * This was the last zbranch, we have to delete this znode from the
  2301. * parent.
  2302. */
  2303. do {
  2304. ubifs_assert(c, !ubifs_zn_obsolete(znode));
  2305. ubifs_assert(c, ubifs_zn_dirty(znode));
  2306. zp = znode->parent;
  2307. n = znode->iip;
  2308. atomic_long_dec(&c->dirty_zn_cnt);
  2309. err = insert_old_idx_znode(c, znode);
  2310. if (err)
  2311. return err;
  2312. if (znode->cnext) {
  2313. __set_bit(OBSOLETE_ZNODE, &znode->flags);
  2314. atomic_long_inc(&c->clean_zn_cnt);
  2315. atomic_long_inc(&ubifs_clean_zn_cnt);
  2316. } else
  2317. kfree(znode);
  2318. znode = zp;
  2319. } while (znode->child_cnt == 1); /* while removing last child */
  2320. /* Remove from znode, entry n - 1 */
  2321. znode->child_cnt -= 1;
  2322. ubifs_assert(c, znode->level != 0);
  2323. for (i = n; i < znode->child_cnt; i++) {
  2324. znode->zbranch[i] = znode->zbranch[i + 1];
  2325. if (znode->zbranch[i].znode)
  2326. znode->zbranch[i].znode->iip = i;
  2327. }
  2328. /*
  2329. * If this is the root and it has only 1 child then
  2330. * collapse the tree.
  2331. */
  2332. if (!znode->parent) {
  2333. while (znode->child_cnt == 1 && znode->level != 0) {
  2334. zp = znode;
  2335. zbr = &znode->zbranch[0];
  2336. znode = get_znode(c, znode, 0);
  2337. if (IS_ERR(znode))
  2338. return PTR_ERR(znode);
  2339. znode = dirty_cow_znode(c, zbr);
  2340. if (IS_ERR(znode))
  2341. return PTR_ERR(znode);
  2342. znode->parent = NULL;
  2343. znode->iip = 0;
  2344. if (c->zroot.len) {
  2345. err = insert_old_idx(c, c->zroot.lnum,
  2346. c->zroot.offs);
  2347. if (err)
  2348. return err;
  2349. }
  2350. c->zroot.lnum = zbr->lnum;
  2351. c->zroot.offs = zbr->offs;
  2352. c->zroot.len = zbr->len;
  2353. c->zroot.znode = znode;
  2354. ubifs_assert(c, !ubifs_zn_obsolete(zp));
  2355. ubifs_assert(c, ubifs_zn_dirty(zp));
  2356. atomic_long_dec(&c->dirty_zn_cnt);
  2357. if (zp->cnext) {
  2358. __set_bit(OBSOLETE_ZNODE, &zp->flags);
  2359. atomic_long_inc(&c->clean_zn_cnt);
  2360. atomic_long_inc(&ubifs_clean_zn_cnt);
  2361. } else
  2362. kfree(zp);
  2363. }
  2364. }
  2365. return 0;
  2366. }
  2367. /**
  2368. * ubifs_tnc_remove - remove an index entry of a node.
  2369. * @c: UBIFS file-system description object
  2370. * @key: key of node
  2371. *
  2372. * Returns %0 on success or negative error code on failure.
  2373. */
  2374. int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
  2375. {
  2376. int found, n, err = 0;
  2377. struct ubifs_znode *znode;
  2378. mutex_lock(&c->tnc_mutex);
  2379. dbg_tnck(key, "key ");
  2380. found = lookup_level0_dirty(c, key, &znode, &n);
  2381. if (found < 0) {
  2382. err = found;
  2383. goto out_unlock;
  2384. }
  2385. if (found == 1)
  2386. err = tnc_delete(c, znode, n);
  2387. if (!err)
  2388. err = dbg_check_tnc(c, 0);
  2389. out_unlock:
  2390. mutex_unlock(&c->tnc_mutex);
  2391. return err;
  2392. }
  2393. /**
  2394. * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
  2395. * @c: UBIFS file-system description object
  2396. * @key: key of node
  2397. * @nm: directory entry name
  2398. *
  2399. * Returns %0 on success or negative error code on failure.
  2400. */
  2401. int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
  2402. const struct fscrypt_name *nm)
  2403. {
  2404. int n, err;
  2405. struct ubifs_znode *znode;
  2406. mutex_lock(&c->tnc_mutex);
  2407. dbg_tnck(key, "key ");
  2408. err = lookup_level0_dirty(c, key, &znode, &n);
  2409. if (err < 0)
  2410. goto out_unlock;
  2411. if (err) {
  2412. if (c->replaying)
  2413. err = fallible_resolve_collision(c, key, &znode, &n,
  2414. nm, 0);
  2415. else
  2416. err = resolve_collision(c, key, &znode, &n, nm);
  2417. dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
  2418. if (err < 0)
  2419. goto out_unlock;
  2420. if (err) {
  2421. /* Ensure the znode is dirtied */
  2422. if (znode->cnext || !ubifs_zn_dirty(znode)) {
  2423. znode = dirty_cow_bottom_up(c, znode);
  2424. if (IS_ERR(znode)) {
  2425. err = PTR_ERR(znode);
  2426. goto out_unlock;
  2427. }
  2428. }
  2429. err = tnc_delete(c, znode, n);
  2430. }
  2431. }
  2432. out_unlock:
  2433. if (!err)
  2434. err = dbg_check_tnc(c, 0);
  2435. mutex_unlock(&c->tnc_mutex);
  2436. return err;
  2437. }
  2438. /**
  2439. * ubifs_tnc_remove_dh - remove an index entry for a "double hashed" node.
  2440. * @c: UBIFS file-system description object
  2441. * @key: key of node
  2442. * @cookie: node cookie for collision resolution
  2443. *
  2444. * Returns %0 on success or negative error code on failure.
  2445. */
  2446. int ubifs_tnc_remove_dh(struct ubifs_info *c, const union ubifs_key *key,
  2447. uint32_t cookie)
  2448. {
  2449. int n, err;
  2450. struct ubifs_znode *znode;
  2451. struct ubifs_dent_node *dent;
  2452. struct ubifs_zbranch *zbr;
  2453. if (!c->double_hash)
  2454. return -EOPNOTSUPP;
  2455. mutex_lock(&c->tnc_mutex);
  2456. err = lookup_level0_dirty(c, key, &znode, &n);
  2457. if (err <= 0)
  2458. goto out_unlock;
  2459. zbr = &znode->zbranch[n];
  2460. dent = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
  2461. if (!dent) {
  2462. err = -ENOMEM;
  2463. goto out_unlock;
  2464. }
  2465. err = tnc_read_hashed_node(c, zbr, dent);
  2466. if (err)
  2467. goto out_free;
  2468. /* If the cookie does not match, we're facing a hash collision. */
  2469. if (le32_to_cpu(dent->cookie) != cookie) {
  2470. union ubifs_key start_key;
  2471. lowest_dent_key(c, &start_key, key_inum(c, key));
  2472. err = ubifs_lookup_level0(c, &start_key, &znode, &n);
  2473. if (unlikely(err < 0))
  2474. goto out_free;
  2475. err = search_dh_cookie(c, key, dent, cookie, &znode, &n, err);
  2476. if (err)
  2477. goto out_free;
  2478. }
  2479. if (znode->cnext || !ubifs_zn_dirty(znode)) {
  2480. znode = dirty_cow_bottom_up(c, znode);
  2481. if (IS_ERR(znode)) {
  2482. err = PTR_ERR(znode);
  2483. goto out_free;
  2484. }
  2485. }
  2486. err = tnc_delete(c, znode, n);
  2487. out_free:
  2488. kfree(dent);
  2489. out_unlock:
  2490. if (!err)
  2491. err = dbg_check_tnc(c, 0);
  2492. mutex_unlock(&c->tnc_mutex);
  2493. return err;
  2494. }
  2495. /**
  2496. * key_in_range - determine if a key falls within a range of keys.
  2497. * @c: UBIFS file-system description object
  2498. * @key: key to check
  2499. * @from_key: lowest key in range
  2500. * @to_key: highest key in range
  2501. *
  2502. * This function returns %1 if the key is in range and %0 otherwise.
  2503. */
  2504. static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
  2505. union ubifs_key *from_key, union ubifs_key *to_key)
  2506. {
  2507. if (keys_cmp(c, key, from_key) < 0)
  2508. return 0;
  2509. if (keys_cmp(c, key, to_key) > 0)
  2510. return 0;
  2511. return 1;
  2512. }
  2513. /**
  2514. * ubifs_tnc_remove_range - remove index entries in range.
  2515. * @c: UBIFS file-system description object
  2516. * @from_key: lowest key to remove
  2517. * @to_key: highest key to remove
  2518. *
  2519. * This function removes index entries starting at @from_key and ending at
  2520. * @to_key. This function returns zero in case of success and a negative error
  2521. * code in case of failure.
  2522. */
  2523. int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
  2524. union ubifs_key *to_key)
  2525. {
  2526. int i, n, k, err = 0;
  2527. struct ubifs_znode *znode;
  2528. union ubifs_key *key;
  2529. mutex_lock(&c->tnc_mutex);
  2530. while (1) {
  2531. /* Find first level 0 znode that contains keys to remove */
  2532. err = ubifs_lookup_level0(c, from_key, &znode, &n);
  2533. if (err < 0)
  2534. goto out_unlock;
  2535. if (err)
  2536. key = from_key;
  2537. else {
  2538. err = tnc_next(c, &znode, &n);
  2539. if (err == -ENOENT) {
  2540. err = 0;
  2541. goto out_unlock;
  2542. }
  2543. if (err < 0)
  2544. goto out_unlock;
  2545. key = &znode->zbranch[n].key;
  2546. if (!key_in_range(c, key, from_key, to_key)) {
  2547. err = 0;
  2548. goto out_unlock;
  2549. }
  2550. }
  2551. /* Ensure the znode is dirtied */
  2552. if (znode->cnext || !ubifs_zn_dirty(znode)) {
  2553. znode = dirty_cow_bottom_up(c, znode);
  2554. if (IS_ERR(znode)) {
  2555. err = PTR_ERR(znode);
  2556. goto out_unlock;
  2557. }
  2558. }
  2559. /* Remove all keys in range except the first */
  2560. for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
  2561. key = &znode->zbranch[i].key;
  2562. if (!key_in_range(c, key, from_key, to_key))
  2563. break;
  2564. lnc_free(&znode->zbranch[i]);
  2565. err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
  2566. znode->zbranch[i].len);
  2567. if (err) {
  2568. ubifs_dump_znode(c, znode);
  2569. goto out_unlock;
  2570. }
  2571. dbg_tnck(key, "removing key ");
  2572. }
  2573. if (k) {
  2574. for (i = n + 1 + k; i < znode->child_cnt; i++)
  2575. znode->zbranch[i - k] = znode->zbranch[i];
  2576. znode->child_cnt -= k;
  2577. }
  2578. /* Now delete the first */
  2579. err = tnc_delete(c, znode, n);
  2580. if (err)
  2581. goto out_unlock;
  2582. }
  2583. out_unlock:
  2584. if (!err)
  2585. err = dbg_check_tnc(c, 0);
  2586. mutex_unlock(&c->tnc_mutex);
  2587. return err;
  2588. }
  2589. /**
  2590. * ubifs_tnc_remove_ino - remove an inode from TNC.
  2591. * @c: UBIFS file-system description object
  2592. * @inum: inode number to remove
  2593. *
  2594. * This function remove inode @inum and all the extended attributes associated
  2595. * with the anode from TNC and returns zero in case of success or a negative
  2596. * error code in case of failure.
  2597. */
  2598. int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
  2599. {
  2600. union ubifs_key key1, key2;
  2601. struct ubifs_dent_node *xent, *pxent = NULL;
  2602. struct fscrypt_name nm = {0};
  2603. dbg_tnc("ino %lu", (unsigned long)inum);
  2604. /*
  2605. * Walk all extended attribute entries and remove them together with
  2606. * corresponding extended attribute inodes.
  2607. */
  2608. lowest_xent_key(c, &key1, inum);
  2609. while (1) {
  2610. ino_t xattr_inum;
  2611. int err;
  2612. xent = ubifs_tnc_next_ent(c, &key1, &nm);
  2613. if (IS_ERR(xent)) {
  2614. err = PTR_ERR(xent);
  2615. if (err == -ENOENT)
  2616. break;
  2617. kfree(pxent);
  2618. return err;
  2619. }
  2620. xattr_inum = le64_to_cpu(xent->inum);
  2621. dbg_tnc("xent '%s', ino %lu", xent->name,
  2622. (unsigned long)xattr_inum);
  2623. ubifs_evict_xattr_inode(c, xattr_inum);
  2624. fname_name(&nm) = xent->name;
  2625. fname_len(&nm) = le16_to_cpu(xent->nlen);
  2626. err = ubifs_tnc_remove_nm(c, &key1, &nm);
  2627. if (err) {
  2628. kfree(pxent);
  2629. kfree(xent);
  2630. return err;
  2631. }
  2632. lowest_ino_key(c, &key1, xattr_inum);
  2633. highest_ino_key(c, &key2, xattr_inum);
  2634. err = ubifs_tnc_remove_range(c, &key1, &key2);
  2635. if (err) {
  2636. kfree(pxent);
  2637. kfree(xent);
  2638. return err;
  2639. }
  2640. kfree(pxent);
  2641. pxent = xent;
  2642. key_read(c, &xent->key, &key1);
  2643. }
  2644. kfree(pxent);
  2645. lowest_ino_key(c, &key1, inum);
  2646. highest_ino_key(c, &key2, inum);
  2647. return ubifs_tnc_remove_range(c, &key1, &key2);
  2648. }
  2649. /**
  2650. * ubifs_tnc_next_ent - walk directory or extended attribute entries.
  2651. * @c: UBIFS file-system description object
  2652. * @key: key of last entry
  2653. * @nm: name of last entry found or %NULL
  2654. *
  2655. * This function finds and reads the next directory or extended attribute entry
  2656. * after the given key (@key) if there is one. @nm is used to resolve
  2657. * collisions.
  2658. *
  2659. * If the name of the current entry is not known and only the key is known,
  2660. * @nm->name has to be %NULL. In this case the semantics of this function is a
  2661. * little bit different and it returns the entry corresponding to this key, not
  2662. * the next one. If the key was not found, the closest "right" entry is
  2663. * returned.
  2664. *
  2665. * If the fist entry has to be found, @key has to contain the lowest possible
  2666. * key value for this inode and @name has to be %NULL.
  2667. *
  2668. * This function returns the found directory or extended attribute entry node
  2669. * in case of success, %-ENOENT is returned if no entry was found, and a
  2670. * negative error code is returned in case of failure.
  2671. */
  2672. struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
  2673. union ubifs_key *key,
  2674. const struct fscrypt_name *nm)
  2675. {
  2676. int n, err, type = key_type(c, key);
  2677. struct ubifs_znode *znode;
  2678. struct ubifs_dent_node *dent;
  2679. struct ubifs_zbranch *zbr;
  2680. union ubifs_key *dkey;
  2681. dbg_tnck(key, "key ");
  2682. ubifs_assert(c, is_hash_key(c, key));
  2683. mutex_lock(&c->tnc_mutex);
  2684. err = ubifs_lookup_level0(c, key, &znode, &n);
  2685. if (unlikely(err < 0))
  2686. goto out_unlock;
  2687. if (fname_len(nm) > 0) {
  2688. if (err) {
  2689. /* Handle collisions */
  2690. if (c->replaying)
  2691. err = fallible_resolve_collision(c, key, &znode, &n,
  2692. nm, 0);
  2693. else
  2694. err = resolve_collision(c, key, &znode, &n, nm);
  2695. dbg_tnc("rc returned %d, znode %p, n %d",
  2696. err, znode, n);
  2697. if (unlikely(err < 0))
  2698. goto out_unlock;
  2699. }
  2700. /* Now find next entry */
  2701. err = tnc_next(c, &znode, &n);
  2702. if (unlikely(err))
  2703. goto out_unlock;
  2704. } else {
  2705. /*
  2706. * The full name of the entry was not given, in which case the
  2707. * behavior of this function is a little different and it
  2708. * returns current entry, not the next one.
  2709. */
  2710. if (!err) {
  2711. /*
  2712. * However, the given key does not exist in the TNC
  2713. * tree and @znode/@n variables contain the closest
  2714. * "preceding" element. Switch to the next one.
  2715. */
  2716. err = tnc_next(c, &znode, &n);
  2717. if (err)
  2718. goto out_unlock;
  2719. }
  2720. }
  2721. zbr = &znode->zbranch[n];
  2722. dent = kmalloc(zbr->len, GFP_NOFS);
  2723. if (unlikely(!dent)) {
  2724. err = -ENOMEM;
  2725. goto out_unlock;
  2726. }
  2727. /*
  2728. * The above 'tnc_next()' call could lead us to the next inode, check
  2729. * this.
  2730. */
  2731. dkey = &zbr->key;
  2732. if (key_inum(c, dkey) != key_inum(c, key) ||
  2733. key_type(c, dkey) != type) {
  2734. err = -ENOENT;
  2735. goto out_free;
  2736. }
  2737. err = tnc_read_hashed_node(c, zbr, dent);
  2738. if (unlikely(err))
  2739. goto out_free;
  2740. mutex_unlock(&c->tnc_mutex);
  2741. return dent;
  2742. out_free:
  2743. kfree(dent);
  2744. out_unlock:
  2745. mutex_unlock(&c->tnc_mutex);
  2746. return ERR_PTR(err);
  2747. }
  2748. /**
  2749. * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
  2750. * @c: UBIFS file-system description object
  2751. *
  2752. * Destroy left-over obsolete znodes from a failed commit.
  2753. */
  2754. static void tnc_destroy_cnext(struct ubifs_info *c)
  2755. {
  2756. struct ubifs_znode *cnext;
  2757. if (!c->cnext)
  2758. return;
  2759. ubifs_assert(c, c->cmt_state == COMMIT_BROKEN);
  2760. cnext = c->cnext;
  2761. do {
  2762. struct ubifs_znode *znode = cnext;
  2763. cnext = cnext->cnext;
  2764. if (ubifs_zn_obsolete(znode))
  2765. kfree(znode);
  2766. else if (!ubifs_zn_cow(znode)) {
  2767. /*
  2768. * Don't forget to update clean znode count after
  2769. * committing failed, because ubifs will check this
  2770. * count while closing tnc. Non-obsolete znode could
  2771. * be re-dirtied during committing process, so dirty
  2772. * flag is untrustable. The flag 'COW_ZNODE' is set
  2773. * for each dirty znode before committing, and it is
  2774. * cleared as long as the znode become clean, so we
  2775. * can statistic clean znode count according to this
  2776. * flag.
  2777. */
  2778. atomic_long_inc(&c->clean_zn_cnt);
  2779. atomic_long_inc(&ubifs_clean_zn_cnt);
  2780. }
  2781. } while (cnext && cnext != c->cnext);
  2782. }
  2783. /**
  2784. * ubifs_tnc_close - close TNC subsystem and free all related resources.
  2785. * @c: UBIFS file-system description object
  2786. */
  2787. void ubifs_tnc_close(struct ubifs_info *c)
  2788. {
  2789. tnc_destroy_cnext(c);
  2790. if (c->zroot.znode) {
  2791. long n, freed;
  2792. n = atomic_long_read(&c->clean_zn_cnt);
  2793. freed = ubifs_destroy_tnc_subtree(c, c->zroot.znode);
  2794. ubifs_assert(c, freed == n);
  2795. atomic_long_sub(n, &ubifs_clean_zn_cnt);
  2796. }
  2797. kfree(c->gap_lebs);
  2798. kfree(c->ilebs);
  2799. destroy_old_idx(c);
  2800. }
  2801. /**
  2802. * left_znode - get the znode to the left.
  2803. * @c: UBIFS file-system description object
  2804. * @znode: znode
  2805. *
  2806. * This function returns a pointer to the znode to the left of @znode or NULL if
  2807. * there is not one. A negative error code is returned on failure.
  2808. */
  2809. static struct ubifs_znode *left_znode(struct ubifs_info *c,
  2810. struct ubifs_znode *znode)
  2811. {
  2812. int level = znode->level;
  2813. while (1) {
  2814. int n = znode->iip - 1;
  2815. /* Go up until we can go left */
  2816. znode = znode->parent;
  2817. if (!znode)
  2818. return NULL;
  2819. if (n >= 0) {
  2820. /* Now go down the rightmost branch to 'level' */
  2821. znode = get_znode(c, znode, n);
  2822. if (IS_ERR(znode))
  2823. return znode;
  2824. while (znode->level != level) {
  2825. n = znode->child_cnt - 1;
  2826. znode = get_znode(c, znode, n);
  2827. if (IS_ERR(znode))
  2828. return znode;
  2829. }
  2830. break;
  2831. }
  2832. }
  2833. return znode;
  2834. }
  2835. /**
  2836. * right_znode - get the znode to the right.
  2837. * @c: UBIFS file-system description object
  2838. * @znode: znode
  2839. *
  2840. * This function returns a pointer to the znode to the right of @znode or NULL
  2841. * if there is not one. A negative error code is returned on failure.
  2842. */
  2843. static struct ubifs_znode *right_znode(struct ubifs_info *c,
  2844. struct ubifs_znode *znode)
  2845. {
  2846. int level = znode->level;
  2847. while (1) {
  2848. int n = znode->iip + 1;
  2849. /* Go up until we can go right */
  2850. znode = znode->parent;
  2851. if (!znode)
  2852. return NULL;
  2853. if (n < znode->child_cnt) {
  2854. /* Now go down the leftmost branch to 'level' */
  2855. znode = get_znode(c, znode, n);
  2856. if (IS_ERR(znode))
  2857. return znode;
  2858. while (znode->level != level) {
  2859. znode = get_znode(c, znode, 0);
  2860. if (IS_ERR(znode))
  2861. return znode;
  2862. }
  2863. break;
  2864. }
  2865. }
  2866. return znode;
  2867. }
  2868. /**
  2869. * lookup_znode - find a particular indexing node from TNC.
  2870. * @c: UBIFS file-system description object
  2871. * @key: index node key to lookup
  2872. * @level: index node level
  2873. * @lnum: index node LEB number
  2874. * @offs: index node offset
  2875. *
  2876. * This function searches an indexing node by its first key @key and its
  2877. * address @lnum:@offs. It looks up the indexing tree by pulling all indexing
  2878. * nodes it traverses to TNC. This function is called for indexing nodes which
  2879. * were found on the media by scanning, for example when garbage-collecting or
  2880. * when doing in-the-gaps commit. This means that the indexing node which is
  2881. * looked for does not have to have exactly the same leftmost key @key, because
  2882. * the leftmost key may have been changed, in which case TNC will contain a
  2883. * dirty znode which still refers the same @lnum:@offs. This function is clever
  2884. * enough to recognize such indexing nodes.
  2885. *
  2886. * Note, if a znode was deleted or changed too much, then this function will
  2887. * not find it. For situations like this UBIFS has the old index RB-tree
  2888. * (indexed by @lnum:@offs).
  2889. *
  2890. * This function returns a pointer to the znode found or %NULL if it is not
  2891. * found. A negative error code is returned on failure.
  2892. */
  2893. static struct ubifs_znode *lookup_znode(struct ubifs_info *c,
  2894. union ubifs_key *key, int level,
  2895. int lnum, int offs)
  2896. {
  2897. struct ubifs_znode *znode, *zn;
  2898. int n, nn;
  2899. ubifs_assert(c, key_type(c, key) < UBIFS_INVALID_KEY);
  2900. /*
  2901. * The arguments have probably been read off flash, so don't assume
  2902. * they are valid.
  2903. */
  2904. if (level < 0)
  2905. return ERR_PTR(-EINVAL);
  2906. /* Get the root znode */
  2907. znode = c->zroot.znode;
  2908. if (!znode) {
  2909. znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
  2910. if (IS_ERR(znode))
  2911. return znode;
  2912. }
  2913. /* Check if it is the one we are looking for */
  2914. if (c->zroot.lnum == lnum && c->zroot.offs == offs)
  2915. return znode;
  2916. /* Descend to the parent level i.e. (level + 1) */
  2917. if (level >= znode->level)
  2918. return NULL;
  2919. while (1) {
  2920. ubifs_search_zbranch(c, znode, key, &n);
  2921. if (n < 0) {
  2922. /*
  2923. * We reached a znode where the leftmost key is greater
  2924. * than the key we are searching for. This is the same
  2925. * situation as the one described in a huge comment at
  2926. * the end of the 'ubifs_lookup_level0()' function. And
  2927. * for exactly the same reasons we have to try to look
  2928. * left before giving up.
  2929. */
  2930. znode = left_znode(c, znode);
  2931. if (!znode)
  2932. return NULL;
  2933. if (IS_ERR(znode))
  2934. return znode;
  2935. ubifs_search_zbranch(c, znode, key, &n);
  2936. ubifs_assert(c, n >= 0);
  2937. }
  2938. if (znode->level == level + 1)
  2939. break;
  2940. znode = get_znode(c, znode, n);
  2941. if (IS_ERR(znode))
  2942. return znode;
  2943. }
  2944. /* Check if the child is the one we are looking for */
  2945. if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs)
  2946. return get_znode(c, znode, n);
  2947. /* If the key is unique, there is nowhere else to look */
  2948. if (!is_hash_key(c, key))
  2949. return NULL;
  2950. /*
  2951. * The key is not unique and so may be also in the znodes to either
  2952. * side.
  2953. */
  2954. zn = znode;
  2955. nn = n;
  2956. /* Look left */
  2957. while (1) {
  2958. /* Move one branch to the left */
  2959. if (n)
  2960. n -= 1;
  2961. else {
  2962. znode = left_znode(c, znode);
  2963. if (!znode)
  2964. break;
  2965. if (IS_ERR(znode))
  2966. return znode;
  2967. n = znode->child_cnt - 1;
  2968. }
  2969. /* Check it */
  2970. if (znode->zbranch[n].lnum == lnum &&
  2971. znode->zbranch[n].offs == offs)
  2972. return get_znode(c, znode, n);
  2973. /* Stop if the key is less than the one we are looking for */
  2974. if (keys_cmp(c, &znode->zbranch[n].key, key) < 0)
  2975. break;
  2976. }
  2977. /* Back to the middle */
  2978. znode = zn;
  2979. n = nn;
  2980. /* Look right */
  2981. while (1) {
  2982. /* Move one branch to the right */
  2983. if (++n >= znode->child_cnt) {
  2984. znode = right_znode(c, znode);
  2985. if (!znode)
  2986. break;
  2987. if (IS_ERR(znode))
  2988. return znode;
  2989. n = 0;
  2990. }
  2991. /* Check it */
  2992. if (znode->zbranch[n].lnum == lnum &&
  2993. znode->zbranch[n].offs == offs)
  2994. return get_znode(c, znode, n);
  2995. /* Stop if the key is greater than the one we are looking for */
  2996. if (keys_cmp(c, &znode->zbranch[n].key, key) > 0)
  2997. break;
  2998. }
  2999. return NULL;
  3000. }
  3001. /**
  3002. * is_idx_node_in_tnc - determine if an index node is in the TNC.
  3003. * @c: UBIFS file-system description object
  3004. * @key: key of index node
  3005. * @level: index node level
  3006. * @lnum: LEB number of index node
  3007. * @offs: offset of index node
  3008. *
  3009. * This function returns %0 if the index node is not referred to in the TNC, %1
  3010. * if the index node is referred to in the TNC and the corresponding znode is
  3011. * dirty, %2 if an index node is referred to in the TNC and the corresponding
  3012. * znode is clean, and a negative error code in case of failure.
  3013. *
  3014. * Note, the @key argument has to be the key of the first child. Also note,
  3015. * this function relies on the fact that 0:0 is never a valid LEB number and
  3016. * offset for a main-area node.
  3017. */
  3018. int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
  3019. int lnum, int offs)
  3020. {
  3021. struct ubifs_znode *znode;
  3022. znode = lookup_znode(c, key, level, lnum, offs);
  3023. if (!znode)
  3024. return 0;
  3025. if (IS_ERR(znode))
  3026. return PTR_ERR(znode);
  3027. return ubifs_zn_dirty(znode) ? 1 : 2;
  3028. }
  3029. /**
  3030. * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
  3031. * @c: UBIFS file-system description object
  3032. * @key: node key
  3033. * @lnum: node LEB number
  3034. * @offs: node offset
  3035. *
  3036. * This function returns %1 if the node is referred to in the TNC, %0 if it is
  3037. * not, and a negative error code in case of failure.
  3038. *
  3039. * Note, this function relies on the fact that 0:0 is never a valid LEB number
  3040. * and offset for a main-area node.
  3041. */
  3042. static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key,
  3043. int lnum, int offs)
  3044. {
  3045. struct ubifs_zbranch *zbr;
  3046. struct ubifs_znode *znode, *zn;
  3047. int n, found, err, nn;
  3048. const int unique = !is_hash_key(c, key);
  3049. found = ubifs_lookup_level0(c, key, &znode, &n);
  3050. if (found < 0)
  3051. return found; /* Error code */
  3052. if (!found)
  3053. return 0;
  3054. zbr = &znode->zbranch[n];
  3055. if (lnum == zbr->lnum && offs == zbr->offs)
  3056. return 1; /* Found it */
  3057. if (unique)
  3058. return 0;
  3059. /*
  3060. * Because the key is not unique, we have to look left
  3061. * and right as well
  3062. */
  3063. zn = znode;
  3064. nn = n;
  3065. /* Look left */
  3066. while (1) {
  3067. err = tnc_prev(c, &znode, &n);
  3068. if (err == -ENOENT)
  3069. break;
  3070. if (err)
  3071. return err;
  3072. if (keys_cmp(c, key, &znode->zbranch[n].key))
  3073. break;
  3074. zbr = &znode->zbranch[n];
  3075. if (lnum == zbr->lnum && offs == zbr->offs)
  3076. return 1; /* Found it */
  3077. }
  3078. /* Look right */
  3079. znode = zn;
  3080. n = nn;
  3081. while (1) {
  3082. err = tnc_next(c, &znode, &n);
  3083. if (err) {
  3084. if (err == -ENOENT)
  3085. return 0;
  3086. return err;
  3087. }
  3088. if (keys_cmp(c, key, &znode->zbranch[n].key))
  3089. break;
  3090. zbr = &znode->zbranch[n];
  3091. if (lnum == zbr->lnum && offs == zbr->offs)
  3092. return 1; /* Found it */
  3093. }
  3094. return 0;
  3095. }
  3096. /**
  3097. * ubifs_tnc_has_node - determine whether a node is in the TNC.
  3098. * @c: UBIFS file-system description object
  3099. * @key: node key
  3100. * @level: index node level (if it is an index node)
  3101. * @lnum: node LEB number
  3102. * @offs: node offset
  3103. * @is_idx: non-zero if the node is an index node
  3104. *
  3105. * This function returns %1 if the node is in the TNC, %0 if it is not, and a
  3106. * negative error code in case of failure. For index nodes, @key has to be the
  3107. * key of the first child. An index node is considered to be in the TNC only if
  3108. * the corresponding znode is clean or has not been loaded.
  3109. */
  3110. int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
  3111. int lnum, int offs, int is_idx)
  3112. {
  3113. int err;
  3114. mutex_lock(&c->tnc_mutex);
  3115. if (is_idx) {
  3116. err = is_idx_node_in_tnc(c, key, level, lnum, offs);
  3117. if (err < 0)
  3118. goto out_unlock;
  3119. if (err == 1)
  3120. /* The index node was found but it was dirty */
  3121. err = 0;
  3122. else if (err == 2)
  3123. /* The index node was found and it was clean */
  3124. err = 1;
  3125. else
  3126. BUG_ON(err != 0);
  3127. } else
  3128. err = is_leaf_node_in_tnc(c, key, lnum, offs);
  3129. out_unlock:
  3130. mutex_unlock(&c->tnc_mutex);
  3131. return err;
  3132. }
  3133. /**
  3134. * ubifs_dirty_idx_node - dirty an index node.
  3135. * @c: UBIFS file-system description object
  3136. * @key: index node key
  3137. * @level: index node level
  3138. * @lnum: index node LEB number
  3139. * @offs: index node offset
  3140. *
  3141. * This function loads and dirties an index node so that it can be garbage
  3142. * collected. The @key argument has to be the key of the first child. This
  3143. * function relies on the fact that 0:0 is never a valid LEB number and offset
  3144. * for a main-area node. Returns %0 on success and a negative error code on
  3145. * failure.
  3146. */
  3147. int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
  3148. int lnum, int offs)
  3149. {
  3150. struct ubifs_znode *znode;
  3151. int err = 0;
  3152. mutex_lock(&c->tnc_mutex);
  3153. znode = lookup_znode(c, key, level, lnum, offs);
  3154. if (!znode)
  3155. goto out_unlock;
  3156. if (IS_ERR(znode)) {
  3157. err = PTR_ERR(znode);
  3158. goto out_unlock;
  3159. }
  3160. znode = dirty_cow_bottom_up(c, znode);
  3161. if (IS_ERR(znode)) {
  3162. err = PTR_ERR(znode);
  3163. goto out_unlock;
  3164. }
  3165. out_unlock:
  3166. mutex_unlock(&c->tnc_mutex);
  3167. return err;
  3168. }
  3169. /**
  3170. * dbg_check_inode_size - check if inode size is correct.
  3171. * @c: UBIFS file-system description object
  3172. * @inode: inode to check
  3173. * @size: inode size
  3174. *
  3175. * This function makes sure that the inode size (@size) is correct and it does
  3176. * not have any pages beyond @size. Returns zero if the inode is OK, %-EINVAL
  3177. * if it has a data page beyond @size, and other negative error code in case of
  3178. * other errors.
  3179. */
  3180. int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
  3181. loff_t size)
  3182. {
  3183. int err, n;
  3184. union ubifs_key from_key, to_key, *key;
  3185. struct ubifs_znode *znode;
  3186. unsigned int block;
  3187. if (!S_ISREG(inode->i_mode))
  3188. return 0;
  3189. if (!dbg_is_chk_gen(c))
  3190. return 0;
  3191. block = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  3192. data_key_init(c, &from_key, inode->i_ino, block);
  3193. highest_data_key(c, &to_key, inode->i_ino);
  3194. mutex_lock(&c->tnc_mutex);
  3195. err = ubifs_lookup_level0(c, &from_key, &znode, &n);
  3196. if (err < 0)
  3197. goto out_unlock;
  3198. if (err) {
  3199. key = &from_key;
  3200. goto out_dump;
  3201. }
  3202. err = tnc_next(c, &znode, &n);
  3203. if (err == -ENOENT) {
  3204. err = 0;
  3205. goto out_unlock;
  3206. }
  3207. if (err < 0)
  3208. goto out_unlock;
  3209. ubifs_assert(c, err == 0);
  3210. key = &znode->zbranch[n].key;
  3211. if (!key_in_range(c, key, &from_key, &to_key))
  3212. goto out_unlock;
  3213. out_dump:
  3214. block = key_block(c, key);
  3215. ubifs_err(c, "inode %lu has size %lld, but there are data at offset %lld",
  3216. (unsigned long)inode->i_ino, size,
  3217. ((loff_t)block) << UBIFS_BLOCK_SHIFT);
  3218. mutex_unlock(&c->tnc_mutex);
  3219. ubifs_dump_inode(c, inode);
  3220. dump_stack();
  3221. return -EINVAL;
  3222. out_unlock:
  3223. mutex_unlock(&c->tnc_mutex);
  3224. return err;
  3225. }