tnc.c 92 KB

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