xattr.c 139 KB

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