tnc.c 92 KB

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