tnc.c 86 KB

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