xattr.c 194 KB

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