xattr.c 194 KB

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