xfs_da_btree.c 70 KB

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