xattr.c 186 KB

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