xattr.c 186 KB

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