xfs_da_btree.c 71 KB

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