xattr.c 141 KB

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