xattr.c 121 KB

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