tnc.c 86 KB

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