xfs_da_btree.c 72 KB

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