xfs_attr_leaf.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_format.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_trans.h"
  32. #include "xfs_inode_item.h"
  33. #include "xfs_bmap_btree.h"
  34. #include "xfs_bmap.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_attr_remote.h"
  37. #include "xfs_attr.h"
  38. #include "xfs_attr_leaf.h"
  39. #include "xfs_error.h"
  40. #include "xfs_trace.h"
  41. #include "xfs_buf_item.h"
  42. #include "xfs_cksum.h"
  43. #include "xfs_dir2.h"
  44. #include "xfs_log.h"
  45. /*
  46. * xfs_attr_leaf.c
  47. *
  48. * Routines to implement leaf blocks of attributes as Btrees of hashed names.
  49. */
  50. /*========================================================================
  51. * Function prototypes for the kernel.
  52. *========================================================================*/
  53. /*
  54. * Routines used for growing the Btree.
  55. */
  56. STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
  57. xfs_dablk_t which_block, struct xfs_buf **bpp);
  58. STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
  59. struct xfs_attr3_icleaf_hdr *ichdr,
  60. struct xfs_da_args *args, int freemap_index);
  61. STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
  62. struct xfs_attr3_icleaf_hdr *ichdr,
  63. struct xfs_buf *leaf_buffer);
  64. STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
  65. xfs_da_state_blk_t *blk1,
  66. xfs_da_state_blk_t *blk2);
  67. STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
  68. xfs_da_state_blk_t *leaf_blk_1,
  69. struct xfs_attr3_icleaf_hdr *ichdr1,
  70. xfs_da_state_blk_t *leaf_blk_2,
  71. struct xfs_attr3_icleaf_hdr *ichdr2,
  72. int *number_entries_in_blk1,
  73. int *number_usedbytes_in_blk1);
  74. /*
  75. * Utility routines.
  76. */
  77. STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
  78. struct xfs_attr_leafblock *src_leaf,
  79. struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
  80. struct xfs_attr_leafblock *dst_leaf,
  81. struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
  82. int move_count);
  83. STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
  84. /*
  85. * attr3 block 'firstused' conversion helpers.
  86. *
  87. * firstused refers to the offset of the first used byte of the nameval region
  88. * of an attr leaf block. The region starts at the tail of the block and expands
  89. * backwards towards the middle. As such, firstused is initialized to the block
  90. * size for an empty leaf block and is reduced from there.
  91. *
  92. * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
  93. * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
  94. * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
  95. * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
  96. * the attr block size. The following helpers manage the conversion between the
  97. * in-core and on-disk formats.
  98. */
  99. static void
  100. xfs_attr3_leaf_firstused_from_disk(
  101. struct xfs_da_geometry *geo,
  102. struct xfs_attr3_icleaf_hdr *to,
  103. struct xfs_attr_leafblock *from)
  104. {
  105. struct xfs_attr3_leaf_hdr *hdr3;
  106. if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
  107. hdr3 = (struct xfs_attr3_leaf_hdr *) from;
  108. to->firstused = be16_to_cpu(hdr3->firstused);
  109. } else {
  110. to->firstused = be16_to_cpu(from->hdr.firstused);
  111. }
  112. /*
  113. * Convert from the magic fsb size value to actual blocksize. This
  114. * should only occur for empty blocks when the block size overflows
  115. * 16-bits.
  116. */
  117. if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
  118. ASSERT(!to->count && !to->usedbytes);
  119. ASSERT(geo->blksize > USHRT_MAX);
  120. to->firstused = geo->blksize;
  121. }
  122. }
  123. static void
  124. xfs_attr3_leaf_firstused_to_disk(
  125. struct xfs_da_geometry *geo,
  126. struct xfs_attr_leafblock *to,
  127. struct xfs_attr3_icleaf_hdr *from)
  128. {
  129. struct xfs_attr3_leaf_hdr *hdr3;
  130. uint32_t firstused;
  131. /* magic value should only be seen on disk */
  132. ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
  133. /*
  134. * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
  135. * value. This only overflows at the max supported value of 64k. Use the
  136. * magic on-disk value to represent block size in this case.
  137. */
  138. firstused = from->firstused;
  139. if (firstused > USHRT_MAX) {
  140. ASSERT(from->firstused == geo->blksize);
  141. firstused = XFS_ATTR3_LEAF_NULLOFF;
  142. }
  143. if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
  144. hdr3 = (struct xfs_attr3_leaf_hdr *) to;
  145. hdr3->firstused = cpu_to_be16(firstused);
  146. } else {
  147. to->hdr.firstused = cpu_to_be16(firstused);
  148. }
  149. }
  150. void
  151. xfs_attr3_leaf_hdr_from_disk(
  152. struct xfs_da_geometry *geo,
  153. struct xfs_attr3_icleaf_hdr *to,
  154. struct xfs_attr_leafblock *from)
  155. {
  156. int i;
  157. ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
  158. from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
  159. if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
  160. struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
  161. to->forw = be32_to_cpu(hdr3->info.hdr.forw);
  162. to->back = be32_to_cpu(hdr3->info.hdr.back);
  163. to->magic = be16_to_cpu(hdr3->info.hdr.magic);
  164. to->count = be16_to_cpu(hdr3->count);
  165. to->usedbytes = be16_to_cpu(hdr3->usedbytes);
  166. xfs_attr3_leaf_firstused_from_disk(geo, to, from);
  167. to->holes = hdr3->holes;
  168. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
  169. to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
  170. to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
  171. }
  172. return;
  173. }
  174. to->forw = be32_to_cpu(from->hdr.info.forw);
  175. to->back = be32_to_cpu(from->hdr.info.back);
  176. to->magic = be16_to_cpu(from->hdr.info.magic);
  177. to->count = be16_to_cpu(from->hdr.count);
  178. to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
  179. xfs_attr3_leaf_firstused_from_disk(geo, to, from);
  180. to->holes = from->hdr.holes;
  181. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
  182. to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
  183. to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
  184. }
  185. }
  186. void
  187. xfs_attr3_leaf_hdr_to_disk(
  188. struct xfs_da_geometry *geo,
  189. struct xfs_attr_leafblock *to,
  190. struct xfs_attr3_icleaf_hdr *from)
  191. {
  192. int i;
  193. ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
  194. from->magic == XFS_ATTR3_LEAF_MAGIC);
  195. if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
  196. struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
  197. hdr3->info.hdr.forw = cpu_to_be32(from->forw);
  198. hdr3->info.hdr.back = cpu_to_be32(from->back);
  199. hdr3->info.hdr.magic = cpu_to_be16(from->magic);
  200. hdr3->count = cpu_to_be16(from->count);
  201. hdr3->usedbytes = cpu_to_be16(from->usedbytes);
  202. xfs_attr3_leaf_firstused_to_disk(geo, to, from);
  203. hdr3->holes = from->holes;
  204. hdr3->pad1 = 0;
  205. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
  206. hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
  207. hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
  208. }
  209. return;
  210. }
  211. to->hdr.info.forw = cpu_to_be32(from->forw);
  212. to->hdr.info.back = cpu_to_be32(from->back);
  213. to->hdr.info.magic = cpu_to_be16(from->magic);
  214. to->hdr.count = cpu_to_be16(from->count);
  215. to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
  216. xfs_attr3_leaf_firstused_to_disk(geo, to, from);
  217. to->hdr.holes = from->holes;
  218. to->hdr.pad1 = 0;
  219. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
  220. to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
  221. to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
  222. }
  223. }
  224. static bool
  225. xfs_attr3_leaf_verify(
  226. struct xfs_buf *bp)
  227. {
  228. struct xfs_mount *mp = bp->b_target->bt_mount;
  229. struct xfs_attr_leafblock *leaf = bp->b_addr;
  230. struct xfs_attr3_icleaf_hdr ichdr;
  231. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
  232. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  233. struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
  234. if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC)
  235. return false;
  236. if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
  237. return false;
  238. if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
  239. return false;
  240. if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
  241. return false;
  242. } else {
  243. if (ichdr.magic != XFS_ATTR_LEAF_MAGIC)
  244. return false;
  245. }
  246. if (ichdr.count == 0)
  247. return false;
  248. /* XXX: need to range check rest of attr header values */
  249. /* XXX: hash order check? */
  250. return true;
  251. }
  252. static void
  253. xfs_attr3_leaf_write_verify(
  254. struct xfs_buf *bp)
  255. {
  256. struct xfs_mount *mp = bp->b_target->bt_mount;
  257. struct xfs_buf_log_item *bip = bp->b_fspriv;
  258. struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
  259. if (!xfs_attr3_leaf_verify(bp)) {
  260. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  261. xfs_verifier_error(bp);
  262. return;
  263. }
  264. if (!xfs_sb_version_hascrc(&mp->m_sb))
  265. return;
  266. if (bip)
  267. hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
  268. xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
  269. }
  270. /*
  271. * leaf/node format detection on trees is sketchy, so a node read can be done on
  272. * leaf level blocks when detection identifies the tree as a node format tree
  273. * incorrectly. In this case, we need to swap the verifier to match the correct
  274. * format of the block being read.
  275. */
  276. static void
  277. xfs_attr3_leaf_read_verify(
  278. struct xfs_buf *bp)
  279. {
  280. struct xfs_mount *mp = bp->b_target->bt_mount;
  281. if (xfs_sb_version_hascrc(&mp->m_sb) &&
  282. !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
  283. xfs_buf_ioerror(bp, -EFSBADCRC);
  284. else if (!xfs_attr3_leaf_verify(bp))
  285. xfs_buf_ioerror(bp, -EFSCORRUPTED);
  286. if (bp->b_error)
  287. xfs_verifier_error(bp);
  288. }
  289. const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
  290. .name = "xfs_attr3_leaf",
  291. .verify_read = xfs_attr3_leaf_read_verify,
  292. .verify_write = xfs_attr3_leaf_write_verify,
  293. };
  294. int
  295. xfs_attr3_leaf_read(
  296. struct xfs_trans *tp,
  297. struct xfs_inode *dp,
  298. xfs_dablk_t bno,
  299. xfs_daddr_t mappedbno,
  300. struct xfs_buf **bpp)
  301. {
  302. int err;
  303. err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
  304. XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
  305. if (!err && tp)
  306. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
  307. return err;
  308. }
  309. /*========================================================================
  310. * Namespace helper routines
  311. *========================================================================*/
  312. /*
  313. * If namespace bits don't match return 0.
  314. * If all match then return 1.
  315. */
  316. STATIC int
  317. xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
  318. {
  319. return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
  320. }
  321. /*========================================================================
  322. * External routines when attribute fork size < XFS_LITINO(mp).
  323. *========================================================================*/
  324. /*
  325. * Query whether the requested number of additional bytes of extended
  326. * attribute space will be able to fit inline.
  327. *
  328. * Returns zero if not, else the di_forkoff fork offset to be used in the
  329. * literal area for attribute data once the new bytes have been added.
  330. *
  331. * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
  332. * special case for dev/uuid inodes, they have fixed size data forks.
  333. */
  334. int
  335. xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
  336. {
  337. int offset;
  338. int minforkoff; /* lower limit on valid forkoff locations */
  339. int maxforkoff; /* upper limit on valid forkoff locations */
  340. int dsize;
  341. xfs_mount_t *mp = dp->i_mount;
  342. /* rounded down */
  343. offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
  344. switch (dp->i_d.di_format) {
  345. case XFS_DINODE_FMT_DEV:
  346. minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  347. return (offset >= minforkoff) ? minforkoff : 0;
  348. case XFS_DINODE_FMT_UUID:
  349. minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
  350. return (offset >= minforkoff) ? minforkoff : 0;
  351. }
  352. /*
  353. * If the requested numbers of bytes is smaller or equal to the
  354. * current attribute fork size we can always proceed.
  355. *
  356. * Note that if_bytes in the data fork might actually be larger than
  357. * the current data fork size is due to delalloc extents. In that
  358. * case either the extent count will go down when they are converted
  359. * to real extents, or the delalloc conversion will take care of the
  360. * literal area rebalancing.
  361. */
  362. if (bytes <= XFS_IFORK_ASIZE(dp))
  363. return dp->i_d.di_forkoff;
  364. /*
  365. * For attr2 we can try to move the forkoff if there is space in the
  366. * literal area, but for the old format we are done if there is no
  367. * space in the fixed attribute fork.
  368. */
  369. if (!(mp->m_flags & XFS_MOUNT_ATTR2))
  370. return 0;
  371. dsize = dp->i_df.if_bytes;
  372. switch (dp->i_d.di_format) {
  373. case XFS_DINODE_FMT_EXTENTS:
  374. /*
  375. * If there is no attr fork and the data fork is extents,
  376. * determine if creating the default attr fork will result
  377. * in the extents form migrating to btree. If so, the
  378. * minimum offset only needs to be the space required for
  379. * the btree root.
  380. */
  381. if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
  382. xfs_default_attroffset(dp))
  383. dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  384. break;
  385. case XFS_DINODE_FMT_BTREE:
  386. /*
  387. * If we have a data btree then keep forkoff if we have one,
  388. * otherwise we are adding a new attr, so then we set
  389. * minforkoff to where the btree root can finish so we have
  390. * plenty of room for attrs
  391. */
  392. if (dp->i_d.di_forkoff) {
  393. if (offset < dp->i_d.di_forkoff)
  394. return 0;
  395. return dp->i_d.di_forkoff;
  396. }
  397. dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
  398. break;
  399. }
  400. /*
  401. * A data fork btree root must have space for at least
  402. * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
  403. */
  404. minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
  405. minforkoff = roundup(minforkoff, 8) >> 3;
  406. /* attr fork btree root can have at least this many key/ptr pairs */
  407. maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
  408. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  409. maxforkoff = maxforkoff >> 3; /* rounded down */
  410. if (offset >= maxforkoff)
  411. return maxforkoff;
  412. if (offset >= minforkoff)
  413. return offset;
  414. return 0;
  415. }
  416. /*
  417. * Switch on the ATTR2 superblock bit (implies also FEATURES2)
  418. */
  419. STATIC void
  420. xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
  421. {
  422. if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
  423. !(xfs_sb_version_hasattr2(&mp->m_sb))) {
  424. spin_lock(&mp->m_sb_lock);
  425. if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
  426. xfs_sb_version_addattr2(&mp->m_sb);
  427. spin_unlock(&mp->m_sb_lock);
  428. xfs_log_sb(tp);
  429. } else
  430. spin_unlock(&mp->m_sb_lock);
  431. }
  432. }
  433. /*
  434. * Create the initial contents of a shortform attribute list.
  435. */
  436. void
  437. xfs_attr_shortform_create(xfs_da_args_t *args)
  438. {
  439. xfs_attr_sf_hdr_t *hdr;
  440. xfs_inode_t *dp;
  441. xfs_ifork_t *ifp;
  442. trace_xfs_attr_sf_create(args);
  443. dp = args->dp;
  444. ASSERT(dp != NULL);
  445. ifp = dp->i_afp;
  446. ASSERT(ifp != NULL);
  447. ASSERT(ifp->if_bytes == 0);
  448. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
  449. ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
  450. dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
  451. ifp->if_flags |= XFS_IFINLINE;
  452. } else {
  453. ASSERT(ifp->if_flags & XFS_IFINLINE);
  454. }
  455. xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
  456. hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
  457. hdr->count = 0;
  458. hdr->totsize = cpu_to_be16(sizeof(*hdr));
  459. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
  460. }
  461. /*
  462. * Add a name/value pair to the shortform attribute list.
  463. * Overflow from the inode has already been checked for.
  464. */
  465. void
  466. xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
  467. {
  468. xfs_attr_shortform_t *sf;
  469. xfs_attr_sf_entry_t *sfe;
  470. int i, offset, size;
  471. xfs_mount_t *mp;
  472. xfs_inode_t *dp;
  473. xfs_ifork_t *ifp;
  474. trace_xfs_attr_sf_add(args);
  475. dp = args->dp;
  476. mp = dp->i_mount;
  477. dp->i_d.di_forkoff = forkoff;
  478. ifp = dp->i_afp;
  479. ASSERT(ifp->if_flags & XFS_IFINLINE);
  480. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  481. sfe = &sf->list[0];
  482. for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
  483. #ifdef DEBUG
  484. if (sfe->namelen != args->namelen)
  485. continue;
  486. if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
  487. continue;
  488. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  489. continue;
  490. ASSERT(0);
  491. #endif
  492. }
  493. offset = (char *)sfe - (char *)sf;
  494. size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  495. xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
  496. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  497. sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
  498. sfe->namelen = args->namelen;
  499. sfe->valuelen = args->valuelen;
  500. sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
  501. memcpy(sfe->nameval, args->name, args->namelen);
  502. memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
  503. sf->hdr.count++;
  504. be16_add_cpu(&sf->hdr.totsize, size);
  505. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
  506. xfs_sbversion_add_attr2(mp, args->trans);
  507. }
  508. /*
  509. * After the last attribute is removed revert to original inode format,
  510. * making all literal area available to the data fork once more.
  511. */
  512. void
  513. xfs_attr_fork_remove(
  514. struct xfs_inode *ip,
  515. struct xfs_trans *tp)
  516. {
  517. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  518. ip->i_d.di_forkoff = 0;
  519. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  520. ASSERT(ip->i_d.di_anextents == 0);
  521. ASSERT(ip->i_afp == NULL);
  522. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  523. }
  524. /*
  525. * Remove an attribute from the shortform attribute list structure.
  526. */
  527. int
  528. xfs_attr_shortform_remove(xfs_da_args_t *args)
  529. {
  530. xfs_attr_shortform_t *sf;
  531. xfs_attr_sf_entry_t *sfe;
  532. int base, size=0, end, totsize, i;
  533. xfs_mount_t *mp;
  534. xfs_inode_t *dp;
  535. trace_xfs_attr_sf_remove(args);
  536. dp = args->dp;
  537. mp = dp->i_mount;
  538. base = sizeof(xfs_attr_sf_hdr_t);
  539. sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
  540. sfe = &sf->list[0];
  541. end = sf->hdr.count;
  542. for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
  543. base += size, i++) {
  544. size = XFS_ATTR_SF_ENTSIZE(sfe);
  545. if (sfe->namelen != args->namelen)
  546. continue;
  547. if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
  548. continue;
  549. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  550. continue;
  551. break;
  552. }
  553. if (i == end)
  554. return -ENOATTR;
  555. /*
  556. * Fix up the attribute fork data, covering the hole
  557. */
  558. end = base + size;
  559. totsize = be16_to_cpu(sf->hdr.totsize);
  560. if (end != totsize)
  561. memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
  562. sf->hdr.count--;
  563. be16_add_cpu(&sf->hdr.totsize, -size);
  564. /*
  565. * Fix up the start offset of the attribute fork
  566. */
  567. totsize -= size;
  568. if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
  569. (mp->m_flags & XFS_MOUNT_ATTR2) &&
  570. (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  571. !(args->op_flags & XFS_DA_OP_ADDNAME)) {
  572. xfs_attr_fork_remove(dp, args->trans);
  573. } else {
  574. xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
  575. dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
  576. ASSERT(dp->i_d.di_forkoff);
  577. ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
  578. (args->op_flags & XFS_DA_OP_ADDNAME) ||
  579. !(mp->m_flags & XFS_MOUNT_ATTR2) ||
  580. dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
  581. xfs_trans_log_inode(args->trans, dp,
  582. XFS_ILOG_CORE | XFS_ILOG_ADATA);
  583. }
  584. xfs_sbversion_add_attr2(mp, args->trans);
  585. return 0;
  586. }
  587. /*
  588. * Look up a name in a shortform attribute list structure.
  589. */
  590. /*ARGSUSED*/
  591. int
  592. xfs_attr_shortform_lookup(xfs_da_args_t *args)
  593. {
  594. xfs_attr_shortform_t *sf;
  595. xfs_attr_sf_entry_t *sfe;
  596. int i;
  597. xfs_ifork_t *ifp;
  598. trace_xfs_attr_sf_lookup(args);
  599. ifp = args->dp->i_afp;
  600. ASSERT(ifp->if_flags & XFS_IFINLINE);
  601. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  602. sfe = &sf->list[0];
  603. for (i = 0; i < sf->hdr.count;
  604. sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
  605. if (sfe->namelen != args->namelen)
  606. continue;
  607. if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
  608. continue;
  609. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  610. continue;
  611. return -EEXIST;
  612. }
  613. return -ENOATTR;
  614. }
  615. /*
  616. * Look up a name in a shortform attribute list structure.
  617. */
  618. /*ARGSUSED*/
  619. int
  620. xfs_attr_shortform_getvalue(xfs_da_args_t *args)
  621. {
  622. xfs_attr_shortform_t *sf;
  623. xfs_attr_sf_entry_t *sfe;
  624. int i;
  625. ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE);
  626. sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
  627. sfe = &sf->list[0];
  628. for (i = 0; i < sf->hdr.count;
  629. sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
  630. if (sfe->namelen != args->namelen)
  631. continue;
  632. if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
  633. continue;
  634. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  635. continue;
  636. if (args->flags & ATTR_KERNOVAL) {
  637. args->valuelen = sfe->valuelen;
  638. return -EEXIST;
  639. }
  640. if (args->valuelen < sfe->valuelen) {
  641. args->valuelen = sfe->valuelen;
  642. return -ERANGE;
  643. }
  644. args->valuelen = sfe->valuelen;
  645. memcpy(args->value, &sfe->nameval[args->namelen],
  646. args->valuelen);
  647. return -EEXIST;
  648. }
  649. return -ENOATTR;
  650. }
  651. /*
  652. * Convert from using the shortform to the leaf.
  653. */
  654. int
  655. xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
  656. {
  657. xfs_inode_t *dp;
  658. xfs_attr_shortform_t *sf;
  659. xfs_attr_sf_entry_t *sfe;
  660. xfs_da_args_t nargs;
  661. char *tmpbuffer;
  662. int error, i, size;
  663. xfs_dablk_t blkno;
  664. struct xfs_buf *bp;
  665. xfs_ifork_t *ifp;
  666. trace_xfs_attr_sf_to_leaf(args);
  667. dp = args->dp;
  668. ifp = dp->i_afp;
  669. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  670. size = be16_to_cpu(sf->hdr.totsize);
  671. tmpbuffer = kmem_alloc(size, KM_SLEEP);
  672. ASSERT(tmpbuffer != NULL);
  673. memcpy(tmpbuffer, ifp->if_u1.if_data, size);
  674. sf = (xfs_attr_shortform_t *)tmpbuffer;
  675. xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
  676. xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK);
  677. bp = NULL;
  678. error = xfs_da_grow_inode(args, &blkno);
  679. if (error) {
  680. /*
  681. * If we hit an IO error middle of the transaction inside
  682. * grow_inode(), we may have inconsistent data. Bail out.
  683. */
  684. if (error == -EIO)
  685. goto out;
  686. xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
  687. memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
  688. goto out;
  689. }
  690. ASSERT(blkno == 0);
  691. error = xfs_attr3_leaf_create(args, blkno, &bp);
  692. if (error) {
  693. error = xfs_da_shrink_inode(args, 0, bp);
  694. bp = NULL;
  695. if (error)
  696. goto out;
  697. xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
  698. memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
  699. goto out;
  700. }
  701. memset((char *)&nargs, 0, sizeof(nargs));
  702. nargs.dp = dp;
  703. nargs.geo = args->geo;
  704. nargs.firstblock = args->firstblock;
  705. nargs.flist = args->flist;
  706. nargs.total = args->total;
  707. nargs.whichfork = XFS_ATTR_FORK;
  708. nargs.trans = args->trans;
  709. nargs.op_flags = XFS_DA_OP_OKNOENT;
  710. sfe = &sf->list[0];
  711. for (i = 0; i < sf->hdr.count; i++) {
  712. nargs.name = sfe->nameval;
  713. nargs.namelen = sfe->namelen;
  714. nargs.value = &sfe->nameval[nargs.namelen];
  715. nargs.valuelen = sfe->valuelen;
  716. nargs.hashval = xfs_da_hashname(sfe->nameval,
  717. sfe->namelen);
  718. nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
  719. error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
  720. ASSERT(error == -ENOATTR);
  721. error = xfs_attr3_leaf_add(bp, &nargs);
  722. ASSERT(error != -ENOSPC);
  723. if (error)
  724. goto out;
  725. sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
  726. }
  727. error = 0;
  728. out:
  729. kmem_free(tmpbuffer);
  730. return error;
  731. }
  732. /*
  733. * Check a leaf attribute block to see if all the entries would fit into
  734. * a shortform attribute list.
  735. */
  736. int
  737. xfs_attr_shortform_allfit(
  738. struct xfs_buf *bp,
  739. struct xfs_inode *dp)
  740. {
  741. struct xfs_attr_leafblock *leaf;
  742. struct xfs_attr_leaf_entry *entry;
  743. xfs_attr_leaf_name_local_t *name_loc;
  744. struct xfs_attr3_icleaf_hdr leafhdr;
  745. int bytes;
  746. int i;
  747. struct xfs_mount *mp = bp->b_target->bt_mount;
  748. leaf = bp->b_addr;
  749. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
  750. entry = xfs_attr3_leaf_entryp(leaf);
  751. bytes = sizeof(struct xfs_attr_sf_hdr);
  752. for (i = 0; i < leafhdr.count; entry++, i++) {
  753. if (entry->flags & XFS_ATTR_INCOMPLETE)
  754. continue; /* don't copy partial entries */
  755. if (!(entry->flags & XFS_ATTR_LOCAL))
  756. return 0;
  757. name_loc = xfs_attr3_leaf_name_local(leaf, i);
  758. if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  759. return 0;
  760. if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
  761. return 0;
  762. bytes += sizeof(struct xfs_attr_sf_entry) - 1
  763. + name_loc->namelen
  764. + be16_to_cpu(name_loc->valuelen);
  765. }
  766. if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
  767. (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  768. (bytes == sizeof(struct xfs_attr_sf_hdr)))
  769. return -1;
  770. return xfs_attr_shortform_bytesfit(dp, bytes);
  771. }
  772. /*
  773. * Convert a leaf attribute list to shortform attribute list
  774. */
  775. int
  776. xfs_attr3_leaf_to_shortform(
  777. struct xfs_buf *bp,
  778. struct xfs_da_args *args,
  779. int forkoff)
  780. {
  781. struct xfs_attr_leafblock *leaf;
  782. struct xfs_attr3_icleaf_hdr ichdr;
  783. struct xfs_attr_leaf_entry *entry;
  784. struct xfs_attr_leaf_name_local *name_loc;
  785. struct xfs_da_args nargs;
  786. struct xfs_inode *dp = args->dp;
  787. char *tmpbuffer;
  788. int error;
  789. int i;
  790. trace_xfs_attr_leaf_to_sf(args);
  791. tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
  792. if (!tmpbuffer)
  793. return -ENOMEM;
  794. memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
  795. leaf = (xfs_attr_leafblock_t *)tmpbuffer;
  796. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
  797. entry = xfs_attr3_leaf_entryp(leaf);
  798. /* XXX (dgc): buffer is about to be marked stale - why zero it? */
  799. memset(bp->b_addr, 0, args->geo->blksize);
  800. /*
  801. * Clean out the prior contents of the attribute list.
  802. */
  803. error = xfs_da_shrink_inode(args, 0, bp);
  804. if (error)
  805. goto out;
  806. if (forkoff == -1) {
  807. ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
  808. ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
  809. xfs_attr_fork_remove(dp, args->trans);
  810. goto out;
  811. }
  812. xfs_attr_shortform_create(args);
  813. /*
  814. * Copy the attributes
  815. */
  816. memset((char *)&nargs, 0, sizeof(nargs));
  817. nargs.geo = args->geo;
  818. nargs.dp = dp;
  819. nargs.firstblock = args->firstblock;
  820. nargs.flist = args->flist;
  821. nargs.total = args->total;
  822. nargs.whichfork = XFS_ATTR_FORK;
  823. nargs.trans = args->trans;
  824. nargs.op_flags = XFS_DA_OP_OKNOENT;
  825. for (i = 0; i < ichdr.count; entry++, i++) {
  826. if (entry->flags & XFS_ATTR_INCOMPLETE)
  827. continue; /* don't copy partial entries */
  828. if (!entry->nameidx)
  829. continue;
  830. ASSERT(entry->flags & XFS_ATTR_LOCAL);
  831. name_loc = xfs_attr3_leaf_name_local(leaf, i);
  832. nargs.name = name_loc->nameval;
  833. nargs.namelen = name_loc->namelen;
  834. nargs.value = &name_loc->nameval[nargs.namelen];
  835. nargs.valuelen = be16_to_cpu(name_loc->valuelen);
  836. nargs.hashval = be32_to_cpu(entry->hashval);
  837. nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
  838. xfs_attr_shortform_add(&nargs, forkoff);
  839. }
  840. error = 0;
  841. out:
  842. kmem_free(tmpbuffer);
  843. return error;
  844. }
  845. /*
  846. * Convert from using a single leaf to a root node and a leaf.
  847. */
  848. int
  849. xfs_attr3_leaf_to_node(
  850. struct xfs_da_args *args)
  851. {
  852. struct xfs_attr_leafblock *leaf;
  853. struct xfs_attr3_icleaf_hdr icleafhdr;
  854. struct xfs_attr_leaf_entry *entries;
  855. struct xfs_da_node_entry *btree;
  856. struct xfs_da3_icnode_hdr icnodehdr;
  857. struct xfs_da_intnode *node;
  858. struct xfs_inode *dp = args->dp;
  859. struct xfs_mount *mp = dp->i_mount;
  860. struct xfs_buf *bp1 = NULL;
  861. struct xfs_buf *bp2 = NULL;
  862. xfs_dablk_t blkno;
  863. int error;
  864. trace_xfs_attr_leaf_to_node(args);
  865. error = xfs_da_grow_inode(args, &blkno);
  866. if (error)
  867. goto out;
  868. error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
  869. if (error)
  870. goto out;
  871. error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
  872. if (error)
  873. goto out;
  874. /* copy leaf to new buffer, update identifiers */
  875. xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
  876. bp2->b_ops = bp1->b_ops;
  877. memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
  878. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  879. struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
  880. hdr3->blkno = cpu_to_be64(bp2->b_bn);
  881. }
  882. xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
  883. /*
  884. * Set up the new root node.
  885. */
  886. error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
  887. if (error)
  888. goto out;
  889. node = bp1->b_addr;
  890. dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
  891. btree = dp->d_ops->node_tree_p(node);
  892. leaf = bp2->b_addr;
  893. xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
  894. entries = xfs_attr3_leaf_entryp(leaf);
  895. /* both on-disk, don't endian-flip twice */
  896. btree[0].hashval = entries[icleafhdr.count - 1].hashval;
  897. btree[0].before = cpu_to_be32(blkno);
  898. icnodehdr.count = 1;
  899. dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
  900. xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
  901. error = 0;
  902. out:
  903. return error;
  904. }
  905. /*========================================================================
  906. * Routines used for growing the Btree.
  907. *========================================================================*/
  908. /*
  909. * Create the initial contents of a leaf attribute list
  910. * or a leaf in a node attribute list.
  911. */
  912. STATIC int
  913. xfs_attr3_leaf_create(
  914. struct xfs_da_args *args,
  915. xfs_dablk_t blkno,
  916. struct xfs_buf **bpp)
  917. {
  918. struct xfs_attr_leafblock *leaf;
  919. struct xfs_attr3_icleaf_hdr ichdr;
  920. struct xfs_inode *dp = args->dp;
  921. struct xfs_mount *mp = dp->i_mount;
  922. struct xfs_buf *bp;
  923. int error;
  924. trace_xfs_attr_leaf_create(args);
  925. error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
  926. XFS_ATTR_FORK);
  927. if (error)
  928. return error;
  929. bp->b_ops = &xfs_attr3_leaf_buf_ops;
  930. xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
  931. leaf = bp->b_addr;
  932. memset(leaf, 0, args->geo->blksize);
  933. memset(&ichdr, 0, sizeof(ichdr));
  934. ichdr.firstused = args->geo->blksize;
  935. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  936. struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
  937. ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
  938. hdr3->blkno = cpu_to_be64(bp->b_bn);
  939. hdr3->owner = cpu_to_be64(dp->i_ino);
  940. uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
  941. ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
  942. } else {
  943. ichdr.magic = XFS_ATTR_LEAF_MAGIC;
  944. ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
  945. }
  946. ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
  947. xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
  948. xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
  949. *bpp = bp;
  950. return 0;
  951. }
  952. /*
  953. * Split the leaf node, rebalance, then add the new entry.
  954. */
  955. int
  956. xfs_attr3_leaf_split(
  957. struct xfs_da_state *state,
  958. struct xfs_da_state_blk *oldblk,
  959. struct xfs_da_state_blk *newblk)
  960. {
  961. xfs_dablk_t blkno;
  962. int error;
  963. trace_xfs_attr_leaf_split(state->args);
  964. /*
  965. * Allocate space for a new leaf node.
  966. */
  967. ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
  968. error = xfs_da_grow_inode(state->args, &blkno);
  969. if (error)
  970. return error;
  971. error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
  972. if (error)
  973. return error;
  974. newblk->blkno = blkno;
  975. newblk->magic = XFS_ATTR_LEAF_MAGIC;
  976. /*
  977. * Rebalance the entries across the two leaves.
  978. * NOTE: rebalance() currently depends on the 2nd block being empty.
  979. */
  980. xfs_attr3_leaf_rebalance(state, oldblk, newblk);
  981. error = xfs_da3_blk_link(state, oldblk, newblk);
  982. if (error)
  983. return error;
  984. /*
  985. * Save info on "old" attribute for "atomic rename" ops, leaf_add()
  986. * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
  987. * "new" attrs info. Will need the "old" info to remove it later.
  988. *
  989. * Insert the "new" entry in the correct block.
  990. */
  991. if (state->inleaf) {
  992. trace_xfs_attr_leaf_add_old(state->args);
  993. error = xfs_attr3_leaf_add(oldblk->bp, state->args);
  994. } else {
  995. trace_xfs_attr_leaf_add_new(state->args);
  996. error = xfs_attr3_leaf_add(newblk->bp, state->args);
  997. }
  998. /*
  999. * Update last hashval in each block since we added the name.
  1000. */
  1001. oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
  1002. newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
  1003. return error;
  1004. }
  1005. /*
  1006. * Add a name to the leaf attribute list structure.
  1007. */
  1008. int
  1009. xfs_attr3_leaf_add(
  1010. struct xfs_buf *bp,
  1011. struct xfs_da_args *args)
  1012. {
  1013. struct xfs_attr_leafblock *leaf;
  1014. struct xfs_attr3_icleaf_hdr ichdr;
  1015. int tablesize;
  1016. int entsize;
  1017. int sum;
  1018. int tmp;
  1019. int i;
  1020. trace_xfs_attr_leaf_add(args);
  1021. leaf = bp->b_addr;
  1022. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
  1023. ASSERT(args->index >= 0 && args->index <= ichdr.count);
  1024. entsize = xfs_attr_leaf_newentsize(args, NULL);
  1025. /*
  1026. * Search through freemap for first-fit on new name length.
  1027. * (may need to figure in size of entry struct too)
  1028. */
  1029. tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
  1030. + xfs_attr3_leaf_hdr_size(leaf);
  1031. for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
  1032. if (tablesize > ichdr.firstused) {
  1033. sum += ichdr.freemap[i].size;
  1034. continue;
  1035. }
  1036. if (!ichdr.freemap[i].size)
  1037. continue; /* no space in this map */
  1038. tmp = entsize;
  1039. if (ichdr.freemap[i].base < ichdr.firstused)
  1040. tmp += sizeof(xfs_attr_leaf_entry_t);
  1041. if (ichdr.freemap[i].size >= tmp) {
  1042. tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
  1043. goto out_log_hdr;
  1044. }
  1045. sum += ichdr.freemap[i].size;
  1046. }
  1047. /*
  1048. * If there are no holes in the address space of the block,
  1049. * and we don't have enough freespace, then compaction will do us
  1050. * no good and we should just give up.
  1051. */
  1052. if (!ichdr.holes && sum < entsize)
  1053. return -ENOSPC;
  1054. /*
  1055. * Compact the entries to coalesce free space.
  1056. * This may change the hdr->count via dropping INCOMPLETE entries.
  1057. */
  1058. xfs_attr3_leaf_compact(args, &ichdr, bp);
  1059. /*
  1060. * After compaction, the block is guaranteed to have only one
  1061. * free region, in freemap[0]. If it is not big enough, give up.
  1062. */
  1063. if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
  1064. tmp = -ENOSPC;
  1065. goto out_log_hdr;
  1066. }
  1067. tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
  1068. out_log_hdr:
  1069. xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
  1070. xfs_trans_log_buf(args->trans, bp,
  1071. XFS_DA_LOGRANGE(leaf, &leaf->hdr,
  1072. xfs_attr3_leaf_hdr_size(leaf)));
  1073. return tmp;
  1074. }
  1075. /*
  1076. * Add a name to a leaf attribute list structure.
  1077. */
  1078. STATIC int
  1079. xfs_attr3_leaf_add_work(
  1080. struct xfs_buf *bp,
  1081. struct xfs_attr3_icleaf_hdr *ichdr,
  1082. struct xfs_da_args *args,
  1083. int mapindex)
  1084. {
  1085. struct xfs_attr_leafblock *leaf;
  1086. struct xfs_attr_leaf_entry *entry;
  1087. struct xfs_attr_leaf_name_local *name_loc;
  1088. struct xfs_attr_leaf_name_remote *name_rmt;
  1089. struct xfs_mount *mp;
  1090. int tmp;
  1091. int i;
  1092. trace_xfs_attr_leaf_add_work(args);
  1093. leaf = bp->b_addr;
  1094. ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
  1095. ASSERT(args->index >= 0 && args->index <= ichdr->count);
  1096. /*
  1097. * Force open some space in the entry array and fill it in.
  1098. */
  1099. entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
  1100. if (args->index < ichdr->count) {
  1101. tmp = ichdr->count - args->index;
  1102. tmp *= sizeof(xfs_attr_leaf_entry_t);
  1103. memmove(entry + 1, entry, tmp);
  1104. xfs_trans_log_buf(args->trans, bp,
  1105. XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
  1106. }
  1107. ichdr->count++;
  1108. /*
  1109. * Allocate space for the new string (at the end of the run).
  1110. */
  1111. mp = args->trans->t_mountp;
  1112. ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
  1113. ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
  1114. ASSERT(ichdr->freemap[mapindex].size >=
  1115. xfs_attr_leaf_newentsize(args, NULL));
  1116. ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
  1117. ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
  1118. ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
  1119. entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
  1120. ichdr->freemap[mapindex].size);
  1121. entry->hashval = cpu_to_be32(args->hashval);
  1122. entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
  1123. entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
  1124. if (args->op_flags & XFS_DA_OP_RENAME) {
  1125. entry->flags |= XFS_ATTR_INCOMPLETE;
  1126. if ((args->blkno2 == args->blkno) &&
  1127. (args->index2 <= args->index)) {
  1128. args->index2++;
  1129. }
  1130. }
  1131. xfs_trans_log_buf(args->trans, bp,
  1132. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  1133. ASSERT((args->index == 0) ||
  1134. (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
  1135. ASSERT((args->index == ichdr->count - 1) ||
  1136. (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
  1137. /*
  1138. * For "remote" attribute values, simply note that we need to
  1139. * allocate space for the "remote" value. We can't actually
  1140. * allocate the extents in this transaction, and we can't decide
  1141. * which blocks they should be as we might allocate more blocks
  1142. * as part of this transaction (a split operation for example).
  1143. */
  1144. if (entry->flags & XFS_ATTR_LOCAL) {
  1145. name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
  1146. name_loc->namelen = args->namelen;
  1147. name_loc->valuelen = cpu_to_be16(args->valuelen);
  1148. memcpy((char *)name_loc->nameval, args->name, args->namelen);
  1149. memcpy((char *)&name_loc->nameval[args->namelen], args->value,
  1150. be16_to_cpu(name_loc->valuelen));
  1151. } else {
  1152. name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
  1153. name_rmt->namelen = args->namelen;
  1154. memcpy((char *)name_rmt->name, args->name, args->namelen);
  1155. entry->flags |= XFS_ATTR_INCOMPLETE;
  1156. /* just in case */
  1157. name_rmt->valuelen = 0;
  1158. name_rmt->valueblk = 0;
  1159. args->rmtblkno = 1;
  1160. args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
  1161. args->rmtvaluelen = args->valuelen;
  1162. }
  1163. xfs_trans_log_buf(args->trans, bp,
  1164. XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
  1165. xfs_attr_leaf_entsize(leaf, args->index)));
  1166. /*
  1167. * Update the control info for this leaf node
  1168. */
  1169. if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
  1170. ichdr->firstused = be16_to_cpu(entry->nameidx);
  1171. ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
  1172. + xfs_attr3_leaf_hdr_size(leaf));
  1173. tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
  1174. + xfs_attr3_leaf_hdr_size(leaf);
  1175. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
  1176. if (ichdr->freemap[i].base == tmp) {
  1177. ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
  1178. ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
  1179. }
  1180. }
  1181. ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
  1182. return 0;
  1183. }
  1184. /*
  1185. * Garbage collect a leaf attribute list block by copying it to a new buffer.
  1186. */
  1187. STATIC void
  1188. xfs_attr3_leaf_compact(
  1189. struct xfs_da_args *args,
  1190. struct xfs_attr3_icleaf_hdr *ichdr_dst,
  1191. struct xfs_buf *bp)
  1192. {
  1193. struct xfs_attr_leafblock *leaf_src;
  1194. struct xfs_attr_leafblock *leaf_dst;
  1195. struct xfs_attr3_icleaf_hdr ichdr_src;
  1196. struct xfs_trans *trans = args->trans;
  1197. char *tmpbuffer;
  1198. trace_xfs_attr_leaf_compact(args);
  1199. tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
  1200. memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
  1201. memset(bp->b_addr, 0, args->geo->blksize);
  1202. leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
  1203. leaf_dst = bp->b_addr;
  1204. /*
  1205. * Copy the on-disk header back into the destination buffer to ensure
  1206. * all the information in the header that is not part of the incore
  1207. * header structure is preserved.
  1208. */
  1209. memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
  1210. /* Initialise the incore headers */
  1211. ichdr_src = *ichdr_dst; /* struct copy */
  1212. ichdr_dst->firstused = args->geo->blksize;
  1213. ichdr_dst->usedbytes = 0;
  1214. ichdr_dst->count = 0;
  1215. ichdr_dst->holes = 0;
  1216. ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
  1217. ichdr_dst->freemap[0].size = ichdr_dst->firstused -
  1218. ichdr_dst->freemap[0].base;
  1219. /* write the header back to initialise the underlying buffer */
  1220. xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
  1221. /*
  1222. * Copy all entry's in the same (sorted) order,
  1223. * but allocate name/value pairs packed and in sequence.
  1224. */
  1225. xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
  1226. leaf_dst, ichdr_dst, 0, ichdr_src.count);
  1227. /*
  1228. * this logs the entire buffer, but the caller must write the header
  1229. * back to the buffer when it is finished modifying it.
  1230. */
  1231. xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
  1232. kmem_free(tmpbuffer);
  1233. }
  1234. /*
  1235. * Compare two leaf blocks "order".
  1236. * Return 0 unless leaf2 should go before leaf1.
  1237. */
  1238. static int
  1239. xfs_attr3_leaf_order(
  1240. struct xfs_buf *leaf1_bp,
  1241. struct xfs_attr3_icleaf_hdr *leaf1hdr,
  1242. struct xfs_buf *leaf2_bp,
  1243. struct xfs_attr3_icleaf_hdr *leaf2hdr)
  1244. {
  1245. struct xfs_attr_leaf_entry *entries1;
  1246. struct xfs_attr_leaf_entry *entries2;
  1247. entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
  1248. entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
  1249. if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
  1250. ((be32_to_cpu(entries2[0].hashval) <
  1251. be32_to_cpu(entries1[0].hashval)) ||
  1252. (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
  1253. be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
  1254. return 1;
  1255. }
  1256. return 0;
  1257. }
  1258. int
  1259. xfs_attr_leaf_order(
  1260. struct xfs_buf *leaf1_bp,
  1261. struct xfs_buf *leaf2_bp)
  1262. {
  1263. struct xfs_attr3_icleaf_hdr ichdr1;
  1264. struct xfs_attr3_icleaf_hdr ichdr2;
  1265. struct xfs_mount *mp = leaf1_bp->b_target->bt_mount;
  1266. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
  1267. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
  1268. return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
  1269. }
  1270. /*
  1271. * Redistribute the attribute list entries between two leaf nodes,
  1272. * taking into account the size of the new entry.
  1273. *
  1274. * NOTE: if new block is empty, then it will get the upper half of the
  1275. * old block. At present, all (one) callers pass in an empty second block.
  1276. *
  1277. * This code adjusts the args->index/blkno and args->index2/blkno2 fields
  1278. * to match what it is doing in splitting the attribute leaf block. Those
  1279. * values are used in "atomic rename" operations on attributes. Note that
  1280. * the "new" and "old" values can end up in different blocks.
  1281. */
  1282. STATIC void
  1283. xfs_attr3_leaf_rebalance(
  1284. struct xfs_da_state *state,
  1285. struct xfs_da_state_blk *blk1,
  1286. struct xfs_da_state_blk *blk2)
  1287. {
  1288. struct xfs_da_args *args;
  1289. struct xfs_attr_leafblock *leaf1;
  1290. struct xfs_attr_leafblock *leaf2;
  1291. struct xfs_attr3_icleaf_hdr ichdr1;
  1292. struct xfs_attr3_icleaf_hdr ichdr2;
  1293. struct xfs_attr_leaf_entry *entries1;
  1294. struct xfs_attr_leaf_entry *entries2;
  1295. int count;
  1296. int totallen;
  1297. int max;
  1298. int space;
  1299. int swap;
  1300. /*
  1301. * Set up environment.
  1302. */
  1303. ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
  1304. ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
  1305. leaf1 = blk1->bp->b_addr;
  1306. leaf2 = blk2->bp->b_addr;
  1307. xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
  1308. xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
  1309. ASSERT(ichdr2.count == 0);
  1310. args = state->args;
  1311. trace_xfs_attr_leaf_rebalance(args);
  1312. /*
  1313. * Check ordering of blocks, reverse if it makes things simpler.
  1314. *
  1315. * NOTE: Given that all (current) callers pass in an empty
  1316. * second block, this code should never set "swap".
  1317. */
  1318. swap = 0;
  1319. if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
  1320. struct xfs_da_state_blk *tmp_blk;
  1321. struct xfs_attr3_icleaf_hdr tmp_ichdr;
  1322. tmp_blk = blk1;
  1323. blk1 = blk2;
  1324. blk2 = tmp_blk;
  1325. /* struct copies to swap them rather than reconverting */
  1326. tmp_ichdr = ichdr1;
  1327. ichdr1 = ichdr2;
  1328. ichdr2 = tmp_ichdr;
  1329. leaf1 = blk1->bp->b_addr;
  1330. leaf2 = blk2->bp->b_addr;
  1331. swap = 1;
  1332. }
  1333. /*
  1334. * Examine entries until we reduce the absolute difference in
  1335. * byte usage between the two blocks to a minimum. Then get
  1336. * the direction to copy and the number of elements to move.
  1337. *
  1338. * "inleaf" is true if the new entry should be inserted into blk1.
  1339. * If "swap" is also true, then reverse the sense of "inleaf".
  1340. */
  1341. state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
  1342. blk2, &ichdr2,
  1343. &count, &totallen);
  1344. if (swap)
  1345. state->inleaf = !state->inleaf;
  1346. /*
  1347. * Move any entries required from leaf to leaf:
  1348. */
  1349. if (count < ichdr1.count) {
  1350. /*
  1351. * Figure the total bytes to be added to the destination leaf.
  1352. */
  1353. /* number entries being moved */
  1354. count = ichdr1.count - count;
  1355. space = ichdr1.usedbytes - totallen;
  1356. space += count * sizeof(xfs_attr_leaf_entry_t);
  1357. /*
  1358. * leaf2 is the destination, compact it if it looks tight.
  1359. */
  1360. max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
  1361. max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
  1362. if (space > max)
  1363. xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
  1364. /*
  1365. * Move high entries from leaf1 to low end of leaf2.
  1366. */
  1367. xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
  1368. ichdr1.count - count, leaf2, &ichdr2, 0, count);
  1369. } else if (count > ichdr1.count) {
  1370. /*
  1371. * I assert that since all callers pass in an empty
  1372. * second buffer, this code should never execute.
  1373. */
  1374. ASSERT(0);
  1375. /*
  1376. * Figure the total bytes to be added to the destination leaf.
  1377. */
  1378. /* number entries being moved */
  1379. count -= ichdr1.count;
  1380. space = totallen - ichdr1.usedbytes;
  1381. space += count * sizeof(xfs_attr_leaf_entry_t);
  1382. /*
  1383. * leaf1 is the destination, compact it if it looks tight.
  1384. */
  1385. max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
  1386. max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
  1387. if (space > max)
  1388. xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
  1389. /*
  1390. * Move low entries from leaf2 to high end of leaf1.
  1391. */
  1392. xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
  1393. ichdr1.count, count);
  1394. }
  1395. xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
  1396. xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
  1397. xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
  1398. xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
  1399. /*
  1400. * Copy out last hashval in each block for B-tree code.
  1401. */
  1402. entries1 = xfs_attr3_leaf_entryp(leaf1);
  1403. entries2 = xfs_attr3_leaf_entryp(leaf2);
  1404. blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
  1405. blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
  1406. /*
  1407. * Adjust the expected index for insertion.
  1408. * NOTE: this code depends on the (current) situation that the
  1409. * second block was originally empty.
  1410. *
  1411. * If the insertion point moved to the 2nd block, we must adjust
  1412. * the index. We must also track the entry just following the
  1413. * new entry for use in an "atomic rename" operation, that entry
  1414. * is always the "old" entry and the "new" entry is what we are
  1415. * inserting. The index/blkno fields refer to the "old" entry,
  1416. * while the index2/blkno2 fields refer to the "new" entry.
  1417. */
  1418. if (blk1->index > ichdr1.count) {
  1419. ASSERT(state->inleaf == 0);
  1420. blk2->index = blk1->index - ichdr1.count;
  1421. args->index = args->index2 = blk2->index;
  1422. args->blkno = args->blkno2 = blk2->blkno;
  1423. } else if (blk1->index == ichdr1.count) {
  1424. if (state->inleaf) {
  1425. args->index = blk1->index;
  1426. args->blkno = blk1->blkno;
  1427. args->index2 = 0;
  1428. args->blkno2 = blk2->blkno;
  1429. } else {
  1430. /*
  1431. * On a double leaf split, the original attr location
  1432. * is already stored in blkno2/index2, so don't
  1433. * overwrite it overwise we corrupt the tree.
  1434. */
  1435. blk2->index = blk1->index - ichdr1.count;
  1436. args->index = blk2->index;
  1437. args->blkno = blk2->blkno;
  1438. if (!state->extravalid) {
  1439. /*
  1440. * set the new attr location to match the old
  1441. * one and let the higher level split code
  1442. * decide where in the leaf to place it.
  1443. */
  1444. args->index2 = blk2->index;
  1445. args->blkno2 = blk2->blkno;
  1446. }
  1447. }
  1448. } else {
  1449. ASSERT(state->inleaf == 1);
  1450. args->index = args->index2 = blk1->index;
  1451. args->blkno = args->blkno2 = blk1->blkno;
  1452. }
  1453. }
  1454. /*
  1455. * Examine entries until we reduce the absolute difference in
  1456. * byte usage between the two blocks to a minimum.
  1457. * GROT: Is this really necessary? With other than a 512 byte blocksize,
  1458. * GROT: there will always be enough room in either block for a new entry.
  1459. * GROT: Do a double-split for this case?
  1460. */
  1461. STATIC int
  1462. xfs_attr3_leaf_figure_balance(
  1463. struct xfs_da_state *state,
  1464. struct xfs_da_state_blk *blk1,
  1465. struct xfs_attr3_icleaf_hdr *ichdr1,
  1466. struct xfs_da_state_blk *blk2,
  1467. struct xfs_attr3_icleaf_hdr *ichdr2,
  1468. int *countarg,
  1469. int *usedbytesarg)
  1470. {
  1471. struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
  1472. struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
  1473. struct xfs_attr_leaf_entry *entry;
  1474. int count;
  1475. int max;
  1476. int index;
  1477. int totallen = 0;
  1478. int half;
  1479. int lastdelta;
  1480. int foundit = 0;
  1481. int tmp;
  1482. /*
  1483. * Examine entries until we reduce the absolute difference in
  1484. * byte usage between the two blocks to a minimum.
  1485. */
  1486. max = ichdr1->count + ichdr2->count;
  1487. half = (max + 1) * sizeof(*entry);
  1488. half += ichdr1->usedbytes + ichdr2->usedbytes +
  1489. xfs_attr_leaf_newentsize(state->args, NULL);
  1490. half /= 2;
  1491. lastdelta = state->args->geo->blksize;
  1492. entry = xfs_attr3_leaf_entryp(leaf1);
  1493. for (count = index = 0; count < max; entry++, index++, count++) {
  1494. #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
  1495. /*
  1496. * The new entry is in the first block, account for it.
  1497. */
  1498. if (count == blk1->index) {
  1499. tmp = totallen + sizeof(*entry) +
  1500. xfs_attr_leaf_newentsize(state->args, NULL);
  1501. if (XFS_ATTR_ABS(half - tmp) > lastdelta)
  1502. break;
  1503. lastdelta = XFS_ATTR_ABS(half - tmp);
  1504. totallen = tmp;
  1505. foundit = 1;
  1506. }
  1507. /*
  1508. * Wrap around into the second block if necessary.
  1509. */
  1510. if (count == ichdr1->count) {
  1511. leaf1 = leaf2;
  1512. entry = xfs_attr3_leaf_entryp(leaf1);
  1513. index = 0;
  1514. }
  1515. /*
  1516. * Figure out if next leaf entry would be too much.
  1517. */
  1518. tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
  1519. index);
  1520. if (XFS_ATTR_ABS(half - tmp) > lastdelta)
  1521. break;
  1522. lastdelta = XFS_ATTR_ABS(half - tmp);
  1523. totallen = tmp;
  1524. #undef XFS_ATTR_ABS
  1525. }
  1526. /*
  1527. * Calculate the number of usedbytes that will end up in lower block.
  1528. * If new entry not in lower block, fix up the count.
  1529. */
  1530. totallen -= count * sizeof(*entry);
  1531. if (foundit) {
  1532. totallen -= sizeof(*entry) +
  1533. xfs_attr_leaf_newentsize(state->args, NULL);
  1534. }
  1535. *countarg = count;
  1536. *usedbytesarg = totallen;
  1537. return foundit;
  1538. }
  1539. /*========================================================================
  1540. * Routines used for shrinking the Btree.
  1541. *========================================================================*/
  1542. /*
  1543. * Check a leaf block and its neighbors to see if the block should be
  1544. * collapsed into one or the other neighbor. Always keep the block
  1545. * with the smaller block number.
  1546. * If the current block is over 50% full, don't try to join it, return 0.
  1547. * If the block is empty, fill in the state structure and return 2.
  1548. * If it can be collapsed, fill in the state structure and return 1.
  1549. * If nothing can be done, return 0.
  1550. *
  1551. * GROT: allow for INCOMPLETE entries in calculation.
  1552. */
  1553. int
  1554. xfs_attr3_leaf_toosmall(
  1555. struct xfs_da_state *state,
  1556. int *action)
  1557. {
  1558. struct xfs_attr_leafblock *leaf;
  1559. struct xfs_da_state_blk *blk;
  1560. struct xfs_attr3_icleaf_hdr ichdr;
  1561. struct xfs_buf *bp;
  1562. xfs_dablk_t blkno;
  1563. int bytes;
  1564. int forward;
  1565. int error;
  1566. int retval;
  1567. int i;
  1568. trace_xfs_attr_leaf_toosmall(state->args);
  1569. /*
  1570. * Check for the degenerate case of the block being over 50% full.
  1571. * If so, it's not worth even looking to see if we might be able
  1572. * to coalesce with a sibling.
  1573. */
  1574. blk = &state->path.blk[ state->path.active-1 ];
  1575. leaf = blk->bp->b_addr;
  1576. xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
  1577. bytes = xfs_attr3_leaf_hdr_size(leaf) +
  1578. ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
  1579. ichdr.usedbytes;
  1580. if (bytes > (state->args->geo->blksize >> 1)) {
  1581. *action = 0; /* blk over 50%, don't try to join */
  1582. return 0;
  1583. }
  1584. /*
  1585. * Check for the degenerate case of the block being empty.
  1586. * If the block is empty, we'll simply delete it, no need to
  1587. * coalesce it with a sibling block. We choose (arbitrarily)
  1588. * to merge with the forward block unless it is NULL.
  1589. */
  1590. if (ichdr.count == 0) {
  1591. /*
  1592. * Make altpath point to the block we want to keep and
  1593. * path point to the block we want to drop (this one).
  1594. */
  1595. forward = (ichdr.forw != 0);
  1596. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1597. error = xfs_da3_path_shift(state, &state->altpath, forward,
  1598. 0, &retval);
  1599. if (error)
  1600. return error;
  1601. if (retval) {
  1602. *action = 0;
  1603. } else {
  1604. *action = 2;
  1605. }
  1606. return 0;
  1607. }
  1608. /*
  1609. * Examine each sibling block to see if we can coalesce with
  1610. * at least 25% free space to spare. We need to figure out
  1611. * whether to merge with the forward or the backward block.
  1612. * We prefer coalescing with the lower numbered sibling so as
  1613. * to shrink an attribute list over time.
  1614. */
  1615. /* start with smaller blk num */
  1616. forward = ichdr.forw < ichdr.back;
  1617. for (i = 0; i < 2; forward = !forward, i++) {
  1618. struct xfs_attr3_icleaf_hdr ichdr2;
  1619. if (forward)
  1620. blkno = ichdr.forw;
  1621. else
  1622. blkno = ichdr.back;
  1623. if (blkno == 0)
  1624. continue;
  1625. error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
  1626. blkno, -1, &bp);
  1627. if (error)
  1628. return error;
  1629. xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
  1630. bytes = state->args->geo->blksize -
  1631. (state->args->geo->blksize >> 2) -
  1632. ichdr.usedbytes - ichdr2.usedbytes -
  1633. ((ichdr.count + ichdr2.count) *
  1634. sizeof(xfs_attr_leaf_entry_t)) -
  1635. xfs_attr3_leaf_hdr_size(leaf);
  1636. xfs_trans_brelse(state->args->trans, bp);
  1637. if (bytes >= 0)
  1638. break; /* fits with at least 25% to spare */
  1639. }
  1640. if (i >= 2) {
  1641. *action = 0;
  1642. return 0;
  1643. }
  1644. /*
  1645. * Make altpath point to the block we want to keep (the lower
  1646. * numbered block) and path point to the block we want to drop.
  1647. */
  1648. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1649. if (blkno < blk->blkno) {
  1650. error = xfs_da3_path_shift(state, &state->altpath, forward,
  1651. 0, &retval);
  1652. } else {
  1653. error = xfs_da3_path_shift(state, &state->path, forward,
  1654. 0, &retval);
  1655. }
  1656. if (error)
  1657. return error;
  1658. if (retval) {
  1659. *action = 0;
  1660. } else {
  1661. *action = 1;
  1662. }
  1663. return 0;
  1664. }
  1665. /*
  1666. * Remove a name from the leaf attribute list structure.
  1667. *
  1668. * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
  1669. * If two leaves are 37% full, when combined they will leave 25% free.
  1670. */
  1671. int
  1672. xfs_attr3_leaf_remove(
  1673. struct xfs_buf *bp,
  1674. struct xfs_da_args *args)
  1675. {
  1676. struct xfs_attr_leafblock *leaf;
  1677. struct xfs_attr3_icleaf_hdr ichdr;
  1678. struct xfs_attr_leaf_entry *entry;
  1679. int before;
  1680. int after;
  1681. int smallest;
  1682. int entsize;
  1683. int tablesize;
  1684. int tmp;
  1685. int i;
  1686. trace_xfs_attr_leaf_remove(args);
  1687. leaf = bp->b_addr;
  1688. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
  1689. ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
  1690. ASSERT(args->index >= 0 && args->index < ichdr.count);
  1691. ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
  1692. xfs_attr3_leaf_hdr_size(leaf));
  1693. entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
  1694. ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
  1695. ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
  1696. /*
  1697. * Scan through free region table:
  1698. * check for adjacency of free'd entry with an existing one,
  1699. * find smallest free region in case we need to replace it,
  1700. * adjust any map that borders the entry table,
  1701. */
  1702. tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
  1703. + xfs_attr3_leaf_hdr_size(leaf);
  1704. tmp = ichdr.freemap[0].size;
  1705. before = after = -1;
  1706. smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
  1707. entsize = xfs_attr_leaf_entsize(leaf, args->index);
  1708. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
  1709. ASSERT(ichdr.freemap[i].base < args->geo->blksize);
  1710. ASSERT(ichdr.freemap[i].size < args->geo->blksize);
  1711. if (ichdr.freemap[i].base == tablesize) {
  1712. ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
  1713. ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
  1714. }
  1715. if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
  1716. be16_to_cpu(entry->nameidx)) {
  1717. before = i;
  1718. } else if (ichdr.freemap[i].base ==
  1719. (be16_to_cpu(entry->nameidx) + entsize)) {
  1720. after = i;
  1721. } else if (ichdr.freemap[i].size < tmp) {
  1722. tmp = ichdr.freemap[i].size;
  1723. smallest = i;
  1724. }
  1725. }
  1726. /*
  1727. * Coalesce adjacent freemap regions,
  1728. * or replace the smallest region.
  1729. */
  1730. if ((before >= 0) || (after >= 0)) {
  1731. if ((before >= 0) && (after >= 0)) {
  1732. ichdr.freemap[before].size += entsize;
  1733. ichdr.freemap[before].size += ichdr.freemap[after].size;
  1734. ichdr.freemap[after].base = 0;
  1735. ichdr.freemap[after].size = 0;
  1736. } else if (before >= 0) {
  1737. ichdr.freemap[before].size += entsize;
  1738. } else {
  1739. ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
  1740. ichdr.freemap[after].size += entsize;
  1741. }
  1742. } else {
  1743. /*
  1744. * Replace smallest region (if it is smaller than free'd entry)
  1745. */
  1746. if (ichdr.freemap[smallest].size < entsize) {
  1747. ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
  1748. ichdr.freemap[smallest].size = entsize;
  1749. }
  1750. }
  1751. /*
  1752. * Did we remove the first entry?
  1753. */
  1754. if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
  1755. smallest = 1;
  1756. else
  1757. smallest = 0;
  1758. /*
  1759. * Compress the remaining entries and zero out the removed stuff.
  1760. */
  1761. memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
  1762. ichdr.usedbytes -= entsize;
  1763. xfs_trans_log_buf(args->trans, bp,
  1764. XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
  1765. entsize));
  1766. tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
  1767. memmove(entry, entry + 1, tmp);
  1768. ichdr.count--;
  1769. xfs_trans_log_buf(args->trans, bp,
  1770. XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
  1771. entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
  1772. memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
  1773. /*
  1774. * If we removed the first entry, re-find the first used byte
  1775. * in the name area. Note that if the entry was the "firstused",
  1776. * then we don't have a "hole" in our block resulting from
  1777. * removing the name.
  1778. */
  1779. if (smallest) {
  1780. tmp = args->geo->blksize;
  1781. entry = xfs_attr3_leaf_entryp(leaf);
  1782. for (i = ichdr.count - 1; i >= 0; entry++, i--) {
  1783. ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
  1784. ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
  1785. if (be16_to_cpu(entry->nameidx) < tmp)
  1786. tmp = be16_to_cpu(entry->nameidx);
  1787. }
  1788. ichdr.firstused = tmp;
  1789. ASSERT(ichdr.firstused != 0);
  1790. } else {
  1791. ichdr.holes = 1; /* mark as needing compaction */
  1792. }
  1793. xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
  1794. xfs_trans_log_buf(args->trans, bp,
  1795. XFS_DA_LOGRANGE(leaf, &leaf->hdr,
  1796. xfs_attr3_leaf_hdr_size(leaf)));
  1797. /*
  1798. * Check if leaf is less than 50% full, caller may want to
  1799. * "join" the leaf with a sibling if so.
  1800. */
  1801. tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
  1802. ichdr.count * sizeof(xfs_attr_leaf_entry_t);
  1803. return tmp < args->geo->magicpct; /* leaf is < 37% full */
  1804. }
  1805. /*
  1806. * Move all the attribute list entries from drop_leaf into save_leaf.
  1807. */
  1808. void
  1809. xfs_attr3_leaf_unbalance(
  1810. struct xfs_da_state *state,
  1811. struct xfs_da_state_blk *drop_blk,
  1812. struct xfs_da_state_blk *save_blk)
  1813. {
  1814. struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
  1815. struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
  1816. struct xfs_attr3_icleaf_hdr drophdr;
  1817. struct xfs_attr3_icleaf_hdr savehdr;
  1818. struct xfs_attr_leaf_entry *entry;
  1819. trace_xfs_attr_leaf_unbalance(state->args);
  1820. drop_leaf = drop_blk->bp->b_addr;
  1821. save_leaf = save_blk->bp->b_addr;
  1822. xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
  1823. xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
  1824. entry = xfs_attr3_leaf_entryp(drop_leaf);
  1825. /*
  1826. * Save last hashval from dying block for later Btree fixup.
  1827. */
  1828. drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
  1829. /*
  1830. * Check if we need a temp buffer, or can we do it in place.
  1831. * Note that we don't check "leaf" for holes because we will
  1832. * always be dropping it, toosmall() decided that for us already.
  1833. */
  1834. if (savehdr.holes == 0) {
  1835. /*
  1836. * dest leaf has no holes, so we add there. May need
  1837. * to make some room in the entry array.
  1838. */
  1839. if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
  1840. drop_blk->bp, &drophdr)) {
  1841. xfs_attr3_leaf_moveents(state->args,
  1842. drop_leaf, &drophdr, 0,
  1843. save_leaf, &savehdr, 0,
  1844. drophdr.count);
  1845. } else {
  1846. xfs_attr3_leaf_moveents(state->args,
  1847. drop_leaf, &drophdr, 0,
  1848. save_leaf, &savehdr,
  1849. savehdr.count, drophdr.count);
  1850. }
  1851. } else {
  1852. /*
  1853. * Destination has holes, so we make a temporary copy
  1854. * of the leaf and add them both to that.
  1855. */
  1856. struct xfs_attr_leafblock *tmp_leaf;
  1857. struct xfs_attr3_icleaf_hdr tmphdr;
  1858. tmp_leaf = kmem_zalloc(state->args->geo->blksize, KM_SLEEP);
  1859. /*
  1860. * Copy the header into the temp leaf so that all the stuff
  1861. * not in the incore header is present and gets copied back in
  1862. * once we've moved all the entries.
  1863. */
  1864. memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
  1865. memset(&tmphdr, 0, sizeof(tmphdr));
  1866. tmphdr.magic = savehdr.magic;
  1867. tmphdr.forw = savehdr.forw;
  1868. tmphdr.back = savehdr.back;
  1869. tmphdr.firstused = state->args->geo->blksize;
  1870. /* write the header to the temp buffer to initialise it */
  1871. xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
  1872. if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
  1873. drop_blk->bp, &drophdr)) {
  1874. xfs_attr3_leaf_moveents(state->args,
  1875. drop_leaf, &drophdr, 0,
  1876. tmp_leaf, &tmphdr, 0,
  1877. drophdr.count);
  1878. xfs_attr3_leaf_moveents(state->args,
  1879. save_leaf, &savehdr, 0,
  1880. tmp_leaf, &tmphdr, tmphdr.count,
  1881. savehdr.count);
  1882. } else {
  1883. xfs_attr3_leaf_moveents(state->args,
  1884. save_leaf, &savehdr, 0,
  1885. tmp_leaf, &tmphdr, 0,
  1886. savehdr.count);
  1887. xfs_attr3_leaf_moveents(state->args,
  1888. drop_leaf, &drophdr, 0,
  1889. tmp_leaf, &tmphdr, tmphdr.count,
  1890. drophdr.count);
  1891. }
  1892. memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
  1893. savehdr = tmphdr; /* struct copy */
  1894. kmem_free(tmp_leaf);
  1895. }
  1896. xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
  1897. xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
  1898. state->args->geo->blksize - 1);
  1899. /*
  1900. * Copy out last hashval in each block for B-tree code.
  1901. */
  1902. entry = xfs_attr3_leaf_entryp(save_leaf);
  1903. save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
  1904. }
  1905. /*========================================================================
  1906. * Routines used for finding things in the Btree.
  1907. *========================================================================*/
  1908. /*
  1909. * Look up a name in a leaf attribute list structure.
  1910. * This is the internal routine, it uses the caller's buffer.
  1911. *
  1912. * Note that duplicate keys are allowed, but only check within the
  1913. * current leaf node. The Btree code must check in adjacent leaf nodes.
  1914. *
  1915. * Return in args->index the index into the entry[] array of either
  1916. * the found entry, or where the entry should have been (insert before
  1917. * that entry).
  1918. *
  1919. * Don't change the args->value unless we find the attribute.
  1920. */
  1921. int
  1922. xfs_attr3_leaf_lookup_int(
  1923. struct xfs_buf *bp,
  1924. struct xfs_da_args *args)
  1925. {
  1926. struct xfs_attr_leafblock *leaf;
  1927. struct xfs_attr3_icleaf_hdr ichdr;
  1928. struct xfs_attr_leaf_entry *entry;
  1929. struct xfs_attr_leaf_entry *entries;
  1930. struct xfs_attr_leaf_name_local *name_loc;
  1931. struct xfs_attr_leaf_name_remote *name_rmt;
  1932. xfs_dahash_t hashval;
  1933. int probe;
  1934. int span;
  1935. trace_xfs_attr_leaf_lookup(args);
  1936. leaf = bp->b_addr;
  1937. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
  1938. entries = xfs_attr3_leaf_entryp(leaf);
  1939. ASSERT(ichdr.count < args->geo->blksize / 8);
  1940. /*
  1941. * Binary search. (note: small blocks will skip this loop)
  1942. */
  1943. hashval = args->hashval;
  1944. probe = span = ichdr.count / 2;
  1945. for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
  1946. span /= 2;
  1947. if (be32_to_cpu(entry->hashval) < hashval)
  1948. probe += span;
  1949. else if (be32_to_cpu(entry->hashval) > hashval)
  1950. probe -= span;
  1951. else
  1952. break;
  1953. }
  1954. ASSERT(probe >= 0 && (!ichdr.count || probe < ichdr.count));
  1955. ASSERT(span <= 4 || be32_to_cpu(entry->hashval) == hashval);
  1956. /*
  1957. * Since we may have duplicate hashval's, find the first matching
  1958. * hashval in the leaf.
  1959. */
  1960. while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
  1961. entry--;
  1962. probe--;
  1963. }
  1964. while (probe < ichdr.count &&
  1965. be32_to_cpu(entry->hashval) < hashval) {
  1966. entry++;
  1967. probe++;
  1968. }
  1969. if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
  1970. args->index = probe;
  1971. return -ENOATTR;
  1972. }
  1973. /*
  1974. * Duplicate keys may be present, so search all of them for a match.
  1975. */
  1976. for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
  1977. entry++, probe++) {
  1978. /*
  1979. * GROT: Add code to remove incomplete entries.
  1980. */
  1981. /*
  1982. * If we are looking for INCOMPLETE entries, show only those.
  1983. * If we are looking for complete entries, show only those.
  1984. */
  1985. if ((args->flags & XFS_ATTR_INCOMPLETE) !=
  1986. (entry->flags & XFS_ATTR_INCOMPLETE)) {
  1987. continue;
  1988. }
  1989. if (entry->flags & XFS_ATTR_LOCAL) {
  1990. name_loc = xfs_attr3_leaf_name_local(leaf, probe);
  1991. if (name_loc->namelen != args->namelen)
  1992. continue;
  1993. if (memcmp(args->name, name_loc->nameval,
  1994. args->namelen) != 0)
  1995. continue;
  1996. if (!xfs_attr_namesp_match(args->flags, entry->flags))
  1997. continue;
  1998. args->index = probe;
  1999. return -EEXIST;
  2000. } else {
  2001. name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
  2002. if (name_rmt->namelen != args->namelen)
  2003. continue;
  2004. if (memcmp(args->name, name_rmt->name,
  2005. args->namelen) != 0)
  2006. continue;
  2007. if (!xfs_attr_namesp_match(args->flags, entry->flags))
  2008. continue;
  2009. args->index = probe;
  2010. args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
  2011. args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
  2012. args->rmtblkcnt = xfs_attr3_rmt_blocks(
  2013. args->dp->i_mount,
  2014. args->rmtvaluelen);
  2015. return -EEXIST;
  2016. }
  2017. }
  2018. args->index = probe;
  2019. return -ENOATTR;
  2020. }
  2021. /*
  2022. * Get the value associated with an attribute name from a leaf attribute
  2023. * list structure.
  2024. */
  2025. int
  2026. xfs_attr3_leaf_getvalue(
  2027. struct xfs_buf *bp,
  2028. struct xfs_da_args *args)
  2029. {
  2030. struct xfs_attr_leafblock *leaf;
  2031. struct xfs_attr3_icleaf_hdr ichdr;
  2032. struct xfs_attr_leaf_entry *entry;
  2033. struct xfs_attr_leaf_name_local *name_loc;
  2034. struct xfs_attr_leaf_name_remote *name_rmt;
  2035. int valuelen;
  2036. leaf = bp->b_addr;
  2037. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
  2038. ASSERT(ichdr.count < args->geo->blksize / 8);
  2039. ASSERT(args->index < ichdr.count);
  2040. entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
  2041. if (entry->flags & XFS_ATTR_LOCAL) {
  2042. name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
  2043. ASSERT(name_loc->namelen == args->namelen);
  2044. ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
  2045. valuelen = be16_to_cpu(name_loc->valuelen);
  2046. if (args->flags & ATTR_KERNOVAL) {
  2047. args->valuelen = valuelen;
  2048. return 0;
  2049. }
  2050. if (args->valuelen < valuelen) {
  2051. args->valuelen = valuelen;
  2052. return -ERANGE;
  2053. }
  2054. args->valuelen = valuelen;
  2055. memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
  2056. } else {
  2057. name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
  2058. ASSERT(name_rmt->namelen == args->namelen);
  2059. ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
  2060. args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
  2061. args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
  2062. args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
  2063. args->rmtvaluelen);
  2064. if (args->flags & ATTR_KERNOVAL) {
  2065. args->valuelen = args->rmtvaluelen;
  2066. return 0;
  2067. }
  2068. if (args->valuelen < args->rmtvaluelen) {
  2069. args->valuelen = args->rmtvaluelen;
  2070. return -ERANGE;
  2071. }
  2072. args->valuelen = args->rmtvaluelen;
  2073. }
  2074. return 0;
  2075. }
  2076. /*========================================================================
  2077. * Utility routines.
  2078. *========================================================================*/
  2079. /*
  2080. * Move the indicated entries from one leaf to another.
  2081. * NOTE: this routine modifies both source and destination leaves.
  2082. */
  2083. /*ARGSUSED*/
  2084. STATIC void
  2085. xfs_attr3_leaf_moveents(
  2086. struct xfs_da_args *args,
  2087. struct xfs_attr_leafblock *leaf_s,
  2088. struct xfs_attr3_icleaf_hdr *ichdr_s,
  2089. int start_s,
  2090. struct xfs_attr_leafblock *leaf_d,
  2091. struct xfs_attr3_icleaf_hdr *ichdr_d,
  2092. int start_d,
  2093. int count)
  2094. {
  2095. struct xfs_attr_leaf_entry *entry_s;
  2096. struct xfs_attr_leaf_entry *entry_d;
  2097. int desti;
  2098. int tmp;
  2099. int i;
  2100. /*
  2101. * Check for nothing to do.
  2102. */
  2103. if (count == 0)
  2104. return;
  2105. /*
  2106. * Set up environment.
  2107. */
  2108. ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
  2109. ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
  2110. ASSERT(ichdr_s->magic == ichdr_d->magic);
  2111. ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
  2112. ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
  2113. + xfs_attr3_leaf_hdr_size(leaf_s));
  2114. ASSERT(ichdr_d->count < args->geo->blksize / 8);
  2115. ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
  2116. + xfs_attr3_leaf_hdr_size(leaf_d));
  2117. ASSERT(start_s < ichdr_s->count);
  2118. ASSERT(start_d <= ichdr_d->count);
  2119. ASSERT(count <= ichdr_s->count);
  2120. /*
  2121. * Move the entries in the destination leaf up to make a hole?
  2122. */
  2123. if (start_d < ichdr_d->count) {
  2124. tmp = ichdr_d->count - start_d;
  2125. tmp *= sizeof(xfs_attr_leaf_entry_t);
  2126. entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
  2127. entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
  2128. memmove(entry_d, entry_s, tmp);
  2129. }
  2130. /*
  2131. * Copy all entry's in the same (sorted) order,
  2132. * but allocate attribute info packed and in sequence.
  2133. */
  2134. entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
  2135. entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
  2136. desti = start_d;
  2137. for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
  2138. ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
  2139. tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
  2140. #ifdef GROT
  2141. /*
  2142. * Code to drop INCOMPLETE entries. Difficult to use as we
  2143. * may also need to change the insertion index. Code turned
  2144. * off for 6.2, should be revisited later.
  2145. */
  2146. if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
  2147. memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
  2148. ichdr_s->usedbytes -= tmp;
  2149. ichdr_s->count -= 1;
  2150. entry_d--; /* to compensate for ++ in loop hdr */
  2151. desti--;
  2152. if ((start_s + i) < offset)
  2153. result++; /* insertion index adjustment */
  2154. } else {
  2155. #endif /* GROT */
  2156. ichdr_d->firstused -= tmp;
  2157. /* both on-disk, don't endian flip twice */
  2158. entry_d->hashval = entry_s->hashval;
  2159. entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
  2160. entry_d->flags = entry_s->flags;
  2161. ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
  2162. <= args->geo->blksize);
  2163. memmove(xfs_attr3_leaf_name(leaf_d, desti),
  2164. xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
  2165. ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
  2166. <= args->geo->blksize);
  2167. memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
  2168. ichdr_s->usedbytes -= tmp;
  2169. ichdr_d->usedbytes += tmp;
  2170. ichdr_s->count -= 1;
  2171. ichdr_d->count += 1;
  2172. tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
  2173. + xfs_attr3_leaf_hdr_size(leaf_d);
  2174. ASSERT(ichdr_d->firstused >= tmp);
  2175. #ifdef GROT
  2176. }
  2177. #endif /* GROT */
  2178. }
  2179. /*
  2180. * Zero out the entries we just copied.
  2181. */
  2182. if (start_s == ichdr_s->count) {
  2183. tmp = count * sizeof(xfs_attr_leaf_entry_t);
  2184. entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
  2185. ASSERT(((char *)entry_s + tmp) <=
  2186. ((char *)leaf_s + args->geo->blksize));
  2187. memset(entry_s, 0, tmp);
  2188. } else {
  2189. /*
  2190. * Move the remaining entries down to fill the hole,
  2191. * then zero the entries at the top.
  2192. */
  2193. tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
  2194. entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
  2195. entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
  2196. memmove(entry_d, entry_s, tmp);
  2197. tmp = count * sizeof(xfs_attr_leaf_entry_t);
  2198. entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
  2199. ASSERT(((char *)entry_s + tmp) <=
  2200. ((char *)leaf_s + args->geo->blksize));
  2201. memset(entry_s, 0, tmp);
  2202. }
  2203. /*
  2204. * Fill in the freemap information
  2205. */
  2206. ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
  2207. ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
  2208. ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
  2209. ichdr_d->freemap[1].base = 0;
  2210. ichdr_d->freemap[2].base = 0;
  2211. ichdr_d->freemap[1].size = 0;
  2212. ichdr_d->freemap[2].size = 0;
  2213. ichdr_s->holes = 1; /* leaf may not be compact */
  2214. }
  2215. /*
  2216. * Pick up the last hashvalue from a leaf block.
  2217. */
  2218. xfs_dahash_t
  2219. xfs_attr_leaf_lasthash(
  2220. struct xfs_buf *bp,
  2221. int *count)
  2222. {
  2223. struct xfs_attr3_icleaf_hdr ichdr;
  2224. struct xfs_attr_leaf_entry *entries;
  2225. struct xfs_mount *mp = bp->b_target->bt_mount;
  2226. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
  2227. entries = xfs_attr3_leaf_entryp(bp->b_addr);
  2228. if (count)
  2229. *count = ichdr.count;
  2230. if (!ichdr.count)
  2231. return 0;
  2232. return be32_to_cpu(entries[ichdr.count - 1].hashval);
  2233. }
  2234. /*
  2235. * Calculate the number of bytes used to store the indicated attribute
  2236. * (whether local or remote only calculate bytes in this block).
  2237. */
  2238. STATIC int
  2239. xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
  2240. {
  2241. struct xfs_attr_leaf_entry *entries;
  2242. xfs_attr_leaf_name_local_t *name_loc;
  2243. xfs_attr_leaf_name_remote_t *name_rmt;
  2244. int size;
  2245. entries = xfs_attr3_leaf_entryp(leaf);
  2246. if (entries[index].flags & XFS_ATTR_LOCAL) {
  2247. name_loc = xfs_attr3_leaf_name_local(leaf, index);
  2248. size = xfs_attr_leaf_entsize_local(name_loc->namelen,
  2249. be16_to_cpu(name_loc->valuelen));
  2250. } else {
  2251. name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
  2252. size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
  2253. }
  2254. return size;
  2255. }
  2256. /*
  2257. * Calculate the number of bytes that would be required to store the new
  2258. * attribute (whether local or remote only calculate bytes in this block).
  2259. * This routine decides as a side effect whether the attribute will be
  2260. * a "local" or a "remote" attribute.
  2261. */
  2262. int
  2263. xfs_attr_leaf_newentsize(
  2264. struct xfs_da_args *args,
  2265. int *local)
  2266. {
  2267. int size;
  2268. size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
  2269. if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
  2270. if (local)
  2271. *local = 1;
  2272. return size;
  2273. }
  2274. if (local)
  2275. *local = 0;
  2276. return xfs_attr_leaf_entsize_remote(args->namelen);
  2277. }
  2278. /*========================================================================
  2279. * Manage the INCOMPLETE flag in a leaf entry
  2280. *========================================================================*/
  2281. /*
  2282. * Clear the INCOMPLETE flag on an entry in a leaf block.
  2283. */
  2284. int
  2285. xfs_attr3_leaf_clearflag(
  2286. struct xfs_da_args *args)
  2287. {
  2288. struct xfs_attr_leafblock *leaf;
  2289. struct xfs_attr_leaf_entry *entry;
  2290. struct xfs_attr_leaf_name_remote *name_rmt;
  2291. struct xfs_buf *bp;
  2292. int error;
  2293. #ifdef DEBUG
  2294. struct xfs_attr3_icleaf_hdr ichdr;
  2295. xfs_attr_leaf_name_local_t *name_loc;
  2296. int namelen;
  2297. char *name;
  2298. #endif /* DEBUG */
  2299. trace_xfs_attr_leaf_clearflag(args);
  2300. /*
  2301. * Set up the operation.
  2302. */
  2303. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  2304. if (error)
  2305. return error;
  2306. leaf = bp->b_addr;
  2307. entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
  2308. ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
  2309. #ifdef DEBUG
  2310. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
  2311. ASSERT(args->index < ichdr.count);
  2312. ASSERT(args->index >= 0);
  2313. if (entry->flags & XFS_ATTR_LOCAL) {
  2314. name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
  2315. namelen = name_loc->namelen;
  2316. name = (char *)name_loc->nameval;
  2317. } else {
  2318. name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
  2319. namelen = name_rmt->namelen;
  2320. name = (char *)name_rmt->name;
  2321. }
  2322. ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
  2323. ASSERT(namelen == args->namelen);
  2324. ASSERT(memcmp(name, args->name, namelen) == 0);
  2325. #endif /* DEBUG */
  2326. entry->flags &= ~XFS_ATTR_INCOMPLETE;
  2327. xfs_trans_log_buf(args->trans, bp,
  2328. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  2329. if (args->rmtblkno) {
  2330. ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
  2331. name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
  2332. name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
  2333. name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
  2334. xfs_trans_log_buf(args->trans, bp,
  2335. XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
  2336. }
  2337. /*
  2338. * Commit the flag value change and start the next trans in series.
  2339. */
  2340. return xfs_trans_roll(&args->trans, args->dp);
  2341. }
  2342. /*
  2343. * Set the INCOMPLETE flag on an entry in a leaf block.
  2344. */
  2345. int
  2346. xfs_attr3_leaf_setflag(
  2347. struct xfs_da_args *args)
  2348. {
  2349. struct xfs_attr_leafblock *leaf;
  2350. struct xfs_attr_leaf_entry *entry;
  2351. struct xfs_attr_leaf_name_remote *name_rmt;
  2352. struct xfs_buf *bp;
  2353. int error;
  2354. #ifdef DEBUG
  2355. struct xfs_attr3_icleaf_hdr ichdr;
  2356. #endif
  2357. trace_xfs_attr_leaf_setflag(args);
  2358. /*
  2359. * Set up the operation.
  2360. */
  2361. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  2362. if (error)
  2363. return error;
  2364. leaf = bp->b_addr;
  2365. #ifdef DEBUG
  2366. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
  2367. ASSERT(args->index < ichdr.count);
  2368. ASSERT(args->index >= 0);
  2369. #endif
  2370. entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
  2371. ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
  2372. entry->flags |= XFS_ATTR_INCOMPLETE;
  2373. xfs_trans_log_buf(args->trans, bp,
  2374. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  2375. if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
  2376. name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
  2377. name_rmt->valueblk = 0;
  2378. name_rmt->valuelen = 0;
  2379. xfs_trans_log_buf(args->trans, bp,
  2380. XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
  2381. }
  2382. /*
  2383. * Commit the flag value change and start the next trans in series.
  2384. */
  2385. return xfs_trans_roll(&args->trans, args->dp);
  2386. }
  2387. /*
  2388. * In a single transaction, clear the INCOMPLETE flag on the leaf entry
  2389. * given by args->blkno/index and set the INCOMPLETE flag on the leaf
  2390. * entry given by args->blkno2/index2.
  2391. *
  2392. * Note that they could be in different blocks, or in the same block.
  2393. */
  2394. int
  2395. xfs_attr3_leaf_flipflags(
  2396. struct xfs_da_args *args)
  2397. {
  2398. struct xfs_attr_leafblock *leaf1;
  2399. struct xfs_attr_leafblock *leaf2;
  2400. struct xfs_attr_leaf_entry *entry1;
  2401. struct xfs_attr_leaf_entry *entry2;
  2402. struct xfs_attr_leaf_name_remote *name_rmt;
  2403. struct xfs_buf *bp1;
  2404. struct xfs_buf *bp2;
  2405. int error;
  2406. #ifdef DEBUG
  2407. struct xfs_attr3_icleaf_hdr ichdr1;
  2408. struct xfs_attr3_icleaf_hdr ichdr2;
  2409. xfs_attr_leaf_name_local_t *name_loc;
  2410. int namelen1, namelen2;
  2411. char *name1, *name2;
  2412. #endif /* DEBUG */
  2413. trace_xfs_attr_leaf_flipflags(args);
  2414. /*
  2415. * Read the block containing the "old" attr
  2416. */
  2417. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
  2418. if (error)
  2419. return error;
  2420. /*
  2421. * Read the block containing the "new" attr, if it is different
  2422. */
  2423. if (args->blkno2 != args->blkno) {
  2424. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
  2425. -1, &bp2);
  2426. if (error)
  2427. return error;
  2428. } else {
  2429. bp2 = bp1;
  2430. }
  2431. leaf1 = bp1->b_addr;
  2432. entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
  2433. leaf2 = bp2->b_addr;
  2434. entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
  2435. #ifdef DEBUG
  2436. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
  2437. ASSERT(args->index < ichdr1.count);
  2438. ASSERT(args->index >= 0);
  2439. xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
  2440. ASSERT(args->index2 < ichdr2.count);
  2441. ASSERT(args->index2 >= 0);
  2442. if (entry1->flags & XFS_ATTR_LOCAL) {
  2443. name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
  2444. namelen1 = name_loc->namelen;
  2445. name1 = (char *)name_loc->nameval;
  2446. } else {
  2447. name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
  2448. namelen1 = name_rmt->namelen;
  2449. name1 = (char *)name_rmt->name;
  2450. }
  2451. if (entry2->flags & XFS_ATTR_LOCAL) {
  2452. name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
  2453. namelen2 = name_loc->namelen;
  2454. name2 = (char *)name_loc->nameval;
  2455. } else {
  2456. name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
  2457. namelen2 = name_rmt->namelen;
  2458. name2 = (char *)name_rmt->name;
  2459. }
  2460. ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
  2461. ASSERT(namelen1 == namelen2);
  2462. ASSERT(memcmp(name1, name2, namelen1) == 0);
  2463. #endif /* DEBUG */
  2464. ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
  2465. ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
  2466. entry1->flags &= ~XFS_ATTR_INCOMPLETE;
  2467. xfs_trans_log_buf(args->trans, bp1,
  2468. XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
  2469. if (args->rmtblkno) {
  2470. ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
  2471. name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
  2472. name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
  2473. name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
  2474. xfs_trans_log_buf(args->trans, bp1,
  2475. XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
  2476. }
  2477. entry2->flags |= XFS_ATTR_INCOMPLETE;
  2478. xfs_trans_log_buf(args->trans, bp2,
  2479. XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
  2480. if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
  2481. name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
  2482. name_rmt->valueblk = 0;
  2483. name_rmt->valuelen = 0;
  2484. xfs_trans_log_buf(args->trans, bp2,
  2485. XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
  2486. }
  2487. /*
  2488. * Commit the flag value change and start the next trans in series.
  2489. */
  2490. error = xfs_trans_roll(&args->trans, args->dp);
  2491. return error;
  2492. }