xattr.c 193 KB

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