xattr.c 139 KB

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