tnc.c 91 KB

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