xattr.c 121 KB

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