xattr.c 120 KB

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