xattr.c 194 KB

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