xattr.c 122 KB

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