xattr.c 186 KB

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