xfs_da_btree.c 70 KB

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