xfs_da_btree.c 72 KB

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