xfs_da_btree.c 66 KB

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