xattr.c 197 KB

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