xattr.c 193 KB

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