xattr.c 193 KB

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