xfs_attr_leaf.c 79 KB

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