xfs_da_btree.c 70 KB

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