xfs_da_btree.c 71 KB

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