xattr.c 200 KB

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