xattr.c 141 KB

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