tnc.c 93 KB

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