xattr.c 192 KB

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