xfs_da_btree.c 64 KB

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