tnc.c 85 KB

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