xattr.c 193 KB

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