xfs_da_btree.c 71 KB

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