xattr.c 189 KB

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