xattr.c 193 KB

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