xattr.c 119 KB

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