xattr.c 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478
  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. struct ocfs2_xattr_def_value_root {
  56. struct ocfs2_xattr_value_root xv;
  57. struct ocfs2_extent_rec er;
  58. };
  59. struct ocfs2_xattr_bucket {
  60. /* The inode these xattrs are associated with */
  61. struct inode *bu_inode;
  62. /* The actual buffers that make up the bucket */
  63. struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  64. /* How many blocks make up one bucket for this filesystem */
  65. int bu_blocks;
  66. };
  67. struct ocfs2_xattr_set_ctxt {
  68. handle_t *handle;
  69. struct ocfs2_alloc_context *meta_ac;
  70. struct ocfs2_alloc_context *data_ac;
  71. struct ocfs2_cached_dealloc_ctxt dealloc;
  72. };
  73. #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
  74. #define OCFS2_XATTR_INLINE_SIZE 80
  75. #define OCFS2_XATTR_HEADER_GAP 4
  76. #define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
  77. - sizeof(struct ocfs2_xattr_header) \
  78. - OCFS2_XATTR_HEADER_GAP)
  79. #define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
  80. - sizeof(struct ocfs2_xattr_block) \
  81. - sizeof(struct ocfs2_xattr_header) \
  82. - OCFS2_XATTR_HEADER_GAP)
  83. static struct ocfs2_xattr_def_value_root def_xv = {
  84. .xv.xr_list.l_count = cpu_to_le16(1),
  85. };
  86. struct xattr_handler *ocfs2_xattr_handlers[] = {
  87. &ocfs2_xattr_user_handler,
  88. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  89. &ocfs2_xattr_acl_access_handler,
  90. &ocfs2_xattr_acl_default_handler,
  91. #endif
  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. #ifdef CONFIG_OCFS2_FS_POSIX_ACL
  99. [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
  100. = &ocfs2_xattr_acl_access_handler,
  101. [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
  102. = &ocfs2_xattr_acl_default_handler,
  103. #endif
  104. [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
  105. [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
  106. };
  107. struct ocfs2_xattr_info {
  108. int name_index;
  109. const char *name;
  110. const void *value;
  111. size_t value_len;
  112. };
  113. struct ocfs2_xattr_search {
  114. struct buffer_head *inode_bh;
  115. /*
  116. * xattr_bh point to the block buffer head which has extended attribute
  117. * when extended attribute in inode, xattr_bh is equal to inode_bh.
  118. */
  119. struct buffer_head *xattr_bh;
  120. struct ocfs2_xattr_header *header;
  121. struct ocfs2_xattr_bucket *bucket;
  122. void *base;
  123. void *end;
  124. struct ocfs2_xattr_entry *here;
  125. int not_found;
  126. };
  127. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  128. struct ocfs2_xattr_header *xh,
  129. int index,
  130. int *block_off,
  131. int *new_offset);
  132. static int ocfs2_xattr_block_find(struct inode *inode,
  133. int name_index,
  134. const char *name,
  135. struct ocfs2_xattr_search *xs);
  136. static int ocfs2_xattr_index_block_find(struct inode *inode,
  137. struct buffer_head *root_bh,
  138. int name_index,
  139. const char *name,
  140. struct ocfs2_xattr_search *xs);
  141. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  142. struct ocfs2_xattr_tree_root *xt,
  143. char *buffer,
  144. size_t buffer_size);
  145. static int ocfs2_xattr_create_index_block(struct inode *inode,
  146. struct ocfs2_xattr_search *xs,
  147. struct ocfs2_xattr_set_ctxt *ctxt);
  148. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  149. struct ocfs2_xattr_info *xi,
  150. struct ocfs2_xattr_search *xs,
  151. struct ocfs2_xattr_set_ctxt *ctxt);
  152. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  153. struct buffer_head *xb_bh);
  154. static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
  155. u64 src_blk, u64 last_blk, u64 to_blk,
  156. unsigned int start_bucket,
  157. u32 *first_hash);
  158. static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
  159. {
  160. return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
  161. }
  162. static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
  163. {
  164. return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
  165. }
  166. static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
  167. {
  168. u16 len = sb->s_blocksize -
  169. offsetof(struct ocfs2_xattr_header, xh_entries);
  170. return len / sizeof(struct ocfs2_xattr_entry);
  171. }
  172. #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
  173. #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
  174. #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
  175. static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
  176. {
  177. struct ocfs2_xattr_bucket *bucket;
  178. int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  179. BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
  180. bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
  181. if (bucket) {
  182. bucket->bu_inode = inode;
  183. bucket->bu_blocks = blks;
  184. }
  185. return bucket;
  186. }
  187. static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
  188. {
  189. int i;
  190. for (i = 0; i < bucket->bu_blocks; i++) {
  191. brelse(bucket->bu_bhs[i]);
  192. bucket->bu_bhs[i] = NULL;
  193. }
  194. }
  195. static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
  196. {
  197. if (bucket) {
  198. ocfs2_xattr_bucket_relse(bucket);
  199. bucket->bu_inode = NULL;
  200. kfree(bucket);
  201. }
  202. }
  203. /*
  204. * A bucket that has never been written to disk doesn't need to be
  205. * read. We just need the buffer_heads. Don't call this for
  206. * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
  207. * them fully.
  208. */
  209. static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
  210. u64 xb_blkno)
  211. {
  212. int i, rc = 0;
  213. for (i = 0; i < bucket->bu_blocks; i++) {
  214. bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
  215. xb_blkno + i);
  216. if (!bucket->bu_bhs[i]) {
  217. rc = -EIO;
  218. mlog_errno(rc);
  219. break;
  220. }
  221. if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
  222. bucket->bu_bhs[i]))
  223. ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
  224. bucket->bu_bhs[i]);
  225. }
  226. if (rc)
  227. ocfs2_xattr_bucket_relse(bucket);
  228. return rc;
  229. }
  230. /* Read the xattr bucket at xb_blkno */
  231. static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
  232. u64 xb_blkno)
  233. {
  234. int rc;
  235. rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
  236. bucket->bu_blocks, bucket->bu_bhs, 0,
  237. NULL);
  238. if (!rc) {
  239. spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  240. rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
  241. bucket->bu_bhs,
  242. bucket->bu_blocks,
  243. &bucket_xh(bucket)->xh_check);
  244. spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  245. if (rc)
  246. mlog_errno(rc);
  247. }
  248. if (rc)
  249. ocfs2_xattr_bucket_relse(bucket);
  250. return rc;
  251. }
  252. static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
  253. struct ocfs2_xattr_bucket *bucket,
  254. int type)
  255. {
  256. int i, rc = 0;
  257. for (i = 0; i < bucket->bu_blocks; i++) {
  258. rc = ocfs2_journal_access(handle,
  259. INODE_CACHE(bucket->bu_inode),
  260. bucket->bu_bhs[i], type);
  261. if (rc) {
  262. mlog_errno(rc);
  263. break;
  264. }
  265. }
  266. return rc;
  267. }
  268. static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
  269. struct ocfs2_xattr_bucket *bucket)
  270. {
  271. int i;
  272. spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  273. ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
  274. bucket->bu_bhs, bucket->bu_blocks,
  275. &bucket_xh(bucket)->xh_check);
  276. spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
  277. for (i = 0; i < bucket->bu_blocks; i++)
  278. ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
  279. }
  280. static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
  281. struct ocfs2_xattr_bucket *src)
  282. {
  283. int i;
  284. int blocksize = src->bu_inode->i_sb->s_blocksize;
  285. BUG_ON(dest->bu_blocks != src->bu_blocks);
  286. BUG_ON(dest->bu_inode != src->bu_inode);
  287. for (i = 0; i < src->bu_blocks; i++) {
  288. memcpy(bucket_block(dest, i), bucket_block(src, i),
  289. blocksize);
  290. }
  291. }
  292. static int ocfs2_validate_xattr_block(struct super_block *sb,
  293. struct buffer_head *bh)
  294. {
  295. int rc;
  296. struct ocfs2_xattr_block *xb =
  297. (struct ocfs2_xattr_block *)bh->b_data;
  298. mlog(0, "Validating xattr block %llu\n",
  299. (unsigned long long)bh->b_blocknr);
  300. BUG_ON(!buffer_uptodate(bh));
  301. /*
  302. * If the ecc fails, we return the error but otherwise
  303. * leave the filesystem running. We know any error is
  304. * local to this block.
  305. */
  306. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
  307. if (rc)
  308. return rc;
  309. /*
  310. * Errors after here are fatal
  311. */
  312. if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
  313. ocfs2_error(sb,
  314. "Extended attribute block #%llu has bad "
  315. "signature %.*s",
  316. (unsigned long long)bh->b_blocknr, 7,
  317. xb->xb_signature);
  318. return -EINVAL;
  319. }
  320. if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
  321. ocfs2_error(sb,
  322. "Extended attribute block #%llu has an "
  323. "invalid xb_blkno of %llu",
  324. (unsigned long long)bh->b_blocknr,
  325. (unsigned long long)le64_to_cpu(xb->xb_blkno));
  326. return -EINVAL;
  327. }
  328. if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
  329. ocfs2_error(sb,
  330. "Extended attribute block #%llu has an invalid "
  331. "xb_fs_generation of #%u",
  332. (unsigned long long)bh->b_blocknr,
  333. le32_to_cpu(xb->xb_fs_generation));
  334. return -EINVAL;
  335. }
  336. return 0;
  337. }
  338. static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
  339. struct buffer_head **bh)
  340. {
  341. int rc;
  342. struct buffer_head *tmp = *bh;
  343. rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
  344. ocfs2_validate_xattr_block);
  345. /* If ocfs2_read_block() got us a new bh, pass it up. */
  346. if (!rc && !*bh)
  347. *bh = tmp;
  348. return rc;
  349. }
  350. static inline const char *ocfs2_xattr_prefix(int name_index)
  351. {
  352. struct xattr_handler *handler = NULL;
  353. if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
  354. handler = ocfs2_xattr_handler_map[name_index];
  355. return handler ? handler->prefix : NULL;
  356. }
  357. static u32 ocfs2_xattr_name_hash(struct inode *inode,
  358. const char *name,
  359. int name_len)
  360. {
  361. /* Get hash value of uuid from super block */
  362. u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
  363. int i;
  364. /* hash extended attribute name */
  365. for (i = 0; i < name_len; i++) {
  366. hash = (hash << OCFS2_HASH_SHIFT) ^
  367. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  368. *name++;
  369. }
  370. return hash;
  371. }
  372. /*
  373. * ocfs2_xattr_hash_entry()
  374. *
  375. * Compute the hash of an extended attribute.
  376. */
  377. static void ocfs2_xattr_hash_entry(struct inode *inode,
  378. struct ocfs2_xattr_header *header,
  379. struct ocfs2_xattr_entry *entry)
  380. {
  381. u32 hash = 0;
  382. char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
  383. hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
  384. entry->xe_name_hash = cpu_to_le32(hash);
  385. return;
  386. }
  387. static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
  388. {
  389. int size = 0;
  390. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  391. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
  392. else
  393. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  394. size += sizeof(struct ocfs2_xattr_entry);
  395. return size;
  396. }
  397. int ocfs2_calc_security_init(struct inode *dir,
  398. struct ocfs2_security_xattr_info *si,
  399. int *want_clusters,
  400. int *xattr_credits,
  401. struct ocfs2_alloc_context **xattr_ac)
  402. {
  403. int ret = 0;
  404. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  405. int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
  406. si->value_len);
  407. /*
  408. * The max space of security xattr taken inline is
  409. * 256(name) + 80(value) + 16(entry) = 352 bytes,
  410. * So reserve one metadata block for it is ok.
  411. */
  412. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
  413. s_size > OCFS2_XATTR_FREE_IN_IBODY) {
  414. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
  415. if (ret) {
  416. mlog_errno(ret);
  417. return ret;
  418. }
  419. *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  420. }
  421. /* reserve clusters for xattr value which will be set in B tree*/
  422. if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
  423. int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
  424. si->value_len);
  425. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  426. new_clusters);
  427. *want_clusters += new_clusters;
  428. }
  429. return ret;
  430. }
  431. int ocfs2_calc_xattr_init(struct inode *dir,
  432. struct buffer_head *dir_bh,
  433. int mode,
  434. struct ocfs2_security_xattr_info *si,
  435. int *want_clusters,
  436. int *xattr_credits,
  437. int *want_meta)
  438. {
  439. int ret = 0;
  440. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  441. int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
  442. if (si->enable)
  443. s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
  444. si->value_len);
  445. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  446. acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
  447. OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
  448. "", NULL, 0);
  449. if (acl_len > 0) {
  450. a_size = ocfs2_xattr_entry_real_size(0, acl_len);
  451. if (S_ISDIR(mode))
  452. a_size <<= 1;
  453. } else if (acl_len != 0 && acl_len != -ENODATA) {
  454. mlog_errno(ret);
  455. return ret;
  456. }
  457. }
  458. if (!(s_size + a_size))
  459. return ret;
  460. /*
  461. * The max space of security xattr taken inline is
  462. * 256(name) + 80(value) + 16(entry) = 352 bytes,
  463. * The max space of acl xattr taken inline is
  464. * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
  465. * when blocksize = 512, may reserve one more cluser for
  466. * xattr bucket, otherwise reserve one metadata block
  467. * for them is ok.
  468. * If this is a new directory with inline data,
  469. * we choose to reserve the entire inline area for
  470. * directory contents and force an external xattr block.
  471. */
  472. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
  473. (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
  474. (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
  475. *want_meta = *want_meta + 1;
  476. *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  477. }
  478. if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
  479. (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
  480. *want_clusters += 1;
  481. *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
  482. }
  483. /*
  484. * reserve credits and clusters for xattrs which has large value
  485. * and have to be set outside
  486. */
  487. if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
  488. new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
  489. si->value_len);
  490. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  491. new_clusters);
  492. *want_clusters += new_clusters;
  493. }
  494. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
  495. acl_len > OCFS2_XATTR_INLINE_SIZE) {
  496. /* for directory, it has DEFAULT and ACCESS two types of acls */
  497. new_clusters = (S_ISDIR(mode) ? 2 : 1) *
  498. ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
  499. *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
  500. new_clusters);
  501. *want_clusters += new_clusters;
  502. }
  503. return ret;
  504. }
  505. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  506. u32 clusters_to_add,
  507. struct ocfs2_xattr_value_buf *vb,
  508. struct ocfs2_xattr_set_ctxt *ctxt)
  509. {
  510. int status = 0;
  511. handle_t *handle = ctxt->handle;
  512. enum ocfs2_alloc_restarted why;
  513. u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
  514. struct ocfs2_extent_tree et;
  515. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  516. ocfs2_init_xattr_value_extent_tree(&et, inode, vb);
  517. status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  518. OCFS2_JOURNAL_ACCESS_WRITE);
  519. if (status < 0) {
  520. mlog_errno(status);
  521. goto leave;
  522. }
  523. prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
  524. status = ocfs2_add_clusters_in_btree(handle,
  525. &et,
  526. &logical_start,
  527. clusters_to_add,
  528. 0,
  529. ctxt->data_ac,
  530. ctxt->meta_ac,
  531. &why);
  532. if (status < 0) {
  533. mlog_errno(status);
  534. goto leave;
  535. }
  536. status = ocfs2_journal_dirty(handle, vb->vb_bh);
  537. if (status < 0) {
  538. mlog_errno(status);
  539. goto leave;
  540. }
  541. clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
  542. /*
  543. * We should have already allocated enough space before the transaction,
  544. * so no need to restart.
  545. */
  546. BUG_ON(why != RESTART_NONE || clusters_to_add);
  547. leave:
  548. return status;
  549. }
  550. static int __ocfs2_remove_xattr_range(struct inode *inode,
  551. struct ocfs2_xattr_value_buf *vb,
  552. u32 cpos, u32 phys_cpos, u32 len,
  553. struct ocfs2_xattr_set_ctxt *ctxt)
  554. {
  555. int ret;
  556. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  557. handle_t *handle = ctxt->handle;
  558. struct ocfs2_extent_tree et;
  559. ocfs2_init_xattr_value_extent_tree(&et, inode, vb);
  560. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  561. OCFS2_JOURNAL_ACCESS_WRITE);
  562. if (ret) {
  563. mlog_errno(ret);
  564. goto out;
  565. }
  566. ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
  567. &ctxt->dealloc);
  568. if (ret) {
  569. mlog_errno(ret);
  570. goto out;
  571. }
  572. le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
  573. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  574. if (ret) {
  575. mlog_errno(ret);
  576. goto out;
  577. }
  578. ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
  579. if (ret)
  580. mlog_errno(ret);
  581. out:
  582. return ret;
  583. }
  584. static int ocfs2_xattr_shrink_size(struct inode *inode,
  585. u32 old_clusters,
  586. u32 new_clusters,
  587. struct ocfs2_xattr_value_buf *vb,
  588. struct ocfs2_xattr_set_ctxt *ctxt)
  589. {
  590. int ret = 0;
  591. u32 trunc_len, cpos, phys_cpos, alloc_size;
  592. u64 block;
  593. if (old_clusters <= new_clusters)
  594. return 0;
  595. cpos = new_clusters;
  596. trunc_len = old_clusters - new_clusters;
  597. while (trunc_len) {
  598. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  599. &alloc_size,
  600. &vb->vb_xv->xr_list);
  601. if (ret) {
  602. mlog_errno(ret);
  603. goto out;
  604. }
  605. if (alloc_size > trunc_len)
  606. alloc_size = trunc_len;
  607. ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
  608. phys_cpos, alloc_size,
  609. ctxt);
  610. if (ret) {
  611. mlog_errno(ret);
  612. goto out;
  613. }
  614. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  615. ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
  616. block, alloc_size);
  617. cpos += alloc_size;
  618. trunc_len -= alloc_size;
  619. }
  620. out:
  621. return ret;
  622. }
  623. static int ocfs2_xattr_value_truncate(struct inode *inode,
  624. struct ocfs2_xattr_value_buf *vb,
  625. int len,
  626. struct ocfs2_xattr_set_ctxt *ctxt)
  627. {
  628. int ret;
  629. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  630. u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
  631. if (new_clusters == old_clusters)
  632. return 0;
  633. if (new_clusters > old_clusters)
  634. ret = ocfs2_xattr_extend_allocation(inode,
  635. new_clusters - old_clusters,
  636. vb, ctxt);
  637. else
  638. ret = ocfs2_xattr_shrink_size(inode,
  639. old_clusters, new_clusters,
  640. vb, ctxt);
  641. return ret;
  642. }
  643. static int ocfs2_xattr_list_entry(char *buffer, size_t size,
  644. size_t *result, const char *prefix,
  645. const char *name, int name_len)
  646. {
  647. char *p = buffer + *result;
  648. int prefix_len = strlen(prefix);
  649. int total_len = prefix_len + name_len + 1;
  650. *result += total_len;
  651. /* we are just looking for how big our buffer needs to be */
  652. if (!size)
  653. return 0;
  654. if (*result > size)
  655. return -ERANGE;
  656. memcpy(p, prefix, prefix_len);
  657. memcpy(p + prefix_len, name, name_len);
  658. p[prefix_len + name_len] = '\0';
  659. return 0;
  660. }
  661. static int ocfs2_xattr_list_entries(struct inode *inode,
  662. struct ocfs2_xattr_header *header,
  663. char *buffer, size_t buffer_size)
  664. {
  665. size_t result = 0;
  666. int i, type, ret;
  667. const char *prefix, *name;
  668. for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
  669. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  670. type = ocfs2_xattr_get_type(entry);
  671. prefix = ocfs2_xattr_prefix(type);
  672. if (prefix) {
  673. name = (const char *)header +
  674. le16_to_cpu(entry->xe_name_offset);
  675. ret = ocfs2_xattr_list_entry(buffer, buffer_size,
  676. &result, prefix, name,
  677. entry->xe_name_len);
  678. if (ret)
  679. return ret;
  680. }
  681. }
  682. return result;
  683. }
  684. static int ocfs2_xattr_ibody_list(struct inode *inode,
  685. struct ocfs2_dinode *di,
  686. char *buffer,
  687. size_t buffer_size)
  688. {
  689. struct ocfs2_xattr_header *header = NULL;
  690. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  691. int ret = 0;
  692. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  693. return ret;
  694. header = (struct ocfs2_xattr_header *)
  695. ((void *)di + inode->i_sb->s_blocksize -
  696. le16_to_cpu(di->i_xattr_inline_size));
  697. ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
  698. return ret;
  699. }
  700. static int ocfs2_xattr_block_list(struct inode *inode,
  701. struct ocfs2_dinode *di,
  702. char *buffer,
  703. size_t buffer_size)
  704. {
  705. struct buffer_head *blk_bh = NULL;
  706. struct ocfs2_xattr_block *xb;
  707. int ret = 0;
  708. if (!di->i_xattr_loc)
  709. return ret;
  710. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  711. &blk_bh);
  712. if (ret < 0) {
  713. mlog_errno(ret);
  714. return ret;
  715. }
  716. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  717. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  718. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  719. ret = ocfs2_xattr_list_entries(inode, header,
  720. buffer, buffer_size);
  721. } else {
  722. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  723. ret = ocfs2_xattr_tree_list_index_block(inode, xt,
  724. buffer, buffer_size);
  725. }
  726. brelse(blk_bh);
  727. return ret;
  728. }
  729. ssize_t ocfs2_listxattr(struct dentry *dentry,
  730. char *buffer,
  731. size_t size)
  732. {
  733. int ret = 0, i_ret = 0, b_ret = 0;
  734. struct buffer_head *di_bh = NULL;
  735. struct ocfs2_dinode *di = NULL;
  736. struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
  737. if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
  738. return -EOPNOTSUPP;
  739. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  740. return ret;
  741. ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
  742. if (ret < 0) {
  743. mlog_errno(ret);
  744. return ret;
  745. }
  746. di = (struct ocfs2_dinode *)di_bh->b_data;
  747. down_read(&oi->ip_xattr_sem);
  748. i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
  749. if (i_ret < 0)
  750. b_ret = 0;
  751. else {
  752. if (buffer) {
  753. buffer += i_ret;
  754. size -= i_ret;
  755. }
  756. b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
  757. buffer, size);
  758. if (b_ret < 0)
  759. i_ret = 0;
  760. }
  761. up_read(&oi->ip_xattr_sem);
  762. ocfs2_inode_unlock(dentry->d_inode, 0);
  763. brelse(di_bh);
  764. return i_ret + b_ret;
  765. }
  766. static int ocfs2_xattr_find_entry(int name_index,
  767. const char *name,
  768. struct ocfs2_xattr_search *xs)
  769. {
  770. struct ocfs2_xattr_entry *entry;
  771. size_t name_len;
  772. int i, cmp = 1;
  773. if (name == NULL)
  774. return -EINVAL;
  775. name_len = strlen(name);
  776. entry = xs->here;
  777. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  778. cmp = name_index - ocfs2_xattr_get_type(entry);
  779. if (!cmp)
  780. cmp = name_len - entry->xe_name_len;
  781. if (!cmp)
  782. cmp = memcmp(name, (xs->base +
  783. le16_to_cpu(entry->xe_name_offset)),
  784. name_len);
  785. if (cmp == 0)
  786. break;
  787. entry += 1;
  788. }
  789. xs->here = entry;
  790. return cmp ? -ENODATA : 0;
  791. }
  792. static int ocfs2_xattr_get_value_outside(struct inode *inode,
  793. struct ocfs2_xattr_value_root *xv,
  794. void *buffer,
  795. size_t len)
  796. {
  797. u32 cpos, p_cluster, num_clusters, bpc, clusters;
  798. u64 blkno;
  799. int i, ret = 0;
  800. size_t cplen, blocksize;
  801. struct buffer_head *bh = NULL;
  802. struct ocfs2_extent_list *el;
  803. el = &xv->xr_list;
  804. clusters = le32_to_cpu(xv->xr_clusters);
  805. bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  806. blocksize = inode->i_sb->s_blocksize;
  807. cpos = 0;
  808. while (cpos < clusters) {
  809. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  810. &num_clusters, el);
  811. if (ret) {
  812. mlog_errno(ret);
  813. goto out;
  814. }
  815. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  816. /* Copy ocfs2_xattr_value */
  817. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  818. ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
  819. &bh, NULL);
  820. if (ret) {
  821. mlog_errno(ret);
  822. goto out;
  823. }
  824. cplen = len >= blocksize ? blocksize : len;
  825. memcpy(buffer, bh->b_data, cplen);
  826. len -= cplen;
  827. buffer += cplen;
  828. brelse(bh);
  829. bh = NULL;
  830. if (len == 0)
  831. break;
  832. }
  833. cpos += num_clusters;
  834. }
  835. out:
  836. return ret;
  837. }
  838. static int ocfs2_xattr_ibody_get(struct inode *inode,
  839. int name_index,
  840. const char *name,
  841. void *buffer,
  842. size_t buffer_size,
  843. struct ocfs2_xattr_search *xs)
  844. {
  845. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  846. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  847. struct ocfs2_xattr_value_root *xv;
  848. size_t size;
  849. int ret = 0;
  850. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  851. return -ENODATA;
  852. xs->end = (void *)di + inode->i_sb->s_blocksize;
  853. xs->header = (struct ocfs2_xattr_header *)
  854. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  855. xs->base = (void *)xs->header;
  856. xs->here = xs->header->xh_entries;
  857. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  858. if (ret)
  859. return ret;
  860. size = le64_to_cpu(xs->here->xe_value_size);
  861. if (buffer) {
  862. if (size > buffer_size)
  863. return -ERANGE;
  864. if (ocfs2_xattr_is_local(xs->here)) {
  865. memcpy(buffer, (void *)xs->base +
  866. le16_to_cpu(xs->here->xe_name_offset) +
  867. OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
  868. } else {
  869. xv = (struct ocfs2_xattr_value_root *)
  870. (xs->base + le16_to_cpu(
  871. xs->here->xe_name_offset) +
  872. OCFS2_XATTR_SIZE(xs->here->xe_name_len));
  873. ret = ocfs2_xattr_get_value_outside(inode, xv,
  874. buffer, size);
  875. if (ret < 0) {
  876. mlog_errno(ret);
  877. return ret;
  878. }
  879. }
  880. }
  881. return size;
  882. }
  883. static int ocfs2_xattr_block_get(struct inode *inode,
  884. int name_index,
  885. const char *name,
  886. void *buffer,
  887. size_t buffer_size,
  888. struct ocfs2_xattr_search *xs)
  889. {
  890. struct ocfs2_xattr_block *xb;
  891. struct ocfs2_xattr_value_root *xv;
  892. size_t size;
  893. int ret = -ENODATA, name_offset, name_len, i;
  894. int uninitialized_var(block_off);
  895. xs->bucket = ocfs2_xattr_bucket_new(inode);
  896. if (!xs->bucket) {
  897. ret = -ENOMEM;
  898. mlog_errno(ret);
  899. goto cleanup;
  900. }
  901. ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
  902. if (ret) {
  903. mlog_errno(ret);
  904. goto cleanup;
  905. }
  906. if (xs->not_found) {
  907. ret = -ENODATA;
  908. goto cleanup;
  909. }
  910. xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  911. size = le64_to_cpu(xs->here->xe_value_size);
  912. if (buffer) {
  913. ret = -ERANGE;
  914. if (size > buffer_size)
  915. goto cleanup;
  916. name_offset = le16_to_cpu(xs->here->xe_name_offset);
  917. name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
  918. i = xs->here - xs->header->xh_entries;
  919. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  920. ret = ocfs2_xattr_bucket_get_name_value(inode,
  921. bucket_xh(xs->bucket),
  922. i,
  923. &block_off,
  924. &name_offset);
  925. xs->base = bucket_block(xs->bucket, block_off);
  926. }
  927. if (ocfs2_xattr_is_local(xs->here)) {
  928. memcpy(buffer, (void *)xs->base +
  929. name_offset + name_len, size);
  930. } else {
  931. xv = (struct ocfs2_xattr_value_root *)
  932. (xs->base + name_offset + name_len);
  933. ret = ocfs2_xattr_get_value_outside(inode, xv,
  934. buffer, size);
  935. if (ret < 0) {
  936. mlog_errno(ret);
  937. goto cleanup;
  938. }
  939. }
  940. }
  941. ret = size;
  942. cleanup:
  943. ocfs2_xattr_bucket_free(xs->bucket);
  944. brelse(xs->xattr_bh);
  945. xs->xattr_bh = NULL;
  946. return ret;
  947. }
  948. int ocfs2_xattr_get_nolock(struct inode *inode,
  949. struct buffer_head *di_bh,
  950. int name_index,
  951. const char *name,
  952. void *buffer,
  953. size_t buffer_size)
  954. {
  955. int ret;
  956. struct ocfs2_dinode *di = NULL;
  957. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  958. struct ocfs2_xattr_search xis = {
  959. .not_found = -ENODATA,
  960. };
  961. struct ocfs2_xattr_search xbs = {
  962. .not_found = -ENODATA,
  963. };
  964. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  965. return -EOPNOTSUPP;
  966. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  967. ret = -ENODATA;
  968. xis.inode_bh = xbs.inode_bh = di_bh;
  969. di = (struct ocfs2_dinode *)di_bh->b_data;
  970. down_read(&oi->ip_xattr_sem);
  971. ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
  972. buffer_size, &xis);
  973. if (ret == -ENODATA && di->i_xattr_loc)
  974. ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
  975. buffer_size, &xbs);
  976. up_read(&oi->ip_xattr_sem);
  977. return ret;
  978. }
  979. /* ocfs2_xattr_get()
  980. *
  981. * Copy an extended attribute into the buffer provided.
  982. * Buffer is NULL to compute the size of buffer required.
  983. */
  984. static int ocfs2_xattr_get(struct inode *inode,
  985. int name_index,
  986. const char *name,
  987. void *buffer,
  988. size_t buffer_size)
  989. {
  990. int ret;
  991. struct buffer_head *di_bh = NULL;
  992. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  993. if (ret < 0) {
  994. mlog_errno(ret);
  995. return ret;
  996. }
  997. ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  998. name, buffer, buffer_size);
  999. ocfs2_inode_unlock(inode, 0);
  1000. brelse(di_bh);
  1001. return ret;
  1002. }
  1003. static int __ocfs2_xattr_set_value_outside(struct inode *inode,
  1004. handle_t *handle,
  1005. struct ocfs2_xattr_value_root *xv,
  1006. const void *value,
  1007. int value_len)
  1008. {
  1009. int ret = 0, i, cp_len;
  1010. u16 blocksize = inode->i_sb->s_blocksize;
  1011. u32 p_cluster, num_clusters;
  1012. u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  1013. u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
  1014. u64 blkno;
  1015. struct buffer_head *bh = NULL;
  1016. BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
  1017. while (cpos < clusters) {
  1018. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  1019. &num_clusters, &xv->xr_list);
  1020. if (ret) {
  1021. mlog_errno(ret);
  1022. goto out;
  1023. }
  1024. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  1025. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  1026. ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
  1027. &bh, NULL);
  1028. if (ret) {
  1029. mlog_errno(ret);
  1030. goto out;
  1031. }
  1032. ret = ocfs2_journal_access(handle,
  1033. INODE_CACHE(inode),
  1034. bh,
  1035. OCFS2_JOURNAL_ACCESS_WRITE);
  1036. if (ret < 0) {
  1037. mlog_errno(ret);
  1038. goto out;
  1039. }
  1040. cp_len = value_len > blocksize ? blocksize : value_len;
  1041. memcpy(bh->b_data, value, cp_len);
  1042. value_len -= cp_len;
  1043. value += cp_len;
  1044. if (cp_len < blocksize)
  1045. memset(bh->b_data + cp_len, 0,
  1046. blocksize - cp_len);
  1047. ret = ocfs2_journal_dirty(handle, bh);
  1048. if (ret < 0) {
  1049. mlog_errno(ret);
  1050. goto out;
  1051. }
  1052. brelse(bh);
  1053. bh = NULL;
  1054. /*
  1055. * XXX: do we need to empty all the following
  1056. * blocks in this cluster?
  1057. */
  1058. if (!value_len)
  1059. break;
  1060. }
  1061. cpos += num_clusters;
  1062. }
  1063. out:
  1064. brelse(bh);
  1065. return ret;
  1066. }
  1067. static int ocfs2_xattr_cleanup(struct inode *inode,
  1068. handle_t *handle,
  1069. struct ocfs2_xattr_info *xi,
  1070. struct ocfs2_xattr_search *xs,
  1071. struct ocfs2_xattr_value_buf *vb,
  1072. size_t offs)
  1073. {
  1074. int ret = 0;
  1075. size_t name_len = strlen(xi->name);
  1076. void *val = xs->base + offs;
  1077. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1078. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  1079. OCFS2_JOURNAL_ACCESS_WRITE);
  1080. if (ret) {
  1081. mlog_errno(ret);
  1082. goto out;
  1083. }
  1084. /* Decrease xattr count */
  1085. le16_add_cpu(&xs->header->xh_count, -1);
  1086. /* Remove the xattr entry and tree root which has already be set*/
  1087. memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
  1088. memset(val, 0, size);
  1089. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  1090. if (ret < 0)
  1091. mlog_errno(ret);
  1092. out:
  1093. return ret;
  1094. }
  1095. static int ocfs2_xattr_update_entry(struct inode *inode,
  1096. handle_t *handle,
  1097. struct ocfs2_xattr_info *xi,
  1098. struct ocfs2_xattr_search *xs,
  1099. struct ocfs2_xattr_value_buf *vb,
  1100. size_t offs)
  1101. {
  1102. int ret;
  1103. ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
  1104. OCFS2_JOURNAL_ACCESS_WRITE);
  1105. if (ret) {
  1106. mlog_errno(ret);
  1107. goto out;
  1108. }
  1109. xs->here->xe_name_offset = cpu_to_le16(offs);
  1110. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1111. if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
  1112. ocfs2_xattr_set_local(xs->here, 1);
  1113. else
  1114. ocfs2_xattr_set_local(xs->here, 0);
  1115. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1116. ret = ocfs2_journal_dirty(handle, vb->vb_bh);
  1117. if (ret < 0)
  1118. mlog_errno(ret);
  1119. out:
  1120. return ret;
  1121. }
  1122. /*
  1123. * ocfs2_xattr_set_value_outside()
  1124. *
  1125. * Set large size value in B tree.
  1126. */
  1127. static int ocfs2_xattr_set_value_outside(struct inode *inode,
  1128. struct ocfs2_xattr_info *xi,
  1129. struct ocfs2_xattr_search *xs,
  1130. struct ocfs2_xattr_set_ctxt *ctxt,
  1131. struct ocfs2_xattr_value_buf *vb,
  1132. size_t offs)
  1133. {
  1134. size_t name_len = strlen(xi->name);
  1135. void *val = xs->base + offs;
  1136. struct ocfs2_xattr_value_root *xv = NULL;
  1137. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1138. int ret = 0;
  1139. memset(val, 0, size);
  1140. memcpy(val, xi->name, name_len);
  1141. xv = (struct ocfs2_xattr_value_root *)
  1142. (val + OCFS2_XATTR_SIZE(name_len));
  1143. xv->xr_clusters = 0;
  1144. xv->xr_last_eb_blk = 0;
  1145. xv->xr_list.l_tree_depth = 0;
  1146. xv->xr_list.l_count = cpu_to_le16(1);
  1147. xv->xr_list.l_next_free_rec = 0;
  1148. vb->vb_xv = xv;
  1149. ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
  1150. if (ret < 0) {
  1151. mlog_errno(ret);
  1152. return ret;
  1153. }
  1154. ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
  1155. if (ret < 0) {
  1156. mlog_errno(ret);
  1157. return ret;
  1158. }
  1159. ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb->vb_xv,
  1160. xi->value, xi->value_len);
  1161. if (ret < 0)
  1162. mlog_errno(ret);
  1163. return ret;
  1164. }
  1165. /*
  1166. * ocfs2_xattr_set_entry_local()
  1167. *
  1168. * Set, replace or remove extended attribute in local.
  1169. */
  1170. static void ocfs2_xattr_set_entry_local(struct inode *inode,
  1171. struct ocfs2_xattr_info *xi,
  1172. struct ocfs2_xattr_search *xs,
  1173. struct ocfs2_xattr_entry *last,
  1174. size_t min_offs)
  1175. {
  1176. size_t name_len = strlen(xi->name);
  1177. int i;
  1178. if (xi->value && xs->not_found) {
  1179. /* Insert the new xattr entry. */
  1180. le16_add_cpu(&xs->header->xh_count, 1);
  1181. ocfs2_xattr_set_type(last, xi->name_index);
  1182. ocfs2_xattr_set_local(last, 1);
  1183. last->xe_name_len = name_len;
  1184. } else {
  1185. void *first_val;
  1186. void *val;
  1187. size_t offs, size;
  1188. first_val = xs->base + min_offs;
  1189. offs = le16_to_cpu(xs->here->xe_name_offset);
  1190. val = xs->base + offs;
  1191. if (le64_to_cpu(xs->here->xe_value_size) >
  1192. OCFS2_XATTR_INLINE_SIZE)
  1193. size = OCFS2_XATTR_SIZE(name_len) +
  1194. OCFS2_XATTR_ROOT_SIZE;
  1195. else
  1196. size = OCFS2_XATTR_SIZE(name_len) +
  1197. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1198. if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
  1199. OCFS2_XATTR_SIZE(xi->value_len)) {
  1200. /* The old and the new value have the
  1201. same size. Just replace the value. */
  1202. ocfs2_xattr_set_local(xs->here, 1);
  1203. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1204. /* Clear value bytes. */
  1205. memset(val + OCFS2_XATTR_SIZE(name_len),
  1206. 0,
  1207. OCFS2_XATTR_SIZE(xi->value_len));
  1208. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1209. xi->value,
  1210. xi->value_len);
  1211. return;
  1212. }
  1213. /* Remove the old name+value. */
  1214. memmove(first_val + size, first_val, val - first_val);
  1215. memset(first_val, 0, size);
  1216. xs->here->xe_name_hash = 0;
  1217. xs->here->xe_name_offset = 0;
  1218. ocfs2_xattr_set_local(xs->here, 1);
  1219. xs->here->xe_value_size = 0;
  1220. min_offs += size;
  1221. /* Adjust all value offsets. */
  1222. last = xs->header->xh_entries;
  1223. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1224. size_t o = le16_to_cpu(last->xe_name_offset);
  1225. if (o < offs)
  1226. last->xe_name_offset = cpu_to_le16(o + size);
  1227. last += 1;
  1228. }
  1229. if (!xi->value) {
  1230. /* Remove the old entry. */
  1231. last -= 1;
  1232. memmove(xs->here, xs->here + 1,
  1233. (void *)last - (void *)xs->here);
  1234. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  1235. le16_add_cpu(&xs->header->xh_count, -1);
  1236. }
  1237. }
  1238. if (xi->value) {
  1239. /* Insert the new name+value. */
  1240. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1241. OCFS2_XATTR_SIZE(xi->value_len);
  1242. void *val = xs->base + min_offs - size;
  1243. xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
  1244. memset(val, 0, size);
  1245. memcpy(val, xi->name, name_len);
  1246. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1247. xi->value,
  1248. xi->value_len);
  1249. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1250. ocfs2_xattr_set_local(xs->here, 1);
  1251. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1252. }
  1253. return;
  1254. }
  1255. /*
  1256. * ocfs2_xattr_set_entry()
  1257. *
  1258. * Set extended attribute entry into inode or block.
  1259. *
  1260. * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
  1261. * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
  1262. * then set value in B tree with set_value_outside().
  1263. */
  1264. static int ocfs2_xattr_set_entry(struct inode *inode,
  1265. struct ocfs2_xattr_info *xi,
  1266. struct ocfs2_xattr_search *xs,
  1267. struct ocfs2_xattr_set_ctxt *ctxt,
  1268. int flag)
  1269. {
  1270. struct ocfs2_xattr_entry *last;
  1271. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1272. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1273. size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
  1274. size_t size_l = 0;
  1275. handle_t *handle = ctxt->handle;
  1276. int free, i, ret;
  1277. struct ocfs2_xattr_info xi_l = {
  1278. .name_index = xi->name_index,
  1279. .name = xi->name,
  1280. .value = xi->value,
  1281. .value_len = xi->value_len,
  1282. };
  1283. struct ocfs2_xattr_value_buf vb = {
  1284. .vb_bh = xs->xattr_bh,
  1285. .vb_access = ocfs2_journal_access_di,
  1286. };
  1287. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1288. BUG_ON(xs->xattr_bh == xs->inode_bh);
  1289. vb.vb_access = ocfs2_journal_access_xb;
  1290. } else
  1291. BUG_ON(xs->xattr_bh != xs->inode_bh);
  1292. /* Compute min_offs, last and free space. */
  1293. last = xs->header->xh_entries;
  1294. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1295. size_t offs = le16_to_cpu(last->xe_name_offset);
  1296. if (offs < min_offs)
  1297. min_offs = offs;
  1298. last += 1;
  1299. }
  1300. free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
  1301. if (free < 0)
  1302. return -EIO;
  1303. if (!xs->not_found) {
  1304. size_t size = 0;
  1305. if (ocfs2_xattr_is_local(xs->here))
  1306. size = OCFS2_XATTR_SIZE(name_len) +
  1307. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1308. else
  1309. size = OCFS2_XATTR_SIZE(name_len) +
  1310. OCFS2_XATTR_ROOT_SIZE;
  1311. free += (size + sizeof(struct ocfs2_xattr_entry));
  1312. }
  1313. /* Check free space in inode or block */
  1314. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1315. if (free < sizeof(struct ocfs2_xattr_entry) +
  1316. OCFS2_XATTR_SIZE(name_len) +
  1317. OCFS2_XATTR_ROOT_SIZE) {
  1318. ret = -ENOSPC;
  1319. goto out;
  1320. }
  1321. size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1322. xi_l.value = (void *)&def_xv;
  1323. xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
  1324. } else if (xi->value) {
  1325. if (free < sizeof(struct ocfs2_xattr_entry) +
  1326. OCFS2_XATTR_SIZE(name_len) +
  1327. OCFS2_XATTR_SIZE(xi->value_len)) {
  1328. ret = -ENOSPC;
  1329. goto out;
  1330. }
  1331. }
  1332. if (!xs->not_found) {
  1333. /* For existing extended attribute */
  1334. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1335. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1336. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1337. void *val = xs->base + offs;
  1338. if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
  1339. /* Replace existing local xattr with tree root */
  1340. ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
  1341. ctxt, &vb, offs);
  1342. if (ret < 0)
  1343. mlog_errno(ret);
  1344. goto out;
  1345. } else if (!ocfs2_xattr_is_local(xs->here)) {
  1346. /* For existing xattr which has value outside */
  1347. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  1348. (val + OCFS2_XATTR_SIZE(name_len));
  1349. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1350. /*
  1351. * If new value need set outside also,
  1352. * first truncate old value to new value,
  1353. * then set new value with set_value_outside().
  1354. */
  1355. ret = ocfs2_xattr_value_truncate(inode,
  1356. &vb,
  1357. xi->value_len,
  1358. ctxt);
  1359. if (ret < 0) {
  1360. mlog_errno(ret);
  1361. goto out;
  1362. }
  1363. ret = ocfs2_xattr_update_entry(inode,
  1364. handle,
  1365. xi,
  1366. xs,
  1367. &vb,
  1368. offs);
  1369. if (ret < 0) {
  1370. mlog_errno(ret);
  1371. goto out;
  1372. }
  1373. ret = __ocfs2_xattr_set_value_outside(inode,
  1374. handle,
  1375. vb.vb_xv,
  1376. xi->value,
  1377. xi->value_len);
  1378. if (ret < 0)
  1379. mlog_errno(ret);
  1380. goto out;
  1381. } else {
  1382. /*
  1383. * If new value need set in local,
  1384. * just trucate old value to zero.
  1385. */
  1386. ret = ocfs2_xattr_value_truncate(inode,
  1387. &vb,
  1388. 0,
  1389. ctxt);
  1390. if (ret < 0)
  1391. mlog_errno(ret);
  1392. }
  1393. }
  1394. }
  1395. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
  1396. OCFS2_JOURNAL_ACCESS_WRITE);
  1397. if (ret) {
  1398. mlog_errno(ret);
  1399. goto out;
  1400. }
  1401. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1402. ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
  1403. OCFS2_JOURNAL_ACCESS_WRITE);
  1404. if (ret) {
  1405. mlog_errno(ret);
  1406. goto out;
  1407. }
  1408. }
  1409. /*
  1410. * Set value in local, include set tree root in local.
  1411. * This is the first step for value size >INLINE_SIZE.
  1412. */
  1413. ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
  1414. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1415. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  1416. if (ret < 0) {
  1417. mlog_errno(ret);
  1418. goto out;
  1419. }
  1420. }
  1421. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
  1422. (flag & OCFS2_INLINE_XATTR_FL)) {
  1423. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1424. unsigned int xattrsize = osb->s_xattr_inline_size;
  1425. /*
  1426. * Adjust extent record count or inline data size
  1427. * to reserve space for extended attribute.
  1428. */
  1429. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1430. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1431. le16_add_cpu(&idata->id_count, -xattrsize);
  1432. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  1433. struct ocfs2_extent_list *el = &di->id2.i_list;
  1434. le16_add_cpu(&el->l_count, -(xattrsize /
  1435. sizeof(struct ocfs2_extent_rec)));
  1436. }
  1437. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  1438. }
  1439. /* Update xattr flag */
  1440. spin_lock(&oi->ip_lock);
  1441. oi->ip_dyn_features |= flag;
  1442. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1443. spin_unlock(&oi->ip_lock);
  1444. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1445. if (ret < 0)
  1446. mlog_errno(ret);
  1447. if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1448. /*
  1449. * Set value outside in B tree.
  1450. * This is the second step for value size > INLINE_SIZE.
  1451. */
  1452. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1453. ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
  1454. &vb, offs);
  1455. if (ret < 0) {
  1456. int ret2;
  1457. mlog_errno(ret);
  1458. /*
  1459. * If set value outside failed, we have to clean
  1460. * the junk tree root we have already set in local.
  1461. */
  1462. ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
  1463. xi, xs, &vb, offs);
  1464. if (ret2 < 0)
  1465. mlog_errno(ret2);
  1466. }
  1467. }
  1468. out:
  1469. return ret;
  1470. }
  1471. static int ocfs2_remove_value_outside(struct inode*inode,
  1472. struct ocfs2_xattr_value_buf *vb,
  1473. struct ocfs2_xattr_header *header)
  1474. {
  1475. int ret = 0, i;
  1476. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1477. struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
  1478. ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
  1479. ctxt.handle = ocfs2_start_trans(osb,
  1480. ocfs2_remove_extent_credits(osb->sb));
  1481. if (IS_ERR(ctxt.handle)) {
  1482. ret = PTR_ERR(ctxt.handle);
  1483. mlog_errno(ret);
  1484. goto out;
  1485. }
  1486. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  1487. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  1488. if (!ocfs2_xattr_is_local(entry)) {
  1489. void *val;
  1490. val = (void *)header +
  1491. le16_to_cpu(entry->xe_name_offset);
  1492. vb->vb_xv = (struct ocfs2_xattr_value_root *)
  1493. (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
  1494. ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
  1495. if (ret < 0) {
  1496. mlog_errno(ret);
  1497. break;
  1498. }
  1499. }
  1500. }
  1501. ocfs2_commit_trans(osb, ctxt.handle);
  1502. ocfs2_schedule_truncate_log_flush(osb, 1);
  1503. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  1504. out:
  1505. return ret;
  1506. }
  1507. static int ocfs2_xattr_ibody_remove(struct inode *inode,
  1508. struct buffer_head *di_bh)
  1509. {
  1510. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1511. struct ocfs2_xattr_header *header;
  1512. int ret;
  1513. struct ocfs2_xattr_value_buf vb = {
  1514. .vb_bh = di_bh,
  1515. .vb_access = ocfs2_journal_access_di,
  1516. };
  1517. header = (struct ocfs2_xattr_header *)
  1518. ((void *)di + inode->i_sb->s_blocksize -
  1519. le16_to_cpu(di->i_xattr_inline_size));
  1520. ret = ocfs2_remove_value_outside(inode, &vb, header);
  1521. return ret;
  1522. }
  1523. static int ocfs2_xattr_block_remove(struct inode *inode,
  1524. struct buffer_head *blk_bh)
  1525. {
  1526. struct ocfs2_xattr_block *xb;
  1527. int ret = 0;
  1528. struct ocfs2_xattr_value_buf vb = {
  1529. .vb_bh = blk_bh,
  1530. .vb_access = ocfs2_journal_access_xb,
  1531. };
  1532. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1533. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1534. struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
  1535. ret = ocfs2_remove_value_outside(inode, &vb, header);
  1536. } else
  1537. ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
  1538. return ret;
  1539. }
  1540. static int ocfs2_xattr_free_block(struct inode *inode,
  1541. u64 block)
  1542. {
  1543. struct inode *xb_alloc_inode;
  1544. struct buffer_head *xb_alloc_bh = NULL;
  1545. struct buffer_head *blk_bh = NULL;
  1546. struct ocfs2_xattr_block *xb;
  1547. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1548. handle_t *handle;
  1549. int ret = 0;
  1550. u64 blk, bg_blkno;
  1551. u16 bit;
  1552. ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
  1553. if (ret < 0) {
  1554. mlog_errno(ret);
  1555. goto out;
  1556. }
  1557. ret = ocfs2_xattr_block_remove(inode, blk_bh);
  1558. if (ret < 0) {
  1559. mlog_errno(ret);
  1560. goto out;
  1561. }
  1562. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1563. blk = le64_to_cpu(xb->xb_blkno);
  1564. bit = le16_to_cpu(xb->xb_suballoc_bit);
  1565. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1566. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  1567. EXTENT_ALLOC_SYSTEM_INODE,
  1568. le16_to_cpu(xb->xb_suballoc_slot));
  1569. if (!xb_alloc_inode) {
  1570. ret = -ENOMEM;
  1571. mlog_errno(ret);
  1572. goto out;
  1573. }
  1574. mutex_lock(&xb_alloc_inode->i_mutex);
  1575. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  1576. if (ret < 0) {
  1577. mlog_errno(ret);
  1578. goto out_mutex;
  1579. }
  1580. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  1581. if (IS_ERR(handle)) {
  1582. ret = PTR_ERR(handle);
  1583. mlog_errno(ret);
  1584. goto out_unlock;
  1585. }
  1586. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  1587. bit, bg_blkno, 1);
  1588. if (ret < 0)
  1589. mlog_errno(ret);
  1590. ocfs2_commit_trans(osb, handle);
  1591. out_unlock:
  1592. ocfs2_inode_unlock(xb_alloc_inode, 1);
  1593. brelse(xb_alloc_bh);
  1594. out_mutex:
  1595. mutex_unlock(&xb_alloc_inode->i_mutex);
  1596. iput(xb_alloc_inode);
  1597. out:
  1598. brelse(blk_bh);
  1599. return ret;
  1600. }
  1601. /*
  1602. * ocfs2_xattr_remove()
  1603. *
  1604. * Free extended attribute resources associated with this inode.
  1605. */
  1606. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  1607. {
  1608. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1609. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1610. handle_t *handle;
  1611. int ret;
  1612. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1613. return 0;
  1614. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1615. return 0;
  1616. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1617. ret = ocfs2_xattr_ibody_remove(inode, di_bh);
  1618. if (ret < 0) {
  1619. mlog_errno(ret);
  1620. goto out;
  1621. }
  1622. }
  1623. if (di->i_xattr_loc) {
  1624. ret = ocfs2_xattr_free_block(inode,
  1625. le64_to_cpu(di->i_xattr_loc));
  1626. if (ret < 0) {
  1627. mlog_errno(ret);
  1628. goto out;
  1629. }
  1630. }
  1631. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1632. OCFS2_INODE_UPDATE_CREDITS);
  1633. if (IS_ERR(handle)) {
  1634. ret = PTR_ERR(handle);
  1635. mlog_errno(ret);
  1636. goto out;
  1637. }
  1638. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  1639. OCFS2_JOURNAL_ACCESS_WRITE);
  1640. if (ret) {
  1641. mlog_errno(ret);
  1642. goto out_commit;
  1643. }
  1644. di->i_xattr_loc = 0;
  1645. spin_lock(&oi->ip_lock);
  1646. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  1647. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1648. spin_unlock(&oi->ip_lock);
  1649. ret = ocfs2_journal_dirty(handle, di_bh);
  1650. if (ret < 0)
  1651. mlog_errno(ret);
  1652. out_commit:
  1653. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1654. out:
  1655. return ret;
  1656. }
  1657. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  1658. struct ocfs2_dinode *di)
  1659. {
  1660. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1661. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  1662. int free;
  1663. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  1664. return 0;
  1665. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1666. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1667. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  1668. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  1669. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  1670. le64_to_cpu(di->i_size);
  1671. } else {
  1672. struct ocfs2_extent_list *el = &di->id2.i_list;
  1673. free = (le16_to_cpu(el->l_count) -
  1674. le16_to_cpu(el->l_next_free_rec)) *
  1675. sizeof(struct ocfs2_extent_rec);
  1676. }
  1677. if (free >= xattrsize)
  1678. return 1;
  1679. return 0;
  1680. }
  1681. /*
  1682. * ocfs2_xattr_ibody_find()
  1683. *
  1684. * Find extended attribute in inode block and
  1685. * fill search info into struct ocfs2_xattr_search.
  1686. */
  1687. static int ocfs2_xattr_ibody_find(struct inode *inode,
  1688. int name_index,
  1689. const char *name,
  1690. struct ocfs2_xattr_search *xs)
  1691. {
  1692. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1693. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1694. int ret;
  1695. int has_space = 0;
  1696. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1697. return 0;
  1698. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1699. down_read(&oi->ip_alloc_sem);
  1700. has_space = ocfs2_xattr_has_space_inline(inode, di);
  1701. up_read(&oi->ip_alloc_sem);
  1702. if (!has_space)
  1703. return 0;
  1704. }
  1705. xs->xattr_bh = xs->inode_bh;
  1706. xs->end = (void *)di + inode->i_sb->s_blocksize;
  1707. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  1708. xs->header = (struct ocfs2_xattr_header *)
  1709. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  1710. else
  1711. xs->header = (struct ocfs2_xattr_header *)
  1712. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  1713. xs->base = (void *)xs->header;
  1714. xs->here = xs->header->xh_entries;
  1715. /* Find the named attribute. */
  1716. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1717. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1718. if (ret && ret != -ENODATA)
  1719. return ret;
  1720. xs->not_found = ret;
  1721. }
  1722. return 0;
  1723. }
  1724. /*
  1725. * ocfs2_xattr_ibody_set()
  1726. *
  1727. * Set, replace or remove an extended attribute into inode block.
  1728. *
  1729. */
  1730. static int ocfs2_xattr_ibody_set(struct inode *inode,
  1731. struct ocfs2_xattr_info *xi,
  1732. struct ocfs2_xattr_search *xs,
  1733. struct ocfs2_xattr_set_ctxt *ctxt)
  1734. {
  1735. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1736. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1737. int ret;
  1738. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1739. return -ENOSPC;
  1740. down_write(&oi->ip_alloc_sem);
  1741. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1742. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  1743. ret = -ENOSPC;
  1744. goto out;
  1745. }
  1746. }
  1747. ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
  1748. (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
  1749. out:
  1750. up_write(&oi->ip_alloc_sem);
  1751. return ret;
  1752. }
  1753. /*
  1754. * ocfs2_xattr_block_find()
  1755. *
  1756. * Find extended attribute in external block and
  1757. * fill search info into struct ocfs2_xattr_search.
  1758. */
  1759. static int ocfs2_xattr_block_find(struct inode *inode,
  1760. int name_index,
  1761. const char *name,
  1762. struct ocfs2_xattr_search *xs)
  1763. {
  1764. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1765. struct buffer_head *blk_bh = NULL;
  1766. struct ocfs2_xattr_block *xb;
  1767. int ret = 0;
  1768. if (!di->i_xattr_loc)
  1769. return ret;
  1770. ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
  1771. &blk_bh);
  1772. if (ret < 0) {
  1773. mlog_errno(ret);
  1774. return ret;
  1775. }
  1776. xs->xattr_bh = blk_bh;
  1777. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1778. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1779. xs->header = &xb->xb_attrs.xb_header;
  1780. xs->base = (void *)xs->header;
  1781. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  1782. xs->here = xs->header->xh_entries;
  1783. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1784. } else
  1785. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  1786. name_index,
  1787. name, xs);
  1788. if (ret && ret != -ENODATA) {
  1789. xs->xattr_bh = NULL;
  1790. goto cleanup;
  1791. }
  1792. xs->not_found = ret;
  1793. return 0;
  1794. cleanup:
  1795. brelse(blk_bh);
  1796. return ret;
  1797. }
  1798. /*
  1799. * ocfs2_xattr_block_set()
  1800. *
  1801. * Set, replace or remove an extended attribute into external block.
  1802. *
  1803. */
  1804. static int ocfs2_xattr_block_set(struct inode *inode,
  1805. struct ocfs2_xattr_info *xi,
  1806. struct ocfs2_xattr_search *xs,
  1807. struct ocfs2_xattr_set_ctxt *ctxt)
  1808. {
  1809. struct buffer_head *new_bh = NULL;
  1810. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1811. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1812. handle_t *handle = ctxt->handle;
  1813. struct ocfs2_xattr_block *xblk = NULL;
  1814. u16 suballoc_bit_start;
  1815. u32 num_got;
  1816. u64 first_blkno;
  1817. int ret;
  1818. if (!xs->xattr_bh) {
  1819. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  1820. xs->inode_bh,
  1821. OCFS2_JOURNAL_ACCESS_CREATE);
  1822. if (ret < 0) {
  1823. mlog_errno(ret);
  1824. goto end;
  1825. }
  1826. ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
  1827. &suballoc_bit_start, &num_got,
  1828. &first_blkno);
  1829. if (ret < 0) {
  1830. mlog_errno(ret);
  1831. goto end;
  1832. }
  1833. new_bh = sb_getblk(inode->i_sb, first_blkno);
  1834. ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
  1835. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
  1836. new_bh,
  1837. OCFS2_JOURNAL_ACCESS_CREATE);
  1838. if (ret < 0) {
  1839. mlog_errno(ret);
  1840. goto end;
  1841. }
  1842. /* Initialize ocfs2_xattr_block */
  1843. xs->xattr_bh = new_bh;
  1844. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  1845. memset(xblk, 0, inode->i_sb->s_blocksize);
  1846. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  1847. xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
  1848. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1849. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  1850. xblk->xb_blkno = cpu_to_le64(first_blkno);
  1851. xs->header = &xblk->xb_attrs.xb_header;
  1852. xs->base = (void *)xs->header;
  1853. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  1854. xs->here = xs->header->xh_entries;
  1855. ret = ocfs2_journal_dirty(handle, new_bh);
  1856. if (ret < 0) {
  1857. mlog_errno(ret);
  1858. goto end;
  1859. }
  1860. di->i_xattr_loc = cpu_to_le64(first_blkno);
  1861. ocfs2_journal_dirty(handle, xs->inode_bh);
  1862. } else
  1863. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1864. if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1865. /* Set extended attribute into external block */
  1866. ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
  1867. OCFS2_HAS_XATTR_FL);
  1868. if (!ret || ret != -ENOSPC)
  1869. goto end;
  1870. ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
  1871. if (ret)
  1872. goto end;
  1873. }
  1874. ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
  1875. end:
  1876. return ret;
  1877. }
  1878. /* Check whether the new xattr can be inserted into the inode. */
  1879. static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
  1880. struct ocfs2_xattr_info *xi,
  1881. struct ocfs2_xattr_search *xs)
  1882. {
  1883. u64 value_size;
  1884. struct ocfs2_xattr_entry *last;
  1885. int free, i;
  1886. size_t min_offs = xs->end - xs->base;
  1887. if (!xs->header)
  1888. return 0;
  1889. last = xs->header->xh_entries;
  1890. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  1891. size_t offs = le16_to_cpu(last->xe_name_offset);
  1892. if (offs < min_offs)
  1893. min_offs = offs;
  1894. last += 1;
  1895. }
  1896. free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
  1897. if (free < 0)
  1898. return 0;
  1899. BUG_ON(!xs->not_found);
  1900. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  1901. value_size = OCFS2_XATTR_ROOT_SIZE;
  1902. else
  1903. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  1904. if (free >= sizeof(struct ocfs2_xattr_entry) +
  1905. OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
  1906. return 1;
  1907. return 0;
  1908. }
  1909. static int ocfs2_calc_xattr_set_need(struct inode *inode,
  1910. struct ocfs2_dinode *di,
  1911. struct ocfs2_xattr_info *xi,
  1912. struct ocfs2_xattr_search *xis,
  1913. struct ocfs2_xattr_search *xbs,
  1914. int *clusters_need,
  1915. int *meta_need,
  1916. int *credits_need)
  1917. {
  1918. int ret = 0, old_in_xb = 0;
  1919. int clusters_add = 0, meta_add = 0, credits = 0;
  1920. struct buffer_head *bh = NULL;
  1921. struct ocfs2_xattr_block *xb = NULL;
  1922. struct ocfs2_xattr_entry *xe = NULL;
  1923. struct ocfs2_xattr_value_root *xv = NULL;
  1924. char *base = NULL;
  1925. int name_offset, name_len = 0;
  1926. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  1927. xi->value_len);
  1928. u64 value_size;
  1929. /*
  1930. * Calculate the clusters we need to write.
  1931. * No matter whether we replace an old one or add a new one,
  1932. * we need this for writing.
  1933. */
  1934. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  1935. credits += new_clusters *
  1936. ocfs2_clusters_to_blocks(inode->i_sb, 1);
  1937. if (xis->not_found && xbs->not_found) {
  1938. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1939. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1940. clusters_add += new_clusters;
  1941. credits += ocfs2_calc_extend_credits(inode->i_sb,
  1942. &def_xv.xv.xr_list,
  1943. new_clusters);
  1944. }
  1945. goto meta_guess;
  1946. }
  1947. if (!xis->not_found) {
  1948. xe = xis->here;
  1949. name_offset = le16_to_cpu(xe->xe_name_offset);
  1950. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  1951. base = xis->base;
  1952. credits += OCFS2_INODE_UPDATE_CREDITS;
  1953. } else {
  1954. int i, block_off = 0;
  1955. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  1956. xe = xbs->here;
  1957. name_offset = le16_to_cpu(xe->xe_name_offset);
  1958. name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  1959. i = xbs->here - xbs->header->xh_entries;
  1960. old_in_xb = 1;
  1961. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  1962. ret = ocfs2_xattr_bucket_get_name_value(inode,
  1963. bucket_xh(xbs->bucket),
  1964. i, &block_off,
  1965. &name_offset);
  1966. base = bucket_block(xbs->bucket, block_off);
  1967. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1968. } else {
  1969. base = xbs->base;
  1970. credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
  1971. }
  1972. }
  1973. /*
  1974. * delete a xattr doesn't need metadata and cluster allocation.
  1975. * so just calculate the credits and return.
  1976. *
  1977. * The credits for removing the value tree will be extended
  1978. * by ocfs2_remove_extent itself.
  1979. */
  1980. if (!xi->value) {
  1981. if (!ocfs2_xattr_is_local(xe))
  1982. credits += ocfs2_remove_extent_credits(inode->i_sb);
  1983. goto out;
  1984. }
  1985. /* do cluster allocation guess first. */
  1986. value_size = le64_to_cpu(xe->xe_value_size);
  1987. if (old_in_xb) {
  1988. /*
  1989. * In xattr set, we always try to set the xe in inode first,
  1990. * so if it can be inserted into inode successfully, the old
  1991. * one will be removed from the xattr block, and this xattr
  1992. * will be inserted into inode as a new xattr in inode.
  1993. */
  1994. if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
  1995. clusters_add += new_clusters;
  1996. credits += ocfs2_remove_extent_credits(inode->i_sb) +
  1997. OCFS2_INODE_UPDATE_CREDITS;
  1998. if (!ocfs2_xattr_is_local(xe))
  1999. credits += ocfs2_calc_extend_credits(
  2000. inode->i_sb,
  2001. &def_xv.xv.xr_list,
  2002. new_clusters);
  2003. goto out;
  2004. }
  2005. }
  2006. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  2007. /* the new values will be stored outside. */
  2008. u32 old_clusters = 0;
  2009. if (!ocfs2_xattr_is_local(xe)) {
  2010. old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
  2011. value_size);
  2012. xv = (struct ocfs2_xattr_value_root *)
  2013. (base + name_offset + name_len);
  2014. value_size = OCFS2_XATTR_ROOT_SIZE;
  2015. } else
  2016. xv = &def_xv.xv;
  2017. if (old_clusters >= new_clusters) {
  2018. credits += ocfs2_remove_extent_credits(inode->i_sb);
  2019. goto out;
  2020. } else {
  2021. meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
  2022. clusters_add += new_clusters - old_clusters;
  2023. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2024. &xv->xr_list,
  2025. new_clusters -
  2026. old_clusters);
  2027. if (value_size >= OCFS2_XATTR_ROOT_SIZE)
  2028. goto out;
  2029. }
  2030. } else {
  2031. /*
  2032. * Now the new value will be stored inside. So if the new
  2033. * value is smaller than the size of value root or the old
  2034. * value, we don't need any allocation, otherwise we have
  2035. * to guess metadata allocation.
  2036. */
  2037. if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
  2038. (!ocfs2_xattr_is_local(xe) &&
  2039. OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
  2040. goto out;
  2041. }
  2042. meta_guess:
  2043. /* calculate metadata allocation. */
  2044. if (di->i_xattr_loc) {
  2045. if (!xbs->xattr_bh) {
  2046. ret = ocfs2_read_xattr_block(inode,
  2047. le64_to_cpu(di->i_xattr_loc),
  2048. &bh);
  2049. if (ret) {
  2050. mlog_errno(ret);
  2051. goto out;
  2052. }
  2053. xb = (struct ocfs2_xattr_block *)bh->b_data;
  2054. } else
  2055. xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
  2056. /*
  2057. * If there is already an xattr tree, good, we can calculate
  2058. * like other b-trees. Otherwise we may have the chance of
  2059. * create a tree, the credit calculation is borrowed from
  2060. * ocfs2_calc_extend_credits with root_el = NULL. And the
  2061. * new tree will be cluster based, so no meta is needed.
  2062. */
  2063. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  2064. struct ocfs2_extent_list *el =
  2065. &xb->xb_attrs.xb_root.xt_list;
  2066. meta_add += ocfs2_extend_meta_needed(el);
  2067. credits += ocfs2_calc_extend_credits(inode->i_sb,
  2068. el, 1);
  2069. } else
  2070. credits += OCFS2_SUBALLOC_ALLOC + 1;
  2071. /*
  2072. * This cluster will be used either for new bucket or for
  2073. * new xattr block.
  2074. * If the cluster size is the same as the bucket size, one
  2075. * more is needed since we may need to extend the bucket
  2076. * also.
  2077. */
  2078. clusters_add += 1;
  2079. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2080. if (OCFS2_XATTR_BUCKET_SIZE ==
  2081. OCFS2_SB(inode->i_sb)->s_clustersize) {
  2082. credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2083. clusters_add += 1;
  2084. }
  2085. } else {
  2086. meta_add += 1;
  2087. credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
  2088. }
  2089. out:
  2090. if (clusters_need)
  2091. *clusters_need = clusters_add;
  2092. if (meta_need)
  2093. *meta_need = meta_add;
  2094. if (credits_need)
  2095. *credits_need = credits;
  2096. brelse(bh);
  2097. return ret;
  2098. }
  2099. static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
  2100. struct ocfs2_dinode *di,
  2101. struct ocfs2_xattr_info *xi,
  2102. struct ocfs2_xattr_search *xis,
  2103. struct ocfs2_xattr_search *xbs,
  2104. struct ocfs2_xattr_set_ctxt *ctxt,
  2105. int *credits)
  2106. {
  2107. int clusters_add, meta_add, ret;
  2108. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2109. memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
  2110. ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
  2111. ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
  2112. &clusters_add, &meta_add, credits);
  2113. if (ret) {
  2114. mlog_errno(ret);
  2115. return ret;
  2116. }
  2117. mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
  2118. "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
  2119. if (meta_add) {
  2120. ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
  2121. &ctxt->meta_ac);
  2122. if (ret) {
  2123. mlog_errno(ret);
  2124. goto out;
  2125. }
  2126. }
  2127. if (clusters_add) {
  2128. ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
  2129. if (ret)
  2130. mlog_errno(ret);
  2131. }
  2132. out:
  2133. if (ret) {
  2134. if (ctxt->meta_ac) {
  2135. ocfs2_free_alloc_context(ctxt->meta_ac);
  2136. ctxt->meta_ac = NULL;
  2137. }
  2138. /*
  2139. * We cannot have an error and a non null ctxt->data_ac.
  2140. */
  2141. }
  2142. return ret;
  2143. }
  2144. static int __ocfs2_xattr_set_handle(struct inode *inode,
  2145. struct ocfs2_dinode *di,
  2146. struct ocfs2_xattr_info *xi,
  2147. struct ocfs2_xattr_search *xis,
  2148. struct ocfs2_xattr_search *xbs,
  2149. struct ocfs2_xattr_set_ctxt *ctxt)
  2150. {
  2151. int ret = 0, credits, old_found;
  2152. if (!xi->value) {
  2153. /* Remove existing extended attribute */
  2154. if (!xis->not_found)
  2155. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2156. else if (!xbs->not_found)
  2157. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2158. } else {
  2159. /* We always try to set extended attribute into inode first*/
  2160. ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
  2161. if (!ret && !xbs->not_found) {
  2162. /*
  2163. * If succeed and that extended attribute existing in
  2164. * external block, then we will remove it.
  2165. */
  2166. xi->value = NULL;
  2167. xi->value_len = 0;
  2168. old_found = xis->not_found;
  2169. xis->not_found = -ENODATA;
  2170. ret = ocfs2_calc_xattr_set_need(inode,
  2171. di,
  2172. xi,
  2173. xis,
  2174. xbs,
  2175. NULL,
  2176. NULL,
  2177. &credits);
  2178. xis->not_found = old_found;
  2179. if (ret) {
  2180. mlog_errno(ret);
  2181. goto out;
  2182. }
  2183. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2184. ctxt->handle->h_buffer_credits);
  2185. if (ret) {
  2186. mlog_errno(ret);
  2187. goto out;
  2188. }
  2189. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2190. } else if (ret == -ENOSPC) {
  2191. if (di->i_xattr_loc && !xbs->xattr_bh) {
  2192. ret = ocfs2_xattr_block_find(inode,
  2193. xi->name_index,
  2194. xi->name, xbs);
  2195. if (ret)
  2196. goto out;
  2197. old_found = xis->not_found;
  2198. xis->not_found = -ENODATA;
  2199. ret = ocfs2_calc_xattr_set_need(inode,
  2200. di,
  2201. xi,
  2202. xis,
  2203. xbs,
  2204. NULL,
  2205. NULL,
  2206. &credits);
  2207. xis->not_found = old_found;
  2208. if (ret) {
  2209. mlog_errno(ret);
  2210. goto out;
  2211. }
  2212. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2213. ctxt->handle->h_buffer_credits);
  2214. if (ret) {
  2215. mlog_errno(ret);
  2216. goto out;
  2217. }
  2218. }
  2219. /*
  2220. * If no space in inode, we will set extended attribute
  2221. * into external block.
  2222. */
  2223. ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
  2224. if (ret)
  2225. goto out;
  2226. if (!xis->not_found) {
  2227. /*
  2228. * If succeed and that extended attribute
  2229. * existing in inode, we will remove it.
  2230. */
  2231. xi->value = NULL;
  2232. xi->value_len = 0;
  2233. xbs->not_found = -ENODATA;
  2234. ret = ocfs2_calc_xattr_set_need(inode,
  2235. di,
  2236. xi,
  2237. xis,
  2238. xbs,
  2239. NULL,
  2240. NULL,
  2241. &credits);
  2242. if (ret) {
  2243. mlog_errno(ret);
  2244. goto out;
  2245. }
  2246. ret = ocfs2_extend_trans(ctxt->handle, credits +
  2247. ctxt->handle->h_buffer_credits);
  2248. if (ret) {
  2249. mlog_errno(ret);
  2250. goto out;
  2251. }
  2252. ret = ocfs2_xattr_ibody_set(inode, xi,
  2253. xis, ctxt);
  2254. }
  2255. }
  2256. }
  2257. if (!ret) {
  2258. /* Update inode ctime. */
  2259. ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
  2260. xis->inode_bh,
  2261. OCFS2_JOURNAL_ACCESS_WRITE);
  2262. if (ret) {
  2263. mlog_errno(ret);
  2264. goto out;
  2265. }
  2266. inode->i_ctime = CURRENT_TIME;
  2267. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  2268. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  2269. ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
  2270. }
  2271. out:
  2272. return ret;
  2273. }
  2274. /*
  2275. * This function only called duing creating inode
  2276. * for init security/acl xattrs of the new inode.
  2277. * All transanction credits have been reserved in mknod.
  2278. */
  2279. int ocfs2_xattr_set_handle(handle_t *handle,
  2280. struct inode *inode,
  2281. struct buffer_head *di_bh,
  2282. int name_index,
  2283. const char *name,
  2284. const void *value,
  2285. size_t value_len,
  2286. int flags,
  2287. struct ocfs2_alloc_context *meta_ac,
  2288. struct ocfs2_alloc_context *data_ac)
  2289. {
  2290. struct ocfs2_dinode *di;
  2291. int ret;
  2292. struct ocfs2_xattr_info xi = {
  2293. .name_index = name_index,
  2294. .name = name,
  2295. .value = value,
  2296. .value_len = value_len,
  2297. };
  2298. struct ocfs2_xattr_search xis = {
  2299. .not_found = -ENODATA,
  2300. };
  2301. struct ocfs2_xattr_search xbs = {
  2302. .not_found = -ENODATA,
  2303. };
  2304. struct ocfs2_xattr_set_ctxt ctxt = {
  2305. .handle = handle,
  2306. .meta_ac = meta_ac,
  2307. .data_ac = data_ac,
  2308. };
  2309. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2310. return -EOPNOTSUPP;
  2311. /*
  2312. * In extreme situation, may need xattr bucket when
  2313. * block size is too small. And we have already reserved
  2314. * the credits for bucket in mknod.
  2315. */
  2316. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
  2317. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  2318. if (!xbs.bucket) {
  2319. mlog_errno(-ENOMEM);
  2320. return -ENOMEM;
  2321. }
  2322. }
  2323. xis.inode_bh = xbs.inode_bh = di_bh;
  2324. di = (struct ocfs2_dinode *)di_bh->b_data;
  2325. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  2326. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  2327. if (ret)
  2328. goto cleanup;
  2329. if (xis.not_found) {
  2330. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  2331. if (ret)
  2332. goto cleanup;
  2333. }
  2334. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  2335. cleanup:
  2336. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  2337. brelse(xbs.xattr_bh);
  2338. ocfs2_xattr_bucket_free(xbs.bucket);
  2339. return ret;
  2340. }
  2341. /*
  2342. * ocfs2_xattr_set()
  2343. *
  2344. * Set, replace or remove an extended attribute for this inode.
  2345. * value is NULL to remove an existing extended attribute, else either
  2346. * create or replace an extended attribute.
  2347. */
  2348. int ocfs2_xattr_set(struct inode *inode,
  2349. int name_index,
  2350. const char *name,
  2351. const void *value,
  2352. size_t value_len,
  2353. int flags)
  2354. {
  2355. struct buffer_head *di_bh = NULL;
  2356. struct ocfs2_dinode *di;
  2357. int ret, credits;
  2358. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2359. struct inode *tl_inode = osb->osb_tl_inode;
  2360. struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
  2361. struct ocfs2_xattr_info xi = {
  2362. .name_index = name_index,
  2363. .name = name,
  2364. .value = value,
  2365. .value_len = value_len,
  2366. };
  2367. struct ocfs2_xattr_search xis = {
  2368. .not_found = -ENODATA,
  2369. };
  2370. struct ocfs2_xattr_search xbs = {
  2371. .not_found = -ENODATA,
  2372. };
  2373. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  2374. return -EOPNOTSUPP;
  2375. /*
  2376. * Only xbs will be used on indexed trees. xis doesn't need a
  2377. * bucket.
  2378. */
  2379. xbs.bucket = ocfs2_xattr_bucket_new(inode);
  2380. if (!xbs.bucket) {
  2381. mlog_errno(-ENOMEM);
  2382. return -ENOMEM;
  2383. }
  2384. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  2385. if (ret < 0) {
  2386. mlog_errno(ret);
  2387. goto cleanup_nolock;
  2388. }
  2389. xis.inode_bh = xbs.inode_bh = di_bh;
  2390. di = (struct ocfs2_dinode *)di_bh->b_data;
  2391. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  2392. /*
  2393. * Scan inode and external block to find the same name
  2394. * extended attribute and collect search infomation.
  2395. */
  2396. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  2397. if (ret)
  2398. goto cleanup;
  2399. if (xis.not_found) {
  2400. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  2401. if (ret)
  2402. goto cleanup;
  2403. }
  2404. if (xis.not_found && xbs.not_found) {
  2405. ret = -ENODATA;
  2406. if (flags & XATTR_REPLACE)
  2407. goto cleanup;
  2408. ret = 0;
  2409. if (!value)
  2410. goto cleanup;
  2411. } else {
  2412. ret = -EEXIST;
  2413. if (flags & XATTR_CREATE)
  2414. goto cleanup;
  2415. }
  2416. mutex_lock(&tl_inode->i_mutex);
  2417. if (ocfs2_truncate_log_needs_flush(osb)) {
  2418. ret = __ocfs2_flush_truncate_log(osb);
  2419. if (ret < 0) {
  2420. mutex_unlock(&tl_inode->i_mutex);
  2421. mlog_errno(ret);
  2422. goto cleanup;
  2423. }
  2424. }
  2425. mutex_unlock(&tl_inode->i_mutex);
  2426. ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
  2427. &xbs, &ctxt, &credits);
  2428. if (ret) {
  2429. mlog_errno(ret);
  2430. goto cleanup;
  2431. }
  2432. /* we need to update inode's ctime field, so add credit for it. */
  2433. credits += OCFS2_INODE_UPDATE_CREDITS;
  2434. ctxt.handle = ocfs2_start_trans(osb, credits);
  2435. if (IS_ERR(ctxt.handle)) {
  2436. ret = PTR_ERR(ctxt.handle);
  2437. mlog_errno(ret);
  2438. goto cleanup;
  2439. }
  2440. ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
  2441. ocfs2_commit_trans(osb, ctxt.handle);
  2442. if (ctxt.data_ac)
  2443. ocfs2_free_alloc_context(ctxt.data_ac);
  2444. if (ctxt.meta_ac)
  2445. ocfs2_free_alloc_context(ctxt.meta_ac);
  2446. if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
  2447. ocfs2_schedule_truncate_log_flush(osb, 1);
  2448. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  2449. cleanup:
  2450. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  2451. ocfs2_inode_unlock(inode, 1);
  2452. cleanup_nolock:
  2453. brelse(di_bh);
  2454. brelse(xbs.xattr_bh);
  2455. ocfs2_xattr_bucket_free(xbs.bucket);
  2456. return ret;
  2457. }
  2458. /*
  2459. * Find the xattr extent rec which may contains name_hash.
  2460. * e_cpos will be the first name hash of the xattr rec.
  2461. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  2462. */
  2463. static int ocfs2_xattr_get_rec(struct inode *inode,
  2464. u32 name_hash,
  2465. u64 *p_blkno,
  2466. u32 *e_cpos,
  2467. u32 *num_clusters,
  2468. struct ocfs2_extent_list *el)
  2469. {
  2470. int ret = 0, i;
  2471. struct buffer_head *eb_bh = NULL;
  2472. struct ocfs2_extent_block *eb;
  2473. struct ocfs2_extent_rec *rec = NULL;
  2474. u64 e_blkno = 0;
  2475. if (el->l_tree_depth) {
  2476. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
  2477. &eb_bh);
  2478. if (ret) {
  2479. mlog_errno(ret);
  2480. goto out;
  2481. }
  2482. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  2483. el = &eb->h_list;
  2484. if (el->l_tree_depth) {
  2485. ocfs2_error(inode->i_sb,
  2486. "Inode %lu has non zero tree depth in "
  2487. "xattr tree block %llu\n", inode->i_ino,
  2488. (unsigned long long)eb_bh->b_blocknr);
  2489. ret = -EROFS;
  2490. goto out;
  2491. }
  2492. }
  2493. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  2494. rec = &el->l_recs[i];
  2495. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  2496. e_blkno = le64_to_cpu(rec->e_blkno);
  2497. break;
  2498. }
  2499. }
  2500. if (!e_blkno) {
  2501. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  2502. "record (%u, %u, 0) in xattr", inode->i_ino,
  2503. le32_to_cpu(rec->e_cpos),
  2504. ocfs2_rec_clusters(el, rec));
  2505. ret = -EROFS;
  2506. goto out;
  2507. }
  2508. *p_blkno = le64_to_cpu(rec->e_blkno);
  2509. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  2510. if (e_cpos)
  2511. *e_cpos = le32_to_cpu(rec->e_cpos);
  2512. out:
  2513. brelse(eb_bh);
  2514. return ret;
  2515. }
  2516. typedef int (xattr_bucket_func)(struct inode *inode,
  2517. struct ocfs2_xattr_bucket *bucket,
  2518. void *para);
  2519. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  2520. struct ocfs2_xattr_bucket *bucket,
  2521. int name_index,
  2522. const char *name,
  2523. u32 name_hash,
  2524. u16 *xe_index,
  2525. int *found)
  2526. {
  2527. int i, ret = 0, cmp = 1, block_off, new_offset;
  2528. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  2529. size_t name_len = strlen(name);
  2530. struct ocfs2_xattr_entry *xe = NULL;
  2531. char *xe_name;
  2532. /*
  2533. * We don't use binary search in the bucket because there
  2534. * may be multiple entries with the same name hash.
  2535. */
  2536. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  2537. xe = &xh->xh_entries[i];
  2538. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  2539. continue;
  2540. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  2541. break;
  2542. cmp = name_index - ocfs2_xattr_get_type(xe);
  2543. if (!cmp)
  2544. cmp = name_len - xe->xe_name_len;
  2545. if (cmp)
  2546. continue;
  2547. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2548. xh,
  2549. i,
  2550. &block_off,
  2551. &new_offset);
  2552. if (ret) {
  2553. mlog_errno(ret);
  2554. break;
  2555. }
  2556. xe_name = bucket_block(bucket, block_off) + new_offset;
  2557. if (!memcmp(name, xe_name, name_len)) {
  2558. *xe_index = i;
  2559. *found = 1;
  2560. ret = 0;
  2561. break;
  2562. }
  2563. }
  2564. return ret;
  2565. }
  2566. /*
  2567. * Find the specified xattr entry in a series of buckets.
  2568. * This series start from p_blkno and last for num_clusters.
  2569. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  2570. * the num of the valid buckets.
  2571. *
  2572. * Return the buffer_head this xattr should reside in. And if the xattr's
  2573. * hash is in the gap of 2 buckets, return the lower bucket.
  2574. */
  2575. static int ocfs2_xattr_bucket_find(struct inode *inode,
  2576. int name_index,
  2577. const char *name,
  2578. u32 name_hash,
  2579. u64 p_blkno,
  2580. u32 first_hash,
  2581. u32 num_clusters,
  2582. struct ocfs2_xattr_search *xs)
  2583. {
  2584. int ret, found = 0;
  2585. struct ocfs2_xattr_header *xh = NULL;
  2586. struct ocfs2_xattr_entry *xe = NULL;
  2587. u16 index = 0;
  2588. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2589. int low_bucket = 0, bucket, high_bucket;
  2590. struct ocfs2_xattr_bucket *search;
  2591. u32 last_hash;
  2592. u64 blkno, lower_blkno = 0;
  2593. search = ocfs2_xattr_bucket_new(inode);
  2594. if (!search) {
  2595. ret = -ENOMEM;
  2596. mlog_errno(ret);
  2597. goto out;
  2598. }
  2599. ret = ocfs2_read_xattr_bucket(search, p_blkno);
  2600. if (ret) {
  2601. mlog_errno(ret);
  2602. goto out;
  2603. }
  2604. xh = bucket_xh(search);
  2605. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  2606. while (low_bucket <= high_bucket) {
  2607. ocfs2_xattr_bucket_relse(search);
  2608. bucket = (low_bucket + high_bucket) / 2;
  2609. blkno = p_blkno + bucket * blk_per_bucket;
  2610. ret = ocfs2_read_xattr_bucket(search, blkno);
  2611. if (ret) {
  2612. mlog_errno(ret);
  2613. goto out;
  2614. }
  2615. xh = bucket_xh(search);
  2616. xe = &xh->xh_entries[0];
  2617. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  2618. high_bucket = bucket - 1;
  2619. continue;
  2620. }
  2621. /*
  2622. * Check whether the hash of the last entry in our
  2623. * bucket is larger than the search one. for an empty
  2624. * bucket, the last one is also the first one.
  2625. */
  2626. if (xh->xh_count)
  2627. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  2628. last_hash = le32_to_cpu(xe->xe_name_hash);
  2629. /* record lower_blkno which may be the insert place. */
  2630. lower_blkno = blkno;
  2631. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  2632. low_bucket = bucket + 1;
  2633. continue;
  2634. }
  2635. /* the searched xattr should reside in this bucket if exists. */
  2636. ret = ocfs2_find_xe_in_bucket(inode, search,
  2637. name_index, name, name_hash,
  2638. &index, &found);
  2639. if (ret) {
  2640. mlog_errno(ret);
  2641. goto out;
  2642. }
  2643. break;
  2644. }
  2645. /*
  2646. * Record the bucket we have found.
  2647. * When the xattr's hash value is in the gap of 2 buckets, we will
  2648. * always set it to the previous bucket.
  2649. */
  2650. if (!lower_blkno)
  2651. lower_blkno = p_blkno;
  2652. /* This should be in cache - we just read it during the search */
  2653. ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
  2654. if (ret) {
  2655. mlog_errno(ret);
  2656. goto out;
  2657. }
  2658. xs->header = bucket_xh(xs->bucket);
  2659. xs->base = bucket_block(xs->bucket, 0);
  2660. xs->end = xs->base + inode->i_sb->s_blocksize;
  2661. if (found) {
  2662. xs->here = &xs->header->xh_entries[index];
  2663. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  2664. (unsigned long long)bucket_blkno(xs->bucket), index);
  2665. } else
  2666. ret = -ENODATA;
  2667. out:
  2668. ocfs2_xattr_bucket_free(search);
  2669. return ret;
  2670. }
  2671. static int ocfs2_xattr_index_block_find(struct inode *inode,
  2672. struct buffer_head *root_bh,
  2673. int name_index,
  2674. const char *name,
  2675. struct ocfs2_xattr_search *xs)
  2676. {
  2677. int ret;
  2678. struct ocfs2_xattr_block *xb =
  2679. (struct ocfs2_xattr_block *)root_bh->b_data;
  2680. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  2681. struct ocfs2_extent_list *el = &xb_root->xt_list;
  2682. u64 p_blkno = 0;
  2683. u32 first_hash, num_clusters = 0;
  2684. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  2685. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2686. return -ENODATA;
  2687. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  2688. name, name_hash, name_index);
  2689. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  2690. &num_clusters, el);
  2691. if (ret) {
  2692. mlog_errno(ret);
  2693. goto out;
  2694. }
  2695. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  2696. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  2697. "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
  2698. first_hash);
  2699. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  2700. p_blkno, first_hash, num_clusters, xs);
  2701. out:
  2702. return ret;
  2703. }
  2704. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  2705. u64 blkno,
  2706. u32 clusters,
  2707. xattr_bucket_func *func,
  2708. void *para)
  2709. {
  2710. int i, ret = 0;
  2711. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  2712. u32 num_buckets = clusters * bpc;
  2713. struct ocfs2_xattr_bucket *bucket;
  2714. bucket = ocfs2_xattr_bucket_new(inode);
  2715. if (!bucket) {
  2716. mlog_errno(-ENOMEM);
  2717. return -ENOMEM;
  2718. }
  2719. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  2720. clusters, (unsigned long long)blkno);
  2721. for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
  2722. ret = ocfs2_read_xattr_bucket(bucket, blkno);
  2723. if (ret) {
  2724. mlog_errno(ret);
  2725. break;
  2726. }
  2727. /*
  2728. * The real bucket num in this series of blocks is stored
  2729. * in the 1st bucket.
  2730. */
  2731. if (i == 0)
  2732. num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
  2733. mlog(0, "iterating xattr bucket %llu, first hash %u\n",
  2734. (unsigned long long)blkno,
  2735. le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
  2736. if (func) {
  2737. ret = func(inode, bucket, para);
  2738. if (ret && ret != -ERANGE)
  2739. mlog_errno(ret);
  2740. /* Fall through to bucket_relse() */
  2741. }
  2742. ocfs2_xattr_bucket_relse(bucket);
  2743. if (ret)
  2744. break;
  2745. }
  2746. ocfs2_xattr_bucket_free(bucket);
  2747. return ret;
  2748. }
  2749. struct ocfs2_xattr_tree_list {
  2750. char *buffer;
  2751. size_t buffer_size;
  2752. size_t result;
  2753. };
  2754. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  2755. struct ocfs2_xattr_header *xh,
  2756. int index,
  2757. int *block_off,
  2758. int *new_offset)
  2759. {
  2760. u16 name_offset;
  2761. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  2762. return -EINVAL;
  2763. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  2764. *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
  2765. *new_offset = name_offset % inode->i_sb->s_blocksize;
  2766. return 0;
  2767. }
  2768. static int ocfs2_list_xattr_bucket(struct inode *inode,
  2769. struct ocfs2_xattr_bucket *bucket,
  2770. void *para)
  2771. {
  2772. int ret = 0, type;
  2773. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  2774. int i, block_off, new_offset;
  2775. const char *prefix, *name;
  2776. for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
  2777. struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
  2778. type = ocfs2_xattr_get_type(entry);
  2779. prefix = ocfs2_xattr_prefix(type);
  2780. if (prefix) {
  2781. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2782. bucket_xh(bucket),
  2783. i,
  2784. &block_off,
  2785. &new_offset);
  2786. if (ret)
  2787. break;
  2788. name = (const char *)bucket_block(bucket, block_off) +
  2789. new_offset;
  2790. ret = ocfs2_xattr_list_entry(xl->buffer,
  2791. xl->buffer_size,
  2792. &xl->result,
  2793. prefix, name,
  2794. entry->xe_name_len);
  2795. if (ret)
  2796. break;
  2797. }
  2798. }
  2799. return ret;
  2800. }
  2801. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  2802. struct ocfs2_xattr_tree_root *xt,
  2803. char *buffer,
  2804. size_t buffer_size)
  2805. {
  2806. struct ocfs2_extent_list *el = &xt->xt_list;
  2807. int ret = 0;
  2808. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  2809. u64 p_blkno = 0;
  2810. struct ocfs2_xattr_tree_list xl = {
  2811. .buffer = buffer,
  2812. .buffer_size = buffer_size,
  2813. .result = 0,
  2814. };
  2815. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2816. return 0;
  2817. while (name_hash > 0) {
  2818. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  2819. &e_cpos, &num_clusters, el);
  2820. if (ret) {
  2821. mlog_errno(ret);
  2822. goto out;
  2823. }
  2824. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  2825. ocfs2_list_xattr_bucket,
  2826. &xl);
  2827. if (ret) {
  2828. if (ret != -ERANGE)
  2829. mlog_errno(ret);
  2830. goto out;
  2831. }
  2832. if (e_cpos == 0)
  2833. break;
  2834. name_hash = e_cpos - 1;
  2835. }
  2836. ret = xl.result;
  2837. out:
  2838. return ret;
  2839. }
  2840. static int cmp_xe(const void *a, const void *b)
  2841. {
  2842. const struct ocfs2_xattr_entry *l = a, *r = b;
  2843. u32 l_hash = le32_to_cpu(l->xe_name_hash);
  2844. u32 r_hash = le32_to_cpu(r->xe_name_hash);
  2845. if (l_hash > r_hash)
  2846. return 1;
  2847. if (l_hash < r_hash)
  2848. return -1;
  2849. return 0;
  2850. }
  2851. static void swap_xe(void *a, void *b, int size)
  2852. {
  2853. struct ocfs2_xattr_entry *l = a, *r = b, tmp;
  2854. tmp = *l;
  2855. memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
  2856. memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
  2857. }
  2858. /*
  2859. * When the ocfs2_xattr_block is filled up, new bucket will be created
  2860. * and all the xattr entries will be moved to the new bucket.
  2861. * The header goes at the start of the bucket, and the names+values are
  2862. * filled from the end. This is why *target starts as the last buffer.
  2863. * Note: we need to sort the entries since they are not saved in order
  2864. * in the ocfs2_xattr_block.
  2865. */
  2866. static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  2867. struct buffer_head *xb_bh,
  2868. struct ocfs2_xattr_bucket *bucket)
  2869. {
  2870. int i, blocksize = inode->i_sb->s_blocksize;
  2871. int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2872. u16 offset, size, off_change;
  2873. struct ocfs2_xattr_entry *xe;
  2874. struct ocfs2_xattr_block *xb =
  2875. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2876. struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
  2877. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  2878. u16 count = le16_to_cpu(xb_xh->xh_count);
  2879. char *src = xb_bh->b_data;
  2880. char *target = bucket_block(bucket, blks - 1);
  2881. mlog(0, "cp xattr from block %llu to bucket %llu\n",
  2882. (unsigned long long)xb_bh->b_blocknr,
  2883. (unsigned long long)bucket_blkno(bucket));
  2884. for (i = 0; i < blks; i++)
  2885. memset(bucket_block(bucket, i), 0, blocksize);
  2886. /*
  2887. * Since the xe_name_offset is based on ocfs2_xattr_header,
  2888. * there is a offset change corresponding to the change of
  2889. * ocfs2_xattr_header's position.
  2890. */
  2891. off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2892. xe = &xb_xh->xh_entries[count - 1];
  2893. offset = le16_to_cpu(xe->xe_name_offset) + off_change;
  2894. size = blocksize - offset;
  2895. /* copy all the names and values. */
  2896. memcpy(target + offset, src + offset, size);
  2897. /* Init new header now. */
  2898. xh->xh_count = xb_xh->xh_count;
  2899. xh->xh_num_buckets = cpu_to_le16(1);
  2900. xh->xh_name_value_len = cpu_to_le16(size);
  2901. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
  2902. /* copy all the entries. */
  2903. target = bucket_block(bucket, 0);
  2904. offset = offsetof(struct ocfs2_xattr_header, xh_entries);
  2905. size = count * sizeof(struct ocfs2_xattr_entry);
  2906. memcpy(target + offset, (char *)xb_xh + offset, size);
  2907. /* Change the xe offset for all the xe because of the move. */
  2908. off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
  2909. offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2910. for (i = 0; i < count; i++)
  2911. le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
  2912. mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
  2913. offset, size, off_change);
  2914. sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
  2915. cmp_xe, swap_xe);
  2916. }
  2917. /*
  2918. * After we move xattr from block to index btree, we have to
  2919. * update ocfs2_xattr_search to the new xe and base.
  2920. *
  2921. * When the entry is in xattr block, xattr_bh indicates the storage place.
  2922. * While if the entry is in index b-tree, "bucket" indicates the
  2923. * real place of the xattr.
  2924. */
  2925. static void ocfs2_xattr_update_xattr_search(struct inode *inode,
  2926. struct ocfs2_xattr_search *xs,
  2927. struct buffer_head *old_bh)
  2928. {
  2929. char *buf = old_bh->b_data;
  2930. struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
  2931. struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
  2932. int i;
  2933. xs->header = bucket_xh(xs->bucket);
  2934. xs->base = bucket_block(xs->bucket, 0);
  2935. xs->end = xs->base + inode->i_sb->s_blocksize;
  2936. if (xs->not_found)
  2937. return;
  2938. i = xs->here - old_xh->xh_entries;
  2939. xs->here = &xs->header->xh_entries[i];
  2940. }
  2941. static int ocfs2_xattr_create_index_block(struct inode *inode,
  2942. struct ocfs2_xattr_search *xs,
  2943. struct ocfs2_xattr_set_ctxt *ctxt)
  2944. {
  2945. int ret;
  2946. u32 bit_off, len;
  2947. u64 blkno;
  2948. handle_t *handle = ctxt->handle;
  2949. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2950. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2951. struct buffer_head *xb_bh = xs->xattr_bh;
  2952. struct ocfs2_xattr_block *xb =
  2953. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2954. struct ocfs2_xattr_tree_root *xr;
  2955. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  2956. mlog(0, "create xattr index block for %llu\n",
  2957. (unsigned long long)xb_bh->b_blocknr);
  2958. BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
  2959. BUG_ON(!xs->bucket);
  2960. /*
  2961. * XXX:
  2962. * We can use this lock for now, and maybe move to a dedicated mutex
  2963. * if performance becomes a problem later.
  2964. */
  2965. down_write(&oi->ip_alloc_sem);
  2966. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
  2967. OCFS2_JOURNAL_ACCESS_WRITE);
  2968. if (ret) {
  2969. mlog_errno(ret);
  2970. goto out;
  2971. }
  2972. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
  2973. 1, 1, &bit_off, &len);
  2974. if (ret) {
  2975. mlog_errno(ret);
  2976. goto out;
  2977. }
  2978. /*
  2979. * The bucket may spread in many blocks, and
  2980. * we will only touch the 1st block and the last block
  2981. * in the whole bucket(one for entry and one for data).
  2982. */
  2983. blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  2984. mlog(0, "allocate 1 cluster from %llu to xattr block\n",
  2985. (unsigned long long)blkno);
  2986. ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
  2987. if (ret) {
  2988. mlog_errno(ret);
  2989. goto out;
  2990. }
  2991. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  2992. OCFS2_JOURNAL_ACCESS_CREATE);
  2993. if (ret) {
  2994. mlog_errno(ret);
  2995. goto out;
  2996. }
  2997. ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
  2998. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  2999. ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
  3000. /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
  3001. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  3002. offsetof(struct ocfs2_xattr_block, xb_attrs));
  3003. xr = &xb->xb_attrs.xb_root;
  3004. xr->xt_clusters = cpu_to_le32(1);
  3005. xr->xt_last_eb_blk = 0;
  3006. xr->xt_list.l_tree_depth = 0;
  3007. xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
  3008. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  3009. xr->xt_list.l_recs[0].e_cpos = 0;
  3010. xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  3011. xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  3012. xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
  3013. ocfs2_journal_dirty(handle, xb_bh);
  3014. out:
  3015. up_write(&oi->ip_alloc_sem);
  3016. return ret;
  3017. }
  3018. static int cmp_xe_offset(const void *a, const void *b)
  3019. {
  3020. const struct ocfs2_xattr_entry *l = a, *r = b;
  3021. u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
  3022. u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
  3023. if (l_name_offset < r_name_offset)
  3024. return 1;
  3025. if (l_name_offset > r_name_offset)
  3026. return -1;
  3027. return 0;
  3028. }
  3029. /*
  3030. * defrag a xattr bucket if we find that the bucket has some
  3031. * holes beteen name/value pairs.
  3032. * We will move all the name/value pairs to the end of the bucket
  3033. * so that we can spare some space for insertion.
  3034. */
  3035. static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  3036. handle_t *handle,
  3037. struct ocfs2_xattr_bucket *bucket)
  3038. {
  3039. int ret, i;
  3040. size_t end, offset, len, value_len;
  3041. struct ocfs2_xattr_header *xh;
  3042. char *entries, *buf, *bucket_buf = NULL;
  3043. u64 blkno = bucket_blkno(bucket);
  3044. u16 xh_free_start;
  3045. size_t blocksize = inode->i_sb->s_blocksize;
  3046. struct ocfs2_xattr_entry *xe;
  3047. /*
  3048. * In order to make the operation more efficient and generic,
  3049. * we copy all the blocks into a contiguous memory and do the
  3050. * defragment there, so if anything is error, we will not touch
  3051. * the real block.
  3052. */
  3053. bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
  3054. if (!bucket_buf) {
  3055. ret = -EIO;
  3056. goto out;
  3057. }
  3058. buf = bucket_buf;
  3059. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3060. memcpy(buf, bucket_block(bucket, i), blocksize);
  3061. ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
  3062. OCFS2_JOURNAL_ACCESS_WRITE);
  3063. if (ret < 0) {
  3064. mlog_errno(ret);
  3065. goto out;
  3066. }
  3067. xh = (struct ocfs2_xattr_header *)bucket_buf;
  3068. entries = (char *)xh->xh_entries;
  3069. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3070. mlog(0, "adjust xattr bucket in %llu, count = %u, "
  3071. "xh_free_start = %u, xh_name_value_len = %u.\n",
  3072. (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
  3073. xh_free_start, le16_to_cpu(xh->xh_name_value_len));
  3074. /*
  3075. * sort all the entries by their offset.
  3076. * the largest will be the first, so that we can
  3077. * move them to the end one by one.
  3078. */
  3079. sort(entries, le16_to_cpu(xh->xh_count),
  3080. sizeof(struct ocfs2_xattr_entry),
  3081. cmp_xe_offset, swap_xe);
  3082. /* Move all name/values to the end of the bucket. */
  3083. xe = xh->xh_entries;
  3084. end = OCFS2_XATTR_BUCKET_SIZE;
  3085. for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
  3086. offset = le16_to_cpu(xe->xe_name_offset);
  3087. if (ocfs2_xattr_is_local(xe))
  3088. value_len = OCFS2_XATTR_SIZE(
  3089. le64_to_cpu(xe->xe_value_size));
  3090. else
  3091. value_len = OCFS2_XATTR_ROOT_SIZE;
  3092. len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
  3093. /*
  3094. * We must make sure that the name/value pair
  3095. * exist in the same block. So adjust end to
  3096. * the previous block end if needed.
  3097. */
  3098. if (((end - len) / blocksize !=
  3099. (end - 1) / blocksize))
  3100. end = end - end % blocksize;
  3101. if (end > offset + len) {
  3102. memmove(bucket_buf + end - len,
  3103. bucket_buf + offset, len);
  3104. xe->xe_name_offset = cpu_to_le16(end - len);
  3105. }
  3106. mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
  3107. "bucket %llu\n", (unsigned long long)blkno);
  3108. end -= len;
  3109. }
  3110. mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
  3111. "bucket %llu\n", (unsigned long long)blkno);
  3112. if (xh_free_start == end)
  3113. goto out;
  3114. memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
  3115. xh->xh_free_start = cpu_to_le16(end);
  3116. /* sort the entries by their name_hash. */
  3117. sort(entries, le16_to_cpu(xh->xh_count),
  3118. sizeof(struct ocfs2_xattr_entry),
  3119. cmp_xe, swap_xe);
  3120. buf = bucket_buf;
  3121. for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
  3122. memcpy(bucket_block(bucket, i), buf, blocksize);
  3123. ocfs2_xattr_bucket_journal_dirty(handle, bucket);
  3124. out:
  3125. kfree(bucket_buf);
  3126. return ret;
  3127. }
  3128. /*
  3129. * prev_blkno points to the start of an existing extent. new_blkno
  3130. * points to a newly allocated extent. Because we know each of our
  3131. * clusters contains more than bucket, we can easily split one cluster
  3132. * at a bucket boundary. So we take the last cluster of the existing
  3133. * extent and split it down the middle. We move the last half of the
  3134. * buckets in the last cluster of the existing extent over to the new
  3135. * extent.
  3136. *
  3137. * first_bh is the buffer at prev_blkno so we can update the existing
  3138. * extent's bucket count. header_bh is the bucket were we were hoping
  3139. * to insert our xattr. If the bucket move places the target in the new
  3140. * extent, we'll update first_bh and header_bh after modifying the old
  3141. * extent.
  3142. *
  3143. * first_hash will be set as the 1st xe's name_hash in the new extent.
  3144. */
  3145. static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
  3146. handle_t *handle,
  3147. struct ocfs2_xattr_bucket *first,
  3148. struct ocfs2_xattr_bucket *target,
  3149. u64 new_blkno,
  3150. u32 num_clusters,
  3151. u32 *first_hash)
  3152. {
  3153. int ret;
  3154. struct super_block *sb = inode->i_sb;
  3155. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
  3156. int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
  3157. int to_move = num_buckets / 2;
  3158. u64 src_blkno;
  3159. u64 last_cluster_blkno = bucket_blkno(first) +
  3160. ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
  3161. BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
  3162. BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
  3163. mlog(0, "move half of xattrs in cluster %llu to %llu\n",
  3164. (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
  3165. ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
  3166. last_cluster_blkno, new_blkno,
  3167. to_move, first_hash);
  3168. if (ret) {
  3169. mlog_errno(ret);
  3170. goto out;
  3171. }
  3172. /* This is the first bucket that got moved */
  3173. src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
  3174. /*
  3175. * If the target bucket was part of the moved buckets, we need to
  3176. * update first and target.
  3177. */
  3178. if (bucket_blkno(target) >= src_blkno) {
  3179. /* Find the block for the new target bucket */
  3180. src_blkno = new_blkno +
  3181. (bucket_blkno(target) - src_blkno);
  3182. ocfs2_xattr_bucket_relse(first);
  3183. ocfs2_xattr_bucket_relse(target);
  3184. /*
  3185. * These shouldn't fail - the buffers are in the
  3186. * journal from ocfs2_cp_xattr_bucket().
  3187. */
  3188. ret = ocfs2_read_xattr_bucket(first, new_blkno);
  3189. if (ret) {
  3190. mlog_errno(ret);
  3191. goto out;
  3192. }
  3193. ret = ocfs2_read_xattr_bucket(target, src_blkno);
  3194. if (ret)
  3195. mlog_errno(ret);
  3196. }
  3197. out:
  3198. return ret;
  3199. }
  3200. /*
  3201. * Find the suitable pos when we divide a bucket into 2.
  3202. * We have to make sure the xattrs with the same hash value exist
  3203. * in the same bucket.
  3204. *
  3205. * If this ocfs2_xattr_header covers more than one hash value, find a
  3206. * place where the hash value changes. Try to find the most even split.
  3207. * The most common case is that all entries have different hash values,
  3208. * and the first check we make will find a place to split.
  3209. */
  3210. static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
  3211. {
  3212. struct ocfs2_xattr_entry *entries = xh->xh_entries;
  3213. int count = le16_to_cpu(xh->xh_count);
  3214. int delta, middle = count / 2;
  3215. /*
  3216. * We start at the middle. Each step gets farther away in both
  3217. * directions. We therefore hit the change in hash value
  3218. * nearest to the middle. Note that this loop does not execute for
  3219. * count < 2.
  3220. */
  3221. for (delta = 0; delta < middle; delta++) {
  3222. /* Let's check delta earlier than middle */
  3223. if (cmp_xe(&entries[middle - delta - 1],
  3224. &entries[middle - delta]))
  3225. return middle - delta;
  3226. /* For even counts, don't walk off the end */
  3227. if ((middle + delta + 1) == count)
  3228. continue;
  3229. /* Now try delta past middle */
  3230. if (cmp_xe(&entries[middle + delta],
  3231. &entries[middle + delta + 1]))
  3232. return middle + delta + 1;
  3233. }
  3234. /* Every entry had the same hash */
  3235. return count;
  3236. }
  3237. /*
  3238. * Move some xattrs in old bucket(blk) to new bucket(new_blk).
  3239. * first_hash will record the 1st hash of the new bucket.
  3240. *
  3241. * Normally half of the xattrs will be moved. But we have to make
  3242. * sure that the xattrs with the same hash value are stored in the
  3243. * same bucket. If all the xattrs in this bucket have the same hash
  3244. * value, the new bucket will be initialized as an empty one and the
  3245. * first_hash will be initialized as (hash_value+1).
  3246. */
  3247. static int ocfs2_divide_xattr_bucket(struct inode *inode,
  3248. handle_t *handle,
  3249. u64 blk,
  3250. u64 new_blk,
  3251. u32 *first_hash,
  3252. int new_bucket_head)
  3253. {
  3254. int ret, i;
  3255. int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
  3256. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  3257. struct ocfs2_xattr_header *xh;
  3258. struct ocfs2_xattr_entry *xe;
  3259. int blocksize = inode->i_sb->s_blocksize;
  3260. mlog(0, "move some of xattrs from bucket %llu to %llu\n",
  3261. (unsigned long long)blk, (unsigned long long)new_blk);
  3262. s_bucket = ocfs2_xattr_bucket_new(inode);
  3263. t_bucket = ocfs2_xattr_bucket_new(inode);
  3264. if (!s_bucket || !t_bucket) {
  3265. ret = -ENOMEM;
  3266. mlog_errno(ret);
  3267. goto out;
  3268. }
  3269. ret = ocfs2_read_xattr_bucket(s_bucket, blk);
  3270. if (ret) {
  3271. mlog_errno(ret);
  3272. goto out;
  3273. }
  3274. ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
  3275. OCFS2_JOURNAL_ACCESS_WRITE);
  3276. if (ret) {
  3277. mlog_errno(ret);
  3278. goto out;
  3279. }
  3280. /*
  3281. * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
  3282. * there's no need to read it.
  3283. */
  3284. ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
  3285. if (ret) {
  3286. mlog_errno(ret);
  3287. goto out;
  3288. }
  3289. /*
  3290. * Hey, if we're overwriting t_bucket, what difference does
  3291. * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
  3292. * same part of ocfs2_cp_xattr_bucket().
  3293. */
  3294. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  3295. new_bucket_head ?
  3296. OCFS2_JOURNAL_ACCESS_CREATE :
  3297. OCFS2_JOURNAL_ACCESS_WRITE);
  3298. if (ret) {
  3299. mlog_errno(ret);
  3300. goto out;
  3301. }
  3302. xh = bucket_xh(s_bucket);
  3303. count = le16_to_cpu(xh->xh_count);
  3304. start = ocfs2_xattr_find_divide_pos(xh);
  3305. if (start == count) {
  3306. xe = &xh->xh_entries[start-1];
  3307. /*
  3308. * initialized a new empty bucket here.
  3309. * The hash value is set as one larger than
  3310. * that of the last entry in the previous bucket.
  3311. */
  3312. for (i = 0; i < t_bucket->bu_blocks; i++)
  3313. memset(bucket_block(t_bucket, i), 0, blocksize);
  3314. xh = bucket_xh(t_bucket);
  3315. xh->xh_free_start = cpu_to_le16(blocksize);
  3316. xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
  3317. le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
  3318. goto set_num_buckets;
  3319. }
  3320. /* copy the whole bucket to the new first. */
  3321. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  3322. /* update the new bucket. */
  3323. xh = bucket_xh(t_bucket);
  3324. /*
  3325. * Calculate the total name/value len and xh_free_start for
  3326. * the old bucket first.
  3327. */
  3328. name_offset = OCFS2_XATTR_BUCKET_SIZE;
  3329. name_value_len = 0;
  3330. for (i = 0; i < start; i++) {
  3331. xe = &xh->xh_entries[i];
  3332. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  3333. if (ocfs2_xattr_is_local(xe))
  3334. xe_len +=
  3335. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3336. else
  3337. xe_len += OCFS2_XATTR_ROOT_SIZE;
  3338. name_value_len += xe_len;
  3339. if (le16_to_cpu(xe->xe_name_offset) < name_offset)
  3340. name_offset = le16_to_cpu(xe->xe_name_offset);
  3341. }
  3342. /*
  3343. * Now begin the modification to the new bucket.
  3344. *
  3345. * In the new bucket, We just move the xattr entry to the beginning
  3346. * and don't touch the name/value. So there will be some holes in the
  3347. * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
  3348. * called.
  3349. */
  3350. xe = &xh->xh_entries[start];
  3351. len = sizeof(struct ocfs2_xattr_entry) * (count - start);
  3352. mlog(0, "mv xattr entry len %d from %d to %d\n", len,
  3353. (int)((char *)xe - (char *)xh),
  3354. (int)((char *)xh->xh_entries - (char *)xh));
  3355. memmove((char *)xh->xh_entries, (char *)xe, len);
  3356. xe = &xh->xh_entries[count - start];
  3357. len = sizeof(struct ocfs2_xattr_entry) * start;
  3358. memset((char *)xe, 0, len);
  3359. le16_add_cpu(&xh->xh_count, -start);
  3360. le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
  3361. /* Calculate xh_free_start for the new bucket. */
  3362. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  3363. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  3364. xe = &xh->xh_entries[i];
  3365. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  3366. if (ocfs2_xattr_is_local(xe))
  3367. xe_len +=
  3368. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3369. else
  3370. xe_len += OCFS2_XATTR_ROOT_SIZE;
  3371. if (le16_to_cpu(xe->xe_name_offset) <
  3372. le16_to_cpu(xh->xh_free_start))
  3373. xh->xh_free_start = xe->xe_name_offset;
  3374. }
  3375. set_num_buckets:
  3376. /* set xh->xh_num_buckets for the new xh. */
  3377. if (new_bucket_head)
  3378. xh->xh_num_buckets = cpu_to_le16(1);
  3379. else
  3380. xh->xh_num_buckets = 0;
  3381. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  3382. /* store the first_hash of the new bucket. */
  3383. if (first_hash)
  3384. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3385. /*
  3386. * Now only update the 1st block of the old bucket. If we
  3387. * just added a new empty bucket, there is no need to modify
  3388. * it.
  3389. */
  3390. if (start == count)
  3391. goto out;
  3392. xh = bucket_xh(s_bucket);
  3393. memset(&xh->xh_entries[start], 0,
  3394. sizeof(struct ocfs2_xattr_entry) * (count - start));
  3395. xh->xh_count = cpu_to_le16(start);
  3396. xh->xh_free_start = cpu_to_le16(name_offset);
  3397. xh->xh_name_value_len = cpu_to_le16(name_value_len);
  3398. ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
  3399. out:
  3400. ocfs2_xattr_bucket_free(s_bucket);
  3401. ocfs2_xattr_bucket_free(t_bucket);
  3402. return ret;
  3403. }
  3404. /*
  3405. * Copy xattr from one bucket to another bucket.
  3406. *
  3407. * The caller must make sure that the journal transaction
  3408. * has enough space for journaling.
  3409. */
  3410. static int ocfs2_cp_xattr_bucket(struct inode *inode,
  3411. handle_t *handle,
  3412. u64 s_blkno,
  3413. u64 t_blkno,
  3414. int t_is_new)
  3415. {
  3416. int ret;
  3417. struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
  3418. BUG_ON(s_blkno == t_blkno);
  3419. mlog(0, "cp bucket %llu to %llu, target is %d\n",
  3420. (unsigned long long)s_blkno, (unsigned long long)t_blkno,
  3421. t_is_new);
  3422. s_bucket = ocfs2_xattr_bucket_new(inode);
  3423. t_bucket = ocfs2_xattr_bucket_new(inode);
  3424. if (!s_bucket || !t_bucket) {
  3425. ret = -ENOMEM;
  3426. mlog_errno(ret);
  3427. goto out;
  3428. }
  3429. ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
  3430. if (ret)
  3431. goto out;
  3432. /*
  3433. * Even if !t_is_new, we're overwriting t_bucket. Thus,
  3434. * there's no need to read it.
  3435. */
  3436. ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
  3437. if (ret)
  3438. goto out;
  3439. /*
  3440. * Hey, if we're overwriting t_bucket, what difference does
  3441. * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
  3442. * cluster to fill, we came here from
  3443. * ocfs2_mv_xattr_buckets(), and it is really new -
  3444. * ACCESS_CREATE is required. But we also might have moved data
  3445. * out of t_bucket before extending back into it.
  3446. * ocfs2_add_new_xattr_bucket() can do this - its call to
  3447. * ocfs2_add_new_xattr_cluster() may have created a new extent
  3448. * and copied out the end of the old extent. Then it re-extends
  3449. * the old extent back to create space for new xattrs. That's
  3450. * how we get here, and the bucket isn't really new.
  3451. */
  3452. ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
  3453. t_is_new ?
  3454. OCFS2_JOURNAL_ACCESS_CREATE :
  3455. OCFS2_JOURNAL_ACCESS_WRITE);
  3456. if (ret)
  3457. goto out;
  3458. ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
  3459. ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
  3460. out:
  3461. ocfs2_xattr_bucket_free(t_bucket);
  3462. ocfs2_xattr_bucket_free(s_bucket);
  3463. return ret;
  3464. }
  3465. /*
  3466. * src_blk points to the start of an existing extent. last_blk points to
  3467. * last cluster in that extent. to_blk points to a newly allocated
  3468. * extent. We copy the buckets from the cluster at last_blk to the new
  3469. * extent. If start_bucket is non-zero, we skip that many buckets before
  3470. * we start copying. The new extent's xh_num_buckets gets set to the
  3471. * number of buckets we copied. The old extent's xh_num_buckets shrinks
  3472. * by the same amount.
  3473. */
  3474. static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
  3475. u64 src_blk, u64 last_blk, u64 to_blk,
  3476. unsigned int start_bucket,
  3477. u32 *first_hash)
  3478. {
  3479. int i, ret, credits;
  3480. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3481. int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3482. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  3483. struct ocfs2_xattr_bucket *old_first, *new_first;
  3484. mlog(0, "mv xattrs from cluster %llu to %llu\n",
  3485. (unsigned long long)last_blk, (unsigned long long)to_blk);
  3486. BUG_ON(start_bucket >= num_buckets);
  3487. if (start_bucket) {
  3488. num_buckets -= start_bucket;
  3489. last_blk += (start_bucket * blks_per_bucket);
  3490. }
  3491. /* The first bucket of the original extent */
  3492. old_first = ocfs2_xattr_bucket_new(inode);
  3493. /* The first bucket of the new extent */
  3494. new_first = ocfs2_xattr_bucket_new(inode);
  3495. if (!old_first || !new_first) {
  3496. ret = -ENOMEM;
  3497. mlog_errno(ret);
  3498. goto out;
  3499. }
  3500. ret = ocfs2_read_xattr_bucket(old_first, src_blk);
  3501. if (ret) {
  3502. mlog_errno(ret);
  3503. goto out;
  3504. }
  3505. /*
  3506. * We need to update the first bucket of the old extent and all
  3507. * the buckets going to the new extent.
  3508. */
  3509. credits = ((num_buckets + 1) * blks_per_bucket) +
  3510. handle->h_buffer_credits;
  3511. ret = ocfs2_extend_trans(handle, credits);
  3512. if (ret) {
  3513. mlog_errno(ret);
  3514. goto out;
  3515. }
  3516. ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
  3517. OCFS2_JOURNAL_ACCESS_WRITE);
  3518. if (ret) {
  3519. mlog_errno(ret);
  3520. goto out;
  3521. }
  3522. for (i = 0; i < num_buckets; i++) {
  3523. ret = ocfs2_cp_xattr_bucket(inode, handle,
  3524. last_blk + (i * blks_per_bucket),
  3525. to_blk + (i * blks_per_bucket),
  3526. 1);
  3527. if (ret) {
  3528. mlog_errno(ret);
  3529. goto out;
  3530. }
  3531. }
  3532. /*
  3533. * Get the new bucket ready before we dirty anything
  3534. * (This actually shouldn't fail, because we already dirtied
  3535. * it once in ocfs2_cp_xattr_bucket()).
  3536. */
  3537. ret = ocfs2_read_xattr_bucket(new_first, to_blk);
  3538. if (ret) {
  3539. mlog_errno(ret);
  3540. goto out;
  3541. }
  3542. ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
  3543. OCFS2_JOURNAL_ACCESS_WRITE);
  3544. if (ret) {
  3545. mlog_errno(ret);
  3546. goto out;
  3547. }
  3548. /* Now update the headers */
  3549. le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
  3550. ocfs2_xattr_bucket_journal_dirty(handle, old_first);
  3551. bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
  3552. ocfs2_xattr_bucket_journal_dirty(handle, new_first);
  3553. if (first_hash)
  3554. *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
  3555. out:
  3556. ocfs2_xattr_bucket_free(new_first);
  3557. ocfs2_xattr_bucket_free(old_first);
  3558. return ret;
  3559. }
  3560. /*
  3561. * Move some xattrs in this cluster to the new cluster.
  3562. * This function should only be called when bucket size == cluster size.
  3563. * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  3564. */
  3565. static int ocfs2_divide_xattr_cluster(struct inode *inode,
  3566. handle_t *handle,
  3567. u64 prev_blk,
  3568. u64 new_blk,
  3569. u32 *first_hash)
  3570. {
  3571. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3572. int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
  3573. BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
  3574. ret = ocfs2_extend_trans(handle, credits);
  3575. if (ret) {
  3576. mlog_errno(ret);
  3577. return ret;
  3578. }
  3579. /* Move half of the xattr in start_blk to the next bucket. */
  3580. return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
  3581. new_blk, first_hash, 1);
  3582. }
  3583. /*
  3584. * Move some xattrs from the old cluster to the new one since they are not
  3585. * contiguous in ocfs2 xattr tree.
  3586. *
  3587. * new_blk starts a new separate cluster, and we will move some xattrs from
  3588. * prev_blk to it. v_start will be set as the first name hash value in this
  3589. * new cluster so that it can be used as e_cpos during tree insertion and
  3590. * don't collide with our original b-tree operations. first_bh and header_bh
  3591. * will also be updated since they will be used in ocfs2_extend_xattr_bucket
  3592. * to extend the insert bucket.
  3593. *
  3594. * The problem is how much xattr should we move to the new one and when should
  3595. * we update first_bh and header_bh?
  3596. * 1. If cluster size > bucket size, that means the previous cluster has more
  3597. * than 1 bucket, so just move half nums of bucket into the new cluster and
  3598. * update the first_bh and header_bh if the insert bucket has been moved
  3599. * to the new cluster.
  3600. * 2. If cluster_size == bucket_size:
  3601. * a) If the previous extent rec has more than one cluster and the insert
  3602. * place isn't in the last cluster, copy the entire last cluster to the
  3603. * new one. This time, we don't need to upate the first_bh and header_bh
  3604. * since they will not be moved into the new cluster.
  3605. * b) Otherwise, move the bottom half of the xattrs in the last cluster into
  3606. * the new one. And we set the extend flag to zero if the insert place is
  3607. * moved into the new allocated cluster since no extend is needed.
  3608. */
  3609. static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
  3610. handle_t *handle,
  3611. struct ocfs2_xattr_bucket *first,
  3612. struct ocfs2_xattr_bucket *target,
  3613. u64 new_blk,
  3614. u32 prev_clusters,
  3615. u32 *v_start,
  3616. int *extend)
  3617. {
  3618. int ret;
  3619. mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
  3620. (unsigned long long)bucket_blkno(first), prev_clusters,
  3621. (unsigned long long)new_blk);
  3622. if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
  3623. ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
  3624. handle,
  3625. first, target,
  3626. new_blk,
  3627. prev_clusters,
  3628. v_start);
  3629. if (ret)
  3630. mlog_errno(ret);
  3631. } else {
  3632. /* The start of the last cluster in the first extent */
  3633. u64 last_blk = bucket_blkno(first) +
  3634. ((prev_clusters - 1) *
  3635. ocfs2_clusters_to_blocks(inode->i_sb, 1));
  3636. if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
  3637. ret = ocfs2_mv_xattr_buckets(inode, handle,
  3638. bucket_blkno(first),
  3639. last_blk, new_blk, 0,
  3640. v_start);
  3641. if (ret)
  3642. mlog_errno(ret);
  3643. } else {
  3644. ret = ocfs2_divide_xattr_cluster(inode, handle,
  3645. last_blk, new_blk,
  3646. v_start);
  3647. if (ret)
  3648. mlog_errno(ret);
  3649. if ((bucket_blkno(target) == last_blk) && extend)
  3650. *extend = 0;
  3651. }
  3652. }
  3653. return ret;
  3654. }
  3655. /*
  3656. * Add a new cluster for xattr storage.
  3657. *
  3658. * If the new cluster is contiguous with the previous one, it will be
  3659. * appended to the same extent record, and num_clusters will be updated.
  3660. * If not, we will insert a new extent for it and move some xattrs in
  3661. * the last cluster into the new allocated one.
  3662. * We also need to limit the maximum size of a btree leaf, otherwise we'll
  3663. * lose the benefits of hashing because we'll have to search large leaves.
  3664. * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
  3665. * if it's bigger).
  3666. *
  3667. * first_bh is the first block of the previous extent rec and header_bh
  3668. * indicates the bucket we will insert the new xattrs. They will be updated
  3669. * when the header_bh is moved into the new cluster.
  3670. */
  3671. static int ocfs2_add_new_xattr_cluster(struct inode *inode,
  3672. struct buffer_head *root_bh,
  3673. struct ocfs2_xattr_bucket *first,
  3674. struct ocfs2_xattr_bucket *target,
  3675. u32 *num_clusters,
  3676. u32 prev_cpos,
  3677. int *extend,
  3678. struct ocfs2_xattr_set_ctxt *ctxt)
  3679. {
  3680. int ret;
  3681. u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3682. u32 prev_clusters = *num_clusters;
  3683. u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
  3684. u64 block;
  3685. handle_t *handle = ctxt->handle;
  3686. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3687. struct ocfs2_extent_tree et;
  3688. mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
  3689. "previous xattr blkno = %llu\n",
  3690. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3691. prev_cpos, (unsigned long long)bucket_blkno(first));
  3692. ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
  3693. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  3694. OCFS2_JOURNAL_ACCESS_WRITE);
  3695. if (ret < 0) {
  3696. mlog_errno(ret);
  3697. goto leave;
  3698. }
  3699. ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
  3700. clusters_to_add, &bit_off, &num_bits);
  3701. if (ret < 0) {
  3702. if (ret != -ENOSPC)
  3703. mlog_errno(ret);
  3704. goto leave;
  3705. }
  3706. BUG_ON(num_bits > clusters_to_add);
  3707. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  3708. mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
  3709. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3710. if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
  3711. (prev_clusters + num_bits) << osb->s_clustersize_bits <=
  3712. OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
  3713. /*
  3714. * If this cluster is contiguous with the old one and
  3715. * adding this new cluster, we don't surpass the limit of
  3716. * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
  3717. * initialized and used like other buckets in the previous
  3718. * cluster.
  3719. * So add it as a contiguous one. The caller will handle
  3720. * its init process.
  3721. */
  3722. v_start = prev_cpos + prev_clusters;
  3723. *num_clusters = prev_clusters + num_bits;
  3724. mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
  3725. num_bits);
  3726. } else {
  3727. ret = ocfs2_adjust_xattr_cross_cluster(inode,
  3728. handle,
  3729. first,
  3730. target,
  3731. block,
  3732. prev_clusters,
  3733. &v_start,
  3734. extend);
  3735. if (ret) {
  3736. mlog_errno(ret);
  3737. goto leave;
  3738. }
  3739. }
  3740. mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
  3741. num_bits, (unsigned long long)block, v_start);
  3742. ret = ocfs2_insert_extent(handle, &et, v_start, block,
  3743. num_bits, 0, ctxt->meta_ac);
  3744. if (ret < 0) {
  3745. mlog_errno(ret);
  3746. goto leave;
  3747. }
  3748. ret = ocfs2_journal_dirty(handle, root_bh);
  3749. if (ret < 0)
  3750. mlog_errno(ret);
  3751. leave:
  3752. return ret;
  3753. }
  3754. /*
  3755. * We are given an extent. 'first' is the bucket at the very front of
  3756. * the extent. The extent has space for an additional bucket past
  3757. * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
  3758. * of the target bucket. We wish to shift every bucket past the target
  3759. * down one, filling in that additional space. When we get back to the
  3760. * target, we split the target between itself and the now-empty bucket
  3761. * at target+1 (aka, target_blkno + blks_per_bucket).
  3762. */
  3763. static int ocfs2_extend_xattr_bucket(struct inode *inode,
  3764. handle_t *handle,
  3765. struct ocfs2_xattr_bucket *first,
  3766. u64 target_blk,
  3767. u32 num_clusters)
  3768. {
  3769. int ret, credits;
  3770. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3771. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3772. u64 end_blk;
  3773. u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
  3774. mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
  3775. "from %llu, len = %u\n", (unsigned long long)target_blk,
  3776. (unsigned long long)bucket_blkno(first), num_clusters);
  3777. /* The extent must have room for an additional bucket */
  3778. BUG_ON(new_bucket >=
  3779. (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
  3780. /* end_blk points to the last existing bucket */
  3781. end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
  3782. /*
  3783. * end_blk is the start of the last existing bucket.
  3784. * Thus, (end_blk - target_blk) covers the target bucket and
  3785. * every bucket after it up to, but not including, the last
  3786. * existing bucket. Then we add the last existing bucket, the
  3787. * new bucket, and the first bucket (3 * blk_per_bucket).
  3788. */
  3789. credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
  3790. handle->h_buffer_credits;
  3791. ret = ocfs2_extend_trans(handle, credits);
  3792. if (ret) {
  3793. mlog_errno(ret);
  3794. goto out;
  3795. }
  3796. ret = ocfs2_xattr_bucket_journal_access(handle, first,
  3797. OCFS2_JOURNAL_ACCESS_WRITE);
  3798. if (ret) {
  3799. mlog_errno(ret);
  3800. goto out;
  3801. }
  3802. while (end_blk != target_blk) {
  3803. ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
  3804. end_blk + blk_per_bucket, 0);
  3805. if (ret)
  3806. goto out;
  3807. end_blk -= blk_per_bucket;
  3808. }
  3809. /* Move half of the xattr in target_blkno to the next bucket. */
  3810. ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
  3811. target_blk + blk_per_bucket, NULL, 0);
  3812. le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
  3813. ocfs2_xattr_bucket_journal_dirty(handle, first);
  3814. out:
  3815. return ret;
  3816. }
  3817. /*
  3818. * Add new xattr bucket in an extent record and adjust the buckets
  3819. * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
  3820. * bucket we want to insert into.
  3821. *
  3822. * In the easy case, we will move all the buckets after target down by
  3823. * one. Half of target's xattrs will be moved to the next bucket.
  3824. *
  3825. * If current cluster is full, we'll allocate a new one. This may not
  3826. * be contiguous. The underlying calls will make sure that there is
  3827. * space for the insert, shifting buckets around if necessary.
  3828. * 'target' may be moved by those calls.
  3829. */
  3830. static int ocfs2_add_new_xattr_bucket(struct inode *inode,
  3831. struct buffer_head *xb_bh,
  3832. struct ocfs2_xattr_bucket *target,
  3833. struct ocfs2_xattr_set_ctxt *ctxt)
  3834. {
  3835. struct ocfs2_xattr_block *xb =
  3836. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3837. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3838. struct ocfs2_extent_list *el = &xb_root->xt_list;
  3839. u32 name_hash =
  3840. le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
  3841. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3842. int ret, num_buckets, extend = 1;
  3843. u64 p_blkno;
  3844. u32 e_cpos, num_clusters;
  3845. /* The bucket at the front of the extent */
  3846. struct ocfs2_xattr_bucket *first;
  3847. mlog(0, "Add new xattr bucket starting from %llu\n",
  3848. (unsigned long long)bucket_blkno(target));
  3849. /* The first bucket of the original extent */
  3850. first = ocfs2_xattr_bucket_new(inode);
  3851. if (!first) {
  3852. ret = -ENOMEM;
  3853. mlog_errno(ret);
  3854. goto out;
  3855. }
  3856. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
  3857. &num_clusters, el);
  3858. if (ret) {
  3859. mlog_errno(ret);
  3860. goto out;
  3861. }
  3862. ret = ocfs2_read_xattr_bucket(first, p_blkno);
  3863. if (ret) {
  3864. mlog_errno(ret);
  3865. goto out;
  3866. }
  3867. num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
  3868. if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
  3869. /*
  3870. * This can move first+target if the target bucket moves
  3871. * to the new extent.
  3872. */
  3873. ret = ocfs2_add_new_xattr_cluster(inode,
  3874. xb_bh,
  3875. first,
  3876. target,
  3877. &num_clusters,
  3878. e_cpos,
  3879. &extend,
  3880. ctxt);
  3881. if (ret) {
  3882. mlog_errno(ret);
  3883. goto out;
  3884. }
  3885. }
  3886. if (extend) {
  3887. ret = ocfs2_extend_xattr_bucket(inode,
  3888. ctxt->handle,
  3889. first,
  3890. bucket_blkno(target),
  3891. num_clusters);
  3892. if (ret)
  3893. mlog_errno(ret);
  3894. }
  3895. out:
  3896. ocfs2_xattr_bucket_free(first);
  3897. return ret;
  3898. }
  3899. static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
  3900. struct ocfs2_xattr_bucket *bucket,
  3901. int offs)
  3902. {
  3903. int block_off = offs >> inode->i_sb->s_blocksize_bits;
  3904. offs = offs % inode->i_sb->s_blocksize;
  3905. return bucket_block(bucket, block_off) + offs;
  3906. }
  3907. /*
  3908. * Handle the normal xattr set, including replace, delete and new.
  3909. *
  3910. * Note: "local" indicates the real data's locality. So we can't
  3911. * just its bucket locality by its length.
  3912. */
  3913. static void ocfs2_xattr_set_entry_normal(struct inode *inode,
  3914. struct ocfs2_xattr_info *xi,
  3915. struct ocfs2_xattr_search *xs,
  3916. u32 name_hash,
  3917. int local)
  3918. {
  3919. struct ocfs2_xattr_entry *last, *xe;
  3920. int name_len = strlen(xi->name);
  3921. struct ocfs2_xattr_header *xh = xs->header;
  3922. u16 count = le16_to_cpu(xh->xh_count), start;
  3923. size_t blocksize = inode->i_sb->s_blocksize;
  3924. char *val;
  3925. size_t offs, size, new_size;
  3926. last = &xh->xh_entries[count];
  3927. if (!xs->not_found) {
  3928. xe = xs->here;
  3929. offs = le16_to_cpu(xe->xe_name_offset);
  3930. if (ocfs2_xattr_is_local(xe))
  3931. size = OCFS2_XATTR_SIZE(name_len) +
  3932. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3933. else
  3934. size = OCFS2_XATTR_SIZE(name_len) +
  3935. OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  3936. /*
  3937. * If the new value will be stored outside, xi->value has been
  3938. * initalized as an empty ocfs2_xattr_value_root, and the same
  3939. * goes with xi->value_len, so we can set new_size safely here.
  3940. * See ocfs2_xattr_set_in_bucket.
  3941. */
  3942. new_size = OCFS2_XATTR_SIZE(name_len) +
  3943. OCFS2_XATTR_SIZE(xi->value_len);
  3944. le16_add_cpu(&xh->xh_name_value_len, -size);
  3945. if (xi->value) {
  3946. if (new_size > size)
  3947. goto set_new_name_value;
  3948. /* Now replace the old value with new one. */
  3949. if (local)
  3950. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3951. else
  3952. xe->xe_value_size = 0;
  3953. val = ocfs2_xattr_bucket_get_val(inode,
  3954. xs->bucket, offs);
  3955. memset(val + OCFS2_XATTR_SIZE(name_len), 0,
  3956. size - OCFS2_XATTR_SIZE(name_len));
  3957. if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
  3958. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  3959. xi->value, xi->value_len);
  3960. le16_add_cpu(&xh->xh_name_value_len, new_size);
  3961. ocfs2_xattr_set_local(xe, local);
  3962. return;
  3963. } else {
  3964. /*
  3965. * Remove the old entry if there is more than one.
  3966. * We don't remove the last entry so that we can
  3967. * use it to indicate the hash value of the empty
  3968. * bucket.
  3969. */
  3970. last -= 1;
  3971. le16_add_cpu(&xh->xh_count, -1);
  3972. if (xh->xh_count) {
  3973. memmove(xe, xe + 1,
  3974. (void *)last - (void *)xe);
  3975. memset(last, 0,
  3976. sizeof(struct ocfs2_xattr_entry));
  3977. } else
  3978. xh->xh_free_start =
  3979. cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  3980. return;
  3981. }
  3982. } else {
  3983. /* find a new entry for insert. */
  3984. int low = 0, high = count - 1, tmp;
  3985. struct ocfs2_xattr_entry *tmp_xe;
  3986. while (low <= high && count) {
  3987. tmp = (low + high) / 2;
  3988. tmp_xe = &xh->xh_entries[tmp];
  3989. if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
  3990. low = tmp + 1;
  3991. else if (name_hash <
  3992. le32_to_cpu(tmp_xe->xe_name_hash))
  3993. high = tmp - 1;
  3994. else {
  3995. low = tmp;
  3996. break;
  3997. }
  3998. }
  3999. xe = &xh->xh_entries[low];
  4000. if (low != count)
  4001. memmove(xe + 1, xe, (void *)last - (void *)xe);
  4002. le16_add_cpu(&xh->xh_count, 1);
  4003. memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
  4004. xe->xe_name_hash = cpu_to_le32(name_hash);
  4005. xe->xe_name_len = name_len;
  4006. ocfs2_xattr_set_type(xe, xi->name_index);
  4007. }
  4008. set_new_name_value:
  4009. /* Insert the new name+value. */
  4010. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
  4011. /*
  4012. * We must make sure that the name/value pair
  4013. * exists in the same block.
  4014. */
  4015. offs = le16_to_cpu(xh->xh_free_start);
  4016. start = offs - size;
  4017. if (start >> inode->i_sb->s_blocksize_bits !=
  4018. (offs - 1) >> inode->i_sb->s_blocksize_bits) {
  4019. offs = offs - offs % blocksize;
  4020. xh->xh_free_start = cpu_to_le16(offs);
  4021. }
  4022. val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
  4023. xe->xe_name_offset = cpu_to_le16(offs - size);
  4024. memset(val, 0, size);
  4025. memcpy(val, xi->name, name_len);
  4026. memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
  4027. xe->xe_value_size = cpu_to_le64(xi->value_len);
  4028. ocfs2_xattr_set_local(xe, local);
  4029. xs->here = xe;
  4030. le16_add_cpu(&xh->xh_free_start, -size);
  4031. le16_add_cpu(&xh->xh_name_value_len, size);
  4032. return;
  4033. }
  4034. /*
  4035. * Set the xattr entry in the specified bucket.
  4036. * The bucket is indicated by xs->bucket and it should have the enough
  4037. * space for the xattr insertion.
  4038. */
  4039. static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
  4040. handle_t *handle,
  4041. struct ocfs2_xattr_info *xi,
  4042. struct ocfs2_xattr_search *xs,
  4043. u32 name_hash,
  4044. int local)
  4045. {
  4046. int ret;
  4047. u64 blkno;
  4048. mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
  4049. (unsigned long)xi->value_len, xi->name_index,
  4050. (unsigned long long)bucket_blkno(xs->bucket));
  4051. if (!xs->bucket->bu_bhs[1]) {
  4052. blkno = bucket_blkno(xs->bucket);
  4053. ocfs2_xattr_bucket_relse(xs->bucket);
  4054. ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
  4055. if (ret) {
  4056. mlog_errno(ret);
  4057. goto out;
  4058. }
  4059. }
  4060. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  4061. OCFS2_JOURNAL_ACCESS_WRITE);
  4062. if (ret < 0) {
  4063. mlog_errno(ret);
  4064. goto out;
  4065. }
  4066. ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
  4067. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  4068. out:
  4069. return ret;
  4070. }
  4071. /*
  4072. * Truncate the specified xe_off entry in xattr bucket.
  4073. * bucket is indicated by header_bh and len is the new length.
  4074. * Both the ocfs2_xattr_value_root and the entry will be updated here.
  4075. *
  4076. * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
  4077. */
  4078. static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
  4079. struct ocfs2_xattr_bucket *bucket,
  4080. int xe_off,
  4081. int len,
  4082. struct ocfs2_xattr_set_ctxt *ctxt)
  4083. {
  4084. int ret, offset;
  4085. u64 value_blk;
  4086. struct ocfs2_xattr_entry *xe;
  4087. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4088. size_t blocksize = inode->i_sb->s_blocksize;
  4089. struct ocfs2_xattr_value_buf vb = {
  4090. .vb_access = ocfs2_journal_access,
  4091. };
  4092. xe = &xh->xh_entries[xe_off];
  4093. BUG_ON(!xe || ocfs2_xattr_is_local(xe));
  4094. offset = le16_to_cpu(xe->xe_name_offset) +
  4095. OCFS2_XATTR_SIZE(xe->xe_name_len);
  4096. value_blk = offset / blocksize;
  4097. /* We don't allow ocfs2_xattr_value to be stored in different block. */
  4098. BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
  4099. vb.vb_bh = bucket->bu_bhs[value_blk];
  4100. BUG_ON(!vb.vb_bh);
  4101. vb.vb_xv = (struct ocfs2_xattr_value_root *)
  4102. (vb.vb_bh->b_data + offset % blocksize);
  4103. /*
  4104. * From here on out we have to dirty the bucket. The generic
  4105. * value calls only modify one of the bucket's bhs, but we need
  4106. * to send the bucket at once. So if they error, they *could* have
  4107. * modified something. We have to assume they did, and dirty
  4108. * the whole bucket. This leaves us in a consistent state.
  4109. */
  4110. mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
  4111. xe_off, (unsigned long long)bucket_blkno(bucket), len);
  4112. ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
  4113. if (ret) {
  4114. mlog_errno(ret);
  4115. goto out;
  4116. }
  4117. ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
  4118. OCFS2_JOURNAL_ACCESS_WRITE);
  4119. if (ret) {
  4120. mlog_errno(ret);
  4121. goto out;
  4122. }
  4123. xe->xe_value_size = cpu_to_le64(len);
  4124. ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
  4125. out:
  4126. return ret;
  4127. }
  4128. static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
  4129. struct ocfs2_xattr_search *xs,
  4130. int len,
  4131. struct ocfs2_xattr_set_ctxt *ctxt)
  4132. {
  4133. int ret, offset;
  4134. struct ocfs2_xattr_entry *xe = xs->here;
  4135. struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
  4136. BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
  4137. offset = xe - xh->xh_entries;
  4138. ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
  4139. offset, len, ctxt);
  4140. if (ret)
  4141. mlog_errno(ret);
  4142. return ret;
  4143. }
  4144. static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
  4145. handle_t *handle,
  4146. struct ocfs2_xattr_search *xs,
  4147. char *val,
  4148. int value_len)
  4149. {
  4150. int ret, offset, block_off;
  4151. struct ocfs2_xattr_value_root *xv;
  4152. struct ocfs2_xattr_entry *xe = xs->here;
  4153. struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
  4154. void *base;
  4155. BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
  4156. ret = ocfs2_xattr_bucket_get_name_value(inode, xh,
  4157. xe - xh->xh_entries,
  4158. &block_off,
  4159. &offset);
  4160. if (ret) {
  4161. mlog_errno(ret);
  4162. goto out;
  4163. }
  4164. base = bucket_block(xs->bucket, block_off);
  4165. xv = (struct ocfs2_xattr_value_root *)(base + offset +
  4166. OCFS2_XATTR_SIZE(xe->xe_name_len));
  4167. ret = __ocfs2_xattr_set_value_outside(inode, handle,
  4168. xv, val, value_len);
  4169. if (ret)
  4170. mlog_errno(ret);
  4171. out:
  4172. return ret;
  4173. }
  4174. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  4175. struct buffer_head *root_bh,
  4176. u64 blkno,
  4177. u32 cpos,
  4178. u32 len)
  4179. {
  4180. int ret;
  4181. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4182. struct inode *tl_inode = osb->osb_tl_inode;
  4183. handle_t *handle;
  4184. struct ocfs2_xattr_block *xb =
  4185. (struct ocfs2_xattr_block *)root_bh->b_data;
  4186. struct ocfs2_alloc_context *meta_ac = NULL;
  4187. struct ocfs2_cached_dealloc_ctxt dealloc;
  4188. struct ocfs2_extent_tree et;
  4189. ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
  4190. ocfs2_init_dealloc_ctxt(&dealloc);
  4191. mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
  4192. cpos, len, (unsigned long long)blkno);
  4193. ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
  4194. len);
  4195. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  4196. if (ret) {
  4197. mlog_errno(ret);
  4198. return ret;
  4199. }
  4200. mutex_lock(&tl_inode->i_mutex);
  4201. if (ocfs2_truncate_log_needs_flush(osb)) {
  4202. ret = __ocfs2_flush_truncate_log(osb);
  4203. if (ret < 0) {
  4204. mlog_errno(ret);
  4205. goto out;
  4206. }
  4207. }
  4208. handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
  4209. if (IS_ERR(handle)) {
  4210. ret = -ENOMEM;
  4211. mlog_errno(ret);
  4212. goto out;
  4213. }
  4214. ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
  4215. OCFS2_JOURNAL_ACCESS_WRITE);
  4216. if (ret) {
  4217. mlog_errno(ret);
  4218. goto out_commit;
  4219. }
  4220. ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
  4221. &dealloc);
  4222. if (ret) {
  4223. mlog_errno(ret);
  4224. goto out_commit;
  4225. }
  4226. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
  4227. ret = ocfs2_journal_dirty(handle, root_bh);
  4228. if (ret) {
  4229. mlog_errno(ret);
  4230. goto out_commit;
  4231. }
  4232. ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
  4233. if (ret)
  4234. mlog_errno(ret);
  4235. out_commit:
  4236. ocfs2_commit_trans(osb, handle);
  4237. out:
  4238. ocfs2_schedule_truncate_log_flush(osb, 1);
  4239. mutex_unlock(&tl_inode->i_mutex);
  4240. if (meta_ac)
  4241. ocfs2_free_alloc_context(meta_ac);
  4242. ocfs2_run_deallocs(osb, &dealloc);
  4243. return ret;
  4244. }
  4245. static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
  4246. handle_t *handle,
  4247. struct ocfs2_xattr_search *xs)
  4248. {
  4249. struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
  4250. struct ocfs2_xattr_entry *last = &xh->xh_entries[
  4251. le16_to_cpu(xh->xh_count) - 1];
  4252. int ret = 0;
  4253. ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
  4254. OCFS2_JOURNAL_ACCESS_WRITE);
  4255. if (ret) {
  4256. mlog_errno(ret);
  4257. return;
  4258. }
  4259. /* Remove the old entry. */
  4260. memmove(xs->here, xs->here + 1,
  4261. (void *)last - (void *)xs->here);
  4262. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  4263. le16_add_cpu(&xh->xh_count, -1);
  4264. ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
  4265. }
  4266. /*
  4267. * Set the xattr name/value in the bucket specified in xs.
  4268. *
  4269. * As the new value in xi may be stored in the bucket or in an outside cluster,
  4270. * we divide the whole process into 3 steps:
  4271. * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
  4272. * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
  4273. * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
  4274. * 4. If the clusters for the new outside value can't be allocated, we need
  4275. * to free the xattr we allocated in set.
  4276. */
  4277. static int ocfs2_xattr_set_in_bucket(struct inode *inode,
  4278. struct ocfs2_xattr_info *xi,
  4279. struct ocfs2_xattr_search *xs,
  4280. struct ocfs2_xattr_set_ctxt *ctxt)
  4281. {
  4282. int ret, local = 1;
  4283. size_t value_len;
  4284. char *val = (char *)xi->value;
  4285. struct ocfs2_xattr_entry *xe = xs->here;
  4286. u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
  4287. strlen(xi->name));
  4288. if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
  4289. /*
  4290. * We need to truncate the xattr storage first.
  4291. *
  4292. * If both the old and new value are stored to
  4293. * outside block, we only need to truncate
  4294. * the storage and then set the value outside.
  4295. *
  4296. * If the new value should be stored within block,
  4297. * we should free all the outside block first and
  4298. * the modification to the xattr block will be done
  4299. * by following steps.
  4300. */
  4301. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  4302. value_len = xi->value_len;
  4303. else
  4304. value_len = 0;
  4305. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  4306. value_len,
  4307. ctxt);
  4308. if (ret)
  4309. goto out;
  4310. if (value_len)
  4311. goto set_value_outside;
  4312. }
  4313. value_len = xi->value_len;
  4314. /* So we have to handle the inside block change now. */
  4315. if (value_len > OCFS2_XATTR_INLINE_SIZE) {
  4316. /*
  4317. * If the new value will be stored outside of block,
  4318. * initalize a new empty value root and insert it first.
  4319. */
  4320. local = 0;
  4321. xi->value = &def_xv;
  4322. xi->value_len = OCFS2_XATTR_ROOT_SIZE;
  4323. }
  4324. ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
  4325. name_hash, local);
  4326. if (ret) {
  4327. mlog_errno(ret);
  4328. goto out;
  4329. }
  4330. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  4331. goto out;
  4332. /* allocate the space now for the outside block storage. */
  4333. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  4334. value_len, ctxt);
  4335. if (ret) {
  4336. mlog_errno(ret);
  4337. if (xs->not_found) {
  4338. /*
  4339. * We can't allocate enough clusters for outside
  4340. * storage and we have allocated xattr already,
  4341. * so need to remove it.
  4342. */
  4343. ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
  4344. }
  4345. goto out;
  4346. }
  4347. set_value_outside:
  4348. ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
  4349. xs, val, value_len);
  4350. out:
  4351. return ret;
  4352. }
  4353. /*
  4354. * check whether the xattr bucket is filled up with the same hash value.
  4355. * If we want to insert the xattr with the same hash, return -ENOSPC.
  4356. * If we want to insert a xattr with different hash value, go ahead
  4357. * and ocfs2_divide_xattr_bucket will handle this.
  4358. */
  4359. static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
  4360. struct ocfs2_xattr_bucket *bucket,
  4361. const char *name)
  4362. {
  4363. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4364. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  4365. if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
  4366. return 0;
  4367. if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
  4368. xh->xh_entries[0].xe_name_hash) {
  4369. mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
  4370. "hash = %u\n",
  4371. (unsigned long long)bucket_blkno(bucket),
  4372. le32_to_cpu(xh->xh_entries[0].xe_name_hash));
  4373. return -ENOSPC;
  4374. }
  4375. return 0;
  4376. }
  4377. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  4378. struct ocfs2_xattr_info *xi,
  4379. struct ocfs2_xattr_search *xs,
  4380. struct ocfs2_xattr_set_ctxt *ctxt)
  4381. {
  4382. struct ocfs2_xattr_header *xh;
  4383. struct ocfs2_xattr_entry *xe;
  4384. u16 count, header_size, xh_free_start;
  4385. int free, max_free, need, old;
  4386. size_t value_size = 0, name_len = strlen(xi->name);
  4387. size_t blocksize = inode->i_sb->s_blocksize;
  4388. int ret, allocation = 0;
  4389. mlog_entry("Set xattr %s in xattr index block\n", xi->name);
  4390. try_again:
  4391. xh = xs->header;
  4392. count = le16_to_cpu(xh->xh_count);
  4393. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4394. header_size = sizeof(struct ocfs2_xattr_header) +
  4395. count * sizeof(struct ocfs2_xattr_entry);
  4396. max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
  4397. le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
  4398. mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
  4399. "of %u which exceed block size\n",
  4400. (unsigned long long)bucket_blkno(xs->bucket),
  4401. header_size);
  4402. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  4403. value_size = OCFS2_XATTR_ROOT_SIZE;
  4404. else if (xi->value)
  4405. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  4406. if (xs->not_found)
  4407. need = sizeof(struct ocfs2_xattr_entry) +
  4408. OCFS2_XATTR_SIZE(name_len) + value_size;
  4409. else {
  4410. need = value_size + OCFS2_XATTR_SIZE(name_len);
  4411. /*
  4412. * We only replace the old value if the new length is smaller
  4413. * than the old one. Otherwise we will allocate new space in the
  4414. * bucket to store it.
  4415. */
  4416. xe = xs->here;
  4417. if (ocfs2_xattr_is_local(xe))
  4418. old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  4419. else
  4420. old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  4421. if (old >= value_size)
  4422. need = 0;
  4423. }
  4424. free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
  4425. /*
  4426. * We need to make sure the new name/value pair
  4427. * can exist in the same block.
  4428. */
  4429. if (xh_free_start % blocksize < need)
  4430. free -= xh_free_start % blocksize;
  4431. mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
  4432. "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
  4433. " %u\n", xs->not_found,
  4434. (unsigned long long)bucket_blkno(xs->bucket),
  4435. free, need, max_free, le16_to_cpu(xh->xh_free_start),
  4436. le16_to_cpu(xh->xh_name_value_len));
  4437. if (free < need ||
  4438. (xs->not_found &&
  4439. count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
  4440. if (need <= max_free &&
  4441. count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  4442. /*
  4443. * We can create the space by defragment. Since only the
  4444. * name/value will be moved, the xe shouldn't be changed
  4445. * in xs.
  4446. */
  4447. ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
  4448. xs->bucket);
  4449. if (ret) {
  4450. mlog_errno(ret);
  4451. goto out;
  4452. }
  4453. xh_free_start = le16_to_cpu(xh->xh_free_start);
  4454. free = xh_free_start - header_size
  4455. - OCFS2_XATTR_HEADER_GAP;
  4456. if (xh_free_start % blocksize < need)
  4457. free -= xh_free_start % blocksize;
  4458. if (free >= need)
  4459. goto xattr_set;
  4460. mlog(0, "Can't get enough space for xattr insert by "
  4461. "defragment. Need %u bytes, but we have %d, so "
  4462. "allocate new bucket for it.\n", need, free);
  4463. }
  4464. /*
  4465. * We have to add new buckets or clusters and one
  4466. * allocation should leave us enough space for insert.
  4467. */
  4468. BUG_ON(allocation);
  4469. /*
  4470. * We do not allow for overlapping ranges between buckets. And
  4471. * the maximum number of collisions we will allow for then is
  4472. * one bucket's worth, so check it here whether we need to
  4473. * add a new bucket for the insert.
  4474. */
  4475. ret = ocfs2_check_xattr_bucket_collision(inode,
  4476. xs->bucket,
  4477. xi->name);
  4478. if (ret) {
  4479. mlog_errno(ret);
  4480. goto out;
  4481. }
  4482. ret = ocfs2_add_new_xattr_bucket(inode,
  4483. xs->xattr_bh,
  4484. xs->bucket,
  4485. ctxt);
  4486. if (ret) {
  4487. mlog_errno(ret);
  4488. goto out;
  4489. }
  4490. /*
  4491. * ocfs2_add_new_xattr_bucket() will have updated
  4492. * xs->bucket if it moved, but it will not have updated
  4493. * any of the other search fields. Thus, we drop it and
  4494. * re-search. Everything should be cached, so it'll be
  4495. * quick.
  4496. */
  4497. ocfs2_xattr_bucket_relse(xs->bucket);
  4498. ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
  4499. xi->name_index,
  4500. xi->name, xs);
  4501. if (ret && ret != -ENODATA)
  4502. goto out;
  4503. xs->not_found = ret;
  4504. allocation = 1;
  4505. goto try_again;
  4506. }
  4507. xattr_set:
  4508. ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
  4509. out:
  4510. mlog_exit(ret);
  4511. return ret;
  4512. }
  4513. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  4514. struct ocfs2_xattr_bucket *bucket,
  4515. void *para)
  4516. {
  4517. int ret = 0;
  4518. struct ocfs2_xattr_header *xh = bucket_xh(bucket);
  4519. u16 i;
  4520. struct ocfs2_xattr_entry *xe;
  4521. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4522. struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
  4523. int credits = ocfs2_remove_extent_credits(osb->sb) +
  4524. ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  4525. ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
  4526. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4527. xe = &xh->xh_entries[i];
  4528. if (ocfs2_xattr_is_local(xe))
  4529. continue;
  4530. ctxt.handle = ocfs2_start_trans(osb, credits);
  4531. if (IS_ERR(ctxt.handle)) {
  4532. ret = PTR_ERR(ctxt.handle);
  4533. mlog_errno(ret);
  4534. break;
  4535. }
  4536. ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
  4537. i, 0, &ctxt);
  4538. ocfs2_commit_trans(osb, ctxt.handle);
  4539. if (ret) {
  4540. mlog_errno(ret);
  4541. break;
  4542. }
  4543. }
  4544. ocfs2_schedule_truncate_log_flush(osb, 1);
  4545. ocfs2_run_deallocs(osb, &ctxt.dealloc);
  4546. return ret;
  4547. }
  4548. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  4549. struct buffer_head *xb_bh)
  4550. {
  4551. struct ocfs2_xattr_block *xb =
  4552. (struct ocfs2_xattr_block *)xb_bh->b_data;
  4553. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  4554. int ret = 0;
  4555. u32 name_hash = UINT_MAX, e_cpos, num_clusters;
  4556. u64 p_blkno;
  4557. if (le16_to_cpu(el->l_next_free_rec) == 0)
  4558. return 0;
  4559. while (name_hash > 0) {
  4560. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  4561. &e_cpos, &num_clusters, el);
  4562. if (ret) {
  4563. mlog_errno(ret);
  4564. goto out;
  4565. }
  4566. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  4567. ocfs2_delete_xattr_in_bucket,
  4568. NULL);
  4569. if (ret) {
  4570. mlog_errno(ret);
  4571. goto out;
  4572. }
  4573. ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
  4574. p_blkno, e_cpos, num_clusters);
  4575. if (ret) {
  4576. mlog_errno(ret);
  4577. break;
  4578. }
  4579. if (e_cpos == 0)
  4580. break;
  4581. name_hash = e_cpos - 1;
  4582. }
  4583. out:
  4584. return ret;
  4585. }
  4586. /*
  4587. * 'security' attributes support
  4588. */
  4589. static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
  4590. size_t list_size, const char *name,
  4591. size_t name_len)
  4592. {
  4593. const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
  4594. const size_t total_len = prefix_len + name_len + 1;
  4595. if (list && total_len <= list_size) {
  4596. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  4597. memcpy(list + prefix_len, name, name_len);
  4598. list[prefix_len + name_len] = '\0';
  4599. }
  4600. return total_len;
  4601. }
  4602. static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
  4603. void *buffer, size_t size)
  4604. {
  4605. if (strcmp(name, "") == 0)
  4606. return -EINVAL;
  4607. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
  4608. buffer, size);
  4609. }
  4610. static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
  4611. const void *value, size_t size, int flags)
  4612. {
  4613. if (strcmp(name, "") == 0)
  4614. return -EINVAL;
  4615. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
  4616. size, flags);
  4617. }
  4618. int ocfs2_init_security_get(struct inode *inode,
  4619. struct inode *dir,
  4620. struct ocfs2_security_xattr_info *si)
  4621. {
  4622. /* check whether ocfs2 support feature xattr */
  4623. if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
  4624. return -EOPNOTSUPP;
  4625. return security_inode_init_security(inode, dir, &si->name, &si->value,
  4626. &si->value_len);
  4627. }
  4628. int ocfs2_init_security_set(handle_t *handle,
  4629. struct inode *inode,
  4630. struct buffer_head *di_bh,
  4631. struct ocfs2_security_xattr_info *si,
  4632. struct ocfs2_alloc_context *xattr_ac,
  4633. struct ocfs2_alloc_context *data_ac)
  4634. {
  4635. return ocfs2_xattr_set_handle(handle, inode, di_bh,
  4636. OCFS2_XATTR_INDEX_SECURITY,
  4637. si->name, si->value, si->value_len, 0,
  4638. xattr_ac, data_ac);
  4639. }
  4640. struct xattr_handler ocfs2_xattr_security_handler = {
  4641. .prefix = XATTR_SECURITY_PREFIX,
  4642. .list = ocfs2_xattr_security_list,
  4643. .get = ocfs2_xattr_security_get,
  4644. .set = ocfs2_xattr_security_set,
  4645. };
  4646. /*
  4647. * 'trusted' attributes support
  4648. */
  4649. static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
  4650. size_t list_size, const char *name,
  4651. size_t name_len)
  4652. {
  4653. const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  4654. const size_t total_len = prefix_len + name_len + 1;
  4655. if (list && total_len <= list_size) {
  4656. memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
  4657. memcpy(list + prefix_len, name, name_len);
  4658. list[prefix_len + name_len] = '\0';
  4659. }
  4660. return total_len;
  4661. }
  4662. static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
  4663. void *buffer, size_t size)
  4664. {
  4665. if (strcmp(name, "") == 0)
  4666. return -EINVAL;
  4667. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
  4668. buffer, size);
  4669. }
  4670. static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
  4671. const void *value, size_t size, int flags)
  4672. {
  4673. if (strcmp(name, "") == 0)
  4674. return -EINVAL;
  4675. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
  4676. size, flags);
  4677. }
  4678. struct xattr_handler ocfs2_xattr_trusted_handler = {
  4679. .prefix = XATTR_TRUSTED_PREFIX,
  4680. .list = ocfs2_xattr_trusted_list,
  4681. .get = ocfs2_xattr_trusted_get,
  4682. .set = ocfs2_xattr_trusted_set,
  4683. };
  4684. /*
  4685. * 'user' attributes support
  4686. */
  4687. static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
  4688. size_t list_size, const char *name,
  4689. size_t name_len)
  4690. {
  4691. const size_t prefix_len = XATTR_USER_PREFIX_LEN;
  4692. const size_t total_len = prefix_len + name_len + 1;
  4693. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4694. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4695. return 0;
  4696. if (list && total_len <= list_size) {
  4697. memcpy(list, XATTR_USER_PREFIX, prefix_len);
  4698. memcpy(list + prefix_len, name, name_len);
  4699. list[prefix_len + name_len] = '\0';
  4700. }
  4701. return total_len;
  4702. }
  4703. static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
  4704. void *buffer, size_t size)
  4705. {
  4706. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4707. if (strcmp(name, "") == 0)
  4708. return -EINVAL;
  4709. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4710. return -EOPNOTSUPP;
  4711. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
  4712. buffer, size);
  4713. }
  4714. static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
  4715. const void *value, size_t size, int flags)
  4716. {
  4717. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4718. if (strcmp(name, "") == 0)
  4719. return -EINVAL;
  4720. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4721. return -EOPNOTSUPP;
  4722. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
  4723. size, flags);
  4724. }
  4725. struct xattr_handler ocfs2_xattr_user_handler = {
  4726. .prefix = XATTR_USER_PREFIX,
  4727. .list = ocfs2_xattr_user_list,
  4728. .get = ocfs2_xattr_user_get,
  4729. .set = ocfs2_xattr_user_set,
  4730. };