xattr.c 156 KB

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