xfs_da_btree.c 72 KB

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