xattr.c 192 KB

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