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