xfs_da_btree.c 70 KB

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