xfs_attr_leaf.c 92 KB

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