xattr.c 122 KB

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