tnc.c 87 KB

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