xfs_da_btree.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  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. node->hdr.info.magic = cpu_to_be16(XFS_DA_NODE_MAGIC);
  122. node->hdr.info.pad = 0;
  123. node->hdr.count = 0;
  124. node->hdr.level = cpu_to_be16(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 (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
  276. bp = addblk->bp;
  277. } else {
  278. ASSERT(state->extravalid);
  279. bp = state->extrablk.bp;
  280. }
  281. node = bp->data;
  282. node->hdr.info.back = cpu_to_be32(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 (node->hdr.info.back) {
  289. if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
  290. bp = addblk->bp;
  291. } else {
  292. ASSERT(state->extravalid);
  293. bp = state->extrablk.bp;
  294. }
  295. node = bp->data;
  296. node->hdr.info.forw = cpu_to_be32(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 (be16_to_cpu(oldroot->hdr.info.magic) == XFS_DA_NODE_MAGIC) {
  343. size = (int)((char *)&oldroot->btree[be16_to_cpu(oldroot->hdr.count)] -
  344. (char *)oldroot);
  345. } else {
  346. ASSERT(XFS_DIR_IS_V2(mp));
  347. ASSERT(be16_to_cpu(oldroot->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
  348. leaf = (xfs_dir2_leaf_t *)oldroot;
  349. size = (int)((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] -
  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. be16_to_cpu(node->hdr.level) + 1, &bp, args->whichfork);
  364. if (error)
  365. return(error);
  366. node = bp->data;
  367. node->btree[0].hashval = cpu_to_be32(blk1->hashval);
  368. node->btree[0].before = cpu_to_be32(blk1->blkno);
  369. node->btree[1].hashval = cpu_to_be32(blk2->hashval);
  370. node->btree[1].before = cpu_to_be32(blk2->blkno);
  371. node->hdr.count = cpu_to_be16(2);
  372. #ifdef DEBUG
  373. if (be16_to_cpu(oldroot->hdr.info.magic) == 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(be16_to_cpu(node->hdr.info.magic) == 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 ((be16_to_cpu(node->hdr.count) + 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 <= be16_to_cpu(node->hdr.count)) {
  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 ((be16_to_cpu(node1->hdr.count) > 0) && (be16_to_cpu(node2->hdr.count) > 0) &&
  488. ((be32_to_cpu(node2->btree[0].hashval) < be32_to_cpu(node1->btree[0].hashval)) ||
  489. (be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval) <
  490. be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval)))) {
  491. tmpnode = node1;
  492. node1 = node2;
  493. node2 = tmpnode;
  494. }
  495. ASSERT(be16_to_cpu(node1->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  496. ASSERT(be16_to_cpu(node2->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  497. count = (be16_to_cpu(node1->hdr.count) - be16_to_cpu(node2->hdr.count)) / 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 = be16_to_cpu(node2->hdr.count)) > 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. be16_add(&node2->hdr.count, count);
  519. tmp = count * (uint)sizeof(xfs_da_node_entry_t);
  520. btree_s = &node1->btree[be16_to_cpu(node1->hdr.count) - count];
  521. btree_d = &node2->btree[0];
  522. memcpy(btree_d, btree_s, tmp);
  523. be16_add(&node1->hdr.count, -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[be16_to_cpu(node1->hdr.count)];
  533. memcpy(btree_d, btree_s, tmp);
  534. be16_add(&node1->hdr.count, 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 = be16_to_cpu(node2->hdr.count) - 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. be16_add(&node2->hdr.count, -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]) * be16_to_cpu(node2->hdr.count)));
  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 = be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval);
  563. blk2->hashval = be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval);
  564. /*
  565. * Adjust the expected index for insertion.
  566. */
  567. if (blk1->index >= be16_to_cpu(node1->hdr.count)) {
  568. blk2->index = blk1->index - be16_to_cpu(node1->hdr.count);
  569. blk1->index = be16_to_cpu(node1->hdr.count) + 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(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  586. ASSERT((oldblk->index >= 0) && (oldblk->index <= be16_to_cpu(node->hdr.count)));
  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 < be16_to_cpu(node->hdr.count)) {
  597. tmp = (be16_to_cpu(node->hdr.count) - oldblk->index) * (uint)sizeof(*btree);
  598. memmove(btree + 1, btree, tmp);
  599. }
  600. btree->hashval = cpu_to_be32(newblk->hashval);
  601. btree->before = cpu_to_be32(newblk->blkno);
  602. xfs_da_log_buf(state->args->trans, oldblk->bp,
  603. XFS_DA_LOGRANGE(node, btree, tmp + sizeof(*btree)));
  604. be16_add(&node->hdr.count, 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 = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1 ].hashval);
  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(be16_to_cpu(oldroot->hdr.info.magic) == 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 (be16_to_cpu(oldroot->hdr.count) > 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 = be32_to_cpu(oldroot->btree[0].before);
  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 (be16_to_cpu(oldroot->hdr.level) == 1) {
  744. ASSERT(be16_to_cpu(blkinfo->magic) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  745. be16_to_cpu(blkinfo->magic) == XFS_ATTR_LEAF_MAGIC);
  746. } else {
  747. ASSERT(be16_to_cpu(blkinfo->magic) == 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(be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC);
  782. node = (xfs_da_intnode_t *)info;
  783. count = be16_to_cpu(node->hdr.count);
  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 (arbitrarily)
  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 != 0);
  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 = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
  821. for (i = 0; i < 2; forward = !forward, i++) {
  822. if (forward)
  823. blkno = be32_to_cpu(info->forw);
  824. else
  825. blkno = be32_to_cpu(info->back);
  826. if (blkno == 0)
  827. continue;
  828. error = xfs_da_read_buf(state->args->trans, state->args->dp,
  829. blkno, -1, &bp, state->args->whichfork);
  830. if (error)
  831. return(error);
  832. ASSERT(bp != NULL);
  833. node = (xfs_da_intnode_t *)info;
  834. count = state->node_ents;
  835. count -= state->node_ents >> 2;
  836. count -= be16_to_cpu(node->hdr.count);
  837. node = bp->data;
  838. ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  839. count -= be16_to_cpu(node->hdr.count);
  840. xfs_da_brelse(state->args->trans, bp);
  841. if (count >= 0)
  842. break; /* fits with at least 25% to spare */
  843. }
  844. if (i >= 2) {
  845. *action = 0;
  846. return(0);
  847. }
  848. /*
  849. * Make altpath point to the block we want to keep (the lower
  850. * numbered block) and path point to the block we want to drop.
  851. */
  852. memcpy(&state->altpath, &state->path, sizeof(state->path));
  853. if (blkno < blk->blkno) {
  854. error = xfs_da_path_shift(state, &state->altpath, forward,
  855. 0, &retval);
  856. if (error) {
  857. return(error);
  858. }
  859. if (retval) {
  860. *action = 0;
  861. return(0);
  862. }
  863. } else {
  864. error = xfs_da_path_shift(state, &state->path, forward,
  865. 0, &retval);
  866. if (error) {
  867. return(error);
  868. }
  869. if (retval) {
  870. *action = 0;
  871. return(0);
  872. }
  873. }
  874. *action = 1;
  875. return(0);
  876. }
  877. /*
  878. * Walk back up the tree adjusting hash values as necessary,
  879. * when we stop making changes, return.
  880. */
  881. void
  882. xfs_da_fixhashpath(xfs_da_state_t *state, xfs_da_state_path_t *path)
  883. {
  884. xfs_da_state_blk_t *blk;
  885. xfs_da_intnode_t *node;
  886. xfs_da_node_entry_t *btree;
  887. xfs_dahash_t lasthash=0;
  888. int level, count;
  889. level = path->active-1;
  890. blk = &path->blk[ level ];
  891. switch (blk->magic) {
  892. case XFS_ATTR_LEAF_MAGIC:
  893. lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
  894. if (count == 0)
  895. return;
  896. break;
  897. case XFS_DIR_LEAF_MAGIC:
  898. ASSERT(XFS_DIR_IS_V1(state->mp));
  899. lasthash = xfs_dir_leaf_lasthash(blk->bp, &count);
  900. if (count == 0)
  901. return;
  902. break;
  903. case XFS_DIR2_LEAFN_MAGIC:
  904. ASSERT(XFS_DIR_IS_V2(state->mp));
  905. lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
  906. if (count == 0)
  907. return;
  908. break;
  909. case XFS_DA_NODE_MAGIC:
  910. lasthash = xfs_da_node_lasthash(blk->bp, &count);
  911. if (count == 0)
  912. return;
  913. break;
  914. }
  915. for (blk--, level--; level >= 0; blk--, level--) {
  916. node = blk->bp->data;
  917. ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  918. btree = &node->btree[ blk->index ];
  919. if (be32_to_cpu(btree->hashval) == lasthash)
  920. break;
  921. blk->hashval = lasthash;
  922. btree->hashval = cpu_to_be32(lasthash);
  923. xfs_da_log_buf(state->args->trans, blk->bp,
  924. XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
  925. lasthash = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
  926. }
  927. }
  928. /*
  929. * Remove an entry from an intermediate node.
  930. */
  931. STATIC void
  932. xfs_da_node_remove(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk)
  933. {
  934. xfs_da_intnode_t *node;
  935. xfs_da_node_entry_t *btree;
  936. int tmp;
  937. node = drop_blk->bp->data;
  938. ASSERT(drop_blk->index < be16_to_cpu(node->hdr.count));
  939. ASSERT(drop_blk->index >= 0);
  940. /*
  941. * Copy over the offending entry, or just zero it out.
  942. */
  943. btree = &node->btree[drop_blk->index];
  944. if (drop_blk->index < (be16_to_cpu(node->hdr.count)-1)) {
  945. tmp = be16_to_cpu(node->hdr.count) - drop_blk->index - 1;
  946. tmp *= (uint)sizeof(xfs_da_node_entry_t);
  947. memmove(btree, btree + 1, tmp);
  948. xfs_da_log_buf(state->args->trans, drop_blk->bp,
  949. XFS_DA_LOGRANGE(node, btree, tmp));
  950. btree = &node->btree[be16_to_cpu(node->hdr.count)-1];
  951. }
  952. memset((char *)btree, 0, sizeof(xfs_da_node_entry_t));
  953. xfs_da_log_buf(state->args->trans, drop_blk->bp,
  954. XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
  955. be16_add(&node->hdr.count, -1);
  956. xfs_da_log_buf(state->args->trans, drop_blk->bp,
  957. XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
  958. /*
  959. * Copy the last hash value from the block to propagate upwards.
  960. */
  961. btree--;
  962. drop_blk->hashval = be32_to_cpu(btree->hashval);
  963. }
  964. /*
  965. * Unbalance the btree elements between two intermediate nodes,
  966. * move all Btree elements from one node into another.
  967. */
  968. STATIC void
  969. xfs_da_node_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
  970. xfs_da_state_blk_t *save_blk)
  971. {
  972. xfs_da_intnode_t *drop_node, *save_node;
  973. xfs_da_node_entry_t *btree;
  974. int tmp;
  975. xfs_trans_t *tp;
  976. drop_node = drop_blk->bp->data;
  977. save_node = save_blk->bp->data;
  978. ASSERT(be16_to_cpu(drop_node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  979. ASSERT(be16_to_cpu(save_node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  980. tp = state->args->trans;
  981. /*
  982. * If the dying block has lower hashvals, then move all the
  983. * elements in the remaining block up to make a hole.
  984. */
  985. if ((be32_to_cpu(drop_node->btree[0].hashval) < be32_to_cpu(save_node->btree[ 0 ].hashval)) ||
  986. (be32_to_cpu(drop_node->btree[be16_to_cpu(drop_node->hdr.count)-1].hashval) <
  987. be32_to_cpu(save_node->btree[be16_to_cpu(save_node->hdr.count)-1].hashval)))
  988. {
  989. btree = &save_node->btree[be16_to_cpu(drop_node->hdr.count)];
  990. tmp = be16_to_cpu(save_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
  991. memmove(btree, &save_node->btree[0], tmp);
  992. btree = &save_node->btree[0];
  993. xfs_da_log_buf(tp, save_blk->bp,
  994. XFS_DA_LOGRANGE(save_node, btree,
  995. (be16_to_cpu(save_node->hdr.count) + be16_to_cpu(drop_node->hdr.count)) *
  996. sizeof(xfs_da_node_entry_t)));
  997. } else {
  998. btree = &save_node->btree[be16_to_cpu(save_node->hdr.count)];
  999. xfs_da_log_buf(tp, save_blk->bp,
  1000. XFS_DA_LOGRANGE(save_node, btree,
  1001. be16_to_cpu(drop_node->hdr.count) *
  1002. sizeof(xfs_da_node_entry_t)));
  1003. }
  1004. /*
  1005. * Move all the B-tree elements from drop_blk to save_blk.
  1006. */
  1007. tmp = be16_to_cpu(drop_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
  1008. memcpy(btree, &drop_node->btree[0], tmp);
  1009. be16_add(&save_node->hdr.count, be16_to_cpu(drop_node->hdr.count));
  1010. xfs_da_log_buf(tp, save_blk->bp,
  1011. XFS_DA_LOGRANGE(save_node, &save_node->hdr,
  1012. sizeof(save_node->hdr)));
  1013. /*
  1014. * Save the last hashval in the remaining block for upward propagation.
  1015. */
  1016. save_blk->hashval = be32_to_cpu(save_node->btree[be16_to_cpu(save_node->hdr.count)-1].hashval);
  1017. }
  1018. /*========================================================================
  1019. * Routines used for finding things in the Btree.
  1020. *========================================================================*/
  1021. /*
  1022. * Walk down the Btree looking for a particular filename, filling
  1023. * in the state structure as we go.
  1024. *
  1025. * We will set the state structure to point to each of the elements
  1026. * in each of the nodes where either the hashval is or should be.
  1027. *
  1028. * We support duplicate hashval's so for each entry in the current
  1029. * node that could contain the desired hashval, descend. This is a
  1030. * pruned depth-first tree search.
  1031. */
  1032. int /* error */
  1033. xfs_da_node_lookup_int(xfs_da_state_t *state, int *result)
  1034. {
  1035. xfs_da_state_blk_t *blk;
  1036. xfs_da_blkinfo_t *curr;
  1037. xfs_da_intnode_t *node;
  1038. xfs_da_node_entry_t *btree;
  1039. xfs_dablk_t blkno;
  1040. int probe, span, max, error, retval;
  1041. xfs_dahash_t hashval;
  1042. xfs_da_args_t *args;
  1043. args = state->args;
  1044. /*
  1045. * Descend thru the B-tree searching each level for the right
  1046. * node to use, until the right hashval is found.
  1047. */
  1048. if (args->whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(state->mp))
  1049. blkno = state->mp->m_dirleafblk;
  1050. else
  1051. blkno = 0;
  1052. for (blk = &state->path.blk[0], state->path.active = 1;
  1053. state->path.active <= XFS_DA_NODE_MAXDEPTH;
  1054. blk++, state->path.active++) {
  1055. /*
  1056. * Read the next node down in the tree.
  1057. */
  1058. blk->blkno = blkno;
  1059. error = xfs_da_read_buf(args->trans, args->dp, blkno,
  1060. -1, &blk->bp, args->whichfork);
  1061. if (error) {
  1062. blk->blkno = 0;
  1063. state->path.active--;
  1064. return(error);
  1065. }
  1066. curr = blk->bp->data;
  1067. ASSERT(be16_to_cpu(curr->magic) == XFS_DA_NODE_MAGIC ||
  1068. be16_to_cpu(curr->magic) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1069. be16_to_cpu(curr->magic) == XFS_ATTR_LEAF_MAGIC);
  1070. /*
  1071. * Search an intermediate node for a match.
  1072. */
  1073. blk->magic = be16_to_cpu(curr->magic);
  1074. if (blk->magic == XFS_DA_NODE_MAGIC) {
  1075. node = blk->bp->data;
  1076. blk->hashval = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
  1077. /*
  1078. * Binary search. (note: small blocks will skip loop)
  1079. */
  1080. max = be16_to_cpu(node->hdr.count);
  1081. probe = span = max / 2;
  1082. hashval = args->hashval;
  1083. for (btree = &node->btree[probe]; span > 4;
  1084. btree = &node->btree[probe]) {
  1085. span /= 2;
  1086. if (be32_to_cpu(btree->hashval) < hashval)
  1087. probe += span;
  1088. else if (be32_to_cpu(btree->hashval) > hashval)
  1089. probe -= span;
  1090. else
  1091. break;
  1092. }
  1093. ASSERT((probe >= 0) && (probe < max));
  1094. ASSERT((span <= 4) || (be32_to_cpu(btree->hashval) == hashval));
  1095. /*
  1096. * Since we may have duplicate hashval's, find the first
  1097. * matching hashval in the node.
  1098. */
  1099. while ((probe > 0) && (be32_to_cpu(btree->hashval) >= hashval)) {
  1100. btree--;
  1101. probe--;
  1102. }
  1103. while ((probe < max) && (be32_to_cpu(btree->hashval) < hashval)) {
  1104. btree++;
  1105. probe++;
  1106. }
  1107. /*
  1108. * Pick the right block to descend on.
  1109. */
  1110. if (probe == max) {
  1111. blk->index = max-1;
  1112. blkno = be32_to_cpu(node->btree[max-1].before);
  1113. } else {
  1114. blk->index = probe;
  1115. blkno = be32_to_cpu(btree->before);
  1116. }
  1117. }
  1118. else if (be16_to_cpu(curr->magic) == XFS_ATTR_LEAF_MAGIC) {
  1119. blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
  1120. break;
  1121. }
  1122. else if (be16_to_cpu(curr->magic) == XFS_DIR_LEAF_MAGIC) {
  1123. blk->hashval = xfs_dir_leaf_lasthash(blk->bp, NULL);
  1124. break;
  1125. }
  1126. else if (be16_to_cpu(curr->magic) == XFS_DIR2_LEAFN_MAGIC) {
  1127. blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
  1128. break;
  1129. }
  1130. }
  1131. /*
  1132. * A leaf block that ends in the hashval that we are interested in
  1133. * (final hashval == search hashval) means that the next block may
  1134. * contain more entries with the same hashval, shift upward to the
  1135. * next leaf and keep searching.
  1136. */
  1137. for (;;) {
  1138. if (blk->magic == XFS_DIR_LEAF_MAGIC) {
  1139. ASSERT(XFS_DIR_IS_V1(state->mp));
  1140. retval = xfs_dir_leaf_lookup_int(blk->bp, args,
  1141. &blk->index);
  1142. } else if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
  1143. ASSERT(XFS_DIR_IS_V2(state->mp));
  1144. retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
  1145. &blk->index, state);
  1146. }
  1147. else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
  1148. retval = xfs_attr_leaf_lookup_int(blk->bp, args);
  1149. blk->index = args->index;
  1150. args->blkno = blk->blkno;
  1151. }
  1152. if (((retval == ENOENT) || (retval == ENOATTR)) &&
  1153. (blk->hashval == args->hashval)) {
  1154. error = xfs_da_path_shift(state, &state->path, 1, 1,
  1155. &retval);
  1156. if (error)
  1157. return(error);
  1158. if (retval == 0) {
  1159. continue;
  1160. }
  1161. else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
  1162. /* path_shift() gives ENOENT */
  1163. retval = XFS_ERROR(ENOATTR);
  1164. }
  1165. }
  1166. break;
  1167. }
  1168. *result = retval;
  1169. return(0);
  1170. }
  1171. /*========================================================================
  1172. * Utility routines.
  1173. *========================================================================*/
  1174. /*
  1175. * Link a new block into a doubly linked list of blocks (of whatever type).
  1176. */
  1177. int /* error */
  1178. xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  1179. xfs_da_state_blk_t *new_blk)
  1180. {
  1181. xfs_da_blkinfo_t *old_info, *new_info, *tmp_info;
  1182. xfs_da_args_t *args;
  1183. int before=0, error;
  1184. xfs_dabuf_t *bp;
  1185. /*
  1186. * Set up environment.
  1187. */
  1188. args = state->args;
  1189. ASSERT(args != NULL);
  1190. old_info = old_blk->bp->data;
  1191. new_info = new_blk->bp->data;
  1192. ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
  1193. old_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1194. old_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1195. ASSERT(old_blk->magic == be16_to_cpu(old_info->magic));
  1196. ASSERT(new_blk->magic == be16_to_cpu(new_info->magic));
  1197. ASSERT(old_blk->magic == new_blk->magic);
  1198. switch (old_blk->magic) {
  1199. case XFS_ATTR_LEAF_MAGIC:
  1200. before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
  1201. break;
  1202. case XFS_DIR_LEAF_MAGIC:
  1203. ASSERT(XFS_DIR_IS_V1(state->mp));
  1204. before = xfs_dir_leaf_order(old_blk->bp, new_blk->bp);
  1205. break;
  1206. case XFS_DIR2_LEAFN_MAGIC:
  1207. ASSERT(XFS_DIR_IS_V2(state->mp));
  1208. before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
  1209. break;
  1210. case XFS_DA_NODE_MAGIC:
  1211. before = xfs_da_node_order(old_blk->bp, new_blk->bp);
  1212. break;
  1213. }
  1214. /*
  1215. * Link blocks in appropriate order.
  1216. */
  1217. if (before) {
  1218. /*
  1219. * Link new block in before existing block.
  1220. */
  1221. new_info->forw = cpu_to_be32(old_blk->blkno);
  1222. new_info->back = old_info->back;
  1223. if (old_info->back) {
  1224. error = xfs_da_read_buf(args->trans, args->dp,
  1225. be32_to_cpu(old_info->back),
  1226. -1, &bp, args->whichfork);
  1227. if (error)
  1228. return(error);
  1229. ASSERT(bp != NULL);
  1230. tmp_info = bp->data;
  1231. ASSERT(be16_to_cpu(tmp_info->magic) == be16_to_cpu(old_info->magic));
  1232. ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
  1233. tmp_info->forw = cpu_to_be32(new_blk->blkno);
  1234. xfs_da_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
  1235. xfs_da_buf_done(bp);
  1236. }
  1237. old_info->back = cpu_to_be32(new_blk->blkno);
  1238. } else {
  1239. /*
  1240. * Link new block in after existing block.
  1241. */
  1242. new_info->forw = old_info->forw;
  1243. new_info->back = cpu_to_be32(old_blk->blkno);
  1244. if (old_info->forw) {
  1245. error = xfs_da_read_buf(args->trans, args->dp,
  1246. be32_to_cpu(old_info->forw),
  1247. -1, &bp, args->whichfork);
  1248. if (error)
  1249. return(error);
  1250. ASSERT(bp != NULL);
  1251. tmp_info = bp->data;
  1252. ASSERT(tmp_info->magic == old_info->magic);
  1253. ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
  1254. tmp_info->back = cpu_to_be32(new_blk->blkno);
  1255. xfs_da_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
  1256. xfs_da_buf_done(bp);
  1257. }
  1258. old_info->forw = cpu_to_be32(new_blk->blkno);
  1259. }
  1260. xfs_da_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
  1261. xfs_da_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
  1262. return(0);
  1263. }
  1264. /*
  1265. * Compare two intermediate nodes for "order".
  1266. */
  1267. STATIC int
  1268. xfs_da_node_order(xfs_dabuf_t *node1_bp, xfs_dabuf_t *node2_bp)
  1269. {
  1270. xfs_da_intnode_t *node1, *node2;
  1271. node1 = node1_bp->data;
  1272. node2 = node2_bp->data;
  1273. ASSERT((be16_to_cpu(node1->hdr.info.magic) == XFS_DA_NODE_MAGIC) &&
  1274. (be16_to_cpu(node2->hdr.info.magic) == XFS_DA_NODE_MAGIC));
  1275. if ((be16_to_cpu(node1->hdr.count) > 0) && (be16_to_cpu(node2->hdr.count) > 0) &&
  1276. ((be32_to_cpu(node2->btree[0].hashval) <
  1277. be32_to_cpu(node1->btree[0].hashval)) ||
  1278. (be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval) <
  1279. be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval)))) {
  1280. return(1);
  1281. }
  1282. return(0);
  1283. }
  1284. /*
  1285. * Pick up the last hashvalue from an intermediate node.
  1286. */
  1287. STATIC uint
  1288. xfs_da_node_lasthash(xfs_dabuf_t *bp, int *count)
  1289. {
  1290. xfs_da_intnode_t *node;
  1291. node = bp->data;
  1292. ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  1293. if (count)
  1294. *count = be16_to_cpu(node->hdr.count);
  1295. if (!node->hdr.count)
  1296. return(0);
  1297. return be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
  1298. }
  1299. /*
  1300. * Unlink a block from a doubly linked list of blocks.
  1301. */
  1302. STATIC int /* error */
  1303. xfs_da_blk_unlink(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
  1304. xfs_da_state_blk_t *save_blk)
  1305. {
  1306. xfs_da_blkinfo_t *drop_info, *save_info, *tmp_info;
  1307. xfs_da_args_t *args;
  1308. xfs_dabuf_t *bp;
  1309. int error;
  1310. /*
  1311. * Set up environment.
  1312. */
  1313. args = state->args;
  1314. ASSERT(args != NULL);
  1315. save_info = save_blk->bp->data;
  1316. drop_info = drop_blk->bp->data;
  1317. ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
  1318. save_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1319. save_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1320. ASSERT(save_blk->magic == be16_to_cpu(save_info->magic));
  1321. ASSERT(drop_blk->magic == be16_to_cpu(drop_info->magic));
  1322. ASSERT(save_blk->magic == drop_blk->magic);
  1323. ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
  1324. (be32_to_cpu(save_info->back) == drop_blk->blkno));
  1325. ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
  1326. (be32_to_cpu(drop_info->back) == save_blk->blkno));
  1327. /*
  1328. * Unlink the leaf block from the doubly linked chain of leaves.
  1329. */
  1330. if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
  1331. save_info->back = drop_info->back;
  1332. if (drop_info->back) {
  1333. error = xfs_da_read_buf(args->trans, args->dp,
  1334. be32_to_cpu(drop_info->back),
  1335. -1, &bp, args->whichfork);
  1336. if (error)
  1337. return(error);
  1338. ASSERT(bp != NULL);
  1339. tmp_info = bp->data;
  1340. ASSERT(tmp_info->magic == save_info->magic);
  1341. ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
  1342. tmp_info->forw = cpu_to_be32(save_blk->blkno);
  1343. xfs_da_log_buf(args->trans, bp, 0,
  1344. sizeof(*tmp_info) - 1);
  1345. xfs_da_buf_done(bp);
  1346. }
  1347. } else {
  1348. save_info->forw = drop_info->forw;
  1349. if (drop_info->forw) {
  1350. error = xfs_da_read_buf(args->trans, args->dp,
  1351. be32_to_cpu(drop_info->forw),
  1352. -1, &bp, args->whichfork);
  1353. if (error)
  1354. return(error);
  1355. ASSERT(bp != NULL);
  1356. tmp_info = bp->data;
  1357. ASSERT(tmp_info->magic == save_info->magic);
  1358. ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
  1359. tmp_info->back = cpu_to_be32(save_blk->blkno);
  1360. xfs_da_log_buf(args->trans, bp, 0,
  1361. sizeof(*tmp_info) - 1);
  1362. xfs_da_buf_done(bp);
  1363. }
  1364. }
  1365. xfs_da_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
  1366. return(0);
  1367. }
  1368. /*
  1369. * Move a path "forward" or "!forward" one block at the current level.
  1370. *
  1371. * This routine will adjust a "path" to point to the next block
  1372. * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
  1373. * Btree, including updating pointers to the intermediate nodes between
  1374. * the new bottom and the root.
  1375. */
  1376. int /* error */
  1377. xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  1378. int forward, int release, int *result)
  1379. {
  1380. xfs_da_state_blk_t *blk;
  1381. xfs_da_blkinfo_t *info;
  1382. xfs_da_intnode_t *node;
  1383. xfs_da_args_t *args;
  1384. xfs_dablk_t blkno=0;
  1385. int level, error;
  1386. /*
  1387. * Roll up the Btree looking for the first block where our
  1388. * current index is not at the edge of the block. Note that
  1389. * we skip the bottom layer because we want the sibling block.
  1390. */
  1391. args = state->args;
  1392. ASSERT(args != NULL);
  1393. ASSERT(path != NULL);
  1394. ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1395. level = (path->active-1) - 1; /* skip bottom layer in path */
  1396. for (blk = &path->blk[level]; level >= 0; blk--, level--) {
  1397. ASSERT(blk->bp != NULL);
  1398. node = blk->bp->data;
  1399. ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  1400. if (forward && (blk->index < be16_to_cpu(node->hdr.count)-1)) {
  1401. blk->index++;
  1402. blkno = be32_to_cpu(node->btree[blk->index].before);
  1403. break;
  1404. } else if (!forward && (blk->index > 0)) {
  1405. blk->index--;
  1406. blkno = be32_to_cpu(node->btree[blk->index].before);
  1407. break;
  1408. }
  1409. }
  1410. if (level < 0) {
  1411. *result = XFS_ERROR(ENOENT); /* we're out of our tree */
  1412. ASSERT(args->oknoent);
  1413. return(0);
  1414. }
  1415. /*
  1416. * Roll down the edge of the subtree until we reach the
  1417. * same depth we were at originally.
  1418. */
  1419. for (blk++, level++; level < path->active; blk++, level++) {
  1420. /*
  1421. * Release the old block.
  1422. * (if it's dirty, trans won't actually let go)
  1423. */
  1424. if (release)
  1425. xfs_da_brelse(args->trans, blk->bp);
  1426. /*
  1427. * Read the next child block.
  1428. */
  1429. blk->blkno = blkno;
  1430. error = xfs_da_read_buf(args->trans, args->dp, blkno, -1,
  1431. &blk->bp, args->whichfork);
  1432. if (error)
  1433. return(error);
  1434. ASSERT(blk->bp != NULL);
  1435. info = blk->bp->data;
  1436. ASSERT(be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC ||
  1437. be16_to_cpu(info->magic) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1438. be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
  1439. blk->magic = be16_to_cpu(info->magic);
  1440. if (blk->magic == XFS_DA_NODE_MAGIC) {
  1441. node = (xfs_da_intnode_t *)info;
  1442. blk->hashval = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
  1443. if (forward)
  1444. blk->index = 0;
  1445. else
  1446. blk->index = be16_to_cpu(node->hdr.count)-1;
  1447. blkno = be32_to_cpu(node->btree[blk->index].before);
  1448. } else {
  1449. ASSERT(level == path->active-1);
  1450. blk->index = 0;
  1451. switch(blk->magic) {
  1452. case XFS_ATTR_LEAF_MAGIC:
  1453. blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
  1454. NULL);
  1455. break;
  1456. case XFS_DIR_LEAF_MAGIC:
  1457. ASSERT(XFS_DIR_IS_V1(state->mp));
  1458. blk->hashval = xfs_dir_leaf_lasthash(blk->bp,
  1459. NULL);
  1460. break;
  1461. case XFS_DIR2_LEAFN_MAGIC:
  1462. ASSERT(XFS_DIR_IS_V2(state->mp));
  1463. blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
  1464. NULL);
  1465. break;
  1466. default:
  1467. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC ||
  1468. blk->magic ==
  1469. XFS_DIRX_LEAF_MAGIC(state->mp));
  1470. break;
  1471. }
  1472. }
  1473. }
  1474. *result = 0;
  1475. return(0);
  1476. }
  1477. /*========================================================================
  1478. * Utility routines.
  1479. *========================================================================*/
  1480. /*
  1481. * Implement a simple hash on a character string.
  1482. * Rotate the hash value by 7 bits, then XOR each character in.
  1483. * This is implemented with some source-level loop unrolling.
  1484. */
  1485. xfs_dahash_t
  1486. xfs_da_hashname(const uchar_t *name, int namelen)
  1487. {
  1488. xfs_dahash_t hash;
  1489. /*
  1490. * Do four characters at a time as long as we can.
  1491. */
  1492. for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
  1493. hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
  1494. (name[3] << 0) ^ rol32(hash, 7 * 4);
  1495. /*
  1496. * Now do the rest of the characters.
  1497. */
  1498. switch (namelen) {
  1499. case 3:
  1500. return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
  1501. rol32(hash, 7 * 3);
  1502. case 2:
  1503. return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
  1504. case 1:
  1505. return (name[0] << 0) ^ rol32(hash, 7 * 1);
  1506. default: /* case 0: */
  1507. return hash;
  1508. }
  1509. }
  1510. /*
  1511. * Add a block to the btree ahead of the file.
  1512. * Return the new block number to the caller.
  1513. */
  1514. int
  1515. xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno)
  1516. {
  1517. xfs_fileoff_t bno, b;
  1518. xfs_bmbt_irec_t map;
  1519. xfs_bmbt_irec_t *mapp;
  1520. xfs_inode_t *dp;
  1521. int nmap, error, w, count, c, got, i, mapi;
  1522. xfs_fsize_t size;
  1523. xfs_trans_t *tp;
  1524. xfs_mount_t *mp;
  1525. dp = args->dp;
  1526. mp = dp->i_mount;
  1527. w = args->whichfork;
  1528. tp = args->trans;
  1529. /*
  1530. * For new directories adjust the file offset and block count.
  1531. */
  1532. if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp)) {
  1533. bno = mp->m_dirleafblk;
  1534. count = mp->m_dirblkfsbs;
  1535. } else {
  1536. bno = 0;
  1537. count = 1;
  1538. }
  1539. /*
  1540. * Find a spot in the file space to put the new block.
  1541. */
  1542. if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, w))) {
  1543. return error;
  1544. }
  1545. if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
  1546. ASSERT(bno >= mp->m_dirleafblk && bno < mp->m_dirfreeblk);
  1547. /*
  1548. * Try mapping it in one filesystem block.
  1549. */
  1550. nmap = 1;
  1551. ASSERT(args->firstblock != NULL);
  1552. if ((error = xfs_bmapi(tp, dp, bno, count,
  1553. XFS_BMAPI_AFLAG(w)|XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|
  1554. XFS_BMAPI_CONTIG,
  1555. args->firstblock, args->total, &map, &nmap,
  1556. args->flist))) {
  1557. return error;
  1558. }
  1559. ASSERT(nmap <= 1);
  1560. if (nmap == 1) {
  1561. mapp = &map;
  1562. mapi = 1;
  1563. }
  1564. /*
  1565. * If we didn't get it and the block might work if fragmented,
  1566. * try without the CONTIG flag. Loop until we get it all.
  1567. */
  1568. else if (nmap == 0 && count > 1) {
  1569. mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
  1570. for (b = bno, mapi = 0; b < bno + count; ) {
  1571. nmap = MIN(XFS_BMAP_MAX_NMAP, count);
  1572. c = (int)(bno + count - b);
  1573. if ((error = xfs_bmapi(tp, dp, b, c,
  1574. XFS_BMAPI_AFLAG(w)|XFS_BMAPI_WRITE|
  1575. XFS_BMAPI_METADATA,
  1576. args->firstblock, args->total,
  1577. &mapp[mapi], &nmap, args->flist))) {
  1578. kmem_free(mapp, sizeof(*mapp) * count);
  1579. return error;
  1580. }
  1581. if (nmap < 1)
  1582. break;
  1583. mapi += nmap;
  1584. b = mapp[mapi - 1].br_startoff +
  1585. mapp[mapi - 1].br_blockcount;
  1586. }
  1587. } else {
  1588. mapi = 0;
  1589. mapp = NULL;
  1590. }
  1591. /*
  1592. * Count the blocks we got, make sure it matches the total.
  1593. */
  1594. for (i = 0, got = 0; i < mapi; i++)
  1595. got += mapp[i].br_blockcount;
  1596. if (got != count || mapp[0].br_startoff != bno ||
  1597. mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
  1598. bno + count) {
  1599. if (mapp != &map)
  1600. kmem_free(mapp, sizeof(*mapp) * count);
  1601. return XFS_ERROR(ENOSPC);
  1602. }
  1603. if (mapp != &map)
  1604. kmem_free(mapp, sizeof(*mapp) * count);
  1605. *new_blkno = (xfs_dablk_t)bno;
  1606. /*
  1607. * For version 1 directories, adjust the file size if it changed.
  1608. */
  1609. if (w == XFS_DATA_FORK && XFS_DIR_IS_V1(mp)) {
  1610. ASSERT(mapi == 1);
  1611. if ((error = xfs_bmap_last_offset(tp, dp, &bno, w)))
  1612. return error;
  1613. size = XFS_FSB_TO_B(mp, bno);
  1614. if (size != dp->i_d.di_size) {
  1615. dp->i_d.di_size = size;
  1616. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1617. }
  1618. }
  1619. return 0;
  1620. }
  1621. /*
  1622. * Ick. We need to always be able to remove a btree block, even
  1623. * if there's no space reservation because the filesystem is full.
  1624. * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
  1625. * It swaps the target block with the last block in the file. The
  1626. * last block in the file can always be removed since it can't cause
  1627. * a bmap btree split to do that.
  1628. */
  1629. STATIC int
  1630. xfs_da_swap_lastblock(xfs_da_args_t *args, xfs_dablk_t *dead_blknop,
  1631. xfs_dabuf_t **dead_bufp)
  1632. {
  1633. xfs_dablk_t dead_blkno, last_blkno, sib_blkno, par_blkno;
  1634. xfs_dabuf_t *dead_buf, *last_buf, *sib_buf, *par_buf;
  1635. xfs_fileoff_t lastoff;
  1636. xfs_inode_t *ip;
  1637. xfs_trans_t *tp;
  1638. xfs_mount_t *mp;
  1639. int error, w, entno, level, dead_level;
  1640. xfs_da_blkinfo_t *dead_info, *sib_info;
  1641. xfs_da_intnode_t *par_node, *dead_node;
  1642. xfs_dir_leafblock_t *dead_leaf;
  1643. xfs_dir2_leaf_t *dead_leaf2;
  1644. xfs_dahash_t dead_hash;
  1645. dead_buf = *dead_bufp;
  1646. dead_blkno = *dead_blknop;
  1647. tp = args->trans;
  1648. ip = args->dp;
  1649. w = args->whichfork;
  1650. ASSERT(w == XFS_DATA_FORK);
  1651. mp = ip->i_mount;
  1652. if (XFS_DIR_IS_V2(mp)) {
  1653. lastoff = mp->m_dirfreeblk;
  1654. error = xfs_bmap_last_before(tp, ip, &lastoff, w);
  1655. } else
  1656. error = xfs_bmap_last_offset(tp, ip, &lastoff, w);
  1657. if (error)
  1658. return error;
  1659. if (unlikely(lastoff == 0)) {
  1660. XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
  1661. mp);
  1662. return XFS_ERROR(EFSCORRUPTED);
  1663. }
  1664. /*
  1665. * Read the last block in the btree space.
  1666. */
  1667. last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
  1668. if ((error = xfs_da_read_buf(tp, ip, last_blkno, -1, &last_buf, w)))
  1669. return error;
  1670. /*
  1671. * Copy the last block into the dead buffer and log it.
  1672. */
  1673. memcpy(dead_buf->data, last_buf->data, mp->m_dirblksize);
  1674. xfs_da_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
  1675. dead_info = dead_buf->data;
  1676. /*
  1677. * Get values from the moved block.
  1678. */
  1679. if (be16_to_cpu(dead_info->magic) == XFS_DIR_LEAF_MAGIC) {
  1680. ASSERT(XFS_DIR_IS_V1(mp));
  1681. dead_leaf = (xfs_dir_leafblock_t *)dead_info;
  1682. dead_level = 0;
  1683. dead_hash =
  1684. INT_GET(dead_leaf->entries[INT_GET(dead_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
  1685. } else if (be16_to_cpu(dead_info->magic) == XFS_DIR2_LEAFN_MAGIC) {
  1686. ASSERT(XFS_DIR_IS_V2(mp));
  1687. dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
  1688. dead_level = 0;
  1689. dead_hash = be32_to_cpu(dead_leaf2->ents[be16_to_cpu(dead_leaf2->hdr.count) - 1].hashval);
  1690. } else {
  1691. ASSERT(be16_to_cpu(dead_info->magic) == XFS_DA_NODE_MAGIC);
  1692. dead_node = (xfs_da_intnode_t *)dead_info;
  1693. dead_level = be16_to_cpu(dead_node->hdr.level);
  1694. dead_hash = be32_to_cpu(dead_node->btree[be16_to_cpu(dead_node->hdr.count) - 1].hashval);
  1695. }
  1696. sib_buf = par_buf = NULL;
  1697. /*
  1698. * If the moved block has a left sibling, fix up the pointers.
  1699. */
  1700. if ((sib_blkno = be32_to_cpu(dead_info->back))) {
  1701. if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
  1702. goto done;
  1703. sib_info = sib_buf->data;
  1704. if (unlikely(
  1705. be32_to_cpu(sib_info->forw) != last_blkno ||
  1706. sib_info->magic != dead_info->magic)) {
  1707. XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
  1708. XFS_ERRLEVEL_LOW, mp);
  1709. error = XFS_ERROR(EFSCORRUPTED);
  1710. goto done;
  1711. }
  1712. sib_info->forw = cpu_to_be32(dead_blkno);
  1713. xfs_da_log_buf(tp, sib_buf,
  1714. XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
  1715. sizeof(sib_info->forw)));
  1716. xfs_da_buf_done(sib_buf);
  1717. sib_buf = NULL;
  1718. }
  1719. /*
  1720. * If the moved block has a right sibling, fix up the pointers.
  1721. */
  1722. if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
  1723. if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
  1724. goto done;
  1725. sib_info = sib_buf->data;
  1726. if (unlikely(
  1727. be32_to_cpu(sib_info->back) != last_blkno ||
  1728. sib_info->magic != dead_info->magic)) {
  1729. XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
  1730. XFS_ERRLEVEL_LOW, mp);
  1731. error = XFS_ERROR(EFSCORRUPTED);
  1732. goto done;
  1733. }
  1734. sib_info->back = cpu_to_be32(dead_blkno);
  1735. xfs_da_log_buf(tp, sib_buf,
  1736. XFS_DA_LOGRANGE(sib_info, &sib_info->back,
  1737. sizeof(sib_info->back)));
  1738. xfs_da_buf_done(sib_buf);
  1739. sib_buf = NULL;
  1740. }
  1741. par_blkno = XFS_DIR_IS_V1(mp) ? 0 : mp->m_dirleafblk;
  1742. level = -1;
  1743. /*
  1744. * Walk down the tree looking for the parent of the moved block.
  1745. */
  1746. for (;;) {
  1747. if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
  1748. goto done;
  1749. par_node = par_buf->data;
  1750. if (unlikely(
  1751. be16_to_cpu(par_node->hdr.info.magic) != XFS_DA_NODE_MAGIC ||
  1752. (level >= 0 && level != be16_to_cpu(par_node->hdr.level) + 1))) {
  1753. XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
  1754. XFS_ERRLEVEL_LOW, mp);
  1755. error = XFS_ERROR(EFSCORRUPTED);
  1756. goto done;
  1757. }
  1758. level = be16_to_cpu(par_node->hdr.level);
  1759. for (entno = 0;
  1760. entno < be16_to_cpu(par_node->hdr.count) &&
  1761. be32_to_cpu(par_node->btree[entno].hashval) < dead_hash;
  1762. entno++)
  1763. continue;
  1764. if (unlikely(entno == be16_to_cpu(par_node->hdr.count))) {
  1765. XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
  1766. XFS_ERRLEVEL_LOW, mp);
  1767. error = XFS_ERROR(EFSCORRUPTED);
  1768. goto done;
  1769. }
  1770. par_blkno = be32_to_cpu(par_node->btree[entno].before);
  1771. if (level == dead_level + 1)
  1772. break;
  1773. xfs_da_brelse(tp, par_buf);
  1774. par_buf = NULL;
  1775. }
  1776. /*
  1777. * We're in the right parent block.
  1778. * Look for the right entry.
  1779. */
  1780. for (;;) {
  1781. for (;
  1782. entno < be16_to_cpu(par_node->hdr.count) &&
  1783. be32_to_cpu(par_node->btree[entno].before) != last_blkno;
  1784. entno++)
  1785. continue;
  1786. if (entno < be16_to_cpu(par_node->hdr.count))
  1787. break;
  1788. par_blkno = be32_to_cpu(par_node->hdr.info.forw);
  1789. xfs_da_brelse(tp, par_buf);
  1790. par_buf = NULL;
  1791. if (unlikely(par_blkno == 0)) {
  1792. XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
  1793. XFS_ERRLEVEL_LOW, mp);
  1794. error = XFS_ERROR(EFSCORRUPTED);
  1795. goto done;
  1796. }
  1797. if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
  1798. goto done;
  1799. par_node = par_buf->data;
  1800. if (unlikely(
  1801. be16_to_cpu(par_node->hdr.level) != level ||
  1802. be16_to_cpu(par_node->hdr.info.magic) != XFS_DA_NODE_MAGIC)) {
  1803. XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
  1804. XFS_ERRLEVEL_LOW, mp);
  1805. error = XFS_ERROR(EFSCORRUPTED);
  1806. goto done;
  1807. }
  1808. entno = 0;
  1809. }
  1810. /*
  1811. * Update the parent entry pointing to the moved block.
  1812. */
  1813. par_node->btree[entno].before = cpu_to_be32(dead_blkno);
  1814. xfs_da_log_buf(tp, par_buf,
  1815. XFS_DA_LOGRANGE(par_node, &par_node->btree[entno].before,
  1816. sizeof(par_node->btree[entno].before)));
  1817. xfs_da_buf_done(par_buf);
  1818. xfs_da_buf_done(dead_buf);
  1819. *dead_blknop = last_blkno;
  1820. *dead_bufp = last_buf;
  1821. return 0;
  1822. done:
  1823. if (par_buf)
  1824. xfs_da_brelse(tp, par_buf);
  1825. if (sib_buf)
  1826. xfs_da_brelse(tp, sib_buf);
  1827. xfs_da_brelse(tp, last_buf);
  1828. return error;
  1829. }
  1830. /*
  1831. * Remove a btree block from a directory or attribute.
  1832. */
  1833. int
  1834. xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  1835. xfs_dabuf_t *dead_buf)
  1836. {
  1837. xfs_inode_t *dp;
  1838. int done, error, w, count;
  1839. xfs_fileoff_t bno;
  1840. xfs_fsize_t size;
  1841. xfs_trans_t *tp;
  1842. xfs_mount_t *mp;
  1843. dp = args->dp;
  1844. w = args->whichfork;
  1845. tp = args->trans;
  1846. mp = dp->i_mount;
  1847. if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
  1848. count = mp->m_dirblkfsbs;
  1849. else
  1850. count = 1;
  1851. for (;;) {
  1852. /*
  1853. * Remove extents. If we get ENOSPC for a dir we have to move
  1854. * the last block to the place we want to kill.
  1855. */
  1856. if ((error = xfs_bunmapi(tp, dp, dead_blkno, count,
  1857. XFS_BMAPI_AFLAG(w)|XFS_BMAPI_METADATA,
  1858. 0, args->firstblock, args->flist,
  1859. &done)) == ENOSPC) {
  1860. if (w != XFS_DATA_FORK)
  1861. goto done;
  1862. if ((error = xfs_da_swap_lastblock(args, &dead_blkno,
  1863. &dead_buf)))
  1864. goto done;
  1865. } else if (error)
  1866. goto done;
  1867. else
  1868. break;
  1869. }
  1870. ASSERT(done);
  1871. xfs_da_binval(tp, dead_buf);
  1872. /*
  1873. * Adjust the directory size for version 1.
  1874. */
  1875. if (w == XFS_DATA_FORK && XFS_DIR_IS_V1(mp)) {
  1876. if ((error = xfs_bmap_last_offset(tp, dp, &bno, w)))
  1877. return error;
  1878. size = XFS_FSB_TO_B(dp->i_mount, bno);
  1879. if (size != dp->i_d.di_size) {
  1880. dp->i_d.di_size = size;
  1881. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1882. }
  1883. }
  1884. return 0;
  1885. done:
  1886. xfs_da_binval(tp, dead_buf);
  1887. return error;
  1888. }
  1889. /*
  1890. * See if the mapping(s) for this btree block are valid, i.e.
  1891. * don't contain holes, are logically contiguous, and cover the whole range.
  1892. */
  1893. STATIC int
  1894. xfs_da_map_covers_blocks(
  1895. int nmap,
  1896. xfs_bmbt_irec_t *mapp,
  1897. xfs_dablk_t bno,
  1898. int count)
  1899. {
  1900. int i;
  1901. xfs_fileoff_t off;
  1902. for (i = 0, off = bno; i < nmap; i++) {
  1903. if (mapp[i].br_startblock == HOLESTARTBLOCK ||
  1904. mapp[i].br_startblock == DELAYSTARTBLOCK) {
  1905. return 0;
  1906. }
  1907. if (off != mapp[i].br_startoff) {
  1908. return 0;
  1909. }
  1910. off += mapp[i].br_blockcount;
  1911. }
  1912. return off == bno + count;
  1913. }
  1914. /*
  1915. * Make a dabuf.
  1916. * Used for get_buf, read_buf, read_bufr, and reada_buf.
  1917. */
  1918. STATIC int
  1919. xfs_da_do_buf(
  1920. xfs_trans_t *trans,
  1921. xfs_inode_t *dp,
  1922. xfs_dablk_t bno,
  1923. xfs_daddr_t *mappedbnop,
  1924. xfs_dabuf_t **bpp,
  1925. int whichfork,
  1926. int caller,
  1927. inst_t *ra)
  1928. {
  1929. xfs_buf_t *bp = NULL;
  1930. xfs_buf_t **bplist;
  1931. int error=0;
  1932. int i;
  1933. xfs_bmbt_irec_t map;
  1934. xfs_bmbt_irec_t *mapp;
  1935. xfs_daddr_t mappedbno;
  1936. xfs_mount_t *mp;
  1937. int nbplist=0;
  1938. int nfsb;
  1939. int nmap;
  1940. xfs_dabuf_t *rbp;
  1941. mp = dp->i_mount;
  1942. if (whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
  1943. nfsb = mp->m_dirblkfsbs;
  1944. else
  1945. nfsb = 1;
  1946. mappedbno = *mappedbnop;
  1947. /*
  1948. * Caller doesn't have a mapping. -2 means don't complain
  1949. * if we land in a hole.
  1950. */
  1951. if (mappedbno == -1 || mappedbno == -2) {
  1952. /*
  1953. * Optimize the one-block case.
  1954. */
  1955. if (nfsb == 1) {
  1956. xfs_fsblock_t fsb;
  1957. if ((error =
  1958. xfs_bmapi_single(trans, dp, whichfork, &fsb,
  1959. (xfs_fileoff_t)bno))) {
  1960. return error;
  1961. }
  1962. mapp = &map;
  1963. if (fsb == NULLFSBLOCK) {
  1964. nmap = 0;
  1965. } else {
  1966. map.br_startblock = fsb;
  1967. map.br_startoff = (xfs_fileoff_t)bno;
  1968. map.br_blockcount = 1;
  1969. nmap = 1;
  1970. }
  1971. } else {
  1972. mapp = kmem_alloc(sizeof(*mapp) * nfsb, KM_SLEEP);
  1973. nmap = nfsb;
  1974. if ((error = xfs_bmapi(trans, dp, (xfs_fileoff_t)bno,
  1975. nfsb,
  1976. XFS_BMAPI_METADATA |
  1977. XFS_BMAPI_AFLAG(whichfork),
  1978. NULL, 0, mapp, &nmap, NULL)))
  1979. goto exit0;
  1980. }
  1981. } else {
  1982. map.br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
  1983. map.br_startoff = (xfs_fileoff_t)bno;
  1984. map.br_blockcount = nfsb;
  1985. mapp = &map;
  1986. nmap = 1;
  1987. }
  1988. if (!xfs_da_map_covers_blocks(nmap, mapp, bno, nfsb)) {
  1989. error = mappedbno == -2 ? 0 : XFS_ERROR(EFSCORRUPTED);
  1990. if (unlikely(error == EFSCORRUPTED)) {
  1991. if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
  1992. int i;
  1993. cmn_err(CE_ALERT, "xfs_da_do_buf: bno %lld\n",
  1994. (long long)bno);
  1995. cmn_err(CE_ALERT, "dir: inode %lld\n",
  1996. (long long)dp->i_ino);
  1997. for (i = 0; i < nmap; i++) {
  1998. cmn_err(CE_ALERT,
  1999. "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d\n",
  2000. i,
  2001. (long long)mapp[i].br_startoff,
  2002. (long long)mapp[i].br_startblock,
  2003. (long long)mapp[i].br_blockcount,
  2004. mapp[i].br_state);
  2005. }
  2006. }
  2007. XFS_ERROR_REPORT("xfs_da_do_buf(1)",
  2008. XFS_ERRLEVEL_LOW, mp);
  2009. }
  2010. goto exit0;
  2011. }
  2012. if (caller != 3 && nmap > 1) {
  2013. bplist = kmem_alloc(sizeof(*bplist) * nmap, KM_SLEEP);
  2014. nbplist = 0;
  2015. } else
  2016. bplist = NULL;
  2017. /*
  2018. * Turn the mapping(s) into buffer(s).
  2019. */
  2020. for (i = 0; i < nmap; i++) {
  2021. int nmapped;
  2022. mappedbno = XFS_FSB_TO_DADDR(mp, mapp[i].br_startblock);
  2023. if (i == 0)
  2024. *mappedbnop = mappedbno;
  2025. nmapped = (int)XFS_FSB_TO_BB(mp, mapp[i].br_blockcount);
  2026. switch (caller) {
  2027. case 0:
  2028. bp = xfs_trans_get_buf(trans, mp->m_ddev_targp,
  2029. mappedbno, nmapped, 0);
  2030. error = bp ? XFS_BUF_GETERROR(bp) : XFS_ERROR(EIO);
  2031. break;
  2032. case 1:
  2033. case 2:
  2034. bp = NULL;
  2035. error = xfs_trans_read_buf(mp, trans, mp->m_ddev_targp,
  2036. mappedbno, nmapped, 0, &bp);
  2037. break;
  2038. case 3:
  2039. xfs_baread(mp->m_ddev_targp, mappedbno, nmapped);
  2040. error = 0;
  2041. bp = NULL;
  2042. break;
  2043. }
  2044. if (error) {
  2045. if (bp)
  2046. xfs_trans_brelse(trans, bp);
  2047. goto exit1;
  2048. }
  2049. if (!bp)
  2050. continue;
  2051. if (caller == 1) {
  2052. if (whichfork == XFS_ATTR_FORK) {
  2053. XFS_BUF_SET_VTYPE_REF(bp, B_FS_ATTR_BTREE,
  2054. XFS_ATTR_BTREE_REF);
  2055. } else {
  2056. XFS_BUF_SET_VTYPE_REF(bp, B_FS_DIR_BTREE,
  2057. XFS_DIR_BTREE_REF);
  2058. }
  2059. }
  2060. if (bplist) {
  2061. bplist[nbplist++] = bp;
  2062. }
  2063. }
  2064. /*
  2065. * Build a dabuf structure.
  2066. */
  2067. if (bplist) {
  2068. rbp = xfs_da_buf_make(nbplist, bplist, ra);
  2069. } else if (bp)
  2070. rbp = xfs_da_buf_make(1, &bp, ra);
  2071. else
  2072. rbp = NULL;
  2073. /*
  2074. * For read_buf, check the magic number.
  2075. */
  2076. if (caller == 1) {
  2077. xfs_dir2_data_t *data;
  2078. xfs_dir2_free_t *free;
  2079. xfs_da_blkinfo_t *info;
  2080. uint magic, magic1;
  2081. info = rbp->data;
  2082. data = rbp->data;
  2083. free = rbp->data;
  2084. magic = be16_to_cpu(info->magic);
  2085. magic1 = be32_to_cpu(data->hdr.magic);
  2086. if (unlikely(
  2087. XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
  2088. (magic != XFS_DIR_LEAF_MAGIC) &&
  2089. (magic != XFS_ATTR_LEAF_MAGIC) &&
  2090. (magic != XFS_DIR2_LEAF1_MAGIC) &&
  2091. (magic != XFS_DIR2_LEAFN_MAGIC) &&
  2092. (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
  2093. (magic1 != XFS_DIR2_DATA_MAGIC) &&
  2094. (be32_to_cpu(free->hdr.magic) != XFS_DIR2_FREE_MAGIC),
  2095. mp, XFS_ERRTAG_DA_READ_BUF,
  2096. XFS_RANDOM_DA_READ_BUF))) {
  2097. xfs_buftrace("DA READ ERROR", rbp->bps[0]);
  2098. XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
  2099. XFS_ERRLEVEL_LOW, mp, info);
  2100. error = XFS_ERROR(EFSCORRUPTED);
  2101. xfs_da_brelse(trans, rbp);
  2102. nbplist = 0;
  2103. goto exit1;
  2104. }
  2105. }
  2106. if (bplist) {
  2107. kmem_free(bplist, sizeof(*bplist) * nmap);
  2108. }
  2109. if (mapp != &map) {
  2110. kmem_free(mapp, sizeof(*mapp) * nfsb);
  2111. }
  2112. if (bpp)
  2113. *bpp = rbp;
  2114. return 0;
  2115. exit1:
  2116. if (bplist) {
  2117. for (i = 0; i < nbplist; i++)
  2118. xfs_trans_brelse(trans, bplist[i]);
  2119. kmem_free(bplist, sizeof(*bplist) * nmap);
  2120. }
  2121. exit0:
  2122. if (mapp != &map)
  2123. kmem_free(mapp, sizeof(*mapp) * nfsb);
  2124. if (bpp)
  2125. *bpp = NULL;
  2126. return error;
  2127. }
  2128. /*
  2129. * Get a buffer for the dir/attr block.
  2130. */
  2131. int
  2132. xfs_da_get_buf(
  2133. xfs_trans_t *trans,
  2134. xfs_inode_t *dp,
  2135. xfs_dablk_t bno,
  2136. xfs_daddr_t mappedbno,
  2137. xfs_dabuf_t **bpp,
  2138. int whichfork)
  2139. {
  2140. return xfs_da_do_buf(trans, dp, bno, &mappedbno, bpp, whichfork, 0,
  2141. (inst_t *)__return_address);
  2142. }
  2143. /*
  2144. * Get a buffer for the dir/attr block, fill in the contents.
  2145. */
  2146. int
  2147. xfs_da_read_buf(
  2148. xfs_trans_t *trans,
  2149. xfs_inode_t *dp,
  2150. xfs_dablk_t bno,
  2151. xfs_daddr_t mappedbno,
  2152. xfs_dabuf_t **bpp,
  2153. int whichfork)
  2154. {
  2155. return xfs_da_do_buf(trans, dp, bno, &mappedbno, bpp, whichfork, 1,
  2156. (inst_t *)__return_address);
  2157. }
  2158. /*
  2159. * Readahead the dir/attr block.
  2160. */
  2161. xfs_daddr_t
  2162. xfs_da_reada_buf(
  2163. xfs_trans_t *trans,
  2164. xfs_inode_t *dp,
  2165. xfs_dablk_t bno,
  2166. int whichfork)
  2167. {
  2168. xfs_daddr_t rval;
  2169. rval = -1;
  2170. if (xfs_da_do_buf(trans, dp, bno, &rval, NULL, whichfork, 3,
  2171. (inst_t *)__return_address))
  2172. return -1;
  2173. else
  2174. return rval;
  2175. }
  2176. /*
  2177. * Calculate the number of bits needed to hold i different values.
  2178. */
  2179. uint
  2180. xfs_da_log2_roundup(uint i)
  2181. {
  2182. uint rval;
  2183. for (rval = 0; rval < NBBY * sizeof(i); rval++) {
  2184. if ((1 << rval) >= i)
  2185. break;
  2186. }
  2187. return(rval);
  2188. }
  2189. kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
  2190. kmem_zone_t *xfs_dabuf_zone; /* dabuf zone */
  2191. /*
  2192. * Allocate a dir-state structure.
  2193. * We don't put them on the stack since they're large.
  2194. */
  2195. xfs_da_state_t *
  2196. xfs_da_state_alloc(void)
  2197. {
  2198. return kmem_zone_zalloc(xfs_da_state_zone, KM_SLEEP);
  2199. }
  2200. /*
  2201. * Kill the altpath contents of a da-state structure.
  2202. */
  2203. STATIC void
  2204. xfs_da_state_kill_altpath(xfs_da_state_t *state)
  2205. {
  2206. int i;
  2207. for (i = 0; i < state->altpath.active; i++) {
  2208. if (state->altpath.blk[i].bp) {
  2209. if (state->altpath.blk[i].bp != state->path.blk[i].bp)
  2210. xfs_da_buf_done(state->altpath.blk[i].bp);
  2211. state->altpath.blk[i].bp = NULL;
  2212. }
  2213. }
  2214. state->altpath.active = 0;
  2215. }
  2216. /*
  2217. * Free a da-state structure.
  2218. */
  2219. void
  2220. xfs_da_state_free(xfs_da_state_t *state)
  2221. {
  2222. int i;
  2223. xfs_da_state_kill_altpath(state);
  2224. for (i = 0; i < state->path.active; i++) {
  2225. if (state->path.blk[i].bp)
  2226. xfs_da_buf_done(state->path.blk[i].bp);
  2227. }
  2228. if (state->extravalid && state->extrablk.bp)
  2229. xfs_da_buf_done(state->extrablk.bp);
  2230. #ifdef DEBUG
  2231. memset((char *)state, 0, sizeof(*state));
  2232. #endif /* DEBUG */
  2233. kmem_zone_free(xfs_da_state_zone, state);
  2234. }
  2235. #ifdef XFS_DABUF_DEBUG
  2236. xfs_dabuf_t *xfs_dabuf_global_list;
  2237. lock_t xfs_dabuf_global_lock;
  2238. #endif
  2239. /*
  2240. * Create a dabuf.
  2241. */
  2242. /* ARGSUSED */
  2243. STATIC xfs_dabuf_t *
  2244. xfs_da_buf_make(int nbuf, xfs_buf_t **bps, inst_t *ra)
  2245. {
  2246. xfs_buf_t *bp;
  2247. xfs_dabuf_t *dabuf;
  2248. int i;
  2249. int off;
  2250. if (nbuf == 1)
  2251. dabuf = kmem_zone_alloc(xfs_dabuf_zone, KM_SLEEP);
  2252. else
  2253. dabuf = kmem_alloc(XFS_DA_BUF_SIZE(nbuf), KM_SLEEP);
  2254. dabuf->dirty = 0;
  2255. #ifdef XFS_DABUF_DEBUG
  2256. dabuf->ra = ra;
  2257. dabuf->target = XFS_BUF_TARGET(bps[0]);
  2258. dabuf->blkno = XFS_BUF_ADDR(bps[0]);
  2259. #endif
  2260. if (nbuf == 1) {
  2261. dabuf->nbuf = 1;
  2262. bp = bps[0];
  2263. dabuf->bbcount = (short)BTOBB(XFS_BUF_COUNT(bp));
  2264. dabuf->data = XFS_BUF_PTR(bp);
  2265. dabuf->bps[0] = bp;
  2266. } else {
  2267. dabuf->nbuf = nbuf;
  2268. for (i = 0, dabuf->bbcount = 0; i < nbuf; i++) {
  2269. dabuf->bps[i] = bp = bps[i];
  2270. dabuf->bbcount += BTOBB(XFS_BUF_COUNT(bp));
  2271. }
  2272. dabuf->data = kmem_alloc(BBTOB(dabuf->bbcount), KM_SLEEP);
  2273. for (i = off = 0; i < nbuf; i++, off += XFS_BUF_COUNT(bp)) {
  2274. bp = bps[i];
  2275. memcpy((char *)dabuf->data + off, XFS_BUF_PTR(bp),
  2276. XFS_BUF_COUNT(bp));
  2277. }
  2278. }
  2279. #ifdef XFS_DABUF_DEBUG
  2280. {
  2281. SPLDECL(s);
  2282. xfs_dabuf_t *p;
  2283. s = mutex_spinlock(&xfs_dabuf_global_lock);
  2284. for (p = xfs_dabuf_global_list; p; p = p->next) {
  2285. ASSERT(p->blkno != dabuf->blkno ||
  2286. p->target != dabuf->target);
  2287. }
  2288. dabuf->prev = NULL;
  2289. if (xfs_dabuf_global_list)
  2290. xfs_dabuf_global_list->prev = dabuf;
  2291. dabuf->next = xfs_dabuf_global_list;
  2292. xfs_dabuf_global_list = dabuf;
  2293. mutex_spinunlock(&xfs_dabuf_global_lock, s);
  2294. }
  2295. #endif
  2296. return dabuf;
  2297. }
  2298. /*
  2299. * Un-dirty a dabuf.
  2300. */
  2301. STATIC void
  2302. xfs_da_buf_clean(xfs_dabuf_t *dabuf)
  2303. {
  2304. xfs_buf_t *bp;
  2305. int i;
  2306. int off;
  2307. if (dabuf->dirty) {
  2308. ASSERT(dabuf->nbuf > 1);
  2309. dabuf->dirty = 0;
  2310. for (i = off = 0; i < dabuf->nbuf;
  2311. i++, off += XFS_BUF_COUNT(bp)) {
  2312. bp = dabuf->bps[i];
  2313. memcpy(XFS_BUF_PTR(bp), (char *)dabuf->data + off,
  2314. XFS_BUF_COUNT(bp));
  2315. }
  2316. }
  2317. }
  2318. /*
  2319. * Release a dabuf.
  2320. */
  2321. void
  2322. xfs_da_buf_done(xfs_dabuf_t *dabuf)
  2323. {
  2324. ASSERT(dabuf);
  2325. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2326. if (dabuf->dirty)
  2327. xfs_da_buf_clean(dabuf);
  2328. if (dabuf->nbuf > 1)
  2329. kmem_free(dabuf->data, BBTOB(dabuf->bbcount));
  2330. #ifdef XFS_DABUF_DEBUG
  2331. {
  2332. SPLDECL(s);
  2333. s = mutex_spinlock(&xfs_dabuf_global_lock);
  2334. if (dabuf->prev)
  2335. dabuf->prev->next = dabuf->next;
  2336. else
  2337. xfs_dabuf_global_list = dabuf->next;
  2338. if (dabuf->next)
  2339. dabuf->next->prev = dabuf->prev;
  2340. mutex_spinunlock(&xfs_dabuf_global_lock, s);
  2341. }
  2342. memset(dabuf, 0, XFS_DA_BUF_SIZE(dabuf->nbuf));
  2343. #endif
  2344. if (dabuf->nbuf == 1)
  2345. kmem_zone_free(xfs_dabuf_zone, dabuf);
  2346. else
  2347. kmem_free(dabuf, XFS_DA_BUF_SIZE(dabuf->nbuf));
  2348. }
  2349. /*
  2350. * Log transaction from a dabuf.
  2351. */
  2352. void
  2353. xfs_da_log_buf(xfs_trans_t *tp, xfs_dabuf_t *dabuf, uint first, uint last)
  2354. {
  2355. xfs_buf_t *bp;
  2356. uint f;
  2357. int i;
  2358. uint l;
  2359. int off;
  2360. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2361. if (dabuf->nbuf == 1) {
  2362. ASSERT(dabuf->data == (void *)XFS_BUF_PTR(dabuf->bps[0]));
  2363. xfs_trans_log_buf(tp, dabuf->bps[0], first, last);
  2364. return;
  2365. }
  2366. dabuf->dirty = 1;
  2367. ASSERT(first <= last);
  2368. for (i = off = 0; i < dabuf->nbuf; i++, off += XFS_BUF_COUNT(bp)) {
  2369. bp = dabuf->bps[i];
  2370. f = off;
  2371. l = f + XFS_BUF_COUNT(bp) - 1;
  2372. if (f < first)
  2373. f = first;
  2374. if (l > last)
  2375. l = last;
  2376. if (f <= l)
  2377. xfs_trans_log_buf(tp, bp, f - off, l - off);
  2378. /*
  2379. * B_DONE is set by xfs_trans_log buf.
  2380. * If we don't set it on a new buffer (get not read)
  2381. * then if we don't put anything in the buffer it won't
  2382. * be set, and at commit it it released into the cache,
  2383. * and then a read will fail.
  2384. */
  2385. else if (!(XFS_BUF_ISDONE(bp)))
  2386. XFS_BUF_DONE(bp);
  2387. }
  2388. ASSERT(last < off);
  2389. }
  2390. /*
  2391. * Release dabuf from a transaction.
  2392. * Have to free up the dabuf before the buffers are released,
  2393. * since the synchronization on the dabuf is really the lock on the buffer.
  2394. */
  2395. void
  2396. xfs_da_brelse(xfs_trans_t *tp, xfs_dabuf_t *dabuf)
  2397. {
  2398. xfs_buf_t *bp;
  2399. xfs_buf_t **bplist;
  2400. int i;
  2401. int nbuf;
  2402. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2403. if ((nbuf = dabuf->nbuf) == 1) {
  2404. bplist = &bp;
  2405. bp = dabuf->bps[0];
  2406. } else {
  2407. bplist = kmem_alloc(nbuf * sizeof(*bplist), KM_SLEEP);
  2408. memcpy(bplist, dabuf->bps, nbuf * sizeof(*bplist));
  2409. }
  2410. xfs_da_buf_done(dabuf);
  2411. for (i = 0; i < nbuf; i++)
  2412. xfs_trans_brelse(tp, bplist[i]);
  2413. if (bplist != &bp)
  2414. kmem_free(bplist, nbuf * sizeof(*bplist));
  2415. }
  2416. /*
  2417. * Invalidate dabuf from a transaction.
  2418. */
  2419. void
  2420. xfs_da_binval(xfs_trans_t *tp, xfs_dabuf_t *dabuf)
  2421. {
  2422. xfs_buf_t *bp;
  2423. xfs_buf_t **bplist;
  2424. int i;
  2425. int nbuf;
  2426. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2427. if ((nbuf = dabuf->nbuf) == 1) {
  2428. bplist = &bp;
  2429. bp = dabuf->bps[0];
  2430. } else {
  2431. bplist = kmem_alloc(nbuf * sizeof(*bplist), KM_SLEEP);
  2432. memcpy(bplist, dabuf->bps, nbuf * sizeof(*bplist));
  2433. }
  2434. xfs_da_buf_done(dabuf);
  2435. for (i = 0; i < nbuf; i++)
  2436. xfs_trans_binval(tp, bplist[i]);
  2437. if (bplist != &bp)
  2438. kmem_free(bplist, nbuf * sizeof(*bplist));
  2439. }
  2440. /*
  2441. * Get the first daddr from a dabuf.
  2442. */
  2443. xfs_daddr_t
  2444. xfs_da_blkno(xfs_dabuf_t *dabuf)
  2445. {
  2446. ASSERT(dabuf->nbuf);
  2447. ASSERT(dabuf->data);
  2448. return XFS_BUF_ADDR(dabuf->bps[0]);
  2449. }