xattr.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * xattr.c
  5. *
  6. * Copyright (C) 2008 Oracle. All rights reserved.
  7. *
  8. * CREDITS:
  9. * Lots of code in this file is taken from ext3.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. */
  26. #include <linux/capability.h>
  27. #include <linux/fs.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/highmem.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/uio.h>
  33. #include <linux/sched.h>
  34. #include <linux/splice.h>
  35. #include <linux/mount.h>
  36. #include <linux/writeback.h>
  37. #include <linux/falloc.h>
  38. #include <linux/sort.h>
  39. #define MLOG_MASK_PREFIX ML_XATTR
  40. #include <cluster/masklog.h>
  41. #include "ocfs2.h"
  42. #include "alloc.h"
  43. #include "dlmglue.h"
  44. #include "file.h"
  45. #include "symlink.h"
  46. #include "sysfile.h"
  47. #include "inode.h"
  48. #include "journal.h"
  49. #include "ocfs2_fs.h"
  50. #include "suballoc.h"
  51. #include "uptodate.h"
  52. #include "buffer_head_io.h"
  53. #include "super.h"
  54. #include "xattr.h"
  55. struct ocfs2_xattr_def_value_root {
  56. struct ocfs2_xattr_value_root xv;
  57. struct ocfs2_extent_rec er;
  58. };
  59. struct ocfs2_xattr_bucket {
  60. struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  61. struct ocfs2_xattr_header *xh;
  62. };
  63. #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
  64. #define OCFS2_XATTR_INLINE_SIZE 80
  65. static struct ocfs2_xattr_def_value_root def_xv = {
  66. .xv.xr_list.l_count = cpu_to_le16(1),
  67. };
  68. struct xattr_handler *ocfs2_xattr_handlers[] = {
  69. &ocfs2_xattr_user_handler,
  70. &ocfs2_xattr_trusted_handler,
  71. NULL
  72. };
  73. static struct xattr_handler *ocfs2_xattr_handler_map[] = {
  74. [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
  75. [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
  76. };
  77. struct ocfs2_xattr_info {
  78. int name_index;
  79. const char *name;
  80. const void *value;
  81. size_t value_len;
  82. };
  83. struct ocfs2_xattr_search {
  84. struct buffer_head *inode_bh;
  85. /*
  86. * xattr_bh point to the block buffer head which has extended attribute
  87. * when extended attribute in inode, xattr_bh is equal to inode_bh.
  88. */
  89. struct buffer_head *xattr_bh;
  90. struct ocfs2_xattr_header *header;
  91. struct ocfs2_xattr_bucket bucket;
  92. void *base;
  93. void *end;
  94. struct ocfs2_xattr_entry *here;
  95. int not_found;
  96. };
  97. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  98. struct ocfs2_xattr_header *xh,
  99. int index,
  100. int *block_off,
  101. int *new_offset);
  102. static int ocfs2_xattr_index_block_find(struct inode *inode,
  103. struct buffer_head *root_bh,
  104. int name_index,
  105. const char *name,
  106. struct ocfs2_xattr_search *xs);
  107. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  108. struct ocfs2_xattr_tree_root *xt,
  109. char *buffer,
  110. size_t buffer_size);
  111. static int ocfs2_xattr_create_index_block(struct inode *inode,
  112. struct ocfs2_xattr_search *xs);
  113. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  114. struct ocfs2_xattr_info *xi,
  115. struct ocfs2_xattr_search *xs);
  116. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  117. struct buffer_head *xb_bh);
  118. static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
  119. {
  120. struct xattr_handler *handler = NULL;
  121. if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
  122. handler = ocfs2_xattr_handler_map[name_index];
  123. return handler;
  124. }
  125. static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
  126. char *prefix,
  127. int prefix_len,
  128. char *name,
  129. int name_len)
  130. {
  131. /* Get hash value of uuid from super block */
  132. u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
  133. int i;
  134. /* hash extended attribute prefix */
  135. for (i = 0; i < prefix_len; i++) {
  136. hash = (hash << OCFS2_HASH_SHIFT) ^
  137. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  138. *prefix++;
  139. }
  140. /* hash extended attribute name */
  141. for (i = 0; i < name_len; i++) {
  142. hash = (hash << OCFS2_HASH_SHIFT) ^
  143. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  144. *name++;
  145. }
  146. return hash;
  147. }
  148. /*
  149. * ocfs2_xattr_hash_entry()
  150. *
  151. * Compute the hash of an extended attribute.
  152. */
  153. static void ocfs2_xattr_hash_entry(struct inode *inode,
  154. struct ocfs2_xattr_header *header,
  155. struct ocfs2_xattr_entry *entry)
  156. {
  157. u32 hash = 0;
  158. struct xattr_handler *handler =
  159. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  160. char *prefix = handler->prefix;
  161. char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
  162. int prefix_len = strlen(handler->prefix);
  163. hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
  164. entry->xe_name_len);
  165. entry->xe_name_hash = cpu_to_le32(hash);
  166. return;
  167. }
  168. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  169. u32 clusters_to_add,
  170. struct buffer_head *xattr_bh,
  171. struct ocfs2_xattr_value_root *xv)
  172. {
  173. int status = 0;
  174. int restart_func = 0;
  175. int credits = 0;
  176. handle_t *handle = NULL;
  177. struct ocfs2_alloc_context *data_ac = NULL;
  178. struct ocfs2_alloc_context *meta_ac = NULL;
  179. enum ocfs2_alloc_restarted why;
  180. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  181. struct ocfs2_extent_list *root_el = &xv->xr_list;
  182. u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
  183. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  184. restart_all:
  185. status = ocfs2_lock_allocators(inode, xattr_bh, root_el,
  186. clusters_to_add, 0, &data_ac,
  187. &meta_ac, OCFS2_XATTR_VALUE_EXTENT, xv);
  188. if (status) {
  189. mlog_errno(status);
  190. goto leave;
  191. }
  192. credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
  193. handle = ocfs2_start_trans(osb, credits);
  194. if (IS_ERR(handle)) {
  195. status = PTR_ERR(handle);
  196. handle = NULL;
  197. mlog_errno(status);
  198. goto leave;
  199. }
  200. restarted_transaction:
  201. status = ocfs2_journal_access(handle, inode, xattr_bh,
  202. OCFS2_JOURNAL_ACCESS_WRITE);
  203. if (status < 0) {
  204. mlog_errno(status);
  205. goto leave;
  206. }
  207. prev_clusters = le32_to_cpu(xv->xr_clusters);
  208. status = ocfs2_add_clusters_in_btree(osb,
  209. inode,
  210. &logical_start,
  211. clusters_to_add,
  212. 0,
  213. xattr_bh,
  214. root_el,
  215. handle,
  216. data_ac,
  217. meta_ac,
  218. &why,
  219. OCFS2_XATTR_VALUE_EXTENT,
  220. xv);
  221. if ((status < 0) && (status != -EAGAIN)) {
  222. if (status != -ENOSPC)
  223. mlog_errno(status);
  224. goto leave;
  225. }
  226. status = ocfs2_journal_dirty(handle, xattr_bh);
  227. if (status < 0) {
  228. mlog_errno(status);
  229. goto leave;
  230. }
  231. clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
  232. if (why != RESTART_NONE && clusters_to_add) {
  233. if (why == RESTART_META) {
  234. mlog(0, "restarting function.\n");
  235. restart_func = 1;
  236. } else {
  237. BUG_ON(why != RESTART_TRANS);
  238. mlog(0, "restarting transaction.\n");
  239. /* TODO: This can be more intelligent. */
  240. credits = ocfs2_calc_extend_credits(osb->sb,
  241. root_el,
  242. clusters_to_add);
  243. status = ocfs2_extend_trans(handle, credits);
  244. if (status < 0) {
  245. /* handle still has to be committed at
  246. * this point. */
  247. status = -ENOMEM;
  248. mlog_errno(status);
  249. goto leave;
  250. }
  251. goto restarted_transaction;
  252. }
  253. }
  254. leave:
  255. if (handle) {
  256. ocfs2_commit_trans(osb, handle);
  257. handle = NULL;
  258. }
  259. if (data_ac) {
  260. ocfs2_free_alloc_context(data_ac);
  261. data_ac = NULL;
  262. }
  263. if (meta_ac) {
  264. ocfs2_free_alloc_context(meta_ac);
  265. meta_ac = NULL;
  266. }
  267. if ((!status) && restart_func) {
  268. restart_func = 0;
  269. goto restart_all;
  270. }
  271. return status;
  272. }
  273. static int __ocfs2_remove_xattr_range(struct inode *inode,
  274. struct buffer_head *root_bh,
  275. struct ocfs2_xattr_value_root *xv,
  276. u32 cpos, u32 phys_cpos, u32 len,
  277. struct ocfs2_cached_dealloc_ctxt *dealloc)
  278. {
  279. int ret;
  280. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  281. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  282. struct inode *tl_inode = osb->osb_tl_inode;
  283. handle_t *handle;
  284. struct ocfs2_alloc_context *meta_ac = NULL;
  285. ret = ocfs2_lock_allocators(inode, root_bh, &xv->xr_list,
  286. 0, 1, NULL, &meta_ac,
  287. OCFS2_XATTR_VALUE_EXTENT, xv);
  288. if (ret) {
  289. mlog_errno(ret);
  290. return ret;
  291. }
  292. mutex_lock(&tl_inode->i_mutex);
  293. if (ocfs2_truncate_log_needs_flush(osb)) {
  294. ret = __ocfs2_flush_truncate_log(osb);
  295. if (ret < 0) {
  296. mlog_errno(ret);
  297. goto out;
  298. }
  299. }
  300. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  301. if (IS_ERR(handle)) {
  302. ret = PTR_ERR(handle);
  303. mlog_errno(ret);
  304. goto out;
  305. }
  306. ret = ocfs2_journal_access(handle, inode, root_bh,
  307. OCFS2_JOURNAL_ACCESS_WRITE);
  308. if (ret) {
  309. mlog_errno(ret);
  310. goto out_commit;
  311. }
  312. ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
  313. dealloc, OCFS2_XATTR_VALUE_EXTENT, xv);
  314. if (ret) {
  315. mlog_errno(ret);
  316. goto out_commit;
  317. }
  318. le32_add_cpu(&xv->xr_clusters, -len);
  319. ret = ocfs2_journal_dirty(handle, root_bh);
  320. if (ret) {
  321. mlog_errno(ret);
  322. goto out_commit;
  323. }
  324. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  325. if (ret)
  326. mlog_errno(ret);
  327. out_commit:
  328. ocfs2_commit_trans(osb, handle);
  329. out:
  330. mutex_unlock(&tl_inode->i_mutex);
  331. if (meta_ac)
  332. ocfs2_free_alloc_context(meta_ac);
  333. return ret;
  334. }
  335. static int ocfs2_xattr_shrink_size(struct inode *inode,
  336. u32 old_clusters,
  337. u32 new_clusters,
  338. struct buffer_head *root_bh,
  339. struct ocfs2_xattr_value_root *xv)
  340. {
  341. int ret = 0;
  342. u32 trunc_len, cpos, phys_cpos, alloc_size;
  343. u64 block;
  344. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  345. struct ocfs2_cached_dealloc_ctxt dealloc;
  346. ocfs2_init_dealloc_ctxt(&dealloc);
  347. if (old_clusters <= new_clusters)
  348. return 0;
  349. cpos = new_clusters;
  350. trunc_len = old_clusters - new_clusters;
  351. while (trunc_len) {
  352. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  353. &alloc_size, &xv->xr_list);
  354. if (ret) {
  355. mlog_errno(ret);
  356. goto out;
  357. }
  358. if (alloc_size > trunc_len)
  359. alloc_size = trunc_len;
  360. ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
  361. phys_cpos, alloc_size,
  362. &dealloc);
  363. if (ret) {
  364. mlog_errno(ret);
  365. goto out;
  366. }
  367. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  368. ocfs2_remove_xattr_clusters_from_cache(inode, block,
  369. alloc_size);
  370. cpos += alloc_size;
  371. trunc_len -= alloc_size;
  372. }
  373. out:
  374. ocfs2_schedule_truncate_log_flush(osb, 1);
  375. ocfs2_run_deallocs(osb, &dealloc);
  376. return ret;
  377. }
  378. static int ocfs2_xattr_value_truncate(struct inode *inode,
  379. struct buffer_head *root_bh,
  380. struct ocfs2_xattr_value_root *xv,
  381. int len)
  382. {
  383. int ret;
  384. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  385. u32 old_clusters = le32_to_cpu(xv->xr_clusters);
  386. if (new_clusters == old_clusters)
  387. return 0;
  388. if (new_clusters > old_clusters)
  389. ret = ocfs2_xattr_extend_allocation(inode,
  390. new_clusters - old_clusters,
  391. root_bh, xv);
  392. else
  393. ret = ocfs2_xattr_shrink_size(inode,
  394. old_clusters, new_clusters,
  395. root_bh, xv);
  396. return ret;
  397. }
  398. static int ocfs2_xattr_list_entries(struct inode *inode,
  399. struct ocfs2_xattr_header *header,
  400. char *buffer, size_t buffer_size)
  401. {
  402. size_t rest = buffer_size;
  403. int i;
  404. for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
  405. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  406. struct xattr_handler *handler =
  407. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  408. if (handler) {
  409. size_t size = handler->list(inode, buffer, rest,
  410. ((char *)header +
  411. le16_to_cpu(entry->xe_name_offset)),
  412. entry->xe_name_len);
  413. if (buffer) {
  414. if (size > rest)
  415. return -ERANGE;
  416. buffer += size;
  417. }
  418. rest -= size;
  419. }
  420. }
  421. return buffer_size - rest;
  422. }
  423. static int ocfs2_xattr_ibody_list(struct inode *inode,
  424. struct ocfs2_dinode *di,
  425. char *buffer,
  426. size_t buffer_size)
  427. {
  428. struct ocfs2_xattr_header *header = NULL;
  429. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  430. int ret = 0;
  431. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  432. return ret;
  433. header = (struct ocfs2_xattr_header *)
  434. ((void *)di + inode->i_sb->s_blocksize -
  435. le16_to_cpu(di->i_xattr_inline_size));
  436. ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
  437. return ret;
  438. }
  439. static int ocfs2_xattr_block_list(struct inode *inode,
  440. struct ocfs2_dinode *di,
  441. char *buffer,
  442. size_t buffer_size)
  443. {
  444. struct buffer_head *blk_bh = NULL;
  445. struct ocfs2_xattr_block *xb;
  446. int ret = 0;
  447. if (!di->i_xattr_loc)
  448. return ret;
  449. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  450. le64_to_cpu(di->i_xattr_loc),
  451. &blk_bh, OCFS2_BH_CACHED, inode);
  452. if (ret < 0) {
  453. mlog_errno(ret);
  454. return ret;
  455. }
  456. /*Verify the signature of xattr block*/
  457. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  458. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  459. ret = -EFAULT;
  460. goto cleanup;
  461. }
  462. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  463. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  464. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  465. ret = ocfs2_xattr_list_entries(inode, header,
  466. buffer, buffer_size);
  467. } else {
  468. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  469. ret = ocfs2_xattr_tree_list_index_block(inode, xt,
  470. buffer, buffer_size);
  471. }
  472. cleanup:
  473. brelse(blk_bh);
  474. return ret;
  475. }
  476. ssize_t ocfs2_listxattr(struct dentry *dentry,
  477. char *buffer,
  478. size_t size)
  479. {
  480. int ret = 0, i_ret = 0, b_ret = 0;
  481. struct buffer_head *di_bh = NULL;
  482. struct ocfs2_dinode *di = NULL;
  483. struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
  484. if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
  485. return -EOPNOTSUPP;
  486. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  487. return ret;
  488. ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
  489. if (ret < 0) {
  490. mlog_errno(ret);
  491. return ret;
  492. }
  493. di = (struct ocfs2_dinode *)di_bh->b_data;
  494. down_read(&oi->ip_xattr_sem);
  495. i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
  496. if (i_ret < 0)
  497. b_ret = 0;
  498. else {
  499. if (buffer) {
  500. buffer += i_ret;
  501. size -= i_ret;
  502. }
  503. b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
  504. buffer, size);
  505. if (b_ret < 0)
  506. i_ret = 0;
  507. }
  508. up_read(&oi->ip_xattr_sem);
  509. ocfs2_inode_unlock(dentry->d_inode, 0);
  510. brelse(di_bh);
  511. return i_ret + b_ret;
  512. }
  513. static int ocfs2_xattr_find_entry(int name_index,
  514. const char *name,
  515. struct ocfs2_xattr_search *xs)
  516. {
  517. struct ocfs2_xattr_entry *entry;
  518. size_t name_len;
  519. int i, cmp = 1;
  520. if (name == NULL)
  521. return -EINVAL;
  522. name_len = strlen(name);
  523. entry = xs->here;
  524. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  525. cmp = name_index - ocfs2_xattr_get_type(entry);
  526. if (!cmp)
  527. cmp = name_len - entry->xe_name_len;
  528. if (!cmp)
  529. cmp = memcmp(name, (xs->base +
  530. le16_to_cpu(entry->xe_name_offset)),
  531. name_len);
  532. if (cmp == 0)
  533. break;
  534. entry += 1;
  535. }
  536. xs->here = entry;
  537. return cmp ? -ENODATA : 0;
  538. }
  539. static int ocfs2_xattr_get_value_outside(struct inode *inode,
  540. struct ocfs2_xattr_value_root *xv,
  541. void *buffer,
  542. size_t len)
  543. {
  544. u32 cpos, p_cluster, num_clusters, bpc, clusters;
  545. u64 blkno;
  546. int i, ret = 0;
  547. size_t cplen, blocksize;
  548. struct buffer_head *bh = NULL;
  549. struct ocfs2_extent_list *el;
  550. el = &xv->xr_list;
  551. clusters = le32_to_cpu(xv->xr_clusters);
  552. bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  553. blocksize = inode->i_sb->s_blocksize;
  554. cpos = 0;
  555. while (cpos < clusters) {
  556. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  557. &num_clusters, el);
  558. if (ret) {
  559. mlog_errno(ret);
  560. goto out;
  561. }
  562. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  563. /* Copy ocfs2_xattr_value */
  564. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  565. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  566. &bh, OCFS2_BH_CACHED, inode);
  567. if (ret) {
  568. mlog_errno(ret);
  569. goto out;
  570. }
  571. cplen = len >= blocksize ? blocksize : len;
  572. memcpy(buffer, bh->b_data, cplen);
  573. len -= cplen;
  574. buffer += cplen;
  575. brelse(bh);
  576. bh = NULL;
  577. if (len == 0)
  578. break;
  579. }
  580. cpos += num_clusters;
  581. }
  582. out:
  583. return ret;
  584. }
  585. static int ocfs2_xattr_ibody_get(struct inode *inode,
  586. int name_index,
  587. const char *name,
  588. void *buffer,
  589. size_t buffer_size,
  590. struct ocfs2_xattr_search *xs)
  591. {
  592. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  593. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  594. struct ocfs2_xattr_value_root *xv;
  595. size_t size;
  596. int ret = 0;
  597. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  598. return -ENODATA;
  599. xs->end = (void *)di + inode->i_sb->s_blocksize;
  600. xs->header = (struct ocfs2_xattr_header *)
  601. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  602. xs->base = (void *)xs->header;
  603. xs->here = xs->header->xh_entries;
  604. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  605. if (ret)
  606. return ret;
  607. size = le64_to_cpu(xs->here->xe_value_size);
  608. if (buffer) {
  609. if (size > buffer_size)
  610. return -ERANGE;
  611. if (ocfs2_xattr_is_local(xs->here)) {
  612. memcpy(buffer, (void *)xs->base +
  613. le16_to_cpu(xs->here->xe_name_offset) +
  614. OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
  615. } else {
  616. xv = (struct ocfs2_xattr_value_root *)
  617. (xs->base + le16_to_cpu(
  618. xs->here->xe_name_offset) +
  619. OCFS2_XATTR_SIZE(xs->here->xe_name_len));
  620. ret = ocfs2_xattr_get_value_outside(inode, xv,
  621. buffer, size);
  622. if (ret < 0) {
  623. mlog_errno(ret);
  624. return ret;
  625. }
  626. }
  627. }
  628. return size;
  629. }
  630. static int ocfs2_xattr_block_get(struct inode *inode,
  631. int name_index,
  632. const char *name,
  633. void *buffer,
  634. size_t buffer_size,
  635. struct ocfs2_xattr_search *xs)
  636. {
  637. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  638. struct buffer_head *blk_bh = NULL;
  639. struct ocfs2_xattr_block *xb;
  640. struct ocfs2_xattr_value_root *xv;
  641. size_t size;
  642. int ret = -ENODATA, name_offset, name_len, block_off, i;
  643. if (!di->i_xattr_loc)
  644. return ret;
  645. memset(&xs->bucket, 0, sizeof(xs->bucket));
  646. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  647. le64_to_cpu(di->i_xattr_loc),
  648. &blk_bh, OCFS2_BH_CACHED, inode);
  649. if (ret < 0) {
  650. mlog_errno(ret);
  651. return ret;
  652. }
  653. /*Verify the signature of xattr block*/
  654. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  655. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  656. ret = -EFAULT;
  657. goto cleanup;
  658. }
  659. xs->xattr_bh = blk_bh;
  660. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  661. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  662. xs->header = &xb->xb_attrs.xb_header;
  663. xs->base = (void *)xs->header;
  664. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  665. xs->here = xs->header->xh_entries;
  666. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  667. } else
  668. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  669. name_index,
  670. name, xs);
  671. if (ret)
  672. goto cleanup;
  673. size = le64_to_cpu(xs->here->xe_value_size);
  674. if (buffer) {
  675. ret = -ERANGE;
  676. if (size > buffer_size)
  677. goto cleanup;
  678. name_offset = le16_to_cpu(xs->here->xe_name_offset);
  679. name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
  680. i = xs->here - xs->header->xh_entries;
  681. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  682. ret = ocfs2_xattr_bucket_get_name_value(inode,
  683. xs->bucket.xh,
  684. i,
  685. &block_off,
  686. &name_offset);
  687. xs->base = xs->bucket.bhs[block_off]->b_data;
  688. }
  689. if (ocfs2_xattr_is_local(xs->here)) {
  690. memcpy(buffer, (void *)xs->base +
  691. name_offset + name_len, size);
  692. } else {
  693. xv = (struct ocfs2_xattr_value_root *)
  694. (xs->base + name_offset + name_len);
  695. ret = ocfs2_xattr_get_value_outside(inode, xv,
  696. buffer, size);
  697. if (ret < 0) {
  698. mlog_errno(ret);
  699. goto cleanup;
  700. }
  701. }
  702. }
  703. ret = size;
  704. cleanup:
  705. for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
  706. brelse(xs->bucket.bhs[i]);
  707. memset(&xs->bucket, 0, sizeof(xs->bucket));
  708. brelse(blk_bh);
  709. return ret;
  710. }
  711. /* ocfs2_xattr_get()
  712. *
  713. * Copy an extended attribute into the buffer provided.
  714. * Buffer is NULL to compute the size of buffer required.
  715. */
  716. int ocfs2_xattr_get(struct inode *inode,
  717. int name_index,
  718. const char *name,
  719. void *buffer,
  720. size_t buffer_size)
  721. {
  722. int ret;
  723. struct ocfs2_dinode *di = NULL;
  724. struct buffer_head *di_bh = NULL;
  725. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  726. struct ocfs2_xattr_search xis = {
  727. .not_found = -ENODATA,
  728. };
  729. struct ocfs2_xattr_search xbs = {
  730. .not_found = -ENODATA,
  731. };
  732. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  733. return -EOPNOTSUPP;
  734. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  735. ret = -ENODATA;
  736. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  737. if (ret < 0) {
  738. mlog_errno(ret);
  739. return ret;
  740. }
  741. xis.inode_bh = xbs.inode_bh = di_bh;
  742. di = (struct ocfs2_dinode *)di_bh->b_data;
  743. down_read(&oi->ip_xattr_sem);
  744. ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
  745. buffer_size, &xis);
  746. if (ret == -ENODATA)
  747. ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
  748. buffer_size, &xbs);
  749. up_read(&oi->ip_xattr_sem);
  750. ocfs2_inode_unlock(inode, 0);
  751. brelse(di_bh);
  752. return ret;
  753. }
  754. static int __ocfs2_xattr_set_value_outside(struct inode *inode,
  755. struct ocfs2_xattr_value_root *xv,
  756. const void *value,
  757. int value_len)
  758. {
  759. int ret = 0, i, cp_len, credits;
  760. u16 blocksize = inode->i_sb->s_blocksize;
  761. u32 p_cluster, num_clusters;
  762. u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  763. u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
  764. u64 blkno;
  765. struct buffer_head *bh = NULL;
  766. handle_t *handle;
  767. BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
  768. credits = clusters * bpc;
  769. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
  770. if (IS_ERR(handle)) {
  771. ret = PTR_ERR(handle);
  772. mlog_errno(ret);
  773. goto out;
  774. }
  775. while (cpos < clusters) {
  776. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  777. &num_clusters, &xv->xr_list);
  778. if (ret) {
  779. mlog_errno(ret);
  780. goto out_commit;
  781. }
  782. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  783. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  784. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  785. &bh, OCFS2_BH_CACHED, inode);
  786. if (ret) {
  787. mlog_errno(ret);
  788. goto out_commit;
  789. }
  790. ret = ocfs2_journal_access(handle,
  791. inode,
  792. bh,
  793. OCFS2_JOURNAL_ACCESS_WRITE);
  794. if (ret < 0) {
  795. mlog_errno(ret);
  796. goto out_commit;
  797. }
  798. cp_len = value_len > blocksize ? blocksize : value_len;
  799. memcpy(bh->b_data, value, cp_len);
  800. value_len -= cp_len;
  801. value += cp_len;
  802. if (cp_len < blocksize)
  803. memset(bh->b_data + cp_len, 0,
  804. blocksize - cp_len);
  805. ret = ocfs2_journal_dirty(handle, bh);
  806. if (ret < 0) {
  807. mlog_errno(ret);
  808. goto out_commit;
  809. }
  810. brelse(bh);
  811. bh = NULL;
  812. /*
  813. * XXX: do we need to empty all the following
  814. * blocks in this cluster?
  815. */
  816. if (!value_len)
  817. break;
  818. }
  819. cpos += num_clusters;
  820. }
  821. out_commit:
  822. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  823. out:
  824. brelse(bh);
  825. return ret;
  826. }
  827. static int ocfs2_xattr_cleanup(struct inode *inode,
  828. struct ocfs2_xattr_info *xi,
  829. struct ocfs2_xattr_search *xs,
  830. size_t offs)
  831. {
  832. handle_t *handle = NULL;
  833. int ret = 0;
  834. size_t name_len = strlen(xi->name);
  835. void *val = xs->base + offs;
  836. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  837. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  838. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  839. if (IS_ERR(handle)) {
  840. ret = PTR_ERR(handle);
  841. mlog_errno(ret);
  842. goto out;
  843. }
  844. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  845. OCFS2_JOURNAL_ACCESS_WRITE);
  846. if (ret) {
  847. mlog_errno(ret);
  848. goto out_commit;
  849. }
  850. /* Decrease xattr count */
  851. le16_add_cpu(&xs->header->xh_count, -1);
  852. /* Remove the xattr entry and tree root which has already be set*/
  853. memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
  854. memset(val, 0, size);
  855. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  856. if (ret < 0)
  857. mlog_errno(ret);
  858. out_commit:
  859. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  860. out:
  861. return ret;
  862. }
  863. static int ocfs2_xattr_update_entry(struct inode *inode,
  864. struct ocfs2_xattr_info *xi,
  865. struct ocfs2_xattr_search *xs,
  866. size_t offs)
  867. {
  868. handle_t *handle = NULL;
  869. int ret = 0;
  870. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  871. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  872. if (IS_ERR(handle)) {
  873. ret = PTR_ERR(handle);
  874. mlog_errno(ret);
  875. goto out;
  876. }
  877. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  878. OCFS2_JOURNAL_ACCESS_WRITE);
  879. if (ret) {
  880. mlog_errno(ret);
  881. goto out_commit;
  882. }
  883. xs->here->xe_name_offset = cpu_to_le16(offs);
  884. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  885. if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
  886. ocfs2_xattr_set_local(xs->here, 1);
  887. else
  888. ocfs2_xattr_set_local(xs->here, 0);
  889. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  890. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  891. if (ret < 0)
  892. mlog_errno(ret);
  893. out_commit:
  894. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  895. out:
  896. return ret;
  897. }
  898. /*
  899. * ocfs2_xattr_set_value_outside()
  900. *
  901. * Set large size value in B tree.
  902. */
  903. static int ocfs2_xattr_set_value_outside(struct inode *inode,
  904. struct ocfs2_xattr_info *xi,
  905. struct ocfs2_xattr_search *xs,
  906. size_t offs)
  907. {
  908. size_t name_len = strlen(xi->name);
  909. void *val = xs->base + offs;
  910. struct ocfs2_xattr_value_root *xv = NULL;
  911. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  912. int ret = 0;
  913. memset(val, 0, size);
  914. memcpy(val, xi->name, name_len);
  915. xv = (struct ocfs2_xattr_value_root *)
  916. (val + OCFS2_XATTR_SIZE(name_len));
  917. xv->xr_clusters = 0;
  918. xv->xr_last_eb_blk = 0;
  919. xv->xr_list.l_tree_depth = 0;
  920. xv->xr_list.l_count = cpu_to_le16(1);
  921. xv->xr_list.l_next_free_rec = 0;
  922. ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
  923. xi->value_len);
  924. if (ret < 0) {
  925. mlog_errno(ret);
  926. return ret;
  927. }
  928. ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
  929. xi->value_len);
  930. if (ret < 0) {
  931. mlog_errno(ret);
  932. return ret;
  933. }
  934. ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
  935. if (ret < 0)
  936. mlog_errno(ret);
  937. return ret;
  938. }
  939. /*
  940. * ocfs2_xattr_set_entry_local()
  941. *
  942. * Set, replace or remove extended attribute in local.
  943. */
  944. static void ocfs2_xattr_set_entry_local(struct inode *inode,
  945. struct ocfs2_xattr_info *xi,
  946. struct ocfs2_xattr_search *xs,
  947. struct ocfs2_xattr_entry *last,
  948. size_t min_offs)
  949. {
  950. size_t name_len = strlen(xi->name);
  951. int i;
  952. if (xi->value && xs->not_found) {
  953. /* Insert the new xattr entry. */
  954. le16_add_cpu(&xs->header->xh_count, 1);
  955. ocfs2_xattr_set_type(last, xi->name_index);
  956. ocfs2_xattr_set_local(last, 1);
  957. last->xe_name_len = name_len;
  958. } else {
  959. void *first_val;
  960. void *val;
  961. size_t offs, size;
  962. first_val = xs->base + min_offs;
  963. offs = le16_to_cpu(xs->here->xe_name_offset);
  964. val = xs->base + offs;
  965. if (le64_to_cpu(xs->here->xe_value_size) >
  966. OCFS2_XATTR_INLINE_SIZE)
  967. size = OCFS2_XATTR_SIZE(name_len) +
  968. OCFS2_XATTR_ROOT_SIZE;
  969. else
  970. size = OCFS2_XATTR_SIZE(name_len) +
  971. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  972. if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
  973. OCFS2_XATTR_SIZE(xi->value_len)) {
  974. /* The old and the new value have the
  975. same size. Just replace the value. */
  976. ocfs2_xattr_set_local(xs->here, 1);
  977. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  978. /* Clear value bytes. */
  979. memset(val + OCFS2_XATTR_SIZE(name_len),
  980. 0,
  981. OCFS2_XATTR_SIZE(xi->value_len));
  982. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  983. xi->value,
  984. xi->value_len);
  985. return;
  986. }
  987. /* Remove the old name+value. */
  988. memmove(first_val + size, first_val, val - first_val);
  989. memset(first_val, 0, size);
  990. xs->here->xe_name_hash = 0;
  991. xs->here->xe_name_offset = 0;
  992. ocfs2_xattr_set_local(xs->here, 1);
  993. xs->here->xe_value_size = 0;
  994. min_offs += size;
  995. /* Adjust all value offsets. */
  996. last = xs->header->xh_entries;
  997. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  998. size_t o = le16_to_cpu(last->xe_name_offset);
  999. if (o < offs)
  1000. last->xe_name_offset = cpu_to_le16(o + size);
  1001. last += 1;
  1002. }
  1003. if (!xi->value) {
  1004. /* Remove the old entry. */
  1005. last -= 1;
  1006. memmove(xs->here, xs->here + 1,
  1007. (void *)last - (void *)xs->here);
  1008. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  1009. le16_add_cpu(&xs->header->xh_count, -1);
  1010. }
  1011. }
  1012. if (xi->value) {
  1013. /* Insert the new name+value. */
  1014. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1015. OCFS2_XATTR_SIZE(xi->value_len);
  1016. void *val = xs->base + min_offs - size;
  1017. xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
  1018. memset(val, 0, size);
  1019. memcpy(val, xi->name, name_len);
  1020. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1021. xi->value,
  1022. xi->value_len);
  1023. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1024. ocfs2_xattr_set_local(xs->here, 1);
  1025. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1026. }
  1027. return;
  1028. }
  1029. /*
  1030. * ocfs2_xattr_set_entry()
  1031. *
  1032. * Set extended attribute entry into inode or block.
  1033. *
  1034. * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
  1035. * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
  1036. * then set value in B tree with set_value_outside().
  1037. */
  1038. static int ocfs2_xattr_set_entry(struct inode *inode,
  1039. struct ocfs2_xattr_info *xi,
  1040. struct ocfs2_xattr_search *xs,
  1041. int flag)
  1042. {
  1043. struct ocfs2_xattr_entry *last;
  1044. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1045. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1046. size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
  1047. size_t size_l = 0;
  1048. handle_t *handle = NULL;
  1049. int free, i, ret;
  1050. struct ocfs2_xattr_info xi_l = {
  1051. .name_index = xi->name_index,
  1052. .name = xi->name,
  1053. .value = xi->value,
  1054. .value_len = xi->value_len,
  1055. };
  1056. /* Compute min_offs, last and free space. */
  1057. last = xs->header->xh_entries;
  1058. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1059. size_t offs = le16_to_cpu(last->xe_name_offset);
  1060. if (offs < min_offs)
  1061. min_offs = offs;
  1062. last += 1;
  1063. }
  1064. free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
  1065. if (free < 0)
  1066. return -EFAULT;
  1067. if (!xs->not_found) {
  1068. size_t size = 0;
  1069. if (ocfs2_xattr_is_local(xs->here))
  1070. size = OCFS2_XATTR_SIZE(name_len) +
  1071. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1072. else
  1073. size = OCFS2_XATTR_SIZE(name_len) +
  1074. OCFS2_XATTR_ROOT_SIZE;
  1075. free += (size + sizeof(struct ocfs2_xattr_entry));
  1076. }
  1077. /* Check free space in inode or block */
  1078. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1079. if (free < sizeof(struct ocfs2_xattr_entry) +
  1080. OCFS2_XATTR_SIZE(name_len) +
  1081. OCFS2_XATTR_ROOT_SIZE) {
  1082. ret = -ENOSPC;
  1083. goto out;
  1084. }
  1085. size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1086. xi_l.value = (void *)&def_xv;
  1087. xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
  1088. } else if (xi->value) {
  1089. if (free < sizeof(struct ocfs2_xattr_entry) +
  1090. OCFS2_XATTR_SIZE(name_len) +
  1091. OCFS2_XATTR_SIZE(xi->value_len)) {
  1092. ret = -ENOSPC;
  1093. goto out;
  1094. }
  1095. }
  1096. if (!xs->not_found) {
  1097. /* For existing extended attribute */
  1098. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1099. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1100. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1101. void *val = xs->base + offs;
  1102. if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
  1103. /* Replace existing local xattr with tree root */
  1104. ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
  1105. offs);
  1106. if (ret < 0)
  1107. mlog_errno(ret);
  1108. goto out;
  1109. } else if (!ocfs2_xattr_is_local(xs->here)) {
  1110. /* For existing xattr which has value outside */
  1111. struct ocfs2_xattr_value_root *xv = NULL;
  1112. xv = (struct ocfs2_xattr_value_root *)(val +
  1113. OCFS2_XATTR_SIZE(name_len));
  1114. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1115. /*
  1116. * If new value need set outside also,
  1117. * first truncate old value to new value,
  1118. * then set new value with set_value_outside().
  1119. */
  1120. ret = ocfs2_xattr_value_truncate(inode,
  1121. xs->xattr_bh,
  1122. xv,
  1123. xi->value_len);
  1124. if (ret < 0) {
  1125. mlog_errno(ret);
  1126. goto out;
  1127. }
  1128. ret = __ocfs2_xattr_set_value_outside(inode,
  1129. xv,
  1130. xi->value,
  1131. xi->value_len);
  1132. if (ret < 0) {
  1133. mlog_errno(ret);
  1134. goto out;
  1135. }
  1136. ret = ocfs2_xattr_update_entry(inode,
  1137. xi,
  1138. xs,
  1139. offs);
  1140. if (ret < 0)
  1141. mlog_errno(ret);
  1142. goto out;
  1143. } else {
  1144. /*
  1145. * If new value need set in local,
  1146. * just trucate old value to zero.
  1147. */
  1148. ret = ocfs2_xattr_value_truncate(inode,
  1149. xs->xattr_bh,
  1150. xv,
  1151. 0);
  1152. if (ret < 0)
  1153. mlog_errno(ret);
  1154. }
  1155. }
  1156. }
  1157. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1158. OCFS2_INODE_UPDATE_CREDITS);
  1159. if (IS_ERR(handle)) {
  1160. ret = PTR_ERR(handle);
  1161. mlog_errno(ret);
  1162. goto out;
  1163. }
  1164. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1165. OCFS2_JOURNAL_ACCESS_WRITE);
  1166. if (ret) {
  1167. mlog_errno(ret);
  1168. goto out_commit;
  1169. }
  1170. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1171. /*set extended attribue in external blcok*/
  1172. ret = ocfs2_extend_trans(handle,
  1173. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  1174. if (ret) {
  1175. mlog_errno(ret);
  1176. goto out_commit;
  1177. }
  1178. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  1179. OCFS2_JOURNAL_ACCESS_WRITE);
  1180. if (ret) {
  1181. mlog_errno(ret);
  1182. goto out_commit;
  1183. }
  1184. }
  1185. /*
  1186. * Set value in local, include set tree root in local.
  1187. * This is the first step for value size >INLINE_SIZE.
  1188. */
  1189. ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
  1190. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1191. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  1192. if (ret < 0) {
  1193. mlog_errno(ret);
  1194. goto out_commit;
  1195. }
  1196. }
  1197. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
  1198. (flag & OCFS2_INLINE_XATTR_FL)) {
  1199. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1200. unsigned int xattrsize = osb->s_xattr_inline_size;
  1201. /*
  1202. * Adjust extent record count or inline data size
  1203. * to reserve space for extended attribute.
  1204. */
  1205. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1206. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1207. le16_add_cpu(&idata->id_count, -xattrsize);
  1208. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  1209. struct ocfs2_extent_list *el = &di->id2.i_list;
  1210. le16_add_cpu(&el->l_count, -(xattrsize /
  1211. sizeof(struct ocfs2_extent_rec)));
  1212. }
  1213. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  1214. }
  1215. /* Update xattr flag */
  1216. spin_lock(&oi->ip_lock);
  1217. oi->ip_dyn_features |= flag;
  1218. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1219. spin_unlock(&oi->ip_lock);
  1220. /* Update inode ctime */
  1221. inode->i_ctime = CURRENT_TIME;
  1222. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1223. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1224. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1225. if (ret < 0)
  1226. mlog_errno(ret);
  1227. out_commit:
  1228. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1229. if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1230. /*
  1231. * Set value outside in B tree.
  1232. * This is the second step for value size > INLINE_SIZE.
  1233. */
  1234. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1235. ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
  1236. if (ret < 0) {
  1237. int ret2;
  1238. mlog_errno(ret);
  1239. /*
  1240. * If set value outside failed, we have to clean
  1241. * the junk tree root we have already set in local.
  1242. */
  1243. ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
  1244. if (ret2 < 0)
  1245. mlog_errno(ret2);
  1246. }
  1247. }
  1248. out:
  1249. return ret;
  1250. }
  1251. static int ocfs2_xattr_free_block(handle_t *handle,
  1252. struct ocfs2_super *osb,
  1253. struct ocfs2_xattr_block *xb)
  1254. {
  1255. struct inode *xb_alloc_inode;
  1256. struct buffer_head *xb_alloc_bh = NULL;
  1257. u64 blk = le64_to_cpu(xb->xb_blkno);
  1258. u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
  1259. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1260. int ret = 0;
  1261. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  1262. EXTENT_ALLOC_SYSTEM_INODE,
  1263. le16_to_cpu(xb->xb_suballoc_slot));
  1264. if (!xb_alloc_inode) {
  1265. ret = -ENOMEM;
  1266. mlog_errno(ret);
  1267. goto out;
  1268. }
  1269. mutex_lock(&xb_alloc_inode->i_mutex);
  1270. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  1271. if (ret < 0) {
  1272. mlog_errno(ret);
  1273. goto out_mutex;
  1274. }
  1275. ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
  1276. if (ret < 0) {
  1277. mlog_errno(ret);
  1278. goto out_unlock;
  1279. }
  1280. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  1281. bit, bg_blkno, 1);
  1282. if (ret < 0)
  1283. mlog_errno(ret);
  1284. out_unlock:
  1285. ocfs2_inode_unlock(xb_alloc_inode, 1);
  1286. brelse(xb_alloc_bh);
  1287. out_mutex:
  1288. mutex_unlock(&xb_alloc_inode->i_mutex);
  1289. iput(xb_alloc_inode);
  1290. out:
  1291. return ret;
  1292. }
  1293. static int ocfs2_remove_value_outside(struct inode*inode,
  1294. struct buffer_head *bh,
  1295. struct ocfs2_xattr_header *header)
  1296. {
  1297. int ret = 0, i;
  1298. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  1299. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  1300. if (!ocfs2_xattr_is_local(entry)) {
  1301. struct ocfs2_xattr_value_root *xv;
  1302. void *val;
  1303. val = (void *)header +
  1304. le16_to_cpu(entry->xe_name_offset);
  1305. xv = (struct ocfs2_xattr_value_root *)
  1306. (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
  1307. ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
  1308. if (ret < 0) {
  1309. mlog_errno(ret);
  1310. return ret;
  1311. }
  1312. }
  1313. }
  1314. return ret;
  1315. }
  1316. static int ocfs2_xattr_ibody_remove(struct inode *inode,
  1317. struct buffer_head *di_bh)
  1318. {
  1319. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1320. struct ocfs2_xattr_header *header;
  1321. int ret;
  1322. header = (struct ocfs2_xattr_header *)
  1323. ((void *)di + inode->i_sb->s_blocksize -
  1324. le16_to_cpu(di->i_xattr_inline_size));
  1325. ret = ocfs2_remove_value_outside(inode, di_bh, header);
  1326. return ret;
  1327. }
  1328. static int ocfs2_xattr_block_remove(struct inode *inode,
  1329. struct buffer_head *blk_bh)
  1330. {
  1331. struct ocfs2_xattr_block *xb;
  1332. int ret = 0;
  1333. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1334. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1335. struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
  1336. ret = ocfs2_remove_value_outside(inode, blk_bh, header);
  1337. } else
  1338. ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
  1339. return ret;
  1340. }
  1341. /*
  1342. * ocfs2_xattr_remove()
  1343. *
  1344. * Free extended attribute resources associated with this inode.
  1345. */
  1346. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  1347. {
  1348. struct ocfs2_xattr_block *xb;
  1349. struct buffer_head *blk_bh = NULL;
  1350. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1351. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1352. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1353. handle_t *handle;
  1354. int ret;
  1355. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1356. return 0;
  1357. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1358. return 0;
  1359. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1360. ret = ocfs2_xattr_ibody_remove(inode, di_bh);
  1361. if (ret < 0) {
  1362. mlog_errno(ret);
  1363. goto out;
  1364. }
  1365. }
  1366. if (di->i_xattr_loc) {
  1367. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1368. le64_to_cpu(di->i_xattr_loc),
  1369. &blk_bh, OCFS2_BH_CACHED, inode);
  1370. if (ret < 0) {
  1371. mlog_errno(ret);
  1372. return ret;
  1373. }
  1374. /*Verify the signature of xattr block*/
  1375. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1376. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1377. ret = -EFAULT;
  1378. goto out;
  1379. }
  1380. ret = ocfs2_xattr_block_remove(inode, blk_bh);
  1381. if (ret < 0) {
  1382. mlog_errno(ret);
  1383. goto out;
  1384. }
  1385. }
  1386. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1387. OCFS2_INODE_UPDATE_CREDITS);
  1388. if (IS_ERR(handle)) {
  1389. ret = PTR_ERR(handle);
  1390. mlog_errno(ret);
  1391. goto out;
  1392. }
  1393. ret = ocfs2_journal_access(handle, inode, di_bh,
  1394. OCFS2_JOURNAL_ACCESS_WRITE);
  1395. if (ret) {
  1396. mlog_errno(ret);
  1397. goto out_commit;
  1398. }
  1399. if (di->i_xattr_loc) {
  1400. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1401. ocfs2_xattr_free_block(handle, osb, xb);
  1402. di->i_xattr_loc = cpu_to_le64(0);
  1403. }
  1404. spin_lock(&oi->ip_lock);
  1405. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  1406. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1407. spin_unlock(&oi->ip_lock);
  1408. ret = ocfs2_journal_dirty(handle, di_bh);
  1409. if (ret < 0)
  1410. mlog_errno(ret);
  1411. out_commit:
  1412. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1413. out:
  1414. brelse(blk_bh);
  1415. return ret;
  1416. }
  1417. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  1418. struct ocfs2_dinode *di)
  1419. {
  1420. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1421. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  1422. int free;
  1423. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  1424. return 0;
  1425. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1426. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1427. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  1428. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  1429. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  1430. le64_to_cpu(di->i_size);
  1431. } else {
  1432. struct ocfs2_extent_list *el = &di->id2.i_list;
  1433. free = (le16_to_cpu(el->l_count) -
  1434. le16_to_cpu(el->l_next_free_rec)) *
  1435. sizeof(struct ocfs2_extent_rec);
  1436. }
  1437. if (free >= xattrsize)
  1438. return 1;
  1439. return 0;
  1440. }
  1441. /*
  1442. * ocfs2_xattr_ibody_find()
  1443. *
  1444. * Find extended attribute in inode block and
  1445. * fill search info into struct ocfs2_xattr_search.
  1446. */
  1447. static int ocfs2_xattr_ibody_find(struct inode *inode,
  1448. int name_index,
  1449. const char *name,
  1450. struct ocfs2_xattr_search *xs)
  1451. {
  1452. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1453. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1454. int ret;
  1455. int has_space = 0;
  1456. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1457. return 0;
  1458. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1459. down_read(&oi->ip_alloc_sem);
  1460. has_space = ocfs2_xattr_has_space_inline(inode, di);
  1461. up_read(&oi->ip_alloc_sem);
  1462. if (!has_space)
  1463. return 0;
  1464. }
  1465. xs->xattr_bh = xs->inode_bh;
  1466. xs->end = (void *)di + inode->i_sb->s_blocksize;
  1467. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  1468. xs->header = (struct ocfs2_xattr_header *)
  1469. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  1470. else
  1471. xs->header = (struct ocfs2_xattr_header *)
  1472. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  1473. xs->base = (void *)xs->header;
  1474. xs->here = xs->header->xh_entries;
  1475. /* Find the named attribute. */
  1476. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1477. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1478. if (ret && ret != -ENODATA)
  1479. return ret;
  1480. xs->not_found = ret;
  1481. }
  1482. return 0;
  1483. }
  1484. /*
  1485. * ocfs2_xattr_ibody_set()
  1486. *
  1487. * Set, replace or remove an extended attribute into inode block.
  1488. *
  1489. */
  1490. static int ocfs2_xattr_ibody_set(struct inode *inode,
  1491. struct ocfs2_xattr_info *xi,
  1492. struct ocfs2_xattr_search *xs)
  1493. {
  1494. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1495. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1496. int ret;
  1497. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1498. return -ENOSPC;
  1499. down_write(&oi->ip_alloc_sem);
  1500. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1501. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  1502. ret = -ENOSPC;
  1503. goto out;
  1504. }
  1505. }
  1506. ret = ocfs2_xattr_set_entry(inode, xi, xs,
  1507. (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
  1508. out:
  1509. up_write(&oi->ip_alloc_sem);
  1510. return ret;
  1511. }
  1512. /*
  1513. * ocfs2_xattr_block_find()
  1514. *
  1515. * Find extended attribute in external block and
  1516. * fill search info into struct ocfs2_xattr_search.
  1517. */
  1518. static int ocfs2_xattr_block_find(struct inode *inode,
  1519. int name_index,
  1520. const char *name,
  1521. struct ocfs2_xattr_search *xs)
  1522. {
  1523. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1524. struct buffer_head *blk_bh = NULL;
  1525. struct ocfs2_xattr_block *xb;
  1526. int ret = 0;
  1527. if (!di->i_xattr_loc)
  1528. return ret;
  1529. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1530. le64_to_cpu(di->i_xattr_loc),
  1531. &blk_bh, OCFS2_BH_CACHED, inode);
  1532. if (ret < 0) {
  1533. mlog_errno(ret);
  1534. return ret;
  1535. }
  1536. /*Verify the signature of xattr block*/
  1537. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1538. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1539. ret = -EFAULT;
  1540. goto cleanup;
  1541. }
  1542. xs->xattr_bh = blk_bh;
  1543. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1544. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1545. xs->header = &xb->xb_attrs.xb_header;
  1546. xs->base = (void *)xs->header;
  1547. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  1548. xs->here = xs->header->xh_entries;
  1549. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1550. } else
  1551. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  1552. name_index,
  1553. name, xs);
  1554. if (ret && ret != -ENODATA) {
  1555. xs->xattr_bh = NULL;
  1556. goto cleanup;
  1557. }
  1558. xs->not_found = ret;
  1559. return 0;
  1560. cleanup:
  1561. brelse(blk_bh);
  1562. return ret;
  1563. }
  1564. /*
  1565. * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
  1566. * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
  1567. * re-initialized.
  1568. */
  1569. static int ocfs2_restore_xattr_block(struct inode *inode,
  1570. struct ocfs2_xattr_search *xs)
  1571. {
  1572. int ret;
  1573. handle_t *handle;
  1574. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1575. struct ocfs2_xattr_block *xb =
  1576. (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1577. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  1578. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  1579. BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
  1580. le16_to_cpu(el->l_next_free_rec) != 0);
  1581. handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  1582. if (IS_ERR(handle)) {
  1583. ret = PTR_ERR(handle);
  1584. handle = NULL;
  1585. goto out;
  1586. }
  1587. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  1588. OCFS2_JOURNAL_ACCESS_WRITE);
  1589. if (ret < 0) {
  1590. mlog_errno(ret);
  1591. goto out_commit;
  1592. }
  1593. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  1594. offsetof(struct ocfs2_xattr_block, xb_attrs));
  1595. xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
  1596. ocfs2_journal_dirty(handle, xs->xattr_bh);
  1597. out_commit:
  1598. ocfs2_commit_trans(osb, handle);
  1599. out:
  1600. return ret;
  1601. }
  1602. /*
  1603. * ocfs2_xattr_block_set()
  1604. *
  1605. * Set, replace or remove an extended attribute into external block.
  1606. *
  1607. */
  1608. static int ocfs2_xattr_block_set(struct inode *inode,
  1609. struct ocfs2_xattr_info *xi,
  1610. struct ocfs2_xattr_search *xs)
  1611. {
  1612. struct buffer_head *new_bh = NULL;
  1613. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1614. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1615. struct ocfs2_alloc_context *meta_ac = NULL;
  1616. handle_t *handle = NULL;
  1617. struct ocfs2_xattr_block *xblk = NULL;
  1618. u16 suballoc_bit_start;
  1619. u32 num_got;
  1620. u64 first_blkno;
  1621. int ret;
  1622. if (!xs->xattr_bh) {
  1623. /*
  1624. * Alloc one external block for extended attribute
  1625. * outside of inode.
  1626. */
  1627. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  1628. if (ret < 0) {
  1629. mlog_errno(ret);
  1630. goto out;
  1631. }
  1632. handle = ocfs2_start_trans(osb,
  1633. OCFS2_XATTR_BLOCK_CREATE_CREDITS);
  1634. if (IS_ERR(handle)) {
  1635. ret = PTR_ERR(handle);
  1636. mlog_errno(ret);
  1637. goto out;
  1638. }
  1639. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1640. OCFS2_JOURNAL_ACCESS_CREATE);
  1641. if (ret < 0) {
  1642. mlog_errno(ret);
  1643. goto out_commit;
  1644. }
  1645. ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
  1646. &suballoc_bit_start, &num_got,
  1647. &first_blkno);
  1648. if (ret < 0) {
  1649. mlog_errno(ret);
  1650. goto out_commit;
  1651. }
  1652. new_bh = sb_getblk(inode->i_sb, first_blkno);
  1653. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  1654. ret = ocfs2_journal_access(handle, inode, new_bh,
  1655. OCFS2_JOURNAL_ACCESS_CREATE);
  1656. if (ret < 0) {
  1657. mlog_errno(ret);
  1658. goto out_commit;
  1659. }
  1660. /* Initialize ocfs2_xattr_block */
  1661. xs->xattr_bh = new_bh;
  1662. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  1663. memset(xblk, 0, inode->i_sb->s_blocksize);
  1664. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  1665. xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
  1666. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1667. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  1668. xblk->xb_blkno = cpu_to_le64(first_blkno);
  1669. xs->header = &xblk->xb_attrs.xb_header;
  1670. xs->base = (void *)xs->header;
  1671. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  1672. xs->here = xs->header->xh_entries;
  1673. ret = ocfs2_journal_dirty(handle, new_bh);
  1674. if (ret < 0) {
  1675. mlog_errno(ret);
  1676. goto out_commit;
  1677. }
  1678. di->i_xattr_loc = cpu_to_le64(first_blkno);
  1679. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1680. if (ret < 0)
  1681. mlog_errno(ret);
  1682. out_commit:
  1683. ocfs2_commit_trans(osb, handle);
  1684. out:
  1685. if (meta_ac)
  1686. ocfs2_free_alloc_context(meta_ac);
  1687. if (ret < 0)
  1688. return ret;
  1689. } else
  1690. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1691. if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1692. /* Set extended attribute into external block */
  1693. ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
  1694. if (!ret || ret != -ENOSPC)
  1695. goto end;
  1696. ret = ocfs2_xattr_create_index_block(inode, xs);
  1697. if (ret)
  1698. goto end;
  1699. }
  1700. ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
  1701. if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
  1702. ret = ocfs2_restore_xattr_block(inode, xs);
  1703. end:
  1704. return ret;
  1705. }
  1706. /*
  1707. * ocfs2_xattr_set()
  1708. *
  1709. * Set, replace or remove an extended attribute for this inode.
  1710. * value is NULL to remove an existing extended attribute, else either
  1711. * create or replace an extended attribute.
  1712. */
  1713. int ocfs2_xattr_set(struct inode *inode,
  1714. int name_index,
  1715. const char *name,
  1716. const void *value,
  1717. size_t value_len,
  1718. int flags)
  1719. {
  1720. struct buffer_head *di_bh = NULL;
  1721. struct ocfs2_dinode *di;
  1722. int ret;
  1723. u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1724. struct ocfs2_xattr_info xi = {
  1725. .name_index = name_index,
  1726. .name = name,
  1727. .value = value,
  1728. .value_len = value_len,
  1729. };
  1730. struct ocfs2_xattr_search xis = {
  1731. .not_found = -ENODATA,
  1732. };
  1733. struct ocfs2_xattr_search xbs = {
  1734. .not_found = -ENODATA,
  1735. };
  1736. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1737. return -EOPNOTSUPP;
  1738. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1739. if (ret < 0) {
  1740. mlog_errno(ret);
  1741. return ret;
  1742. }
  1743. xis.inode_bh = xbs.inode_bh = di_bh;
  1744. di = (struct ocfs2_dinode *)di_bh->b_data;
  1745. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  1746. /*
  1747. * Scan inode and external block to find the same name
  1748. * extended attribute and collect search infomation.
  1749. */
  1750. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  1751. if (ret)
  1752. goto cleanup;
  1753. if (xis.not_found) {
  1754. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  1755. if (ret)
  1756. goto cleanup;
  1757. }
  1758. if (xis.not_found && xbs.not_found) {
  1759. ret = -ENODATA;
  1760. if (flags & XATTR_REPLACE)
  1761. goto cleanup;
  1762. ret = 0;
  1763. if (!value)
  1764. goto cleanup;
  1765. } else {
  1766. ret = -EEXIST;
  1767. if (flags & XATTR_CREATE)
  1768. goto cleanup;
  1769. }
  1770. if (!value) {
  1771. /* Remove existing extended attribute */
  1772. if (!xis.not_found)
  1773. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1774. else if (!xbs.not_found)
  1775. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1776. } else {
  1777. /* We always try to set extended attribute into inode first*/
  1778. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1779. if (!ret && !xbs.not_found) {
  1780. /*
  1781. * If succeed and that extended attribute existing in
  1782. * external block, then we will remove it.
  1783. */
  1784. xi.value = NULL;
  1785. xi.value_len = 0;
  1786. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1787. } else if (ret == -ENOSPC) {
  1788. if (di->i_xattr_loc && !xbs.xattr_bh) {
  1789. ret = ocfs2_xattr_block_find(inode, name_index,
  1790. name, &xbs);
  1791. if (ret)
  1792. goto cleanup;
  1793. }
  1794. /*
  1795. * If no space in inode, we will set extended attribute
  1796. * into external block.
  1797. */
  1798. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1799. if (ret)
  1800. goto cleanup;
  1801. if (!xis.not_found) {
  1802. /*
  1803. * If succeed and that extended attribute
  1804. * existing in inode, we will remove it.
  1805. */
  1806. xi.value = NULL;
  1807. xi.value_len = 0;
  1808. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1809. }
  1810. }
  1811. }
  1812. cleanup:
  1813. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  1814. ocfs2_inode_unlock(inode, 1);
  1815. brelse(di_bh);
  1816. brelse(xbs.xattr_bh);
  1817. for (i = 0; i < blk_per_bucket; i++)
  1818. brelse(xbs.bucket.bhs[i]);
  1819. return ret;
  1820. }
  1821. static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
  1822. int name_index,
  1823. const char *suffix_name)
  1824. {
  1825. struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
  1826. char *prefix = handler->prefix;
  1827. int prefix_len = strlen(handler->prefix);
  1828. return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
  1829. (char *)suffix_name, strlen(suffix_name));
  1830. }
  1831. /*
  1832. * Find the xattr extent rec which may contains name_hash.
  1833. * e_cpos will be the first name hash of the xattr rec.
  1834. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  1835. */
  1836. static int ocfs2_xattr_get_rec(struct inode *inode,
  1837. u32 name_hash,
  1838. u64 *p_blkno,
  1839. u32 *e_cpos,
  1840. u32 *num_clusters,
  1841. struct ocfs2_extent_list *el)
  1842. {
  1843. int ret = 0, i;
  1844. struct buffer_head *eb_bh = NULL;
  1845. struct ocfs2_extent_block *eb;
  1846. struct ocfs2_extent_rec *rec = NULL;
  1847. u64 e_blkno = 0;
  1848. if (el->l_tree_depth) {
  1849. ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
  1850. if (ret) {
  1851. mlog_errno(ret);
  1852. goto out;
  1853. }
  1854. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  1855. el = &eb->h_list;
  1856. if (el->l_tree_depth) {
  1857. ocfs2_error(inode->i_sb,
  1858. "Inode %lu has non zero tree depth in "
  1859. "xattr tree block %llu\n", inode->i_ino,
  1860. (unsigned long long)eb_bh->b_blocknr);
  1861. ret = -EROFS;
  1862. goto out;
  1863. }
  1864. }
  1865. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1866. rec = &el->l_recs[i];
  1867. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  1868. e_blkno = le64_to_cpu(rec->e_blkno);
  1869. break;
  1870. }
  1871. }
  1872. if (!e_blkno) {
  1873. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  1874. "record (%u, %u, 0) in xattr", inode->i_ino,
  1875. le32_to_cpu(rec->e_cpos),
  1876. ocfs2_rec_clusters(el, rec));
  1877. ret = -EROFS;
  1878. goto out;
  1879. }
  1880. *p_blkno = le64_to_cpu(rec->e_blkno);
  1881. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  1882. if (e_cpos)
  1883. *e_cpos = le32_to_cpu(rec->e_cpos);
  1884. out:
  1885. brelse(eb_bh);
  1886. return ret;
  1887. }
  1888. typedef int (xattr_bucket_func)(struct inode *inode,
  1889. struct ocfs2_xattr_bucket *bucket,
  1890. void *para);
  1891. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  1892. struct buffer_head *header_bh,
  1893. int name_index,
  1894. const char *name,
  1895. u32 name_hash,
  1896. u16 *xe_index,
  1897. int *found)
  1898. {
  1899. int i, ret = 0, cmp = 1, block_off, new_offset;
  1900. struct ocfs2_xattr_header *xh =
  1901. (struct ocfs2_xattr_header *)header_bh->b_data;
  1902. size_t name_len = strlen(name);
  1903. struct ocfs2_xattr_entry *xe = NULL;
  1904. struct buffer_head *name_bh = NULL;
  1905. char *xe_name;
  1906. /*
  1907. * We don't use binary search in the bucket because there
  1908. * may be multiple entries with the same name hash.
  1909. */
  1910. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  1911. xe = &xh->xh_entries[i];
  1912. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  1913. continue;
  1914. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  1915. break;
  1916. cmp = name_index - ocfs2_xattr_get_type(xe);
  1917. if (!cmp)
  1918. cmp = name_len - xe->xe_name_len;
  1919. if (cmp)
  1920. continue;
  1921. ret = ocfs2_xattr_bucket_get_name_value(inode,
  1922. xh,
  1923. i,
  1924. &block_off,
  1925. &new_offset);
  1926. if (ret) {
  1927. mlog_errno(ret);
  1928. break;
  1929. }
  1930. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1931. header_bh->b_blocknr + block_off,
  1932. &name_bh, OCFS2_BH_CACHED, inode);
  1933. if (ret) {
  1934. mlog_errno(ret);
  1935. break;
  1936. }
  1937. xe_name = name_bh->b_data + new_offset;
  1938. cmp = memcmp(name, xe_name, name_len);
  1939. brelse(name_bh);
  1940. name_bh = NULL;
  1941. if (cmp == 0) {
  1942. *xe_index = i;
  1943. *found = 1;
  1944. ret = 0;
  1945. break;
  1946. }
  1947. }
  1948. return ret;
  1949. }
  1950. /*
  1951. * Find the specified xattr entry in a series of buckets.
  1952. * This series start from p_blkno and last for num_clusters.
  1953. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  1954. * the num of the valid buckets.
  1955. *
  1956. * Return the buffer_head this xattr should reside in. And if the xattr's
  1957. * hash is in the gap of 2 buckets, return the lower bucket.
  1958. */
  1959. static int ocfs2_xattr_bucket_find(struct inode *inode,
  1960. int name_index,
  1961. const char *name,
  1962. u32 name_hash,
  1963. u64 p_blkno,
  1964. u32 first_hash,
  1965. u32 num_clusters,
  1966. struct ocfs2_xattr_search *xs)
  1967. {
  1968. int ret, found = 0;
  1969. struct buffer_head *bh = NULL;
  1970. struct buffer_head *lower_bh = NULL;
  1971. struct ocfs2_xattr_header *xh = NULL;
  1972. struct ocfs2_xattr_entry *xe = NULL;
  1973. u16 index = 0;
  1974. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1975. int low_bucket = 0, bucket, high_bucket;
  1976. u32 last_hash;
  1977. u64 blkno;
  1978. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
  1979. &bh, OCFS2_BH_CACHED, inode);
  1980. if (ret) {
  1981. mlog_errno(ret);
  1982. goto out;
  1983. }
  1984. xh = (struct ocfs2_xattr_header *)bh->b_data;
  1985. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  1986. while (low_bucket <= high_bucket) {
  1987. brelse(bh);
  1988. bh = NULL;
  1989. bucket = (low_bucket + high_bucket) / 2;
  1990. blkno = p_blkno + bucket * blk_per_bucket;
  1991. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
  1992. &bh, OCFS2_BH_CACHED, inode);
  1993. if (ret) {
  1994. mlog_errno(ret);
  1995. goto out;
  1996. }
  1997. xh = (struct ocfs2_xattr_header *)bh->b_data;
  1998. xe = &xh->xh_entries[0];
  1999. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  2000. high_bucket = bucket - 1;
  2001. continue;
  2002. }
  2003. /*
  2004. * Check whether the hash of the last entry in our
  2005. * bucket is larger than the search one.
  2006. */
  2007. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  2008. last_hash = le32_to_cpu(xe->xe_name_hash);
  2009. /* record lower_bh which may be the insert place. */
  2010. brelse(lower_bh);
  2011. lower_bh = bh;
  2012. bh = NULL;
  2013. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  2014. low_bucket = bucket + 1;
  2015. continue;
  2016. }
  2017. /* the searched xattr should reside in this bucket if exists. */
  2018. ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
  2019. name_index, name, name_hash,
  2020. &index, &found);
  2021. if (ret) {
  2022. mlog_errno(ret);
  2023. goto out;
  2024. }
  2025. break;
  2026. }
  2027. /*
  2028. * Record the bucket we have found.
  2029. * When the xattr's hash value is in the gap of 2 buckets, we will
  2030. * always set it to the previous bucket.
  2031. */
  2032. if (!lower_bh) {
  2033. /*
  2034. * We can't find any bucket whose first name_hash is less
  2035. * than the find name_hash.
  2036. */
  2037. BUG_ON(bh->b_blocknr != p_blkno);
  2038. lower_bh = bh;
  2039. bh = NULL;
  2040. }
  2041. xs->bucket.bhs[0] = lower_bh;
  2042. xs->bucket.xh = (struct ocfs2_xattr_header *)
  2043. xs->bucket.bhs[0]->b_data;
  2044. lower_bh = NULL;
  2045. xs->header = xs->bucket.xh;
  2046. xs->base = xs->bucket.bhs[0]->b_data;
  2047. xs->end = xs->base + inode->i_sb->s_blocksize;
  2048. if (found) {
  2049. /*
  2050. * If we have found the xattr enty, read all the blocks in
  2051. * this bucket.
  2052. */
  2053. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  2054. xs->bucket.bhs[0]->b_blocknr + 1,
  2055. blk_per_bucket - 1, &xs->bucket.bhs[1],
  2056. OCFS2_BH_CACHED, inode);
  2057. if (ret) {
  2058. mlog_errno(ret);
  2059. goto out;
  2060. }
  2061. xs->here = &xs->header->xh_entries[index];
  2062. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  2063. (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
  2064. } else
  2065. ret = -ENODATA;
  2066. out:
  2067. brelse(bh);
  2068. brelse(lower_bh);
  2069. return ret;
  2070. }
  2071. static int ocfs2_xattr_index_block_find(struct inode *inode,
  2072. struct buffer_head *root_bh,
  2073. int name_index,
  2074. const char *name,
  2075. struct ocfs2_xattr_search *xs)
  2076. {
  2077. int ret;
  2078. struct ocfs2_xattr_block *xb =
  2079. (struct ocfs2_xattr_block *)root_bh->b_data;
  2080. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  2081. struct ocfs2_extent_list *el = &xb_root->xt_list;
  2082. u64 p_blkno = 0;
  2083. u32 first_hash, num_clusters = 0;
  2084. u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
  2085. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2086. return -ENODATA;
  2087. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  2088. name, name_hash, name_index);
  2089. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  2090. &num_clusters, el);
  2091. if (ret) {
  2092. mlog_errno(ret);
  2093. goto out;
  2094. }
  2095. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  2096. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  2097. "in the rec is %u\n", num_clusters, p_blkno, first_hash);
  2098. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  2099. p_blkno, first_hash, num_clusters, xs);
  2100. out:
  2101. return ret;
  2102. }
  2103. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  2104. u64 blkno,
  2105. u32 clusters,
  2106. xattr_bucket_func *func,
  2107. void *para)
  2108. {
  2109. int i, j, ret = 0;
  2110. int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2111. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  2112. u32 num_buckets = clusters * bpc;
  2113. struct ocfs2_xattr_bucket bucket;
  2114. memset(&bucket, 0, sizeof(bucket));
  2115. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  2116. clusters, blkno);
  2117. for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
  2118. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  2119. blkno, blk_per_bucket,
  2120. bucket.bhs, OCFS2_BH_CACHED, inode);
  2121. if (ret) {
  2122. mlog_errno(ret);
  2123. goto out;
  2124. }
  2125. bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
  2126. /*
  2127. * The real bucket num in this series of blocks is stored
  2128. * in the 1st bucket.
  2129. */
  2130. if (i == 0)
  2131. num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
  2132. mlog(0, "iterating xattr bucket %llu\n", blkno);
  2133. if (func) {
  2134. ret = func(inode, &bucket, para);
  2135. if (ret) {
  2136. mlog_errno(ret);
  2137. break;
  2138. }
  2139. }
  2140. for (j = 0; j < blk_per_bucket; j++)
  2141. brelse(bucket.bhs[j]);
  2142. memset(&bucket, 0, sizeof(bucket));
  2143. }
  2144. out:
  2145. for (j = 0; j < blk_per_bucket; j++)
  2146. brelse(bucket.bhs[j]);
  2147. return ret;
  2148. }
  2149. struct ocfs2_xattr_tree_list {
  2150. char *buffer;
  2151. size_t buffer_size;
  2152. };
  2153. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  2154. struct ocfs2_xattr_header *xh,
  2155. int index,
  2156. int *block_off,
  2157. int *new_offset)
  2158. {
  2159. u16 name_offset;
  2160. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  2161. return -EINVAL;
  2162. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  2163. *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
  2164. *new_offset = name_offset % inode->i_sb->s_blocksize;
  2165. return 0;
  2166. }
  2167. static int ocfs2_list_xattr_bucket(struct inode *inode,
  2168. struct ocfs2_xattr_bucket *bucket,
  2169. void *para)
  2170. {
  2171. int ret = 0;
  2172. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  2173. size_t size;
  2174. int i, block_off, new_offset;
  2175. for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
  2176. struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
  2177. struct xattr_handler *handler =
  2178. ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
  2179. if (handler) {
  2180. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2181. bucket->xh,
  2182. i,
  2183. &block_off,
  2184. &new_offset);
  2185. if (ret)
  2186. break;
  2187. size = handler->list(inode, xl->buffer, xl->buffer_size,
  2188. bucket->bhs[block_off]->b_data +
  2189. new_offset,
  2190. entry->xe_name_len);
  2191. if (xl->buffer) {
  2192. if (size > xl->buffer_size)
  2193. return -ERANGE;
  2194. xl->buffer += size;
  2195. }
  2196. xl->buffer_size -= size;
  2197. }
  2198. }
  2199. return ret;
  2200. }
  2201. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  2202. struct ocfs2_xattr_tree_root *xt,
  2203. char *buffer,
  2204. size_t buffer_size)
  2205. {
  2206. struct ocfs2_extent_list *el = &xt->xt_list;
  2207. int ret = 0;
  2208. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  2209. u64 p_blkno = 0;
  2210. struct ocfs2_xattr_tree_list xl = {
  2211. .buffer = buffer,
  2212. .buffer_size = buffer_size,
  2213. };
  2214. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2215. return 0;
  2216. while (name_hash > 0) {
  2217. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  2218. &e_cpos, &num_clusters, el);
  2219. if (ret) {
  2220. mlog_errno(ret);
  2221. goto out;
  2222. }
  2223. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  2224. ocfs2_list_xattr_bucket,
  2225. &xl);
  2226. if (ret) {
  2227. mlog_errno(ret);
  2228. goto out;
  2229. }
  2230. if (e_cpos == 0)
  2231. break;
  2232. name_hash = e_cpos - 1;
  2233. }
  2234. ret = buffer_size - xl.buffer_size;
  2235. out:
  2236. return ret;
  2237. }
  2238. static int cmp_xe(const void *a, const void *b)
  2239. {
  2240. const struct ocfs2_xattr_entry *l = a, *r = b;
  2241. u32 l_hash = le32_to_cpu(l->xe_name_hash);
  2242. u32 r_hash = le32_to_cpu(r->xe_name_hash);
  2243. if (l_hash > r_hash)
  2244. return 1;
  2245. if (l_hash < r_hash)
  2246. return -1;
  2247. return 0;
  2248. }
  2249. static void swap_xe(void *a, void *b, int size)
  2250. {
  2251. struct ocfs2_xattr_entry *l = a, *r = b, tmp;
  2252. tmp = *l;
  2253. memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
  2254. memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
  2255. }
  2256. /*
  2257. * When the ocfs2_xattr_block is filled up, new bucket will be created
  2258. * and all the xattr entries will be moved to the new bucket.
  2259. * Note: we need to sort the entries since they are not saved in order
  2260. * in the ocfs2_xattr_block.
  2261. */
  2262. static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  2263. struct buffer_head *xb_bh,
  2264. struct buffer_head *xh_bh,
  2265. struct buffer_head *data_bh)
  2266. {
  2267. int i, blocksize = inode->i_sb->s_blocksize;
  2268. u16 offset, size, off_change;
  2269. struct ocfs2_xattr_entry *xe;
  2270. struct ocfs2_xattr_block *xb =
  2271. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2272. struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
  2273. struct ocfs2_xattr_header *xh =
  2274. (struct ocfs2_xattr_header *)xh_bh->b_data;
  2275. u16 count = le16_to_cpu(xb_xh->xh_count);
  2276. char *target = xh_bh->b_data, *src = xb_bh->b_data;
  2277. mlog(0, "cp xattr from block %llu to bucket %llu\n",
  2278. (unsigned long long)xb_bh->b_blocknr,
  2279. (unsigned long long)xh_bh->b_blocknr);
  2280. memset(xh_bh->b_data, 0, blocksize);
  2281. if (data_bh)
  2282. memset(data_bh->b_data, 0, blocksize);
  2283. /*
  2284. * Since the xe_name_offset is based on ocfs2_xattr_header,
  2285. * there is a offset change corresponding to the change of
  2286. * ocfs2_xattr_header's position.
  2287. */
  2288. off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2289. xe = &xb_xh->xh_entries[count - 1];
  2290. offset = le16_to_cpu(xe->xe_name_offset) + off_change;
  2291. size = blocksize - offset;
  2292. /* copy all the names and values. */
  2293. if (data_bh)
  2294. target = data_bh->b_data;
  2295. memcpy(target + offset, src + offset, size);
  2296. /* Init new header now. */
  2297. xh->xh_count = xb_xh->xh_count;
  2298. xh->xh_num_buckets = cpu_to_le16(1);
  2299. xh->xh_name_value_len = cpu_to_le16(size);
  2300. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
  2301. /* copy all the entries. */
  2302. target = xh_bh->b_data;
  2303. offset = offsetof(struct ocfs2_xattr_header, xh_entries);
  2304. size = count * sizeof(struct ocfs2_xattr_entry);
  2305. memcpy(target + offset, (char *)xb_xh + offset, size);
  2306. /* Change the xe offset for all the xe because of the move. */
  2307. off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
  2308. offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2309. for (i = 0; i < count; i++)
  2310. le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
  2311. mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
  2312. offset, size, off_change);
  2313. sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
  2314. cmp_xe, swap_xe);
  2315. }
  2316. /*
  2317. * After we move xattr from block to index btree, we have to
  2318. * update ocfs2_xattr_search to the new xe and base.
  2319. *
  2320. * When the entry is in xattr block, xattr_bh indicates the storage place.
  2321. * While if the entry is in index b-tree, "bucket" indicates the
  2322. * real place of the xattr.
  2323. */
  2324. static int ocfs2_xattr_update_xattr_search(struct inode *inode,
  2325. struct ocfs2_xattr_search *xs,
  2326. struct buffer_head *old_bh,
  2327. struct buffer_head *new_bh)
  2328. {
  2329. int ret = 0;
  2330. char *buf = old_bh->b_data;
  2331. struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
  2332. struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
  2333. int i, blocksize = inode->i_sb->s_blocksize;
  2334. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2335. xs->bucket.bhs[0] = new_bh;
  2336. get_bh(new_bh);
  2337. xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
  2338. xs->header = xs->bucket.xh;
  2339. xs->base = new_bh->b_data;
  2340. xs->end = xs->base + inode->i_sb->s_blocksize;
  2341. if (!xs->not_found) {
  2342. if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
  2343. ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  2344. xs->bucket.bhs[0]->b_blocknr + 1,
  2345. blk_per_bucket - 1, &xs->bucket.bhs[1],
  2346. OCFS2_BH_CACHED, inode);
  2347. if (ret) {
  2348. mlog_errno(ret);
  2349. return ret;
  2350. }
  2351. i = xs->here - old_xh->xh_entries;
  2352. xs->here = &xs->header->xh_entries[i];
  2353. }
  2354. }
  2355. return ret;
  2356. }
  2357. static int ocfs2_xattr_create_index_block(struct inode *inode,
  2358. struct ocfs2_xattr_search *xs)
  2359. {
  2360. int ret, credits = OCFS2_SUBALLOC_ALLOC;
  2361. u32 bit_off, len;
  2362. u64 blkno;
  2363. handle_t *handle;
  2364. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2365. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2366. struct ocfs2_alloc_context *data_ac;
  2367. struct buffer_head *xh_bh = NULL, *data_bh = NULL;
  2368. struct buffer_head *xb_bh = xs->xattr_bh;
  2369. struct ocfs2_xattr_block *xb =
  2370. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2371. struct ocfs2_xattr_tree_root *xr;
  2372. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  2373. u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2374. mlog(0, "create xattr index block for %llu\n",
  2375. (unsigned long long)xb_bh->b_blocknr);
  2376. BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
  2377. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  2378. if (ret) {
  2379. mlog_errno(ret);
  2380. goto out;
  2381. }
  2382. /*
  2383. * XXX:
  2384. * We can use this lock for now, and maybe move to a dedicated mutex
  2385. * if performance becomes a problem later.
  2386. */
  2387. down_write(&oi->ip_alloc_sem);
  2388. /*
  2389. * 3 more credits, one for xattr block update, one for the 1st block
  2390. * of the new xattr bucket and one for the value/data.
  2391. */
  2392. credits += 3;
  2393. handle = ocfs2_start_trans(osb, credits);
  2394. if (IS_ERR(handle)) {
  2395. ret = PTR_ERR(handle);
  2396. mlog_errno(ret);
  2397. goto out_sem;
  2398. }
  2399. ret = ocfs2_journal_access(handle, inode, xb_bh,
  2400. OCFS2_JOURNAL_ACCESS_WRITE);
  2401. if (ret) {
  2402. mlog_errno(ret);
  2403. goto out_commit;
  2404. }
  2405. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
  2406. if (ret) {
  2407. mlog_errno(ret);
  2408. goto out_commit;
  2409. }
  2410. /*
  2411. * The bucket may spread in many blocks, and
  2412. * we will only touch the 1st block and the last block
  2413. * in the whole bucket(one for entry and one for data).
  2414. */
  2415. blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  2416. mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
  2417. xh_bh = sb_getblk(inode->i_sb, blkno);
  2418. if (!xh_bh) {
  2419. ret = -EIO;
  2420. mlog_errno(ret);
  2421. goto out_commit;
  2422. }
  2423. ocfs2_set_new_buffer_uptodate(inode, xh_bh);
  2424. ret = ocfs2_journal_access(handle, inode, xh_bh,
  2425. OCFS2_JOURNAL_ACCESS_CREATE);
  2426. if (ret) {
  2427. mlog_errno(ret);
  2428. goto out_commit;
  2429. }
  2430. if (bpb > 1) {
  2431. data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
  2432. if (!data_bh) {
  2433. ret = -EIO;
  2434. mlog_errno(ret);
  2435. goto out_commit;
  2436. }
  2437. ocfs2_set_new_buffer_uptodate(inode, data_bh);
  2438. ret = ocfs2_journal_access(handle, inode, data_bh,
  2439. OCFS2_JOURNAL_ACCESS_CREATE);
  2440. if (ret) {
  2441. mlog_errno(ret);
  2442. goto out_commit;
  2443. }
  2444. }
  2445. ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
  2446. ocfs2_journal_dirty(handle, xh_bh);
  2447. if (data_bh)
  2448. ocfs2_journal_dirty(handle, data_bh);
  2449. ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
  2450. /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
  2451. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  2452. offsetof(struct ocfs2_xattr_block, xb_attrs));
  2453. xr = &xb->xb_attrs.xb_root;
  2454. xr->xt_clusters = cpu_to_le32(1);
  2455. xr->xt_last_eb_blk = 0;
  2456. xr->xt_list.l_tree_depth = 0;
  2457. xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
  2458. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  2459. xr->xt_list.l_recs[0].e_cpos = 0;
  2460. xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  2461. xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  2462. xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
  2463. ret = ocfs2_journal_dirty(handle, xb_bh);
  2464. if (ret) {
  2465. mlog_errno(ret);
  2466. goto out_commit;
  2467. }
  2468. out_commit:
  2469. ocfs2_commit_trans(osb, handle);
  2470. out_sem:
  2471. up_write(&oi->ip_alloc_sem);
  2472. out:
  2473. if (data_ac)
  2474. ocfs2_free_alloc_context(data_ac);
  2475. brelse(xh_bh);
  2476. brelse(data_bh);
  2477. return ret;
  2478. }
  2479. static int cmp_xe_offset(const void *a, const void *b)
  2480. {
  2481. const struct ocfs2_xattr_entry *l = a, *r = b;
  2482. u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
  2483. u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
  2484. if (l_name_offset < r_name_offset)
  2485. return 1;
  2486. if (l_name_offset > r_name_offset)
  2487. return -1;
  2488. return 0;
  2489. }
  2490. /*
  2491. * defrag a xattr bucket if we find that the bucket has some
  2492. * holes beteen name/value pairs.
  2493. * We will move all the name/value pairs to the end of the bucket
  2494. * so that we can spare some space for insertion.
  2495. */
  2496. static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  2497. struct ocfs2_xattr_bucket *bucket)
  2498. {
  2499. int ret, i;
  2500. size_t end, offset, len, value_len;
  2501. struct ocfs2_xattr_header *xh;
  2502. char *entries, *buf, *bucket_buf = NULL;
  2503. u64 blkno = bucket->bhs[0]->b_blocknr;
  2504. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2505. u16 xh_free_start;
  2506. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2507. size_t blocksize = inode->i_sb->s_blocksize;
  2508. handle_t *handle;
  2509. struct buffer_head **bhs;
  2510. struct ocfs2_xattr_entry *xe;
  2511. bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2512. GFP_NOFS);
  2513. if (!bhs)
  2514. return -ENOMEM;
  2515. ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
  2516. OCFS2_BH_CACHED, inode);
  2517. if (ret)
  2518. goto out;
  2519. /*
  2520. * In order to make the operation more efficient and generic,
  2521. * we copy all the blocks into a contiguous memory and do the
  2522. * defragment there, so if anything is error, we will not touch
  2523. * the real block.
  2524. */
  2525. bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
  2526. if (!bucket_buf) {
  2527. ret = -EIO;
  2528. goto out;
  2529. }
  2530. buf = bucket_buf;
  2531. for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
  2532. memcpy(buf, bhs[i]->b_data, blocksize);
  2533. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
  2534. if (IS_ERR(handle)) {
  2535. ret = PTR_ERR(handle);
  2536. handle = NULL;
  2537. mlog_errno(ret);
  2538. goto out;
  2539. }
  2540. for (i = 0; i < blk_per_bucket; i++) {
  2541. ret = ocfs2_journal_access(handle, inode, bhs[i],
  2542. OCFS2_JOURNAL_ACCESS_WRITE);
  2543. if (ret < 0) {
  2544. mlog_errno(ret);
  2545. goto commit;
  2546. }
  2547. }
  2548. xh = (struct ocfs2_xattr_header *)bucket_buf;
  2549. entries = (char *)xh->xh_entries;
  2550. xh_free_start = le16_to_cpu(xh->xh_free_start);
  2551. mlog(0, "adjust xattr bucket in %llu, count = %u, "
  2552. "xh_free_start = %u, xh_name_value_len = %u.\n",
  2553. blkno, le16_to_cpu(xh->xh_count), xh_free_start,
  2554. le16_to_cpu(xh->xh_name_value_len));
  2555. /*
  2556. * sort all the entries by their offset.
  2557. * the largest will be the first, so that we can
  2558. * move them to the end one by one.
  2559. */
  2560. sort(entries, le16_to_cpu(xh->xh_count),
  2561. sizeof(struct ocfs2_xattr_entry),
  2562. cmp_xe_offset, swap_xe);
  2563. /* Move all name/values to the end of the bucket. */
  2564. xe = xh->xh_entries;
  2565. end = OCFS2_XATTR_BUCKET_SIZE;
  2566. for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
  2567. offset = le16_to_cpu(xe->xe_name_offset);
  2568. if (ocfs2_xattr_is_local(xe))
  2569. value_len = OCFS2_XATTR_SIZE(
  2570. le64_to_cpu(xe->xe_value_size));
  2571. else
  2572. value_len = OCFS2_XATTR_ROOT_SIZE;
  2573. len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
  2574. /*
  2575. * We must make sure that the name/value pair
  2576. * exist in the same block. So adjust end to
  2577. * the previous block end if needed.
  2578. */
  2579. if (((end - len) / blocksize !=
  2580. (end - 1) / blocksize))
  2581. end = end - end % blocksize;
  2582. if (end > offset + len) {
  2583. memmove(bucket_buf + end - len,
  2584. bucket_buf + offset, len);
  2585. xe->xe_name_offset = cpu_to_le16(end - len);
  2586. }
  2587. mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
  2588. "bucket %llu\n", (unsigned long long)blkno);
  2589. end -= len;
  2590. }
  2591. mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
  2592. "bucket %llu\n", (unsigned long long)blkno);
  2593. if (xh_free_start == end)
  2594. goto commit;
  2595. memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
  2596. xh->xh_free_start = cpu_to_le16(end);
  2597. /* sort the entries by their name_hash. */
  2598. sort(entries, le16_to_cpu(xh->xh_count),
  2599. sizeof(struct ocfs2_xattr_entry),
  2600. cmp_xe, swap_xe);
  2601. buf = bucket_buf;
  2602. for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
  2603. memcpy(bhs[i]->b_data, buf, blocksize);
  2604. ocfs2_journal_dirty(handle, bhs[i]);
  2605. }
  2606. commit:
  2607. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  2608. out:
  2609. if (bhs) {
  2610. for (i = 0; i < blk_per_bucket; i++)
  2611. brelse(bhs[i]);
  2612. }
  2613. kfree(bhs);
  2614. kfree(bucket_buf);
  2615. return ret;
  2616. }
  2617. /*
  2618. * Move half nums of the xattr bucket in the previous cluster to this new
  2619. * cluster. We only touch the last cluster of the previous extend record.
  2620. *
  2621. * first_bh is the first buffer_head of a series of bucket in the same
  2622. * extent rec and header_bh is the header of one bucket in this cluster.
  2623. * They will be updated if we move the data header_bh contains to the new
  2624. * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
  2625. */
  2626. static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
  2627. handle_t *handle,
  2628. struct buffer_head **first_bh,
  2629. struct buffer_head **header_bh,
  2630. u64 new_blkno,
  2631. u64 prev_blkno,
  2632. u32 num_clusters,
  2633. u32 *first_hash)
  2634. {
  2635. int i, ret, credits;
  2636. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2637. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2638. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  2639. int blocksize = inode->i_sb->s_blocksize;
  2640. struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
  2641. struct ocfs2_xattr_header *new_xh;
  2642. struct ocfs2_xattr_header *xh =
  2643. (struct ocfs2_xattr_header *)((*first_bh)->b_data);
  2644. BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
  2645. BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
  2646. prev_bh = *first_bh;
  2647. get_bh(prev_bh);
  2648. xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
  2649. prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
  2650. mlog(0, "move half of xattrs in cluster %llu to %llu\n",
  2651. prev_blkno, new_blkno);
  2652. /*
  2653. * We need to update the 1st half of the new cluster and
  2654. * 1 more for the update of the 1st bucket of the previous
  2655. * extent record.
  2656. */
  2657. credits = bpc / 2 + 1;
  2658. ret = ocfs2_extend_trans(handle, credits);
  2659. if (ret) {
  2660. mlog_errno(ret);
  2661. goto out;
  2662. }
  2663. ret = ocfs2_journal_access(handle, inode, prev_bh,
  2664. OCFS2_JOURNAL_ACCESS_WRITE);
  2665. if (ret) {
  2666. mlog_errno(ret);
  2667. goto out;
  2668. }
  2669. for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
  2670. old_bh = new_bh = NULL;
  2671. new_bh = sb_getblk(inode->i_sb, new_blkno);
  2672. if (!new_bh) {
  2673. ret = -EIO;
  2674. mlog_errno(ret);
  2675. goto out;
  2676. }
  2677. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  2678. ret = ocfs2_journal_access(handle, inode, new_bh,
  2679. OCFS2_JOURNAL_ACCESS_CREATE);
  2680. if (ret < 0) {
  2681. mlog_errno(ret);
  2682. brelse(new_bh);
  2683. goto out;
  2684. }
  2685. ret = ocfs2_read_block(osb, prev_blkno,
  2686. &old_bh, OCFS2_BH_CACHED, inode);
  2687. if (ret < 0) {
  2688. mlog_errno(ret);
  2689. brelse(new_bh);
  2690. goto out;
  2691. }
  2692. memcpy(new_bh->b_data, old_bh->b_data, blocksize);
  2693. if (i == 0) {
  2694. new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
  2695. new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
  2696. if (first_hash)
  2697. *first_hash = le32_to_cpu(
  2698. new_xh->xh_entries[0].xe_name_hash);
  2699. new_first_bh = new_bh;
  2700. get_bh(new_first_bh);
  2701. }
  2702. ocfs2_journal_dirty(handle, new_bh);
  2703. if (*header_bh == old_bh) {
  2704. brelse(*header_bh);
  2705. *header_bh = new_bh;
  2706. get_bh(*header_bh);
  2707. brelse(*first_bh);
  2708. *first_bh = new_first_bh;
  2709. get_bh(*first_bh);
  2710. }
  2711. brelse(new_bh);
  2712. brelse(old_bh);
  2713. }
  2714. le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
  2715. ocfs2_journal_dirty(handle, prev_bh);
  2716. out:
  2717. brelse(prev_bh);
  2718. brelse(new_first_bh);
  2719. return ret;
  2720. }
  2721. static int ocfs2_read_xattr_bucket(struct inode *inode,
  2722. u64 blkno,
  2723. struct buffer_head **bhs,
  2724. int new)
  2725. {
  2726. int ret = 0;
  2727. u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2728. if (!new)
  2729. return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
  2730. blk_per_bucket, bhs,
  2731. OCFS2_BH_CACHED, inode);
  2732. for (i = 0; i < blk_per_bucket; i++) {
  2733. bhs[i] = sb_getblk(inode->i_sb, blkno + i);
  2734. if (bhs[i] == NULL) {
  2735. ret = -EIO;
  2736. mlog_errno(ret);
  2737. break;
  2738. }
  2739. ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
  2740. }
  2741. return ret;
  2742. }
  2743. /*
  2744. * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
  2745. * first_hash will record the 1st hash of the new bucket.
  2746. */
  2747. static int ocfs2_half_xattr_bucket(struct inode *inode,
  2748. handle_t *handle,
  2749. u64 blk,
  2750. u64 new_blk,
  2751. u32 *first_hash,
  2752. int new_bucket_head)
  2753. {
  2754. int ret, i;
  2755. u16 count, start, len, name_value_len, xe_len, name_offset;
  2756. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2757. struct buffer_head **s_bhs, **t_bhs = NULL;
  2758. struct ocfs2_xattr_header *xh;
  2759. struct ocfs2_xattr_entry *xe;
  2760. int blocksize = inode->i_sb->s_blocksize;
  2761. mlog(0, "move half of xattrs from bucket %llu to %llu\n",
  2762. blk, new_blk);
  2763. s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
  2764. if (!s_bhs)
  2765. return -ENOMEM;
  2766. ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
  2767. if (ret) {
  2768. mlog_errno(ret);
  2769. goto out;
  2770. }
  2771. ret = ocfs2_journal_access(handle, inode, s_bhs[0],
  2772. OCFS2_JOURNAL_ACCESS_WRITE);
  2773. if (ret) {
  2774. mlog_errno(ret);
  2775. goto out;
  2776. }
  2777. t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
  2778. if (!t_bhs) {
  2779. ret = -ENOMEM;
  2780. goto out;
  2781. }
  2782. ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
  2783. if (ret) {
  2784. mlog_errno(ret);
  2785. goto out;
  2786. }
  2787. for (i = 0; i < blk_per_bucket; i++) {
  2788. ret = ocfs2_journal_access(handle, inode, t_bhs[i],
  2789. OCFS2_JOURNAL_ACCESS_CREATE);
  2790. if (ret) {
  2791. mlog_errno(ret);
  2792. goto out;
  2793. }
  2794. }
  2795. /* copy the whole bucket to the new first. */
  2796. for (i = 0; i < blk_per_bucket; i++)
  2797. memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
  2798. /* update the new bucket. */
  2799. xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
  2800. count = le16_to_cpu(xh->xh_count);
  2801. start = count / 2;
  2802. /*
  2803. * Calculate the total name/value len and xh_free_start for
  2804. * the old bucket first.
  2805. */
  2806. name_offset = OCFS2_XATTR_BUCKET_SIZE;
  2807. name_value_len = 0;
  2808. for (i = 0; i < start; i++) {
  2809. xe = &xh->xh_entries[i];
  2810. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2811. if (ocfs2_xattr_is_local(xe))
  2812. xe_len +=
  2813. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  2814. else
  2815. xe_len += OCFS2_XATTR_ROOT_SIZE;
  2816. name_value_len += xe_len;
  2817. if (le16_to_cpu(xe->xe_name_offset) < name_offset)
  2818. name_offset = le16_to_cpu(xe->xe_name_offset);
  2819. }
  2820. /*
  2821. * Now begin the modification to the new bucket.
  2822. *
  2823. * In the new bucket, We just move the xattr entry to the beginning
  2824. * and don't touch the name/value. So there will be some holes in the
  2825. * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
  2826. * called.
  2827. */
  2828. xe = &xh->xh_entries[start];
  2829. len = sizeof(struct ocfs2_xattr_entry) * (count - start);
  2830. mlog(0, "mv xattr entry len %d from %d to %d\n", len,
  2831. (int)((char *)xe - (char *)xh),
  2832. (int)((char *)xh->xh_entries - (char *)xh));
  2833. memmove((char *)xh->xh_entries, (char *)xe, len);
  2834. xe = &xh->xh_entries[count - start];
  2835. len = sizeof(struct ocfs2_xattr_entry) * start;
  2836. memset((char *)xe, 0, len);
  2837. le16_add_cpu(&xh->xh_count, -start);
  2838. le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
  2839. /* Calculate xh_free_start for the new bucket. */
  2840. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  2841. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  2842. xe = &xh->xh_entries[i];
  2843. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2844. if (ocfs2_xattr_is_local(xe))
  2845. xe_len +=
  2846. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  2847. else
  2848. xe_len += OCFS2_XATTR_ROOT_SIZE;
  2849. if (le16_to_cpu(xe->xe_name_offset) <
  2850. le16_to_cpu(xh->xh_free_start))
  2851. xh->xh_free_start = xe->xe_name_offset;
  2852. }
  2853. /* set xh->xh_num_buckets for the new xh. */
  2854. if (new_bucket_head)
  2855. xh->xh_num_buckets = cpu_to_le16(1);
  2856. else
  2857. xh->xh_num_buckets = 0;
  2858. for (i = 0; i < blk_per_bucket; i++) {
  2859. ocfs2_journal_dirty(handle, t_bhs[i]);
  2860. if (ret)
  2861. mlog_errno(ret);
  2862. }
  2863. /* store the first_hash of the new bucket. */
  2864. if (first_hash)
  2865. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  2866. /*
  2867. * Now only update the 1st block of the old bucket.
  2868. * Please note that the entry has been sorted already above.
  2869. */
  2870. xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
  2871. memset(&xh->xh_entries[start], 0,
  2872. sizeof(struct ocfs2_xattr_entry) * (count - start));
  2873. xh->xh_count = cpu_to_le16(start);
  2874. xh->xh_free_start = cpu_to_le16(name_offset);
  2875. xh->xh_name_value_len = cpu_to_le16(name_value_len);
  2876. ocfs2_journal_dirty(handle, s_bhs[0]);
  2877. if (ret)
  2878. mlog_errno(ret);
  2879. out:
  2880. if (s_bhs) {
  2881. for (i = 0; i < blk_per_bucket; i++)
  2882. brelse(s_bhs[i]);
  2883. }
  2884. kfree(s_bhs);
  2885. if (t_bhs) {
  2886. for (i = 0; i < blk_per_bucket; i++)
  2887. brelse(t_bhs[i]);
  2888. }
  2889. kfree(t_bhs);
  2890. return ret;
  2891. }
  2892. /*
  2893. * Copy xattr from one bucket to another bucket.
  2894. *
  2895. * The caller must make sure that the journal transaction
  2896. * has enough space for journaling.
  2897. */
  2898. static int ocfs2_cp_xattr_bucket(struct inode *inode,
  2899. handle_t *handle,
  2900. u64 s_blkno,
  2901. u64 t_blkno,
  2902. int t_is_new)
  2903. {
  2904. int ret, i;
  2905. int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2906. int blocksize = inode->i_sb->s_blocksize;
  2907. struct buffer_head **s_bhs, **t_bhs = NULL;
  2908. BUG_ON(s_blkno == t_blkno);
  2909. mlog(0, "cp bucket %llu to %llu, target is %d\n",
  2910. s_blkno, t_blkno, t_is_new);
  2911. s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2912. GFP_NOFS);
  2913. if (!s_bhs)
  2914. return -ENOMEM;
  2915. ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
  2916. if (ret)
  2917. goto out;
  2918. t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2919. GFP_NOFS);
  2920. if (!t_bhs) {
  2921. ret = -ENOMEM;
  2922. goto out;
  2923. }
  2924. ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
  2925. if (ret)
  2926. goto out;
  2927. for (i = 0; i < blk_per_bucket; i++) {
  2928. ret = ocfs2_journal_access(handle, inode, t_bhs[i],
  2929. OCFS2_JOURNAL_ACCESS_WRITE);
  2930. if (ret)
  2931. goto out;
  2932. }
  2933. for (i = 0; i < blk_per_bucket; i++) {
  2934. memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
  2935. ocfs2_journal_dirty(handle, t_bhs[i]);
  2936. }
  2937. out:
  2938. if (s_bhs) {
  2939. for (i = 0; i < blk_per_bucket; i++)
  2940. brelse(s_bhs[i]);
  2941. }
  2942. kfree(s_bhs);
  2943. if (t_bhs) {
  2944. for (i = 0; i < blk_per_bucket; i++)
  2945. brelse(t_bhs[i]);
  2946. }
  2947. kfree(t_bhs);
  2948. return ret;
  2949. }
  2950. /*
  2951. * Copy one xattr cluster from src_blk to to_blk.
  2952. * The to_blk will become the first bucket header of the cluster, so its
  2953. * xh_num_buckets will be initialized as the bucket num in the cluster.
  2954. */
  2955. static int ocfs2_cp_xattr_cluster(struct inode *inode,
  2956. handle_t *handle,
  2957. struct buffer_head *first_bh,
  2958. u64 src_blk,
  2959. u64 to_blk,
  2960. u32 *first_hash)
  2961. {
  2962. int i, ret, credits;
  2963. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2964. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2965. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  2966. struct buffer_head *bh = NULL;
  2967. struct ocfs2_xattr_header *xh;
  2968. u64 to_blk_start = to_blk;
  2969. mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
  2970. /*
  2971. * We need to update the new cluster and 1 more for the update of
  2972. * the 1st bucket of the previous extent rec.
  2973. */
  2974. credits = bpc + 1;
  2975. ret = ocfs2_extend_trans(handle, credits);
  2976. if (ret) {
  2977. mlog_errno(ret);
  2978. goto out;
  2979. }
  2980. ret = ocfs2_journal_access(handle, inode, first_bh,
  2981. OCFS2_JOURNAL_ACCESS_WRITE);
  2982. if (ret) {
  2983. mlog_errno(ret);
  2984. goto out;
  2985. }
  2986. for (i = 0; i < num_buckets; i++) {
  2987. ret = ocfs2_cp_xattr_bucket(inode, handle,
  2988. src_blk, to_blk, 1);
  2989. if (ret) {
  2990. mlog_errno(ret);
  2991. goto out;
  2992. }
  2993. src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2994. to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2995. }
  2996. /* update the old bucket header. */
  2997. xh = (struct ocfs2_xattr_header *)first_bh->b_data;
  2998. le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
  2999. ocfs2_journal_dirty(handle, first_bh);
  3000. /* update the new bucket header. */
  3001. ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
  3002. if (ret < 0) {
  3003. mlog_errno(ret);
  3004. goto out;
  3005. }
  3006. ret = ocfs2_journal_access(handle, inode, bh,
  3007. OCFS2_JOURNAL_ACCESS_WRITE);
  3008. if (ret) {
  3009. mlog_errno(ret);
  3010. goto out;
  3011. }
  3012. xh = (struct ocfs2_xattr_header *)bh->b_data;
  3013. xh->xh_num_buckets = cpu_to_le16(num_buckets);
  3014. ocfs2_journal_dirty(handle, bh);
  3015. if (first_hash)
  3016. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3017. out:
  3018. brelse(bh);
  3019. return ret;
  3020. }
  3021. /*
  3022. * Move half of the xattrs in this cluster to the new cluster.
  3023. * This function should only be called when bucket size == cluster size.
  3024. * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  3025. */
  3026. static int ocfs2_half_xattr_cluster(struct inode *inode,
  3027. handle_t *handle,
  3028. u64 prev_blk,
  3029. u64 new_blk,
  3030. u32 *first_hash)
  3031. {
  3032. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3033. int ret, credits = 2 * blk_per_bucket;
  3034. BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
  3035. ret = ocfs2_extend_trans(handle, credits);
  3036. if (ret) {
  3037. mlog_errno(ret);
  3038. return ret;
  3039. }
  3040. /* Move half of the xattr in start_blk to the next bucket. */
  3041. return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
  3042. new_blk, first_hash, 1);
  3043. }
  3044. /*
  3045. * Move some xattrs from the old cluster to the new one since they are not
  3046. * contiguous in ocfs2 xattr tree.
  3047. *
  3048. * new_blk starts a new separate cluster, and we will move some xattrs from
  3049. * prev_blk to it. v_start will be set as the first name hash value in this
  3050. * new cluster so that it can be used as e_cpos during tree insertion and
  3051. * don't collide with our original b-tree operations. first_bh and header_bh
  3052. * will also be updated since they will be used in ocfs2_extend_xattr_bucket
  3053. * to extend the insert bucket.
  3054. *
  3055. * The problem is how much xattr should we move to the new one and when should
  3056. * we update first_bh and header_bh?
  3057. * 1. If cluster size > bucket size, that means the previous cluster has more
  3058. * than 1 bucket, so just move half nums of bucket into the new cluster and
  3059. * update the first_bh and header_bh if the insert bucket has been moved
  3060. * to the new cluster.
  3061. * 2. If cluster_size == bucket_size:
  3062. * a) If the previous extent rec has more than one cluster and the insert
  3063. * place isn't in the last cluster, copy the entire last cluster to the
  3064. * new one. This time, we don't need to upate the first_bh and header_bh
  3065. * since they will not be moved into the new cluster.
  3066. * b) Otherwise, move the bottom half of the xattrs in the last cluster into
  3067. * the new one. And we set the extend flag to zero if the insert place is
  3068. * moved into the new allocated cluster since no extend is needed.
  3069. */
  3070. static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
  3071. handle_t *handle,
  3072. struct buffer_head **first_bh,
  3073. struct buffer_head **header_bh,
  3074. u64 new_blk,
  3075. u64 prev_blk,
  3076. u32 prev_clusters,
  3077. u32 *v_start,
  3078. int *extend)
  3079. {
  3080. int ret = 0;
  3081. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3082. mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
  3083. prev_blk, prev_clusters, new_blk);
  3084. if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
  3085. ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
  3086. handle,
  3087. first_bh,
  3088. header_bh,
  3089. new_blk,
  3090. prev_blk,
  3091. prev_clusters,
  3092. v_start);
  3093. else {
  3094. u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
  3095. if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
  3096. ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
  3097. last_blk, new_blk,
  3098. v_start);
  3099. else {
  3100. ret = ocfs2_half_xattr_cluster(inode, handle,
  3101. last_blk, new_blk,
  3102. v_start);
  3103. if ((*header_bh)->b_blocknr == last_blk && extend)
  3104. *extend = 0;
  3105. }
  3106. }
  3107. return ret;
  3108. }
  3109. /*
  3110. * Add a new cluster for xattr storage.
  3111. *
  3112. * If the new cluster is contiguous with the previous one, it will be
  3113. * appended to the same extent record, and num_clusters will be updated.
  3114. * If not, we will insert a new extent for it and move some xattrs in
  3115. * the last cluster into the new allocated one.
  3116. * We also need to limit the maximum size of a btree leaf, otherwise we'll
  3117. * lose the benefits of hashing because we'll have to search large leaves.
  3118. * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
  3119. * if it's bigger).
  3120. *
  3121. * first_bh is the first block of the previous extent rec and header_bh
  3122. * indicates the bucket we will insert the new xattrs. They will be updated
  3123. * when the header_bh is moved into the new cluster.
  3124. */
  3125. static int ocfs2_add_new_xattr_cluster(struct inode *inode,
  3126. struct buffer_head *root_bh,
  3127. struct buffer_head **first_bh,
  3128. struct buffer_head **header_bh,
  3129. u32 *num_clusters,
  3130. u32 prev_cpos,
  3131. u64 prev_blkno,
  3132. int *extend)
  3133. {
  3134. int ret, credits;
  3135. u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3136. u32 prev_clusters = *num_clusters;
  3137. u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
  3138. u64 block;
  3139. handle_t *handle = NULL;
  3140. struct ocfs2_alloc_context *data_ac = NULL;
  3141. struct ocfs2_alloc_context *meta_ac = NULL;
  3142. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3143. struct ocfs2_xattr_block *xb =
  3144. (struct ocfs2_xattr_block *)root_bh->b_data;
  3145. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3146. struct ocfs2_extent_list *root_el = &xb_root->xt_list;
  3147. enum ocfs2_extent_tree_type type = OCFS2_XATTR_TREE_EXTENT;
  3148. mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
  3149. "previous xattr blkno = %llu\n",
  3150. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3151. prev_cpos, prev_blkno);
  3152. ret = ocfs2_lock_allocators(inode, root_bh, root_el,
  3153. clusters_to_add, 0, &data_ac,
  3154. &meta_ac, type, NULL);
  3155. if (ret) {
  3156. mlog_errno(ret);
  3157. goto leave;
  3158. }
  3159. credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
  3160. handle = ocfs2_start_trans(osb, credits);
  3161. if (IS_ERR(handle)) {
  3162. ret = PTR_ERR(handle);
  3163. handle = NULL;
  3164. mlog_errno(ret);
  3165. goto leave;
  3166. }
  3167. ret = ocfs2_journal_access(handle, inode, root_bh,
  3168. OCFS2_JOURNAL_ACCESS_WRITE);
  3169. if (ret < 0) {
  3170. mlog_errno(ret);
  3171. goto leave;
  3172. }
  3173. ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
  3174. clusters_to_add, &bit_off, &num_bits);
  3175. if (ret < 0) {
  3176. if (ret != -ENOSPC)
  3177. mlog_errno(ret);
  3178. goto leave;
  3179. }
  3180. BUG_ON(num_bits > clusters_to_add);
  3181. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  3182. mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
  3183. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3184. if (prev_blkno + prev_clusters * bpc == block &&
  3185. (prev_clusters + num_bits) << osb->s_clustersize_bits <=
  3186. OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
  3187. /*
  3188. * If this cluster is contiguous with the old one and
  3189. * adding this new cluster, we don't surpass the limit of
  3190. * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
  3191. * initialized and used like other buckets in the previous
  3192. * cluster.
  3193. * So add it as a contiguous one. The caller will handle
  3194. * its init process.
  3195. */
  3196. v_start = prev_cpos + prev_clusters;
  3197. *num_clusters = prev_clusters + num_bits;
  3198. mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
  3199. num_bits);
  3200. } else {
  3201. ret = ocfs2_adjust_xattr_cross_cluster(inode,
  3202. handle,
  3203. first_bh,
  3204. header_bh,
  3205. block,
  3206. prev_blkno,
  3207. prev_clusters,
  3208. &v_start,
  3209. extend);
  3210. if (ret) {
  3211. mlog_errno(ret);
  3212. goto leave;
  3213. }
  3214. }
  3215. mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
  3216. num_bits, block, v_start);
  3217. ret = ocfs2_xattr_tree_insert_extent(osb, handle, inode, root_bh,
  3218. v_start, block, num_bits,
  3219. 0, meta_ac);
  3220. if (ret < 0) {
  3221. mlog_errno(ret);
  3222. goto leave;
  3223. }
  3224. ret = ocfs2_journal_dirty(handle, root_bh);
  3225. if (ret < 0) {
  3226. mlog_errno(ret);
  3227. goto leave;
  3228. }
  3229. leave:
  3230. if (handle)
  3231. ocfs2_commit_trans(osb, handle);
  3232. if (data_ac)
  3233. ocfs2_free_alloc_context(data_ac);
  3234. if (meta_ac)
  3235. ocfs2_free_alloc_context(meta_ac);
  3236. return ret;
  3237. }
  3238. /*
  3239. * Extend a new xattr bucket and move xattrs to the end one by one until
  3240. * We meet with start_bh. Only move half of the xattrs to the bucket after it.
  3241. */
  3242. static int ocfs2_extend_xattr_bucket(struct inode *inode,
  3243. struct buffer_head *first_bh,
  3244. struct buffer_head *start_bh,
  3245. u32 num_clusters)
  3246. {
  3247. int ret, credits;
  3248. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3249. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3250. u64 start_blk = start_bh->b_blocknr, end_blk;
  3251. u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
  3252. handle_t *handle;
  3253. struct ocfs2_xattr_header *first_xh =
  3254. (struct ocfs2_xattr_header *)first_bh->b_data;
  3255. u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
  3256. mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
  3257. "from %llu, len = %u\n", start_blk,
  3258. (unsigned long long)first_bh->b_blocknr, num_clusters);
  3259. BUG_ON(bucket >= num_buckets);
  3260. end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
  3261. /*
  3262. * We will touch all the buckets after the start_bh(include it).
  3263. * Add one more bucket and modify the first_bh.
  3264. */
  3265. credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
  3266. handle = ocfs2_start_trans(osb, credits);
  3267. if (IS_ERR(handle)) {
  3268. ret = PTR_ERR(handle);
  3269. handle = NULL;
  3270. mlog_errno(ret);
  3271. goto out;
  3272. }
  3273. ret = ocfs2_journal_access(handle, inode, first_bh,
  3274. OCFS2_JOURNAL_ACCESS_WRITE);
  3275. if (ret) {
  3276. mlog_errno(ret);
  3277. goto commit;
  3278. }
  3279. while (end_blk != start_blk) {
  3280. ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
  3281. end_blk + blk_per_bucket, 0);
  3282. if (ret)
  3283. goto commit;
  3284. end_blk -= blk_per_bucket;
  3285. }
  3286. /* Move half of the xattr in start_blk to the next bucket. */
  3287. ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
  3288. start_blk + blk_per_bucket, NULL, 0);
  3289. le16_add_cpu(&first_xh->xh_num_buckets, 1);
  3290. ocfs2_journal_dirty(handle, first_bh);
  3291. commit:
  3292. ocfs2_commit_trans(osb, handle);
  3293. out:
  3294. return ret;
  3295. }
  3296. /*
  3297. * Add new xattr bucket in an extent record and adjust the buckets accordingly.
  3298. * xb_bh is the ocfs2_xattr_block.
  3299. * We will move all the buckets starting from header_bh to the next place. As
  3300. * for this one, half num of its xattrs will be moved to the next one.
  3301. *
  3302. * We will allocate a new cluster if current cluster is full and adjust
  3303. * header_bh and first_bh if the insert place is moved to the new cluster.
  3304. */
  3305. static int ocfs2_add_new_xattr_bucket(struct inode *inode,
  3306. struct buffer_head *xb_bh,
  3307. struct buffer_head *header_bh)
  3308. {
  3309. struct ocfs2_xattr_header *first_xh = NULL;
  3310. struct buffer_head *first_bh = NULL;
  3311. struct ocfs2_xattr_block *xb =
  3312. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3313. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3314. struct ocfs2_extent_list *el = &xb_root->xt_list;
  3315. struct ocfs2_xattr_header *xh =
  3316. (struct ocfs2_xattr_header *)header_bh->b_data;
  3317. u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3318. struct super_block *sb = inode->i_sb;
  3319. struct ocfs2_super *osb = OCFS2_SB(sb);
  3320. int ret, num_buckets, extend = 1;
  3321. u64 p_blkno;
  3322. u32 e_cpos, num_clusters;
  3323. mlog(0, "Add new xattr bucket starting form %llu\n",
  3324. (unsigned long long)header_bh->b_blocknr);
  3325. /*
  3326. * Add refrence for header_bh here because it may be
  3327. * changed in ocfs2_add_new_xattr_cluster and we need
  3328. * to free it in the end.
  3329. */
  3330. get_bh(header_bh);
  3331. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
  3332. &num_clusters, el);
  3333. if (ret) {
  3334. mlog_errno(ret);
  3335. goto out;
  3336. }
  3337. ret = ocfs2_read_block(osb, p_blkno,
  3338. &first_bh, OCFS2_BH_CACHED, inode);
  3339. if (ret) {
  3340. mlog_errno(ret);
  3341. goto out;
  3342. }
  3343. num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
  3344. first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
  3345. if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
  3346. ret = ocfs2_add_new_xattr_cluster(inode,
  3347. xb_bh,
  3348. &first_bh,
  3349. &header_bh,
  3350. &num_clusters,
  3351. e_cpos,
  3352. p_blkno,
  3353. &extend);
  3354. if (ret) {
  3355. mlog_errno(ret);
  3356. goto out;
  3357. }
  3358. }
  3359. if (extend)
  3360. ret = ocfs2_extend_xattr_bucket(inode,
  3361. first_bh,
  3362. header_bh,
  3363. num_clusters);
  3364. if (ret)
  3365. mlog_errno(ret);
  3366. out:
  3367. brelse(first_bh);
  3368. brelse(header_bh);
  3369. return ret;
  3370. }
  3371. static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
  3372. struct ocfs2_xattr_bucket *bucket,
  3373. int offs)
  3374. {
  3375. int block_off = offs >> inode->i_sb->s_blocksize_bits;
  3376. offs = offs % inode->i_sb->s_blocksize;
  3377. return bucket->bhs[block_off]->b_data + offs;
  3378. }
  3379. /*
  3380. * Handle the normal xattr set, including replace, delete and new.
  3381. * When the bucket is empty, "is_empty" is set and the caller can
  3382. * free this bucket.
  3383. *
  3384. * Note: "local" indicates the real data's locality. So we can't
  3385. * just its bucket locality by its length.
  3386. */
  3387. static void ocfs2_xattr_set_entry_normal(struct inode *inode,
  3388. struct ocfs2_xattr_info *xi,
  3389. struct ocfs2_xattr_search *xs,
  3390. u32 name_hash,
  3391. int local,
  3392. int *is_empty)
  3393. {
  3394. struct ocfs2_xattr_entry *last, *xe;
  3395. int name_len = strlen(xi->name);
  3396. struct ocfs2_xattr_header *xh = xs->header;
  3397. u16 count = le16_to_cpu(xh->xh_count), start;
  3398. size_t blocksize = inode->i_sb->s_blocksize;
  3399. char *val;
  3400. size_t offs, size, new_size;
  3401. last = &xh->xh_entries[count];
  3402. if (!xs->not_found) {
  3403. xe = xs->here;
  3404. offs = le16_to_cpu(xe->xe_name_offset);
  3405. if (ocfs2_xattr_is_local(xe))
  3406. size = OCFS2_XATTR_SIZE(name_len) +
  3407. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3408. else
  3409. size = OCFS2_XATTR_SIZE(name_len) +
  3410. OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  3411. /*
  3412. * If the new value will be stored outside, xi->value has been
  3413. * initalized as an empty ocfs2_xattr_value_root, and the same
  3414. * goes with xi->value_len, so we can set new_size safely here.
  3415. * See ocfs2_xattr_set_in_bucket.
  3416. */
  3417. new_size = OCFS2_XATTR_SIZE(name_len) +
  3418. OCFS2_XATTR_SIZE(xi->value_len);
  3419. le16_add_cpu(&xh->xh_name_value_len, -size);
  3420. if (xi->value) {
  3421. if (new_size > size)
  3422. goto set_new_name_value;
  3423. /* Now replace the old value with new one. */
  3424. if (local)
  3425. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3426. else
  3427. xe->xe_value_size = 0;
  3428. val = ocfs2_xattr_bucket_get_val(inode,
  3429. &xs->bucket, offs);
  3430. memset(val + OCFS2_XATTR_SIZE(name_len), 0,
  3431. size - OCFS2_XATTR_SIZE(name_len));
  3432. if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
  3433. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  3434. xi->value, xi->value_len);
  3435. le16_add_cpu(&xh->xh_name_value_len, new_size);
  3436. ocfs2_xattr_set_local(xe, local);
  3437. return;
  3438. } else {
  3439. /* Remove the old entry. */
  3440. last -= 1;
  3441. memmove(xe, xe + 1,
  3442. (void *)last - (void *)xe);
  3443. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  3444. le16_add_cpu(&xh->xh_count, -1);
  3445. if (xh->xh_count == 0 && is_empty)
  3446. *is_empty = 1;
  3447. return;
  3448. }
  3449. } else {
  3450. /* find a new entry for insert. */
  3451. int low = 0, high = count - 1, tmp;
  3452. struct ocfs2_xattr_entry *tmp_xe;
  3453. while (low <= high) {
  3454. tmp = (low + high) / 2;
  3455. tmp_xe = &xh->xh_entries[tmp];
  3456. if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
  3457. low = tmp + 1;
  3458. else if (name_hash <
  3459. le32_to_cpu(tmp_xe->xe_name_hash))
  3460. high = tmp - 1;
  3461. else
  3462. break;
  3463. }
  3464. xe = &xh->xh_entries[low];
  3465. if (low != count)
  3466. memmove(xe + 1, xe, (void *)last - (void *)xe);
  3467. le16_add_cpu(&xh->xh_count, 1);
  3468. memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
  3469. xe->xe_name_hash = cpu_to_le32(name_hash);
  3470. xe->xe_name_len = name_len;
  3471. ocfs2_xattr_set_type(xe, xi->name_index);
  3472. }
  3473. set_new_name_value:
  3474. /* Insert the new name+value. */
  3475. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
  3476. /*
  3477. * We must make sure that the name/value pair
  3478. * exists in the same block.
  3479. */
  3480. offs = le16_to_cpu(xh->xh_free_start);
  3481. start = offs - size;
  3482. if (start >> inode->i_sb->s_blocksize_bits !=
  3483. (offs - 1) >> inode->i_sb->s_blocksize_bits) {
  3484. offs = offs - offs % blocksize;
  3485. xh->xh_free_start = cpu_to_le16(offs);
  3486. }
  3487. val = ocfs2_xattr_bucket_get_val(inode,
  3488. &xs->bucket, offs - size);
  3489. xe->xe_name_offset = cpu_to_le16(offs - size);
  3490. memset(val, 0, size);
  3491. memcpy(val, xi->name, name_len);
  3492. memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
  3493. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3494. ocfs2_xattr_set_local(xe, local);
  3495. xs->here = xe;
  3496. le16_add_cpu(&xh->xh_free_start, -size);
  3497. le16_add_cpu(&xh->xh_name_value_len, size);
  3498. return;
  3499. }
  3500. static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
  3501. handle_t *handle,
  3502. struct ocfs2_xattr_search *xs,
  3503. struct buffer_head **bhs,
  3504. u16 bh_num)
  3505. {
  3506. int ret = 0, off, block_off;
  3507. struct ocfs2_xattr_entry *xe = xs->here;
  3508. /*
  3509. * First calculate all the blocks we should journal_access
  3510. * and journal_dirty. The first block should always be touched.
  3511. */
  3512. ret = ocfs2_journal_dirty(handle, bhs[0]);
  3513. if (ret)
  3514. mlog_errno(ret);
  3515. /* calc the data. */
  3516. off = le16_to_cpu(xe->xe_name_offset);
  3517. block_off = off >> inode->i_sb->s_blocksize_bits;
  3518. ret = ocfs2_journal_dirty(handle, bhs[block_off]);
  3519. if (ret)
  3520. mlog_errno(ret);
  3521. return ret;
  3522. }
  3523. /*
  3524. * Set the xattr entry in the specified bucket.
  3525. * The bucket is indicated by xs->bucket and it should have the enough
  3526. * space for the xattr insertion.
  3527. */
  3528. static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
  3529. struct ocfs2_xattr_info *xi,
  3530. struct ocfs2_xattr_search *xs,
  3531. u32 name_hash,
  3532. int local,
  3533. int *bucket_empty)
  3534. {
  3535. int i, ret;
  3536. handle_t *handle = NULL;
  3537. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3538. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3539. mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
  3540. (unsigned long)xi->value_len, xi->name_index,
  3541. (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
  3542. if (!xs->bucket.bhs[1]) {
  3543. ret = ocfs2_read_blocks(osb,
  3544. xs->bucket.bhs[0]->b_blocknr + 1,
  3545. blk_per_bucket - 1, &xs->bucket.bhs[1],
  3546. OCFS2_BH_CACHED, inode);
  3547. if (ret) {
  3548. mlog_errno(ret);
  3549. goto out;
  3550. }
  3551. }
  3552. handle = ocfs2_start_trans(osb, blk_per_bucket);
  3553. if (IS_ERR(handle)) {
  3554. ret = PTR_ERR(handle);
  3555. handle = NULL;
  3556. mlog_errno(ret);
  3557. goto out;
  3558. }
  3559. for (i = 0; i < blk_per_bucket; i++) {
  3560. ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
  3561. OCFS2_JOURNAL_ACCESS_WRITE);
  3562. if (ret < 0) {
  3563. mlog_errno(ret);
  3564. goto out;
  3565. }
  3566. }
  3567. ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash,
  3568. local, bucket_empty);
  3569. /*Only dirty the blocks we have touched in set xattr. */
  3570. ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
  3571. xs->bucket.bhs, blk_per_bucket);
  3572. if (ret)
  3573. mlog_errno(ret);
  3574. out:
  3575. ocfs2_commit_trans(osb, handle);
  3576. return ret;
  3577. }
  3578. static int ocfs2_xattr_value_update_size(struct inode *inode,
  3579. struct buffer_head *xe_bh,
  3580. struct ocfs2_xattr_entry *xe,
  3581. u64 new_size)
  3582. {
  3583. int ret;
  3584. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3585. handle_t *handle = NULL;
  3586. handle = ocfs2_start_trans(osb, 1);
  3587. if (handle == NULL) {
  3588. ret = -ENOMEM;
  3589. mlog_errno(ret);
  3590. goto out;
  3591. }
  3592. ret = ocfs2_journal_access(handle, inode, xe_bh,
  3593. OCFS2_JOURNAL_ACCESS_WRITE);
  3594. if (ret < 0) {
  3595. mlog_errno(ret);
  3596. goto out_commit;
  3597. }
  3598. xe->xe_value_size = cpu_to_le64(new_size);
  3599. ret = ocfs2_journal_dirty(handle, xe_bh);
  3600. if (ret < 0)
  3601. mlog_errno(ret);
  3602. out_commit:
  3603. ocfs2_commit_trans(osb, handle);
  3604. out:
  3605. return ret;
  3606. }
  3607. /*
  3608. * Truncate the specified xe_off entry in xattr bucket.
  3609. * bucket is indicated by header_bh and len is the new length.
  3610. * Both the ocfs2_xattr_value_root and the entry will be updated here.
  3611. *
  3612. * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
  3613. */
  3614. static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
  3615. struct buffer_head *header_bh,
  3616. int xe_off,
  3617. int len)
  3618. {
  3619. int ret, offset;
  3620. u64 value_blk;
  3621. struct buffer_head *value_bh = NULL;
  3622. struct ocfs2_xattr_value_root *xv;
  3623. struct ocfs2_xattr_entry *xe;
  3624. struct ocfs2_xattr_header *xh =
  3625. (struct ocfs2_xattr_header *)header_bh->b_data;
  3626. size_t blocksize = inode->i_sb->s_blocksize;
  3627. xe = &xh->xh_entries[xe_off];
  3628. BUG_ON(!xe || ocfs2_xattr_is_local(xe));
  3629. offset = le16_to_cpu(xe->xe_name_offset) +
  3630. OCFS2_XATTR_SIZE(xe->xe_name_len);
  3631. value_blk = offset / blocksize;
  3632. /* We don't allow ocfs2_xattr_value to be stored in different block. */
  3633. BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
  3634. value_blk += header_bh->b_blocknr;
  3635. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
  3636. &value_bh, OCFS2_BH_CACHED, inode);
  3637. if (ret) {
  3638. mlog_errno(ret);
  3639. goto out;
  3640. }
  3641. xv = (struct ocfs2_xattr_value_root *)
  3642. (value_bh->b_data + offset % blocksize);
  3643. mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
  3644. xe_off, (unsigned long long)header_bh->b_blocknr, len);
  3645. ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
  3646. if (ret) {
  3647. mlog_errno(ret);
  3648. goto out;
  3649. }
  3650. ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
  3651. if (ret) {
  3652. mlog_errno(ret);
  3653. goto out;
  3654. }
  3655. out:
  3656. brelse(value_bh);
  3657. return ret;
  3658. }
  3659. static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
  3660. struct ocfs2_xattr_search *xs,
  3661. int len)
  3662. {
  3663. int ret, offset;
  3664. struct ocfs2_xattr_entry *xe = xs->here;
  3665. struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
  3666. BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
  3667. offset = xe - xh->xh_entries;
  3668. ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
  3669. offset, len);
  3670. if (ret)
  3671. mlog_errno(ret);
  3672. return ret;
  3673. }
  3674. static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
  3675. struct ocfs2_xattr_search *xs,
  3676. char *val,
  3677. int value_len)
  3678. {
  3679. int offset;
  3680. struct ocfs2_xattr_value_root *xv;
  3681. struct ocfs2_xattr_entry *xe = xs->here;
  3682. BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
  3683. offset = le16_to_cpu(xe->xe_name_offset) +
  3684. OCFS2_XATTR_SIZE(xe->xe_name_len);
  3685. xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
  3686. return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
  3687. }
  3688. /*
  3689. * Remove the xattr bucket pointed by bucket_bh.
  3690. * All the buckets after it in the same xattr extent rec will be
  3691. * move forward one by one.
  3692. */
  3693. static int ocfs2_rm_xattr_bucket(struct inode *inode,
  3694. struct buffer_head *first_bh,
  3695. struct ocfs2_xattr_bucket *bucket)
  3696. {
  3697. int ret = 0, credits;
  3698. struct ocfs2_xattr_header *xh =
  3699. (struct ocfs2_xattr_header *)first_bh->b_data;
  3700. u16 bucket_num = le16_to_cpu(xh->xh_num_buckets);
  3701. u64 end, start = bucket->bhs[0]->b_blocknr;
  3702. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3703. handle_t *handle;
  3704. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3705. end = first_bh->b_blocknr + (bucket_num - 1) * blk_per_bucket;
  3706. mlog(0, "rm xattr bucket %llu\n", start);
  3707. /*
  3708. * We need to update the first xattr_header and all the buckets starting
  3709. * from start in this xattr rec.
  3710. *
  3711. * XXX: Should we empty the old last bucket here?
  3712. */
  3713. credits = 1 + end - start;
  3714. handle = ocfs2_start_trans(osb, credits);
  3715. if (IS_ERR(handle)) {
  3716. ret = PTR_ERR(handle);
  3717. mlog_errno(ret);
  3718. return ret;
  3719. }
  3720. ret = ocfs2_journal_access(handle, inode, first_bh,
  3721. OCFS2_JOURNAL_ACCESS_WRITE);
  3722. if (ret) {
  3723. mlog_errno(ret);
  3724. goto out_commit;
  3725. }
  3726. while (start < end) {
  3727. ret = ocfs2_cp_xattr_bucket(inode, handle,
  3728. start + blk_per_bucket,
  3729. start, 0);
  3730. if (ret) {
  3731. mlog_errno(ret);
  3732. goto out_commit;
  3733. }
  3734. start += blk_per_bucket;
  3735. }
  3736. /* update the first_bh. */
  3737. xh->xh_num_buckets = cpu_to_le16(bucket_num - 1);
  3738. ocfs2_journal_dirty(handle, first_bh);
  3739. out_commit:
  3740. ocfs2_commit_trans(osb, handle);
  3741. return ret;
  3742. }
  3743. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  3744. struct buffer_head *root_bh,
  3745. u64 blkno,
  3746. u32 cpos,
  3747. u32 len)
  3748. {
  3749. int ret;
  3750. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3751. struct inode *tl_inode = osb->osb_tl_inode;
  3752. handle_t *handle;
  3753. struct ocfs2_xattr_block *xb =
  3754. (struct ocfs2_xattr_block *)root_bh->b_data;
  3755. struct ocfs2_extent_list *root_el = &xb->xb_attrs.xb_root.xt_list;
  3756. struct ocfs2_alloc_context *meta_ac = NULL;
  3757. struct ocfs2_cached_dealloc_ctxt dealloc;
  3758. ocfs2_init_dealloc_ctxt(&dealloc);
  3759. mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
  3760. cpos, len, (unsigned long long)blkno);
  3761. ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
  3762. ret = ocfs2_lock_allocators(inode, root_bh, root_el,
  3763. 0, 1, NULL, &meta_ac,
  3764. OCFS2_XATTR_TREE_EXTENT, NULL);
  3765. if (ret) {
  3766. mlog_errno(ret);
  3767. return ret;
  3768. }
  3769. mutex_lock(&tl_inode->i_mutex);
  3770. if (ocfs2_truncate_log_needs_flush(osb)) {
  3771. ret = __ocfs2_flush_truncate_log(osb);
  3772. if (ret < 0) {
  3773. mlog_errno(ret);
  3774. goto out;
  3775. }
  3776. }
  3777. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  3778. if (handle == NULL) {
  3779. ret = -ENOMEM;
  3780. mlog_errno(ret);
  3781. goto out;
  3782. }
  3783. ret = ocfs2_journal_access(handle, inode, root_bh,
  3784. OCFS2_JOURNAL_ACCESS_WRITE);
  3785. if (ret) {
  3786. mlog_errno(ret);
  3787. goto out_commit;
  3788. }
  3789. ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
  3790. &dealloc, OCFS2_XATTR_TREE_EXTENT, NULL);
  3791. if (ret) {
  3792. mlog_errno(ret);
  3793. goto out_commit;
  3794. }
  3795. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
  3796. ret = ocfs2_journal_dirty(handle, root_bh);
  3797. if (ret) {
  3798. mlog_errno(ret);
  3799. goto out_commit;
  3800. }
  3801. ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
  3802. if (ret)
  3803. mlog_errno(ret);
  3804. out_commit:
  3805. ocfs2_commit_trans(osb, handle);
  3806. out:
  3807. ocfs2_schedule_truncate_log_flush(osb, 1);
  3808. mutex_unlock(&tl_inode->i_mutex);
  3809. if (meta_ac)
  3810. ocfs2_free_alloc_context(meta_ac);
  3811. ocfs2_run_deallocs(osb, &dealloc);
  3812. return ret;
  3813. }
  3814. /*
  3815. * Free the xattr bucket indicated by xs->bucket and if all the buckets
  3816. * in the clusters is free, free the clusters also.
  3817. */
  3818. static int ocfs2_xattr_bucket_shrink(struct inode *inode,
  3819. struct ocfs2_xattr_info *xi,
  3820. struct ocfs2_xattr_search *xs,
  3821. u32 name_hash)
  3822. {
  3823. int ret;
  3824. u32 e_cpos, num_clusters;
  3825. u64 p_blkno;
  3826. struct buffer_head *first_bh = NULL;
  3827. struct ocfs2_xattr_header *first_xh;
  3828. struct ocfs2_xattr_block *xb =
  3829. (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  3830. BUG_ON(xs->header->xh_count != 0);
  3831. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  3832. &e_cpos, &num_clusters,
  3833. &xb->xb_attrs.xb_root.xt_list);
  3834. if (ret) {
  3835. mlog_errno(ret);
  3836. return ret;
  3837. }
  3838. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
  3839. &first_bh, OCFS2_BH_CACHED, inode);
  3840. if (ret) {
  3841. mlog_errno(ret);
  3842. return ret;
  3843. }
  3844. ret = ocfs2_rm_xattr_bucket(inode, first_bh, &xs->bucket);
  3845. if (ret) {
  3846. mlog_errno(ret);
  3847. goto out;
  3848. }
  3849. first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
  3850. if (first_xh->xh_num_buckets == 0)
  3851. ret = ocfs2_rm_xattr_cluster(inode, xs->xattr_bh,
  3852. p_blkno, e_cpos,
  3853. num_clusters);
  3854. out:
  3855. brelse(first_bh);
  3856. return ret;
  3857. }
  3858. static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
  3859. struct ocfs2_xattr_search *xs)
  3860. {
  3861. handle_t *handle = NULL;
  3862. struct ocfs2_xattr_header *xh = xs->bucket.xh;
  3863. struct ocfs2_xattr_entry *last = &xh->xh_entries[
  3864. le16_to_cpu(xh->xh_count) - 1];
  3865. int ret = 0;
  3866. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
  3867. if (IS_ERR(handle)) {
  3868. ret = PTR_ERR(handle);
  3869. mlog_errno(ret);
  3870. return;
  3871. }
  3872. ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
  3873. OCFS2_JOURNAL_ACCESS_WRITE);
  3874. if (ret) {
  3875. mlog_errno(ret);
  3876. goto out_commit;
  3877. }
  3878. /* Remove the old entry. */
  3879. memmove(xs->here, xs->here + 1,
  3880. (void *)last - (void *)xs->here);
  3881. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  3882. le16_add_cpu(&xh->xh_count, -1);
  3883. ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
  3884. if (ret < 0)
  3885. mlog_errno(ret);
  3886. out_commit:
  3887. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  3888. }
  3889. /*
  3890. * Set the xattr name/value in the bucket specified in xs.
  3891. *
  3892. * As the new value in xi may be stored in the bucket or in an outside cluster,
  3893. * we divide the whole process into 3 steps:
  3894. * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
  3895. * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
  3896. * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
  3897. * 4. If the clusters for the new outside value can't be allocated, we need
  3898. * to free the xattr we allocated in set.
  3899. */
  3900. static int ocfs2_xattr_set_in_bucket(struct inode *inode,
  3901. struct ocfs2_xattr_info *xi,
  3902. struct ocfs2_xattr_search *xs)
  3903. {
  3904. int ret, local = 1, bucket_empty = 0;
  3905. size_t value_len;
  3906. char *val = (char *)xi->value;
  3907. struct ocfs2_xattr_entry *xe = xs->here;
  3908. u32 name_hash = ocfs2_xattr_hash_by_name(inode,
  3909. xi->name_index, xi->name);
  3910. if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
  3911. /*
  3912. * We need to truncate the xattr storage first.
  3913. *
  3914. * If both the old and new value are stored to
  3915. * outside block, we only need to truncate
  3916. * the storage and then set the value outside.
  3917. *
  3918. * If the new value should be stored within block,
  3919. * we should free all the outside block first and
  3920. * the modification to the xattr block will be done
  3921. * by following steps.
  3922. */
  3923. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  3924. value_len = xi->value_len;
  3925. else
  3926. value_len = 0;
  3927. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  3928. value_len);
  3929. if (ret)
  3930. goto out;
  3931. if (value_len)
  3932. goto set_value_outside;
  3933. }
  3934. value_len = xi->value_len;
  3935. /* So we have to handle the inside block change now. */
  3936. if (value_len > OCFS2_XATTR_INLINE_SIZE) {
  3937. /*
  3938. * If the new value will be stored outside of block,
  3939. * initalize a new empty value root and insert it first.
  3940. */
  3941. local = 0;
  3942. xi->value = &def_xv;
  3943. xi->value_len = OCFS2_XATTR_ROOT_SIZE;
  3944. }
  3945. ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash,
  3946. local, &bucket_empty);
  3947. if (ret) {
  3948. mlog_errno(ret);
  3949. goto out;
  3950. }
  3951. if (value_len > OCFS2_XATTR_INLINE_SIZE) {
  3952. /* allocate the space now for the outside block storage. */
  3953. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  3954. value_len);
  3955. if (ret) {
  3956. mlog_errno(ret);
  3957. if (xs->not_found) {
  3958. /*
  3959. * We can't allocate enough clusters for outside
  3960. * storage and we have allocated xattr already,
  3961. * so need to remove it.
  3962. */
  3963. ocfs2_xattr_bucket_remove_xs(inode, xs);
  3964. }
  3965. goto out;
  3966. }
  3967. } else {
  3968. if (bucket_empty)
  3969. ret = ocfs2_xattr_bucket_shrink(inode, xi,
  3970. xs, name_hash);
  3971. goto out;
  3972. }
  3973. set_value_outside:
  3974. ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
  3975. out:
  3976. return ret;
  3977. }
  3978. /* check whether the xattr bucket is filled up with the same hash value. */
  3979. static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
  3980. struct ocfs2_xattr_bucket *bucket)
  3981. {
  3982. struct ocfs2_xattr_header *xh = bucket->xh;
  3983. if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
  3984. xh->xh_entries[0].xe_name_hash) {
  3985. mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
  3986. "hash = %u\n",
  3987. (unsigned long long)bucket->bhs[0]->b_blocknr,
  3988. le32_to_cpu(xh->xh_entries[0].xe_name_hash));
  3989. return -ENOSPC;
  3990. }
  3991. return 0;
  3992. }
  3993. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  3994. struct ocfs2_xattr_info *xi,
  3995. struct ocfs2_xattr_search *xs)
  3996. {
  3997. struct ocfs2_xattr_header *xh;
  3998. struct ocfs2_xattr_entry *xe;
  3999. u16 count, header_size, xh_free_start;
  4000. int i, free, max_free, need, old;
  4001. size_t value_size = 0, name_len = strlen(xi->name);
  4002. size_t blocksize = inode->i_sb->s_blocksize;
  4003. int ret, allocation = 0;
  4004. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4005. mlog_entry("Set xattr %s in xattr index block\n", xi->name);
  4006. try_again:
  4007. xh = xs->header;
  4008. count = le16_to_cpu(xh->xh_count);
  4009. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4010. header_size = sizeof(struct ocfs2_xattr_header) +
  4011. count * sizeof(struct ocfs2_xattr_entry);
  4012. max_free = OCFS2_XATTR_BUCKET_SIZE -
  4013. le16_to_cpu(xh->xh_name_value_len) - header_size;
  4014. mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
  4015. "of %u which exceed block size\n",
  4016. (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
  4017. header_size);
  4018. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  4019. value_size = OCFS2_XATTR_ROOT_SIZE;
  4020. else if (xi->value)
  4021. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  4022. if (xs->not_found)
  4023. need = sizeof(struct ocfs2_xattr_entry) +
  4024. OCFS2_XATTR_SIZE(name_len) + value_size;
  4025. else {
  4026. need = value_size + OCFS2_XATTR_SIZE(name_len);
  4027. /*
  4028. * We only replace the old value if the new length is smaller
  4029. * than the old one. Otherwise we will allocate new space in the
  4030. * bucket to store it.
  4031. */
  4032. xe = xs->here;
  4033. if (ocfs2_xattr_is_local(xe))
  4034. old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  4035. else
  4036. old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  4037. if (old >= value_size)
  4038. need = 0;
  4039. }
  4040. free = xh_free_start - header_size;
  4041. /*
  4042. * We need to make sure the new name/value pair
  4043. * can exist in the same block.
  4044. */
  4045. if (xh_free_start % blocksize < need)
  4046. free -= xh_free_start % blocksize;
  4047. mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
  4048. "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
  4049. " %u\n", xs->not_found,
  4050. (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
  4051. free, need, max_free, le16_to_cpu(xh->xh_free_start),
  4052. le16_to_cpu(xh->xh_name_value_len));
  4053. if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  4054. if (need <= max_free &&
  4055. count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  4056. /*
  4057. * We can create the space by defragment. Since only the
  4058. * name/value will be moved, the xe shouldn't be changed
  4059. * in xs.
  4060. */
  4061. ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
  4062. if (ret) {
  4063. mlog_errno(ret);
  4064. goto out;
  4065. }
  4066. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4067. free = xh_free_start - header_size;
  4068. if (xh_free_start % blocksize < need)
  4069. free -= xh_free_start % blocksize;
  4070. if (free >= need)
  4071. goto xattr_set;
  4072. mlog(0, "Can't get enough space for xattr insert by "
  4073. "defragment. Need %u bytes, but we have %d, so "
  4074. "allocate new bucket for it.\n", need, free);
  4075. }
  4076. /*
  4077. * We have to add new buckets or clusters and one
  4078. * allocation should leave us enough space for insert.
  4079. */
  4080. BUG_ON(allocation);
  4081. /*
  4082. * We do not allow for overlapping ranges between buckets. And
  4083. * the maximum number of collisions we will allow for then is
  4084. * one bucket's worth, so check it here whether we need to
  4085. * add a new bucket for the insert.
  4086. */
  4087. ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
  4088. if (ret) {
  4089. mlog_errno(ret);
  4090. goto out;
  4091. }
  4092. ret = ocfs2_add_new_xattr_bucket(inode,
  4093. xs->xattr_bh,
  4094. xs->bucket.bhs[0]);
  4095. if (ret) {
  4096. mlog_errno(ret);
  4097. goto out;
  4098. }
  4099. for (i = 0; i < blk_per_bucket; i++)
  4100. brelse(xs->bucket.bhs[i]);
  4101. memset(&xs->bucket, 0, sizeof(xs->bucket));
  4102. ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
  4103. xi->name_index,
  4104. xi->name, xs);
  4105. if (ret && ret != -ENODATA)
  4106. goto out;
  4107. xs->not_found = ret;
  4108. allocation = 1;
  4109. goto try_again;
  4110. }
  4111. xattr_set:
  4112. ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
  4113. out:
  4114. mlog_exit(ret);
  4115. return ret;
  4116. }
  4117. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  4118. struct ocfs2_xattr_bucket *bucket,
  4119. void *para)
  4120. {
  4121. int ret = 0;
  4122. struct ocfs2_xattr_header *xh = bucket->xh;
  4123. u16 i;
  4124. struct ocfs2_xattr_entry *xe;
  4125. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4126. xe = &xh->xh_entries[i];
  4127. if (ocfs2_xattr_is_local(xe))
  4128. continue;
  4129. ret = ocfs2_xattr_bucket_value_truncate(inode,
  4130. bucket->bhs[0],
  4131. i, 0);
  4132. if (ret) {
  4133. mlog_errno(ret);
  4134. break;
  4135. }
  4136. }
  4137. return ret;
  4138. }
  4139. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  4140. struct buffer_head *xb_bh)
  4141. {
  4142. struct ocfs2_xattr_block *xb =
  4143. (struct ocfs2_xattr_block *)xb_bh->b_data;
  4144. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  4145. int ret = 0;
  4146. u32 name_hash = UINT_MAX, e_cpos, num_clusters;
  4147. u64 p_blkno;
  4148. if (le16_to_cpu(el->l_next_free_rec) == 0)
  4149. return 0;
  4150. while (name_hash > 0) {
  4151. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  4152. &e_cpos, &num_clusters, el);
  4153. if (ret) {
  4154. mlog_errno(ret);
  4155. goto out;
  4156. }
  4157. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  4158. ocfs2_delete_xattr_in_bucket,
  4159. NULL);
  4160. if (ret) {
  4161. mlog_errno(ret);
  4162. goto out;
  4163. }
  4164. ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
  4165. p_blkno, e_cpos, num_clusters);
  4166. if (ret) {
  4167. mlog_errno(ret);
  4168. break;
  4169. }
  4170. if (e_cpos == 0)
  4171. break;
  4172. name_hash = e_cpos - 1;
  4173. }
  4174. out:
  4175. return ret;
  4176. }