tnc.c 72 KB

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