xattr.c 193 KB

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