xattr.c 194 KB

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