tnc.c 86 KB

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