xfs_da_btree.c 70 KB

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