xfs_da_btree.c 66 KB

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