xattr.c 122 KB

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