xattr.c 181 KB

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