xattr.c 193 KB

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