xfs_da_btree.c 66 KB

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