tnc.c 87 KB

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