xattr.c 136 KB

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