tnc.c 87 KB

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