xattr.c 121 KB

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