xattr.c 139 KB

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