xfs_da_btree.c 72 KB

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