xfs_da_btree.c 66 KB

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