xfs_da_btree.c 66 KB

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