xattr.c 192 KB

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