xattr.c 184 KB

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