xattr.c 121 KB

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