xattr.c 140 KB

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