xattr.c 138 KB

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