xattr.c 122 KB

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