tnc.c 85 KB

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