tnc.c 77 KB

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