xfs_da_btree.c 73 KB

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