tnc.c 87 KB

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