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