xattr.c 121 KB

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