xfs_da_btree.c 72 KB

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