xattr.c 196 KB

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