xfs_da_btree.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2013 Red Hat, Inc.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_fs.h"
  9. #include "xfs_shared.h"
  10. #include "xfs_format.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_trans_resv.h"
  13. #include "xfs_bit.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_dir2.h"
  17. #include "xfs_dir2_priv.h"
  18. #include "xfs_trans.h"
  19. #include "xfs_bmap.h"
  20. #include "xfs_attr_leaf.h"
  21. #include "xfs_error.h"
  22. #include "xfs_trace.h"
  23. #include "xfs_buf_item.h"
  24. #include "xfs_log.h"
  25. #include "xfs_errortag.h"
  26. #include "xfs_health.h"
  27. /*
  28. * xfs_da_btree.c
  29. *
  30. * Routines to implement directories as Btrees of hashed names.
  31. */
  32. /*========================================================================
  33. * Function prototypes for the kernel.
  34. *========================================================================*/
  35. /*
  36. * Routines used for growing the Btree.
  37. */
  38. STATIC int xfs_da3_root_split(xfs_da_state_t *state,
  39. xfs_da_state_blk_t *existing_root,
  40. xfs_da_state_blk_t *new_child);
  41. STATIC int xfs_da3_node_split(xfs_da_state_t *state,
  42. xfs_da_state_blk_t *existing_blk,
  43. xfs_da_state_blk_t *split_blk,
  44. xfs_da_state_blk_t *blk_to_add,
  45. int treelevel,
  46. int *result);
  47. STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
  48. xfs_da_state_blk_t *node_blk_1,
  49. xfs_da_state_blk_t *node_blk_2);
  50. STATIC void xfs_da3_node_add(xfs_da_state_t *state,
  51. xfs_da_state_blk_t *old_node_blk,
  52. xfs_da_state_blk_t *new_node_blk);
  53. /*
  54. * Routines used for shrinking the Btree.
  55. */
  56. STATIC int xfs_da3_root_join(xfs_da_state_t *state,
  57. xfs_da_state_blk_t *root_blk);
  58. STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
  59. STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
  60. xfs_da_state_blk_t *drop_blk);
  61. STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
  62. xfs_da_state_blk_t *src_node_blk,
  63. xfs_da_state_blk_t *dst_node_blk);
  64. /*
  65. * Utility routines.
  66. */
  67. STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state,
  68. xfs_da_state_blk_t *drop_blk,
  69. xfs_da_state_blk_t *save_blk);
  70. struct kmem_cache *xfs_da_state_cache; /* anchor for dir/attr state */
  71. /*
  72. * Allocate a dir-state structure.
  73. * We don't put them on the stack since they're large.
  74. */
  75. struct xfs_da_state *
  76. xfs_da_state_alloc(
  77. struct xfs_da_args *args)
  78. {
  79. struct xfs_da_state *state;
  80. state = kmem_cache_zalloc(xfs_da_state_cache,
  81. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  82. state->args = args;
  83. state->mp = args->dp->i_mount;
  84. return state;
  85. }
  86. /*
  87. * Kill the altpath contents of a da-state structure.
  88. */
  89. STATIC void
  90. xfs_da_state_kill_altpath(xfs_da_state_t *state)
  91. {
  92. int i;
  93. for (i = 0; i < state->altpath.active; i++)
  94. state->altpath.blk[i].bp = NULL;
  95. state->altpath.active = 0;
  96. }
  97. /*
  98. * Free a da-state structure.
  99. */
  100. void
  101. xfs_da_state_free(xfs_da_state_t *state)
  102. {
  103. xfs_da_state_kill_altpath(state);
  104. #ifdef DEBUG
  105. memset((char *)state, 0, sizeof(*state));
  106. #endif /* DEBUG */
  107. kmem_cache_free(xfs_da_state_cache, state);
  108. }
  109. void
  110. xfs_da_state_reset(
  111. struct xfs_da_state *state,
  112. struct xfs_da_args *args)
  113. {
  114. xfs_da_state_kill_altpath(state);
  115. memset(state, 0, sizeof(struct xfs_da_state));
  116. state->args = args;
  117. state->mp = state->args->dp->i_mount;
  118. }
  119. static inline int xfs_dabuf_nfsb(struct xfs_mount *mp, int whichfork)
  120. {
  121. if (whichfork == XFS_DATA_FORK)
  122. return mp->m_dir_geo->fsbcount;
  123. return mp->m_attr_geo->fsbcount;
  124. }
  125. void
  126. xfs_da3_node_hdr_from_disk(
  127. struct xfs_mount *mp,
  128. struct xfs_da3_icnode_hdr *to,
  129. struct xfs_da_intnode *from)
  130. {
  131. if (xfs_has_crc(mp)) {
  132. struct xfs_da3_intnode *from3 = (struct xfs_da3_intnode *)from;
  133. to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
  134. to->back = be32_to_cpu(from3->hdr.info.hdr.back);
  135. to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
  136. to->count = be16_to_cpu(from3->hdr.__count);
  137. to->level = be16_to_cpu(from3->hdr.__level);
  138. to->btree = from3->__btree;
  139. ASSERT(to->magic == XFS_DA3_NODE_MAGIC);
  140. } else {
  141. to->forw = be32_to_cpu(from->hdr.info.forw);
  142. to->back = be32_to_cpu(from->hdr.info.back);
  143. to->magic = be16_to_cpu(from->hdr.info.magic);
  144. to->count = be16_to_cpu(from->hdr.__count);
  145. to->level = be16_to_cpu(from->hdr.__level);
  146. to->btree = from->__btree;
  147. ASSERT(to->magic == XFS_DA_NODE_MAGIC);
  148. }
  149. }
  150. void
  151. xfs_da3_node_hdr_to_disk(
  152. struct xfs_mount *mp,
  153. struct xfs_da_intnode *to,
  154. struct xfs_da3_icnode_hdr *from)
  155. {
  156. if (xfs_has_crc(mp)) {
  157. struct xfs_da3_intnode *to3 = (struct xfs_da3_intnode *)to;
  158. ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
  159. to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
  160. to3->hdr.info.hdr.back = cpu_to_be32(from->back);
  161. to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
  162. to3->hdr.__count = cpu_to_be16(from->count);
  163. to3->hdr.__level = cpu_to_be16(from->level);
  164. } else {
  165. ASSERT(from->magic == XFS_DA_NODE_MAGIC);
  166. to->hdr.info.forw = cpu_to_be32(from->forw);
  167. to->hdr.info.back = cpu_to_be32(from->back);
  168. to->hdr.info.magic = cpu_to_be16(from->magic);
  169. to->hdr.__count = cpu_to_be16(from->count);
  170. to->hdr.__level = cpu_to_be16(from->level);
  171. }
  172. }
  173. /*
  174. * Verify an xfs_da3_blkinfo structure. Note that the da3 fields are only
  175. * accessible on v5 filesystems. This header format is common across da node,
  176. * attr leaf and dir leaf blocks.
  177. */
  178. xfs_failaddr_t
  179. xfs_da3_blkinfo_verify(
  180. struct xfs_buf *bp,
  181. struct xfs_da3_blkinfo *hdr3)
  182. {
  183. struct xfs_mount *mp = bp->b_mount;
  184. struct xfs_da_blkinfo *hdr = &hdr3->hdr;
  185. if (!xfs_verify_magic16(bp, hdr->magic))
  186. return __this_address;
  187. if (xfs_has_crc(mp)) {
  188. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
  189. return __this_address;
  190. if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
  191. return __this_address;
  192. if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
  193. return __this_address;
  194. }
  195. return NULL;
  196. }
  197. static xfs_failaddr_t
  198. xfs_da3_node_verify(
  199. struct xfs_buf *bp)
  200. {
  201. struct xfs_mount *mp = bp->b_mount;
  202. struct xfs_da_intnode *hdr = bp->b_addr;
  203. struct xfs_da3_icnode_hdr ichdr;
  204. xfs_failaddr_t fa;
  205. xfs_da3_node_hdr_from_disk(mp, &ichdr, hdr);
  206. fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
  207. if (fa)
  208. return fa;
  209. if (ichdr.level == 0)
  210. return __this_address;
  211. if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
  212. return __this_address;
  213. if (ichdr.count == 0)
  214. return __this_address;
  215. /*
  216. * we don't know if the node is for and attribute or directory tree,
  217. * so only fail if the count is outside both bounds
  218. */
  219. if (ichdr.count > mp->m_dir_geo->node_ents &&
  220. ichdr.count > mp->m_attr_geo->node_ents)
  221. return __this_address;
  222. /* XXX: hash order check? */
  223. return NULL;
  224. }
  225. xfs_failaddr_t
  226. xfs_da3_node_header_check(
  227. struct xfs_buf *bp,
  228. xfs_ino_t owner)
  229. {
  230. struct xfs_mount *mp = bp->b_mount;
  231. if (xfs_has_crc(mp)) {
  232. struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
  233. if (hdr3->hdr.magic != cpu_to_be16(XFS_DA3_NODE_MAGIC))
  234. return __this_address;
  235. if (be64_to_cpu(hdr3->owner) != owner)
  236. return __this_address;
  237. }
  238. return NULL;
  239. }
  240. xfs_failaddr_t
  241. xfs_da3_header_check(
  242. struct xfs_buf *bp,
  243. xfs_ino_t owner)
  244. {
  245. struct xfs_mount *mp = bp->b_mount;
  246. struct xfs_da_blkinfo *hdr = bp->b_addr;
  247. if (!xfs_has_crc(mp))
  248. return NULL;
  249. switch (hdr->magic) {
  250. case cpu_to_be16(XFS_ATTR3_LEAF_MAGIC):
  251. return xfs_attr3_leaf_header_check(bp, owner);
  252. case cpu_to_be16(XFS_DA3_NODE_MAGIC):
  253. return xfs_da3_node_header_check(bp, owner);
  254. case cpu_to_be16(XFS_DIR3_LEAF1_MAGIC):
  255. case cpu_to_be16(XFS_DIR3_LEAFN_MAGIC):
  256. return xfs_dir3_leaf_header_check(bp, owner);
  257. }
  258. ASSERT(0);
  259. return NULL;
  260. }
  261. static void
  262. xfs_da3_node_write_verify(
  263. struct xfs_buf *bp)
  264. {
  265. struct xfs_mount *mp = bp->b_mount;
  266. struct xfs_buf_log_item *bip = bp->b_log_item;
  267. struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
  268. xfs_failaddr_t fa;
  269. fa = xfs_da3_node_verify(bp);
  270. if (fa) {
  271. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  272. return;
  273. }
  274. if (!xfs_has_crc(mp))
  275. return;
  276. if (bip)
  277. hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
  278. xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
  279. }
  280. /*
  281. * leaf/node format detection on trees is sketchy, so a node read can be done on
  282. * leaf level blocks when detection identifies the tree as a node format tree
  283. * incorrectly. In this case, we need to swap the verifier to match the correct
  284. * format of the block being read.
  285. */
  286. static void
  287. xfs_da3_node_read_verify(
  288. struct xfs_buf *bp)
  289. {
  290. struct xfs_da_blkinfo *info = bp->b_addr;
  291. xfs_failaddr_t fa;
  292. switch (be16_to_cpu(info->magic)) {
  293. case XFS_DA3_NODE_MAGIC:
  294. if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
  295. xfs_verifier_error(bp, -EFSBADCRC,
  296. __this_address);
  297. break;
  298. }
  299. fallthrough;
  300. case XFS_DA_NODE_MAGIC:
  301. fa = xfs_da3_node_verify(bp);
  302. if (fa)
  303. xfs_verifier_error(bp, -EFSCORRUPTED, fa);
  304. return;
  305. case XFS_ATTR_LEAF_MAGIC:
  306. case XFS_ATTR3_LEAF_MAGIC:
  307. bp->b_ops = &xfs_attr3_leaf_buf_ops;
  308. bp->b_ops->verify_read(bp);
  309. return;
  310. case XFS_DIR2_LEAFN_MAGIC:
  311. case XFS_DIR3_LEAFN_MAGIC:
  312. bp->b_ops = &xfs_dir3_leafn_buf_ops;
  313. bp->b_ops->verify_read(bp);
  314. return;
  315. default:
  316. xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
  317. break;
  318. }
  319. }
  320. /* Verify the structure of a da3 block. */
  321. static xfs_failaddr_t
  322. xfs_da3_node_verify_struct(
  323. struct xfs_buf *bp)
  324. {
  325. struct xfs_da_blkinfo *info = bp->b_addr;
  326. switch (be16_to_cpu(info->magic)) {
  327. case XFS_DA3_NODE_MAGIC:
  328. case XFS_DA_NODE_MAGIC:
  329. return xfs_da3_node_verify(bp);
  330. case XFS_ATTR_LEAF_MAGIC:
  331. case XFS_ATTR3_LEAF_MAGIC:
  332. bp->b_ops = &xfs_attr3_leaf_buf_ops;
  333. return bp->b_ops->verify_struct(bp);
  334. case XFS_DIR2_LEAFN_MAGIC:
  335. case XFS_DIR3_LEAFN_MAGIC:
  336. bp->b_ops = &xfs_dir3_leafn_buf_ops;
  337. return bp->b_ops->verify_struct(bp);
  338. default:
  339. return __this_address;
  340. }
  341. }
  342. const struct xfs_buf_ops xfs_da3_node_buf_ops = {
  343. .name = "xfs_da3_node",
  344. .magic16 = { cpu_to_be16(XFS_DA_NODE_MAGIC),
  345. cpu_to_be16(XFS_DA3_NODE_MAGIC) },
  346. .verify_read = xfs_da3_node_read_verify,
  347. .verify_write = xfs_da3_node_write_verify,
  348. .verify_struct = xfs_da3_node_verify_struct,
  349. };
  350. static int
  351. xfs_da3_node_set_type(
  352. struct xfs_trans *tp,
  353. struct xfs_inode *dp,
  354. int whichfork,
  355. struct xfs_buf *bp)
  356. {
  357. struct xfs_da_blkinfo *info = bp->b_addr;
  358. switch (be16_to_cpu(info->magic)) {
  359. case XFS_DA_NODE_MAGIC:
  360. case XFS_DA3_NODE_MAGIC:
  361. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
  362. return 0;
  363. case XFS_ATTR_LEAF_MAGIC:
  364. case XFS_ATTR3_LEAF_MAGIC:
  365. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_ATTR_LEAF_BUF);
  366. return 0;
  367. case XFS_DIR2_LEAFN_MAGIC:
  368. case XFS_DIR3_LEAFN_MAGIC:
  369. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
  370. return 0;
  371. default:
  372. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, tp->t_mountp,
  373. info, sizeof(*info));
  374. xfs_trans_brelse(tp, bp);
  375. xfs_dirattr_mark_sick(dp, whichfork);
  376. return -EFSCORRUPTED;
  377. }
  378. }
  379. int
  380. xfs_da3_node_read(
  381. struct xfs_trans *tp,
  382. struct xfs_inode *dp,
  383. xfs_dablk_t bno,
  384. struct xfs_buf **bpp,
  385. int whichfork)
  386. {
  387. int error;
  388. error = xfs_da_read_buf(tp, dp, bno, 0, bpp, whichfork,
  389. &xfs_da3_node_buf_ops);
  390. if (error || !*bpp || !tp)
  391. return error;
  392. return xfs_da3_node_set_type(tp, dp, whichfork, *bpp);
  393. }
  394. int
  395. xfs_da3_node_read_mapped(
  396. struct xfs_trans *tp,
  397. struct xfs_inode *dp,
  398. xfs_daddr_t mappedbno,
  399. struct xfs_buf **bpp,
  400. int whichfork)
  401. {
  402. struct xfs_mount *mp = dp->i_mount;
  403. int error;
  404. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, mappedbno,
  405. XFS_FSB_TO_BB(mp, xfs_dabuf_nfsb(mp, whichfork)), 0,
  406. bpp, &xfs_da3_node_buf_ops);
  407. if (xfs_metadata_is_sick(error))
  408. xfs_dirattr_mark_sick(dp, whichfork);
  409. if (error || !*bpp)
  410. return error;
  411. if (whichfork == XFS_ATTR_FORK)
  412. xfs_buf_set_ref(*bpp, XFS_ATTR_BTREE_REF);
  413. else
  414. xfs_buf_set_ref(*bpp, XFS_DIR_BTREE_REF);
  415. if (!tp)
  416. return 0;
  417. return xfs_da3_node_set_type(tp, dp, whichfork, *bpp);
  418. }
  419. /*
  420. * Copy src directory/attr leaf/node buffer to the dst.
  421. * For v5 file systems make sure the right blkno is stamped in.
  422. */
  423. void
  424. xfs_da_buf_copy(
  425. struct xfs_buf *dst,
  426. struct xfs_buf *src,
  427. size_t size)
  428. {
  429. struct xfs_da3_blkinfo *da3 = dst->b_addr;
  430. memcpy(dst->b_addr, src->b_addr, size);
  431. dst->b_ops = src->b_ops;
  432. xfs_trans_buf_copy_type(dst, src);
  433. if (xfs_has_crc(dst->b_mount))
  434. da3->blkno = cpu_to_be64(xfs_buf_daddr(dst));
  435. }
  436. /*========================================================================
  437. * Routines used for growing the Btree.
  438. *========================================================================*/
  439. /*
  440. * Create the initial contents of an intermediate node.
  441. */
  442. int
  443. xfs_da3_node_create(
  444. struct xfs_da_args *args,
  445. xfs_dablk_t blkno,
  446. int level,
  447. struct xfs_buf **bpp,
  448. int whichfork)
  449. {
  450. struct xfs_da_intnode *node;
  451. struct xfs_trans *tp = args->trans;
  452. struct xfs_mount *mp = tp->t_mountp;
  453. struct xfs_da3_icnode_hdr ichdr = {0};
  454. struct xfs_buf *bp;
  455. int error;
  456. struct xfs_inode *dp = args->dp;
  457. trace_xfs_da_node_create(args);
  458. ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
  459. error = xfs_da_get_buf(tp, dp, blkno, &bp, whichfork);
  460. if (error)
  461. return error;
  462. bp->b_ops = &xfs_da3_node_buf_ops;
  463. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
  464. node = bp->b_addr;
  465. if (xfs_has_crc(mp)) {
  466. struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
  467. memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
  468. ichdr.magic = XFS_DA3_NODE_MAGIC;
  469. hdr3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
  470. hdr3->info.owner = cpu_to_be64(args->owner);
  471. uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
  472. } else {
  473. ichdr.magic = XFS_DA_NODE_MAGIC;
  474. }
  475. ichdr.level = level;
  476. xfs_da3_node_hdr_to_disk(dp->i_mount, node, &ichdr);
  477. xfs_trans_log_buf(tp, bp,
  478. XFS_DA_LOGRANGE(node, &node->hdr, args->geo->node_hdr_size));
  479. *bpp = bp;
  480. return 0;
  481. }
  482. /*
  483. * Split a leaf node, rebalance, then possibly split
  484. * intermediate nodes, rebalance, etc.
  485. */
  486. int /* error */
  487. xfs_da3_split(
  488. struct xfs_da_state *state)
  489. {
  490. struct xfs_da_state_blk *oldblk;
  491. struct xfs_da_state_blk *newblk;
  492. struct xfs_da_state_blk *addblk;
  493. struct xfs_da_intnode *node;
  494. int max;
  495. int action = 0;
  496. int error;
  497. int i;
  498. trace_xfs_da_split(state->args);
  499. if (XFS_TEST_ERROR(false, state->mp, XFS_ERRTAG_DA_LEAF_SPLIT))
  500. return -EIO;
  501. /*
  502. * Walk back up the tree splitting/inserting/adjusting as necessary.
  503. * If we need to insert and there isn't room, split the node, then
  504. * decide which fragment to insert the new block from below into.
  505. * Note that we may split the root this way, but we need more fixup.
  506. */
  507. max = state->path.active - 1;
  508. ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
  509. ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
  510. state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
  511. addblk = &state->path.blk[max]; /* initial dummy value */
  512. for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
  513. oldblk = &state->path.blk[i];
  514. newblk = &state->altpath.blk[i];
  515. /*
  516. * If a leaf node then
  517. * Allocate a new leaf node, then rebalance across them.
  518. * else if an intermediate node then
  519. * We split on the last layer, must we split the node?
  520. */
  521. switch (oldblk->magic) {
  522. case XFS_ATTR_LEAF_MAGIC:
  523. error = xfs_attr3_leaf_split(state, oldblk, newblk);
  524. if (error < 0)
  525. return error; /* GROT: attr is inconsistent */
  526. if (!error) {
  527. addblk = newblk;
  528. break;
  529. }
  530. /*
  531. * Entry wouldn't fit, split the leaf again. The new
  532. * extrablk will be consumed by xfs_da3_node_split if
  533. * the node is split.
  534. */
  535. state->extravalid = 1;
  536. if (state->inleaf) {
  537. state->extraafter = 0; /* before newblk */
  538. trace_xfs_attr_leaf_split_before(state->args);
  539. error = xfs_attr3_leaf_split(state, oldblk,
  540. &state->extrablk);
  541. } else {
  542. state->extraafter = 1; /* after newblk */
  543. trace_xfs_attr_leaf_split_after(state->args);
  544. error = xfs_attr3_leaf_split(state, newblk,
  545. &state->extrablk);
  546. }
  547. if (error == 1)
  548. return -ENOSPC;
  549. if (error)
  550. return error; /* GROT: attr inconsistent */
  551. addblk = newblk;
  552. break;
  553. case XFS_DIR2_LEAFN_MAGIC:
  554. error = xfs_dir2_leafn_split(state, oldblk, newblk);
  555. if (error)
  556. return error;
  557. addblk = newblk;
  558. break;
  559. case XFS_DA_NODE_MAGIC:
  560. error = xfs_da3_node_split(state, oldblk, newblk, addblk,
  561. max - i, &action);
  562. addblk->bp = NULL;
  563. if (error)
  564. return error; /* GROT: dir is inconsistent */
  565. /*
  566. * Record the newly split block for the next time thru?
  567. */
  568. if (action)
  569. addblk = newblk;
  570. else
  571. addblk = NULL;
  572. break;
  573. }
  574. /*
  575. * Update the btree to show the new hashval for this child.
  576. */
  577. xfs_da3_fixhashpath(state, &state->path);
  578. }
  579. if (!addblk)
  580. return 0;
  581. /*
  582. * xfs_da3_node_split() should have consumed any extra blocks we added
  583. * during a double leaf split in the attr fork. This is guaranteed as
  584. * we can't be here if the attr fork only has a single leaf block.
  585. */
  586. ASSERT(state->extravalid == 0 ||
  587. state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
  588. /*
  589. * Split the root node.
  590. */
  591. ASSERT(state->path.active == 0);
  592. oldblk = &state->path.blk[0];
  593. error = xfs_da3_root_split(state, oldblk, addblk);
  594. if (error)
  595. goto out;
  596. /*
  597. * Update pointers to the node which used to be block 0 and just got
  598. * bumped because of the addition of a new root node. Note that the
  599. * original block 0 could be at any position in the list of blocks in
  600. * the tree.
  601. *
  602. * Note: the magic numbers and sibling pointers are in the same physical
  603. * place for both v2 and v3 headers (by design). Hence it doesn't matter
  604. * which version of the xfs_da_intnode structure we use here as the
  605. * result will be the same using either structure.
  606. */
  607. node = oldblk->bp->b_addr;
  608. if (node->hdr.info.forw) {
  609. if (be32_to_cpu(node->hdr.info.forw) != addblk->blkno) {
  610. xfs_buf_mark_corrupt(oldblk->bp);
  611. xfs_da_mark_sick(state->args);
  612. error = -EFSCORRUPTED;
  613. goto out;
  614. }
  615. node = addblk->bp->b_addr;
  616. node->hdr.info.back = cpu_to_be32(oldblk->blkno);
  617. xfs_trans_log_buf(state->args->trans, addblk->bp,
  618. XFS_DA_LOGRANGE(node, &node->hdr.info,
  619. sizeof(node->hdr.info)));
  620. }
  621. node = oldblk->bp->b_addr;
  622. if (node->hdr.info.back) {
  623. if (be32_to_cpu(node->hdr.info.back) != addblk->blkno) {
  624. xfs_buf_mark_corrupt(oldblk->bp);
  625. xfs_da_mark_sick(state->args);
  626. error = -EFSCORRUPTED;
  627. goto out;
  628. }
  629. node = addblk->bp->b_addr;
  630. node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
  631. xfs_trans_log_buf(state->args->trans, addblk->bp,
  632. XFS_DA_LOGRANGE(node, &node->hdr.info,
  633. sizeof(node->hdr.info)));
  634. }
  635. out:
  636. addblk->bp = NULL;
  637. return error;
  638. }
  639. /*
  640. * Split the root. We have to create a new root and point to the two
  641. * parts (the split old root) that we just created. Copy block zero to
  642. * the EOF, extending the inode in process.
  643. */
  644. STATIC int /* error */
  645. xfs_da3_root_split(
  646. struct xfs_da_state *state,
  647. struct xfs_da_state_blk *blk1,
  648. struct xfs_da_state_blk *blk2)
  649. {
  650. struct xfs_da_intnode *node;
  651. struct xfs_da_intnode *oldroot;
  652. struct xfs_da_node_entry *btree;
  653. struct xfs_da3_icnode_hdr nodehdr;
  654. struct xfs_da_args *args;
  655. struct xfs_buf *bp;
  656. struct xfs_inode *dp;
  657. struct xfs_trans *tp;
  658. struct xfs_dir2_leaf *leaf;
  659. xfs_dablk_t blkno;
  660. int level;
  661. int error;
  662. int size;
  663. trace_xfs_da_root_split(state->args);
  664. /*
  665. * Copy the existing (incorrect) block from the root node position
  666. * to a free space somewhere.
  667. */
  668. args = state->args;
  669. error = xfs_da_grow_inode(args, &blkno);
  670. if (error)
  671. return error;
  672. dp = args->dp;
  673. tp = args->trans;
  674. error = xfs_da_get_buf(tp, dp, blkno, &bp, args->whichfork);
  675. if (error)
  676. return error;
  677. node = bp->b_addr;
  678. oldroot = blk1->bp->b_addr;
  679. if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
  680. oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
  681. struct xfs_da3_icnode_hdr icnodehdr;
  682. xfs_da3_node_hdr_from_disk(dp->i_mount, &icnodehdr, oldroot);
  683. btree = icnodehdr.btree;
  684. size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
  685. level = icnodehdr.level;
  686. } else {
  687. struct xfs_dir3_icleaf_hdr leafhdr;
  688. leaf = (xfs_dir2_leaf_t *)oldroot;
  689. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
  690. ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
  691. leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
  692. size = (int)((char *)&leafhdr.ents[leafhdr.count] -
  693. (char *)leaf);
  694. level = 0;
  695. }
  696. /*
  697. * Copy old root to new buffer and log it.
  698. */
  699. xfs_da_buf_copy(bp, blk1->bp, size);
  700. xfs_trans_log_buf(tp, bp, 0, size - 1);
  701. /*
  702. * Update blk1 to point to new buffer.
  703. */
  704. blk1->bp = bp;
  705. blk1->blkno = blkno;
  706. /*
  707. * Set up the new root node.
  708. */
  709. error = xfs_da3_node_create(args,
  710. (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
  711. level + 1, &bp, args->whichfork);
  712. if (error)
  713. return error;
  714. node = bp->b_addr;
  715. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  716. btree = nodehdr.btree;
  717. btree[0].hashval = cpu_to_be32(blk1->hashval);
  718. btree[0].before = cpu_to_be32(blk1->blkno);
  719. btree[1].hashval = cpu_to_be32(blk2->hashval);
  720. btree[1].before = cpu_to_be32(blk2->blkno);
  721. nodehdr.count = 2;
  722. xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
  723. #ifdef DEBUG
  724. if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  725. oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
  726. ASSERT(blk1->blkno >= args->geo->leafblk &&
  727. blk1->blkno < args->geo->freeblk);
  728. ASSERT(blk2->blkno >= args->geo->leafblk &&
  729. blk2->blkno < args->geo->freeblk);
  730. }
  731. #endif
  732. /* Header is already logged by xfs_da_node_create */
  733. xfs_trans_log_buf(tp, bp,
  734. XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
  735. return 0;
  736. }
  737. /*
  738. * Split the node, rebalance, then add the new entry.
  739. */
  740. STATIC int /* error */
  741. xfs_da3_node_split(
  742. struct xfs_da_state *state,
  743. struct xfs_da_state_blk *oldblk,
  744. struct xfs_da_state_blk *newblk,
  745. struct xfs_da_state_blk *addblk,
  746. int treelevel,
  747. int *result)
  748. {
  749. struct xfs_da_intnode *node;
  750. struct xfs_da3_icnode_hdr nodehdr;
  751. xfs_dablk_t blkno;
  752. int newcount;
  753. int error;
  754. int useextra;
  755. struct xfs_inode *dp = state->args->dp;
  756. trace_xfs_da_node_split(state->args);
  757. node = oldblk->bp->b_addr;
  758. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  759. /*
  760. * With V2 dirs the extra block is data or freespace.
  761. */
  762. useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
  763. newcount = 1 + useextra;
  764. /*
  765. * Do we have to split the node?
  766. */
  767. if (nodehdr.count + newcount > state->args->geo->node_ents) {
  768. /*
  769. * Allocate a new node, add to the doubly linked chain of
  770. * nodes, then move some of our excess entries into it.
  771. */
  772. error = xfs_da_grow_inode(state->args, &blkno);
  773. if (error)
  774. return error; /* GROT: dir is inconsistent */
  775. error = xfs_da3_node_create(state->args, blkno, treelevel,
  776. &newblk->bp, state->args->whichfork);
  777. if (error)
  778. return error; /* GROT: dir is inconsistent */
  779. newblk->blkno = blkno;
  780. newblk->magic = XFS_DA_NODE_MAGIC;
  781. xfs_da3_node_rebalance(state, oldblk, newblk);
  782. error = xfs_da3_blk_link(state, oldblk, newblk);
  783. if (error)
  784. return error;
  785. *result = 1;
  786. } else {
  787. *result = 0;
  788. }
  789. /*
  790. * Insert the new entry(s) into the correct block
  791. * (updating last hashval in the process).
  792. *
  793. * xfs_da3_node_add() inserts BEFORE the given index,
  794. * and as a result of using node_lookup_int() we always
  795. * point to a valid entry (not after one), but a split
  796. * operation always results in a new block whose hashvals
  797. * FOLLOW the current block.
  798. *
  799. * If we had double-split op below us, then add the extra block too.
  800. */
  801. node = oldblk->bp->b_addr;
  802. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  803. if (oldblk->index <= nodehdr.count) {
  804. oldblk->index++;
  805. xfs_da3_node_add(state, oldblk, addblk);
  806. if (useextra) {
  807. if (state->extraafter)
  808. oldblk->index++;
  809. xfs_da3_node_add(state, oldblk, &state->extrablk);
  810. state->extravalid = 0;
  811. }
  812. } else {
  813. newblk->index++;
  814. xfs_da3_node_add(state, newblk, addblk);
  815. if (useextra) {
  816. if (state->extraafter)
  817. newblk->index++;
  818. xfs_da3_node_add(state, newblk, &state->extrablk);
  819. state->extravalid = 0;
  820. }
  821. }
  822. return 0;
  823. }
  824. /*
  825. * Balance the btree elements between two intermediate nodes,
  826. * usually one full and one empty.
  827. *
  828. * NOTE: if blk2 is empty, then it will get the upper half of blk1.
  829. */
  830. STATIC void
  831. xfs_da3_node_rebalance(
  832. struct xfs_da_state *state,
  833. struct xfs_da_state_blk *blk1,
  834. struct xfs_da_state_blk *blk2)
  835. {
  836. struct xfs_da_intnode *node1;
  837. struct xfs_da_intnode *node2;
  838. struct xfs_da_node_entry *btree1;
  839. struct xfs_da_node_entry *btree2;
  840. struct xfs_da_node_entry *btree_s;
  841. struct xfs_da_node_entry *btree_d;
  842. struct xfs_da3_icnode_hdr nodehdr1;
  843. struct xfs_da3_icnode_hdr nodehdr2;
  844. struct xfs_trans *tp;
  845. int count;
  846. int tmp;
  847. int swap = 0;
  848. struct xfs_inode *dp = state->args->dp;
  849. trace_xfs_da_node_rebalance(state->args);
  850. node1 = blk1->bp->b_addr;
  851. node2 = blk2->bp->b_addr;
  852. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
  853. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
  854. btree1 = nodehdr1.btree;
  855. btree2 = nodehdr2.btree;
  856. /*
  857. * Figure out how many entries need to move, and in which direction.
  858. * Swap the nodes around if that makes it simpler.
  859. */
  860. if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
  861. ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
  862. (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
  863. be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
  864. swap(node1, node2);
  865. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
  866. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
  867. btree1 = nodehdr1.btree;
  868. btree2 = nodehdr2.btree;
  869. swap = 1;
  870. }
  871. count = (nodehdr1.count - nodehdr2.count) / 2;
  872. if (count == 0)
  873. return;
  874. tp = state->args->trans;
  875. /*
  876. * Two cases: high-to-low and low-to-high.
  877. */
  878. if (count > 0) {
  879. /*
  880. * Move elements in node2 up to make a hole.
  881. */
  882. tmp = nodehdr2.count;
  883. if (tmp > 0) {
  884. tmp *= (uint)sizeof(xfs_da_node_entry_t);
  885. btree_s = &btree2[0];
  886. btree_d = &btree2[count];
  887. memmove(btree_d, btree_s, tmp);
  888. }
  889. /*
  890. * Move the req'd B-tree elements from high in node1 to
  891. * low in node2.
  892. */
  893. nodehdr2.count += count;
  894. tmp = count * (uint)sizeof(xfs_da_node_entry_t);
  895. btree_s = &btree1[nodehdr1.count - count];
  896. btree_d = &btree2[0];
  897. memcpy(btree_d, btree_s, tmp);
  898. nodehdr1.count -= count;
  899. } else {
  900. /*
  901. * Move the req'd B-tree elements from low in node2 to
  902. * high in node1.
  903. */
  904. count = -count;
  905. tmp = count * (uint)sizeof(xfs_da_node_entry_t);
  906. btree_s = &btree2[0];
  907. btree_d = &btree1[nodehdr1.count];
  908. memcpy(btree_d, btree_s, tmp);
  909. nodehdr1.count += count;
  910. xfs_trans_log_buf(tp, blk1->bp,
  911. XFS_DA_LOGRANGE(node1, btree_d, tmp));
  912. /*
  913. * Move elements in node2 down to fill the hole.
  914. */
  915. tmp = nodehdr2.count - count;
  916. tmp *= (uint)sizeof(xfs_da_node_entry_t);
  917. btree_s = &btree2[count];
  918. btree_d = &btree2[0];
  919. memmove(btree_d, btree_s, tmp);
  920. nodehdr2.count -= count;
  921. }
  922. /*
  923. * Log header of node 1 and all current bits of node 2.
  924. */
  925. xfs_da3_node_hdr_to_disk(dp->i_mount, node1, &nodehdr1);
  926. xfs_trans_log_buf(tp, blk1->bp,
  927. XFS_DA_LOGRANGE(node1, &node1->hdr,
  928. state->args->geo->node_hdr_size));
  929. xfs_da3_node_hdr_to_disk(dp->i_mount, node2, &nodehdr2);
  930. xfs_trans_log_buf(tp, blk2->bp,
  931. XFS_DA_LOGRANGE(node2, &node2->hdr,
  932. state->args->geo->node_hdr_size +
  933. (sizeof(btree2[0]) * nodehdr2.count)));
  934. /*
  935. * Record the last hashval from each block for upward propagation.
  936. * (note: don't use the swapped node pointers)
  937. */
  938. if (swap) {
  939. node1 = blk1->bp->b_addr;
  940. node2 = blk2->bp->b_addr;
  941. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
  942. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
  943. btree1 = nodehdr1.btree;
  944. btree2 = nodehdr2.btree;
  945. }
  946. blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
  947. blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
  948. /*
  949. * Adjust the expected index for insertion.
  950. */
  951. if (blk1->index >= nodehdr1.count) {
  952. blk2->index = blk1->index - nodehdr1.count;
  953. blk1->index = nodehdr1.count + 1; /* make it invalid */
  954. }
  955. }
  956. /*
  957. * Add a new entry to an intermediate node.
  958. */
  959. STATIC void
  960. xfs_da3_node_add(
  961. struct xfs_da_state *state,
  962. struct xfs_da_state_blk *oldblk,
  963. struct xfs_da_state_blk *newblk)
  964. {
  965. struct xfs_da_intnode *node;
  966. struct xfs_da3_icnode_hdr nodehdr;
  967. struct xfs_da_node_entry *btree;
  968. int tmp;
  969. struct xfs_inode *dp = state->args->dp;
  970. trace_xfs_da_node_add(state->args);
  971. node = oldblk->bp->b_addr;
  972. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  973. btree = nodehdr.btree;
  974. ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
  975. ASSERT(newblk->blkno != 0);
  976. if (state->args->whichfork == XFS_DATA_FORK)
  977. ASSERT(newblk->blkno >= state->args->geo->leafblk &&
  978. newblk->blkno < state->args->geo->freeblk);
  979. /*
  980. * We may need to make some room before we insert the new node.
  981. */
  982. tmp = 0;
  983. if (oldblk->index < nodehdr.count) {
  984. tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
  985. memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
  986. }
  987. btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
  988. btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
  989. xfs_trans_log_buf(state->args->trans, oldblk->bp,
  990. XFS_DA_LOGRANGE(node, &btree[oldblk->index],
  991. tmp + sizeof(*btree)));
  992. nodehdr.count += 1;
  993. xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
  994. xfs_trans_log_buf(state->args->trans, oldblk->bp,
  995. XFS_DA_LOGRANGE(node, &node->hdr,
  996. state->args->geo->node_hdr_size));
  997. /*
  998. * Copy the last hash value from the oldblk to propagate upwards.
  999. */
  1000. oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
  1001. }
  1002. /*========================================================================
  1003. * Routines used for shrinking the Btree.
  1004. *========================================================================*/
  1005. /*
  1006. * Deallocate an empty leaf node, remove it from its parent,
  1007. * possibly deallocating that block, etc...
  1008. */
  1009. int
  1010. xfs_da3_join(
  1011. struct xfs_da_state *state)
  1012. {
  1013. struct xfs_da_state_blk *drop_blk;
  1014. struct xfs_da_state_blk *save_blk;
  1015. int action = 0;
  1016. int error;
  1017. trace_xfs_da_join(state->args);
  1018. drop_blk = &state->path.blk[ state->path.active-1 ];
  1019. save_blk = &state->altpath.blk[ state->path.active-1 ];
  1020. ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
  1021. ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
  1022. drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1023. /*
  1024. * Walk back up the tree joining/deallocating as necessary.
  1025. * When we stop dropping blocks, break out.
  1026. */
  1027. for ( ; state->path.active >= 2; drop_blk--, save_blk--,
  1028. state->path.active--) {
  1029. /*
  1030. * See if we can combine the block with a neighbor.
  1031. * (action == 0) => no options, just leave
  1032. * (action == 1) => coalesce, then unlink
  1033. * (action == 2) => block empty, unlink it
  1034. */
  1035. switch (drop_blk->magic) {
  1036. case XFS_ATTR_LEAF_MAGIC:
  1037. error = xfs_attr3_leaf_toosmall(state, &action);
  1038. if (error)
  1039. return error;
  1040. if (action == 0)
  1041. return 0;
  1042. xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
  1043. break;
  1044. case XFS_DIR2_LEAFN_MAGIC:
  1045. error = xfs_dir2_leafn_toosmall(state, &action);
  1046. if (error)
  1047. return error;
  1048. if (action == 0)
  1049. return 0;
  1050. xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
  1051. break;
  1052. case XFS_DA_NODE_MAGIC:
  1053. /*
  1054. * Remove the offending node, fixup hashvals,
  1055. * check for a toosmall neighbor.
  1056. */
  1057. xfs_da3_node_remove(state, drop_blk);
  1058. xfs_da3_fixhashpath(state, &state->path);
  1059. error = xfs_da3_node_toosmall(state, &action);
  1060. if (error)
  1061. return error;
  1062. if (action == 0)
  1063. return 0;
  1064. xfs_da3_node_unbalance(state, drop_blk, save_blk);
  1065. break;
  1066. }
  1067. xfs_da3_fixhashpath(state, &state->altpath);
  1068. error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
  1069. xfs_da_state_kill_altpath(state);
  1070. if (error)
  1071. return error;
  1072. error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
  1073. drop_blk->bp);
  1074. drop_blk->bp = NULL;
  1075. if (error)
  1076. return error;
  1077. }
  1078. /*
  1079. * We joined all the way to the top. If it turns out that
  1080. * we only have one entry in the root, make the child block
  1081. * the new root.
  1082. */
  1083. xfs_da3_node_remove(state, drop_blk);
  1084. xfs_da3_fixhashpath(state, &state->path);
  1085. error = xfs_da3_root_join(state, &state->path.blk[0]);
  1086. return error;
  1087. }
  1088. #ifdef DEBUG
  1089. static void
  1090. xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
  1091. {
  1092. __be16 magic = blkinfo->magic;
  1093. if (level == 1) {
  1094. ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1095. magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
  1096. magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
  1097. magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
  1098. } else {
  1099. ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
  1100. magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
  1101. }
  1102. ASSERT(!blkinfo->forw);
  1103. ASSERT(!blkinfo->back);
  1104. }
  1105. #else /* !DEBUG */
  1106. #define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
  1107. #endif /* !DEBUG */
  1108. /*
  1109. * We have only one entry in the root. Copy the only remaining child of
  1110. * the old root to block 0 as the new root node.
  1111. */
  1112. STATIC int
  1113. xfs_da3_root_join(
  1114. struct xfs_da_state *state,
  1115. struct xfs_da_state_blk *root_blk)
  1116. {
  1117. struct xfs_da_intnode *oldroot;
  1118. struct xfs_da_args *args;
  1119. xfs_dablk_t child;
  1120. struct xfs_buf *bp;
  1121. struct xfs_da3_icnode_hdr oldroothdr;
  1122. int error;
  1123. struct xfs_inode *dp = state->args->dp;
  1124. xfs_failaddr_t fa;
  1125. trace_xfs_da_root_join(state->args);
  1126. ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
  1127. args = state->args;
  1128. oldroot = root_blk->bp->b_addr;
  1129. xfs_da3_node_hdr_from_disk(dp->i_mount, &oldroothdr, oldroot);
  1130. ASSERT(oldroothdr.forw == 0);
  1131. ASSERT(oldroothdr.back == 0);
  1132. /*
  1133. * If the root has more than one child, then don't do anything.
  1134. */
  1135. if (oldroothdr.count > 1)
  1136. return 0;
  1137. /*
  1138. * Read in the (only) child block, then copy those bytes into
  1139. * the root block's buffer and free the original child block.
  1140. */
  1141. child = be32_to_cpu(oldroothdr.btree[0].before);
  1142. ASSERT(child != 0);
  1143. error = xfs_da3_node_read(args->trans, dp, child, &bp, args->whichfork);
  1144. if (error)
  1145. return error;
  1146. fa = xfs_da3_header_check(bp, args->owner);
  1147. if (fa) {
  1148. __xfs_buf_mark_corrupt(bp, fa);
  1149. xfs_trans_brelse(args->trans, bp);
  1150. xfs_da_mark_sick(args);
  1151. return -EFSCORRUPTED;
  1152. }
  1153. xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
  1154. /*
  1155. * Copy child to root buffer and log it.
  1156. */
  1157. xfs_da_buf_copy(root_blk->bp, bp, args->geo->blksize);
  1158. xfs_trans_log_buf(args->trans, root_blk->bp, 0,
  1159. args->geo->blksize - 1);
  1160. /*
  1161. * Now we can drop the child buffer.
  1162. */
  1163. error = xfs_da_shrink_inode(args, child, bp);
  1164. return error;
  1165. }
  1166. /*
  1167. * Check a node block and its neighbors to see if the block should be
  1168. * collapsed into one or the other neighbor. Always keep the block
  1169. * with the smaller block number.
  1170. * If the current block is over 50% full, don't try to join it, return 0.
  1171. * If the block is empty, fill in the state structure and return 2.
  1172. * If it can be collapsed, fill in the state structure and return 1.
  1173. * If nothing can be done, return 0.
  1174. */
  1175. STATIC int
  1176. xfs_da3_node_toosmall(
  1177. struct xfs_da_state *state,
  1178. int *action)
  1179. {
  1180. struct xfs_da_intnode *node;
  1181. struct xfs_da_state_blk *blk;
  1182. struct xfs_da_blkinfo *info;
  1183. xfs_dablk_t blkno;
  1184. struct xfs_buf *bp;
  1185. xfs_failaddr_t fa;
  1186. struct xfs_da3_icnode_hdr nodehdr;
  1187. int count;
  1188. int forward;
  1189. int error;
  1190. int retval;
  1191. int i;
  1192. struct xfs_inode *dp = state->args->dp;
  1193. trace_xfs_da_node_toosmall(state->args);
  1194. /*
  1195. * Check for the degenerate case of the block being over 50% full.
  1196. * If so, it's not worth even looking to see if we might be able
  1197. * to coalesce with a sibling.
  1198. */
  1199. blk = &state->path.blk[ state->path.active-1 ];
  1200. info = blk->bp->b_addr;
  1201. node = (xfs_da_intnode_t *)info;
  1202. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  1203. if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
  1204. *action = 0; /* blk over 50%, don't try to join */
  1205. return 0; /* blk over 50%, don't try to join */
  1206. }
  1207. /*
  1208. * Check for the degenerate case of the block being empty.
  1209. * If the block is empty, we'll simply delete it, no need to
  1210. * coalesce it with a sibling block. We choose (arbitrarily)
  1211. * to merge with the forward block unless it is NULL.
  1212. */
  1213. if (nodehdr.count == 0) {
  1214. /*
  1215. * Make altpath point to the block we want to keep and
  1216. * path point to the block we want to drop (this one).
  1217. */
  1218. forward = (info->forw != 0);
  1219. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1220. error = xfs_da3_path_shift(state, &state->altpath, forward,
  1221. 0, &retval);
  1222. if (error)
  1223. return error;
  1224. if (retval) {
  1225. *action = 0;
  1226. } else {
  1227. *action = 2;
  1228. }
  1229. return 0;
  1230. }
  1231. /*
  1232. * Examine each sibling block to see if we can coalesce with
  1233. * at least 25% free space to spare. We need to figure out
  1234. * whether to merge with the forward or the backward block.
  1235. * We prefer coalescing with the lower numbered sibling so as
  1236. * to shrink a directory over time.
  1237. */
  1238. count = state->args->geo->node_ents;
  1239. count -= state->args->geo->node_ents >> 2;
  1240. count -= nodehdr.count;
  1241. /* start with smaller blk num */
  1242. forward = nodehdr.forw < nodehdr.back;
  1243. for (i = 0; i < 2; forward = !forward, i++) {
  1244. struct xfs_da3_icnode_hdr thdr;
  1245. if (forward)
  1246. blkno = nodehdr.forw;
  1247. else
  1248. blkno = nodehdr.back;
  1249. if (blkno == 0)
  1250. continue;
  1251. error = xfs_da3_node_read(state->args->trans, dp, blkno, &bp,
  1252. state->args->whichfork);
  1253. if (error)
  1254. return error;
  1255. fa = xfs_da3_node_header_check(bp, state->args->owner);
  1256. if (fa) {
  1257. __xfs_buf_mark_corrupt(bp, fa);
  1258. xfs_trans_brelse(state->args->trans, bp);
  1259. xfs_da_mark_sick(state->args);
  1260. return -EFSCORRUPTED;
  1261. }
  1262. node = bp->b_addr;
  1263. xfs_da3_node_hdr_from_disk(dp->i_mount, &thdr, node);
  1264. xfs_trans_brelse(state->args->trans, bp);
  1265. if (count - thdr.count >= 0)
  1266. break; /* fits with at least 25% to spare */
  1267. }
  1268. if (i >= 2) {
  1269. *action = 0;
  1270. return 0;
  1271. }
  1272. /*
  1273. * Make altpath point to the block we want to keep (the lower
  1274. * numbered block) and path point to the block we want to drop.
  1275. */
  1276. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1277. if (blkno < blk->blkno) {
  1278. error = xfs_da3_path_shift(state, &state->altpath, forward,
  1279. 0, &retval);
  1280. } else {
  1281. error = xfs_da3_path_shift(state, &state->path, forward,
  1282. 0, &retval);
  1283. }
  1284. if (error)
  1285. return error;
  1286. if (retval) {
  1287. *action = 0;
  1288. return 0;
  1289. }
  1290. *action = 1;
  1291. return 0;
  1292. }
  1293. /*
  1294. * Pick up the last hashvalue from an intermediate node.
  1295. */
  1296. STATIC uint
  1297. xfs_da3_node_lasthash(
  1298. struct xfs_inode *dp,
  1299. struct xfs_buf *bp,
  1300. int *count)
  1301. {
  1302. struct xfs_da3_icnode_hdr nodehdr;
  1303. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, bp->b_addr);
  1304. if (count)
  1305. *count = nodehdr.count;
  1306. if (!nodehdr.count)
  1307. return 0;
  1308. return be32_to_cpu(nodehdr.btree[nodehdr.count - 1].hashval);
  1309. }
  1310. /*
  1311. * Walk back up the tree adjusting hash values as necessary,
  1312. * when we stop making changes, return.
  1313. */
  1314. void
  1315. xfs_da3_fixhashpath(
  1316. struct xfs_da_state *state,
  1317. struct xfs_da_state_path *path)
  1318. {
  1319. struct xfs_da_state_blk *blk;
  1320. struct xfs_da_intnode *node;
  1321. struct xfs_da_node_entry *btree;
  1322. xfs_dahash_t lasthash=0;
  1323. int level;
  1324. int count;
  1325. struct xfs_inode *dp = state->args->dp;
  1326. trace_xfs_da_fixhashpath(state->args);
  1327. level = path->active-1;
  1328. blk = &path->blk[ level ];
  1329. switch (blk->magic) {
  1330. case XFS_ATTR_LEAF_MAGIC:
  1331. lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
  1332. if (count == 0)
  1333. return;
  1334. break;
  1335. case XFS_DIR2_LEAFN_MAGIC:
  1336. lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
  1337. if (count == 0)
  1338. return;
  1339. break;
  1340. case XFS_DA_NODE_MAGIC:
  1341. lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
  1342. if (count == 0)
  1343. return;
  1344. break;
  1345. }
  1346. for (blk--, level--; level >= 0; blk--, level--) {
  1347. struct xfs_da3_icnode_hdr nodehdr;
  1348. node = blk->bp->b_addr;
  1349. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  1350. btree = nodehdr.btree;
  1351. if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
  1352. break;
  1353. blk->hashval = lasthash;
  1354. btree[blk->index].hashval = cpu_to_be32(lasthash);
  1355. xfs_trans_log_buf(state->args->trans, blk->bp,
  1356. XFS_DA_LOGRANGE(node, &btree[blk->index],
  1357. sizeof(*btree)));
  1358. lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
  1359. }
  1360. }
  1361. /*
  1362. * Remove an entry from an intermediate node.
  1363. */
  1364. STATIC void
  1365. xfs_da3_node_remove(
  1366. struct xfs_da_state *state,
  1367. struct xfs_da_state_blk *drop_blk)
  1368. {
  1369. struct xfs_da_intnode *node;
  1370. struct xfs_da3_icnode_hdr nodehdr;
  1371. struct xfs_da_node_entry *btree;
  1372. int index;
  1373. int tmp;
  1374. struct xfs_inode *dp = state->args->dp;
  1375. trace_xfs_da_node_remove(state->args);
  1376. node = drop_blk->bp->b_addr;
  1377. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  1378. ASSERT(drop_blk->index < nodehdr.count);
  1379. ASSERT(drop_blk->index >= 0);
  1380. /*
  1381. * Copy over the offending entry, or just zero it out.
  1382. */
  1383. index = drop_blk->index;
  1384. btree = nodehdr.btree;
  1385. if (index < nodehdr.count - 1) {
  1386. tmp = nodehdr.count - index - 1;
  1387. tmp *= (uint)sizeof(xfs_da_node_entry_t);
  1388. memmove(&btree[index], &btree[index + 1], tmp);
  1389. xfs_trans_log_buf(state->args->trans, drop_blk->bp,
  1390. XFS_DA_LOGRANGE(node, &btree[index], tmp));
  1391. index = nodehdr.count - 1;
  1392. }
  1393. memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
  1394. xfs_trans_log_buf(state->args->trans, drop_blk->bp,
  1395. XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
  1396. nodehdr.count -= 1;
  1397. xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
  1398. xfs_trans_log_buf(state->args->trans, drop_blk->bp,
  1399. XFS_DA_LOGRANGE(node, &node->hdr, state->args->geo->node_hdr_size));
  1400. /*
  1401. * Copy the last hash value from the block to propagate upwards.
  1402. */
  1403. drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
  1404. }
  1405. /*
  1406. * Unbalance the elements between two intermediate nodes,
  1407. * move all Btree elements from one node into another.
  1408. */
  1409. STATIC void
  1410. xfs_da3_node_unbalance(
  1411. struct xfs_da_state *state,
  1412. struct xfs_da_state_blk *drop_blk,
  1413. struct xfs_da_state_blk *save_blk)
  1414. {
  1415. struct xfs_da_intnode *drop_node;
  1416. struct xfs_da_intnode *save_node;
  1417. struct xfs_da_node_entry *drop_btree;
  1418. struct xfs_da_node_entry *save_btree;
  1419. struct xfs_da3_icnode_hdr drop_hdr;
  1420. struct xfs_da3_icnode_hdr save_hdr;
  1421. struct xfs_trans *tp;
  1422. int sindex;
  1423. int tmp;
  1424. struct xfs_inode *dp = state->args->dp;
  1425. trace_xfs_da_node_unbalance(state->args);
  1426. drop_node = drop_blk->bp->b_addr;
  1427. save_node = save_blk->bp->b_addr;
  1428. xfs_da3_node_hdr_from_disk(dp->i_mount, &drop_hdr, drop_node);
  1429. xfs_da3_node_hdr_from_disk(dp->i_mount, &save_hdr, save_node);
  1430. drop_btree = drop_hdr.btree;
  1431. save_btree = save_hdr.btree;
  1432. tp = state->args->trans;
  1433. /*
  1434. * If the dying block has lower hashvals, then move all the
  1435. * elements in the remaining block up to make a hole.
  1436. */
  1437. if ((be32_to_cpu(drop_btree[0].hashval) <
  1438. be32_to_cpu(save_btree[0].hashval)) ||
  1439. (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
  1440. be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
  1441. /* XXX: check this - is memmove dst correct? */
  1442. tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
  1443. memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
  1444. sindex = 0;
  1445. xfs_trans_log_buf(tp, save_blk->bp,
  1446. XFS_DA_LOGRANGE(save_node, &save_btree[0],
  1447. (save_hdr.count + drop_hdr.count) *
  1448. sizeof(xfs_da_node_entry_t)));
  1449. } else {
  1450. sindex = save_hdr.count;
  1451. xfs_trans_log_buf(tp, save_blk->bp,
  1452. XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
  1453. drop_hdr.count * sizeof(xfs_da_node_entry_t)));
  1454. }
  1455. /*
  1456. * Move all the B-tree elements from drop_blk to save_blk.
  1457. */
  1458. tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
  1459. memcpy(&save_btree[sindex], &drop_btree[0], tmp);
  1460. save_hdr.count += drop_hdr.count;
  1461. xfs_da3_node_hdr_to_disk(dp->i_mount, save_node, &save_hdr);
  1462. xfs_trans_log_buf(tp, save_blk->bp,
  1463. XFS_DA_LOGRANGE(save_node, &save_node->hdr,
  1464. state->args->geo->node_hdr_size));
  1465. /*
  1466. * Save the last hashval in the remaining block for upward propagation.
  1467. */
  1468. save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
  1469. }
  1470. /*========================================================================
  1471. * Routines used for finding things in the Btree.
  1472. *========================================================================*/
  1473. /*
  1474. * Walk down the Btree looking for a particular filename, filling
  1475. * in the state structure as we go.
  1476. *
  1477. * We will set the state structure to point to each of the elements
  1478. * in each of the nodes where either the hashval is or should be.
  1479. *
  1480. * We support duplicate hashval's so for each entry in the current
  1481. * node that could contain the desired hashval, descend. This is a
  1482. * pruned depth-first tree search.
  1483. */
  1484. int /* error */
  1485. xfs_da3_node_lookup_int(
  1486. struct xfs_da_state *state,
  1487. int *result)
  1488. {
  1489. struct xfs_da_state_blk *blk;
  1490. struct xfs_da_blkinfo *curr;
  1491. struct xfs_da_intnode *node;
  1492. struct xfs_da_node_entry *btree;
  1493. struct xfs_da3_icnode_hdr nodehdr;
  1494. struct xfs_da_args *args;
  1495. xfs_failaddr_t fa;
  1496. xfs_dablk_t blkno;
  1497. xfs_dahash_t hashval;
  1498. xfs_dahash_t btreehashval;
  1499. int probe;
  1500. int span;
  1501. int max;
  1502. int error;
  1503. int retval;
  1504. unsigned int expected_level = 0;
  1505. uint16_t magic;
  1506. struct xfs_inode *dp = state->args->dp;
  1507. args = state->args;
  1508. /*
  1509. * Descend thru the B-tree searching each level for the right
  1510. * node to use, until the right hashval is found.
  1511. */
  1512. blkno = args->geo->leafblk;
  1513. for (blk = &state->path.blk[0], state->path.active = 1;
  1514. state->path.active <= XFS_DA_NODE_MAXDEPTH;
  1515. blk++, state->path.active++) {
  1516. /*
  1517. * Read the next node down in the tree.
  1518. */
  1519. blk->blkno = blkno;
  1520. error = xfs_da3_node_read(args->trans, args->dp, blkno,
  1521. &blk->bp, args->whichfork);
  1522. if (error) {
  1523. blk->blkno = 0;
  1524. state->path.active--;
  1525. return error;
  1526. }
  1527. curr = blk->bp->b_addr;
  1528. magic = be16_to_cpu(curr->magic);
  1529. if (magic == XFS_ATTR_LEAF_MAGIC ||
  1530. magic == XFS_ATTR3_LEAF_MAGIC) {
  1531. fa = xfs_attr3_leaf_header_check(blk->bp, args->owner);
  1532. if (fa) {
  1533. __xfs_buf_mark_corrupt(blk->bp, fa);
  1534. xfs_da_mark_sick(args);
  1535. return -EFSCORRUPTED;
  1536. }
  1537. blk->magic = XFS_ATTR_LEAF_MAGIC;
  1538. blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
  1539. break;
  1540. }
  1541. if (magic == XFS_DIR2_LEAFN_MAGIC ||
  1542. magic == XFS_DIR3_LEAFN_MAGIC) {
  1543. fa = xfs_dir3_leaf_header_check(blk->bp, args->owner);
  1544. if (fa) {
  1545. __xfs_buf_mark_corrupt(blk->bp, fa);
  1546. xfs_da_mark_sick(args);
  1547. return -EFSCORRUPTED;
  1548. }
  1549. blk->magic = XFS_DIR2_LEAFN_MAGIC;
  1550. blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
  1551. blk->bp, NULL);
  1552. break;
  1553. }
  1554. if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) {
  1555. xfs_buf_mark_corrupt(blk->bp);
  1556. xfs_da_mark_sick(args);
  1557. return -EFSCORRUPTED;
  1558. }
  1559. fa = xfs_da3_node_header_check(blk->bp, args->owner);
  1560. if (fa) {
  1561. __xfs_buf_mark_corrupt(blk->bp, fa);
  1562. xfs_da_mark_sick(args);
  1563. return -EFSCORRUPTED;
  1564. }
  1565. blk->magic = XFS_DA_NODE_MAGIC;
  1566. /*
  1567. * Search an intermediate node for a match.
  1568. */
  1569. node = blk->bp->b_addr;
  1570. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
  1571. btree = nodehdr.btree;
  1572. /* Tree taller than we can handle; bail out! */
  1573. if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
  1574. xfs_buf_mark_corrupt(blk->bp);
  1575. xfs_da_mark_sick(args);
  1576. return -EFSCORRUPTED;
  1577. }
  1578. /* Check the level from the root. */
  1579. if (blkno == args->geo->leafblk)
  1580. expected_level = nodehdr.level - 1;
  1581. else if (expected_level != nodehdr.level) {
  1582. xfs_buf_mark_corrupt(blk->bp);
  1583. xfs_da_mark_sick(args);
  1584. return -EFSCORRUPTED;
  1585. } else
  1586. expected_level--;
  1587. max = nodehdr.count;
  1588. blk->hashval = be32_to_cpu(btree[max - 1].hashval);
  1589. /*
  1590. * Binary search. (note: small blocks will skip loop)
  1591. */
  1592. probe = span = max / 2;
  1593. hashval = args->hashval;
  1594. while (span > 4) {
  1595. span /= 2;
  1596. btreehashval = be32_to_cpu(btree[probe].hashval);
  1597. if (btreehashval < hashval)
  1598. probe += span;
  1599. else if (btreehashval > hashval)
  1600. probe -= span;
  1601. else
  1602. break;
  1603. }
  1604. ASSERT((probe >= 0) && (probe < max));
  1605. ASSERT((span <= 4) ||
  1606. (be32_to_cpu(btree[probe].hashval) == hashval));
  1607. /*
  1608. * Since we may have duplicate hashval's, find the first
  1609. * matching hashval in the node.
  1610. */
  1611. while (probe > 0 &&
  1612. be32_to_cpu(btree[probe].hashval) >= hashval) {
  1613. probe--;
  1614. }
  1615. while (probe < max &&
  1616. be32_to_cpu(btree[probe].hashval) < hashval) {
  1617. probe++;
  1618. }
  1619. /*
  1620. * Pick the right block to descend on.
  1621. */
  1622. if (probe == max) {
  1623. blk->index = max - 1;
  1624. blkno = be32_to_cpu(btree[max - 1].before);
  1625. } else {
  1626. blk->index = probe;
  1627. blkno = be32_to_cpu(btree[probe].before);
  1628. }
  1629. /* We can't point back to the root. */
  1630. if (XFS_IS_CORRUPT(dp->i_mount, blkno == args->geo->leafblk)) {
  1631. xfs_da_mark_sick(args);
  1632. return -EFSCORRUPTED;
  1633. }
  1634. }
  1635. if (XFS_IS_CORRUPT(dp->i_mount, expected_level != 0)) {
  1636. xfs_da_mark_sick(args);
  1637. return -EFSCORRUPTED;
  1638. }
  1639. /*
  1640. * A leaf block that ends in the hashval that we are interested in
  1641. * (final hashval == search hashval) means that the next block may
  1642. * contain more entries with the same hashval, shift upward to the
  1643. * next leaf and keep searching.
  1644. */
  1645. for (;;) {
  1646. if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
  1647. retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
  1648. &blk->index, state);
  1649. } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
  1650. retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
  1651. blk->index = args->index;
  1652. args->blkno = blk->blkno;
  1653. } else {
  1654. ASSERT(0);
  1655. xfs_da_mark_sick(args);
  1656. return -EFSCORRUPTED;
  1657. }
  1658. if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
  1659. (blk->hashval == args->hashval)) {
  1660. error = xfs_da3_path_shift(state, &state->path, 1, 1,
  1661. &retval);
  1662. if (error)
  1663. return error;
  1664. if (retval == 0) {
  1665. continue;
  1666. } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
  1667. /* path_shift() gives ENOENT */
  1668. retval = -ENOATTR;
  1669. }
  1670. }
  1671. break;
  1672. }
  1673. *result = retval;
  1674. return 0;
  1675. }
  1676. /*========================================================================
  1677. * Utility routines.
  1678. *========================================================================*/
  1679. /*
  1680. * Compare two intermediate nodes for "order".
  1681. */
  1682. STATIC int
  1683. xfs_da3_node_order(
  1684. struct xfs_inode *dp,
  1685. struct xfs_buf *node1_bp,
  1686. struct xfs_buf *node2_bp)
  1687. {
  1688. struct xfs_da_intnode *node1;
  1689. struct xfs_da_intnode *node2;
  1690. struct xfs_da_node_entry *btree1;
  1691. struct xfs_da_node_entry *btree2;
  1692. struct xfs_da3_icnode_hdr node1hdr;
  1693. struct xfs_da3_icnode_hdr node2hdr;
  1694. node1 = node1_bp->b_addr;
  1695. node2 = node2_bp->b_addr;
  1696. xfs_da3_node_hdr_from_disk(dp->i_mount, &node1hdr, node1);
  1697. xfs_da3_node_hdr_from_disk(dp->i_mount, &node2hdr, node2);
  1698. btree1 = node1hdr.btree;
  1699. btree2 = node2hdr.btree;
  1700. if (node1hdr.count > 0 && node2hdr.count > 0 &&
  1701. ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
  1702. (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
  1703. be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
  1704. return 1;
  1705. }
  1706. return 0;
  1707. }
  1708. /*
  1709. * Link a new block into a doubly linked list of blocks (of whatever type).
  1710. */
  1711. int /* error */
  1712. xfs_da3_blk_link(
  1713. struct xfs_da_state *state,
  1714. struct xfs_da_state_blk *old_blk,
  1715. struct xfs_da_state_blk *new_blk)
  1716. {
  1717. struct xfs_da_blkinfo *old_info;
  1718. struct xfs_da_blkinfo *new_info;
  1719. struct xfs_da_blkinfo *tmp_info;
  1720. struct xfs_da_args *args;
  1721. struct xfs_buf *bp;
  1722. xfs_failaddr_t fa;
  1723. int before = 0;
  1724. int error;
  1725. struct xfs_inode *dp = state->args->dp;
  1726. /*
  1727. * Set up environment.
  1728. */
  1729. args = state->args;
  1730. ASSERT(args != NULL);
  1731. old_info = old_blk->bp->b_addr;
  1732. new_info = new_blk->bp->b_addr;
  1733. ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
  1734. old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
  1735. old_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1736. switch (old_blk->magic) {
  1737. case XFS_ATTR_LEAF_MAGIC:
  1738. before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
  1739. break;
  1740. case XFS_DIR2_LEAFN_MAGIC:
  1741. before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
  1742. break;
  1743. case XFS_DA_NODE_MAGIC:
  1744. before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
  1745. break;
  1746. }
  1747. /*
  1748. * Link blocks in appropriate order.
  1749. */
  1750. if (before) {
  1751. /*
  1752. * Link new block in before existing block.
  1753. */
  1754. trace_xfs_da_link_before(args);
  1755. new_info->forw = cpu_to_be32(old_blk->blkno);
  1756. new_info->back = old_info->back;
  1757. if (old_info->back) {
  1758. error = xfs_da3_node_read(args->trans, dp,
  1759. be32_to_cpu(old_info->back),
  1760. &bp, args->whichfork);
  1761. if (error)
  1762. return error;
  1763. fa = xfs_da3_header_check(bp, args->owner);
  1764. if (fa) {
  1765. __xfs_buf_mark_corrupt(bp, fa);
  1766. xfs_trans_brelse(args->trans, bp);
  1767. xfs_da_mark_sick(args);
  1768. return -EFSCORRUPTED;
  1769. }
  1770. ASSERT(bp != NULL);
  1771. tmp_info = bp->b_addr;
  1772. ASSERT(tmp_info->magic == old_info->magic);
  1773. ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
  1774. tmp_info->forw = cpu_to_be32(new_blk->blkno);
  1775. xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
  1776. }
  1777. old_info->back = cpu_to_be32(new_blk->blkno);
  1778. } else {
  1779. /*
  1780. * Link new block in after existing block.
  1781. */
  1782. trace_xfs_da_link_after(args);
  1783. new_info->forw = old_info->forw;
  1784. new_info->back = cpu_to_be32(old_blk->blkno);
  1785. if (old_info->forw) {
  1786. error = xfs_da3_node_read(args->trans, dp,
  1787. be32_to_cpu(old_info->forw),
  1788. &bp, args->whichfork);
  1789. if (error)
  1790. return error;
  1791. fa = xfs_da3_header_check(bp, args->owner);
  1792. if (fa) {
  1793. __xfs_buf_mark_corrupt(bp, fa);
  1794. xfs_trans_brelse(args->trans, bp);
  1795. xfs_da_mark_sick(args);
  1796. return -EFSCORRUPTED;
  1797. }
  1798. ASSERT(bp != NULL);
  1799. tmp_info = bp->b_addr;
  1800. ASSERT(tmp_info->magic == old_info->magic);
  1801. ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
  1802. tmp_info->back = cpu_to_be32(new_blk->blkno);
  1803. xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
  1804. }
  1805. old_info->forw = cpu_to_be32(new_blk->blkno);
  1806. }
  1807. xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
  1808. xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
  1809. return 0;
  1810. }
  1811. /*
  1812. * Unlink a block from a doubly linked list of blocks.
  1813. */
  1814. STATIC int /* error */
  1815. xfs_da3_blk_unlink(
  1816. struct xfs_da_state *state,
  1817. struct xfs_da_state_blk *drop_blk,
  1818. struct xfs_da_state_blk *save_blk)
  1819. {
  1820. struct xfs_da_blkinfo *drop_info;
  1821. struct xfs_da_blkinfo *save_info;
  1822. struct xfs_da_blkinfo *tmp_info;
  1823. struct xfs_da_args *args;
  1824. struct xfs_buf *bp;
  1825. xfs_failaddr_t fa;
  1826. int error;
  1827. /*
  1828. * Set up environment.
  1829. */
  1830. args = state->args;
  1831. ASSERT(args != NULL);
  1832. save_info = save_blk->bp->b_addr;
  1833. drop_info = drop_blk->bp->b_addr;
  1834. ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
  1835. save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
  1836. save_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1837. ASSERT(save_blk->magic == drop_blk->magic);
  1838. ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
  1839. (be32_to_cpu(save_info->back) == drop_blk->blkno));
  1840. ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
  1841. (be32_to_cpu(drop_info->back) == save_blk->blkno));
  1842. /*
  1843. * Unlink the leaf block from the doubly linked chain of leaves.
  1844. */
  1845. if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
  1846. trace_xfs_da_unlink_back(args);
  1847. save_info->back = drop_info->back;
  1848. if (drop_info->back) {
  1849. error = xfs_da3_node_read(args->trans, args->dp,
  1850. be32_to_cpu(drop_info->back),
  1851. &bp, args->whichfork);
  1852. if (error)
  1853. return error;
  1854. fa = xfs_da3_header_check(bp, args->owner);
  1855. if (fa) {
  1856. __xfs_buf_mark_corrupt(bp, fa);
  1857. xfs_trans_brelse(args->trans, bp);
  1858. xfs_da_mark_sick(args);
  1859. return -EFSCORRUPTED;
  1860. }
  1861. ASSERT(bp != NULL);
  1862. tmp_info = bp->b_addr;
  1863. ASSERT(tmp_info->magic == save_info->magic);
  1864. ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
  1865. tmp_info->forw = cpu_to_be32(save_blk->blkno);
  1866. xfs_trans_log_buf(args->trans, bp, 0,
  1867. sizeof(*tmp_info) - 1);
  1868. }
  1869. } else {
  1870. trace_xfs_da_unlink_forward(args);
  1871. save_info->forw = drop_info->forw;
  1872. if (drop_info->forw) {
  1873. error = xfs_da3_node_read(args->trans, args->dp,
  1874. be32_to_cpu(drop_info->forw),
  1875. &bp, args->whichfork);
  1876. if (error)
  1877. return error;
  1878. fa = xfs_da3_header_check(bp, args->owner);
  1879. if (fa) {
  1880. __xfs_buf_mark_corrupt(bp, fa);
  1881. xfs_trans_brelse(args->trans, bp);
  1882. xfs_da_mark_sick(args);
  1883. return -EFSCORRUPTED;
  1884. }
  1885. ASSERT(bp != NULL);
  1886. tmp_info = bp->b_addr;
  1887. ASSERT(tmp_info->magic == save_info->magic);
  1888. ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
  1889. tmp_info->back = cpu_to_be32(save_blk->blkno);
  1890. xfs_trans_log_buf(args->trans, bp, 0,
  1891. sizeof(*tmp_info) - 1);
  1892. }
  1893. }
  1894. xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
  1895. return 0;
  1896. }
  1897. /*
  1898. * Move a path "forward" or "!forward" one block at the current level.
  1899. *
  1900. * This routine will adjust a "path" to point to the next block
  1901. * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
  1902. * Btree, including updating pointers to the intermediate nodes between
  1903. * the new bottom and the root.
  1904. */
  1905. int /* error */
  1906. xfs_da3_path_shift(
  1907. struct xfs_da_state *state,
  1908. struct xfs_da_state_path *path,
  1909. int forward,
  1910. int release,
  1911. int *result)
  1912. {
  1913. struct xfs_da_state_blk *blk;
  1914. struct xfs_da_blkinfo *info;
  1915. struct xfs_da_args *args;
  1916. struct xfs_da_node_entry *btree;
  1917. struct xfs_da3_icnode_hdr nodehdr;
  1918. struct xfs_buf *bp;
  1919. xfs_failaddr_t fa;
  1920. xfs_dablk_t blkno = 0;
  1921. int level;
  1922. int error;
  1923. struct xfs_inode *dp = state->args->dp;
  1924. trace_xfs_da_path_shift(state->args);
  1925. /*
  1926. * Roll up the Btree looking for the first block where our
  1927. * current index is not at the edge of the block. Note that
  1928. * we skip the bottom layer because we want the sibling block.
  1929. */
  1930. args = state->args;
  1931. ASSERT(args != NULL);
  1932. ASSERT(path != NULL);
  1933. ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1934. level = (path->active-1) - 1; /* skip bottom layer in path */
  1935. for (; level >= 0; level--) {
  1936. blk = &path->blk[level];
  1937. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
  1938. blk->bp->b_addr);
  1939. if (forward && (blk->index < nodehdr.count - 1)) {
  1940. blk->index++;
  1941. blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
  1942. break;
  1943. } else if (!forward && (blk->index > 0)) {
  1944. blk->index--;
  1945. blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
  1946. break;
  1947. }
  1948. }
  1949. if (level < 0) {
  1950. *result = -ENOENT; /* we're out of our tree */
  1951. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  1952. return 0;
  1953. }
  1954. /*
  1955. * Roll down the edge of the subtree until we reach the
  1956. * same depth we were at originally.
  1957. */
  1958. for (blk++, level++; level < path->active; blk++, level++) {
  1959. /*
  1960. * Read the next child block into a local buffer.
  1961. */
  1962. error = xfs_da3_node_read(args->trans, dp, blkno, &bp,
  1963. args->whichfork);
  1964. if (error)
  1965. return error;
  1966. /*
  1967. * Release the old block (if it's dirty, the trans doesn't
  1968. * actually let go) and swap the local buffer into the path
  1969. * structure. This ensures failure of the above read doesn't set
  1970. * a NULL buffer in an active slot in the path.
  1971. */
  1972. if (release)
  1973. xfs_trans_brelse(args->trans, blk->bp);
  1974. blk->blkno = blkno;
  1975. blk->bp = bp;
  1976. info = blk->bp->b_addr;
  1977. ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
  1978. info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
  1979. info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  1980. info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
  1981. info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
  1982. info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
  1983. /*
  1984. * Note: we flatten the magic number to a single type so we
  1985. * don't have to compare against crc/non-crc types elsewhere.
  1986. */
  1987. switch (be16_to_cpu(info->magic)) {
  1988. case XFS_DA_NODE_MAGIC:
  1989. case XFS_DA3_NODE_MAGIC:
  1990. fa = xfs_da3_node_header_check(blk->bp, args->owner);
  1991. if (fa) {
  1992. __xfs_buf_mark_corrupt(blk->bp, fa);
  1993. xfs_da_mark_sick(args);
  1994. return -EFSCORRUPTED;
  1995. }
  1996. blk->magic = XFS_DA_NODE_MAGIC;
  1997. xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
  1998. bp->b_addr);
  1999. btree = nodehdr.btree;
  2000. blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
  2001. if (forward)
  2002. blk->index = 0;
  2003. else
  2004. blk->index = nodehdr.count - 1;
  2005. blkno = be32_to_cpu(btree[blk->index].before);
  2006. break;
  2007. case XFS_ATTR_LEAF_MAGIC:
  2008. case XFS_ATTR3_LEAF_MAGIC:
  2009. fa = xfs_attr3_leaf_header_check(blk->bp, args->owner);
  2010. if (fa) {
  2011. __xfs_buf_mark_corrupt(blk->bp, fa);
  2012. xfs_da_mark_sick(args);
  2013. return -EFSCORRUPTED;
  2014. }
  2015. blk->magic = XFS_ATTR_LEAF_MAGIC;
  2016. ASSERT(level == path->active-1);
  2017. blk->index = 0;
  2018. blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
  2019. break;
  2020. case XFS_DIR2_LEAFN_MAGIC:
  2021. case XFS_DIR3_LEAFN_MAGIC:
  2022. fa = xfs_dir3_leaf_header_check(blk->bp, args->owner);
  2023. if (fa) {
  2024. __xfs_buf_mark_corrupt(blk->bp, fa);
  2025. xfs_da_mark_sick(args);
  2026. return -EFSCORRUPTED;
  2027. }
  2028. blk->magic = XFS_DIR2_LEAFN_MAGIC;
  2029. ASSERT(level == path->active-1);
  2030. blk->index = 0;
  2031. blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
  2032. blk->bp, NULL);
  2033. break;
  2034. default:
  2035. ASSERT(0);
  2036. break;
  2037. }
  2038. }
  2039. *result = 0;
  2040. return 0;
  2041. }
  2042. /*========================================================================
  2043. * Utility routines.
  2044. *========================================================================*/
  2045. /*
  2046. * Implement a simple hash on a character string.
  2047. * Rotate the hash value by 7 bits, then XOR each character in.
  2048. * This is implemented with some source-level loop unrolling.
  2049. */
  2050. xfs_dahash_t
  2051. xfs_da_hashname(const uint8_t *name, int namelen)
  2052. {
  2053. xfs_dahash_t hash;
  2054. /*
  2055. * Do four characters at a time as long as we can.
  2056. */
  2057. for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
  2058. hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
  2059. (name[3] << 0) ^ rol32(hash, 7 * 4);
  2060. /*
  2061. * Now do the rest of the characters.
  2062. */
  2063. switch (namelen) {
  2064. case 3:
  2065. return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
  2066. rol32(hash, 7 * 3);
  2067. case 2:
  2068. return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
  2069. case 1:
  2070. return (name[0] << 0) ^ rol32(hash, 7 * 1);
  2071. default: /* case 0: */
  2072. return hash;
  2073. }
  2074. }
  2075. enum xfs_dacmp
  2076. xfs_da_compname(
  2077. struct xfs_da_args *args,
  2078. const unsigned char *name,
  2079. int len)
  2080. {
  2081. return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
  2082. XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
  2083. }
  2084. int
  2085. xfs_da_grow_inode_int(
  2086. struct xfs_da_args *args,
  2087. xfs_fileoff_t *bno,
  2088. int count)
  2089. {
  2090. struct xfs_trans *tp = args->trans;
  2091. struct xfs_inode *dp = args->dp;
  2092. int w = args->whichfork;
  2093. xfs_rfsblock_t nblks = dp->i_nblocks;
  2094. struct xfs_bmbt_irec map, *mapp = &map;
  2095. int nmap, error, got, i, mapi = 1;
  2096. /*
  2097. * Find a spot in the file space to put the new block.
  2098. */
  2099. error = xfs_bmap_first_unused(tp, dp, count, bno, w);
  2100. if (error)
  2101. return error;
  2102. /*
  2103. * Try mapping it in one filesystem block.
  2104. */
  2105. nmap = 1;
  2106. error = xfs_bmapi_write(tp, dp, *bno, count,
  2107. xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
  2108. args->total, &map, &nmap);
  2109. if (error == -ENOSPC && count > 1) {
  2110. xfs_fileoff_t b;
  2111. int c;
  2112. /*
  2113. * If we didn't get it and the block might work if fragmented,
  2114. * try without the CONTIG flag. Loop until we get it all.
  2115. */
  2116. mapp = kmalloc(sizeof(*mapp) * count,
  2117. GFP_KERNEL | __GFP_NOFAIL);
  2118. for (b = *bno, mapi = 0; b < *bno + count; ) {
  2119. c = (int)(*bno + count - b);
  2120. nmap = min(XFS_BMAP_MAX_NMAP, c);
  2121. error = xfs_bmapi_write(tp, dp, b, c,
  2122. xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
  2123. args->total, &mapp[mapi], &nmap);
  2124. if (error)
  2125. goto out_free_map;
  2126. mapi += nmap;
  2127. b = mapp[mapi - 1].br_startoff +
  2128. mapp[mapi - 1].br_blockcount;
  2129. }
  2130. }
  2131. if (error)
  2132. goto out_free_map;
  2133. /*
  2134. * Count the blocks we got, make sure it matches the total.
  2135. */
  2136. for (i = 0, got = 0; i < mapi; i++)
  2137. got += mapp[i].br_blockcount;
  2138. if (got != count || mapp[0].br_startoff != *bno ||
  2139. mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
  2140. *bno + count) {
  2141. error = -ENOSPC;
  2142. goto out_free_map;
  2143. }
  2144. /* account for newly allocated blocks in reserved blocks total */
  2145. args->total -= dp->i_nblocks - nblks;
  2146. out_free_map:
  2147. if (mapp != &map)
  2148. kfree(mapp);
  2149. return error;
  2150. }
  2151. /*
  2152. * Add a block to the btree ahead of the file.
  2153. * Return the new block number to the caller.
  2154. */
  2155. int
  2156. xfs_da_grow_inode(
  2157. struct xfs_da_args *args,
  2158. xfs_dablk_t *new_blkno)
  2159. {
  2160. xfs_fileoff_t bno;
  2161. int error;
  2162. trace_xfs_da_grow_inode(args);
  2163. bno = args->geo->leafblk;
  2164. error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
  2165. if (!error)
  2166. *new_blkno = (xfs_dablk_t)bno;
  2167. return error;
  2168. }
  2169. /*
  2170. * Ick. We need to always be able to remove a btree block, even
  2171. * if there's no space reservation because the filesystem is full.
  2172. * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
  2173. * It swaps the target block with the last block in the file. The
  2174. * last block in the file can always be removed since it can't cause
  2175. * a bmap btree split to do that.
  2176. */
  2177. STATIC int
  2178. xfs_da3_swap_lastblock(
  2179. struct xfs_da_args *args,
  2180. xfs_dablk_t *dead_blknop,
  2181. struct xfs_buf **dead_bufp)
  2182. {
  2183. struct xfs_da_blkinfo *dead_info;
  2184. struct xfs_da_blkinfo *sib_info;
  2185. struct xfs_da_intnode *par_node;
  2186. struct xfs_da_intnode *dead_node;
  2187. struct xfs_dir2_leaf *dead_leaf2;
  2188. struct xfs_da_node_entry *btree;
  2189. struct xfs_da3_icnode_hdr par_hdr;
  2190. struct xfs_inode *dp;
  2191. struct xfs_trans *tp;
  2192. struct xfs_mount *mp;
  2193. struct xfs_buf *dead_buf;
  2194. struct xfs_buf *last_buf;
  2195. struct xfs_buf *sib_buf;
  2196. struct xfs_buf *par_buf;
  2197. xfs_failaddr_t fa;
  2198. xfs_dahash_t dead_hash;
  2199. xfs_fileoff_t lastoff;
  2200. xfs_dablk_t dead_blkno;
  2201. xfs_dablk_t last_blkno;
  2202. xfs_dablk_t sib_blkno;
  2203. xfs_dablk_t par_blkno;
  2204. int error;
  2205. int w;
  2206. int entno;
  2207. int level;
  2208. int dead_level;
  2209. trace_xfs_da_swap_lastblock(args);
  2210. dead_buf = *dead_bufp;
  2211. dead_blkno = *dead_blknop;
  2212. tp = args->trans;
  2213. dp = args->dp;
  2214. w = args->whichfork;
  2215. ASSERT(w == XFS_DATA_FORK);
  2216. mp = dp->i_mount;
  2217. lastoff = args->geo->freeblk;
  2218. error = xfs_bmap_last_before(tp, dp, &lastoff, w);
  2219. if (error)
  2220. return error;
  2221. if (XFS_IS_CORRUPT(mp, lastoff == 0)) {
  2222. xfs_da_mark_sick(args);
  2223. return -EFSCORRUPTED;
  2224. }
  2225. /*
  2226. * Read the last block in the btree space.
  2227. */
  2228. last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
  2229. error = xfs_da3_node_read(tp, dp, last_blkno, &last_buf, w);
  2230. if (error)
  2231. return error;
  2232. fa = xfs_da3_header_check(last_buf, args->owner);
  2233. if (fa) {
  2234. __xfs_buf_mark_corrupt(last_buf, fa);
  2235. xfs_trans_brelse(tp, last_buf);
  2236. xfs_da_mark_sick(args);
  2237. return -EFSCORRUPTED;
  2238. }
  2239. /*
  2240. * Copy the last block into the dead buffer and log it.
  2241. */
  2242. xfs_da_buf_copy(dead_buf, last_buf, args->geo->blksize);
  2243. xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
  2244. dead_info = dead_buf->b_addr;
  2245. /*
  2246. * Get values from the moved block.
  2247. */
  2248. if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
  2249. dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
  2250. struct xfs_dir3_icleaf_hdr leafhdr;
  2251. struct xfs_dir2_leaf_entry *ents;
  2252. dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
  2253. xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr,
  2254. dead_leaf2);
  2255. ents = leafhdr.ents;
  2256. dead_level = 0;
  2257. dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
  2258. } else {
  2259. struct xfs_da3_icnode_hdr deadhdr;
  2260. dead_node = (xfs_da_intnode_t *)dead_info;
  2261. xfs_da3_node_hdr_from_disk(dp->i_mount, &deadhdr, dead_node);
  2262. btree = deadhdr.btree;
  2263. dead_level = deadhdr.level;
  2264. dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
  2265. }
  2266. sib_buf = par_buf = NULL;
  2267. /*
  2268. * If the moved block has a left sibling, fix up the pointers.
  2269. */
  2270. if ((sib_blkno = be32_to_cpu(dead_info->back))) {
  2271. error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
  2272. if (error)
  2273. goto done;
  2274. fa = xfs_da3_header_check(sib_buf, args->owner);
  2275. if (fa) {
  2276. __xfs_buf_mark_corrupt(sib_buf, fa);
  2277. xfs_da_mark_sick(args);
  2278. error = -EFSCORRUPTED;
  2279. goto done;
  2280. }
  2281. sib_info = sib_buf->b_addr;
  2282. if (XFS_IS_CORRUPT(mp,
  2283. be32_to_cpu(sib_info->forw) != last_blkno ||
  2284. sib_info->magic != dead_info->magic)) {
  2285. xfs_da_mark_sick(args);
  2286. error = -EFSCORRUPTED;
  2287. goto done;
  2288. }
  2289. sib_info->forw = cpu_to_be32(dead_blkno);
  2290. xfs_trans_log_buf(tp, sib_buf,
  2291. XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
  2292. sizeof(sib_info->forw)));
  2293. sib_buf = NULL;
  2294. }
  2295. /*
  2296. * If the moved block has a right sibling, fix up the pointers.
  2297. */
  2298. if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
  2299. error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
  2300. if (error)
  2301. goto done;
  2302. fa = xfs_da3_header_check(sib_buf, args->owner);
  2303. if (fa) {
  2304. __xfs_buf_mark_corrupt(sib_buf, fa);
  2305. xfs_da_mark_sick(args);
  2306. error = -EFSCORRUPTED;
  2307. goto done;
  2308. }
  2309. sib_info = sib_buf->b_addr;
  2310. if (XFS_IS_CORRUPT(mp,
  2311. be32_to_cpu(sib_info->back) != last_blkno ||
  2312. sib_info->magic != dead_info->magic)) {
  2313. xfs_da_mark_sick(args);
  2314. error = -EFSCORRUPTED;
  2315. goto done;
  2316. }
  2317. sib_info->back = cpu_to_be32(dead_blkno);
  2318. xfs_trans_log_buf(tp, sib_buf,
  2319. XFS_DA_LOGRANGE(sib_info, &sib_info->back,
  2320. sizeof(sib_info->back)));
  2321. sib_buf = NULL;
  2322. }
  2323. par_blkno = args->geo->leafblk;
  2324. level = -1;
  2325. /*
  2326. * Walk down the tree looking for the parent of the moved block.
  2327. */
  2328. for (;;) {
  2329. error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
  2330. if (error)
  2331. goto done;
  2332. fa = xfs_da3_node_header_check(par_buf, args->owner);
  2333. if (fa) {
  2334. __xfs_buf_mark_corrupt(par_buf, fa);
  2335. xfs_da_mark_sick(args);
  2336. error = -EFSCORRUPTED;
  2337. goto done;
  2338. }
  2339. par_node = par_buf->b_addr;
  2340. xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
  2341. if (XFS_IS_CORRUPT(mp,
  2342. level >= 0 && level != par_hdr.level + 1)) {
  2343. xfs_da_mark_sick(args);
  2344. error = -EFSCORRUPTED;
  2345. goto done;
  2346. }
  2347. level = par_hdr.level;
  2348. btree = par_hdr.btree;
  2349. for (entno = 0;
  2350. entno < par_hdr.count &&
  2351. be32_to_cpu(btree[entno].hashval) < dead_hash;
  2352. entno++)
  2353. continue;
  2354. if (XFS_IS_CORRUPT(mp, entno == par_hdr.count)) {
  2355. xfs_da_mark_sick(args);
  2356. error = -EFSCORRUPTED;
  2357. goto done;
  2358. }
  2359. par_blkno = be32_to_cpu(btree[entno].before);
  2360. if (level == dead_level + 1)
  2361. break;
  2362. xfs_trans_brelse(tp, par_buf);
  2363. par_buf = NULL;
  2364. }
  2365. /*
  2366. * We're in the right parent block.
  2367. * Look for the right entry.
  2368. */
  2369. for (;;) {
  2370. for (;
  2371. entno < par_hdr.count &&
  2372. be32_to_cpu(btree[entno].before) != last_blkno;
  2373. entno++)
  2374. continue;
  2375. if (entno < par_hdr.count)
  2376. break;
  2377. par_blkno = par_hdr.forw;
  2378. xfs_trans_brelse(tp, par_buf);
  2379. par_buf = NULL;
  2380. if (XFS_IS_CORRUPT(mp, par_blkno == 0)) {
  2381. xfs_da_mark_sick(args);
  2382. error = -EFSCORRUPTED;
  2383. goto done;
  2384. }
  2385. error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
  2386. if (error)
  2387. goto done;
  2388. fa = xfs_da3_node_header_check(par_buf, args->owner);
  2389. if (fa) {
  2390. __xfs_buf_mark_corrupt(par_buf, fa);
  2391. xfs_da_mark_sick(args);
  2392. error = -EFSCORRUPTED;
  2393. goto done;
  2394. }
  2395. par_node = par_buf->b_addr;
  2396. xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
  2397. if (XFS_IS_CORRUPT(mp, par_hdr.level != level)) {
  2398. xfs_da_mark_sick(args);
  2399. error = -EFSCORRUPTED;
  2400. goto done;
  2401. }
  2402. btree = par_hdr.btree;
  2403. entno = 0;
  2404. }
  2405. /*
  2406. * Update the parent entry pointing to the moved block.
  2407. */
  2408. btree[entno].before = cpu_to_be32(dead_blkno);
  2409. xfs_trans_log_buf(tp, par_buf,
  2410. XFS_DA_LOGRANGE(par_node, &btree[entno].before,
  2411. sizeof(btree[entno].before)));
  2412. *dead_blknop = last_blkno;
  2413. *dead_bufp = last_buf;
  2414. return 0;
  2415. done:
  2416. if (par_buf)
  2417. xfs_trans_brelse(tp, par_buf);
  2418. if (sib_buf)
  2419. xfs_trans_brelse(tp, sib_buf);
  2420. xfs_trans_brelse(tp, last_buf);
  2421. return error;
  2422. }
  2423. /*
  2424. * Remove a btree block from a directory or attribute.
  2425. */
  2426. int
  2427. xfs_da_shrink_inode(
  2428. struct xfs_da_args *args,
  2429. xfs_dablk_t dead_blkno,
  2430. struct xfs_buf *dead_buf)
  2431. {
  2432. struct xfs_inode *dp;
  2433. int done, error, w, count;
  2434. struct xfs_trans *tp;
  2435. trace_xfs_da_shrink_inode(args);
  2436. dp = args->dp;
  2437. w = args->whichfork;
  2438. tp = args->trans;
  2439. count = args->geo->fsbcount;
  2440. for (;;) {
  2441. /*
  2442. * Remove extents. If we get ENOSPC for a dir we have to move
  2443. * the last block to the place we want to kill.
  2444. */
  2445. error = xfs_bunmapi(tp, dp, dead_blkno, count,
  2446. xfs_bmapi_aflag(w), 0, &done);
  2447. if (error == -ENOSPC) {
  2448. if (w != XFS_DATA_FORK)
  2449. break;
  2450. error = xfs_da3_swap_lastblock(args, &dead_blkno,
  2451. &dead_buf);
  2452. if (error)
  2453. break;
  2454. } else {
  2455. break;
  2456. }
  2457. }
  2458. xfs_trans_binval(tp, dead_buf);
  2459. return error;
  2460. }
  2461. static int
  2462. xfs_dabuf_map(
  2463. struct xfs_inode *dp,
  2464. xfs_dablk_t bno,
  2465. unsigned int flags,
  2466. int whichfork,
  2467. struct xfs_buf_map **mapp,
  2468. int *nmaps)
  2469. {
  2470. struct xfs_mount *mp = dp->i_mount;
  2471. int nfsb = xfs_dabuf_nfsb(mp, whichfork);
  2472. struct xfs_bmbt_irec irec, *irecs = &irec;
  2473. struct xfs_buf_map *map = *mapp;
  2474. xfs_fileoff_t off = bno;
  2475. int error = 0, nirecs, i;
  2476. if (nfsb > 1)
  2477. irecs = kzalloc(sizeof(irec) * nfsb,
  2478. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  2479. nirecs = nfsb;
  2480. error = xfs_bmapi_read(dp, bno, nfsb, irecs, &nirecs,
  2481. xfs_bmapi_aflag(whichfork));
  2482. if (error)
  2483. goto out_free_irecs;
  2484. /*
  2485. * Use the caller provided map for the single map case, else allocate a
  2486. * larger one that needs to be free by the caller.
  2487. */
  2488. if (nirecs > 1) {
  2489. map = kzalloc(nirecs * sizeof(struct xfs_buf_map),
  2490. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  2491. if (!map) {
  2492. error = -ENOMEM;
  2493. goto out_free_irecs;
  2494. }
  2495. *mapp = map;
  2496. }
  2497. for (i = 0; i < nirecs; i++) {
  2498. if (irecs[i].br_startblock == HOLESTARTBLOCK ||
  2499. irecs[i].br_startblock == DELAYSTARTBLOCK)
  2500. goto invalid_mapping;
  2501. if (off != irecs[i].br_startoff)
  2502. goto invalid_mapping;
  2503. map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
  2504. map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
  2505. off += irecs[i].br_blockcount;
  2506. }
  2507. if (off != bno + nfsb)
  2508. goto invalid_mapping;
  2509. *nmaps = nirecs;
  2510. out_free_irecs:
  2511. if (irecs != &irec)
  2512. kfree(irecs);
  2513. return error;
  2514. invalid_mapping:
  2515. /* Caller ok with no mapping. */
  2516. if (XFS_IS_CORRUPT(mp, !(flags & XFS_DABUF_MAP_HOLE_OK))) {
  2517. xfs_dirattr_mark_sick(dp, whichfork);
  2518. error = -EFSCORRUPTED;
  2519. if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
  2520. xfs_alert(mp, "%s: bno %u inode %llu",
  2521. __func__, bno, dp->i_ino);
  2522. for (i = 0; i < nirecs; i++) {
  2523. xfs_alert(mp,
  2524. "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
  2525. i, irecs[i].br_startoff,
  2526. irecs[i].br_startblock,
  2527. irecs[i].br_blockcount,
  2528. irecs[i].br_state);
  2529. }
  2530. }
  2531. } else {
  2532. *nmaps = 0;
  2533. }
  2534. goto out_free_irecs;
  2535. }
  2536. /*
  2537. * Get a buffer for the dir/attr block.
  2538. */
  2539. int
  2540. xfs_da_get_buf(
  2541. struct xfs_trans *tp,
  2542. struct xfs_inode *dp,
  2543. xfs_dablk_t bno,
  2544. struct xfs_buf **bpp,
  2545. int whichfork)
  2546. {
  2547. struct xfs_mount *mp = dp->i_mount;
  2548. struct xfs_buf *bp;
  2549. struct xfs_buf_map map, *mapp = &map;
  2550. int nmap = 1;
  2551. int error;
  2552. *bpp = NULL;
  2553. error = xfs_dabuf_map(dp, bno, 0, whichfork, &mapp, &nmap);
  2554. if (error || nmap == 0)
  2555. goto out_free;
  2556. error = xfs_trans_get_buf_map(tp, mp->m_ddev_targp, mapp, nmap, 0, &bp);
  2557. if (error)
  2558. goto out_free;
  2559. *bpp = bp;
  2560. out_free:
  2561. if (mapp != &map)
  2562. kfree(mapp);
  2563. return error;
  2564. }
  2565. /*
  2566. * Get a buffer for the dir/attr block, fill in the contents.
  2567. */
  2568. int
  2569. xfs_da_read_buf(
  2570. struct xfs_trans *tp,
  2571. struct xfs_inode *dp,
  2572. xfs_dablk_t bno,
  2573. unsigned int flags,
  2574. struct xfs_buf **bpp,
  2575. int whichfork,
  2576. const struct xfs_buf_ops *ops)
  2577. {
  2578. struct xfs_mount *mp = dp->i_mount;
  2579. struct xfs_buf *bp;
  2580. struct xfs_buf_map map, *mapp = &map;
  2581. int nmap = 1;
  2582. int error;
  2583. *bpp = NULL;
  2584. error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
  2585. if (error || !nmap)
  2586. goto out_free;
  2587. error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
  2588. &bp, ops);
  2589. if (xfs_metadata_is_sick(error))
  2590. xfs_dirattr_mark_sick(dp, whichfork);
  2591. /*
  2592. * ENODATA from disk implies a disk medium failure; ENODATA for
  2593. * xattrs means attribute not found, so disambiguate that here.
  2594. */
  2595. if (error == -ENODATA && whichfork == XFS_ATTR_FORK)
  2596. error = -EIO;
  2597. if (error)
  2598. goto out_free;
  2599. if (whichfork == XFS_ATTR_FORK)
  2600. xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
  2601. else
  2602. xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
  2603. *bpp = bp;
  2604. out_free:
  2605. if (mapp != &map)
  2606. kfree(mapp);
  2607. return error;
  2608. }
  2609. /*
  2610. * Readahead the dir/attr block.
  2611. */
  2612. int
  2613. xfs_da_reada_buf(
  2614. struct xfs_inode *dp,
  2615. xfs_dablk_t bno,
  2616. unsigned int flags,
  2617. int whichfork,
  2618. const struct xfs_buf_ops *ops)
  2619. {
  2620. struct xfs_buf_map map;
  2621. struct xfs_buf_map *mapp;
  2622. int nmap;
  2623. int error;
  2624. mapp = &map;
  2625. nmap = 1;
  2626. error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
  2627. if (error || !nmap)
  2628. goto out_free;
  2629. xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
  2630. out_free:
  2631. if (mapp != &map)
  2632. kfree(mapp);
  2633. return error;
  2634. }