xfs_attr_leaf.c 82 KB

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