xattr.c 192 KB

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