xattr.c 192 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313
  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. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  2115. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  2116. EXTENT_ALLOC_SYSTEM_INODE,
  2117. le16_to_cpu(xb->xb_suballoc_slot));
  2118. if (!xb_alloc_inode) {
  2119. ret = -ENOMEM;
  2120. mlog_errno(ret);
  2121. goto out;
  2122. }
  2123. mutex_lock(&xb_alloc_inode->i_mutex);
  2124. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  2125. if (ret < 0) {
  2126. mlog_errno(ret);
  2127. goto out_mutex;
  2128. }
  2129. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  2130. if (IS_ERR(handle)) {
  2131. ret = PTR_ERR(handle);
  2132. mlog_errno(ret);
  2133. goto out_unlock;
  2134. }
  2135. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  2136. bit, bg_blkno, 1);
  2137. if (ret < 0)
  2138. mlog_errno(ret);
  2139. ocfs2_commit_trans(osb, handle);
  2140. out_unlock:
  2141. ocfs2_inode_unlock(xb_alloc_inode, 1);
  2142. brelse(xb_alloc_bh);
  2143. out_mutex:
  2144. mutex_unlock(&xb_alloc_inode->i_mutex);
  2145. iput(xb_alloc_inode);
  2146. out:
  2147. brelse(blk_bh);
  2148. return ret;
  2149. }
  2150. /*
  2151. * ocfs2_xattr_remove()
  2152. *
  2153. * Free extended attribute resources associated with this inode.
  2154. */
  2155. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  2156. {
  2157. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2158. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2159. struct ocfs2_refcount_tree *ref_tree = NULL;
  2160. struct buffer_head *ref_root_bh = NULL;
  2161. struct ocfs2_caching_info *ref_ci = NULL;
  2162. handle_t *handle;
  2163. int ret;
  2164. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2165. return 0;
  2166. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  2167. return 0;
  2168. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
  2169. ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
  2170. le64_to_cpu(di->i_refcount_loc),
  2171. 1, &ref_tree, &ref_root_bh);
  2172. if (ret) {
  2173. mlog_errno(ret);
  2174. goto out;
  2175. }
  2176. ref_ci = &ref_tree->rf_ci;
  2177. }
  2178. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  2179. ret = ocfs2_xattr_ibody_remove(inode, di_bh,
  2180. ref_ci, ref_root_bh);
  2181. if (ret < 0) {
  2182. mlog_errno(ret);
  2183. goto out;
  2184. }
  2185. }
  2186. if (di->i_xattr_loc) {
  2187. ret = ocfs2_xattr_free_block(inode,
  2188. le64_to_cpu(di->i_xattr_loc),
  2189. ref_ci, ref_root_bh);
  2190. if (ret < 0) {
  2191. mlog_errno(ret);
  2192. goto out;
  2193. }
  2194. }
  2195. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  2196. OCFS2_INODE_UPDATE_CREDITS);
  2197. if (IS_ERR(handle)) {
  2198. ret = PTR_ERR(handle);
  2199. mlog_errno(ret);
  2200. goto out;
  2201. }
  2202. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  2203. OCFS2_JOURNAL_ACCESS_WRITE);
  2204. if (ret) {
  2205. mlog_errno(ret);
  2206. goto out_commit;
  2207. }
  2208. di->i_xattr_loc = 0;
  2209. spin_lock(&oi->ip_lock);
  2210. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  2211. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  2212. spin_unlock(&oi->ip_lock);
  2213. ocfs2_journal_dirty(handle, di_bh);
  2214. out_commit:
  2215. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  2216. out:
  2217. if (ref_tree)
  2218. ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
  2219. brelse(ref_root_bh);
  2220. return ret;
  2221. }
  2222. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  2223. struct ocfs2_dinode *di)
  2224. {
  2225. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2226. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  2227. int free;
  2228. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  2229. return 0;
  2230. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  2231. struct ocfs2_inline_data *idata = &di->id2.i_data;
  2232. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  2233. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  2234. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  2235. le64_to_cpu(di->i_size);
  2236. } else {
  2237. struct ocfs2_extent_list *el = &di->id2.i_list;
  2238. free = (le16_to_cpu(el->l_count) -
  2239. le16_to_cpu(el->l_next_free_rec)) *
  2240. sizeof(struct ocfs2_extent_rec);
  2241. }
  2242. if (free >= xattrsize)
  2243. return 1;
  2244. return 0;
  2245. }
  2246. /*
  2247. * ocfs2_xattr_ibody_find()
  2248. *
  2249. * Find extended attribute in inode block and
  2250. * fill search info into struct ocfs2_xattr_search.
  2251. */
  2252. static int ocfs2_xattr_ibody_find(struct inode *inode,
  2253. int name_index,
  2254. const char *name,
  2255. struct ocfs2_xattr_search *xs)
  2256. {
  2257. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2258. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  2259. int ret;
  2260. int has_space = 0;
  2261. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  2262. return 0;
  2263. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  2264. down_read(&oi->ip_alloc_sem);
  2265. has_space = ocfs2_xattr_has_space_inline(inode, di);
  2266. up_read(&oi->ip_alloc_sem);
  2267. if (!has_space)
  2268. return 0;
  2269. }
  2270. xs->xattr_bh = xs->inode_bh;
  2271. xs->end = (void *)di + inode->i_sb->s_blocksize;
  2272. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  2273. xs->header = (struct ocfs2_xattr_header *)
  2274. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  2275. else
  2276. xs->header = (struct ocfs2_xattr_header *)
  2277. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  2278. xs->base = (void *)xs->header;
  2279. xs->here = xs->header->xh_entries;
  2280. /* Find the named attribute. */
  2281. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  2282. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  2283. if (ret && ret != -ENODATA)
  2284. return ret;
  2285. xs->not_found = ret;
  2286. }
  2287. return 0;
  2288. }
  2289. static int ocfs2_xattr_ibody_init(struct inode *inode,
  2290. struct buffer_head *di_bh,
  2291. struct ocfs2_xattr_set_ctxt *ctxt)
  2292. {
  2293. int ret;
  2294. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2295. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  2296. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2297. unsigned int xattrsize = osb->s_xattr_inline_size;
  2298. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  2299. ret = -ENOSPC;
  2300. goto out;
  2301. }
  2302. ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
  2303. OCFS2_JOURNAL_ACCESS_WRITE);
  2304. if (ret) {
  2305. mlog_errno(ret);
  2306. goto out;
  2307. }
  2308. /*
  2309. * Adjust extent record count or inline data size
  2310. * to reserve space for extended attribute.
  2311. */
  2312. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  2313. struct ocfs2_inline_data *idata = &di->id2.i_data;
  2314. le16_add_cpu(&idata->id_count, -xattrsize);
  2315. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  2316. struct ocfs2_extent_list *el = &di->id2.i_list;
  2317. le16_add_cpu(&el->l_count, -(xattrsize /
  2318. sizeof(struct ocfs2_extent_rec)));
  2319. }
  2320. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  2321. spin_lock(&oi->ip_lock);
  2322. oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
  2323. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  2324. spin_unlock(&oi->ip_lock);
  2325. ocfs2_journal_dirty(ctxt->handle, di_bh);
  2326. out:
  2327. return ret;
  2328. }
  2329. /*
  2330. * ocfs2_xattr_ibody_set()
  2331. *
  2332. * Set, replace or remove an extended attribute into inode block.
  2333. *
  2334. */
  2335. static int ocfs2_xattr_ibody_set(struct inode *inode,
  2336. struct ocfs2_xattr_info *xi,
  2337. struct ocfs2_xattr_search *xs,
  2338. struct ocfs2_xattr_set_ctxt *ctxt)
  2339. {
  2340. int ret;
  2341. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2342. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  2343. struct ocfs2_xa_loc loc;
  2344. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  2345. return -ENOSPC;
  2346. down_write(&oi->ip_alloc_sem);
  2347. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  2348. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  2349. ret = -ENOSPC;
  2350. goto out;
  2351. }
  2352. }
  2353. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  2354. ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
  2355. if (ret) {
  2356. if (ret != -ENOSPC)
  2357. mlog_errno(ret);
  2358. goto out;
  2359. }
  2360. }
  2361. ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
  2362. xs->not_found ? NULL : xs->here);
  2363. ret = ocfs2_xa_set(&loc, xi, ctxt);
  2364. if (ret) {
  2365. if (ret != -ENOSPC)
  2366. mlog_errno(ret);
  2367. goto out;
  2368. }
  2369. xs->here = loc.xl_entry;
  2370. out:
  2371. up_write(&oi->ip_alloc_sem);
  2372. return ret;
  2373. }
  2374. /*
  2375. * ocfs2_xattr_block_find()
  2376. *
  2377. * Find extended attribute in external block and
  2378. * fill search info into struct ocfs2_xattr_search.
  2379. */
  2380. static int ocfs2_xattr_block_find(struct inode *inode,
  2381. int name_index,
  2382. const char *name,
  2383. struct ocfs2_xattr_search *xs)
  2384. {
  2385. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  2386. struct buffer_head *blk_bh = NULL;
  2387. struct ocfs2_xattr_block *xb;
  2388. int ret = 0;
  2389. if (!di->i_xattr_loc)
  2390. return ret;
  2391. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  2392. &blk_bh);
  2393. if (ret < 0) {
  2394. mlog_errno(ret);
  2395. return ret;
  2396. }
  2397. xs->xattr_bh = blk_bh;
  2398. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  2399. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  2400. xs->header = &xb->xb_attrs.xb_header;
  2401. xs->base = (void *)xs->header;
  2402. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  2403. xs->here = xs->header->xh_entries;
  2404. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  2405. } else
  2406. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  2407. name_index,
  2408. name, xs);
  2409. if (ret && ret != -ENODATA) {
  2410. xs->xattr_bh = NULL;
  2411. goto cleanup;
  2412. }
  2413. xs->not_found = ret;
  2414. return 0;
  2415. cleanup:
  2416. brelse(blk_bh);
  2417. return ret;
  2418. }
  2419. static int ocfs2_create_xattr_block(struct inode *inode,
  2420. struct buffer_head *inode_bh,
  2421. struct ocfs2_xattr_set_ctxt *ctxt,
  2422. int indexed,
  2423. struct buffer_head **ret_bh)
  2424. {
  2425. int ret;
  2426. u16 suballoc_bit_start;
  2427. u32 num_got;
  2428. u64 first_blkno;
  2429. struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
  2430. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2431. struct buffer_head *new_bh = NULL;
  2432. struct ocfs2_xattr_block *xblk;
  2433. ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
  2434. inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
  2435. if (ret < 0) {
  2436. mlog_errno(ret);
  2437. goto end;
  2438. }
  2439. ret = ocfs2_claim_metadata(osb, ctxt->handle, ctxt->meta_ac, 1,
  2440. &suballoc_bit_start, &num_got,
  2441. &first_blkno);
  2442. if (ret < 0) {
  2443. mlog_errno(ret);
  2444. goto end;
  2445. }
  2446. new_bh = sb_getblk(inode->i_sb, first_blkno);
  2447. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
  2448. ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
  2449. new_bh,
  2450. OCFS2_JOURNAL_ACCESS_CREATE);
  2451. if (ret < 0) {
  2452. mlog_errno(ret);
  2453. goto end;
  2454. }
  2455. /* Initialize ocfs2_xattr_block */
  2456. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  2457. memset(xblk, 0, inode->i_sb->s_blocksize);
  2458. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  2459. xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
  2460. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  2461. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  2462. xblk->xb_blkno = cpu_to_le64(first_blkno);
  2463. if (indexed) {
  2464. struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
  2465. xr->xt_clusters = cpu_to_le32(1);
  2466. xr->xt_last_eb_blk = 0;
  2467. xr->xt_list.l_tree_depth = 0;
  2468. xr->xt_list.l_count = cpu_to_le16(
  2469. ocfs2_xattr_recs_per_xb(inode->i_sb));
  2470. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  2471. xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
  2472. }
  2473. ocfs2_journal_dirty(ctxt->handle, new_bh);
  2474. /* Add it to the inode */
  2475. di->i_xattr_loc = cpu_to_le64(first_blkno);
  2476. spin_lock(&OCFS2_I(inode)->ip_lock);
  2477. OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
  2478. di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  2479. spin_unlock(&OCFS2_I(inode)->ip_lock);
  2480. ocfs2_journal_dirty(ctxt->handle, inode_bh);
  2481. *ret_bh = new_bh;
  2482. new_bh = NULL;
  2483. end:
  2484. brelse(new_bh);
  2485. return ret;
  2486. }
  2487. /*
  2488. * ocfs2_xattr_block_set()
  2489. *
  2490. * Set, replace or remove an extended attribute into external block.
  2491. *
  2492. */
  2493. static int ocfs2_xattr_block_set(struct inode *inode,
  2494. struct ocfs2_xattr_info *xi,
  2495. struct ocfs2_xattr_search *xs,
  2496. struct ocfs2_xattr_set_ctxt *ctxt)
  2497. {
  2498. struct buffer_head *new_bh = NULL;
  2499. struct ocfs2_xattr_block *xblk = NULL;
  2500. int ret;
  2501. struct ocfs2_xa_loc loc;
  2502. if (!xs->xattr_bh) {
  2503. ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
  2504. 0, &new_bh);
  2505. if (ret) {
  2506. mlog_errno(ret);
  2507. goto end;
  2508. }
  2509. xs->xattr_bh = new_bh;
  2510. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  2511. xs->header = &xblk->xb_attrs.xb_header;
  2512. xs->base = (void *)xs->header;
  2513. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  2514. xs->here = xs->header->xh_entries;
  2515. } else
  2516. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  2517. if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
  2518. ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
  2519. xs->not_found ? NULL : xs->here);
  2520. ret = ocfs2_xa_set(&loc, xi, ctxt);
  2521. if (!ret)
  2522. xs->here = loc.xl_entry;
  2523. else if (ret != -ENOSPC)
  2524. goto end;
  2525. else {
  2526. ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
  2527. if (ret)
  2528. goto end;
  2529. }
  2530. }
  2531. if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
  2532. ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
  2533. end:
  2534. return ret;
  2535. }
  2536. /* Check whether the new xattr can be inserted into the inode. */
  2537. static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
  2538. struct ocfs2_xattr_info *xi,
  2539. struct ocfs2_xattr_search *xs)
  2540. {
  2541. struct ocfs2_xattr_entry *last;
  2542. int free, i;
  2543. size_t min_offs = xs->end - xs->base;
  2544. if (!xs->header)
  2545. return 0;
  2546. last = xs->header->xh_entries;
  2547. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  2548. size_t offs = le16_to_cpu(last->xe_name_offset);
  2549. if (offs < min_offs)
  2550. min_offs = offs;
  2551. last += 1;
  2552. }
  2553. free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
  2554. if (free < 0)
  2555. return 0;
  2556. BUG_ON(!xs->not_found);
  2557. if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
  2558. return 1;
  2559. return 0;
  2560. }
  2561. static int ocfs2_calc_xattr_set_need(struct inode *inode,
  2562. struct ocfs2_dinode *di,
  2563. struct ocfs2_xattr_info *xi,
  2564. struct ocfs2_xattr_search *xis,
  2565. struct ocfs2_xattr_search *xbs,
  2566. int *clusters_need,
  2567. int *meta_need,
  2568. int *credits_need)
  2569. {
  2570. int ret = 0, old_in_xb = 0;
  2571. int clusters_add = 0, meta_add = 0, credits = 0;
  2572. struct buffer_head *bh = NULL;
  2573. struct ocfs2_xattr_block *xb = NULL;
  2574. struct ocfs2_xattr_entry *xe = NULL;
  2575. struct ocfs2_xattr_value_root *xv = NULL;
  2576. char *base = NULL;
  2577. int name_offset, name_len = 0;
  2578. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  2579. xi->xi_value_len);
  2580. u64 value_size;
  2581. /*
  2582. * Calculate the clusters we need to write.
  2583. * No matter whether we replace an old one or add a new one,
  2584. * we need this for writing.
  2585. */
  2586. if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
  2587. credits += new_clusters *
  2588. ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2589. if (xis->not_found && xbs->not_found) {
  2590. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2591. if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
  2592. clusters_add += new_clusters;
  2593. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2594. &def_xv.xv.xr_list,
  2595. new_clusters);
  2596. }
  2597. goto meta_guess;
  2598. }
  2599. if (!xis->not_found) {
  2600. xe = xis->here;
  2601. name_offset = le16_to_cpu(xe->xe_name_offset);
  2602. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2603. base = xis->base;
  2604. credits += OCFS2_INODE_UPDATE_CREDITS;
  2605. } else {
  2606. int i, block_off = 0;
  2607. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  2608. xe = xbs->here;
  2609. name_offset = le16_to_cpu(xe->xe_name_offset);
  2610. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2611. i = xbs->here - xbs->header->xh_entries;
  2612. old_in_xb = 1;
  2613. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  2614. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  2615. bucket_xh(xbs->bucket),
  2616. i, &block_off,
  2617. &name_offset);
  2618. base = bucket_block(xbs->bucket, block_off);
  2619. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2620. } else {
  2621. base = xbs->base;
  2622. credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
  2623. }
  2624. }
  2625. /*
  2626. * delete a xattr doesn't need metadata and cluster allocation.
  2627. * so just calculate the credits and return.
  2628. *
  2629. * The credits for removing the value tree will be extended
  2630. * by ocfs2_remove_extent itself.
  2631. */
  2632. if (!xi->xi_value) {
  2633. if (!ocfs2_xattr_is_local(xe))
  2634. credits += ocfs2_remove_extent_credits(inode->i_sb);
  2635. goto out;
  2636. }
  2637. /* do cluster allocation guess first. */
  2638. value_size = le64_to_cpu(xe->xe_value_size);
  2639. if (old_in_xb) {
  2640. /*
  2641. * In xattr set, we always try to set the xe in inode first,
  2642. * so if it can be inserted into inode successfully, the old
  2643. * one will be removed from the xattr block, and this xattr
  2644. * will be inserted into inode as a new xattr in inode.
  2645. */
  2646. if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
  2647. clusters_add += new_clusters;
  2648. credits += ocfs2_remove_extent_credits(inode->i_sb) +
  2649. OCFS2_INODE_UPDATE_CREDITS;
  2650. if (!ocfs2_xattr_is_local(xe))
  2651. credits += ocfs2_calc_extend_credits(
  2652. inode->i_sb,
  2653. &def_xv.xv.xr_list,
  2654. new_clusters);
  2655. goto out;
  2656. }
  2657. }
  2658. if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
  2659. /* the new values will be stored outside. */
  2660. u32 old_clusters = 0;
  2661. if (!ocfs2_xattr_is_local(xe)) {
  2662. old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  2663. value_size);
  2664. xv = (struct ocfs2_xattr_value_root *)
  2665. (base + name_offset + name_len);
  2666. value_size = OCFS2_XATTR_ROOT_SIZE;
  2667. } else
  2668. xv = &def_xv.xv;
  2669. if (old_clusters >= new_clusters) {
  2670. credits += ocfs2_remove_extent_credits(inode->i_sb);
  2671. goto out;
  2672. } else {
  2673. meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
  2674. clusters_add += new_clusters - old_clusters;
  2675. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2676. &xv->xr_list,
  2677. new_clusters -
  2678. old_clusters);
  2679. if (value_size >= OCFS2_XATTR_ROOT_SIZE)
  2680. goto out;
  2681. }
  2682. } else {
  2683. /*
  2684. * Now the new value will be stored inside. So if the new
  2685. * value is smaller than the size of value root or the old
  2686. * value, we don't need any allocation, otherwise we have
  2687. * to guess metadata allocation.
  2688. */
  2689. if ((ocfs2_xattr_is_local(xe) &&
  2690. (value_size >= xi->xi_value_len)) ||
  2691. (!ocfs2_xattr_is_local(xe) &&
  2692. OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
  2693. goto out;
  2694. }
  2695. meta_guess:
  2696. /* calculate metadata allocation. */
  2697. if (di->i_xattr_loc) {
  2698. if (!xbs->xattr_bh) {
  2699. ret = ocfs2_read_xattr_block(inode,
  2700. le64_to_cpu(di->i_xattr_loc),
  2701. &bh);
  2702. if (ret) {
  2703. mlog_errno(ret);
  2704. goto out;
  2705. }
  2706. xb = (struct ocfs2_xattr_block *)bh->b_data;
  2707. } else
  2708. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  2709. /*
  2710. * If there is already an xattr tree, good, we can calculate
  2711. * like other b-trees. Otherwise we may have the chance of
  2712. * create a tree, the credit calculation is borrowed from
  2713. * ocfs2_calc_extend_credits with root_el = NULL. And the
  2714. * new tree will be cluster based, so no meta is needed.
  2715. */
  2716. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  2717. struct ocfs2_extent_list *el =
  2718. &xb->xb_attrs.xb_root.xt_list;
  2719. meta_add += ocfs2_extend_meta_needed(el);
  2720. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2721. el, 1);
  2722. } else
  2723. credits += OCFS2_SUBALLOC_ALLOC + 1;
  2724. /*
  2725. * This cluster will be used either for new bucket or for
  2726. * new xattr block.
  2727. * If the cluster size is the same as the bucket size, one
  2728. * more is needed since we may need to extend the bucket
  2729. * also.
  2730. */
  2731. clusters_add += 1;
  2732. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2733. if (OCFS2_XATTR_BUCKET_SIZE ==
  2734. OCFS2_SB(inode->i_sb)->s_clustersize) {
  2735. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2736. clusters_add += 1;
  2737. }
  2738. } else {
  2739. meta_add += 1;
  2740. credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  2741. }
  2742. out:
  2743. if (clusters_need)
  2744. *clusters_need = clusters_add;
  2745. if (meta_need)
  2746. *meta_need = meta_add;
  2747. if (credits_need)
  2748. *credits_need = credits;
  2749. brelse(bh);
  2750. return ret;
  2751. }
  2752. static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
  2753. struct ocfs2_dinode *di,
  2754. struct ocfs2_xattr_info *xi,
  2755. struct ocfs2_xattr_search *xis,
  2756. struct ocfs2_xattr_search *xbs,
  2757. struct ocfs2_xattr_set_ctxt *ctxt,
  2758. int extra_meta,
  2759. int *credits)
  2760. {
  2761. int clusters_add, meta_add, ret;
  2762. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2763. memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
  2764. ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
  2765. ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
  2766. &clusters_add, &meta_add, credits);
  2767. if (ret) {
  2768. mlog_errno(ret);
  2769. return ret;
  2770. }
  2771. meta_add += extra_meta;
  2772. mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
  2773. "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
  2774. if (meta_add) {
  2775. ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
  2776. &ctxt->meta_ac);
  2777. if (ret) {
  2778. mlog_errno(ret);
  2779. goto out;
  2780. }
  2781. }
  2782. if (clusters_add) {
  2783. ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
  2784. if (ret)
  2785. mlog_errno(ret);
  2786. }
  2787. out:
  2788. if (ret) {
  2789. if (ctxt->meta_ac) {
  2790. ocfs2_free_alloc_context(ctxt->meta_ac);
  2791. ctxt->meta_ac = NULL;
  2792. }
  2793. /*
  2794. * We cannot have an error and a non null ctxt->data_ac.
  2795. */
  2796. }
  2797. return ret;
  2798. }
  2799. static int __ocfs2_xattr_set_handle(struct inode *inode,
  2800. struct ocfs2_dinode *di,
  2801. struct ocfs2_xattr_info *xi,
  2802. struct ocfs2_xattr_search *xis,
  2803. struct ocfs2_xattr_search *xbs,
  2804. struct ocfs2_xattr_set_ctxt *ctxt)
  2805. {
  2806. int ret = 0, credits, old_found;
  2807. if (!xi->xi_value) {
  2808. /* Remove existing extended attribute */
  2809. if (!xis->not_found)
  2810. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2811. else if (!xbs->not_found)
  2812. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2813. } else {
  2814. /* We always try to set extended attribute into inode first*/
  2815. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2816. if (!ret && !xbs->not_found) {
  2817. /*
  2818. * If succeed and that extended attribute existing in
  2819. * external block, then we will remove it.
  2820. */
  2821. xi->xi_value = NULL;
  2822. xi->xi_value_len = 0;
  2823. old_found = xis->not_found;
  2824. xis->not_found = -ENODATA;
  2825. ret = ocfs2_calc_xattr_set_need(inode,
  2826. di,
  2827. xi,
  2828. xis,
  2829. xbs,
  2830. NULL,
  2831. NULL,
  2832. &credits);
  2833. xis->not_found = old_found;
  2834. if (ret) {
  2835. mlog_errno(ret);
  2836. goto out;
  2837. }
  2838. ret = ocfs2_extend_trans(ctxt->handle, credits);
  2839. if (ret) {
  2840. mlog_errno(ret);
  2841. goto out;
  2842. }
  2843. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2844. } else if (ret == -ENOSPC) {
  2845. if (di->i_xattr_loc && !xbs->xattr_bh) {
  2846. ret = ocfs2_xattr_block_find(inode,
  2847. xi->xi_name_index,
  2848. xi->xi_name, xbs);
  2849. if (ret)
  2850. goto out;
  2851. old_found = xis->not_found;
  2852. xis->not_found = -ENODATA;
  2853. ret = ocfs2_calc_xattr_set_need(inode,
  2854. di,
  2855. xi,
  2856. xis,
  2857. xbs,
  2858. NULL,
  2859. NULL,
  2860. &credits);
  2861. xis->not_found = old_found;
  2862. if (ret) {
  2863. mlog_errno(ret);
  2864. goto out;
  2865. }
  2866. ret = ocfs2_extend_trans(ctxt->handle, credits);
  2867. if (ret) {
  2868. mlog_errno(ret);
  2869. goto out;
  2870. }
  2871. }
  2872. /*
  2873. * If no space in inode, we will set extended attribute
  2874. * into external block.
  2875. */
  2876. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2877. if (ret)
  2878. goto out;
  2879. if (!xis->not_found) {
  2880. /*
  2881. * If succeed and that extended attribute
  2882. * existing in inode, we will remove it.
  2883. */
  2884. xi->xi_value = NULL;
  2885. xi->xi_value_len = 0;
  2886. xbs->not_found = -ENODATA;
  2887. ret = ocfs2_calc_xattr_set_need(inode,
  2888. di,
  2889. xi,
  2890. xis,
  2891. xbs,
  2892. NULL,
  2893. NULL,
  2894. &credits);
  2895. if (ret) {
  2896. mlog_errno(ret);
  2897. goto out;
  2898. }
  2899. ret = ocfs2_extend_trans(ctxt->handle, credits);
  2900. if (ret) {
  2901. mlog_errno(ret);
  2902. goto out;
  2903. }
  2904. ret = ocfs2_xattr_ibody_set(inode, xi,
  2905. xis, ctxt);
  2906. }
  2907. }
  2908. }
  2909. if (!ret) {
  2910. /* Update inode ctime. */
  2911. ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
  2912. xis->inode_bh,
  2913. OCFS2_JOURNAL_ACCESS_WRITE);
  2914. if (ret) {
  2915. mlog_errno(ret);
  2916. goto out;
  2917. }
  2918. inode->i_ctime = CURRENT_TIME;
  2919. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  2920. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  2921. ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
  2922. }
  2923. out:
  2924. return ret;
  2925. }
  2926. /*
  2927. * This function only called duing creating inode
  2928. * for init security/acl xattrs of the new inode.
  2929. * All transanction credits have been reserved in mknod.
  2930. */
  2931. int ocfs2_xattr_set_handle(handle_t *handle,
  2932. struct inode *inode,
  2933. struct buffer_head *di_bh,
  2934. int name_index,
  2935. const char *name,
  2936. const void *value,
  2937. size_t value_len,
  2938. int flags,
  2939. struct ocfs2_alloc_context *meta_ac,
  2940. struct ocfs2_alloc_context *data_ac)
  2941. {
  2942. struct ocfs2_dinode *di;
  2943. int ret;
  2944. struct ocfs2_xattr_info xi = {
  2945. .xi_name_index = name_index,
  2946. .xi_name = name,
  2947. .xi_name_len = strlen(name),
  2948. .xi_value = value,
  2949. .xi_value_len = value_len,
  2950. };
  2951. struct ocfs2_xattr_search xis = {
  2952. .not_found = -ENODATA,
  2953. };
  2954. struct ocfs2_xattr_search xbs = {
  2955. .not_found = -ENODATA,
  2956. };
  2957. struct ocfs2_xattr_set_ctxt ctxt = {
  2958. .handle = handle,
  2959. .meta_ac = meta_ac,
  2960. .data_ac = data_ac,
  2961. };
  2962. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2963. return -EOPNOTSUPP;
  2964. /*
  2965. * In extreme situation, may need xattr bucket when
  2966. * block size is too small. And we have already reserved
  2967. * the credits for bucket in mknod.
  2968. */
  2969. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
  2970. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  2971. if (!xbs.bucket) {
  2972. mlog_errno(-ENOMEM);
  2973. return -ENOMEM;
  2974. }
  2975. }
  2976. xis.inode_bh = xbs.inode_bh = di_bh;
  2977. di = (struct ocfs2_dinode *)di_bh->b_data;
  2978. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  2979. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  2980. if (ret)
  2981. goto cleanup;
  2982. if (xis.not_found) {
  2983. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  2984. if (ret)
  2985. goto cleanup;
  2986. }
  2987. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  2988. cleanup:
  2989. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  2990. brelse(xbs.xattr_bh);
  2991. ocfs2_xattr_bucket_free(xbs.bucket);
  2992. return ret;
  2993. }
  2994. /*
  2995. * ocfs2_xattr_set()
  2996. *
  2997. * Set, replace or remove an extended attribute for this inode.
  2998. * value is NULL to remove an existing extended attribute, else either
  2999. * create or replace an extended attribute.
  3000. */
  3001. int ocfs2_xattr_set(struct inode *inode,
  3002. int name_index,
  3003. const char *name,
  3004. const void *value,
  3005. size_t value_len,
  3006. int flags)
  3007. {
  3008. struct buffer_head *di_bh = NULL;
  3009. struct ocfs2_dinode *di;
  3010. int ret, credits, ref_meta = 0, ref_credits = 0;
  3011. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3012. struct inode *tl_inode = osb->osb_tl_inode;
  3013. struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
  3014. struct ocfs2_refcount_tree *ref_tree = NULL;
  3015. struct ocfs2_xattr_info xi = {
  3016. .xi_name_index = name_index,
  3017. .xi_name = name,
  3018. .xi_name_len = strlen(name),
  3019. .xi_value = value,
  3020. .xi_value_len = value_len,
  3021. };
  3022. struct ocfs2_xattr_search xis = {
  3023. .not_found = -ENODATA,
  3024. };
  3025. struct ocfs2_xattr_search xbs = {
  3026. .not_found = -ENODATA,
  3027. };
  3028. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  3029. return -EOPNOTSUPP;
  3030. /*
  3031. * Only xbs will be used on indexed trees. xis doesn't need a
  3032. * bucket.
  3033. */
  3034. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  3035. if (!xbs.bucket) {
  3036. mlog_errno(-ENOMEM);
  3037. return -ENOMEM;
  3038. }
  3039. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  3040. if (ret < 0) {
  3041. mlog_errno(ret);
  3042. goto cleanup_nolock;
  3043. }
  3044. xis.inode_bh = xbs.inode_bh = di_bh;
  3045. di = (struct ocfs2_dinode *)di_bh->b_data;
  3046. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  3047. /*
  3048. * Scan inode and external block to find the same name
  3049. * extended attribute and collect search infomation.
  3050. */
  3051. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  3052. if (ret)
  3053. goto cleanup;
  3054. if (xis.not_found) {
  3055. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  3056. if (ret)
  3057. goto cleanup;
  3058. }
  3059. if (xis.not_found && xbs.not_found) {
  3060. ret = -ENODATA;
  3061. if (flags & XATTR_REPLACE)
  3062. goto cleanup;
  3063. ret = 0;
  3064. if (!value)
  3065. goto cleanup;
  3066. } else {
  3067. ret = -EEXIST;
  3068. if (flags & XATTR_CREATE)
  3069. goto cleanup;
  3070. }
  3071. /* Check whether the value is refcounted and do some prepartion. */
  3072. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
  3073. (!xis.not_found || !xbs.not_found)) {
  3074. ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
  3075. &xis, &xbs, &ref_tree,
  3076. &ref_meta, &ref_credits);
  3077. if (ret) {
  3078. mlog_errno(ret);
  3079. goto cleanup;
  3080. }
  3081. }
  3082. mutex_lock(&tl_inode->i_mutex);
  3083. if (ocfs2_truncate_log_needs_flush(osb)) {
  3084. ret = __ocfs2_flush_truncate_log(osb);
  3085. if (ret < 0) {
  3086. mutex_unlock(&tl_inode->i_mutex);
  3087. mlog_errno(ret);
  3088. goto cleanup;
  3089. }
  3090. }
  3091. mutex_unlock(&tl_inode->i_mutex);
  3092. ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
  3093. &xbs, &ctxt, ref_meta, &credits);
  3094. if (ret) {
  3095. mlog_errno(ret);
  3096. goto cleanup;
  3097. }
  3098. /* we need to update inode's ctime field, so add credit for it. */
  3099. credits += OCFS2_INODE_UPDATE_CREDITS;
  3100. ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
  3101. if (IS_ERR(ctxt.handle)) {
  3102. ret = PTR_ERR(ctxt.handle);
  3103. mlog_errno(ret);
  3104. goto cleanup;
  3105. }
  3106. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  3107. ocfs2_commit_trans(osb, ctxt.handle);
  3108. if (ctxt.data_ac)
  3109. ocfs2_free_alloc_context(ctxt.data_ac);
  3110. if (ctxt.meta_ac)
  3111. ocfs2_free_alloc_context(ctxt.meta_ac);
  3112. if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
  3113. ocfs2_schedule_truncate_log_flush(osb, 1);
  3114. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  3115. cleanup:
  3116. if (ref_tree)
  3117. ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
  3118. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  3119. if (!value && !ret) {
  3120. ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
  3121. if (ret)
  3122. mlog_errno(ret);
  3123. }
  3124. ocfs2_inode_unlock(inode, 1);
  3125. cleanup_nolock:
  3126. brelse(di_bh);
  3127. brelse(xbs.xattr_bh);
  3128. ocfs2_xattr_bucket_free(xbs.bucket);
  3129. return ret;
  3130. }
  3131. /*
  3132. * Find the xattr extent rec which may contains name_hash.
  3133. * e_cpos will be the first name hash of the xattr rec.
  3134. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  3135. */
  3136. static int ocfs2_xattr_get_rec(struct inode *inode,
  3137. u32 name_hash,
  3138. u64 *p_blkno,
  3139. u32 *e_cpos,
  3140. u32 *num_clusters,
  3141. struct ocfs2_extent_list *el)
  3142. {
  3143. int ret = 0, i;
  3144. struct buffer_head *eb_bh = NULL;
  3145. struct ocfs2_extent_block *eb;
  3146. struct ocfs2_extent_rec *rec = NULL;
  3147. u64 e_blkno = 0;
  3148. if (el->l_tree_depth) {
  3149. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
  3150. &eb_bh);
  3151. if (ret) {
  3152. mlog_errno(ret);
  3153. goto out;
  3154. }
  3155. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  3156. el = &eb->h_list;
  3157. if (el->l_tree_depth) {
  3158. ocfs2_error(inode->i_sb,
  3159. "Inode %lu has non zero tree depth in "
  3160. "xattr tree block %llu\n", inode->i_ino,
  3161. (unsigned long long)eb_bh->b_blocknr);
  3162. ret = -EROFS;
  3163. goto out;
  3164. }
  3165. }
  3166. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  3167. rec = &el->l_recs[i];
  3168. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  3169. e_blkno = le64_to_cpu(rec->e_blkno);
  3170. break;
  3171. }
  3172. }
  3173. if (!e_blkno) {
  3174. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  3175. "record (%u, %u, 0) in xattr", inode->i_ino,
  3176. le32_to_cpu(rec->e_cpos),
  3177. ocfs2_rec_clusters(el, rec));
  3178. ret = -EROFS;
  3179. goto out;
  3180. }
  3181. *p_blkno = le64_to_cpu(rec->e_blkno);
  3182. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  3183. if (e_cpos)
  3184. *e_cpos = le32_to_cpu(rec->e_cpos);
  3185. out:
  3186. brelse(eb_bh);
  3187. return ret;
  3188. }
  3189. typedef int (xattr_bucket_func)(struct inode *inode,
  3190. struct ocfs2_xattr_bucket *bucket,
  3191. void *para);
  3192. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  3193. struct ocfs2_xattr_bucket *bucket,
  3194. int name_index,
  3195. const char *name,
  3196. u32 name_hash,
  3197. u16 *xe_index,
  3198. int *found)
  3199. {
  3200. int i, ret = 0, cmp = 1, block_off, new_offset;
  3201. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  3202. size_t name_len = strlen(name);
  3203. struct ocfs2_xattr_entry *xe = NULL;
  3204. char *xe_name;
  3205. /*
  3206. * We don't use binary search in the bucket because there
  3207. * may be multiple entries with the same name hash.
  3208. */
  3209. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  3210. xe = &xh->xh_entries[i];
  3211. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  3212. continue;
  3213. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  3214. break;
  3215. cmp = name_index - ocfs2_xattr_get_type(xe);
  3216. if (!cmp)
  3217. cmp = name_len - xe->xe_name_len;
  3218. if (cmp)
  3219. continue;
  3220. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  3221. xh,
  3222. i,
  3223. &block_off,
  3224. &new_offset);
  3225. if (ret) {
  3226. mlog_errno(ret);
  3227. break;
  3228. }
  3229. xe_name = bucket_block(bucket, block_off) + new_offset;
  3230. if (!memcmp(name, xe_name, name_len)) {
  3231. *xe_index = i;
  3232. *found = 1;
  3233. ret = 0;
  3234. break;
  3235. }
  3236. }
  3237. return ret;
  3238. }
  3239. /*
  3240. * Find the specified xattr entry in a series of buckets.
  3241. * This series start from p_blkno and last for num_clusters.
  3242. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  3243. * the num of the valid buckets.
  3244. *
  3245. * Return the buffer_head this xattr should reside in. And if the xattr's
  3246. * hash is in the gap of 2 buckets, return the lower bucket.
  3247. */
  3248. static int ocfs2_xattr_bucket_find(struct inode *inode,
  3249. int name_index,
  3250. const char *name,
  3251. u32 name_hash,
  3252. u64 p_blkno,
  3253. u32 first_hash,
  3254. u32 num_clusters,
  3255. struct ocfs2_xattr_search *xs)
  3256. {
  3257. int ret, found = 0;
  3258. struct ocfs2_xattr_header *xh = NULL;
  3259. struct ocfs2_xattr_entry *xe = NULL;
  3260. u16 index = 0;
  3261. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3262. int low_bucket = 0, bucket, high_bucket;
  3263. struct ocfs2_xattr_bucket *search;
  3264. u32 last_hash;
  3265. u64 blkno, lower_blkno = 0;
  3266. search = ocfs2_xattr_bucket_new(inode);
  3267. if (!search) {
  3268. ret = -ENOMEM;
  3269. mlog_errno(ret);
  3270. goto out;
  3271. }
  3272. ret = ocfs2_read_xattr_bucket(search, p_blkno);
  3273. if (ret) {
  3274. mlog_errno(ret);
  3275. goto out;
  3276. }
  3277. xh = bucket_xh(search);
  3278. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  3279. while (low_bucket <= high_bucket) {
  3280. ocfs2_xattr_bucket_relse(search);
  3281. bucket = (low_bucket + high_bucket) / 2;
  3282. blkno = p_blkno + bucket * blk_per_bucket;
  3283. ret = ocfs2_read_xattr_bucket(search, blkno);
  3284. if (ret) {
  3285. mlog_errno(ret);
  3286. goto out;
  3287. }
  3288. xh = bucket_xh(search);
  3289. xe = &xh->xh_entries[0];
  3290. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  3291. high_bucket = bucket - 1;
  3292. continue;
  3293. }
  3294. /*
  3295. * Check whether the hash of the last entry in our
  3296. * bucket is larger than the search one. for an empty
  3297. * bucket, the last one is also the first one.
  3298. */
  3299. if (xh->xh_count)
  3300. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  3301. last_hash = le32_to_cpu(xe->xe_name_hash);
  3302. /* record lower_blkno which may be the insert place. */
  3303. lower_blkno = blkno;
  3304. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  3305. low_bucket = bucket + 1;
  3306. continue;
  3307. }
  3308. /* the searched xattr should reside in this bucket if exists. */
  3309. ret = ocfs2_find_xe_in_bucket(inode, search,
  3310. name_index, name, name_hash,
  3311. &index, &found);
  3312. if (ret) {
  3313. mlog_errno(ret);
  3314. goto out;
  3315. }
  3316. break;
  3317. }
  3318. /*
  3319. * Record the bucket we have found.
  3320. * When the xattr's hash value is in the gap of 2 buckets, we will
  3321. * always set it to the previous bucket.
  3322. */
  3323. if (!lower_blkno)
  3324. lower_blkno = p_blkno;
  3325. /* This should be in cache - we just read it during the search */
  3326. ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
  3327. if (ret) {
  3328. mlog_errno(ret);
  3329. goto out;
  3330. }
  3331. xs->header = bucket_xh(xs->bucket);
  3332. xs->base = bucket_block(xs->bucket, 0);
  3333. xs->end = xs->base + inode->i_sb->s_blocksize;
  3334. if (found) {
  3335. xs->here = &xs->header->xh_entries[index];
  3336. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  3337. (unsigned long long)bucket_blkno(xs->bucket), index);
  3338. } else
  3339. ret = -ENODATA;
  3340. out:
  3341. ocfs2_xattr_bucket_free(search);
  3342. return ret;
  3343. }
  3344. static int ocfs2_xattr_index_block_find(struct inode *inode,
  3345. struct buffer_head *root_bh,
  3346. int name_index,
  3347. const char *name,
  3348. struct ocfs2_xattr_search *xs)
  3349. {
  3350. int ret;
  3351. struct ocfs2_xattr_block *xb =
  3352. (struct ocfs2_xattr_block *)root_bh->b_data;
  3353. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3354. struct ocfs2_extent_list *el = &xb_root->xt_list;
  3355. u64 p_blkno = 0;
  3356. u32 first_hash, num_clusters = 0;
  3357. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  3358. if (le16_to_cpu(el->l_next_free_rec) == 0)
  3359. return -ENODATA;
  3360. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  3361. name, name_hash, name_index);
  3362. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  3363. &num_clusters, el);
  3364. if (ret) {
  3365. mlog_errno(ret);
  3366. goto out;
  3367. }
  3368. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  3369. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  3370. "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
  3371. first_hash);
  3372. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  3373. p_blkno, first_hash, num_clusters, xs);
  3374. out:
  3375. return ret;
  3376. }
  3377. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  3378. u64 blkno,
  3379. u32 clusters,
  3380. xattr_bucket_func *func,
  3381. void *para)
  3382. {
  3383. int i, ret = 0;
  3384. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  3385. u32 num_buckets = clusters * bpc;
  3386. struct ocfs2_xattr_bucket *bucket;
  3387. bucket = ocfs2_xattr_bucket_new(inode);
  3388. if (!bucket) {
  3389. mlog_errno(-ENOMEM);
  3390. return -ENOMEM;
  3391. }
  3392. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  3393. clusters, (unsigned long long)blkno);
  3394. for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
  3395. ret = ocfs2_read_xattr_bucket(bucket, blkno);
  3396. if (ret) {
  3397. mlog_errno(ret);
  3398. break;
  3399. }
  3400. /*
  3401. * The real bucket num in this series of blocks is stored
  3402. * in the 1st bucket.
  3403. */
  3404. if (i == 0)
  3405. num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
  3406. mlog(0, "iterating xattr bucket %llu, first hash %u\n",
  3407. (unsigned long long)blkno,
  3408. le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
  3409. if (func) {
  3410. ret = func(inode, bucket, para);
  3411. if (ret && ret != -ERANGE)
  3412. mlog_errno(ret);
  3413. /* Fall through to bucket_relse() */
  3414. }
  3415. ocfs2_xattr_bucket_relse(bucket);
  3416. if (ret)
  3417. break;
  3418. }
  3419. ocfs2_xattr_bucket_free(bucket);
  3420. return ret;
  3421. }
  3422. struct ocfs2_xattr_tree_list {
  3423. char *buffer;
  3424. size_t buffer_size;
  3425. size_t result;
  3426. };
  3427. static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
  3428. struct ocfs2_xattr_header *xh,
  3429. int index,
  3430. int *block_off,
  3431. int *new_offset)
  3432. {
  3433. u16 name_offset;
  3434. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  3435. return -EINVAL;
  3436. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  3437. *block_off = name_offset >> sb->s_blocksize_bits;
  3438. *new_offset = name_offset % sb->s_blocksize;
  3439. return 0;
  3440. }
  3441. static int ocfs2_list_xattr_bucket(struct inode *inode,
  3442. struct ocfs2_xattr_bucket *bucket,
  3443. void *para)
  3444. {
  3445. int ret = 0, type;
  3446. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  3447. int i, block_off, new_offset;
  3448. const char *prefix, *name;
  3449. for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
  3450. struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
  3451. type = ocfs2_xattr_get_type(entry);
  3452. prefix = ocfs2_xattr_prefix(type);
  3453. if (prefix) {
  3454. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  3455. bucket_xh(bucket),
  3456. i,
  3457. &block_off,
  3458. &new_offset);
  3459. if (ret)
  3460. break;
  3461. name = (const char *)bucket_block(bucket, block_off) +
  3462. new_offset;
  3463. ret = ocfs2_xattr_list_entry(xl->buffer,
  3464. xl->buffer_size,
  3465. &xl->result,
  3466. prefix, name,
  3467. entry->xe_name_len);
  3468. if (ret)
  3469. break;
  3470. }
  3471. }
  3472. return ret;
  3473. }
  3474. static int ocfs2_iterate_xattr_index_block(struct inode *inode,
  3475. struct buffer_head *blk_bh,
  3476. xattr_tree_rec_func *rec_func,
  3477. void *para)
  3478. {
  3479. struct ocfs2_xattr_block *xb =
  3480. (struct ocfs2_xattr_block *)blk_bh->b_data;
  3481. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  3482. int ret = 0;
  3483. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  3484. u64 p_blkno = 0;
  3485. if (!el->l_next_free_rec || !rec_func)
  3486. return 0;
  3487. while (name_hash > 0) {
  3488. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  3489. &e_cpos, &num_clusters, el);
  3490. if (ret) {
  3491. mlog_errno(ret);
  3492. break;
  3493. }
  3494. ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
  3495. num_clusters, para);
  3496. if (ret) {
  3497. if (ret != -ERANGE)
  3498. mlog_errno(ret);
  3499. break;
  3500. }
  3501. if (e_cpos == 0)
  3502. break;
  3503. name_hash = e_cpos - 1;
  3504. }
  3505. return ret;
  3506. }
  3507. static int ocfs2_list_xattr_tree_rec(struct inode *inode,
  3508. struct buffer_head *root_bh,
  3509. u64 blkno, u32 cpos, u32 len, void *para)
  3510. {
  3511. return ocfs2_iterate_xattr_buckets(inode, blkno, len,
  3512. ocfs2_list_xattr_bucket, para);
  3513. }
  3514. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  3515. struct buffer_head *blk_bh,
  3516. char *buffer,
  3517. size_t buffer_size)
  3518. {
  3519. int ret;
  3520. struct ocfs2_xattr_tree_list xl = {
  3521. .buffer = buffer,
  3522. .buffer_size = buffer_size,
  3523. .result = 0,
  3524. };
  3525. ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
  3526. ocfs2_list_xattr_tree_rec, &xl);
  3527. if (ret) {
  3528. mlog_errno(ret);
  3529. goto out;
  3530. }
  3531. ret = xl.result;
  3532. out:
  3533. return ret;
  3534. }
  3535. static int cmp_xe(const void *a, const void *b)
  3536. {
  3537. const struct ocfs2_xattr_entry *l = a, *r = b;
  3538. u32 l_hash = le32_to_cpu(l->xe_name_hash);
  3539. u32 r_hash = le32_to_cpu(r->xe_name_hash);
  3540. if (l_hash > r_hash)
  3541. return 1;
  3542. if (l_hash < r_hash)
  3543. return -1;
  3544. return 0;
  3545. }
  3546. static void swap_xe(void *a, void *b, int size)
  3547. {
  3548. struct ocfs2_xattr_entry *l = a, *r = b, tmp;
  3549. tmp = *l;
  3550. memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
  3551. memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
  3552. }
  3553. /*
  3554. * When the ocfs2_xattr_block is filled up, new bucket will be created
  3555. * and all the xattr entries will be moved to the new bucket.
  3556. * The header goes at the start of the bucket, and the names+values are
  3557. * filled from the end. This is why *target starts as the last buffer.
  3558. * Note: we need to sort the entries since they are not saved in order
  3559. * in the ocfs2_xattr_block.
  3560. */
  3561. static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  3562. struct buffer_head *xb_bh,
  3563. struct ocfs2_xattr_bucket *bucket)
  3564. {
  3565. int i, blocksize = inode->i_sb->s_blocksize;
  3566. int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3567. u16 offset, size, off_change;
  3568. struct ocfs2_xattr_entry *xe;
  3569. struct ocfs2_xattr_block *xb =
  3570. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3571. struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
  3572. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  3573. u16 count = le16_to_cpu(xb_xh->xh_count);
  3574. char *src = xb_bh->b_data;
  3575. char *target = bucket_block(bucket, blks - 1);
  3576. mlog(0, "cp xattr from block %llu to bucket %llu\n",
  3577. (unsigned long long)xb_bh->b_blocknr,
  3578. (unsigned long long)bucket_blkno(bucket));
  3579. for (i = 0; i < blks; i++)
  3580. memset(bucket_block(bucket, i), 0, blocksize);
  3581. /*
  3582. * Since the xe_name_offset is based on ocfs2_xattr_header,
  3583. * there is a offset change corresponding to the change of
  3584. * ocfs2_xattr_header's position.
  3585. */
  3586. off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  3587. xe = &xb_xh->xh_entries[count - 1];
  3588. offset = le16_to_cpu(xe->xe_name_offset) + off_change;
  3589. size = blocksize - offset;
  3590. /* copy all the names and values. */
  3591. memcpy(target + offset, src + offset, size);
  3592. /* Init new header now. */
  3593. xh->xh_count = xb_xh->xh_count;
  3594. xh->xh_num_buckets = cpu_to_le16(1);
  3595. xh->xh_name_value_len = cpu_to_le16(size);
  3596. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
  3597. /* copy all the entries. */
  3598. target = bucket_block(bucket, 0);
  3599. offset = offsetof(struct ocfs2_xattr_header, xh_entries);
  3600. size = count * sizeof(struct ocfs2_xattr_entry);
  3601. memcpy(target + offset, (char *)xb_xh + offset, size);
  3602. /* Change the xe offset for all the xe because of the move. */
  3603. off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
  3604. offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  3605. for (i = 0; i < count; i++)
  3606. le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
  3607. mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
  3608. offset, size, off_change);
  3609. sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
  3610. cmp_xe, swap_xe);
  3611. }
  3612. /*
  3613. * After we move xattr from block to index btree, we have to
  3614. * update ocfs2_xattr_search to the new xe and base.
  3615. *
  3616. * When the entry is in xattr block, xattr_bh indicates the storage place.
  3617. * While if the entry is in index b-tree, "bucket" indicates the
  3618. * real place of the xattr.
  3619. */
  3620. static void ocfs2_xattr_update_xattr_search(struct inode *inode,
  3621. struct ocfs2_xattr_search *xs,
  3622. struct buffer_head *old_bh)
  3623. {
  3624. char *buf = old_bh->b_data;
  3625. struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
  3626. struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
  3627. int i;
  3628. xs->header = bucket_xh(xs->bucket);
  3629. xs->base = bucket_block(xs->bucket, 0);
  3630. xs->end = xs->base + inode->i_sb->s_blocksize;
  3631. if (xs->not_found)
  3632. return;
  3633. i = xs->here - old_xh->xh_entries;
  3634. xs->here = &xs->header->xh_entries[i];
  3635. }
  3636. static int ocfs2_xattr_create_index_block(struct inode *inode,
  3637. struct ocfs2_xattr_search *xs,
  3638. struct ocfs2_xattr_set_ctxt *ctxt)
  3639. {
  3640. int ret;
  3641. u32 bit_off, len;
  3642. u64 blkno;
  3643. handle_t *handle = ctxt->handle;
  3644. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3645. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  3646. struct buffer_head *xb_bh = xs->xattr_bh;
  3647. struct ocfs2_xattr_block *xb =
  3648. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3649. struct ocfs2_xattr_tree_root *xr;
  3650. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  3651. mlog(0, "create xattr index block for %llu\n",
  3652. (unsigned long long)xb_bh->b_blocknr);
  3653. BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
  3654. BUG_ON(!xs->bucket);
  3655. /*
  3656. * XXX:
  3657. * We can use this lock for now, and maybe move to a dedicated mutex
  3658. * if performance becomes a problem later.
  3659. */
  3660. down_write(&oi->ip_alloc_sem);
  3661. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
  3662. OCFS2_JOURNAL_ACCESS_WRITE);
  3663. if (ret) {
  3664. mlog_errno(ret);
  3665. goto out;
  3666. }
  3667. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
  3668. 1, 1, &bit_off, &len);
  3669. if (ret) {
  3670. mlog_errno(ret);
  3671. goto out;
  3672. }
  3673. /*
  3674. * The bucket may spread in many blocks, and
  3675. * we will only touch the 1st block and the last block
  3676. * in the whole bucket(one for entry and one for data).
  3677. */
  3678. blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  3679. mlog(0, "allocate 1 cluster from %llu to xattr block\n",
  3680. (unsigned long long)blkno);
  3681. ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
  3682. if (ret) {
  3683. mlog_errno(ret);
  3684. goto out;
  3685. }
  3686. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  3687. OCFS2_JOURNAL_ACCESS_CREATE);
  3688. if (ret) {
  3689. mlog_errno(ret);
  3690. goto out;
  3691. }
  3692. ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
  3693. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  3694. ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
  3695. /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
  3696. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  3697. offsetof(struct ocfs2_xattr_block, xb_attrs));
  3698. xr = &xb->xb_attrs.xb_root;
  3699. xr->xt_clusters = cpu_to_le32(1);
  3700. xr->xt_last_eb_blk = 0;
  3701. xr->xt_list.l_tree_depth = 0;
  3702. xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
  3703. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  3704. xr->xt_list.l_recs[0].e_cpos = 0;
  3705. xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  3706. xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  3707. xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
  3708. ocfs2_journal_dirty(handle, xb_bh);
  3709. out:
  3710. up_write(&oi->ip_alloc_sem);
  3711. return ret;
  3712. }
  3713. static int cmp_xe_offset(const void *a, const void *b)
  3714. {
  3715. const struct ocfs2_xattr_entry *l = a, *r = b;
  3716. u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
  3717. u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
  3718. if (l_name_offset < r_name_offset)
  3719. return 1;
  3720. if (l_name_offset > r_name_offset)
  3721. return -1;
  3722. return 0;
  3723. }
  3724. /*
  3725. * defrag a xattr bucket if we find that the bucket has some
  3726. * holes beteen name/value pairs.
  3727. * We will move all the name/value pairs to the end of the bucket
  3728. * so that we can spare some space for insertion.
  3729. */
  3730. static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  3731. handle_t *handle,
  3732. struct ocfs2_xattr_bucket *bucket)
  3733. {
  3734. int ret, i;
  3735. size_t end, offset, len;
  3736. struct ocfs2_xattr_header *xh;
  3737. char *entries, *buf, *bucket_buf = NULL;
  3738. u64 blkno = bucket_blkno(bucket);
  3739. u16 xh_free_start;
  3740. size_t blocksize = inode->i_sb->s_blocksize;
  3741. struct ocfs2_xattr_entry *xe;
  3742. /*
  3743. * In order to make the operation more efficient and generic,
  3744. * we copy all the blocks into a contiguous memory and do the
  3745. * defragment there, so if anything is error, we will not touch
  3746. * the real block.
  3747. */
  3748. bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
  3749. if (!bucket_buf) {
  3750. ret = -EIO;
  3751. goto out;
  3752. }
  3753. buf = bucket_buf;
  3754. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3755. memcpy(buf, bucket_block(bucket, i), blocksize);
  3756. ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
  3757. OCFS2_JOURNAL_ACCESS_WRITE);
  3758. if (ret < 0) {
  3759. mlog_errno(ret);
  3760. goto out;
  3761. }
  3762. xh = (struct ocfs2_xattr_header *)bucket_buf;
  3763. entries = (char *)xh->xh_entries;
  3764. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3765. mlog(0, "adjust xattr bucket in %llu, count = %u, "
  3766. "xh_free_start = %u, xh_name_value_len = %u.\n",
  3767. (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
  3768. xh_free_start, le16_to_cpu(xh->xh_name_value_len));
  3769. /*
  3770. * sort all the entries by their offset.
  3771. * the largest will be the first, so that we can
  3772. * move them to the end one by one.
  3773. */
  3774. sort(entries, le16_to_cpu(xh->xh_count),
  3775. sizeof(struct ocfs2_xattr_entry),
  3776. cmp_xe_offset, swap_xe);
  3777. /* Move all name/values to the end of the bucket. */
  3778. xe = xh->xh_entries;
  3779. end = OCFS2_XATTR_BUCKET_SIZE;
  3780. for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
  3781. offset = le16_to_cpu(xe->xe_name_offset);
  3782. len = namevalue_size_xe(xe);
  3783. /*
  3784. * We must make sure that the name/value pair
  3785. * exist in the same block. So adjust end to
  3786. * the previous block end if needed.
  3787. */
  3788. if (((end - len) / blocksize !=
  3789. (end - 1) / blocksize))
  3790. end = end - end % blocksize;
  3791. if (end > offset + len) {
  3792. memmove(bucket_buf + end - len,
  3793. bucket_buf + offset, len);
  3794. xe->xe_name_offset = cpu_to_le16(end - len);
  3795. }
  3796. mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
  3797. "bucket %llu\n", (unsigned long long)blkno);
  3798. end -= len;
  3799. }
  3800. mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
  3801. "bucket %llu\n", (unsigned long long)blkno);
  3802. if (xh_free_start == end)
  3803. goto out;
  3804. memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
  3805. xh->xh_free_start = cpu_to_le16(end);
  3806. /* sort the entries by their name_hash. */
  3807. sort(entries, le16_to_cpu(xh->xh_count),
  3808. sizeof(struct ocfs2_xattr_entry),
  3809. cmp_xe, swap_xe);
  3810. buf = bucket_buf;
  3811. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3812. memcpy(bucket_block(bucket, i), buf, blocksize);
  3813. ocfs2_xattr_bucket_journal_dirty(handle, bucket);
  3814. out:
  3815. kfree(bucket_buf);
  3816. return ret;
  3817. }
  3818. /*
  3819. * prev_blkno points to the start of an existing extent. new_blkno
  3820. * points to a newly allocated extent. Because we know each of our
  3821. * clusters contains more than bucket, we can easily split one cluster
  3822. * at a bucket boundary. So we take the last cluster of the existing
  3823. * extent and split it down the middle. We move the last half of the
  3824. * buckets in the last cluster of the existing extent over to the new
  3825. * extent.
  3826. *
  3827. * first_bh is the buffer at prev_blkno so we can update the existing
  3828. * extent's bucket count. header_bh is the bucket were we were hoping
  3829. * to insert our xattr. If the bucket move places the target in the new
  3830. * extent, we'll update first_bh and header_bh after modifying the old
  3831. * extent.
  3832. *
  3833. * first_hash will be set as the 1st xe's name_hash in the new extent.
  3834. */
  3835. static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
  3836. handle_t *handle,
  3837. struct ocfs2_xattr_bucket *first,
  3838. struct ocfs2_xattr_bucket *target,
  3839. u64 new_blkno,
  3840. u32 num_clusters,
  3841. u32 *first_hash)
  3842. {
  3843. int ret;
  3844. struct super_block *sb = inode->i_sb;
  3845. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
  3846. int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
  3847. int to_move = num_buckets / 2;
  3848. u64 src_blkno;
  3849. u64 last_cluster_blkno = bucket_blkno(first) +
  3850. ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
  3851. BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
  3852. BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
  3853. mlog(0, "move half of xattrs in cluster %llu to %llu\n",
  3854. (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
  3855. ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
  3856. last_cluster_blkno, new_blkno,
  3857. to_move, first_hash);
  3858. if (ret) {
  3859. mlog_errno(ret);
  3860. goto out;
  3861. }
  3862. /* This is the first bucket that got moved */
  3863. src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
  3864. /*
  3865. * If the target bucket was part of the moved buckets, we need to
  3866. * update first and target.
  3867. */
  3868. if (bucket_blkno(target) >= src_blkno) {
  3869. /* Find the block for the new target bucket */
  3870. src_blkno = new_blkno +
  3871. (bucket_blkno(target) - src_blkno);
  3872. ocfs2_xattr_bucket_relse(first);
  3873. ocfs2_xattr_bucket_relse(target);
  3874. /*
  3875. * These shouldn't fail - the buffers are in the
  3876. * journal from ocfs2_cp_xattr_bucket().
  3877. */
  3878. ret = ocfs2_read_xattr_bucket(first, new_blkno);
  3879. if (ret) {
  3880. mlog_errno(ret);
  3881. goto out;
  3882. }
  3883. ret = ocfs2_read_xattr_bucket(target, src_blkno);
  3884. if (ret)
  3885. mlog_errno(ret);
  3886. }
  3887. out:
  3888. return ret;
  3889. }
  3890. /*
  3891. * Find the suitable pos when we divide a bucket into 2.
  3892. * We have to make sure the xattrs with the same hash value exist
  3893. * in the same bucket.
  3894. *
  3895. * If this ocfs2_xattr_header covers more than one hash value, find a
  3896. * place where the hash value changes. Try to find the most even split.
  3897. * The most common case is that all entries have different hash values,
  3898. * and the first check we make will find a place to split.
  3899. */
  3900. static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
  3901. {
  3902. struct ocfs2_xattr_entry *entries = xh->xh_entries;
  3903. int count = le16_to_cpu(xh->xh_count);
  3904. int delta, middle = count / 2;
  3905. /*
  3906. * We start at the middle. Each step gets farther away in both
  3907. * directions. We therefore hit the change in hash value
  3908. * nearest to the middle. Note that this loop does not execute for
  3909. * count < 2.
  3910. */
  3911. for (delta = 0; delta < middle; delta++) {
  3912. /* Let's check delta earlier than middle */
  3913. if (cmp_xe(&entries[middle - delta - 1],
  3914. &entries[middle - delta]))
  3915. return middle - delta;
  3916. /* For even counts, don't walk off the end */
  3917. if ((middle + delta + 1) == count)
  3918. continue;
  3919. /* Now try delta past middle */
  3920. if (cmp_xe(&entries[middle + delta],
  3921. &entries[middle + delta + 1]))
  3922. return middle + delta + 1;
  3923. }
  3924. /* Every entry had the same hash */
  3925. return count;
  3926. }
  3927. /*
  3928. * Move some xattrs in old bucket(blk) to new bucket(new_blk).
  3929. * first_hash will record the 1st hash of the new bucket.
  3930. *
  3931. * Normally half of the xattrs will be moved. But we have to make
  3932. * sure that the xattrs with the same hash value are stored in the
  3933. * same bucket. If all the xattrs in this bucket have the same hash
  3934. * value, the new bucket will be initialized as an empty one and the
  3935. * first_hash will be initialized as (hash_value+1).
  3936. */
  3937. static int ocfs2_divide_xattr_bucket(struct inode *inode,
  3938. handle_t *handle,
  3939. u64 blk,
  3940. u64 new_blk,
  3941. u32 *first_hash,
  3942. int new_bucket_head)
  3943. {
  3944. int ret, i;
  3945. int count, start, len, name_value_len = 0, name_offset = 0;
  3946. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  3947. struct ocfs2_xattr_header *xh;
  3948. struct ocfs2_xattr_entry *xe;
  3949. int blocksize = inode->i_sb->s_blocksize;
  3950. mlog(0, "move some of xattrs from bucket %llu to %llu\n",
  3951. (unsigned long long)blk, (unsigned long long)new_blk);
  3952. s_bucket = ocfs2_xattr_bucket_new(inode);
  3953. t_bucket = ocfs2_xattr_bucket_new(inode);
  3954. if (!s_bucket || !t_bucket) {
  3955. ret = -ENOMEM;
  3956. mlog_errno(ret);
  3957. goto out;
  3958. }
  3959. ret = ocfs2_read_xattr_bucket(s_bucket, blk);
  3960. if (ret) {
  3961. mlog_errno(ret);
  3962. goto out;
  3963. }
  3964. ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
  3965. OCFS2_JOURNAL_ACCESS_WRITE);
  3966. if (ret) {
  3967. mlog_errno(ret);
  3968. goto out;
  3969. }
  3970. /*
  3971. * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
  3972. * there's no need to read it.
  3973. */
  3974. ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
  3975. if (ret) {
  3976. mlog_errno(ret);
  3977. goto out;
  3978. }
  3979. /*
  3980. * Hey, if we're overwriting t_bucket, what difference does
  3981. * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
  3982. * same part of ocfs2_cp_xattr_bucket().
  3983. */
  3984. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  3985. new_bucket_head ?
  3986. OCFS2_JOURNAL_ACCESS_CREATE :
  3987. OCFS2_JOURNAL_ACCESS_WRITE);
  3988. if (ret) {
  3989. mlog_errno(ret);
  3990. goto out;
  3991. }
  3992. xh = bucket_xh(s_bucket);
  3993. count = le16_to_cpu(xh->xh_count);
  3994. start = ocfs2_xattr_find_divide_pos(xh);
  3995. if (start == count) {
  3996. xe = &xh->xh_entries[start-1];
  3997. /*
  3998. * initialized a new empty bucket here.
  3999. * The hash value is set as one larger than
  4000. * that of the last entry in the previous bucket.
  4001. */
  4002. for (i = 0; i < t_bucket->bu_blocks; i++)
  4003. memset(bucket_block(t_bucket, i), 0, blocksize);
  4004. xh = bucket_xh(t_bucket);
  4005. xh->xh_free_start = cpu_to_le16(blocksize);
  4006. xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
  4007. le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
  4008. goto set_num_buckets;
  4009. }
  4010. /* copy the whole bucket to the new first. */
  4011. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  4012. /* update the new bucket. */
  4013. xh = bucket_xh(t_bucket);
  4014. /*
  4015. * Calculate the total name/value len and xh_free_start for
  4016. * the old bucket first.
  4017. */
  4018. name_offset = OCFS2_XATTR_BUCKET_SIZE;
  4019. name_value_len = 0;
  4020. for (i = 0; i < start; i++) {
  4021. xe = &xh->xh_entries[i];
  4022. name_value_len += namevalue_size_xe(xe);
  4023. if (le16_to_cpu(xe->xe_name_offset) < name_offset)
  4024. name_offset = le16_to_cpu(xe->xe_name_offset);
  4025. }
  4026. /*
  4027. * Now begin the modification to the new bucket.
  4028. *
  4029. * In the new bucket, We just move the xattr entry to the beginning
  4030. * and don't touch the name/value. So there will be some holes in the
  4031. * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
  4032. * called.
  4033. */
  4034. xe = &xh->xh_entries[start];
  4035. len = sizeof(struct ocfs2_xattr_entry) * (count - start);
  4036. mlog(0, "mv xattr entry len %d from %d to %d\n", len,
  4037. (int)((char *)xe - (char *)xh),
  4038. (int)((char *)xh->xh_entries - (char *)xh));
  4039. memmove((char *)xh->xh_entries, (char *)xe, len);
  4040. xe = &xh->xh_entries[count - start];
  4041. len = sizeof(struct ocfs2_xattr_entry) * start;
  4042. memset((char *)xe, 0, len);
  4043. le16_add_cpu(&xh->xh_count, -start);
  4044. le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
  4045. /* Calculate xh_free_start for the new bucket. */
  4046. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  4047. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4048. xe = &xh->xh_entries[i];
  4049. if (le16_to_cpu(xe->xe_name_offset) <
  4050. le16_to_cpu(xh->xh_free_start))
  4051. xh->xh_free_start = xe->xe_name_offset;
  4052. }
  4053. set_num_buckets:
  4054. /* set xh->xh_num_buckets for the new xh. */
  4055. if (new_bucket_head)
  4056. xh->xh_num_buckets = cpu_to_le16(1);
  4057. else
  4058. xh->xh_num_buckets = 0;
  4059. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  4060. /* store the first_hash of the new bucket. */
  4061. if (first_hash)
  4062. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  4063. /*
  4064. * Now only update the 1st block of the old bucket. If we
  4065. * just added a new empty bucket, there is no need to modify
  4066. * it.
  4067. */
  4068. if (start == count)
  4069. goto out;
  4070. xh = bucket_xh(s_bucket);
  4071. memset(&xh->xh_entries[start], 0,
  4072. sizeof(struct ocfs2_xattr_entry) * (count - start));
  4073. xh->xh_count = cpu_to_le16(start);
  4074. xh->xh_free_start = cpu_to_le16(name_offset);
  4075. xh->xh_name_value_len = cpu_to_le16(name_value_len);
  4076. ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
  4077. out:
  4078. ocfs2_xattr_bucket_free(s_bucket);
  4079. ocfs2_xattr_bucket_free(t_bucket);
  4080. return ret;
  4081. }
  4082. /*
  4083. * Copy xattr from one bucket to another bucket.
  4084. *
  4085. * The caller must make sure that the journal transaction
  4086. * has enough space for journaling.
  4087. */
  4088. static int ocfs2_cp_xattr_bucket(struct inode *inode,
  4089. handle_t *handle,
  4090. u64 s_blkno,
  4091. u64 t_blkno,
  4092. int t_is_new)
  4093. {
  4094. int ret;
  4095. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  4096. BUG_ON(s_blkno == t_blkno);
  4097. mlog(0, "cp bucket %llu to %llu, target is %d\n",
  4098. (unsigned long long)s_blkno, (unsigned long long)t_blkno,
  4099. t_is_new);
  4100. s_bucket = ocfs2_xattr_bucket_new(inode);
  4101. t_bucket = ocfs2_xattr_bucket_new(inode);
  4102. if (!s_bucket || !t_bucket) {
  4103. ret = -ENOMEM;
  4104. mlog_errno(ret);
  4105. goto out;
  4106. }
  4107. ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
  4108. if (ret)
  4109. goto out;
  4110. /*
  4111. * Even if !t_is_new, we're overwriting t_bucket. Thus,
  4112. * there's no need to read it.
  4113. */
  4114. ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
  4115. if (ret)
  4116. goto out;
  4117. /*
  4118. * Hey, if we're overwriting t_bucket, what difference does
  4119. * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
  4120. * cluster to fill, we came here from
  4121. * ocfs2_mv_xattr_buckets(), and it is really new -
  4122. * ACCESS_CREATE is required. But we also might have moved data
  4123. * out of t_bucket before extending back into it.
  4124. * ocfs2_add_new_xattr_bucket() can do this - its call to
  4125. * ocfs2_add_new_xattr_cluster() may have created a new extent
  4126. * and copied out the end of the old extent. Then it re-extends
  4127. * the old extent back to create space for new xattrs. That's
  4128. * how we get here, and the bucket isn't really new.
  4129. */
  4130. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  4131. t_is_new ?
  4132. OCFS2_JOURNAL_ACCESS_CREATE :
  4133. OCFS2_JOURNAL_ACCESS_WRITE);
  4134. if (ret)
  4135. goto out;
  4136. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  4137. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  4138. out:
  4139. ocfs2_xattr_bucket_free(t_bucket);
  4140. ocfs2_xattr_bucket_free(s_bucket);
  4141. return ret;
  4142. }
  4143. /*
  4144. * src_blk points to the start of an existing extent. last_blk points to
  4145. * last cluster in that extent. to_blk points to a newly allocated
  4146. * extent. We copy the buckets from the cluster at last_blk to the new
  4147. * extent. If start_bucket is non-zero, we skip that many buckets before
  4148. * we start copying. The new extent's xh_num_buckets gets set to the
  4149. * number of buckets we copied. The old extent's xh_num_buckets shrinks
  4150. * by the same amount.
  4151. */
  4152. static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
  4153. u64 src_blk, u64 last_blk, u64 to_blk,
  4154. unsigned int start_bucket,
  4155. u32 *first_hash)
  4156. {
  4157. int i, ret, credits;
  4158. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4159. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4160. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  4161. struct ocfs2_xattr_bucket *old_first, *new_first;
  4162. mlog(0, "mv xattrs from cluster %llu to %llu\n",
  4163. (unsigned long long)last_blk, (unsigned long long)to_blk);
  4164. BUG_ON(start_bucket >= num_buckets);
  4165. if (start_bucket) {
  4166. num_buckets -= start_bucket;
  4167. last_blk += (start_bucket * blks_per_bucket);
  4168. }
  4169. /* The first bucket of the original extent */
  4170. old_first = ocfs2_xattr_bucket_new(inode);
  4171. /* The first bucket of the new extent */
  4172. new_first = ocfs2_xattr_bucket_new(inode);
  4173. if (!old_first || !new_first) {
  4174. ret = -ENOMEM;
  4175. mlog_errno(ret);
  4176. goto out;
  4177. }
  4178. ret = ocfs2_read_xattr_bucket(old_first, src_blk);
  4179. if (ret) {
  4180. mlog_errno(ret);
  4181. goto out;
  4182. }
  4183. /*
  4184. * We need to update the first bucket of the old extent and all
  4185. * the buckets going to the new extent.
  4186. */
  4187. credits = ((num_buckets + 1) * blks_per_bucket);
  4188. ret = ocfs2_extend_trans(handle, credits);
  4189. if (ret) {
  4190. mlog_errno(ret);
  4191. goto out;
  4192. }
  4193. ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
  4194. OCFS2_JOURNAL_ACCESS_WRITE);
  4195. if (ret) {
  4196. mlog_errno(ret);
  4197. goto out;
  4198. }
  4199. for (i = 0; i < num_buckets; i++) {
  4200. ret = ocfs2_cp_xattr_bucket(inode, handle,
  4201. last_blk + (i * blks_per_bucket),
  4202. to_blk + (i * blks_per_bucket),
  4203. 1);
  4204. if (ret) {
  4205. mlog_errno(ret);
  4206. goto out;
  4207. }
  4208. }
  4209. /*
  4210. * Get the new bucket ready before we dirty anything
  4211. * (This actually shouldn't fail, because we already dirtied
  4212. * it once in ocfs2_cp_xattr_bucket()).
  4213. */
  4214. ret = ocfs2_read_xattr_bucket(new_first, to_blk);
  4215. if (ret) {
  4216. mlog_errno(ret);
  4217. goto out;
  4218. }
  4219. ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
  4220. OCFS2_JOURNAL_ACCESS_WRITE);
  4221. if (ret) {
  4222. mlog_errno(ret);
  4223. goto out;
  4224. }
  4225. /* Now update the headers */
  4226. le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
  4227. ocfs2_xattr_bucket_journal_dirty(handle, old_first);
  4228. bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
  4229. ocfs2_xattr_bucket_journal_dirty(handle, new_first);
  4230. if (first_hash)
  4231. *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
  4232. out:
  4233. ocfs2_xattr_bucket_free(new_first);
  4234. ocfs2_xattr_bucket_free(old_first);
  4235. return ret;
  4236. }
  4237. /*
  4238. * Move some xattrs in this cluster to the new cluster.
  4239. * This function should only be called when bucket size == cluster size.
  4240. * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  4241. */
  4242. static int ocfs2_divide_xattr_cluster(struct inode *inode,
  4243. handle_t *handle,
  4244. u64 prev_blk,
  4245. u64 new_blk,
  4246. u32 *first_hash)
  4247. {
  4248. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4249. int ret, credits = 2 * blk_per_bucket;
  4250. BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
  4251. ret = ocfs2_extend_trans(handle, credits);
  4252. if (ret) {
  4253. mlog_errno(ret);
  4254. return ret;
  4255. }
  4256. /* Move half of the xattr in start_blk to the next bucket. */
  4257. return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
  4258. new_blk, first_hash, 1);
  4259. }
  4260. /*
  4261. * Move some xattrs from the old cluster to the new one since they are not
  4262. * contiguous in ocfs2 xattr tree.
  4263. *
  4264. * new_blk starts a new separate cluster, and we will move some xattrs from
  4265. * prev_blk to it. v_start will be set as the first name hash value in this
  4266. * new cluster so that it can be used as e_cpos during tree insertion and
  4267. * don't collide with our original b-tree operations. first_bh and header_bh
  4268. * will also be updated since they will be used in ocfs2_extend_xattr_bucket
  4269. * to extend the insert bucket.
  4270. *
  4271. * The problem is how much xattr should we move to the new one and when should
  4272. * we update first_bh and header_bh?
  4273. * 1. If cluster size > bucket size, that means the previous cluster has more
  4274. * than 1 bucket, so just move half nums of bucket into the new cluster and
  4275. * update the first_bh and header_bh if the insert bucket has been moved
  4276. * to the new cluster.
  4277. * 2. If cluster_size == bucket_size:
  4278. * a) If the previous extent rec has more than one cluster and the insert
  4279. * place isn't in the last cluster, copy the entire last cluster to the
  4280. * new one. This time, we don't need to upate the first_bh and header_bh
  4281. * since they will not be moved into the new cluster.
  4282. * b) Otherwise, move the bottom half of the xattrs in the last cluster into
  4283. * the new one. And we set the extend flag to zero if the insert place is
  4284. * moved into the new allocated cluster since no extend is needed.
  4285. */
  4286. static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
  4287. handle_t *handle,
  4288. struct ocfs2_xattr_bucket *first,
  4289. struct ocfs2_xattr_bucket *target,
  4290. u64 new_blk,
  4291. u32 prev_clusters,
  4292. u32 *v_start,
  4293. int *extend)
  4294. {
  4295. int ret;
  4296. mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
  4297. (unsigned long long)bucket_blkno(first), prev_clusters,
  4298. (unsigned long long)new_blk);
  4299. if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
  4300. ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
  4301. handle,
  4302. first, target,
  4303. new_blk,
  4304. prev_clusters,
  4305. v_start);
  4306. if (ret)
  4307. mlog_errno(ret);
  4308. } else {
  4309. /* The start of the last cluster in the first extent */
  4310. u64 last_blk = bucket_blkno(first) +
  4311. ((prev_clusters - 1) *
  4312. ocfs2_clusters_to_blocks(inode->i_sb, 1));
  4313. if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
  4314. ret = ocfs2_mv_xattr_buckets(inode, handle,
  4315. bucket_blkno(first),
  4316. last_blk, new_blk, 0,
  4317. v_start);
  4318. if (ret)
  4319. mlog_errno(ret);
  4320. } else {
  4321. ret = ocfs2_divide_xattr_cluster(inode, handle,
  4322. last_blk, new_blk,
  4323. v_start);
  4324. if (ret)
  4325. mlog_errno(ret);
  4326. if ((bucket_blkno(target) == last_blk) && extend)
  4327. *extend = 0;
  4328. }
  4329. }
  4330. return ret;
  4331. }
  4332. /*
  4333. * Add a new cluster for xattr storage.
  4334. *
  4335. * If the new cluster is contiguous with the previous one, it will be
  4336. * appended to the same extent record, and num_clusters will be updated.
  4337. * If not, we will insert a new extent for it and move some xattrs in
  4338. * the last cluster into the new allocated one.
  4339. * We also need to limit the maximum size of a btree leaf, otherwise we'll
  4340. * lose the benefits of hashing because we'll have to search large leaves.
  4341. * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
  4342. * if it's bigger).
  4343. *
  4344. * first_bh is the first block of the previous extent rec and header_bh
  4345. * indicates the bucket we will insert the new xattrs. They will be updated
  4346. * when the header_bh is moved into the new cluster.
  4347. */
  4348. static int ocfs2_add_new_xattr_cluster(struct inode *inode,
  4349. struct buffer_head *root_bh,
  4350. struct ocfs2_xattr_bucket *first,
  4351. struct ocfs2_xattr_bucket *target,
  4352. u32 *num_clusters,
  4353. u32 prev_cpos,
  4354. int *extend,
  4355. struct ocfs2_xattr_set_ctxt *ctxt)
  4356. {
  4357. int ret;
  4358. u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  4359. u32 prev_clusters = *num_clusters;
  4360. u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
  4361. u64 block;
  4362. handle_t *handle = ctxt->handle;
  4363. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4364. struct ocfs2_extent_tree et;
  4365. mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
  4366. "previous xattr blkno = %llu\n",
  4367. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  4368. prev_cpos, (unsigned long long)bucket_blkno(first));
  4369. ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
  4370. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  4371. OCFS2_JOURNAL_ACCESS_WRITE);
  4372. if (ret < 0) {
  4373. mlog_errno(ret);
  4374. goto leave;
  4375. }
  4376. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
  4377. clusters_to_add, &bit_off, &num_bits);
  4378. if (ret < 0) {
  4379. if (ret != -ENOSPC)
  4380. mlog_errno(ret);
  4381. goto leave;
  4382. }
  4383. BUG_ON(num_bits > clusters_to_add);
  4384. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  4385. mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
  4386. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  4387. if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
  4388. (prev_clusters + num_bits) << osb->s_clustersize_bits <=
  4389. OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
  4390. /*
  4391. * If this cluster is contiguous with the old one and
  4392. * adding this new cluster, we don't surpass the limit of
  4393. * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
  4394. * initialized and used like other buckets in the previous
  4395. * cluster.
  4396. * So add it as a contiguous one. The caller will handle
  4397. * its init process.
  4398. */
  4399. v_start = prev_cpos + prev_clusters;
  4400. *num_clusters = prev_clusters + num_bits;
  4401. mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
  4402. num_bits);
  4403. } else {
  4404. ret = ocfs2_adjust_xattr_cross_cluster(inode,
  4405. handle,
  4406. first,
  4407. target,
  4408. block,
  4409. prev_clusters,
  4410. &v_start,
  4411. extend);
  4412. if (ret) {
  4413. mlog_errno(ret);
  4414. goto leave;
  4415. }
  4416. }
  4417. mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
  4418. num_bits, (unsigned long long)block, v_start);
  4419. ret = ocfs2_insert_extent(handle, &et, v_start, block,
  4420. num_bits, 0, ctxt->meta_ac);
  4421. if (ret < 0) {
  4422. mlog_errno(ret);
  4423. goto leave;
  4424. }
  4425. ocfs2_journal_dirty(handle, root_bh);
  4426. leave:
  4427. return ret;
  4428. }
  4429. /*
  4430. * We are given an extent. 'first' is the bucket at the very front of
  4431. * the extent. The extent has space for an additional bucket past
  4432. * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
  4433. * of the target bucket. We wish to shift every bucket past the target
  4434. * down one, filling in that additional space. When we get back to the
  4435. * target, we split the target between itself and the now-empty bucket
  4436. * at target+1 (aka, target_blkno + blks_per_bucket).
  4437. */
  4438. static int ocfs2_extend_xattr_bucket(struct inode *inode,
  4439. handle_t *handle,
  4440. struct ocfs2_xattr_bucket *first,
  4441. u64 target_blk,
  4442. u32 num_clusters)
  4443. {
  4444. int ret, credits;
  4445. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4446. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4447. u64 end_blk;
  4448. u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
  4449. mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
  4450. "from %llu, len = %u\n", (unsigned long long)target_blk,
  4451. (unsigned long long)bucket_blkno(first), num_clusters);
  4452. /* The extent must have room for an additional bucket */
  4453. BUG_ON(new_bucket >=
  4454. (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
  4455. /* end_blk points to the last existing bucket */
  4456. end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
  4457. /*
  4458. * end_blk is the start of the last existing bucket.
  4459. * Thus, (end_blk - target_blk) covers the target bucket and
  4460. * every bucket after it up to, but not including, the last
  4461. * existing bucket. Then we add the last existing bucket, the
  4462. * new bucket, and the first bucket (3 * blk_per_bucket).
  4463. */
  4464. credits = (end_blk - target_blk) + (3 * blk_per_bucket);
  4465. ret = ocfs2_extend_trans(handle, credits);
  4466. if (ret) {
  4467. mlog_errno(ret);
  4468. goto out;
  4469. }
  4470. ret = ocfs2_xattr_bucket_journal_access(handle, first,
  4471. OCFS2_JOURNAL_ACCESS_WRITE);
  4472. if (ret) {
  4473. mlog_errno(ret);
  4474. goto out;
  4475. }
  4476. while (end_blk != target_blk) {
  4477. ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
  4478. end_blk + blk_per_bucket, 0);
  4479. if (ret)
  4480. goto out;
  4481. end_blk -= blk_per_bucket;
  4482. }
  4483. /* Move half of the xattr in target_blkno to the next bucket. */
  4484. ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
  4485. target_blk + blk_per_bucket, NULL, 0);
  4486. le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
  4487. ocfs2_xattr_bucket_journal_dirty(handle, first);
  4488. out:
  4489. return ret;
  4490. }
  4491. /*
  4492. * Add new xattr bucket in an extent record and adjust the buckets
  4493. * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
  4494. * bucket we want to insert into.
  4495. *
  4496. * In the easy case, we will move all the buckets after target down by
  4497. * one. Half of target's xattrs will be moved to the next bucket.
  4498. *
  4499. * If current cluster is full, we'll allocate a new one. This may not
  4500. * be contiguous. The underlying calls will make sure that there is
  4501. * space for the insert, shifting buckets around if necessary.
  4502. * 'target' may be moved by those calls.
  4503. */
  4504. static int ocfs2_add_new_xattr_bucket(struct inode *inode,
  4505. struct buffer_head *xb_bh,
  4506. struct ocfs2_xattr_bucket *target,
  4507. struct ocfs2_xattr_set_ctxt *ctxt)
  4508. {
  4509. struct ocfs2_xattr_block *xb =
  4510. (struct ocfs2_xattr_block *)xb_bh->b_data;
  4511. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  4512. struct ocfs2_extent_list *el = &xb_root->xt_list;
  4513. u32 name_hash =
  4514. le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
  4515. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4516. int ret, num_buckets, extend = 1;
  4517. u64 p_blkno;
  4518. u32 e_cpos, num_clusters;
  4519. /* The bucket at the front of the extent */
  4520. struct ocfs2_xattr_bucket *first;
  4521. mlog(0, "Add new xattr bucket starting from %llu\n",
  4522. (unsigned long long)bucket_blkno(target));
  4523. /* The first bucket of the original extent */
  4524. first = ocfs2_xattr_bucket_new(inode);
  4525. if (!first) {
  4526. ret = -ENOMEM;
  4527. mlog_errno(ret);
  4528. goto out;
  4529. }
  4530. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
  4531. &num_clusters, el);
  4532. if (ret) {
  4533. mlog_errno(ret);
  4534. goto out;
  4535. }
  4536. ret = ocfs2_read_xattr_bucket(first, p_blkno);
  4537. if (ret) {
  4538. mlog_errno(ret);
  4539. goto out;
  4540. }
  4541. num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
  4542. if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
  4543. /*
  4544. * This can move first+target if the target bucket moves
  4545. * to the new extent.
  4546. */
  4547. ret = ocfs2_add_new_xattr_cluster(inode,
  4548. xb_bh,
  4549. first,
  4550. target,
  4551. &num_clusters,
  4552. e_cpos,
  4553. &extend,
  4554. ctxt);
  4555. if (ret) {
  4556. mlog_errno(ret);
  4557. goto out;
  4558. }
  4559. }
  4560. if (extend) {
  4561. ret = ocfs2_extend_xattr_bucket(inode,
  4562. ctxt->handle,
  4563. first,
  4564. bucket_blkno(target),
  4565. num_clusters);
  4566. if (ret)
  4567. mlog_errno(ret);
  4568. }
  4569. out:
  4570. ocfs2_xattr_bucket_free(first);
  4571. return ret;
  4572. }
  4573. static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
  4574. struct ocfs2_xattr_bucket *bucket,
  4575. int offs)
  4576. {
  4577. int block_off = offs >> inode->i_sb->s_blocksize_bits;
  4578. offs = offs % inode->i_sb->s_blocksize;
  4579. return bucket_block(bucket, block_off) + offs;
  4580. }
  4581. /*
  4582. * Truncate the specified xe_off entry in xattr bucket.
  4583. * bucket is indicated by header_bh and len is the new length.
  4584. * Both the ocfs2_xattr_value_root and the entry will be updated here.
  4585. *
  4586. * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
  4587. */
  4588. static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
  4589. struct ocfs2_xattr_bucket *bucket,
  4590. int xe_off,
  4591. int len,
  4592. struct ocfs2_xattr_set_ctxt *ctxt)
  4593. {
  4594. int ret, offset;
  4595. u64 value_blk;
  4596. struct ocfs2_xattr_entry *xe;
  4597. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4598. size_t blocksize = inode->i_sb->s_blocksize;
  4599. struct ocfs2_xattr_value_buf vb = {
  4600. .vb_access = ocfs2_journal_access,
  4601. };
  4602. xe = &xh->xh_entries[xe_off];
  4603. BUG_ON(!xe || ocfs2_xattr_is_local(xe));
  4604. offset = le16_to_cpu(xe->xe_name_offset) +
  4605. OCFS2_XATTR_SIZE(xe->xe_name_len);
  4606. value_blk = offset / blocksize;
  4607. /* We don't allow ocfs2_xattr_value to be stored in different block. */
  4608. BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
  4609. vb.vb_bh = bucket->bu_bhs[value_blk];
  4610. BUG_ON(!vb.vb_bh);
  4611. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  4612. (vb.vb_bh->b_data + offset % blocksize);
  4613. /*
  4614. * From here on out we have to dirty the bucket. The generic
  4615. * value calls only modify one of the bucket's bhs, but we need
  4616. * to send the bucket at once. So if they error, they *could* have
  4617. * modified something. We have to assume they did, and dirty
  4618. * the whole bucket. This leaves us in a consistent state.
  4619. */
  4620. mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
  4621. xe_off, (unsigned long long)bucket_blkno(bucket), len);
  4622. ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
  4623. if (ret) {
  4624. mlog_errno(ret);
  4625. goto out;
  4626. }
  4627. ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
  4628. OCFS2_JOURNAL_ACCESS_WRITE);
  4629. if (ret) {
  4630. mlog_errno(ret);
  4631. goto out;
  4632. }
  4633. xe->xe_value_size = cpu_to_le64(len);
  4634. ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
  4635. out:
  4636. return ret;
  4637. }
  4638. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  4639. struct buffer_head *root_bh,
  4640. u64 blkno,
  4641. u32 cpos,
  4642. u32 len,
  4643. void *para)
  4644. {
  4645. int ret;
  4646. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4647. struct inode *tl_inode = osb->osb_tl_inode;
  4648. handle_t *handle;
  4649. struct ocfs2_xattr_block *xb =
  4650. (struct ocfs2_xattr_block *)root_bh->b_data;
  4651. struct ocfs2_alloc_context *meta_ac = NULL;
  4652. struct ocfs2_cached_dealloc_ctxt dealloc;
  4653. struct ocfs2_extent_tree et;
  4654. ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
  4655. ocfs2_delete_xattr_in_bucket, para);
  4656. if (ret) {
  4657. mlog_errno(ret);
  4658. return ret;
  4659. }
  4660. ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
  4661. ocfs2_init_dealloc_ctxt(&dealloc);
  4662. mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
  4663. cpos, len, (unsigned long long)blkno);
  4664. ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
  4665. len);
  4666. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  4667. if (ret) {
  4668. mlog_errno(ret);
  4669. return ret;
  4670. }
  4671. mutex_lock(&tl_inode->i_mutex);
  4672. if (ocfs2_truncate_log_needs_flush(osb)) {
  4673. ret = __ocfs2_flush_truncate_log(osb);
  4674. if (ret < 0) {
  4675. mlog_errno(ret);
  4676. goto out;
  4677. }
  4678. }
  4679. handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
  4680. if (IS_ERR(handle)) {
  4681. ret = -ENOMEM;
  4682. mlog_errno(ret);
  4683. goto out;
  4684. }
  4685. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  4686. OCFS2_JOURNAL_ACCESS_WRITE);
  4687. if (ret) {
  4688. mlog_errno(ret);
  4689. goto out_commit;
  4690. }
  4691. ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
  4692. &dealloc);
  4693. if (ret) {
  4694. mlog_errno(ret);
  4695. goto out_commit;
  4696. }
  4697. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
  4698. ocfs2_journal_dirty(handle, root_bh);
  4699. ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
  4700. if (ret)
  4701. mlog_errno(ret);
  4702. out_commit:
  4703. ocfs2_commit_trans(osb, handle);
  4704. out:
  4705. ocfs2_schedule_truncate_log_flush(osb, 1);
  4706. mutex_unlock(&tl_inode->i_mutex);
  4707. if (meta_ac)
  4708. ocfs2_free_alloc_context(meta_ac);
  4709. ocfs2_run_deallocs(osb, &dealloc);
  4710. return ret;
  4711. }
  4712. /*
  4713. * check whether the xattr bucket is filled up with the same hash value.
  4714. * If we want to insert the xattr with the same hash, return -ENOSPC.
  4715. * If we want to insert a xattr with different hash value, go ahead
  4716. * and ocfs2_divide_xattr_bucket will handle this.
  4717. */
  4718. static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
  4719. struct ocfs2_xattr_bucket *bucket,
  4720. const char *name)
  4721. {
  4722. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4723. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  4724. if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
  4725. return 0;
  4726. if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
  4727. xh->xh_entries[0].xe_name_hash) {
  4728. mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
  4729. "hash = %u\n",
  4730. (unsigned long long)bucket_blkno(bucket),
  4731. le32_to_cpu(xh->xh_entries[0].xe_name_hash));
  4732. return -ENOSPC;
  4733. }
  4734. return 0;
  4735. }
  4736. /*
  4737. * Try to set the entry in the current bucket. If we fail, the caller
  4738. * will handle getting us another bucket.
  4739. */
  4740. static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
  4741. struct ocfs2_xattr_info *xi,
  4742. struct ocfs2_xattr_search *xs,
  4743. struct ocfs2_xattr_set_ctxt *ctxt)
  4744. {
  4745. int ret;
  4746. struct ocfs2_xa_loc loc;
  4747. mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
  4748. ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
  4749. xs->not_found ? NULL : xs->here);
  4750. ret = ocfs2_xa_set(&loc, xi, ctxt);
  4751. if (!ret) {
  4752. xs->here = loc.xl_entry;
  4753. goto out;
  4754. }
  4755. if (ret != -ENOSPC) {
  4756. mlog_errno(ret);
  4757. goto out;
  4758. }
  4759. /* Ok, we need space. Let's try defragmenting the bucket. */
  4760. ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
  4761. xs->bucket);
  4762. if (ret) {
  4763. mlog_errno(ret);
  4764. goto out;
  4765. }
  4766. ret = ocfs2_xa_set(&loc, xi, ctxt);
  4767. if (!ret) {
  4768. xs->here = loc.xl_entry;
  4769. goto out;
  4770. }
  4771. if (ret != -ENOSPC)
  4772. mlog_errno(ret);
  4773. out:
  4774. mlog_exit(ret);
  4775. return ret;
  4776. }
  4777. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  4778. struct ocfs2_xattr_info *xi,
  4779. struct ocfs2_xattr_search *xs,
  4780. struct ocfs2_xattr_set_ctxt *ctxt)
  4781. {
  4782. int ret;
  4783. mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
  4784. ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
  4785. if (!ret)
  4786. goto out;
  4787. if (ret != -ENOSPC) {
  4788. mlog_errno(ret);
  4789. goto out;
  4790. }
  4791. /* Ack, need more space. Let's try to get another bucket! */
  4792. /*
  4793. * We do not allow for overlapping ranges between buckets. And
  4794. * the maximum number of collisions we will allow for then is
  4795. * one bucket's worth, so check it here whether we need to
  4796. * add a new bucket for the insert.
  4797. */
  4798. ret = ocfs2_check_xattr_bucket_collision(inode,
  4799. xs->bucket,
  4800. xi->xi_name);
  4801. if (ret) {
  4802. mlog_errno(ret);
  4803. goto out;
  4804. }
  4805. ret = ocfs2_add_new_xattr_bucket(inode,
  4806. xs->xattr_bh,
  4807. xs->bucket,
  4808. ctxt);
  4809. if (ret) {
  4810. mlog_errno(ret);
  4811. goto out;
  4812. }
  4813. /*
  4814. * ocfs2_add_new_xattr_bucket() will have updated
  4815. * xs->bucket if it moved, but it will not have updated
  4816. * any of the other search fields. Thus, we drop it and
  4817. * re-search. Everything should be cached, so it'll be
  4818. * quick.
  4819. */
  4820. ocfs2_xattr_bucket_relse(xs->bucket);
  4821. ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
  4822. xi->xi_name_index,
  4823. xi->xi_name, xs);
  4824. if (ret && ret != -ENODATA)
  4825. goto out;
  4826. xs->not_found = ret;
  4827. /* Ok, we have a new bucket, let's try again */
  4828. ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
  4829. if (ret && (ret != -ENOSPC))
  4830. mlog_errno(ret);
  4831. out:
  4832. mlog_exit(ret);
  4833. return ret;
  4834. }
  4835. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  4836. struct ocfs2_xattr_bucket *bucket,
  4837. void *para)
  4838. {
  4839. int ret = 0, ref_credits;
  4840. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4841. u16 i;
  4842. struct ocfs2_xattr_entry *xe;
  4843. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4844. struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
  4845. int credits = ocfs2_remove_extent_credits(osb->sb) +
  4846. ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4847. struct ocfs2_xattr_value_root *xv;
  4848. struct ocfs2_rm_xattr_bucket_para *args =
  4849. (struct ocfs2_rm_xattr_bucket_para *)para;
  4850. ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
  4851. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4852. xe = &xh->xh_entries[i];
  4853. if (ocfs2_xattr_is_local(xe))
  4854. continue;
  4855. ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
  4856. i, &xv, NULL);
  4857. ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
  4858. args->ref_ci,
  4859. args->ref_root_bh,
  4860. &ctxt.meta_ac,
  4861. &ref_credits);
  4862. ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
  4863. if (IS_ERR(ctxt.handle)) {
  4864. ret = PTR_ERR(ctxt.handle);
  4865. mlog_errno(ret);
  4866. break;
  4867. }
  4868. ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
  4869. i, 0, &ctxt);
  4870. ocfs2_commit_trans(osb, ctxt.handle);
  4871. if (ctxt.meta_ac) {
  4872. ocfs2_free_alloc_context(ctxt.meta_ac);
  4873. ctxt.meta_ac = NULL;
  4874. }
  4875. if (ret) {
  4876. mlog_errno(ret);
  4877. break;
  4878. }
  4879. }
  4880. if (ctxt.meta_ac)
  4881. ocfs2_free_alloc_context(ctxt.meta_ac);
  4882. ocfs2_schedule_truncate_log_flush(osb, 1);
  4883. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  4884. return ret;
  4885. }
  4886. /*
  4887. * Whenever we modify a xattr value root in the bucket(e.g, CoW
  4888. * or change the extent record flag), we need to recalculate
  4889. * the metaecc for the whole bucket. So it is done here.
  4890. *
  4891. * Note:
  4892. * We have to give the extra credits for the caller.
  4893. */
  4894. static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
  4895. handle_t *handle,
  4896. void *para)
  4897. {
  4898. int ret;
  4899. struct ocfs2_xattr_bucket *bucket =
  4900. (struct ocfs2_xattr_bucket *)para;
  4901. ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
  4902. OCFS2_JOURNAL_ACCESS_WRITE);
  4903. if (ret) {
  4904. mlog_errno(ret);
  4905. return ret;
  4906. }
  4907. ocfs2_xattr_bucket_journal_dirty(handle, bucket);
  4908. return 0;
  4909. }
  4910. /*
  4911. * Special action we need if the xattr value is refcounted.
  4912. *
  4913. * 1. If the xattr is refcounted, lock the tree.
  4914. * 2. CoW the xattr if we are setting the new value and the value
  4915. * will be stored outside.
  4916. * 3. In other case, decrease_refcount will work for us, so just
  4917. * lock the refcount tree, calculate the meta and credits is OK.
  4918. *
  4919. * We have to do CoW before ocfs2_init_xattr_set_ctxt since
  4920. * currently CoW is a completed transaction, while this function
  4921. * will also lock the allocators and let us deadlock. So we will
  4922. * CoW the whole xattr value.
  4923. */
  4924. static int ocfs2_prepare_refcount_xattr(struct inode *inode,
  4925. struct ocfs2_dinode *di,
  4926. struct ocfs2_xattr_info *xi,
  4927. struct ocfs2_xattr_search *xis,
  4928. struct ocfs2_xattr_search *xbs,
  4929. struct ocfs2_refcount_tree **ref_tree,
  4930. int *meta_add,
  4931. int *credits)
  4932. {
  4933. int ret = 0;
  4934. struct ocfs2_xattr_block *xb;
  4935. struct ocfs2_xattr_entry *xe;
  4936. char *base;
  4937. u32 p_cluster, num_clusters;
  4938. unsigned int ext_flags;
  4939. int name_offset, name_len;
  4940. struct ocfs2_xattr_value_buf vb;
  4941. struct ocfs2_xattr_bucket *bucket = NULL;
  4942. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4943. struct ocfs2_post_refcount refcount;
  4944. struct ocfs2_post_refcount *p = NULL;
  4945. struct buffer_head *ref_root_bh = NULL;
  4946. if (!xis->not_found) {
  4947. xe = xis->here;
  4948. name_offset = le16_to_cpu(xe->xe_name_offset);
  4949. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  4950. base = xis->base;
  4951. vb.vb_bh = xis->inode_bh;
  4952. vb.vb_access = ocfs2_journal_access_di;
  4953. } else {
  4954. int i, block_off = 0;
  4955. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  4956. xe = xbs->here;
  4957. name_offset = le16_to_cpu(xe->xe_name_offset);
  4958. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  4959. i = xbs->here - xbs->header->xh_entries;
  4960. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  4961. ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
  4962. bucket_xh(xbs->bucket),
  4963. i, &block_off,
  4964. &name_offset);
  4965. if (ret) {
  4966. mlog_errno(ret);
  4967. goto out;
  4968. }
  4969. base = bucket_block(xbs->bucket, block_off);
  4970. vb.vb_bh = xbs->bucket->bu_bhs[block_off];
  4971. vb.vb_access = ocfs2_journal_access;
  4972. if (ocfs2_meta_ecc(osb)) {
  4973. /*create parameters for ocfs2_post_refcount. */
  4974. bucket = xbs->bucket;
  4975. refcount.credits = bucket->bu_blocks;
  4976. refcount.para = bucket;
  4977. refcount.func =
  4978. ocfs2_xattr_bucket_post_refcount;
  4979. p = &refcount;
  4980. }
  4981. } else {
  4982. base = xbs->base;
  4983. vb.vb_bh = xbs->xattr_bh;
  4984. vb.vb_access = ocfs2_journal_access_xb;
  4985. }
  4986. }
  4987. if (ocfs2_xattr_is_local(xe))
  4988. goto out;
  4989. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  4990. (base + name_offset + name_len);
  4991. ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
  4992. &num_clusters, &vb.vb_xv->xr_list,
  4993. &ext_flags);
  4994. if (ret) {
  4995. mlog_errno(ret);
  4996. goto out;
  4997. }
  4998. /*
  4999. * We just need to check the 1st extent record, since we always
  5000. * CoW the whole xattr. So there shouldn't be a xattr with
  5001. * some REFCOUNT extent recs after the 1st one.
  5002. */
  5003. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  5004. goto out;
  5005. ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
  5006. 1, ref_tree, &ref_root_bh);
  5007. if (ret) {
  5008. mlog_errno(ret);
  5009. goto out;
  5010. }
  5011. /*
  5012. * If we are deleting the xattr or the new size will be stored inside,
  5013. * cool, leave it there, the xattr truncate process will remove them
  5014. * for us(it still needs the refcount tree lock and the meta, credits).
  5015. * And the worse case is that every cluster truncate will split the
  5016. * refcount tree, and make the original extent become 3. So we will need
  5017. * 2 * cluster more extent recs at most.
  5018. */
  5019. if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
  5020. ret = ocfs2_refcounted_xattr_delete_need(inode,
  5021. &(*ref_tree)->rf_ci,
  5022. ref_root_bh, vb.vb_xv,
  5023. meta_add, credits);
  5024. if (ret)
  5025. mlog_errno(ret);
  5026. goto out;
  5027. }
  5028. ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
  5029. *ref_tree, ref_root_bh, 0,
  5030. le32_to_cpu(vb.vb_xv->xr_clusters), p);
  5031. if (ret)
  5032. mlog_errno(ret);
  5033. out:
  5034. brelse(ref_root_bh);
  5035. return ret;
  5036. }
  5037. /*
  5038. * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
  5039. * The physical clusters will be added to refcount tree.
  5040. */
  5041. static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
  5042. struct ocfs2_xattr_value_root *xv,
  5043. struct ocfs2_extent_tree *value_et,
  5044. struct ocfs2_caching_info *ref_ci,
  5045. struct buffer_head *ref_root_bh,
  5046. struct ocfs2_cached_dealloc_ctxt *dealloc,
  5047. struct ocfs2_post_refcount *refcount)
  5048. {
  5049. int ret = 0;
  5050. u32 clusters = le32_to_cpu(xv->xr_clusters);
  5051. u32 cpos, p_cluster, num_clusters;
  5052. struct ocfs2_extent_list *el = &xv->xr_list;
  5053. unsigned int ext_flags;
  5054. cpos = 0;
  5055. while (cpos < clusters) {
  5056. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  5057. &num_clusters, el, &ext_flags);
  5058. cpos += num_clusters;
  5059. if ((ext_flags & OCFS2_EXT_REFCOUNTED))
  5060. continue;
  5061. BUG_ON(!p_cluster);
  5062. ret = ocfs2_add_refcount_flag(inode, value_et,
  5063. ref_ci, ref_root_bh,
  5064. cpos - num_clusters,
  5065. p_cluster, num_clusters,
  5066. dealloc, refcount);
  5067. if (ret) {
  5068. mlog_errno(ret);
  5069. break;
  5070. }
  5071. }
  5072. return ret;
  5073. }
  5074. /*
  5075. * Given a normal ocfs2_xattr_header, refcount all the entries which
  5076. * have value stored outside.
  5077. * Used for xattrs stored in inode and ocfs2_xattr_block.
  5078. */
  5079. static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
  5080. struct ocfs2_xattr_value_buf *vb,
  5081. struct ocfs2_xattr_header *header,
  5082. struct ocfs2_caching_info *ref_ci,
  5083. struct buffer_head *ref_root_bh,
  5084. struct ocfs2_cached_dealloc_ctxt *dealloc)
  5085. {
  5086. struct ocfs2_xattr_entry *xe;
  5087. struct ocfs2_xattr_value_root *xv;
  5088. struct ocfs2_extent_tree et;
  5089. int i, ret = 0;
  5090. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  5091. xe = &header->xh_entries[i];
  5092. if (ocfs2_xattr_is_local(xe))
  5093. continue;
  5094. xv = (struct ocfs2_xattr_value_root *)((void *)header +
  5095. le16_to_cpu(xe->xe_name_offset) +
  5096. OCFS2_XATTR_SIZE(xe->xe_name_len));
  5097. vb->vb_xv = xv;
  5098. ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
  5099. ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
  5100. ref_ci, ref_root_bh,
  5101. dealloc, NULL);
  5102. if (ret) {
  5103. mlog_errno(ret);
  5104. break;
  5105. }
  5106. }
  5107. return ret;
  5108. }
  5109. static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
  5110. struct buffer_head *fe_bh,
  5111. struct ocfs2_caching_info *ref_ci,
  5112. struct buffer_head *ref_root_bh,
  5113. struct ocfs2_cached_dealloc_ctxt *dealloc)
  5114. {
  5115. struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
  5116. struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
  5117. (fe_bh->b_data + inode->i_sb->s_blocksize -
  5118. le16_to_cpu(di->i_xattr_inline_size));
  5119. struct ocfs2_xattr_value_buf vb = {
  5120. .vb_bh = fe_bh,
  5121. .vb_access = ocfs2_journal_access_di,
  5122. };
  5123. return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
  5124. ref_ci, ref_root_bh, dealloc);
  5125. }
  5126. struct ocfs2_xattr_tree_value_refcount_para {
  5127. struct ocfs2_caching_info *ref_ci;
  5128. struct buffer_head *ref_root_bh;
  5129. struct ocfs2_cached_dealloc_ctxt *dealloc;
  5130. };
  5131. static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
  5132. struct ocfs2_xattr_bucket *bucket,
  5133. int offset,
  5134. struct ocfs2_xattr_value_root **xv,
  5135. struct buffer_head **bh)
  5136. {
  5137. int ret, block_off, name_offset;
  5138. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  5139. struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
  5140. void *base;
  5141. ret = ocfs2_xattr_bucket_get_name_value(sb,
  5142. bucket_xh(bucket),
  5143. offset,
  5144. &block_off,
  5145. &name_offset);
  5146. if (ret) {
  5147. mlog_errno(ret);
  5148. goto out;
  5149. }
  5150. base = bucket_block(bucket, block_off);
  5151. *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
  5152. OCFS2_XATTR_SIZE(xe->xe_name_len));
  5153. if (bh)
  5154. *bh = bucket->bu_bhs[block_off];
  5155. out:
  5156. return ret;
  5157. }
  5158. /*
  5159. * For a given xattr bucket, refcount all the entries which
  5160. * have value stored outside.
  5161. */
  5162. static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
  5163. struct ocfs2_xattr_bucket *bucket,
  5164. void *para)
  5165. {
  5166. int i, ret = 0;
  5167. struct ocfs2_extent_tree et;
  5168. struct ocfs2_xattr_tree_value_refcount_para *ref =
  5169. (struct ocfs2_xattr_tree_value_refcount_para *)para;
  5170. struct ocfs2_xattr_header *xh =
  5171. (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
  5172. struct ocfs2_xattr_entry *xe;
  5173. struct ocfs2_xattr_value_buf vb = {
  5174. .vb_access = ocfs2_journal_access,
  5175. };
  5176. struct ocfs2_post_refcount refcount = {
  5177. .credits = bucket->bu_blocks,
  5178. .para = bucket,
  5179. .func = ocfs2_xattr_bucket_post_refcount,
  5180. };
  5181. struct ocfs2_post_refcount *p = NULL;
  5182. /* We only need post_refcount if we support metaecc. */
  5183. if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
  5184. p = &refcount;
  5185. mlog(0, "refcount bucket %llu, count = %u\n",
  5186. (unsigned long long)bucket_blkno(bucket),
  5187. le16_to_cpu(xh->xh_count));
  5188. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  5189. xe = &xh->xh_entries[i];
  5190. if (ocfs2_xattr_is_local(xe))
  5191. continue;
  5192. ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
  5193. &vb.vb_xv, &vb.vb_bh);
  5194. if (ret) {
  5195. mlog_errno(ret);
  5196. break;
  5197. }
  5198. ocfs2_init_xattr_value_extent_tree(&et,
  5199. INODE_CACHE(inode), &vb);
  5200. ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
  5201. &et, ref->ref_ci,
  5202. ref->ref_root_bh,
  5203. ref->dealloc, p);
  5204. if (ret) {
  5205. mlog_errno(ret);
  5206. break;
  5207. }
  5208. }
  5209. return ret;
  5210. }
  5211. static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
  5212. struct buffer_head *root_bh,
  5213. u64 blkno, u32 cpos, u32 len, void *para)
  5214. {
  5215. return ocfs2_iterate_xattr_buckets(inode, blkno, len,
  5216. ocfs2_xattr_bucket_value_refcount,
  5217. para);
  5218. }
  5219. static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
  5220. struct buffer_head *blk_bh,
  5221. struct ocfs2_caching_info *ref_ci,
  5222. struct buffer_head *ref_root_bh,
  5223. struct ocfs2_cached_dealloc_ctxt *dealloc)
  5224. {
  5225. int ret = 0;
  5226. struct ocfs2_xattr_block *xb =
  5227. (struct ocfs2_xattr_block *)blk_bh->b_data;
  5228. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  5229. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  5230. struct ocfs2_xattr_value_buf vb = {
  5231. .vb_bh = blk_bh,
  5232. .vb_access = ocfs2_journal_access_xb,
  5233. };
  5234. ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
  5235. ref_ci, ref_root_bh,
  5236. dealloc);
  5237. } else {
  5238. struct ocfs2_xattr_tree_value_refcount_para para = {
  5239. .ref_ci = ref_ci,
  5240. .ref_root_bh = ref_root_bh,
  5241. .dealloc = dealloc,
  5242. };
  5243. ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
  5244. ocfs2_refcount_xattr_tree_rec,
  5245. &para);
  5246. }
  5247. return ret;
  5248. }
  5249. int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
  5250. struct buffer_head *fe_bh,
  5251. struct ocfs2_caching_info *ref_ci,
  5252. struct buffer_head *ref_root_bh,
  5253. struct ocfs2_cached_dealloc_ctxt *dealloc)
  5254. {
  5255. int ret = 0;
  5256. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  5257. struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
  5258. struct buffer_head *blk_bh = NULL;
  5259. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  5260. ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
  5261. ref_ci, ref_root_bh,
  5262. dealloc);
  5263. if (ret) {
  5264. mlog_errno(ret);
  5265. goto out;
  5266. }
  5267. }
  5268. if (!di->i_xattr_loc)
  5269. goto out;
  5270. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  5271. &blk_bh);
  5272. if (ret < 0) {
  5273. mlog_errno(ret);
  5274. goto out;
  5275. }
  5276. ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
  5277. ref_root_bh, dealloc);
  5278. if (ret)
  5279. mlog_errno(ret);
  5280. brelse(blk_bh);
  5281. out:
  5282. return ret;
  5283. }
  5284. typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
  5285. /*
  5286. * Store the information we need in xattr reflink.
  5287. * old_bh and new_bh are inode bh for the old and new inode.
  5288. */
  5289. struct ocfs2_xattr_reflink {
  5290. struct inode *old_inode;
  5291. struct inode *new_inode;
  5292. struct buffer_head *old_bh;
  5293. struct buffer_head *new_bh;
  5294. struct ocfs2_caching_info *ref_ci;
  5295. struct buffer_head *ref_root_bh;
  5296. struct ocfs2_cached_dealloc_ctxt *dealloc;
  5297. should_xattr_reflinked *xattr_reflinked;
  5298. };
  5299. /*
  5300. * Given a xattr header and xe offset,
  5301. * return the proper xv and the corresponding bh.
  5302. * xattr in inode, block and xattr tree have different implementaions.
  5303. */
  5304. typedef int (get_xattr_value_root)(struct super_block *sb,
  5305. struct buffer_head *bh,
  5306. struct ocfs2_xattr_header *xh,
  5307. int offset,
  5308. struct ocfs2_xattr_value_root **xv,
  5309. struct buffer_head **ret_bh,
  5310. void *para);
  5311. /*
  5312. * Calculate all the xattr value root metadata stored in this xattr header and
  5313. * credits we need if we create them from the scratch.
  5314. * We use get_xattr_value_root so that all types of xattr container can use it.
  5315. */
  5316. static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
  5317. struct buffer_head *bh,
  5318. struct ocfs2_xattr_header *xh,
  5319. int *metas, int *credits,
  5320. int *num_recs,
  5321. get_xattr_value_root *func,
  5322. void *para)
  5323. {
  5324. int i, ret = 0;
  5325. struct ocfs2_xattr_value_root *xv;
  5326. struct ocfs2_xattr_entry *xe;
  5327. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  5328. xe = &xh->xh_entries[i];
  5329. if (ocfs2_xattr_is_local(xe))
  5330. continue;
  5331. ret = func(sb, bh, xh, i, &xv, NULL, para);
  5332. if (ret) {
  5333. mlog_errno(ret);
  5334. break;
  5335. }
  5336. *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
  5337. le16_to_cpu(xv->xr_list.l_next_free_rec);
  5338. *credits += ocfs2_calc_extend_credits(sb,
  5339. &def_xv.xv.xr_list,
  5340. le32_to_cpu(xv->xr_clusters));
  5341. /*
  5342. * If the value is a tree with depth > 1, We don't go deep
  5343. * to the extent block, so just calculate a maximum record num.
  5344. */
  5345. if (!xv->xr_list.l_tree_depth)
  5346. *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
  5347. else
  5348. *num_recs += ocfs2_clusters_for_bytes(sb,
  5349. XATTR_SIZE_MAX);
  5350. }
  5351. return ret;
  5352. }
  5353. /* Used by xattr inode and block to return the right xv and buffer_head. */
  5354. static int ocfs2_get_xattr_value_root(struct super_block *sb,
  5355. struct buffer_head *bh,
  5356. struct ocfs2_xattr_header *xh,
  5357. int offset,
  5358. struct ocfs2_xattr_value_root **xv,
  5359. struct buffer_head **ret_bh,
  5360. void *para)
  5361. {
  5362. struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
  5363. *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
  5364. le16_to_cpu(xe->xe_name_offset) +
  5365. OCFS2_XATTR_SIZE(xe->xe_name_len));
  5366. if (ret_bh)
  5367. *ret_bh = bh;
  5368. return 0;
  5369. }
  5370. /*
  5371. * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
  5372. * It is only used for inline xattr and xattr block.
  5373. */
  5374. static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
  5375. struct ocfs2_xattr_header *xh,
  5376. struct buffer_head *ref_root_bh,
  5377. int *credits,
  5378. struct ocfs2_alloc_context **meta_ac)
  5379. {
  5380. int ret, meta_add = 0, num_recs = 0;
  5381. struct ocfs2_refcount_block *rb =
  5382. (struct ocfs2_refcount_block *)ref_root_bh->b_data;
  5383. *credits = 0;
  5384. ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
  5385. &meta_add, credits, &num_recs,
  5386. ocfs2_get_xattr_value_root,
  5387. NULL);
  5388. if (ret) {
  5389. mlog_errno(ret);
  5390. goto out;
  5391. }
  5392. /*
  5393. * We need to add/modify num_recs in refcount tree, so just calculate
  5394. * an approximate number we need for refcount tree change.
  5395. * Sometimes we need to split the tree, and after split, half recs
  5396. * will be moved to the new block, and a new block can only provide
  5397. * half number of recs. So we multiple new blocks by 2.
  5398. */
  5399. num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
  5400. meta_add += num_recs;
  5401. *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
  5402. if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
  5403. *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
  5404. le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
  5405. else
  5406. *credits += 1;
  5407. ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
  5408. if (ret)
  5409. mlog_errno(ret);
  5410. out:
  5411. return ret;
  5412. }
  5413. /*
  5414. * Given a xattr header, reflink all the xattrs in this container.
  5415. * It can be used for inode, block and bucket.
  5416. *
  5417. * NOTE:
  5418. * Before we call this function, the caller has memcpy the xattr in
  5419. * old_xh to the new_xh.
  5420. *
  5421. * If args.xattr_reflinked is set, call it to decide whether the xe should
  5422. * be reflinked or not. If not, remove it from the new xattr header.
  5423. */
  5424. static int ocfs2_reflink_xattr_header(handle_t *handle,
  5425. struct ocfs2_xattr_reflink *args,
  5426. struct buffer_head *old_bh,
  5427. struct ocfs2_xattr_header *xh,
  5428. struct buffer_head *new_bh,
  5429. struct ocfs2_xattr_header *new_xh,
  5430. struct ocfs2_xattr_value_buf *vb,
  5431. struct ocfs2_alloc_context *meta_ac,
  5432. get_xattr_value_root *func,
  5433. void *para)
  5434. {
  5435. int ret = 0, i, j;
  5436. struct super_block *sb = args->old_inode->i_sb;
  5437. struct buffer_head *value_bh;
  5438. struct ocfs2_xattr_entry *xe, *last;
  5439. struct ocfs2_xattr_value_root *xv, *new_xv;
  5440. struct ocfs2_extent_tree data_et;
  5441. u32 clusters, cpos, p_cluster, num_clusters;
  5442. unsigned int ext_flags = 0;
  5443. mlog(0, "reflink xattr in container %llu, count = %u\n",
  5444. (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
  5445. last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
  5446. for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
  5447. xe = &xh->xh_entries[i];
  5448. if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
  5449. xe = &new_xh->xh_entries[j];
  5450. le16_add_cpu(&new_xh->xh_count, -1);
  5451. if (new_xh->xh_count) {
  5452. memmove(xe, xe + 1,
  5453. (void *)last - (void *)xe);
  5454. memset(last, 0,
  5455. sizeof(struct ocfs2_xattr_entry));
  5456. }
  5457. /*
  5458. * We don't want j to increase in the next round since
  5459. * it is already moved ahead.
  5460. */
  5461. j--;
  5462. continue;
  5463. }
  5464. if (ocfs2_xattr_is_local(xe))
  5465. continue;
  5466. ret = func(sb, old_bh, xh, i, &xv, NULL, para);
  5467. if (ret) {
  5468. mlog_errno(ret);
  5469. break;
  5470. }
  5471. ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
  5472. if (ret) {
  5473. mlog_errno(ret);
  5474. break;
  5475. }
  5476. /*
  5477. * For the xattr which has l_tree_depth = 0, all the extent
  5478. * recs have already be copied to the new xh with the
  5479. * propriate OCFS2_EXT_REFCOUNTED flag we just need to
  5480. * increase the refount count int the refcount tree.
  5481. *
  5482. * For the xattr which has l_tree_depth > 0, we need
  5483. * to initialize it to the empty default value root,
  5484. * and then insert the extents one by one.
  5485. */
  5486. if (xv->xr_list.l_tree_depth) {
  5487. memcpy(new_xv, &def_xv, sizeof(def_xv));
  5488. vb->vb_xv = new_xv;
  5489. vb->vb_bh = value_bh;
  5490. ocfs2_init_xattr_value_extent_tree(&data_et,
  5491. INODE_CACHE(args->new_inode), vb);
  5492. }
  5493. clusters = le32_to_cpu(xv->xr_clusters);
  5494. cpos = 0;
  5495. while (cpos < clusters) {
  5496. ret = ocfs2_xattr_get_clusters(args->old_inode,
  5497. cpos,
  5498. &p_cluster,
  5499. &num_clusters,
  5500. &xv->xr_list,
  5501. &ext_flags);
  5502. if (ret) {
  5503. mlog_errno(ret);
  5504. goto out;
  5505. }
  5506. BUG_ON(!p_cluster);
  5507. if (xv->xr_list.l_tree_depth) {
  5508. ret = ocfs2_insert_extent(handle,
  5509. &data_et, cpos,
  5510. ocfs2_clusters_to_blocks(
  5511. args->old_inode->i_sb,
  5512. p_cluster),
  5513. num_clusters, ext_flags,
  5514. meta_ac);
  5515. if (ret) {
  5516. mlog_errno(ret);
  5517. goto out;
  5518. }
  5519. }
  5520. ret = ocfs2_increase_refcount(handle, args->ref_ci,
  5521. args->ref_root_bh,
  5522. p_cluster, num_clusters,
  5523. meta_ac, args->dealloc);
  5524. if (ret) {
  5525. mlog_errno(ret);
  5526. goto out;
  5527. }
  5528. cpos += num_clusters;
  5529. }
  5530. }
  5531. out:
  5532. return ret;
  5533. }
  5534. static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
  5535. {
  5536. int ret = 0, credits = 0;
  5537. handle_t *handle;
  5538. struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
  5539. struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
  5540. int inline_size = le16_to_cpu(di->i_xattr_inline_size);
  5541. int header_off = osb->sb->s_blocksize - inline_size;
  5542. struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
  5543. (args->old_bh->b_data + header_off);
  5544. struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
  5545. (args->new_bh->b_data + header_off);
  5546. struct ocfs2_alloc_context *meta_ac = NULL;
  5547. struct ocfs2_inode_info *new_oi;
  5548. struct ocfs2_dinode *new_di;
  5549. struct ocfs2_xattr_value_buf vb = {
  5550. .vb_bh = args->new_bh,
  5551. .vb_access = ocfs2_journal_access_di,
  5552. };
  5553. ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
  5554. &credits, &meta_ac);
  5555. if (ret) {
  5556. mlog_errno(ret);
  5557. goto out;
  5558. }
  5559. handle = ocfs2_start_trans(osb, credits);
  5560. if (IS_ERR(handle)) {
  5561. ret = PTR_ERR(handle);
  5562. mlog_errno(ret);
  5563. goto out;
  5564. }
  5565. ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
  5566. args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  5567. if (ret) {
  5568. mlog_errno(ret);
  5569. goto out_commit;
  5570. }
  5571. memcpy(args->new_bh->b_data + header_off,
  5572. args->old_bh->b_data + header_off, inline_size);
  5573. new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
  5574. new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
  5575. ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
  5576. args->new_bh, new_xh, &vb, meta_ac,
  5577. ocfs2_get_xattr_value_root, NULL);
  5578. if (ret) {
  5579. mlog_errno(ret);
  5580. goto out_commit;
  5581. }
  5582. new_oi = OCFS2_I(args->new_inode);
  5583. spin_lock(&new_oi->ip_lock);
  5584. new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
  5585. new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
  5586. spin_unlock(&new_oi->ip_lock);
  5587. ocfs2_journal_dirty(handle, args->new_bh);
  5588. out_commit:
  5589. ocfs2_commit_trans(osb, handle);
  5590. out:
  5591. if (meta_ac)
  5592. ocfs2_free_alloc_context(meta_ac);
  5593. return ret;
  5594. }
  5595. static int ocfs2_create_empty_xattr_block(struct inode *inode,
  5596. struct buffer_head *fe_bh,
  5597. struct buffer_head **ret_bh,
  5598. int indexed)
  5599. {
  5600. int ret;
  5601. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  5602. struct ocfs2_xattr_set_ctxt ctxt;
  5603. memset(&ctxt, 0, sizeof(ctxt));
  5604. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
  5605. if (ret < 0) {
  5606. mlog_errno(ret);
  5607. return ret;
  5608. }
  5609. ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
  5610. if (IS_ERR(ctxt.handle)) {
  5611. ret = PTR_ERR(ctxt.handle);
  5612. mlog_errno(ret);
  5613. goto out;
  5614. }
  5615. mlog(0, "create new xattr block for inode %llu, index = %d\n",
  5616. (unsigned long long)fe_bh->b_blocknr, indexed);
  5617. ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
  5618. ret_bh);
  5619. if (ret)
  5620. mlog_errno(ret);
  5621. ocfs2_commit_trans(osb, ctxt.handle);
  5622. out:
  5623. ocfs2_free_alloc_context(ctxt.meta_ac);
  5624. return ret;
  5625. }
  5626. static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
  5627. struct buffer_head *blk_bh,
  5628. struct buffer_head *new_blk_bh)
  5629. {
  5630. int ret = 0, credits = 0;
  5631. handle_t *handle;
  5632. struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
  5633. struct ocfs2_dinode *new_di;
  5634. struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
  5635. int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  5636. struct ocfs2_xattr_block *xb =
  5637. (struct ocfs2_xattr_block *)blk_bh->b_data;
  5638. struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
  5639. struct ocfs2_xattr_block *new_xb =
  5640. (struct ocfs2_xattr_block *)new_blk_bh->b_data;
  5641. struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
  5642. struct ocfs2_alloc_context *meta_ac;
  5643. struct ocfs2_xattr_value_buf vb = {
  5644. .vb_bh = new_blk_bh,
  5645. .vb_access = ocfs2_journal_access_xb,
  5646. };
  5647. ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
  5648. &credits, &meta_ac);
  5649. if (ret) {
  5650. mlog_errno(ret);
  5651. return ret;
  5652. }
  5653. /* One more credits in case we need to add xattr flags in new inode. */
  5654. handle = ocfs2_start_trans(osb, credits + 1);
  5655. if (IS_ERR(handle)) {
  5656. ret = PTR_ERR(handle);
  5657. mlog_errno(ret);
  5658. goto out;
  5659. }
  5660. if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
  5661. ret = ocfs2_journal_access_di(handle,
  5662. INODE_CACHE(args->new_inode),
  5663. args->new_bh,
  5664. OCFS2_JOURNAL_ACCESS_WRITE);
  5665. if (ret) {
  5666. mlog_errno(ret);
  5667. goto out_commit;
  5668. }
  5669. }
  5670. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
  5671. new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
  5672. if (ret) {
  5673. mlog_errno(ret);
  5674. goto out_commit;
  5675. }
  5676. memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
  5677. osb->sb->s_blocksize - header_off);
  5678. ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
  5679. new_blk_bh, new_xh, &vb, meta_ac,
  5680. ocfs2_get_xattr_value_root, NULL);
  5681. if (ret) {
  5682. mlog_errno(ret);
  5683. goto out_commit;
  5684. }
  5685. ocfs2_journal_dirty(handle, new_blk_bh);
  5686. if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
  5687. new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
  5688. spin_lock(&new_oi->ip_lock);
  5689. new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
  5690. new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
  5691. spin_unlock(&new_oi->ip_lock);
  5692. ocfs2_journal_dirty(handle, args->new_bh);
  5693. }
  5694. out_commit:
  5695. ocfs2_commit_trans(osb, handle);
  5696. out:
  5697. ocfs2_free_alloc_context(meta_ac);
  5698. return ret;
  5699. }
  5700. struct ocfs2_reflink_xattr_tree_args {
  5701. struct ocfs2_xattr_reflink *reflink;
  5702. struct buffer_head *old_blk_bh;
  5703. struct buffer_head *new_blk_bh;
  5704. struct ocfs2_xattr_bucket *old_bucket;
  5705. struct ocfs2_xattr_bucket *new_bucket;
  5706. };
  5707. /*
  5708. * NOTE:
  5709. * We have to handle the case that both old bucket and new bucket
  5710. * will call this function to get the right ret_bh.
  5711. * So The caller must give us the right bh.
  5712. */
  5713. static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
  5714. struct buffer_head *bh,
  5715. struct ocfs2_xattr_header *xh,
  5716. int offset,
  5717. struct ocfs2_xattr_value_root **xv,
  5718. struct buffer_head **ret_bh,
  5719. void *para)
  5720. {
  5721. struct ocfs2_reflink_xattr_tree_args *args =
  5722. (struct ocfs2_reflink_xattr_tree_args *)para;
  5723. struct ocfs2_xattr_bucket *bucket;
  5724. if (bh == args->old_bucket->bu_bhs[0])
  5725. bucket = args->old_bucket;
  5726. else
  5727. bucket = args->new_bucket;
  5728. return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
  5729. xv, ret_bh);
  5730. }
  5731. struct ocfs2_value_tree_metas {
  5732. int num_metas;
  5733. int credits;
  5734. int num_recs;
  5735. };
  5736. static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
  5737. struct buffer_head *bh,
  5738. struct ocfs2_xattr_header *xh,
  5739. int offset,
  5740. struct ocfs2_xattr_value_root **xv,
  5741. struct buffer_head **ret_bh,
  5742. void *para)
  5743. {
  5744. struct ocfs2_xattr_bucket *bucket =
  5745. (struct ocfs2_xattr_bucket *)para;
  5746. return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
  5747. xv, ret_bh);
  5748. }
  5749. static int ocfs2_calc_value_tree_metas(struct inode *inode,
  5750. struct ocfs2_xattr_bucket *bucket,
  5751. void *para)
  5752. {
  5753. struct ocfs2_value_tree_metas *metas =
  5754. (struct ocfs2_value_tree_metas *)para;
  5755. struct ocfs2_xattr_header *xh =
  5756. (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
  5757. /* Add the credits for this bucket first. */
  5758. metas->credits += bucket->bu_blocks;
  5759. return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
  5760. xh, &metas->num_metas,
  5761. &metas->credits, &metas->num_recs,
  5762. ocfs2_value_tree_metas_in_bucket,
  5763. bucket);
  5764. }
  5765. /*
  5766. * Given a xattr extent rec starting from blkno and having len clusters,
  5767. * iterate all the buckets calculate how much metadata we need for reflinking
  5768. * all the ocfs2_xattr_value_root and lock the allocators accordingly.
  5769. */
  5770. static int ocfs2_lock_reflink_xattr_rec_allocators(
  5771. struct ocfs2_reflink_xattr_tree_args *args,
  5772. struct ocfs2_extent_tree *xt_et,
  5773. u64 blkno, u32 len, int *credits,
  5774. struct ocfs2_alloc_context **meta_ac,
  5775. struct ocfs2_alloc_context **data_ac)
  5776. {
  5777. int ret, num_free_extents;
  5778. struct ocfs2_value_tree_metas metas;
  5779. struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
  5780. struct ocfs2_refcount_block *rb;
  5781. memset(&metas, 0, sizeof(metas));
  5782. ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
  5783. ocfs2_calc_value_tree_metas, &metas);
  5784. if (ret) {
  5785. mlog_errno(ret);
  5786. goto out;
  5787. }
  5788. *credits = metas.credits;
  5789. /*
  5790. * Calculate we need for refcount tree change.
  5791. *
  5792. * We need to add/modify num_recs in refcount tree, so just calculate
  5793. * an approximate number we need for refcount tree change.
  5794. * Sometimes we need to split the tree, and after split, half recs
  5795. * will be moved to the new block, and a new block can only provide
  5796. * half number of recs. So we multiple new blocks by 2.
  5797. * In the end, we have to add credits for modifying the already
  5798. * existed refcount block.
  5799. */
  5800. rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
  5801. metas.num_recs =
  5802. (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
  5803. ocfs2_refcount_recs_per_rb(osb->sb) * 2;
  5804. metas.num_metas += metas.num_recs;
  5805. *credits += metas.num_recs +
  5806. metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
  5807. if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
  5808. *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
  5809. le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
  5810. else
  5811. *credits += 1;
  5812. /* count in the xattr tree change. */
  5813. num_free_extents = ocfs2_num_free_extents(osb, xt_et);
  5814. if (num_free_extents < 0) {
  5815. ret = num_free_extents;
  5816. mlog_errno(ret);
  5817. goto out;
  5818. }
  5819. if (num_free_extents < len)
  5820. metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
  5821. *credits += ocfs2_calc_extend_credits(osb->sb,
  5822. xt_et->et_root_el, len);
  5823. if (metas.num_metas) {
  5824. ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
  5825. meta_ac);
  5826. if (ret) {
  5827. mlog_errno(ret);
  5828. goto out;
  5829. }
  5830. }
  5831. if (len) {
  5832. ret = ocfs2_reserve_clusters(osb, len, data_ac);
  5833. if (ret)
  5834. mlog_errno(ret);
  5835. }
  5836. out:
  5837. if (ret) {
  5838. if (*meta_ac) {
  5839. ocfs2_free_alloc_context(*meta_ac);
  5840. meta_ac = NULL;
  5841. }
  5842. }
  5843. return ret;
  5844. }
  5845. static int ocfs2_reflink_xattr_buckets(handle_t *handle,
  5846. u64 blkno, u64 new_blkno, u32 clusters,
  5847. struct ocfs2_alloc_context *meta_ac,
  5848. struct ocfs2_alloc_context *data_ac,
  5849. struct ocfs2_reflink_xattr_tree_args *args)
  5850. {
  5851. int i, j, ret = 0;
  5852. struct super_block *sb = args->reflink->old_inode->i_sb;
  5853. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
  5854. u32 num_buckets = clusters * bpc;
  5855. int bpb = args->old_bucket->bu_blocks;
  5856. struct ocfs2_xattr_value_buf vb = {
  5857. .vb_access = ocfs2_journal_access,
  5858. };
  5859. for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
  5860. ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
  5861. if (ret) {
  5862. mlog_errno(ret);
  5863. break;
  5864. }
  5865. ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
  5866. if (ret) {
  5867. mlog_errno(ret);
  5868. break;
  5869. }
  5870. /*
  5871. * The real bucket num in this series of blocks is stored
  5872. * in the 1st bucket.
  5873. */
  5874. if (i == 0)
  5875. num_buckets = le16_to_cpu(
  5876. bucket_xh(args->old_bucket)->xh_num_buckets);
  5877. ret = ocfs2_xattr_bucket_journal_access(handle,
  5878. args->new_bucket,
  5879. OCFS2_JOURNAL_ACCESS_CREATE);
  5880. if (ret) {
  5881. mlog_errno(ret);
  5882. break;
  5883. }
  5884. for (j = 0; j < bpb; j++)
  5885. memcpy(bucket_block(args->new_bucket, j),
  5886. bucket_block(args->old_bucket, j),
  5887. sb->s_blocksize);
  5888. ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
  5889. ret = ocfs2_reflink_xattr_header(handle, args->reflink,
  5890. args->old_bucket->bu_bhs[0],
  5891. bucket_xh(args->old_bucket),
  5892. args->new_bucket->bu_bhs[0],
  5893. bucket_xh(args->new_bucket),
  5894. &vb, meta_ac,
  5895. ocfs2_get_reflink_xattr_value_root,
  5896. args);
  5897. if (ret) {
  5898. mlog_errno(ret);
  5899. break;
  5900. }
  5901. /*
  5902. * Re-access and dirty the bucket to calculate metaecc.
  5903. * Because we may extend the transaction in reflink_xattr_header
  5904. * which will let the already accessed block gone.
  5905. */
  5906. ret = ocfs2_xattr_bucket_journal_access(handle,
  5907. args->new_bucket,
  5908. OCFS2_JOURNAL_ACCESS_WRITE);
  5909. if (ret) {
  5910. mlog_errno(ret);
  5911. break;
  5912. }
  5913. ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
  5914. ocfs2_xattr_bucket_relse(args->old_bucket);
  5915. ocfs2_xattr_bucket_relse(args->new_bucket);
  5916. }
  5917. ocfs2_xattr_bucket_relse(args->old_bucket);
  5918. ocfs2_xattr_bucket_relse(args->new_bucket);
  5919. return ret;
  5920. }
  5921. /*
  5922. * Create the same xattr extent record in the new inode's xattr tree.
  5923. */
  5924. static int ocfs2_reflink_xattr_rec(struct inode *inode,
  5925. struct buffer_head *root_bh,
  5926. u64 blkno,
  5927. u32 cpos,
  5928. u32 len,
  5929. void *para)
  5930. {
  5931. int ret, credits = 0;
  5932. u32 p_cluster, num_clusters;
  5933. u64 new_blkno;
  5934. handle_t *handle;
  5935. struct ocfs2_reflink_xattr_tree_args *args =
  5936. (struct ocfs2_reflink_xattr_tree_args *)para;
  5937. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  5938. struct ocfs2_alloc_context *meta_ac = NULL;
  5939. struct ocfs2_alloc_context *data_ac = NULL;
  5940. struct ocfs2_extent_tree et;
  5941. ocfs2_init_xattr_tree_extent_tree(&et,
  5942. INODE_CACHE(args->reflink->new_inode),
  5943. args->new_blk_bh);
  5944. ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
  5945. len, &credits,
  5946. &meta_ac, &data_ac);
  5947. if (ret) {
  5948. mlog_errno(ret);
  5949. goto out;
  5950. }
  5951. handle = ocfs2_start_trans(osb, credits);
  5952. if (IS_ERR(handle)) {
  5953. ret = PTR_ERR(handle);
  5954. mlog_errno(ret);
  5955. goto out;
  5956. }
  5957. ret = ocfs2_claim_clusters(osb, handle, data_ac,
  5958. len, &p_cluster, &num_clusters);
  5959. if (ret) {
  5960. mlog_errno(ret);
  5961. goto out_commit;
  5962. }
  5963. new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
  5964. mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
  5965. (unsigned long long)blkno, (unsigned long long)new_blkno, len);
  5966. ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
  5967. meta_ac, data_ac, args);
  5968. if (ret) {
  5969. mlog_errno(ret);
  5970. goto out_commit;
  5971. }
  5972. mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
  5973. (unsigned long long)new_blkno, len, cpos);
  5974. ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
  5975. len, 0, meta_ac);
  5976. if (ret)
  5977. mlog_errno(ret);
  5978. out_commit:
  5979. ocfs2_commit_trans(osb, handle);
  5980. out:
  5981. if (meta_ac)
  5982. ocfs2_free_alloc_context(meta_ac);
  5983. if (data_ac)
  5984. ocfs2_free_alloc_context(data_ac);
  5985. return ret;
  5986. }
  5987. /*
  5988. * Create reflinked xattr buckets.
  5989. * We will add bucket one by one, and refcount all the xattrs in the bucket
  5990. * if they are stored outside.
  5991. */
  5992. static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
  5993. struct buffer_head *blk_bh,
  5994. struct buffer_head *new_blk_bh)
  5995. {
  5996. int ret;
  5997. struct ocfs2_reflink_xattr_tree_args para;
  5998. memset(&para, 0, sizeof(para));
  5999. para.reflink = args;
  6000. para.old_blk_bh = blk_bh;
  6001. para.new_blk_bh = new_blk_bh;
  6002. para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
  6003. if (!para.old_bucket) {
  6004. mlog_errno(-ENOMEM);
  6005. return -ENOMEM;
  6006. }
  6007. para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
  6008. if (!para.new_bucket) {
  6009. ret = -ENOMEM;
  6010. mlog_errno(ret);
  6011. goto out;
  6012. }
  6013. ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
  6014. ocfs2_reflink_xattr_rec,
  6015. &para);
  6016. if (ret)
  6017. mlog_errno(ret);
  6018. out:
  6019. ocfs2_xattr_bucket_free(para.old_bucket);
  6020. ocfs2_xattr_bucket_free(para.new_bucket);
  6021. return ret;
  6022. }
  6023. static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
  6024. struct buffer_head *blk_bh)
  6025. {
  6026. int ret, indexed = 0;
  6027. struct buffer_head *new_blk_bh = NULL;
  6028. struct ocfs2_xattr_block *xb =
  6029. (struct ocfs2_xattr_block *)blk_bh->b_data;
  6030. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
  6031. indexed = 1;
  6032. ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
  6033. &new_blk_bh, indexed);
  6034. if (ret) {
  6035. mlog_errno(ret);
  6036. goto out;
  6037. }
  6038. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
  6039. ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
  6040. else
  6041. ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
  6042. if (ret)
  6043. mlog_errno(ret);
  6044. out:
  6045. brelse(new_blk_bh);
  6046. return ret;
  6047. }
  6048. static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
  6049. {
  6050. int type = ocfs2_xattr_get_type(xe);
  6051. return type != OCFS2_XATTR_INDEX_SECURITY &&
  6052. type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
  6053. type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  6054. }
  6055. int ocfs2_reflink_xattrs(struct inode *old_inode,
  6056. struct buffer_head *old_bh,
  6057. struct inode *new_inode,
  6058. struct buffer_head *new_bh,
  6059. bool preserve_security)
  6060. {
  6061. int ret;
  6062. struct ocfs2_xattr_reflink args;
  6063. struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
  6064. struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
  6065. struct buffer_head *blk_bh = NULL;
  6066. struct ocfs2_cached_dealloc_ctxt dealloc;
  6067. struct ocfs2_refcount_tree *ref_tree;
  6068. struct buffer_head *ref_root_bh = NULL;
  6069. ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
  6070. le64_to_cpu(di->i_refcount_loc),
  6071. 1, &ref_tree, &ref_root_bh);
  6072. if (ret) {
  6073. mlog_errno(ret);
  6074. goto out;
  6075. }
  6076. ocfs2_init_dealloc_ctxt(&dealloc);
  6077. args.old_inode = old_inode;
  6078. args.new_inode = new_inode;
  6079. args.old_bh = old_bh;
  6080. args.new_bh = new_bh;
  6081. args.ref_ci = &ref_tree->rf_ci;
  6082. args.ref_root_bh = ref_root_bh;
  6083. args.dealloc = &dealloc;
  6084. if (preserve_security)
  6085. args.xattr_reflinked = NULL;
  6086. else
  6087. args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
  6088. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  6089. ret = ocfs2_reflink_xattr_inline(&args);
  6090. if (ret) {
  6091. mlog_errno(ret);
  6092. goto out_unlock;
  6093. }
  6094. }
  6095. if (!di->i_xattr_loc)
  6096. goto out_unlock;
  6097. ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
  6098. &blk_bh);
  6099. if (ret < 0) {
  6100. mlog_errno(ret);
  6101. goto out_unlock;
  6102. }
  6103. ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
  6104. if (ret)
  6105. mlog_errno(ret);
  6106. brelse(blk_bh);
  6107. out_unlock:
  6108. ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
  6109. ref_tree, 1);
  6110. brelse(ref_root_bh);
  6111. if (ocfs2_dealloc_has_cluster(&dealloc)) {
  6112. ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
  6113. ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
  6114. }
  6115. out:
  6116. return ret;
  6117. }
  6118. /*
  6119. * Initialize security and acl for a already created inode.
  6120. * Used for reflink a non-preserve-security file.
  6121. *
  6122. * It uses common api like ocfs2_xattr_set, so the caller
  6123. * must not hold any lock expect i_mutex.
  6124. */
  6125. int ocfs2_init_security_and_acl(struct inode *dir,
  6126. struct inode *inode)
  6127. {
  6128. int ret = 0;
  6129. struct buffer_head *dir_bh = NULL;
  6130. struct ocfs2_security_xattr_info si = {
  6131. .enable = 1,
  6132. };
  6133. ret = ocfs2_init_security_get(inode, dir, &si);
  6134. if (!ret) {
  6135. ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
  6136. si.name, si.value, si.value_len,
  6137. XATTR_CREATE);
  6138. if (ret) {
  6139. mlog_errno(ret);
  6140. goto leave;
  6141. }
  6142. } else if (ret != -EOPNOTSUPP) {
  6143. mlog_errno(ret);
  6144. goto leave;
  6145. }
  6146. ret = ocfs2_inode_lock(dir, &dir_bh, 0);
  6147. if (ret) {
  6148. mlog_errno(ret);
  6149. goto leave;
  6150. }
  6151. ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
  6152. if (ret)
  6153. mlog_errno(ret);
  6154. ocfs2_inode_unlock(dir, 0);
  6155. brelse(dir_bh);
  6156. leave:
  6157. return ret;
  6158. }
  6159. /*
  6160. * 'security' attributes support
  6161. */
  6162. static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
  6163. size_t list_size, const char *name,
  6164. size_t name_len, int type)
  6165. {
  6166. const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
  6167. const size_t total_len = prefix_len + name_len + 1;
  6168. if (list && total_len <= list_size) {
  6169. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  6170. memcpy(list + prefix_len, name, name_len);
  6171. list[prefix_len + name_len] = '\0';
  6172. }
  6173. return total_len;
  6174. }
  6175. static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
  6176. void *buffer, size_t size, int type)
  6177. {
  6178. if (strcmp(name, "") == 0)
  6179. return -EINVAL;
  6180. return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
  6181. name, buffer, size);
  6182. }
  6183. static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
  6184. const void *value, size_t size, int flags, int type)
  6185. {
  6186. if (strcmp(name, "") == 0)
  6187. return -EINVAL;
  6188. return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
  6189. name, value, size, flags);
  6190. }
  6191. int ocfs2_init_security_get(struct inode *inode,
  6192. struct inode *dir,
  6193. struct ocfs2_security_xattr_info *si)
  6194. {
  6195. /* check whether ocfs2 support feature xattr */
  6196. if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
  6197. return -EOPNOTSUPP;
  6198. return security_inode_init_security(inode, dir, &si->name, &si->value,
  6199. &si->value_len);
  6200. }
  6201. int ocfs2_init_security_set(handle_t *handle,
  6202. struct inode *inode,
  6203. struct buffer_head *di_bh,
  6204. struct ocfs2_security_xattr_info *si,
  6205. struct ocfs2_alloc_context *xattr_ac,
  6206. struct ocfs2_alloc_context *data_ac)
  6207. {
  6208. return ocfs2_xattr_set_handle(handle, inode, di_bh,
  6209. OCFS2_XATTR_INDEX_SECURITY,
  6210. si->name, si->value, si->value_len, 0,
  6211. xattr_ac, data_ac);
  6212. }
  6213. struct xattr_handler ocfs2_xattr_security_handler = {
  6214. .prefix = XATTR_SECURITY_PREFIX,
  6215. .list = ocfs2_xattr_security_list,
  6216. .get = ocfs2_xattr_security_get,
  6217. .set = ocfs2_xattr_security_set,
  6218. };
  6219. /*
  6220. * 'trusted' attributes support
  6221. */
  6222. static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
  6223. size_t list_size, const char *name,
  6224. size_t name_len, int type)
  6225. {
  6226. const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  6227. const size_t total_len = prefix_len + name_len + 1;
  6228. if (list && total_len <= list_size) {
  6229. memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
  6230. memcpy(list + prefix_len, name, name_len);
  6231. list[prefix_len + name_len] = '\0';
  6232. }
  6233. return total_len;
  6234. }
  6235. static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
  6236. void *buffer, size_t size, int type)
  6237. {
  6238. if (strcmp(name, "") == 0)
  6239. return -EINVAL;
  6240. return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
  6241. name, buffer, size);
  6242. }
  6243. static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
  6244. const void *value, size_t size, int flags, int type)
  6245. {
  6246. if (strcmp(name, "") == 0)
  6247. return -EINVAL;
  6248. return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
  6249. name, value, size, flags);
  6250. }
  6251. struct xattr_handler ocfs2_xattr_trusted_handler = {
  6252. .prefix = XATTR_TRUSTED_PREFIX,
  6253. .list = ocfs2_xattr_trusted_list,
  6254. .get = ocfs2_xattr_trusted_get,
  6255. .set = ocfs2_xattr_trusted_set,
  6256. };
  6257. /*
  6258. * 'user' attributes support
  6259. */
  6260. static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
  6261. size_t list_size, const char *name,
  6262. size_t name_len, int type)
  6263. {
  6264. const size_t prefix_len = XATTR_USER_PREFIX_LEN;
  6265. const size_t total_len = prefix_len + name_len + 1;
  6266. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  6267. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  6268. return 0;
  6269. if (list && total_len <= list_size) {
  6270. memcpy(list, XATTR_USER_PREFIX, prefix_len);
  6271. memcpy(list + prefix_len, name, name_len);
  6272. list[prefix_len + name_len] = '\0';
  6273. }
  6274. return total_len;
  6275. }
  6276. static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
  6277. void *buffer, size_t size, int type)
  6278. {
  6279. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  6280. if (strcmp(name, "") == 0)
  6281. return -EINVAL;
  6282. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  6283. return -EOPNOTSUPP;
  6284. return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
  6285. buffer, size);
  6286. }
  6287. static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
  6288. const void *value, size_t size, int flags, int type)
  6289. {
  6290. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  6291. if (strcmp(name, "") == 0)
  6292. return -EINVAL;
  6293. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  6294. return -EOPNOTSUPP;
  6295. return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
  6296. name, value, size, flags);
  6297. }
  6298. struct xattr_handler ocfs2_xattr_user_handler = {
  6299. .prefix = XATTR_USER_PREFIX,
  6300. .list = ocfs2_xattr_user_list,
  6301. .get = ocfs2_xattr_user_get,
  6302. .set = ocfs2_xattr_user_set,
  6303. };