xattr.c 132 KB

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