xfs_attr_leaf.c 85 KB

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