xfs_da_btree.c 63 KB

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