xfs_da_btree.c 73 KB

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