xfs_da_btree.c 70 KB

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