xattr.c 141 KB

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