xattr.c 140 KB

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