xattr.c 194 KB

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