xattr.c 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045
  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. #include <linux/security.h>
  38. #define MLOG_MASK_PREFIX ML_XATTR
  39. #include <cluster/masklog.h>
  40. #include "ocfs2.h"
  41. #include "alloc.h"
  42. #include "blockcheck.h"
  43. #include "dlmglue.h"
  44. #include "file.h"
  45. #include "symlink.h"
  46. #include "sysfile.h"
  47. #include "inode.h"
  48. #include "journal.h"
  49. #include "ocfs2_fs.h"
  50. #include "suballoc.h"
  51. #include "uptodate.h"
  52. #include "buffer_head_io.h"
  53. #include "super.h"
  54. #include "xattr.h"
  55. #include "refcounttree.h"
  56. struct ocfs2_xattr_def_value_root {
  57. struct ocfs2_xattr_value_root xv;
  58. struct ocfs2_extent_rec er;
  59. };
  60. struct ocfs2_xattr_bucket {
  61. /* The inode these xattrs are associated with */
  62. struct inode *bu_inode;
  63. /* The actual buffers that make up the bucket */
  64. struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  65. /* How many blocks make up one bucket for this filesystem */
  66. int bu_blocks;
  67. };
  68. struct ocfs2_xattr_set_ctxt {
  69. handle_t *handle;
  70. struct ocfs2_alloc_context *meta_ac;
  71. struct ocfs2_alloc_context *data_ac;
  72. struct ocfs2_cached_dealloc_ctxt dealloc;
  73. };
  74. #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
  75. #define OCFS2_XATTR_INLINE_SIZE 80
  76. #define OCFS2_XATTR_HEADER_GAP 4
  77. #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
  78. - sizeof(struct ocfs2_xattr_header) \
  79. - OCFS2_XATTR_HEADER_GAP)
  80. #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
  81. - sizeof(struct ocfs2_xattr_block) \
  82. - sizeof(struct ocfs2_xattr_header) \
  83. - OCFS2_XATTR_HEADER_GAP)
  84. static struct ocfs2_xattr_def_value_root def_xv = {
  85. .xv.xr_list.l_count = cpu_to_le16(1),
  86. };
  87. struct xattr_handler *ocfs2_xattr_handlers[] = {
  88. &ocfs2_xattr_user_handler,
  89. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  90. &ocfs2_xattr_acl_access_handler,
  91. &ocfs2_xattr_acl_default_handler,
  92. #endif
  93. &ocfs2_xattr_trusted_handler,
  94. &ocfs2_xattr_security_handler,
  95. NULL
  96. };
  97. static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
  98. [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
  99. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  100. [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
  101. = &ocfs2_xattr_acl_access_handler,
  102. [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
  103. = &ocfs2_xattr_acl_default_handler,
  104. #endif
  105. [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
  106. [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
  107. };
  108. struct ocfs2_xattr_info {
  109. int name_index;
  110. const char *name;
  111. const void *value;
  112. size_t value_len;
  113. };
  114. struct ocfs2_xattr_search {
  115. struct buffer_head *inode_bh;
  116. /*
  117. * xattr_bh point to the block buffer head which has extended attribute
  118. * when extended attribute in inode, xattr_bh is equal to inode_bh.
  119. */
  120. struct buffer_head *xattr_bh;
  121. struct ocfs2_xattr_header *header;
  122. struct ocfs2_xattr_bucket *bucket;
  123. void *base;
  124. void *end;
  125. struct ocfs2_xattr_entry *here;
  126. int not_found;
  127. };
  128. static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
  129. struct ocfs2_xattr_header *xh,
  130. int index,
  131. int *block_off,
  132. int *new_offset);
  133. static int ocfs2_xattr_block_find(struct inode *inode,
  134. int name_index,
  135. const char *name,
  136. struct ocfs2_xattr_search *xs);
  137. static int ocfs2_xattr_index_block_find(struct inode *inode,
  138. struct buffer_head *root_bh,
  139. int name_index,
  140. const char *name,
  141. struct ocfs2_xattr_search *xs);
  142. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  143. struct buffer_head *blk_bh,
  144. char *buffer,
  145. size_t buffer_size);
  146. static int ocfs2_xattr_create_index_block(struct inode *inode,
  147. struct ocfs2_xattr_search *xs,
  148. struct ocfs2_xattr_set_ctxt *ctxt);
  149. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  150. struct ocfs2_xattr_info *xi,
  151. struct ocfs2_xattr_search *xs,
  152. struct ocfs2_xattr_set_ctxt *ctxt);
  153. typedef int (xattr_tree_rec_func)(struct inode *inode,
  154. struct buffer_head *root_bh,
  155. u64 blkno, u32 cpos, u32 len, void *para);
  156. static int ocfs2_iterate_xattr_index_block(struct inode *inode,
  157. struct buffer_head *root_bh,
  158. xattr_tree_rec_func *rec_func,
  159. void *para);
  160. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  161. struct ocfs2_xattr_bucket *bucket,
  162. void *para);
  163. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  164. struct buffer_head *root_bh,
  165. u64 blkno,
  166. u32 cpos,
  167. u32 len,
  168. void *para);
  169. static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
  170. u64 src_blk, u64 last_blk, u64 to_blk,
  171. unsigned int start_bucket,
  172. u32 *first_hash);
  173. static int ocfs2_prepare_refcount_xattr(struct inode *inode,
  174. struct ocfs2_dinode *di,
  175. struct ocfs2_xattr_info *xi,
  176. struct ocfs2_xattr_search *xis,
  177. struct ocfs2_xattr_search *xbs,
  178. struct ocfs2_refcount_tree **ref_tree,
  179. int *meta_need,
  180. int *credits);
  181. static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
  182. {
  183. return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
  184. }
  185. static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
  186. {
  187. return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
  188. }
  189. static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
  190. {
  191. u16 len = sb->s_blocksize -
  192. offsetof(struct ocfs2_xattr_header, xh_entries);
  193. return len / sizeof(struct ocfs2_xattr_entry);
  194. }
  195. #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
  196. #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
  197. #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
  198. static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
  199. {
  200. struct ocfs2_xattr_bucket *bucket;
  201. int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  202. BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
  203. bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
  204. if (bucket) {
  205. bucket->bu_inode = inode;
  206. bucket->bu_blocks = blks;
  207. }
  208. return bucket;
  209. }
  210. static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
  211. {
  212. int i;
  213. for (i = 0; i < bucket->bu_blocks; i++) {
  214. brelse(bucket->bu_bhs[i]);
  215. bucket->bu_bhs[i] = NULL;
  216. }
  217. }
  218. static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
  219. {
  220. if (bucket) {
  221. ocfs2_xattr_bucket_relse(bucket);
  222. bucket->bu_inode = NULL;
  223. kfree(bucket);
  224. }
  225. }
  226. /*
  227. * A bucket that has never been written to disk doesn't need to be
  228. * read. We just need the buffer_heads. Don't call this for
  229. * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
  230. * them fully.
  231. */
  232. static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
  233. u64 xb_blkno)
  234. {
  235. int i, rc = 0;
  236. for (i = 0; i < bucket->bu_blocks; i++) {
  237. bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
  238. xb_blkno + i);
  239. if (!bucket->bu_bhs[i]) {
  240. rc = -EIO;
  241. mlog_errno(rc);
  242. break;
  243. }
  244. if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
  245. bucket->bu_bhs[i]))
  246. ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
  247. bucket->bu_bhs[i]);
  248. }
  249. if (rc)
  250. ocfs2_xattr_bucket_relse(bucket);
  251. return rc;
  252. }
  253. /* Read the xattr bucket at xb_blkno */
  254. static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
  255. u64 xb_blkno)
  256. {
  257. int rc;
  258. rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
  259. bucket->bu_blocks, bucket->bu_bhs, 0,
  260. NULL);
  261. if (!rc) {
  262. spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  263. rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
  264. bucket->bu_bhs,
  265. bucket->bu_blocks,
  266. &bucket_xh(bucket)->xh_check);
  267. spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  268. if (rc)
  269. mlog_errno(rc);
  270. }
  271. if (rc)
  272. ocfs2_xattr_bucket_relse(bucket);
  273. return rc;
  274. }
  275. static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
  276. struct ocfs2_xattr_bucket *bucket,
  277. int type)
  278. {
  279. int i, rc = 0;
  280. for (i = 0; i < bucket->bu_blocks; i++) {
  281. rc = ocfs2_journal_access(handle,
  282. INODE_CACHE(bucket->bu_inode),
  283. bucket->bu_bhs[i], type);
  284. if (rc) {
  285. mlog_errno(rc);
  286. break;
  287. }
  288. }
  289. return rc;
  290. }
  291. static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
  292. struct ocfs2_xattr_bucket *bucket)
  293. {
  294. int i;
  295. spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  296. ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
  297. bucket->bu_bhs, bucket->bu_blocks,
  298. &bucket_xh(bucket)->xh_check);
  299. spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  300. for (i = 0; i < bucket->bu_blocks; i++)
  301. ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
  302. }
  303. static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
  304. struct ocfs2_xattr_bucket *src)
  305. {
  306. int i;
  307. int blocksize = src->bu_inode->i_sb->s_blocksize;
  308. BUG_ON(dest->bu_blocks != src->bu_blocks);
  309. BUG_ON(dest->bu_inode != src->bu_inode);
  310. for (i = 0; i < src->bu_blocks; i++) {
  311. memcpy(bucket_block(dest, i), bucket_block(src, i),
  312. blocksize);
  313. }
  314. }
  315. static int ocfs2_validate_xattr_block(struct super_block *sb,
  316. struct buffer_head *bh)
  317. {
  318. int rc;
  319. struct ocfs2_xattr_block *xb =
  320. (struct ocfs2_xattr_block *)bh->b_data;
  321. mlog(0, "Validating xattr block %llu\n",
  322. (unsigned long long)bh->b_blocknr);
  323. BUG_ON(!buffer_uptodate(bh));
  324. /*
  325. * If the ecc fails, we return the error but otherwise
  326. * leave the filesystem running. We know any error is
  327. * local to this block.
  328. */
  329. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
  330. if (rc)
  331. return rc;
  332. /*
  333. * Errors after here are fatal
  334. */
  335. if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
  336. ocfs2_error(sb,
  337. "Extended attribute block #%llu has bad "
  338. "signature %.*s",
  339. (unsigned long long)bh->b_blocknr, 7,
  340. xb->xb_signature);
  341. return -EINVAL;
  342. }
  343. if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
  344. ocfs2_error(sb,
  345. "Extended attribute block #%llu has an "
  346. "invalid xb_blkno of %llu",
  347. (unsigned long long)bh->b_blocknr,
  348. (unsigned long long)le64_to_cpu(xb->xb_blkno));
  349. return -EINVAL;
  350. }
  351. if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  352. ocfs2_error(sb,
  353. "Extended attribute block #%llu has an invalid "
  354. "xb_fs_generation of #%u",
  355. (unsigned long long)bh->b_blocknr,
  356. le32_to_cpu(xb->xb_fs_generation));
  357. return -EINVAL;
  358. }
  359. return 0;
  360. }
  361. static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
  362. struct buffer_head **bh)
  363. {
  364. int rc;
  365. struct buffer_head *tmp = *bh;
  366. rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
  367. ocfs2_validate_xattr_block);
  368. /* If ocfs2_read_block() got us a new bh, pass it up. */
  369. if (!rc && !*bh)
  370. *bh = tmp;
  371. return rc;
  372. }
  373. static inline const char *ocfs2_xattr_prefix(int name_index)
  374. {
  375. struct xattr_handler *handler = NULL;
  376. if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
  377. handler = ocfs2_xattr_handler_map[name_index];
  378. return handler ? handler->prefix : NULL;
  379. }
  380. static u32 ocfs2_xattr_name_hash(struct inode *inode,
  381. const char *name,
  382. int name_len)
  383. {
  384. /* Get hash value of uuid from super block */
  385. u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
  386. int i;
  387. /* hash extended attribute name */
  388. for (i = 0; i < name_len; i++) {
  389. hash = (hash << OCFS2_HASH_SHIFT) ^
  390. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  391. *name++;
  392. }
  393. return hash;
  394. }
  395. /*
  396. * ocfs2_xattr_hash_entry()
  397. *
  398. * Compute the hash of an extended attribute.
  399. */
  400. static void ocfs2_xattr_hash_entry(struct inode *inode,
  401. struct ocfs2_xattr_header *header,
  402. struct ocfs2_xattr_entry *entry)
  403. {
  404. u32 hash = 0;
  405. char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
  406. hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
  407. entry->xe_name_hash = cpu_to_le32(hash);
  408. return;
  409. }
  410. static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
  411. {
  412. int size = 0;
  413. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  414. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
  415. else
  416. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  417. size += sizeof(struct ocfs2_xattr_entry);
  418. return size;
  419. }
  420. int ocfs2_calc_security_init(struct inode *dir,
  421. struct ocfs2_security_xattr_info *si,
  422. int *want_clusters,
  423. int *xattr_credits,
  424. struct ocfs2_alloc_context **xattr_ac)
  425. {
  426. int ret = 0;
  427. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  428. int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
  429. si->value_len);
  430. /*
  431. * The max space of security xattr taken inline is
  432. * 256(name) + 80(value) + 16(entry) = 352 bytes,
  433. * So reserve one metadata block for it is ok.
  434. */
  435. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
  436. s_size > OCFS2_XATTR_FREE_IN_IBODY) {
  437. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
  438. if (ret) {
  439. mlog_errno(ret);
  440. return ret;
  441. }
  442. *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  443. }
  444. /* reserve clusters for xattr value which will be set in B tree*/
  445. if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
  446. int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
  447. si->value_len);
  448. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  449. new_clusters);
  450. *want_clusters += new_clusters;
  451. }
  452. return ret;
  453. }
  454. int ocfs2_calc_xattr_init(struct inode *dir,
  455. struct buffer_head *dir_bh,
  456. int mode,
  457. struct ocfs2_security_xattr_info *si,
  458. int *want_clusters,
  459. int *xattr_credits,
  460. int *want_meta)
  461. {
  462. int ret = 0;
  463. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  464. int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
  465. if (si->enable)
  466. s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
  467. si->value_len);
  468. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  469. acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
  470. OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
  471. "", NULL, 0);
  472. if (acl_len > 0) {
  473. a_size = ocfs2_xattr_entry_real_size(0, acl_len);
  474. if (S_ISDIR(mode))
  475. a_size <<= 1;
  476. } else if (acl_len != 0 && acl_len != -ENODATA) {
  477. mlog_errno(ret);
  478. return ret;
  479. }
  480. }
  481. if (!(s_size + a_size))
  482. return ret;
  483. /*
  484. * The max space of security xattr taken inline is
  485. * 256(name) + 80(value) + 16(entry) = 352 bytes,
  486. * The max space of acl xattr taken inline is
  487. * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
  488. * when blocksize = 512, may reserve one more cluser for
  489. * xattr bucket, otherwise reserve one metadata block
  490. * for them is ok.
  491. * If this is a new directory with inline data,
  492. * we choose to reserve the entire inline area for
  493. * directory contents and force an external xattr block.
  494. */
  495. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
  496. (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
  497. (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
  498. *want_meta = *want_meta + 1;
  499. *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  500. }
  501. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
  502. (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
  503. *want_clusters += 1;
  504. *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
  505. }
  506. /*
  507. * reserve credits and clusters for xattrs which has large value
  508. * and have to be set outside
  509. */
  510. if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
  511. new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
  512. si->value_len);
  513. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  514. new_clusters);
  515. *want_clusters += new_clusters;
  516. }
  517. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
  518. acl_len > OCFS2_XATTR_INLINE_SIZE) {
  519. /* for directory, it has DEFAULT and ACCESS two types of acls */
  520. new_clusters = (S_ISDIR(mode) ? 2 : 1) *
  521. ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
  522. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  523. new_clusters);
  524. *want_clusters += new_clusters;
  525. }
  526. return ret;
  527. }
  528. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  529. u32 clusters_to_add,
  530. struct ocfs2_xattr_value_buf *vb,
  531. struct ocfs2_xattr_set_ctxt *ctxt)
  532. {
  533. int status = 0;
  534. handle_t *handle = ctxt->handle;
  535. enum ocfs2_alloc_restarted why;
  536. u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
  537. struct ocfs2_extent_tree et;
  538. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  539. ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
  540. status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  541. OCFS2_JOURNAL_ACCESS_WRITE);
  542. if (status < 0) {
  543. mlog_errno(status);
  544. goto leave;
  545. }
  546. prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
  547. status = ocfs2_add_clusters_in_btree(handle,
  548. &et,
  549. &logical_start,
  550. clusters_to_add,
  551. 0,
  552. ctxt->data_ac,
  553. ctxt->meta_ac,
  554. &why);
  555. if (status < 0) {
  556. mlog_errno(status);
  557. goto leave;
  558. }
  559. status = ocfs2_journal_dirty(handle, vb->vb_bh);
  560. if (status < 0) {
  561. mlog_errno(status);
  562. goto leave;
  563. }
  564. clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
  565. /*
  566. * We should have already allocated enough space before the transaction,
  567. * so no need to restart.
  568. */
  569. BUG_ON(why != RESTART_NONE || clusters_to_add);
  570. leave:
  571. return status;
  572. }
  573. static int __ocfs2_remove_xattr_range(struct inode *inode,
  574. struct ocfs2_xattr_value_buf *vb,
  575. u32 cpos, u32 phys_cpos, u32 len,
  576. unsigned int ext_flags,
  577. struct ocfs2_xattr_set_ctxt *ctxt)
  578. {
  579. int ret;
  580. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  581. handle_t *handle = ctxt->handle;
  582. struct ocfs2_extent_tree et;
  583. ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
  584. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  585. OCFS2_JOURNAL_ACCESS_WRITE);
  586. if (ret) {
  587. mlog_errno(ret);
  588. goto out;
  589. }
  590. ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
  591. &ctxt->dealloc);
  592. if (ret) {
  593. mlog_errno(ret);
  594. goto out;
  595. }
  596. le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
  597. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  598. if (ret) {
  599. mlog_errno(ret);
  600. goto out;
  601. }
  602. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  603. ret = ocfs2_decrease_refcount(inode, handle,
  604. ocfs2_blocks_to_clusters(inode->i_sb,
  605. phys_blkno),
  606. len, ctxt->meta_ac, &ctxt->dealloc, 1);
  607. else
  608. ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
  609. phys_blkno, len);
  610. if (ret)
  611. mlog_errno(ret);
  612. out:
  613. return ret;
  614. }
  615. static int ocfs2_xattr_shrink_size(struct inode *inode,
  616. u32 old_clusters,
  617. u32 new_clusters,
  618. struct ocfs2_xattr_value_buf *vb,
  619. struct ocfs2_xattr_set_ctxt *ctxt)
  620. {
  621. int ret = 0;
  622. unsigned int ext_flags;
  623. u32 trunc_len, cpos, phys_cpos, alloc_size;
  624. u64 block;
  625. if (old_clusters <= new_clusters)
  626. return 0;
  627. cpos = new_clusters;
  628. trunc_len = old_clusters - new_clusters;
  629. while (trunc_len) {
  630. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  631. &alloc_size,
  632. &vb->vb_xv->xr_list, &ext_flags);
  633. if (ret) {
  634. mlog_errno(ret);
  635. goto out;
  636. }
  637. if (alloc_size > trunc_len)
  638. alloc_size = trunc_len;
  639. ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
  640. phys_cpos, alloc_size,
  641. ext_flags, ctxt);
  642. if (ret) {
  643. mlog_errno(ret);
  644. goto out;
  645. }
  646. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  647. ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
  648. block, alloc_size);
  649. cpos += alloc_size;
  650. trunc_len -= alloc_size;
  651. }
  652. out:
  653. return ret;
  654. }
  655. static int ocfs2_xattr_value_truncate(struct inode *inode,
  656. struct ocfs2_xattr_value_buf *vb,
  657. int len,
  658. struct ocfs2_xattr_set_ctxt *ctxt)
  659. {
  660. int ret;
  661. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  662. u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
  663. if (new_clusters == old_clusters)
  664. return 0;
  665. if (new_clusters > old_clusters)
  666. ret = ocfs2_xattr_extend_allocation(inode,
  667. new_clusters - old_clusters,
  668. vb, ctxt);
  669. else
  670. ret = ocfs2_xattr_shrink_size(inode,
  671. old_clusters, new_clusters,
  672. vb, ctxt);
  673. return ret;
  674. }
  675. static int ocfs2_xattr_list_entry(char *buffer, size_t size,
  676. size_t *result, const char *prefix,
  677. const char *name, int name_len)
  678. {
  679. char *p = buffer + *result;
  680. int prefix_len = strlen(prefix);
  681. int total_len = prefix_len + name_len + 1;
  682. *result += total_len;
  683. /* we are just looking for how big our buffer needs to be */
  684. if (!size)
  685. return 0;
  686. if (*result > size)
  687. return -ERANGE;
  688. memcpy(p, prefix, prefix_len);
  689. memcpy(p + prefix_len, name, name_len);
  690. p[prefix_len + name_len] = '\0';
  691. return 0;
  692. }
  693. static int ocfs2_xattr_list_entries(struct inode *inode,
  694. struct ocfs2_xattr_header *header,
  695. char *buffer, size_t buffer_size)
  696. {
  697. size_t result = 0;
  698. int i, type, ret;
  699. const char *prefix, *name;
  700. for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
  701. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  702. type = ocfs2_xattr_get_type(entry);
  703. prefix = ocfs2_xattr_prefix(type);
  704. if (prefix) {
  705. name = (const char *)header +
  706. le16_to_cpu(entry->xe_name_offset);
  707. ret = ocfs2_xattr_list_entry(buffer, buffer_size,
  708. &result, prefix, name,
  709. entry->xe_name_len);
  710. if (ret)
  711. return ret;
  712. }
  713. }
  714. return result;
  715. }
  716. int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
  717. struct ocfs2_dinode *di)
  718. {
  719. struct ocfs2_xattr_header *xh;
  720. int i;
  721. xh = (struct ocfs2_xattr_header *)
  722. ((void *)di + inode->i_sb->s_blocksize -
  723. le16_to_cpu(di->i_xattr_inline_size));
  724. for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
  725. if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
  726. return 1;
  727. return 0;
  728. }
  729. static int ocfs2_xattr_ibody_list(struct inode *inode,
  730. struct ocfs2_dinode *di,
  731. char *buffer,
  732. size_t buffer_size)
  733. {
  734. struct ocfs2_xattr_header *header = NULL;
  735. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  736. int ret = 0;
  737. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  738. return ret;
  739. header = (struct ocfs2_xattr_header *)
  740. ((void *)di + inode->i_sb->s_blocksize -
  741. le16_to_cpu(di->i_xattr_inline_size));
  742. ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
  743. return ret;
  744. }
  745. static int ocfs2_xattr_block_list(struct inode *inode,
  746. struct ocfs2_dinode *di,
  747. char *buffer,
  748. size_t buffer_size)
  749. {
  750. struct buffer_head *blk_bh = NULL;
  751. struct ocfs2_xattr_block *xb;
  752. int ret = 0;
  753. if (!di->i_xattr_loc)
  754. return ret;
  755. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  756. &blk_bh);
  757. if (ret < 0) {
  758. mlog_errno(ret);
  759. return ret;
  760. }
  761. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  762. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  763. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  764. ret = ocfs2_xattr_list_entries(inode, header,
  765. buffer, buffer_size);
  766. } else
  767. ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
  768. buffer, buffer_size);
  769. brelse(blk_bh);
  770. return ret;
  771. }
  772. ssize_t ocfs2_listxattr(struct dentry *dentry,
  773. char *buffer,
  774. size_t size)
  775. {
  776. int ret = 0, i_ret = 0, b_ret = 0;
  777. struct buffer_head *di_bh = NULL;
  778. struct ocfs2_dinode *di = NULL;
  779. struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
  780. if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
  781. return -EOPNOTSUPP;
  782. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  783. return ret;
  784. ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
  785. if (ret < 0) {
  786. mlog_errno(ret);
  787. return ret;
  788. }
  789. di = (struct ocfs2_dinode *)di_bh->b_data;
  790. down_read(&oi->ip_xattr_sem);
  791. i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
  792. if (i_ret < 0)
  793. b_ret = 0;
  794. else {
  795. if (buffer) {
  796. buffer += i_ret;
  797. size -= i_ret;
  798. }
  799. b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
  800. buffer, size);
  801. if (b_ret < 0)
  802. i_ret = 0;
  803. }
  804. up_read(&oi->ip_xattr_sem);
  805. ocfs2_inode_unlock(dentry->d_inode, 0);
  806. brelse(di_bh);
  807. return i_ret + b_ret;
  808. }
  809. static int ocfs2_xattr_find_entry(int name_index,
  810. const char *name,
  811. struct ocfs2_xattr_search *xs)
  812. {
  813. struct ocfs2_xattr_entry *entry;
  814. size_t name_len;
  815. int i, cmp = 1;
  816. if (name == NULL)
  817. return -EINVAL;
  818. name_len = strlen(name);
  819. entry = xs->here;
  820. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  821. cmp = name_index - ocfs2_xattr_get_type(entry);
  822. if (!cmp)
  823. cmp = name_len - entry->xe_name_len;
  824. if (!cmp)
  825. cmp = memcmp(name, (xs->base +
  826. le16_to_cpu(entry->xe_name_offset)),
  827. name_len);
  828. if (cmp == 0)
  829. break;
  830. entry += 1;
  831. }
  832. xs->here = entry;
  833. return cmp ? -ENODATA : 0;
  834. }
  835. static int ocfs2_xattr_get_value_outside(struct inode *inode,
  836. struct ocfs2_xattr_value_root *xv,
  837. void *buffer,
  838. size_t len)
  839. {
  840. u32 cpos, p_cluster, num_clusters, bpc, clusters;
  841. u64 blkno;
  842. int i, ret = 0;
  843. size_t cplen, blocksize;
  844. struct buffer_head *bh = NULL;
  845. struct ocfs2_extent_list *el;
  846. el = &xv->xr_list;
  847. clusters = le32_to_cpu(xv->xr_clusters);
  848. bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  849. blocksize = inode->i_sb->s_blocksize;
  850. cpos = 0;
  851. while (cpos < clusters) {
  852. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  853. &num_clusters, el, NULL);
  854. if (ret) {
  855. mlog_errno(ret);
  856. goto out;
  857. }
  858. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  859. /* Copy ocfs2_xattr_value */
  860. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  861. ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
  862. &bh, NULL);
  863. if (ret) {
  864. mlog_errno(ret);
  865. goto out;
  866. }
  867. cplen = len >= blocksize ? blocksize : len;
  868. memcpy(buffer, bh->b_data, cplen);
  869. len -= cplen;
  870. buffer += cplen;
  871. brelse(bh);
  872. bh = NULL;
  873. if (len == 0)
  874. break;
  875. }
  876. cpos += num_clusters;
  877. }
  878. out:
  879. return ret;
  880. }
  881. static int ocfs2_xattr_ibody_get(struct inode *inode,
  882. int name_index,
  883. const char *name,
  884. void *buffer,
  885. size_t buffer_size,
  886. struct ocfs2_xattr_search *xs)
  887. {
  888. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  889. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  890. struct ocfs2_xattr_value_root *xv;
  891. size_t size;
  892. int ret = 0;
  893. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  894. return -ENODATA;
  895. xs->end = (void *)di + inode->i_sb->s_blocksize;
  896. xs->header = (struct ocfs2_xattr_header *)
  897. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  898. xs->base = (void *)xs->header;
  899. xs->here = xs->header->xh_entries;
  900. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  901. if (ret)
  902. return ret;
  903. size = le64_to_cpu(xs->here->xe_value_size);
  904. if (buffer) {
  905. if (size > buffer_size)
  906. return -ERANGE;
  907. if (ocfs2_xattr_is_local(xs->here)) {
  908. memcpy(buffer, (void *)xs->base +
  909. le16_to_cpu(xs->here->xe_name_offset) +
  910. OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
  911. } else {
  912. xv = (struct ocfs2_xattr_value_root *)
  913. (xs->base + le16_to_cpu(
  914. xs->here->xe_name_offset) +
  915. OCFS2_XATTR_SIZE(xs->here->xe_name_len));
  916. ret = ocfs2_xattr_get_value_outside(inode, xv,
  917. buffer, size);
  918. if (ret < 0) {
  919. mlog_errno(ret);
  920. return ret;
  921. }
  922. }
  923. }
  924. return size;
  925. }
  926. static int ocfs2_xattr_block_get(struct inode *inode,
  927. int name_index,
  928. const char *name,
  929. void *buffer,
  930. size_t buffer_size,
  931. struct ocfs2_xattr_search *xs)
  932. {
  933. struct ocfs2_xattr_block *xb;
  934. struct ocfs2_xattr_value_root *xv;
  935. size_t size;
  936. int ret = -ENODATA, name_offset, name_len, i;
  937. int uninitialized_var(block_off);
  938. xs->bucket = ocfs2_xattr_bucket_new(inode);
  939. if (!xs->bucket) {
  940. ret = -ENOMEM;
  941. mlog_errno(ret);
  942. goto cleanup;
  943. }
  944. ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
  945. if (ret) {
  946. mlog_errno(ret);
  947. goto cleanup;
  948. }
  949. if (xs->not_found) {
  950. ret = -ENODATA;
  951. goto cleanup;
  952. }
  953. xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  954. size = le64_to_cpu(xs->here->xe_value_size);
  955. if (buffer) {
  956. ret = -ERANGE;
  957. if (size > buffer_size)
  958. goto cleanup;
  959. name_offset = le16_to_cpu(xs->here->xe_name_offset);
  960. name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
  961. i = xs->here - xs->header->xh_entries;
  962. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  963. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  964. bucket_xh(xs->bucket),
  965. i,
  966. &block_off,
  967. &name_offset);
  968. xs->base = bucket_block(xs->bucket, block_off);
  969. }
  970. if (ocfs2_xattr_is_local(xs->here)) {
  971. memcpy(buffer, (void *)xs->base +
  972. name_offset + name_len, size);
  973. } else {
  974. xv = (struct ocfs2_xattr_value_root *)
  975. (xs->base + name_offset + name_len);
  976. ret = ocfs2_xattr_get_value_outside(inode, xv,
  977. buffer, size);
  978. if (ret < 0) {
  979. mlog_errno(ret);
  980. goto cleanup;
  981. }
  982. }
  983. }
  984. ret = size;
  985. cleanup:
  986. ocfs2_xattr_bucket_free(xs->bucket);
  987. brelse(xs->xattr_bh);
  988. xs->xattr_bh = NULL;
  989. return ret;
  990. }
  991. int ocfs2_xattr_get_nolock(struct inode *inode,
  992. struct buffer_head *di_bh,
  993. int name_index,
  994. const char *name,
  995. void *buffer,
  996. size_t buffer_size)
  997. {
  998. int ret;
  999. struct ocfs2_dinode *di = NULL;
  1000. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1001. struct ocfs2_xattr_search xis = {
  1002. .not_found = -ENODATA,
  1003. };
  1004. struct ocfs2_xattr_search xbs = {
  1005. .not_found = -ENODATA,
  1006. };
  1007. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1008. return -EOPNOTSUPP;
  1009. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1010. ret = -ENODATA;
  1011. xis.inode_bh = xbs.inode_bh = di_bh;
  1012. di = (struct ocfs2_dinode *)di_bh->b_data;
  1013. down_read(&oi->ip_xattr_sem);
  1014. ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
  1015. buffer_size, &xis);
  1016. if (ret == -ENODATA && di->i_xattr_loc)
  1017. ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
  1018. buffer_size, &xbs);
  1019. up_read(&oi->ip_xattr_sem);
  1020. return ret;
  1021. }
  1022. /* ocfs2_xattr_get()
  1023. *
  1024. * Copy an extended attribute into the buffer provided.
  1025. * Buffer is NULL to compute the size of buffer required.
  1026. */
  1027. static int ocfs2_xattr_get(struct inode *inode,
  1028. int name_index,
  1029. const char *name,
  1030. void *buffer,
  1031. size_t buffer_size)
  1032. {
  1033. int ret;
  1034. struct buffer_head *di_bh = NULL;
  1035. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  1036. if (ret < 0) {
  1037. mlog_errno(ret);
  1038. return ret;
  1039. }
  1040. ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  1041. name, buffer, buffer_size);
  1042. ocfs2_inode_unlock(inode, 0);
  1043. brelse(di_bh);
  1044. return ret;
  1045. }
  1046. static int __ocfs2_xattr_set_value_outside(struct inode *inode,
  1047. handle_t *handle,
  1048. struct ocfs2_xattr_value_buf *vb,
  1049. const void *value,
  1050. int value_len)
  1051. {
  1052. int ret = 0, i, cp_len;
  1053. u16 blocksize = inode->i_sb->s_blocksize;
  1054. u32 p_cluster, num_clusters;
  1055. u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  1056. u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
  1057. u64 blkno;
  1058. struct buffer_head *bh = NULL;
  1059. unsigned int ext_flags;
  1060. struct ocfs2_xattr_value_root *xv = vb->vb_xv;
  1061. BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
  1062. while (cpos < clusters) {
  1063. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  1064. &num_clusters, &xv->xr_list,
  1065. &ext_flags);
  1066. if (ret) {
  1067. mlog_errno(ret);
  1068. goto out;
  1069. }
  1070. BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
  1071. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  1072. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  1073. ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
  1074. &bh, NULL);
  1075. if (ret) {
  1076. mlog_errno(ret);
  1077. goto out;
  1078. }
  1079. ret = ocfs2_journal_access(handle,
  1080. INODE_CACHE(inode),
  1081. bh,
  1082. OCFS2_JOURNAL_ACCESS_WRITE);
  1083. if (ret < 0) {
  1084. mlog_errno(ret);
  1085. goto out;
  1086. }
  1087. cp_len = value_len > blocksize ? blocksize : value_len;
  1088. memcpy(bh->b_data, value, cp_len);
  1089. value_len -= cp_len;
  1090. value += cp_len;
  1091. if (cp_len < blocksize)
  1092. memset(bh->b_data + cp_len, 0,
  1093. blocksize - cp_len);
  1094. ret = ocfs2_journal_dirty(handle, bh);
  1095. if (ret < 0) {
  1096. mlog_errno(ret);
  1097. goto out;
  1098. }
  1099. brelse(bh);
  1100. bh = NULL;
  1101. /*
  1102. * XXX: do we need to empty all the following
  1103. * blocks in this cluster?
  1104. */
  1105. if (!value_len)
  1106. break;
  1107. }
  1108. cpos += num_clusters;
  1109. }
  1110. out:
  1111. brelse(bh);
  1112. return ret;
  1113. }
  1114. static int ocfs2_xattr_cleanup(struct inode *inode,
  1115. handle_t *handle,
  1116. struct ocfs2_xattr_info *xi,
  1117. struct ocfs2_xattr_search *xs,
  1118. struct ocfs2_xattr_value_buf *vb,
  1119. size_t offs)
  1120. {
  1121. int ret = 0;
  1122. size_t name_len = strlen(xi->name);
  1123. void *val = xs->base + offs;
  1124. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1125. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  1126. OCFS2_JOURNAL_ACCESS_WRITE);
  1127. if (ret) {
  1128. mlog_errno(ret);
  1129. goto out;
  1130. }
  1131. /* Decrease xattr count */
  1132. le16_add_cpu(&xs->header->xh_count, -1);
  1133. /* Remove the xattr entry and tree root which has already be set*/
  1134. memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
  1135. memset(val, 0, size);
  1136. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  1137. if (ret < 0)
  1138. mlog_errno(ret);
  1139. out:
  1140. return ret;
  1141. }
  1142. static int ocfs2_xattr_update_entry(struct inode *inode,
  1143. handle_t *handle,
  1144. struct ocfs2_xattr_info *xi,
  1145. struct ocfs2_xattr_search *xs,
  1146. struct ocfs2_xattr_value_buf *vb,
  1147. size_t offs)
  1148. {
  1149. int ret;
  1150. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  1151. OCFS2_JOURNAL_ACCESS_WRITE);
  1152. if (ret) {
  1153. mlog_errno(ret);
  1154. goto out;
  1155. }
  1156. xs->here->xe_name_offset = cpu_to_le16(offs);
  1157. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1158. if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
  1159. ocfs2_xattr_set_local(xs->here, 1);
  1160. else
  1161. ocfs2_xattr_set_local(xs->here, 0);
  1162. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1163. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  1164. if (ret < 0)
  1165. mlog_errno(ret);
  1166. out:
  1167. return ret;
  1168. }
  1169. /*
  1170. * ocfs2_xattr_set_value_outside()
  1171. *
  1172. * Set large size value in B tree.
  1173. */
  1174. static int ocfs2_xattr_set_value_outside(struct inode *inode,
  1175. struct ocfs2_xattr_info *xi,
  1176. struct ocfs2_xattr_search *xs,
  1177. struct ocfs2_xattr_set_ctxt *ctxt,
  1178. struct ocfs2_xattr_value_buf *vb,
  1179. size_t offs)
  1180. {
  1181. size_t name_len = strlen(xi->name);
  1182. void *val = xs->base + offs;
  1183. struct ocfs2_xattr_value_root *xv = NULL;
  1184. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1185. int ret = 0;
  1186. memset(val, 0, size);
  1187. memcpy(val, xi->name, name_len);
  1188. xv = (struct ocfs2_xattr_value_root *)
  1189. (val + OCFS2_XATTR_SIZE(name_len));
  1190. xv->xr_clusters = 0;
  1191. xv->xr_last_eb_blk = 0;
  1192. xv->xr_list.l_tree_depth = 0;
  1193. xv->xr_list.l_count = cpu_to_le16(1);
  1194. xv->xr_list.l_next_free_rec = 0;
  1195. vb->vb_xv = xv;
  1196. ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
  1197. if (ret < 0) {
  1198. mlog_errno(ret);
  1199. return ret;
  1200. }
  1201. ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
  1202. if (ret < 0) {
  1203. mlog_errno(ret);
  1204. return ret;
  1205. }
  1206. ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
  1207. xi->value, xi->value_len);
  1208. if (ret < 0)
  1209. mlog_errno(ret);
  1210. return ret;
  1211. }
  1212. /*
  1213. * ocfs2_xattr_set_entry_local()
  1214. *
  1215. * Set, replace or remove extended attribute in local.
  1216. */
  1217. static void ocfs2_xattr_set_entry_local(struct inode *inode,
  1218. struct ocfs2_xattr_info *xi,
  1219. struct ocfs2_xattr_search *xs,
  1220. struct ocfs2_xattr_entry *last,
  1221. size_t min_offs)
  1222. {
  1223. size_t name_len = strlen(xi->name);
  1224. int i;
  1225. if (xi->value && xs->not_found) {
  1226. /* Insert the new xattr entry. */
  1227. le16_add_cpu(&xs->header->xh_count, 1);
  1228. ocfs2_xattr_set_type(last, xi->name_index);
  1229. ocfs2_xattr_set_local(last, 1);
  1230. last->xe_name_len = name_len;
  1231. } else {
  1232. void *first_val;
  1233. void *val;
  1234. size_t offs, size;
  1235. first_val = xs->base + min_offs;
  1236. offs = le16_to_cpu(xs->here->xe_name_offset);
  1237. val = xs->base + offs;
  1238. if (le64_to_cpu(xs->here->xe_value_size) >
  1239. OCFS2_XATTR_INLINE_SIZE)
  1240. size = OCFS2_XATTR_SIZE(name_len) +
  1241. OCFS2_XATTR_ROOT_SIZE;
  1242. else
  1243. size = OCFS2_XATTR_SIZE(name_len) +
  1244. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1245. if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
  1246. OCFS2_XATTR_SIZE(xi->value_len)) {
  1247. /* The old and the new value have the
  1248. same size. Just replace the value. */
  1249. ocfs2_xattr_set_local(xs->here, 1);
  1250. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1251. /* Clear value bytes. */
  1252. memset(val + OCFS2_XATTR_SIZE(name_len),
  1253. 0,
  1254. OCFS2_XATTR_SIZE(xi->value_len));
  1255. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1256. xi->value,
  1257. xi->value_len);
  1258. return;
  1259. }
  1260. /* Remove the old name+value. */
  1261. memmove(first_val + size, first_val, val - first_val);
  1262. memset(first_val, 0, size);
  1263. xs->here->xe_name_hash = 0;
  1264. xs->here->xe_name_offset = 0;
  1265. ocfs2_xattr_set_local(xs->here, 1);
  1266. xs->here->xe_value_size = 0;
  1267. min_offs += size;
  1268. /* Adjust all value offsets. */
  1269. last = xs->header->xh_entries;
  1270. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1271. size_t o = le16_to_cpu(last->xe_name_offset);
  1272. if (o < offs)
  1273. last->xe_name_offset = cpu_to_le16(o + size);
  1274. last += 1;
  1275. }
  1276. if (!xi->value) {
  1277. /* Remove the old entry. */
  1278. last -= 1;
  1279. memmove(xs->here, xs->here + 1,
  1280. (void *)last - (void *)xs->here);
  1281. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  1282. le16_add_cpu(&xs->header->xh_count, -1);
  1283. }
  1284. }
  1285. if (xi->value) {
  1286. /* Insert the new name+value. */
  1287. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1288. OCFS2_XATTR_SIZE(xi->value_len);
  1289. void *val = xs->base + min_offs - size;
  1290. xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
  1291. memset(val, 0, size);
  1292. memcpy(val, xi->name, name_len);
  1293. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1294. xi->value,
  1295. xi->value_len);
  1296. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1297. ocfs2_xattr_set_local(xs->here, 1);
  1298. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1299. }
  1300. return;
  1301. }
  1302. /*
  1303. * ocfs2_xattr_set_entry()
  1304. *
  1305. * Set extended attribute entry into inode or block.
  1306. *
  1307. * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
  1308. * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
  1309. * then set value in B tree with set_value_outside().
  1310. */
  1311. static int ocfs2_xattr_set_entry(struct inode *inode,
  1312. struct ocfs2_xattr_info *xi,
  1313. struct ocfs2_xattr_search *xs,
  1314. struct ocfs2_xattr_set_ctxt *ctxt,
  1315. int flag)
  1316. {
  1317. struct ocfs2_xattr_entry *last;
  1318. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1319. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1320. size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
  1321. size_t size_l = 0;
  1322. handle_t *handle = ctxt->handle;
  1323. int free, i, ret;
  1324. struct ocfs2_xattr_info xi_l = {
  1325. .name_index = xi->name_index,
  1326. .name = xi->name,
  1327. .value = xi->value,
  1328. .value_len = xi->value_len,
  1329. };
  1330. struct ocfs2_xattr_value_buf vb = {
  1331. .vb_bh = xs->xattr_bh,
  1332. .vb_access = ocfs2_journal_access_di,
  1333. };
  1334. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1335. BUG_ON(xs->xattr_bh == xs->inode_bh);
  1336. vb.vb_access = ocfs2_journal_access_xb;
  1337. } else
  1338. BUG_ON(xs->xattr_bh != xs->inode_bh);
  1339. /* Compute min_offs, last and free space. */
  1340. last = xs->header->xh_entries;
  1341. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1342. size_t offs = le16_to_cpu(last->xe_name_offset);
  1343. if (offs < min_offs)
  1344. min_offs = offs;
  1345. last += 1;
  1346. }
  1347. free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
  1348. if (free < 0)
  1349. return -EIO;
  1350. if (!xs->not_found) {
  1351. size_t size = 0;
  1352. if (ocfs2_xattr_is_local(xs->here))
  1353. size = OCFS2_XATTR_SIZE(name_len) +
  1354. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1355. else
  1356. size = OCFS2_XATTR_SIZE(name_len) +
  1357. OCFS2_XATTR_ROOT_SIZE;
  1358. free += (size + sizeof(struct ocfs2_xattr_entry));
  1359. }
  1360. /* Check free space in inode or block */
  1361. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1362. if (free < sizeof(struct ocfs2_xattr_entry) +
  1363. OCFS2_XATTR_SIZE(name_len) +
  1364. OCFS2_XATTR_ROOT_SIZE) {
  1365. ret = -ENOSPC;
  1366. goto out;
  1367. }
  1368. size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1369. xi_l.value = (void *)&def_xv;
  1370. xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
  1371. } else if (xi->value) {
  1372. if (free < sizeof(struct ocfs2_xattr_entry) +
  1373. OCFS2_XATTR_SIZE(name_len) +
  1374. OCFS2_XATTR_SIZE(xi->value_len)) {
  1375. ret = -ENOSPC;
  1376. goto out;
  1377. }
  1378. }
  1379. if (!xs->not_found) {
  1380. /* For existing extended attribute */
  1381. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1382. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1383. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1384. void *val = xs->base + offs;
  1385. if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
  1386. /* Replace existing local xattr with tree root */
  1387. ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
  1388. ctxt, &vb, offs);
  1389. if (ret < 0)
  1390. mlog_errno(ret);
  1391. goto out;
  1392. } else if (!ocfs2_xattr_is_local(xs->here)) {
  1393. /* For existing xattr which has value outside */
  1394. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  1395. (val + OCFS2_XATTR_SIZE(name_len));
  1396. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1397. /*
  1398. * If new value need set outside also,
  1399. * first truncate old value to new value,
  1400. * then set new value with set_value_outside().
  1401. */
  1402. ret = ocfs2_xattr_value_truncate(inode,
  1403. &vb,
  1404. xi->value_len,
  1405. ctxt);
  1406. if (ret < 0) {
  1407. mlog_errno(ret);
  1408. goto out;
  1409. }
  1410. ret = ocfs2_xattr_update_entry(inode,
  1411. handle,
  1412. xi,
  1413. xs,
  1414. &vb,
  1415. offs);
  1416. if (ret < 0) {
  1417. mlog_errno(ret);
  1418. goto out;
  1419. }
  1420. ret = __ocfs2_xattr_set_value_outside(inode,
  1421. handle,
  1422. &vb,
  1423. xi->value,
  1424. xi->value_len);
  1425. if (ret < 0)
  1426. mlog_errno(ret);
  1427. goto out;
  1428. } else {
  1429. /*
  1430. * If new value need set in local,
  1431. * just trucate old value to zero.
  1432. */
  1433. ret = ocfs2_xattr_value_truncate(inode,
  1434. &vb,
  1435. 0,
  1436. ctxt);
  1437. if (ret < 0)
  1438. mlog_errno(ret);
  1439. }
  1440. }
  1441. }
  1442. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
  1443. OCFS2_JOURNAL_ACCESS_WRITE);
  1444. if (ret) {
  1445. mlog_errno(ret);
  1446. goto out;
  1447. }
  1448. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1449. ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
  1450. OCFS2_JOURNAL_ACCESS_WRITE);
  1451. if (ret) {
  1452. mlog_errno(ret);
  1453. goto out;
  1454. }
  1455. }
  1456. /*
  1457. * Set value in local, include set tree root in local.
  1458. * This is the first step for value size >INLINE_SIZE.
  1459. */
  1460. ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
  1461. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1462. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  1463. if (ret < 0) {
  1464. mlog_errno(ret);
  1465. goto out;
  1466. }
  1467. }
  1468. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
  1469. (flag & OCFS2_INLINE_XATTR_FL)) {
  1470. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1471. unsigned int xattrsize = osb->s_xattr_inline_size;
  1472. /*
  1473. * Adjust extent record count or inline data size
  1474. * to reserve space for extended attribute.
  1475. */
  1476. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1477. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1478. le16_add_cpu(&idata->id_count, -xattrsize);
  1479. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  1480. struct ocfs2_extent_list *el = &di->id2.i_list;
  1481. le16_add_cpu(&el->l_count, -(xattrsize /
  1482. sizeof(struct ocfs2_extent_rec)));
  1483. }
  1484. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  1485. }
  1486. /* Update xattr flag */
  1487. spin_lock(&oi->ip_lock);
  1488. oi->ip_dyn_features |= flag;
  1489. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1490. spin_unlock(&oi->ip_lock);
  1491. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1492. if (ret < 0)
  1493. mlog_errno(ret);
  1494. if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1495. /*
  1496. * Set value outside in B tree.
  1497. * This is the second step for value size > INLINE_SIZE.
  1498. */
  1499. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1500. ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
  1501. &vb, offs);
  1502. if (ret < 0) {
  1503. int ret2;
  1504. mlog_errno(ret);
  1505. /*
  1506. * If set value outside failed, we have to clean
  1507. * the junk tree root we have already set in local.
  1508. */
  1509. ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
  1510. xi, xs, &vb, offs);
  1511. if (ret2 < 0)
  1512. mlog_errno(ret2);
  1513. }
  1514. }
  1515. out:
  1516. return ret;
  1517. }
  1518. static int ocfs2_remove_value_outside(struct inode*inode,
  1519. struct ocfs2_xattr_value_buf *vb,
  1520. struct ocfs2_xattr_header *header)
  1521. {
  1522. int ret = 0, i;
  1523. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1524. struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
  1525. ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
  1526. ctxt.handle = ocfs2_start_trans(osb,
  1527. ocfs2_remove_extent_credits(osb->sb));
  1528. if (IS_ERR(ctxt.handle)) {
  1529. ret = PTR_ERR(ctxt.handle);
  1530. mlog_errno(ret);
  1531. goto out;
  1532. }
  1533. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  1534. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  1535. if (!ocfs2_xattr_is_local(entry)) {
  1536. void *val;
  1537. val = (void *)header +
  1538. le16_to_cpu(entry->xe_name_offset);
  1539. vb->vb_xv = (struct ocfs2_xattr_value_root *)
  1540. (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
  1541. ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
  1542. if (ret < 0) {
  1543. mlog_errno(ret);
  1544. break;
  1545. }
  1546. }
  1547. }
  1548. ocfs2_commit_trans(osb, ctxt.handle);
  1549. ocfs2_schedule_truncate_log_flush(osb, 1);
  1550. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  1551. out:
  1552. return ret;
  1553. }
  1554. static int ocfs2_xattr_ibody_remove(struct inode *inode,
  1555. struct buffer_head *di_bh)
  1556. {
  1557. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1558. struct ocfs2_xattr_header *header;
  1559. int ret;
  1560. struct ocfs2_xattr_value_buf vb = {
  1561. .vb_bh = di_bh,
  1562. .vb_access = ocfs2_journal_access_di,
  1563. };
  1564. header = (struct ocfs2_xattr_header *)
  1565. ((void *)di + inode->i_sb->s_blocksize -
  1566. le16_to_cpu(di->i_xattr_inline_size));
  1567. ret = ocfs2_remove_value_outside(inode, &vb, header);
  1568. return ret;
  1569. }
  1570. static int ocfs2_xattr_block_remove(struct inode *inode,
  1571. struct buffer_head *blk_bh)
  1572. {
  1573. struct ocfs2_xattr_block *xb;
  1574. int ret = 0;
  1575. struct ocfs2_xattr_value_buf vb = {
  1576. .vb_bh = blk_bh,
  1577. .vb_access = ocfs2_journal_access_xb,
  1578. };
  1579. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1580. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1581. struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
  1582. ret = ocfs2_remove_value_outside(inode, &vb, header);
  1583. } else
  1584. ret = ocfs2_iterate_xattr_index_block(inode,
  1585. blk_bh,
  1586. ocfs2_rm_xattr_cluster,
  1587. NULL);
  1588. return ret;
  1589. }
  1590. static int ocfs2_xattr_free_block(struct inode *inode,
  1591. u64 block)
  1592. {
  1593. struct inode *xb_alloc_inode;
  1594. struct buffer_head *xb_alloc_bh = NULL;
  1595. struct buffer_head *blk_bh = NULL;
  1596. struct ocfs2_xattr_block *xb;
  1597. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1598. handle_t *handle;
  1599. int ret = 0;
  1600. u64 blk, bg_blkno;
  1601. u16 bit;
  1602. ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
  1603. if (ret < 0) {
  1604. mlog_errno(ret);
  1605. goto out;
  1606. }
  1607. ret = ocfs2_xattr_block_remove(inode, blk_bh);
  1608. if (ret < 0) {
  1609. mlog_errno(ret);
  1610. goto out;
  1611. }
  1612. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1613. blk = le64_to_cpu(xb->xb_blkno);
  1614. bit = le16_to_cpu(xb->xb_suballoc_bit);
  1615. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1616. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  1617. EXTENT_ALLOC_SYSTEM_INODE,
  1618. le16_to_cpu(xb->xb_suballoc_slot));
  1619. if (!xb_alloc_inode) {
  1620. ret = -ENOMEM;
  1621. mlog_errno(ret);
  1622. goto out;
  1623. }
  1624. mutex_lock(&xb_alloc_inode->i_mutex);
  1625. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  1626. if (ret < 0) {
  1627. mlog_errno(ret);
  1628. goto out_mutex;
  1629. }
  1630. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  1631. if (IS_ERR(handle)) {
  1632. ret = PTR_ERR(handle);
  1633. mlog_errno(ret);
  1634. goto out_unlock;
  1635. }
  1636. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  1637. bit, bg_blkno, 1);
  1638. if (ret < 0)
  1639. mlog_errno(ret);
  1640. ocfs2_commit_trans(osb, handle);
  1641. out_unlock:
  1642. ocfs2_inode_unlock(xb_alloc_inode, 1);
  1643. brelse(xb_alloc_bh);
  1644. out_mutex:
  1645. mutex_unlock(&xb_alloc_inode->i_mutex);
  1646. iput(xb_alloc_inode);
  1647. out:
  1648. brelse(blk_bh);
  1649. return ret;
  1650. }
  1651. /*
  1652. * ocfs2_xattr_remove()
  1653. *
  1654. * Free extended attribute resources associated with this inode.
  1655. */
  1656. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  1657. {
  1658. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1659. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1660. handle_t *handle;
  1661. int ret;
  1662. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1663. return 0;
  1664. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1665. return 0;
  1666. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1667. ret = ocfs2_xattr_ibody_remove(inode, di_bh);
  1668. if (ret < 0) {
  1669. mlog_errno(ret);
  1670. goto out;
  1671. }
  1672. }
  1673. if (di->i_xattr_loc) {
  1674. ret = ocfs2_xattr_free_block(inode,
  1675. le64_to_cpu(di->i_xattr_loc));
  1676. if (ret < 0) {
  1677. mlog_errno(ret);
  1678. goto out;
  1679. }
  1680. }
  1681. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1682. OCFS2_INODE_UPDATE_CREDITS);
  1683. if (IS_ERR(handle)) {
  1684. ret = PTR_ERR(handle);
  1685. mlog_errno(ret);
  1686. goto out;
  1687. }
  1688. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1689. OCFS2_JOURNAL_ACCESS_WRITE);
  1690. if (ret) {
  1691. mlog_errno(ret);
  1692. goto out_commit;
  1693. }
  1694. di->i_xattr_loc = 0;
  1695. spin_lock(&oi->ip_lock);
  1696. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  1697. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1698. spin_unlock(&oi->ip_lock);
  1699. ret = ocfs2_journal_dirty(handle, di_bh);
  1700. if (ret < 0)
  1701. mlog_errno(ret);
  1702. out_commit:
  1703. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1704. out:
  1705. return ret;
  1706. }
  1707. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  1708. struct ocfs2_dinode *di)
  1709. {
  1710. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1711. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  1712. int free;
  1713. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  1714. return 0;
  1715. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1716. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1717. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  1718. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  1719. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  1720. le64_to_cpu(di->i_size);
  1721. } else {
  1722. struct ocfs2_extent_list *el = &di->id2.i_list;
  1723. free = (le16_to_cpu(el->l_count) -
  1724. le16_to_cpu(el->l_next_free_rec)) *
  1725. sizeof(struct ocfs2_extent_rec);
  1726. }
  1727. if (free >= xattrsize)
  1728. return 1;
  1729. return 0;
  1730. }
  1731. /*
  1732. * ocfs2_xattr_ibody_find()
  1733. *
  1734. * Find extended attribute in inode block and
  1735. * fill search info into struct ocfs2_xattr_search.
  1736. */
  1737. static int ocfs2_xattr_ibody_find(struct inode *inode,
  1738. int name_index,
  1739. const char *name,
  1740. struct ocfs2_xattr_search *xs)
  1741. {
  1742. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1743. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1744. int ret;
  1745. int has_space = 0;
  1746. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1747. return 0;
  1748. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1749. down_read(&oi->ip_alloc_sem);
  1750. has_space = ocfs2_xattr_has_space_inline(inode, di);
  1751. up_read(&oi->ip_alloc_sem);
  1752. if (!has_space)
  1753. return 0;
  1754. }
  1755. xs->xattr_bh = xs->inode_bh;
  1756. xs->end = (void *)di + inode->i_sb->s_blocksize;
  1757. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  1758. xs->header = (struct ocfs2_xattr_header *)
  1759. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  1760. else
  1761. xs->header = (struct ocfs2_xattr_header *)
  1762. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  1763. xs->base = (void *)xs->header;
  1764. xs->here = xs->header->xh_entries;
  1765. /* Find the named attribute. */
  1766. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1767. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1768. if (ret && ret != -ENODATA)
  1769. return ret;
  1770. xs->not_found = ret;
  1771. }
  1772. return 0;
  1773. }
  1774. /*
  1775. * ocfs2_xattr_ibody_set()
  1776. *
  1777. * Set, replace or remove an extended attribute into inode block.
  1778. *
  1779. */
  1780. static int ocfs2_xattr_ibody_set(struct inode *inode,
  1781. struct ocfs2_xattr_info *xi,
  1782. struct ocfs2_xattr_search *xs,
  1783. struct ocfs2_xattr_set_ctxt *ctxt)
  1784. {
  1785. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1786. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1787. int ret;
  1788. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1789. return -ENOSPC;
  1790. down_write(&oi->ip_alloc_sem);
  1791. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1792. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  1793. ret = -ENOSPC;
  1794. goto out;
  1795. }
  1796. }
  1797. ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
  1798. (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
  1799. out:
  1800. up_write(&oi->ip_alloc_sem);
  1801. return ret;
  1802. }
  1803. /*
  1804. * ocfs2_xattr_block_find()
  1805. *
  1806. * Find extended attribute in external block and
  1807. * fill search info into struct ocfs2_xattr_search.
  1808. */
  1809. static int ocfs2_xattr_block_find(struct inode *inode,
  1810. int name_index,
  1811. const char *name,
  1812. struct ocfs2_xattr_search *xs)
  1813. {
  1814. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1815. struct buffer_head *blk_bh = NULL;
  1816. struct ocfs2_xattr_block *xb;
  1817. int ret = 0;
  1818. if (!di->i_xattr_loc)
  1819. return ret;
  1820. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  1821. &blk_bh);
  1822. if (ret < 0) {
  1823. mlog_errno(ret);
  1824. return ret;
  1825. }
  1826. xs->xattr_bh = blk_bh;
  1827. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1828. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1829. xs->header = &xb->xb_attrs.xb_header;
  1830. xs->base = (void *)xs->header;
  1831. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  1832. xs->here = xs->header->xh_entries;
  1833. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1834. } else
  1835. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  1836. name_index,
  1837. name, xs);
  1838. if (ret && ret != -ENODATA) {
  1839. xs->xattr_bh = NULL;
  1840. goto cleanup;
  1841. }
  1842. xs->not_found = ret;
  1843. return 0;
  1844. cleanup:
  1845. brelse(blk_bh);
  1846. return ret;
  1847. }
  1848. static int ocfs2_create_xattr_block(handle_t *handle,
  1849. struct inode *inode,
  1850. struct buffer_head *inode_bh,
  1851. struct ocfs2_alloc_context *meta_ac,
  1852. struct buffer_head **ret_bh,
  1853. int indexed)
  1854. {
  1855. int ret;
  1856. u16 suballoc_bit_start;
  1857. u32 num_got;
  1858. u64 first_blkno;
  1859. struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
  1860. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1861. struct buffer_head *new_bh = NULL;
  1862. struct ocfs2_xattr_block *xblk;
  1863. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
  1864. OCFS2_JOURNAL_ACCESS_CREATE);
  1865. if (ret < 0) {
  1866. mlog_errno(ret);
  1867. goto end;
  1868. }
  1869. ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
  1870. &suballoc_bit_start, &num_got,
  1871. &first_blkno);
  1872. if (ret < 0) {
  1873. mlog_errno(ret);
  1874. goto end;
  1875. }
  1876. new_bh = sb_getblk(inode->i_sb, first_blkno);
  1877. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
  1878. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
  1879. new_bh,
  1880. OCFS2_JOURNAL_ACCESS_CREATE);
  1881. if (ret < 0) {
  1882. mlog_errno(ret);
  1883. goto end;
  1884. }
  1885. /* Initialize ocfs2_xattr_block */
  1886. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  1887. memset(xblk, 0, inode->i_sb->s_blocksize);
  1888. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  1889. xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
  1890. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1891. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  1892. xblk->xb_blkno = cpu_to_le64(first_blkno);
  1893. if (indexed) {
  1894. struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
  1895. xr->xt_clusters = cpu_to_le32(1);
  1896. xr->xt_last_eb_blk = 0;
  1897. xr->xt_list.l_tree_depth = 0;
  1898. xr->xt_list.l_count = cpu_to_le16(
  1899. ocfs2_xattr_recs_per_xb(inode->i_sb));
  1900. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  1901. xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
  1902. }
  1903. ret = ocfs2_journal_dirty(handle, new_bh);
  1904. if (ret < 0) {
  1905. mlog_errno(ret);
  1906. goto end;
  1907. }
  1908. di->i_xattr_loc = cpu_to_le64(first_blkno);
  1909. ocfs2_journal_dirty(handle, inode_bh);
  1910. *ret_bh = new_bh;
  1911. new_bh = NULL;
  1912. end:
  1913. brelse(new_bh);
  1914. return ret;
  1915. }
  1916. /*
  1917. * ocfs2_xattr_block_set()
  1918. *
  1919. * Set, replace or remove an extended attribute into external block.
  1920. *
  1921. */
  1922. static int ocfs2_xattr_block_set(struct inode *inode,
  1923. struct ocfs2_xattr_info *xi,
  1924. struct ocfs2_xattr_search *xs,
  1925. struct ocfs2_xattr_set_ctxt *ctxt)
  1926. {
  1927. struct buffer_head *new_bh = NULL;
  1928. handle_t *handle = ctxt->handle;
  1929. struct ocfs2_xattr_block *xblk = NULL;
  1930. int ret;
  1931. if (!xs->xattr_bh) {
  1932. ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
  1933. ctxt->meta_ac, &new_bh, 0);
  1934. if (ret) {
  1935. mlog_errno(ret);
  1936. goto end;
  1937. }
  1938. xs->xattr_bh = new_bh;
  1939. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1940. xs->header = &xblk->xb_attrs.xb_header;
  1941. xs->base = (void *)xs->header;
  1942. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  1943. xs->here = xs->header->xh_entries;
  1944. } else
  1945. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1946. if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1947. /* Set extended attribute into external block */
  1948. ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
  1949. OCFS2_HAS_XATTR_FL);
  1950. if (!ret || ret != -ENOSPC)
  1951. goto end;
  1952. ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
  1953. if (ret)
  1954. goto end;
  1955. }
  1956. ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
  1957. end:
  1958. return ret;
  1959. }
  1960. /* Check whether the new xattr can be inserted into the inode. */
  1961. static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
  1962. struct ocfs2_xattr_info *xi,
  1963. struct ocfs2_xattr_search *xs)
  1964. {
  1965. u64 value_size;
  1966. struct ocfs2_xattr_entry *last;
  1967. int free, i;
  1968. size_t min_offs = xs->end - xs->base;
  1969. if (!xs->header)
  1970. return 0;
  1971. last = xs->header->xh_entries;
  1972. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  1973. size_t offs = le16_to_cpu(last->xe_name_offset);
  1974. if (offs < min_offs)
  1975. min_offs = offs;
  1976. last += 1;
  1977. }
  1978. free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
  1979. if (free < 0)
  1980. return 0;
  1981. BUG_ON(!xs->not_found);
  1982. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  1983. value_size = OCFS2_XATTR_ROOT_SIZE;
  1984. else
  1985. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  1986. if (free >= sizeof(struct ocfs2_xattr_entry) +
  1987. OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
  1988. return 1;
  1989. return 0;
  1990. }
  1991. static int ocfs2_calc_xattr_set_need(struct inode *inode,
  1992. struct ocfs2_dinode *di,
  1993. struct ocfs2_xattr_info *xi,
  1994. struct ocfs2_xattr_search *xis,
  1995. struct ocfs2_xattr_search *xbs,
  1996. int *clusters_need,
  1997. int *meta_need,
  1998. int *credits_need)
  1999. {
  2000. int ret = 0, old_in_xb = 0;
  2001. int clusters_add = 0, meta_add = 0, credits = 0;
  2002. struct buffer_head *bh = NULL;
  2003. struct ocfs2_xattr_block *xb = NULL;
  2004. struct ocfs2_xattr_entry *xe = NULL;
  2005. struct ocfs2_xattr_value_root *xv = NULL;
  2006. char *base = NULL;
  2007. int name_offset, name_len = 0;
  2008. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  2009. xi->value_len);
  2010. u64 value_size;
  2011. /*
  2012. * Calculate the clusters we need to write.
  2013. * No matter whether we replace an old one or add a new one,
  2014. * we need this for writing.
  2015. */
  2016. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  2017. credits += new_clusters *
  2018. ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2019. if (xis->not_found && xbs->not_found) {
  2020. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2021. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  2022. clusters_add += new_clusters;
  2023. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2024. &def_xv.xv.xr_list,
  2025. new_clusters);
  2026. }
  2027. goto meta_guess;
  2028. }
  2029. if (!xis->not_found) {
  2030. xe = xis->here;
  2031. name_offset = le16_to_cpu(xe->xe_name_offset);
  2032. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2033. base = xis->base;
  2034. credits += OCFS2_INODE_UPDATE_CREDITS;
  2035. } else {
  2036. int i, block_off = 0;
  2037. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  2038. xe = xbs->here;
  2039. name_offset = le16_to_cpu(xe->xe_name_offset);
  2040. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2041. i = xbs->here - xbs->header->xh_entries;
  2042. old_in_xb = 1;
  2043. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  2044. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  2045. bucket_xh(xbs->bucket),
  2046. i, &block_off,
  2047. &name_offset);
  2048. base = bucket_block(xbs->bucket, block_off);
  2049. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2050. } else {
  2051. base = xbs->base;
  2052. credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
  2053. }
  2054. }
  2055. /*
  2056. * delete a xattr doesn't need metadata and cluster allocation.
  2057. * so just calculate the credits and return.
  2058. *
  2059. * The credits for removing the value tree will be extended
  2060. * by ocfs2_remove_extent itself.
  2061. */
  2062. if (!xi->value) {
  2063. if (!ocfs2_xattr_is_local(xe))
  2064. credits += ocfs2_remove_extent_credits(inode->i_sb);
  2065. goto out;
  2066. }
  2067. /* do cluster allocation guess first. */
  2068. value_size = le64_to_cpu(xe->xe_value_size);
  2069. if (old_in_xb) {
  2070. /*
  2071. * In xattr set, we always try to set the xe in inode first,
  2072. * so if it can be inserted into inode successfully, the old
  2073. * one will be removed from the xattr block, and this xattr
  2074. * will be inserted into inode as a new xattr in inode.
  2075. */
  2076. if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
  2077. clusters_add += new_clusters;
  2078. credits += ocfs2_remove_extent_credits(inode->i_sb) +
  2079. OCFS2_INODE_UPDATE_CREDITS;
  2080. if (!ocfs2_xattr_is_local(xe))
  2081. credits += ocfs2_calc_extend_credits(
  2082. inode->i_sb,
  2083. &def_xv.xv.xr_list,
  2084. new_clusters);
  2085. goto out;
  2086. }
  2087. }
  2088. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  2089. /* the new values will be stored outside. */
  2090. u32 old_clusters = 0;
  2091. if (!ocfs2_xattr_is_local(xe)) {
  2092. old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  2093. value_size);
  2094. xv = (struct ocfs2_xattr_value_root *)
  2095. (base + name_offset + name_len);
  2096. value_size = OCFS2_XATTR_ROOT_SIZE;
  2097. } else
  2098. xv = &def_xv.xv;
  2099. if (old_clusters >= new_clusters) {
  2100. credits += ocfs2_remove_extent_credits(inode->i_sb);
  2101. goto out;
  2102. } else {
  2103. meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
  2104. clusters_add += new_clusters - old_clusters;
  2105. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2106. &xv->xr_list,
  2107. new_clusters -
  2108. old_clusters);
  2109. if (value_size >= OCFS2_XATTR_ROOT_SIZE)
  2110. goto out;
  2111. }
  2112. } else {
  2113. /*
  2114. * Now the new value will be stored inside. So if the new
  2115. * value is smaller than the size of value root or the old
  2116. * value, we don't need any allocation, otherwise we have
  2117. * to guess metadata allocation.
  2118. */
  2119. if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
  2120. (!ocfs2_xattr_is_local(xe) &&
  2121. OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
  2122. goto out;
  2123. }
  2124. meta_guess:
  2125. /* calculate metadata allocation. */
  2126. if (di->i_xattr_loc) {
  2127. if (!xbs->xattr_bh) {
  2128. ret = ocfs2_read_xattr_block(inode,
  2129. le64_to_cpu(di->i_xattr_loc),
  2130. &bh);
  2131. if (ret) {
  2132. mlog_errno(ret);
  2133. goto out;
  2134. }
  2135. xb = (struct ocfs2_xattr_block *)bh->b_data;
  2136. } else
  2137. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  2138. /*
  2139. * If there is already an xattr tree, good, we can calculate
  2140. * like other b-trees. Otherwise we may have the chance of
  2141. * create a tree, the credit calculation is borrowed from
  2142. * ocfs2_calc_extend_credits with root_el = NULL. And the
  2143. * new tree will be cluster based, so no meta is needed.
  2144. */
  2145. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  2146. struct ocfs2_extent_list *el =
  2147. &xb->xb_attrs.xb_root.xt_list;
  2148. meta_add += ocfs2_extend_meta_needed(el);
  2149. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2150. el, 1);
  2151. } else
  2152. credits += OCFS2_SUBALLOC_ALLOC + 1;
  2153. /*
  2154. * This cluster will be used either for new bucket or for
  2155. * new xattr block.
  2156. * If the cluster size is the same as the bucket size, one
  2157. * more is needed since we may need to extend the bucket
  2158. * also.
  2159. */
  2160. clusters_add += 1;
  2161. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2162. if (OCFS2_XATTR_BUCKET_SIZE ==
  2163. OCFS2_SB(inode->i_sb)->s_clustersize) {
  2164. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2165. clusters_add += 1;
  2166. }
  2167. } else {
  2168. meta_add += 1;
  2169. credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  2170. }
  2171. out:
  2172. if (clusters_need)
  2173. *clusters_need = clusters_add;
  2174. if (meta_need)
  2175. *meta_need = meta_add;
  2176. if (credits_need)
  2177. *credits_need = credits;
  2178. brelse(bh);
  2179. return ret;
  2180. }
  2181. static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
  2182. struct ocfs2_dinode *di,
  2183. struct ocfs2_xattr_info *xi,
  2184. struct ocfs2_xattr_search *xis,
  2185. struct ocfs2_xattr_search *xbs,
  2186. struct ocfs2_xattr_set_ctxt *ctxt,
  2187. int extra_meta,
  2188. int *credits)
  2189. {
  2190. int clusters_add, meta_add, ret;
  2191. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2192. memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
  2193. ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
  2194. ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
  2195. &clusters_add, &meta_add, credits);
  2196. if (ret) {
  2197. mlog_errno(ret);
  2198. return ret;
  2199. }
  2200. meta_add += extra_meta;
  2201. mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
  2202. "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
  2203. if (meta_add) {
  2204. ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
  2205. &ctxt->meta_ac);
  2206. if (ret) {
  2207. mlog_errno(ret);
  2208. goto out;
  2209. }
  2210. }
  2211. if (clusters_add) {
  2212. ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
  2213. if (ret)
  2214. mlog_errno(ret);
  2215. }
  2216. out:
  2217. if (ret) {
  2218. if (ctxt->meta_ac) {
  2219. ocfs2_free_alloc_context(ctxt->meta_ac);
  2220. ctxt->meta_ac = NULL;
  2221. }
  2222. /*
  2223. * We cannot have an error and a non null ctxt->data_ac.
  2224. */
  2225. }
  2226. return ret;
  2227. }
  2228. static int __ocfs2_xattr_set_handle(struct inode *inode,
  2229. struct ocfs2_dinode *di,
  2230. struct ocfs2_xattr_info *xi,
  2231. struct ocfs2_xattr_search *xis,
  2232. struct ocfs2_xattr_search *xbs,
  2233. struct ocfs2_xattr_set_ctxt *ctxt)
  2234. {
  2235. int ret = 0, credits, old_found;
  2236. if (!xi->value) {
  2237. /* Remove existing extended attribute */
  2238. if (!xis->not_found)
  2239. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2240. else if (!xbs->not_found)
  2241. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2242. } else {
  2243. /* We always try to set extended attribute into inode first*/
  2244. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2245. if (!ret && !xbs->not_found) {
  2246. /*
  2247. * If succeed and that extended attribute existing in
  2248. * external block, then we will remove it.
  2249. */
  2250. xi->value = NULL;
  2251. xi->value_len = 0;
  2252. old_found = xis->not_found;
  2253. xis->not_found = -ENODATA;
  2254. ret = ocfs2_calc_xattr_set_need(inode,
  2255. di,
  2256. xi,
  2257. xis,
  2258. xbs,
  2259. NULL,
  2260. NULL,
  2261. &credits);
  2262. xis->not_found = old_found;
  2263. if (ret) {
  2264. mlog_errno(ret);
  2265. goto out;
  2266. }
  2267. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2268. ctxt->handle->h_buffer_credits);
  2269. if (ret) {
  2270. mlog_errno(ret);
  2271. goto out;
  2272. }
  2273. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2274. } else if (ret == -ENOSPC) {
  2275. if (di->i_xattr_loc && !xbs->xattr_bh) {
  2276. ret = ocfs2_xattr_block_find(inode,
  2277. xi->name_index,
  2278. xi->name, xbs);
  2279. if (ret)
  2280. goto out;
  2281. old_found = xis->not_found;
  2282. xis->not_found = -ENODATA;
  2283. ret = ocfs2_calc_xattr_set_need(inode,
  2284. di,
  2285. xi,
  2286. xis,
  2287. xbs,
  2288. NULL,
  2289. NULL,
  2290. &credits);
  2291. xis->not_found = old_found;
  2292. if (ret) {
  2293. mlog_errno(ret);
  2294. goto out;
  2295. }
  2296. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2297. ctxt->handle->h_buffer_credits);
  2298. if (ret) {
  2299. mlog_errno(ret);
  2300. goto out;
  2301. }
  2302. }
  2303. /*
  2304. * If no space in inode, we will set extended attribute
  2305. * into external block.
  2306. */
  2307. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2308. if (ret)
  2309. goto out;
  2310. if (!xis->not_found) {
  2311. /*
  2312. * If succeed and that extended attribute
  2313. * existing in inode, we will remove it.
  2314. */
  2315. xi->value = NULL;
  2316. xi->value_len = 0;
  2317. xbs->not_found = -ENODATA;
  2318. ret = ocfs2_calc_xattr_set_need(inode,
  2319. di,
  2320. xi,
  2321. xis,
  2322. xbs,
  2323. NULL,
  2324. NULL,
  2325. &credits);
  2326. if (ret) {
  2327. mlog_errno(ret);
  2328. goto out;
  2329. }
  2330. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2331. ctxt->handle->h_buffer_credits);
  2332. if (ret) {
  2333. mlog_errno(ret);
  2334. goto out;
  2335. }
  2336. ret = ocfs2_xattr_ibody_set(inode, xi,
  2337. xis, ctxt);
  2338. }
  2339. }
  2340. }
  2341. if (!ret) {
  2342. /* Update inode ctime. */
  2343. ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
  2344. xis->inode_bh,
  2345. OCFS2_JOURNAL_ACCESS_WRITE);
  2346. if (ret) {
  2347. mlog_errno(ret);
  2348. goto out;
  2349. }
  2350. inode->i_ctime = CURRENT_TIME;
  2351. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  2352. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  2353. ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
  2354. }
  2355. out:
  2356. return ret;
  2357. }
  2358. /*
  2359. * This function only called duing creating inode
  2360. * for init security/acl xattrs of the new inode.
  2361. * All transanction credits have been reserved in mknod.
  2362. */
  2363. int ocfs2_xattr_set_handle(handle_t *handle,
  2364. struct inode *inode,
  2365. struct buffer_head *di_bh,
  2366. int name_index,
  2367. const char *name,
  2368. const void *value,
  2369. size_t value_len,
  2370. int flags,
  2371. struct ocfs2_alloc_context *meta_ac,
  2372. struct ocfs2_alloc_context *data_ac)
  2373. {
  2374. struct ocfs2_dinode *di;
  2375. int ret;
  2376. struct ocfs2_xattr_info xi = {
  2377. .name_index = name_index,
  2378. .name = name,
  2379. .value = value,
  2380. .value_len = value_len,
  2381. };
  2382. struct ocfs2_xattr_search xis = {
  2383. .not_found = -ENODATA,
  2384. };
  2385. struct ocfs2_xattr_search xbs = {
  2386. .not_found = -ENODATA,
  2387. };
  2388. struct ocfs2_xattr_set_ctxt ctxt = {
  2389. .handle = handle,
  2390. .meta_ac = meta_ac,
  2391. .data_ac = data_ac,
  2392. };
  2393. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2394. return -EOPNOTSUPP;
  2395. /*
  2396. * In extreme situation, may need xattr bucket when
  2397. * block size is too small. And we have already reserved
  2398. * the credits for bucket in mknod.
  2399. */
  2400. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
  2401. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  2402. if (!xbs.bucket) {
  2403. mlog_errno(-ENOMEM);
  2404. return -ENOMEM;
  2405. }
  2406. }
  2407. xis.inode_bh = xbs.inode_bh = di_bh;
  2408. di = (struct ocfs2_dinode *)di_bh->b_data;
  2409. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  2410. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  2411. if (ret)
  2412. goto cleanup;
  2413. if (xis.not_found) {
  2414. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  2415. if (ret)
  2416. goto cleanup;
  2417. }
  2418. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  2419. cleanup:
  2420. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  2421. brelse(xbs.xattr_bh);
  2422. ocfs2_xattr_bucket_free(xbs.bucket);
  2423. return ret;
  2424. }
  2425. /*
  2426. * ocfs2_xattr_set()
  2427. *
  2428. * Set, replace or remove an extended attribute for this inode.
  2429. * value is NULL to remove an existing extended attribute, else either
  2430. * create or replace an extended attribute.
  2431. */
  2432. int ocfs2_xattr_set(struct inode *inode,
  2433. int name_index,
  2434. const char *name,
  2435. const void *value,
  2436. size_t value_len,
  2437. int flags)
  2438. {
  2439. struct buffer_head *di_bh = NULL;
  2440. struct ocfs2_dinode *di;
  2441. int ret, credits, ref_meta = 0, ref_credits = 0;
  2442. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2443. struct inode *tl_inode = osb->osb_tl_inode;
  2444. struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
  2445. struct ocfs2_refcount_tree *ref_tree = NULL;
  2446. struct ocfs2_xattr_info xi = {
  2447. .name_index = name_index,
  2448. .name = name,
  2449. .value = value,
  2450. .value_len = value_len,
  2451. };
  2452. struct ocfs2_xattr_search xis = {
  2453. .not_found = -ENODATA,
  2454. };
  2455. struct ocfs2_xattr_search xbs = {
  2456. .not_found = -ENODATA,
  2457. };
  2458. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2459. return -EOPNOTSUPP;
  2460. /*
  2461. * Only xbs will be used on indexed trees. xis doesn't need a
  2462. * bucket.
  2463. */
  2464. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  2465. if (!xbs.bucket) {
  2466. mlog_errno(-ENOMEM);
  2467. return -ENOMEM;
  2468. }
  2469. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  2470. if (ret < 0) {
  2471. mlog_errno(ret);
  2472. goto cleanup_nolock;
  2473. }
  2474. xis.inode_bh = xbs.inode_bh = di_bh;
  2475. di = (struct ocfs2_dinode *)di_bh->b_data;
  2476. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  2477. /*
  2478. * Scan inode and external block to find the same name
  2479. * extended attribute and collect search infomation.
  2480. */
  2481. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  2482. if (ret)
  2483. goto cleanup;
  2484. if (xis.not_found) {
  2485. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  2486. if (ret)
  2487. goto cleanup;
  2488. }
  2489. if (xis.not_found && xbs.not_found) {
  2490. ret = -ENODATA;
  2491. if (flags & XATTR_REPLACE)
  2492. goto cleanup;
  2493. ret = 0;
  2494. if (!value)
  2495. goto cleanup;
  2496. } else {
  2497. ret = -EEXIST;
  2498. if (flags & XATTR_CREATE)
  2499. goto cleanup;
  2500. }
  2501. /* Check whether the value is refcounted and do some prepartion. */
  2502. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
  2503. (!xis.not_found || !xbs.not_found)) {
  2504. ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
  2505. &xis, &xbs, &ref_tree,
  2506. &ref_meta, &ref_credits);
  2507. if (ret) {
  2508. mlog_errno(ret);
  2509. goto cleanup;
  2510. }
  2511. }
  2512. mutex_lock(&tl_inode->i_mutex);
  2513. if (ocfs2_truncate_log_needs_flush(osb)) {
  2514. ret = __ocfs2_flush_truncate_log(osb);
  2515. if (ret < 0) {
  2516. mutex_unlock(&tl_inode->i_mutex);
  2517. mlog_errno(ret);
  2518. goto cleanup;
  2519. }
  2520. }
  2521. mutex_unlock(&tl_inode->i_mutex);
  2522. ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
  2523. &xbs, &ctxt, ref_meta, &credits);
  2524. if (ret) {
  2525. mlog_errno(ret);
  2526. goto cleanup;
  2527. }
  2528. /* we need to update inode's ctime field, so add credit for it. */
  2529. credits += OCFS2_INODE_UPDATE_CREDITS;
  2530. ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
  2531. if (IS_ERR(ctxt.handle)) {
  2532. ret = PTR_ERR(ctxt.handle);
  2533. mlog_errno(ret);
  2534. goto cleanup;
  2535. }
  2536. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  2537. ocfs2_commit_trans(osb, ctxt.handle);
  2538. if (ctxt.data_ac)
  2539. ocfs2_free_alloc_context(ctxt.data_ac);
  2540. if (ctxt.meta_ac)
  2541. ocfs2_free_alloc_context(ctxt.meta_ac);
  2542. if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
  2543. ocfs2_schedule_truncate_log_flush(osb, 1);
  2544. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  2545. cleanup:
  2546. if (ref_tree)
  2547. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  2548. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  2549. if (!value && !ret) {
  2550. ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
  2551. if (ret)
  2552. mlog_errno(ret);
  2553. }
  2554. ocfs2_inode_unlock(inode, 1);
  2555. cleanup_nolock:
  2556. brelse(di_bh);
  2557. brelse(xbs.xattr_bh);
  2558. ocfs2_xattr_bucket_free(xbs.bucket);
  2559. return ret;
  2560. }
  2561. /*
  2562. * Find the xattr extent rec which may contains name_hash.
  2563. * e_cpos will be the first name hash of the xattr rec.
  2564. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  2565. */
  2566. static int ocfs2_xattr_get_rec(struct inode *inode,
  2567. u32 name_hash,
  2568. u64 *p_blkno,
  2569. u32 *e_cpos,
  2570. u32 *num_clusters,
  2571. struct ocfs2_extent_list *el)
  2572. {
  2573. int ret = 0, i;
  2574. struct buffer_head *eb_bh = NULL;
  2575. struct ocfs2_extent_block *eb;
  2576. struct ocfs2_extent_rec *rec = NULL;
  2577. u64 e_blkno = 0;
  2578. if (el->l_tree_depth) {
  2579. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
  2580. &eb_bh);
  2581. if (ret) {
  2582. mlog_errno(ret);
  2583. goto out;
  2584. }
  2585. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  2586. el = &eb->h_list;
  2587. if (el->l_tree_depth) {
  2588. ocfs2_error(inode->i_sb,
  2589. "Inode %lu has non zero tree depth in "
  2590. "xattr tree block %llu\n", inode->i_ino,
  2591. (unsigned long long)eb_bh->b_blocknr);
  2592. ret = -EROFS;
  2593. goto out;
  2594. }
  2595. }
  2596. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  2597. rec = &el->l_recs[i];
  2598. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  2599. e_blkno = le64_to_cpu(rec->e_blkno);
  2600. break;
  2601. }
  2602. }
  2603. if (!e_blkno) {
  2604. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  2605. "record (%u, %u, 0) in xattr", inode->i_ino,
  2606. le32_to_cpu(rec->e_cpos),
  2607. ocfs2_rec_clusters(el, rec));
  2608. ret = -EROFS;
  2609. goto out;
  2610. }
  2611. *p_blkno = le64_to_cpu(rec->e_blkno);
  2612. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  2613. if (e_cpos)
  2614. *e_cpos = le32_to_cpu(rec->e_cpos);
  2615. out:
  2616. brelse(eb_bh);
  2617. return ret;
  2618. }
  2619. typedef int (xattr_bucket_func)(struct inode *inode,
  2620. struct ocfs2_xattr_bucket *bucket,
  2621. void *para);
  2622. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  2623. struct ocfs2_xattr_bucket *bucket,
  2624. int name_index,
  2625. const char *name,
  2626. u32 name_hash,
  2627. u16 *xe_index,
  2628. int *found)
  2629. {
  2630. int i, ret = 0, cmp = 1, block_off, new_offset;
  2631. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  2632. size_t name_len = strlen(name);
  2633. struct ocfs2_xattr_entry *xe = NULL;
  2634. char *xe_name;
  2635. /*
  2636. * We don't use binary search in the bucket because there
  2637. * may be multiple entries with the same name hash.
  2638. */
  2639. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  2640. xe = &xh->xh_entries[i];
  2641. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  2642. continue;
  2643. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  2644. break;
  2645. cmp = name_index - ocfs2_xattr_get_type(xe);
  2646. if (!cmp)
  2647. cmp = name_len - xe->xe_name_len;
  2648. if (cmp)
  2649. continue;
  2650. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  2651. xh,
  2652. i,
  2653. &block_off,
  2654. &new_offset);
  2655. if (ret) {
  2656. mlog_errno(ret);
  2657. break;
  2658. }
  2659. xe_name = bucket_block(bucket, block_off) + new_offset;
  2660. if (!memcmp(name, xe_name, name_len)) {
  2661. *xe_index = i;
  2662. *found = 1;
  2663. ret = 0;
  2664. break;
  2665. }
  2666. }
  2667. return ret;
  2668. }
  2669. /*
  2670. * Find the specified xattr entry in a series of buckets.
  2671. * This series start from p_blkno and last for num_clusters.
  2672. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  2673. * the num of the valid buckets.
  2674. *
  2675. * Return the buffer_head this xattr should reside in. And if the xattr's
  2676. * hash is in the gap of 2 buckets, return the lower bucket.
  2677. */
  2678. static int ocfs2_xattr_bucket_find(struct inode *inode,
  2679. int name_index,
  2680. const char *name,
  2681. u32 name_hash,
  2682. u64 p_blkno,
  2683. u32 first_hash,
  2684. u32 num_clusters,
  2685. struct ocfs2_xattr_search *xs)
  2686. {
  2687. int ret, found = 0;
  2688. struct ocfs2_xattr_header *xh = NULL;
  2689. struct ocfs2_xattr_entry *xe = NULL;
  2690. u16 index = 0;
  2691. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2692. int low_bucket = 0, bucket, high_bucket;
  2693. struct ocfs2_xattr_bucket *search;
  2694. u32 last_hash;
  2695. u64 blkno, lower_blkno = 0;
  2696. search = ocfs2_xattr_bucket_new(inode);
  2697. if (!search) {
  2698. ret = -ENOMEM;
  2699. mlog_errno(ret);
  2700. goto out;
  2701. }
  2702. ret = ocfs2_read_xattr_bucket(search, p_blkno);
  2703. if (ret) {
  2704. mlog_errno(ret);
  2705. goto out;
  2706. }
  2707. xh = bucket_xh(search);
  2708. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  2709. while (low_bucket <= high_bucket) {
  2710. ocfs2_xattr_bucket_relse(search);
  2711. bucket = (low_bucket + high_bucket) / 2;
  2712. blkno = p_blkno + bucket * blk_per_bucket;
  2713. ret = ocfs2_read_xattr_bucket(search, blkno);
  2714. if (ret) {
  2715. mlog_errno(ret);
  2716. goto out;
  2717. }
  2718. xh = bucket_xh(search);
  2719. xe = &xh->xh_entries[0];
  2720. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  2721. high_bucket = bucket - 1;
  2722. continue;
  2723. }
  2724. /*
  2725. * Check whether the hash of the last entry in our
  2726. * bucket is larger than the search one. for an empty
  2727. * bucket, the last one is also the first one.
  2728. */
  2729. if (xh->xh_count)
  2730. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  2731. last_hash = le32_to_cpu(xe->xe_name_hash);
  2732. /* record lower_blkno which may be the insert place. */
  2733. lower_blkno = blkno;
  2734. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  2735. low_bucket = bucket + 1;
  2736. continue;
  2737. }
  2738. /* the searched xattr should reside in this bucket if exists. */
  2739. ret = ocfs2_find_xe_in_bucket(inode, search,
  2740. name_index, name, name_hash,
  2741. &index, &found);
  2742. if (ret) {
  2743. mlog_errno(ret);
  2744. goto out;
  2745. }
  2746. break;
  2747. }
  2748. /*
  2749. * Record the bucket we have found.
  2750. * When the xattr's hash value is in the gap of 2 buckets, we will
  2751. * always set it to the previous bucket.
  2752. */
  2753. if (!lower_blkno)
  2754. lower_blkno = p_blkno;
  2755. /* This should be in cache - we just read it during the search */
  2756. ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
  2757. if (ret) {
  2758. mlog_errno(ret);
  2759. goto out;
  2760. }
  2761. xs->header = bucket_xh(xs->bucket);
  2762. xs->base = bucket_block(xs->bucket, 0);
  2763. xs->end = xs->base + inode->i_sb->s_blocksize;
  2764. if (found) {
  2765. xs->here = &xs->header->xh_entries[index];
  2766. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  2767. (unsigned long long)bucket_blkno(xs->bucket), index);
  2768. } else
  2769. ret = -ENODATA;
  2770. out:
  2771. ocfs2_xattr_bucket_free(search);
  2772. return ret;
  2773. }
  2774. static int ocfs2_xattr_index_block_find(struct inode *inode,
  2775. struct buffer_head *root_bh,
  2776. int name_index,
  2777. const char *name,
  2778. struct ocfs2_xattr_search *xs)
  2779. {
  2780. int ret;
  2781. struct ocfs2_xattr_block *xb =
  2782. (struct ocfs2_xattr_block *)root_bh->b_data;
  2783. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  2784. struct ocfs2_extent_list *el = &xb_root->xt_list;
  2785. u64 p_blkno = 0;
  2786. u32 first_hash, num_clusters = 0;
  2787. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  2788. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2789. return -ENODATA;
  2790. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  2791. name, name_hash, name_index);
  2792. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  2793. &num_clusters, el);
  2794. if (ret) {
  2795. mlog_errno(ret);
  2796. goto out;
  2797. }
  2798. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  2799. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  2800. "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
  2801. first_hash);
  2802. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  2803. p_blkno, first_hash, num_clusters, xs);
  2804. out:
  2805. return ret;
  2806. }
  2807. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  2808. u64 blkno,
  2809. u32 clusters,
  2810. xattr_bucket_func *func,
  2811. void *para)
  2812. {
  2813. int i, ret = 0;
  2814. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  2815. u32 num_buckets = clusters * bpc;
  2816. struct ocfs2_xattr_bucket *bucket;
  2817. bucket = ocfs2_xattr_bucket_new(inode);
  2818. if (!bucket) {
  2819. mlog_errno(-ENOMEM);
  2820. return -ENOMEM;
  2821. }
  2822. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  2823. clusters, (unsigned long long)blkno);
  2824. for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
  2825. ret = ocfs2_read_xattr_bucket(bucket, blkno);
  2826. if (ret) {
  2827. mlog_errno(ret);
  2828. break;
  2829. }
  2830. /*
  2831. * The real bucket num in this series of blocks is stored
  2832. * in the 1st bucket.
  2833. */
  2834. if (i == 0)
  2835. num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
  2836. mlog(0, "iterating xattr bucket %llu, first hash %u\n",
  2837. (unsigned long long)blkno,
  2838. le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
  2839. if (func) {
  2840. ret = func(inode, bucket, para);
  2841. if (ret && ret != -ERANGE)
  2842. mlog_errno(ret);
  2843. /* Fall through to bucket_relse() */
  2844. }
  2845. ocfs2_xattr_bucket_relse(bucket);
  2846. if (ret)
  2847. break;
  2848. }
  2849. ocfs2_xattr_bucket_free(bucket);
  2850. return ret;
  2851. }
  2852. struct ocfs2_xattr_tree_list {
  2853. char *buffer;
  2854. size_t buffer_size;
  2855. size_t result;
  2856. };
  2857. static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
  2858. struct ocfs2_xattr_header *xh,
  2859. int index,
  2860. int *block_off,
  2861. int *new_offset)
  2862. {
  2863. u16 name_offset;
  2864. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  2865. return -EINVAL;
  2866. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  2867. *block_off = name_offset >> sb->s_blocksize_bits;
  2868. *new_offset = name_offset % sb->s_blocksize;
  2869. return 0;
  2870. }
  2871. static int ocfs2_list_xattr_bucket(struct inode *inode,
  2872. struct ocfs2_xattr_bucket *bucket,
  2873. void *para)
  2874. {
  2875. int ret = 0, type;
  2876. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  2877. int i, block_off, new_offset;
  2878. const char *prefix, *name;
  2879. for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
  2880. struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
  2881. type = ocfs2_xattr_get_type(entry);
  2882. prefix = ocfs2_xattr_prefix(type);
  2883. if (prefix) {
  2884. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  2885. bucket_xh(bucket),
  2886. i,
  2887. &block_off,
  2888. &new_offset);
  2889. if (ret)
  2890. break;
  2891. name = (const char *)bucket_block(bucket, block_off) +
  2892. new_offset;
  2893. ret = ocfs2_xattr_list_entry(xl->buffer,
  2894. xl->buffer_size,
  2895. &xl->result,
  2896. prefix, name,
  2897. entry->xe_name_len);
  2898. if (ret)
  2899. break;
  2900. }
  2901. }
  2902. return ret;
  2903. }
  2904. static int ocfs2_iterate_xattr_index_block(struct inode *inode,
  2905. struct buffer_head *blk_bh,
  2906. xattr_tree_rec_func *rec_func,
  2907. void *para)
  2908. {
  2909. struct ocfs2_xattr_block *xb =
  2910. (struct ocfs2_xattr_block *)blk_bh->b_data;
  2911. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  2912. int ret = 0;
  2913. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  2914. u64 p_blkno = 0;
  2915. if (!el->l_next_free_rec || !rec_func)
  2916. return 0;
  2917. while (name_hash > 0) {
  2918. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  2919. &e_cpos, &num_clusters, el);
  2920. if (ret) {
  2921. mlog_errno(ret);
  2922. break;
  2923. }
  2924. ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
  2925. num_clusters, para);
  2926. if (ret) {
  2927. if (ret != -ERANGE)
  2928. mlog_errno(ret);
  2929. break;
  2930. }
  2931. if (e_cpos == 0)
  2932. break;
  2933. name_hash = e_cpos - 1;
  2934. }
  2935. return ret;
  2936. }
  2937. static int ocfs2_list_xattr_tree_rec(struct inode *inode,
  2938. struct buffer_head *root_bh,
  2939. u64 blkno, u32 cpos, u32 len, void *para)
  2940. {
  2941. return ocfs2_iterate_xattr_buckets(inode, blkno, len,
  2942. ocfs2_list_xattr_bucket, para);
  2943. }
  2944. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  2945. struct buffer_head *blk_bh,
  2946. char *buffer,
  2947. size_t buffer_size)
  2948. {
  2949. int ret;
  2950. struct ocfs2_xattr_tree_list xl = {
  2951. .buffer = buffer,
  2952. .buffer_size = buffer_size,
  2953. .result = 0,
  2954. };
  2955. ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
  2956. ocfs2_list_xattr_tree_rec, &xl);
  2957. if (ret) {
  2958. mlog_errno(ret);
  2959. goto out;
  2960. }
  2961. ret = xl.result;
  2962. out:
  2963. return ret;
  2964. }
  2965. static int cmp_xe(const void *a, const void *b)
  2966. {
  2967. const struct ocfs2_xattr_entry *l = a, *r = b;
  2968. u32 l_hash = le32_to_cpu(l->xe_name_hash);
  2969. u32 r_hash = le32_to_cpu(r->xe_name_hash);
  2970. if (l_hash > r_hash)
  2971. return 1;
  2972. if (l_hash < r_hash)
  2973. return -1;
  2974. return 0;
  2975. }
  2976. static void swap_xe(void *a, void *b, int size)
  2977. {
  2978. struct ocfs2_xattr_entry *l = a, *r = b, tmp;
  2979. tmp = *l;
  2980. memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
  2981. memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
  2982. }
  2983. /*
  2984. * When the ocfs2_xattr_block is filled up, new bucket will be created
  2985. * and all the xattr entries will be moved to the new bucket.
  2986. * The header goes at the start of the bucket, and the names+values are
  2987. * filled from the end. This is why *target starts as the last buffer.
  2988. * Note: we need to sort the entries since they are not saved in order
  2989. * in the ocfs2_xattr_block.
  2990. */
  2991. static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  2992. struct buffer_head *xb_bh,
  2993. struct ocfs2_xattr_bucket *bucket)
  2994. {
  2995. int i, blocksize = inode->i_sb->s_blocksize;
  2996. int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2997. u16 offset, size, off_change;
  2998. struct ocfs2_xattr_entry *xe;
  2999. struct ocfs2_xattr_block *xb =
  3000. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3001. struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
  3002. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  3003. u16 count = le16_to_cpu(xb_xh->xh_count);
  3004. char *src = xb_bh->b_data;
  3005. char *target = bucket_block(bucket, blks - 1);
  3006. mlog(0, "cp xattr from block %llu to bucket %llu\n",
  3007. (unsigned long long)xb_bh->b_blocknr,
  3008. (unsigned long long)bucket_blkno(bucket));
  3009. for (i = 0; i < blks; i++)
  3010. memset(bucket_block(bucket, i), 0, blocksize);
  3011. /*
  3012. * Since the xe_name_offset is based on ocfs2_xattr_header,
  3013. * there is a offset change corresponding to the change of
  3014. * ocfs2_xattr_header's position.
  3015. */
  3016. off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  3017. xe = &xb_xh->xh_entries[count - 1];
  3018. offset = le16_to_cpu(xe->xe_name_offset) + off_change;
  3019. size = blocksize - offset;
  3020. /* copy all the names and values. */
  3021. memcpy(target + offset, src + offset, size);
  3022. /* Init new header now. */
  3023. xh->xh_count = xb_xh->xh_count;
  3024. xh->xh_num_buckets = cpu_to_le16(1);
  3025. xh->xh_name_value_len = cpu_to_le16(size);
  3026. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
  3027. /* copy all the entries. */
  3028. target = bucket_block(bucket, 0);
  3029. offset = offsetof(struct ocfs2_xattr_header, xh_entries);
  3030. size = count * sizeof(struct ocfs2_xattr_entry);
  3031. memcpy(target + offset, (char *)xb_xh + offset, size);
  3032. /* Change the xe offset for all the xe because of the move. */
  3033. off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
  3034. offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  3035. for (i = 0; i < count; i++)
  3036. le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
  3037. mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
  3038. offset, size, off_change);
  3039. sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
  3040. cmp_xe, swap_xe);
  3041. }
  3042. /*
  3043. * After we move xattr from block to index btree, we have to
  3044. * update ocfs2_xattr_search to the new xe and base.
  3045. *
  3046. * When the entry is in xattr block, xattr_bh indicates the storage place.
  3047. * While if the entry is in index b-tree, "bucket" indicates the
  3048. * real place of the xattr.
  3049. */
  3050. static void ocfs2_xattr_update_xattr_search(struct inode *inode,
  3051. struct ocfs2_xattr_search *xs,
  3052. struct buffer_head *old_bh)
  3053. {
  3054. char *buf = old_bh->b_data;
  3055. struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
  3056. struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
  3057. int i;
  3058. xs->header = bucket_xh(xs->bucket);
  3059. xs->base = bucket_block(xs->bucket, 0);
  3060. xs->end = xs->base + inode->i_sb->s_blocksize;
  3061. if (xs->not_found)
  3062. return;
  3063. i = xs->here - old_xh->xh_entries;
  3064. xs->here = &xs->header->xh_entries[i];
  3065. }
  3066. static int ocfs2_xattr_create_index_block(struct inode *inode,
  3067. struct ocfs2_xattr_search *xs,
  3068. struct ocfs2_xattr_set_ctxt *ctxt)
  3069. {
  3070. int ret;
  3071. u32 bit_off, len;
  3072. u64 blkno;
  3073. handle_t *handle = ctxt->handle;
  3074. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3075. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  3076. struct buffer_head *xb_bh = xs->xattr_bh;
  3077. struct ocfs2_xattr_block *xb =
  3078. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3079. struct ocfs2_xattr_tree_root *xr;
  3080. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  3081. mlog(0, "create xattr index block for %llu\n",
  3082. (unsigned long long)xb_bh->b_blocknr);
  3083. BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
  3084. BUG_ON(!xs->bucket);
  3085. /*
  3086. * XXX:
  3087. * We can use this lock for now, and maybe move to a dedicated mutex
  3088. * if performance becomes a problem later.
  3089. */
  3090. down_write(&oi->ip_alloc_sem);
  3091. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
  3092. OCFS2_JOURNAL_ACCESS_WRITE);
  3093. if (ret) {
  3094. mlog_errno(ret);
  3095. goto out;
  3096. }
  3097. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
  3098. 1, 1, &bit_off, &len);
  3099. if (ret) {
  3100. mlog_errno(ret);
  3101. goto out;
  3102. }
  3103. /*
  3104. * The bucket may spread in many blocks, and
  3105. * we will only touch the 1st block and the last block
  3106. * in the whole bucket(one for entry and one for data).
  3107. */
  3108. blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  3109. mlog(0, "allocate 1 cluster from %llu to xattr block\n",
  3110. (unsigned long long)blkno);
  3111. ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
  3112. if (ret) {
  3113. mlog_errno(ret);
  3114. goto out;
  3115. }
  3116. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  3117. OCFS2_JOURNAL_ACCESS_CREATE);
  3118. if (ret) {
  3119. mlog_errno(ret);
  3120. goto out;
  3121. }
  3122. ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
  3123. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  3124. ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
  3125. /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
  3126. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  3127. offsetof(struct ocfs2_xattr_block, xb_attrs));
  3128. xr = &xb->xb_attrs.xb_root;
  3129. xr->xt_clusters = cpu_to_le32(1);
  3130. xr->xt_last_eb_blk = 0;
  3131. xr->xt_list.l_tree_depth = 0;
  3132. xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
  3133. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  3134. xr->xt_list.l_recs[0].e_cpos = 0;
  3135. xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  3136. xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  3137. xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
  3138. ocfs2_journal_dirty(handle, xb_bh);
  3139. out:
  3140. up_write(&oi->ip_alloc_sem);
  3141. return ret;
  3142. }
  3143. static int cmp_xe_offset(const void *a, const void *b)
  3144. {
  3145. const struct ocfs2_xattr_entry *l = a, *r = b;
  3146. u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
  3147. u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
  3148. if (l_name_offset < r_name_offset)
  3149. return 1;
  3150. if (l_name_offset > r_name_offset)
  3151. return -1;
  3152. return 0;
  3153. }
  3154. /*
  3155. * defrag a xattr bucket if we find that the bucket has some
  3156. * holes beteen name/value pairs.
  3157. * We will move all the name/value pairs to the end of the bucket
  3158. * so that we can spare some space for insertion.
  3159. */
  3160. static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  3161. handle_t *handle,
  3162. struct ocfs2_xattr_bucket *bucket)
  3163. {
  3164. int ret, i;
  3165. size_t end, offset, len, value_len;
  3166. struct ocfs2_xattr_header *xh;
  3167. char *entries, *buf, *bucket_buf = NULL;
  3168. u64 blkno = bucket_blkno(bucket);
  3169. u16 xh_free_start;
  3170. size_t blocksize = inode->i_sb->s_blocksize;
  3171. struct ocfs2_xattr_entry *xe;
  3172. /*
  3173. * In order to make the operation more efficient and generic,
  3174. * we copy all the blocks into a contiguous memory and do the
  3175. * defragment there, so if anything is error, we will not touch
  3176. * the real block.
  3177. */
  3178. bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
  3179. if (!bucket_buf) {
  3180. ret = -EIO;
  3181. goto out;
  3182. }
  3183. buf = bucket_buf;
  3184. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3185. memcpy(buf, bucket_block(bucket, i), blocksize);
  3186. ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
  3187. OCFS2_JOURNAL_ACCESS_WRITE);
  3188. if (ret < 0) {
  3189. mlog_errno(ret);
  3190. goto out;
  3191. }
  3192. xh = (struct ocfs2_xattr_header *)bucket_buf;
  3193. entries = (char *)xh->xh_entries;
  3194. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3195. mlog(0, "adjust xattr bucket in %llu, count = %u, "
  3196. "xh_free_start = %u, xh_name_value_len = %u.\n",
  3197. (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
  3198. xh_free_start, le16_to_cpu(xh->xh_name_value_len));
  3199. /*
  3200. * sort all the entries by their offset.
  3201. * the largest will be the first, so that we can
  3202. * move them to the end one by one.
  3203. */
  3204. sort(entries, le16_to_cpu(xh->xh_count),
  3205. sizeof(struct ocfs2_xattr_entry),
  3206. cmp_xe_offset, swap_xe);
  3207. /* Move all name/values to the end of the bucket. */
  3208. xe = xh->xh_entries;
  3209. end = OCFS2_XATTR_BUCKET_SIZE;
  3210. for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
  3211. offset = le16_to_cpu(xe->xe_name_offset);
  3212. if (ocfs2_xattr_is_local(xe))
  3213. value_len = OCFS2_XATTR_SIZE(
  3214. le64_to_cpu(xe->xe_value_size));
  3215. else
  3216. value_len = OCFS2_XATTR_ROOT_SIZE;
  3217. len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
  3218. /*
  3219. * We must make sure that the name/value pair
  3220. * exist in the same block. So adjust end to
  3221. * the previous block end if needed.
  3222. */
  3223. if (((end - len) / blocksize !=
  3224. (end - 1) / blocksize))
  3225. end = end - end % blocksize;
  3226. if (end > offset + len) {
  3227. memmove(bucket_buf + end - len,
  3228. bucket_buf + offset, len);
  3229. xe->xe_name_offset = cpu_to_le16(end - len);
  3230. }
  3231. mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
  3232. "bucket %llu\n", (unsigned long long)blkno);
  3233. end -= len;
  3234. }
  3235. mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
  3236. "bucket %llu\n", (unsigned long long)blkno);
  3237. if (xh_free_start == end)
  3238. goto out;
  3239. memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
  3240. xh->xh_free_start = cpu_to_le16(end);
  3241. /* sort the entries by their name_hash. */
  3242. sort(entries, le16_to_cpu(xh->xh_count),
  3243. sizeof(struct ocfs2_xattr_entry),
  3244. cmp_xe, swap_xe);
  3245. buf = bucket_buf;
  3246. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3247. memcpy(bucket_block(bucket, i), buf, blocksize);
  3248. ocfs2_xattr_bucket_journal_dirty(handle, bucket);
  3249. out:
  3250. kfree(bucket_buf);
  3251. return ret;
  3252. }
  3253. /*
  3254. * prev_blkno points to the start of an existing extent. new_blkno
  3255. * points to a newly allocated extent. Because we know each of our
  3256. * clusters contains more than bucket, we can easily split one cluster
  3257. * at a bucket boundary. So we take the last cluster of the existing
  3258. * extent and split it down the middle. We move the last half of the
  3259. * buckets in the last cluster of the existing extent over to the new
  3260. * extent.
  3261. *
  3262. * first_bh is the buffer at prev_blkno so we can update the existing
  3263. * extent's bucket count. header_bh is the bucket were we were hoping
  3264. * to insert our xattr. If the bucket move places the target in the new
  3265. * extent, we'll update first_bh and header_bh after modifying the old
  3266. * extent.
  3267. *
  3268. * first_hash will be set as the 1st xe's name_hash in the new extent.
  3269. */
  3270. static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
  3271. handle_t *handle,
  3272. struct ocfs2_xattr_bucket *first,
  3273. struct ocfs2_xattr_bucket *target,
  3274. u64 new_blkno,
  3275. u32 num_clusters,
  3276. u32 *first_hash)
  3277. {
  3278. int ret;
  3279. struct super_block *sb = inode->i_sb;
  3280. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
  3281. int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
  3282. int to_move = num_buckets / 2;
  3283. u64 src_blkno;
  3284. u64 last_cluster_blkno = bucket_blkno(first) +
  3285. ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
  3286. BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
  3287. BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
  3288. mlog(0, "move half of xattrs in cluster %llu to %llu\n",
  3289. (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
  3290. ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
  3291. last_cluster_blkno, new_blkno,
  3292. to_move, first_hash);
  3293. if (ret) {
  3294. mlog_errno(ret);
  3295. goto out;
  3296. }
  3297. /* This is the first bucket that got moved */
  3298. src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
  3299. /*
  3300. * If the target bucket was part of the moved buckets, we need to
  3301. * update first and target.
  3302. */
  3303. if (bucket_blkno(target) >= src_blkno) {
  3304. /* Find the block for the new target bucket */
  3305. src_blkno = new_blkno +
  3306. (bucket_blkno(target) - src_blkno);
  3307. ocfs2_xattr_bucket_relse(first);
  3308. ocfs2_xattr_bucket_relse(target);
  3309. /*
  3310. * These shouldn't fail - the buffers are in the
  3311. * journal from ocfs2_cp_xattr_bucket().
  3312. */
  3313. ret = ocfs2_read_xattr_bucket(first, new_blkno);
  3314. if (ret) {
  3315. mlog_errno(ret);
  3316. goto out;
  3317. }
  3318. ret = ocfs2_read_xattr_bucket(target, src_blkno);
  3319. if (ret)
  3320. mlog_errno(ret);
  3321. }
  3322. out:
  3323. return ret;
  3324. }
  3325. /*
  3326. * Find the suitable pos when we divide a bucket into 2.
  3327. * We have to make sure the xattrs with the same hash value exist
  3328. * in the same bucket.
  3329. *
  3330. * If this ocfs2_xattr_header covers more than one hash value, find a
  3331. * place where the hash value changes. Try to find the most even split.
  3332. * The most common case is that all entries have different hash values,
  3333. * and the first check we make will find a place to split.
  3334. */
  3335. static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
  3336. {
  3337. struct ocfs2_xattr_entry *entries = xh->xh_entries;
  3338. int count = le16_to_cpu(xh->xh_count);
  3339. int delta, middle = count / 2;
  3340. /*
  3341. * We start at the middle. Each step gets farther away in both
  3342. * directions. We therefore hit the change in hash value
  3343. * nearest to the middle. Note that this loop does not execute for
  3344. * count < 2.
  3345. */
  3346. for (delta = 0; delta < middle; delta++) {
  3347. /* Let's check delta earlier than middle */
  3348. if (cmp_xe(&entries[middle - delta - 1],
  3349. &entries[middle - delta]))
  3350. return middle - delta;
  3351. /* For even counts, don't walk off the end */
  3352. if ((middle + delta + 1) == count)
  3353. continue;
  3354. /* Now try delta past middle */
  3355. if (cmp_xe(&entries[middle + delta],
  3356. &entries[middle + delta + 1]))
  3357. return middle + delta + 1;
  3358. }
  3359. /* Every entry had the same hash */
  3360. return count;
  3361. }
  3362. /*
  3363. * Move some xattrs in old bucket(blk) to new bucket(new_blk).
  3364. * first_hash will record the 1st hash of the new bucket.
  3365. *
  3366. * Normally half of the xattrs will be moved. But we have to make
  3367. * sure that the xattrs with the same hash value are stored in the
  3368. * same bucket. If all the xattrs in this bucket have the same hash
  3369. * value, the new bucket will be initialized as an empty one and the
  3370. * first_hash will be initialized as (hash_value+1).
  3371. */
  3372. static int ocfs2_divide_xattr_bucket(struct inode *inode,
  3373. handle_t *handle,
  3374. u64 blk,
  3375. u64 new_blk,
  3376. u32 *first_hash,
  3377. int new_bucket_head)
  3378. {
  3379. int ret, i;
  3380. int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
  3381. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  3382. struct ocfs2_xattr_header *xh;
  3383. struct ocfs2_xattr_entry *xe;
  3384. int blocksize = inode->i_sb->s_blocksize;
  3385. mlog(0, "move some of xattrs from bucket %llu to %llu\n",
  3386. (unsigned long long)blk, (unsigned long long)new_blk);
  3387. s_bucket = ocfs2_xattr_bucket_new(inode);
  3388. t_bucket = ocfs2_xattr_bucket_new(inode);
  3389. if (!s_bucket || !t_bucket) {
  3390. ret = -ENOMEM;
  3391. mlog_errno(ret);
  3392. goto out;
  3393. }
  3394. ret = ocfs2_read_xattr_bucket(s_bucket, blk);
  3395. if (ret) {
  3396. mlog_errno(ret);
  3397. goto out;
  3398. }
  3399. ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
  3400. OCFS2_JOURNAL_ACCESS_WRITE);
  3401. if (ret) {
  3402. mlog_errno(ret);
  3403. goto out;
  3404. }
  3405. /*
  3406. * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
  3407. * there's no need to read it.
  3408. */
  3409. ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
  3410. if (ret) {
  3411. mlog_errno(ret);
  3412. goto out;
  3413. }
  3414. /*
  3415. * Hey, if we're overwriting t_bucket, what difference does
  3416. * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
  3417. * same part of ocfs2_cp_xattr_bucket().
  3418. */
  3419. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  3420. new_bucket_head ?
  3421. OCFS2_JOURNAL_ACCESS_CREATE :
  3422. OCFS2_JOURNAL_ACCESS_WRITE);
  3423. if (ret) {
  3424. mlog_errno(ret);
  3425. goto out;
  3426. }
  3427. xh = bucket_xh(s_bucket);
  3428. count = le16_to_cpu(xh->xh_count);
  3429. start = ocfs2_xattr_find_divide_pos(xh);
  3430. if (start == count) {
  3431. xe = &xh->xh_entries[start-1];
  3432. /*
  3433. * initialized a new empty bucket here.
  3434. * The hash value is set as one larger than
  3435. * that of the last entry in the previous bucket.
  3436. */
  3437. for (i = 0; i < t_bucket->bu_blocks; i++)
  3438. memset(bucket_block(t_bucket, i), 0, blocksize);
  3439. xh = bucket_xh(t_bucket);
  3440. xh->xh_free_start = cpu_to_le16(blocksize);
  3441. xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
  3442. le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
  3443. goto set_num_buckets;
  3444. }
  3445. /* copy the whole bucket to the new first. */
  3446. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  3447. /* update the new bucket. */
  3448. xh = bucket_xh(t_bucket);
  3449. /*
  3450. * Calculate the total name/value len and xh_free_start for
  3451. * the old bucket first.
  3452. */
  3453. name_offset = OCFS2_XATTR_BUCKET_SIZE;
  3454. name_value_len = 0;
  3455. for (i = 0; i < start; i++) {
  3456. xe = &xh->xh_entries[i];
  3457. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  3458. if (ocfs2_xattr_is_local(xe))
  3459. xe_len +=
  3460. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3461. else
  3462. xe_len += OCFS2_XATTR_ROOT_SIZE;
  3463. name_value_len += xe_len;
  3464. if (le16_to_cpu(xe->xe_name_offset) < name_offset)
  3465. name_offset = le16_to_cpu(xe->xe_name_offset);
  3466. }
  3467. /*
  3468. * Now begin the modification to the new bucket.
  3469. *
  3470. * In the new bucket, We just move the xattr entry to the beginning
  3471. * and don't touch the name/value. So there will be some holes in the
  3472. * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
  3473. * called.
  3474. */
  3475. xe = &xh->xh_entries[start];
  3476. len = sizeof(struct ocfs2_xattr_entry) * (count - start);
  3477. mlog(0, "mv xattr entry len %d from %d to %d\n", len,
  3478. (int)((char *)xe - (char *)xh),
  3479. (int)((char *)xh->xh_entries - (char *)xh));
  3480. memmove((char *)xh->xh_entries, (char *)xe, len);
  3481. xe = &xh->xh_entries[count - start];
  3482. len = sizeof(struct ocfs2_xattr_entry) * start;
  3483. memset((char *)xe, 0, len);
  3484. le16_add_cpu(&xh->xh_count, -start);
  3485. le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
  3486. /* Calculate xh_free_start for the new bucket. */
  3487. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  3488. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  3489. xe = &xh->xh_entries[i];
  3490. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  3491. if (ocfs2_xattr_is_local(xe))
  3492. xe_len +=
  3493. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3494. else
  3495. xe_len += OCFS2_XATTR_ROOT_SIZE;
  3496. if (le16_to_cpu(xe->xe_name_offset) <
  3497. le16_to_cpu(xh->xh_free_start))
  3498. xh->xh_free_start = xe->xe_name_offset;
  3499. }
  3500. set_num_buckets:
  3501. /* set xh->xh_num_buckets for the new xh. */
  3502. if (new_bucket_head)
  3503. xh->xh_num_buckets = cpu_to_le16(1);
  3504. else
  3505. xh->xh_num_buckets = 0;
  3506. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  3507. /* store the first_hash of the new bucket. */
  3508. if (first_hash)
  3509. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3510. /*
  3511. * Now only update the 1st block of the old bucket. If we
  3512. * just added a new empty bucket, there is no need to modify
  3513. * it.
  3514. */
  3515. if (start == count)
  3516. goto out;
  3517. xh = bucket_xh(s_bucket);
  3518. memset(&xh->xh_entries[start], 0,
  3519. sizeof(struct ocfs2_xattr_entry) * (count - start));
  3520. xh->xh_count = cpu_to_le16(start);
  3521. xh->xh_free_start = cpu_to_le16(name_offset);
  3522. xh->xh_name_value_len = cpu_to_le16(name_value_len);
  3523. ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
  3524. out:
  3525. ocfs2_xattr_bucket_free(s_bucket);
  3526. ocfs2_xattr_bucket_free(t_bucket);
  3527. return ret;
  3528. }
  3529. /*
  3530. * Copy xattr from one bucket to another bucket.
  3531. *
  3532. * The caller must make sure that the journal transaction
  3533. * has enough space for journaling.
  3534. */
  3535. static int ocfs2_cp_xattr_bucket(struct inode *inode,
  3536. handle_t *handle,
  3537. u64 s_blkno,
  3538. u64 t_blkno,
  3539. int t_is_new)
  3540. {
  3541. int ret;
  3542. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  3543. BUG_ON(s_blkno == t_blkno);
  3544. mlog(0, "cp bucket %llu to %llu, target is %d\n",
  3545. (unsigned long long)s_blkno, (unsigned long long)t_blkno,
  3546. t_is_new);
  3547. s_bucket = ocfs2_xattr_bucket_new(inode);
  3548. t_bucket = ocfs2_xattr_bucket_new(inode);
  3549. if (!s_bucket || !t_bucket) {
  3550. ret = -ENOMEM;
  3551. mlog_errno(ret);
  3552. goto out;
  3553. }
  3554. ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
  3555. if (ret)
  3556. goto out;
  3557. /*
  3558. * Even if !t_is_new, we're overwriting t_bucket. Thus,
  3559. * there's no need to read it.
  3560. */
  3561. ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
  3562. if (ret)
  3563. goto out;
  3564. /*
  3565. * Hey, if we're overwriting t_bucket, what difference does
  3566. * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
  3567. * cluster to fill, we came here from
  3568. * ocfs2_mv_xattr_buckets(), and it is really new -
  3569. * ACCESS_CREATE is required. But we also might have moved data
  3570. * out of t_bucket before extending back into it.
  3571. * ocfs2_add_new_xattr_bucket() can do this - its call to
  3572. * ocfs2_add_new_xattr_cluster() may have created a new extent
  3573. * and copied out the end of the old extent. Then it re-extends
  3574. * the old extent back to create space for new xattrs. That's
  3575. * how we get here, and the bucket isn't really new.
  3576. */
  3577. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  3578. t_is_new ?
  3579. OCFS2_JOURNAL_ACCESS_CREATE :
  3580. OCFS2_JOURNAL_ACCESS_WRITE);
  3581. if (ret)
  3582. goto out;
  3583. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  3584. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  3585. out:
  3586. ocfs2_xattr_bucket_free(t_bucket);
  3587. ocfs2_xattr_bucket_free(s_bucket);
  3588. return ret;
  3589. }
  3590. /*
  3591. * src_blk points to the start of an existing extent. last_blk points to
  3592. * last cluster in that extent. to_blk points to a newly allocated
  3593. * extent. We copy the buckets from the cluster at last_blk to the new
  3594. * extent. If start_bucket is non-zero, we skip that many buckets before
  3595. * we start copying. The new extent's xh_num_buckets gets set to the
  3596. * number of buckets we copied. The old extent's xh_num_buckets shrinks
  3597. * by the same amount.
  3598. */
  3599. static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
  3600. u64 src_blk, u64 last_blk, u64 to_blk,
  3601. unsigned int start_bucket,
  3602. u32 *first_hash)
  3603. {
  3604. int i, ret, credits;
  3605. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3606. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3607. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  3608. struct ocfs2_xattr_bucket *old_first, *new_first;
  3609. mlog(0, "mv xattrs from cluster %llu to %llu\n",
  3610. (unsigned long long)last_blk, (unsigned long long)to_blk);
  3611. BUG_ON(start_bucket >= num_buckets);
  3612. if (start_bucket) {
  3613. num_buckets -= start_bucket;
  3614. last_blk += (start_bucket * blks_per_bucket);
  3615. }
  3616. /* The first bucket of the original extent */
  3617. old_first = ocfs2_xattr_bucket_new(inode);
  3618. /* The first bucket of the new extent */
  3619. new_first = ocfs2_xattr_bucket_new(inode);
  3620. if (!old_first || !new_first) {
  3621. ret = -ENOMEM;
  3622. mlog_errno(ret);
  3623. goto out;
  3624. }
  3625. ret = ocfs2_read_xattr_bucket(old_first, src_blk);
  3626. if (ret) {
  3627. mlog_errno(ret);
  3628. goto out;
  3629. }
  3630. /*
  3631. * We need to update the first bucket of the old extent and all
  3632. * the buckets going to the new extent.
  3633. */
  3634. credits = ((num_buckets + 1) * blks_per_bucket) +
  3635. handle->h_buffer_credits;
  3636. ret = ocfs2_extend_trans(handle, credits);
  3637. if (ret) {
  3638. mlog_errno(ret);
  3639. goto out;
  3640. }
  3641. ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
  3642. OCFS2_JOURNAL_ACCESS_WRITE);
  3643. if (ret) {
  3644. mlog_errno(ret);
  3645. goto out;
  3646. }
  3647. for (i = 0; i < num_buckets; i++) {
  3648. ret = ocfs2_cp_xattr_bucket(inode, handle,
  3649. last_blk + (i * blks_per_bucket),
  3650. to_blk + (i * blks_per_bucket),
  3651. 1);
  3652. if (ret) {
  3653. mlog_errno(ret);
  3654. goto out;
  3655. }
  3656. }
  3657. /*
  3658. * Get the new bucket ready before we dirty anything
  3659. * (This actually shouldn't fail, because we already dirtied
  3660. * it once in ocfs2_cp_xattr_bucket()).
  3661. */
  3662. ret = ocfs2_read_xattr_bucket(new_first, to_blk);
  3663. if (ret) {
  3664. mlog_errno(ret);
  3665. goto out;
  3666. }
  3667. ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
  3668. OCFS2_JOURNAL_ACCESS_WRITE);
  3669. if (ret) {
  3670. mlog_errno(ret);
  3671. goto out;
  3672. }
  3673. /* Now update the headers */
  3674. le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
  3675. ocfs2_xattr_bucket_journal_dirty(handle, old_first);
  3676. bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
  3677. ocfs2_xattr_bucket_journal_dirty(handle, new_first);
  3678. if (first_hash)
  3679. *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
  3680. out:
  3681. ocfs2_xattr_bucket_free(new_first);
  3682. ocfs2_xattr_bucket_free(old_first);
  3683. return ret;
  3684. }
  3685. /*
  3686. * Move some xattrs in this cluster to the new cluster.
  3687. * This function should only be called when bucket size == cluster size.
  3688. * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  3689. */
  3690. static int ocfs2_divide_xattr_cluster(struct inode *inode,
  3691. handle_t *handle,
  3692. u64 prev_blk,
  3693. u64 new_blk,
  3694. u32 *first_hash)
  3695. {
  3696. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3697. int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
  3698. BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
  3699. ret = ocfs2_extend_trans(handle, credits);
  3700. if (ret) {
  3701. mlog_errno(ret);
  3702. return ret;
  3703. }
  3704. /* Move half of the xattr in start_blk to the next bucket. */
  3705. return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
  3706. new_blk, first_hash, 1);
  3707. }
  3708. /*
  3709. * Move some xattrs from the old cluster to the new one since they are not
  3710. * contiguous in ocfs2 xattr tree.
  3711. *
  3712. * new_blk starts a new separate cluster, and we will move some xattrs from
  3713. * prev_blk to it. v_start will be set as the first name hash value in this
  3714. * new cluster so that it can be used as e_cpos during tree insertion and
  3715. * don't collide with our original b-tree operations. first_bh and header_bh
  3716. * will also be updated since they will be used in ocfs2_extend_xattr_bucket
  3717. * to extend the insert bucket.
  3718. *
  3719. * The problem is how much xattr should we move to the new one and when should
  3720. * we update first_bh and header_bh?
  3721. * 1. If cluster size > bucket size, that means the previous cluster has more
  3722. * than 1 bucket, so just move half nums of bucket into the new cluster and
  3723. * update the first_bh and header_bh if the insert bucket has been moved
  3724. * to the new cluster.
  3725. * 2. If cluster_size == bucket_size:
  3726. * a) If the previous extent rec has more than one cluster and the insert
  3727. * place isn't in the last cluster, copy the entire last cluster to the
  3728. * new one. This time, we don't need to upate the first_bh and header_bh
  3729. * since they will not be moved into the new cluster.
  3730. * b) Otherwise, move the bottom half of the xattrs in the last cluster into
  3731. * the new one. And we set the extend flag to zero if the insert place is
  3732. * moved into the new allocated cluster since no extend is needed.
  3733. */
  3734. static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
  3735. handle_t *handle,
  3736. struct ocfs2_xattr_bucket *first,
  3737. struct ocfs2_xattr_bucket *target,
  3738. u64 new_blk,
  3739. u32 prev_clusters,
  3740. u32 *v_start,
  3741. int *extend)
  3742. {
  3743. int ret;
  3744. mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
  3745. (unsigned long long)bucket_blkno(first), prev_clusters,
  3746. (unsigned long long)new_blk);
  3747. if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
  3748. ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
  3749. handle,
  3750. first, target,
  3751. new_blk,
  3752. prev_clusters,
  3753. v_start);
  3754. if (ret)
  3755. mlog_errno(ret);
  3756. } else {
  3757. /* The start of the last cluster in the first extent */
  3758. u64 last_blk = bucket_blkno(first) +
  3759. ((prev_clusters - 1) *
  3760. ocfs2_clusters_to_blocks(inode->i_sb, 1));
  3761. if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
  3762. ret = ocfs2_mv_xattr_buckets(inode, handle,
  3763. bucket_blkno(first),
  3764. last_blk, new_blk, 0,
  3765. v_start);
  3766. if (ret)
  3767. mlog_errno(ret);
  3768. } else {
  3769. ret = ocfs2_divide_xattr_cluster(inode, handle,
  3770. last_blk, new_blk,
  3771. v_start);
  3772. if (ret)
  3773. mlog_errno(ret);
  3774. if ((bucket_blkno(target) == last_blk) && extend)
  3775. *extend = 0;
  3776. }
  3777. }
  3778. return ret;
  3779. }
  3780. /*
  3781. * Add a new cluster for xattr storage.
  3782. *
  3783. * If the new cluster is contiguous with the previous one, it will be
  3784. * appended to the same extent record, and num_clusters will be updated.
  3785. * If not, we will insert a new extent for it and move some xattrs in
  3786. * the last cluster into the new allocated one.
  3787. * We also need to limit the maximum size of a btree leaf, otherwise we'll
  3788. * lose the benefits of hashing because we'll have to search large leaves.
  3789. * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
  3790. * if it's bigger).
  3791. *
  3792. * first_bh is the first block of the previous extent rec and header_bh
  3793. * indicates the bucket we will insert the new xattrs. They will be updated
  3794. * when the header_bh is moved into the new cluster.
  3795. */
  3796. static int ocfs2_add_new_xattr_cluster(struct inode *inode,
  3797. struct buffer_head *root_bh,
  3798. struct ocfs2_xattr_bucket *first,
  3799. struct ocfs2_xattr_bucket *target,
  3800. u32 *num_clusters,
  3801. u32 prev_cpos,
  3802. int *extend,
  3803. struct ocfs2_xattr_set_ctxt *ctxt)
  3804. {
  3805. int ret;
  3806. u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3807. u32 prev_clusters = *num_clusters;
  3808. u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
  3809. u64 block;
  3810. handle_t *handle = ctxt->handle;
  3811. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3812. struct ocfs2_extent_tree et;
  3813. mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
  3814. "previous xattr blkno = %llu\n",
  3815. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3816. prev_cpos, (unsigned long long)bucket_blkno(first));
  3817. ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
  3818. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  3819. OCFS2_JOURNAL_ACCESS_WRITE);
  3820. if (ret < 0) {
  3821. mlog_errno(ret);
  3822. goto leave;
  3823. }
  3824. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
  3825. clusters_to_add, &bit_off, &num_bits);
  3826. if (ret < 0) {
  3827. if (ret != -ENOSPC)
  3828. mlog_errno(ret);
  3829. goto leave;
  3830. }
  3831. BUG_ON(num_bits > clusters_to_add);
  3832. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  3833. mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
  3834. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3835. if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
  3836. (prev_clusters + num_bits) << osb->s_clustersize_bits <=
  3837. OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
  3838. /*
  3839. * If this cluster is contiguous with the old one and
  3840. * adding this new cluster, we don't surpass the limit of
  3841. * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
  3842. * initialized and used like other buckets in the previous
  3843. * cluster.
  3844. * So add it as a contiguous one. The caller will handle
  3845. * its init process.
  3846. */
  3847. v_start = prev_cpos + prev_clusters;
  3848. *num_clusters = prev_clusters + num_bits;
  3849. mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
  3850. num_bits);
  3851. } else {
  3852. ret = ocfs2_adjust_xattr_cross_cluster(inode,
  3853. handle,
  3854. first,
  3855. target,
  3856. block,
  3857. prev_clusters,
  3858. &v_start,
  3859. extend);
  3860. if (ret) {
  3861. mlog_errno(ret);
  3862. goto leave;
  3863. }
  3864. }
  3865. mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
  3866. num_bits, (unsigned long long)block, v_start);
  3867. ret = ocfs2_insert_extent(handle, &et, v_start, block,
  3868. num_bits, 0, ctxt->meta_ac);
  3869. if (ret < 0) {
  3870. mlog_errno(ret);
  3871. goto leave;
  3872. }
  3873. ret = ocfs2_journal_dirty(handle, root_bh);
  3874. if (ret < 0)
  3875. mlog_errno(ret);
  3876. leave:
  3877. return ret;
  3878. }
  3879. /*
  3880. * We are given an extent. 'first' is the bucket at the very front of
  3881. * the extent. The extent has space for an additional bucket past
  3882. * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
  3883. * of the target bucket. We wish to shift every bucket past the target
  3884. * down one, filling in that additional space. When we get back to the
  3885. * target, we split the target between itself and the now-empty bucket
  3886. * at target+1 (aka, target_blkno + blks_per_bucket).
  3887. */
  3888. static int ocfs2_extend_xattr_bucket(struct inode *inode,
  3889. handle_t *handle,
  3890. struct ocfs2_xattr_bucket *first,
  3891. u64 target_blk,
  3892. u32 num_clusters)
  3893. {
  3894. int ret, credits;
  3895. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3896. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3897. u64 end_blk;
  3898. u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
  3899. mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
  3900. "from %llu, len = %u\n", (unsigned long long)target_blk,
  3901. (unsigned long long)bucket_blkno(first), num_clusters);
  3902. /* The extent must have room for an additional bucket */
  3903. BUG_ON(new_bucket >=
  3904. (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
  3905. /* end_blk points to the last existing bucket */
  3906. end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
  3907. /*
  3908. * end_blk is the start of the last existing bucket.
  3909. * Thus, (end_blk - target_blk) covers the target bucket and
  3910. * every bucket after it up to, but not including, the last
  3911. * existing bucket. Then we add the last existing bucket, the
  3912. * new bucket, and the first bucket (3 * blk_per_bucket).
  3913. */
  3914. credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
  3915. handle->h_buffer_credits;
  3916. ret = ocfs2_extend_trans(handle, credits);
  3917. if (ret) {
  3918. mlog_errno(ret);
  3919. goto out;
  3920. }
  3921. ret = ocfs2_xattr_bucket_journal_access(handle, first,
  3922. OCFS2_JOURNAL_ACCESS_WRITE);
  3923. if (ret) {
  3924. mlog_errno(ret);
  3925. goto out;
  3926. }
  3927. while (end_blk != target_blk) {
  3928. ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
  3929. end_blk + blk_per_bucket, 0);
  3930. if (ret)
  3931. goto out;
  3932. end_blk -= blk_per_bucket;
  3933. }
  3934. /* Move half of the xattr in target_blkno to the next bucket. */
  3935. ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
  3936. target_blk + blk_per_bucket, NULL, 0);
  3937. le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
  3938. ocfs2_xattr_bucket_journal_dirty(handle, first);
  3939. out:
  3940. return ret;
  3941. }
  3942. /*
  3943. * Add new xattr bucket in an extent record and adjust the buckets
  3944. * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
  3945. * bucket we want to insert into.
  3946. *
  3947. * In the easy case, we will move all the buckets after target down by
  3948. * one. Half of target's xattrs will be moved to the next bucket.
  3949. *
  3950. * If current cluster is full, we'll allocate a new one. This may not
  3951. * be contiguous. The underlying calls will make sure that there is
  3952. * space for the insert, shifting buckets around if necessary.
  3953. * 'target' may be moved by those calls.
  3954. */
  3955. static int ocfs2_add_new_xattr_bucket(struct inode *inode,
  3956. struct buffer_head *xb_bh,
  3957. struct ocfs2_xattr_bucket *target,
  3958. struct ocfs2_xattr_set_ctxt *ctxt)
  3959. {
  3960. struct ocfs2_xattr_block *xb =
  3961. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3962. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3963. struct ocfs2_extent_list *el = &xb_root->xt_list;
  3964. u32 name_hash =
  3965. le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
  3966. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3967. int ret, num_buckets, extend = 1;
  3968. u64 p_blkno;
  3969. u32 e_cpos, num_clusters;
  3970. /* The bucket at the front of the extent */
  3971. struct ocfs2_xattr_bucket *first;
  3972. mlog(0, "Add new xattr bucket starting from %llu\n",
  3973. (unsigned long long)bucket_blkno(target));
  3974. /* The first bucket of the original extent */
  3975. first = ocfs2_xattr_bucket_new(inode);
  3976. if (!first) {
  3977. ret = -ENOMEM;
  3978. mlog_errno(ret);
  3979. goto out;
  3980. }
  3981. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
  3982. &num_clusters, el);
  3983. if (ret) {
  3984. mlog_errno(ret);
  3985. goto out;
  3986. }
  3987. ret = ocfs2_read_xattr_bucket(first, p_blkno);
  3988. if (ret) {
  3989. mlog_errno(ret);
  3990. goto out;
  3991. }
  3992. num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
  3993. if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
  3994. /*
  3995. * This can move first+target if the target bucket moves
  3996. * to the new extent.
  3997. */
  3998. ret = ocfs2_add_new_xattr_cluster(inode,
  3999. xb_bh,
  4000. first,
  4001. target,
  4002. &num_clusters,
  4003. e_cpos,
  4004. &extend,
  4005. ctxt);
  4006. if (ret) {
  4007. mlog_errno(ret);
  4008. goto out;
  4009. }
  4010. }
  4011. if (extend) {
  4012. ret = ocfs2_extend_xattr_bucket(inode,
  4013. ctxt->handle,
  4014. first,
  4015. bucket_blkno(target),
  4016. num_clusters);
  4017. if (ret)
  4018. mlog_errno(ret);
  4019. }
  4020. out:
  4021. ocfs2_xattr_bucket_free(first);
  4022. return ret;
  4023. }
  4024. static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
  4025. struct ocfs2_xattr_bucket *bucket,
  4026. int offs)
  4027. {
  4028. int block_off = offs >> inode->i_sb->s_blocksize_bits;
  4029. offs = offs % inode->i_sb->s_blocksize;
  4030. return bucket_block(bucket, block_off) + offs;
  4031. }
  4032. /*
  4033. * Handle the normal xattr set, including replace, delete and new.
  4034. *
  4035. * Note: "local" indicates the real data's locality. So we can't
  4036. * just its bucket locality by its length.
  4037. */
  4038. static void ocfs2_xattr_set_entry_normal(struct inode *inode,
  4039. struct ocfs2_xattr_info *xi,
  4040. struct ocfs2_xattr_search *xs,
  4041. u32 name_hash,
  4042. int local)
  4043. {
  4044. struct ocfs2_xattr_entry *last, *xe;
  4045. int name_len = strlen(xi->name);
  4046. struct ocfs2_xattr_header *xh = xs->header;
  4047. u16 count = le16_to_cpu(xh->xh_count), start;
  4048. size_t blocksize = inode->i_sb->s_blocksize;
  4049. char *val;
  4050. size_t offs, size, new_size;
  4051. last = &xh->xh_entries[count];
  4052. if (!xs->not_found) {
  4053. xe = xs->here;
  4054. offs = le16_to_cpu(xe->xe_name_offset);
  4055. if (ocfs2_xattr_is_local(xe))
  4056. size = OCFS2_XATTR_SIZE(name_len) +
  4057. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  4058. else
  4059. size = OCFS2_XATTR_SIZE(name_len) +
  4060. OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  4061. /*
  4062. * If the new value will be stored outside, xi->value has been
  4063. * initalized as an empty ocfs2_xattr_value_root, and the same
  4064. * goes with xi->value_len, so we can set new_size safely here.
  4065. * See ocfs2_xattr_set_in_bucket.
  4066. */
  4067. new_size = OCFS2_XATTR_SIZE(name_len) +
  4068. OCFS2_XATTR_SIZE(xi->value_len);
  4069. le16_add_cpu(&xh->xh_name_value_len, -size);
  4070. if (xi->value) {
  4071. if (new_size > size)
  4072. goto set_new_name_value;
  4073. /* Now replace the old value with new one. */
  4074. if (local)
  4075. xe->xe_value_size = cpu_to_le64(xi->value_len);
  4076. else
  4077. xe->xe_value_size = 0;
  4078. val = ocfs2_xattr_bucket_get_val(inode,
  4079. xs->bucket, offs);
  4080. memset(val + OCFS2_XATTR_SIZE(name_len), 0,
  4081. size - OCFS2_XATTR_SIZE(name_len));
  4082. if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
  4083. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  4084. xi->value, xi->value_len);
  4085. le16_add_cpu(&xh->xh_name_value_len, new_size);
  4086. ocfs2_xattr_set_local(xe, local);
  4087. return;
  4088. } else {
  4089. /*
  4090. * Remove the old entry if there is more than one.
  4091. * We don't remove the last entry so that we can
  4092. * use it to indicate the hash value of the empty
  4093. * bucket.
  4094. */
  4095. last -= 1;
  4096. le16_add_cpu(&xh->xh_count, -1);
  4097. if (xh->xh_count) {
  4098. memmove(xe, xe + 1,
  4099. (void *)last - (void *)xe);
  4100. memset(last, 0,
  4101. sizeof(struct ocfs2_xattr_entry));
  4102. } else
  4103. xh->xh_free_start =
  4104. cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  4105. return;
  4106. }
  4107. } else {
  4108. /* find a new entry for insert. */
  4109. int low = 0, high = count - 1, tmp;
  4110. struct ocfs2_xattr_entry *tmp_xe;
  4111. while (low <= high && count) {
  4112. tmp = (low + high) / 2;
  4113. tmp_xe = &xh->xh_entries[tmp];
  4114. if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
  4115. low = tmp + 1;
  4116. else if (name_hash <
  4117. le32_to_cpu(tmp_xe->xe_name_hash))
  4118. high = tmp - 1;
  4119. else {
  4120. low = tmp;
  4121. break;
  4122. }
  4123. }
  4124. xe = &xh->xh_entries[low];
  4125. if (low != count)
  4126. memmove(xe + 1, xe, (void *)last - (void *)xe);
  4127. le16_add_cpu(&xh->xh_count, 1);
  4128. memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
  4129. xe->xe_name_hash = cpu_to_le32(name_hash);
  4130. xe->xe_name_len = name_len;
  4131. ocfs2_xattr_set_type(xe, xi->name_index);
  4132. }
  4133. set_new_name_value:
  4134. /* Insert the new name+value. */
  4135. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
  4136. /*
  4137. * We must make sure that the name/value pair
  4138. * exists in the same block.
  4139. */
  4140. offs = le16_to_cpu(xh->xh_free_start);
  4141. start = offs - size;
  4142. if (start >> inode->i_sb->s_blocksize_bits !=
  4143. (offs - 1) >> inode->i_sb->s_blocksize_bits) {
  4144. offs = offs - offs % blocksize;
  4145. xh->xh_free_start = cpu_to_le16(offs);
  4146. }
  4147. val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
  4148. xe->xe_name_offset = cpu_to_le16(offs - size);
  4149. memset(val, 0, size);
  4150. memcpy(val, xi->name, name_len);
  4151. memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
  4152. xe->xe_value_size = cpu_to_le64(xi->value_len);
  4153. ocfs2_xattr_set_local(xe, local);
  4154. xs->here = xe;
  4155. le16_add_cpu(&xh->xh_free_start, -size);
  4156. le16_add_cpu(&xh->xh_name_value_len, size);
  4157. return;
  4158. }
  4159. /*
  4160. * Set the xattr entry in the specified bucket.
  4161. * The bucket is indicated by xs->bucket and it should have the enough
  4162. * space for the xattr insertion.
  4163. */
  4164. static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
  4165. handle_t *handle,
  4166. struct ocfs2_xattr_info *xi,
  4167. struct ocfs2_xattr_search *xs,
  4168. u32 name_hash,
  4169. int local)
  4170. {
  4171. int ret;
  4172. u64 blkno;
  4173. mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
  4174. (unsigned long)xi->value_len, xi->name_index,
  4175. (unsigned long long)bucket_blkno(xs->bucket));
  4176. if (!xs->bucket->bu_bhs[1]) {
  4177. blkno = bucket_blkno(xs->bucket);
  4178. ocfs2_xattr_bucket_relse(xs->bucket);
  4179. ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
  4180. if (ret) {
  4181. mlog_errno(ret);
  4182. goto out;
  4183. }
  4184. }
  4185. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  4186. OCFS2_JOURNAL_ACCESS_WRITE);
  4187. if (ret < 0) {
  4188. mlog_errno(ret);
  4189. goto out;
  4190. }
  4191. ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
  4192. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  4193. out:
  4194. return ret;
  4195. }
  4196. /*
  4197. * Truncate the specified xe_off entry in xattr bucket.
  4198. * bucket is indicated by header_bh and len is the new length.
  4199. * Both the ocfs2_xattr_value_root and the entry will be updated here.
  4200. *
  4201. * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
  4202. */
  4203. static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
  4204. struct ocfs2_xattr_bucket *bucket,
  4205. int xe_off,
  4206. int len,
  4207. struct ocfs2_xattr_set_ctxt *ctxt)
  4208. {
  4209. int ret, offset;
  4210. u64 value_blk;
  4211. struct ocfs2_xattr_entry *xe;
  4212. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4213. size_t blocksize = inode->i_sb->s_blocksize;
  4214. struct ocfs2_xattr_value_buf vb = {
  4215. .vb_access = ocfs2_journal_access,
  4216. };
  4217. xe = &xh->xh_entries[xe_off];
  4218. BUG_ON(!xe || ocfs2_xattr_is_local(xe));
  4219. offset = le16_to_cpu(xe->xe_name_offset) +
  4220. OCFS2_XATTR_SIZE(xe->xe_name_len);
  4221. value_blk = offset / blocksize;
  4222. /* We don't allow ocfs2_xattr_value to be stored in different block. */
  4223. BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
  4224. vb.vb_bh = bucket->bu_bhs[value_blk];
  4225. BUG_ON(!vb.vb_bh);
  4226. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  4227. (vb.vb_bh->b_data + offset % blocksize);
  4228. /*
  4229. * From here on out we have to dirty the bucket. The generic
  4230. * value calls only modify one of the bucket's bhs, but we need
  4231. * to send the bucket at once. So if they error, they *could* have
  4232. * modified something. We have to assume they did, and dirty
  4233. * the whole bucket. This leaves us in a consistent state.
  4234. */
  4235. mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
  4236. xe_off, (unsigned long long)bucket_blkno(bucket), len);
  4237. ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
  4238. if (ret) {
  4239. mlog_errno(ret);
  4240. goto out;
  4241. }
  4242. ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
  4243. OCFS2_JOURNAL_ACCESS_WRITE);
  4244. if (ret) {
  4245. mlog_errno(ret);
  4246. goto out;
  4247. }
  4248. xe->xe_value_size = cpu_to_le64(len);
  4249. ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
  4250. out:
  4251. return ret;
  4252. }
  4253. static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
  4254. struct ocfs2_xattr_search *xs,
  4255. int len,
  4256. struct ocfs2_xattr_set_ctxt *ctxt)
  4257. {
  4258. int ret, offset;
  4259. struct ocfs2_xattr_entry *xe = xs->here;
  4260. struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
  4261. BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
  4262. offset = xe - xh->xh_entries;
  4263. ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
  4264. offset, len, ctxt);
  4265. if (ret)
  4266. mlog_errno(ret);
  4267. return ret;
  4268. }
  4269. static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
  4270. handle_t *handle,
  4271. struct ocfs2_xattr_search *xs,
  4272. char *val,
  4273. int value_len)
  4274. {
  4275. int ret, offset, block_off;
  4276. struct ocfs2_xattr_value_root *xv;
  4277. struct ocfs2_xattr_entry *xe = xs->here;
  4278. struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
  4279. void *base;
  4280. struct ocfs2_xattr_value_buf vb = {
  4281. .vb_access = ocfs2_journal_access,
  4282. };
  4283. BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
  4284. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
  4285. xe - xh->xh_entries,
  4286. &block_off,
  4287. &offset);
  4288. if (ret) {
  4289. mlog_errno(ret);
  4290. goto out;
  4291. }
  4292. base = bucket_block(xs->bucket, block_off);
  4293. xv = (struct ocfs2_xattr_value_root *)(base + offset +
  4294. OCFS2_XATTR_SIZE(xe->xe_name_len));
  4295. vb.vb_xv = xv;
  4296. vb.vb_bh = xs->bucket->bu_bhs[block_off];
  4297. ret = __ocfs2_xattr_set_value_outside(inode, handle,
  4298. &vb, val, value_len);
  4299. if (ret)
  4300. mlog_errno(ret);
  4301. out:
  4302. return ret;
  4303. }
  4304. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  4305. struct buffer_head *root_bh,
  4306. u64 blkno,
  4307. u32 cpos,
  4308. u32 len,
  4309. void *para)
  4310. {
  4311. int ret;
  4312. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4313. struct inode *tl_inode = osb->osb_tl_inode;
  4314. handle_t *handle;
  4315. struct ocfs2_xattr_block *xb =
  4316. (struct ocfs2_xattr_block *)root_bh->b_data;
  4317. struct ocfs2_alloc_context *meta_ac = NULL;
  4318. struct ocfs2_cached_dealloc_ctxt dealloc;
  4319. struct ocfs2_extent_tree et;
  4320. ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
  4321. ocfs2_delete_xattr_in_bucket, NULL);
  4322. if (ret) {
  4323. mlog_errno(ret);
  4324. return ret;
  4325. }
  4326. ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
  4327. ocfs2_init_dealloc_ctxt(&dealloc);
  4328. mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
  4329. cpos, len, (unsigned long long)blkno);
  4330. ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
  4331. len);
  4332. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  4333. if (ret) {
  4334. mlog_errno(ret);
  4335. return ret;
  4336. }
  4337. mutex_lock(&tl_inode->i_mutex);
  4338. if (ocfs2_truncate_log_needs_flush(osb)) {
  4339. ret = __ocfs2_flush_truncate_log(osb);
  4340. if (ret < 0) {
  4341. mlog_errno(ret);
  4342. goto out;
  4343. }
  4344. }
  4345. handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
  4346. if (IS_ERR(handle)) {
  4347. ret = -ENOMEM;
  4348. mlog_errno(ret);
  4349. goto out;
  4350. }
  4351. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  4352. OCFS2_JOURNAL_ACCESS_WRITE);
  4353. if (ret) {
  4354. mlog_errno(ret);
  4355. goto out_commit;
  4356. }
  4357. ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
  4358. &dealloc);
  4359. if (ret) {
  4360. mlog_errno(ret);
  4361. goto out_commit;
  4362. }
  4363. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
  4364. ret = ocfs2_journal_dirty(handle, root_bh);
  4365. if (ret) {
  4366. mlog_errno(ret);
  4367. goto out_commit;
  4368. }
  4369. ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
  4370. if (ret)
  4371. mlog_errno(ret);
  4372. out_commit:
  4373. ocfs2_commit_trans(osb, handle);
  4374. out:
  4375. ocfs2_schedule_truncate_log_flush(osb, 1);
  4376. mutex_unlock(&tl_inode->i_mutex);
  4377. if (meta_ac)
  4378. ocfs2_free_alloc_context(meta_ac);
  4379. ocfs2_run_deallocs(osb, &dealloc);
  4380. return ret;
  4381. }
  4382. static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
  4383. handle_t *handle,
  4384. struct ocfs2_xattr_search *xs)
  4385. {
  4386. struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
  4387. struct ocfs2_xattr_entry *last = &xh->xh_entries[
  4388. le16_to_cpu(xh->xh_count) - 1];
  4389. int ret = 0;
  4390. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  4391. OCFS2_JOURNAL_ACCESS_WRITE);
  4392. if (ret) {
  4393. mlog_errno(ret);
  4394. return;
  4395. }
  4396. /* Remove the old entry. */
  4397. memmove(xs->here, xs->here + 1,
  4398. (void *)last - (void *)xs->here);
  4399. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  4400. le16_add_cpu(&xh->xh_count, -1);
  4401. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  4402. }
  4403. /*
  4404. * Set the xattr name/value in the bucket specified in xs.
  4405. *
  4406. * As the new value in xi may be stored in the bucket or in an outside cluster,
  4407. * we divide the whole process into 3 steps:
  4408. * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
  4409. * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
  4410. * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
  4411. * 4. If the clusters for the new outside value can't be allocated, we need
  4412. * to free the xattr we allocated in set.
  4413. */
  4414. static int ocfs2_xattr_set_in_bucket(struct inode *inode,
  4415. struct ocfs2_xattr_info *xi,
  4416. struct ocfs2_xattr_search *xs,
  4417. struct ocfs2_xattr_set_ctxt *ctxt)
  4418. {
  4419. int ret, local = 1;
  4420. size_t value_len;
  4421. char *val = (char *)xi->value;
  4422. struct ocfs2_xattr_entry *xe = xs->here;
  4423. u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
  4424. strlen(xi->name));
  4425. if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
  4426. /*
  4427. * We need to truncate the xattr storage first.
  4428. *
  4429. * If both the old and new value are stored to
  4430. * outside block, we only need to truncate
  4431. * the storage and then set the value outside.
  4432. *
  4433. * If the new value should be stored within block,
  4434. * we should free all the outside block first and
  4435. * the modification to the xattr block will be done
  4436. * by following steps.
  4437. */
  4438. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  4439. value_len = xi->value_len;
  4440. else
  4441. value_len = 0;
  4442. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  4443. value_len,
  4444. ctxt);
  4445. if (ret)
  4446. goto out;
  4447. if (value_len)
  4448. goto set_value_outside;
  4449. }
  4450. value_len = xi->value_len;
  4451. /* So we have to handle the inside block change now. */
  4452. if (value_len > OCFS2_XATTR_INLINE_SIZE) {
  4453. /*
  4454. * If the new value will be stored outside of block,
  4455. * initalize a new empty value root and insert it first.
  4456. */
  4457. local = 0;
  4458. xi->value = &def_xv;
  4459. xi->value_len = OCFS2_XATTR_ROOT_SIZE;
  4460. }
  4461. ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
  4462. name_hash, local);
  4463. if (ret) {
  4464. mlog_errno(ret);
  4465. goto out;
  4466. }
  4467. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  4468. goto out;
  4469. /* allocate the space now for the outside block storage. */
  4470. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  4471. value_len, ctxt);
  4472. if (ret) {
  4473. mlog_errno(ret);
  4474. if (xs->not_found) {
  4475. /*
  4476. * We can't allocate enough clusters for outside
  4477. * storage and we have allocated xattr already,
  4478. * so need to remove it.
  4479. */
  4480. ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
  4481. }
  4482. goto out;
  4483. }
  4484. set_value_outside:
  4485. ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
  4486. xs, val, value_len);
  4487. out:
  4488. return ret;
  4489. }
  4490. /*
  4491. * check whether the xattr bucket is filled up with the same hash value.
  4492. * If we want to insert the xattr with the same hash, return -ENOSPC.
  4493. * If we want to insert a xattr with different hash value, go ahead
  4494. * and ocfs2_divide_xattr_bucket will handle this.
  4495. */
  4496. static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
  4497. struct ocfs2_xattr_bucket *bucket,
  4498. const char *name)
  4499. {
  4500. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4501. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  4502. if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
  4503. return 0;
  4504. if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
  4505. xh->xh_entries[0].xe_name_hash) {
  4506. mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
  4507. "hash = %u\n",
  4508. (unsigned long long)bucket_blkno(bucket),
  4509. le32_to_cpu(xh->xh_entries[0].xe_name_hash));
  4510. return -ENOSPC;
  4511. }
  4512. return 0;
  4513. }
  4514. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  4515. struct ocfs2_xattr_info *xi,
  4516. struct ocfs2_xattr_search *xs,
  4517. struct ocfs2_xattr_set_ctxt *ctxt)
  4518. {
  4519. struct ocfs2_xattr_header *xh;
  4520. struct ocfs2_xattr_entry *xe;
  4521. u16 count, header_size, xh_free_start;
  4522. int free, max_free, need, old;
  4523. size_t value_size = 0, name_len = strlen(xi->name);
  4524. size_t blocksize = inode->i_sb->s_blocksize;
  4525. int ret, allocation = 0;
  4526. mlog_entry("Set xattr %s in xattr index block\n", xi->name);
  4527. try_again:
  4528. xh = xs->header;
  4529. count = le16_to_cpu(xh->xh_count);
  4530. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4531. header_size = sizeof(struct ocfs2_xattr_header) +
  4532. count * sizeof(struct ocfs2_xattr_entry);
  4533. max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
  4534. le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
  4535. mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
  4536. "of %u which exceed block size\n",
  4537. (unsigned long long)bucket_blkno(xs->bucket),
  4538. header_size);
  4539. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  4540. value_size = OCFS2_XATTR_ROOT_SIZE;
  4541. else if (xi->value)
  4542. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  4543. if (xs->not_found)
  4544. need = sizeof(struct ocfs2_xattr_entry) +
  4545. OCFS2_XATTR_SIZE(name_len) + value_size;
  4546. else {
  4547. need = value_size + OCFS2_XATTR_SIZE(name_len);
  4548. /*
  4549. * We only replace the old value if the new length is smaller
  4550. * than the old one. Otherwise we will allocate new space in the
  4551. * bucket to store it.
  4552. */
  4553. xe = xs->here;
  4554. if (ocfs2_xattr_is_local(xe))
  4555. old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  4556. else
  4557. old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  4558. if (old >= value_size)
  4559. need = 0;
  4560. }
  4561. free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
  4562. /*
  4563. * We need to make sure the new name/value pair
  4564. * can exist in the same block.
  4565. */
  4566. if (xh_free_start % blocksize < need)
  4567. free -= xh_free_start % blocksize;
  4568. mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
  4569. "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
  4570. " %u\n", xs->not_found,
  4571. (unsigned long long)bucket_blkno(xs->bucket),
  4572. free, need, max_free, le16_to_cpu(xh->xh_free_start),
  4573. le16_to_cpu(xh->xh_name_value_len));
  4574. if (free < need ||
  4575. (xs->not_found &&
  4576. count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
  4577. if (need <= max_free &&
  4578. count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  4579. /*
  4580. * We can create the space by defragment. Since only the
  4581. * name/value will be moved, the xe shouldn't be changed
  4582. * in xs.
  4583. */
  4584. ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
  4585. xs->bucket);
  4586. if (ret) {
  4587. mlog_errno(ret);
  4588. goto out;
  4589. }
  4590. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4591. free = xh_free_start - header_size
  4592. - OCFS2_XATTR_HEADER_GAP;
  4593. if (xh_free_start % blocksize < need)
  4594. free -= xh_free_start % blocksize;
  4595. if (free >= need)
  4596. goto xattr_set;
  4597. mlog(0, "Can't get enough space for xattr insert by "
  4598. "defragment. Need %u bytes, but we have %d, so "
  4599. "allocate new bucket for it.\n", need, free);
  4600. }
  4601. /*
  4602. * We have to add new buckets or clusters and one
  4603. * allocation should leave us enough space for insert.
  4604. */
  4605. BUG_ON(allocation);
  4606. /*
  4607. * We do not allow for overlapping ranges between buckets. And
  4608. * the maximum number of collisions we will allow for then is
  4609. * one bucket's worth, so check it here whether we need to
  4610. * add a new bucket for the insert.
  4611. */
  4612. ret = ocfs2_check_xattr_bucket_collision(inode,
  4613. xs->bucket,
  4614. xi->name);
  4615. if (ret) {
  4616. mlog_errno(ret);
  4617. goto out;
  4618. }
  4619. ret = ocfs2_add_new_xattr_bucket(inode,
  4620. xs->xattr_bh,
  4621. xs->bucket,
  4622. ctxt);
  4623. if (ret) {
  4624. mlog_errno(ret);
  4625. goto out;
  4626. }
  4627. /*
  4628. * ocfs2_add_new_xattr_bucket() will have updated
  4629. * xs->bucket if it moved, but it will not have updated
  4630. * any of the other search fields. Thus, we drop it and
  4631. * re-search. Everything should be cached, so it'll be
  4632. * quick.
  4633. */
  4634. ocfs2_xattr_bucket_relse(xs->bucket);
  4635. ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
  4636. xi->name_index,
  4637. xi->name, xs);
  4638. if (ret && ret != -ENODATA)
  4639. goto out;
  4640. xs->not_found = ret;
  4641. allocation = 1;
  4642. goto try_again;
  4643. }
  4644. xattr_set:
  4645. ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
  4646. out:
  4647. mlog_exit(ret);
  4648. return ret;
  4649. }
  4650. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  4651. struct ocfs2_xattr_bucket *bucket,
  4652. void *para)
  4653. {
  4654. int ret = 0;
  4655. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4656. u16 i;
  4657. struct ocfs2_xattr_entry *xe;
  4658. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4659. struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
  4660. int credits = ocfs2_remove_extent_credits(osb->sb) +
  4661. ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4662. ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
  4663. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4664. xe = &xh->xh_entries[i];
  4665. if (ocfs2_xattr_is_local(xe))
  4666. continue;
  4667. ctxt.handle = ocfs2_start_trans(osb, credits);
  4668. if (IS_ERR(ctxt.handle)) {
  4669. ret = PTR_ERR(ctxt.handle);
  4670. mlog_errno(ret);
  4671. break;
  4672. }
  4673. ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
  4674. i, 0, &ctxt);
  4675. ocfs2_commit_trans(osb, ctxt.handle);
  4676. if (ret) {
  4677. mlog_errno(ret);
  4678. break;
  4679. }
  4680. }
  4681. ocfs2_schedule_truncate_log_flush(osb, 1);
  4682. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  4683. return ret;
  4684. }
  4685. /*
  4686. * Whenever we modify a xattr value root in the bucket(e.g, CoW
  4687. * or change the extent record flag), we need to recalculate
  4688. * the metaecc for the whole bucket. So it is done here.
  4689. *
  4690. * Note:
  4691. * We have to give the extra credits for the caller.
  4692. */
  4693. static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
  4694. handle_t *handle,
  4695. void *para)
  4696. {
  4697. int ret;
  4698. struct ocfs2_xattr_bucket *bucket =
  4699. (struct ocfs2_xattr_bucket *)para;
  4700. ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
  4701. OCFS2_JOURNAL_ACCESS_WRITE);
  4702. if (ret) {
  4703. mlog_errno(ret);
  4704. return ret;
  4705. }
  4706. ocfs2_xattr_bucket_journal_dirty(handle, bucket);
  4707. return 0;
  4708. }
  4709. /*
  4710. * Special action we need if the xattr value is refcounted.
  4711. *
  4712. * 1. If the xattr is refcounted, lock the tree.
  4713. * 2. CoW the xattr if we are setting the new value and the value
  4714. * will be stored outside.
  4715. * 3. In other case, decrease_refcount will work for us, so just
  4716. * lock the refcount tree, calculate the meta and credits is OK.
  4717. *
  4718. * We have to do CoW before ocfs2_init_xattr_set_ctxt since
  4719. * currently CoW is a completed transaction, while this function
  4720. * will also lock the allocators and let us deadlock. So we will
  4721. * CoW the whole xattr value.
  4722. */
  4723. static int ocfs2_prepare_refcount_xattr(struct inode *inode,
  4724. struct ocfs2_dinode *di,
  4725. struct ocfs2_xattr_info *xi,
  4726. struct ocfs2_xattr_search *xis,
  4727. struct ocfs2_xattr_search *xbs,
  4728. struct ocfs2_refcount_tree **ref_tree,
  4729. int *meta_add,
  4730. int *credits)
  4731. {
  4732. int ret = 0;
  4733. struct ocfs2_xattr_block *xb;
  4734. struct ocfs2_xattr_entry *xe;
  4735. char *base;
  4736. u32 p_cluster, num_clusters;
  4737. unsigned int ext_flags;
  4738. int name_offset, name_len;
  4739. struct ocfs2_xattr_value_buf vb;
  4740. struct ocfs2_xattr_bucket *bucket = NULL;
  4741. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4742. struct ocfs2_post_refcount refcount;
  4743. struct ocfs2_post_refcount *p = NULL;
  4744. struct buffer_head *ref_root_bh = NULL;
  4745. if (!xis->not_found) {
  4746. xe = xis->here;
  4747. name_offset = le16_to_cpu(xe->xe_name_offset);
  4748. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  4749. base = xis->base;
  4750. vb.vb_bh = xis->inode_bh;
  4751. vb.vb_access = ocfs2_journal_access_di;
  4752. } else {
  4753. int i, block_off = 0;
  4754. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  4755. xe = xbs->here;
  4756. name_offset = le16_to_cpu(xe->xe_name_offset);
  4757. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  4758. i = xbs->here - xbs->header->xh_entries;
  4759. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  4760. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  4761. bucket_xh(xbs->bucket),
  4762. i, &block_off,
  4763. &name_offset);
  4764. if (ret) {
  4765. mlog_errno(ret);
  4766. goto out;
  4767. }
  4768. base = bucket_block(xbs->bucket, block_off);
  4769. vb.vb_bh = xbs->bucket->bu_bhs[block_off];
  4770. vb.vb_access = ocfs2_journal_access;
  4771. if (ocfs2_meta_ecc(osb)) {
  4772. /*create parameters for ocfs2_post_refcount. */
  4773. bucket = xbs->bucket;
  4774. refcount.credits = bucket->bu_blocks;
  4775. refcount.para = bucket;
  4776. refcount.func =
  4777. ocfs2_xattr_bucket_post_refcount;
  4778. p = &refcount;
  4779. }
  4780. } else {
  4781. base = xbs->base;
  4782. vb.vb_bh = xbs->xattr_bh;
  4783. vb.vb_access = ocfs2_journal_access_xb;
  4784. }
  4785. }
  4786. if (ocfs2_xattr_is_local(xe))
  4787. goto out;
  4788. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  4789. (base + name_offset + name_len);
  4790. ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
  4791. &num_clusters, &vb.vb_xv->xr_list,
  4792. &ext_flags);
  4793. if (ret) {
  4794. mlog_errno(ret);
  4795. goto out;
  4796. }
  4797. /*
  4798. * We just need to check the 1st extent record, since we always
  4799. * CoW the whole xattr. So there shouldn't be a xattr with
  4800. * some REFCOUNT extent recs after the 1st one.
  4801. */
  4802. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  4803. goto out;
  4804. ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
  4805. 1, ref_tree, &ref_root_bh);
  4806. if (ret) {
  4807. mlog_errno(ret);
  4808. goto out;
  4809. }
  4810. /*
  4811. * If we are deleting the xattr or the new size will be stored inside,
  4812. * cool, leave it there, the xattr truncate process will remove them
  4813. * for us(it still needs the refcount tree lock and the meta, credits).
  4814. * And the worse case is that every cluster truncate will split the
  4815. * refcount tree, and make the original extent become 3. So we will need
  4816. * 2 * cluster more extent recs at most.
  4817. */
  4818. if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) {
  4819. ret = ocfs2_refcounted_xattr_delete_need(inode,
  4820. &(*ref_tree)->rf_ci,
  4821. ref_root_bh, vb.vb_xv,
  4822. meta_add, credits);
  4823. if (ret)
  4824. mlog_errno(ret);
  4825. goto out;
  4826. }
  4827. ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
  4828. *ref_tree, ref_root_bh, 0,
  4829. le32_to_cpu(vb.vb_xv->xr_clusters), p);
  4830. if (ret)
  4831. mlog_errno(ret);
  4832. out:
  4833. brelse(ref_root_bh);
  4834. return ret;
  4835. }
  4836. /*
  4837. * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
  4838. * The physical clusters will be added to refcount tree.
  4839. */
  4840. static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
  4841. struct ocfs2_xattr_value_root *xv,
  4842. struct ocfs2_extent_tree *value_et,
  4843. struct ocfs2_caching_info *ref_ci,
  4844. struct buffer_head *ref_root_bh,
  4845. struct ocfs2_cached_dealloc_ctxt *dealloc,
  4846. struct ocfs2_post_refcount *refcount)
  4847. {
  4848. int ret = 0;
  4849. u32 clusters = le32_to_cpu(xv->xr_clusters);
  4850. u32 cpos, p_cluster, num_clusters;
  4851. struct ocfs2_extent_list *el = &xv->xr_list;
  4852. unsigned int ext_flags;
  4853. cpos = 0;
  4854. while (cpos < clusters) {
  4855. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  4856. &num_clusters, el, &ext_flags);
  4857. cpos += num_clusters;
  4858. if ((ext_flags & OCFS2_EXT_REFCOUNTED))
  4859. continue;
  4860. BUG_ON(!p_cluster);
  4861. ret = ocfs2_add_refcount_flag(inode, value_et,
  4862. ref_ci, ref_root_bh,
  4863. cpos - num_clusters,
  4864. p_cluster, num_clusters,
  4865. dealloc, refcount);
  4866. if (ret) {
  4867. mlog_errno(ret);
  4868. break;
  4869. }
  4870. }
  4871. return ret;
  4872. }
  4873. /*
  4874. * Given a normal ocfs2_xattr_header, refcount all the entries which
  4875. * have value stored outside.
  4876. * Used for xattrs stored in inode and ocfs2_xattr_block.
  4877. */
  4878. static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
  4879. struct ocfs2_xattr_value_buf *vb,
  4880. struct ocfs2_xattr_header *header,
  4881. struct ocfs2_caching_info *ref_ci,
  4882. struct buffer_head *ref_root_bh,
  4883. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4884. {
  4885. struct ocfs2_xattr_entry *xe;
  4886. struct ocfs2_xattr_value_root *xv;
  4887. struct ocfs2_extent_tree et;
  4888. int i, ret = 0;
  4889. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  4890. xe = &header->xh_entries[i];
  4891. if (ocfs2_xattr_is_local(xe))
  4892. continue;
  4893. xv = (struct ocfs2_xattr_value_root *)((void *)header +
  4894. le16_to_cpu(xe->xe_name_offset) +
  4895. OCFS2_XATTR_SIZE(xe->xe_name_len));
  4896. vb->vb_xv = xv;
  4897. ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
  4898. ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
  4899. ref_ci, ref_root_bh,
  4900. dealloc, NULL);
  4901. if (ret) {
  4902. mlog_errno(ret);
  4903. break;
  4904. }
  4905. }
  4906. return ret;
  4907. }
  4908. static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
  4909. struct buffer_head *fe_bh,
  4910. struct ocfs2_caching_info *ref_ci,
  4911. struct buffer_head *ref_root_bh,
  4912. struct ocfs2_cached_dealloc_ctxt *dealloc)
  4913. {
  4914. struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
  4915. struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
  4916. (fe_bh->b_data + inode->i_sb->s_blocksize -
  4917. le16_to_cpu(di->i_xattr_inline_size));
  4918. struct ocfs2_xattr_value_buf vb = {
  4919. .vb_bh = fe_bh,
  4920. .vb_access = ocfs2_journal_access_di,
  4921. };
  4922. return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
  4923. ref_ci, ref_root_bh, dealloc);
  4924. }
  4925. struct ocfs2_xattr_tree_value_refcount_para {
  4926. struct ocfs2_caching_info *ref_ci;
  4927. struct buffer_head *ref_root_bh;
  4928. struct ocfs2_cached_dealloc_ctxt *dealloc;
  4929. };
  4930. static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
  4931. struct ocfs2_xattr_bucket *bucket,
  4932. int offset,
  4933. struct ocfs2_xattr_value_root **xv,
  4934. struct buffer_head **bh)
  4935. {
  4936. int ret, block_off, name_offset;
  4937. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4938. struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
  4939. void *base;
  4940. ret = ocfs2_xattr_bucket_get_name_value(sb,
  4941. bucket_xh(bucket),
  4942. offset,
  4943. &block_off,
  4944. &name_offset);
  4945. if (ret) {
  4946. mlog_errno(ret);
  4947. goto out;
  4948. }
  4949. base = bucket_block(bucket, block_off);
  4950. *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
  4951. OCFS2_XATTR_SIZE(xe->xe_name_len));
  4952. if (bh)
  4953. *bh = bucket->bu_bhs[block_off];
  4954. out:
  4955. return ret;
  4956. }
  4957. /*
  4958. * For a given xattr bucket, refcount all the entries which
  4959. * have value stored outside.
  4960. */
  4961. static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
  4962. struct ocfs2_xattr_bucket *bucket,
  4963. void *para)
  4964. {
  4965. int i, ret = 0;
  4966. struct ocfs2_extent_tree et;
  4967. struct ocfs2_xattr_tree_value_refcount_para *ref =
  4968. (struct ocfs2_xattr_tree_value_refcount_para *)para;
  4969. struct ocfs2_xattr_header *xh =
  4970. (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
  4971. struct ocfs2_xattr_entry *xe;
  4972. struct ocfs2_xattr_value_buf vb = {
  4973. .vb_access = ocfs2_journal_access,
  4974. };
  4975. struct ocfs2_post_refcount refcount = {
  4976. .credits = bucket->bu_blocks,
  4977. .para = bucket,
  4978. .func = ocfs2_xattr_bucket_post_refcount,
  4979. };
  4980. struct ocfs2_post_refcount *p = NULL;
  4981. /* We only need post_refcount if we support metaecc. */
  4982. if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
  4983. p = &refcount;
  4984. mlog(0, "refcount bucket %llu, count = %u\n",
  4985. (unsigned long long)bucket_blkno(bucket),
  4986. le16_to_cpu(xh->xh_count));
  4987. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4988. xe = &xh->xh_entries[i];
  4989. if (ocfs2_xattr_is_local(xe))
  4990. continue;
  4991. ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
  4992. &vb.vb_xv, &vb.vb_bh);
  4993. if (ret) {
  4994. mlog_errno(ret);
  4995. break;
  4996. }
  4997. ocfs2_init_xattr_value_extent_tree(&et,
  4998. INODE_CACHE(inode), &vb);
  4999. ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
  5000. &et, ref->ref_ci,
  5001. ref->ref_root_bh,
  5002. ref->dealloc, p);
  5003. if (ret) {
  5004. mlog_errno(ret);
  5005. break;
  5006. }
  5007. }
  5008. return ret;
  5009. }
  5010. static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
  5011. struct buffer_head *root_bh,
  5012. u64 blkno, u32 cpos, u32 len, void *para)
  5013. {
  5014. return ocfs2_iterate_xattr_buckets(inode, blkno, len,
  5015. ocfs2_xattr_bucket_value_refcount,
  5016. para);
  5017. }
  5018. static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
  5019. struct buffer_head *blk_bh,
  5020. struct ocfs2_caching_info *ref_ci,
  5021. struct buffer_head *ref_root_bh,
  5022. struct ocfs2_cached_dealloc_ctxt *dealloc)
  5023. {
  5024. int ret = 0;
  5025. struct ocfs2_xattr_block *xb =
  5026. (struct ocfs2_xattr_block *)blk_bh->b_data;
  5027. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  5028. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  5029. struct ocfs2_xattr_value_buf vb = {
  5030. .vb_bh = blk_bh,
  5031. .vb_access = ocfs2_journal_access_xb,
  5032. };
  5033. ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
  5034. ref_ci, ref_root_bh,
  5035. dealloc);
  5036. } else {
  5037. struct ocfs2_xattr_tree_value_refcount_para para = {
  5038. .ref_ci = ref_ci,
  5039. .ref_root_bh = ref_root_bh,
  5040. .dealloc = dealloc,
  5041. };
  5042. ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
  5043. ocfs2_refcount_xattr_tree_rec,
  5044. &para);
  5045. }
  5046. return ret;
  5047. }
  5048. int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
  5049. struct buffer_head *fe_bh,
  5050. struct ocfs2_caching_info *ref_ci,
  5051. struct buffer_head *ref_root_bh,
  5052. struct ocfs2_cached_dealloc_ctxt *dealloc)
  5053. {
  5054. int ret = 0;
  5055. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  5056. struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
  5057. struct buffer_head *blk_bh = NULL;
  5058. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  5059. ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
  5060. ref_ci, ref_root_bh,
  5061. dealloc);
  5062. if (ret) {
  5063. mlog_errno(ret);
  5064. goto out;
  5065. }
  5066. }
  5067. if (!di->i_xattr_loc)
  5068. goto out;
  5069. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  5070. &blk_bh);
  5071. if (ret < 0) {
  5072. mlog_errno(ret);
  5073. goto out;
  5074. }
  5075. ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
  5076. ref_root_bh, dealloc);
  5077. if (ret)
  5078. mlog_errno(ret);
  5079. brelse(blk_bh);
  5080. out:
  5081. return ret;
  5082. }
  5083. /*
  5084. * 'security' attributes support
  5085. */
  5086. static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
  5087. size_t list_size, const char *name,
  5088. size_t name_len)
  5089. {
  5090. const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
  5091. const size_t total_len = prefix_len + name_len + 1;
  5092. if (list && total_len <= list_size) {
  5093. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  5094. memcpy(list + prefix_len, name, name_len);
  5095. list[prefix_len + name_len] = '\0';
  5096. }
  5097. return total_len;
  5098. }
  5099. static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
  5100. void *buffer, size_t size)
  5101. {
  5102. if (strcmp(name, "") == 0)
  5103. return -EINVAL;
  5104. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
  5105. buffer, size);
  5106. }
  5107. static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
  5108. const void *value, size_t size, int flags)
  5109. {
  5110. if (strcmp(name, "") == 0)
  5111. return -EINVAL;
  5112. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
  5113. size, flags);
  5114. }
  5115. int ocfs2_init_security_get(struct inode *inode,
  5116. struct inode *dir,
  5117. struct ocfs2_security_xattr_info *si)
  5118. {
  5119. /* check whether ocfs2 support feature xattr */
  5120. if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
  5121. return -EOPNOTSUPP;
  5122. return security_inode_init_security(inode, dir, &si->name, &si->value,
  5123. &si->value_len);
  5124. }
  5125. int ocfs2_init_security_set(handle_t *handle,
  5126. struct inode *inode,
  5127. struct buffer_head *di_bh,
  5128. struct ocfs2_security_xattr_info *si,
  5129. struct ocfs2_alloc_context *xattr_ac,
  5130. struct ocfs2_alloc_context *data_ac)
  5131. {
  5132. return ocfs2_xattr_set_handle(handle, inode, di_bh,
  5133. OCFS2_XATTR_INDEX_SECURITY,
  5134. si->name, si->value, si->value_len, 0,
  5135. xattr_ac, data_ac);
  5136. }
  5137. struct xattr_handler ocfs2_xattr_security_handler = {
  5138. .prefix = XATTR_SECURITY_PREFIX,
  5139. .list = ocfs2_xattr_security_list,
  5140. .get = ocfs2_xattr_security_get,
  5141. .set = ocfs2_xattr_security_set,
  5142. };
  5143. /*
  5144. * 'trusted' attributes support
  5145. */
  5146. static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
  5147. size_t list_size, const char *name,
  5148. size_t name_len)
  5149. {
  5150. const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  5151. const size_t total_len = prefix_len + name_len + 1;
  5152. if (list && total_len <= list_size) {
  5153. memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
  5154. memcpy(list + prefix_len, name, name_len);
  5155. list[prefix_len + name_len] = '\0';
  5156. }
  5157. return total_len;
  5158. }
  5159. static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
  5160. void *buffer, size_t size)
  5161. {
  5162. if (strcmp(name, "") == 0)
  5163. return -EINVAL;
  5164. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
  5165. buffer, size);
  5166. }
  5167. static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
  5168. const void *value, size_t size, int flags)
  5169. {
  5170. if (strcmp(name, "") == 0)
  5171. return -EINVAL;
  5172. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
  5173. size, flags);
  5174. }
  5175. struct xattr_handler ocfs2_xattr_trusted_handler = {
  5176. .prefix = XATTR_TRUSTED_PREFIX,
  5177. .list = ocfs2_xattr_trusted_list,
  5178. .get = ocfs2_xattr_trusted_get,
  5179. .set = ocfs2_xattr_trusted_set,
  5180. };
  5181. /*
  5182. * 'user' attributes support
  5183. */
  5184. static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
  5185. size_t list_size, const char *name,
  5186. size_t name_len)
  5187. {
  5188. const size_t prefix_len = XATTR_USER_PREFIX_LEN;
  5189. const size_t total_len = prefix_len + name_len + 1;
  5190. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  5191. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  5192. return 0;
  5193. if (list && total_len <= list_size) {
  5194. memcpy(list, XATTR_USER_PREFIX, prefix_len);
  5195. memcpy(list + prefix_len, name, name_len);
  5196. list[prefix_len + name_len] = '\0';
  5197. }
  5198. return total_len;
  5199. }
  5200. static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
  5201. void *buffer, size_t size)
  5202. {
  5203. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  5204. if (strcmp(name, "") == 0)
  5205. return -EINVAL;
  5206. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  5207. return -EOPNOTSUPP;
  5208. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
  5209. buffer, size);
  5210. }
  5211. static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
  5212. const void *value, size_t size, int flags)
  5213. {
  5214. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  5215. if (strcmp(name, "") == 0)
  5216. return -EINVAL;
  5217. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  5218. return -EOPNOTSUPP;
  5219. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
  5220. size, flags);
  5221. }
  5222. struct xattr_handler ocfs2_xattr_user_handler = {
  5223. .prefix = XATTR_USER_PREFIX,
  5224. .list = ocfs2_xattr_user_list,
  5225. .get = ocfs2_xattr_user_get,
  5226. .set = ocfs2_xattr_user_set,
  5227. };