xattr.c 136 KB

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