xattr.c 195 KB

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