tnc.c 86 KB

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