xfs_da_btree.c 72 KB

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