xfs_attr_leaf.c 85 KB

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