xattr.c 194 KB

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