xattr.c 194 KB

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