xattr.c 139 KB

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