xfs_da_btree.c 65 KB

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