xfs_da_btree.c 70 KB

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